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.

160 lines
4.2 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(l2Tx *L2Tx) (*L2Tx, error) {
  24. // calculate TxType
  25. var txType TxType
  26. if l2Tx.ToIdx == Idx(1) {
  27. txType = TxTypeExit
  28. } else if l2Tx.ToIdx >= IdxUserThreshold {
  29. txType = TxTypeTransfer
  30. } else {
  31. return l2Tx, tracerr.Wrap(fmt.Errorf("Can not determine type of L2Tx, invalid ToIdx value: %d", l2Tx.ToIdx))
  32. }
  33. // if TxType!=l2Tx.TxType return error
  34. if l2Tx.Type != "" && l2Tx.Type != txType {
  35. return l2Tx, tracerr.Wrap(fmt.Errorf("L2Tx.Type: %s, should be: %s", l2Tx.Type, txType))
  36. }
  37. l2Tx.Type = txType
  38. var txid [TxIDLen]byte
  39. txid[0] = TxIDPrefixL2Tx
  40. fromIdxBytes, err := l2Tx.FromIdx.Bytes()
  41. if err != nil {
  42. return l2Tx, tracerr.Wrap(err)
  43. }
  44. copy(txid[1:7], fromIdxBytes[:])
  45. nonceBytes, err := l2Tx.Nonce.Bytes()
  46. if err != nil {
  47. return l2Tx, tracerr.Wrap(err)
  48. }
  49. copy(txid[7:12], nonceBytes[:])
  50. l2Tx.TxID = TxID(txid)
  51. return l2Tx, nil
  52. }
  53. // Tx returns a *Tx from the L2Tx
  54. func (tx *L2Tx) Tx() *Tx {
  55. batchNum := new(BatchNum)
  56. *batchNum = tx.BatchNum
  57. fee := new(FeeSelector)
  58. *fee = tx.Fee
  59. nonce := new(Nonce)
  60. *nonce = tx.Nonce
  61. return &Tx{
  62. IsL1: false,
  63. TxID: tx.TxID,
  64. Type: tx.Type,
  65. Position: tx.Position,
  66. FromIdx: tx.FromIdx,
  67. ToIdx: tx.ToIdx,
  68. Amount: tx.Amount,
  69. BatchNum: batchNum,
  70. EthBlockNum: tx.EthBlockNum,
  71. Fee: fee,
  72. Nonce: nonce,
  73. }
  74. }
  75. // PoolL2Tx returns the data structure of PoolL2Tx with the parameters of a
  76. // L2Tx filled
  77. func (tx L2Tx) PoolL2Tx() *PoolL2Tx {
  78. return &PoolL2Tx{
  79. TxID: tx.TxID,
  80. FromIdx: tx.FromIdx,
  81. ToIdx: tx.ToIdx,
  82. Amount: tx.Amount,
  83. Fee: tx.Fee,
  84. Nonce: tx.Nonce,
  85. Type: tx.Type,
  86. }
  87. }
  88. // L2TxsToPoolL2Txs returns an array of []*PoolL2Tx from an array of []*L2Tx,
  89. // where the PoolL2Tx only have the parameters of a L2Tx filled.
  90. func L2TxsToPoolL2Txs(txs []L2Tx) []PoolL2Tx {
  91. var r []PoolL2Tx
  92. for _, tx := range txs {
  93. r = append(r, *tx.PoolL2Tx())
  94. }
  95. return r
  96. }
  97. // BytesDataAvailability encodes a L2Tx into []byte for the Data Availability
  98. func (tx L2Tx) BytesDataAvailability(nLevels uint32) ([]byte, error) {
  99. idxLen := nLevels / 8 //nolint:gomnd
  100. b := make([]byte, ((nLevels*2)+16+8)/8) //nolint:gomnd
  101. fromIdxBytes, err := tx.FromIdx.Bytes()
  102. if err != nil {
  103. return nil, tracerr.Wrap(err)
  104. }
  105. copy(b[0:idxLen], fromIdxBytes[6-idxLen:]) // [6-idxLen:] as is BigEndian
  106. toIdxBytes, err := tx.ToIdx.Bytes()
  107. if err != nil {
  108. return nil, tracerr.Wrap(err)
  109. }
  110. copy(b[idxLen:idxLen*2], toIdxBytes[6-idxLen:])
  111. amountFloat16, err := NewFloat16(tx.Amount)
  112. if err != nil {
  113. return nil, tracerr.Wrap(err)
  114. }
  115. copy(b[idxLen*2:idxLen*2+2], amountFloat16.Bytes())
  116. b[idxLen*2+2] = byte(tx.Fee)
  117. return b[:], nil
  118. }
  119. // L2TxFromBytesDataAvailability decodes a L2Tx from []byte (Data Availability)
  120. func L2TxFromBytesDataAvailability(b []byte, nLevels int) (*L2Tx, error) {
  121. idxLen := nLevels / 8 //nolint:gomnd
  122. tx := &L2Tx{}
  123. var err error
  124. var paddedFromIdxBytes [6]byte
  125. copy(paddedFromIdxBytes[6-idxLen:], b[0:idxLen])
  126. tx.FromIdx, err = IdxFromBytes(paddedFromIdxBytes[:])
  127. if err != nil {
  128. return nil, tracerr.Wrap(err)
  129. }
  130. var paddedToIdxBytes [6]byte
  131. copy(paddedToIdxBytes[6-idxLen:6], b[idxLen:idxLen*2])
  132. tx.ToIdx, err = IdxFromBytes(paddedToIdxBytes[:])
  133. if err != nil {
  134. return nil, tracerr.Wrap(err)
  135. }
  136. tx.Amount = Float16FromBytes(b[idxLen*2 : idxLen*2+2]).BigInt()
  137. tx.Fee = FeeSelector(b[idxLen*2+2])
  138. return tx, nil
  139. }