From 48962ac92b94babc27d1789088295b602ee745b0 Mon Sep 17 00:00:00 2001 From: protolambda Date: Thu, 2 Nov 2023 20:21:41 +0100 Subject: [PATCH 1/2] op-node: static-peers list local-peer check and flag description update --- op-node/flags/p2p_flags.go | 5 +++-- op-node/p2p/host.go | 10 +++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/op-node/flags/p2p_flags.go b/op-node/flags/p2p_flags.go index 4f680eae7e8d..34577d831f5b 100644 --- a/op-node/flags/p2p_flags.go +++ b/op-node/flags/p2p_flags.go @@ -189,8 +189,9 @@ func P2PFlags(envPrefix string) []cli.Flag { EnvVars: p2pEnv(envPrefix, "BOOTNODES"), }, &cli.StringFlag{ - Name: StaticPeersName, - Usage: "Comma-separated multiaddr-format peer list. Static connections to make and maintain, these peers will be regarded as trusted.", + Name: StaticPeersName, + Usage: "Comma-separated multiaddr-format peer list. Static connections to make and maintain, these peers will be regarded as trusted. " + + "Addresses of the local peer are ignored. Duplicate/Alternative addresses for the same peer all apply, but only a single connection per peer is maintained.", Required: false, Value: "", EnvVars: p2pEnv(envPrefix, "STATIC"), diff --git a/op-node/p2p/host.go b/op-node/p2p/host.go index 5396bed05d9c..9496d4bf7cc0 100644 --- a/op-node/p2p/host.go +++ b/op-node/p2p/host.go @@ -229,13 +229,17 @@ func (conf *Config) Host(log log.Logger, reporter metrics.Reporter, metrics Host return nil, err } - staticPeers := make([]*peer.AddrInfo, len(conf.StaticPeers)) - for i, peerAddr := range conf.StaticPeers { + staticPeers := make([]*peer.AddrInfo, 0, len(conf.StaticPeers)) + for _, peerAddr := range conf.StaticPeers { addr, err := peer.AddrInfoFromP2pAddr(peerAddr) if err != nil { return nil, fmt.Errorf("bad peer address: %w", err) } - staticPeers[i] = addr + if addr.ID == h.ID() { + log.Info("Static-peer list contains address of local peer, ignoring the address.", "address", addr) + continue + } + staticPeers = append(staticPeers, addr) } out := &extraHost{ From 14d154d9cd6f552afa8288fe3495d5ffdee2179f Mon Sep 17 00:00:00 2001 From: protolambda Date: Thu, 2 Nov 2023 20:47:49 +0100 Subject: [PATCH 2/2] op-node: static-peers test --- op-node/p2p/host.go | 2 +- op-node/p2p/host_test.go | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/op-node/p2p/host.go b/op-node/p2p/host.go index 9496d4bf7cc0..6011617f5c78 100644 --- a/op-node/p2p/host.go +++ b/op-node/p2p/host.go @@ -236,7 +236,7 @@ func (conf *Config) Host(log log.Logger, reporter metrics.Reporter, metrics Host return nil, fmt.Errorf("bad peer address: %w", err) } if addr.ID == h.ID() { - log.Info("Static-peer list contains address of local peer, ignoring the address.", "address", addr) + log.Info("Static-peer list contains address of local peer, ignoring the address.", "peer_id", addr.ID, "addrs", addr.Addrs) continue } staticPeers = append(staticPeers, addr) diff --git a/op-node/p2p/host_test.go b/op-node/p2p/host_test.go index 1a44c25664b0..66454c70fe23 100644 --- a/op-node/p2p/host_test.go +++ b/op-node/p2p/host_test.go @@ -15,6 +15,7 @@ import ( "github.com/libp2p/go-libp2p/core/network" "github.com/libp2p/go-libp2p/core/peer" mocknet "github.com/libp2p/go-libp2p/p2p/net/mock" + ma "github.com/multiformats/go-multiaddr" "github.com/stretchr/testify/require" "golang.org/x/exp/slices" @@ -139,6 +140,13 @@ func TestP2PFull(t *testing.T) { confB.StaticPeers, err = peer.AddrInfoToP2pAddrs(&peer.AddrInfo{ID: hostA.ID(), Addrs: hostA.Addrs()}) require.NoError(t, err) + // Add address of host B itself, it shouldn't connect or cause issues. + idB, err := peer.IDFromPublicKey(confB.Priv.GetPublic()) + require.NoError(t, err) + altAddrB, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/12345/p2p/" + idB.String()) + require.NoError(t, err) + confB.StaticPeers = append(confB.StaticPeers, altAddrB) + logB := testlog.Logger(t, log.LvlError).New("host", "B") nodeB, err := NewNodeP2P(context.Background(), &rollup.Config{}, logB, &confB, &mockGossipIn{}, nil, runCfgB, metrics.NoopMetrics) @@ -146,6 +154,9 @@ func TestP2PFull(t *testing.T) { defer nodeB.Close() hostB := nodeB.Host() + require.True(t, nodeB.IsStatic(hostA.ID()), "node A must be static peer of node B") + require.False(t, nodeB.IsStatic(hostB.ID()), "node B must not be static peer of node B itself") + select { case <-time.After(time.Second): t.Fatal("failed to connect new host")