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.

194 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. // TODO: this should be changed for "mini Tx"
  105. type Tx struct {
  106. // Generic
  107. IsL1 bool `meddler:"is_l1"`
  108. TxID TxID `meddler:"id"`
  109. Type TxType `meddler:"type"`
  110. Position int `meddler:"position"`
  111. FromIdx Idx `meddler:"from_idx"`
  112. ToIdx Idx `meddler:"to_idx"`
  113. Amount *big.Int `meddler:"amount,bigint"`
  114. AmountFloat float64 `meddler:"amount_f"`
  115. TokenID TokenID `meddler:"token_id"`
  116. USD *float64 `meddler:"amount_usd"`
  117. BatchNum *BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
  118. EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
  119. // L1
  120. ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
  121. 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
  122. FromEthAddr ethCommon.Address `meddler:"from_eth_addr"`
  123. FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
  124. LoadAmount *big.Int `meddler:"load_amount,bigintnull"`
  125. LoadAmountFloat *float64 `meddler:"load_amount_f"`
  126. LoadAmountUSD *float64 `meddler:"load_amount_usd"`
  127. // L2
  128. Fee *FeeSelector `meddler:"fee"`
  129. FeeUSD *float64 `meddler:"fee_usd"`
  130. Nonce *Nonce `meddler:"nonce"`
  131. }
  132. func (tx *Tx) String() string {
  133. buf := bytes.NewBufferString("")
  134. fmt.Fprintf(buf, "Type: %s, ", tx.Type)
  135. fmt.Fprintf(buf, "FromIdx: %s, ", tx.FromIdx)
  136. if tx.Type == TxTypeTransfer ||
  137. tx.Type == TxTypeDepositTransfer ||
  138. tx.Type == TxTypeCreateAccountDepositTransfer {
  139. fmt.Fprintf(buf, "ToIdx: %s, ", tx.ToIdx)
  140. }
  141. if tx.Type == TxTypeDeposit ||
  142. tx.Type == TxTypeDepositTransfer ||
  143. tx.Type == TxTypeCreateAccountDepositTransfer {
  144. fmt.Fprintf(buf, "LoadAmount: %d, ", tx.LoadAmount)
  145. }
  146. if tx.Type != TxTypeDeposit {
  147. fmt.Fprintf(buf, "Amount: %s, ", tx.Amount)
  148. }
  149. if tx.Type == TxTypeTransfer ||
  150. tx.Type == TxTypeDepositTransfer ||
  151. tx.Type == TxTypeCreateAccountDepositTransfer {
  152. fmt.Fprintf(buf, "Fee: %d, ", tx.Fee)
  153. }
  154. fmt.Fprintf(buf, "TokenID: %d", tx.TokenID)
  155. return buf.String()
  156. }
  157. // L1Tx returns a *L1Tx from the Tx
  158. func (tx *Tx) L1Tx() (*L1Tx, error) {
  159. return &L1Tx{
  160. TxID: tx.TxID,
  161. ToForgeL1TxsNum: tx.ToForgeL1TxsNum,
  162. Position: tx.Position,
  163. UserOrigin: *tx.UserOrigin,
  164. FromIdx: tx.FromIdx,
  165. FromEthAddr: tx.FromEthAddr,
  166. FromBJJ: tx.FromBJJ,
  167. ToIdx: tx.ToIdx,
  168. TokenID: tx.TokenID,
  169. Amount: tx.Amount,
  170. LoadAmount: tx.LoadAmount,
  171. EthBlockNum: tx.EthBlockNum,
  172. Type: tx.Type,
  173. BatchNum: tx.BatchNum,
  174. }, nil
  175. }