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"`
18 CollectedFees map[TokenID]*big.Int `meddler:"fees_collected,json"`
19 FeeIdxsCoordinator []Idx `meddler:"fee_idxs_coordinator,json"`
20 StateRoot *big.Int `meddler:"state_root,bigint"`
21 NumAccounts int `meddler:"num_accounts"`
22 LastIdx int64 `meddler:"last_idx"`
23 ExitRoot *big.Int `meddler:"exit_root,bigint"`
24 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.
25 SlotNum int64 `meddler:"slot_num"` // Slot in which the batch is forged
26 TotalFeesUSD *float64 `meddler:"total_fees_usd"`
27}
28
29// BatchNum identifies a batch
30type BatchNum int64
31
32// Bytes returns a byte array of length 4 representing the BatchNum
33func (bn BatchNum) Bytes() []byte {
34 var batchNumBytes [batchNumBytesLen]byte
35 binary.BigEndian.PutUint64(batchNumBytes[:], uint64(bn))
36 return batchNumBytes[:]
37}
38
39// BatchNumFromBytes returns BatchNum from a []byte
40func BatchNumFromBytes(b []byte) (BatchNum, error) {
41 if len(b) != batchNumBytesLen {
42 return 0, fmt.Errorf("can not parse BatchNumFromBytes, bytes len %d, expected %d", len(b), batchNumBytesLen)
43 }
44 batchNum := binary.BigEndian.Uint64(b[:batchNumBytesLen])
45 return BatchNum(batchNum), nil
46}
47
48// BatchData contains the information of a Batch
49type BatchData struct {
50 // L1UserTxs that were forged in the batch
51 L1Batch bool // TODO: Remove once Batch.ForgeL1TxsNum is a pointer
52 // L1UserTxs []common.L1Tx
53 L1CoordinatorTxs []L1Tx
54 L2Txs []L2Tx
55 CreatedAccounts []Account
56 ExitTree []ExitInfo
57 Batch Batch
58}
59
60// NewBatchData creates an empty BatchData with the slices initialized.
61func NewBatchData() *BatchData {
62 return &BatchData{
63 L1Batch: false,
64 // L1UserTxs: make([]common.L1Tx, 0),
65 L1CoordinatorTxs: make([]L1Tx, 0),
66 L2Txs: make([]L2Tx, 0),
67 CreatedAccounts: make([]Account, 0),
68 ExitTree: make([]ExitInfo, 0),
69 Batch: Batch{},
70 }
71}