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.

204 lines
5.7 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 == nil {
  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. var toForge uint64 = 0
  79. if l1Tx.ToForgeL1TxsNum != nil {
  80. toForge = uint64(*l1Tx.ToForgeL1TxsNum)
  81. }
  82. binary.BigEndian.PutUint64(toForgeL1TxsNumBytes[:], toForge)
  83. copy(txid[1:9], toForgeL1TxsNumBytes[:])
  84. var positionBytes [2]byte
  85. binary.BigEndian.PutUint16(positionBytes[:], uint16(l1Tx.Position))
  86. copy(txid[9:11], positionBytes[:])
  87. l1Tx.TxID = TxID(txid)
  88. return l1Tx, nil
  89. }
  90. // Tx returns a *Tx from the L1Tx
  91. func (tx *L1Tx) Tx() *Tx {
  92. f := new(big.Float).SetInt(tx.Amount)
  93. amountFloat, _ := f.Float64()
  94. userOrigin := new(bool)
  95. *userOrigin = tx.UserOrigin
  96. fromEthAddr := new(ethCommon.Address)
  97. *fromEthAddr = tx.FromEthAddr
  98. toIdx := new(Idx)
  99. *toIdx = tx.ToIdx
  100. genericTx := &Tx{
  101. IsL1: true,
  102. TxID: tx.TxID,
  103. Type: tx.Type,
  104. Position: tx.Position,
  105. FromIdx: tx.FromIdx,
  106. ToIdx: toIdx,
  107. Amount: tx.Amount,
  108. AmountFloat: amountFloat,
  109. TokenID: tx.TokenID,
  110. ToForgeL1TxsNum: tx.ToForgeL1TxsNum,
  111. UserOrigin: userOrigin,
  112. FromEthAddr: fromEthAddr,
  113. FromBJJ: tx.FromBJJ,
  114. LoadAmount: tx.LoadAmount,
  115. EthBlockNum: tx.EthBlockNum,
  116. USD: tx.USD,
  117. LoadAmountUSD: tx.LoadAmountUSD,
  118. }
  119. if tx.LoadAmount != nil {
  120. lf := new(big.Float).SetInt(tx.LoadAmount)
  121. loadAmountFloat, _ := lf.Float64()
  122. genericTx.LoadAmountFloat = &loadAmountFloat
  123. }
  124. return genericTx
  125. }
  126. // Bytes encodes a L1Tx into []byte
  127. func (tx *L1Tx) Bytes(nLevels int) ([]byte, error) {
  128. var b [L1TxBytesLen]byte
  129. copy(b[0:20], tx.FromEthAddr.Bytes())
  130. pkComp := tx.FromBJJ.Compress()
  131. copy(b[20:52], pkComp[:])
  132. fromIdxBytes, err := tx.FromIdx.Bytes()
  133. if err != nil {
  134. return nil, err
  135. }
  136. copy(b[52:58], fromIdxBytes[:])
  137. loadAmountFloat16, err := NewFloat16(tx.LoadAmount)
  138. if err != nil {
  139. return nil, err
  140. }
  141. copy(b[58:60], loadAmountFloat16.Bytes())
  142. amountFloat16, err := NewFloat16(tx.Amount)
  143. if err != nil {
  144. return nil, err
  145. }
  146. copy(b[60:62], amountFloat16.Bytes())
  147. copy(b[62:66], tx.TokenID.Bytes())
  148. toIdxBytes, err := tx.ToIdx.Bytes()
  149. if err != nil {
  150. return nil, err
  151. }
  152. copy(b[66:72], toIdxBytes[:])
  153. return b[:], nil
  154. }
  155. // L1TxFromBytes decodes a L1Tx from []byte
  156. func L1TxFromBytes(b []byte) (*L1Tx, error) {
  157. if len(b) != L1TxBytesLen {
  158. return nil, fmt.Errorf("Can not parse L1Tx bytes, expected length %d, current: %d", 68, len(b))
  159. }
  160. tx := &L1Tx{}
  161. var err error
  162. tx.FromEthAddr = ethCommon.BytesToAddress(b[0:20])
  163. pkCompB := b[20:52]
  164. var pkComp babyjub.PublicKeyComp
  165. copy(pkComp[:], pkCompB)
  166. tx.FromBJJ, err = pkComp.Decompress()
  167. if err != nil {
  168. return nil, err
  169. }
  170. fromIdx, err := IdxFromBytes(b[52:58])
  171. if err != nil {
  172. return nil, err
  173. }
  174. if fromIdx != 0 {
  175. tx.FromIdx = new(Idx)
  176. *tx.FromIdx = fromIdx
  177. }
  178. tx.LoadAmount = Float16FromBytes(b[58:60]).BigInt()
  179. tx.Amount = Float16FromBytes(b[60:62]).BigInt()
  180. tx.TokenID, err = TokenIDFromBytes(b[62:66])
  181. if err != nil {
  182. return nil, err
  183. }
  184. tx.ToIdx, err = IdxFromBytes(b[66:72])
  185. if err != nil {
  186. return nil, err
  187. }
  188. return tx, nil
  189. }