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.

46 lines
1.7 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
  11. SlotNum SlotNum // Slot in which the batch is forged
  12. EthTxHash ethCommon.Hash
  13. EthBlockNum uint64 // Ethereum block in which the batch is forged
  14. ExitRoot Hash
  15. OldStateRoot Hash
  16. NewStateRoot Hash
  17. OldNumAccounts int
  18. NewNumAccounts int
  19. ToForgeL1TxsNum uint32 // optional, Only when the batch forges L1 txs. Identifier that corresponds to the group of L1 txs forged in the current batch.
  20. ToForgeL1TxsHash ethCommon.Hash // optional, Only when the batch forges L1 txs. Frozen from pendingL1TxsHash (which are the group of L1UserTxs), to be forged in ToForgeL1TxsNum + 1.
  21. ForgedL1TxsHash ethCommon.Hash // optional, Only when the batch forges L1 txs. This will be the Hash of the group of L1 txs (L1UserTxs + L1CoordinatorTx) forged in the current batch.
  22. CollectedFees map[TokenID]*big.Int
  23. ForgerAddr ethCommon.Address // TODO: Should this be retrieved via slot reference?
  24. }
  25. // BatchNum identifies a batch
  26. type BatchNum uint32
  27. // Bytes returns a byte array of length 4 representing the BatchNum
  28. func (bn BatchNum) Bytes() []byte {
  29. var batchNumBytes [4]byte
  30. binary.LittleEndian.PutUint32(batchNumBytes[:], uint32(bn))
  31. return batchNumBytes[:]
  32. }
  33. // BatchNumFromBytes returns BatchNum from a []byte
  34. func BatchNumFromBytes(b []byte) (BatchNum, error) {
  35. if len(b) != 4 {
  36. return 0, fmt.Errorf("can not parse BatchNumFromBytes, bytes len %d, expected 4", len(b))
  37. }
  38. batchNum := binary.LittleEndian.Uint32(b[:4])
  39. return BatchNum(batchNum), nil
  40. }