Skip to content

Commit

Permalink
WIP: add evm-scoped db type to ensure that services are not violating…
Browse files Browse the repository at this point in the history
… db boundaries. hint: they are and it's painful
  • Loading branch information
krehermann committed Sep 27, 2023
1 parent 832e137 commit a86f233
Show file tree
Hide file tree
Showing 77 changed files with 600 additions and 459 deletions.
6 changes: 3 additions & 3 deletions core/chains/evm/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions core/chains/evm/db/db.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
5 changes: 2 additions & 3 deletions core/chains/evm/evm_txm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ 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"
"github.com/smartcontractkit/chainlink/v2/core/logger"
)

func newEvmTxm(
db *sqlx.DB,
db *evmdb.ScopedDB,
cfg evmconfig.EVM,
evmRPCEnabled bool,
databaseConfig txmgr.DatabaseConfig,
Expand Down
4 changes: 2 additions & 2 deletions core/chains/evm/forwarders/forwarder_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions core/chains/evm/forwarders/forwarder_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

Expand Down Expand Up @@ -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{
Expand Down
5 changes: 3 additions & 2 deletions core/chains/evm/forwarders/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down
17 changes: 10 additions & 7 deletions core/chains/evm/forwarders/orm_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package forwarders
package forwarders_test

import (
"database/sql"
Expand All @@ -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}
Expand Down
6 changes: 3 additions & 3 deletions core/chains/evm/headtracker/head_broadcaster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions core/chains/evm/headtracker/head_saver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit a86f233

Please sign in to comment.