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