From 1d489ac612d01d04887635f81431ae09167261cf Mon Sep 17 00:00:00 2001 From: tclemos Date: Tue, 21 Mar 2023 13:32:15 -0300 Subject: [PATCH 1/7] allow pool to be blocked --- db/migrations/pool/0006.sql | 8 + pool/errors.go | 3 + pool/interfaces.go | 1 + pool/pgpoolstorage/pgpoolstorage.go | 16 ++ pool/pool.go | 8 + pool/pool_test.go | 385 +++++++++++++--------------- 6 files changed, 217 insertions(+), 204 deletions(-) create mode 100644 db/migrations/pool/0006.sql diff --git a/db/migrations/pool/0006.sql b/db/migrations/pool/0006.sql new file mode 100644 index 0000000000..7b1ad411d6 --- /dev/null +++ b/db/migrations/pool/0006.sql @@ -0,0 +1,8 @@ +-- +migrate Up +CREATE TABLE pool.blocked +( + addr varchar NOT NULL PRIMARY KEY +); + +-- +migrate Down +DROP TABLE pool.blocked; \ No newline at end of file diff --git a/pool/errors.go b/pool/errors.go index 9d11d80ad3..901c4840c1 100644 --- a/pool/errors.go +++ b/pool/errors.go @@ -27,6 +27,9 @@ var ( // ErrInvalidSender is returned if the transaction contains an invalid signature. ErrInvalidSender = errors.New("invalid sender") + // ErrInvalidSender is returned if the transaction is sent by a blocked account. + ErrBlockedSender = errors.New("blocked sender") + // ErrNonceTooLow is returned if the nonce of a transaction is lower than the // one present in the local chain. ErrNonceTooLow = errors.New("nonce too low") diff --git a/pool/interfaces.go b/pool/interfaces.go index 5866ec0597..fd5387f5d6 100644 --- a/pool/interfaces.go +++ b/pool/interfaces.go @@ -32,6 +32,7 @@ type storage interface { GetTxZkCountersByHash(ctx context.Context, hash common.Hash) (*state.ZKCounters, error) DeleteTransactionByHash(ctx context.Context, hash common.Hash) error MarkWIPTxsAsPending(ctx context.Context) error + IsAddressBlocked(ctx context.Context, address common.Address) (bool, error) } type stateInterface interface { diff --git a/pool/pgpoolstorage/pgpoolstorage.go b/pool/pgpoolstorage/pgpoolstorage.go index 8ff0971d68..188008e940 100644 --- a/pool/pgpoolstorage/pgpoolstorage.go +++ b/pool/pgpoolstorage/pgpoolstorage.go @@ -622,3 +622,19 @@ func (p *PostgresPoolStorage) UpdateTxWIPStatus(ctx context.Context, hash common } return nil } + +// IsAddressBlocked check if an address is added to the blocked table +func (p *PostgresPoolStorage) IsAddressBlocked(ctx context.Context, address common.Address) (bool, error) { + sql := `SELECT addr FROM pool.blocked WHERE addr = $1` + var addr string + err := p.db.QueryRow(ctx, sql, address.String()).Scan(&addr) + if err != nil { + if errors.Is(err, pgx.ErrNoRows) { + return false, nil + } else { + return false, err + } + } + + return true, nil +} diff --git a/pool/pool.go b/pool/pool.go index 57fa96b1eb..ca4762ff87 100644 --- a/pool/pool.go +++ b/pool/pool.go @@ -197,6 +197,14 @@ func (p *Pool) validateTx(ctx context.Context, tx types.Transaction) error { return ErrInvalidSender } + // check if sender is blocked + blocked, err := p.storage.IsAddressBlocked(ctx, from) + if err != nil { + return err + } else if blocked { + return ErrBlockedSender + } + lastL2BlockNumber, err := p.state.GetLastL2BlockNumber(ctx, nil) if err != nil { return err diff --git a/pool/pool_test.go b/pool/pool_test.go index 3f582819d5..f1e3da057e 100644 --- a/pool/pool_test.go +++ b/pool/pool_test.go @@ -63,18 +63,15 @@ func TestMain(m *testing.M) { } func Test_AddTx(t *testing.T) { - initOrResetDB() + initOrResetDB(t) stateSqlDB, err := db.NewSQLDB(stateDBCfg) - if err != nil { - panic(err) - } + require.NoError(t, err) defer stateSqlDB.Close() //nolint:gosec,errcheck poolSqlDB, err := db.NewSQLDB(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) + defer poolSqlDB.Close() //nolint:gosec,errcheck st := newState(stateSqlDB) @@ -102,9 +99,7 @@ func Test_AddTx(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) const chainID = 2576980377 cfg := pool.Config{ @@ -114,31 +109,24 @@ func Test_AddTx(t *testing.T) { txRLPHash := "0xf86e8212658082520894fd8b27a263e19f0e9592180e61f0f8c9dfeb1ff6880de0b6b3a764000080850133333355a01eac4c2defc7ed767ae36bbd02613c581b8fb87d0e4f579c9ee3a7cfdb16faa7a043ce30f43d952b9d034cf8f04fecb631192a5dbc7ee2a47f1f49c0d022a8849d" b, err := hex.DecodeHex(txRLPHash) - if err != nil { - t.Error(err) - } + require.NoError(t, err) + tx := new(types.Transaction) tx.UnmarshalBinary(b) //nolint:gosec,errcheck err = p.AddTx(ctx, *tx, "") - if err != nil { - t.Error(err) - } + require.NoError(t, err) rows, err := poolSqlDB.Query(ctx, "SELECT hash, encoded, decoded, status, used_steps FROM pool.transaction") + require.NoError(t, err) defer rows.Close() // nolint:staticcheck - if err != nil { - t.Error(err) - } c := 0 for rows.Next() { var hash, encoded, decoded, status string var usedSteps int err := rows.Scan(&hash, &encoded, &decoded, &status, &usedSteps) - if err != nil { - t.Error(err) - } + require.NoError(t, err) b, _ := tx.MarshalJSON() assert.Equal(t, "0xa3cff5abdf47d4feb8204a45c0a8c58fc9b9bb9b29c6588c1d206b746815e9cc", hash, "invalid hash") @@ -153,18 +141,14 @@ func Test_AddTx(t *testing.T) { } func Test_AddPreEIP155Tx(t *testing.T) { - initOrResetDB() + initOrResetDB(t) stateSqlDB, err := db.NewSQLDB(stateDBCfg) - if err != nil { - panic(err) - } + require.NoError(t, err) defer stateSqlDB.Close() //nolint:gosec,errcheck poolSqlDB, err := db.NewSQLDB(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) defer poolSqlDB.Close() //nolint:gosec,errcheck st := newState(stateSqlDB) @@ -197,9 +181,7 @@ func Test_AddPreEIP155Tx(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) const chainID = 2576980377 cfg := pool.Config{ @@ -219,8 +201,8 @@ func Test_AddPreEIP155Tx(t *testing.T) { require.NoError(t, err) rows, err := poolSqlDB.Query(ctx, "SELECT hash, encoded, decoded, status FROM pool.transaction") - defer rows.Close() // nolint:staticcheck require.NoError(t, err) + defer rows.Close() // nolint:staticcheck c := 0 for rows.Next() { @@ -245,12 +227,10 @@ func Test_AddPreEIP155Tx(t *testing.T) { } func Test_GetPendingTxs(t *testing.T) { - initOrResetDB() + initOrResetDB(t) stateSqlDB, err := db.NewSQLDB(stateDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) defer stateSqlDB.Close() //nolint:gosec,errcheck st := newState(stateSqlDB) @@ -269,9 +249,7 @@ func Test_GetPendingTxs(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) cfg := pool.Config{ FreeClaimGasLimit: 150000, @@ -292,15 +270,12 @@ func Test_GetPendingTxs(t *testing.T) { tx := types.NewTransaction(uint64(i), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) signedTx, err := auth.Signer(auth.From, tx) require.NoError(t, err) - if err := p.AddTx(ctx, *signedTx, ""); err != nil { - t.Error(err) - } + err = p.AddTx(ctx, *signedTx, "") + require.NoError(t, err) } txs, err := p.GetPendingTxs(ctx, false, limit) - if err != nil { - t.Error(err) - } + require.NoError(t, err) assert.Equal(t, limit, len(txs)) @@ -310,12 +285,10 @@ func Test_GetPendingTxs(t *testing.T) { } func Test_GetPendingTxsZeroPassed(t *testing.T) { - initOrResetDB() + initOrResetDB(t) stateSqlDB, err := db.NewSQLDB(stateDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) defer stateSqlDB.Close() //nolint:gosec,errcheck st := newState(stateSqlDB) @@ -334,9 +307,7 @@ func Test_GetPendingTxsZeroPassed(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) cfg := pool.Config{ FreeClaimGasLimit: 150000, } @@ -356,15 +327,12 @@ func Test_GetPendingTxsZeroPassed(t *testing.T) { tx := types.NewTransaction(uint64(i), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) signedTx, err := auth.Signer(auth.From, tx) require.NoError(t, err) - if err := p.AddTx(ctx, *signedTx, ""); err != nil { - t.Error(err) - } + err = p.AddTx(ctx, *signedTx, "") + require.NoError(t, err) } txs, err := p.GetPendingTxs(ctx, false, limit) - if err != nil { - t.Error(err) - } + require.NoError(t, err) assert.Equal(t, txsCount, len(txs)) @@ -375,12 +343,10 @@ func Test_GetPendingTxsZeroPassed(t *testing.T) { func Test_GetTopPendingTxByProfitabilityAndZkCounters(t *testing.T) { ctx := context.Background() - initOrResetDB() + initOrResetDB(t) stateSqlDB, err := db.NewSQLDB(stateDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) defer stateSqlDB.Close() st := newState(stateSqlDB) @@ -398,9 +364,7 @@ func Test_GetTopPendingTxByProfitabilityAndZkCounters(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) cfg := pool.Config{ FreeClaimGasLimit: 150000, } @@ -419,9 +383,8 @@ func Test_GetTopPendingTxByProfitabilityAndZkCounters(t *testing.T) { tx := types.NewTransaction(uint64(i), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10+int64(i)), []byte{}) signedTx, err := auth.Signer(auth.From, tx) require.NoError(t, err) - if err := p.AddTx(ctx, *signedTx, ""); err != nil { - t.Error(err) - } + err = p.AddTx(ctx, *signedTx, "") + require.NoError(t, err) } txs, err := p.GetTxs(ctx, pool.TxStatusPending, false, 1, 10) @@ -433,18 +396,14 @@ func Test_GetTopPendingTxByProfitabilityAndZkCounters(t *testing.T) { func Test_UpdateTxsStatus(t *testing.T) { ctx := context.Background() - initOrResetDB() + initOrResetDB(t) stateSqlDB, err := db.NewSQLDB(stateDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) defer stateSqlDB.Close() //nolint:gosec,errcheck poolSqlDB, err := db.NewSQLDB(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) defer poolSqlDB.Close() //nolint:gosec,errcheck st := newState(stateSqlDB) @@ -462,9 +421,7 @@ func Test_UpdateTxsStatus(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) cfg := pool.Config{ FreeClaimGasLimit: 150000, } @@ -479,45 +436,35 @@ func Test_UpdateTxsStatus(t *testing.T) { tx1 := types.NewTransaction(uint64(0), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) signedTx1, err := auth.Signer(auth.From, tx1) require.NoError(t, err) - if err := p.AddTx(ctx, *signedTx1, ""); err != nil { - t.Error(err) - } + err = p.AddTx(ctx, *signedTx1, "") + require.NoError(t, err) tx2 := types.NewTransaction(uint64(1), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) signedTx2, err := auth.Signer(auth.From, tx2) require.NoError(t, err) - if err := p.AddTx(ctx, *signedTx2, ""); err != nil { - t.Error(err) - } + err = p.AddTx(ctx, *signedTx2, "") + require.NoError(t, err) err = p.UpdateTxsStatus(ctx, []string{signedTx1.Hash().String(), signedTx2.Hash().String()}, pool.TxStatusInvalid) - if err != nil { - t.Error(err) - } + require.NoError(t, err) var count int err = poolSqlDB.QueryRow(ctx, "SELECT COUNT(*) FROM pool.transaction WHERE status = $1", pool.TxStatusInvalid).Scan(&count) - if err != nil { - t.Error(err) - } + require.NoError(t, err) assert.Equal(t, 2, count) } func Test_UpdateTxStatus(t *testing.T) { ctx := context.Background() - initOrResetDB() + initOrResetDB(t) stateSqlDB, err := db.NewSQLDB(stateDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) defer stateSqlDB.Close() //nolint:gosec,errcheck poolSqlDB, err := db.NewSQLDB(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) defer poolSqlDB.Close() //nolint:gosec,errcheck st := newState(stateSqlDB) @@ -535,9 +482,7 @@ func Test_UpdateTxStatus(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) cfg := pool.Config{ FreeClaimGasLimit: 150000, } @@ -552,70 +497,54 @@ func Test_UpdateTxStatus(t *testing.T) { tx := types.NewTransaction(uint64(0), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) signedTx, err := auth.Signer(auth.From, tx) require.NoError(t, err) - if err := p.AddTx(ctx, *signedTx, ""); err != nil { - t.Error(err) - } + err = p.AddTx(ctx, *signedTx, "") + require.NoError(t, err) err = p.UpdateTxStatus(ctx, signedTx.Hash(), pool.TxStatusInvalid, false) - if err != nil { - t.Error(err) - } + require.NoError(t, err) rows, err := poolSqlDB.Query(ctx, "SELECT status FROM pool.transaction WHERE hash = $1", signedTx.Hash().Hex()) + require.NoError(t, err) defer rows.Close() // nolint:staticcheck - if err != nil { - t.Error(err) - } var state string rows.Next() - if err := rows.Scan(&state); err != nil { - t.Error(err) - } + err = rows.Scan(&state) + require.NoError(t, err) assert.Equal(t, pool.TxStatusInvalid, pool.TxStatus(state)) } func Test_SetAndGetGasPrice(t *testing.T) { - initOrResetDB() + initOrResetDB(t) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) cfg := pool.Config{ FreeClaimGasLimit: 150000, } p := pool.NewPool(cfg, s, nil, common.Address{}, chainID.Uint64()) nBig, err := rand.Int(rand.Reader, big.NewInt(0).SetUint64(math.MaxUint64)) - if err != nil { - t.Error(err) - } + require.NoError(t, err) expectedGasPrice := nBig.Uint64() ctx := context.Background() err = p.SetGasPrice(ctx, expectedGasPrice) - if err != nil { - t.Error(err) - } + require.NoError(t, err) gasPrice, err := p.GetGasPrice(ctx) - if err != nil { - t.Error(err) - } + require.NoError(t, err) assert.Equal(t, expectedGasPrice, gasPrice) } func TestGetPendingTxSince(t *testing.T) { - initOrResetDB() + initOrResetDB(t) stateSqlDB, err := db.NewSQLDB(stateDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) defer stateSqlDB.Close() //nolint:gosec,errcheck st := newState(stateSqlDB) @@ -634,9 +563,7 @@ func TestGetPendingTxSince(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) cfg := pool.Config{ FreeClaimGasLimit: 150000, } @@ -660,26 +587,21 @@ func TestGetPendingTxSince(t *testing.T) { signedTx, err := auth.Signer(auth.From, tx) require.NoError(t, err) txsAddedTime = append(txsAddedTime, time.Now()) - if err := p.AddTx(ctx, *signedTx, ""); err != nil { - t.Error(err) - } + err = p.AddTx(ctx, *signedTx, "") + require.NoError(t, err) txsAddedHashes = append(txsAddedHashes, signedTx.Hash()) time.Sleep(1 * time.Second) } txHashes, err := p.GetPendingTxHashesSince(ctx, timeBeforeTxs) - if err != nil { - t.Error(err) - } + require.NoError(t, err) assert.Equal(t, txsCount, len(txHashes)) for i, txHash := range txHashes { assert.Equal(t, txHash.Hex(), txsAddedHashes[i].Hex()) } txHashes, err = p.GetPendingTxHashesSince(ctx, txsAddedTime[5]) - if err != nil { - t.Error(err) - } + require.NoError(t, err) assert.Equal(t, 5, len(txHashes)) assert.Equal(t, txHashes[0].Hex(), txsAddedHashes[5].Hex()) assert.Equal(t, txHashes[1].Hex(), txsAddedHashes[6].Hex()) @@ -688,40 +610,30 @@ func TestGetPendingTxSince(t *testing.T) { assert.Equal(t, txHashes[4].Hex(), txsAddedHashes[9].Hex()) txHashes, err = p.GetPendingTxHashesSince(ctx, txsAddedTime[8]) - if err != nil { - t.Error(err) - } + require.NoError(t, err) assert.Equal(t, 2, len(txHashes)) assert.Equal(t, txHashes[0].Hex(), txsAddedHashes[8].Hex()) assert.Equal(t, txHashes[1].Hex(), txsAddedHashes[9].Hex()) txHashes, err = p.GetPendingTxHashesSince(ctx, txsAddedTime[9]) - if err != nil { - t.Error(err) - } + require.NoError(t, err) assert.Equal(t, 1, len(txHashes)) assert.Equal(t, txHashes[0].Hex(), txsAddedHashes[9].Hex()) txHashes, err = p.GetPendingTxHashesSince(ctx, txsAddedTime[9].Add(1*time.Second)) - if err != nil { - t.Error(err) - } + require.NoError(t, err) assert.Equal(t, 0, len(txHashes)) } func Test_DeleteTransactionsByHashes(t *testing.T) { ctx := context.Background() - initOrResetDB() + initOrResetDB(t) stateSqlDB, err := db.NewSQLDB(stateDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) defer stateSqlDB.Close() //nolint:gosec,errcheck poolSqlDB, err := db.NewSQLDB(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) defer poolSqlDB.Close() //nolint:gosec,errcheck st := newState(stateSqlDB) @@ -739,9 +651,7 @@ func Test_DeleteTransactionsByHashes(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) cfg := pool.Config{ FreeClaimGasLimit: 150000, } @@ -756,43 +666,33 @@ func Test_DeleteTransactionsByHashes(t *testing.T) { tx1 := types.NewTransaction(uint64(0), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) signedTx1, err := auth.Signer(auth.From, tx1) require.NoError(t, err) - if err := p.AddTx(ctx, *signedTx1, ""); err != nil { - t.Error(err) - } + err = p.AddTx(ctx, *signedTx1, "") + require.NoError(t, err) tx2 := types.NewTransaction(uint64(1), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) signedTx2, err := auth.Signer(auth.From, tx2) require.NoError(t, err) - if err := p.AddTx(ctx, *signedTx2, ""); err != nil { - t.Error(err) - } + err = p.AddTx(ctx, *signedTx2, "") + require.NoError(t, err) err = p.DeleteTransactionsByHashes(ctx, []common.Hash{signedTx1.Hash(), signedTx2.Hash()}) - if err != nil { - t.Error(err) - } + require.NoError(t, err) var count int err = poolSqlDB.QueryRow(ctx, "SELECT COUNT(*) FROM pool.transaction").Scan(&count) - if err != nil { - t.Error(err) - } + require.NoError(t, err) assert.Equal(t, 0, count) } func Test_TryAddIncompatibleTxs(t *testing.T) { - initOrResetDB() + initOrResetDB(t) stateSqlDB, err := db.NewSQLDB(stateDBCfg) - if err != nil { - panic(err) - } + require.NoError(t, err) defer stateSqlDB.Close() //nolint:gosec,errcheck poolSqlDB, err := db.NewSQLDB(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) defer poolSqlDB.Close() //nolint:gosec,errcheck st := newState(stateSqlDB) @@ -823,9 +723,7 @@ func Test_TryAddIncompatibleTxs(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) type testCase struct { name string @@ -930,22 +828,19 @@ func newState(sqlDB *pgxpool.Pool) *state.State { return st } -func initOrResetDB() { - if err := dbutils.InitOrResetState(stateDBCfg); err != nil { - panic(err) - } - if err := dbutils.InitOrResetPool(poolDBCfg); err != nil { - panic(err) - } +func initOrResetDB(t *testing.T) { + err := dbutils.InitOrResetState(stateDBCfg) + require.NoError(t, err) + + err = dbutils.InitOrResetPool(poolDBCfg) + require.NoError(t, err) } func Test_AddTxWithIntrinsicGasTooLow(t *testing.T) { - initOrResetDB() + initOrResetDB(t) stateSqlDB, err := db.NewSQLDB(stateDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) defer stateSqlDB.Close() //nolint:gosec,errcheck st := newState(stateSqlDB) @@ -964,9 +859,7 @@ func Test_AddTxWithIntrinsicGasTooLow(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) cfg := pool.Config{ FreeClaimGasLimit: 150000, } @@ -1071,12 +964,10 @@ func Test_AddTxWithIntrinsicGasTooLow(t *testing.T) { } func Test_AddRevertedTx(t *testing.T) { - initOrResetDB() + initOrResetDB(t) stateSqlDB, err := db.NewSQLDB(stateDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) defer stateSqlDB.Close() //nolint:gosec,errcheck st := newState(stateSqlDB) @@ -1095,9 +986,7 @@ func Test_AddRevertedTx(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) cfg := pool.Config{ FreeClaimGasLimit: 150000, } @@ -1132,3 +1021,91 @@ func Test_AddRevertedTx(t *testing.T) { assert.Equal(t, pool.TxStatusPending, txs[0].Status) } } + +func Test_BlockedAddress(t *testing.T) { + initOrResetDB(t) + + stateSqlDB, err := db.NewSQLDB(stateDBCfg) + require.NoError(t, err) + defer stateSqlDB.Close() //nolint:gosec,errcheck + + poolSqlDB, err := db.NewSQLDB(poolDBCfg) + require.NoError(t, err) + defer poolSqlDB.Close() //nolint:gosec,errcheck + + st := newState(stateSqlDB) + + const chainID = 2576980377 + auth := operations.MustGetAuth(operations.DefaultSequencerPrivateKey, chainID) + + genesisBlock := state.Block{ + BlockNumber: 0, + BlockHash: state.ZeroHash, + ParentHash: state.ZeroHash, + ReceivedAt: time.Now(), + } + genesis := state.Genesis{ + Actions: []*state.GenesisAction{ + { + Address: auth.From.String(), + Type: int(merkletree.LeafTypeBalance), + Value: "1000000000000000000000", + }, + }, + } + ctx := context.Background() + dbTx, err := st.BeginStateTransaction(ctx) + require.NoError(t, err) + _, err = st.SetGenesis(ctx, genesisBlock, genesis, dbTx) + require.NoError(t, err) + require.NoError(t, dbTx.Commit(ctx)) + + s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) + require.NoError(t, err) + + cfg := pool.Config{FreeClaimGasLimit: 150000} + p := pool.NewPool(cfg, s, st, common.Address{}, chainID) + + gasPrice, err := p.GetGasPrice(ctx) + require.NoError(t, err) + + // Add tx while address is not blocked + tx := types.NewTx(&types.LegacyTx{ + Nonce: 0, + GasPrice: big.NewInt(0).SetInt64(int64(gasPrice)), + Gas: 24000, + To: &auth.From, + Value: big.NewInt(1000), + }) + signedTx, err := auth.Signer(auth.From, tx) + require.NoError(t, err) + + err = p.AddTx(ctx, *signedTx, "") + require.NoError(t, err) + + // block address + _, err = poolSqlDB.Exec(ctx, "INSERT INTO pool.blocked(addr) VALUES($1)", auth.From.String()) + require.NoError(t, err) + + // get blocked when try to add new tx + tx = types.NewTx(&types.LegacyTx{ + Nonce: 1, + GasPrice: big.NewInt(0).SetInt64(int64(gasPrice)), + Gas: 24000, + To: &auth.From, + Value: big.NewInt(1000), + }) + signedTx, err = auth.Signer(auth.From, tx) + require.NoError(t, err) + + err = p.AddTx(ctx, *signedTx, "") + require.Equal(t, pool.ErrBlockedSender, err) + + // remove block + _, err = poolSqlDB.Exec(ctx, "DELETE FROM pool.blocked WHERE addr = $1", auth.From.String()) + require.NoError(t, err) + + // allowed to add tx again + err = p.AddTx(ctx, *signedTx, "") + require.NoError(t, err) +} From d227f2cdff9c8ba450ef717b473133aa4b834426 Mon Sep 17 00:00:00 2001 From: tclemos Date: Tue, 21 Mar 2023 14:02:49 -0300 Subject: [PATCH 2/7] block pool in memory --- pool/interfaces.go | 2 +- pool/pgpoolstorage/pgpoolstorage.go | 27 ++++++++++----- pool/pool.go | 53 +++++++++++++++++++++-------- pool/pool_test.go | 32 +++-------------- 4 files changed, 64 insertions(+), 50 deletions(-) diff --git a/pool/interfaces.go b/pool/interfaces.go index fd5387f5d6..3660b56e5e 100644 --- a/pool/interfaces.go +++ b/pool/interfaces.go @@ -32,7 +32,7 @@ type storage interface { GetTxZkCountersByHash(ctx context.Context, hash common.Hash) (*state.ZKCounters, error) DeleteTransactionByHash(ctx context.Context, hash common.Hash) error MarkWIPTxsAsPending(ctx context.Context) error - IsAddressBlocked(ctx context.Context, address common.Address) (bool, error) + GetAllAddressesBlocked(ctx context.Context) ([]common.Address, error) } type stateInterface interface { diff --git a/pool/pgpoolstorage/pgpoolstorage.go b/pool/pgpoolstorage/pgpoolstorage.go index 188008e940..fcffa55152 100644 --- a/pool/pgpoolstorage/pgpoolstorage.go +++ b/pool/pgpoolstorage/pgpoolstorage.go @@ -623,18 +623,29 @@ func (p *PostgresPoolStorage) UpdateTxWIPStatus(ctx context.Context, hash common return nil } -// IsAddressBlocked check if an address is added to the blocked table -func (p *PostgresPoolStorage) IsAddressBlocked(ctx context.Context, address common.Address) (bool, error) { - sql := `SELECT addr FROM pool.blocked WHERE addr = $1` - var addr string - err := p.db.QueryRow(ctx, sql, address.String()).Scan(&addr) +// GetAddressBlocked get all addresses blocked +func (p *PostgresPoolStorage) GetAllAddressesBlocked(ctx context.Context) ([]common.Address, error) { + sql := `SELECT addr FROM pool.blocked` + + rows, err := p.db.Query(ctx, sql) if err != nil { if errors.Is(err, pgx.ErrNoRows) { - return false, nil + return nil, nil } else { - return false, err + return nil, err + } + } + defer rows.Close() + + var addrs []common.Address + for rows.Next() { + var addr string + err := rows.Scan(&addr) + if err != nil { + return nil, err } + addrs = append(addrs, common.HexToAddress(addr)) } - return true, nil + return addrs, nil } diff --git a/pool/pool.go b/pool/pool.go index ca4762ff87..ba5ed71f23 100644 --- a/pool/pool.go +++ b/pool/pool.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "math/big" + "sync" "time" "github.com/0xPolygonHermez/zkevm-node/log" @@ -29,6 +30,10 @@ const ( // bridgeClaimMethodSignature for tracking bridgeClaimMethodSignature method bridgeClaimMethodSignature = "0x2cffd02e" + + // intervalToUpdateBlockedAddressesInMinutes is time it takes to sync the + // blocked address list from db to memory + intervalToUpdateBlockedAddressesInMinutes = 5 ) var ( @@ -45,20 +50,42 @@ var ( // that uses a postgres database to store the data type Pool struct { storage - state stateInterface - l2BridgeAddr common.Address - chainID uint64 - cfg Config + state stateInterface + l2BridgeAddr common.Address + chainID uint64 + cfg Config + blockedAddresses sync.Map } // NewPool creates and initializes an instance of Pool func NewPool(cfg Config, s storage, st stateInterface, l2BridgeAddr common.Address, chainID uint64) *Pool { - return &Pool{ - cfg: cfg, - storage: s, - state: st, - l2BridgeAddr: l2BridgeAddr, - chainID: chainID, + p := &Pool{ + cfg: cfg, + storage: s, + state: st, + l2BridgeAddr: l2BridgeAddr, + chainID: chainID, + blockedAddresses: sync.Map{}, + } + + refreshBlockedAddresses(p) + go func() { + time.Sleep(intervalToUpdateBlockedAddressesInMinutes * time.Minute) + refreshBlockedAddresses(p) + }() + return p +} + +// refreshBlockedAddresses refreshes the list of blocked addresses for the provided instance of pool +func refreshBlockedAddresses(p *Pool) { + bas, err := p.storage.GetAllAddressesBlocked(context.Background()) + if err != nil { + log.Error("failed to load blocked addresses") + return + } + + for _, ba := range bas { + p.blockedAddresses.Store(ba, 1) } } @@ -198,10 +225,8 @@ func (p *Pool) validateTx(ctx context.Context, tx types.Transaction) error { } // check if sender is blocked - blocked, err := p.storage.IsAddressBlocked(ctx, from) - if err != nil { - return err - } else if blocked { + _, blocked := p.blockedAddresses.Load(from) + if blocked { return ErrBlockedSender } diff --git a/pool/pool_test.go b/pool/pool_test.go index f1e3da057e..2e7648c518 100644 --- a/pool/pool_test.go +++ b/pool/pool_test.go @@ -1063,13 +1063,17 @@ func Test_BlockedAddress(t *testing.T) { s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) require.NoError(t, err) + // block address + _, err = poolSqlDB.Exec(ctx, "INSERT INTO pool.blocked(addr) VALUES($1)", auth.From.String()) + require.NoError(t, err) + cfg := pool.Config{FreeClaimGasLimit: 150000} p := pool.NewPool(cfg, s, st, common.Address{}, chainID) gasPrice, err := p.GetGasPrice(ctx) require.NoError(t, err) - // Add tx while address is not blocked + // Add tx while address is blocked tx := types.NewTx(&types.LegacyTx{ Nonce: 0, GasPrice: big.NewInt(0).SetInt64(int64(gasPrice)), @@ -1080,32 +1084,6 @@ func Test_BlockedAddress(t *testing.T) { signedTx, err := auth.Signer(auth.From, tx) require.NoError(t, err) - err = p.AddTx(ctx, *signedTx, "") - require.NoError(t, err) - - // block address - _, err = poolSqlDB.Exec(ctx, "INSERT INTO pool.blocked(addr) VALUES($1)", auth.From.String()) - require.NoError(t, err) - - // get blocked when try to add new tx - tx = types.NewTx(&types.LegacyTx{ - Nonce: 1, - GasPrice: big.NewInt(0).SetInt64(int64(gasPrice)), - Gas: 24000, - To: &auth.From, - Value: big.NewInt(1000), - }) - signedTx, err = auth.Signer(auth.From, tx) - require.NoError(t, err) - err = p.AddTx(ctx, *signedTx, "") require.Equal(t, pool.ErrBlockedSender, err) - - // remove block - _, err = poolSqlDB.Exec(ctx, "DELETE FROM pool.blocked WHERE addr = $1", auth.From.String()) - require.NoError(t, err) - - // allowed to add tx again - err = p.AddTx(ctx, *signedTx, "") - require.NoError(t, err) } From 33a78200d4b840fe368d41045359d0040c27ff91 Mon Sep 17 00:00:00 2001 From: tclemos Date: Tue, 21 Mar 2023 14:08:01 -0300 Subject: [PATCH 3/7] fix linter issues --- pool/errors.go | 2 +- pool/pgpoolstorage/pgpoolstorage.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pool/errors.go b/pool/errors.go index 901c4840c1..d05bacc78d 100644 --- a/pool/errors.go +++ b/pool/errors.go @@ -27,7 +27,7 @@ var ( // ErrInvalidSender is returned if the transaction contains an invalid signature. ErrInvalidSender = errors.New("invalid sender") - // ErrInvalidSender is returned if the transaction is sent by a blocked account. + // ErrBlockedSender is returned if the transaction is sent by a blocked account. ErrBlockedSender = errors.New("blocked sender") // ErrNonceTooLow is returned if the nonce of a transaction is lower than the diff --git a/pool/pgpoolstorage/pgpoolstorage.go b/pool/pgpoolstorage/pgpoolstorage.go index fcffa55152..af5f11f0b2 100644 --- a/pool/pgpoolstorage/pgpoolstorage.go +++ b/pool/pgpoolstorage/pgpoolstorage.go @@ -623,7 +623,7 @@ func (p *PostgresPoolStorage) UpdateTxWIPStatus(ctx context.Context, hash common return nil } -// GetAddressBlocked get all addresses blocked +// GetAllAddressesBlocked get all addresses blocked func (p *PostgresPoolStorage) GetAllAddressesBlocked(ctx context.Context) ([]common.Address, error) { sql := `SELECT addr FROM pool.blocked` From 3002f8ebf2681e94cc4cb02f26d9fb976ebebe92 Mon Sep 17 00:00:00 2001 From: tclemos Date: Tue, 21 Mar 2023 14:10:13 -0300 Subject: [PATCH 4/7] refactoring --- pool/pool.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pool/pool.go b/pool/pool.go index ba5ed71f23..c897c26128 100644 --- a/pool/pool.go +++ b/pool/pool.go @@ -68,16 +68,16 @@ func NewPool(cfg Config, s storage, st stateInterface, l2BridgeAddr common.Addre blockedAddresses: sync.Map{}, } - refreshBlockedAddresses(p) - go func() { + p.refreshBlockedAddresses() + go func(p *Pool) { time.Sleep(intervalToUpdateBlockedAddressesInMinutes * time.Minute) - refreshBlockedAddresses(p) - }() + p.refreshBlockedAddresses() + }(p) return p } // refreshBlockedAddresses refreshes the list of blocked addresses for the provided instance of pool -func refreshBlockedAddresses(p *Pool) { +func (p *Pool) refreshBlockedAddresses() { bas, err := p.storage.GetAllAddressesBlocked(context.Background()) if err != nil { log.Error("failed to load blocked addresses") From b4d1d170ef46fddcd847dcfad54b4ffe4cb5c3bf Mon Sep 17 00:00:00 2001 From: tclemos Date: Tue, 21 Mar 2023 14:34:56 -0300 Subject: [PATCH 5/7] add a config to set interval to refresh blocked pool --- config/default.go | 1 + .../environments/local/local.node.config.toml | 1 + .../public/public.node.config.toml | 1 + pool/config.go | 9 +- pool/interfaces.go | 1 + pool/pgpoolstorage/pgpoolstorage.go | 16 +++ pool/pool.go | 42 ++++-- pool/pool_test.go | 136 ++++++++++++------ test/config/debug.node.config.toml | 1 + test/config/test.node.config.toml | 1 + 10 files changed, 149 insertions(+), 60 deletions(-) diff --git a/config/default.go b/config/default.go index 5f1932e6ef..bc50cb6717 100644 --- a/config/default.go +++ b/config/default.go @@ -20,6 +20,7 @@ MaxConns = 200 [Pool] FreeClaimGasLimit = 150000 +IntervalToRefreshBlockedAddresses = "5m" [Pool.DB] User = "pool_user" Password = "pool_password" diff --git a/config/environments/local/local.node.config.toml b/config/environments/local/local.node.config.toml index 93a985bdee..e5a1eba0a0 100644 --- a/config/environments/local/local.node.config.toml +++ b/config/environments/local/local.node.config.toml @@ -16,6 +16,7 @@ MaxConns = 200 [Pool] FreeClaimGasLimit = 1500000 +IntervalToRefreshBlockedAddresses = "5m" [Pool.DB] User = "pool_user" Password = "pool_password" diff --git a/config/environments/public/public.node.config.toml b/config/environments/public/public.node.config.toml index f25eca4d84..e4fb86e8e5 100644 --- a/config/environments/public/public.node.config.toml +++ b/config/environments/public/public.node.config.toml @@ -14,6 +14,7 @@ MaxConns = 200 [Pool] FreeClaimGasLimit = 1500000 +IntervalToRefreshBlockedAddresses = "5m" [Pool.DB] User = "pool_user" Password = "pool_password" diff --git a/pool/config.go b/pool/config.go index 6a35a4a3f2..f403431bc2 100644 --- a/pool/config.go +++ b/pool/config.go @@ -1,10 +1,17 @@ package pool -import "github.com/0xPolygonHermez/zkevm-node/db" +import ( + "github.com/0xPolygonHermez/zkevm-node/config/types" + "github.com/0xPolygonHermez/zkevm-node/db" +) // Config is the pool configuration type Config struct { // FreeClaimGasLimit is the max gas allowed use to do a free claim FreeClaimGasLimit uint64 `mapstructure:"FreeClaimGasLimit"` DB db.Config `mapstructure:"DB"` + + // IntervalToRefreshBlockedAddresses is the time it takes to sync the + // blocked address list from db to memory + IntervalToRefreshBlockedAddresses types.Duration `mapstructure:"IntervalToRefreshBlockedAddresses"` } diff --git a/pool/interfaces.go b/pool/interfaces.go index 3660b56e5e..722b098c0b 100644 --- a/pool/interfaces.go +++ b/pool/interfaces.go @@ -33,6 +33,7 @@ type storage interface { DeleteTransactionByHash(ctx context.Context, hash common.Hash) error MarkWIPTxsAsPending(ctx context.Context) error GetAllAddressesBlocked(ctx context.Context) ([]common.Address, error) + IsAddressBlocked(ctx context.Context, address common.Address) (bool, error) } type stateInterface interface { diff --git a/pool/pgpoolstorage/pgpoolstorage.go b/pool/pgpoolstorage/pgpoolstorage.go index af5f11f0b2..dbb3a36835 100644 --- a/pool/pgpoolstorage/pgpoolstorage.go +++ b/pool/pgpoolstorage/pgpoolstorage.go @@ -649,3 +649,19 @@ func (p *PostgresPoolStorage) GetAllAddressesBlocked(ctx context.Context) ([]com return addrs, nil } + +// IsAddressBlocked check if an address is added to the blocked table +func (p *PostgresPoolStorage) IsAddressBlocked(ctx context.Context, address common.Address) (bool, error) { + sql := `SELECT addr FROM pool.blocked WHERE addr = $1` + var addr string + err := p.db.QueryRow(ctx, sql, address.String()).Scan(&addr) + if err != nil { + if errors.Is(err, pgx.ErrNoRows) { + return false, nil + } else { + return false, err + } + } + + return true, nil +} diff --git a/pool/pool.go b/pool/pool.go index c897c26128..fcbea5ccc0 100644 --- a/pool/pool.go +++ b/pool/pool.go @@ -30,10 +30,6 @@ const ( // bridgeClaimMethodSignature for tracking bridgeClaimMethodSignature method bridgeClaimMethodSignature = "0x2cffd02e" - - // intervalToUpdateBlockedAddressesInMinutes is time it takes to sync the - // blocked address list from db to memory - intervalToUpdateBlockedAddressesInMinutes = 5 ) var ( @@ -69,23 +65,45 @@ func NewPool(cfg Config, s storage, st stateInterface, l2BridgeAddr common.Addre } p.refreshBlockedAddresses() - go func(p *Pool) { - time.Sleep(intervalToUpdateBlockedAddressesInMinutes * time.Minute) - p.refreshBlockedAddresses() - }(p) + go func(cfg *Config, p *Pool) { + for { + time.Sleep(cfg.IntervalToRefreshBlockedAddresses.Duration) + p.refreshBlockedAddresses() + } + }(&cfg, p) return p } // refreshBlockedAddresses refreshes the list of blocked addresses for the provided instance of pool func (p *Pool) refreshBlockedAddresses() { - bas, err := p.storage.GetAllAddressesBlocked(context.Background()) + blockedAddresses, err := p.storage.GetAllAddressesBlocked(context.Background()) if err != nil { log.Error("failed to load blocked addresses") return } - for _, ba := range bas { - p.blockedAddresses.Store(ba, 1) + for _, blockedAddress := range blockedAddresses { + p.blockedAddresses.Store(blockedAddress.String(), 1) + } + + unblockedAddresses := []string{} + p.blockedAddresses.Range(func(key, value any) bool { + addrHex := key.(string) + addr := common.HexToAddress(addrHex) + blocked, err := p.storage.IsAddressBlocked(context.Background(), addr) + if err != nil { + log.Error("failed to check if %v is blocked", addrHex) + return true + } + + if !blocked { + unblockedAddresses = append(unblockedAddresses, addrHex) + } + return true + }) + + for _, unblockedAddress := range unblockedAddresses { + p.blockedAddresses.Delete(unblockedAddress) } } @@ -225,7 +243,7 @@ func (p *Pool) validateTx(ctx context.Context, tx types.Transaction) error { } // check if sender is blocked - _, blocked := p.blockedAddresses.Load(from) + _, blocked := p.blockedAddresses.Load(from.String()) if blocked { return ErrBlockedSender } diff --git a/pool/pool_test.go b/pool/pool_test.go index 2e7648c518..1f67ca16fe 100644 --- a/pool/pool_test.go +++ b/pool/pool_test.go @@ -11,6 +11,7 @@ import ( "testing" "time" + "github.com/0xPolygonHermez/zkevm-node/config/types" "github.com/0xPolygonHermez/zkevm-node/db" "github.com/0xPolygonHermez/zkevm-node/encoding" "github.com/0xPolygonHermez/zkevm-node/hex" @@ -26,7 +27,7 @@ import ( "github.com/0xPolygonHermez/zkevm-node/test/testutils" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" + ethTypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/jackc/pgx/v4/pgxpool" "github.com/stretchr/testify/assert" @@ -103,7 +104,8 @@ func Test_AddTx(t *testing.T) { const chainID = 2576980377 cfg := pool.Config{ - FreeClaimGasLimit: 150000, + FreeClaimGasLimit: 150000, + IntervalToRefreshBlockedAddresses: types.NewDuration(5 * time.Minute), } p := pool.NewPool(cfg, s, st, common.Address{}, chainID) @@ -111,7 +113,7 @@ func Test_AddTx(t *testing.T) { b, err := hex.DecodeHex(txRLPHash) require.NoError(t, err) - tx := new(types.Transaction) + tx := new(ethTypes.Transaction) tx.UnmarshalBinary(b) //nolint:gosec,errcheck err = p.AddTx(ctx, *tx, "") @@ -185,7 +187,8 @@ func Test_AddPreEIP155Tx(t *testing.T) { const chainID = 2576980377 cfg := pool.Config{ - FreeClaimGasLimit: 150000, + FreeClaimGasLimit: 150000, + IntervalToRefreshBlockedAddresses: types.NewDuration(5 * time.Minute), } p := pool.NewPool(cfg, s, st, common.Address{}, chainID) @@ -252,7 +255,8 @@ func Test_GetPendingTxs(t *testing.T) { require.NoError(t, err) cfg := pool.Config{ - FreeClaimGasLimit: 150000, + FreeClaimGasLimit: 150000, + IntervalToRefreshBlockedAddresses: types.NewDuration(5 * time.Minute), } p := pool.NewPool(cfg, s, st, common.Address{}, chainID.Uint64()) @@ -267,7 +271,7 @@ func Test_GetPendingTxs(t *testing.T) { // insert pending transactions for i := 0; i < txsCount; i++ { - tx := types.NewTransaction(uint64(i), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) + tx := ethTypes.NewTransaction(uint64(i), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) signedTx, err := auth.Signer(auth.From, tx) require.NoError(t, err) err = p.AddTx(ctx, *signedTx, "") @@ -309,7 +313,8 @@ func Test_GetPendingTxsZeroPassed(t *testing.T) { s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) require.NoError(t, err) cfg := pool.Config{ - FreeClaimGasLimit: 150000, + FreeClaimGasLimit: 150000, + IntervalToRefreshBlockedAddresses: types.NewDuration(5 * time.Minute), } p := pool.NewPool(cfg, s, st, common.Address{}, chainID.Uint64()) @@ -324,7 +329,7 @@ func Test_GetPendingTxsZeroPassed(t *testing.T) { // insert pending transactions for i := 0; i < txsCount; i++ { - tx := types.NewTransaction(uint64(i), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) + tx := ethTypes.NewTransaction(uint64(i), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) signedTx, err := auth.Signer(auth.From, tx) require.NoError(t, err) err = p.AddTx(ctx, *signedTx, "") @@ -366,7 +371,8 @@ func Test_GetTopPendingTxByProfitabilityAndZkCounters(t *testing.T) { s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) require.NoError(t, err) cfg := pool.Config{ - FreeClaimGasLimit: 150000, + FreeClaimGasLimit: 150000, + IntervalToRefreshBlockedAddresses: types.NewDuration(5 * time.Minute), } p := pool.NewPool(cfg, s, st, common.Address{}, chainID.Uint64()) @@ -380,7 +386,7 @@ func Test_GetTopPendingTxByProfitabilityAndZkCounters(t *testing.T) { // insert pending transactions for i := 0; i < txsCount; i++ { - tx := types.NewTransaction(uint64(i), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10+int64(i)), []byte{}) + tx := ethTypes.NewTransaction(uint64(i), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10+int64(i)), []byte{}) signedTx, err := auth.Signer(auth.From, tx) require.NoError(t, err) err = p.AddTx(ctx, *signedTx, "") @@ -423,7 +429,8 @@ func Test_UpdateTxsStatus(t *testing.T) { s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) require.NoError(t, err) cfg := pool.Config{ - FreeClaimGasLimit: 150000, + FreeClaimGasLimit: 150000, + IntervalToRefreshBlockedAddresses: types.NewDuration(5 * time.Minute), } p := pool.NewPool(cfg, s, st, common.Address{}, chainID.Uint64()) @@ -433,13 +440,13 @@ func Test_UpdateTxsStatus(t *testing.T) { auth, err := bind.NewKeyedTransactorWithChainID(privateKey, chainID) require.NoError(t, err) - tx1 := types.NewTransaction(uint64(0), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) + tx1 := ethTypes.NewTransaction(uint64(0), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) signedTx1, err := auth.Signer(auth.From, tx1) require.NoError(t, err) err = p.AddTx(ctx, *signedTx1, "") require.NoError(t, err) - tx2 := types.NewTransaction(uint64(1), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) + tx2 := ethTypes.NewTransaction(uint64(1), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) signedTx2, err := auth.Signer(auth.From, tx2) require.NoError(t, err) err = p.AddTx(ctx, *signedTx2, "") @@ -484,7 +491,8 @@ func Test_UpdateTxStatus(t *testing.T) { s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) require.NoError(t, err) cfg := pool.Config{ - FreeClaimGasLimit: 150000, + FreeClaimGasLimit: 150000, + IntervalToRefreshBlockedAddresses: types.NewDuration(5 * time.Minute), } p := pool.NewPool(cfg, s, st, common.Address{}, chainID.Uint64()) @@ -494,7 +502,7 @@ func Test_UpdateTxStatus(t *testing.T) { auth, err := bind.NewKeyedTransactorWithChainID(privateKey, chainID) require.NoError(t, err) - tx := types.NewTransaction(uint64(0), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) + tx := ethTypes.NewTransaction(uint64(0), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) signedTx, err := auth.Signer(auth.From, tx) require.NoError(t, err) err = p.AddTx(ctx, *signedTx, "") @@ -521,7 +529,8 @@ func Test_SetAndGetGasPrice(t *testing.T) { s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) require.NoError(t, err) cfg := pool.Config{ - FreeClaimGasLimit: 150000, + FreeClaimGasLimit: 150000, + IntervalToRefreshBlockedAddresses: types.NewDuration(5 * time.Minute), } p := pool.NewPool(cfg, s, nil, common.Address{}, chainID.Uint64()) @@ -565,7 +574,8 @@ func TestGetPendingTxSince(t *testing.T) { s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) require.NoError(t, err) cfg := pool.Config{ - FreeClaimGasLimit: 150000, + FreeClaimGasLimit: 150000, + IntervalToRefreshBlockedAddresses: types.NewDuration(5 * time.Minute), } p := pool.NewPool(cfg, s, st, common.Address{}, chainID.Uint64()) @@ -583,7 +593,7 @@ func TestGetPendingTxSince(t *testing.T) { timeBeforeTxs := time.Now() // insert pending transactions for i := 0; i < txsCount; i++ { - tx := types.NewTransaction(uint64(i), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) + tx := ethTypes.NewTransaction(uint64(i), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) signedTx, err := auth.Signer(auth.From, tx) require.NoError(t, err) txsAddedTime = append(txsAddedTime, time.Now()) @@ -653,7 +663,8 @@ func Test_DeleteTransactionsByHashes(t *testing.T) { s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) require.NoError(t, err) cfg := pool.Config{ - FreeClaimGasLimit: 150000, + FreeClaimGasLimit: 150000, + IntervalToRefreshBlockedAddresses: types.NewDuration(5 * time.Minute), } p := pool.NewPool(cfg, s, st, common.Address{}, chainID.Uint64()) @@ -663,13 +674,13 @@ func Test_DeleteTransactionsByHashes(t *testing.T) { auth, err := bind.NewKeyedTransactorWithChainID(privateKey, chainID) require.NoError(t, err) - tx1 := types.NewTransaction(uint64(0), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) + tx1 := ethTypes.NewTransaction(uint64(0), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) signedTx1, err := auth.Signer(auth.From, tx1) require.NoError(t, err) err = p.AddTx(ctx, *signedTx1, "") require.NoError(t, err) - tx2 := types.NewTransaction(uint64(1), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) + tx2 := ethTypes.NewTransaction(uint64(1), common.Address{}, big.NewInt(10), uint64(100000), big.NewInt(10), []byte{}) signedTx2, err := auth.Signer(auth.From, tx2) require.NoError(t, err) err = p.AddTx(ctx, *signedTx2, "") @@ -727,7 +738,7 @@ func Test_TryAddIncompatibleTxs(t *testing.T) { type testCase struct { name string - createIncompatibleTx func() types.Transaction + createIncompatibleTx func() ethTypes.Transaction expectedError error } @@ -748,8 +759,8 @@ func Test_TryAddIncompatibleTxs(t *testing.T) { testCases := []testCase{ { name: "Gas price over 256 bits", - createIncompatibleTx: func() types.Transaction { - tx := types.NewTransaction(uint64(0), + createIncompatibleTx: func() ethTypes.Transaction { + tx := ethTypes.NewTransaction(uint64(0), common.HexToAddress("0x1"), big.NewInt(1), uint64(1000000), bigIntOver256Bits, nil) signedTx, err := auth.Signer(auth.From, tx) @@ -760,8 +771,8 @@ func Test_TryAddIncompatibleTxs(t *testing.T) { }, { name: "Value over 256 bits", - createIncompatibleTx: func() types.Transaction { - tx := types.NewTransaction(uint64(0), + createIncompatibleTx: func() ethTypes.Transaction { + tx := ethTypes.NewTransaction(uint64(0), common.HexToAddress("0x1"), bigIntOver256Bits, uint64(1000000), big.NewInt(1), nil) signedTx, err := auth.Signer(auth.From, tx) @@ -772,9 +783,9 @@ func Test_TryAddIncompatibleTxs(t *testing.T) { }, { name: "data over 30k bytes", - createIncompatibleTx: func() types.Transaction { + createIncompatibleTx: func() ethTypes.Transaction { data := [30001]byte{} - tx := types.NewTransaction(uint64(0), + tx := ethTypes.NewTransaction(uint64(0), common.HexToAddress("0x1"), big.NewInt(1), uint64(1000000), big.NewInt(1), data[:]) signedTx, err := auth.Signer(auth.From, tx) @@ -785,8 +796,8 @@ func Test_TryAddIncompatibleTxs(t *testing.T) { }, { name: "chain id over 64 bits", - createIncompatibleTx: func() types.Transaction { - tx := types.NewTransaction(uint64(0), + createIncompatibleTx: func() ethTypes.Transaction { + tx := ethTypes.NewTransaction(uint64(0), common.HexToAddress("0x1"), big.NewInt(1), uint64(1000000), big.NewInt(1), nil) signedTx, err := authChainIdOver64Bits.Signer(authChainIdOver64Bits.From, tx) @@ -797,7 +808,8 @@ func Test_TryAddIncompatibleTxs(t *testing.T) { }, } cfg := pool.Config{ - FreeClaimGasLimit: 150000, + FreeClaimGasLimit: 150000, + IntervalToRefreshBlockedAddresses: types.NewDuration(5 * time.Minute), } for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { @@ -861,7 +873,8 @@ func Test_AddTxWithIntrinsicGasTooLow(t *testing.T) { s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) require.NoError(t, err) cfg := pool.Config{ - FreeClaimGasLimit: 150000, + FreeClaimGasLimit: 150000, + IntervalToRefreshBlockedAddresses: types.NewDuration(5 * time.Minute), } p := pool.NewPool(cfg, s, st, common.Address{}, chainID.Uint64()) @@ -872,7 +885,7 @@ func Test_AddTxWithIntrinsicGasTooLow(t *testing.T) { require.NoError(t, err) // insert transaction - tx := types.NewTx(&types.LegacyTx{ + tx := ethTypes.NewTx(ðTypes.LegacyTx{ Nonce: uint64(0), To: &common.Address{}, Value: big.NewInt(10), @@ -886,7 +899,7 @@ func Test_AddTxWithIntrinsicGasTooLow(t *testing.T) { require.Error(t, err) assert.Equal(t, err.Error(), pool.ErrIntrinsicGas.Error()) - tx = types.NewTx(&types.LegacyTx{ + tx = ethTypes.NewTx(ðTypes.LegacyTx{ Nonce: uint64(0), To: nil, Value: big.NewInt(10), @@ -900,7 +913,7 @@ func Test_AddTxWithIntrinsicGasTooLow(t *testing.T) { require.Error(t, err) assert.Equal(t, err.Error(), pool.ErrIntrinsicGas.Error()) - tx = types.NewTx(&types.LegacyTx{ + tx = ethTypes.NewTx(ðTypes.LegacyTx{ Nonce: uint64(0), To: &common.Address{}, Value: big.NewInt(10), @@ -913,7 +926,7 @@ func Test_AddTxWithIntrinsicGasTooLow(t *testing.T) { err = p.AddTx(ctx, *signedTx, "") require.NoError(t, err) - tx = types.NewTx(&types.LegacyTx{ + tx = ethTypes.NewTx(ðTypes.LegacyTx{ Nonce: uint64(1), To: &common.Address{}, Value: big.NewInt(10), @@ -927,7 +940,7 @@ func Test_AddTxWithIntrinsicGasTooLow(t *testing.T) { require.Error(t, err) assert.Equal(t, err.Error(), pool.ErrIntrinsicGas.Error()) - tx = types.NewTx(&types.LegacyTx{ + tx = ethTypes.NewTx(ðTypes.LegacyTx{ Nonce: uint64(1), To: &common.Address{}, Value: big.NewInt(10), @@ -941,7 +954,7 @@ func Test_AddTxWithIntrinsicGasTooLow(t *testing.T) { require.Error(t, err) assert.Equal(t, err.Error(), pool.ErrIntrinsicGas.Error()) - tx = types.NewTx(&types.LegacyTx{ + tx = ethTypes.NewTx(ðTypes.LegacyTx{ Nonce: uint64(1), To: &common.Address{}, Value: big.NewInt(10), @@ -988,7 +1001,8 @@ func Test_AddRevertedTx(t *testing.T) { s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) require.NoError(t, err) cfg := pool.Config{ - FreeClaimGasLimit: 150000, + FreeClaimGasLimit: 150000, + IntervalToRefreshBlockedAddresses: types.NewDuration(5 * time.Minute), } p := pool.NewPool(cfg, s, st, common.Address{}, chainID.Uint64()) @@ -1001,7 +1015,7 @@ func Test_AddRevertedTx(t *testing.T) { // insert transaction revertScData, err := hex.DecodeHex(Revert.RevertBin) require.NoError(t, err) - tx := types.NewTx(&types.LegacyTx{ + tx := ethTypes.NewTx(ðTypes.LegacyTx{ Nonce: uint64(0), Gas: uint64(1000000), GasPrice: big.NewInt(10), @@ -1063,18 +1077,14 @@ func Test_BlockedAddress(t *testing.T) { s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) require.NoError(t, err) - // block address - _, err = poolSqlDB.Exec(ctx, "INSERT INTO pool.blocked(addr) VALUES($1)", auth.From.String()) - require.NoError(t, err) - - cfg := pool.Config{FreeClaimGasLimit: 150000} + cfg := pool.Config{FreeClaimGasLimit: 150000, IntervalToRefreshBlockedAddresses: types.NewDuration(5 * time.Second)} p := pool.NewPool(cfg, s, st, common.Address{}, chainID) gasPrice, err := p.GetGasPrice(ctx) require.NoError(t, err) - // Add tx while address is blocked - tx := types.NewTx(&types.LegacyTx{ + // Add tx while address is not blocked + tx := ethTypes.NewTx(ðTypes.LegacyTx{ Nonce: 0, GasPrice: big.NewInt(0).SetInt64(int64(gasPrice)), Gas: 24000, @@ -1084,6 +1094,38 @@ func Test_BlockedAddress(t *testing.T) { signedTx, err := auth.Signer(auth.From, tx) require.NoError(t, err) + err = p.AddTx(ctx, *signedTx, "") + require.NoError(t, err) + + // block address + _, err = poolSqlDB.Exec(ctx, "INSERT INTO pool.blocked(addr) VALUES($1)", auth.From.String()) + require.NoError(t, err) + + // wait it to refresh + time.Sleep(cfg.IntervalToRefreshBlockedAddresses.Duration) + + // get blocked when try to add new tx + tx = ethTypes.NewTx(ðTypes.LegacyTx{ + Nonce: 1, + GasPrice: big.NewInt(0).SetInt64(int64(gasPrice)), + Gas: 24000, + To: &auth.From, + Value: big.NewInt(1000), + }) + signedTx, err = auth.Signer(auth.From, tx) + require.NoError(t, err) + err = p.AddTx(ctx, *signedTx, "") require.Equal(t, pool.ErrBlockedSender, err) + + // remove block + _, err = poolSqlDB.Exec(ctx, "DELETE FROM pool.blocked WHERE addr = $1", auth.From.String()) + require.NoError(t, err) + + // wait it to refresh + time.Sleep(cfg.IntervalToRefreshBlockedAddresses.Duration) + + // allowed to add tx again + err = p.AddTx(ctx, *signedTx, "") + require.NoError(t, err) } diff --git a/test/config/debug.node.config.toml b/test/config/debug.node.config.toml index 6eeee04156..e691ced95d 100644 --- a/test/config/debug.node.config.toml +++ b/test/config/debug.node.config.toml @@ -16,6 +16,7 @@ MaxConns = 10 [Pool] FreeClaimGasLimit = 1500000 +IntervalToRefreshBlockedAddresses = "5m" [Pool.DB] User = "pool_user" Password = "pool_password" diff --git a/test/config/test.node.config.toml b/test/config/test.node.config.toml index 4b8f32eee4..2f1aa55f4d 100644 --- a/test/config/test.node.config.toml +++ b/test/config/test.node.config.toml @@ -16,6 +16,7 @@ MaxConns = 200 [Pool] FreeClaimGasLimit = 1500000 +IntervalToRefreshBlockedAddresses = "5m" [Pool.DB] User = "pool_user" Password = "pool_password" From 202cbce76197d7a73f4a5cbef22fd7f8b2d75ba3 Mon Sep 17 00:00:00 2001 From: tclemos Date: Thu, 30 Mar 2023 18:45:32 +0200 Subject: [PATCH 6/7] fix unit test --- pool/pool_test.go | 115 +++++++++++++++++----------------------------- 1 file changed, 43 insertions(+), 72 deletions(-) diff --git a/pool/pool_test.go b/pool/pool_test.go index 2597d65643..82bcf8f9f6 100644 --- a/pool/pool_test.go +++ b/pool/pool_test.go @@ -111,14 +111,12 @@ func Test_AddTx(t *testing.T) { require.NoError(t, err) const chainID = 2576980377 - p := setupPool(t, s, st, chainID, ctx) + p := setupPool(t, cfg, s, st, chainID, ctx) tx := new(ethTypes.Transaction) expectedTxEncoded := "0xf86880843b9aca008252089400000000000000000000000000000000000000008080850133333355a03ee24709870c8dbc67884c9c8acb864c1aceaaa7332b9a3db0d7a5d7c68eb8e4a0302980b070f5e3ffca3dc27b07daf69d66ab27d4df648e0b3ed059cf23aa168d" b, err := hex.DecodeHex(expectedTxEncoded) - if err != nil { - t.Error(err) - } + require.NoError(t, err) tx.UnmarshalBinary(b) //nolint:gosec,errcheck err = p.AddTx(ctx, *tx, "") @@ -157,9 +155,7 @@ func Test_AddTx_OversizedData(t *testing.T) { defer stateSqlDB.Close() //nolint:gosec,errcheck poolSqlDB, err := db.NewSQLDB(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) defer poolSqlDB.Close() //nolint:gosec,errcheck st := newState(stateSqlDB) @@ -187,9 +183,7 @@ func Test_AddTx_OversizedData(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) const chainID = 2576980377 p := pool.NewPool(cfg, s, st, common.Address{}, chainID) @@ -252,7 +246,7 @@ func Test_AddPreEIP155Tx(t *testing.T) { require.NoError(t, err) const chainID = 2576980377 - p := setupPool(t, s, st, chainID, ctx) + p := setupPool(t, cfg, s, st, chainID, ctx) batchL2Data := "0xe580843b9aca00830186a0941275fbb540c8efc58b812ba83b0d0b8b9917ae98808464fbb77c6b39bdc5f8e458aba689f2a1ff8c543a94e4817bda40f3fe34080c4ab26c1e3c2fc2cda93bc32f0a79940501fd505dcf48d94abfde932ebf1417f502cb0d9de81b" b, err := hex.DecodeHex(batchL2Data) @@ -314,10 +308,8 @@ func Test_GetPendingTxs(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } - p := setupPool(t, s, st, chainID.Uint64(), ctx) + require.NoError(t, err) + p := setupPool(t, cfg, s, st, chainID.Uint64(), ctx) const txsCount = 10 const limit = 5 @@ -370,10 +362,8 @@ func Test_GetPendingTxsZeroPassed(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } - p := setupPool(t, s, st, chainID.Uint64(), ctx) + require.NoError(t, err) + p := setupPool(t, cfg, s, st, chainID.Uint64(), ctx) const txsCount = 10 const limit = 0 @@ -426,10 +416,8 @@ func Test_GetTopPendingTxByProfitabilityAndZkCounters(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } - p := setupPool(t, s, st, chainID.Uint64(), ctx) + require.NoError(t, err) + p := setupPool(t, cfg, s, st, chainID.Uint64(), ctx) const txsCount = 10 @@ -482,10 +470,8 @@ func Test_UpdateTxsStatus(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } - p := setupPool(t, s, st, chainID.Uint64(), ctx) + require.NoError(t, err) + p := setupPool(t, cfg, s, st, chainID.Uint64(), ctx) privateKey, err := crypto.HexToECDSA(strings.TrimPrefix(senderPrivateKey, "0x")) require.NoError(t, err) @@ -542,10 +528,8 @@ func Test_UpdateTxStatus(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } - p := setupPool(t, s, st, chainID.Uint64(), ctx) + require.NoError(t, err) + p := setupPool(t, cfg, s, st, chainID.Uint64(), ctx) privateKey, err := crypto.HexToECDSA(strings.TrimPrefix(senderPrivateKey, "0x")) require.NoError(t, err) @@ -577,9 +561,7 @@ func Test_SetAndGetGasPrice(t *testing.T) { initOrResetDB(t) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) p := pool.NewPool(cfg, s, nil, common.Address{}, chainID.Uint64()) nBig, err := rand.Int(rand.Reader, big.NewInt(0).SetUint64(math.MaxUint64)) @@ -620,10 +602,8 @@ func TestGetPendingTxSince(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } - p := setupPool(t, s, st, chainID.Uint64(), ctx) + require.NoError(t, err) + p := setupPool(t, cfg, s, st, chainID.Uint64(), ctx) const txsCount = 10 @@ -708,10 +688,8 @@ func Test_DeleteTransactionsByHashes(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } - p := setupPool(t, s, st, chainID.Uint64(), ctx) + require.NoError(t, err) + p := setupPool(t, cfg, s, st, chainID.Uint64(), ctx) privateKey, err := crypto.HexToECDSA(strings.TrimPrefix(senderPrivateKey, "0x")) require.NoError(t, err) @@ -855,7 +833,7 @@ func Test_TryAddIncompatibleTxs(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { incompatibleTx := testCase.createIncompatibleTx() - p := setupPool(t, s, st, incompatibleTx.ChainId().Uint64(), ctx) + p := setupPool(t, cfg, s, st, incompatibleTx.ChainId().Uint64(), ctx) err = p.AddTx(ctx, incompatibleTx, "") assert.Equal(t, testCase.expectedError, err) }) @@ -912,10 +890,8 @@ func Test_AddTxWithIntrinsicGasTooLow(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } - p := setupPool(t, s, st, chainID.Uint64(), ctx) + require.NoError(t, err) + p := setupPool(t, cfg, s, st, chainID.Uint64(), ctx) privateKey, err := crypto.HexToECDSA(strings.TrimPrefix(senderPrivateKey, "0x")) require.NoError(t, err) @@ -1076,9 +1052,7 @@ func Test_AddTx_GasPriceErr(t *testing.T) { defer stateSqlDB.Close() //nolint:gosec,errcheck poolSqlDB, err := db.NewSQLDB(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) defer poolSqlDB.Close() //nolint:gosec,errcheck st := newState(stateSqlDB) @@ -1106,12 +1080,10 @@ func Test_AddTx_GasPriceErr(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) const chainID = 2576980377 - p := setupPool(t, s, st, chainID, ctx) + p := setupPool(t, cfg, s, st, chainID, ctx) tx := ethTypes.NewTx(ðTypes.LegacyTx{ Nonce: tc.nonce, To: tc.to, @@ -1162,10 +1134,8 @@ func Test_AddRevertedTx(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - if err != nil { - t.Error(err) - } - p := setupPool(t, s, st, chainID.Uint64(), ctx) + require.NoError(t, err) + p := setupPool(t, cfg, s, st, chainID.Uint64(), ctx) privateKey, err := crypto.HexToECDSA(strings.TrimPrefix(senderPrivateKey, "0x")) require.NoError(t, err) @@ -1241,8 +1211,16 @@ func Test_BlockedAddress(t *testing.T) { require.NoError(t, err) - cfg := pool.Config{FreeClaimGasLimit: 150000, IntervalToRefreshBlockedAddresses: cfgTypes.NewDuration(5 * time.Second)} - p := pool.NewPool(cfg, s, st, common.Address{}, chainID) + cfg := pool.Config{ + FreeClaimGasLimit: 150000, + MaxTxBytesSize: 30132, + MaxTxDataBytesSize: 30000, + MinAllowedGasPriceInterval: cfgTypes.NewDuration(5 * time.Minute), + PollMinAllowedGasPriceInterval: cfgTypes.NewDuration(15 * time.Second), + DefaultMinGasPriceAllowed: 1000000000, + IntervalToRefreshBlockedAddresses: cfgTypes.NewDuration(5 * time.Second), + } + p := setupPool(t, cfg, s, st, chainID, ctx) gasPrice, err := p.GetGasPrice(ctx) require.NoError(t, err) @@ -1294,12 +1272,10 @@ func Test_BlockedAddress(t *testing.T) { require.NoError(t, err) } -func setupPool(t *testing.T, s *pgpoolstorage.PostgresPoolStorage, st *state.State, chainID uint64, ctx context.Context) *pool.Pool { +func setupPool(t *testing.T, cfg pool.Config, s *pgpoolstorage.PostgresPoolStorage, st *state.State, chainID uint64, ctx context.Context) *pool.Pool { p := pool.NewPool(cfg, s, st, l2BridgeAddr, chainID) err := p.SetGasPrice(ctx, gasPrice.Uint64()) - if err != nil { - t.Error(err) - } + require.NoError(t, err) p.StartPollingMinSuggestedGasPrice(ctx) return p } @@ -1308,9 +1284,7 @@ func Test_AvoidDuplicatedClaims(t *testing.T) { initOrResetDB(t) stateSqlDB, err := db.NewSQLDB(stateDBCfg) - if err != nil { - t.Error(err) - } + require.NoError(t, err) defer stateSqlDB.Close() //nolint:gosec,errcheck st := newState(stateSqlDB) @@ -1331,11 +1305,8 @@ func Test_AvoidDuplicatedClaims(t *testing.T) { require.NoError(t, dbTx.Commit(ctx)) s, err := pgpoolstorage.NewPostgresPoolStorage(poolDBCfg) - - if err != nil { - t.Error(err) - } - p := setupPool(t, s, st, chainID.Uint64(), ctx) + require.NoError(t, err) + p := setupPool(t, cfg, s, st, chainID.Uint64(), ctx) privateKey, err := crypto.HexToECDSA(strings.TrimPrefix(senderPrivateKey, "0x")) require.NoError(t, err) From 4c31415342bfa0c6c70e60b2cad2b8336f336122 Mon Sep 17 00:00:00 2001 From: tclemos Date: Mon, 3 Apr 2023 11:59:18 -0300 Subject: [PATCH 7/7] remove unnecessary db check for blocked address --- pool/interfaces.go | 1 - pool/pgpoolstorage/pgpoolstorage.go | 16 ---------------- pool/pool.go | 12 +++++------- 3 files changed, 5 insertions(+), 24 deletions(-) diff --git a/pool/interfaces.go b/pool/interfaces.go index 572fcf7988..da5f866b6d 100644 --- a/pool/interfaces.go +++ b/pool/interfaces.go @@ -33,7 +33,6 @@ type storage interface { DeleteTransactionByHash(ctx context.Context, hash common.Hash) error MarkWIPTxsAsPending(ctx context.Context) error GetAllAddressesBlocked(ctx context.Context) ([]common.Address, error) - IsAddressBlocked(ctx context.Context, address common.Address) (bool, error) MinGasPriceSince(ctx context.Context, timestamp time.Time) (uint64, error) DepositCountExists(ctx context.Context, depositCount uint64) (bool, error) } diff --git a/pool/pgpoolstorage/pgpoolstorage.go b/pool/pgpoolstorage/pgpoolstorage.go index 077ff5ca07..883b597de2 100644 --- a/pool/pgpoolstorage/pgpoolstorage.go +++ b/pool/pgpoolstorage/pgpoolstorage.go @@ -683,22 +683,6 @@ func (p *PostgresPoolStorage) GetAllAddressesBlocked(ctx context.Context) ([]com return addrs, nil } -// IsAddressBlocked check if an address is added to the blocked table -func (p *PostgresPoolStorage) IsAddressBlocked(ctx context.Context, address common.Address) (bool, error) { - sql := `SELECT addr FROM pool.blocked WHERE addr = $1` - var addr string - err := p.db.QueryRow(ctx, sql, address.String()).Scan(&addr) - if err != nil { - if errors.Is(err, pgx.ErrNoRows) { - return false, nil - } else { - return false, err - } - } - - return true, nil -} - // DepositCountExists checks if already exists a transaction in the pool with the // provided deposit count func (p *PostgresPoolStorage) DepositCountExists(ctx context.Context, depositCount uint64) (bool, error) { diff --git a/pool/pool.go b/pool/pool.go index d9952a1163..c966eaa6e4 100644 --- a/pool/pool.go +++ b/pool/pool.go @@ -85,23 +85,21 @@ func (p *Pool) refreshBlockedAddresses() { return } + blockedAddressesMap := sync.Map{} for _, blockedAddress := range blockedAddresses { + blockedAddressesMap.Store(blockedAddress.String(), 1) p.blockedAddresses.Store(blockedAddress.String(), 1) } unblockedAddresses := []string{} p.blockedAddresses.Range(func(key, value any) bool { addrHex := key.(string) - addr := common.HexToAddress(addrHex) - blocked, err := p.storage.IsAddressBlocked(context.Background(), addr) - if err != nil { - log.Error("failed to check if %v is blocked", addrHex) + _, found := blockedAddressesMap.Load(addrHex) + if found { return true } - if !blocked { - unblockedAddresses = append(unblockedAddresses, addrHex) - } + unblockedAddresses = append(unblockedAddresses, addrHex) return true })