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

p2p: Make p2p listen address configurable via config.NetAddress #5721

Merged
merged 2 commits into from
Sep 1, 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
32 changes: 31 additions & 1 deletion network/p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"context"
"fmt"
"runtime"
"strings"
"time"

"github.com/algorand/go-algorand/config"
Expand Down Expand Up @@ -83,13 +84,22 @@
version := config.GetCurrentVersion()
ua := fmt.Sprintf("algod/%d.%d (%s; commit=%s; %d) %s(%s)", version.Major, version.Minor, version.Channel, version.CommitHash, version.BuildNumber, runtime.GOOS, runtime.GOARCH)

var listenAddr string
if cfg.NetAddress != "" {
if parsedListenAddr, perr := netAddressToListenAddress(cfg.NetAddress); perr == nil {
listenAddr = parsedListenAddr

Check warning on line 90 in network/p2p/p2p.go

View check run for this annotation

Codecov / codecov/patch

network/p2p/p2p.go#L87-L90

Added lines #L87 - L90 were not covered by tests
}
} else {
listenAddr = "/ip4/0.0.0.0/tcp/0"

Check warning on line 93 in network/p2p/p2p.go

View check run for this annotation

Codecov / codecov/patch

network/p2p/p2p.go#L92-L93

Added lines #L92 - L93 were not covered by tests
}

h, err := libp2p.New(
libp2p.Identity(privKey),
libp2p.UserAgent(ua),
libp2p.Transport(tcp.NewTCPTransport),
libp2p.Muxer("/yamux/1.0.0", &ymx),
libp2p.Peerstore(pstore),
libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"),
libp2p.ListenAddrStrings(listenAddr),

Check warning on line 102 in network/p2p/p2p.go

View check run for this annotation

Codecov / codecov/patch

network/p2p/p2p.go#L102

Added line #L102 was not covered by tests
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -173,3 +183,23 @@
func (s *serviceImpl) ClosePeer(peer peer.ID) error {
return s.host.Network().ClosePeer(peer)
}

// netAddressToListenAddress converts a netAddress in "ip:port" format to a listen address
// that can be passed in to libp2p.ListenAddrStrings
func netAddressToListenAddress(netAddress string) (string, error) {
Eric-Warehime marked this conversation as resolved.
Show resolved Hide resolved
// split the string on ":"
// if there are more than 2 parts, return an error
parts := strings.Split(netAddress, ":")
if len(parts) != 2 {
return "", fmt.Errorf("invalid netAddress %s; required format is \"ip:port\"", netAddress)
}
ip := "0.0.0.0"
if parts[0] != "" {
ip = parts[0]
}
if parts[1] == "" {
return "", fmt.Errorf("invalid netAddress %s, port is required", netAddress)
}

return fmt.Sprintf("/ip4/%s/tcp/%s", ip, parts[1]), nil
}
76 changes: 76 additions & 0 deletions network/p2p/p2p_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.

package p2p

import (
"fmt"
"testing"

"github.com/algorand/go-algorand/test/partitiontest"
"github.com/stretchr/testify/require"
)

// Tests the helper function netAddressToListenAddress which converts
// a config value netAddress to a multiaddress usable by libp2p.
func TestNetAddressToListenAddress(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

tests := []struct {
input string
output string
err bool
}{
{
input: "192.168.1.1:8080",
output: "/ip4/192.168.1.1/tcp/8080",
err: false,
},
{
input: ":8080",
output: "/ip4/0.0.0.0/tcp/8080",
err: false,
},
{
input: "192.168.1.1:",
output: "",
err: true,
},
{
input: "192.168.1.1",
output: "",
err: true,
},
{
input: "192.168.1.1:8080:9090",
output: "",
err: true,
},
}

for _, test := range tests { //nolint:paralleltest
t.Run(fmt.Sprintf("input: %s", test.input), func(t *testing.T) {
res, err := netAddressToListenAddress(test.input)
if test.err {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, test.output, res)
}
})
}
}