Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/ccip-develop' into ng/merge-chai…
Browse files Browse the repository at this point in the history
…nlink-new
  • Loading branch information
0xnogo committed Jul 22, 2024
2 parents 54ebf32 + 054de17 commit c30ef14
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 366 deletions.
42 changes: 38 additions & 4 deletions core/services/ocr2/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ func (d *Delegate) cleanupEVM(ctx context.Context, jb job.Job, relayID types.Rel
// an inconsistent state. This assumes UnregisterFilter will return nil if the filter wasn't found
// at all (no rows deleted).
spec := jb.OCR2OracleSpec
transmitterID := spec.TransmitterID.String
chain, err := d.legacyChains.Get(relayID.ChainID)
if err != nil {
d.lggr.Errorw("cleanupEVM: failed to get chain id", "chainId", relayID.ChainID, "err", err)
Expand All @@ -320,15 +321,48 @@ func (d *Delegate) cleanupEVM(ctx context.Context, jb job.Job, relayID types.Rel
}
filters = append(filters, filters21...)
case types.CCIPCommit:
err = ccipcommit.UnregisterCommitPluginLpFilters(context.Background(), d.lggr, jb, d.legacyChains)
// Write PluginConfig bytes to send source/dest relayer provider + info outside of top level rargs/pargs over the wire
var pluginJobSpecConfig ccipconfig.CommitPluginJobSpecConfig
err = json.Unmarshal(spec.PluginConfig.Bytes(), &pluginJobSpecConfig)
if err != nil {
return err
}

dstProvider, err2 := d.ccipCommitGetDstProvider(ctx, jb, pluginJobSpecConfig, transmitterID)
if err2 != nil {
return err2
}

srcProvider, _, err2 := d.ccipCommitGetSrcProvider(ctx, jb, pluginJobSpecConfig, transmitterID, dstProvider)
if err2 != nil {
return err2
}
err2 = ccipcommit.UnregisterCommitPluginLpFilters(srcProvider, dstProvider)
if err != nil {
d.lggr.Errorw("failed to unregister ccip commit plugin filters", "err", err, "spec", spec)
d.lggr.Errorw("failed to unregister ccip commit plugin filters", "err", err2, "spec", spec)
}
return nil
case types.CCIPExecution:
err = ccipexec.UnregisterExecPluginLpFilters(context.Background(), d.lggr, jb, d.legacyChains)
// PROVIDER BASED ARG CONSTRUCTION
// Write PluginConfig bytes to send source/dest relayer provider + info outside of top level rargs/pargs over the wire
var pluginJobSpecConfig ccipconfig.ExecPluginJobSpecConfig
err = json.Unmarshal(spec.PluginConfig.Bytes(), &pluginJobSpecConfig)
if err != nil {
d.lggr.Errorw("failed to unregister ccip exec plugin filters", "err", err, "spec", spec)
return err
}

dstProvider, err2 := d.ccipExecGetDstProvider(ctx, jb, pluginJobSpecConfig, transmitterID)
if err2 != nil {
return err2
}

srcProvider, _, err2 := d.ccipExecGetSrcProvider(ctx, jb, pluginJobSpecConfig, transmitterID, dstProvider)
if err2 != nil {
return err2
}
err2 = ccipexec.UnregisterExecPluginLpFilters(srcProvider, dstProvider)
if err2 != nil {
d.lggr.Errorw("failed to unregister ccip exec plugin filters", "err", err2, "spec", spec)
}
return nil
default:
Expand Down
66 changes: 3 additions & 63 deletions core/services/ocr2/plugins/ccip/ccipcommit/initializers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/Masterminds/semver/v3"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
libocr2 "github.com/smartcontractkit/libocr/offchainreporting2plus"
"go.uber.org/multierr"

Expand All @@ -22,16 +21,13 @@ import (
commonlogger "github.com/smartcontractkit/chainlink-common/pkg/logger"
commontypes "github.com/smartcontractkit/chainlink-common/pkg/types"

cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccip"

cciporm "github.com/smartcontractkit/chainlink/v2/core/services/ccip"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/cache"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipcalc"
db "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdb"

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr"
"github.com/smartcontractkit/chainlink/v2/core/chains/legacyevm"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/commit_store"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip"
Expand Down Expand Up @@ -225,22 +221,13 @@ func CommitReportToEthTxMeta(typ ccipconfig.ContractType, ver semver.Version) (f
// https://github.com/smartcontractkit/ccip/blob/68e2197472fb017dd4e5630d21e7878d58bc2a44/core/services/feeds/service.go#L716
// TODO once that transaction is broken up, we should be able to simply rely on oracle.Close() to cleanup the filters.
// Until then we have to deterministically reload the readers from the spec (and thus their filters) and close them.
func UnregisterCommitPluginLpFilters(_ context.Context, lggr logger.Logger, jb job.Job, chainSet legacyevm.LegacyChainContainer) error {
params, err := extractJobSpecParams(jb, chainSet)
if err != nil {
return err
}
versionFinder := factory.NewEvmVersionFinder()
// TODO CCIP-2498 Use provider to close
func UnregisterCommitPluginLpFilters(srcProvider commontypes.CCIPCommitProvider, dstProvider commontypes.CCIPCommitProvider) error {
unregisterFuncs := []func() error{
func() error {
return factory.CloseCommitStoreReader(lggr, versionFinder, params.commitStoreAddress, params.destChain.Client(), params.destChain.LogPoller())
},
func() error {
return factory.CloseOnRampReader(lggr, versionFinder, params.commitStoreStaticCfg.SourceChainSelector, params.commitStoreStaticCfg.ChainSelector, cciptypes.Address(params.commitStoreStaticCfg.OnRamp.String()), params.sourceChain.LogPoller(), params.sourceChain.Client())
return srcProvider.Close()
},
func() error {
return factory.CloseOffRampReader(lggr, versionFinder, params.pluginConfig.OffRamp, params.destChain.Client(), params.destChain.LogPoller(), params.destChain.GasEstimator(), params.destChain.Config().EVM().GasEstimator().PriceMax().ToInt())
return dstProvider.Close()
},
}

Expand All @@ -252,50 +239,3 @@ func UnregisterCommitPluginLpFilters(_ context.Context, lggr logger.Logger, jb j
}
return multiErr
}

type jobSpecParams struct {
pluginConfig ccipconfig.CommitPluginJobSpecConfig
commitStoreAddress cciptypes.Address
commitStoreStaticCfg commit_store.CommitStoreStaticConfig
sourceChain legacyevm.Chain
destChain legacyevm.Chain
}

func extractJobSpecParams(jb job.Job, chainSet legacyevm.LegacyChainContainer) (*jobSpecParams, error) {
if jb.OCR2OracleSpec == nil {
return nil, errors.New("spec is nil")
}
spec := jb.OCR2OracleSpec

var pluginConfig ccipconfig.CommitPluginJobSpecConfig
err := json.Unmarshal(spec.PluginConfig.Bytes(), &pluginConfig)
if err != nil {
return nil, err
}
// ensure addresses are formatted properly - (lowercase to eip55 for evm)
pluginConfig.OffRamp = ccipcalc.HexToAddress(string(pluginConfig.OffRamp))

destChain, _, err := ccipconfig.GetChainFromSpec(spec, chainSet)
if err != nil {
return nil, err
}

commitStoreAddress := common.HexToAddress(spec.ContractID)
staticConfig, err := ccipdata.FetchCommitStoreStaticConfig(commitStoreAddress, destChain.Client())
if err != nil {
return nil, fmt.Errorf("get commit store static config: %w", err)
}

sourceChain, _, err := ccipconfig.GetChainByChainSelector(chainSet, staticConfig.SourceChainSelector)
if err != nil {
return nil, err
}

return &jobSpecParams{
pluginConfig: pluginConfig,
commitStoreAddress: ccipcalc.EvmAddrToGeneric(commitStoreAddress),
commitStoreStaticCfg: staticConfig,
sourceChain: sourceChain,
destChain: destChain,
}, nil
}
81 changes: 0 additions & 81 deletions core/services/ocr2/plugins/ccip/ccipcommit/initializers_test.go

This file was deleted.

92 changes: 7 additions & 85 deletions core/services/ocr2/plugins/ccip/ccipexec/initializers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (
"encoding/json"
"fmt"
"math/big"
"strconv"
"time"

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

"github.com/Masterminds/semver/v3"
"go.uber.org/multierr"

chainselectors "github.com/smartcontractkit/chain-selectors"
libocr2 "github.com/smartcontractkit/libocr/offchainreporting2plus"

commonlogger "github.com/smartcontractkit/chainlink-common/pkg/logger"
Expand All @@ -24,7 +22,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipcalc"

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr"
"github.com/smartcontractkit/chainlink/v2/core/chains/legacyevm"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip"
Expand Down Expand Up @@ -199,33 +196,17 @@ func NewExecServices(ctx context.Context, lggr logger.Logger, jb job.Job, srcPro
// UnregisterExecPluginLpFilters unregisters all the registered filters for both source and dest chains.
// See comment in UnregisterCommitPluginLpFilters
// It MUST mirror the filters registered in NewExecServices.
func UnregisterExecPluginLpFilters(ctx context.Context, lggr logger.Logger, jb job.Job, chainSet legacyevm.LegacyChainContainer) error {
params, err := extractJobSpecParams(lggr, jb, chainSet, false)
if err != nil {
return err
}

offRampAddress, err := params.offRampReader.Address(ctx)
if err != nil {
return fmt.Errorf("get offramp reader address: %w", err)
}

versionFinder := factory.NewEvmVersionFinder()
// This currently works because the filters registered by the created custom providers when the job is first added
// are stored in the db. Those same filters are unregistered (i.e. deleted from the db) by the newly created providers
// that are passed in from cleanupEVM, as while the providers have no knowledge of each other, they are created
// on the same source and dest relayer.
func UnregisterExecPluginLpFilters(srcProvider types.CCIPExecProvider, dstProvider types.CCIPExecProvider) error {
unregisterFuncs := []func() error{
func() error {
return factory.CloseCommitStoreReader(lggr, versionFinder, params.offRampConfig.CommitStore, params.destChain.Client(), params.destChain.LogPoller())
return srcProvider.Close()
},
func() error {
return factory.CloseOnRampReader(lggr, versionFinder, params.offRampConfig.SourceChainSelector, params.offRampConfig.ChainSelector, params.offRampConfig.OnRamp, params.sourceChain.LogPoller(), params.sourceChain.Client())
},
func() error {
return factory.CloseOffRampReader(lggr, versionFinder, offRampAddress, params.destChain.Client(), params.destChain.LogPoller(), params.destChain.GasEstimator(), params.destChain.Config().EVM().GasEstimator().PriceMax().ToInt())
},
func() error { // usdc token data reader
if usdcDisabled := params.pluginConfig.USDCConfig.AttestationAPI == ""; usdcDisabled {
return nil
}
return ccipdata.CloseUSDCReader(lggr, jobIDToString(jb.ID), params.pluginConfig.USDCConfig.SourceMessageTransmitterAddress, params.sourceChain.LogPoller())
return dstProvider.Close()
},
}

Expand All @@ -243,62 +224,3 @@ func UnregisterExecPluginLpFilters(ctx context.Context, lggr logger.Logger, jb j
func ExecReportToEthTxMeta(ctx context.Context, typ ccipconfig.ContractType, ver semver.Version) (func(report []byte) (*txmgr.TxMeta, error), error) {
return factory.ExecReportToEthTxMeta(ctx, typ, ver)
}

type jobSpecParams struct {
pluginConfig ccipconfig.ExecPluginJobSpecConfig
offRampConfig cciptypes.OffRampStaticConfig
offRampReader ccipdata.OffRampReader
sourceChain legacyevm.Chain
destChain legacyevm.Chain
}

func extractJobSpecParams(lggr logger.Logger, jb job.Job, chainSet legacyevm.LegacyChainContainer, registerFilters bool) (*jobSpecParams, error) {
if jb.OCR2OracleSpec == nil {
return nil, fmt.Errorf("spec is nil")
}
spec := jb.OCR2OracleSpec
var pluginConfig ccipconfig.ExecPluginJobSpecConfig
err := json.Unmarshal(spec.PluginConfig.Bytes(), &pluginConfig)
if err != nil {
return nil, err
}

destChain, _, err := ccipconfig.GetChainFromSpec(spec, chainSet)
if err != nil {
return nil, err
}

versionFinder := factory.NewEvmVersionFinder()
offRampAddress := ccipcalc.HexToAddress(spec.ContractID)
offRampReader, err := factory.NewOffRampReader(lggr, versionFinder, offRampAddress, destChain.Client(), destChain.LogPoller(), destChain.GasEstimator(), destChain.Config().EVM().GasEstimator().PriceMax().ToInt(), registerFilters)
if err != nil {
return nil, fmt.Errorf("create offRampReader: %w", err)
}

offRampConfig, err := offRampReader.GetStaticConfig(context.Background())
if err != nil {
return nil, fmt.Errorf("get offRamp static config: %w", err)
}

chainID, err := chainselectors.ChainIdFromSelector(offRampConfig.SourceChainSelector)
if err != nil {
return nil, err
}

sourceChain, err := chainSet.Get(strconv.FormatUint(chainID, 10))
if err != nil {
return nil, fmt.Errorf("open source chain: %w", err)
}

return &jobSpecParams{
pluginConfig: pluginConfig,
offRampConfig: offRampConfig,
offRampReader: offRampReader,
sourceChain: sourceChain,
destChain: destChain,
}, nil
}

func jobIDToString(id int32) string {
return fmt.Sprintf("job_%d", id)
}
Loading

0 comments on commit c30ef14

Please sign in to comment.