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.

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