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.

188 lines
4.9 KiB

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