Skip to content

Commit 62e5c1c

Browse files
test(p2p): fix host factory option handling and enhance addressbook tests
1 parent b39629d commit 62e5c1c

3 files changed

Lines changed: 20 additions & 11 deletions

File tree

pkg/p2p/libp2p/connections_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,16 +1579,18 @@ func TestAddressbookPersistence(t *testing.T) {
15791579

15801580
ctx := t.Context()
15811581

1582-
ab := addressbook.New(mock.NewStateStore())
1582+
ab1 := addressbook.New(mock.NewStateStore())
1583+
ab2 := addressbook.New(mock.NewStateStore())
15831584

15841585
s1, overlay1 := newService(t, 1, libp2pServiceOpts{
1586+
Addressbook: ab1,
15851587
libp2pOpts: libp2p.Options{
15861588
FullNode: true,
15871589
},
15881590
})
15891591

15901592
s2, overlay2 := newService(t, 1, libp2pServiceOpts{
1591-
Addressbook: ab,
1593+
Addressbook: ab2,
15921594
emptyUnderlays: true,
15931595
libp2pOpts: libp2p.Options{
15941596
FullNode: true,
@@ -1611,13 +1613,19 @@ func TestAddressbookPersistence(t *testing.T) {
16111613
expectPeersEventually(t, s1, overlay2)
16121614

16131615
// Verify s1 (has underlays) is persisted in s2's addressbook
1614-
addr, err := ab.Get(overlay1)
1616+
addr, err := ab2.Get(overlay1)
16151617
if err != nil {
16161618
t.Fatal(err)
16171619
}
16181620
if !containsAtLeastOne(addr.Underlays, s1Addrs) {
16191621
t.Fatalf("expected at least one of s1's addresses %v in addressbook, got %v", s1Addrs, addr.Underlays)
16201622
}
1623+
1624+
// Verify s2 (empty underlays) is NOT persisted in s1's addressbook
1625+
_, err = ab1.Get(overlay2)
1626+
if err == nil {
1627+
t.Fatal("expected s2 (inbound-only peer with empty underlays) not to be persisted in s1's addressbook")
1628+
}
16211629
})
16221630
}
16231631

pkg/p2p/libp2p/export_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ func SetAutoTLSCertManager(o *Options, m autoTLSCertManager) {
5353
o.autoTLSCertManager = m
5454
}
5555

56+
func SetHostFactory(o *Options, factory func(...libp2pm.Option) (host.Host, error)) {
57+
o.hostFactory = factory
58+
}
59+
5660
type AutoTLSCertManager = autoTLSCertManager
5761

5862
var NewCompositeAddressResolver = newCompositeAddressResolver

pkg/p2p/libp2p/libp2p_test.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,11 @@ func newService(t *testing.T, networkID uint64, o libp2pServiceOpts) (s *libp2p.
8888
// If emptyUnderlays is set, use a custom host factory that creates a host with no listen addresses
8989
// This simulates an inbound-only peer (browser, WebRTC connection, strict NAT)
9090
if o.emptyUnderlays {
91-
opts = libp2p.WithHostFactory(
92-
func(hostOpts ...libp2pm.Option) (host.Host, error) {
93-
// Add NoListenAddrs option to prevent the host from listening on any addresses
94-
hostOpts = append(hostOpts, libp2pm.NoListenAddrs)
95-
return libp2pm.New(hostOpts...)
96-
},
97-
)
98-
opts.Nonce = nonce
91+
libp2p.SetHostFactory(&opts, func(hostOpts ...libp2pm.Option) (host.Host, error) {
92+
// Add NoListenAddrs option to prevent the host from listening on any addresses
93+
hostOpts = append(hostOpts, libp2pm.NoListenAddrs)
94+
return libp2pm.New(hostOpts...)
95+
})
9996
}
10097

10198
if o.autoTLSCertManager != nil {

0 commit comments

Comments
 (0)