Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Check node address is valid #565

Merged
merged 8 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion network/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"time"

"github.com/multiformats/go-multiaddr"
"github.com/pactus-project/pactus/util/errors"
)

Expand Down Expand Up @@ -61,12 +62,25 @@ func DefaultConfig() *Config {
}
}

func validateAddresses(address []string) error {
for _, addr := range address {
_, err := multiaddr.NewMultiaddr(addr)
if err != nil {
return err
}
}
return nil
}

// SanityCheck performs basic checks on the configuration.
func (conf *Config) SanityCheck() error {
if conf.EnableRelay {
if len(conf.RelayAddrs) == 0 {
return errors.Errorf(errors.ErrInvalidConfig, "at least one relay address should be defined")
}
}
return nil
if err := validateAddresses(conf.RelayAddrs); err != nil {
kehiy marked this conversation as resolved.
Show resolved Hide resolved
return err
}
return validateAddresses(conf.Listens)
}
22 changes: 20 additions & 2 deletions network/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,30 @@ import (
"github.com/stretchr/testify/assert"
)

func TestConf(t *testing.T) {
func TestDefaultConfigCheck(t *testing.T) {
conf := DefaultConfig()

conf.EnableRelay = true
assert.Error(t, conf.SanityCheck())

conf.EnableRelay = false
conf.Listens = []string{""}
assert.Error(t, conf.SanityCheck())

conf.Listens = []string{"127.0.0.1"}
assert.Error(t, conf.SanityCheck())

conf.Listens = []string{"/ip4"}
assert.Error(t, conf.SanityCheck())

conf.RelayAddrs = []string{"/ip4"}
assert.Error(t, conf.SanityCheck())

conf.RelayAddrs = []string{}
conf.Listens = []string{}

conf.RelayAddrs = []string{"/ip4/127.0.0.1"}
assert.NoError(t, conf.SanityCheck())

conf.Listens = []string{"/ip4/127.0.0.1"}
assert.NoError(t, conf.SanityCheck())
}