Skip to content

Commit

Permalink
feat: match configs by domainID
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 committed Oct 9, 2024
1 parent eed1e97 commit 6baf516
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 9 deletions.
49 changes: 43 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,23 @@ func processRawConfig(rawConfig RawConfig, config *Config) (*Config, error) {

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)
}

Expand All @@ -118,6 +125,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"
Expand Down
81 changes: 78 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}}})
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}}})
Expand All @@ -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{
Expand All @@ -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,
},
Expand Down Expand Up @@ -241,6 +253,7 @@ func (s *GetConfigTestSuite) Test_SharedConfigLengthMismatch() {
"type": "evm",
"bridge": "0xd606A00c1A39dA53EA7Bb3Ab570BBE40b156EB66",
"erc721Handler": "0x75dF75bcdCa8eA2360c562b4aaDBAF3dfAf5b19b",
"gasLimit": 300,
"maxGasPrice": 2e+10,
"from": "0xff93B45308FD417dF303D6515aB04D9e89a750Ca",
"name": "evm2",
Expand All @@ -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
Expand All @@ -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{
Expand Down Expand Up @@ -319,6 +381,7 @@ func (s *GetConfigTestSuite) Test_GetConfigFromFile() {
},
},
ChainConfigs: []map[string]interface{}{{
"id": float64(1),
"type": "evm",
"name": "chain1",
}},
Expand Down Expand Up @@ -354,6 +417,7 @@ func (s *GetConfigTestSuite) Test_GetConfigFromFile() {
},
},
ChainConfigs: []map[string]interface{}{{
"id": float64(1),
"type": "evm",
"name": "chain1",
}},
Expand All @@ -380,6 +444,7 @@ func (s *GetConfigTestSuite) Test_GetConfigFromFile() {
},

ChainConfigs: []map[string]interface{}{{
"id": float64(1),
"type": "evm",
"name": "chain1",
}},
Expand Down Expand Up @@ -410,6 +475,7 @@ func (s *GetConfigTestSuite) Test_GetConfigFromFile() {
},
},
ChainConfigs: []map[string]interface{}{{
"id": float64(1),
"type": "evm",
"name": "evm1",
}},
Expand Down Expand Up @@ -446,6 +512,7 @@ func (s *GetConfigTestSuite) Test_GetConfigFromFile() {
},
},
ChainConfigs: []map[string]interface{}{{
"id": float64(1),
"type": "evm",
"name": "evm1",
}},
Expand Down Expand Up @@ -484,6 +551,7 @@ func (s *GetConfigTestSuite) Test_GetConfigFromFile() {
},
},
ChainConfigs: []map[string]interface{}{{
"id": float64(1),
"type": "evm",
"name": "evm1",
}},
Expand Down Expand Up @@ -522,6 +590,7 @@ func (s *GetConfigTestSuite) Test_GetConfigFromFile() {
},
},
ChainConfigs: []map[string]interface{}{{
"id": float64(1),
"type": "evm",
"name": "evm1",
}},
Expand All @@ -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")

Expand Down

0 comments on commit 6baf516

Please sign in to comment.