From bbc055437b727aa614796ff8c91f038004c86608 Mon Sep 17 00:00:00 2001 From: Will Winder Date: Fri, 23 Aug 2024 14:29:20 -0400 Subject: [PATCH] Initialize reader configs when module loads. (#1353) ## Motivation Ensure the `mustGetMethodName` and `mustGetEventName` functions are called as early as possible. ## Solution Change config functions to be variables, this way they should evaluate as the module loads rather than when the CCIP Reader object is initialized. --- .../integrationhelpers/integration_helpers.go | 2 +- .../ccip/configs/evm/contract_reader.go | 270 +++++++++--------- core/capabilities/ccip/delegate.go | 2 +- .../ccip/oraclecreator/inprocess.go | 4 +- 4 files changed, 136 insertions(+), 142 deletions(-) diff --git a/core/capabilities/ccip/ccip_integration_tests/integrationhelpers/integration_helpers.go b/core/capabilities/ccip/ccip_integration_tests/integrationhelpers/integration_helpers.go index 3009ecf222..4b6a0c84e6 100644 --- a/core/capabilities/ccip/ccip_integration_tests/integrationhelpers/integration_helpers.go +++ b/core/capabilities/ccip/ccip_integration_tests/integrationhelpers/integration_helpers.go @@ -230,7 +230,7 @@ func (t *TestUniverse) AddCapability(p2pIDs [][32]byte) { } func NewHomeChainReader(t *testing.T, logPoller logpoller.LogPoller, headTracker logpoller.HeadTracker, client client.Client, ccAddress common.Address) ccipreader.HomeChain { - cr := NewReader(t, logPoller, headTracker, client, ccAddress, configsevm.HomeChainReaderConfigRaw()) + cr := NewReader(t, logPoller, headTracker, client, ccAddress, configsevm.HomeChainReaderConfigRaw) hcr := ccipreader.NewHomeChainReader(cr, logger.TestLogger(t), 500*time.Millisecond, types.BoundContract{ Address: ccAddress.String(), diff --git a/core/capabilities/ccip/configs/evm/contract_reader.go b/core/capabilities/ccip/configs/evm/contract_reader.go index 6774c8c1fe..fd5cc49fa3 100644 --- a/core/capabilities/ccip/configs/evm/contract_reader.go +++ b/core/capabilities/ccip/configs/evm/contract_reader.go @@ -27,7 +27,7 @@ var ( // 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() + rawConfig := SourceReaderConfig encoded, err := json.Marshal(rawConfig) if err != nil { panic(fmt.Errorf("failed to marshal ChainReaderConfig into JSON: %w", err)) @@ -39,7 +39,7 @@ func MustSourceReaderConfig() []byte { // 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() + rawConfig := DestReaderConfig encoded, err := json.Marshal(rawConfig) if err != nil { panic(fmt.Errorf("failed to marshal ChainReaderConfig into JSON: %w", err)) @@ -49,165 +49,159 @@ func MustDestReaderConfig() []byte { } // DestReaderConfig returns a ChainReaderConfig that can be used to read from the offramp. -func DestReaderConfig() evmrelaytypes.ChainReaderConfig { - return evmrelaytypes.ChainReaderConfig{ - Contracts: map[string]evmrelaytypes.ChainContractReader{ - consts.ContractNameOffRamp: { - ContractABI: offramp.OffRampABI, - ContractPollingFilter: evmrelaytypes.ContractPollingFilter{ - GenericEventNames: []string{ - mustGetEventName(consts.EventNameExecutionStateChanged, offrampABI), - mustGetEventName(consts.EventNameCommitReportAccepted, offrampABI), - }, +var DestReaderConfig = evmrelaytypes.ChainReaderConfig{ + Contracts: map[string]evmrelaytypes.ChainContractReader{ + consts.ContractNameOffRamp: { + ContractABI: offramp.OffRampABI, + ContractPollingFilter: evmrelaytypes.ContractPollingFilter{ + GenericEventNames: []string{ + mustGetEventName(consts.EventNameExecutionStateChanged, offrampABI), + mustGetEventName(consts.EventNameCommitReportAccepted, offrampABI), }, - Configs: map[string]*evmrelaytypes.ChainReaderDefinition{ - consts.MethodNameGetExecutionState: { - ChainSpecificName: mustGetMethodName("getExecutionState", offrampABI), - ReadType: evmrelaytypes.Method, - }, - consts.MethodNameGetMerkleRoot: { - ChainSpecificName: mustGetMethodName("getMerkleRoot", offrampABI), - ReadType: evmrelaytypes.Method, - }, - consts.MethodNameIsBlessed: { - ChainSpecificName: mustGetMethodName("isBlessed", offrampABI), - ReadType: evmrelaytypes.Method, - }, - consts.MethodNameGetLatestPriceSequenceNumber: { - ChainSpecificName: mustGetMethodName("getLatestPriceSequenceNumber", offrampABI), - ReadType: evmrelaytypes.Method, - }, - consts.MethodNameOfframpGetStaticConfig: { - ChainSpecificName: mustGetMethodName("getStaticConfig", offrampABI), - ReadType: evmrelaytypes.Method, - }, - consts.MethodNameOfframpGetDynamicConfig: { - ChainSpecificName: mustGetMethodName("getDynamicConfig", offrampABI), - ReadType: evmrelaytypes.Method, - }, - consts.MethodNameGetSourceChainConfig: { - ChainSpecificName: mustGetMethodName("getSourceChainConfig", offrampABI), - ReadType: evmrelaytypes.Method, - }, - consts.EventNameCommitReportAccepted: { - ChainSpecificName: mustGetEventName(consts.EventNameCommitReportAccepted, offrampABI), - ReadType: evmrelaytypes.Event, - }, - consts.EventNameExecutionStateChanged: { - ChainSpecificName: mustGetEventName(consts.EventNameExecutionStateChanged, offrampABI), - ReadType: evmrelaytypes.Event, - }, + }, + Configs: map[string]*evmrelaytypes.ChainReaderDefinition{ + consts.MethodNameGetExecutionState: { + ChainSpecificName: mustGetMethodName("getExecutionState", offrampABI), + ReadType: evmrelaytypes.Method, + }, + consts.MethodNameGetMerkleRoot: { + ChainSpecificName: mustGetMethodName("getMerkleRoot", offrampABI), + ReadType: evmrelaytypes.Method, + }, + consts.MethodNameIsBlessed: { + ChainSpecificName: mustGetMethodName("isBlessed", offrampABI), + ReadType: evmrelaytypes.Method, + }, + consts.MethodNameGetLatestPriceSequenceNumber: { + ChainSpecificName: mustGetMethodName("getLatestPriceSequenceNumber", offrampABI), + ReadType: evmrelaytypes.Method, + }, + consts.MethodNameOfframpGetStaticConfig: { + ChainSpecificName: mustGetMethodName("getStaticConfig", offrampABI), + ReadType: evmrelaytypes.Method, + }, + consts.MethodNameOfframpGetDynamicConfig: { + ChainSpecificName: mustGetMethodName("getDynamicConfig", offrampABI), + ReadType: evmrelaytypes.Method, + }, + consts.MethodNameGetSourceChainConfig: { + ChainSpecificName: mustGetMethodName("getSourceChainConfig", offrampABI), + ReadType: evmrelaytypes.Method, + }, + consts.EventNameCommitReportAccepted: { + ChainSpecificName: mustGetEventName(consts.EventNameCommitReportAccepted, offrampABI), + ReadType: evmrelaytypes.Event, + }, + consts.EventNameExecutionStateChanged: { + ChainSpecificName: mustGetEventName(consts.EventNameExecutionStateChanged, offrampABI), + ReadType: evmrelaytypes.Event, }, }, }, - } + }, } // SourceReaderConfig returns a ChainReaderConfig that can be used to read from the onramp. -func SourceReaderConfig() evmrelaytypes.ChainReaderConfig { - return evmrelaytypes.ChainReaderConfig{ - Contracts: map[string]evmrelaytypes.ChainContractReader{ - consts.ContractNameOnRamp: { - ContractABI: onramp.OnRampABI, - ContractPollingFilter: evmrelaytypes.ContractPollingFilter{ - GenericEventNames: []string{ - mustGetEventName(consts.EventNameCCIPSendRequested, onrampABI), - }, +var SourceReaderConfig = evmrelaytypes.ChainReaderConfig{ + Contracts: map[string]evmrelaytypes.ChainContractReader{ + consts.ContractNameOnRamp: { + ContractABI: onramp.OnRampABI, + ContractPollingFilter: evmrelaytypes.ContractPollingFilter{ + GenericEventNames: []string{ + mustGetEventName(consts.EventNameCCIPSendRequested, onrampABI), }, - Configs: map[string]*evmrelaytypes.ChainReaderDefinition{ - // all "{external|public} view" functions in the onramp except for getFee and getPoolBySourceToken are here. - // getFee is not expected to get called offchain and is only called by end-user contracts. - consts.MethodNameGetExpectedNextSequenceNumber: { - ChainSpecificName: mustGetMethodName("getExpectedNextSequenceNumber", onrampABI), - ReadType: evmrelaytypes.Method, - }, - consts.MethodNameOnrampGetStaticConfig: { - ChainSpecificName: mustGetMethodName("getStaticConfig", onrampABI), - ReadType: evmrelaytypes.Method, - }, - consts.MethodNameOnrampGetDynamicConfig: { - ChainSpecificName: mustGetMethodName("getDynamicConfig", onrampABI), - ReadType: evmrelaytypes.Method, - }, - consts.EventNameCCIPSendRequested: { - ChainSpecificName: mustGetEventName(consts.EventNameCCIPSendRequested, onrampABI), - ReadType: evmrelaytypes.Event, - EventDefinitions: &evmrelaytypes.EventDefinitions{ - GenericDataWordNames: map[string]uint8{ - consts.EventAttributeSequenceNumber: 5, - }, + }, + Configs: map[string]*evmrelaytypes.ChainReaderDefinition{ + // all "{external|public} view" functions in the onramp except for getFee and getPoolBySourceToken are here. + // getFee is not expected to get called offchain and is only called by end-user contracts. + consts.MethodNameGetExpectedNextSequenceNumber: { + ChainSpecificName: mustGetMethodName("getExpectedNextSequenceNumber", onrampABI), + ReadType: evmrelaytypes.Method, + }, + consts.MethodNameOnrampGetStaticConfig: { + ChainSpecificName: mustGetMethodName("getStaticConfig", onrampABI), + ReadType: evmrelaytypes.Method, + }, + consts.MethodNameOnrampGetDynamicConfig: { + ChainSpecificName: mustGetMethodName("getDynamicConfig", onrampABI), + ReadType: evmrelaytypes.Method, + }, + consts.EventNameCCIPSendRequested: { + ChainSpecificName: mustGetEventName(consts.EventNameCCIPSendRequested, onrampABI), + ReadType: evmrelaytypes.Event, + EventDefinitions: &evmrelaytypes.EventDefinitions{ + GenericDataWordNames: map[string]uint8{ + consts.EventAttributeSequenceNumber: 5, }, }, }, }, - consts.ContractNamePriceRegistry: { - ContractABI: price_registry.PriceRegistryABI, - Configs: map[string]*evmrelaytypes.ChainReaderDefinition{ - // TODO: update with the consts from https://github.com/smartcontractkit/chainlink-ccip/pull/39 - // in a followup. - "GetStaticConfig": { - ChainSpecificName: mustGetMethodName("getStaticConfig", priceRegistryABI), - ReadType: evmrelaytypes.Method, - }, - "GetDestChainConfig": { - ChainSpecificName: mustGetMethodName("getDestChainConfig", priceRegistryABI), - ReadType: evmrelaytypes.Method, - }, - "GetPremiumMultiplierWeiPerEth": { - ChainSpecificName: mustGetMethodName("getPremiumMultiplierWeiPerEth", priceRegistryABI), - ReadType: evmrelaytypes.Method, - }, - "GetTokenTransferFeeConfig": { - ChainSpecificName: mustGetMethodName("getTokenTransferFeeConfig", priceRegistryABI), - ReadType: evmrelaytypes.Method, - }, - "ProcessMessageArgs": { - ChainSpecificName: mustGetMethodName("processMessageArgs", priceRegistryABI), - ReadType: evmrelaytypes.Method, - }, - "ValidatePoolReturnData": { - ChainSpecificName: mustGetMethodName("validatePoolReturnData", priceRegistryABI), - ReadType: evmrelaytypes.Method, - }, - "GetValidatedTokenPrice": { - ChainSpecificName: mustGetMethodName("getValidatedTokenPrice", priceRegistryABI), - ReadType: evmrelaytypes.Method, - }, - "GetFeeTokens": { - ChainSpecificName: mustGetMethodName("getFeeTokens", priceRegistryABI), - ReadType: evmrelaytypes.Method, - }, + }, + consts.ContractNamePriceRegistry: { + ContractABI: price_registry.PriceRegistryABI, + Configs: map[string]*evmrelaytypes.ChainReaderDefinition{ + // TODO: update with the consts from https://github.com/smartcontractkit/chainlink-ccip/pull/39 + // in a followup. + "GetStaticConfig": { + ChainSpecificName: mustGetMethodName("getStaticConfig", priceRegistryABI), + ReadType: evmrelaytypes.Method, + }, + "GetDestChainConfig": { + ChainSpecificName: mustGetMethodName("getDestChainConfig", priceRegistryABI), + ReadType: evmrelaytypes.Method, + }, + "GetPremiumMultiplierWeiPerEth": { + ChainSpecificName: mustGetMethodName("getPremiumMultiplierWeiPerEth", priceRegistryABI), + ReadType: evmrelaytypes.Method, + }, + "GetTokenTransferFeeConfig": { + ChainSpecificName: mustGetMethodName("getTokenTransferFeeConfig", priceRegistryABI), + ReadType: evmrelaytypes.Method, + }, + "ProcessMessageArgs": { + ChainSpecificName: mustGetMethodName("processMessageArgs", priceRegistryABI), + ReadType: evmrelaytypes.Method, + }, + "ValidatePoolReturnData": { + ChainSpecificName: mustGetMethodName("validatePoolReturnData", priceRegistryABI), + ReadType: evmrelaytypes.Method, + }, + "GetValidatedTokenPrice": { + ChainSpecificName: mustGetMethodName("getValidatedTokenPrice", priceRegistryABI), + ReadType: evmrelaytypes.Method, + }, + "GetFeeTokens": { + ChainSpecificName: mustGetMethodName("getFeeTokens", priceRegistryABI), + ReadType: evmrelaytypes.Method, }, }, }, - } + }, } // HomeChainReaderConfigRaw returns a ChainReaderConfig that can be used to read from the home chain. -func HomeChainReaderConfigRaw() evmrelaytypes.ChainReaderConfig { - return evmrelaytypes.ChainReaderConfig{ - Contracts: map[string]evmrelaytypes.ChainContractReader{ - consts.ContractNameCapabilitiesRegistry: { - ContractABI: kcr.CapabilitiesRegistryABI, - Configs: map[string]*evmrelaytypes.ChainReaderDefinition{ - consts.MethodNameGetCapability: { - ChainSpecificName: mustGetMethodName("getCapability", capabilitiesRegsitryABI), - }, +var HomeChainReaderConfigRaw = evmrelaytypes.ChainReaderConfig{ + Contracts: map[string]evmrelaytypes.ChainContractReader{ + consts.ContractNameCapabilitiesRegistry: { + ContractABI: kcr.CapabilitiesRegistryABI, + Configs: map[string]*evmrelaytypes.ChainReaderDefinition{ + consts.MethodNameGetCapability: { + ChainSpecificName: mustGetMethodName("getCapability", capabilitiesRegsitryABI), }, }, - consts.ContractNameCCIPConfig: { - ContractABI: ccip_config.CCIPConfigABI, - Configs: map[string]*evmrelaytypes.ChainReaderDefinition{ - consts.MethodNameGetAllChainConfigs: { - ChainSpecificName: mustGetMethodName("getAllChainConfigs", ccipConfigABI), - }, - consts.MethodNameGetOCRConfig: { - ChainSpecificName: mustGetMethodName("getOCRConfig", ccipConfigABI), - }, + }, + consts.ContractNameCCIPConfig: { + ContractABI: ccip_config.CCIPConfigABI, + Configs: map[string]*evmrelaytypes.ChainReaderDefinition{ + consts.MethodNameGetAllChainConfigs: { + ChainSpecificName: mustGetMethodName("getAllChainConfigs", ccipConfigABI), + }, + consts.MethodNameGetOCRConfig: { + ChainSpecificName: mustGetMethodName("getOCRConfig", ccipConfigABI), }, }, }, - } + }, } func mustGetEventName(event string, tabi abi.ABI) string { diff --git a/core/capabilities/ccip/delegate.go b/core/capabilities/ccip/delegate.go index afa8152ea5..ef3822c4de 100644 --- a/core/capabilities/ccip/delegate.go +++ b/core/capabilities/ccip/delegate.go @@ -239,7 +239,7 @@ func (d *Delegate) getHomeChainContractReader( homeChain.LogPoller(), homeChain.HeadTracker(), homeChain.Client(), - configsevm.HomeChainReaderConfigRaw(), + configsevm.HomeChainReaderConfigRaw, ) if err != nil { return nil, types.BoundContract{}, fmt.Errorf("failed to create home chain contract reader: %w", err) diff --git a/core/capabilities/ccip/oraclecreator/inprocess.go b/core/capabilities/ccip/oraclecreator/inprocess.go index 266ffc2d77..d705367d57 100644 --- a/core/capabilities/ccip/oraclecreator/inprocess.go +++ b/core/capabilities/ccip/oraclecreator/inprocess.go @@ -178,9 +178,9 @@ func (i *inprocessOracleCreator) CreatePluginOracle(pluginType cctypes.PluginTyp for _, chain := range i.chains.Slice() { var chainReaderConfig evmrelaytypes.ChainReaderConfig if chain.ID().Uint64() == destChainID { - chainReaderConfig = evmconfig.DestReaderConfig() + chainReaderConfig = evmconfig.DestReaderConfig } else { - chainReaderConfig = evmconfig.SourceReaderConfig() + chainReaderConfig = evmconfig.SourceReaderConfig } cr, err2 := evm.NewChainReaderService( context.Background(),