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	// Ethereum block in which the batch is forged
 18	EthBlockNum        int64                `meddler:"eth_block_num"`
 19	ForgerAddr         ethCommon.Address    `meddler:"forger_addr"`
 20	CollectedFees      map[TokenID]*big.Int `meddler:"fees_collected,json"`
 21	FeeIdxsCoordinator []Idx                `meddler:"fee_idxs_coordinator,json"`
 22	StateRoot          *big.Int             `meddler:"state_root,bigint"`
 23	NumAccounts        int                  `meddler:"num_accounts"`
 24	LastIdx            int64                `meddler:"last_idx"`
 25	ExitRoot           *big.Int             `meddler:"exit_root,bigint"`
 26	// ForgeL1TxsNum is optional, Only when the batch forges L1 txs. Identifier that corresponds
 27	// to the group of L1 txs forged in the current batch.
 28	ForgeL1TxsNum *int64   `meddler:"forge_l1_txs_num"`
 29	SlotNum       int64    `meddler:"slot_num"` // Slot in which the batch is forged
 30	TotalFeesUSD  *float64 `meddler:"total_fees_usd"`
 31}
 32
 33// NewEmptyBatch creates a new empty batch
 34func NewEmptyBatch() *Batch {
 35	return &Batch{
 36		BatchNum:           0,
 37		EthBlockNum:        0,
 38		ForgerAddr:         ethCommon.Address{},
 39		CollectedFees:      make(map[TokenID]*big.Int),
 40		FeeIdxsCoordinator: make([]Idx, 0),
 41		StateRoot:          big.NewInt(0),
 42		NumAccounts:        0,
 43		LastIdx:            0,
 44		ExitRoot:           big.NewInt(0),
 45		ForgeL1TxsNum:      nil,
 46		SlotNum:            0,
 47		TotalFeesUSD:       nil,
 48	}
 49}
 50
 51// BatchNum identifies a batch
 52type BatchNum int64
 53
 54// Bytes returns a byte array of length 4 representing the BatchNum
 55func (bn BatchNum) Bytes() []byte {
 56	var batchNumBytes [batchNumBytesLen]byte
 57	binary.BigEndian.PutUint64(batchNumBytes[:], uint64(bn))
 58	return batchNumBytes[:]
 59}
 60
 61// BigInt returns a *big.Int representing the BatchNum
 62func (bn BatchNum) BigInt() *big.Int {
 63	return big.NewInt(int64(bn))
 64}
 65
 66// BatchNumFromBytes returns BatchNum from a []byte
 67func BatchNumFromBytes(b []byte) (BatchNum, error) {
 68	if len(b) != batchNumBytesLen {
 69		return 0,
 70			tracerr.Wrap(fmt.Errorf("can not parse BatchNumFromBytes, bytes len %d, expected %d",
 71				len(b), batchNumBytesLen))
 72	}
 73	batchNum := binary.BigEndian.Uint64(b[:batchNumBytesLen])
 74	return BatchNum(batchNum), nil
 75}
 76
 77// BatchData contains the information of a Batch
 78type BatchData struct {
 79	L1Batch bool // TODO: Remove once Batch.ForgeL1TxsNum is a pointer
 80	// L1UserTxs that were forged in the batch
 81	L1UserTxs        []L1Tx
 82	L1CoordinatorTxs []L1Tx
 83	L2Txs            []L2Tx
 84	CreatedAccounts  []Account
 85	UpdatedAccounts  []AccountUpdate
 86	ExitTree         []ExitInfo
 87	Batch            Batch
 88}
 89
 90// NewBatchData creates an empty BatchData with the slices initialized.
 91func NewBatchData() *BatchData {
 92	return &BatchData{
 93		L1Batch: false,
 94		// L1UserTxs:        make([]common.L1Tx, 0),
 95		L1CoordinatorTxs: make([]L1Tx, 0),
 96		L2Txs:            make([]L2Tx, 0),
 97		CreatedAccounts:  make([]Account, 0),
 98		ExitTree:         make([]ExitInfo, 0),
 99		Batch:            Batch{},
100	}
101}