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// NewEmptyBatch creates a new empty batch
31func NewEmptyBatch() *Batch {
32	return &Batch{
33		BatchNum:           0,
34		EthBlockNum:        0,
35		ForgerAddr:         ethCommon.Address{},
36		CollectedFees:      make(map[TokenID]*big.Int),
37		FeeIdxsCoordinator: make([]Idx, 0),
38		StateRoot:          big.NewInt(0),
39		NumAccounts:        0,
40		LastIdx:            0,
41		ExitRoot:           big.NewInt(0),
42		ForgeL1TxsNum:      nil,
43		SlotNum:            0,
44		TotalFeesUSD:       nil,
45	}
46}
47
48// BatchNum identifies a batch
49type BatchNum int64
50
51// Bytes returns a byte array of length 4 representing the BatchNum
52func (bn BatchNum) Bytes() []byte {
53	var batchNumBytes [batchNumBytesLen]byte
54	binary.BigEndian.PutUint64(batchNumBytes[:], uint64(bn))
55	return batchNumBytes[:]
56}
57
58// BigInt returns a *big.Int representing the BatchNum
59func (bn BatchNum) BigInt() *big.Int {
60	return big.NewInt(int64(bn))
61}
62
63// BatchNumFromBytes returns BatchNum from a []byte
64func BatchNumFromBytes(b []byte) (BatchNum, error) {
65	if len(b) != batchNumBytesLen {
66		return 0, tracerr.Wrap(fmt.Errorf("can not parse BatchNumFromBytes, bytes len %d, expected %d", len(b), batchNumBytesLen))
67	}
68	batchNum := binary.BigEndian.Uint64(b[:batchNumBytesLen])
69	return BatchNum(batchNum), nil
70}
71
72// BatchData contains the information of a Batch
73type BatchData struct {
74	L1Batch bool // TODO: Remove once Batch.ForgeL1TxsNum is a pointer
75	// L1UserTxs that were forged in the batch
76	L1UserTxs        []L1Tx
77	L1CoordinatorTxs []L1Tx
78	L2Txs            []L2Tx
79	CreatedAccounts  []Account
80	UpdatedAccounts  []AccountUpdate
81	ExitTree         []ExitInfo
82	Batch            Batch
83}
84
85// NewBatchData creates an empty BatchData with the slices initialized.
86func NewBatchData() *BatchData {
87	return &BatchData{
88		L1Batch: false,
89		// L1UserTxs:        make([]common.L1Tx, 0),
90		L1CoordinatorTxs: make([]L1Tx, 0),
91		L2Txs:            make([]L2Tx, 0),
92		CreatedAccounts:  make([]Account, 0),
93		ExitTree:         make([]ExitInfo, 0),
94		Batch:            Batch{},
95	}
96}