Skip to content

Commit 35c38d9

Browse files
fix p2p stats safety
1 parent fbd193d commit 35c38d9

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

p2p/kademlia/dht.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,26 @@ func (s *DHT) PeersSnapshot() []*Node {
463463
if s == nil || s.ht == nil {
464464
return nil
465465
}
466-
return s.ht.nodes()
466+
in := s.ht.nodes()
467+
if len(in) == 0 {
468+
return nil
469+
}
470+
471+
out := make([]*Node, 0, len(in))
472+
for _, n := range in {
473+
if n == nil {
474+
continue
475+
}
476+
cp := *n
477+
if n.ID != nil {
478+
cp.ID = append([]byte(nil), n.ID...)
479+
}
480+
if n.HashedID != nil {
481+
cp.HashedID = append([]byte(nil), n.HashedID...)
482+
}
483+
out = append(out, &cp)
484+
}
485+
return out
467486
}
468487

469488
func (s *DHT) NetworkHandleMetricsSnapshot() map[string]HandleCounters {

p2p/p2p.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/hex"
66
"fmt"
7+
"sync"
78
"time"
89

910
"github.com/LumeraProtocol/supernode/v2/p2p/kademlia"
@@ -49,6 +50,7 @@ type p2p struct {
4950
keyring keyring.Keyring // Add the keyring field
5051
rqstore rqstore.Store
5152
stats *p2pStatsManager
53+
statsOnce sync.Once
5254
}
5355

5456
// Run the kademlia network
@@ -172,8 +174,13 @@ func (s *p2p) Stats(ctx context.Context) (*StatsSnapshot, error) {
172174
if err := ctx.Err(); err != nil {
173175
return nil, err
174176
}
177+
s.statsOnce.Do(func() {
178+
if s.stats == nil {
179+
s.stats = newP2PStatsManager()
180+
}
181+
})
175182
if s.stats == nil {
176-
s.stats = newP2PStatsManager()
183+
return nil, errors.New("p2p stats manager is nil")
177184
}
178185
return s.stats.Stats(ctx, s)
179186
}

p2p/p2p_stats.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,20 @@ func cloneSnapshot(in *StatsSnapshot) *StatsSnapshot {
210210
out := *in
211211

212212
if in.Peers != nil {
213-
out.Peers = append([]*kademlia.Node(nil), in.Peers...)
213+
out.Peers = make([]*kademlia.Node, len(in.Peers))
214+
for i, peer := range in.Peers {
215+
if peer == nil {
216+
continue
217+
}
218+
cp := *peer
219+
if peer.ID != nil {
220+
cp.ID = append([]byte(nil), peer.ID...)
221+
}
222+
if peer.HashedID != nil {
223+
cp.HashedID = append([]byte(nil), peer.HashedID...)
224+
}
225+
out.Peers[i] = &cp
226+
}
214227
}
215228

216229
if in.BanList != nil {

supernode/status/service.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ func (s *SupernodeStatusService) GetStatus(ctx context.Context, includeP2PMetric
136136
if peers := snap.Peers; len(peers) > 0 {
137137
resp.Network.PeerAddresses = make([]string, 0, len(peers))
138138
for _, peer := range peers {
139+
if peer == nil {
140+
continue
141+
}
139142
resp.Network.PeerAddresses = append(resp.Network.PeerAddresses, fmt.Sprintf("%s@%s:%d", string(peer.ID), peer.IP, peer.Port))
140143
}
141144
}

0 commit comments

Comments
 (0)