package p2p import ( "slices" "testing" "time" "github.com/libp2p/go-libp2p/core/network" "github.com/libp2p/go-libp2p/core/peer" ) func quickExchange(c *Config) { c.DiscoverInterval = 200 * time.Millisecond // the exchange runs on the same pace } // A newcomer that only knows a peer of another region finds its own region // through it: that peer is connected to a node of the region and says so. func TestARegionIsFoundThroughAPeerOfAnotherRegion(t *testing.T) { a := startNode(t, nil, "u1x") // the region the newcomer wants b := startNode(t, nil, "u0u") // another region, but connected to a connect(t, b, a) eventually(t, "b sees a subscribed to u1x", func() bool { return slices.Contains(b.RegionPeers("u1x"), a.host.ID()) }) c := startNodeWith(t, nil, quickExchange, "u1x") connect(t, c, b) // the only peer c knows eventually(t, "c finds a, the region's node, through b", func() bool { return slices.Contains(c.RegionPeers("u1x"), a.host.ID()) && c.host.Network().Connectedness(a.host.ID()) == network.Connected }) } // Listings then flow: the point of finding the region's peers. func TestAListingReachesANewcomerThatFoundItsRegionThroughAnotherOne(t *testing.T) { a := startNode(t, nil, "u1x") l := a.listing(t, "u1xjhx") a.store(t, l) b := startNode(t, nil, "u0u") connect(t, b, a) eventually(t, "b sees a subscribed to u1x", func() bool { return slices.Contains(b.RegionPeers("u1x"), a.host.ID()) }) c := startNodeWith(t, nil, quickExchange, "u1x") connect(t, c, b) eventually(t, "the listing arrives at c", func() bool { return count(c) == 1 }) } func TestPeerExchangeNamesRegionPeersFirstAndNeverTheAskerOrABannedPeer(t *testing.T) { a := startNode(t, nil, "u1x") b := startNode(t, nil, "u0u") d := startNode(t, nil, "u0u") x := startNode(t, nil, "u1x") // asks connect(t, a, b) connect(t, a, d) connect(t, a, x) eventually(t, "a has three peers", func() bool { return len(a.host.Network().Peers()) == 3 }) eventually(t, "a sees x subscribed", func() bool { return slices.Contains(a.RegionPeers("u1x"), x.host.ID()) }) got := a.peersFor(x.host.ID(), "u1x") ids := map[string]bool{} for _, p := range got { ids[p.ID] = true } if ids[x.host.ID().String()] || ids[a.host.ID().String()] { t.Fatalf("the answer names the asker or the answerer: %v", got) } if !ids[b.host.ID().String()] || !ids[d.host.ID().String()] { t.Fatalf("the answer must offer the other connected peers too: %v", got) } // Region peers come first. y := startNode(t, nil, "u1x") connect(t, a, y) eventually(t, "a sees y subscribed", func() bool { return slices.Contains(a.RegionPeers("u1x"), y.host.ID()) }) got = a.peersFor(x.host.ID(), "u1x") if got[0].ID != y.host.ID().String() { t.Fatalf("a peer of the region must come first, got %v", got) } for i := 0; i < maxBadPerWindow; i++ { a.guard.bad(b.host.ID()) } for _, p := range a.peersFor(x.host.ID(), "u1x") { if p.ID == b.host.ID().String() { t.Fatal("a banned peer was passed on") } } } func TestAnOldNodeSimplyDoesNotAnswerAndNothingBreaks(t *testing.T) { old := startNodeWith(t, nil, func(c *Config) {}, "u0u") old.oldSync = true c := startNodeWith(t, nil, quickExchange, "u1x") connect(t, c, old) if got := c.askForPeers(c.ctx, old.host.ID(), "u1x"); len(got) != 0 { t.Fatalf("an old node answered: %v", got) } } func TestRememberedPeersOfMyRegionsAreRedialedFirst(t *testing.T) { mk := func(id string, seen int64, regions ...string) cachedPeer { return cachedPeer{ID: id, Seen: seen, Regions: regions} } list := []cachedPeer{ // newest first, as the file is read mk("newest-other", 50, "u0u"), mk("newer-untagged", 40), mk("older-mine", 30, "u0u", "u1x"), mk("oldest-mine", 20, "u1x"), } orderForRedial(list, []string{"u1x"}) var got []string for _, p := range list { got = append(got, p.ID) } want := []string{"older-mine", "oldest-mine", "newest-other", "newer-untagged"} if !slices.Equal(got, want) { t.Fatalf("order %v, want %v", got, want) } } func TestPeersAreRememberedWithTheRegionsTheySubscribeTo(t *testing.T) { dir := t.TempDir() file := dir + "/peers.json" a := startNodeWith(t, nil, func(c *Config) { c.PeerCache = file }, "u1x") same := startNode(t, nil, "u1x") other := startNode(t, nil, "u0u") connect(t, a, same) connect(t, a, other) eventually(t, "a sees both subscriptions", func() bool { return slices.Contains(a.RegionPeers("u1x"), same.host.ID()) }) a.rememberPeers() tags := map[string][]string{} for _, p := range readPeerCache(file, time.Now()) { tags[p.ID] = p.Regions } if !slices.Equal(tags[peer.ID(same.host.ID()).String()], []string{"u1x"}) { t.Fatalf("the peer of my region is tagged %v", tags[same.host.ID().String()]) } if len(tags[other.host.ID().String()]) != 0 { t.Fatalf("a peer of another region is tagged %v", tags[other.host.ID().String()]) } }