Skip to content

Commit 2f2ef4a

Browse files
authored
fix(kademlia): always connect to bootnodes on startup to identify them (#4910)
1 parent 440ff5c commit 2f2ef4a

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

pkg/topology/kademlia/internal/metrics/metrics.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func IsBootnode(b bool) RecordOp {
3838
return func(cs *Counters) {
3939
cs.Lock()
4040
defer cs.Unlock()
41-
cs.isBootnode = b
41+
cs.IsBootnode = b
4242
}
4343
}
4444

@@ -147,13 +147,15 @@ type Snapshot struct {
147147
LatencyEWMA time.Duration
148148
Reachability p2p.ReachabilityStatus
149149
Healthy bool
150+
IsBootnode bool
150151
}
151152

152153
// persistentCounters is a helper struct used for persisting selected counters.
153154
type persistentCounters struct {
154155
PeerAddress swarm.Address `json:"peerAddress"`
155156
LastSeenTimestamp int64 `json:"lastSeenTimestamp"`
156157
ConnTotalDuration time.Duration `json:"connTotalDuration"`
158+
IsBootnode bool `json:"isBootnode"`
157159
}
158160

159161
// Counters represents a collection of peer metrics
@@ -164,7 +166,7 @@ type Counters struct {
164166
// Bookkeeping.
165167
isLoggedIn bool
166168
peerAddress swarm.Address
167-
isBootnode bool
169+
IsBootnode bool
168170

169171
// Counters.
170172
lastSeenTimestamp int64
@@ -187,6 +189,7 @@ func (cs *Counters) UnmarshalJSON(b []byte) (err error) {
187189
cs.peerAddress = val.PeerAddress
188190
cs.lastSeenTimestamp = val.LastSeenTimestamp
189191
cs.connTotalDuration = val.ConnTotalDuration
192+
cs.IsBootnode = val.IsBootnode
190193
cs.Unlock()
191194
return nil
192195
}
@@ -198,6 +201,7 @@ func (cs *Counters) MarshalJSON() ([]byte, error) {
198201
PeerAddress: cs.peerAddress,
199202
LastSeenTimestamp: cs.lastSeenTimestamp,
200203
ConnTotalDuration: cs.connTotalDuration,
204+
IsBootnode: cs.IsBootnode,
201205
}
202206
cs.Unlock()
203207
return json.Marshal(val)
@@ -224,6 +228,7 @@ func (cs *Counters) snapshot(t time.Time) *Snapshot {
224228
LatencyEWMA: cs.latencyEWMA,
225229
Reachability: cs.ReachabilityStatus,
226230
Healthy: cs.Healthy,
231+
IsBootnode: cs.IsBootnode,
227232
}
228233
}
229234

@@ -249,6 +254,7 @@ func NewCollector(db *shed.DB) (*Collector, error) {
249254
peerAddress: val.PeerAddress,
250255
lastSeenTimestamp: val.LastSeenTimestamp,
251256
connTotalDuration: val.ConnTotalDuration,
257+
IsBootnode: val.IsBootnode,
252258
})
253259
}
254260

@@ -321,7 +327,7 @@ type ExcludeOp func(*Counters) bool
321327
// IsBootnode is used to filter bootnode peers.
322328
func Bootnode() ExcludeOp {
323329
return func(cs *Counters) bool {
324-
return cs.isBootnode
330+
return cs.IsBootnode
325331
}
326332
}
327333

pkg/topology/kademlia/internal/metrics/metrics_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ func TestPeerMetricsCollector(t *testing.T) {
116116
t.Fatalf("Snapshot(%q, ...): session connection duration counter mismatch: have %q; want %q", addr, have, want)
117117
}
118118

119+
// Bootnode.
120+
mc.Record(addr, metrics.IsBootnode(false))
121+
ss = snapshot(t, mc, t2, addr)
122+
if have, want := ss.IsBootnode, false; have != want {
123+
t.Fatalf("Snapshot(%q, ...): latency mismatch: have %v; want %v", addr, have, want)
124+
}
125+
mc.Record(addr, metrics.IsBootnode(true))
126+
ss = snapshot(t, mc, t2, addr)
127+
if have, want := ss.IsBootnode, true; have != want {
128+
t.Fatalf("Snapshot(%q, ...): is bootnode mismatch: have %v; want %v", addr, have, want)
129+
}
130+
119131
// Latency.
120132
mc.Record(addr, metrics.PeerLatency(t4))
121133
ss = snapshot(t, mc, t2, addr)
@@ -188,6 +200,7 @@ func TestPeerMetricsCollector(t *testing.T) {
188200
want = &metrics.Snapshot{
189201
LastSeenTimestamp: ss.LastSeenTimestamp,
190202
ConnectionTotalDuration: 2 * ss.ConnectionTotalDuration, // 2x because we've already logout with t3 and login with t1 again.
203+
IsBootnode: true,
191204
}
192205
if diff := cmp.Diff(have, want); diff != "" {
193206
t.Fatalf("unexpected snapshot difference:\n%s", diff)

pkg/topology/kademlia/kademlia.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,11 @@ func (k *Kad) balancedSlotPeers(pseudoAddr swarm.Address, peers []swarm.Address,
744744
return ret
745745
}
746746

747-
func (k *Kad) Start(_ context.Context) error {
747+
func (k *Kad) Start(ctx context.Context) error {
748+
749+
// always discover bootnodes on startup to exclude them from protocol requests
750+
k.connectBootNodes(ctx)
751+
748752
k.wg.Add(1)
749753
go k.manage()
750754

0 commit comments

Comments
 (0)