Skip to content

Commit

Permalink
Remove chainIds from JobSpecs (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-sekara authored Aug 18, 2023
1 parent f5ea4f8 commit f0e1c27
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 56 deletions.
1 change: 0 additions & 1 deletion core/scripts/ccip/dione/jobspecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func NewCCIPJobSpecParams(sourceChainConfig *rhea.EVMChainConfig, sourceLane rhe
SourceStartBlock: sourceLane.DeploySettings.DeployedAtBlock,
DestStartBlock: destLane.DeploySettings.DeployedAtBlock,
P2PV2Bootstrappers: []string{}, // Set in env vars
SourceEvmChainId: sourceChainConfig.EvmChainId,
DestEvmChainId: destChainConfig.EvmChainId,
Version: version,
}
Expand Down
27 changes: 18 additions & 9 deletions core/services/ocr2/plugins/ccip/commit_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ func NewCommitServices(lggr logger.Logger, jb job.Job, chainSet evm.ChainSet, ne
if err != nil {
return nil, errors.Wrap(err, "failed getting the static config from the commitStore")
}
sourceChain, err := chainSet.Get(big.NewInt(0).SetUint64(uint64(pluginConfig.SourceEvmChainId)))
chainId, err := ccipconfig.ChainIdFromSelector(staticConfig.SourceChainSelector)
if err != nil {
return nil, err
}
sourceChain, err := chainSet.Get(big.NewInt(0).SetUint64(chainId))
if err != nil {
return nil, errors.Wrap(err, "unable to open source chain")
}
Expand Down Expand Up @@ -120,11 +124,11 @@ func NewCommitServices(lggr logger.Logger, jb job.Job, chainSet evm.ChainSet, ne
leafHasher := hasher.NewLeafHasher(staticConfig.SourceChainSelector, staticConfig.ChainSelector, onRamp.Address(), hasher.NewKeccakCtx())
// Note that lggr already has the jobName and contractID (commit store)
commitLggr := lggr.Named("CCIPCommit").With(
"sourceChain", ChainName(pluginConfig.SourceEvmChainId),
"sourceChain", ChainName(int64(chainId)),
"destChain", ChainName(destChainID))
checkFinalityTags, ok := checkFinalityTags[pluginConfig.SourceEvmChainId]
checkFinalityTags, ok := checkFinalityTags[int64(chainId)]
if !ok {
return nil, errors.Errorf("finality information for chain %d not supported", pluginConfig.SourceEvmChainId)
return nil, errors.Errorf("finality information for chain %d not supported", chainId)
}
wrappedPluginFactory := NewCommitReportingPluginFactory(
CommitPluginConfig{
Expand Down Expand Up @@ -271,17 +275,22 @@ func UnregisterCommitPluginLpFilters(ctx context.Context, q pg.Queryer, spec *jo
if err != nil {
return err
}

sourceChain, err := chainSet.Get(big.NewInt(0).SetUint64(uint64(pluginConfig.SourceEvmChainId)))
commitStore, err := LoadCommitStore(common.HexToAddress(spec.ContractID), CommitPluginLabel, destChain.Client())
if err != nil {
return err
}

commitStore, err := LoadCommitStore(common.HexToAddress(spec.ContractID), CommitPluginLabel, destChain.Client())
staticConfig, err := commitStore.GetStaticConfig(&bind.CallOpts{})
if err != nil {
return err
}
chainId, err := ccipconfig.ChainIdFromSelector(staticConfig.SourceChainSelector)
if err != nil {
return err
}
sourceChain, err := chainSet.Get(big.NewInt(0).SetUint64(chainId))
if err != nil {
return err
}

return unregisterCommitPluginFilters(ctx, q, sourceChain.LogPoller(), destChain.LogPoller(), commitStore, common.HexToAddress(pluginConfig.OffRamp))
}

Expand Down
45 changes: 42 additions & 3 deletions core/services/ocr2/plugins/ccip/config/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,50 @@
package config

import "fmt"

// TODO Replace with https://github.com/smartcontractkit/ccip-chain-selectors after making it public
var EvmChainIdToChainSelector = map[uint64]uint64{
// Testnets
97: 13264668187771770619, // BSC Testnet
420: 2664363617261496610, // Optimism Goerli
1000: 11787463284727550157, // Tests
1337: 3379446385462418246, // Tests
2337: 12922642891491394802, // Tests
84531: 5790810961207155433, // BASE Goerli
80001: 12532609583862916517, // Polygon Mumbai
421613: 6101244977088475029, // Arbitrum Goerli
11155111: 16015286601757825753, // Sepolia

// Mainnets
1: 5009297550715157269, // Ethereum
10: 3734403246176062136, // Optimism
56: 11344663589394136015, // BSC
137: 4051577828743386545, // Polygon
8453: 15971525489660198786, // BASE
42161: 4949039107694359620, // Arbitrum
43114: 6433500567565415381, // Avalanche
}

func ChainIdFromSelector(chainSelectorId uint64) (uint64, error) {
for k, v := range EvmChainIdToChainSelector {
if v == chainSelectorId {
return k, nil
}
}
return 0, fmt.Errorf("chain not found for chain selector %d", chainSelectorId)
}

func SelectorFromChainId(chainId uint64) (uint64, error) {
if chainSelectorId, exist := EvmChainIdToChainSelector[chainId]; exist {
return chainSelectorId, nil
}
return 0, fmt.Errorf("chain selector not found for chain %d", chainId)
}

// CommitPluginConfig contains the plugin specific variables for the ccip.CCIPCommit plugin.
// We use ID here to keep it as general as possible, e.g. abstracting for chains which don't have an address concept.
type CommitPluginConfig struct {
SourceStartBlock, DestStartBlock int64 // Only for first time job add.
SourceEvmChainId int64
SourceStartBlock, DestStartBlock int64 // Only for first time job add.
OffRamp string `json:"offRamp"`
// TokenPricesUSDPipeline should contain a token price pipeline for the following tokens:
// The SOURCE chain wrapped native
Expand All @@ -15,5 +55,4 @@ type CommitPluginConfig struct {
// ExecutionPluginConfig contains the plugin specific variables for the ccip.CCIPExecution plugin.
type ExecutionPluginConfig struct {
SourceStartBlock, DestStartBlock int64 // Only for first time job add.
SourceEvmChainId int64
}
14 changes: 11 additions & 3 deletions core/services/ocr2/plugins/ccip/execution_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ func NewExecutionServices(lggr logger.Logger, jb job.Job, chainSet evm.ChainSet,
if err != nil {
return nil, err
}
sourceChain, err := chainSet.Get(big.NewInt(0).SetUint64(uint64(pluginConfig.SourceEvmChainId)))
chainId, err := ccipconfig.ChainIdFromSelector(offRampConfig.SourceChainSelector)
if err != nil {
return nil, err
}
sourceChain, err := chainSet.Get(big.NewInt(0).SetUint64(chainId))
if err != nil {
return nil, errors.Wrap(err, "unable to open source chain")
}
Expand Down Expand Up @@ -96,7 +100,7 @@ func NewExecutionServices(lggr logger.Logger, jb job.Job, chainSet evm.ChainSet,
}

execLggr := lggr.Named("CCIPExecution").With(
"sourceChain", ChainName(pluginConfig.SourceEvmChainId),
"sourceChain", ChainName(int64(chainId)),
"destChain", ChainName(destChainID))

wrappedPluginFactory := NewExecutionReportingPluginFactory(
Expand Down Expand Up @@ -237,7 +241,11 @@ func UnregisterExecPluginLpFilters(ctx context.Context, q pg.Queryer, spec *job.
if err != nil {
return err
}
sourceChain, err := chainSet.Get(big.NewInt(0).SetUint64(uint64(pluginConfig.SourceEvmChainId)))
chainId, err := ccipconfig.ChainIdFromSelector(offRampConfig.SourceChainSelector)
if err != nil {
return err
}
sourceChain, err := chainSet.Get(big.NewInt(0).SetUint64(chainId))
if err != nil {
return errors.Wrap(err, "unable to open source chain")
}
Expand Down
4 changes: 2 additions & 2 deletions core/services/ocr2/plugins/ccip/testhelpers/ccip_contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ var (
HundredLink = Link(100)
LinkUSDValue = func(amount int64) *big.Int { return new(big.Int).Mul(big.NewInt(1e18), big.NewInt(amount)) }
SourceChainID = uint64(1000)
SourceChainSelector = uint64(1500)
SourceChainSelector = uint64(11787463284727550157)
DestChainID = uint64(1337)
DestChainSelector = uint64(3000)
DestChainSelector = uint64(3379446385462418246)
)

type MaybeRevertReceiver struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type CCIPJobSpecParams struct {
CommitStore common.Address
SourceChainName string
DestChainName string
SourceEvmChainId uint64
DestEvmChainId uint64
TokenPricesUSDPipeline string
SourceStartBlock uint64
Expand Down Expand Up @@ -88,8 +87,7 @@ func (params CCIPJobSpecParams) CommitJobSpec() (*client.OCR2TaskJobSpec, error)
ContractConfigTrackerPollInterval: models.Interval(20 * time.Second),
P2PV2Bootstrappers: params.P2PV2Bootstrappers,
PluginConfig: map[string]interface{}{
"sourceEvmChainId": params.SourceEvmChainId,
"offRamp": fmt.Sprintf(`"%s"`, params.OffRamp.Hex()),
"offRamp": fmt.Sprintf(`"%s"`, params.OffRamp.Hex()),
"tokenPricesUSDPipeline": fmt.Sprintf(`"""
%s
"""`, params.TokenPricesUSDPipeline),
Expand Down Expand Up @@ -126,9 +124,7 @@ func (params CCIPJobSpecParams) ExecutionJobSpec() (*client.OCR2TaskJobSpec, err
ContractConfigTrackerPollInterval: models.Interval(20 * time.Second),

P2PV2Bootstrappers: params.P2PV2Bootstrappers,
PluginConfig: map[string]interface{}{
"sourceEvmChainId": params.SourceEvmChainId,
},
PluginConfig: map[string]interface{}{},
RelayConfig: map[string]interface{}{
"chainID": params.DestEvmChainId,
},
Expand Down Expand Up @@ -168,7 +164,6 @@ func (c *CCIPIntegrationTestHarness) NewCCIPJobSpecParams(tokenPricesUSDPipeline
CommitStore: c.Dest.CommitStore.Address(),
OffRamp: c.Dest.OffRamp.Address(),
DestEvmChainId: c.Dest.ChainID,
SourceEvmChainId: c.Source.ChainID,
SourceChainName: "SimulatedSource",
DestChainName: "SimulatedDest",
TokenPricesUSDPipeline: tokenPricesUSDPipeline,
Expand Down
36 changes: 6 additions & 30 deletions integration-tests/actions/ccip_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/router"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/abihelpers"
ccipConfig "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/config"
ccipconfig "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/config"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/testhelpers"
integrationtesthelpers "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/testhelpers/integration"
"github.com/smartcontractkit/chainlink/v2/core/store/models"
Expand Down Expand Up @@ -69,30 +70,6 @@ type CCIPTOMLEnv struct {
Networks []blockchain.EVMNetwork
}

var EvmChainIdToChainSelector = func(chainId uint64) (uint64, error) {
mapSelector := map[uint64]uint64{
// Testnets
420: 2664363617261496610, // Optimism Goerli
1337: 3379446385462418246, // Tests
2337: 12922642891491394802, // Tests
43113: 14767482510784806043, // Avax Fuji
80001: 12532609583862916517, // Polygon Mumbai
421613: 6101244977088475029, // Arbitrum Goerli
11155111: 16015286601757825753, // Sepolia
// Mainnets
1: 5009297550715157269, // Ethereum
10: 3734403246176062136, // Optimism
137: 4051577828743386545, // Polygon
42161: 4949039107694359620, // Arbitrum
43114: 6433500567565415381, // Avalanche
}
chainSelector, ok := mapSelector[chainId]
if !ok {
return 0, fmt.Errorf("chain id %d not found in chain selector", chainId)
}
return chainSelector, nil
}

var (
NetworkName = func(name string) string {
return strings.ReplaceAll(strings.ToLower(name), " ", "-")
Expand Down Expand Up @@ -512,11 +489,11 @@ func (sourceCCIP *SourceCCIPModule) DeployContracts(lane *laneconfig.LaneConfig)
if len(sourceCCIP.TransferAmount) != len(sourceCCIP.Common.BridgeTokens) {
sourceCCIP.TransferAmount = sourceCCIP.TransferAmount[:len(sourceCCIP.Common.BridgeTokens)]
}
sourceChainSelector, err := EvmChainIdToChainSelector(sourceCCIP.Common.ChainClient.GetChainID().Uint64())
sourceChainSelector, err := ccipconfig.SelectorFromChainId(sourceCCIP.Common.ChainClient.GetChainID().Uint64())
if err != nil {
return errors.WithStack(err)
}
destChainSelector, err := EvmChainIdToChainSelector(sourceCCIP.DestinationChainId)
destChainSelector, err := ccipconfig.SelectorFromChainId(sourceCCIP.DestinationChainId)
if err != nil {
return errors.WithStack(err)
}
Expand Down Expand Up @@ -842,7 +819,7 @@ func (sourceCCIP *SourceCCIPModule) SendRequest(
if err != nil {
return common.Hash{}, d, nil, fmt.Errorf("failed encoding the options field: %+v", err)
}
destChainSelector, err := EvmChainIdToChainSelector(sourceCCIP.DestinationChainId)
destChainSelector, err := ccipconfig.SelectorFromChainId(sourceCCIP.DestinationChainId)
if err != nil {
return common.Hash{}, d, nil, fmt.Errorf("failed getting the chain selector: %+v", err)
}
Expand Down Expand Up @@ -959,11 +936,11 @@ func (destCCIP *DestCCIPModule) DeployContracts(
}

destCCIP.LoadContracts(lane)
sourceChainSelector, err := EvmChainIdToChainSelector(destCCIP.SourceChainId)
sourceChainSelector, err := ccipconfig.SelectorFromChainId(destCCIP.SourceChainId)
if err != nil {
return errors.WithStack(err)
}
destChainSelector, err := EvmChainIdToChainSelector(destCCIP.Common.ChainClient.GetChainID().Uint64())
destChainSelector, err := ccipconfig.SelectorFromChainId(destCCIP.Common.ChainClient.GetChainID().Uint64())
if err != nil {
return errors.WithStack(err)
}
Expand Down Expand Up @@ -1884,7 +1861,6 @@ func (lane *CCIPLane) DeployNewCCIPLane(
CommitStore: lane.Dest.CommitStore.EthAddress,
SourceChainName: sourceChainClient.GetNetworkName(),
DestChainName: destChainClient.GetNetworkName(),
SourceEvmChainId: sourceChainClient.GetChainID().Uint64(),
DestEvmChainId: destChainClient.GetChainID().Uint64(),
SourceStartBlock: lane.Source.SrcStartBlock,
DestStartBlock: currentBlockOnDest,
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/load/ccip_loadgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/smartcontractkit/chainlink/integration-tests/actions"
"github.com/smartcontractkit/chainlink/integration-tests/testreporters"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/router"
ccipconfig "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/config"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/testhelpers"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
Expand Down Expand Up @@ -161,7 +162,7 @@ func (c *CCIPE2ELoad) Call(_ *wasp.Generator) *wasp.CallResult {
var sendTx *types.Transaction
var err error

destChainSelector, err := actions.EvmChainIdToChainSelector(sourceCCIP.DestinationChainId)
destChainSelector, err := ccipconfig.SelectorFromChainId(sourceCCIP.DestinationChainId)
if err != nil {
res.Error = err.Error()
res.Failed = true
Expand Down

0 comments on commit f0e1c27

Please sign in to comment.