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.

190 lines
5.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package common
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "math/big"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/iden3/go-iden3-crypto/babyjub"
  8. )
  9. const (
  10. // L1TxBytesLen is the length of the byte array that represents the L1Tx
  11. L1TxBytesLen = 72
  12. )
  13. // L1Tx is a struct that represents a L1 tx
  14. type L1Tx struct {
  15. // Stored in DB: mandatory fileds
  16. // TxID (12 bytes) for L1Tx is:
  17. // bytes: | 1 | 8 | 2 | 1 |
  18. // values: | type | ToForgeL1TxsNum | Position | 0 (padding) |
  19. // where type:
  20. // - L1UserTx: 0
  21. // - L1CoordinatorTx: 1
  22. TxID TxID
  23. ToForgeL1TxsNum int64 // toForgeL1TxsNum in which the tx was forged / will be forged
  24. Position int
  25. UserOrigin bool // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes
  26. FromIdx Idx // FromIdx is used by L1Tx/Deposit to indicate the Idx receiver of the L1Tx.LoadAmount (deposit)
  27. FromEthAddr ethCommon.Address
  28. FromBJJ *babyjub.PublicKey
  29. ToIdx Idx // ToIdx is ignored in L1Tx/Deposit, but used in the L1Tx/DepositAndTransfer
  30. TokenID TokenID
  31. Amount *big.Int
  32. LoadAmount *big.Int
  33. EthBlockNum int64 // Ethereum Block Number in which this L1Tx was added to the queue
  34. Type TxType
  35. BatchNum *BatchNum
  36. USD *float64
  37. LoadAmountUSD *float64
  38. }
  39. // NewL1Tx returns the given L1Tx with the TxId & Type parameters calculated
  40. // from the L1Tx values
  41. func NewL1Tx(l1Tx *L1Tx) (*L1Tx, error) {
  42. // calculate TxType
  43. var txType TxType
  44. if l1Tx.FromIdx == Idx(0) {
  45. if l1Tx.ToIdx == Idx(0) {
  46. txType = TxTypeCreateAccountDeposit
  47. } else if l1Tx.ToIdx >= IdxUserThreshold {
  48. txType = TxTypeCreateAccountDepositTransfer
  49. } else {
  50. return l1Tx, fmt.Errorf("Can not determine type of L1Tx, invalid ToIdx value: %d", l1Tx.ToIdx)
  51. }
  52. } else if l1Tx.FromIdx >= IdxUserThreshold {
  53. if l1Tx.ToIdx == Idx(0) {
  54. txType = TxTypeDeposit
  55. } else if l1Tx.ToIdx == Idx(1) {
  56. txType = TxTypeExit
  57. } else if l1Tx.ToIdx >= IdxUserThreshold {
  58. if l1Tx.LoadAmount.Int64() == int64(0) {
  59. txType = TxTypeForceTransfer
  60. } else {
  61. txType = TxTypeDepositTransfer
  62. }
  63. } else {
  64. return l1Tx, fmt.Errorf("Can not determine type of L1Tx, invalid ToIdx value: %d", l1Tx.ToIdx)
  65. }
  66. } else {
  67. return l1Tx, fmt.Errorf("Can not determine type of L1Tx, invalid FromIdx value: %d", l1Tx.FromIdx)
  68. }
  69. if l1Tx.Type != "" && l1Tx.Type != txType {
  70. return l1Tx, fmt.Errorf("L1Tx.Type: %s, should be: %s", l1Tx.Type, txType)
  71. }
  72. l1Tx.Type = txType
  73. var txid [TxIDLen]byte
  74. if !l1Tx.UserOrigin {
  75. txid[0] = TxIDPrefixL1CoordTx
  76. }
  77. var toForgeL1TxsNumBytes [8]byte
  78. binary.BigEndian.PutUint64(toForgeL1TxsNumBytes[:], uint64(l1Tx.ToForgeL1TxsNum))
  79. copy(txid[1:9], toForgeL1TxsNumBytes[:])
  80. var positionBytes [2]byte
  81. binary.BigEndian.PutUint16(positionBytes[:], uint16(l1Tx.Position))
  82. copy(txid[9:11], positionBytes[:])
  83. l1Tx.TxID = TxID(txid)
  84. return l1Tx, nil
  85. }
  86. // Tx returns a *Tx from the L1Tx
  87. func (tx *L1Tx) Tx() *Tx {
  88. f := new(big.Float).SetInt(tx.Amount)
  89. amountFloat, _ := f.Float64()
  90. genericTx := &Tx{
  91. IsL1: true,
  92. TxID: tx.TxID,
  93. Type: tx.Type,
  94. Position: tx.Position,
  95. FromIdx: tx.FromIdx,
  96. ToIdx: tx.ToIdx,
  97. Amount: tx.Amount,
  98. AmountFloat: amountFloat,
  99. TokenID: tx.TokenID,
  100. ToForgeL1TxsNum: tx.ToForgeL1TxsNum,
  101. UserOrigin: tx.UserOrigin,
  102. FromEthAddr: tx.FromEthAddr,
  103. FromBJJ: tx.FromBJJ,
  104. LoadAmount: tx.LoadAmount,
  105. EthBlockNum: tx.EthBlockNum,
  106. USD: tx.USD,
  107. LoadAmountUSD: tx.LoadAmountUSD,
  108. }
  109. if tx.LoadAmount != nil {
  110. lf := new(big.Float).SetInt(tx.LoadAmount)
  111. loadAmountFloat, _ := lf.Float64()
  112. genericTx.LoadAmountFloat = &loadAmountFloat
  113. }
  114. return genericTx
  115. }
  116. // Bytes encodes a L1Tx into []byte
  117. func (tx *L1Tx) Bytes(nLevels int) ([]byte, error) {
  118. var b [L1TxBytesLen]byte
  119. copy(b[0:20], tx.FromEthAddr.Bytes())
  120. pkComp := tx.FromBJJ.Compress()
  121. copy(b[20:52], pkComp[:])
  122. fromIdxBytes, err := tx.FromIdx.Bytes()
  123. if err != nil {
  124. return nil, err
  125. }
  126. copy(b[52:58], fromIdxBytes[:])
  127. loadAmountFloat16, err := NewFloat16(tx.LoadAmount)
  128. if err != nil {
  129. return nil, err
  130. }
  131. copy(b[58:60], loadAmountFloat16.Bytes())
  132. amountFloat16, err := NewFloat16(tx.Amount)
  133. if err != nil {
  134. return nil, err
  135. }
  136. copy(b[60:62], amountFloat16.Bytes())
  137. copy(b[62:66], tx.TokenID.Bytes())
  138. toIdxBytes, err := tx.ToIdx.Bytes()
  139. if err != nil {
  140. return nil, err
  141. }
  142. copy(b[66:72], toIdxBytes[:])
  143. return b[:], nil
  144. }
  145. // L1TxFromBytes decodes a L1Tx from []byte
  146. func L1TxFromBytes(b []byte) (*L1Tx, error) {
  147. if len(b) != L1TxBytesLen {
  148. return nil, fmt.Errorf("Can not parse L1Tx bytes, expected length %d, current: %d", 68, len(b))
  149. }
  150. tx := &L1Tx{}
  151. var err error
  152. tx.FromEthAddr = ethCommon.BytesToAddress(b[0:20])
  153. pkCompB := b[20:52]
  154. var pkComp babyjub.PublicKeyComp
  155. copy(pkComp[:], pkCompB)
  156. tx.FromBJJ, err = pkComp.Decompress()
  157. if err != nil {
  158. return nil, err
  159. }
  160. tx.FromIdx, err = IdxFromBytes(b[52:58])
  161. if err != nil {
  162. return nil, err
  163. }
  164. tx.LoadAmount = Float16FromBytes(b[58:60]).BigInt()
  165. tx.Amount = Float16FromBytes(b[60:62]).BigInt()
  166. tx.TokenID, err = TokenIDFromBytes(b[62:66])
  167. if err != nil {
  168. return nil, err
  169. }
  170. tx.ToIdx, err = IdxFromBytes(b[66:72])
  171. if err != nil {
  172. return nil, err
  173. }
  174. return tx, nil
  175. }