Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor forwarder ORM #12130

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions core/chains/evm/forwarders/forwarder_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"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"

Check failure on line 26 in core/chains/evm/forwarders/forwarder_manager.go

View workflow job for this annotation

GitHub Actions / Core Tests (go_core_fuzz)

"github.com/smartcontractkit/chainlink/v2/core/services/pg" imported and not used

Check failure on line 26 in core/chains/evm/forwarders/forwarder_manager.go

View workflow job for this annotation

GitHub Actions / Core Tests (go_core_race_tests)

"github.com/smartcontractkit/chainlink/v2/core/services/pg" imported and not used

Check failure on line 26 in core/chains/evm/forwarders/forwarder_manager.go

View workflow job for this annotation

GitHub Actions / lint

"github.com/smartcontractkit/chainlink/v2/core/services/pg" imported and not used

Check failure on line 26 in core/chains/evm/forwarders/forwarder_manager.go

View workflow job for this annotation

GitHub Actions / Core Tests (go_core_tests)

"github.com/smartcontractkit/chainlink/v2/core/services/pg" imported and not used

Check failure on line 26 in core/chains/evm/forwarders/forwarder_manager.go

View workflow job for this annotation

GitHub Actions / Flakey Test Detection

"github.com/smartcontractkit/chainlink/v2/core/services/pg" imported and not used

Check failure on line 26 in core/chains/evm/forwarders/forwarder_manager.go

View workflow job for this annotation

GitHub Actions / Core Tests (go_core_race_tests)

"github.com/smartcontractkit/chainlink/v2/core/services/pg" imported and not used

Check failure on line 26 in core/chains/evm/forwarders/forwarder_manager.go

View workflow job for this annotation

GitHub Actions / lint

"github.com/smartcontractkit/chainlink/v2/core/services/pg" imported and not used

Check failure on line 26 in core/chains/evm/forwarders/forwarder_manager.go

View workflow job for this annotation

GitHub Actions / Core Tests (go_core_fuzz)

"github.com/smartcontractkit/chainlink/v2/core/services/pg" imported and not used

Check failure on line 26 in core/chains/evm/forwarders/forwarder_manager.go

View workflow job for this annotation

GitHub Actions / Core Tests (go_core_tests)

"github.com/smartcontractkit/chainlink/v2/core/services/pg" imported and not used

Check failure on line 26 in core/chains/evm/forwarders/forwarder_manager.go

View workflow job for this annotation

GitHub Actions / Flakey Test Detection

"github.com/smartcontractkit/chainlink/v2/core/services/pg" imported and not used
)

var forwardABI = evmtypes.MustGetABI(authorized_forwarder.AuthorizedForwarderABI).Methods["forward"]
Expand Down Expand Up @@ -56,13 +56,13 @@
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 *sqlx.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 +80,7 @@
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 errors.Wrapf(err, "Failed to retrieve forwarders for chain %d", chainId)
}
Expand Down Expand Up @@ -113,7 +113,9 @@

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()))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
fwdrs, err := f.ORM.FindForwardersByChain(ctx, big.Big(*f.evmClient.ConfiguredChainID()))
if err != nil {
return common.Address{}, err
}
Expand Down
25 changes: 14 additions & 11 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 All @@ -61,12 +63,12 @@ func TestFwdMgr_MaybeForwardTransaction(t *testing.T) {

evmClient := client.NewSimulatedBackendClient(t, ec, testutils.FixtureChainID)
lp := logpoller.NewLogPoller(logpoller.NewORM(testutils.FixtureChainID, db, lggr), evmClient, lggr, 100*time.Millisecond, false, 2, 3, 2, 1000)
fwdMgr := forwarders.NewFwdMgr(db, evmClient, lp, lggr, evmcfg.EVM(), evmcfg.Database())
fwdMgr.ORM = forwarders.NewORM(db, logger.Test(t), cfg.Database())
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 @@ -79,22 +81,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.Queryer, 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 @@ -114,12 +117,12 @@ func TestFwdMgr_AccountUnauthorizedToForward_SkipsForwarding(t *testing.T) {

evmClient := client.NewSimulatedBackendClient(t, ec, testutils.FixtureChainID)
lp := logpoller.NewLogPoller(logpoller.NewORM(testutils.FixtureChainID, db, lggr), evmClient, lggr, 100*time.Millisecond, false, 2, 3, 2, 1000)
fwdMgr := forwarders.NewFwdMgr(db, evmClient, lp, lggr, evmcfg.EVM(), evmcfg.Database())
fwdMgr.ORM = forwarders.NewORM(db, logger.Test(t), cfg.Database())
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
Loading