Add BatchQueue implementation & minor updates

This commit is contained in:
arnaucube
2020-08-13 18:25:50 +02:00
parent 9df30affc6
commit a1c339c918
6 changed files with 123 additions and 12 deletions

64
coordinator/batch.go Normal file
View File

@@ -0,0 +1,64 @@
package coordinator
import (
"github.com/hermeznetwork/hermez-node/common"
)
// BatchInfo contans the Batch information
type BatchInfo struct {
batchNum uint64
serverProof *ServerProofInfo
zkInputs *common.ZKInputs
L1UserTxsExtra []common.L1Tx
L1OperatorTxs []common.L1Tx
L2Txs []common.PoolL2Tx
// FeesInfo
}
// NewBatchInfo creates a new BatchInfo with the given batchNum &
// ServerProofInfo
func NewBatchInfo(batchNum uint64, serverProof *ServerProofInfo) BatchInfo {
return BatchInfo{
batchNum: batchNum,
serverProof: serverProof,
}
}
// AddTxsInfo adds the l1UserTxs, l1OperatorTxs and l2Txs to the BatchInfo data
// structure
func (bi *BatchInfo) AddTxsInfo(l1UserTxsExtra, l1OperatorTxs []common.L1Tx, l2Txs []common.PoolL2Tx) {
// TBD parameter: feesInfo
bi.L1UserTxsExtra = l1UserTxsExtra
bi.L1OperatorTxs = l1OperatorTxs
bi.L2Txs = l2Txs
}
// AddTxsInfo adds the ZKInputs to the BatchInfo data structure
func (bi *BatchInfo) AddZKInputs(zkInputs *common.ZKInputs) {
bi.zkInputs = zkInputs
}
// AddTxsInfo adds the ServerProofInfo to the BatchInfo data structure
func (bi *BatchInfo) AddServerProof(serverProof *ServerProofInfo) {
bi.serverProof = serverProof
}
// BatchQueue implements a FIFO queue of BatchInfo
type BatchQueue struct {
queue []*BatchInfo
}
// Push adds the given BatchInfo to the BatchQueue
func (bq *BatchQueue) Push(b *BatchInfo) {
bq.queue = append(bq.queue, b)
}
// Pop pops the first BatchInfo from the BatchQueue
func (bq *BatchQueue) Pop() *BatchInfo {
if len(bq.queue) == 0 {
return nil
}
b := bq.queue[0]
bq.queue = bq.queue[1:]
return b
}

26
coordinator/batch_test.go Normal file
View File

@@ -0,0 +1,26 @@
package coordinator
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestBatchQueue(t *testing.T) {
bq := BatchQueue{}
bq.Push(&BatchInfo{
batchNum: 0,
})
bq.Push(&BatchInfo{
batchNum: 2,
})
bq.Push(&BatchInfo{
batchNum: 1,
})
assert.Equal(t, uint64(0), bq.Pop().batchNum)
assert.Equal(t, uint64(2), bq.Pop().batchNum)
assert.Equal(t, uint64(1), bq.Pop().batchNum)
assert.Nil(t, bq.Pop())
}

21
coordinator/proofpool.go Normal file
View File

@@ -0,0 +1,21 @@
package coordinator
import "github.com/hermeznetwork/hermez-node/common"
type ServerProofInfo struct {
// TODO
Available bool
}
func (p *ServerProofInfo) CalculateProof(zkInputs *common.ZKInputs) error {
return nil
}
type ServerProofPool struct {
pool []ServerProofInfo
}
func (p *ServerProofPool) GetNextAvailable() (*ServerProofInfo, error) {
return nil, nil
}