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.

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