@@ -49,7 +49,7 @@ const (
49
49
50
50
// Default option values
51
51
const (
52
- defaultBitSuffixLength = 2 // the number of bits used to create pseudo addresses for balancing, 2^2, 8 addresses
52
+ defaultBitSuffixLength = 4 // the number of bits used to create pseudo addresses for balancing, 2^2, 8 addresses
53
53
defaultLowWaterMark = 3 // the number of peers in consecutive deepest bins that constitute as nearest neighbours
54
54
defaultSaturationPeers = 8
55
55
defaultOverSaturationPeers = 18
@@ -111,18 +111,21 @@ type kadOptions struct {
111
111
StaticNodes []swarm.Address
112
112
ExcludeFunc excludeFunc
113
113
114
- TimeToRetry time.Duration
115
- ShortRetry time.Duration
116
- PruneWakeup time.Duration
117
- BitSuffixLength int // additional depth of common prefix for bin
114
+ TimeToRetry time.Duration
115
+ ShortRetry time.Duration
116
+ PruneWakeup time.Duration
117
+ // BitSuffixLength int // additional depth of common prefix for bin
118
118
SaturationPeers int
119
119
OverSaturationPeers int
120
120
BootnodeOverSaturationPeers int
121
121
BroadcastBinSize int
122
122
LowWaterMark int
123
123
}
124
124
125
- var saturationCounts = [swarm .MaxBins ]int {64 , 32 , 16 , 12 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 }
125
+ var (
126
+ saturationCounts = [swarm .MaxBins ]int {64 , 32 , 16 , 12 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 }
127
+ prefixBits = [swarm .MaxBins ]int {6 , 5 , 4 , 4 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 }
128
+ )
126
129
127
130
func newKadOptions (o Options ) kadOptions {
128
131
ko := kadOptions {
@@ -134,12 +137,10 @@ func newKadOptions(o Options) kadOptions {
134
137
StaticNodes : o .StaticNodes ,
135
138
ExcludeFunc : o .ExcludeFunc ,
136
139
// copy or use default
137
- TimeToRetry : defaultValDuration (o .TimeToRetry , defaultTimeToRetry ),
138
- ShortRetry : defaultValDuration (o .ShortRetry , defaultShortRetry ),
139
- PruneWakeup : defaultValDuration (o .PruneWakeup , defaultPruneWakeup ),
140
- BitSuffixLength : defaultValInt (o .BitSuffixLength , defaultBitSuffixLength ),
141
- SaturationPeers : defaultValInt (o .SaturationPeers , defaultSaturationPeers ),
142
- OverSaturationPeers : defaultValInt (o .OverSaturationPeers , defaultOverSaturationPeers ),
140
+ TimeToRetry : defaultValDuration (o .TimeToRetry , defaultTimeToRetry ),
141
+ ShortRetry : defaultValDuration (o .ShortRetry , defaultShortRetry ),
142
+ PruneWakeup : defaultValDuration (o .PruneWakeup , defaultPruneWakeup ),
143
+ // BitSuffixLength: defaultValInt(o.BitSuffixLength, defaultBitSuffixLength),
143
144
BootnodeOverSaturationPeers : defaultValInt (o .BootnodeOverSaturationPeers , defaultBootNodeOverSaturationPeers ),
144
145
BroadcastBinSize : defaultValInt (o .BroadcastBinSize , defaultBroadcastBinSize ),
145
146
LowWaterMark : defaultValInt (o .LowWaterMark , defaultLowWaterMark ),
@@ -167,11 +168,7 @@ func defaultValDuration(v *time.Duration, d time.Duration) time.Duration {
167
168
}
168
169
169
170
func makeSaturationFunc (o kadOptions ) binSaturationFunc {
170
- os := o .OverSaturationPeers
171
- if o .BootnodeMode {
172
- os = o .BootnodeOverSaturationPeers
173
- }
174
- return binSaturated (os , isStaticPeer (o .StaticNodes ))
171
+ return binSaturated (isStaticPeer (o .StaticNodes ))
175
172
}
176
173
177
174
// Kad is the Swarm forwarding kademlia implementation.
@@ -258,11 +255,7 @@ func New(
258
255
k .opt .PruneFunc = k .pruneOversaturatedBins
259
256
}
260
257
261
- os := k .opt .OverSaturationPeers
262
- if k .opt .BootnodeMode {
263
- os = k .opt .BootnodeOverSaturationPeers
264
- }
265
- k .opt .PruneCountFunc = binPruneCount (os , isStaticPeer (k .opt .StaticNodes ))
258
+ k .opt .PruneCountFunc = binPruneCount (isStaticPeer (k .opt .StaticNodes ))
266
259
267
260
if k .opt .ExcludeFunc == nil {
268
261
k .opt .ExcludeFunc = func (f ... im.ExcludeOp ) peerExcludeFunc {
@@ -272,9 +265,7 @@ func New(
272
265
}
273
266
}
274
267
275
- if k .opt .BitSuffixLength > 0 {
276
- k .commonBinPrefixes = generateCommonBinPrefixes (k .base , k .opt .BitSuffixLength )
277
- }
268
+ k .commonBinPrefixes = generateCommonBinPrefixes (k .base , prefixBits [:])
278
269
279
270
k .bgBroadcastCtx , k .bgBroadcastCancel = context .WithCancel (context .Background ())
280
271
@@ -317,12 +308,12 @@ func (k *Kad) connectBalanced(wg *sync.WaitGroup, peerConnChan chan<- *peerConnI
317
308
318
309
// Connect to closest known peer which we haven't tried connecting to recently.
319
310
320
- _ , exists := nClosePeerInSlice (binConnectedPeers , pseudoAddr , noopSanctionedPeerFn , uint8 (i + k . opt . BitSuffixLength + 1 ))
311
+ _ , exists := nClosePeerInSlice (binConnectedPeers , pseudoAddr , noopSanctionedPeerFn , uint8 (i + prefixBits [ i ] + 1 ))
321
312
if exists {
322
313
continue
323
314
}
324
315
325
- closestKnownPeer , exists := nClosePeerInSlice (binPeers , pseudoAddr , skipPeers , uint8 (i + k . opt . BitSuffixLength + 1 ))
316
+ closestKnownPeer , exists := nClosePeerInSlice (binPeers , pseudoAddr , skipPeers , uint8 (i + prefixBits [ i ] + 1 ))
326
317
if ! exists {
327
318
continue
328
319
}
@@ -738,7 +729,7 @@ func (k *Kad) balancedSlotPeers(pseudoAddr swarm.Address, peers []swarm.Address,
738
729
var ret []swarm.Address
739
730
740
731
for _ , peer := range peers {
741
- if int (swarm .ExtendedProximity (peer .Bytes (), pseudoAddr .Bytes ())) >= po + k . opt . BitSuffixLength + 1 {
732
+ if int (swarm .ExtendedProximity (peer .Bytes (), pseudoAddr .Bytes ())) >= po + prefixBits [ po ] + 1 {
742
733
ret = append (ret , peer )
743
734
}
744
735
}
@@ -866,7 +857,7 @@ func (k *Kad) connectBootNodes(ctx context.Context) {
866
857
// binSaturated indicates whether a certain bin is saturated or not.
867
858
// when a bin is not saturated it means we would like to proactively
868
859
// initiate connections to other peers in the bin.
869
- func binSaturated (oversaturationAmount int , staticNode staticPeerFunc ) binSaturationFunc {
860
+ func binSaturated (staticNode staticPeerFunc ) binSaturationFunc {
870
861
return func (bin uint8 , connected * pslice.PSlice , exclude peerExcludeFunc ) bool {
871
862
size := 0
872
863
_ = connected .EachBin (func (addr swarm.Address , po uint8 ) (bool , bool , error ) {
@@ -881,7 +872,7 @@ func binSaturated(oversaturationAmount int, staticNode staticPeerFunc) binSatura
881
872
}
882
873
883
874
// binPruneCount counts how many peers should be pruned from a bin.
884
- func binPruneCount (oversaturationAmount int , staticNode staticPeerFunc ) pruneCountFunc {
875
+ func binPruneCount (staticNode staticPeerFunc ) pruneCountFunc {
885
876
return func (bin uint8 , connected * pslice.PSlice , exclude peerExcludeFunc ) (int , int ) {
886
877
size := 0
887
878
_ = connected .EachBin (func (addr swarm.Address , po uint8 ) (bool , bool , error ) {
0 commit comments