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.

153 lines
4.1 KiB

  1. package common
  2. import (
  3. "fmt"
  4. "math/big"
  5. )
  6. // L2Tx is a struct that represents an already forged L2 tx
  7. type L2Tx struct {
  8. // Stored in DB: mandatory fileds
  9. TxID TxID `meddler:"id"`
  10. BatchNum BatchNum `meddler:"batch_num"` // batchNum in which this tx was forged.
  11. Position int `meddler:"position"`
  12. FromIdx Idx `meddler:"from_idx"`
  13. ToIdx Idx `meddler:"to_idx"`
  14. Amount *big.Int `meddler:"amount,bigint"`
  15. Fee FeeSelector `meddler:"fee"`
  16. Nonce Nonce `meddler:"nonce"`
  17. Type TxType `meddler:"type"`
  18. EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L2Tx was added to the queue
  19. }
  20. // NewL2Tx returns the given L2Tx with the TxId & Type parameters calculated
  21. // from the L2Tx values
  22. func NewL2Tx(l2Tx *L2Tx) (*L2Tx, error) {
  23. // calculate TxType
  24. var txType TxType
  25. if l2Tx.ToIdx == Idx(1) {
  26. txType = TxTypeExit
  27. } else if l2Tx.ToIdx >= IdxUserThreshold {
  28. txType = TxTypeTransfer
  29. } else {
  30. return l2Tx, fmt.Errorf("Can not determine type of L2Tx, invalid ToIdx value: %d", l2Tx.ToIdx)
  31. }
  32. // if TxType!=l2Tx.TxType return error
  33. if l2Tx.Type != "" && l2Tx.Type != txType {
  34. return l2Tx, fmt.Errorf("L2Tx.Type: %s, should be: %s", l2Tx.Type, txType)
  35. }
  36. l2Tx.Type = txType
  37. var txid [TxIDLen]byte
  38. txid[0] = TxIDPrefixL2Tx
  39. fromIdxBytes, err := l2Tx.FromIdx.Bytes()
  40. if err != nil {
  41. return l2Tx, err
  42. }
  43. copy(txid[1:7], fromIdxBytes[:])
  44. nonceBytes, err := l2Tx.Nonce.Bytes()
  45. if err != nil {
  46. return l2Tx, err
  47. }
  48. copy(txid[7:12], nonceBytes[:])
  49. l2Tx.TxID = TxID(txid)
  50. return l2Tx, nil
  51. }
  52. // Tx returns a *Tx from the L2Tx
  53. func (tx *L2Tx) Tx() *Tx {
  54. batchNum := new(BatchNum)
  55. *batchNum = tx.BatchNum
  56. fee := new(FeeSelector)
  57. *fee = tx.Fee
  58. nonce := new(Nonce)
  59. *nonce = tx.Nonce
  60. return &Tx{
  61. IsL1: false,
  62. TxID: tx.TxID,
  63. Type: tx.Type,
  64. Position: tx.Position,
  65. FromIdx: tx.FromIdx,
  66. ToIdx: tx.ToIdx,
  67. Amount: tx.Amount,
  68. BatchNum: batchNum,
  69. EthBlockNum: tx.EthBlockNum,
  70. Fee: fee,
  71. Nonce: nonce,
  72. }
  73. }
  74. // PoolL2Tx returns the data structure of PoolL2Tx with the parameters of a
  75. // L2Tx filled
  76. func (tx L2Tx) PoolL2Tx() *PoolL2Tx {
  77. return &PoolL2Tx{
  78. TxID: tx.TxID,
  79. FromIdx: tx.FromIdx,
  80. ToIdx: tx.ToIdx,
  81. Amount: tx.Amount,
  82. Fee: tx.Fee,
  83. Nonce: tx.Nonce,
  84. Type: tx.Type,
  85. }
  86. }
  87. // L2TxsToPoolL2Txs returns an array of []*PoolL2Tx from an array of []*L2Tx,
  88. // where the PoolL2Tx only have the parameters of a L2Tx filled.
  89. func L2TxsToPoolL2Txs(txs []L2Tx) []PoolL2Tx {
  90. var r []PoolL2Tx
  91. for _, tx := range txs {
  92. r = append(r, *tx.PoolL2Tx())
  93. }
  94. return r
  95. }
  96. // Bytes encodes a L2Tx into []byte
  97. func (tx *L2Tx) Bytes(nLevels int) ([]byte, error) {
  98. fromIdxNumBytes := nLevels / 8 //nolint:gomnd
  99. toIdxNumBytes := nLevels / 8 //nolint:gomnd
  100. var b []byte
  101. fromIdxBytes, err := tx.FromIdx.Bytes()
  102. if err != nil {
  103. return nil, err
  104. }
  105. b = append(b, fromIdxBytes[6-fromIdxNumBytes:]...)
  106. toIdxBytes, err := tx.ToIdx.Bytes()
  107. if err != nil {
  108. return nil, err
  109. }
  110. b = append(b, toIdxBytes[6-toIdxNumBytes:]...)
  111. amountFloat16, err := NewFloat16(tx.Amount)
  112. if err != nil {
  113. return nil, err
  114. }
  115. b = append(b, amountFloat16.Bytes()...)
  116. b = append(b, byte(tx.Fee))
  117. return b[:], nil
  118. }
  119. // L2TxFromBytes decodes a L1Tx from []byte
  120. func L2TxFromBytes(b []byte, nLevels int) (*L2Tx, error) {
  121. fromIdxNumByte := nLevels / 8 //nolint:gomnd
  122. toIdxNumByte := fromIdxNumByte + nLevels/8 //nolint:gomnd
  123. amountLenBytes := 2
  124. amountNumByte := toIdxNumByte + amountLenBytes
  125. tx := &L2Tx{}
  126. var err error
  127. var paddedFromIdxBytes [6]byte
  128. copy(paddedFromIdxBytes[6-len(b[0:fromIdxNumByte]):], b[0:fromIdxNumByte])
  129. tx.FromIdx, err = IdxFromBytes(paddedFromIdxBytes[:])
  130. if err != nil {
  131. return nil, err
  132. }
  133. var paddedToIdxBytes [6]byte
  134. copy(paddedToIdxBytes[6-len(b[fromIdxNumByte:toIdxNumByte]):6], b[fromIdxNumByte:toIdxNumByte])
  135. tx.ToIdx, err = IdxFromBytes(paddedToIdxBytes[:])
  136. if err != nil {
  137. return nil, err
  138. }
  139. tx.Amount = Float16FromBytes(b[toIdxNumByte:amountNumByte]).BigInt()
  140. tx.Fee = FeeSelector(b[amountNumByte])
  141. return tx, nil
  142. }