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