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 StateRoot *big.Int `meddler:"state_root,bigint"`
20 NumAccounts int `meddler:"num_accounts"`
21 LastIdx int64 `meddler:"last_idx"`
22 ExitRoot *big.Int `meddler:"exit_root,bigint"`
23 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.
24 SlotNum int64 `meddler:"slot_num"` // Slot in which the batch is forged
25 TotalFeesUSD *float64 `meddler:"total_fees_usd"`
26}
27
28// BatchNum identifies a batch
29type BatchNum int64
30
31// Bytes returns a byte array of length 4 representing the BatchNum
32func (bn BatchNum) Bytes() []byte {
33 var batchNumBytes [batchNumBytesLen]byte
34 binary.BigEndian.PutUint64(batchNumBytes[:], uint64(bn))
35 return batchNumBytes[:]
36}
37
38// BatchNumFromBytes returns BatchNum from a []byte
39func BatchNumFromBytes(b []byte) (BatchNum, error) {
40 if len(b) != batchNumBytesLen {
41 return 0, fmt.Errorf("can not parse BatchNumFromBytes, bytes len %d, expected %d", len(b), batchNumBytesLen)
42 }
43 batchNum := binary.BigEndian.Uint64(b[:batchNumBytesLen])
44 return BatchNum(batchNum), nil
45}