Skip to content

Commit a89cf6d

Browse files
authored
test: disable NAT port mapping, outbound dials, inbound connections (#12591)
My poor network. It deserves to be treated with respect and dignity and doesn't deserve to be spammed. 1. Disable NAT port mapping. Because no, we don't want our integration tests nodes to be reachable. 2. Disable all but a single localhost/quic transport. No need to do more work than necessary. 3. Set the connection manager limits to be really high. This probably doesn't matter, but there's no need to be killing connections in our integration tests. 4. Reject all outbound dials to non-localhost addresses.
1 parent d4b8af9 commit a89cf6d

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

itests/kit/ensemble.go

+11
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import (
1616
"github.com/google/uuid"
1717
"github.com/ipfs/go-datastore"
1818
"github.com/ipfs/go-datastore/namespace"
19+
"github.com/libp2p/go-libp2p"
1920
libp2pcrypto "github.com/libp2p/go-libp2p/core/crypto"
2021
"github.com/libp2p/go-libp2p/core/peer"
22+
"github.com/libp2p/go-libp2p/p2p/net/conngater"
2123
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
2224
"github.com/stretchr/testify/require"
2325

@@ -57,6 +59,7 @@ import (
5759
"github.com/filecoin-project/lotus/node/config"
5860
"github.com/filecoin-project/lotus/node/modules"
5961
"github.com/filecoin-project/lotus/node/modules/dtypes"
62+
"github.com/filecoin-project/lotus/node/modules/lp2p"
6063
testing2 "github.com/filecoin-project/lotus/node/modules/testing"
6164
"github.com/filecoin-project/lotus/node/repo"
6265
"github.com/filecoin-project/lotus/storage/paths"
@@ -438,6 +441,13 @@ func (n *Ensemble) Start() *Ensemble {
438441
node.If(full.options.disableLibp2p, node.MockHost(n.mn)),
439442
node.Test(),
440443

444+
// If we're using real libp2p, disable outbound connections to all but localhost.
445+
node.If(!full.options.disableLibp2p,
446+
node.Override(node.ConnGaterKey, func(gater *conngater.BasicConnectionGater) (opts lp2p.Libp2pOpts, err error) {
447+
opts.Opts = append(opts.Opts, libp2p.ConnectionGater(&loopbackConnGater{gater}))
448+
return
449+
})),
450+
441451
// so that we subscribe to pubsub topics immediately
442452
node.Override(new(dtypes.Bootstrapper), dtypes.Bootstrapper(true)),
443453

@@ -707,6 +717,7 @@ func (n *Ensemble) Start() *Ensemble {
707717
node.Repo(r),
708718
node.Test(),
709719

720+
node.Override(node.DefaultTransportsKey, lp2p.QUIC),
710721
node.If(m.options.disableLibp2p, node.MockHost(n.mn)),
711722
node.Override(new(v1api.RawFullNodeAPI), m.FullNode),
712723
node.Override(new(*lotusminer.Miner), lotusminer.NewTestMiner(mineBlock, m.ActorAddr)),

itests/kit/node_opts.go

+34
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ package kit
22

33
import (
44
"math"
5+
"time"
6+
7+
"github.com/libp2p/go-libp2p/core/connmgr"
8+
"github.com/libp2p/go-libp2p/core/peer"
9+
multiaddr "github.com/multiformats/go-multiaddr"
10+
manet "github.com/multiformats/go-multiaddr/net"
511

612
"github.com/filecoin-project/go-f3/manifest"
713
"github.com/filecoin-project/go-state-types/abi"
@@ -56,6 +62,23 @@ type nodeOpts struct {
5662
workerName string
5763
}
5864

65+
// Libp2p connection gater that only allows outbound connections to loopback addresses.
66+
type loopbackConnGater struct{ connmgr.ConnectionGater }
67+
68+
// InterceptAddrDial implements connmgr.ConnectionGater.
69+
func (l *loopbackConnGater) InterceptAddrDial(p peer.ID, a multiaddr.Multiaddr) (allow bool) {
70+
if !l.ConnectionGater.InterceptAddrDial(p, a) {
71+
return false
72+
}
73+
ip, err := manet.ToIP(a)
74+
if err != nil {
75+
return false
76+
}
77+
return ip.IsLoopback()
78+
}
79+
80+
var _ connmgr.ConnectionGater = (*loopbackConnGater)(nil)
81+
5982
// DefaultNodeOpts are the default options that will be applied to test nodes.
6083
var DefaultNodeOpts = nodeOpts{
6184
balance: big.Mul(big.NewInt(100000000), types.NewInt(buildconstants.FilecoinPrecision)),
@@ -69,6 +92,17 @@ var DefaultNodeOpts = nodeOpts{
6992
cfg.Fevm.EnableEthRPC = true
7093
cfg.Events.MaxFilterHeightRange = math.MaxInt64
7194
cfg.Events.EnableActorEventsAPI = true
95+
96+
// Disable external networking ffs.
97+
cfg.Libp2p.ListenAddresses = []string{
98+
"/ip4/127.0.0.1/udp/0/quic-v1",
99+
}
100+
cfg.Libp2p.DisableNatPortMap = true
101+
102+
// Nerf the connection manager.
103+
cfg.Libp2p.ConnMgrLow = 1024
104+
cfg.Libp2p.ConnMgrHigh = 2048
105+
cfg.Libp2p.ConnMgrGrace = config.Duration(time.Hour)
72106
return nil
73107
},
74108
},

0 commit comments

Comments
 (0)