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.

88 lines
2.9 KiB

  1. package common
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "math/big"
  6. )
  7. const (
  8. idxBytesLen = 4
  9. // maxIdxValue is the maximum value that Idx can have (32 bits: maxIdxValue=2**32-1)
  10. maxIdxValue = 0xffffffff
  11. )
  12. // Idx represents the account Index in the MerkleTree
  13. type Idx uint32
  14. // Bytes returns a byte array representing the Idx
  15. func (idx Idx) Bytes() []byte {
  16. var b [4]byte
  17. binary.LittleEndian.PutUint32(b[:], uint32(idx))
  18. return b[:]
  19. }
  20. // BigInt returns a *big.Int representing the Idx
  21. func (idx Idx) BigInt() *big.Int {
  22. return big.NewInt(int64(idx))
  23. }
  24. // IdxFromBytes returns Idx from a byte array
  25. func IdxFromBytes(b []byte) (Idx, error) {
  26. if len(b) != idxBytesLen {
  27. return 0, fmt.Errorf("can not parse Idx, bytes len %d, expected 4", len(b))
  28. }
  29. idx := binary.LittleEndian.Uint32(b[:4])
  30. return Idx(idx), nil
  31. }
  32. // IdxFromBigInt converts a *big.Int to Idx type
  33. func IdxFromBigInt(b *big.Int) (Idx, error) {
  34. if b.Int64() > maxIdxValue {
  35. return 0, ErrNumOverflow
  36. }
  37. return Idx(uint32(b.Int64())), nil
  38. }
  39. // TxID is the identifier of a Hermez network transaction
  40. type TxID Hash // Hash is a guess
  41. // TxType is a string that represents the type of a Hermez network transaction
  42. type TxType string
  43. const (
  44. // TxTypeExit represents L2->L1 token transfer. A leaf for this account appears in the exit tree of the block
  45. TxTypeExit TxType = "Exit"
  46. // TxTypeWithdrawn represents the balance that was moved from L2->L1 has been widthrawn from the smart contract
  47. TxTypeWithdrawn TxType = "Withdrawn"
  48. // TxTypeTransfer represents L2->L2 token transfer
  49. TxTypeTransfer TxType = "Transfer"
  50. // TxTypeDeposit represents L1->L2 transfer
  51. TxTypeDeposit TxType = "Deposit"
  52. // TxTypeCreateAccountDeposit represents creation of a new leaf in the state tree (newAcconut) + L1->L2 transfer
  53. TxTypeCreateAccountDeposit TxType = "CreateAccountDeposit"
  54. // TxTypeCreateAccountDepositTransfer represents L1->L2 transfer + L2->L2 transfer
  55. TxTypeCreateAccountDepositTransfer TxType = "CreateAccountDepositTransfer"
  56. // TxTypeDepositTransfer TBD
  57. TxTypeDepositTransfer TxType = "TxTypeDepositTransfer"
  58. // TxTypeForceTransfer TBD
  59. TxTypeForceTransfer TxType = "TxTypeForceTransfer"
  60. // TxTypeForceExit TBD
  61. TxTypeForceExit TxType = "TxTypeForceExit"
  62. // TxTypeTransferToEthAddr TBD
  63. TxTypeTransferToEthAddr TxType = "TxTypeTransferToEthAddr"
  64. // TxTypeTransferToBJJ TBD
  65. TxTypeTransferToBJJ TxType = "TxTypeTransferToBJJ"
  66. )
  67. // Tx is a struct used by the TxSelector & BatchBuilder as a generic type generated from L1Tx & PoolL2Tx
  68. type Tx struct {
  69. TxID TxID
  70. FromIdx Idx // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
  71. ToIdx Idx // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositTransfer
  72. Amount *big.Int
  73. Nonce Nonce // effective 40 bits used
  74. Fee FeeSelector
  75. Type TxType // optional, descrives which kind of tx it's
  76. BatchNum BatchNum // batchNum in which this tx was forged. Presence indicates "forged" state.
  77. }