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

Add special transmitter for OCR2 feeds #13323

Merged
merged 2 commits into from
May 28, 2024
Merged
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
105 changes: 105 additions & 0 deletions core/services/ocrcommon/transmitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ocrcommon
import (
"context"
"math/big"
"slices"

"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
Expand Down Expand Up @@ -63,6 +64,51 @@ func NewTransmitter(
}, nil
}

type txManagerOCR2 interface {
CreateTransaction(ctx context.Context, txRequest txmgr.TxRequest) (tx txmgr.Tx, err error)
GetForwarderForEOAOCR2Feeds(ctx context.Context, eoa, ocr2AggregatorID common.Address) (forwarder common.Address, err error)
}

type ocr2FeedsTransmitter struct {
ocr2Aggregator common.Address
txManagerOCR2
transmitter
}

// NewOCR2FeedsTransmitter creates a new eth transmitter that handles OCR2 Feeds specific logic surrounding forwarders.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind explaining this "specific logic" in a comment here? I'm curious what is special about forwarders in OCR2 Feeds that we need a separate transmitter.

Copy link
Contributor Author

@ilija42 ilija42 May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, was about to add a description. This change should allow the ocr2 feeds job to always validate its sender before transmitting. This means checking if forwarders are authorized for EOA and if they are set as a transmitter on the OCR2 aggregator contract. Previously forwarders were chosen once during job start.

// ocr2FeedsTransmitter validates forwarders before every transmission, enabling smooth onchain config changes without job restarts.
func NewOCR2FeedsTransmitter(
txm txManagerOCR2,
fromAddresses []common.Address,
ocr2Aggregator common.Address,
gasLimit uint64,
effectiveTransmitterAddress common.Address,
strategy types.TxStrategy,
checker txmgr.TransmitCheckerSpec,
chainID *big.Int,
keystore roundRobinKeystore,
) (Transmitter, error) {
// Ensure that a keystore is provided.
if keystore == nil {
return nil, errors.New("nil keystore provided to transmitter")
}

return &ocr2FeedsTransmitter{
ocr2Aggregator: ocr2Aggregator,
txManagerOCR2: txm,
transmitter: transmitter{
txm: txm,
fromAddresses: fromAddresses,
gasLimit: gasLimit,
effectiveTransmitterAddress: effectiveTransmitterAddress,
strategy: strategy,
checker: checker,
chainID: chainID,
keystore: keystore,
},
}, nil
}

func (t *transmitter) CreateEthTransaction(ctx context.Context, toAddress common.Address, payload []byte, txMeta *txmgr.TxMeta) error {
roundRobinFromAddress, err := t.keystore.GetRoundRobinAddress(ctx, t.chainID, t.fromAddresses...)
if err != nil {
Expand Down Expand Up @@ -94,3 +140,62 @@ func (t *transmitter) forwarderAddress() common.Address {
}
return t.effectiveTransmitterAddress
}

func (t *ocr2FeedsTransmitter) CreateEthTransaction(ctx context.Context, toAddress common.Address, payload []byte, txMeta *txmgr.TxMeta) error {
roundRobinFromAddress, err := t.keystore.GetRoundRobinAddress(ctx, t.chainID, t.fromAddresses...)
if err != nil {
return errors.Wrap(err, "skipped OCR transmission, error getting round-robin address")
}

forwarderAddress, err := t.forwarderAddress(ctx, roundRobinFromAddress, toAddress)
if err != nil {
return err
}

_, err = t.txm.CreateTransaction(ctx, txmgr.TxRequest{
FromAddress: roundRobinFromAddress,
ToAddress: toAddress,
EncodedPayload: payload,
FeeLimit: t.gasLimit,
ForwarderAddress: forwarderAddress,
Strategy: t.strategy,
Checker: t.checker,
Meta: txMeta,
})

return errors.Wrap(err, "skipped OCR transmission")
}

// FromAddress for ocr2FeedsTransmitter returns valid forwarder or effectiveTransmitterAddress if forwarders are not set.
func (t *ocr2FeedsTransmitter) FromAddress() common.Address {
roundRobinFromAddress, err := t.keystore.GetRoundRobinAddress(context.Background(), t.chainID, t.fromAddresses...)
if err != nil {
return t.effectiveTransmitterAddress
}

forwarderAddress, err := t.forwarderAddress(context.Background(), roundRobinFromAddress, t.ocr2Aggregator)
Copy link
Contributor Author

@ilija42 ilija42 May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that context.Background() works here? Might require libocr changes to pass in actual context

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already incoming. I will update this as part of that work.

if err != nil || forwarderAddress == (common.Address{}) {
return t.effectiveTransmitterAddress
}

return forwarderAddress
}

func (t *ocr2FeedsTransmitter) forwarderAddress(ctx context.Context, eoa, ocr2Aggregator common.Address) (common.Address, error) {
// If effectiveTransmitterAddress is in fromAddresses, then forwarders aren't set.
if slices.Contains(t.fromAddresses, t.effectiveTransmitterAddress) {
return common.Address{}, nil
}

forwarderAddress, err := t.GetForwarderForEOAOCR2Feeds(ctx, eoa, ocr2Aggregator)
if err != nil {
return common.Address{}, err
}

// if forwarder address is in fromAddresses, then none of the forwarders are valid
if slices.Contains(t.fromAddresses, forwarderAddress) {
forwarderAddress = common.Address{}
}

return forwarderAddress, nil
}
37 changes: 27 additions & 10 deletions core/services/relay/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,17 +566,34 @@ func newOnChainContractTransmitter(ctx context.Context, lggr logger.Logger, rarg
gasLimit = uint64(*opts.pluginGasLimit)
}

transmitter, err := ocrcommon.NewTransmitter(
configWatcher.chain.TxManager(),
fromAddresses,
gasLimit,
effectiveTransmitterAddress,
strategy,
checker,
configWatcher.chain.ID(),
ethKeystore,
)
var transmitter Transmitter
var err error

switch commontypes.OCR2PluginType(rargs.ProviderType) {
case commontypes.Median:
transmitter, err = ocrcommon.NewOCR2FeedsTransmitter(
configWatcher.chain.TxManager(),
fromAddresses,
common.HexToAddress(rargs.ContractID),
gasLimit,
effectiveTransmitterAddress,
strategy,
checker,
configWatcher.chain.ID(),
ethKeystore,
)
default:
transmitter, err = ocrcommon.NewTransmitter(
configWatcher.chain.TxManager(),
fromAddresses,
gasLimit,
effectiveTransmitterAddress,
strategy,
checker,
configWatcher.chain.ID(),
ethKeystore,
)
}
if err != nil {
return nil, pkgerrors.Wrap(err, "failed to create transmitter")
}
Expand Down
Loading