Skip to content

Commit

Permalink
Use GetSourceChainConfig instead of OffRampGetAllSourceChainConfigs
Browse files Browse the repository at this point in the history
  • Loading branch information
reductionista committed Jan 29, 2025
1 parent 7cd7574 commit 47b91ad
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
6 changes: 4 additions & 2 deletions internal/plugincommon/discovery/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/smartcontractkit/libocr/commontypes"
ragep2ptypes "github.com/smartcontractkit/libocr/ragep2p/types"
"golang.org/x/exp/maps"

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

Expand Down Expand Up @@ -65,8 +66,9 @@ func (cdp *ContractDiscoveryProcessor) Observation(
return dt.Observation{}, fmt.Errorf("unable to get fchain: %w", err)
}

// TODO: discover the full list of source chain selectors and pass it into DiscoverContracts.
contracts, err := (*cdp.reader).DiscoverContracts(ctx)
chainConfigs, err := cdp.homechain.GetAllChainConfigs()

contracts, err := (*cdp.reader).DiscoverContracts(ctx, maps.Keys(chainConfigs))
if err != nil {
return dt.Observation{}, fmt.Errorf("unable to discover contracts: %w", err)
}
Expand Down
53 changes: 33 additions & 20 deletions pkg/reader/ccip.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ func (r *ccipChainReader) GetRMNRemoteConfig(
func (r *ccipChainReader) discoverOffRampContracts(
ctx context.Context,
chain cciptypes.ChainSelector,
sourceChains []cciptypes.ChainSelector,
) (ContractAddresses, error) {
// Exit without an error if we cannot read the destination.
if err := validateExtendedReaderExistence(r.contractReaders, chain); err != nil {
Expand All @@ -721,7 +722,7 @@ func (r *ccipChainReader) discoverOffRampContracts(

// OnRamps are in the offRamp SourceChainConfig.
{
sourceConfigs, err := r.getAllOffRampSourceChainsConfig(ctx, chain)
sourceConfigs, err := r.getAllOffRampSourceChainsConfig(ctx, chain, sourceChains)
if err != nil {
return nil, fmt.Errorf("unable to get SourceChainsConfig: %w", err)
}
Expand Down Expand Up @@ -776,12 +777,14 @@ func (r *ccipChainReader) discoverOffRampContracts(
return resp, nil
}

func (r *ccipChainReader) DiscoverContracts(ctx context.Context) (ContractAddresses, error) {
func (r *ccipChainReader) DiscoverContracts(ctx context.Context,
allChains []cciptypes.ChainSelector,
) (ContractAddresses, error) {
var resp ContractAddresses

// Discover destination contracts if the dest chain is supported.
if err := validateExtendedReaderExistence(r.contractReaders, r.destChain); err == nil {
resp, err = r.discoverOffRampContracts(ctx, r.destChain)
resp, err = r.discoverOffRampContracts(ctx, r.destChain, allChains)
if err != nil {
return nil, fmt.Errorf("discover destination contracts: %w", err)
}
Expand Down Expand Up @@ -1065,38 +1068,48 @@ type selectorsAndConfigs struct {
// getAllOffRampSourceChainsConfig get all enabled source chain configs from the offRamp for the provided chain.
func (r *ccipChainReader) getAllOffRampSourceChainsConfig(
ctx context.Context,
chain cciptypes.ChainSelector,
destChain cciptypes.ChainSelector,
sourceChains []cciptypes.ChainSelector,
) (map[cciptypes.ChainSelector]sourceChainConfig, error) {
if err := validateExtendedReaderExistence(r.contractReaders, chain); err != nil {
if err := validateExtendedReaderExistence(r.contractReaders, destChain); err != nil {
return nil, fmt.Errorf("validate extended reader existence: %w", err)
}

configs := make(map[cciptypes.ChainSelector]sourceChainConfig)

var resp selectorsAndConfigs
//var resp map[string]any
err := r.contractReaders[chain].ExtendedGetLatestValue(
ctx,
consts.ContractNameOffRamp,
consts.MethodNameOffRampGetAllSourceChainConfigs,
primitives.Unconfirmed,
map[string]any{},
&resp,
params := make([]any, 0, len(sourceChains))
results := make([]types.BatchReadResult, 0, len(sourceChains))

for _, chain := range sourceChains {
params = append(params, chain)
}

results, err := r.contractReaders[destChain].ExtendedBatchGetLatestValues(
ctx, consts.ContractNameOffRamp, consts.MethodNameGetSourceChainConfig, params, sourceChainConfig{},
)

if err != nil {
return nil, fmt.Errorf("failed to get source chain configs: %w", err)
}

if len(resp.SourceChainConfigs) != len(resp.Selectors) {
return nil, fmt.Errorf("selectors and source chain configs length mismatch: %v", resp)
if len(results) != len(sourceChains) {
return nil, fmt.Errorf("selectors and source chain configs length mismatch: chains=%v, configs=%v",
sourceChains, results)
}

r.lggr.Debugw("got source chain configs", "configs", resp)
r.lggr.Debugw("got source chain configs", "configs", results)

// Populate the map.
for i := range resp.Selectors {
chainSel := cciptypes.ChainSelector(resp.Selectors[i])
cfg := resp.SourceChainConfigs[i]
for i, chainSel := range sourceChains {
v, err := results[i].GetResult()
if err != nil {
return nil, fmt.Errorf("GetSourceChainConfig for chainSelector=%d failed: %w", err)
}

cfg, ok := v.(sourceChainConfig)
if !ok {
return nil, fmt.Errorf("invalid result type from GetSourceChainConfig for chainSelector=%d: %w", err)
}

enabled, err := cfg.check()
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions pkg/reader/ccip_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,11 @@ type CCIPReader interface {
destChainSelector cciptypes.ChainSelector,
) (rmntypes.RemoteConfig, error)

// DiscoverContracts reads from all available contract readers to discover contract addresses.
DiscoverContracts(ctx context.Context) (ContractAddresses, error)
// DiscoverContracts reads the destination chain for contract addresses. They are returned per
// contract and source chain selector.
// allChains is needed because there is currently no way to discover all source contracts. So we allow them
// to be passed in here. We'll attempt to fetch the source config from the offramp for each of them.
DiscoverContracts(ctx context.Context, allChains []cciptypes.ChainSelector) (ContractAddresses, error)

// LinkPriceUSD gets the LINK price in 1e-18 USDs from the FeeQuoter contract on the destination chain.
// For example, if the price is 1 LINK = 10 USD, this function will return 10e18 (10 * 1e18). You can think of this
Expand Down

0 comments on commit 47b91ad

Please sign in to comment.