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.

133 lines
4.6 KiB

  1. package common
  2. import (
  3. "database/sql/driver"
  4. "encoding/hex"
  5. "fmt"
  6. "math/big"
  7. ethCommon "github.com/ethereum/go-ethereum/common"
  8. "github.com/iden3/go-iden3-crypto/babyjub"
  9. )
  10. const (
  11. // TXIDPrefixL1UserTx is the prefix that determines that the TxID is
  12. // for a L1UserTx
  13. //nolinter:gomnd
  14. TxIDPrefixL1UserTx = byte(0)
  15. // TXIDPrefixL1CoordTx is the prefix that determines that the TxID is
  16. // for a L1CoordinatorTx
  17. //nolinter:gomnd
  18. TxIDPrefixL1CoordTx = byte(1)
  19. // TxIDPrefixL2Tx is the prefix that determines that the TxID is for a
  20. // L2Tx (or PoolL2Tx)
  21. //nolinter:gomnd
  22. TxIDPrefixL2Tx = byte(2)
  23. // TxIDLen is the length of the TxID byte array
  24. TxIDLen = 12
  25. )
  26. // TxID is the identifier of a Hermez network transaction
  27. type TxID [TxIDLen]byte
  28. // Scan implements Scanner for database/sql.
  29. func (txid *TxID) Scan(src interface{}) error {
  30. srcB, ok := src.([]byte)
  31. if !ok {
  32. return fmt.Errorf("can't scan %T into TxID", src)
  33. }
  34. if len(srcB) != TxIDLen {
  35. return fmt.Errorf("can't scan []byte of len %d into TxID, need %d", len(srcB), TxIDLen)
  36. }
  37. copy(txid[:], srcB)
  38. return nil
  39. }
  40. // Value implements valuer for database/sql.
  41. func (txid TxID) Value() (driver.Value, error) {
  42. return txid[:], nil
  43. }
  44. // String returns a string hexadecimal representation of the TxID
  45. func (txid TxID) String() string {
  46. return "0x" + hex.EncodeToString(txid[:])
  47. }
  48. // TxType is a string that represents the type of a Hermez network transaction
  49. type TxType string
  50. const (
  51. // TxTypeExit represents L2->L1 token transfer. A leaf for this account appears in the exit tree of the block
  52. TxTypeExit TxType = "Exit"
  53. // TxTypeTransfer represents L2->L2 token transfer
  54. TxTypeTransfer TxType = "Transfer"
  55. // TxTypeDeposit represents L1->L2 transfer
  56. TxTypeDeposit TxType = "Deposit"
  57. // TxTypeCreateAccountDeposit represents creation of a new leaf in the state tree (newAcconut) + L1->L2 transfer
  58. TxTypeCreateAccountDeposit TxType = "CreateAccountDeposit"
  59. // TxTypeCreateAccountDepositTransfer represents L1->L2 transfer + L2->L2 transfer
  60. TxTypeCreateAccountDepositTransfer TxType = "CreateAccountDepositTransfer"
  61. // TxTypeDepositTransfer TBD
  62. TxTypeDepositTransfer TxType = "DepositTransfer"
  63. // TxTypeForceTransfer TBD
  64. TxTypeForceTransfer TxType = "ForceTransfer"
  65. // TxTypeForceExit TBD
  66. TxTypeForceExit TxType = "ForceExit"
  67. // TxTypeTransferToEthAddr TBD
  68. TxTypeTransferToEthAddr TxType = "TransferToEthAddr"
  69. // TxTypeTransferToBJJ TBD
  70. TxTypeTransferToBJJ TxType = "TransferToBJJ"
  71. )
  72. // Tx is a struct used by the TxSelector & BatchBuilder as a generic type generated from L1Tx & PoolL2Tx
  73. // TODO: this should be changed for "mini Tx"
  74. type Tx struct {
  75. // Generic
  76. IsL1 bool `meddler:"is_l1"`
  77. TxID TxID `meddler:"id"`
  78. Type TxType `meddler:"type"`
  79. Position int `meddler:"position"`
  80. FromIdx Idx `meddler:"from_idx"`
  81. ToIdx Idx `meddler:"to_idx"`
  82. Amount *big.Int `meddler:"amount,bigint"`
  83. AmountFloat float64 `meddler:"amount_f"`
  84. TokenID TokenID `meddler:"token_id"`
  85. USD *float64 `meddler:"amount_usd"`
  86. BatchNum *BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
  87. EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
  88. // L1
  89. ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
  90. UserOrigin *bool `meddler:"user_origin"` // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
  91. FromEthAddr ethCommon.Address `meddler:"from_eth_addr"`
  92. FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
  93. LoadAmount *big.Int `meddler:"load_amount,bigintnull"`
  94. LoadAmountFloat *float64 `meddler:"load_amount_f"`
  95. LoadAmountUSD *float64 `meddler:"load_amount_usd"`
  96. // L2
  97. Fee *FeeSelector `meddler:"fee"`
  98. FeeUSD *float64 `meddler:"fee_usd"`
  99. Nonce *Nonce `meddler:"nonce"`
  100. }
  101. // L1Tx returns a *L1Tx from the Tx
  102. func (tx *Tx) L1Tx() (*L1Tx, error) {
  103. return &L1Tx{
  104. TxID: tx.TxID,
  105. ToForgeL1TxsNum: tx.ToForgeL1TxsNum,
  106. Position: tx.Position,
  107. UserOrigin: *tx.UserOrigin,
  108. FromIdx: tx.FromIdx,
  109. FromEthAddr: tx.FromEthAddr,
  110. FromBJJ: tx.FromBJJ,
  111. ToIdx: tx.ToIdx,
  112. TokenID: tx.TokenID,
  113. Amount: tx.Amount,
  114. LoadAmount: tx.LoadAmount,
  115. EthBlockNum: tx.EthBlockNum,
  116. Type: tx.Type,
  117. BatchNum: tx.BatchNum,
  118. }, nil
  119. }