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.

211 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 = 33
  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",
  46. len(srcB), TxIDLen))
  47. }
  48. copy(txid[:], srcB)
  49. return nil
  50. }
  51. // Value implements valuer for database/sql.
  52. func (txid TxID) Value() (driver.Value, error) {
  53. return txid[:], nil
  54. }
  55. // String returns a string hexadecimal representation of the TxID
  56. func (txid TxID) String() string {
  57. return "0x" + hex.EncodeToString(txid[:])
  58. }
  59. // NewTxIDFromString returns a string hexadecimal representation of the TxID
  60. func NewTxIDFromString(idStr string) (TxID, error) {
  61. txid := TxID{}
  62. idStr = strings.TrimPrefix(idStr, "0x")
  63. decoded, err := hex.DecodeString(idStr)
  64. if err != nil {
  65. return TxID{}, tracerr.Wrap(err)
  66. }
  67. if len(decoded) != TxIDLen {
  68. return txid, tracerr.Wrap(errors.New("Invalid idStr"))
  69. }
  70. copy(txid[:], decoded)
  71. return txid, nil
  72. }
  73. // MarshalText marshals a TxID
  74. func (txid TxID) MarshalText() ([]byte, error) {
  75. return []byte(txid.String()), nil
  76. }
  77. // UnmarshalText unmarshals a TxID
  78. func (txid *TxID) UnmarshalText(data []byte) error {
  79. idStr := string(data)
  80. id, err := NewTxIDFromString(idStr)
  81. if err != nil {
  82. return tracerr.Wrap(err)
  83. }
  84. *txid = id
  85. return nil
  86. }
  87. // TxType is a string that represents the type of a Hermez network transaction
  88. type TxType string
  89. const (
  90. // TxTypeExit represents L2->L1 token transfer. A leaf for this account appears in the exit
  91. // tree of the block
  92. TxTypeExit TxType = "Exit"
  93. // TxTypeTransfer represents L2->L2 token transfer
  94. TxTypeTransfer TxType = "Transfer"
  95. // TxTypeDeposit represents L1->L2 transfer
  96. TxTypeDeposit TxType = "Deposit"
  97. // TxTypeCreateAccountDeposit represents creation of a new leaf in the state tree
  98. // (newAcconut) + L1->L2 transfer
  99. TxTypeCreateAccountDeposit TxType = "CreateAccountDeposit"
  100. // TxTypeCreateAccountDepositTransfer represents L1->L2 transfer + L2->L2 transfer
  101. TxTypeCreateAccountDepositTransfer TxType = "CreateAccountDepositTransfer"
  102. // TxTypeDepositTransfer TBD
  103. TxTypeDepositTransfer TxType = "DepositTransfer"
  104. // TxTypeForceTransfer TBD
  105. TxTypeForceTransfer TxType = "ForceTransfer"
  106. // TxTypeForceExit TBD
  107. TxTypeForceExit TxType = "ForceExit"
  108. // TxTypeTransferToEthAddr TBD
  109. TxTypeTransferToEthAddr TxType = "TransferToEthAddr"
  110. // TxTypeTransferToBJJ TBD
  111. TxTypeTransferToBJJ TxType = "TransferToBJJ"
  112. )
  113. // Tx is a struct used by the TxSelector & BatchBuilder as a generic type generated from L1Tx &
  114. // PoolL2Tx
  115. type Tx struct {
  116. // Generic
  117. IsL1 bool `meddler:"is_l1"`
  118. TxID TxID `meddler:"id"`
  119. Type TxType `meddler:"type"`
  120. Position int `meddler:"position"`
  121. FromIdx Idx `meddler:"from_idx"`
  122. ToIdx Idx `meddler:"to_idx"`
  123. Amount *big.Int `meddler:"amount,bigint"`
  124. AmountFloat float64 `meddler:"amount_f"`
  125. TokenID TokenID `meddler:"token_id"`
  126. USD *float64 `meddler:"amount_usd"`
  127. // BatchNum in which this tx was forged. If the tx is L2, this must be != 0
  128. BatchNum *BatchNum `meddler:"batch_num"`
  129. // Ethereum Block Number in which this L1Tx was added to the queue
  130. EthBlockNum int64 `meddler:"eth_block_num"`
  131. // L1
  132. // ToForgeL1TxsNum in which the tx was forged / will be forged
  133. ToForgeL1TxsNum *int64 `meddler:"to_forge_l1_txs_num"`
  134. // UserOrigin is set to true if the tx was originated by a user, false if it was aoriginated
  135. // by a coordinator. Note that this differ from the spec for implementation simplification
  136. // purpposes
  137. UserOrigin *bool `meddler:"user_origin"`
  138. FromEthAddr ethCommon.Address `meddler:"from_eth_addr"`
  139. FromBJJ babyjub.PublicKeyComp `meddler:"from_bjj"`
  140. DepositAmount *big.Int `meddler:"deposit_amount,bigintnull"`
  141. DepositAmountFloat *float64 `meddler:"deposit_amount_f"`
  142. DepositAmountUSD *float64 `meddler:"deposit_amount_usd"`
  143. // L2
  144. Fee *FeeSelector `meddler:"fee"`
  145. FeeUSD *float64 `meddler:"fee_usd"`
  146. Nonce *Nonce `meddler:"nonce"`
  147. }
  148. func (tx *Tx) String() string {
  149. buf := bytes.NewBufferString("")
  150. fmt.Fprintf(buf, "Type: %s, ", tx.Type)
  151. fmt.Fprintf(buf, "FromIdx: %s, ", tx.FromIdx)
  152. if tx.Type == TxTypeTransfer ||
  153. tx.Type == TxTypeDepositTransfer ||
  154. tx.Type == TxTypeCreateAccountDepositTransfer {
  155. fmt.Fprintf(buf, "ToIdx: %s, ", tx.ToIdx)
  156. }
  157. if tx.Type == TxTypeDeposit ||
  158. tx.Type == TxTypeDepositTransfer ||
  159. tx.Type == TxTypeCreateAccountDepositTransfer {
  160. fmt.Fprintf(buf, "DepositAmount: %d, ", tx.DepositAmount)
  161. }
  162. if tx.Type != TxTypeDeposit {
  163. fmt.Fprintf(buf, "Amount: %s, ", tx.Amount)
  164. }
  165. if tx.Type == TxTypeTransfer ||
  166. tx.Type == TxTypeDepositTransfer ||
  167. tx.Type == TxTypeCreateAccountDepositTransfer {
  168. fmt.Fprintf(buf, "Fee: %d, ", tx.Fee)
  169. }
  170. fmt.Fprintf(buf, "TokenID: %d", tx.TokenID)
  171. return buf.String()
  172. }
  173. // L1Tx returns a *L1Tx from the Tx
  174. func (tx *Tx) L1Tx() (*L1Tx, error) {
  175. return &L1Tx{
  176. TxID: tx.TxID,
  177. ToForgeL1TxsNum: tx.ToForgeL1TxsNum,
  178. Position: tx.Position,
  179. UserOrigin: *tx.UserOrigin,
  180. FromIdx: tx.FromIdx,
  181. FromEthAddr: tx.FromEthAddr,
  182. FromBJJ: tx.FromBJJ,
  183. ToIdx: tx.ToIdx,
  184. TokenID: tx.TokenID,
  185. Amount: tx.Amount,
  186. DepositAmount: tx.DepositAmount,
  187. EthBlockNum: tx.EthBlockNum,
  188. Type: tx.Type,
  189. BatchNum: tx.BatchNum,
  190. }, nil
  191. }