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.

201 lines
6.5 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/hermeznetwork/tracerr"
  12. "github.com/iden3/go-iden3-crypto/babyjub"
  13. )
  14. const (
  15. // TXIDPrefixL1UserTx is the prefix that determines that the TxID is
  16. // for a L1UserTx
  17. //nolinter:gomnd
  18. TxIDPrefixL1UserTx = byte(0)
  19. // TXIDPrefixL1CoordTx is the prefix that determines that the TxID is
  20. // for a L1CoordinatorTx
  21. //nolinter:gomnd
  22. TxIDPrefixL1CoordTx = byte(1)
  23. // TxIDPrefixL2Tx is the prefix that determines that the TxID is for a
  24. // L2Tx (or PoolL2Tx)
  25. //nolinter:gomnd
  26. TxIDPrefixL2Tx = byte(2)
  27. // TxIDLen is the length of the TxID byte array
  28. TxIDLen = 12
  29. )
  30. var (
  31. // SignatureConstantBytes contains the SignatureConstant in byte array
  32. // format, which is equivalent to 3322668559 as uint32 in byte array in
  33. // big endian representation.
  34. SignatureConstantBytes = []byte{198, 11, 230, 15}
  35. )
  36. // TxID is the identifier of a Hermez network transaction
  37. type TxID [TxIDLen]byte
  38. // Scan implements Scanner for database/sql.
  39. func (txid *TxID) Scan(src interface{}) error {
  40. srcB, ok := src.([]byte)
  41. if !ok {
  42. return tracerr.Wrap(fmt.Errorf("can't scan %T into TxID", src))
  43. }
  44. if len(srcB) != TxIDLen {
  45. return tracerr.Wrap(fmt.Errorf("can't scan []byte of len %d into TxID, need %d", len(srcB), TxIDLen))
  46. }
  47. copy(txid[:], srcB)
  48. return nil
  49. }
  50. // Value implements valuer for database/sql.
  51. func (txid TxID) Value() (driver.Value, error) {
  52. return txid[:], nil
  53. }
  54. // String returns a string hexadecimal representation of the TxID
  55. func (txid TxID) String() string {
  56. return "0x" + hex.EncodeToString(txid[:])
  57. }
  58. // NewTxIDFromString returns a string hexadecimal representation of the TxID
  59. func NewTxIDFromString(idStr string) (TxID, error) {
  60. txid := TxID{}
  61. idStr = strings.TrimPrefix(idStr, "0x")
  62. decoded, err := hex.DecodeString(idStr)
  63. if err != nil {
  64. return TxID{}, tracerr.Wrap(err)
  65. }
  66. if len(decoded) != TxIDLen {
  67. return txid, tracerr.Wrap(errors.New("Invalid idStr"))
  68. }
  69. copy(txid[:], decoded)
  70. return txid, nil
  71. }
  72. // MarshalText marshals a TxID
  73. func (txid TxID) MarshalText() ([]byte, error) {
  74. return []byte(txid.String()), nil
  75. }
  76. // UnmarshalText unmarshals a TxID
  77. func (txid *TxID) UnmarshalText(data []byte) error {
  78. idStr := string(data)
  79. id, err := NewTxIDFromString(idStr)
  80. if err != nil {
  81. return tracerr.Wrap(err)
  82. }
  83. *txid = id
  84. return nil
  85. }
  86. // TxType is a string that represents the type of a Hermez network transaction
  87. type TxType string
  88. const (
  89. // TxTypeExit represents L2->L1 token transfer. A leaf for this account appears in the exit tree of the block
  90. TxTypeExit TxType = "Exit"
  91. // TxTypeTransfer represents L2->L2 token transfer
  92. TxTypeTransfer TxType = "Transfer"
  93. // TxTypeDeposit represents L1->L2 transfer
  94. TxTypeDeposit TxType = "Deposit"
  95. // TxTypeCreateAccountDeposit represents creation of a new leaf in the state tree (newAcconut) + L1->L2 transfer
  96. TxTypeCreateAccountDeposit TxType = "CreateAccountDeposit"
  97. // TxTypeCreateAccountDepositTransfer represents L1->L2 transfer + L2->L2 transfer
  98. TxTypeCreateAccountDepositTransfer TxType = "CreateAccountDepositTransfer"
  99. // TxTypeDepositTransfer TBD
  100. TxTypeDepositTransfer TxType = "DepositTransfer"
  101. // TxTypeForceTransfer TBD
  102. TxTypeForceTransfer TxType = "ForceTransfer"
  103. // TxTypeForceExit TBD
  104. TxTypeForceExit TxType = "ForceExit"
  105. // TxTypeTransferToEthAddr TBD
  106. TxTypeTransferToEthAddr TxType = "TransferToEthAddr"
  107. // TxTypeTransferToBJJ TBD
  108. TxTypeTransferToBJJ TxType = "TransferToBJJ"
  109. )
  110. // Tx is a struct used by the TxSelector & BatchBuilder as a generic type generated from L1Tx & PoolL2Tx
  111. type Tx struct {
  112. // Generic
  113. IsL1 bool `meddler:"is_l1"`
  114. TxID TxID `meddler:"id"`
  115. Type TxType `meddler:"type"`
  116. Position int `meddler:"position"`
  117. FromIdx Idx `meddler:"from_idx"`
  118. ToIdx Idx `meddler:"to_idx"`
  119. Amount *big.Int `meddler:"amount,bigint"`
  120. AmountFloat float64 `meddler:"amount_f"`
  121. TokenID TokenID `meddler:"token_id"`
  122. USD *float64 `meddler:"amount_usd"`
  123. BatchNum *BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged. If the tx is L2, this must be != 0
  124. EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
  125. // L1
  126. ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"` // toForgeL1TxsNum in which the tx was forged / will be forged
  127. 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
  128. FromEthAddr ethCommon.Address `meddler:"from_eth_addr"`
  129. FromBJJ *babyjub.PublicKey `meddler:"from_bjj"`
  130. DepositAmount *big.Int `meddler:"deposit_amount,bigintnull"`
  131. DepositAmountFloat *float64 `meddler:"deposit_amount_f"`
  132. DepositAmountUSD *float64 `meddler:"deposit_amount_usd"`
  133. // L2
  134. Fee *FeeSelector `meddler:"fee"`
  135. FeeUSD *float64 `meddler:"fee_usd"`
  136. Nonce *Nonce `meddler:"nonce"`
  137. }
  138. func (tx *Tx) String() string {
  139. buf := bytes.NewBufferString("")
  140. fmt.Fprintf(buf, "Type: %s, ", tx.Type)
  141. fmt.Fprintf(buf, "FromIdx: %s, ", tx.FromIdx)
  142. if tx.Type == TxTypeTransfer ||
  143. tx.Type == TxTypeDepositTransfer ||
  144. tx.Type == TxTypeCreateAccountDepositTransfer {
  145. fmt.Fprintf(buf, "ToIdx: %s, ", tx.ToIdx)
  146. }
  147. if tx.Type == TxTypeDeposit ||
  148. tx.Type == TxTypeDepositTransfer ||
  149. tx.Type == TxTypeCreateAccountDepositTransfer {
  150. fmt.Fprintf(buf, "DepositAmount: %d, ", tx.DepositAmount)
  151. }
  152. if tx.Type != TxTypeDeposit {
  153. fmt.Fprintf(buf, "Amount: %s, ", tx.Amount)
  154. }
  155. if tx.Type == TxTypeTransfer ||
  156. tx.Type == TxTypeDepositTransfer ||
  157. tx.Type == TxTypeCreateAccountDepositTransfer {
  158. fmt.Fprintf(buf, "Fee: %d, ", tx.Fee)
  159. }
  160. fmt.Fprintf(buf, "TokenID: %d", tx.TokenID)
  161. return buf.String()
  162. }
  163. // L1Tx returns a *L1Tx from the Tx
  164. func (tx *Tx) L1Tx() (*L1Tx, error) {
  165. return &L1Tx{
  166. TxID: tx.TxID,
  167. ToForgeL1TxsNum: tx.ToForgeL1TxsNum,
  168. Position: tx.Position,
  169. UserOrigin: *tx.UserOrigin,
  170. FromIdx: tx.FromIdx,
  171. FromEthAddr: tx.FromEthAddr,
  172. FromBJJ: tx.FromBJJ,
  173. ToIdx: tx.ToIdx,
  174. TokenID: tx.TokenID,
  175. Amount: tx.Amount,
  176. DepositAmount: tx.DepositAmount,
  177. EthBlockNum: tx.EthBlockNum,
  178. Type: tx.Type,
  179. BatchNum: tx.BatchNum,
  180. }, nil
  181. }