Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
makramkd committed Jul 18, 2024
1 parent be4c010 commit bcd7c3b
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 54 deletions.
50 changes: 50 additions & 0 deletions core/services/ccipcapability/configs/getters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package configs

import (
"fmt"

cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
configstypes "github.com/smartcontractkit/chainlink/v2/core/services/ccipcapability/configs/types"
"github.com/smartcontractkit/chainlink/v2/core/services/ccipcapability/configs/v1_6_0"
v1_6_0_evm "github.com/smartcontractkit/chainlink/v2/core/services/ccipcapability/configs/v1_6_0/evm"
evmrelaytypes "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/types"
)

func GetCCIPReaderContractReaderConfig(
ccipCapVersion string,
chainSelector cciptypes.ChainSelector,
chainSide configstypes.ChainSide,
) ([]byte, error) {
switch ccipCapVersion {
case "v1.6.0":
return v1_6_0.GetCCIPReaderContractReaderConfig(chainSelector, chainSide)
default:
return nil, fmt.Errorf("unsupported ccip capability version: %s", ccipCapVersion)
}
}

func GetHomeChainContractReaderConfig(
ccipCapVersion string,
) (evmrelaytypes.ChainReaderConfig, error) {
switch ccipCapVersion {
case "v1.6.0":
return v1_6_0_evm.HomeChainReaderConfigRaw(), nil
default:
return evmrelaytypes.ChainReaderConfig{}, fmt.Errorf("unsupported ccip capability version: %s", ccipCapVersion)
}
}

func GetChainWriterConfig(
ccipCapVersion string,
chainSelector cciptypes.ChainSelector,
fromAddress string,
maxGasPrice *assets.Wei,
) ([]byte, error) {
switch ccipCapVersion {
case "v1.6.0":
return v1_6_0.GetChainWriterConfig(chainSelector, fromAddress, maxGasPrice)
default:
return nil, fmt.Errorf("unsupported ccip capability version: %s", ccipCapVersion)
}
}
12 changes: 12 additions & 0 deletions core/services/ccipcapability/configs/types/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package types

// ChainSide represents the two sides of a CCIP plugin,
// the source and the destination chains.
type ChainSide string

const (
// ChainSideSource represents the source chain of a CCIP plugin.
ChainSideSource ChainSide = "source"
// ChainSideDest represents the destination chain of a CCIP plugin.
ChainSideDest ChainSide = "dest"
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package evm

import (
"encoding/json"
"fmt"

"github.com/ethereum/go-ethereum/accounts/abi"
Expand All @@ -20,16 +19,6 @@ var (
offrampABI = evmtypes.MustGetABI(evm_2_evm_multi_offramp.EVM2EVMMultiOffRampABI)
)

func MustChainWriterConfig(fromAddress common.Address, maxGasPrice *assets.Wei) []byte {
rawConfig := ChainWriterConfigRaw(fromAddress, maxGasPrice)
encoded, err := json.Marshal(rawConfig)
if err != nil {
panic(fmt.Errorf("failed to marshal ChainWriterConfig: %w", err))
}

return encoded
}

// ChainWriterConfigRaw returns a ChainWriterConfig that can be used to transmit commit and execute reports.
func ChainWriterConfigRaw(fromAddress common.Address, maxGasPrice *assets.Wei) evmrelaytypes.ChainWriterConfig {
return evmrelaytypes.ChainWriterConfig{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package evm

import (
"encoding/json"
"fmt"

"github.com/ethereum/go-ethereum/accounts/abi"
Expand All @@ -13,6 +12,7 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/evm_2_evm_multi_offramp"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/evm_2_evm_multi_onramp"
kcr "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/keystone/generated/capabilities_registry"
configstypes "github.com/smartcontractkit/chainlink/v2/core/services/ccipcapability/configs/types"
evmrelaytypes "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/types"
)

Expand All @@ -22,28 +22,15 @@ var (
ccipConfigABI = evmtypes.MustGetABI(ccip_config.CCIPConfigABI)
)

// MustSourceReaderConfig returns a ChainReaderConfig that can be used to read from the onramp.
// The configuration is marshaled into JSON so that it can be passed to the relayer NewContractReader() method.
func MustSourceReaderConfig() []byte {
rawConfig := SourceReaderConfig()
encoded, err := json.Marshal(rawConfig)
if err != nil {
panic(fmt.Errorf("failed to marshal ChainReaderConfig into JSON: %w", err))
func CCIPReaderContractReaderConfig(chainSide configstypes.ChainSide) (evmrelaytypes.ChainReaderConfig, error) {
switch chainSide {
case configstypes.ChainSideDest:
return DestReaderConfig(), nil
case configstypes.ChainSideSource:
return SourceReaderConfig(), nil
default:
return evmrelaytypes.ChainReaderConfig{}, fmt.Errorf("unsupported chain side: %s", chainSide)
}

return encoded
}

// MustDestReaderConfig returns a ChainReaderConfig that can be used to read from the offramp.
// The configuration is marshaled into JSON so that it can be passed to the relayer NewContractReader() method.
func MustDestReaderConfig() []byte {
rawConfig := DestReaderConfig()
encoded, err := json.Marshal(rawConfig)
if err != nil {
panic(fmt.Errorf("failed to marshal ChainReaderConfig into JSON: %w", err))
}

return encoded
}

// DestReaderConfig returns a ChainReaderConfig that can be used to read from the offramp.
Expand Down
42 changes: 42 additions & 0 deletions core/services/ccipcapability/configs/v1_6_0/getters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package v1_6_0

import (
"encoding/json"
"fmt"

"github.com/ethereum/go-ethereum/common"
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
configstypes "github.com/smartcontractkit/chainlink/v2/core/services/ccipcapability/configs/types"
v1_6_0_evm "github.com/smartcontractkit/chainlink/v2/core/services/ccipcapability/configs/v1_6_0/evm"
)

// GetCCIPReaderContractReaderConfig returns the marshaled JSON configuration for the CCIP reader contract reader.
// A different configuration is returned based on chain family and based on whether we are setting up the chain
// reader to read a source or a destination chain.
func GetCCIPReaderContractReaderConfig(chainSelector cciptypes.ChainSelector, chainSide configstypes.ChainSide) ([]byte, error) {
switch family := getFamily(chainSelector); family {
case "EVM":
cfg, err := v1_6_0_evm.CCIPReaderContractReaderConfig(chainSide)
if err != nil {
return nil, fmt.Errorf("failed to get EVM CCIP reader contract reader config: %w", err)
}
return json.Marshal(cfg)
default:
return nil, fmt.Errorf("unsupported chain family: %s", family)
}
}

func GetChainWriterConfig(chainSelector cciptypes.ChainSelector, fromAddress string, maxGasPrice *assets.Wei) ([]byte, error) {
switch family := getFamily(chainSelector); family {
case "EVM":
return json.Marshal(v1_6_0_evm.ChainWriterConfigRaw(common.HexToAddress(fromAddress), maxGasPrice))
default:
return nil, fmt.Errorf("unsupported chain family: %s", family)
}
}

// TODO: move this API to chain-selectors library.
func getFamily(_ cciptypes.ChainSelector) string {
return "EVM"
}
10 changes: 8 additions & 2 deletions core/services/ccipcapability/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
kcr "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/keystone/generated/capabilities_registry"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/ccipcapability/common"
configsevm "github.com/smartcontractkit/chainlink/v2/core/services/ccipcapability/configs/evm"
"github.com/smartcontractkit/chainlink/v2/core/services/ccipcapability/configs"
"github.com/smartcontractkit/chainlink/v2/core/services/ccipcapability/launcher"
"github.com/smartcontractkit/chainlink/v2/core/services/ccipcapability/oraclecreator"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
Expand Down Expand Up @@ -132,6 +132,7 @@ func (d *Delegate) ServicesForSpec(ctx context.Context, spec job.Job) (services
)

oracleCreator := oraclecreator.New(
spec.CCIPSpec.CapabilityVersion,
ocrKeys,
transmitterKeys,
d.chains,
Expand Down Expand Up @@ -230,12 +231,17 @@ func (d *Delegate) getHomeChainContractReader(
return nil, fmt.Errorf("home chain relayer not found, chain id: %s, err: %w", homeChainRelayID.String(), err)
}

readerConfig, err := configs.GetHomeChainContractReaderConfig(capabilityVersion)
if err != nil {
return nil, fmt.Errorf("failed to get home chain contract reader config: %w", err)
}

reader, err := evm.NewChainReaderService(
context.Background(),
d.lggr,
homeChain.LogPoller(),
homeChain.Client(),
configsevm.HomeChainReaderConfigRaw(),
readerConfig,
)
if err != nil {
return nil, fmt.Errorf("failed to create home chain contract reader: %w", err)
Expand Down
6 changes: 3 additions & 3 deletions core/services/ccipcapability/launcher/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func TestIntegration_Launcher(t *testing.T) {

hcr := uni.HomeChainReader
launcher := New(
it.CcipCapabilityVersion,
it.CcipCapabilityLabelledName,
it.CCIPCapabilityVersion,
it.CCIPCapabilityLabelledName,
p2pIDs[0],
logger.TestLogger(t),
hcr,
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestIntegration_Launcher(t *testing.T) {
require.NoError(t, err)
uni.Backend.Commit()

ccipCapabilityID, err := uni.CapReg.GetHashedCapabilityId(nil, it.CcipCapabilityLabelledName, it.CcipCapabilityVersion)
ccipCapabilityID, err := uni.CapReg.GetHashedCapabilityId(nil, it.CCIPCapabilityLabelledName, it.CCIPCapabilityVersion)
require.NoError(t, err)

uni.AddDONToRegistry(
Expand Down
73 changes: 64 additions & 9 deletions core/services/ccipcapability/oraclecreator/inprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package oraclecreator

import (
"context"
"encoding/json"
"fmt"
"time"

Expand All @@ -27,7 +28,9 @@ import (
"github.com/smartcontractkit/chainlink-common/pkg/types"
"github.com/smartcontractkit/chainlink/v2/core/chains/legacyevm"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/ccipcapability/configs"
evmconfigs "github.com/smartcontractkit/chainlink/v2/core/services/ccipcapability/configs/evm"
configstypes "github.com/smartcontractkit/chainlink/v2/core/services/ccipcapability/configs/types"
"github.com/smartcontractkit/chainlink/v2/core/services/ccipcapability/ocrimpls"
cctypes "github.com/smartcontractkit/chainlink/v2/core/services/ccipcapability/types"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
Expand All @@ -47,6 +50,7 @@ var _ cctypes.OracleCreator = &inprocessOracleCreator{}
// inprocessOracleCreator creates oracles that reference plugins running
// in the same process as the chainlink node, i.e not LOOPPs.
type inprocessOracleCreator struct {
ccipCapabilityVersion string
ocrKeyBundles map[string]ocr2key.KeyBundle
transmitters map[types.RelayID][]string
chains legacyevm.LegacyChainContainer
Expand All @@ -63,6 +67,7 @@ type inprocessOracleCreator struct {
}

func New(
ccipCapabilityVersion string,
ocrKeyBundles map[string]ocr2key.KeyBundle,
transmitters map[types.RelayID][]string,
chains legacyevm.LegacyChainContainer,
Expand Down Expand Up @@ -150,12 +155,22 @@ func (i *inprocessOracleCreator) CreatePluginOracle(pluginType cctypes.PluginTyp
contractReaders := make(map[cciptypes.ChainSelector]types.ContractReader)
chainWriters := make(map[cciptypes.ChainSelector]types.ChainWriter)
for _, chain := range i.chains.Slice() {
var chainReaderConfig evmrelaytypes.ChainReaderConfig
chainSelector, ok := chainsel.EvmChainIdToChainSelector()[chain.ID().Uint64()]
if !ok {
return nil, fmt.Errorf("failed to get chain selector from chain ID %s", chain.ID())
}

var chainSide configstypes.ChainSide
if chain.ID().Uint64() == destChainID {
chainReaderConfig = evmconfigs.DestReaderConfig()
chainSide = configstypes.ChainSideDest
} else {
chainReaderConfig = evmconfigs.SourceReaderConfig()
chainSide = configstypes.ChainSideSource
}
crConfig, err2 := getEVMCCIPReaderConfig(i.ccipCapabilityVersion, cciptypes.ChainSelector(chainSelector), chainSide)
if err2 != nil {
return nil, fmt.Errorf("failed to get CCIP reader config for chain %s: %w", chain.ID(), err2)
}

cr, err2 := evm.NewChainReaderService(
context.Background(),
i.lggr.
Expand All @@ -164,7 +179,7 @@ func (i *inprocessOracleCreator) CreatePluginOracle(pluginType cctypes.PluginTyp
Named(pluginType.String()),
chain.LogPoller(),
chain.Client(),
chainReaderConfig,
crConfig,
)
if err2 != nil {
return nil, fmt.Errorf("failed to create contract reader for chain %s: %w", chain.ID(), err2)
Expand Down Expand Up @@ -218,11 +233,6 @@ func (i *inprocessOracleCreator) CreatePluginOracle(pluginType cctypes.PluginTyp
return nil, fmt.Errorf("failed to start chain writer for chain %s: %w", chain.ID(), err2)
}

chainSelector, ok := chainsel.EvmChainIdToChainSelector()[chain.ID().Uint64()]
if !ok {
return nil, fmt.Errorf("failed to get chain selector from chain ID %s", chain.ID())
}

contractReaders[cciptypes.ChainSelector(chainSelector)] = cr
chainWriters[cciptypes.ChainSelector(chainSelector)] = cw
}
Expand Down Expand Up @@ -337,3 +347,48 @@ func defaultLocalConfig() ocrtypes.LocalConfig {
DevelopmentMode: "false",
}
}

func getEVMCCIPReaderConfig(
ccipCapVersion string,
chainSelector cciptypes.ChainSelector,
chainSide configstypes.ChainSide,
) (evmrelaytypes.ChainReaderConfig, error) {
configBytes, err := configs.GetCCIPReaderContractReaderConfig(ccipCapVersion, chainSelector, chainSide)
if err != nil {
return evmrelaytypes.ChainReaderConfig{}, fmt.Errorf("failed to get CCIP reader contract reader config: %w", err)
}

// unmarshal it back into evmrelaytypes.ChainReaderConfig because we're instantiating
// the EVM chain reader directly.
// TODO: in the future, this should not be done, and instead we pass the raw bytes.
// Once we're using relayers, this should be possible.
var chainReaderConfig evmrelaytypes.ChainReaderConfig
err = json.Unmarshal(configBytes, &chainReaderConfig)
if err != nil {
return evmrelaytypes.ChainReaderConfig{}, fmt.Errorf("failed to unmarshal CCIP reader contract reader config: %w", err)
}

return chainReaderConfig, nil
}

func getEVMChainWriterConfig(
ccipCapVersion string,
chainSelector cciptypes.ChainSelector,
) (evmrelaytypes.ChainWriterConfig, error) {
configBytes, err := configs.GetChainWriterConfig(ccipCapVersion, chainSelector)
if err != nil {
return evmrelaytypes.ChainWriterConfig{}, fmt.Errorf("failed to get chain writer config: %w", err)
}

// unmarshal it back into evmrelaytypes.ChainWriterConfig because we're instantiating
// the EVM chain writer directly.
// TODO: in the future, this should not be done, and instead we pass the raw bytes.
// Once we're using relayers, this should be possible.
var chainWriterConfig evmrelaytypes.ChainWriterConfig
err = json.Unmarshal(configBytes, &chainWriterConfig)
if err != nil {
return evmrelaytypes.ChainWriterConfig{}, fmt.Errorf("failed to unmarshal chain writer config: %w", err)
}

return chainWriterConfig, nil
}
4 changes: 4 additions & 0 deletions core/services/ccipcapability/oraclecreator/inprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

const (
ccipCapabilityVersion = "v1.6.0-dev"
)

func TestOracleCreator_CreateBootstrap(t *testing.T) {
db := pgtest.NewSqlxDB(t)

Expand Down
Loading

0 comments on commit bcd7c3b

Please sign in to comment.