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.

150 lines
5.0 KiB

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