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.

92 lines
2.5 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
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. fee := common.FeeSelector(i % 255) //nolint:gomnd
  42. token := tokens[i%len(tokens)]
  43. tx := &common.PoolL2Tx{
  44. FromIdx: common.Idx(i),
  45. ToIdx: common.Idx(i + 1),
  46. ToEthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
  47. ToBJJ: privK.Public(),
  48. TokenID: token.TokenID,
  49. Amount: big.NewInt(int64(i)),
  50. Fee: fee,
  51. Nonce: common.Nonce(i),
  52. State: state,
  53. Signature: privK.SignPoseidon(big.NewInt(int64(i))),
  54. }
  55. var err error
  56. tx, err = common.NewPoolL2Tx(tx)
  57. if err != nil {
  58. panic(err)
  59. }
  60. if i%2 == 0 { // Optional parameters: rq
  61. tx.RqFromIdx = common.Idx(i)
  62. tx.RqToIdx = common.Idx(i + 1)
  63. tx.RqToEthAddr = ethCommon.BigToAddress(big.NewInt(int64(i)))
  64. tx.RqToBJJ = privK.Public()
  65. tx.RqTokenID = common.TokenID(i)
  66. tx.RqAmount = big.NewInt(int64(i))
  67. tx.RqFee = common.FeeSelector(i)
  68. tx.RqNonce = common.Nonce(i)
  69. }
  70. txs = append(txs, tx)
  71. }
  72. return txs
  73. }
  74. // GenAuths generates account creation authorizations
  75. func GenAuths(nAuths int) []*common.AccountCreationAuth {
  76. auths := []*common.AccountCreationAuth{}
  77. for i := 0; i < nAuths; i++ {
  78. privK := babyjub.NewRandPrivKey()
  79. auths = append(auths, &common.AccountCreationAuth{
  80. EthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
  81. BJJ: privK.Public(),
  82. Signature: []byte(strconv.Itoa(i)),
  83. Timestamp: time.Now(),
  84. })
  85. }
  86. return auths
  87. }