Skip to content

Commit

Permalink
Initialize reader configs when module loads. (#1353)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
winder authored Aug 23, 2024
1 parent e9e71f5 commit bbc0554
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
270 changes: 132 additions & 138 deletions core/capabilities/ccip/configs/evm/contract_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion core/capabilities/ccip/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions core/capabilities/ccip/oraclecreator/inprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit bbc0554

Please sign in to comment.