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.6 KiB

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. /*
  25. WARNING: this should be replaced by transaktio
  26. txs := make([]*common.PoolL2Tx, 0, n)
  27. privK := babyjub.NewRandPrivKey()
  28. for i := 256; i < 256+n; i++ {
  29. var state common.PoolL2TxState
  30. //nolint:gomnd
  31. if i%4 == 0 {
  32. state = common.PoolL2TxStatePending
  33. //nolint:gomnd
  34. } else if i%4 == 1 {
  35. state = common.PoolL2TxStateInvalid
  36. //nolint:gomnd
  37. } else if i%4 == 2 {
  38. state = common.PoolL2TxStateForging
  39. //nolint:gomnd
  40. } else if i%4 == 3 {
  41. state = common.PoolL2TxStateForged
  42. }
  43. fee := common.FeeSelector(i % 255) //nolint:gomnd
  44. token := tokens[i%len(tokens)]
  45. tx := &common.PoolL2Tx{
  46. FromIdx: common.Idx(i),
  47. ToIdx: common.Idx(i + 1),
  48. ToEthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
  49. ToBJJ: privK.Public(),
  50. TokenID: token.TokenID,
  51. Amount: big.NewInt(int64(i)),
  52. Fee: fee,
  53. Nonce: common.Nonce(i),
  54. State: state,
  55. Signature: privK.SignPoseidon(big.NewInt(int64(i))).Compress(),
  56. }
  57. var err error
  58. tx, err = common.NewPoolL2Tx(tx)
  59. if err != nil {
  60. panic(err)
  61. }
  62. if i%2 == 0 { // Optional parameters: rq
  63. tx.RqFromIdx = common.Idx(i)
  64. tx.RqToIdx = common.Idx(i + 1)
  65. tx.RqToEthAddr = ethCommon.BigToAddress(big.NewInt(int64(i)))
  66. tx.RqToBJJ = privK.Public()
  67. tx.RqTokenID = common.TokenID(i)
  68. tx.RqAmount = big.NewInt(int64(i))
  69. tx.RqFee = common.FeeSelector(i)
  70. tx.RqNonce = common.Nonce(i)
  71. }
  72. txs = append(txs, tx)
  73. }
  74. return txs
  75. */
  76. return nil
  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. }