You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.6 KiB

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