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.

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