Skip to content

Commit

Permalink
Merge pull request ethereum-optimism#8014 from ethereum-optimism/filt…
Browse files Browse the repository at this point in the history
…er-static-peers

op-node: static-peers list local-peer check and flag description update
  • Loading branch information
protolambda committed Nov 2, 2023
2 parents 413aac1 + 14d154d commit e6c5afa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
5 changes: 3 additions & 2 deletions op-node/flags/p2p_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
10 changes: 7 additions & 3 deletions op-node/p2p/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.", "peer_id", addr.ID, "addrs", addr.Addrs)
continue
}
staticPeers = append(staticPeers, addr)
}

out := &extraHost{
Expand Down
11 changes: 11 additions & 0 deletions op-node/p2p/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -139,13 +140,23 @@ 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)
require.NoError(t, err)
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")
Expand Down

0 comments on commit e6c5afa

Please sign in to comment.