From a86f233e75e45e601e0680df156afdb4b2d02e53 Mon Sep 17 00:00:00 2001 From: krehermann Date: Tue, 26 Sep 2023 19:08:46 -0600 Subject: [PATCH] WIP: add evm-scoped db type to ensure that services are not violating db boundaries. hint: they are and it's painful --- core/chains/evm/chain.go | 6 +- core/chains/evm/db/db.go | 39 +++++ core/chains/evm/evm_txm.go | 5 +- .../evm/forwarders/forwarder_manager.go | 4 +- .../evm/forwarders/forwarder_manager_test.go | 5 +- core/chains/evm/forwarders/orm.go | 5 +- core/chains/evm/forwarders/orm_test.go | 17 ++- .../evm/headtracker/head_broadcaster_test.go | 6 +- .../chains/evm/headtracker/head_saver_test.go | 7 +- .../evm/headtracker/head_tracker_test.go | 80 +++++----- core/chains/evm/headtracker/orm.go | 19 ++- core/chains/evm/headtracker/orm_test.go | 34 ++--- core/chains/evm/legacy_container.go | 37 +++++ core/chains/evm/log/orm.go | 7 +- .../evm/logpoller/observability_test.go | 6 +- core/chains/evm/logpoller/orm.go | 143 +++++++++--------- core/chains/evm/txmgr/builder.go | 5 +- core/chains/evm/txmgr/evm_tx_store.go | 5 +- core/cmd/ocr2vrf_configure_commands.go | 7 +- core/cmd/shell.go | 3 +- core/cmd/shell_local.go | 8 +- core/cmd/shell_local_test.go | 5 +- core/internal/cltest/cltest.go | 8 +- core/internal/cltest/factories.go | 3 +- core/internal/cltest/heavyweight/orm.go | 9 +- core/internal/cltest/job_factories.go | 4 +- core/internal/features/features_test.go | 10 +- .../features/ocr2/features_ocr2_test.go | 2 +- core/internal/testutils/evmtest/db/dbtest.go | 15 ++ core/internal/testutils/evmtest/evmtest.go | 12 +- core/internal/testutils/pgtest/pgtest.go | 22 +-- core/services/blockhashstore/bhs_test.go | 10 +- core/services/blockhashstore/delegate_test.go | 8 +- core/services/chainlink/application.go | 24 ++- .../chainlink/relayer_chain_interoperators.go | 13 +- .../relayer_chain_interoperators_test.go | 6 +- core/services/cron/cron_test.go | 15 +- core/services/directrequest/delegate_test.go | 20 ++- core/services/feeds/orm_test.go | 4 +- core/services/feeds/service_test.go | 10 +- .../fluxmonitorv2/integrations_test.go | 7 +- core/services/fluxmonitorv2/orm_test.go | 17 ++- core/services/functions/listener_test.go | 9 +- core/services/job/helpers_test.go | 4 +- core/services/job/job_orm_test.go | 73 ++++----- .../job/job_pipeline_orm_integration_test.go | 5 +- core/services/job/runner_integration_test.go | 8 +- core/services/job/spawner_test.go | 9 +- core/services/keeper/integration_test.go | 9 +- .../keeper/registry1_1_synchronizer_test.go | 22 +-- .../keeper/registry1_2_synchronizer_test.go | 24 +-- .../keeper/registry1_3_synchronizer_test.go | 33 ++-- .../registry_synchronizer_helper_test.go | 4 +- core/services/keeper/upkeep_executer_test.go | 4 +- core/services/keystore/eth_test.go | 4 +- core/services/ocr2/delegate_test.go | 4 +- .../evm21/logprovider/integration_test.go | 13 +- .../plugins/ocr2keeper/integration_test.go | 2 +- .../internal/ocr2vrf_integration_test.go | 2 +- core/services/pg/locked_db.go | 3 +- core/services/pipeline/orm_test.go | 15 +- core/services/pipeline/runner_test.go | 9 +- core/services/pipeline/task.eth_call_test.go | 8 +- core/services/pipeline/task.eth_tx_test.go | 6 +- .../promreporter/prom_reporter_test.go | 12 +- core/services/relay/evm/config_poller_test.go | 36 ++--- core/services/relay/evm/evm.go | 14 +- core/services/relay/evm/evm_test.go | 4 +- .../relay/evm/functions/config_poller_test.go | 9 +- .../relay/evm/mercury/helpers_test.go | 6 +- core/services/relay/evm/mercury/orm.go | 1 + .../relay/evm/relayer_extender_test.go | 3 +- core/services/vrf/delegate_test.go | 14 +- core/services/vrf/v1/integration_test.go | 4 +- core/web/evm_forwarders_controller.go | 6 +- core/web/evm_transactions_controller_test.go | 6 +- core/web/evm_tx_attempts_controller_test.go | 2 +- 77 files changed, 600 insertions(+), 459 deletions(-) create mode 100644 core/chains/evm/db/db.go create mode 100644 core/chains/evm/legacy_container.go create mode 100644 core/internal/testutils/evmtest/db/dbtest.go diff --git a/core/chains/evm/chain.go b/core/chains/evm/chain.go index 9a2fd608031..ec551274eec 100644 --- a/core/chains/evm/chain.go +++ b/core/chains/evm/chain.go @@ -11,8 +11,6 @@ import ( "go.uber.org/multierr" "golang.org/x/exp/maps" - "github.com/smartcontractkit/sqlx" - gotoml "github.com/pelletier/go-toml/v2" "github.com/smartcontractkit/chainlink-relay/pkg/types" @@ -22,6 +20,7 @@ import ( evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" evmconfig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/config" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/config/toml" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/headtracker" httypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/headtracker/types" @@ -162,13 +161,14 @@ func (c ChainRelayExtenderConfig) Validate() error { } type ChainOpts struct { + // TODO BCF-2509 does this need the entire app config? AppConfig AppConfig EventBroadcaster pg.EventBroadcaster MailMon *utils.MailboxMonitor GasEstimator gas.EvmFeeEstimator - *sqlx.DB + DB *evmdb.ScopedDB // TODO BCF-2513 remove test code from the API // Gen-functions are useful for dependency injection by tests diff --git a/core/chains/evm/db/db.go b/core/chains/evm/db/db.go new file mode 100644 index 00000000000..c167fa1c958 --- /dev/null +++ b/core/chains/evm/db/db.go @@ -0,0 +1,39 @@ +package db + +import ( + "net/url" + + "github.com/google/uuid" + "github.com/smartcontractkit/sqlx" + + "github.com/smartcontractkit/chainlink/v2/core/config" + "github.com/smartcontractkit/chainlink/v2/core/services/pg" +) + +type ScopedDB struct { + *sqlx.DB +} + +var schema = "evm" + +func NewScopedDB(uuid uuid.UUID, cfg config.Database) (*ScopedDB, error) { + db, err := pg.OpenUnlockedDB(uuid, cfg, pg.WithSchema(schema)) + if err != nil { + return nil, err + } + return &ScopedDB{DB: db}, nil +} + +func (s *ScopedDB) SqlxDB() *sqlx.DB { + return s.DB +} + +func ScopedConnection(dbURL url.URL) (evmScopedConnection url.URL) { + return pg.SchemaScopedConnection(dbURL, schema) +} + +func UseEVMSchema() pg.ConnectionOpt { + return func(u *url.URL) error { + return pg.WithSchema(schema)(u) + } +} diff --git a/core/chains/evm/evm_txm.go b/core/chains/evm/evm_txm.go index d2f4178c7d9..3c2e9eb048c 100644 --- a/core/chains/evm/evm_txm.go +++ b/core/chains/evm/evm_txm.go @@ -3,10 +3,9 @@ package evm import ( "fmt" - "github.com/smartcontractkit/sqlx" - evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" evmconfig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/config" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr" @@ -14,7 +13,7 @@ import ( ) func newEvmTxm( - db *sqlx.DB, + db *evmdb.ScopedDB, cfg evmconfig.EVM, evmRPCEnabled bool, databaseConfig txmgr.DatabaseConfig, diff --git a/core/chains/evm/forwarders/forwarder_manager.go b/core/chains/evm/forwarders/forwarder_manager.go index 80b8b3d8b94..c2fae684614 100644 --- a/core/chains/evm/forwarders/forwarder_manager.go +++ b/core/chains/evm/forwarders/forwarder_manager.go @@ -9,9 +9,9 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/pkg/errors" - "github.com/smartcontractkit/sqlx" evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" evmlogpoller "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller" evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types" "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/authorized_forwarder" @@ -52,7 +52,7 @@ type FwdMgr struct { wg sync.WaitGroup } -func NewFwdMgr(db *sqlx.DB, client evmclient.Client, logpoller evmlogpoller.LogPoller, l logger.Logger, cfg Config, dbConfig pg.QConfig) *FwdMgr { +func NewFwdMgr(db *evmdb.ScopedDB, client evmclient.Client, logpoller evmlogpoller.LogPoller, l logger.Logger, cfg Config, dbConfig pg.QConfig) *FwdMgr { lggr := logger.Sugared(l.Named("EVMForwarderManager")) fwdMgr := FwdMgr{ logger: lggr, diff --git a/core/chains/evm/forwarders/forwarder_manager_test.go b/core/chains/evm/forwarders/forwarder_manager_test.go index bcbe43ef9b6..b281d85b810 100644 --- a/core/chains/evm/forwarders/forwarder_manager_test.go +++ b/core/chains/evm/forwarders/forwarder_manager_test.go @@ -21,6 +21,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/pg" @@ -33,8 +34,8 @@ var SimpleOracleCallABI = evmtypes.MustGetABI(operator_wrapper.OperatorABI).Meth func TestFwdMgr_MaybeForwardTransaction(t *testing.T) { lggr := logger.TestLogger(t) - db := pgtest.NewSqlxDB(t) cfg := configtest.NewTestGeneralConfig(t) + db := evmtestdb.NewScopedDB(t, cfg.Database()) evmcfg := evmtest.NewChainScopedConfig(t, cfg) owner := testutils.MustNewSimTransactor(t) @@ -92,8 +93,8 @@ func TestFwdMgr_MaybeForwardTransaction(t *testing.T) { func TestFwdMgr_AccountUnauthorizedToForward_SkipsForwarding(t *testing.T) { lggr := logger.TestLogger(t) - db := pgtest.NewSqlxDB(t) cfg := configtest.NewTestGeneralConfig(t) + db := evmtestdb.NewScopedDB(t, cfg.Database()) evmcfg := evmtest.NewChainScopedConfig(t, cfg) owner := testutils.MustNewSimTransactor(t) ec := backends.NewSimulatedBackend(map[common.Address]core.GenesisAccount{ diff --git a/core/chains/evm/forwarders/orm.go b/core/chains/evm/forwarders/orm.go index 287698d22f6..db21134bbc6 100644 --- a/core/chains/evm/forwarders/orm.go +++ b/core/chains/evm/forwarders/orm.go @@ -7,6 +7,7 @@ import ( "github.com/pkg/errors" "github.com/smartcontractkit/sqlx" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/pg" "github.com/smartcontractkit/chainlink/v2/core/utils" @@ -28,8 +29,8 @@ type orm struct { var _ ORM = (*orm)(nil) -func NewORM(db *sqlx.DB, lggr logger.Logger, cfg pg.QConfig) *orm { - return &orm{pg.NewQ(db, lggr, cfg)} +func NewORM(db *evmdb.ScopedDB, lggr logger.Logger, cfg pg.QConfig) *orm { + return &orm{pg.NewQ(db.SqlxDB(), lggr, cfg)} } // CreateForwarder creates the Forwarder address associated with the current EVM chain id. diff --git a/core/chains/evm/forwarders/orm_test.go b/core/chains/evm/forwarders/orm_test.go index a3d5c2831fe..008609969c2 100644 --- a/core/chains/evm/forwarders/orm_test.go +++ b/core/chains/evm/forwarders/orm_test.go @@ -1,4 +1,4 @@ -package forwarders +package forwarders_test import ( "database/sql" @@ -9,27 +9,30 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" + "github.com/smartcontractkit/chainlink/v2/core/chains/evm/forwarders" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" + configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/pg" "github.com/smartcontractkit/chainlink/v2/core/utils" - - "github.com/smartcontractkit/sqlx" ) type TestORM struct { - ORM - db *sqlx.DB + forwarders.ORM + db *evmdb.ScopedDB } func setupORM(t *testing.T) *TestORM { t.Helper() var ( - db = pgtest.NewSqlxDB(t) + cfg = configtest.NewTestGeneralConfig(t) + db = evmtestdb.NewScopedDB(t, cfg.Database()) lggr = logger.TestLogger(t) - orm = NewORM(db, lggr, pgtest.NewQConfig(true)) + orm = forwarders.NewORM(db, lggr, pgtest.NewQConfig(true)) ) return &TestORM{ORM: orm, db: db} diff --git a/core/chains/evm/headtracker/head_broadcaster_test.go b/core/chains/evm/headtracker/head_broadcaster_test.go index c478920e00e..7dc383aaf09 100644 --- a/core/chains/evm/headtracker/head_broadcaster_test.go +++ b/core/chains/evm/headtracker/head_broadcaster_test.go @@ -19,7 +19,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" @@ -47,7 +47,7 @@ func TestHeadBroadcaster_Subscribe(t *testing.T) { c.EVM[0].HeadTracker.SamplingInterval = &models.Duration{} }) evmCfg := evmtest.NewChainScopedConfig(t, cfg) - db := pgtest.NewSqlxDB(t) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) logger := logger.TestLogger(t) sub := commonmocks.NewSubscription(t) @@ -69,7 +69,7 @@ func TestHeadBroadcaster_Subscribe(t *testing.T) { checker2 := &cltest.MockHeadTrackable{} hb := headtracker.NewHeadBroadcaster(logger) - orm := headtracker.NewORM(db, logger, cfg.Database(), *ethClient.ConfiguredChainID()) + orm := headtracker.NewORM(evmdb, logger, cfg.Database(), *ethClient.ConfiguredChainID()) hs := headtracker.NewHeadSaver(logger, orm, evmCfg.EVM(), evmCfg.EVM().HeadTracker()) mailMon := utils.NewMailboxMonitor(t.Name()) ht := headtracker.NewHeadTracker(logger, ethClient, evmCfg.EVM(), evmCfg.EVM().HeadTracker(), hb, hs, mailMon) diff --git a/core/chains/evm/headtracker/head_saver_test.go b/core/chains/evm/headtracker/head_saver_test.go index 5ab43679f4b..889f4edbde6 100644 --- a/core/chains/evm/headtracker/head_saver_test.go +++ b/core/chains/evm/headtracker/head_saver_test.go @@ -11,7 +11,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/logger" ) @@ -42,11 +42,12 @@ func (c *config) BlockEmissionIdleWarningThreshold() time.Duration { } func configureSaver(t *testing.T) (httypes.HeadSaver, headtracker.ORM) { - db := pgtest.NewSqlxDB(t) lggr := logger.TestLogger(t) cfg := configtest.NewGeneralConfig(t, nil) + + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) htCfg := &config{finalityDepth: uint32(1)} - orm := headtracker.NewORM(db, lggr, cfg.Database(), cltest.FixtureChainID) + orm := headtracker.NewORM(evmdb, lggr, cfg.Database(), cltest.FixtureChainID) saver := headtracker.NewHeadSaver(lggr, orm, htCfg, &headTrackerConfig{historyDepth: 6}) return saver, orm } diff --git a/core/chains/evm/headtracker/head_tracker_test.go b/core/chains/evm/headtracker/head_tracker_test.go index ad582f4fb93..f466eb210c5 100644 --- a/core/chains/evm/headtracker/head_tracker_test.go +++ b/core/chains/evm/headtracker/head_tracker_test.go @@ -20,24 +20,24 @@ import ( gethCommon "github.com/ethereum/go-ethereum/common" gethTypes "github.com/ethereum/go-ethereum/core/types" "github.com/onsi/gomega" - "github.com/smartcontractkit/sqlx" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" commonmocks "github.com/smartcontractkit/chainlink/v2/common/types/mocks" evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/headtracker" httypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/headtracker/types" evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types" "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/utils" ) -func firstHead(t *testing.T, db *sqlx.DB) (h evmtypes.Head) { +func firstHead(t *testing.T, db *evmdb.ScopedDB) (h evmtypes.Head) { if err := db.Get(&h, `SELECT * FROM heads ORDER BY number ASC LIMIT 1`); err != nil { t.Fatal(err) } @@ -48,12 +48,12 @@ func TestHeadTracker_New(t *testing.T) { t.Parallel() config := cltest.NewTestChainScopedConfig(t) - db := evmtest.NewScopedDB(t, config.Database()) + evmdb := evmtestdb.NewScopedDB(t, config.Database()) logger := logger.TestLogger(t) ethClient := evmtest.NewEthClientMockWithDefaultChain(t) ethClient.On("HeadByNumber", mock.Anything, (*big.Int)(nil)).Return(cltest.Head(0), nil) - orm := headtracker.NewORM(db, logger, config.Database(), cltest.FixtureChainID) + orm := headtracker.NewORM(evmdb, logger, config.Database(), cltest.FixtureChainID) assert.Nil(t, orm.IdempotentInsertHead(testutils.Context(t), cltest.Head(1))) last := cltest.Head(16) assert.Nil(t, orm.IdempotentInsertHead(testutils.Context(t), last)) @@ -72,7 +72,7 @@ func TestHeadTracker_Save_InsertsAndTrimsTable(t *testing.T) { t.Parallel() config := cltest.NewTestChainScopedConfig(t) - db := evmtest.NewScopedDB(t, config.Database()) + db := evmtestdb.NewScopedDB(t, config.Database()) logger := logger.TestLogger(t) ethClient := evmtest.NewEthClientMockWithDefaultChain(t) @@ -116,10 +116,10 @@ func TestHeadTracker_Get(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - db := pgtest.NewSqlxDB(t) logger := logger.TestLogger(t) config := cltest.NewTestChainScopedConfig(t) - orm := headtracker.NewORM(db, logger, config.Database(), cltest.FixtureChainID) + evmdb := evmtestdb.NewScopedDB(t, config.Database()) + orm := headtracker.NewORM(evmdb, logger, config.Database(), cltest.FixtureChainID) ethClient := evmtest.NewEthClientMockWithDefaultChain(t) chStarted := make(chan struct{}) @@ -163,10 +163,10 @@ func TestHeadTracker_Get(t *testing.T) { func TestHeadTracker_Start_NewHeads(t *testing.T) { t.Parallel() - db := pgtest.NewSqlxDB(t) logger := logger.TestLogger(t) config := cltest.NewTestChainScopedConfig(t) - orm := headtracker.NewORM(db, logger, config.Database(), cltest.FixtureChainID) + evmdb := evmtestdb.NewScopedDB(t, config.Database()) + orm := headtracker.NewORM(evmdb, logger, config.Database(), cltest.FixtureChainID) ethClient := evmtest.NewEthClientMockWithDefaultChain(t) chStarted := make(chan struct{}) @@ -188,10 +188,10 @@ func TestHeadTracker_Start_NewHeads(t *testing.T) { func TestHeadTracker_Start_CancelContext(t *testing.T) { t.Parallel() - db := pgtest.NewSqlxDB(t) logger := logger.TestLogger(t) config := cltest.NewTestChainScopedConfig(t) - orm := headtracker.NewORM(db, logger, config.Database(), cltest.FixtureChainID) + evmdb := evmtestdb.NewScopedDB(t, config.Database()) + orm := headtracker.NewORM(evmdb, logger, config.Database(), cltest.FixtureChainID) ethClient := evmtest.NewEthClientMockWithDefaultChain(t) chStarted := make(chan struct{}) ethClient.On("HeadByNumber", mock.Anything, (*big.Int)(nil)).Run(func(args mock.Arguments) { @@ -228,10 +228,10 @@ func TestHeadTracker_CallsHeadTrackableCallbacks(t *testing.T) { t.Parallel() g := gomega.NewWithT(t) - db := pgtest.NewSqlxDB(t) logger := logger.TestLogger(t) config := cltest.NewTestChainScopedConfig(t) - orm := headtracker.NewORM(db, logger, config.Database(), cltest.FixtureChainID) + evmdb := evmtestdb.NewScopedDB(t, config.Database()) + orm := headtracker.NewORM(evmdb, logger, config.Database(), cltest.FixtureChainID) ethClient := evmtest.NewEthClientMockWithDefaultChain(t) @@ -267,10 +267,10 @@ func TestHeadTracker_ReconnectOnError(t *testing.T) { t.Parallel() g := gomega.NewWithT(t) - db := pgtest.NewSqlxDB(t) logger := logger.TestLogger(t) config := cltest.NewTestChainScopedConfig(t) - orm := headtracker.NewORM(db, logger, config.Database(), cltest.FixtureChainID) + evmdb := evmtestdb.NewScopedDB(t, config.Database()) + orm := headtracker.NewORM(evmdb, logger, config.Database(), cltest.FixtureChainID) ethClient := evmtest.NewEthClientMockWithDefaultChain(t) mockEth := &evmtest.MockEth{EthClient: ethClient} @@ -303,10 +303,10 @@ func TestHeadTracker_ResubscribeOnSubscriptionError(t *testing.T) { t.Parallel() g := gomega.NewWithT(t) - db := pgtest.NewSqlxDB(t) logger := logger.TestLogger(t) config := cltest.NewTestChainScopedConfig(t) - orm := headtracker.NewORM(db, logger, config.Database(), cltest.FixtureChainID) + evmdb := evmtestdb.NewScopedDB(t, config.Database()) + orm := headtracker.NewORM(evmdb, logger, config.Database(), cltest.FixtureChainID) ethClient := evmtest.NewEthClientMockWithDefaultChain(t) @@ -350,9 +350,9 @@ func TestHeadTracker_ResubscribeOnSubscriptionError(t *testing.T) { func TestHeadTracker_Start_LoadsLatestChain(t *testing.T) { t.Parallel() - db := pgtest.NewSqlxDB(t) logger := logger.TestLogger(t) config := cltest.NewTestChainScopedConfig(t) + evmdb := evmtestdb.NewScopedDB(t, config.Database()) ethClient := evmtest.NewEthClientMockWithDefaultChain(t) heads := []*evmtypes.Head{ @@ -385,7 +385,7 @@ func TestHeadTracker_Start_LoadsLatestChain(t *testing.T) { func(ctx context.Context, ch chan<- *evmtypes.Head) error { return nil }, ) - orm := headtracker.NewORM(db, logger, config.Database(), cltest.FixtureChainID) + orm := headtracker.NewORM(evmdb, logger, config.Database(), cltest.FixtureChainID) trackable := &cltest.MockHeadTrackable{} ht := createHeadTrackerWithChecker(t, ethClient, config.EVM(), config.EVM().HeadTracker(), orm, trackable) @@ -415,7 +415,6 @@ func TestHeadTracker_Start_LoadsLatestChain(t *testing.T) { func TestHeadTracker_SwitchesToLongestChainWithHeadSamplingEnabled(t *testing.T) { t.Parallel() - db := pgtest.NewSqlxDB(t) logger := logger.TestLogger(t) config := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) { @@ -425,10 +424,11 @@ func TestHeadTracker_SwitchesToLongestChainWithHeadSamplingEnabled(t *testing.T) c.EVM[0].HeadTracker.SamplingInterval = models.MustNewDuration(2500 * time.Millisecond) }) + evmdb := evmtestdb.NewScopedDB(t, config.Database()) ethClient := evmtest.NewEthClientMockWithDefaultChain(t) checker := commonmocks.NewHeadTrackable[*evmtypes.Head, gethCommon.Hash](t) - orm := headtracker.NewORM(db, logger, config.Database(), *evmtest.MustGetDefaultChainID(t, config.EVMConfigs())) + orm := headtracker.NewORM(evmdb, logger, config.Database(), *evmtest.MustGetDefaultChainID(t, config.EVMConfigs())) csCfg := evmtest.NewChainScopedConfig(t, config) ht := createHeadTrackerWithChecker(t, ethClient, csCfg.EVM(), csCfg.EVM().HeadTracker(), orm, checker) @@ -543,7 +543,6 @@ func TestHeadTracker_SwitchesToLongestChainWithHeadSamplingEnabled(t *testing.T) func TestHeadTracker_SwitchesToLongestChainWithHeadSamplingDisabled(t *testing.T) { t.Parallel() - db := pgtest.NewSqlxDB(t) logger := logger.TestLogger(t) config := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) { @@ -555,8 +554,9 @@ func TestHeadTracker_SwitchesToLongestChainWithHeadSamplingDisabled(t *testing.T ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + evmdb := evmtestdb.NewScopedDB(t, config.Database()) checker := commonmocks.NewHeadTrackable[*evmtypes.Head, gethCommon.Hash](t) - orm := headtracker.NewORM(db, logger, config.Database(), cltest.FixtureChainID) + orm := headtracker.NewORM(evmdb, logger, config.Database(), cltest.FixtureChainID) evmcfg := evmtest.NewChainScopedConfig(t, config) ht := createHeadTrackerWithChecker(t, ethClient, evmcfg.EVM(), evmcfg.EVM().HeadTracker(), orm, checker) @@ -770,10 +770,10 @@ func TestHeadTracker_Backfill(t *testing.T) { ctx := testutils.Context(t) t.Run("does nothing if all the heads are in database", func(t *testing.T) { - db := pgtest.NewSqlxDB(t) cfg := configtest.NewGeneralConfig(t, nil) logger := logger.TestLogger(t) - orm := headtracker.NewORM(db, logger, cfg.Database(), cltest.FixtureChainID) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) + orm := headtracker.NewORM(evmdb, logger, cfg.Database(), cltest.FixtureChainID) for i := range heads { require.NoError(t, orm.IdempotentInsertHead(testutils.Context(t), &heads[i])) } @@ -787,10 +787,10 @@ func TestHeadTracker_Backfill(t *testing.T) { }) t.Run("fetches a missing head", func(t *testing.T) { - db := pgtest.NewSqlxDB(t) cfg := configtest.NewGeneralConfig(t, nil) logger := logger.TestLogger(t) - orm := headtracker.NewORM(db, logger, cfg.Database(), cltest.FixtureChainID) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) + orm := headtracker.NewORM(evmdb, logger, cfg.Database(), cltest.FixtureChainID) for i := range heads { require.NoError(t, orm.IdempotentInsertHead(testutils.Context(t), &heads[i])) } @@ -823,10 +823,10 @@ func TestHeadTracker_Backfill(t *testing.T) { }) t.Run("fetches only heads that are missing", func(t *testing.T) { - db := pgtest.NewSqlxDB(t) cfg := configtest.NewGeneralConfig(t, nil) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) logger := logger.TestLogger(t) - orm := headtracker.NewORM(db, logger, cfg.Database(), cltest.FixtureChainID) + orm := headtracker.NewORM(evmdb, logger, cfg.Database(), cltest.FixtureChainID) for i := range heads { require.NoError(t, orm.IdempotentInsertHead(testutils.Context(t), &heads[i])) } @@ -856,10 +856,10 @@ func TestHeadTracker_Backfill(t *testing.T) { }) t.Run("does not backfill if chain length is already greater than or equal to depth", func(t *testing.T) { - db := pgtest.NewSqlxDB(t) cfg := configtest.NewGeneralConfig(t, nil) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) logger := logger.TestLogger(t) - orm := headtracker.NewORM(db, logger, cfg.Database(), cltest.FixtureChainID) + orm := headtracker.NewORM(evmdb, logger, cfg.Database(), cltest.FixtureChainID) for i := range heads { require.NoError(t, orm.IdempotentInsertHead(testutils.Context(t), &heads[i])) } @@ -877,10 +877,10 @@ func TestHeadTracker_Backfill(t *testing.T) { }) t.Run("only backfills to height 0 if chain length would otherwise cause it to try and fetch a negative head", func(t *testing.T) { - db := pgtest.NewSqlxDB(t) cfg := configtest.NewGeneralConfig(t, nil) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) logger := logger.TestLogger(t) - orm := headtracker.NewORM(db, logger, cfg.Database(), cltest.FixtureChainID) + orm := headtracker.NewORM(evmdb, logger, cfg.Database(), cltest.FixtureChainID) ethClient := evmtest.NewEthClientMock(t) ethClient.On("ConfiguredChainID", mock.Anything).Return(evmtest.MustGetDefaultChainID(t, cfg.EVMConfigs()), nil) @@ -902,10 +902,10 @@ func TestHeadTracker_Backfill(t *testing.T) { }) t.Run("abandons backfill and returns error if the eth node returns not found", func(t *testing.T) { - db := pgtest.NewSqlxDB(t) cfg := configtest.NewGeneralConfig(t, nil) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) logger := logger.TestLogger(t) - orm := headtracker.NewORM(db, logger, cfg.Database(), cltest.FixtureChainID) + orm := headtracker.NewORM(evmdb, logger, cfg.Database(), cltest.FixtureChainID) for i := range heads { require.NoError(t, orm.IdempotentInsertHead(testutils.Context(t), &heads[i])) } @@ -933,10 +933,10 @@ func TestHeadTracker_Backfill(t *testing.T) { }) t.Run("abandons backfill and returns error if the context time budget is exceeded", func(t *testing.T) { - db := pgtest.NewSqlxDB(t) cfg := configtest.NewGeneralConfig(t, nil) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) logger := logger.TestLogger(t) - orm := headtracker.NewORM(db, logger, cfg.Database(), cltest.FixtureChainID) + orm := headtracker.NewORM(evmdb, logger, cfg.Database(), cltest.FixtureChainID) for i := range heads { require.NoError(t, orm.IdempotentInsertHead(testutils.Context(t), &heads[i])) } @@ -962,10 +962,10 @@ func TestHeadTracker_Backfill(t *testing.T) { }) t.Run("abandons backfill and returns error when fetching a block by hash fails, indicating a reorg", func(t *testing.T) { - db := pgtest.NewSqlxDB(t) cfg := configtest.NewGeneralConfig(t, nil) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) logger := logger.TestLogger(t) - orm := headtracker.NewORM(db, logger, cfg.Database(), cltest.FixtureChainID) + orm := headtracker.NewORM(evmdb, logger, cfg.Database(), cltest.FixtureChainID) ethClient := evmtest.NewEthClientMock(t) ethClient.On("ConfiguredChainID", mock.Anything).Return(evmtest.MustGetDefaultChainID(t, cfg.EVMConfigs()), nil) ethClient.On("HeadByHash", mock.Anything, h14.Hash).Return(&h14, nil).Once() diff --git a/core/chains/evm/headtracker/orm.go b/core/chains/evm/headtracker/orm.go index 426df68b301..9c9d938d885 100644 --- a/core/chains/evm/headtracker/orm.go +++ b/core/chains/evm/headtracker/orm.go @@ -8,8 +8,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/pkg/errors" - "github.com/smartcontractkit/sqlx" - + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/pg" @@ -35,15 +34,15 @@ type orm struct { chainID utils.Big } -func NewORM(db *sqlx.DB, lggr logger.Logger, cfg pg.QConfig, chainID big.Int) ORM { - return &orm{pg.NewQ(db, lggr.Named("HeadTrackerORM"), cfg), utils.Big(chainID)} +func NewORM(db *evmdb.ScopedDB, lggr logger.Logger, cfg pg.QConfig, chainID big.Int) ORM { + return &orm{pg.NewQ(db.SqlxDB(), lggr.Named("HeadTrackerORM"), cfg), utils.Big(chainID)} } func (orm *orm) IdempotentInsertHead(ctx context.Context, head *evmtypes.Head) error { // listener guarantees head.EVMChainID to be equal to orm.chainID q := orm.q.WithOpts(pg.WithParentCtx(ctx)) query := ` - INSERT INTO evm.heads (hash, number, parent_hash, created_at, timestamp, l1_block_number, evm_chain_id, base_fee_per_gas) VALUES ( + INSERT INTO heads (hash, number, parent_hash, created_at, timestamp, l1_block_number, evm_chain_id, base_fee_per_gas) VALUES ( :hash, :number, :parent_hash, :created_at, :timestamp, :l1_block_number, :evm_chain_id, :base_fee_per_gas) ON CONFLICT (evm_chain_id, hash) DO NOTHING` err := q.ExecQNamed(query, head) @@ -53,11 +52,11 @@ func (orm *orm) IdempotentInsertHead(ctx context.Context, head *evmtypes.Head) e func (orm *orm) TrimOldHeads(ctx context.Context, n uint) (err error) { q := orm.q.WithOpts(pg.WithParentCtx(ctx)) return q.ExecQ(` - DELETE FROM evm.heads + DELETE FROM heads WHERE evm_chain_id = $1 AND number < ( SELECT min(number) FROM ( SELECT number - FROM evm.heads + FROM heads WHERE evm_chain_id = $1 ORDER BY number DESC LIMIT $2 @@ -68,7 +67,7 @@ func (orm *orm) TrimOldHeads(ctx context.Context, n uint) (err error) { func (orm *orm) LatestHead(ctx context.Context) (head *evmtypes.Head, err error) { head = new(evmtypes.Head) q := orm.q.WithOpts(pg.WithParentCtx(ctx)) - err = q.Get(head, `SELECT * FROM evm.heads WHERE evm_chain_id = $1 ORDER BY number DESC, created_at DESC, id DESC LIMIT 1`, orm.chainID) + err = q.Get(head, `SELECT * FROM heads WHERE evm_chain_id = $1 ORDER BY number DESC, created_at DESC, id DESC LIMIT 1`, orm.chainID) if errors.Is(err, sql.ErrNoRows) { return nil, nil } @@ -78,7 +77,7 @@ func (orm *orm) LatestHead(ctx context.Context) (head *evmtypes.Head, err error) func (orm *orm) LatestHeads(ctx context.Context, limit uint) (heads []*evmtypes.Head, err error) { q := orm.q.WithOpts(pg.WithParentCtx(ctx)) - err = q.Select(&heads, `SELECT * FROM evm.heads WHERE evm_chain_id = $1 ORDER BY number DESC, created_at DESC, id DESC LIMIT $2`, orm.chainID, limit) + err = q.Select(&heads, `SELECT * FROM heads WHERE evm_chain_id = $1 ORDER BY number DESC, created_at DESC, id DESC LIMIT $2`, orm.chainID, limit) err = errors.Wrap(err, "LatestHeads failed") return } @@ -86,7 +85,7 @@ func (orm *orm) LatestHeads(ctx context.Context, limit uint) (heads []*evmtypes. func (orm *orm) HeadByHash(ctx context.Context, hash common.Hash) (head *evmtypes.Head, err error) { q := orm.q.WithOpts(pg.WithParentCtx(ctx)) head = new(evmtypes.Head) - err = q.Get(head, `SELECT * FROM evm.heads WHERE evm_chain_id = $1 AND hash = $2`, orm.chainID, hash) + err = q.Get(head, `SELECT * FROM heads WHERE evm_chain_id = $1 AND hash = $2`, orm.chainID, hash) if errors.Is(err, sql.ErrNoRows) { return nil, nil } diff --git a/core/chains/evm/headtracker/orm_test.go b/core/chains/evm/headtracker/orm_test.go index 5b106ac1018..40d90b6c94f 100644 --- a/core/chains/evm/headtracker/orm_test.go +++ b/core/chains/evm/headtracker/orm_test.go @@ -13,17 +13,20 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/chains/evm/headtracker" "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/logger" ) +func setupORM(t *testing.T) headtracker.ORM { + logger := logger.TestLogger(t) + cfg := configtest.NewGeneralConfig(t, nil) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) + return headtracker.NewORM(evmdb, logger, cfg.Database(), cltest.FixtureChainID) +} func TestORM_IdempotentInsertHead(t *testing.T) { t.Parallel() - db := pgtest.NewSqlxDB(t) - logger := logger.TestLogger(t) - cfg := configtest.NewGeneralConfig(t, nil) - orm := headtracker.NewORM(db, logger, cfg.Database(), cltest.FixtureChainID) + orm := setupORM(t) // Returns nil when inserting first head head := cltest.Head(0) @@ -46,10 +49,7 @@ func TestORM_IdempotentInsertHead(t *testing.T) { func TestORM_TrimOldHeads(t *testing.T) { t.Parallel() - db := pgtest.NewSqlxDB(t) - logger := logger.TestLogger(t) - cfg := configtest.NewGeneralConfig(t, nil) - orm := headtracker.NewORM(db, logger, cfg.Database(), cltest.FixtureChainID) + orm := setupORM(t) for i := 0; i < 10; i++ { head := cltest.Head(i) @@ -71,10 +71,7 @@ func TestORM_TrimOldHeads(t *testing.T) { func TestORM_HeadByHash(t *testing.T) { t.Parallel() - db := pgtest.NewSqlxDB(t) - logger := logger.TestLogger(t) - cfg := configtest.NewGeneralConfig(t, nil) - orm := headtracker.NewORM(db, logger, cfg.Database(), cltest.FixtureChainID) + orm := setupORM(t) var hash common.Hash for i := 0; i < 10; i++ { @@ -94,10 +91,7 @@ func TestORM_HeadByHash(t *testing.T) { func TestORM_HeadByHash_NotFound(t *testing.T) { t.Parallel() - db := pgtest.NewSqlxDB(t) - logger := logger.TestLogger(t) - cfg := configtest.NewGeneralConfig(t, nil) - orm := headtracker.NewORM(db, logger, cfg.Database(), cltest.FixtureChainID) + orm := setupORM(t) hash := cltest.Head(123).Hash head, err := orm.HeadByHash(testutils.Context(t), hash) @@ -109,11 +103,7 @@ func TestORM_HeadByHash_NotFound(t *testing.T) { func TestORM_LatestHeads_NoRows(t *testing.T) { t.Parallel() - db := pgtest.NewSqlxDB(t) - logger := logger.TestLogger(t) - cfg := configtest.NewGeneralConfig(t, nil) - orm := headtracker.NewORM(db, logger, cfg.Database(), cltest.FixtureChainID) - + orm := setupORM(t) heads, err := orm.LatestHeads(testutils.Context(t), 100) require.Zero(t, len(heads)) diff --git a/core/chains/evm/legacy_container.go b/core/chains/evm/legacy_container.go new file mode 100644 index 00000000000..9e177f500a5 --- /dev/null +++ b/core/chains/evm/legacy_container.go @@ -0,0 +1,37 @@ +package evm + +import ( + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" + "github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr" +) + +type LegacyContainer interface { + DB() *evmdb.ScopedDB + TxmORM() txmgr.EvmTxStore + Chains() LegacyChainContainer +} + +type LegacyContainerImpl struct { + chains *LegacyChains + db *evmdb.ScopedDB + txmorm txmgr.EvmTxStore +} + +func (l *LegacyContainerImpl) DB() *evmdb.ScopedDB { + return l.db +} + +func (l *LegacyContainerImpl) TxmORM() txmgr.EvmTxStore { + return l.txmorm +} + +func (l *LegacyContainerImpl) Chains() LegacyChainContainer { + return l.chains +} +func NewLegacyContainerImpl(db *evmdb.ScopedDB, txm txmgr.EvmTxStore, chains *LegacyChains) *LegacyContainerImpl { + return &LegacyContainerImpl{ + db: db, + txmorm: txm, + chains: chains, + } +} diff --git a/core/chains/evm/log/orm.go b/core/chains/evm/log/orm.go index 4e51940f344..19a280e0a6c 100644 --- a/core/chains/evm/log/orm.go +++ b/core/chains/evm/log/orm.go @@ -9,8 +9,7 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/pkg/errors" - "github.com/smartcontractkit/sqlx" - + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/pg" "github.com/smartcontractkit/chainlink/v2/core/utils" @@ -53,8 +52,8 @@ type orm struct { var _ ORM = (*orm)(nil) -func NewORM(db *sqlx.DB, lggr logger.Logger, cfg pg.QConfig, evmChainID big.Int) *orm { - return &orm{pg.NewQ(db, lggr, cfg), *utils.NewBig(&evmChainID)} +func NewORM(db *evmdb.ScopedDB, lggr logger.Logger, cfg pg.QConfig, evmChainID big.Int) *orm { + return &orm{pg.NewQ(db.SqlxDB(), lggr, cfg), *utils.NewBig(&evmChainID)} } func (o *orm) WasBroadcastConsumed(blockHash common.Hash, logIndex uint, jobID int32, qopts ...pg.QOpt) (consumed bool, err error) { diff --git a/core/chains/evm/logpoller/observability_test.go b/core/chains/evm/logpoller/observability_test.go index 5bd0a772d96..0485fa423f8 100644 --- a/core/chains/evm/logpoller/observability_test.go +++ b/core/chains/evm/logpoller/observability_test.go @@ -15,6 +15,8 @@ import ( "github.com/prometheus/client_golang/prometheus/testutil" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" + configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/pg" @@ -98,7 +100,7 @@ func TestMetricsAreProperlyPopulatedWithLabels(t *testing.T) { func TestNotPublishingDatasetSizeInCaseOfError(t *testing.T) { lp := createObservedPollLogger(t, 420) - _, err := withObservedQueryAndResults(lp, "errorQuery", func() ([]string, error) { return nil, fmt.Errorf("error") }) + _, err := WithObservedQueryAndResults(lp, "errorQuery", func() ([]string, error) { return nil, fmt.Errorf("error") }) require.Error(t, err) require.Equal(t, 1, counterFromHistogramByLabels(t, lp.queryDuration, "420", "errorQuery")) @@ -107,7 +109,7 @@ func TestNotPublishingDatasetSizeInCaseOfError(t *testing.T) { func createObservedPollLogger(t *testing.T, chainId int64) *ObservedLogPoller { lggr, _ := logger.TestLoggerObserved(t, zapcore.ErrorLevel) - db := pgtest.NewSqlxDB(t) + db := evmtestdb.NewScopedDB(t, configtest.NewTestGeneralConfig(t).Database()) orm := NewORM(big.NewInt(chainId), db, lggr, pgtest.NewQConfig(true)) return NewObservedLogPoller( orm, nil, lggr, 1, 1, 1, 1, 1000, diff --git a/core/chains/evm/logpoller/orm.go b/core/chains/evm/logpoller/orm.go index c062ef3e080..398f0737f63 100644 --- a/core/chains/evm/logpoller/orm.go +++ b/core/chains/evm/logpoller/orm.go @@ -11,6 +11,7 @@ import ( "github.com/pkg/errors" "github.com/smartcontractkit/sqlx" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/pg" "github.com/smartcontractkit/chainlink/v2/core/utils" @@ -22,9 +23,9 @@ type ORM struct { } // NewORM creates an ORM scoped to chainID. -func NewORM(chainID *big.Int, db *sqlx.DB, lggr logger.Logger, cfg pg.QConfig) *ORM { +func NewORM(chainID *big.Int, db *evmdb.ScopedDB, lggr logger.Logger, cfg pg.QConfig) *ORM { namedLogger := lggr.Named("Configs") - q := pg.NewQ(db, namedLogger, cfg) + q := pg.NewQ(db.SqlxDB(), namedLogger, cfg) return &ORM{ chainID: chainID, q: q, @@ -34,7 +35,7 @@ func NewORM(chainID *big.Int, db *sqlx.DB, lggr logger.Logger, cfg pg.QConfig) * // InsertBlock is idempotent to support replays. func (o *ORM) InsertBlock(h common.Hash, n int64, t time.Time, qopts ...pg.QOpt) error { q := o.q.WithOpts(qopts...) - err := q.ExecQ(`INSERT INTO evm.log_poller_blocks (evm_chain_id, block_hash, block_number, block_timestamp, created_at) + err := q.ExecQ(`INSERT INTO log_poller_blocks (evm_chain_id, block_hash, block_number, block_timestamp, created_at) VALUES ($1, $2, $3, $4, NOW()) ON CONFLICT DO NOTHING`, utils.NewBig(o.chainID), h[:], n, t) return err } @@ -54,7 +55,7 @@ func (o *ORM) InsertFilter(filter Filter, qopts ...pg.QOpt) (err error) { for _, ev := range filter.EventSigs { events = append(events, ev.Bytes()) } - return q.ExecQ(`INSERT INTO evm.log_poller_filters + return q.ExecQ(`INSERT INTO log_poller_filters (name, evm_chain_id, retention, created_at, address, event) SELECT * FROM (SELECT $1, $2::NUMERIC, $3::BIGINT, NOW()) x, @@ -67,7 +68,7 @@ func (o *ORM) InsertFilter(filter Filter, qopts ...pg.QOpt) (err error) { // DeleteFilter removes all events,address pairs associated with the Filter func (o *ORM) DeleteFilter(name string, qopts ...pg.QOpt) error { q := o.q.WithOpts(qopts...) - return q.ExecQ(`DELETE FROM evm.log_poller_filters WHERE name = $1 AND evm_chain_id = $2`, name, utils.NewBig(o.chainID)) + return q.ExecQ(`DELETE FROM log_poller_filters WHERE name = $1 AND evm_chain_id = $2`, name, utils.NewBig(o.chainID)) } // LoadFiltersForChain returns all filters for this chain @@ -78,7 +79,7 @@ func (o *ORM) LoadFilters(qopts ...pg.QOpt) (map[string]Filter, error) { ARRAY_AGG(DISTINCT address)::BYTEA[] AS addresses, ARRAY_AGG(DISTINCT event)::BYTEA[] AS event_sigs, MAX(retention) AS retention - FROM evm.log_poller_filters WHERE evm_chain_id = $1 + FROM log_poller_filters WHERE evm_chain_id = $1 GROUP BY name`, utils.NewBig(o.chainID)) filters := make(map[string]Filter) for _, filter := range rows { @@ -91,7 +92,7 @@ func (o *ORM) LoadFilters(qopts ...pg.QOpt) (map[string]Filter, error) { func (o *ORM) SelectBlockByHash(h common.Hash, qopts ...pg.QOpt) (*LogPollerBlock, error) { q := o.q.WithOpts(qopts...) var b LogPollerBlock - if err := q.Get(&b, `SELECT * FROM evm.log_poller_blocks WHERE block_hash = $1 AND evm_chain_id = $2`, h, utils.NewBig(o.chainID)); err != nil { + if err := q.Get(&b, `SELECT * FROM log_poller_blocks WHERE block_hash = $1 AND evm_chain_id = $2`, h, utils.NewBig(o.chainID)); err != nil { return nil, err } return &b, nil @@ -100,7 +101,7 @@ func (o *ORM) SelectBlockByHash(h common.Hash, qopts ...pg.QOpt) (*LogPollerBloc func (o *ORM) SelectBlockByNumber(n int64, qopts ...pg.QOpt) (*LogPollerBlock, error) { q := o.q.WithOpts(qopts...) var b LogPollerBlock - if err := q.Get(&b, `SELECT * FROM evm.log_poller_blocks WHERE block_number = $1 AND evm_chain_id = $2`, n, utils.NewBig(o.chainID)); err != nil { + if err := q.Get(&b, `SELECT * FROM log_poller_blocks WHERE block_number = $1 AND evm_chain_id = $2`, n, utils.NewBig(o.chainID)); err != nil { return nil, err } return &b, nil @@ -109,7 +110,7 @@ func (o *ORM) SelectBlockByNumber(n int64, qopts ...pg.QOpt) (*LogPollerBlock, e func (o *ORM) SelectLatestBlock(qopts ...pg.QOpt) (*LogPollerBlock, error) { q := o.q.WithOpts(qopts...) var b LogPollerBlock - if err := q.Get(&b, `SELECT * FROM evm.log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1`, utils.NewBig(o.chainID)); err != nil { + if err := q.Get(&b, `SELECT * FROM log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1`, utils.NewBig(o.chainID)); err != nil { return nil, err } return &b, nil @@ -118,11 +119,11 @@ func (o *ORM) SelectLatestBlock(qopts ...pg.QOpt) (*LogPollerBlock, error) { func (o *ORM) SelectLatestLogEventSigWithConfs(eventSig common.Hash, address common.Address, confs int, qopts ...pg.QOpt) (*Log, error) { q := o.q.WithOpts(qopts...) var l Log - if err := q.Get(&l, `SELECT * FROM evm.logs + if err := q.Get(&l, `SELECT * FROM logs WHERE evm_chain_id = $1 AND event_sig = $2 AND address = $3 - AND (block_number + $4) <= (SELECT COALESCE(block_number, 0) FROM evm.log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) + AND (block_number + $4) <= (SELECT COALESCE(block_number, 0) FROM log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) ORDER BY (block_number, log_index) DESC LIMIT 1`, utils.NewBig(o.chainID), eventSig, address, confs); err != nil { return nil, err } @@ -132,19 +133,19 @@ func (o *ORM) SelectLatestLogEventSigWithConfs(eventSig common.Hash, address com // DeleteBlocksAfter delete all blocks after and including start. func (o *ORM) DeleteBlocksAfter(start int64, qopts ...pg.QOpt) error { q := o.q.WithOpts(qopts...) - return q.ExecQ(`DELETE FROM evm.log_poller_blocks WHERE block_number >= $1 AND evm_chain_id = $2`, start, utils.NewBig(o.chainID)) + return q.ExecQ(`DELETE FROM log_poller_blocks WHERE block_number >= $1 AND evm_chain_id = $2`, start, utils.NewBig(o.chainID)) } // DeleteBlocksBefore delete all blocks before and including end. func (o *ORM) DeleteBlocksBefore(end int64, qopts ...pg.QOpt) error { q := o.q.WithOpts(qopts...) - _, err := q.Exec(`DELETE FROM evm.log_poller_blocks WHERE block_number <= $1 AND evm_chain_id = $2`, end, utils.NewBig(o.chainID)) + _, err := q.Exec(`DELETE FROM log_poller_blocks WHERE block_number <= $1 AND evm_chain_id = $2`, end, utils.NewBig(o.chainID)) return err } func (o *ORM) DeleteLogsAfter(start int64, qopts ...pg.QOpt) error { q := o.q.WithOpts(qopts...) - return q.ExecQ(`DELETE FROM evm.logs WHERE block_number >= $1 AND evm_chain_id = $2`, start, utils.NewBig(o.chainID)) + return q.ExecQ(`DELETE FROM logs WHERE block_number >= $1 AND evm_chain_id = $2`, start, utils.NewBig(o.chainID)) } type Exp struct { @@ -161,9 +162,9 @@ func (o *ORM) DeleteExpiredLogs(qopts ...pg.QOpt) error { return q.ExecQ(`WITH r AS ( SELECT address, event, MAX(retention) AS retention - FROM evm.log_poller_filters WHERE evm_chain_id=$1 + FROM log_poller_filters WHERE evm_chain_id=$1 GROUP BY evm_chain_id,address, event HAVING NOT 0 = ANY(ARRAY_AGG(retention)) - ) DELETE FROM evm.logs l USING r + ) DELETE FROM logs l USING r WHERE l.evm_chain_id = $1 AND l.address=r.address AND l.event_sig=r.event AND l.created_at <= STATEMENT_TIMESTAMP() - (r.retention / 10^9 * interval '1 second')`, // retention is in nanoseconds (time.Duration aka BIGINT) utils.NewBig(o.chainID)) @@ -185,7 +186,7 @@ func (o *ORM) InsertLogs(logs []Log, qopts ...pg.QOpt) error { end = len(logs) } - err := q.ExecQNamed(`INSERT INTO evm.logs + err := q.ExecQNamed(`INSERT INTO logs (evm_chain_id, log_index, block_hash, block_number, block_timestamp, address, event_sig, topics, tx_hash, data, created_at) VALUES (:evm_chain_id, :log_index, :block_hash, :block_number, :block_timestamp, :address, :event_sig, :topics, :tx_hash, :data, NOW()) ON CONFLICT DO NOTHING`, logs[start:end]) @@ -206,7 +207,7 @@ func (o *ORM) InsertLogs(logs []Log, qopts ...pg.QOpt) error { func (o *ORM) SelectLogsByBlockRange(start, end int64) ([]Log, error) { var logs []Log err := o.q.Select(&logs, ` - SELECT * FROM evm.logs + SELECT * FROM logs WHERE block_number >= $1 AND block_number <= $2 AND evm_chain_id = $3 ORDER BY (block_number, log_index, created_at)`, start, end, utils.NewBig(o.chainID)) if err != nil { @@ -220,10 +221,10 @@ func (o *ORM) SelectLogsByBlockRangeFilter(start, end int64, address common.Addr var logs []Log q := o.q.WithOpts(qopts...) err := q.Select(&logs, ` - SELECT * FROM evm.logs - WHERE evm.logs.block_number >= $1 AND evm.logs.block_number <= $2 AND evm.logs.evm_chain_id = $3 + SELECT * FROM logs + WHERE logs.block_number >= $1 AND logs.block_number <= $2 AND logs.evm_chain_id = $3 AND address = $4 AND event_sig = $5 - ORDER BY (evm.logs.block_number, evm.logs.log_index)`, start, end, utils.NewBig(o.chainID), address, eventSig.Bytes()) + ORDER BY (logs.block_number, logs.log_index)`, start, end, utils.NewBig(o.chainID), address, eventSig.Bytes()) if err != nil { return nil, err } @@ -235,12 +236,12 @@ func (o *ORM) SelectLogsCreatedAfter(eventSig []byte, address common.Address, af var logs []Log q := o.q.WithOpts(qopts...) err := q.Select(&logs, ` - SELECT * FROM evm.logs + SELECT * FROM logs WHERE evm_chain_id = $1 AND address = $2 AND event_sig = $3 AND created_at > $4 - AND (block_number + $5) <= (SELECT COALESCE(block_number, 0) FROM evm.log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) + AND (block_number + $5) <= (SELECT COALESCE(block_number, 0) FROM log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) ORDER BY created_at ASC`, utils.NewBig(o.chainID), address, eventSig, after, confs) if err != nil { return nil, err @@ -267,12 +268,12 @@ func (o *ORM) SelectLogsWithSigsByBlockRangeFilter(start, end int64, address com ` SELECT * -FROM evm.logs -WHERE evm.logs.block_number BETWEEN :start AND :end - AND evm.logs.evm_chain_id = :chainid - AND evm.logs.address = :address - AND evm.logs.event_sig IN (:EventSigs) -ORDER BY (evm.logs.block_number, evm.logs.log_index)`, a) +FROM logs +WHERE logs.block_number BETWEEN :start AND :end + AND logs.evm_chain_id = :chainid + AND logs.address = :address + AND logs.event_sig IN (:EventSigs) +ORDER BY (logs.block_number, logs.log_index)`, a) if err != nil { return nil, errors.Wrap(err, "sqlx Named") } @@ -292,7 +293,7 @@ func (o *ORM) GetBlocksRange(start uint64, end uint64, qopts ...pg.QOpt) ([]LogP var blocks []LogPollerBlock q := o.q.WithOpts(qopts...) err := q.Select(&blocks, ` - SELECT * FROM evm.log_poller_blocks + SELECT * FROM log_poller_blocks WHERE block_number >= $1 AND block_number <= $2 AND evm_chain_id = $3 ORDER BY block_number ASC`, start, end, utils.NewBig(o.chainID)) if err != nil { @@ -309,13 +310,13 @@ func (o *ORM) SelectLatestLogEventSigsAddrsWithConfs(fromBlock int64, addresses q := o.q.WithOpts(qopts...) err := q.Select(&logs, ` - SELECT * FROM evm.logs WHERE (block_number, address, event_sig) IN ( - SELECT MAX(block_number), address, event_sig FROM evm.logs + SELECT * FROM logs WHERE (block_number, address, event_sig) IN ( + SELECT MAX(block_number), address, event_sig FROM logs WHERE evm_chain_id = $1 AND event_sig = ANY($2) AND address = ANY($3) AND block_number > $4 AND - block_number <= (SELECT COALESCE(block_number, 0) FROM evm.log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $5 + block_number <= (SELECT COALESCE(block_number, 0) FROM log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $5 GROUP BY event_sig, address ) ORDER BY block_number ASC @@ -334,12 +335,12 @@ func (o *ORM) SelectLatestBlockNumberEventSigsAddrsWithConfs(fromBlock int64, ev q := o.q.WithOpts(qopts...) err := q.Get(&blockNumber, ` - SELECT COALESCE(MAX(block_number), 0) FROM evm.logs + SELECT COALESCE(MAX(block_number), 0) FROM logs WHERE evm_chain_id = $1 AND event_sig = ANY($2) AND address = ANY($3) AND block_number > $4 AND - block_number <= (SELECT COALESCE(block_number, 0) FROM evm.log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $5`, + block_number <= (SELECT COALESCE(block_number, 0) FROM log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $5`, o.chainID.Int64(), sigs, addrs, fromBlock, confs) if err != nil { return 0, err @@ -351,13 +352,13 @@ func (o *ORM) SelectDataWordRange(address common.Address, eventSig common.Hash, var logs []Log q := o.q.WithOpts(qopts...) err := q.Select(&logs, - `SELECT * FROM evm.logs - WHERE evm.logs.evm_chain_id = $1 + `SELECT * FROM logs + WHERE logs.evm_chain_id = $1 AND address = $2 AND event_sig = $3 AND substring(data from 32*$4+1 for 32) >= $5 AND substring(data from 32*$4+1 for 32) <= $6 - AND block_number <= (SELECT COALESCE(block_number, 0) FROM evm.log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $7 - ORDER BY (evm.logs.block_number, evm.logs.log_index)`, utils.NewBig(o.chainID), address, eventSig.Bytes(), wordIndex, wordValueMin.Bytes(), wordValueMax.Bytes(), confs) + AND block_number <= (SELECT COALESCE(block_number, 0) FROM log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $7 + ORDER BY (logs.block_number, logs.log_index)`, utils.NewBig(o.chainID), address, eventSig.Bytes(), wordIndex, wordValueMin.Bytes(), wordValueMax.Bytes(), confs) if err != nil { return nil, err } @@ -368,12 +369,12 @@ func (o *ORM) SelectDataWordGreaterThan(address common.Address, eventSig common. var logs []Log q := o.q.WithOpts(qopts...) err := q.Select(&logs, - `SELECT * FROM evm.logs - WHERE evm.logs.evm_chain_id = $1 + `SELECT * FROM logs + WHERE logs.evm_chain_id = $1 AND address = $2 AND event_sig = $3 AND substring(data from 32*$4+1 for 32) >= $5 - AND block_number <= (SELECT COALESCE(block_number, 0) FROM evm.log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $6 - ORDER BY (evm.logs.block_number, evm.logs.log_index)`, utils.NewBig(o.chainID), address, eventSig.Bytes(), wordIndex, wordValueMin.Bytes(), confs) + AND block_number <= (SELECT COALESCE(block_number, 0) FROM log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $6 + ORDER BY (logs.block_number, logs.log_index)`, utils.NewBig(o.chainID), address, eventSig.Bytes(), wordIndex, wordValueMin.Bytes(), confs) if err != nil { return nil, err } @@ -388,12 +389,12 @@ func (o *ORM) SelectIndexLogsTopicGreaterThan(address common.Address, eventSig c var logs []Log q := o.q.WithOpts(qopts...) err := q.Select(&logs, - `SELECT * FROM evm.logs - WHERE evm.logs.evm_chain_id = $1 + `SELECT * FROM logs + WHERE logs.evm_chain_id = $1 AND address = $2 AND event_sig = $3 AND topics[$4] >= $5 - AND block_number <= (SELECT COALESCE(block_number, 0) FROM evm.log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $6 - ORDER BY (evm.logs.block_number, evm.logs.log_index)`, utils.NewBig(o.chainID), address, eventSig.Bytes(), topicIndex+1, topicValueMin.Bytes(), confs) + AND block_number <= (SELECT COALESCE(block_number, 0) FROM log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $6 + ORDER BY (logs.block_number, logs.log_index)`, utils.NewBig(o.chainID), address, eventSig.Bytes(), topicIndex+1, topicValueMin.Bytes(), confs) if err != nil { return nil, err } @@ -407,12 +408,12 @@ func (o *ORM) SelectUntilBlockHashDataWordGreaterThan(address common.Address, ev // We want to mimic the behaviour of the ETH RPC which errors if blockhash not found. var block LogPollerBlock if err := tx.Get(&block, - `SELECT * FROM evm.log_poller_blocks + `SELECT * FROM log_poller_blocks WHERE evm_chain_id = $1 AND block_hash = $2`, utils.NewBig(o.chainID), untilBlockHash); err != nil { return err } return q.Select(&logs, - `SELECT * FROM evm.logs + `SELECT * FROM logs WHERE evm_chain_id = $1 AND address = $2 AND event_sig = $3 AND substring(data from 32*$4+1 for 32) >= $5 @@ -433,13 +434,13 @@ func (o *ORM) SelectIndexLogsTopicRange(address common.Address, eventSig common. var logs []Log q := o.q.WithOpts(qopts...) err := q.Select(&logs, - `SELECT * FROM evm.logs - WHERE evm.logs.evm_chain_id = $1 + `SELECT * FROM logs + WHERE logs.evm_chain_id = $1 AND address = $2 AND event_sig = $3 AND topics[$4] >= $5 AND topics[$4] <= $6 - AND block_number <= (SELECT COALESCE(block_number, 0) FROM evm.log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $7 - ORDER BY (evm.logs.block_number, evm.logs.log_index)`, utils.NewBig(o.chainID), address, eventSig.Bytes(), topicIndex+1, topicValueMin.Bytes(), topicValueMax.Bytes(), confs) + AND block_number <= (SELECT COALESCE(block_number, 0) FROM log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $7 + ORDER BY (logs.block_number, logs.log_index)`, utils.NewBig(o.chainID), address, eventSig.Bytes(), topicIndex+1, topicValueMin.Bytes(), topicValueMax.Bytes(), confs) if err != nil { return nil, err } @@ -456,12 +457,12 @@ func (o *ORM) SelectIndexedLogs(address common.Address, eventSig common.Hash, to topicValuesBytes := concatBytes(topicValues) // Add 1 since postgresql arrays are 1-indexed. err := q.Select(&logs, ` - SELECT * FROM evm.logs - WHERE evm.logs.evm_chain_id = $1 + SELECT * FROM logs + WHERE logs.evm_chain_id = $1 AND address = $2 AND event_sig = $3 AND topics[$4] = ANY($5) - AND block_number <= (SELECT COALESCE(block_number, 0) FROM evm.log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $6 - ORDER BY (evm.logs.block_number, evm.logs.log_index)`, utils.NewBig(o.chainID), address, eventSig.Bytes(), topicIndex+1, topicValuesBytes, confs) + AND block_number <= (SELECT COALESCE(block_number, 0) FROM log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $6 + ORDER BY (logs.block_number, logs.log_index)`, utils.NewBig(o.chainID), address, eventSig.Bytes(), topicIndex+1, topicValuesBytes, confs) if err != nil { return nil, err } @@ -478,11 +479,11 @@ func (o *ORM) SelectIndexedLogsByBlockRangeFilter(start, end int64, address comm topicValuesBytes := concatBytes(topicValues) q := o.q.WithOpts(qopts...) err := q.Select(&logs, ` - SELECT * FROM evm.logs - WHERE evm.logs.block_number >= $1 AND evm.logs.block_number <= $2 AND evm.logs.evm_chain_id = $3 + SELECT * FROM logs + WHERE logs.block_number >= $1 AND logs.block_number <= $2 AND logs.evm_chain_id = $3 AND address = $4 AND event_sig = $5 AND topics[$6] = ANY($7) - ORDER BY (evm.logs.block_number, evm.logs.log_index)`, start, end, utils.NewBig(o.chainID), address, eventSig.Bytes(), topicIndex+1, topicValuesBytes) + ORDER BY (logs.block_number, logs.log_index)`, start, end, utils.NewBig(o.chainID), address, eventSig.Bytes(), topicIndex+1, topicValuesBytes) if err != nil { return nil, err } @@ -503,12 +504,12 @@ func (o *ORM) SelectIndexedLogsCreatedAfter(address common.Address, eventSig com topicValuesBytes := concatBytes(topicValues) // Add 1 since postgresql arrays are 1-indexed. err := q.Select(&logs, ` - SELECT * FROM evm.logs - WHERE evm.logs.evm_chain_id = $1 + SELECT * FROM logs + WHERE logs.evm_chain_id = $1 AND address = $2 AND event_sig = $3 AND topics[$4] = ANY($5) AND created_at > $6 - AND block_number <= (SELECT COALESCE(block_number, 0) FROM evm.log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $7 + AND block_number <= (SELECT COALESCE(block_number, 0) FROM log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $7 ORDER BY created_at ASC`, utils.NewBig(o.chainID), address, eventSig.Bytes(), topicIndex+1, topicValuesBytes, after, confs) if err != nil { return nil, err @@ -520,11 +521,11 @@ func (o *ORM) SelectIndexedLogsByTxHash(eventSig common.Hash, txHash common.Hash q := o.q.WithOpts(qopts...) var logs []Log err := q.Select(&logs, ` - SELECT * FROM evm.logs - WHERE evm.logs.evm_chain_id = $1 + SELECT * FROM logs + WHERE logs.evm_chain_id = $1 AND tx_hash = $2 AND event_sig = $3 - ORDER BY (evm.logs.block_number, evm.logs.log_index)`, + ORDER BY (logs.block_number, logs.log_index)`, utils.NewBig(o.chainID), txHash.Bytes(), eventSig.Bytes()) if err != nil { return nil, err @@ -543,25 +544,25 @@ func (o *ORM) SelectIndexedLogsWithSigsExcluding(sigA, sigB common.Hash, topicIn err := q.Select(&logs, ` SELECT * - FROM evm.logs + FROM logs WHERE evm_chain_id = $1 AND address = $2 AND event_sig = $3 AND block_number BETWEEN $6 AND $7 - AND block_number <= (SELECT COALESCE(block_number, 0) FROM evm.log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $8 + AND block_number <= (SELECT COALESCE(block_number, 0) FROM log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $8 EXCEPT SELECT a.* - FROM evm.logs AS a - INNER JOIN evm.logs B + FROM logs AS a + INNER JOIN logs B ON a.evm_chain_id = b.evm_chain_id AND a.address = b.address AND a.topics[$5] = b.topics[$5] AND a.event_sig = $3 AND b.event_sig = $4 AND b.block_number BETWEEN $6 AND $7 - AND b.block_number <= (SELECT COALESCE(block_number, 0) FROM evm.log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $8 + AND b.block_number <= (SELECT COALESCE(block_number, 0) FROM log_poller_blocks WHERE evm_chain_id = $1 ORDER BY block_number DESC LIMIT 1) - $8 ORDER BY block_number,log_index ASC `, utils.NewBig(o.chainID), address, sigA.Bytes(), sigB.Bytes(), topicIndex+1, startBlock, endBlock, confs) diff --git a/core/chains/evm/txmgr/builder.go b/core/chains/evm/txmgr/builder.go index 464cf6f9c59..ee1120f5a08 100644 --- a/core/chains/evm/txmgr/builder.go +++ b/core/chains/evm/txmgr/builder.go @@ -4,12 +4,11 @@ import ( "math/big" "time" - "github.com/smartcontractkit/sqlx" - "github.com/smartcontractkit/chainlink/v2/common/txmgr" txmgrtypes "github.com/smartcontractkit/chainlink/v2/common/txmgr/types" evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/config" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/forwarders" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller" @@ -21,7 +20,7 @@ import ( // NewTxm constructs the necessary dependencies for the EvmTxm (broadcaster, confirmer, etc) and returns a new EvmTxManager func NewTxm( - db *sqlx.DB, + db *evmdb.ScopedDB, chainConfig ChainConfig, fCfg FeeConfig, txConfig config.Transactions, diff --git a/core/chains/evm/txmgr/evm_tx_store.go b/core/chains/evm/txmgr/evm_tx_store.go index 52cd50cba32..9aa34fec7e1 100644 --- a/core/chains/evm/txmgr/evm_tx_store.go +++ b/core/chains/evm/txmgr/evm_tx_store.go @@ -21,6 +21,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/common/txmgr" txmgrtypes "github.com/smartcontractkit/chainlink/v2/common/txmgr/types" "github.com/smartcontractkit/chainlink/v2/core/assets" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/label" evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types" @@ -320,13 +321,13 @@ func dbEthTxAttemptsToEthTxAttempts(dbEthTxAttempt []DbEthTxAttempt) []TxAttempt } func NewTxStore( - db *sqlx.DB, + db *evmdb.ScopedDB, lggr logger.Logger, cfg pg.QConfig, ) *evmTxStore { namedLogger := lggr.Named("TxmStore") ctx, cancel := context.WithCancel(context.Background()) - q := pg.NewQ(db, namedLogger, cfg, pg.WithParentCtx(ctx)) + q := pg.NewQ(db.SqlxDB(), namedLogger, cfg, pg.WithParentCtx(ctx)) return &evmTxStore{ q: q, logger: namedLogger, diff --git a/core/cmd/ocr2vrf_configure_commands.go b/core/cmd/ocr2vrf_configure_commands.go index 01bfd89c32b..b56636b09e8 100644 --- a/core/cmd/ocr2vrf_configure_commands.go +++ b/core/cmd/ocr2vrf_configure_commands.go @@ -14,6 +14,7 @@ import ( "github.com/pkg/errors" "github.com/urfave/cli" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/forwarders" "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/authorized_forwarder" "github.com/smartcontractkit/chainlink/v2/core/logger" @@ -240,8 +241,12 @@ func (s *Shell) ConfigureOCR2VRFNode(c *cli.Context, owner *bind.TransactOpts, e return nil, err } + evmDB, err := evmdb.NewScopedDB(s.Config.AppID(), s.Config.Database()) + if err != nil { + return nil, err + } // Create forwarder for management in forwarder_manager.go. - orm := forwarders.NewORM(ldb.DB(), lggr, s.Config.Database()) + orm := forwarders.NewORM(evmDB, lggr, s.Config.Database()) _, err = orm.CreateForwarder(common.HexToAddress(forwarderAddress), *utils.NewBigI(chainID)) if err != nil { return nil, err diff --git a/core/cmd/shell.go b/core/cmd/shell.go index 78e02d6ff71..1cc0e9a434b 100644 --- a/core/cmd/shell.go +++ b/core/cmd/shell.go @@ -35,6 +35,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/build" "github.com/smartcontractkit/chainlink/v2/core/chains/evm" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" "github.com/smartcontractkit/chainlink/v2/core/config" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/logger/audit" @@ -152,7 +153,7 @@ func (n ChainlinkAppFactory) NewApplication(ctx context.Context, cfg chainlink.G GRPCOpts: grpcOpts, } - evmScopedDB, err := pg.OpenUnlockedDB(cfg.AppID(), cfg.Database(), pg.WithSchema("evm")) //pg.NewLockedDB(cfg.AppID(), cfg.Database(), cfg.Database().Lock(), lggr) + evmScopedDB, err := evmdb.NewScopedDB(cfg.AppID(), cfg.Database()) if err != nil { return nil, err } diff --git a/core/cmd/shell_local.go b/core/cmd/shell_local.go index 372aad01384..823913937db 100644 --- a/core/cmd/shell_local.go +++ b/core/cmd/shell_local.go @@ -33,6 +33,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/assets" "github.com/smartcontractkit/chainlink/v2/core/build" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr" evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types" @@ -633,7 +634,12 @@ func (s *Shell) RebroadcastTransactions(c *cli.Context) (err error) { s.Logger.Infof("Rebroadcasting transactions from %v to %v", beginningNonce, endingNonce) - orm := txmgr.NewTxStore(app.GetSqlxDB(), lggr, s.Config.Database()) + evmDB, err := evmdb.NewScopedDB(s.Config.AppID(), s.Config.Database()) + if err != nil { + return s.errorOut(fmt.Errorf("could not open evm db: %w", err)) + } + + orm := txmgr.NewTxStore(evmDB, lggr, s.Config.Database()) txBuilder := txmgr.NewEvmTxAttemptBuilder(*ethClient.ConfiguredChainID(), chain.Config().EVM().GasEstimator(), keyStore.Eth(), nil) cfg := txmgr.NewEvmTxmConfig(chain.Config().EVM()) feeCfg := txmgr.NewEvmTxmFeeConfig(chain.Config().EVM().GasEstimator()) diff --git a/core/cmd/shell_local_test.go b/core/cmd/shell_local_test.go index f54cf7d6e9a..8a7e680b4f7 100644 --- a/core/cmd/shell_local_test.go +++ b/core/cmd/shell_local_test.go @@ -18,6 +18,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/logger/audit" @@ -90,7 +91,7 @@ func TestShell_RunNodeWithPasswords(t *testing.T) { AppConfig: cfg, EventBroadcaster: pg.NewNullEventBroadcaster(), MailMon: &utils.MailboxMonitor{}, - DB: evmtest.NewScopedDB(t, cfg.Database()), + DB: evmtestdb.NewScopedDB(t, cfg.Database()), }, } testRelayers := genTestEVMRelayers(t, opts, keyStore) @@ -195,7 +196,7 @@ func TestShell_RunNodeWithAPICredentialsFile(t *testing.T) { EventBroadcaster: pg.NewNullEventBroadcaster(), MailMon: &utils.MailboxMonitor{}, - DB: evmtest.NewScopedDB(t, cfg.Database()), + DB: evmtestdb.NewScopedDB(t, cfg.Database()), }, } testRelayers := genTestEVMRelayers(t, opts, keyStore) diff --git a/core/internal/cltest/cltest.go b/core/internal/cltest/cltest.go index 662ee7de452..ebe7f67e8ac 100644 --- a/core/internal/cltest/cltest.go +++ b/core/internal/cltest/cltest.go @@ -53,6 +53,7 @@ import ( evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" evmclimocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client/mocks" evmconfig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/config" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas" httypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/headtracker/types" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr" @@ -62,6 +63,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest" configtest2 "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" clhttptest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/httptest" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/keystest" "github.com/smartcontractkit/chainlink/v2/core/logger" @@ -395,7 +397,7 @@ func NewApplicationWithConfig(t testing.TB, cfg chainlink.GeneralConfig, flagsAn AppConfig: cfg, EventBroadcaster: eventBroadcaster, MailMon: mailMon, - DB: evmtest.NewScopedDB(t, cfg.Database()), + DB: evmtestdb.NewScopedDB(t, cfg.Database()), }, CSAETHKeystore: keyStore, } @@ -1627,11 +1629,11 @@ func MustGetStateForKey(t testing.TB, kst keystore.Eth, key ethkey.KeyV2) ethkey return state } -func NewTxStore(t *testing.T, db *sqlx.DB, cfg pg.QConfig) txmgr.EvmTxStore { +func NewTxStore(t *testing.T, db *evmdb.ScopedDB, cfg pg.QConfig) txmgr.EvmTxStore { return txmgr.NewTxStore(db, logger.TestLogger(t), cfg) } -func NewTestTxStore(t *testing.T, db *sqlx.DB, cfg pg.QConfig) txmgr.TestEvmTxStore { +func NewTestTxStore(t *testing.T, db *evmdb.ScopedDB, cfg pg.QConfig) txmgr.TestEvmTxStore { return txmgr.NewTxStore(db, logger.TestLogger(t), cfg) } diff --git a/core/internal/cltest/factories.go b/core/internal/cltest/factories.go index cb0b51743e4..7c6b02c232f 100644 --- a/core/internal/cltest/factories.go +++ b/core/internal/cltest/factories.go @@ -28,6 +28,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/assets" "github.com/smartcontractkit/chainlink/v2/core/auth" "github.com/smartcontractkit/chainlink/v2/core/bridges" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/headtracker" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr" @@ -583,7 +584,7 @@ func MustGenerateRandomKeyState(t testing.TB) ethkey.State { return ethkey.State{Address: NewEIP55Address()} } -func MustInsertHead(t *testing.T, db *sqlx.DB, cfg pg.QConfig, number int64) evmtypes.Head { +func MustInsertHead(t *testing.T, db *evmdb.ScopedDB, cfg pg.QConfig, number int64) evmtypes.Head { h := evmtypes.NewHead(big.NewInt(number), utils.NewHash(), utils.NewHash(), 0, utils.NewBig(&FixtureChainID)) horm := headtracker.NewORM(db, logger.TestLogger(t), cfg, FixtureChainID) diff --git a/core/internal/cltest/heavyweight/orm.go b/core/internal/cltest/heavyweight/orm.go index 3690a986e2e..efa5f2acd87 100644 --- a/core/internal/cltest/heavyweight/orm.go +++ b/core/internal/cltest/heavyweight/orm.go @@ -18,7 +18,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" "github.com/smartcontractkit/chainlink/v2/core/cmd" + "github.com/smartcontractkit/chainlink/v2/core/config" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest2 "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" @@ -42,7 +44,12 @@ func FullTestDBNoFixturesV2(t *testing.T, name string, overrideFn func(c *chainl func FullTestDBEmptyV2(t *testing.T, name string, overrideFn func(c *chainlink.Config, s *chainlink.Secrets)) (chainlink.GeneralConfig, *sqlx.DB) { return prepareFullTestDBV2(t, name, true, false, overrideFn) } - +func EVMDB(t *testing.T, cfg config.Database) *evmdb.ScopedDB { + evmConn := evmdb.ScopedConnection(cfg.URL()) + edb, err := pg.NewConnection(evmConn.String(), dialects.Postgres, cfg) + require.NoError(t, err) + return &evmdb.ScopedDB{edb} +} func prepareFullTestDBV2(t *testing.T, name string, empty bool, loadFixtures bool, overrideFn func(c *chainlink.Config, s *chainlink.Secrets)) (chainlink.GeneralConfig, *sqlx.DB) { testutils.SkipShort(t, "FullTestDB") diff --git a/core/internal/cltest/job_factories.go b/core/internal/cltest/job_factories.go index 910ffe79e38..ada20b47aff 100644 --- a/core/internal/cltest/job_factories.go +++ b/core/internal/cltest/job_factories.go @@ -11,6 +11,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/bridges" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/job" "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey" @@ -61,11 +62,12 @@ func MustInsertWebhookSpec(t *testing.T, db *sqlx.DB) (job.Job, job.WebhookSpec) func getORMs(t *testing.T, db *sqlx.DB) (jobORM job.ORM, pipelineORM pipeline.ORM) { config := configtest.NewTestGeneralConfig(t) + evmdb := evmtestdb.NewScopedDB(t, config.Database()) keyStore := NewKeyStore(t, db, config.Database()) lggr := logger.TestLogger(t) pipelineORM = pipeline.NewORM(db, lggr, config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgeORM := bridges.NewORM(db, lggr, config.Database()) - cc := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) + cc := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: evmdb, GeneralConfig: config, KeyStore: keyStore.Eth()}) legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(cc) jobORM = job.NewORM(db, legacyChains, pipelineORM, bridgeORM, keyStore, lggr, config.Database()) t.Cleanup(func() { jobORM.Close() }) diff --git a/core/internal/features/features_test.go b/core/internal/features/features_test.go index 39eee3f7b43..5a443afaf37 100644 --- a/core/internal/features/features_test.go +++ b/core/internal/features/features_test.go @@ -56,6 +56,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest2 "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" @@ -828,7 +829,7 @@ func setupForwarderEnabledNode( b.Commit() // add forwarder address to be tracked in db - forwarderORM := forwarders.NewORM(app.GetSqlxDB(), logger.TestLogger(t), config.Database()) + forwarderORM := forwarders.NewORM(app.GetRelayers().LegacyEVM().DB(), logger.TestLogger(t), config.Database()) chainID := utils.Big(*b.Blockchain().Config().ChainID) _, err = forwarderORM.CreateForwarder(forwarder, chainID) require.NoError(t, err) @@ -1327,11 +1328,12 @@ func TestIntegration_BlockHistoryEstimator(t *testing.T) { ethClient.On("ConfiguredChainID").Return(big.NewInt(client.NullClientChainID)).Maybe() chchNewHeads := make(chan evmtest.RawSub[*evmtypes.Head], 1) - db := pgtest.NewSqlxDB(t) - kst := cltest.NewKeyStore(t, db, cfg.Database()) + coredb := pgtest.NewSqlxDB(t) + kst := cltest.NewKeyStore(t, coredb, cfg.Database()) require.NoError(t, kst.Unlock(cltest.Password)) - cc := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, KeyStore: kst.Eth(), Client: ethClient, GeneralConfig: cfg}) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) + cc := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: evmdb, KeyStore: kst.Eth(), Client: ethClient, GeneralConfig: cfg}) b41 := evmtypes.Block{ Number: 41, diff --git a/core/internal/features/ocr2/features_ocr2_test.go b/core/internal/features/ocr2/features_ocr2_test.go index 3883e0319ed..eed892550af 100644 --- a/core/internal/features/ocr2/features_ocr2_test.go +++ b/core/internal/features/ocr2/features_ocr2_test.go @@ -170,7 +170,7 @@ func setupNodeOCR2( b.Commit() // add forwarder address to be tracked in db - forwarderORM := forwarders.NewORM(app.GetSqlxDB(), logger.TestLogger(t), config.Database()) + forwarderORM := forwarders.NewORM(app.GetRelayers().LegacyEVM().DB(), logger.TestLogger(t), config.Database()) chainID := utils.Big(*b.Blockchain().Config().ChainID) _, err = forwarderORM.CreateForwarder(faddr, chainID) require.NoError(t, err) diff --git a/core/internal/testutils/evmtest/db/dbtest.go b/core/internal/testutils/evmtest/db/dbtest.go new file mode 100644 index 00000000000..3292d2b73be --- /dev/null +++ b/core/internal/testutils/evmtest/db/dbtest.go @@ -0,0 +1,15 @@ +package db + +import ( + "testing" + + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" + "github.com/smartcontractkit/chainlink/v2/core/config" + "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" + "github.com/smartcontractkit/chainlink/v2/core/services/pg" +) + +func NewScopedDB(t testing.TB, cfg config.Database) *evmdb.ScopedDB { + evmURL := evmdb.ScopedConnection(cfg.URL()) + return &evmdb.ScopedDB{pgtest.NewSqlxDB(t, pg.WithURL(evmURL))} +} diff --git a/core/internal/testutils/evmtest/evmtest.go b/core/internal/testutils/evmtest/evmtest.go index fa7023ee2cb..0527c5b852e 100644 --- a/core/internal/testutils/evmtest/evmtest.go +++ b/core/internal/testutils/evmtest/evmtest.go @@ -9,7 +9,6 @@ import ( "github.com/ethereum/go-ethereum" "github.com/pelletier/go-toml/v2" - "github.com/smartcontractkit/sqlx" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" "golang.org/x/exp/slices" @@ -24,15 +23,15 @@ import ( evmclimocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client/mocks" evmconfig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/config" evmtoml "github.com/smartcontractkit/chainlink/v2/core/chains/evm/config/toml" + + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas" httypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/headtracker/types" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/log" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr" evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types" - "github.com/smartcontractkit/chainlink/v2/core/config" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/keystore" "github.com/smartcontractkit/chainlink/v2/core/services/pg" @@ -64,7 +63,7 @@ type TestChainOpts struct { LogPoller logpoller.LogPoller GeneralConfig evm.AppConfig HeadTracker httypes.HeadTracker - DB *sqlx.DB + DB *evmdb.ScopedDB TxManager txmgr.TxManager KeyStore keystore.Eth MailMon *utils.MailboxMonitor @@ -370,8 +369,3 @@ func (r *RawSub[T]) TrySend(t T) { case r.ch <- t: } } - -func NewScopedDB(t testing.TB, cfg config.Database) *sqlx.DB { - u := pg.SchemaScopedConnection(cfg.URL(), "evm") - return pgtest.NewSqlxDB(t, pg.WithURL(u)) -} diff --git a/core/internal/testutils/pgtest/pgtest.go b/core/internal/testutils/pgtest/pgtest.go index a9adbe12da5..dfa66856c43 100644 --- a/core/internal/testutils/pgtest/pgtest.go +++ b/core/internal/testutils/pgtest/pgtest.go @@ -30,24 +30,26 @@ func NewSqlDB(t *testing.T) *sql.DB { return db } -func uniqueConnection(t testing.TB) *url.URL { - url := testutils.MustParseURL(t, defaultDBURL.String()) - // inject uuid by default because the transaction wrapped driver requires it - q := url.Query() - q.Add("uuid", uuid.New().String()) - url.RawQuery = q.Encode() - return url +func withUUID() pg.ConnectionOpt { + return func(u *url.URL) error { + q := u.Query() + q.Add("uuid", uuid.New().String()) + u.RawQuery = q.Encode() + return nil + } } func NewSqlxDB(t testing.TB, opts ...pg.ConnectionOpt) *sqlx.DB { testutils.SkipShortDB(t) - url := uniqueConnection(t) + url := testutils.MustParseURL(t, defaultDBURL.String()) + // tx wrapped db driver requires a uuid + opts = append(opts, withUUID()) for _, opt := range opts { assert.NoError(t, opt(url)) } - db, err := sqlx.Open(string(dialects.TransactionWrappedPostgres), url.String()) - require.NoError(t, err) + db := sqlx.MustConnect(string(dialects.TransactionWrappedPostgres), url.String()) + t.Cleanup(func() { assert.NoError(t, db.Close()) }) db.MapperFunc(reflectx.CamelToSnakeASCII) diff --git a/core/services/blockhashstore/bhs_test.go b/core/services/blockhashstore/bhs_test.go index 79494ea41f6..c049af84fa3 100644 --- a/core/services/blockhashstore/bhs_test.go +++ b/core/services/blockhashstore/bhs_test.go @@ -14,6 +14,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/blockhashstore" @@ -24,17 +25,18 @@ import ( ) func TestStoreRotatesFromAddresses(t *testing.T) { - db := pgtest.NewSqlxDB(t) ethClient := evmtest.NewEthClientMockWithDefaultChain(t) cfg := configtest.NewTestGeneralConfig(t) - kst := cltest.NewKeyStore(t, db, cfg.Database()) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) + coredb := pgtest.NewSqlxDB(t) + kst := cltest.NewKeyStore(t, coredb, cfg.Database()) require.NoError(t, kst.Unlock(cltest.Password)) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, KeyStore: kst.Eth(), GeneralConfig: cfg, Client: ethClient}) + relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: evmdb, KeyStore: kst.Eth(), GeneralConfig: cfg, Client: ethClient}) legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) chain, err := legacyChains.Get(cltest.FixtureChainID.String()) require.NoError(t, err) lggr := logger.TestLogger(t) - ks := keystore.New(db, utils.FastScryptParams, lggr, cfg.Database()) + ks := keystore.New(coredb, utils.FastScryptParams, lggr, cfg.Database()) require.NoError(t, ks.Unlock("blah")) k1, err := ks.Eth().Create(&cltest.FixtureChainID) require.NoError(t, err) diff --git a/core/services/blockhashstore/delegate_test.go b/core/services/blockhashstore/delegate_test.go index 3060f617876..0278baa2ec2 100644 --- a/core/services/blockhashstore/delegate_test.go +++ b/core/services/blockhashstore/delegate_test.go @@ -17,6 +17,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/blockhashstore" @@ -53,8 +54,9 @@ func createTestDelegate(t *testing.T) (*blockhashstore.Delegate, *testData) { cfg := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) { c.Feature.LogPoller = func(b bool) *bool { return &b }(true) }) - db := pgtest.NewSqlxDB(t) - kst := cltest.NewKeyStore(t, db, cfg.Database()).Eth() + coredb := pgtest.NewSqlxDB(t) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) + kst := cltest.NewKeyStore(t, coredb, cfg.Database()).Eth() sendingKey, _ := cltest.MustAddRandomKeyToKeystore(t, kst) lp := &mocklp.LogPoller{} lp.On("RegisterFilter", mock.Anything).Return(nil) @@ -63,7 +65,7 @@ func createTestDelegate(t *testing.T) (*blockhashstore.Delegate, *testData) { relayExtenders := evmtest.NewChainRelayExtenders( t, evmtest.TestChainOpts{ - DB: db, + DB: evmdb, KeyStore: kst, GeneralConfig: cfg, Client: ethClient, diff --git a/core/services/chainlink/application.go b/core/services/chainlink/application.go index 814c8a1c10b..3067f06ec0b 100644 --- a/core/services/chainlink/application.go +++ b/core/services/chainlink/application.go @@ -109,15 +109,15 @@ type Application interface { // and Store. The JobSubscriber and Scheduler are also available // in the services package, but the Store has its own package. type ChainlinkApplication struct { - relayers *CoreRelayerChainInteroperators - EventBroadcaster pg.EventBroadcaster - jobORM job.ORM - jobSpawner job.Spawner - pipelineORM pipeline.ORM - pipelineRunner pipeline.Runner - bridgeORM bridges.ORM - sessionORM sessions.ORM - txmStorageService txmgr.EvmTxStore + relayers *CoreRelayerChainInteroperators + EventBroadcaster pg.EventBroadcaster + jobORM job.ORM + jobSpawner job.Spawner + pipelineORM pipeline.ORM + pipelineRunner pipeline.Runner + bridgeORM bridges.ORM + sessionORM sessions.ORM + //txmStorageService txmgr.EvmTxStore FeedsService feeds.Service webhookJobRunner webhook.JobRunner Config GeneralConfig @@ -269,7 +269,6 @@ func NewApplication(opts ApplicationOpts) (Application, error) { mercuryORM = mercury.NewORM(db, globalLogger, cfg.Database()) pipelineRunner = pipeline.NewRunner(pipelineORM, bridgeORM, cfg.JobPipeline(), cfg.WebServer(), legacyEVMChains, keyStore.Eth(), keyStore.VRF(), globalLogger, restrictedHTTPClient, unrestrictedHTTPClient) jobORM = job.NewORM(db, legacyEVMChains, pipelineORM, bridgeORM, keyStore, globalLogger, cfg.Database()) - txmORM = txmgr.NewTxStore(db, globalLogger, cfg.Database()) ) for _, chain := range legacyEVMChains.Slice() { @@ -458,7 +457,6 @@ func NewApplication(opts ApplicationOpts) (Application, error) { pipelineORM: pipelineORM, bridgeORM: bridgeORM, sessionORM: sessionORM, - txmStorageService: txmORM, FeedsService: feedsService, Config: cfg, webhookJobRunner: webhookJobRunner, @@ -635,7 +633,7 @@ func (app *ChainlinkApplication) SessionORM() sessions.ORM { // TODO BCF-2516 remove this all together remove EVM specifics func (app *ChainlinkApplication) EVMORM() evmtypes.Configs { - return app.GetRelayers().LegacyEVMChains().ChainNodeConfigs() + return app.GetRelayers().LegacyEVM().Chains().ChainNodeConfigs() } func (app *ChainlinkApplication) PipelineORM() pipeline.ORM { @@ -643,7 +641,7 @@ func (app *ChainlinkApplication) PipelineORM() pipeline.ORM { } func (app *ChainlinkApplication) TxmStorageService() txmgr.EvmTxStore { - return app.txmStorageService + return app.GetRelayers().LegacyEVM().TxmORM() } func (app *ChainlinkApplication) GetExternalInitiatorManager() webhook.ExternalInitiatorManager { diff --git a/core/services/chainlink/relayer_chain_interoperators.go b/core/services/chainlink/relayer_chain_interoperators.go index 6513184d9ec..d858f2d87a3 100644 --- a/core/services/chainlink/relayer_chain_interoperators.go +++ b/core/services/chainlink/relayer_chain_interoperators.go @@ -13,6 +13,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/chains" "github.com/smartcontractkit/chainlink/v2/core/chains/cosmos" "github.com/smartcontractkit/chainlink/v2/core/chains/evm" + "github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr" "github.com/smartcontractkit/chainlink/v2/core/services" "github.com/smartcontractkit/chainlink/v2/core/services/ocr2" "github.com/smartcontractkit/chainlink/v2/core/services/relay" @@ -35,6 +36,7 @@ type RelayerChainInteroperators interface { LoopRelayerStorer LegacyChainer + LegacyEVM() evm.LegacyContainer ChainsNodesStatuser } @@ -79,6 +81,7 @@ type CoreRelayerChainInteroperators struct { loopRelayers map[relay.ID]loop.Relayer legacyChains legacyChains + legacyEVM evm.LegacyContainer // we keep an explicit list of services because the legacy implementations have more than // just the relayer service srvs []services.ServiceCtx @@ -116,7 +119,11 @@ func InitEVM(ctx context.Context, factory RelayerFactory, config EVMFactoryConfi op.loopRelayers[id] = a legacyMap[id.ChainID] = a.Chain() } - op.legacyChains.EVMChains = evm.NewLegacyChains(legacyMap, config.AppConfig.EVMConfigs()) + + chains := evm.NewLegacyChains(legacyMap, config.AppConfig.EVMConfigs()) + op.legacyChains.EVMChains = chains + txmORM := txmgr.NewTxStore(config.DB, factory.Logger, config.AppConfig.Database()) + op.legacyEVM = evm.NewLegacyContainerImpl(config.DB, txmORM, chains) return nil } } @@ -338,6 +345,10 @@ func (rs *CoreRelayerChainInteroperators) List(filter FilterFn) RelayerChainInte } } +func (rs *CoreRelayerChainInteroperators) LegacyEVM() evm.LegacyContainer { + return rs.legacyEVM +} + // Returns a slice of [loop.Relayer]. A typically usage pattern to is // use [List(criteria)].Slice() for range based operations func (rs *CoreRelayerChainInteroperators) Slice() []loop.Relayer { diff --git a/core/services/chainlink/relayer_chain_interoperators_test.go b/core/services/chainlink/relayer_chain_interoperators_test.go index 409f828d098..2fb1ec6a891 100644 --- a/core/services/chainlink/relayer_chain_interoperators_test.go +++ b/core/services/chainlink/relayer_chain_interoperators_test.go @@ -21,7 +21,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/chains/starknet" "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" @@ -210,7 +210,7 @@ func TestCoreRelayerChainInteroperators(t *testing.T) { AppConfig: cfg, EventBroadcaster: pg.NewNullEventBroadcaster(), MailMon: &utils.MailboxMonitor{}, - DB: evmtest.NewScopedDB(t, cfg.Database()), + DB: evmtestdb.NewScopedDB(t, cfg.Database()), }, CSAETHKeystore: keyStore, }), @@ -285,7 +285,7 @@ func TestCoreRelayerChainInteroperators(t *testing.T) { AppConfig: cfg, EventBroadcaster: pg.NewNullEventBroadcaster(), MailMon: &utils.MailboxMonitor{}, - DB: evmtest.NewScopedDB(t, cfg.Database()), + DB: evmtestdb.NewScopedDB(t, cfg.Database()), }, CSAETHKeystore: keyStore, }), diff --git a/core/services/cron/cron_test.go b/core/services/cron/cron_test.go index 174f80586ae..771893b1b0d 100644 --- a/core/services/cron/cron_test.go +++ b/core/services/cron/cron_test.go @@ -13,6 +13,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/cron" @@ -25,15 +26,15 @@ import ( func TestCronV2Pipeline(t *testing.T) { runner := pipelinemocks.NewRunner(t) cfg := configtest.NewTestGeneralConfig(t) - db := pgtest.NewSqlxDB(t) - - keyStore := cltest.NewKeyStore(t, db, cfg.Database()) - relayerExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: cfg, Client: evmtest.NewEthClientMockWithDefaultChain(t), KeyStore: keyStore.Eth()}) + coredb := pgtest.NewSqlxDB(t) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) + keyStore := cltest.NewKeyStore(t, coredb, cfg.Database()) + relayerExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: evmdb, GeneralConfig: cfg, Client: evmtest.NewEthClientMockWithDefaultChain(t), KeyStore: keyStore.Eth()}) lggr := logger.TestLogger(t) - orm := pipeline.NewORM(db, lggr, cfg.Database(), cfg.JobPipeline().MaxSuccessfulRuns()) - btORM := bridges.NewORM(db, lggr, cfg.Database()) + orm := pipeline.NewORM(coredb, lggr, cfg.Database(), cfg.JobPipeline().MaxSuccessfulRuns()) + btORM := bridges.NewORM(coredb, lggr, cfg.Database()) legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayerExtenders) - jobORM := job.NewORM(db, legacyChains, orm, btORM, keyStore, lggr, cfg.Database()) + jobORM := job.NewORM(coredb, legacyChains, orm, btORM, keyStore, lggr, cfg.Database()) jb := &job.Job{ Type: job.Cron, diff --git a/core/services/directrequest/delegate_test.go b/core/services/directrequest/delegate_test.go index ffd78443cc2..84be0a3b1da 100644 --- a/core/services/directrequest/delegate_test.go +++ b/core/services/directrequest/delegate_test.go @@ -22,6 +22,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" @@ -44,7 +45,9 @@ func TestDelegate_ServicesForSpec(t *testing.T) { }) keyStore := cltest.NewKeyStore(t, db, cfg.Database()) mailMon := srvctest.Start(t, utils.NewMailboxMonitor(t.Name())) - relayerExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: cfg, Client: ethClient, MailMon: mailMon, KeyStore: keyStore.Eth()}) + + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) + relayerExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: evmdb, GeneralConfig: cfg, Client: ethClient, MailMon: mailMon, KeyStore: keyStore.Eth()}) lggr := logger.TestLogger(t) legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayerExtenders) @@ -82,14 +85,17 @@ func NewDirectRequestUniverseWithConfig(t *testing.T, cfg chainlink.GeneralConfi mailMon := srvctest.Start(t, utils.NewMailboxMonitor(t.Name())) - db := pgtest.NewSqlxDB(t) - keyStore := cltest.NewKeyStore(t, db, cfg.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: cfg, Client: ethClient, LogBroadcaster: broadcaster, MailMon: mailMon, KeyStore: keyStore.Eth()}) + coredb := pgtest.NewSqlxDB(t) + keyStore := cltest.NewKeyStore(t, coredb, cfg.Database()) + + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) + relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: evmdb, GeneralConfig: cfg, Client: ethClient, LogBroadcaster: broadcaster, MailMon: mailMon, KeyStore: keyStore.Eth()}) + lggr := logger.TestLogger(t) - orm := pipeline.NewORM(db, lggr, cfg.Database(), cfg.JobPipeline().MaxSuccessfulRuns()) - btORM := bridges.NewORM(db, lggr, cfg.Database()) + orm := pipeline.NewORM(coredb, lggr, cfg.Database(), cfg.JobPipeline().MaxSuccessfulRuns()) + btORM := bridges.NewORM(coredb, lggr, cfg.Database()) legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) - jobORM := job.NewORM(db, legacyChains, orm, btORM, keyStore, lggr, cfg.Database()) + jobORM := job.NewORM(coredb, legacyChains, orm, btORM, keyStore, lggr, cfg.Database()) delegate := directrequest.NewDelegate(lggr, runner, orm, legacyChains, mailMon) jb := cltest.MakeDirectRequestJobSpec(t) diff --git a/core/services/feeds/orm_test.go b/core/services/feeds/orm_test.go index f2dacc4f02f..61b2fe7d904 100644 --- a/core/services/feeds/orm_test.go +++ b/core/services/feeds/orm_test.go @@ -16,6 +16,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/feeds" @@ -1477,7 +1478,8 @@ func createJob(t *testing.T, db *sqlx.DB, externalJobID uuid.UUID) *job.Job { lggr = logger.TestLogger(t) pipelineORM = pipeline.NewORM(db, lggr, config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgeORM = bridges.NewORM(db, lggr, config.Database()) - relayExtenders = evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) + evmdb = evmtestdb.NewScopedDB(t, config.Database()) + relayExtenders = evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: evmdb, GeneralConfig: config, KeyStore: keyStore.Eth()}) ) legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) orm := job.NewORM(db, legacyChains, pipelineORM, bridgeORM, keyStore, lggr, config.Database()) diff --git a/core/services/feeds/service_test.go b/core/services/feeds/service_test.go index 85f14e407f8..14c7b2123ce 100644 --- a/core/services/feeds/service_test.go +++ b/core/services/feeds/service_test.go @@ -17,6 +17,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" @@ -175,12 +176,13 @@ func setupTestServiceCfg(t *testing.T, overrideCfg func(c *chainlink.Config, s * lggr := logger.TestLogger(t) - db := pgtest.NewSqlxDB(t) gcfg := configtest.NewGeneralConfig(t, overrideCfg) + coredb := pgtest.NewSqlxDB(t) + evmdb := evmtestdb.NewScopedDB(t, gcfg.Database()) keyStore := new(ksmocks.Master) scopedConfig := evmtest.NewChainScopedConfig(t, gcfg) - ethKeyStore := cltest.NewKeyStore(t, db, gcfg.Database()).Eth() - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: gcfg, + ethKeyStore := cltest.NewKeyStore(t, coredb, gcfg.Database()).Eth() + relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: evmdb, GeneralConfig: gcfg, HeadTracker: headtracker.NullTracker, KeyStore: ethKeyStore}) legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) keyStore.On("Eth").Return(ethKeyStore) @@ -188,7 +190,7 @@ func setupTestServiceCfg(t *testing.T, overrideCfg func(c *chainlink.Config, s * keyStore.On("P2P").Return(p2pKeystore) keyStore.On("OCR").Return(ocr1Keystore) keyStore.On("OCR2").Return(ocr2Keystore) - svc := feeds.NewService(orm, jobORM, db, spawner, keyStore, scopedConfig.Insecure(), scopedConfig.JobPipeline(), scopedConfig.OCR(), scopedConfig.OCR2(), scopedConfig.Database(), legacyChains, lggr, "1.0.0") + svc := feeds.NewService(orm, jobORM, coredb, spawner, keyStore, scopedConfig.Insecure(), scopedConfig.JobPipeline(), scopedConfig.OCR(), scopedConfig.OCR2(), scopedConfig.Database(), legacyChains, lggr, "1.0.0") svc.SetConnectionsManager(connMgr) return &TestService{ diff --git a/core/services/fluxmonitorv2/integrations_test.go b/core/services/fluxmonitorv2/integrations_test.go index 5b6a94cac58..d14387ab1ca 100644 --- a/core/services/fluxmonitorv2/integrations_test.go +++ b/core/services/fluxmonitorv2/integrations_test.go @@ -27,6 +27,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/assets" "github.com/smartcontractkit/chainlink/v2/core/bridges" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/log" "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/flags_wrapper" faw "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/flux_aggregator_wrapper" @@ -404,7 +405,7 @@ func assertPipelineRunCreated(t *testing.T, db *sqlx.DB, roundID int64, result i return run } -func checkLogWasConsumed(t *testing.T, fa fluxAggregatorUniverse, db *sqlx.DB, pipelineSpecID int32, blockNumber uint64, cfg pg.QConfig) { +func checkLogWasConsumed(t *testing.T, fa fluxAggregatorUniverse, db *evmdb.ScopedDB, pipelineSpecID int32, blockNumber uint64, cfg pg.QConfig) { t.Helper() lggr := logger.TestLogger(t) lggr.Infof("Waiting for log on block: %v, job id: %v", blockNumber, pipelineSpecID) @@ -559,7 +560,7 @@ func TestFluxMonitor_Deviation(t *testing.T) { // Need to wait until NewRound log is consumed - otherwise there is a chance // it will arrive after the next answer is submitted, and cause // DeleteFluxMonitorRoundsBackThrough to delete previous stats - checkLogWasConsumed(t, fa, app.GetSqlxDB(), int32(jobId), receiptBlock, app.GetConfig().Database()) + checkLogWasConsumed(t, fa, app.GetRelayers().LegacyEVM().DB(), int32(jobId), receiptBlock, app.GetConfig().Database()) lggr.Info("Updating price to 103") // Change reported price to a value outside the deviation @@ -588,7 +589,7 @@ func TestFluxMonitor_Deviation(t *testing.T) { // Need to wait until NewRound log is consumed - otherwise there is a chance // it will arrive after the next answer is submitted, and cause // DeleteFluxMonitorRoundsBackThrough to delete previous stats - checkLogWasConsumed(t, fa, app.GetSqlxDB(), int32(jobId), receiptBlock, app.GetConfig().Database()) + checkLogWasConsumed(t, fa, app.GetRelayers().LegacyEVM().DB(), int32(jobId), receiptBlock, app.GetConfig().Database()) // Should not received a submission as it is inside the deviation reportPrice.Store(104) diff --git a/core/services/fluxmonitorv2/orm_test.go b/core/services/fluxmonitorv2/orm_test.go index a24f516c7f0..139244341dd 100644 --- a/core/services/fluxmonitorv2/orm_test.go +++ b/core/services/fluxmonitorv2/orm_test.go @@ -17,6 +17,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/fluxmonitorv2" @@ -85,22 +86,24 @@ func TestORM_UpdateFluxMonitorRoundStats(t *testing.T) { t.Parallel() cfg := configtest.NewGeneralConfig(t, nil) - db := pgtest.NewSqlxDB(t) - keyStore := cltest.NewKeyStore(t, db, cfg.Database()) + coredb := pgtest.NewSqlxDB(t) + keyStore := cltest.NewKeyStore(t, coredb, cfg.Database()) + + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) lggr := logger.TestLogger(t) // Instantiate a real pipeline ORM because we need to create a pipeline run // for the foreign key constraint of the stats record - pipelineORM := pipeline.NewORM(db, lggr, cfg.Database(), cfg.JobPipeline().MaxSuccessfulRuns()) - bridgeORM := bridges.NewORM(db, lggr, cfg.Database()) + pipelineORM := pipeline.NewORM(coredb, lggr, cfg.Database(), cfg.JobPipeline().MaxSuccessfulRuns()) + bridgeORM := bridges.NewORM(coredb, lggr, cfg.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{GeneralConfig: cfg, DB: db, KeyStore: keyStore.Eth()}) + relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{GeneralConfig: cfg, DB: evmdb, KeyStore: keyStore.Eth()}) legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) // Instantiate a real job ORM because we need to create a job to satisfy // a check in pipeline.CreateRun - jobORM := job.NewORM(db, legacyChains, pipelineORM, bridgeORM, keyStore, lggr, cfg.Database()) - orm := newORM(t, db, cfg.Database(), nil) + jobORM := job.NewORM(coredb, legacyChains, pipelineORM, bridgeORM, keyStore, lggr, cfg.Database()) + orm := newORM(t, coredb, cfg.Database(), nil) address := testutils.NewAddress() var roundID uint32 = 1 diff --git a/core/services/functions/listener_test.go b/core/services/functions/listener_test.go index a06fcf3e3b2..b7e5f6baa0f 100644 --- a/core/services/functions/listener_test.go +++ b/core/services/functions/listener_test.go @@ -26,6 +26,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" @@ -87,9 +88,11 @@ func NewFunctionsListenerUniverse(t *testing.T, timeoutSec int, pruneFrequencySe broadcaster.On("AddDependents", 1) mailMon := srvctest.Start(t, utils.NewMailboxMonitor(t.Name())) - db := pgtest.NewSqlxDB(t) - kst := cltest.NewKeyStore(t, db, cfg.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: cfg, Client: ethClient, KeyStore: kst.Eth(), LogBroadcaster: broadcaster, MailMon: mailMon}) + coredb := pgtest.NewSqlxDB(t) + kst := cltest.NewKeyStore(t, coredb, cfg.Database()) + + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) + relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: evmdb, GeneralConfig: cfg, Client: ethClient, KeyStore: kst.Eth(), LogBroadcaster: broadcaster, MailMon: mailMon}) legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) chain := legacyChains.Slice()[0] diff --git a/core/services/job/helpers_test.go b/core/services/job/helpers_test.go index 167ed5297cc..92ede524d70 100644 --- a/core/services/job/helpers_test.go +++ b/core/services/job/helpers_test.go @@ -20,6 +20,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" "github.com/smartcontractkit/chainlink/v2/core/services/job" @@ -223,7 +224,8 @@ func makeMinimalHTTPOracleSpec(t *testing.T, db *sqlx.DB, cfg chainlink.GeneralC } s := fmt.Sprintf(minimalNonBootstrapTemplate, contractAddress, transmitterAddress, keyBundle, fetchUrl, timeout) keyStore := cltest.NewKeyStore(t, db, pgtest.NewQConfig(true)) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, Client: evmtest.NewEthClientMockWithDefaultChain(t), GeneralConfig: cfg, KeyStore: keyStore.Eth()}) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) + relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: evmdb, Client: evmtest.NewEthClientMockWithDefaultChain(t), GeneralConfig: cfg, KeyStore: keyStore.Eth()}) legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) _, err := ocr.ValidatedOracleSpecToml(legacyChains, s) require.NoError(t, err) diff --git a/core/services/job/job_orm_test.go b/core/services/job/job_orm_test.go index bf81064e6a6..b826952eedf 100644 --- a/core/services/job/job_orm_test.go +++ b/core/services/job/job_orm_test.go @@ -17,15 +17,18 @@ import ( "github.com/smartcontractkit/chainlink-relay/pkg/types" + "github.com/smartcontractkit/chainlink/v2/core/services/keystore" "github.com/smartcontractkit/chainlink/v2/core/services/relay" "github.com/smartcontractkit/chainlink/v2/core/assets" "github.com/smartcontractkit/chainlink/v2/core/bridges" + "github.com/smartcontractkit/chainlink/v2/core/chains/evm" evmcfg "github.com/smartcontractkit/chainlink/v2/core/chains/evm/config/toml" "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/blockhashstore" @@ -84,8 +87,7 @@ func TestORM(t *testing.T) { pipelineORM := pipeline.NewORM(db, logger.TestLogger(t), config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, logger.TestLogger(t), config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: ethKeyStore}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, ethKeyStore) orm := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) borm := bridges.NewORM(db, logger.TestLogger(t), config.Database()) @@ -318,8 +320,8 @@ func TestORM_DeleteJob_DeletesAssociatedRecords(t *testing.T) { lggr := logger.TestLogger(t) pipelineORM := pipeline.NewORM(db, lggr, config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, lggr, config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) jobORM := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) scopedConfig := evmtest.NewChainScopedConfig(t, config) korm := keeper.NewORM(db, logger.TestLogger(t), scopedConfig.Database()) @@ -418,8 +420,7 @@ func TestORM_CreateJob_VRFV2(t *testing.T) { lggr := logger.TestLogger(t) pipelineORM := pipeline.NewORM(db, lggr, config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, lggr, config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) jobORM := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) @@ -501,8 +502,7 @@ func TestORM_CreateJob_VRFV2Plus(t *testing.T) { lggr := logger.TestLogger(t) pipelineORM := pipeline.NewORM(db, lggr, config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, lggr, config.Database()) - cc := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(cc) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) jobORM := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) fromAddresses := []string{cltest.NewEIP55Address().String(), cltest.NewEIP55Address().String()} @@ -586,8 +586,7 @@ func TestORM_CreateJob_OCRBootstrap(t *testing.T) { lggr := logger.TestLogger(t) pipelineORM := pipeline.NewORM(db, lggr, config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, lggr, config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) jobORM := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) jb, err := ocrbootstrap.ValidatedBootstrapSpecToml(testspecs.OCRBootstrapSpec) @@ -615,8 +614,7 @@ func TestORM_CreateJob_EVMChainID_Validation(t *testing.T) { pipelineORM := pipeline.NewORM(db, lggr, config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, lggr, config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) jobORM := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) t.Run("evm chain id validation for ocr works", func(t *testing.T) { @@ -712,8 +710,7 @@ func TestORM_CreateJob_OCR_DuplicatedContractAddress(t *testing.T) { pipelineORM := pipeline.NewORM(db, lggr, config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, lggr, config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) jobORM := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) // defaultChainID is deprecated @@ -781,8 +778,7 @@ func TestORM_CreateJob_OCR2_DuplicatedContractAddress(t *testing.T) { pipelineORM := pipeline.NewORM(db, lggr, config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, lggr, config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) jobORM := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) _, address := cltest.MustInsertRandomKey(t, keyStore.Eth()) @@ -844,9 +840,7 @@ func TestORM_CreateJob_OCR2_Sending_Keys_Transmitter_Keys_Validations(t *testing pipelineORM := pipeline.NewORM(db, lggr, config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, lggr, config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - require.True(t, relayExtenders.Len() > 0) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) jobORM := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) jb, err := ocr2validate.ValidatedOracleSpecToml(config.OCR2(), config.Insecure(), testspecs.OCR2EVMSpecMinimal) @@ -957,8 +951,7 @@ func Test_FindJobs(t *testing.T) { pipelineORM := pipeline.NewORM(db, logger.TestLogger(t), config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, logger.TestLogger(t), config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) orm := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) _, bridge := cltest.MustCreateBridge(t, db, cltest.BridgeOpts{}, config.Database()) @@ -1037,8 +1030,7 @@ func Test_FindJob(t *testing.T) { pipelineORM := pipeline.NewORM(db, logger.TestLogger(t), config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, logger.TestLogger(t), config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) orm := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) _, bridge := cltest.MustCreateBridge(t, db, cltest.BridgeOpts{}, config.Database()) @@ -1215,8 +1207,7 @@ func Test_FindJobsByPipelineSpecIDs(t *testing.T) { pipelineORM := pipeline.NewORM(db, logger.TestLogger(t), config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, logger.TestLogger(t), config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) orm := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) jb, err := directrequest.ValidatedDirectRequestSpec(testspecs.DirectRequestSpec) @@ -1256,7 +1247,7 @@ func Test_FindJobsByPipelineSpecIDs(t *testing.T) { Enabled: ptr(true), }) }) - relayExtenders2 := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: newCfg, KeyStore: keyStore.Eth()}) + relayExtenders2 := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: evmtestdb.NewScopedDB(t, newCfg.Database()), GeneralConfig: newCfg, KeyStore: keyStore.Eth()}) legacyChains2 := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders2) orm2 := NewTestORM(t, db, legacyChains2, pipelineORM, bridgesORM, keyStore, config.Database()) @@ -1278,8 +1269,7 @@ func Test_FindPipelineRuns(t *testing.T) { pipelineORM := pipeline.NewORM(db, logger.TestLogger(t), config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, logger.TestLogger(t), config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) orm := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) _, bridge := cltest.MustCreateBridge(t, db, cltest.BridgeOpts{}, config.Database()) @@ -1339,8 +1329,7 @@ func Test_PipelineRunsByJobID(t *testing.T) { pipelineORM := pipeline.NewORM(db, logger.TestLogger(t), config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, logger.TestLogger(t), config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) orm := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) _, bridge := cltest.MustCreateBridge(t, db, cltest.BridgeOpts{}, config.Database()) @@ -1400,8 +1389,7 @@ func Test_FindPipelineRunIDsByJobID(t *testing.T) { lggr := logger.TestLogger(t) pipelineORM := pipeline.NewORM(db, logger.TestLogger(t), config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, lggr, config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) orm := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) _, address := cltest.MustInsertRandomKey(t, keyStore.Eth()) @@ -1508,8 +1496,7 @@ func Test_FindPipelineRunsByIDs(t *testing.T) { pipelineORM := pipeline.NewORM(db, logger.TestLogger(t), config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, logger.TestLogger(t), config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) orm := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) _, bridge := cltest.MustCreateBridge(t, db, cltest.BridgeOpts{}, config.Database()) @@ -1566,8 +1553,7 @@ func Test_FindPipelineRunByID(t *testing.T) { pipelineORM := pipeline.NewORM(db, logger.TestLogger(t), config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, logger.TestLogger(t), config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) orm := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) jb, err := directrequest.ValidatedDirectRequestSpec(testspecs.DirectRequestSpec) @@ -1611,8 +1597,7 @@ func Test_FindJobWithoutSpecErrors(t *testing.T) { pipelineORM := pipeline.NewORM(db, logger.TestLogger(t), config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, logger.TestLogger(t), config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) orm := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) jb, err := directrequest.ValidatedDirectRequestSpec(testspecs.DirectRequestSpec) @@ -1650,8 +1635,7 @@ func Test_FindSpecErrorsByJobIDs(t *testing.T) { pipelineORM := pipeline.NewORM(db, logger.TestLogger(t), config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, logger.TestLogger(t), config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) orm := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) jb, err := directrequest.ValidatedDirectRequestSpec(testspecs.DirectRequestSpec) @@ -1686,8 +1670,7 @@ func Test_CountPipelineRunsByJobID(t *testing.T) { pipelineORM := pipeline.NewORM(db, logger.TestLogger(t), config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgesORM := bridges.NewORM(db, logger.TestLogger(t), config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) orm := NewTestORM(t, db, legacyChains, pipelineORM, bridgesORM, keyStore, config.Database()) _, bridge := cltest.MustCreateBridge(t, db, cltest.BridgeOpts{}, config.Database()) @@ -1738,3 +1721,9 @@ func mustInsertPipelineRun(t *testing.T, orm pipeline.ORM, j job.Job) pipeline.R require.NoError(t, err) return run } + +func setupLegacyEVMChains(t *testing.T, cfg evm.AppConfig, ks keystore.Eth) *evm.LegacyChains { + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) + relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: evmdb, GeneralConfig: cfg, KeyStore: ks}) + return evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) +} diff --git a/core/services/job/job_pipeline_orm_integration_test.go b/core/services/job/job_pipeline_orm_integration_test.go index a5d3ee984da..e1ce4fa54b9 100644 --- a/core/services/job/job_pipeline_orm_integration_test.go +++ b/core/services/job/job_pipeline_orm_integration_test.go @@ -12,12 +12,10 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest2 "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" "github.com/smartcontractkit/chainlink/v2/core/services/pipeline" - evmrelay "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm" "github.com/smartcontractkit/chainlink/v2/core/store/models" ) @@ -151,8 +149,7 @@ func TestPipelineORM_Integration(t *testing.T) { clearJobsDb(t, db) orm := pipeline.NewORM(db, logger.TestLogger(t), cfg.Database(), cfg.JobPipeline().MaxSuccessfulRuns()) btORM := bridges.NewORM(db, logger.TestLogger(t), cfg.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{Client: evmtest.NewEthClientMockWithDefaultChain(t), DB: db, GeneralConfig: config, KeyStore: ethKeyStore}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) runner := pipeline.NewRunner(orm, btORM, config.JobPipeline(), cfg.WebServer(), legacyChains, nil, nil, lggr, nil, nil) jobORM := NewTestORM(t, db, legacyChains, orm, btORM, keyStore, cfg.Database()) diff --git a/core/services/job/runner_integration_test.go b/core/services/job/runner_integration_test.go index a886c52f283..6294499d35c 100644 --- a/core/services/job/runner_integration_test.go +++ b/core/services/job/runner_integration_test.go @@ -81,8 +81,7 @@ func TestRunner(t *testing.T) { pipelineORM := pipeline.NewORM(db, logger.TestLogger(t), config.Database(), config.JobPipeline().MaxSuccessfulRuns()) btORM := bridges.NewORM(db, logger.TestLogger(t), config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, Client: ethClient, GeneralConfig: config, KeyStore: ethKeyStore}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) c := clhttptest.NewTestLocalOnlyHTTPClient() runner := pipeline.NewRunner(pipelineORM, btORM, config.JobPipeline(), config.WebServer(), legacyChains, nil, nil, logger.TestLogger(t), c, c) @@ -767,8 +766,7 @@ func TestRunner_Success_Callback_AsyncJob(t *testing.T) { app := cltest.NewApplicationWithConfig(t, cfg, ethClient, cltest.UseRealExternalInitiatorManager) keyStore := cltest.NewKeyStore(t, app.GetSqlxDB(), pgtest.NewQConfig(true)) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: app.GetSqlxDB(), Client: ethClient, GeneralConfig: cfg, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, cfg, keyStore.Eth()) require.NoError(t, app.Start(testutils.Context(t))) var ( @@ -949,7 +947,7 @@ func TestRunner_Error_Callback_AsyncJob(t *testing.T) { app := cltest.NewApplicationWithConfig(t, cfg, ethClient, cltest.UseRealExternalInitiatorManager) keyStore := cltest.NewKeyStore(t, app.GetSqlxDB(), pgtest.NewQConfig(true)) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: app.GetSqlxDB(), Client: ethClient, GeneralConfig: cfg, KeyStore: keyStore.Eth()}) + relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: app.GetRelayers().LegacyEVM().DB(), Client: ethClient, GeneralConfig: cfg, KeyStore: keyStore.Eth()}) legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) require.NoError(t, app.Start(testutils.Context(t))) diff --git a/core/services/job/spawner_test.go b/core/services/job/spawner_test.go index 7dbf811f783..e7dc1064d0c 100644 --- a/core/services/job/spawner_test.go +++ b/core/services/job/spawner_test.go @@ -22,6 +22,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest2 "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" @@ -95,8 +96,7 @@ func TestSpawner_CreateJobDeleteJob(t *testing.T) { }). Return(nil).Maybe() - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, Client: ethClient, GeneralConfig: config, KeyStore: ethKeyStore}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) t.Run("should respect its dependents", func(t *testing.T) { lggr := logger.TestLogger(t) orm := NewTestORM(t, db, legacyChains, pipeline.NewORM(db, lggr, config.Database(), config.JobPipeline().MaxSuccessfulRuns()), bridges.NewORM(db, lggr, config.Database()), keyStore, config.Database()) @@ -270,8 +270,9 @@ func TestSpawner_CreateJobDeleteJob(t *testing.T) { c.Feature.LogPoller = func(b bool) *bool { return &b }(true) }) lp := &mocklp.LogPoller{} + evmdb := evmtestdb.NewScopedDB(t, config.Database()) testopts := evmtest.TestChainOpts{ - DB: db, + DB: evmdb, Client: ethClient, GeneralConfig: config, LogPoller: lp, @@ -285,7 +286,7 @@ func TestSpawner_CreateJobDeleteJob(t *testing.T) { chain := evmtest.MustGetDefaultChain(t, legacyChains) evmRelayer, err := evmrelayer.NewRelayer(lggr, chain, evmrelayer.RelayerOpts{ - DB: db, + DB: evmdb, QConfig: testopts.GeneralConfig.Database(), CSAETHKeystore: keyStore, EventBroadcaster: pg.NewNullEventBroadcaster(), diff --git a/core/services/keeper/integration_test.go b/core/services/keeper/integration_test.go index 39431063bcd..be1e6a81ff7 100644 --- a/core/services/keeper/integration_test.go +++ b/core/services/keeper/integration_test.go @@ -13,6 +13,7 @@ import ( "github.com/ethereum/go-ethereum/eth/ethconfig" "github.com/onsi/gomega" "github.com/pkg/errors" + "github.com/smartcontractkit/libocr/gethwrappers/link_token_interface" "github.com/stretchr/testify/require" @@ -247,6 +248,7 @@ func TestKeeperEthIntegration(t *testing.T) { c.EVM[0].MinIncomingConfirmations = ptr[uint32](1) // disable reorg protection for this test c.EVM[0].HeadTracker.MaxBufferSize = ptr[uint32](100) // helps prevent missed heads }) + scopedConfig := evmtest.NewChainScopedConfig(t, config) korm := keeper.NewORM(db, logger.TestLogger(t), scopedConfig.Database()) @@ -407,13 +409,16 @@ func TestKeeperForwarderEthIntegration(t *testing.T) { c.EVM[0].Transactions.ForwardersEnabled = ptr(true) // Enable Operator Forwarder flow c.EVM[0].ChainID = (*utils.Big)(testutils.SimulatedChainID) }) + + require.NoError(t, err) + scopedConfig := evmtest.NewChainScopedConfig(t, config) korm := keeper.NewORM(db, logger.TestLogger(t), scopedConfig.Database()) app := cltest.NewApplicationWithConfigV2AndKeyOnSimulatedBlockchain(t, config, backend.Backend(), nodeKey) require.NoError(t, app.Start(testutils.Context(t))) - - forwarderORM := forwarders.NewORM(db, logger.TestLogger(t), config.Database()) + evmdb := heavyweight.EVMDB(t, config.Database()) + forwarderORM := forwarders.NewORM(evmdb, logger.TestLogger(t), config.Database()) chainID := utils.Big(*backend.ConfiguredChainID()) _, err = forwarderORM.CreateForwarder(fwdrAddress, chainID) require.NoError(t, err) diff --git a/core/services/keeper/registry1_1_synchronizer_test.go b/core/services/keeper/registry1_1_synchronizer_test.go index 8444aa50dd4..02ba1ef10e9 100644 --- a/core/services/keeper/registry1_1_synchronizer_test.go +++ b/core/services/keeper/registry1_1_synchronizer_test.go @@ -20,6 +20,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/keeper" @@ -229,8 +230,7 @@ func Test_RegistrySynchronizer1_1_ConfigSetLog(t *testing.T) { registryMock.MockResponse("getKeeperList", []common.Address{fromAddress}).Once() registryMock.MockResponse("getConfig", newConfig).Once() - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} log := registry1_1.KeeperRegistryConfigSet{} logBroadcast := logmocks.NewBroadcast(t) @@ -277,8 +277,7 @@ func Test_RegistrySynchronizer1_1_KeepersUpdatedLog(t *testing.T) { registryMock.MockResponse("getConfig", registryConfig1_1).Once() registryMock.MockResponse("getKeeperList", addresses).Once() - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} log := registry1_1.KeeperRegistryKeepersUpdated{} logBroadcast := logmocks.NewBroadcast(t) @@ -318,8 +317,7 @@ func Test_RegistrySynchronizer1_1_UpkeepCanceledLog(t *testing.T) { cltest.WaitForCount(t, db, "keeper_registries", 1) cltest.WaitForCount(t, db, "upkeep_registrations", 3) - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} log := registry1_1.KeeperRegistryUpkeepCanceled{Id: big.NewInt(1)} logBroadcast := logmocks.NewBroadcast(t) @@ -360,8 +358,7 @@ func Test_RegistrySynchronizer1_1_UpkeepRegisteredLog(t *testing.T) { registryMock := cltest.NewContractMockReceiver(t, ethMock, keeper.Registry1_1ABI, contractAddress) registryMock.MockResponse("getUpkeep", upkeepConfig1_1).Once() - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} log := registry1_1.KeeperRegistryUpkeepRegistered{Id: big.NewInt(1)} logBroadcast := logmocks.NewBroadcast(t) @@ -403,8 +400,7 @@ func Test_RegistrySynchronizer1_1_UpkeepPerformedLog(t *testing.T) { pgtest.MustExec(t, db, `UPDATE upkeep_registrations SET last_run_block_height = 100`) - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash, BlockNumber: 200} log := registry1_1.KeeperRegistryUpkeepPerformed{Id: big.NewInt(0), From: fromAddress} logBroadcast := logmocks.NewBroadcast(t) @@ -431,3 +427,9 @@ func Test_RegistrySynchronizer1_1_UpkeepPerformedLog(t *testing.T) { return upkeep.LastKeeperIndex.Int64 }, testutils.WaitTimeout(t), cltest.DBPollingInterval).Should(gomega.Equal(int64(0))) } + +func getTestHead(t *testing.T, number int64) evmtypes.Head { + cfg := configtest.NewGeneralConfig(t, nil) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) + return cltest.MustInsertHead(t, evmdb, cfg.Database(), number) +} diff --git a/core/services/keeper/registry1_2_synchronizer_test.go b/core/services/keeper/registry1_2_synchronizer_test.go index f593a638f99..c48764a74ef 100644 --- a/core/services/keeper/registry1_2_synchronizer_test.go +++ b/core/services/keeper/registry1_2_synchronizer_test.go @@ -252,8 +252,7 @@ func Test_RegistrySynchronizer1_2_ConfigSetLog(t *testing.T) { Keepers: []common.Address{fromAddress}, }).Once() - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} log := registry1_2.KeeperRegistryConfigSet{} logBroadcast := logmocks.NewBroadcast(t) @@ -304,8 +303,7 @@ func Test_RegistrySynchronizer1_2_KeepersUpdatedLog(t *testing.T) { Keepers: addresses, }).Once() - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} log := registry1_2.KeeperRegistryKeepersUpdated{} logBroadcast := logmocks.NewBroadcast(t) @@ -347,8 +345,7 @@ func Test_RegistrySynchronizer1_2_UpkeepCanceledLog(t *testing.T) { cltest.WaitForCount(t, db, "keeper_registries", 1) cltest.WaitForCount(t, db, "upkeep_registrations", 3) - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} log := registry1_2.KeeperRegistryUpkeepCanceled{Id: big.NewInt(3)} logBroadcast := logmocks.NewBroadcast(t) @@ -390,8 +387,7 @@ func Test_RegistrySynchronizer1_2_UpkeepRegisteredLog(t *testing.T) { registryMock := cltest.NewContractMockReceiver(t, ethMock, keeper.Registry1_2ABI, contractAddress) registryMock.MockResponse("getUpkeep", upkeepConfig1_2).Once() - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} log := registry1_2.KeeperRegistryUpkeepRegistered{Id: big.NewInt(420)} logBroadcast := logmocks.NewBroadcast(t) @@ -434,8 +430,7 @@ func Test_RegistrySynchronizer1_2_UpkeepPerformedLog(t *testing.T) { pgtest.MustExec(t, db, `UPDATE upkeep_registrations SET last_run_block_height = 100`) - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash, BlockNumber: 200} log := registry1_2.KeeperRegistryUpkeepPerformed{Id: big.NewInt(3), From: fromAddress} logBroadcast := logmocks.NewBroadcast(t) @@ -500,8 +495,7 @@ func Test_RegistrySynchronizer1_2_UpkeepGasLimitSetLog(t *testing.T) { newConfig.ExecuteGas = 4_000_000 // change from default registryMock.MockResponse("getUpkeep", newConfig).Once() - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} log := registry1_2.KeeperRegistryUpkeepGasLimitSet{Id: big.NewInt(3), GasLimit: big.NewInt(4_000_000)} logBroadcast := logmocks.NewBroadcast(t) @@ -543,8 +537,7 @@ func Test_RegistrySynchronizer1_2_UpkeepReceivedLog(t *testing.T) { registryMock := cltest.NewContractMockReceiver(t, ethMock, keeper.Registry1_2ABI, contractAddress) registryMock.MockResponse("getUpkeep", upkeepConfig1_2).Once() - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} log := registry1_2.KeeperRegistryUpkeepReceived{Id: big.NewInt(420)} logBroadcast := logmocks.NewBroadcast(t) @@ -583,8 +576,7 @@ func Test_RegistrySynchronizer1_2_UpkeepMigratedLog(t *testing.T) { cltest.WaitForCount(t, db, "keeper_registries", 1) cltest.WaitForCount(t, db, "upkeep_registrations", 3) - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} log := registry1_2.KeeperRegistryUpkeepMigrated{Id: big.NewInt(3)} logBroadcast := logmocks.NewBroadcast(t) diff --git a/core/services/keeper/registry1_3_synchronizer_test.go b/core/services/keeper/registry1_3_synchronizer_test.go index 53b5cbf983f..42141898a8a 100644 --- a/core/services/keeper/registry1_3_synchronizer_test.go +++ b/core/services/keeper/registry1_3_synchronizer_test.go @@ -257,8 +257,7 @@ func Test_RegistrySynchronizer1_3_ConfigSetLog(t *testing.T) { Keepers: []common.Address{fromAddress}, }).Once() - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} log := registry1_3.KeeperRegistryConfigSet{} logBroadcast := logmocks.NewBroadcast(t) @@ -309,8 +308,7 @@ func Test_RegistrySynchronizer1_3_KeepersUpdatedLog(t *testing.T) { Keepers: addresses, }).Once() - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} log := registry1_3.KeeperRegistryKeepersUpdated{} logBroadcast := logmocks.NewBroadcast(t) @@ -352,8 +350,7 @@ func Test_RegistrySynchronizer1_3_UpkeepCanceledLog(t *testing.T) { cltest.WaitForCount(t, db, "keeper_registries", 1) cltest.WaitForCount(t, db, "upkeep_registrations", 3) - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} log := registry1_3.KeeperRegistryUpkeepCanceled{Id: big.NewInt(3)} logBroadcast := logmocks.NewBroadcast(t) @@ -395,8 +392,7 @@ func Test_RegistrySynchronizer1_3_UpkeepRegisteredLog(t *testing.T) { registryMock := cltest.NewContractMockReceiver(t, ethMock, keeper.Registry1_3ABI, contractAddress) registryMock.MockResponse("getUpkeep", upkeepConfig1_3).Once() - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} log := registry1_3.KeeperRegistryUpkeepRegistered{Id: big.NewInt(420)} logBroadcast := logmocks.NewBroadcast(t) @@ -439,8 +435,7 @@ func Test_RegistrySynchronizer1_3_UpkeepPerformedLog(t *testing.T) { pgtest.MustExec(t, db, `UPDATE upkeep_registrations SET last_run_block_height = 100`) - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash, BlockNumber: 200} log := registry1_3.KeeperRegistryUpkeepPerformed{Id: big.NewInt(3), From: fromAddress} logBroadcast := logmocks.NewBroadcast(t) @@ -505,8 +500,7 @@ func Test_RegistrySynchronizer1_3_UpkeepGasLimitSetLog(t *testing.T) { newConfig.ExecuteGas = 4_000_000 // change from default registryMock.MockResponse("getUpkeep", newConfig).Once() - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} log := registry1_3.KeeperRegistryUpkeepGasLimitSet{Id: big.NewInt(3), GasLimit: big.NewInt(4_000_000)} logBroadcast := logmocks.NewBroadcast(t) @@ -548,8 +542,7 @@ func Test_RegistrySynchronizer1_3_UpkeepReceivedLog(t *testing.T) { registryMock := cltest.NewContractMockReceiver(t, ethMock, keeper.Registry1_3ABI, contractAddress) registryMock.MockResponse("getUpkeep", upkeepConfig1_3).Once() - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} log := registry1_3.KeeperRegistryUpkeepReceived{Id: big.NewInt(420)} logBroadcast := logmocks.NewBroadcast(t) @@ -588,8 +581,7 @@ func Test_RegistrySynchronizer1_3_UpkeepMigratedLog(t *testing.T) { cltest.WaitForCount(t, db, "keeper_registries", 1) cltest.WaitForCount(t, db, "upkeep_registrations", 3) - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} log := registry1_3.KeeperRegistryUpkeepMigrated{Id: big.NewInt(3)} logBroadcast := logmocks.NewBroadcast(t) @@ -630,8 +622,7 @@ func Test_RegistrySynchronizer1_3_UpkeepPausedLog_UpkeepUnpausedLog(t *testing.T cltest.WaitForCount(t, db, "keeper_registries", 1) cltest.WaitForCount(t, db, "upkeep_registrations", 3) - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} log := registry1_3.KeeperRegistryUpkeepPaused{Id: upkeepId} logBroadcast := logmocks.NewBroadcast(t) @@ -646,8 +637,7 @@ func Test_RegistrySynchronizer1_3_UpkeepPausedLog_UpkeepUnpausedLog(t *testing.T cltest.WaitForCount(t, db, "upkeep_registrations", 2) - cfg = configtest.NewGeneralConfig(t, nil) - head = cltest.MustInsertHead(t, db, cfg.Database(), 2) + head = getTestHead(t, 2) rawLog = types.Log{BlockHash: head.Hash} unpausedlog := registry1_3.KeeperRegistryUpkeepUnpaused{Id: upkeepId} logBroadcast = logmocks.NewBroadcast(t) @@ -700,8 +690,7 @@ func Test_RegistrySynchronizer1_3_UpkeepCheckDataUpdatedLog(t *testing.T) { cltest.WaitForCount(t, db, "keeper_registries", 1) cltest.WaitForCount(t, db, "upkeep_registrations", 1) - cfg := configtest.NewGeneralConfig(t, nil) - head := cltest.MustInsertHead(t, db, cfg.Database(), 1) + head := getTestHead(t, 1) rawLog := types.Log{BlockHash: head.Hash} _ = logmocks.NewBroadcast(t) newCheckData := []byte("Chainlink") diff --git a/core/services/keeper/registry_synchronizer_helper_test.go b/core/services/keeper/registry_synchronizer_helper_test.go index 6b9bb815a89..ab22963bfbe 100644 --- a/core/services/keeper/registry_synchronizer_helper_test.go +++ b/core/services/keeper/registry_synchronizer_helper_test.go @@ -15,6 +15,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/job" @@ -43,7 +44,8 @@ func setupRegistrySync(t *testing.T, version keeper.RegistryVersion) ( lbMock := logmocks.NewBroadcaster(t) lbMock.On("AddDependents", 1).Maybe() j := cltest.MustInsertKeeperJob(t, db, korm, cltest.NewEIP55Address(), cltest.NewEIP55Address()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, Client: ethClient, LogBroadcaster: lbMock, GeneralConfig: cfg, KeyStore: keyStore.Eth()}) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) + relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: evmdb, Client: ethClient, LogBroadcaster: lbMock, GeneralConfig: cfg, KeyStore: keyStore.Eth()}) legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) ch := evmtest.MustGetDefaultChain(t, legacyChains) jpv2 := cltest.NewJobPipelineV2(t, cfg.WebServer(), cfg.JobPipeline(), cfg.Database(), legacyChains, db, keyStore, nil, nil) diff --git a/core/services/keeper/upkeep_executer_test.go b/core/services/keeper/upkeep_executer_test.go index 702633e1d08..3d22180e3fb 100644 --- a/core/services/keeper/upkeep_executer_test.go +++ b/core/services/keeper/upkeep_executer_test.go @@ -26,6 +26,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" @@ -77,7 +78,8 @@ func setup(t *testing.T, estimator gas.EvmFeeEstimator, overrideFn func(c *chain ethClient.On("IsL2").Return(false).Maybe() ethClient.On("HeadByNumber", mock.Anything, mock.Anything).Maybe().Return(&evmtypes.Head{Number: 1, Hash: utils.NewHash()}, nil) txm := txmmocks.NewMockEvmTxManager(t) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{TxManager: txm, DB: db, Client: ethClient, KeyStore: keyStore.Eth(), GeneralConfig: cfg, GasEstimator: estimator}) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) + relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{TxManager: txm, DB: evmdb, Client: ethClient, KeyStore: keyStore.Eth(), GeneralConfig: cfg, GasEstimator: estimator}) legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) jpv2 := cltest.NewJobPipelineV2(t, cfg.WebServer(), cfg.JobPipeline(), cfg.Database(), legacyChains, db, keyStore, nil, nil) ch := evmtest.MustGetDefaultChain(t, legacyChains) diff --git a/core/services/keystore/eth_test.go b/core/services/keystore/eth_test.go index 78131bec133..ff97f6e5e8c 100644 --- a/core/services/keystore/eth_test.go +++ b/core/services/keystore/eth_test.go @@ -19,6 +19,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/services/keystore" "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey" @@ -32,6 +33,7 @@ func Test_EthKeyStore(t *testing.T) { cfg := configtest.NewTestGeneralConfig(t) keyStore := keystore.ExposedNewMaster(t, db, cfg.Database()) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) err := keyStore.Unlock(cltest.Password) require.NoError(t, err) ethKeyStore := keyStore.Eth() @@ -112,7 +114,7 @@ func Test_EthKeyStore(t *testing.T) { cltest.AssertCount(t, db, statesTableName, 1) // add one eth_tx - txStore := cltest.NewTestTxStore(t, db, cfg.Database()) + txStore := cltest.NewTestTxStore(t, evmdb, cfg.Database()) cltest.MustInsertConfirmedEthTxWithLegacyAttempt(t, txStore, 0, 42, key.Address) _, err = ethKeyStore.Delete(key.ID()) diff --git a/core/services/ocr2/delegate_test.go b/core/services/ocr2/delegate_test.go index 97c9faa44d4..36c1258259e 100644 --- a/core/services/ocr2/delegate_test.go +++ b/core/services/ocr2/delegate_test.go @@ -15,6 +15,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" @@ -43,8 +44,9 @@ func TestGetEVMEffectiveTransmitterID(t *testing.T) { require.NoError(t, keyStore.OCR2().Add(cltest.DefaultOCR2Key)) lggr := logger.TestLogger(t) + evmdb := evmtestdb.NewScopedDB(t, config.Database()) txManager := txmmocks.NewMockEvmTxManager(t) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth(), TxManager: txManager}) + relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: evmdb, GeneralConfig: config, KeyStore: keyStore.Eth(), TxManager: txManager}) require.True(t, relayExtenders.Len() > 0) legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) diff --git a/core/services/ocr2/plugins/ocr2keeper/evm21/logprovider/integration_test.go b/core/services/ocr2/plugins/ocr2keeper/evm21/logprovider/integration_test.go index b5f229f6015..ad1e2aebe5b 100644 --- a/core/services/ocr2/plugins/ocr2keeper/evm21/logprovider/integration_test.go +++ b/core/services/ocr2/plugins/ocr2keeper/evm21/logprovider/integration_test.go @@ -20,13 +20,13 @@ import ( "go.uber.org/zap/zapcore" "golang.org/x/time/rate" - "github.com/smartcontractkit/sqlx" - ocr2keepers "github.com/smartcontractkit/ocr2keepers/pkg/v3/types" "github.com/smartcontractkit/chainlink/v2/core/assets" + "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller" "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/automation_utils_2_1" "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/log_upkeep_counter_wrapper" @@ -662,7 +662,7 @@ func newPlainLogTriggerConfig(upkeepAddr common.Address) logprovider.LogTriggerC } } -func setupDependencies(t *testing.T, db *sqlx.DB, backend *backends.SimulatedBackend) (logpoller.LogPollerTest, *evmclient.SimulatedBackendClient, abi.ABI) { +func setupDependencies(t *testing.T, db *evmdb.ScopedDB, backend *backends.SimulatedBackend) (logpoller.LogPollerTest, *evmclient.SimulatedBackendClient, abi.ABI) { ethClient := evmclient.NewSimulatedBackendClient(t, backend, big.NewInt(1337)) pollerLggr := logger.TestLogger(t) pollerLggr.SetLogLevel(zapcore.WarnLevel) @@ -703,8 +703,8 @@ func setupBackend(t *testing.T) (*backends.SimulatedBackend, func(), []*bind.Tra func ptr[T any](v T) *T { return &v } -func setupDB(t *testing.T) *sqlx.DB { - _, db := heavyweight.FullTestDBV2(t, fmt.Sprintf("%s%d", "chainlink_test", 5432), func(c *chainlink.Config, s *chainlink.Secrets) { +func setupDB(t *testing.T) *evmdb.ScopedDB { + cfg, _ := heavyweight.FullTestDBV2(t, fmt.Sprintf("%s%d", "chainlink_test", 5432), func(c *chainlink.Config, s *chainlink.Secrets) { c.Feature.LogPoller = ptr(true) c.OCR.Enabled = ptr(false) @@ -713,7 +713,8 @@ func setupDB(t *testing.T) *sqlx.DB { c.EVM[0].Transactions.ForwardersEnabled = ptr(true) c.EVM[0].GasEstimator.Mode = ptr("FixedPrice") }) - return db + evmdb := heavyweight.EVMDB(t, cfg.Database()) + return evmdb } type mockUpkeepStateStore struct { diff --git a/core/services/ocr2/plugins/ocr2keeper/integration_test.go b/core/services/ocr2/plugins/ocr2keeper/integration_test.go index eea9c1574cf..1a70e14d6fd 100644 --- a/core/services/ocr2/plugins/ocr2keeper/integration_test.go +++ b/core/services/ocr2/plugins/ocr2keeper/integration_test.go @@ -449,7 +449,7 @@ func setupForwarderForNode( backend.Commit() // add forwarder address to be tracked in db - forwarderORM := forwarders.NewORM(app.GetSqlxDB(), logger.TestLogger(t), app.GetConfig().Database()) + forwarderORM := forwarders.NewORM(app.GetRelayers().LegacyEVM().DB(), logger.TestLogger(t), app.GetConfig().Database()) chainID := utils.Big(*backend.Blockchain().Config().ChainID) _, err = forwarderORM.CreateForwarder(faddr, chainID) require.NoError(t, err) diff --git a/core/services/ocr2/plugins/ocr2vrf/internal/ocr2vrf_integration_test.go b/core/services/ocr2/plugins/ocr2vrf/internal/ocr2vrf_integration_test.go index c6a4552683a..cb357962638 100644 --- a/core/services/ocr2/plugins/ocr2vrf/internal/ocr2vrf_integration_test.go +++ b/core/services/ocr2/plugins/ocr2vrf/internal/ocr2vrf_integration_test.go @@ -281,7 +281,7 @@ func setupNodeOCR2( b.Commit() // Add the forwarder to the node's forwarder manager. - forwarderORM := forwarders.NewORM(app.GetSqlxDB(), logger.TestLogger(t), config.Database()) + forwarderORM := forwarders.NewORM(app.GetRelayers().LegacyEVM().DB(), logger.TestLogger(t), config.Database()) chainID := utils.Big(*b.Blockchain().Config().ChainID) _, err = forwarderORM.CreateForwarder(faddr, chainID) require.NoError(t, err) diff --git a/core/services/pg/locked_db.go b/core/services/pg/locked_db.go index f4f8bfa3b36..7d946baee41 100644 --- a/core/services/pg/locked_db.go +++ b/core/services/pg/locked_db.go @@ -175,9 +175,8 @@ func SchemaScopedConnection(conn url.URL, schema string) url.URL { // https://www.postgresql.org/docs/16/app-psql.html u := conn - opt := "options=-csearch_path=" queryVals := u.Query() - queryVals.Add(opt, schema) + queryVals.Add("options", "-csearch_path="+schema) u.RawQuery = queryVals.Encode() return u diff --git a/core/services/pipeline/orm_test.go b/core/services/pipeline/orm_test.go index 4c03ce16ef8..b5ddc7aa742 100644 --- a/core/services/pipeline/orm_test.go +++ b/core/services/pipeline/orm_test.go @@ -12,15 +12,18 @@ import ( "gopkg.in/guregu/null.v4" "github.com/smartcontractkit/chainlink/v2/core/bridges" + "github.com/smartcontractkit/chainlink/v2/core/chains/evm" "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" "github.com/smartcontractkit/chainlink/v2/core/internal/cltest/heavyweight" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest2 "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" "github.com/smartcontractkit/chainlink/v2/core/services/job" + "github.com/smartcontractkit/chainlink/v2/core/services/keystore" "github.com/smartcontractkit/chainlink/v2/core/services/pg" "github.com/smartcontractkit/chainlink/v2/core/services/pipeline" evmrelay "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm" @@ -521,8 +524,7 @@ func Test_GetUnfinishedRuns_Keepers(t *testing.T) { porm := pipeline.NewORM(db, lggr, config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgeORM := bridges.NewORM(db, lggr, config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) jorm := job.NewORM(db, legacyChains, porm, bridgeORM, keyStore, lggr, config.Database()) defer func() { assert.NoError(t, jorm.Close()) }() @@ -623,8 +625,7 @@ func Test_GetUnfinishedRuns_DirectRequest(t *testing.T) { porm := pipeline.NewORM(db, lggr, config.Database(), config.JobPipeline().MaxSuccessfulRuns()) bridgeORM := bridges.NewORM(db, lggr, config.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: config, KeyStore: keyStore.Eth()}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, config, keyStore.Eth()) jorm := job.NewORM(db, legacyChains, porm, bridgeORM, keyStore, lggr, config.Database()) defer func() { assert.NoError(t, jorm.Close()) }() @@ -765,3 +766,9 @@ func Test_Prune(t *testing.T) { cnt = pgtest.MustCount(t, db, "SELECT count(*) FROM pipeline_runs WHERE pipeline_spec_id = $1 AND state = $2", ps2.ID, pipeline.RunStatusSuspended) assert.Equal(t, 3, cnt) } + +func setupLegacyEVMChains(t testing.TB, config evm.AppConfig, ks keystore.Eth) *evm.LegacyChains { + evmdb := evmtestdb.NewScopedDB(t, config.Database()) + relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: evmdb, GeneralConfig: config, KeyStore: ks}) + return evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) +} diff --git a/core/services/pipeline/runner_test.go b/core/services/pipeline/runner_test.go index 22b70829ba5..dfce7a1a361 100644 --- a/core/services/pipeline/runner_test.go +++ b/core/services/pipeline/runner_test.go @@ -20,14 +20,11 @@ import ( "github.com/stretchr/testify/require" "gopkg.in/guregu/null.v4" - evmrelay "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm" - "github.com/smartcontractkit/chainlink/v2/core/bridges" bridgesMocks "github.com/smartcontractkit/chainlink/v2/core/bridges/mocks" "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" clhttptest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/httptest" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" @@ -43,8 +40,7 @@ import ( func newRunner(t testing.TB, db *sqlx.DB, bridgeORM bridges.ORM, cfg chainlink.GeneralConfig) (pipeline.Runner, *mocks.ORM) { lggr := logger.TestLogger(t) ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth() - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: cfg, KeyStore: ethKeyStore}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, cfg, ethKeyStore) orm := mocks.NewORM(t) q := pg.NewQ(db, lggr, cfg.Database()) @@ -480,8 +476,7 @@ func Test_PipelineRunner_HandleFaultsPersistRun(t *testing.T) { Return(nil) cfg := configtest.NewTestGeneralConfig(t) ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth() - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: cfg, KeyStore: ethKeyStore}) - legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) + legacyChains := setupLegacyEVMChains(t, cfg, ethKeyStore) lggr := logger.TestLogger(t) r := pipeline.NewRunner(orm, btORM, cfg.JobPipeline(), cfg.WebServer(), legacyChains, ethKeyStore, nil, lggr, nil, nil) diff --git a/core/services/pipeline/task.eth_call_test.go b/core/services/pipeline/task.eth_call_test.go index 77a10681fb4..1d4e5a33426 100644 --- a/core/services/pipeline/task.eth_call_test.go +++ b/core/services/pipeline/task.eth_call_test.go @@ -19,7 +19,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" keystoremocks "github.com/smartcontractkit/chainlink/v2/core/services/keystore/mocks" @@ -254,12 +254,12 @@ func TestETHCallTask(t *testing.T) { lggr := logger.TestLogger(t) keyStore := keystoremocks.NewEth(t) - txManager := txmmocks.NewMockEvmTxManager(t) - db := pgtest.NewSqlxDB(t) var legacyChains evm.LegacyChainContainer if test.expectedErrorCause != nil || test.expectedErrorContains != "" { - exts := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: cfg, TxManager: txManager, KeyStore: keyStore}) + txManager := txmmocks.NewMockEvmTxManager(t) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) + exts := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: evmdb, GeneralConfig: cfg, TxManager: txManager, KeyStore: keyStore}) legacyChains = evmrelay.NewLegacyChainsFromRelayerExtenders(exts) } else { legacyChains = cltest.NewLegacyChainsWithMockChain(t, ethClient, cfg) diff --git a/core/services/pipeline/task.eth_tx_test.go b/core/services/pipeline/task.eth_tx_test.go index a280e6f2720..57cd35d69fd 100644 --- a/core/services/pipeline/task.eth_tx_test.go +++ b/core/services/pipeline/task.eth_tx_test.go @@ -17,7 +17,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/logger" clnull "github.com/smartcontractkit/chainlink/v2/core/null" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" @@ -566,14 +566,14 @@ func TestETHTxTask(t *testing.T) { keyStore := keystoremocks.NewEth(t) txManager := txmmocks.NewMockEvmTxManager(t) - db := pgtest.NewSqlxDB(t) cfg := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) { c.EVM[0].GasEstimator.LimitDefault = ptr(defaultGasLimit) c.EVM[0].GasEstimator.LimitJobType.DR = ptr(drJobTypeGasLimit) }) lggr := logger.TestLogger(t) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: db, GeneralConfig: cfg, + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) + relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{DB: evmdb, GeneralConfig: cfg, TxManager: txManager, KeyStore: keyStore}) legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) diff --git a/core/services/promreporter/prom_reporter_test.go b/core/services/promreporter/prom_reporter_test.go index 9629045cb0b..87c227a4554 100644 --- a/core/services/promreporter/prom_reporter_test.go +++ b/core/services/promreporter/prom_reporter_test.go @@ -9,6 +9,7 @@ import ( evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/services/promreporter" "github.com/stretchr/testify/assert" @@ -55,10 +56,11 @@ func Test_PromReporter_OnNewLongestChain(t *testing.T) { }) t.Run("with unconfirmed evm.txes", func(t *testing.T) { - db := pgtest.NewSqlxDB(t) + coredb := pgtest.NewSqlxDB(t) cfg := configtest.NewGeneralConfig(t, nil) - txStore := cltest.NewTestTxStore(t, db, cfg.Database()) - ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth() + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) + txStore := cltest.NewTestTxStore(t, evmdb, cfg.Database()) + ethKeyStore := cltest.NewKeyStore(t, coredb, cfg.Database()).Eth() _, fromAddress := cltest.MustAddRandomKeyToKeystore(t, ethKeyStore) var subscribeCalls atomic.Int32 @@ -75,14 +77,14 @@ func Test_PromReporter_OnNewLongestChain(t *testing.T) { subscribeCalls.Add(1) }). Return() - reporter := promreporter.NewPromReporter(db.DB, logger.TestLogger(t), backend, 10*time.Millisecond) + reporter := promreporter.NewPromReporter(coredb.DB, logger.TestLogger(t), backend, 10*time.Millisecond) require.NoError(t, reporter.Start(testutils.Context(t))) defer func() { assert.NoError(t, reporter.Close()) }() etx := cltest.MustInsertUnconfirmedEthTxWithBroadcastLegacyAttempt(t, txStore, 0, fromAddress) cltest.MustInsertUnconfirmedEthTxWithBroadcastLegacyAttempt(t, txStore, 1, fromAddress) cltest.MustInsertUnconfirmedEthTxWithBroadcastLegacyAttempt(t, txStore, 2, fromAddress) - require.NoError(t, utils.JustError(db.Exec(`UPDATE evm.tx_attempts SET broadcast_before_block_num = 7 WHERE eth_tx_id = $1`, etx.ID))) + require.NoError(t, utils.JustError(coredb.Exec(`UPDATE evm.tx_attempts SET broadcast_before_block_num = 7 WHERE eth_tx_id = $1`, etx.ID))) head := newHead() reporter.OnNewLongestChain(testutils.Context(t), &head) diff --git a/core/services/relay/evm/config_poller_test.go b/core/services/relay/evm/config_poller_test.go index 73c16a19596..c032b2253e4 100644 --- a/core/services/relay/evm/config_poller_test.go +++ b/core/services/relay/evm/config_poller_test.go @@ -1,4 +1,4 @@ -package evm +package evm_test import ( "database/sql" @@ -28,6 +28,8 @@ import ( ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types" ocrtypes2 "github.com/smartcontractkit/libocr/offchainreporting2plus/types" + configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" + "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" evmClientMocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client/mocks" @@ -35,9 +37,10 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller/mocks" "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/link_token_interface" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/testhelpers" + evmrelay "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm" "github.com/smartcontractkit/chainlink/v2/core/utils" ) @@ -82,18 +85,18 @@ func TestConfigPoller(t *testing.T) { require.NoError(t, err) b.Commit() - db := pgtest.NewSqlxDB(t) - cfg := pgtest.NewQConfig(false) + cfg := configtest.NewTestGeneralConfig(t) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) ethClient = evmclient.NewSimulatedBackendClient(t, b, testutils.SimulatedChainID) ctx := testutils.Context(t) - lorm := logpoller.NewORM(testutils.SimulatedChainID, db, lggr, cfg) + lorm := logpoller.NewORM(testutils.SimulatedChainID, evmdb, lggr, cfg.Database()) lp = logpoller.NewLogPoller(lorm, ethClient, lggr, 100*time.Millisecond, 1, 2, 2, 1000) require.NoError(t, lp.Start(ctx)) t.Cleanup(func() { lp.Close() }) } t.Run("LatestConfig errors if there is no config in logs and config store is unconfigured", func(t *testing.T) { - cp, err := NewConfigPoller(lggr, ethClient, lp, ocrAddress, nil) + cp, err := evmrelay.NewConfigPoller(lggr, ethClient, lp, ocrAddress, nil) require.NoError(t, err) _, err = cp.LatestConfig(testutils.Context(t), 0) @@ -102,7 +105,7 @@ func TestConfigPoller(t *testing.T) { }) t.Run("happy path (with config store)", func(t *testing.T) { - cp, err := NewConfigPoller(lggr, ethClient, lp, ocrAddress, &configStoreContractAddr) + cp, err := evmrelay.NewConfigPoller(lggr, ethClient, lp, ocrAddress, &configStoreContractAddr) require.NoError(t, err) // Should have no config to begin with. _, configDigest, err := cp.LatestConfigDetails(testutils.Context(t)) @@ -173,7 +176,7 @@ func TestConfigPoller(t *testing.T) { mp.On("LatestLogByEventSigWithConfs", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, sql.ErrNoRows) t.Run("if callLatestConfigDetails succeeds", func(t *testing.T) { - cp, err := newConfigPoller(lggr, ethClient, mp, ocrAddress, &configStoreContractAddr) + cp, err := evmrelay.NewConfigPoller(lggr, ethClient, mp, ocrAddress, &configStoreContractAddr) require.NoError(t, err) t.Run("when config has not been set, returns zero values", func(t *testing.T) { @@ -210,12 +213,9 @@ func TestConfigPoller(t *testing.T) { failingClient := new(evmClientMocks.Client) failingClient.On("ConfiguredChainID").Return(big.NewInt(42)) failingClient.On("CallContract", mock.Anything, mock.Anything, mock.Anything).Return(nil, errors.New("something exploded")) - cp, err := newConfigPoller(lggr, failingClient, mp, ocrAddress, &configStoreContractAddr) + cp, err := evmrelay.NewConfigPoller(lggr, failingClient, mp, ocrAddress, &configStoreContractAddr) require.NoError(t, err) - cp.configStoreContractAddr = &configStoreContractAddr - cp.configStoreContract = configStoreContract - _, _, err = cp.LatestConfigDetails(testutils.Context(t)) assert.EqualError(t, err, "something exploded") @@ -249,7 +249,7 @@ func TestConfigPoller(t *testing.T) { mp.On("LatestLogByEventSigWithConfs", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, sql.ErrNoRows) t.Run("if callReadConfig succeeds", func(t *testing.T) { - cp, err := newConfigPoller(lggr, ethClient, mp, ocrAddress, &configStoreContractAddr) + cp, err := evmrelay.NewConfigPoller(lggr, ethClient, mp, ocrAddress, &configStoreContractAddr) require.NoError(t, err) t.Run("when config has not been set, returns error", func(t *testing.T) { @@ -271,9 +271,9 @@ func TestConfigPoller(t *testing.T) { DeltaC: 10, }, ocrContract, user) - signerAddresses, err := OnchainPublicKeyToAddress(contractConfig.Signers) + signerAddresses, err := evmrelay.OnchainPublicKeyToAddress(contractConfig.Signers) require.NoError(t, err) - transmitterAddresses, err := AccountToAddress(contractConfig.Transmitters) + transmitterAddresses, err := evmrelay.AccountToAddress(contractConfig.Transmitters) require.NoError(t, err) configuration := ocrconfigurationstoreevmsimple.OCRConfigurationStoreEVMSimpleConfigurationEVMSimple{ @@ -311,7 +311,7 @@ func TestConfigPoller(t *testing.T) { // initial call to retrieve config store address from aggregator return *callArgs.To == ocrAddress }), mock.Anything).Return(nil, errors.New("something exploded")).Once() - cp, err := newConfigPoller(lggr, failingClient, mp, ocrAddress, &configStoreContractAddr) + cp, err := evmrelay.NewConfigPoller(lggr, failingClient, mp, ocrAddress, &configStoreContractAddr) require.NoError(t, err) _, err = cp.LatestConfig(testutils.Context(t), 0) @@ -359,9 +359,9 @@ func setConfig(t *testing.T, pluginConfig median.OffchainConfig, ocrContract *oc onchainConfig, ) require.NoError(t, err) - signerAddresses, err := OnchainPublicKeyToAddress(signers) + signerAddresses, err := evmrelay.OnchainPublicKeyToAddress(signers) require.NoError(t, err) - transmitterAddresses, err := AccountToAddress(transmitters) + transmitterAddresses, err := evmrelay.AccountToAddress(transmitters) require.NoError(t, err) _, err = ocrContract.SetConfig(user, signerAddresses, transmitterAddresses, threshold, onchainConfig, offchainConfigVersion, offchainConfig) require.NoError(t, err) diff --git a/core/services/relay/evm/evm.go b/core/services/relay/evm/evm.go index 6ae2052c408..7d60d04a33a 100644 --- a/core/services/relay/evm/evm.go +++ b/core/services/relay/evm/evm.go @@ -16,7 +16,6 @@ import ( "github.com/smartcontractkit/libocr/offchainreporting2/reportingplugin/median/evmreportcodec" "github.com/smartcontractkit/libocr/offchainreporting2plus/chains/evmutil" ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types" - "github.com/smartcontractkit/sqlx" "go.uber.org/multierr" "golang.org/x/exp/maps" @@ -24,6 +23,7 @@ import ( txmgrcommon "github.com/smartcontractkit/chainlink/v2/common/txmgr" "github.com/smartcontractkit/chainlink/v2/core/chains/evm" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" txm "github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services" @@ -48,7 +48,7 @@ import ( var _ relaytypes.Relayer = &Relayer{} type Relayer struct { - db *sqlx.DB + db *evmdb.ScopedDB chain evm.Chain lggr logger.Logger ks CSAETHKeystore @@ -63,7 +63,7 @@ type CSAETHKeystore interface { } type RelayerOpts struct { - *sqlx.DB + DB *evmdb.ScopedDB pg.QConfig CSAETHKeystore pg.EventBroadcaster @@ -187,7 +187,10 @@ func (r *Relayer) NewMercuryProvider(rargs relaytypes.RelayArgs, pargs relaytype default: return nil, fmt.Errorf("invalid feed version %d", feedID.Version()) } - transmitter := mercury.NewTransmitter(r.lggr, configWatcher.ContractConfigTracker(), client, privKey.PublicKey, rargs.JobID, *relayConfig.FeedID, r.db, r.pgCfg, transmitterCodec) + // KRR note to self: this is going to break because the mercury transmitter is using db tables outside the `evm` schema but mercury is using the relayer's db which is scoped to evm. + // this new provider code should be passed a connection string and make it's own DB. might need some sort of DB manager so that multiple providers can share the same DB object. not sure + // how many times NewProvider is called. + transmitter := mercury.NewTransmitter(r.lggr, configWatcher.ContractConfigTracker(), client, privKey.PublicKey, rargs.JobID, *relayConfig.FeedID, r.db.SqlxDB(), r.pgCfg, transmitterCodec) return NewMercuryProvider(configWatcher, transmitter, reportCodecV1, reportCodecV2, reportCodecV3, r.lggr), nil } @@ -512,7 +515,8 @@ func (r *Relayer) NewMedianProvider(rargs relaytypes.RelayArgs, pargs relaytypes return nil, err } - medianContract, err := newMedianContract(configWatcher.ContractConfigTracker(), configWatcher.contractAddress, configWatcher.chain, rargs.JobID, r.db, r.lggr) + // KRR note to self: this is going to break because the queries that use the `db` are not scoped to the `evm` schema + medianContract, err := newMedianContract(configWatcher.ContractConfigTracker(), configWatcher.contractAddress, configWatcher.chain, rargs.JobID, r.db.SqlxDB(), r.lggr) if err != nil { return nil, err } diff --git a/core/services/relay/evm/evm_test.go b/core/services/relay/evm/evm_test.go index df7cd8eb818..3a628c9b919 100644 --- a/core/services/relay/evm/evm_test.go +++ b/core/services/relay/evm/evm_test.go @@ -3,9 +3,9 @@ package evm_test import ( "testing" - "github.com/smartcontractkit/sqlx" "github.com/stretchr/testify/assert" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/services/pg" "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm" @@ -14,7 +14,7 @@ import ( func TestRelayerOpts_Validate(t *testing.T) { cfg := configtest.NewTestGeneralConfig(t) type fields struct { - DB *sqlx.DB + DB *evmdb.ScopedDB QConfig pg.QConfig CSAETHKeystore evm.CSAETHKeystore EventBroadcaster pg.EventBroadcaster diff --git a/core/services/relay/evm/functions/config_poller_test.go b/core/services/relay/evm/functions/config_poller_test.go index d6573ef3544..ae5e8f89e44 100644 --- a/core/services/relay/evm/functions/config_poller_test.go +++ b/core/services/relay/evm/functions/config_poller_test.go @@ -27,6 +27,8 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller" "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/link_token_interface" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" + configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm" @@ -72,14 +74,15 @@ func runTest(t *testing.T, pluginType functions.FunctionsPluginType, expectedDig ) require.NoError(t, err) b.Commit() - db := pgtest.NewSqlxDB(t) - defer db.Close() + cfg := pgtest.NewQConfig(false) ethClient := evmclient.NewSimulatedBackendClient(t, b, big.NewInt(1337)) defer ethClient.Close() lggr := logger.TestLogger(t) ctx := testutils.Context(t) - lorm := logpoller.NewORM(big.NewInt(1337), db, lggr, cfg) + evmdb := evmtestdb.NewScopedDB(t, configtest.NewTestGeneralConfig(t).Database()) + defer evmdb.Close() + lorm := logpoller.NewORM(big.NewInt(1337), evmdb, lggr, cfg) lp := logpoller.NewLogPoller(lorm, ethClient, lggr, 100*time.Millisecond, 1, 2, 2, 1000) defer lp.Close() require.NoError(t, lp.Start(ctx)) diff --git a/core/services/relay/evm/mercury/helpers_test.go b/core/services/relay/evm/mercury/helpers_test.go index 4e3587b5de6..8f769318e91 100644 --- a/core/services/relay/evm/mercury/helpers_test.go +++ b/core/services/relay/evm/mercury/helpers_test.go @@ -18,6 +18,7 @@ import ( ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types" evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" + evmdb "github.com/smartcontractkit/chainlink/v2/core/chains/evm/db" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller" "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/llo-feeds/generated/verifier" "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/llo-feeds/generated/verifier_proxy" @@ -163,12 +164,13 @@ func SetupTH(t *testing.T, feedID common.Hash) TestHarness { require.NoError(t, err) b.Commit() - db := pgtest.NewSqlxDB(t) cfg := pgtest.NewQConfig(false) ethClient := evmclient.NewSimulatedBackendClient(t, b, big.NewInt(1337)) lggr := logger.TestLogger(t) ctx := testutils.Context(t) - lorm := logpoller.NewORM(big.NewInt(1337), db, lggr, cfg) + // explicitly scope the db rather than use evmtestdb.NewScopedDB because that requires a config, which causes an import cycle + evmScopedDB := &evmdb.ScopedDB{pgtest.NewSqlxDB(t, evmdb.UseEVMSchema())} + lorm := logpoller.NewORM(big.NewInt(1337), evmScopedDB, lggr, cfg) lp := logpoller.NewLogPoller(lorm, ethClient, lggr, 100*time.Millisecond, 1, 2, 2, 1000) eventBroadcaster := pgmocks.NewEventBroadcaster(t) subscription := pgmocks.NewSubscription(t) diff --git a/core/services/relay/evm/mercury/orm.go b/core/services/relay/evm/mercury/orm.go index dd7d7b33e74..4df5abfd163 100644 --- a/core/services/relay/evm/mercury/orm.go +++ b/core/services/relay/evm/mercury/orm.go @@ -54,6 +54,7 @@ func (o *orm) InsertTransmitRequest(req *pb.TransmitRequest, jobID int32, report wg.Add(2) var err1, err2 error + // KRR note to self: this is going to break because mercury is inside the evm relayer and the evm relayer is being scoped to the `evm` schema go func() { defer wg.Done() err1 = q.ExecQ(` diff --git a/core/services/relay/evm/relayer_extender_test.go b/core/services/relay/evm/relayer_extender_test.go index dc8433d4f0f..0a9fca52bcd 100644 --- a/core/services/relay/evm/relayer_extender_test.go +++ b/core/services/relay/evm/relayer_extender_test.go @@ -14,6 +14,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" evmrelay "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm" @@ -34,7 +35,7 @@ func TestChainRelayExtenders(t *testing.T) { kst := cltest.NewKeyStore(t, db, cfg.Database()) require.NoError(t, kst.Unlock(cltest.Password)) - opts := evmtest.NewChainRelayExtOpts(t, evmtest.TestChainOpts{DB: evmtest.NewScopedDB(t, cfg.Database()), KeyStore: kst.Eth(), GeneralConfig: cfg}) + opts := evmtest.NewChainRelayExtOpts(t, evmtest.TestChainOpts{DB: evmtestdb.NewScopedDB(t, cfg.Database()), KeyStore: kst.Eth(), GeneralConfig: cfg}) opts.GenEthClient = func(*big.Int) evmclient.Client { return cltest.NewEthMocksWithStartupAssertions(t) } diff --git a/core/services/vrf/delegate_test.go b/core/services/vrf/delegate_test.go index ae0accc329e..dc599fda272 100644 --- a/core/services/vrf/delegate_test.go +++ b/core/services/vrf/delegate_test.go @@ -23,6 +23,7 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" + evmtestdb "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest/db" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/chainlink" @@ -64,7 +65,7 @@ type vrfUniverse struct { cid big.Int } -func buildVrfUni(t *testing.T, db *sqlx.DB, cfg chainlink.GeneralConfig) vrfUniverse { +func buildVrfUni(t *testing.T, coredb *sqlx.DB, cfg chainlink.GeneralConfig) vrfUniverse { // Mock all chain interactions lb := log_mocks.NewBroadcaster(t) lb.On("AddDependents", 1).Maybe() @@ -74,13 +75,14 @@ func buildVrfUni(t *testing.T, db *sqlx.DB, cfg chainlink.GeneralConfig) vrfUniv hb := headtracker.NewHeadBroadcaster(lggr) // Don't mock db interactions - prm := pipeline.NewORM(db, lggr, cfg.Database(), cfg.JobPipeline().MaxSuccessfulRuns()) - btORM := bridges.NewORM(db, lggr, cfg.Database()) + prm := pipeline.NewORM(coredb, lggr, cfg.Database(), cfg.JobPipeline().MaxSuccessfulRuns()) + btORM := bridges.NewORM(coredb, lggr, cfg.Database()) txm := txmmocks.NewMockEvmTxManager(t) - ks := keystore.New(db, utils.FastScryptParams, lggr, cfg.Database()) - relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{LogBroadcaster: lb, KeyStore: ks.Eth(), Client: ec, DB: db, GeneralConfig: cfg, TxManager: txm}) + ks := keystore.New(coredb, utils.FastScryptParams, lggr, cfg.Database()) + evmdb := evmtestdb.NewScopedDB(t, cfg.Database()) + relayExtenders := evmtest.NewChainRelayExtenders(t, evmtest.TestChainOpts{LogBroadcaster: lb, KeyStore: ks.Eth(), Client: ec, DB: evmdb, GeneralConfig: cfg, TxManager: txm}) legacyChains := evmrelay.NewLegacyChainsFromRelayerExtenders(relayExtenders) - jrm := job.NewORM(db, legacyChains, prm, btORM, ks, lggr, cfg.Database()) + jrm := job.NewORM(coredb, legacyChains, prm, btORM, ks, lggr, cfg.Database()) t.Cleanup(func() { jrm.Close() }) pr := pipeline.NewRunner(prm, btORM, cfg.JobPipeline(), cfg.WebServer(), legacyChains, ks.Eth(), ks.VRF(), lggr, nil, nil) require.NoError(t, ks.Unlock(testutils.Password)) diff --git a/core/services/vrf/v1/integration_test.go b/core/services/vrf/v1/integration_test.go index 14cbb36c9d6..c97e6c52a2d 100644 --- a/core/services/vrf/v1/integration_test.go +++ b/core/services/vrf/v1/integration_test.go @@ -97,7 +97,7 @@ func TestIntegration_VRF_JPV2(t *testing.T) { // Ensure the eth transaction gets confirmed on chain. gomega.NewWithT(t).Eventually(func() bool { - orm := txmgr.NewTxStore(app.GetSqlxDB(), app.GetLogger(), app.GetConfig().Database()) + orm := txmgr.NewTxStore(app.GetRelayers().LegacyEVM().DB(), app.GetLogger(), app.GetConfig().Database()) uc, err2 := orm.CountUnconfirmedTransactions(key1.Address, testutils.SimulatedChainID) require.NoError(t, err2) return uc == 0 @@ -214,7 +214,7 @@ func TestIntegration_VRF_WithBHS(t *testing.T) { // Ensure the eth transaction gets confirmed on chain. gomega.NewWithT(t).Eventually(func() bool { - orm := txmgr.NewTxStore(app.GetSqlxDB(), app.GetLogger(), app.GetConfig().Database()) + orm := txmgr.NewTxStore(app.GetRelayers().LegacyEVM().DB(), app.GetLogger(), app.GetConfig().Database()) uc, err2 := orm.CountUnconfirmedTransactions(key.Address, testutils.SimulatedChainID) require.NoError(t, err2) return uc == 0 diff --git a/core/web/evm_forwarders_controller.go b/core/web/evm_forwarders_controller.go index 6228723506d..7c7b7c4a14d 100644 --- a/core/web/evm_forwarders_controller.go +++ b/core/web/evm_forwarders_controller.go @@ -25,7 +25,7 @@ type EVMForwardersController struct { // Index lists EVM forwarders. func (cc *EVMForwardersController) Index(c *gin.Context, size, page, offset int) { - orm := forwarders.NewORM(cc.App.GetSqlxDB(), cc.App.GetLogger(), cc.App.GetConfig().Database()) + orm := forwarders.NewORM(cc.App.GetRelayers().LegacyEVM().DB(), cc.App.GetLogger(), cc.App.GetConfig().Database()) fwds, count, err := orm.FindForwarders(0, size) if err != nil { @@ -55,7 +55,7 @@ func (cc *EVMForwardersController) Track(c *gin.Context) { jsonAPIError(c, http.StatusUnprocessableEntity, err) return } - orm := forwarders.NewORM(cc.App.GetSqlxDB(), cc.App.GetLogger(), cc.App.GetConfig().Database()) + orm := forwarders.NewORM(cc.App.GetRelayers().LegacyEVM().DB(), cc.App.GetLogger(), cc.App.GetConfig().Database()) fwd, err := orm.CreateForwarder(request.Address, *request.EVMChainID) if err != nil { @@ -94,7 +94,7 @@ func (cc *EVMForwardersController) Delete(c *gin.Context) { return chain.LogPoller().UnregisterFilter(forwarders.FilterName(addr), pg.WithQueryer(tx)) } - orm := forwarders.NewORM(cc.App.GetSqlxDB(), cc.App.GetLogger(), cc.App.GetConfig().Database()) + orm := forwarders.NewORM(cc.App.GetRelayers().LegacyEVM().DB(), cc.App.GetLogger(), cc.App.GetConfig().Database()) err = orm.DeleteForwarder(id, filterCleanup) if err != nil { diff --git a/core/web/evm_transactions_controller_test.go b/core/web/evm_transactions_controller_test.go index 951d9d99259..0d1a87cf6f7 100644 --- a/core/web/evm_transactions_controller_test.go +++ b/core/web/evm_transactions_controller_test.go @@ -25,7 +25,7 @@ func TestTransactionsController_Index_Success(t *testing.T) { require.NoError(t, app.Start(testutils.Context(t))) db := app.GetSqlxDB() - txStore := cltest.NewTestTxStore(t, app.GetSqlxDB(), app.GetConfig().Database()) + txStore := cltest.NewTestTxStore(t, app.GetRelayers().LegacyEVM().DB(), app.GetConfig().Database()) ethKeyStore := cltest.NewKeyStore(t, db, app.Config.Database()).Eth() client := app.NewHTTPClient(&cltest.User{}) _, from := cltest.MustInsertRandomKey(t, ethKeyStore, 0) @@ -81,7 +81,7 @@ func TestTransactionsController_Show_Success(t *testing.T) { app := cltest.NewApplicationWithKey(t) require.NoError(t, app.Start(testutils.Context(t))) - txStore := cltest.NewTestTxStore(t, app.GetSqlxDB(), app.GetConfig().Database()) + txStore := cltest.NewTestTxStore(t, app.GetRelayers().LegacyEVM().DB(), app.GetConfig().Database()) client := app.NewHTTPClient(&cltest.User{}) _, from := cltest.MustInsertRandomKey(t, app.KeyStore.Eth(), 0) @@ -114,7 +114,7 @@ func TestTransactionsController_Show_NotFound(t *testing.T) { app := cltest.NewApplicationWithKey(t) require.NoError(t, app.Start(testutils.Context(t))) - txStore := cltest.NewTestTxStore(t, app.GetSqlxDB(), app.GetConfig().Database()) + txStore := cltest.NewTestTxStore(t, app.GetRelayers().LegacyEVM().DB(), app.GetConfig().Database()) client := app.NewHTTPClient(&cltest.User{}) _, from := cltest.MustInsertRandomKey(t, app.KeyStore.Eth(), 0) tx := cltest.MustInsertUnconfirmedEthTxWithBroadcastLegacyAttempt(t, txStore, 1, from) diff --git a/core/web/evm_tx_attempts_controller_test.go b/core/web/evm_tx_attempts_controller_test.go index abf80add213..f918beefa8d 100644 --- a/core/web/evm_tx_attempts_controller_test.go +++ b/core/web/evm_tx_attempts_controller_test.go @@ -20,7 +20,7 @@ func TestTxAttemptsController_Index_Success(t *testing.T) { app := cltest.NewApplicationWithKey(t) require.NoError(t, app.Start(testutils.Context(t))) - txStore := cltest.NewTestTxStore(t, app.GetSqlxDB(), app.GetConfig().Database()) + txStore := cltest.NewTestTxStore(t, app.GetRelayers().LegacyEVM().DB(), app.GetConfig().Database()) client := app.NewHTTPClient(&cltest.User{}) _, from := cltest.MustInsertRandomKey(t, app.KeyStore.Eth(), 0)