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}