Skip to content

Commit

Permalink
Add function to translate addresses in configuration
Browse files Browse the repository at this point in the history
Mostly for the use in tests
  • Loading branch information
tzdybal committed Apr 21, 2021
1 parent 404d4ea commit 09a7bbf
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
25 changes: 25 additions & 0 deletions conv/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,34 @@ package conv
import (
"strings"

"github.com/lazyledger/optimint/config"
"github.com/multiformats/go-multiaddr"
)

func TranslateAddresses(conf *config.NodeConfig) error {
if conf.P2P.ListenAddress != "" {
addr, err := GetMultiAddr(conf.P2P.ListenAddress)
if err != nil {
return err
}
conf.P2P.ListenAddress = addr.String()
}

seeds := strings.Split(conf.P2P.Seeds, ",")
for i, seed := range seeds {
if seed != "" {
addr, err := GetMultiAddr(seed)
if err != nil {
return err
}
seeds[i] = addr.String()
}
}
conf.P2P.Seeds = strings.Join(seeds, ",")

return nil
}

func GetMultiAddr(addr string) (multiaddr.Multiaddr, error) {
var err error
var p2pId multiaddr.Multiaddr
Expand Down
58 changes: 58 additions & 0 deletions conv/addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,66 @@ import (

"github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/assert"

"github.com/lazyledger/optimint/config"
)

func TestTranslateAddresses(t *testing.T) {
t.Parallel()

invalidCosmos := "foobar"
validCosmos := "127.0.0.1:1234"
validOptimint := "/ip4/127.0.0.1/tcp/1234"

cases := []struct {
name string
input config.NodeConfig
expected config.NodeConfig
expectedErr string
}{
{"empty", config.NodeConfig{}, config.NodeConfig{}, ""},
{
"valid listen address",
config.NodeConfig{P2P: config.P2PConfig{ListenAddress: validCosmos}},
config.NodeConfig{P2P: config.P2PConfig{ListenAddress: validOptimint}},
"",
},
{
"valid seed address",
config.NodeConfig{P2P: config.P2PConfig{Seeds: validCosmos + "," + validCosmos}},
config.NodeConfig{P2P: config.P2PConfig{Seeds: validOptimint + "," + validOptimint}},
"",
},
{
"invalid listen address",
config.NodeConfig{P2P: config.P2PConfig{ListenAddress: invalidCosmos}},
config.NodeConfig{},
ErrInvalidAddress.Error(),
},
{
"invalid seed address",
config.NodeConfig{P2P: config.P2PConfig{Seeds: validCosmos + "," + invalidCosmos}},
config.NodeConfig{},
ErrInvalidAddress.Error(),
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
assert := assert.New(t)
// c.input is changed in place
err := TranslateAddresses(&c.input)
if c.expectedErr != "" {
assert.Error(err)
assert.True(strings.HasPrefix(err.Error(), c.expectedErr), "invalid error message")
} else {
assert.NoError(err)
assert.Equal(c.expected, c.input)
}
})
}
}

func TestGetMultiaddr(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 09a7bbf

Please sign in to comment.