Skip to content

Commit

Permalink
Refactor EVM ORMs (#11899)
Browse files Browse the repository at this point in the history
* Initial commit

* Refactor headtracker orm

* Remove unused loggers

* Remove comments

* Add timeout

* Refactor log_poller ORM

* Refactor logpoller ORM

* Fix logpoller tests

* Update logpoller orm

* Use EventSig

* update logpoller orm

* Update orm.go

* Update logpoller_wrapper_test.go

* Update log_poller_test.go

* Remove query

* Remove ORM timeouts

* Add context

* Use testutils for context

* Use testutils context

* Use testutils context

* Use ctx

* Refactor forwarder ORM

* Generate tidy

* Fix logpoller mocks

* Remove pg dependency

* Fix mock calls

* Fix mock calls

* Fix mock calls

* Use request context

* Update context

* Update contexts

* Fix mock call args

* Unexport orm

* Fix arg name

* update logpoller

* unexport orm

* Use query

* fix tests

* fix imports

* Use pkgerrors

* Use registry ctx

* Use context

* Use ctx

* Use ctx

* Update orm.go

* Use context

* Use context

* Use context

* Propagate context

* Propagate context

* Update listener_test.go

* Fix context

* Export DbORM struct

* Update orm.go

* Pass context

* Pass context

* Update orm.go

* Use testcontext

* Initialize context

* Update context

* Propagate context

* core/services/chainlink: start using sqlutil.DB instead of pg.Q (#12386)

* Check bind errors

* Add close timeout

* Add changeset

---------

Co-authored-by: Jordan Krage <jmank88@gmail.com>
  • Loading branch information
DylanTinianov and jmank88 authored Mar 14, 2024
1 parent 5546698 commit 67560b9
Show file tree
Hide file tree
Showing 140 changed files with 1,932 additions and 1,882 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-ears-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": patch
---

Refactor EVM ORMs to remove pg dependency
15 changes: 7 additions & 8 deletions core/chains/evm/forwarders/forwarder_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
"github.com/ethereum/go-ethereum/core/types"
pkgerrors "github.com/pkg/errors"

"github.com/jmoiron/sqlx"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink-common/pkg/sqlutil"
"github.com/smartcontractkit/chainlink-common/pkg/utils"

evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
Expand All @@ -23,7 +22,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/authorized_forwarder"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/authorized_receiver"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/offchain_aggregator_wrapper"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
)

var forwardABI = evmtypes.MustGetABI(authorized_forwarder.AuthorizedForwarderABI).Methods["forward"]
Expand Down Expand Up @@ -56,13 +54,13 @@ 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 sqlutil.DB, client evmclient.Client, logpoller evmlogpoller.LogPoller, l logger.Logger, cfg Config) *FwdMgr {
lggr := logger.Sugared(logger.Named(l, "EVMForwarderManager"))
fwdMgr := FwdMgr{
logger: lggr,
cfg: cfg,
evmClient: client,
ORM: NewORM(db, lggr, dbConfig),
ORM: NewORM(db),
logpoller: logpoller,
sendersCache: make(map[common.Address][]common.Address),
}
Expand All @@ -80,7 +78,7 @@ func (f *FwdMgr) Start(ctx context.Context) error {
f.logger.Debug("Initializing EVM forwarder manager")
chainId := f.evmClient.ConfiguredChainID()

fwdrs, err := f.ORM.FindForwardersByChain(big.Big(*chainId))
fwdrs, err := f.ORM.FindForwardersByChain(ctx, big.Big(*chainId))
if err != nil {
return pkgerrors.Wrapf(err, "Failed to retrieve forwarders for chain %d", chainId)
}
Expand Down Expand Up @@ -113,7 +111,7 @@ func FilterName(addr common.Address) string {

func (f *FwdMgr) ForwarderFor(addr common.Address) (forwarder common.Address, err error) {
// Gets forwarders for current chain.
fwdrs, err := f.ORM.FindForwardersByChain(big.Big(*f.evmClient.ConfiguredChainID()))
fwdrs, err := f.ORM.FindForwardersByChain(f.ctx, big.Big(*f.evmClient.ConfiguredChainID()))
if err != nil {
return common.Address{}, err
}
Expand Down Expand Up @@ -211,6 +209,7 @@ func (f *FwdMgr) subscribeSendersChangedLogs(addr common.Address) error {
}

err := f.logpoller.RegisterFilter(
f.ctx,
evmlogpoller.Filter{
Name: FilterName(addr),
EventSigs: []common.Hash{authChangedTopic},
Expand Down Expand Up @@ -251,11 +250,11 @@ func (f *FwdMgr) runLoop() {
}

logs, err := f.logpoller.LatestLogEventSigsAddrsWithConfs(
f.ctx,
f.latestBlock,
[]common.Hash{authChangedTopic},
addrs,
evmlogpoller.Confirmations(f.cfg.FinalityDepth()),
pg.WithParentCtx(f.ctx),
)
if err != nil {
f.logger.Errorw("Failed to retrieve latest log round", "err", err)
Expand Down
29 changes: 16 additions & 13 deletions core/chains/evm/forwarders/forwarder_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"
"time"

"github.com/smartcontractkit/chainlink-common/pkg/sqlutil"

"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
Expand All @@ -26,7 +28,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
)

var GetAuthorisedSendersABI = evmtypes.MustGetABI(authorized_receiver.AuthorizedReceiverABI).Methods["getAuthorizedSenders"]
Expand All @@ -39,6 +40,7 @@ func TestFwdMgr_MaybeForwardTransaction(t *testing.T) {
cfg := configtest.NewTestGeneralConfig(t)
evmcfg := evmtest.NewChainScopedConfig(t, cfg)
owner := testutils.MustNewSimTransactor(t)
ctx := testutils.Context(t)

ec := backends.NewSimulatedBackend(map[common.Address]core.GenesisAccount{
owner.From: {
Expand Down Expand Up @@ -68,13 +70,13 @@ func TestFwdMgr_MaybeForwardTransaction(t *testing.T) {
RpcBatchSize: 2,
KeepFinalizedBlocksDepth: 1000,
}
lp := logpoller.NewLogPoller(logpoller.NewORM(testutils.FixtureChainID, db, lggr, pgtest.NewQConfig(true)), evmClient, lggr, lpOpts)
fwdMgr := forwarders.NewFwdMgr(db, evmClient, lp, lggr, evmcfg.EVM(), evmcfg.Database())
fwdMgr.ORM = forwarders.NewORM(db, logger.Test(t), cfg.Database())
lp := logpoller.NewLogPoller(logpoller.NewORM(testutils.FixtureChainID, db, lggr), evmClient, lggr, lpOpts)
fwdMgr := forwarders.NewFwdMgr(db, evmClient, lp, lggr, evmcfg.EVM())
fwdMgr.ORM = forwarders.NewORM(db)

fwd, err := fwdMgr.ORM.CreateForwarder(forwarderAddr, ubig.Big(*testutils.FixtureChainID))
fwd, err := fwdMgr.ORM.CreateForwarder(ctx, forwarderAddr, ubig.Big(*testutils.FixtureChainID))
require.NoError(t, err)
lst, err := fwdMgr.ORM.FindForwardersByChain(ubig.Big(*testutils.FixtureChainID))
lst, err := fwdMgr.ORM.FindForwardersByChain(ctx, ubig.Big(*testutils.FixtureChainID))
require.NoError(t, err)
require.Equal(t, len(lst), 1)
require.Equal(t, lst[0].Address, forwarderAddr)
Expand All @@ -87,22 +89,23 @@ func TestFwdMgr_MaybeForwardTransaction(t *testing.T) {
require.NoError(t, err)

cleanupCalled := false
cleanup := func(tx pg.Queryer, evmChainId int64, addr common.Address) error {
cleanup := func(tx sqlutil.DB, evmChainId int64, addr common.Address) error {
require.Equal(t, testutils.FixtureChainID.Int64(), evmChainId)
require.Equal(t, forwarderAddr, addr)
require.NotNil(t, tx)
cleanupCalled = true
return nil
}

err = fwdMgr.ORM.DeleteForwarder(fwd.ID, cleanup)
err = fwdMgr.ORM.DeleteForwarder(ctx, fwd.ID, cleanup)
assert.NoError(t, err)
assert.True(t, cleanupCalled)
}

func TestFwdMgr_AccountUnauthorizedToForward_SkipsForwarding(t *testing.T) {
lggr := logger.Test(t)
db := pgtest.NewSqlxDB(t)
ctx := testutils.Context(t)
cfg := configtest.NewTestGeneralConfig(t)
evmcfg := evmtest.NewChainScopedConfig(t, cfg)
owner := testutils.MustNewSimTransactor(t)
Expand All @@ -128,13 +131,13 @@ func TestFwdMgr_AccountUnauthorizedToForward_SkipsForwarding(t *testing.T) {
RpcBatchSize: 2,
KeepFinalizedBlocksDepth: 1000,
}
lp := logpoller.NewLogPoller(logpoller.NewORM(testutils.FixtureChainID, db, lggr, pgtest.NewQConfig(true)), evmClient, lggr, lpOpts)
fwdMgr := forwarders.NewFwdMgr(db, evmClient, lp, lggr, evmcfg.EVM(), evmcfg.Database())
fwdMgr.ORM = forwarders.NewORM(db, logger.Test(t), cfg.Database())
lp := logpoller.NewLogPoller(logpoller.NewORM(testutils.FixtureChainID, db, lggr), evmClient, lggr, lpOpts)
fwdMgr := forwarders.NewFwdMgr(db, evmClient, lp, lggr, evmcfg.EVM())
fwdMgr.ORM = forwarders.NewORM(db)

_, err = fwdMgr.ORM.CreateForwarder(forwarderAddr, ubig.Big(*testutils.FixtureChainID))
_, err = fwdMgr.ORM.CreateForwarder(ctx, forwarderAddr, ubig.Big(*testutils.FixtureChainID))
require.NoError(t, err)
lst, err := fwdMgr.ORM.FindForwardersByChain(ubig.Big(*testutils.FixtureChainID))
lst, err := fwdMgr.ORM.FindForwardersByChain(ctx, ubig.Big(*testutils.FixtureChainID))
require.NoError(t, err)
require.Equal(t, len(lst), 1)
require.Equal(t, lst[0].Address, forwarderAddr)
Expand Down
90 changes: 46 additions & 44 deletions core/chains/evm/forwarders/mocks/orm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 67560b9

Please sign in to comment.