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.

236 lines
6.1 KiB

  1. package common
  2. import (
  3. "fmt"
  4. "math/big"
  5. ethCrypto "github.com/ethereum/go-ethereum/crypto"
  6. "github.com/hermeznetwork/tracerr"
  7. )
  8. // L2Tx is a struct that represents an already forged L2 tx
  9. type L2Tx struct {
  10. // Stored in DB: mandatory fields
  11. TxID TxID `meddler:"id"`
  12. BatchNum BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged.
  13. Position int `meddler:"position"`
  14. FromIdx Idx `meddler:"from_idx"`
  15. ToIdx Idx `meddler:"to_idx"`
  16. // TokenID is filled by the TxProcessor
  17. TokenID TokenID `meddler:"token_id"`
  18. Amount *big.Int `meddler:"amount,bigint"`
  19. Fee FeeSelector `meddler:"fee"`
  20. // Nonce is filled by the TxProcessor
  21. Nonce Nonce `meddler:"nonce"`
  22. Type TxType `meddler:"type"`
  23. // EthBlockNum in which this L2Tx was added to the queue
  24. EthBlockNum int64 `meddler:"eth_block_num"`
  25. }
  26. // NewL2Tx returns the given L2Tx with the TxId & Type parameters calculated
  27. // from the L2Tx values
  28. func NewL2Tx(tx *L2Tx) (*L2Tx, error) {
  29. txTypeOld := tx.Type
  30. if err := tx.SetType(); err != nil {
  31. return nil, tracerr.Wrap(err)
  32. }
  33. // If original Type doesn't match the correct one, return error
  34. if txTypeOld != "" && txTypeOld != tx.Type {
  35. return nil, tracerr.Wrap(fmt.Errorf("L2Tx.Type: %s, should be: %s",
  36. tx.Type, txTypeOld))
  37. }
  38. txIDOld := tx.TxID
  39. if err := tx.SetID(); err != nil {
  40. return nil, tracerr.Wrap(err)
  41. }
  42. // If original TxID doesn't match the correct one, return error
  43. if txIDOld != (TxID{}) && txIDOld != tx.TxID {
  44. return tx, tracerr.Wrap(fmt.Errorf("L2Tx.TxID: %s, should be: %s",
  45. tx.TxID.String(), txIDOld.String()))
  46. }
  47. return tx, nil
  48. }
  49. // SetType sets the type of the transaction. Uses (FromIdx, Nonce).
  50. func (tx *L2Tx) SetType() error {
  51. if tx.ToIdx == Idx(1) {
  52. tx.Type = TxTypeExit
  53. } else if tx.ToIdx >= IdxUserThreshold {
  54. tx.Type = TxTypeTransfer
  55. } else {
  56. return tracerr.Wrap(fmt.Errorf(
  57. "cannot determine type of L2Tx, invalid ToIdx value: %d", tx.ToIdx))
  58. }
  59. return nil
  60. }
  61. // SetID sets the ID of the transaction
  62. func (tx *L2Tx) SetID() error {
  63. txID, err := tx.CalculateTxID()
  64. if err != nil {
  65. return err
  66. }
  67. tx.TxID = txID
  68. return nil
  69. }
  70. // CalculateTxID returns the TxID of the transaction. This method is used to
  71. // set the TxID for L2Tx and for PoolL2Tx.
  72. func (tx L2Tx) CalculateTxID() ([TxIDLen]byte, error) {
  73. var txID TxID
  74. var b []byte
  75. // FromIdx
  76. fromIdxBytes, err := tx.FromIdx.Bytes()
  77. if err != nil {
  78. return txID, tracerr.Wrap(err)
  79. }
  80. b = append(b, fromIdxBytes[:]...)
  81. // TokenID
  82. b = append(b, tx.TokenID.Bytes()[:]...)
  83. // Amount
  84. amountFloat40, err := NewFloat40(tx.Amount)
  85. if err != nil {
  86. return txID, tracerr.Wrap(fmt.Errorf("%s: %d", err, tx.Amount))
  87. }
  88. amountFloat40Bytes, err := amountFloat40.Bytes()
  89. if err != nil {
  90. return txID, tracerr.Wrap(err)
  91. }
  92. b = append(b, amountFloat40Bytes...)
  93. // Nonce
  94. nonceBytes, err := tx.Nonce.Bytes()
  95. if err != nil {
  96. return txID, tracerr.Wrap(err)
  97. }
  98. b = append(b, nonceBytes[:]...)
  99. // Fee
  100. b = append(b, byte(tx.Fee))
  101. // calculate hash
  102. h := ethCrypto.Keccak256Hash(b).Bytes()
  103. txID[0] = TxIDPrefixL2Tx
  104. copy(txID[1:], h)
  105. return txID, nil
  106. }
  107. // Tx returns a *Tx from the L2Tx
  108. func (tx *L2Tx) Tx() *Tx {
  109. batchNum := new(BatchNum)
  110. *batchNum = tx.BatchNum
  111. fee := new(FeeSelector)
  112. *fee = tx.Fee
  113. nonce := new(Nonce)
  114. *nonce = tx.Nonce
  115. return &Tx{
  116. IsL1: false,
  117. TxID: tx.TxID,
  118. Type: tx.Type,
  119. Position: tx.Position,
  120. FromIdx: tx.FromIdx,
  121. ToIdx: tx.ToIdx,
  122. TokenID: tx.TokenID,
  123. Amount: tx.Amount,
  124. BatchNum: batchNum,
  125. EthBlockNum: tx.EthBlockNum,
  126. Fee: fee,
  127. Nonce: nonce,
  128. }
  129. }
  130. // PoolL2Tx returns the data structure of PoolL2Tx with the parameters of a
  131. // L2Tx filled
  132. func (tx L2Tx) PoolL2Tx() *PoolL2Tx {
  133. return &PoolL2Tx{
  134. TxID: tx.TxID,
  135. FromIdx: tx.FromIdx,
  136. ToIdx: tx.ToIdx,
  137. TokenID: tx.TokenID,
  138. Amount: tx.Amount,
  139. Fee: tx.Fee,
  140. Nonce: tx.Nonce,
  141. Type: tx.Type,
  142. }
  143. }
  144. // L2TxsToPoolL2Txs returns an array of []*PoolL2Tx from an array of []*L2Tx,
  145. // where the PoolL2Tx only have the parameters of a L2Tx filled.
  146. func L2TxsToPoolL2Txs(txs []L2Tx) []PoolL2Tx {
  147. var r []PoolL2Tx
  148. for _, tx := range txs {
  149. r = append(r, *tx.PoolL2Tx())
  150. }
  151. return r
  152. }
  153. // TxIDsFromL2Txs returns an array of TxID from the []L2Tx
  154. func TxIDsFromL2Txs(txs []L2Tx) []TxID {
  155. txIDs := make([]TxID, len(txs))
  156. for i, tx := range txs {
  157. txIDs[i] = tx.TxID
  158. }
  159. return txIDs
  160. }
  161. // BytesDataAvailability encodes a L2Tx into []byte for the Data Availability
  162. // [ fromIdx | toIdx | amountFloat40 | Fee ]
  163. func (tx L2Tx) BytesDataAvailability(nLevels uint32) ([]byte, error) {
  164. idxLen := nLevels / 8 //nolint:gomnd
  165. b := make([]byte, ((nLevels*2)+40+8)/8) //nolint:gomnd
  166. fromIdxBytes, err := tx.FromIdx.Bytes()
  167. if err != nil {
  168. return nil, tracerr.Wrap(err)
  169. }
  170. copy(b[0:idxLen], fromIdxBytes[6-idxLen:]) // [6-idxLen:] as is BigEndian
  171. toIdxBytes, err := tx.ToIdx.Bytes()
  172. if err != nil {
  173. return nil, tracerr.Wrap(err)
  174. }
  175. copy(b[idxLen:idxLen*2], toIdxBytes[6-idxLen:])
  176. amountFloat40, err := NewFloat40(tx.Amount)
  177. if err != nil {
  178. return nil, tracerr.Wrap(err)
  179. }
  180. amountFloat40Bytes, err := amountFloat40.Bytes()
  181. if err != nil {
  182. return nil, tracerr.Wrap(err)
  183. }
  184. copy(b[idxLen*2:idxLen*2+Float40BytesLength], amountFloat40Bytes)
  185. b[idxLen*2+Float40BytesLength] = byte(tx.Fee)
  186. return b[:], nil
  187. }
  188. // L2TxFromBytesDataAvailability decodes a L2Tx from []byte (Data Availability)
  189. func L2TxFromBytesDataAvailability(b []byte, nLevels int) (*L2Tx, error) {
  190. idxLen := nLevels / 8 //nolint:gomnd
  191. tx := &L2Tx{}
  192. var err error
  193. var paddedFromIdxBytes [6]byte
  194. copy(paddedFromIdxBytes[6-idxLen:], b[0:idxLen])
  195. tx.FromIdx, err = IdxFromBytes(paddedFromIdxBytes[:])
  196. if err != nil {
  197. return nil, tracerr.Wrap(err)
  198. }
  199. var paddedToIdxBytes [6]byte
  200. copy(paddedToIdxBytes[6-idxLen:6], b[idxLen:idxLen*2])
  201. tx.ToIdx, err = IdxFromBytes(paddedToIdxBytes[:])
  202. if err != nil {
  203. return nil, tracerr.Wrap(err)
  204. }
  205. tx.Amount, err = Float40FromBytes(b[idxLen*2 : idxLen*2+Float40BytesLength]).BigInt()
  206. if err != nil {
  207. return nil, tracerr.Wrap(err)
  208. }
  209. tx.Fee = FeeSelector(b[idxLen*2+Float40BytesLength])
  210. return tx, nil
  211. }