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.

193 lines
6.1 KiB

  1. package common
  2. import (
  3. "bytes"
  4. "database/sql/driver"
  5. "encoding/hex"
  6. "errors"
  7. "fmt"
  8. "math/big"
  9. "strings"
  10. ethCommon "github.com/ethereum/go-ethereum/common"
  11. "github.com/iden3/go-iden3-crypto/babyjub"
  12. )
  13. const (
  14. // TXIDPrefixL1UserTx is the prefix that determines that the TxID is
  15. // for a L1UserTx
  16. //nolinter:gomnd
  17. TxIDPrefixL1UserTx = byte(0)
  18. // TXIDPrefixL1CoordTx is the prefix that determines that the TxID is
  19. // for a L1CoordinatorTx
  20. //nolinter:gomnd
  21. TxIDPrefixL1CoordTx = byte(1)
  22. // TxIDPrefixL2Tx is the prefix that determines that the TxID is for a
  23. // L2Tx (or PoolL2Tx)
  24. //nolinter:gomnd
  25. TxIDPrefixL2Tx = byte(2)
  26. // TxIDLen is the length of the TxID byte array
  27. TxIDLen = 12
  28. )
  29. // TxID is the identifier of a Hermez network transaction
  30. type TxID [TxIDLen]byte
  31. // Scan implements Scanner for database/sql.
  32. func (txid *TxID) Scan(src interface{}) error {
  33. srcB, ok := src.([]byte)
  34. if !ok {
  35. return fmt.Errorf("can't scan %T into TxID", src)
  36. }
  37. if len(srcB) != TxIDLen {
  38. return fmt.Errorf("can't scan []byte of len %d into TxID, need %d", len(srcB), TxIDLen)
  39. }
  40. copy(txid[:], srcB)
  41. return nil
  42. }
  43. // Value implements valuer for database/sql.
  44. func (txid TxID) Value() (driver.Value, error) {
  45. return txid[:], nil
  46. }
  47. // String returns a string hexadecimal representation of the TxID
  48. func (txid TxID) String() string {
  49. return "0x" + hex.EncodeToString(txid[:])
  50. }
  51. // NewTxIDFromString returns a string hexadecimal representation of the TxID
  52. func NewTxIDFromString(idStr string) (TxID, error) {
  53. txid := TxID{}
  54. idStr = strings.TrimPrefix(idStr, "0x")
  55. decoded, err := hex.DecodeString(idStr)
  56. if err != nil {
  57. return TxID{}, err
  58. }
  59. if len(decoded) != TxIDLen {
  60. return txid, errors.New("Invalid idStr")
  61. }
  62. copy(txid[:], decoded)
  63. return txid, nil
  64. }
  65. // MarshalText marshals a TxID
  66. func (txid TxID) MarshalText() ([]byte, error) {
  67. return []byte(txid.String()), nil
  68. }
  69. // UnmarshalText unmarshals a TxID
  70. func (txid *TxID) UnmarshalText(data []byte) error {
  71. idStr := string(data)
  72. id, err := NewTxIDFromString(idStr)
  73. if err != nil {
  74. return err
  75. }
  76. *txid = id
  77. return nil
  78. }
  79. // TxType is a string that represents the type of a Hermez network transaction
  80. type TxType string
  81. const (
  82. // TxTypeExit represents L2->L1 token transfer. A leaf for this account appears in the exit tree of the block
  83. TxTypeExit TxType = "Exit"
  84. // TxTypeTransfer represents L2->L2 token transfer
  85. TxTypeTransfer TxType = "Transfer"
  86. // TxTypeDeposit represents L1->L2 transfer
  87. TxTypeDeposit TxType = "Deposit"
  88. // TxTypeCreateAccountDeposit represents creation of a new leaf in the state tree (newAcconut) + L1->L2 transfer
  89. TxTypeCreateAccountDeposit TxType = "CreateAccountDeposit"
  90. // TxTypeCreateAccountDepositTransfer represents L1->L2 transfer + L2->L2 transfer
  91. TxTypeCreateAccountDepositTransfer TxType = "CreateAccountDepositTransfer"
  92. // TxTypeDepositTransfer TBD
  93. TxTypeDepositTransfer TxType = "DepositTransfer"
  94. // TxTypeForceTransfer TBD
  95. TxTypeForceTransfer TxType = "ForceTransfer"
  96. // TxTypeForceExit TBD
  97. TxTypeForceExit TxType = "ForceExit"
  98. // TxTypeTransferToEthAddr TBD
  99. TxTypeTransferToEthAddr TxType = "TransferToEthAddr"
  100. // TxTypeTransferToBJJ TBD
  101. TxTypeTransferToBJJ TxType = "TransferToBJJ"
  102. )
  103. // Tx is a struct used by the TxSelector & BatchBuilder as a generic type generated from L1Tx & PoolL2Tx
  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. func (tx *Tx) String() string {
  132. buf := bytes.NewBufferString("")
  133. fmt.Fprintf(buf, "Type: %s, ", tx.Type)
  134. fmt.Fprintf(buf, "FromIdx: %s, ", tx.FromIdx)
  135. if tx.Type == TxTypeTransfer ||
  136. tx.Type == TxTypeDepositTransfer ||
  137. tx.Type == TxTypeCreateAccountDepositTransfer {
  138. fmt.Fprintf(buf, "ToIdx: %s, ", tx.ToIdx)
  139. }
  140. if tx.Type == TxTypeDeposit ||
  141. tx.Type == TxTypeDepositTransfer ||
  142. tx.Type == TxTypeCreateAccountDepositTransfer {
  143. fmt.Fprintf(buf, "LoadAmount: %d, ", tx.LoadAmount)
  144. }
  145. if tx.Type != TxTypeDeposit {
  146. fmt.Fprintf(buf, "Amount: %s, ", tx.Amount)
  147. }
  148. if tx.Type == TxTypeTransfer ||
  149. tx.Type == TxTypeDepositTransfer ||
  150. tx.Type == TxTypeCreateAccountDepositTransfer {
  151. fmt.Fprintf(buf, "Fee: %d, ", tx.Fee)
  152. }
  153. fmt.Fprintf(buf, "TokenID: %d", tx.TokenID)
  154. return buf.String()
  155. }
  156. // L1Tx returns a *L1Tx from the Tx
  157. func (tx *Tx) L1Tx() (*L1Tx, error) {
  158. return &L1Tx{
  159. TxID: tx.TxID,
  160. ToForgeL1TxsNum: tx.ToForgeL1TxsNum,
  161. Position: tx.Position,
  162. UserOrigin: *tx.UserOrigin,
  163. FromIdx: tx.FromIdx,
  164. FromEthAddr: tx.FromEthAddr,
  165. FromBJJ: tx.FromBJJ,
  166. ToIdx: tx.ToIdx,
  167. TokenID: tx.TokenID,
  168. Amount: tx.Amount,
  169. LoadAmount: tx.LoadAmount,
  170. EthBlockNum: tx.EthBlockNum,
  171. Type: tx.Type,
  172. BatchNum: tx.BatchNum,
  173. }, nil
  174. }