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.

110 lines
3.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package test
  2. import (
  3. "math/big"
  4. "strconv"
  5. "time"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/hermeznetwork/hermez-node/common"
  8. "github.com/iden3/go-iden3-crypto/babyjub"
  9. "github.com/jmoiron/sqlx"
  10. )
  11. // CleanL2DB deletes 'tx_pool' and 'account_creation_auth' from the given DB
  12. func CleanL2DB(db *sqlx.DB) {
  13. if _, err := db.Exec("DELETE FROM tx_pool;"); err != nil {
  14. panic(err)
  15. }
  16. if _, err := db.Exec("DELETE FROM account_creation_auth;"); err != nil {
  17. panic(err)
  18. }
  19. }
  20. // GenPoolTxs generates L2 pool txs.
  21. // WARNING: This tx doesn't follow the protocol (signature, txID, ...)
  22. // it's just to test getting/setting from/to the DB.
  23. func GenPoolTxs(n int, tokens []common.Token) []*common.PoolL2Tx {
  24. txs := make([]*common.PoolL2Tx, 0, n)
  25. privK := babyjub.NewRandPrivKey()
  26. for i := 256; i < 256+n; i++ {
  27. var state common.PoolL2TxState
  28. //nolint:gomnd
  29. if i%4 == 0 {
  30. state = common.PoolL2TxStatePending
  31. //nolint:gomnd
  32. } else if i%4 == 1 {
  33. state = common.PoolL2TxStateInvalid
  34. //nolint:gomnd
  35. } else if i%4 == 2 {
  36. state = common.PoolL2TxStateForging
  37. //nolint:gomnd
  38. } else if i%4 == 3 {
  39. state = common.PoolL2TxStateForged
  40. }
  41. f := new(big.Float).SetInt(big.NewInt(int64(i)))
  42. amountF, _ := f.Float64()
  43. var usd, absFee *float64
  44. fee := common.FeeSelector(i % 255) //nolint:gomnd
  45. token := tokens[i%len(tokens)]
  46. if token.USD != nil {
  47. usd = new(float64)
  48. absFee = new(float64)
  49. *usd = *token.USD * amountF
  50. *absFee = fee.Percentage() * *usd
  51. }
  52. tx := &common.PoolL2Tx{
  53. FromIdx: common.Idx(i),
  54. ToIdx: common.Idx(i + 1),
  55. ToEthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
  56. ToBJJ: privK.Public(),
  57. TokenID: token.TokenID,
  58. Amount: big.NewInt(int64(i)),
  59. AmountFloat: amountF,
  60. USD: usd,
  61. Fee: fee,
  62. Nonce: common.Nonce(i),
  63. State: state,
  64. Signature: privK.SignPoseidon(big.NewInt(int64(i))),
  65. Timestamp: time.Now().UTC(),
  66. TokenSymbol: token.Symbol,
  67. AbsoluteFee: absFee,
  68. AbsoluteFeeUpdate: token.USDUpdate,
  69. }
  70. var err error
  71. tx, err = common.NewPoolL2Tx(tx)
  72. if err != nil {
  73. panic(err)
  74. }
  75. if i%2 == 0 { // Optional parameters: rq
  76. tx.RqFromIdx = common.Idx(i)
  77. tx.RqToIdx = common.Idx(i + 1)
  78. tx.RqToEthAddr = ethCommon.BigToAddress(big.NewInt(int64(i)))
  79. tx.RqToBJJ = privK.Public()
  80. tx.RqTokenID = common.TokenID(i)
  81. tx.RqAmount = big.NewInt(int64(i))
  82. tx.RqFee = common.FeeSelector(i)
  83. tx.RqNonce = uint64(i)
  84. }
  85. if i%3 == 0 { // Optional parameters: things that get updated "a posteriori"
  86. tx.BatchNum = 489
  87. }
  88. txs = append(txs, tx)
  89. }
  90. return txs
  91. }
  92. // GenAuths generates account creation authorizations
  93. func GenAuths(nAuths int) []*common.AccountCreationAuth {
  94. auths := []*common.AccountCreationAuth{}
  95. for i := 0; i < nAuths; i++ {
  96. privK := babyjub.NewRandPrivKey()
  97. auths = append(auths, &common.AccountCreationAuth{
  98. EthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
  99. BJJ: privK.Public(),
  100. Signature: []byte(strconv.Itoa(i)),
  101. Timestamp: time.Now(),
  102. })
  103. }
  104. return auths
  105. }