Skip to content

Commit

Permalink
Use ListenAddress in the P2P config rather than Ip and Port
Browse files Browse the repository at this point in the history
  • Loading branch information
charithabandi committed Jan 8, 2025
1 parent d16aa5c commit 69b8150
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
7 changes: 4 additions & 3 deletions app/setup/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,12 @@ func GenerateNodeRoot(ncfg *NodeGenConfig) error {
cfg := custom.DefaultConfig() // not config.DefaultConfig(), so custom command config is used

// P2P
cfg.P2P.Port = uint64(ncfg.PortOffset + 6600)
cfg.P2P.IP = "127.0.0.1"
port := uint64(ncfg.PortOffset + 6600)
host := "127.0.0.1"
if ncfg.IP != "" {
cfg.P2P.IP = ncfg.IP
host = ncfg.IP
}
cfg.P2P.ListenAddress = net.JoinHostPort(host, strconv.FormatUint(port, 10))
cfg.P2P.Pex = !ncfg.NoPEX

cfg.P2P.BootNodes = ncfg.BootNodes
Expand Down
18 changes: 8 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,9 @@ func DefaultConfig() *Config {
LogFormat: log.FormatUnstructured,
LogOutput: []string{"stdout", "kwild.log"},
P2P: PeerConfig{
IP: "0.0.0.0",
Port: 6600,
Pex: true,
BootNodes: []string{},
ListenAddress: "0.0.0.0:6600",
Pex: true,
BootNodes: []string{},
},
Consensus: ConsensusConfig{
ProposeTimeout: Duration(1000 * time.Millisecond),
Expand Down Expand Up @@ -237,12 +236,11 @@ type Config struct {

// PeerConfig corresponds to the [p2p] section of the config.
type PeerConfig struct {
IP string `toml:"ip" comment:"IP address to listen on for P2P connections"`
Port uint64 `toml:"port" comment:"port to listen on for P2P connections"`
Pex bool `toml:"pex" comment:"enable peer exchange"`
BootNodes []string `toml:"bootnodes" comment:"bootnodes to connect to on startup"`
PrivateMode bool `toml:"private" comment:"operate in private mode using a node ID whitelist"`
Whitelist []string `toml:"whitelist" comment:"allowed node IDs when in private mode"`
ListenAddress string `toml:"listen" comment:"address in host:port format to listen on for P2P connections"`
Pex bool `toml:"pex" comment:"enable peer exchange"`
BootNodes []string `toml:"bootnodes" comment:"bootnodes to connect to on startup"`
PrivateMode bool `toml:"private" comment:"operate in private mode using a node ID whitelist"`
Whitelist []string `toml:"whitelist" comment:"allowed node IDs when in private mode"`
}

type DBConfig struct {
Expand Down
14 changes: 5 additions & 9 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ func TestConfigSaveAndLoad(t *testing.T) {
LogLevel: log.LevelDebug,
LogFormat: log.FormatJSON,
P2P: PeerConfig{
IP: "192.168.1.1",
Port: 8080,
Pex: false,
BootNodes: []string{"/ip4/192.168.1.1/tcp/8080/p2p/test"},
ListenAddress: "0.0.0.0:6600",
Pex: false,
BootNodes: []string{"/ip4/192.168.1.1/tcp/8080/p2p/test"},
},
DB: DBConfig{
Host: "127.0.0.1",
Expand Down Expand Up @@ -98,11 +97,8 @@ func TestConfigSaveAndLoad(t *testing.T) {
if loaded.LogFormat != tt.config.LogFormat {
t.Errorf("LogFormat mismatch: got %v, want %v", loaded.LogFormat, tt.config.LogFormat)
}
if loaded.P2P.IP != tt.config.P2P.IP {
t.Errorf("P2P.IP mismatch: got %v, want %v", loaded.P2P.IP, tt.config.P2P.IP)
}
if loaded.P2P.Port != tt.config.P2P.Port {
t.Errorf("P2P.Port mismatch: got %v, want %v", loaded.P2P.Port, tt.config.P2P.Port)
if loaded.P2P.ListenAddress != tt.config.P2P.ListenAddress {
t.Errorf("P2P.ListenAddress mismatch: got %v, want %v", loaded.P2P.ListenAddress, tt.config.P2P.ListenAddress)
}
if loaded.P2P.Pex != tt.config.P2P.Pex {
t.Errorf("P2P.Pex mismatch: got %v, want %v", loaded.P2P.Pex, tt.config.P2P.Pex)
Expand Down
3 changes: 2 additions & 1 deletion node/engine/parse/gen/kuneiform_lexer.go

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

10 changes: 7 additions & 3 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ func NewNode(cfg *Config, opts ...Option) (*Node, error) {
var err error
host := options.host
if host == nil {
host, err = newHost(cfg.P2P.IP, cfg.P2P.Port, cfg.ChainID, cfg.PrivKey, cg, logger)
ip, port, err := net.SplitHostPort(cfg.P2P.ListenAddress)
if err != nil {
return nil, fmt.Errorf("invalid P2P listen address: %w", err)
}
host, err = newHost(ip, port, cfg.ChainID, cfg.PrivKey, cg, logger)
if err != nil {
return nil, fmt.Errorf("cannot create host: %w", err)
}
Expand Down Expand Up @@ -758,7 +762,7 @@ func NewKey(r io.Reader) crypto.PrivateKey {
return privKey
}

func newHost(ip string, port uint64, chainID string, privKey crypto.PrivateKey, wl connmgr.ConnectionGater, logger log.Logger) (host.Host, error) {
func newHost(ip string, port string, chainID string, privKey crypto.PrivateKey, wl connmgr.ConnectionGater, logger log.Logger) (host.Host, error) {
// convert to the libp2p crypto key type
var privKeyP2P p2pcrypto.PrivKey
var err error
Expand All @@ -779,7 +783,7 @@ func newHost(ip string, port uint64, chainID string, privKey crypto.PrivateKey,
return nil, fmt.Errorf("unable to resolve %v: %w", ip, err)
}

sourceMultiAddr, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/%s/%s/tcp/%d", ipv, ip, port))
sourceMultiAddr, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/%s/%s/tcp/%s", ipv, ip, port))

// listenAddrs := libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0", "/ip4/0.0.0.0/tcp/0/ws")

Expand Down

0 comments on commit 69b8150

Please sign in to comment.