Skip to content

Commit

Permalink
Add special transmitter for OCR2 feeds (#13323)
Browse files Browse the repository at this point in the history
* Add special transmitter for OCR2 feeds

* Add ocr2FeedsTransmitter FromAddress()

* Cherry-pick some forwarders context changes from (#13171)
  • Loading branch information
ilija42 committed May 28, 2024
1 parent 5e27b2f commit 8585cc9
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 83 deletions.
36 changes: 18 additions & 18 deletions common/txmgr/mocks/tx_manager.go

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

16 changes: 8 additions & 8 deletions common/txmgr/txmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ type TxManager[
services.Service
Trigger(addr ADDR)
CreateTransaction(ctx context.Context, txRequest txmgrtypes.TxRequest[ADDR, TX_HASH]) (etx txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE], err error)
GetForwarderForEOA(eoa ADDR) (forwarder ADDR, err error)
GetForwarderForEOAOCR2Feeds(eoa, ocr2AggregatorID ADDR) (forwarder ADDR, err error)
GetForwarderForEOA(ctx context.Context, eoa ADDR) (forwarder ADDR, err error)
GetForwarderForEOAOCR2Feeds(ctx context.Context, eoa, ocr2AggregatorID ADDR) (forwarder ADDR, err error)
RegisterResumeCallback(fn ResumeCallback)
SendNativeToken(ctx context.Context, chainID CHAIN_ID, from, to ADDR, value big.Int, gasLimit uint64) (etx txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE], err error)
Reset(addr ADDR, abandon bool) error
Expand Down Expand Up @@ -546,20 +546,20 @@ func (b *Txm[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) CreateTran
}

// Calls forwarderMgr to get a proper forwarder for a given EOA.
func (b *Txm[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) GetForwarderForEOA(eoa ADDR) (forwarder ADDR, err error) {
func (b *Txm[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) GetForwarderForEOA(ctx context.Context, eoa ADDR) (forwarder ADDR, err error) {
if !b.txConfig.ForwardersEnabled() {
return forwarder, fmt.Errorf("forwarding is not enabled, to enable set Transactions.ForwardersEnabled =true")
}
forwarder, err = b.fwdMgr.ForwarderFor(eoa)
forwarder, err = b.fwdMgr.ForwarderFor(ctx, eoa)
return
}

// GetForwarderForEOAOCR2Feeds calls forwarderMgr to get a proper forwarder for a given EOA and checks if its set as a transmitter on the OCR2Aggregator contract.
func (b *Txm[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) GetForwarderForEOAOCR2Feeds(eoa, ocr2Aggregator ADDR) (forwarder ADDR, err error) {
func (b *Txm[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) GetForwarderForEOAOCR2Feeds(ctx context.Context, eoa, ocr2Aggregator ADDR) (forwarder ADDR, err error) {
if !b.txConfig.ForwardersEnabled() {
return forwarder, fmt.Errorf("forwarding is not enabled, to enable set Transactions.ForwardersEnabled =true")
}
forwarder, err = b.fwdMgr.ForwarderForOCR2Feeds(eoa, ocr2Aggregator)
forwarder, err = b.fwdMgr.ForwarderForOCR2Feeds(ctx, eoa, ocr2Aggregator)
return
}

Expand Down Expand Up @@ -656,10 +656,10 @@ func (n *NullTxManager[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) Tri
func (n *NullTxManager[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) CreateTransaction(ctx context.Context, txRequest txmgrtypes.TxRequest[ADDR, TX_HASH]) (etx txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE], err error) {
return etx, errors.New(n.ErrMsg)
}
func (n *NullTxManager[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) GetForwarderForEOA(addr ADDR) (fwdr ADDR, err error) {
func (n *NullTxManager[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) GetForwarderForEOA(ctx context.Context, addr ADDR) (fwdr ADDR, err error) {
return fwdr, err
}
func (n *NullTxManager[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) GetForwarderForEOAOCR2Feeds(_, _ ADDR) (fwdr ADDR, err error) {
func (n *NullTxManager[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) GetForwarderForEOAOCR2Feeds(ctx context.Context, _, _ ADDR) (fwdr ADDR, err error) {
return fwdr, err
}

Expand Down
7 changes: 5 additions & 2 deletions common/txmgr/types/forwarder_manager.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package types

import (
"context"

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

"github.com/smartcontractkit/chainlink/v2/common/types"
)

//go:generate mockery --quiet --name ForwarderManager --output ./mocks/ --case=underscore
type ForwarderManager[ADDR types.Hashable] interface {
services.Service
ForwarderFor(addr ADDR) (forwarder ADDR, err error)
ForwarderForOCR2Feeds(eoa, ocr2Aggregator ADDR) (forwarder ADDR, err error)
ForwarderFor(ctx context.Context, addr ADDR) (forwarder ADDR, err error)
ForwarderForOCR2Feeds(ctx context.Context, eoa, ocr2Aggregator ADDR) (forwarder ADDR, err error)
// Converts payload to be forwarder-friendly
ConvertPayload(dest ADDR, origPayload []byte) ([]byte, error)
}
36 changes: 18 additions & 18 deletions common/txmgr/types/mocks/forwarder_manager.go

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

10 changes: 5 additions & 5 deletions core/chains/evm/forwarders/forwarder_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ func FilterName(addr common.Address) string {
return evmlogpoller.FilterName("ForwarderManager AuthorizedSendersChanged", addr.String())
}

func (f *FwdMgr) ForwarderFor(addr common.Address) (forwarder common.Address, err error) {
func (f *FwdMgr) ForwarderFor(ctx context.Context, addr common.Address) (forwarder common.Address, err error) {
// Gets forwarders for current chain.
fwdrs, err := f.ORM.FindForwardersByChain(f.ctx, big.Big(*f.evmClient.ConfiguredChainID()))
fwdrs, err := f.ORM.FindForwardersByChain(ctx, big.Big(*f.evmClient.ConfiguredChainID()))
if err != nil {
return common.Address{}, err
}
Expand All @@ -133,8 +133,8 @@ func (f *FwdMgr) ForwarderFor(addr common.Address) (forwarder common.Address, er
return common.Address{}, pkgerrors.Errorf("Cannot find forwarder for given EOA")
}

func (f *FwdMgr) ForwarderForOCR2Feeds(eoa, ocr2Aggregator common.Address) (forwarder common.Address, err error) {
fwdrs, err := f.ORM.FindForwardersByChain(f.ctx, big.Big(*f.evmClient.ConfiguredChainID()))
func (f *FwdMgr) ForwarderForOCR2Feeds(ctx context.Context, eoa, ocr2Aggregator common.Address) (forwarder common.Address, err error) {
fwdrs, err := f.ORM.FindForwardersByChain(ctx, big.Big(*f.evmClient.ConfiguredChainID()))
if err != nil {
return common.Address{}, err
}
Expand All @@ -144,7 +144,7 @@ func (f *FwdMgr) ForwarderForOCR2Feeds(eoa, ocr2Aggregator common.Address) (forw
return common.Address{}, err
}

transmitters, err := offchainAggregator.GetTransmitters(&bind.CallOpts{Context: f.ctx})
transmitters, err := offchainAggregator.GetTransmitters(&bind.CallOpts{Context: ctx})
if err != nil {
return common.Address{}, pkgerrors.Errorf("failed to get ocr2 aggregator transmitters: %s", err.Error())
}
Expand Down
11 changes: 6 additions & 5 deletions core/chains/evm/forwarders/forwarder_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/sqlutil"
"github.com/smartcontractkit/chainlink-common/pkg/utils"

"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/testhelpers"

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
Expand Down Expand Up @@ -86,7 +87,7 @@ func TestFwdMgr_MaybeForwardTransaction(t *testing.T) {
require.Equal(t, lst[0].Address, forwarderAddr)

require.NoError(t, fwdMgr.Start(testutils.Context(t)))
addr, err := fwdMgr.ForwarderFor(owner.From)
addr, err := fwdMgr.ForwarderFor(ctx, owner.From)
require.NoError(t, err)
require.Equal(t, addr.String(), forwarderAddr.String())
err = fwdMgr.Close()
Expand Down Expand Up @@ -148,7 +149,7 @@ func TestFwdMgr_AccountUnauthorizedToForward_SkipsForwarding(t *testing.T) {

err = fwdMgr.Start(testutils.Context(t))
require.NoError(t, err)
addr, err := fwdMgr.ForwarderFor(owner.From)
addr, err := fwdMgr.ForwarderFor(ctx, owner.From)
require.ErrorContains(t, err, "Cannot find forwarder for given EOA")
require.True(t, utils.IsZero(addr))
err = fwdMgr.Close()
Expand Down Expand Up @@ -214,7 +215,7 @@ func TestFwdMgr_InvalidForwarderForOCR2FeedsStates(t *testing.T) {
fwdMgr = forwarders.NewFwdMgr(db, evmClient, lp, lggr, evmcfg.EVM())
require.NoError(t, fwdMgr.Start(testutils.Context(t)))
// cannot find forwarder because it isn't authorized nor added as a transmitter
addr, err := fwdMgr.ForwarderForOCR2Feeds(owner.From, ocr2Address)
addr, err := fwdMgr.ForwarderForOCR2Feeds(ctx, owner.From, ocr2Address)
require.ErrorContains(t, err, "Cannot find forwarder for given EOA")
require.True(t, utils.IsZero(addr))

Expand All @@ -227,7 +228,7 @@ func TestFwdMgr_InvalidForwarderForOCR2FeedsStates(t *testing.T) {
require.Equal(t, owner.From, authorizedSenders[0])

// cannot find forwarder because it isn't added as a transmitter
addr, err = fwdMgr.ForwarderForOCR2Feeds(owner.From, ocr2Address)
addr, err = fwdMgr.ForwarderForOCR2Feeds(ctx, owner.From, ocr2Address)
require.ErrorContains(t, err, "Cannot find forwarder for given EOA")
require.True(t, utils.IsZero(addr))

Expand All @@ -251,7 +252,7 @@ func TestFwdMgr_InvalidForwarderForOCR2FeedsStates(t *testing.T) {
// create new fwd to have an empty cache that has to fetch authorized forwarders from log poller
fwdMgr = forwarders.NewFwdMgr(db, evmClient, lp, lggr, evmcfg.EVM())
require.NoError(t, fwdMgr.Start(testutils.Context(t)))
addr, err = fwdMgr.ForwarderForOCR2Feeds(owner.From, ocr2Address)
addr, err = fwdMgr.ForwarderForOCR2Feeds(ctx, owner.From, ocr2Address)
require.NoError(t, err, "forwarder should be valid and found because it is both authorized and set as a transmitter")
require.Equal(t, forwarderAddr, addr)
require.NoError(t, fwdMgr.Close())
Expand Down
2 changes: 1 addition & 1 deletion core/services/keeper/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (d *Delegate) ServicesForSpec(ctx context.Context, spec job.Job) (services
// In the case of forwarding, the keeper address is the forwarder contract deployed onchain between EOA and Registry.
effectiveKeeperAddress := spec.KeeperSpec.FromAddress.Address()
if spec.ForwardingAllowed {
fwdrAddress, fwderr := chain.TxManager().GetForwarderForEOA(spec.KeeperSpec.FromAddress.Address())
fwdrAddress, fwderr := chain.TxManager().GetForwarderForEOA(ctx, spec.KeeperSpec.FromAddress.Address())
if fwderr == nil {
effectiveKeeperAddress = fwdrAddress
} else {
Expand Down
2 changes: 1 addition & 1 deletion core/services/keeper/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func TestKeeperForwarderEthIntegration(t *testing.T) {
_, err = forwarderORM.CreateForwarder(ctx, fwdrAddress, chainID)
require.NoError(t, err)

addr, err := app.GetRelayers().LegacyEVMChains().Slice()[0].TxManager().GetForwarderForEOA(nodeAddress)
addr, err := app.GetRelayers().LegacyEVMChains().Slice()[0].TxManager().GetForwarderForEOA(ctx, nodeAddress)
require.NoError(t, err)
require.Equal(t, addr, fwdrAddress)

Expand Down
2 changes: 1 addition & 1 deletion core/services/ocr/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (d *Delegate) ServicesForSpec(ctx context.Context, jb job.Job) (services []
// In the case of forwarding, the transmitter address is the forwarder contract deployed onchain between EOA and OCR contract.
effectiveTransmitterAddress := concreteSpec.TransmitterAddress.Address()
if jb.ForwardingAllowed {
fwdrAddress, fwderr := chain.TxManager().GetForwarderForEOA(effectiveTransmitterAddress)
fwdrAddress, fwderr := chain.TxManager().GetForwarderForEOA(ctx, effectiveTransmitterAddress)
if fwderr == nil {
effectiveTransmitterAddress = fwdrAddress
} else {
Expand Down
8 changes: 4 additions & 4 deletions core/services/ocr2/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func (d *Delegate) ServicesForSpec(ctx context.Context, jb job.Job) ([]job.Servi
if err2 != nil {
return nil, fmt.Errorf("ServicesForSpec: could not get EVM chain %s: %w", rid.ChainID, err2)
}
effectiveTransmitterID, err2 = GetEVMEffectiveTransmitterID(&jb, chain, lggr)
effectiveTransmitterID, err2 = GetEVMEffectiveTransmitterID(ctx, &jb, chain, lggr)
if err2 != nil {
return nil, fmt.Errorf("ServicesForSpec failed to get evm transmitterID: %w", err2)
}
Expand Down Expand Up @@ -469,7 +469,7 @@ func (d *Delegate) ServicesForSpec(ctx context.Context, jb job.Job) ([]job.Servi
}
}

func GetEVMEffectiveTransmitterID(jb *job.Job, chain legacyevm.Chain, lggr logger.SugaredLogger) (string, error) {
func GetEVMEffectiveTransmitterID(ctx context.Context, jb *job.Job, chain legacyevm.Chain, lggr logger.SugaredLogger) (string, error) {
spec := jb.OCR2OracleSpec
if spec.PluginType == types.Mercury || spec.PluginType == types.LLO {
return spec.TransmitterID.String, nil
Expand Down Expand Up @@ -500,9 +500,9 @@ func GetEVMEffectiveTransmitterID(jb *job.Job, chain legacyevm.Chain, lggr logge
var effectiveTransmitterID common.Address
// Median forwarders need special handling because of OCR2Aggregator transmitters whitelist.
if spec.PluginType == types.Median {
effectiveTransmitterID, err = chain.TxManager().GetForwarderForEOAOCR2Feeds(common.HexToAddress(spec.TransmitterID.String), common.HexToAddress(spec.ContractID))
effectiveTransmitterID, err = chain.TxManager().GetForwarderForEOAOCR2Feeds(ctx, common.HexToAddress(spec.TransmitterID.String), common.HexToAddress(spec.ContractID))
} else {
effectiveTransmitterID, err = chain.TxManager().GetForwarderForEOA(common.HexToAddress(spec.TransmitterID.String))
effectiveTransmitterID, err = chain.TxManager().GetForwarderForEOA(ctx, common.HexToAddress(spec.TransmitterID.String))
}

if err == nil {
Expand Down
Loading

0 comments on commit 8585cc9

Please sign in to comment.