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.

82 lines
2.8 KiB

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