arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/common/batch.go
common/batch.goRaw
 1package common
 2
 3import (
 4	"encoding/binary"
 5	"fmt"
 6	"math/big"
 7
 8	ethCommon "github.com/ethereum/go-ethereum/common"
 9)
10
11// Batch is a struct that represents Hermez network batch
12type Batch struct {
13	BatchNum         BatchNum
14	SlotNum          SlotNum // Slot in which the batch is forged
15	EthTxHash        ethCommon.Hash
16	EthBlockNum      uint64 // Ethereum block in which the batch is forged
17	ExitRoot         Hash
18	OldStateRoot     Hash
19	NewStateRoot     Hash
20	OldNumAccounts   int
21	NewNumAccounts   int
22	ToForgeL1TxsNum  uint32         // optional, Only when the batch forges L1 txs. Identifier that corresponds to the group of L1 txs forged in the current batch.
23	ToForgeL1TxsHash ethCommon.Hash // optional, Only when the batch forges L1 txs. Frozen from pendingL1TxsHash (which are the group of L1UserTxs), to be forged in ToForgeL1TxsNum + 1.
24	ForgedL1TxsHash  ethCommon.Hash // optional, Only when the batch forges L1 txs. This will be the Hash of the group of L1 txs (L1UserTxs + L1CoordinatorTx) forged in the current batch.
25	CollectedFees    map[TokenID]*big.Int
26	ForgerAddr       ethCommon.Address // TODO: Should this be retrieved via slot reference?
27}
28
29// BatchNum identifies a batch
30type BatchNum uint32
31
32// Bytes returns a byte array of length 4 representing the BatchNum
33func (bn BatchNum) Bytes() []byte {
34	var batchNumBytes [4]byte
35	binary.LittleEndian.PutUint32(batchNumBytes[:], uint32(bn))
36	return batchNumBytes[:]
37}
38
39// BatchNumFromBytes returns BatchNum from a []byte
40func BatchNumFromBytes(b []byte) (BatchNum, error) {
41	if len(b) != 4 {
42		return 0, fmt.Errorf("can not parse BatchNumFromBytes, bytes len %d, expected 4", len(b))
43	}
44	batchNum := binary.LittleEndian.Uint32(b[:4])
45	return BatchNum(batchNum), nil
46}