From f6958751cf3838aedca255d8469525fa3056f2ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20Petruni=C4=87?= Date: Wed, 23 Oct 2024 11:12:00 +0200 Subject: [PATCH] feat: match configs by domainID (#375) --- app/app.go | 2 +- config/config.go | 55 +++++++++++++++++++++++++---- config/config_test.go | 81 +++++++++++++++++++++++++++++++++++++++++-- example/app/app.go | 3 +- 4 files changed, 129 insertions(+), 12 deletions(-) diff --git a/app/app.go b/app/app.go index 2a66468f..4b076768 100644 --- a/app/app.go +++ b/app/app.go @@ -76,7 +76,7 @@ func Run() error { configFlag := viper.GetString(config.ConfigFlagName) configURL := viper.GetString("config-url") - configuration := &config.Config{} + var configuration *config.Config if configURL != "" { configuration, err = config.GetSharedConfigFromNetwork(configURL, configuration) panicOnError(err) diff --git a/config/config.go b/config/config.go index e7d3c576..a5597115 100644 --- a/config/config.go +++ b/config/config.go @@ -97,19 +97,32 @@ func processRawConfig(rawConfig RawConfig, config *Config) (*Config, error) { if err != nil { return config, err } + if config == nil { + config := &Config{} + config.RelayerConfig = relayerConfig + config.ChainConfigs = rawConfig.ChainConfigs + return config, nil + } chainConfigs := make([]map[string]interface{}, 0) for i, chain := range rawConfig.ChainConfigs { - if i < len(config.ChainConfigs) { - err := mergo.Merge(&chain, config.ChainConfigs[i]) - if err != nil { - return config, err - } + if chain["id"] == 0 || chain["id"] == nil { + return config, fmt.Errorf("chain 'id' not configured for chain %d", i) } - if chain["type"] == "" || chain["type"] == nil { return config, fmt.Errorf("chain 'type' must be provided for every configured chain") } + + chainConfig, err := findChainConfig(chain["id"], config.ChainConfigs) + if err != nil { + return config, err + } + + err = mergo.Merge(&chain, chainConfig) + if err != nil { + return config, err + } + chainConfigs = append(chainConfigs, chain) } @@ -118,6 +131,36 @@ func processRawConfig(rawConfig RawConfig, config *Config) (*Config, error) { return config, nil } +func findChainConfig(id interface{}, configs []map[string]interface{}) (interface{}, error) { + for _, config := range configs { + if compareDomainID(id, config["id"]) { + return config, nil + } + } + + return nil, fmt.Errorf("missing chain %v", id) +} + +func compareDomainID(a, b interface{}) bool { + switch a := a.(type) { + case int: + switch b := b.(type) { + case int: + return a == b + case float64: + return float64(a) == b + } + case float64: + switch b := b.(type) { + case int: + return a == float64(b) + case float64: + return a == b + } + } + return false +} + var ( // Flags for running the app ConfigFlagName = "config" diff --git a/config/config_test.go b/config/config_test.go index c9dcd8de..f615bc29 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -74,9 +74,11 @@ func (s *GetConfigTestSuite) Test_GetConfigFromENV() { // load from Env cnf, err := config.GetConfigFromENV(&config.Config{ChainConfigs: []map[string]interface{}{{ + "id": 1, "blockConfirmations": 5, "gasLimit": 500, }, { + "id": 2, "blockConfirmations": 3, "gasLimit": 300, }}}) @@ -147,7 +149,7 @@ func (s *GetConfigTestSuite) Test_GetConfigFromENV() { }, *cnf) } -func (s *GetConfigTestSuite) Test_SharedConfigLengthMismatch() { +func (s *GetConfigTestSuite) Test_GetConfigFromENV_RandomOrder() { _ = os.Setenv("SYG_CHAINS", `[ { "id":1, @@ -179,14 +181,21 @@ func (s *GetConfigTestSuite) Test_SharedConfigLengthMismatch() { _ = os.Setenv("SYG_RELAYER_MPCCONFIG_KEY", "test-pk") _ = os.Setenv("SYG_RELAYER_MPCCONFIG_KEYSHAREPATH", "/cfg/keyshares/0.keyshare") + _ = os.Setenv("SYG_RELAYER_MPCCONFIG_FROSTKEYSHAREPATH", "/cfg/keyshares/0-frost.keyshare") _ = os.Setenv("SYG_RELAYER_MPCCONFIG_PORT", "9000") - _ = os.Setenv("SYG_RELAYER_MPCCONFIG_TOPOLOGYCONFIGURATION_ENCRYPTIONKEY", "test-enc-key") _ = os.Setenv("SYG_RELAYER_MPCCONFIG_TOPOLOGYCONFIGURATION_URL", "http://test.com") _ = os.Setenv("SYG_RELAYER_MPCCONFIG_TOPOLOGYCONFIGURATION_PATH", "path") + _ = os.Setenv("SYG_RELAYER_ENV", "TEST") + _ = os.Setenv("SYG_RELAYER_ID", "123") // load from Env cnf, err := config.GetConfigFromENV(&config.Config{ChainConfigs: []map[string]interface{}{{ + "id": 2, + "blockConfirmations": 3, + "gasLimit": 300, + }, { + "id": 1, "blockConfirmations": 5, "gasLimit": 500, }}}) @@ -197,6 +206,8 @@ func (s *GetConfigTestSuite) Test_SharedConfigLengthMismatch() { RelayerConfig: relayer.RelayerConfig{ LogLevel: 1, LogFile: "out.log", + Env: "TEST", + Id: "123", HealthPort: 9001, MpcConfig: relayer.MpcRelayerConfig{ TopologyConfiguration: relayer.TopologyConfiguration{ @@ -206,6 +217,7 @@ func (s *GetConfigTestSuite) Test_SharedConfigLengthMismatch() { }, Port: 9000, KeysharePath: "/cfg/keyshares/0.keyshare", + FrostKeysharePath: "/cfg/keyshares/0-frost.keyshare", Key: "test-pk", CommHealthCheckInterval: 5 * time.Minute, }, @@ -241,6 +253,7 @@ func (s *GetConfigTestSuite) Test_SharedConfigLengthMismatch() { "type": "evm", "bridge": "0xd606A00c1A39dA53EA7Bb3Ab570BBE40b156EB66", "erc721Handler": "0x75dF75bcdCa8eA2360c562b4aaDBAF3dfAf5b19b", + "gasLimit": 300, "maxGasPrice": 2e+10, "from": "0xff93B45308FD417dF303D6515aB04D9e89a750Ca", "name": "evm2", @@ -253,6 +266,54 @@ func (s *GetConfigTestSuite) Test_SharedConfigLengthMismatch() { }, *cnf) } +func (s *GetConfigTestSuite) Test_SharedConfigLengthMismatch() { + _ = os.Setenv("SYG_CHAINS", `[ + { + "id":1, + "from":"0xff93B45308FD417dF303D6515aB04D9e89a750Ca", + "name":"evm1", + "type":"evm", + "endpoint":"ws://evm1-1:8546", + "bridge":"0xd606A00c1A39dA53EA7Bb3Ab570BBE40b156EB66", + "erc20Handler":"0x3cA3808176Ad060Ad80c4e08F30d85973Ef1d99e", + "erc721Handler":"0x75dF75bcdCa8eA2360c562b4aaDBAF3dfAf5b19b", + "genericHandler":"0xe1588E2c6a002AE93AeD325A910Ed30961874109", + "maxGasPrice":20000000000, + "blockConfirmations":2 + }, + { + "id":2, + "from":"0xff93B45308FD417dF303D6515aB04D9e89a750Ca", + "name":"evm2", + "type":"evm", + "endpoint":"ws://evm2-1:8546", + "bridge":"0xd606A00c1A39dA53EA7Bb3Ab570BBE40b156EB66", + "erc20Handler":"0x3cA3808176Ad060Ad80c4e08F30d85973Ef1d99e", + "erc721Handler":"0x75dF75bcdCa8eA2360c562b4aaDBAF3dfAf5b19b", + "genericHandler":"0xe1588E2c6a002AE93AeD325A910Ed30961874109", + "maxGasPrice":20000000000, + "blockConfirmations":2 +} +]`) + + _ = os.Setenv("SYG_RELAYER_MPCCONFIG_KEY", "test-pk") + _ = os.Setenv("SYG_RELAYER_MPCCONFIG_KEYSHAREPATH", "/cfg/keyshares/0.keyshare") + _ = os.Setenv("SYG_RELAYER_MPCCONFIG_PORT", "9000") + + _ = os.Setenv("SYG_RELAYER_MPCCONFIG_TOPOLOGYCONFIGURATION_ENCRYPTIONKEY", "test-enc-key") + _ = os.Setenv("SYG_RELAYER_MPCCONFIG_TOPOLOGYCONFIGURATION_URL", "http://test.com") + _ = os.Setenv("SYG_RELAYER_MPCCONFIG_TOPOLOGYCONFIGURATION_PATH", "path") + + // load from Env + _, err := config.GetConfigFromENV(&config.Config{ChainConfigs: []map[string]interface{}{{ + "id": 1, + "blockConfirmations": 5, + "gasLimit": 500, + }}}) + + s.NotNil(err) +} + type ConfigTestCase struct { name string inConfig config.RawConfig @@ -267,6 +328,7 @@ func (s *GetConfigTestSuite) Test_GetConfigFromFile() { name: "missing chain type", inConfig: config.RawConfig{ ChainConfigs: []map[string]interface{}{{ + "id": float64(1), "name": "chain1", }}, RelayerConfig: relayer.RawRelayerConfig{ @@ -319,6 +381,7 @@ func (s *GetConfigTestSuite) Test_GetConfigFromFile() { }, }, ChainConfigs: []map[string]interface{}{{ + "id": float64(1), "type": "evm", "name": "chain1", }}, @@ -354,6 +417,7 @@ func (s *GetConfigTestSuite) Test_GetConfigFromFile() { }, }, ChainConfigs: []map[string]interface{}{{ + "id": float64(1), "type": "evm", "name": "chain1", }}, @@ -380,6 +444,7 @@ func (s *GetConfigTestSuite) Test_GetConfigFromFile() { }, ChainConfigs: []map[string]interface{}{{ + "id": float64(1), "type": "evm", "name": "chain1", }}, @@ -410,6 +475,7 @@ func (s *GetConfigTestSuite) Test_GetConfigFromFile() { }, }, ChainConfigs: []map[string]interface{}{{ + "id": float64(1), "type": "evm", "name": "evm1", }}, @@ -446,6 +512,7 @@ func (s *GetConfigTestSuite) Test_GetConfigFromFile() { }, }, ChainConfigs: []map[string]interface{}{{ + "id": float64(1), "type": "evm", "name": "evm1", }}, @@ -484,6 +551,7 @@ func (s *GetConfigTestSuite) Test_GetConfigFromFile() { }, }, ChainConfigs: []map[string]interface{}{{ + "id": float64(1), "type": "evm", "name": "evm1", }}, @@ -522,6 +590,7 @@ func (s *GetConfigTestSuite) Test_GetConfigFromFile() { }, }, ChainConfigs: []map[string]interface{}{{ + "id": float64(1), "type": "evm", "name": "evm1", }}, @@ -534,7 +603,13 @@ func (s *GetConfigTestSuite) Test_GetConfigFromFile() { file, _ := json.Marshal(t.inConfig) _ = os.WriteFile("test.json", file, 0644) - conf, err := config.GetConfigFromFile("test.json", &config.Config{}) + conf, err := config.GetConfigFromFile("test.json", &config.Config{ + ChainConfigs: []map[string]interface{}{ + { + "id": 1, + }, + }, + }) _ = os.Remove("test.json") diff --git a/example/app/app.go b/example/app/app.go index e6743c5f..8bca26dd 100644 --- a/example/app/app.go +++ b/example/app/app.go @@ -67,8 +67,7 @@ import ( ) func Run() error { - configuration := &config.Config{} - configuration, err := config.GetConfigFromFile(viper.GetString(config.ConfigFlagName), configuration) + configuration, err := config.GetConfigFromFile(viper.GetString(config.ConfigFlagName), nil) if err != nil { panic(err) }