Skip to content

Commit 1b0b619

Browse files
committed
les: restored setConnectedBias function
1 parent 35f26f9 commit 1b0b619

File tree

6 files changed

+48
-27
lines changed

6 files changed

+48
-27
lines changed

les/api.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,13 @@ func (api *PrivateLightServerAPI) SetDefaultParams(params map[string]interface{}
192192
// So that already connected client won't be kicked out very soon and we can ensure all
193193
// connected clients can have enough time to request or sync some data.
194194
// When the input parameter `bias` < 0 (illegal), return error.
195-
/*func (api *PrivateLightServerAPI) SetConnectedBias(bias time.Duration) error {
195+
func (api *PrivateLightServerAPI) SetConnectedBias(bias time.Duration) error {
196196
if bias < time.Duration(0) {
197197
return fmt.Errorf("bias illegal: %v less than 0", bias)
198198
}
199199
api.server.clientPool.setConnectedBias(bias)
200200
return nil
201-
}*/
202-
//TODO fix regression
201+
}
203202

204203
// AddBalance updates the balance of a client (either overwrites it or adds to it).
205204
// It also updates the balance meta info string.

les/clientpool.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ const (
3636
defaultPosExpTC = 36000 // default time constant (in seconds) for exponentially reducing positive balance
3737
defaultNegExpTC = 3600 // default time constant (in seconds) for exponentially reducing negative balance
3838

39-
// activeBias is applied to already connected clients So that
39+
// defaultConnectedBias is applied to already connected clients So that
4040
// already connected client won't be kicked out very soon and we
4141
// can ensure all connected clients can have enough time to request
4242
// or sync some data.
4343
//
4444
// todo(rjl493456442) make it configurable. It can be the option of
4545
// free trial time!
46-
activeBias = time.Minute * 3
47-
inactiveTimeout = time.Second * 10
46+
defaultConnectedBias = time.Minute * 3
47+
inactiveTimeout = time.Second * 10
4848
)
4949

5050
var (
@@ -93,6 +93,7 @@ type clientPool struct {
9393
defaultPosFactors, defaultNegFactors lps.PriceFactors
9494
posExpTC, negExpTC uint64
9595
minCap uint64 // The minimal capacity value allowed for any client
96+
connectedBias time.Duration
9697
capLimit uint64
9798
freeClientCap uint64 // The capacity value of each free client
9899
}
@@ -121,7 +122,7 @@ type clientInfo struct {
121122
}
122123

123124
// newClientPool creates a new client pool
124-
func newClientPool(lespayDb ethdb.Database, minCap, freeClientCap uint64, activeBias time.Duration, clock mclock.Clock, removePeer func(enode.ID)) *clientPool {
125+
func newClientPool(lespayDb ethdb.Database, minCap, freeClientCap uint64, connectedBias time.Duration, clock mclock.Clock, removePeer func(enode.ID)) *clientPool {
125126
if minCap > freeClientCap {
126127
panic(nil)
127128
}
@@ -132,11 +133,12 @@ func newClientPool(lespayDb ethdb.Database, minCap, freeClientCap uint64, active
132133
PriorityPoolSetup: priorityPoolSetup,
133134
clock: clock,
134135
minCap: minCap,
136+
connectedBias: connectedBias,
135137
freeClientCap: freeClientCap,
136138
removePeer: removePeer,
137139
}
138140
pool.bt = lps.NewBalanceTracker(ns, balanceTrackerSetup, lespayDb, clock, &utils.Expirer{}, &utils.Expirer{})
139-
pool.pp = lps.NewPriorityPool(ns, priorityPoolSetup, clock, minCap, activeBias, 4)
141+
pool.pp = lps.NewPriorityPool(ns, priorityPoolSetup, clock, minCap, connectedBias, 4)
140142

141143
// set default expiration constants used by tests
142144
// Note: server overwrites this if token sale is active
@@ -242,7 +244,7 @@ func (f *clientPool) connect(peer clientPoolPeer, reqCapacity uint64) (uint64, e
242244
}
243245

244246
f.ns.SetState(node, f.InactiveFlag, nodestate.Flags{}, 0)
245-
if _, allowed := f.pp.RequestCapacity(node, reqCapacity, activeBias, true); allowed {
247+
if _, allowed := f.pp.RequestCapacity(node, reqCapacity, f.connectedBias, true); allowed {
246248
return reqCapacity, nil
247249
}
248250
if !peer.allowInactive() {
@@ -251,6 +253,17 @@ func (f *clientPool) connect(peer clientPoolPeer, reqCapacity uint64) (uint64, e
251253
return 0, nil
252254
}
253255

256+
// setConnectedBias sets the connection bias, which is applied to already connected clients
257+
// So that already connected client won't be kicked out very soon and we can ensure all
258+
// connected clients can have enough time to request or sync some data.
259+
func (f *clientPool) setConnectedBias(bias time.Duration) {
260+
f.lock.Lock()
261+
defer f.lock.Unlock()
262+
263+
f.connectedBias = bias
264+
f.pp.SetActiveBias(bias)
265+
}
266+
254267
// disconnect should be called when a connection is terminated. If the disconnection
255268
// was initiated by the pool itself using disconnectFn then calling disconnect is
256269
// not necessary but permitted.

les/clientpool_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func TestConnectPaidClient(t *testing.T) {
218218
clock mclock.Simulated
219219
db = rawdb.NewMemoryDatabase()
220220
)
221-
pool := newClientPool(db, 1, 1, activeBias, &clock, func(id enode.ID) {})
221+
pool := newClientPool(db, 1, 1, defaultConnectedBias, &clock, func(id enode.ID) {})
222222
defer pool.stop()
223223
pool.setLimits(10, uint64(10))
224224
pool.setDefaultFactors(lps.PriceFactors{1, 0, 1}, lps.PriceFactors{1, 0, 1})
@@ -236,7 +236,7 @@ func TestConnectPaidClientToSmallPool(t *testing.T) {
236236
clock mclock.Simulated
237237
db = rawdb.NewMemoryDatabase()
238238
)
239-
pool := newClientPool(db, 1, 1, activeBias, &clock, func(id enode.ID) {})
239+
pool := newClientPool(db, 1, 1, defaultConnectedBias, &clock, func(id enode.ID) {})
240240
defer pool.stop()
241241
pool.setLimits(10, uint64(10)) // Total capacity limit is 10
242242
pool.setDefaultFactors(lps.PriceFactors{1, 0, 1}, lps.PriceFactors{1, 0, 1})
@@ -256,7 +256,7 @@ func TestConnectPaidClientToFullPool(t *testing.T) {
256256
db = rawdb.NewMemoryDatabase()
257257
)
258258
removeFn := func(enode.ID) {} // Noop
259-
pool := newClientPool(db, 1, 1, activeBias, &clock, removeFn)
259+
pool := newClientPool(db, 1, 1, defaultConnectedBias, &clock, removeFn)
260260
defer pool.stop()
261261
pool.setLimits(10, uint64(10)) // Total capacity limit is 10
262262
pool.setDefaultFactors(lps.PriceFactors{1, 0, 1}, lps.PriceFactors{1, 0, 1})
@@ -285,7 +285,7 @@ func TestPaidClientKickedOut(t *testing.T) {
285285
removeFn := func(id enode.ID) {
286286
kickedCh <- int(id[0])
287287
}
288-
pool := newClientPool(db, 1, 1, activeBias, &clock, removeFn)
288+
pool := newClientPool(db, 1, 1, defaultConnectedBias, &clock, removeFn)
289289
pool.bt.SetExpirationTCs(0, 0)
290290
defer pool.stop()
291291
pool.setLimits(10, uint64(10)) // Total capacity limit is 10
@@ -296,7 +296,7 @@ func TestPaidClientKickedOut(t *testing.T) {
296296
pool.connect(newPoolTestPeer(i, kickedCh), 1)
297297
clock.Run(time.Millisecond)
298298
}
299-
clock.Run(activeBias + time.Second*11)
299+
clock.Run(defaultConnectedBias + time.Second*11)
300300
if cap, _ := pool.connect(newPoolTestPeer(11, kickedCh), 0); cap == 0 {
301301
t.Fatalf("Free client should be accepted")
302302
}
@@ -315,7 +315,7 @@ func TestConnectFreeClient(t *testing.T) {
315315
clock mclock.Simulated
316316
db = rawdb.NewMemoryDatabase()
317317
)
318-
pool := newClientPool(db, 1, 1, activeBias, &clock, func(id enode.ID) {})
318+
pool := newClientPool(db, 1, 1, defaultConnectedBias, &clock, func(id enode.ID) {})
319319
defer pool.stop()
320320
pool.setLimits(10, uint64(10))
321321
pool.setDefaultFactors(lps.PriceFactors{1, 0, 1}, lps.PriceFactors{1, 0, 1})
@@ -333,7 +333,7 @@ func TestConnectFreeClientToFullPool(t *testing.T) {
333333
db = rawdb.NewMemoryDatabase()
334334
)
335335
removeFn := func(enode.ID) {} // Noop
336-
pool := newClientPool(db, 1, 1, activeBias, &clock, removeFn)
336+
pool := newClientPool(db, 1, 1, defaultConnectedBias, &clock, removeFn)
337337
defer pool.stop()
338338
pool.setLimits(10, uint64(10)) // Total capacity limit is 10
339339
pool.setDefaultFactors(lps.PriceFactors{1, 0, 1}, lps.PriceFactors{1, 0, 1})
@@ -362,7 +362,7 @@ func TestFreeClientKickedOut(t *testing.T) {
362362
kicked = make(chan int, 100)
363363
)
364364
removeFn := func(id enode.ID) { kicked <- int(id[0]) }
365-
pool := newClientPool(db, 1, 1, activeBias, &clock, removeFn)
365+
pool := newClientPool(db, 1, 1, defaultConnectedBias, &clock, removeFn)
366366
defer pool.stop()
367367
pool.setLimits(10, uint64(10)) // Total capacity limit is 10
368368
pool.setDefaultFactors(lps.PriceFactors{1, 0, 1}, lps.PriceFactors{1, 0, 1})
@@ -403,7 +403,7 @@ func TestPositiveBalanceCalculation(t *testing.T) {
403403
kicked = make(chan int, 10)
404404
)
405405
removeFn := func(id enode.ID) { kicked <- int(id[0]) } // Noop
406-
pool := newClientPool(db, 1, 1, activeBias, &clock, removeFn)
406+
pool := newClientPool(db, 1, 1, defaultConnectedBias, &clock, removeFn)
407407
defer pool.stop()
408408
pool.setLimits(10, uint64(10)) // Total capacity limit is 10
409409
pool.setDefaultFactors(lps.PriceFactors{1, 0, 1}, lps.PriceFactors{1, 0, 1})
@@ -426,7 +426,7 @@ func TestDowngradePriorityClient(t *testing.T) {
426426
kicked = make(chan int, 10)
427427
)
428428
removeFn := func(id enode.ID) { kicked <- int(id[0]) } // Noop
429-
pool := newClientPool(db, 1, 1, activeBias, &clock, removeFn)
429+
pool := newClientPool(db, 1, 1, defaultConnectedBias, &clock, removeFn)
430430
defer pool.stop()
431431
pool.setLimits(10, uint64(10)) // Total capacity limit is 10
432432
pool.setDefaultFactors(lps.PriceFactors{1, 0, 1}, lps.PriceFactors{1, 0, 1})
@@ -460,7 +460,7 @@ func TestNegativeBalanceCalculation(t *testing.T) {
460460
clock mclock.Simulated
461461
db = rawdb.NewMemoryDatabase()
462462
)
463-
pool := newClientPool(db, 1, 1, activeBias, &clock, func(id enode.ID) {})
463+
pool := newClientPool(db, 1, 1, defaultConnectedBias, &clock, func(id enode.ID) {})
464464
defer pool.stop()
465465
pool.setLimits(10, uint64(10)) // Total capacity limit is 10
466466
pool.setDefaultFactors(lps.PriceFactors{1e-3, 0, 1}, lps.PriceFactors{1e-3, 0, 1})
@@ -495,7 +495,7 @@ func TestInactiveClient(t *testing.T) {
495495
clock mclock.Simulated
496496
db = rawdb.NewMemoryDatabase()
497497
)
498-
pool := newClientPool(db, 1, 1, activeBias, &clock, func(id enode.ID) {})
498+
pool := newClientPool(db, 1, 1, defaultConnectedBias, &clock, func(id enode.ID) {})
499499
defer pool.stop()
500500
pool.setLimits(2, uint64(2))
501501

les/lespay/server/prioritypool.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ type PriorityPool struct {
9999
activeCount, activeCap uint64
100100
maxCount, maxCap uint64
101101
minCap uint64
102-
minBias time.Duration
102+
activeBias time.Duration
103103
capacityStepDiv uint64
104104
}
105105

@@ -126,15 +126,15 @@ type ppNodeInfo struct {
126126
}
127127

128128
// NewPriorityPool creates a new PriorityPool
129-
func NewPriorityPool(ns *nodestate.NodeStateMachine, setup PriorityPoolSetup, clock mclock.Clock, minCap uint64, minBias time.Duration, capacityStepDiv uint64) *PriorityPool {
129+
func NewPriorityPool(ns *nodestate.NodeStateMachine, setup PriorityPoolSetup, clock mclock.Clock, minCap uint64, activeBias time.Duration, capacityStepDiv uint64) *PriorityPool {
130130
pp := &PriorityPool{
131131
ns: ns,
132132
PriorityPoolSetup: setup,
133133
clock: clock,
134134
activeQueue: prque.NewLazyQueue(activeSetIndex, activePriority, activeMaxPriority, clock, lazyQueueRefresh),
135135
inactiveQueue: prque.New(inactiveSetIndex),
136136
minCap: minCap,
137-
minBias: minBias,
137+
activeBias: activeBias,
138138
capacityStepDiv: capacityStepDiv,
139139
}
140140

@@ -246,6 +246,15 @@ func (pp *PriorityPool) SetLimits(maxCount, maxCap uint64) {
246246
}
247247
}
248248

249+
// SetActiveBias sets the bias applied when trying to activate inactive nodes
250+
func (pp *PriorityPool) SetActiveBias(bias time.Duration) {
251+
pp.lock.Lock()
252+
defer pp.lock.Unlock()
253+
254+
pp.activeBias = bias
255+
pp.tryActivate()
256+
}
257+
249258
// ActiveCapacity returns the total capacity of currently active nodes
250259
func (pp *PriorityPool) ActiveCapacity() uint64 {
251260
pp.lock.Lock()
@@ -452,7 +461,7 @@ func (pp *PriorityPool) tryActivate() []capUpdate {
452461
c := pp.inactiveQueue.PopItem().(*ppNodeInfo)
453462
pp.markForChange(c)
454463
pp.setCapacity(c, pp.minCap)
455-
c.bias = pp.minBias
464+
c.bias = pp.activeBias
456465
pp.activeQueue.Push(c)
457466
pp.enforceLimits()
458467
if c.capacity > 0 {

les/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func NewLesServer(node *node.Node, e *eth.Ethereum, config *eth.Config) (*LesSer
117117
srv.maxCapacity = totalRecharge
118118
}
119119
srv.fcManager.SetCapacityLimits(srv.freeCapacity, srv.maxCapacity, srv.freeCapacity*2)
120-
srv.clientPool = newClientPool(srv.chainDb, srv.minCapacity, srv.freeCapacity, activeBias, mclock.System{}, func(id enode.ID) { go srv.peers.disconnect(id.String()) })
120+
srv.clientPool = newClientPool(srv.chainDb, srv.minCapacity, srv.freeCapacity, defaultConnectedBias, mclock.System{}, func(id enode.ID) { go srv.peers.disconnect(id.String()) })
121121
srv.clientPool.setDefaultFactors(lps.PriceFactors{0, 1, 1}, lps.PriceFactors{0, 1, 1})
122122

123123
checkpoint := srv.latestLocalCheckpoint()

les/test_helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func newTestServerHandler(blocks int, indexers []*core.ChainIndexer, db ethdb.Da
284284
}
285285
server.costTracker, server.freeCapacity = newCostTracker(db, server.config)
286286
server.costTracker.testCostList = testCostList(0) // Disable flow control mechanism.
287-
server.clientPool = newClientPool(db, testBufRecharge, testBufRecharge, activeBias, clock, func(id enode.ID) {})
287+
server.clientPool = newClientPool(db, testBufRecharge, testBufRecharge, defaultConnectedBias, clock, func(id enode.ID) {})
288288
server.clientPool.setLimits(10000, 10000) // Assign enough capacity for clientpool
289289
server.handler = newServerHandler(server, simulation.Blockchain(), db, txpool, func() bool { return true })
290290
if server.oracle != nil {

0 commit comments

Comments
 (0)