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.

107 lines
2.9 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. ethCrypto "github.com/ethereum/go-ethereum/crypto"
  4. "github.com/hermeznetwork/hermez-node/common"
  5. "github.com/iden3/go-iden3-crypto/babyjub"
  6. "github.com/jmoiron/sqlx"
  7. )
  8. // CleanL2DB deletes 'tx_pool' and 'account_creation_auth' from the given DB
  9. func CleanL2DB(db *sqlx.DB) {
  10. if _, err := db.Exec("DELETE FROM tx_pool;"); err != nil {
  11. panic(err)
  12. }
  13. if _, err := db.Exec("DELETE FROM account_creation_auth;"); err != nil {
  14. panic(err)
  15. }
  16. }
  17. // GenPoolTxs generates L2 pool txs.
  18. // WARNING: This tx doesn't follow the protocol (signature, txID, ...)
  19. // it's just to test getting/setting from/to the DB.
  20. func GenPoolTxs(n int, tokens []common.Token) []*common.PoolL2Tx {
  21. /*
  22. WARNING: this should be replaced by transaktio
  23. txs := make([]*common.PoolL2Tx, 0, n)
  24. privK := babyjub.NewRandPrivKey()
  25. for i := 256; i < 256+n; i++ {
  26. var state common.PoolL2TxState
  27. //nolint:gomnd
  28. if i%4 == 0 {
  29. state = common.PoolL2TxStatePending
  30. //nolint:gomnd
  31. } else if i%4 == 1 {
  32. state = common.PoolL2TxStateInvalid
  33. //nolint:gomnd
  34. } else if i%4 == 2 {
  35. state = common.PoolL2TxStateForging
  36. //nolint:gomnd
  37. } else if i%4 == 3 {
  38. state = common.PoolL2TxStateForged
  39. }
  40. fee := common.FeeSelector(i % 255) //nolint:gomnd
  41. token := tokens[i%len(tokens)]
  42. tx := &common.PoolL2Tx{
  43. FromIdx: common.Idx(i),
  44. ToIdx: common.Idx(i + 1),
  45. ToEthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
  46. ToBJJ: privK.Public(),
  47. TokenID: token.TokenID,
  48. Amount: big.NewInt(int64(i)),
  49. Fee: fee,
  50. Nonce: common.Nonce(i),
  51. State: state,
  52. Signature: privK.SignPoseidon(big.NewInt(int64(i))).Compress(),
  53. }
  54. var err error
  55. tx, err = common.NewPoolL2Tx(tx)
  56. if err != nil {
  57. panic(err)
  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 = common.Nonce(i)
  68. }
  69. txs = append(txs, tx)
  70. }
  71. return txs
  72. */
  73. return nil
  74. }
  75. // GenAuths generates account creation authorizations
  76. func GenAuths(nAuths int) []*common.AccountCreationAuth {
  77. auths := []*common.AccountCreationAuth{}
  78. for i := 0; i < nAuths; i++ {
  79. // Generate keys
  80. ethPrivK, err := ethCrypto.GenerateKey()
  81. if err != nil {
  82. panic(err)
  83. }
  84. bjjPrivK := babyjub.NewRandPrivKey()
  85. // Generate auth
  86. auth := &common.AccountCreationAuth{
  87. EthAddr: ethCrypto.PubkeyToAddress(ethPrivK.PublicKey),
  88. BJJ: bjjPrivK.Public(),
  89. }
  90. // Sign
  91. h, err := auth.HashToSign()
  92. if err != nil {
  93. panic(err)
  94. }
  95. signature, err := ethCrypto.Sign(h, ethPrivK)
  96. if err != nil {
  97. panic(err)
  98. }
  99. auth.Signature = signature
  100. auths = append(auths, auth)
  101. }
  102. return auths
  103. }