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
11const batchNumBytesLen = 8
12
13// Batch is a struct that represents Hermez network batch
14type Batch struct {
15 BatchNum BatchNum `meddler:"batch_num"`
16 EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum block in which the batch is forged
17 ForgerAddr ethCommon.Address `meddler:"forger_addr"` // TODO: Should this be retrieved via slot reference?
18 CollectedFees map[TokenID]*big.Int `meddler:"fees_collected,json"`
19 StateRoot Hash `meddler:"state_root"`
20 NumAccounts int `meddler:"num_accounts"`
21 ExitRoot Hash `meddler:"exit_root"`
22 ForgeL1TxsNum *int64 `meddler:"forge_l1_txs_num"` // optional, Only when the batch forges L1 txs. Identifier that corresponds to the group of L1 txs forged in the current batch.
23 SlotNum SlotNum `meddler:"slot_num"` // Slot in which the batch is forged
24}
25
26// BatchNum identifies a batch
27type BatchNum int64
28
29// Bytes returns a byte array of length 4 representing the BatchNum
30func (bn BatchNum) Bytes() []byte {
31 var batchNumBytes [batchNumBytesLen]byte
32 binary.BigEndian.PutUint64(batchNumBytes[:], uint64(bn))
33 return batchNumBytes[:]
34}
35
36// BatchNumFromBytes returns BatchNum from a []byte
37func BatchNumFromBytes(b []byte) (BatchNum, error) {
38 if len(b) != batchNumBytesLen {
39 return 0, fmt.Errorf("can not parse BatchNumFromBytes, bytes len %d, expected %d", len(b), batchNumBytesLen)
40 }
41 batchNum := binary.BigEndian.Uint64(b[:batchNumBytesLen])
42 return BatchNum(batchNum), nil
43}