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