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.

158 lines
3.9 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. idxLen := nLevels / 8 //nolint:gomnd
  99. b := make([]byte, ((nLevels*2)+16+8)/8) //nolint:gomnd
  100. fromIdxBytes, err := tx.FromIdx.Bytes()
  101. if err != nil {
  102. return nil, err
  103. }
  104. copy(b[0:idxLen], fromIdxBytes[6-idxLen:]) // [6-idxLen:] as is BigEndian
  105. toIdxBytes, err := tx.ToIdx.Bytes()
  106. if err != nil {
  107. return nil, err
  108. }
  109. copy(b[idxLen:idxLen*2], toIdxBytes[6-idxLen:])
  110. amountFloat16, err := NewFloat16(tx.Amount)
  111. if err != nil {
  112. return nil, err
  113. }
  114. copy(b[idxLen*2:idxLen*2+2], amountFloat16.Bytes())
  115. b[idxLen*2+2] = byte(tx.Fee)
  116. return b[:], nil
  117. }
  118. // L2TxFromBytes decodes a L1Tx from []byte
  119. func L2TxFromBytes(b []byte, nLevels int) (*L2Tx, error) {
  120. idxLen := nLevels / 8 //nolint:gomnd
  121. tx := &L2Tx{}
  122. var err error
  123. var paddedFromIdxBytes [6]byte
  124. copy(paddedFromIdxBytes[6-idxLen:], b[0:idxLen])
  125. tx.FromIdx, err = IdxFromBytes(paddedFromIdxBytes[:])
  126. if err != nil {
  127. return nil, err
  128. }
  129. var paddedToIdxBytes [6]byte
  130. copy(paddedToIdxBytes[6-idxLen:6], b[idxLen:idxLen*2])
  131. tx.ToIdx, err = IdxFromBytes(paddedToIdxBytes[:])
  132. if err != nil {
  133. return nil, err
  134. }
  135. tx.Amount = Float16FromBytes(b[idxLen*2 : idxLen*2+2]).BigInt()
  136. tx.Fee = FeeSelector(b[idxLen*2+2])
  137. return tx, nil
  138. }