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.

166 lines
5.3 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. // MarshalText marshals a TxID
  65. func (txid TxID) MarshalText() ([]byte, error) {
  66. return []byte(txid.String()), nil
  67. }
  68. // UnmarshalText unmarshals a TxID
  69. func (txid *TxID) UnmarshalText(data []byte) error {
  70. idStr := string(data)
  71. id, err := NewTxIDFromString(idStr)
  72. if err != nil {
  73. return err
  74. }
  75. *txid = id
  76. return nil
  77. }
  78. // TxType is a string that represents the type of a Hermez network transaction
  79. type TxType string
  80. const (
  81. // TxTypeExit represents L2->L1 token transfer. A leaf for this account appears in the exit tree of the block
  82. TxTypeExit TxType = "Exit"
  83. // TxTypeTransfer represents L2->L2 token transfer
  84. TxTypeTransfer TxType = "Transfer"
  85. // TxTypeDeposit represents L1->L2 transfer
  86. TxTypeDeposit TxType = "Deposit"
  87. // TxTypeCreateAccountDeposit represents creation of a new leaf in the state tree (newAcconut) + L1->L2 transfer
  88. TxTypeCreateAccountDeposit TxType = "CreateAccountDeposit"
  89. // TxTypeCreateAccountDepositTransfer represents L1->L2 transfer + L2->L2 transfer
  90. TxTypeCreateAccountDepositTransfer TxType = "CreateAccountDepositTransfer"
  91. // TxTypeDepositTransfer TBD
  92. TxTypeDepositTransfer TxType = "DepositTransfer"
  93. // TxTypeForceTransfer TBD
  94. TxTypeForceTransfer TxType = "ForceTransfer"
  95. // TxTypeForceExit TBD
  96. TxTypeForceExit TxType = "ForceExit"
  97. // TxTypeTransferToEthAddr TBD
  98. TxTypeTransferToEthAddr TxType = "TransferToEthAddr"
  99. // TxTypeTransferToBJJ TBD
  100. TxTypeTransferToBJJ TxType = "TransferToBJJ"
  101. )
  102. // Tx is a struct used by the TxSelector & BatchBuilder as a generic type generated from L1Tx & PoolL2Tx
  103. // TODO: this should be changed for "mini Tx"
  104. type Tx struct {
  105. // Generic
  106. IsL1 bool `meddler:"is_l1"`
  107. TxID TxID `meddler:"id"`
  108. Type TxType `meddler:"type"`
  109. Position int `meddler:"position"`
  110. FromIdx Idx `meddler:"from_idx"`
  111. ToIdx Idx `meddler:"to_idx"`
  112. Amount *big.Int `meddler:"amount,bigint"`
  113. AmountFloat float64 `meddler:"amount_f"`
  114. TokenID TokenID `meddler:"token_id"`
  115. USD *float64 `meddler:"amount_usd"`
  116. BatchNum *BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
  117. EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
  118. // L1
  119. ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
  120. 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
  121. FromEthAddr ethCommon.Address `meddler:"from_eth_addr"`
  122. FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
  123. LoadAmount *big.Int `meddler:"load_amount,bigintnull"`
  124. LoadAmountFloat *float64 `meddler:"load_amount_f"`
  125. LoadAmountUSD *float64 `meddler:"load_amount_usd"`
  126. // L2
  127. Fee *FeeSelector `meddler:"fee"`
  128. FeeUSD *float64 `meddler:"fee_usd"`
  129. Nonce *Nonce `meddler:"nonce"`
  130. }
  131. // L1Tx returns a *L1Tx from the Tx
  132. func (tx *Tx) L1Tx() (*L1Tx, error) {
  133. return &L1Tx{
  134. TxID: tx.TxID,
  135. ToForgeL1TxsNum: tx.ToForgeL1TxsNum,
  136. Position: tx.Position,
  137. UserOrigin: *tx.UserOrigin,
  138. FromIdx: tx.FromIdx,
  139. FromEthAddr: tx.FromEthAddr,
  140. FromBJJ: tx.FromBJJ,
  141. ToIdx: tx.ToIdx,
  142. TokenID: tx.TokenID,
  143. Amount: tx.Amount,
  144. LoadAmount: tx.LoadAmount,
  145. EthBlockNum: tx.EthBlockNum,
  146. Type: tx.Type,
  147. BatchNum: tx.BatchNum,
  148. }, nil
  149. }