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.

235 lines
6.0 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"` // EthereumBlockNumber 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. amountFloat40, err := NewFloat40(tx.Amount)
  84. if err != nil {
  85. return txID, tracerr.Wrap(fmt.Errorf("%s: %d", err, tx.Amount))
  86. }
  87. amountFloat40Bytes, err := amountFloat40.Bytes()
  88. if err != nil {
  89. return txID, tracerr.Wrap(err)
  90. }
  91. b = append(b, amountFloat40Bytes...)
  92. // Nonce
  93. nonceBytes, err := tx.Nonce.Bytes()
  94. if err != nil {
  95. return txID, tracerr.Wrap(err)
  96. }
  97. b = append(b, nonceBytes[:]...)
  98. // Fee
  99. b = append(b, byte(tx.Fee))
  100. // calculate hash
  101. h := ethCrypto.Keccak256Hash(b).Bytes()
  102. txID[0] = TxIDPrefixL2Tx
  103. copy(txID[1:], h)
  104. return txID, nil
  105. }
  106. // Tx returns a *Tx from the L2Tx
  107. func (tx *L2Tx) Tx() *Tx {
  108. batchNum := new(BatchNum)
  109. *batchNum = tx.BatchNum
  110. fee := new(FeeSelector)
  111. *fee = tx.Fee
  112. nonce := new(Nonce)
  113. *nonce = tx.Nonce
  114. return &Tx{
  115. IsL1: false,
  116. TxID: tx.TxID,
  117. Type: tx.Type,
  118. Position: tx.Position,
  119. FromIdx: tx.FromIdx,
  120. ToIdx: tx.ToIdx,
  121. TokenID: tx.TokenID,
  122. Amount: tx.Amount,
  123. BatchNum: batchNum,
  124. EthBlockNum: tx.EthBlockNum,
  125. Fee: fee,
  126. Nonce: nonce,
  127. }
  128. }
  129. // PoolL2Tx returns the data structure of PoolL2Tx with the parameters of a
  130. // L2Tx filled
  131. func (tx L2Tx) PoolL2Tx() *PoolL2Tx {
  132. return &PoolL2Tx{
  133. TxID: tx.TxID,
  134. FromIdx: tx.FromIdx,
  135. ToIdx: tx.ToIdx,
  136. TokenID: tx.TokenID,
  137. Amount: tx.Amount,
  138. Fee: tx.Fee,
  139. Nonce: tx.Nonce,
  140. Type: tx.Type,
  141. }
  142. }
  143. // L2TxsToPoolL2Txs returns an array of []*PoolL2Tx from an array of []*L2Tx,
  144. // where the PoolL2Tx only have the parameters of a L2Tx filled.
  145. func L2TxsToPoolL2Txs(txs []L2Tx) []PoolL2Tx {
  146. var r []PoolL2Tx
  147. for _, tx := range txs {
  148. r = append(r, *tx.PoolL2Tx())
  149. }
  150. return r
  151. }
  152. // TxIDsFromL2Txs returns an array of TxID from the []L2Tx
  153. func TxIDsFromL2Txs(txs []L2Tx) []TxID {
  154. txIDs := make([]TxID, len(txs))
  155. for i, tx := range txs {
  156. txIDs[i] = tx.TxID
  157. }
  158. return txIDs
  159. }
  160. // BytesDataAvailability encodes a L2Tx into []byte for the Data Availability
  161. // [ fromIdx | toIdx | amountFloat40 | Fee ]
  162. func (tx L2Tx) BytesDataAvailability(nLevels uint32) ([]byte, error) {
  163. idxLen := nLevels / 8 //nolint:gomnd
  164. b := make([]byte, ((nLevels*2)+40+8)/8) //nolint:gomnd
  165. fromIdxBytes, err := tx.FromIdx.Bytes()
  166. if err != nil {
  167. return nil, tracerr.Wrap(err)
  168. }
  169. copy(b[0:idxLen], fromIdxBytes[6-idxLen:]) // [6-idxLen:] as is BigEndian
  170. toIdxBytes, err := tx.ToIdx.Bytes()
  171. if err != nil {
  172. return nil, tracerr.Wrap(err)
  173. }
  174. copy(b[idxLen:idxLen*2], toIdxBytes[6-idxLen:])
  175. amountFloat40, err := NewFloat40(tx.Amount)
  176. if err != nil {
  177. return nil, tracerr.Wrap(err)
  178. }
  179. amountFloat40Bytes, err := amountFloat40.Bytes()
  180. if err != nil {
  181. return nil, tracerr.Wrap(err)
  182. }
  183. copy(b[idxLen*2:idxLen*2+5], amountFloat40Bytes)
  184. b[idxLen*2+5] = byte(tx.Fee)
  185. return b[:], nil
  186. }
  187. // L2TxFromBytesDataAvailability decodes a L2Tx from []byte (Data Availability)
  188. func L2TxFromBytesDataAvailability(b []byte, nLevels int) (*L2Tx, error) {
  189. idxLen := nLevels / 8 //nolint:gomnd
  190. tx := &L2Tx{}
  191. var err error
  192. var paddedFromIdxBytes [6]byte
  193. copy(paddedFromIdxBytes[6-idxLen:], b[0:idxLen])
  194. tx.FromIdx, err = IdxFromBytes(paddedFromIdxBytes[:])
  195. if err != nil {
  196. return nil, tracerr.Wrap(err)
  197. }
  198. var paddedToIdxBytes [6]byte
  199. copy(paddedToIdxBytes[6-idxLen:6], b[idxLen:idxLen*2])
  200. tx.ToIdx, err = IdxFromBytes(paddedToIdxBytes[:])
  201. if err != nil {
  202. return nil, tracerr.Wrap(err)
  203. }
  204. tx.Amount, err = Float40FromBytes(b[idxLen*2 : idxLen*2+5]).BigInt()
  205. if err != nil {
  206. return nil, tracerr.Wrap(err)
  207. }
  208. tx.Fee = FeeSelector(b[idxLen*2+5])
  209. return tx, nil
  210. }