Skip to content

Commit 15ac8cd

Browse files
Remove subnet filter from Peer.TrackedSubnets() (#2975)
1 parent 54c4b53 commit 15ac8cd

File tree

5 files changed

+225
-210
lines changed

5 files changed

+225
-210
lines changed

network/metrics.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import (
1212
"github.com/ava-labs/avalanchego/ids"
1313
"github.com/ava-labs/avalanchego/network/peer"
1414
"github.com/ava-labs/avalanchego/utils"
15-
"github.com/ava-labs/avalanchego/utils/constants"
1615
"github.com/ava-labs/avalanchego/utils/set"
1716
)
1817

1918
type metrics struct {
19+
// trackedSubnets does not include the primary network ID
20+
trackedSubnets set.Set[ids.ID]
21+
2022
numTracked prometheus.Gauge
2123
numPeers prometheus.Gauge
2224
numSubnetPeers *prometheus.GaugeVec
@@ -41,8 +43,13 @@ type metrics struct {
4143
peerConnectedStartTimesSum float64
4244
}
4345

44-
func newMetrics(namespace string, registerer prometheus.Registerer, initialSubnetIDs set.Set[ids.ID]) (*metrics, error) {
46+
func newMetrics(
47+
namespace string,
48+
registerer prometheus.Registerer,
49+
trackedSubnets set.Set[ids.ID],
50+
) (*metrics, error) {
4551
m := &metrics{
52+
trackedSubnets: trackedSubnets,
4653
numPeers: prometheus.NewGauge(prometheus.GaugeOpts{
4754
Namespace: namespace,
4855
Name: "peers",
@@ -169,11 +176,7 @@ func newMetrics(namespace string, registerer prometheus.Registerer, initialSubne
169176
)
170177

171178
// init subnet tracker metrics with tracked subnets
172-
for subnetID := range initialSubnetIDs {
173-
// no need to track primary network ID
174-
if subnetID == constants.PrimaryNetworkID {
175-
continue
176-
}
179+
for subnetID := range trackedSubnets {
177180
// initialize to 0
178181
subnetIDStr := subnetID.String()
179182
m.numSubnetPeers.WithLabelValues(subnetIDStr).Set(0)
@@ -189,8 +192,10 @@ func (m *metrics) markConnected(peer peer.Peer) {
189192
m.connected.Inc()
190193

191194
trackedSubnets := peer.TrackedSubnets()
192-
for subnetID := range trackedSubnets {
193-
m.numSubnetPeers.WithLabelValues(subnetID.String()).Inc()
195+
for subnetID := range m.trackedSubnets {
196+
if trackedSubnets.Contains(subnetID) {
197+
m.numSubnetPeers.WithLabelValues(subnetID.String()).Inc()
198+
}
194199
}
195200

196201
m.lock.Lock()
@@ -206,8 +211,10 @@ func (m *metrics) markDisconnected(peer peer.Peer) {
206211
m.disconnected.Inc()
207212

208213
trackedSubnets := peer.TrackedSubnets()
209-
for subnetID := range trackedSubnets {
210-
m.numSubnetPeers.WithLabelValues(subnetID.String()).Dec()
214+
for subnetID := range m.trackedSubnets {
215+
if trackedSubnets.Contains(subnetID) {
216+
m.numSubnetPeers.WithLabelValues(subnetID.String()).Dec()
217+
}
211218
}
212219

213220
m.lock.Lock()

network/network.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,12 @@ func (n *network) Connected(nodeID ids.NodeID) {
460460

461461
peerVersion := peer.Version()
462462
n.router.Connected(nodeID, peerVersion, constants.PrimaryNetworkID)
463-
for subnetID := range peer.TrackedSubnets() {
464-
n.router.Connected(nodeID, peerVersion, subnetID)
463+
464+
trackedSubnets := peer.TrackedSubnets()
465+
for subnetID := range n.peerConfig.MySubnets {
466+
if trackedSubnets.Contains(subnetID) {
467+
n.router.Connected(nodeID, peerVersion, subnetID)
468+
}
465469
}
466470
}
467471

@@ -694,8 +698,7 @@ func (n *network) getPeers(
694698
continue
695699
}
696700

697-
trackedSubnets := peer.TrackedSubnets()
698-
if subnetID != constants.PrimaryNetworkID && !trackedSubnets.Contains(subnetID) {
701+
if trackedSubnets := peer.TrackedSubnets(); !trackedSubnets.Contains(subnetID) {
699702
continue
700703
}
701704

@@ -731,8 +734,7 @@ func (n *network) samplePeers(
731734
numValidatorsToSample+config.NonValidators+config.Peers,
732735
func(p peer.Peer) bool {
733736
// Only return peers that are tracking [subnetID]
734-
trackedSubnets := p.TrackedSubnets()
735-
if subnetID != constants.PrimaryNetworkID && !trackedSubnets.Contains(subnetID) {
737+
if trackedSubnets := p.TrackedSubnets(); !trackedSubnets.Contains(subnetID) {
736738
return false
737739
}
738740

network/peer/config.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ type Config struct {
3333
Network Network
3434
Router router.InboundHandler
3535
VersionCompatibility version.Compatibility
36-
MySubnets set.Set[ids.ID]
37-
Beacons validators.Manager
38-
Validators validators.Manager
39-
NetworkID uint32
40-
PingFrequency time.Duration
41-
PongTimeout time.Duration
42-
MaxClockDifference time.Duration
36+
// MySubnets does not include the primary network ID
37+
MySubnets set.Set[ids.ID]
38+
Beacons validators.Manager
39+
Validators validators.Manager
40+
NetworkID uint32
41+
PingFrequency time.Duration
42+
PongTimeout time.Duration
43+
MaxClockDifference time.Duration
4344

4445
SupportedACPs []uint32
4546
ObjectedACPs []uint32

network/peer/peer.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ const (
3535
// maxBloomSaltLen restricts the allowed size of the bloom salt to prevent
3636
// excessively expensive bloom filter contains checks.
3737
maxBloomSaltLen = 32
38+
// maxNumTrackedSubnets limits how many subnets a peer can track to prevent
39+
// excessive memory usage.
40+
maxNumTrackedSubnets = 16
3841

3942
disconnectingLog = "disconnecting from peer"
4043
failedToCreateMessageLog = "failed to create message"
@@ -139,8 +142,8 @@ type peer struct {
139142
// version is the claimed version the peer is running that we received in
140143
// the Handshake message.
141144
version *version.Application
142-
// trackedSubnets is the subset of subnetIDs the peer sent us in the Handshake
143-
// message that we are also tracking.
145+
// trackedSubnets are the subnetIDs the peer sent us in the Handshake
146+
// message. The primary network ID is always included.
144147
trackedSubnets set.Set[ids.ID]
145148
// options of ACPs provided in the Handshake message.
146149
supportedACPs set.Set[uint32]
@@ -271,9 +274,8 @@ func (p *peer) Info() Info {
271274
publicIPStr = p.ip.IPPort.String()
272275
}
273276

274-
uptimes := make(map[ids.ID]json.Uint32, p.trackedSubnets.Len())
275-
276-
for subnetID := range p.trackedSubnets {
277+
uptimes := make(map[ids.ID]json.Uint32, p.MySubnets.Len())
278+
for subnetID := range p.MySubnets {
277279
uptime, exist := p.ObservedUptime(subnetID)
278280
if !exist {
279281
continue
@@ -851,8 +853,12 @@ func (p *peer) getUptimes() (uint32, []*p2p.SubnetUptime) {
851853
primaryUptime = 0
852854
}
853855

854-
subnetUptimes := make([]*p2p.SubnetUptime, 0, p.trackedSubnets.Len())
855-
for subnetID := range p.trackedSubnets {
856+
subnetUptimes := make([]*p2p.SubnetUptime, 0, p.MySubnets.Len())
857+
for subnetID := range p.MySubnets {
858+
if !p.trackedSubnets.Contains(subnetID) {
859+
continue
860+
}
861+
856862
subnetUptime, err := p.UptimeCalculator.CalculateUptimePercent(p.id, subnetID)
857863
if err != nil {
858864
p.Log.Debug(failedToGetUptimeLog,
@@ -951,6 +957,18 @@ func (p *peer) handleHandshake(msg *p2p.Handshake) {
951957
}
952958

953959
// handle subnet IDs
960+
if numTrackedSubnets := len(msg.TrackedSubnets); numTrackedSubnets > maxNumTrackedSubnets {
961+
p.Log.Debug(malformedMessageLog,
962+
zap.Stringer("nodeID", p.id),
963+
zap.Stringer("messageOp", message.HandshakeOp),
964+
zap.String("field", "trackedSubnets"),
965+
zap.Int("numTrackedSubnets", numTrackedSubnets),
966+
)
967+
p.StartClose()
968+
return
969+
}
970+
971+
p.trackedSubnets.Add(constants.PrimaryNetworkID)
954972
for _, subnetIDBytes := range msg.TrackedSubnets {
955973
subnetID, err := ids.ToID(subnetIDBytes)
956974
if err != nil {
@@ -963,10 +981,7 @@ func (p *peer) handleHandshake(msg *p2p.Handshake) {
963981
p.StartClose()
964982
return
965983
}
966-
// add only if we also track this subnet
967-
if p.MySubnets.Contains(subnetID) {
968-
p.trackedSubnets.Add(subnetID)
969-
}
984+
p.trackedSubnets.Add(subnetID)
970985
}
971986

972987
for _, acp := range msg.SupportedAcps {

0 commit comments

Comments
 (0)