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.

169 lines
4.2 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. fromIdx := new(Idx)
  65. *fromIdx = tx.FromIdx
  66. toIdx := new(Idx)
  67. *toIdx = tx.ToIdx
  68. return &Tx{
  69. IsL1: false,
  70. TxID: tx.TxID,
  71. Type: tx.Type,
  72. Position: tx.Position,
  73. FromIdx: fromIdx,
  74. ToIdx: toIdx,
  75. Amount: tx.Amount,
  76. USD: tx.USD,
  77. AmountFloat: amountFloat,
  78. BatchNum: batchNum,
  79. EthBlockNum: tx.EthBlockNum,
  80. Fee: fee,
  81. FeeUSD: tx.FeeUSD,
  82. Nonce: nonce,
  83. }
  84. }
  85. // PoolL2Tx returns the data structure of PoolL2Tx with the parameters of a
  86. // L2Tx filled
  87. func (tx *L2Tx) PoolL2Tx() *PoolL2Tx {
  88. batchNum := new(BatchNum)
  89. *batchNum = tx.BatchNum
  90. toIdx := new(Idx)
  91. *toIdx = tx.ToIdx
  92. return &PoolL2Tx{
  93. TxID: tx.TxID,
  94. BatchNum: batchNum,
  95. FromIdx: tx.FromIdx,
  96. ToIdx: toIdx,
  97. Amount: tx.Amount,
  98. Fee: tx.Fee,
  99. Nonce: tx.Nonce,
  100. Type: tx.Type,
  101. }
  102. }
  103. // L2TxsToPoolL2Txs returns an array of []*PoolL2Tx from an array of []*L2Tx,
  104. // where the PoolL2Tx only have the parameters of a L2Tx filled.
  105. func L2TxsToPoolL2Txs(txs []*L2Tx) []PoolL2Tx {
  106. var r []PoolL2Tx
  107. for _, tx := range txs {
  108. r = append(r, *tx.PoolL2Tx())
  109. }
  110. return r
  111. }
  112. // Bytes encodes a L2Tx into []byte
  113. func (tx *L2Tx) Bytes(nLevels int) ([]byte, error) {
  114. fromIdxNumBytes := nLevels / 8 //nolint:gomnd
  115. toIdxNumBytes := nLevels / 8 //nolint:gomnd
  116. var b []byte
  117. fromIdxBytes, err := tx.FromIdx.Bytes()
  118. if err != nil {
  119. return nil, err
  120. }
  121. b = append(b, fromIdxBytes[6-fromIdxNumBytes:]...)
  122. toIdxBytes, err := tx.ToIdx.Bytes()
  123. if err != nil {
  124. return nil, err
  125. }
  126. b = append(b, toIdxBytes[6-toIdxNumBytes:]...)
  127. amountFloat16, err := NewFloat16(tx.Amount)
  128. if err != nil {
  129. return nil, err
  130. }
  131. b = append(b, amountFloat16.Bytes()...)
  132. b = append(b, byte(tx.Fee))
  133. return b[:], nil
  134. }
  135. // L2TxFromBytes decodes a L1Tx from []byte
  136. func L2TxFromBytes(b []byte, nLevels int) (*L2Tx, error) {
  137. fromIdxNumByte := nLevels / 8 //nolint:gomnd
  138. toIdxNumByte := fromIdxNumByte + nLevels/8 //nolint:gomnd
  139. amountLenBytes := 2
  140. amountNumByte := toIdxNumByte + amountLenBytes
  141. tx := &L2Tx{}
  142. var err error
  143. var paddedFromIdxBytes [6]byte
  144. copy(paddedFromIdxBytes[6-len(b[0:fromIdxNumByte]):], b[0:fromIdxNumByte])
  145. tx.FromIdx, err = IdxFromBytes(paddedFromIdxBytes[:])
  146. if err != nil {
  147. return nil, err
  148. }
  149. var paddedToIdxBytes [6]byte
  150. copy(paddedToIdxBytes[6-len(b[fromIdxNumByte:toIdxNumByte]):6], b[fromIdxNumByte:toIdxNumByte])
  151. tx.ToIdx, err = IdxFromBytes(paddedToIdxBytes[:])
  152. if err != nil {
  153. return nil, err
  154. }
  155. tx.Amount = Float16FromBytes(b[toIdxNumByte:amountNumByte]).BigInt()
  156. tx.Fee = FeeSelector(b[amountNumByte])
  157. return tx, nil
  158. }