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.

96 lines
2.7 KiB

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) []*common.PoolL2Tx {
  24. txs := make([]*common.PoolL2Tx, 0, n)
  25. privK := babyjub.NewRandPrivKey()
  26. for i := 0; i < 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. tx := &common.PoolL2Tx{
  44. TxID: common.TxID(common.Hash([]byte(strconv.Itoa(i)))),
  45. FromIdx: common.Idx(i),
  46. ToIdx: common.Idx(i + 1),
  47. ToEthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
  48. ToBJJ: privK.Public(),
  49. TokenID: common.TokenID(i),
  50. Amount: big.NewInt(int64(i)),
  51. AmountFloat: amountF,
  52. //nolint:gomnd
  53. Fee: common.FeeSelector(i % 255),
  54. Nonce: common.Nonce(i),
  55. State: state,
  56. Signature: privK.SignPoseidon(big.NewInt(int64(i))),
  57. Timestamp: time.Now().UTC(),
  58. }
  59. if i%2 == 0 { // Optional parameters: rq
  60. tx.RqFromIdx = common.Idx(i)
  61. tx.RqToIdx = common.Idx(i + 1)
  62. tx.RqToEthAddr = ethCommon.BigToAddress(big.NewInt(int64(i)))
  63. tx.RqToBJJ = privK.Public()
  64. tx.RqTokenID = common.TokenID(i)
  65. tx.RqAmount = big.NewInt(int64(i))
  66. tx.RqFee = common.FeeSelector(i)
  67. tx.RqNonce = uint64(i)
  68. }
  69. if i%3 == 0 { // Optional parameters: things that get updated "a posteriori"
  70. tx.BatchNum = 489
  71. tx.AbsoluteFee = 39.12345
  72. tx.AbsoluteFeeUpdate = time.Now().UTC()
  73. }
  74. txs = append(txs, tx)
  75. }
  76. return txs
  77. }
  78. // GenAuths generates account creation authorizations
  79. func GenAuths(nAuths int) []*common.AccountCreationAuth {
  80. auths := []*common.AccountCreationAuth{}
  81. for i := 0; i < nAuths; i++ {
  82. privK := babyjub.NewRandPrivKey()
  83. auths = append(auths, &common.AccountCreationAuth{
  84. EthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
  85. BJJ: privK.Public(),
  86. Signature: []byte(strconv.Itoa(i)),
  87. Timestamp: time.Now(),
  88. })
  89. }
  90. return auths
  91. }