-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathexport_test.go
More file actions
124 lines (103 loc) · 3.09 KB
/
Copy pathexport_test.go
File metadata and controls
124 lines (103 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package kademlia
import (
"context"
"github.com/ethersphere/bee/v2/pkg/swarm"
"github.com/ethersphere/bee/v2/pkg/topology"
im "github.com/ethersphere/bee/v2/pkg/topology/kademlia/internal/metrics"
"github.com/ethersphere/bee/v2/pkg/topology/pslice"
)
var (
PruneOversaturatedBinsFunc = func(k *Kad) func(uint8) {
return k.pruneOversaturatedBins
}
GenerateCommonBinPrefixes = generateCommonBinPrefixes
)
// MarkConnectedPeersSeen runs the sweep the manage loop performs on every
// lastSeenRefreshInterval tick.
func (k *Kad) MarkConnectedPeersSeen() error {
return k.markConnectedPeersSeen()
}
// RebroadcastNeighborhood runs the neighborhood gossip the manage loop
// performs every fifteen minutes.
func (k *Kad) RebroadcastNeighborhood(ctx context.Context) {
k.rebroadcastNeighborhood(ctx)
}
const (
DefaultBitSuffixLength = defaultBitSuffixLength
DefaultSaturationPeers = defaultSaturationPeers
DefaultOverSaturationPeers = defaultOverSaturationPeers
MaxConnAttempts = maxConnAttempts
)
type (
PeerExcludeFunc = peerExcludeFunc
ExcludeFunc = excludeFunc
)
func (k *Kad) IsWithinConnectionDepth(addr swarm.Address) bool {
return swarm.Proximity(k.base.Bytes(), addr.Bytes()) >= k.ConnectionDepth()
}
func (k *Kad) ConnectionDepth() uint8 {
k.depthMu.RLock()
defer k.depthMu.RUnlock()
return k.depth
}
func (k *Kad) StorageRadius() uint8 {
k.depthMu.RLock()
defer k.depthMu.RUnlock()
return k.storageRadius
}
// IsBalanced returns if Kademlia is balanced to bin.
func (k *Kad) IsBalanced(bin uint8) bool {
if int(bin) >= len(k.commonBinPrefixes) {
return false
}
// for each pseudo address
for i := range k.commonBinPrefixes[bin] {
pseudoAddr := k.commonBinPrefixes[bin][i]
closestConnectedPeer, err := closestPeer(k.connectedPeers, pseudoAddr)
if err != nil {
return false
}
closestConnectedPO := swarm.ExtendedProximity(closestConnectedPeer.Bytes(), pseudoAddr.Bytes())
if int(closestConnectedPO) < int(bin)+k.opt.BitSuffixLength+1 {
return false
}
}
return true
}
func closestPeer(peers *pslice.PSlice, addr swarm.Address) (swarm.Address, error) {
closest := swarm.ZeroAddress
err := peers.EachBinRev(func(peer swarm.Address, po uint8) (bool, bool, error) {
if closest.IsZero() {
closest = peer
return false, false, nil
}
closer, err := peer.Closer(addr, closest)
if err != nil {
return false, false, err
}
if closer {
closest = peer
}
return false, false, nil
})
if err != nil {
return closest, err
}
// check if found
if closest.IsZero() {
return closest, topology.ErrNotFound
}
return closest, nil
}
func (k *Kad) Trigger() {
k.manageC <- struct{}{}
}
// MarkAsBootnode marks the given address as a bootnode in the metrics
// collector, mirroring what the production connectBootNodes path does. Used by
// tests to assert iterator filtering behavior.
func (k *Kad) MarkAsBootnode(addr swarm.Address) {
k.collector.Record(addr, im.IsBootnode(true))
}