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.

179 lines
4.7 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, 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, 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. // BytesDataAvailability encodes a L2Tx into []byte for the Data Availability
  116. func (tx L2Tx) BytesDataAvailability(nLevels uint32) ([]byte, error) {
  117. idxLen := nLevels / 8 //nolint:gomnd
  118. b := make([]byte, ((nLevels*2)+16+8)/8) //nolint:gomnd
  119. fromIdxBytes, err := tx.FromIdx.Bytes()
  120. if err != nil {
  121. return nil, tracerr.Wrap(err)
  122. }
  123. copy(b[0:idxLen], fromIdxBytes[6-idxLen:]) // [6-idxLen:] as is BigEndian
  124. toIdxBytes, err := tx.ToIdx.Bytes()
  125. if err != nil {
  126. return nil, tracerr.Wrap(err)
  127. }
  128. copy(b[idxLen:idxLen*2], toIdxBytes[6-idxLen:])
  129. amountFloat16, err := NewFloat16(tx.Amount)
  130. if err != nil {
  131. return nil, tracerr.Wrap(err)
  132. }
  133. copy(b[idxLen*2:idxLen*2+2], amountFloat16.Bytes())
  134. b[idxLen*2+2] = byte(tx.Fee)
  135. return b[:], nil
  136. }
  137. // L2TxFromBytesDataAvailability decodes a L2Tx from []byte (Data Availability)
  138. func L2TxFromBytesDataAvailability(b []byte, nLevels int) (*L2Tx, error) {
  139. idxLen := nLevels / 8 //nolint:gomnd
  140. tx := &L2Tx{}
  141. var err error
  142. var paddedFromIdxBytes [6]byte
  143. copy(paddedFromIdxBytes[6-idxLen:], b[0:idxLen])
  144. tx.FromIdx, err = IdxFromBytes(paddedFromIdxBytes[:])
  145. if err != nil {
  146. return nil, tracerr.Wrap(err)
  147. }
  148. var paddedToIdxBytes [6]byte
  149. copy(paddedToIdxBytes[6-idxLen:6], b[idxLen:idxLen*2])
  150. tx.ToIdx, err = IdxFromBytes(paddedToIdxBytes[:])
  151. if err != nil {
  152. return nil, tracerr.Wrap(err)
  153. }
  154. tx.Amount = Float16FromBytes(b[idxLen*2 : idxLen*2+2]).BigInt()
  155. tx.Fee = FeeSelector(b[idxLen*2+2])
  156. return tx, nil
  157. }