From 4eb9a0811b19c7551cd4d618d6ddee26deee7916 Mon Sep 17 00:00:00 2001 From: Agustina Aldasoro Date: Mon, 1 Jul 2024 17:48:03 -0300 Subject: [PATCH] Use only one constructor --- .../relay/evm/contract_transmitter_test.go | 35 +++++++------ core/services/relay/evm/evm.go | 49 ++++++------------- core/services/relay/evm/ocr2keeper.go | 2 +- core/services/relay/evm/ocr2vrf.go | 4 +- 4 files changed, 36 insertions(+), 54 deletions(-) diff --git a/core/services/relay/evm/contract_transmitter_test.go b/core/services/relay/evm/contract_transmitter_test.go index 3f5240a2ab..182cda63ce 100644 --- a/core/services/relay/evm/contract_transmitter_test.go +++ b/core/services/relay/evm/contract_transmitter_test.go @@ -98,7 +98,7 @@ func Test_contractTransmitterNoSignatures_Transmit_SignaturesAreNotTransmitted(t report := types.Report{} var signatures = oneSignature() - oc := createContractTransmitter(t, transmitter, true) + oc := createContractTransmitter(ctx, t, transmitter, WithExcludeSignatures()) err := oc.Transmit(ctx, reportCtx, report, signatures) require.NoError(t, err) @@ -121,7 +121,7 @@ func Test_contractTransmitter_Transmit_SignaturesAreTransmitted(t *testing.T) { report := types.Report{} var signatures = oneSignature() - oc := createContractTransmitter(t, transmitter, false) + oc := createContractTransmitter(ctx, t, transmitter) err := oc.Transmit(ctx, reportCtx, report, signatures) require.NoError(t, err) @@ -151,18 +151,21 @@ func oneSignature() []ocrtypes.AttributedOnchainSignature { return []libocr.AttributedOnchainSignature{{Signature: signaturesData, Signer: commontypes.OracleID(54)}} } -func createContractTransmitter(t *testing.T, transmitter Transmitter, excludeSignatures bool) contractTransmitter { - contractABI, _ := abi.JSON(strings.NewReader(ocr2aggregator.OCR2AggregatorMetaData.ABI)) - fields := contractTransmitter{ - contractAddress: gethcommon.Address{}, - contractABI: contractABI, - transmitter: transmitter, - transmittedEventSig: gethcommon.Hash{}, - lp: lpmocks.NewLogPoller(t), - contractReader: evmclimocks.NewClient(t), - lggr: logger.TestLogger(t), - reportToEvmTxMeta: reportToEvmTxMetaNoop, - excludeSigs: excludeSignatures, - } - return fields +func createContractTransmitter(ctx context.Context, t *testing.T, transmitter Transmitter, ops ...OCRTransmitterOption) *contractTransmitter { + contractABI, err := abi.JSON(strings.NewReader(ocr2aggregator.OCR2AggregatorMetaData.ABI)) + require.NoError(t, err) + lp := lpmocks.NewLogPoller(t) + lp.On("RegisterFilter", mock.Anything, mock.Anything).Return(nil) + contractTransmitter, err := NewOCRContractTransmitter( + ctx, + gethcommon.Address{}, + evmclimocks.NewClient(t), + contractABI, + transmitter, + lp, + logger.TestLogger(t), + ops..., + ) + require.NoError(t, err) + return contractTransmitter } diff --git a/core/services/relay/evm/evm.go b/core/services/relay/evm/evm.go index 2fdfc2eae8..533cb69097 100644 --- a/core/services/relay/evm/evm.go +++ b/core/services/relay/evm/evm.go @@ -10,12 +10,6 @@ import ( "math/big" "strings" "sync" - "time" - - "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip" - "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/ccipcommit" - "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/ccipexec" - ccipconfig "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/config" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" @@ -23,6 +17,11 @@ import ( pkgerrors "github.com/pkg/errors" "golang.org/x/exp/maps" + "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip" + "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/ccipcommit" + "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/ccipexec" + ccipconfig "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/config" + "github.com/smartcontractkit/libocr/gethwrappers2/ocr2aggregator" "github.com/smartcontractkit/libocr/offchainreporting2/reportingplugin/median" "github.com/smartcontractkit/libocr/offchainreporting2/reportingplugin/median/evmreportcodec" @@ -275,7 +274,7 @@ func (r *Relayer) NewPluginProvider(rargs commontypes.RelayArgs, pargs commontyp return nil, err } - transmitter, err := newOnChainContractTransmitter(ctx, r.lggr, rargs, r.ks.Eth(), configWatcher, configTransmitterOpts{}, OCR2AggregatorTransmissionContractABI, nil, 0) + transmitter, err := newOnChainContractTransmitter(ctx, r.lggr, rargs, r.ks.Eth(), configWatcher, configTransmitterOpts{}, OCR2AggregatorTransmissionContractABI) if err != nil { return nil, err } @@ -432,7 +431,7 @@ func (r *Relayer) NewCCIPCommitProvider(rargs commontypes.RelayArgs, pargs commo subjectID := chainToUUID(configWatcher.chain.ID()) contractTransmitter, err := newOnChainContractTransmitter(ctx, r.lggr, rargs, r.ks.Eth(), configWatcher, configTransmitterOpts{ subjectID: &subjectID, - }, OCR2AggregatorTransmissionContractABI, fn, 0) + }, OCR2AggregatorTransmissionContractABI, WithReportToEthMetadata(fn)) if err != nil { return nil, err } @@ -501,9 +500,11 @@ func (r *Relayer) NewCCIPExecProvider(rargs commontypes.RelayArgs, pargs commont return nil, err } subjectID := chainToUUID(configWatcher.chain.ID()) - contractTransmitter, err := newOnChainContractTransmitterExcludeSignatures(ctx, r.lggr, rargs, r.ks.Eth(), configWatcher, configTransmitterOpts{ + contractTransmitter, err := newOnChainContractTransmitter(ctx, r.lggr, rargs, r.ks.Eth(), configWatcher, configTransmitterOpts{ subjectID: &subjectID, - }, OCR2AggregatorTransmissionContractABI, fn, 0) + }, OCR2AggregatorTransmissionContractABI, + WithReportToEthMetadata(fn), + WithExcludeSignatures()) if err != nil { return nil, err } @@ -739,27 +740,7 @@ type configTransmitterOpts struct { } // newOnChainContractTransmitter creates a new contract transmitter. -func newOnChainContractTransmitter(ctx context.Context, lggr logger.Logger, rargs commontypes.RelayArgs, ethKeystore keystore.Eth, configWatcher *configWatcher, opts configTransmitterOpts, transmissionContractABI abi.ABI, reportToEvmTxMeta ReportToEthMetadata, transmissionContractRetention time.Duration) (*contractTransmitter, error) { - transmitter, err := generateTransmitterFrom(ctx, rargs, ethKeystore, configWatcher, opts) - if err != nil { - return nil, err - } - - return NewOCRContractTransmitter( - ctx, - configWatcher.contractAddress, - configWatcher.chain.Client(), - transmissionContractABI, - transmitter, - configWatcher.chain.LogPoller(), - lggr, - WithReportToEthMetadata(reportToEvmTxMeta), - WithRetention(transmissionContractRetention), - ) -} - -// newOnChainContractTransmitterExcludeSignatures creates a new contract transmitter that avoids sending the signatures as they are validated offchain. -func newOnChainContractTransmitterExcludeSignatures(ctx context.Context, lggr logger.Logger, rargs commontypes.RelayArgs, ethKeystore keystore.Eth, configWatcher *configWatcher, opts configTransmitterOpts, transmissionContractABI abi.ABI, reportToEvmTxMeta ReportToEthMetadata, transmissionContractRetention time.Duration) (*contractTransmitter, error) { +func newOnChainContractTransmitter(ctx context.Context, lggr logger.Logger, rargs commontypes.RelayArgs, ethKeystore keystore.Eth, configWatcher *configWatcher, opts configTransmitterOpts, transmissionContractABI abi.ABI, ocrTransmitterOpts ...OCRTransmitterOption) (*contractTransmitter, error) { transmitter, err := generateTransmitterFrom(ctx, rargs, ethKeystore, configWatcher, opts) if err != nil { return nil, err @@ -773,9 +754,7 @@ func newOnChainContractTransmitterExcludeSignatures(ctx context.Context, lggr lo transmitter, configWatcher.chain.LogPoller(), lggr, - WithReportToEthMetadata(reportToEvmTxMeta), - WithRetention(transmissionContractRetention), - WithExcludeSignatures(), + ocrTransmitterOpts..., ) } @@ -898,7 +877,7 @@ func (r *Relayer) NewMedianProvider(rargs commontypes.RelayArgs, pargs commontyp reportCodec := evmreportcodec.ReportCodec{} - contractTransmitter, err := newOnChainContractTransmitter(ctx, lggr, rargs, r.ks.Eth(), configWatcher, configTransmitterOpts{}, OCR2AggregatorTransmissionContractABI, nil, 0) + contractTransmitter, err := newOnChainContractTransmitter(ctx, lggr, rargs, r.ks.Eth(), configWatcher, configTransmitterOpts{}, OCR2AggregatorTransmissionContractABI) if err != nil { return nil, err } diff --git a/core/services/relay/evm/ocr2keeper.go b/core/services/relay/evm/ocr2keeper.go index 359c246075..b2d19c1170 100644 --- a/core/services/relay/evm/ocr2keeper.go +++ b/core/services/relay/evm/ocr2keeper.go @@ -90,7 +90,7 @@ func (r *ocr2keeperRelayer) NewOCR2KeeperProvider(rargs commontypes.RelayArgs, p } gasLimit := cfgWatcher.chain.Config().EVM().OCR2().Automation().GasLimit() - contractTransmitter, err := newOnChainContractTransmitter(ctx, r.lggr, rargs, r.ethKeystore, cfgWatcher, configTransmitterOpts{pluginGasLimit: &gasLimit}, OCR2AggregatorTransmissionContractABI, nil, 0) + contractTransmitter, err := newOnChainContractTransmitter(ctx, r.lggr, rargs, r.ethKeystore, cfgWatcher, configTransmitterOpts{pluginGasLimit: &gasLimit}, OCR2AggregatorTransmissionContractABI) if err != nil { return nil, err } diff --git a/core/services/relay/evm/ocr2vrf.go b/core/services/relay/evm/ocr2vrf.go index 2f9755e1c7..900449b76b 100644 --- a/core/services/relay/evm/ocr2vrf.go +++ b/core/services/relay/evm/ocr2vrf.go @@ -63,7 +63,7 @@ func (r *ocr2vrfRelayer) NewDKGProvider(rargs commontypes.RelayArgs, pargs commo if err != nil { return nil, err } - contractTransmitter, err := newOnChainContractTransmitter(ctx, r.lggr, rargs, r.ethKeystore, configWatcher, configTransmitterOpts{}, OCR2AggregatorTransmissionContractABI, nil, 0) + contractTransmitter, err := newOnChainContractTransmitter(ctx, r.lggr, rargs, r.ethKeystore, configWatcher, configTransmitterOpts{}, OCR2AggregatorTransmissionContractABI) if err != nil { return nil, err } @@ -89,7 +89,7 @@ func (r *ocr2vrfRelayer) NewOCR2VRFProvider(rargs commontypes.RelayArgs, pargs c if err != nil { return nil, err } - contractTransmitter, err := newOnChainContractTransmitter(ctx, r.lggr, rargs, r.ethKeystore, configWatcher, configTransmitterOpts{}, OCR2AggregatorTransmissionContractABI, nil, 0) + contractTransmitter, err := newOnChainContractTransmitter(ctx, r.lggr, rargs, r.ethKeystore, configWatcher, configTransmitterOpts{}, OCR2AggregatorTransmissionContractABI) if err != nil { return nil, err }