Skip to content

Commit

Permalink
fix: fix topology resolution (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 authored Jan 30, 2023
1 parent 2957ab0 commit fefefbe
Show file tree
Hide file tree
Showing 9 changed files with 305 additions and 33 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ genmocks:
mockgen -source=./chains/evm/calls/events/listener.go -destination=./chains/evm/calls/events/mock/listener.go
mockgen -source=./chains/substrate/events/handlers.go -destination=./chains/substrate/events/mock/handlers.go
mockgen -destination=chains/evm/listener/mock/core/listener.go github.com/ChainSafe/chainbridge-core/chains/evm/listener EventListener,DepositHandler
mockgen -source=./topology/topology.go -destination=./topology/mock/topology.go


e2e-test:
Expand Down
5 changes: 3 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"math/big"
"net/http"
"os"
"os/signal"
"strings"
Expand Down Expand Up @@ -69,8 +70,8 @@ func Run() error {
logger.ConfigureLogger(configuration.RelayerConfig.LogLevel, os.Stdout)

log.Info().Msg("Successfully loaded configuration")

topologyProvider, err := topology.NewNetworkTopologyProvider(configuration.RelayerConfig.MpcConfig.TopologyConfiguration)
topologyProvider, err := topology.NewNetworkTopologyProvider(configuration.RelayerConfig.MpcConfig.TopologyConfiguration, http.DefaultClient)
panicOnError(err)
topologyStore := topology.NewTopologyStore(configuration.RelayerConfig.MpcConfig.TopologyConfiguration.Path)
networkTopology, err := topologyStore.Topology()
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func processRawConfig(rawConfig RawConfig, config *Config) (*Config, error) {
}

for i, chain := range rawConfig.ChainConfigs {
if len(config.ChainConfigs) < len(rawConfig.ChainConfigs) {
if i >= len(config.ChainConfigs) {
config.ChainConfigs = append(config.ChainConfigs, chain)
} else {
err := mergo.Merge(&chain, config.ChainConfigs[i])
Expand Down
77 changes: 77 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,83 @@ func (s *GetConfigTestSuite) Test_GetConfigFromENV() {
}, *cnf)
}

func (s *GetConfigTestSuite) Test_SharedConfigLengthMismatch() {
_ = os.Setenv("SYG_DOM_1", "{\n \"id\": 1,\n \"from\": \"0xff93B45308FD417dF303D6515aB04D9e89a750Ca\",\n \"name\": \"evm1\",\n \"type\": \"evm\",\n \"endpoint\": \"ws://evm1-1:8546\",\n \"bridge\": \"0xd606A00c1A39dA53EA7Bb3Ab570BBE40b156EB66\",\n \"erc20Handler\": \"0x3cA3808176Ad060Ad80c4e08F30d85973Ef1d99e\",\n \"erc721Handler\": \"0x75dF75bcdCa8eA2360c562b4aaDBAF3dfAf5b19b\",\n \"genericHandler\": \"0xe1588E2c6a002AE93AeD325A910Ed30961874109\",\n \"maxGasPrice\": 20000000000,\n \"blockConfirmations\": 2\n }")
_ = os.Setenv("SYG_DOM_2", "{\n \"id\": 2,\n \"from\": \"0xff93B45308FD417dF303D6515aB04D9e89a750Ca\",\n \"name\": \"evm2\",\n \"type\": \"evm\",\n \"endpoint\": \"ws://evm2-1:8546\",\n \"bridge\": \"0xd606A00c1A39dA53EA7Bb3Ab570BBE40b156EB66\",\n \"erc20Handler\": \"0x3cA3808176Ad060Ad80c4e08F30d85973Ef1d99e\",\n \"erc721Handler\": \"0x75dF75bcdCa8eA2360c562b4aaDBAF3dfAf5b19b\",\n \"genericHandler\": \"0xe1588E2c6a002AE93AeD325A910Ed30961874109\",\n \"maxGasPrice\": 20000000000,\n \"blockConfirmations\": 2\n }")

_ = 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
cnf, err := config.GetConfigFromENV(&config.Config{ChainConfigs: []map[string]interface{}{{
"blockConfirmations": 5,
"gasLimit": 500,
}}})

s.Nil(err)

s.Equal(config.Config{
RelayerConfig: relayer.RelayerConfig{
RelayerConfig: coreRelayer.RelayerConfig{
LogLevel: 1,
LogFile: "out.log",
},
HealthPort: 9001,
MpcConfig: relayer.MpcRelayerConfig{
TopologyConfiguration: relayer.TopologyConfiguration{
EncryptionKey: "test-enc-key",
Url: "http://test.com",
Path: "path",
},
Port: 9000,
KeysharePath: "/cfg/keyshares/0.keyshare",
Key: "test-pk",
},
BullyConfig: relayer.BullyConfig{
PingWaitTime: 1 * time.Second,
PingBackOff: 1 * time.Second,
PingInterval: 1 * time.Second,
ElectionWaitTime: 2 * time.Second,
BullyWaitTime: 25 * time.Second,
},
},
ChainConfigs: []map[string]interface{}{
{
"id": float64(1),
"type": "evm",
"bridge": "0xd606A00c1A39dA53EA7Bb3Ab570BBE40b156EB66",
"erc721Handler": "0x75dF75bcdCa8eA2360c562b4aaDBAF3dfAf5b19b",
"gasLimit": 500,
"maxGasPrice": 2e+10,
"from": "0xff93B45308FD417dF303D6515aB04D9e89a750Ca",
"name": "evm1",
"endpoint": "ws://evm1-1:8546",
"erc20Handler": "0x3cA3808176Ad060Ad80c4e08F30d85973Ef1d99e",
"genericHandler": "0xe1588E2c6a002AE93AeD325A910Ed30961874109",
"blockConfirmations": float64(2),
},
{
"id": float64(2),
"type": "evm",
"bridge": "0xd606A00c1A39dA53EA7Bb3Ab570BBE40b156EB66",
"erc721Handler": "0x75dF75bcdCa8eA2360c562b4aaDBAF3dfAf5b19b",
"maxGasPrice": 2e+10,
"from": "0xff93B45308FD417dF303D6515aB04D9e89a750Ca",
"name": "evm2",
"endpoint": "ws://evm2-1:8546",
"erc20Handler": "0x3cA3808176Ad060Ad80c4e08F30d85973Ef1d99e",
"genericHandler": "0xe1588E2c6a002AE93AeD325A910Ed30961874109",
"blockConfirmations": float64(2),
},
},
}, *cnf)
}

type ConfigTestCase struct {
name string
inConfig config.RawConfig
Expand Down
8 changes: 4 additions & 4 deletions topology/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ func NewAESEncryption(key []byte) (*AESEncryption, error) {
}

func (ae *AESEncryption) Decrypt(data string) []byte {
bytes, _ := hex.DecodeString(data)
iv := bytes[:aes.BlockSize]
bytes = bytes[aes.BlockSize:]
stream := cipher.NewCTR(ae.block, iv)
iv := data[:aes.BlockSize]
bytes, _ := hex.DecodeString(data[aes.BlockSize:])

stream := cipher.NewCTR(ae.block, []byte(iv))
dst := make([]byte, len(bytes))
stream.XORKeyStream(dst, bytes)
return dst
Expand Down
9 changes: 5 additions & 4 deletions topology/encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@ func TestRunAESEncryptionTestSuite(t *testing.T) {
}

func (s *AESEncryptionTestSuite) SetupTest() {
cipherKey := []byte("asuperstrong32bitpasswordgohere!")
cipherKey := []byte("v8y/B?E(H+MbQeTh")
s.aesEncryption, _ = topology.NewAESEncryption(cipherKey)
}

func (s *AESEncryptionTestSuite) Test_Decryption() {
expectedTopology := topology.RawTopology{
Peers: []topology.RawPeer{
{PeerAddress: "peerAddress1"},
{PeerAddress: "peerAddress2"},
{PeerAddress: "/dns4/relayer2/tcp/9001/p2p/QmeTuMtdpPB7zKDgmobEwSvxodrf5aFVSmBXX3SQJVjJaT"},
{PeerAddress: "/dns4/relayer3/tcp/9002/p2p/QmYAYuLUPNwYEBYJaKHcE7NKjUhiUV8txx2xDXHvcYa1xK"},
{PeerAddress: "/dns4/relayer1/tcp/9000/p2p/QmcvEg7jGvuxdsUFRUiE4VdrL2P1Yeju5L83BsJvvXz7zX"},
},
Threshold: "2",
}

encryptedData := "313233343536373831323334353637389343bf912f861f9fd58c1bb5ef6236a5e41a4a3f5698c7f01859a38f90c2d943092158bb485f5cc3653aa8dbf254fea4a0d15bc48c70565aed8057d5a1e2f4de999163b035ec30ce989295535197b6fed02b9584962793058b"
encryptedData := "12345678123456786c49ea9bdd0d37f3ad3d266b4ef5d6ef243027b25bd5092091bcd26488e185c4f466c6795535593dc41b03fcd8997985a78bc784c9f561bac89683a0170e5632ec0fc9237a97ebe38f783067d7f0d19dfe708349ca10759e6091228de7899ee10d679c8b444132bfd8106e1d28e944facb21be60182b7c069f264244ab545871ee6d15a1f070cacada34647bc7d2404384b3ee54b9058ec14ae9e017610f392adeb05d33d524e10043908887d932e5a974c8200639c0dc8d77e1cfb65ecbd2f9c731c61212d1a928b5436f3540cfbd981070b5567ced664ef20cc795ebb792231df08f05987a5d9458664d34666995fb15a969440dfd28db35fbd79f9e11cbcfd42409259c4bb1006c0907d2d4b170698e90452ead9ab7f4e41309fe8c586ccee54cc9cfaf3ac22d00b6c6f583a3f7a1fe3ddd470aa12ad9cf63f072798dadb5a21004529fec4a5914d68a18fd0b3fc33079d4ff09af44416b732f024b75b40dd0"
decryptedData := s.aesEncryption.Decrypt(encryptedData)

decryptedTopology := topology.RawTopology{}
Expand Down
126 changes: 126 additions & 0 deletions topology/mock/topology.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 27 additions & 21 deletions topology/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,42 +39,47 @@ func (nt NetworkTopology) IsAllowedPeer(peer peer.ID) bool {
return false
}

type RawTopology struct {
Peers []RawPeer `mapstructure:"Peers" json:"peers"`
Threshold string `mapstructure:"Threshold" json:"threshold"`
}

type RawPeer struct {
PeerAddress string `mapstructure:"PeerAddress" json:"peerAddress"`
}
type Fetcher interface {
Get(url string) (*http.Response, error)
}

type Decrypter interface {
Decrypt(data string) []byte
}

type NetworkTopologyProvider interface {
NetworkTopology() (NetworkTopology, error)
}

func NewNetworkTopologyProvider(config relayer.TopologyConfiguration) (NetworkTopologyProvider, error) {
func NewNetworkTopologyProvider(config relayer.TopologyConfiguration, fetcher Fetcher) (NetworkTopologyProvider, error) {
decrypter, err := NewAESEncryption([]byte(config.EncryptionKey))
if err != nil {
return nil, err
}

return &topologyProvider{
return &TopologyProvider{
decrypter: decrypter,
url: config.Url,
fetcher: fetcher,
}, nil
}

type RawTopology struct {
Peers []RawPeer `mapstructure:"Peers" json:"peers"`
Threshold string `mapstructure:"Threshold" json:"threshold"`
}

type RawPeer struct {
PeerAddress string `mapstructure:"PeerAddress" json:"peerAddress"`
}

type Decrypter interface {
Decrypt(data string) []byte
}

type topologyProvider struct {
type TopologyProvider struct {
url string
decrypter Decrypter
fetcher Fetcher
}

func (t *topologyProvider) NetworkTopology() (NetworkTopology, error) {
resp, err := http.Get(t.url)
func (t *TopologyProvider) NetworkTopology() (NetworkTopology, error) {
resp, err := t.fetcher.Get(t.url)
if err != nil {
return NetworkTopology{}, err
}
Expand All @@ -84,8 +89,9 @@ func (t *topologyProvider) NetworkTopology() (NetworkTopology, error) {
return NetworkTopology{}, err
}

unecryptedBody := t.decrypter.Decrypt(string(body))
rawTopology := &RawTopology{}
err = json.Unmarshal(body, rawTopology)
err = json.Unmarshal(unecryptedBody, rawTopology)
if err != nil {
return NetworkTopology{}, err
}
Expand All @@ -107,8 +113,8 @@ func ProcessRawTopology(rawTopology *RawTopology) (NetworkTopology, error) {
if err != nil {
return NetworkTopology{}, fmt.Errorf("unable to parse mpc threshold from topology %v", err)
}
if threshold <= 1 {
return NetworkTopology{}, fmt.Errorf("mpc threshold must be bigger then 1 %v", err)
if threshold < 1 {
return NetworkTopology{}, fmt.Errorf("mpc threshold must be bigger then 0 %v", err)
}
return NetworkTopology{Peers: peers, Threshold: int(threshold)}, nil
}
Loading

0 comments on commit fefefbe

Please sign in to comment.