Skip to content

Commit 422604b

Browse files
zsfelfoldifjl
authored andcommitted
les: rename UpdateBalance to AddBalance and simplify return format (#20304)
1 parent 57d6976 commit 422604b

5 files changed

Lines changed: 28 additions & 31 deletions

File tree

‎internal/web3ext/web3ext.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -824,8 +824,8 @@ web3._extend({
824824
params: 1
825825
}),
826826
new web3._extend.Method({
827-
name: 'updateBalance',
828-
call: 'les_updateBalance',
827+
name: 'addBalance',
828+
call: 'les_addBalance',
829829
params: 3
830830
}),
831831
],

‎les/api.go‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,11 @@ func (api *PrivateLightServerAPI) setParams(params map[string]interface{}, clien
170170
return
171171
}
172172

173-
// UpdateBalance updates the balance of a client (either overwrites it or adds to it).
173+
// AddBalance updates the balance of a client (either overwrites it or adds to it).
174174
// It also updates the balance meta info string.
175-
func (api *PrivateLightServerAPI) UpdateBalance(id enode.ID, value int64, meta string) (map[string]uint64, error) {
176-
oldBalance, newBalance, err := api.server.clientPool.updateBalance(id, value, meta)
177-
m := make(map[string]uint64)
178-
m["old"] = oldBalance
179-
m["new"] = newBalance
180-
return m, err
175+
func (api *PrivateLightServerAPI) AddBalance(id enode.ID, value int64, meta string) ([2]uint64, error) {
176+
oldBalance, newBalance, err := api.server.clientPool.addBalance(id, value, meta)
177+
return [2]uint64{oldBalance, newBalance}, err
181178
}
182179

183180
// SetClientParams sets client parameters for all clients listed in the ids list

‎les/balance.go‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (bt *balanceTracker) stop(now mclock.AbsTime) {
8080
defer bt.lock.Unlock()
8181

8282
bt.stopped = true
83-
bt.updateBalance(now)
83+
bt.addBalance(now)
8484
bt.negTimeFactor = 0
8585
bt.negRequestFactor = 0
8686
bt.timeFactor = 0
@@ -173,7 +173,7 @@ func (bt *balanceTracker) getPriority(now mclock.AbsTime) int64 {
173173
bt.lock.Lock()
174174
defer bt.lock.Unlock()
175175

176-
bt.updateBalance(now)
176+
bt.addBalance(now)
177177
return bt.balanceToPriority(bt.balance)
178178
}
179179

@@ -194,8 +194,8 @@ func (bt *balanceTracker) estimatedPriority(at mclock.AbsTime, addReqCost bool)
194194
return bt.balanceToPriority(bt.reducedBalance(at, avgReqCost))
195195
}
196196

197-
// updateBalance updates balance based on the time factor
198-
func (bt *balanceTracker) updateBalance(now mclock.AbsTime) {
197+
// addBalance updates balance based on the time factor
198+
func (bt *balanceTracker) addBalance(now mclock.AbsTime) {
199199
if now > bt.lastUpdate {
200200
bt.balance = bt.reducedBalance(now, 0)
201201
bt.lastUpdate = now
@@ -253,7 +253,7 @@ func (bt *balanceTracker) updateAfter(dt time.Duration) {
253253

254254
if bt.callbackCount != 0 {
255255
now := bt.clock.Now()
256-
bt.updateBalance(now)
256+
bt.addBalance(now)
257257
bt.checkCallbacks(now)
258258
}
259259
})
@@ -270,7 +270,7 @@ func (bt *balanceTracker) requestCost(cost uint64) {
270270
return
271271
}
272272
now := bt.clock.Now()
273-
bt.updateBalance(now)
273+
bt.addBalance(now)
274274
fcost := float64(cost)
275275

276276
if bt.balance.pos != 0 {
@@ -302,7 +302,7 @@ func (bt *balanceTracker) getBalance(now mclock.AbsTime) (uint64, uint64) {
302302
bt.lock.Lock()
303303
defer bt.lock.Unlock()
304304

305-
bt.updateBalance(now)
305+
bt.addBalance(now)
306306
return bt.balance.pos, bt.balance.neg
307307
}
308308

@@ -312,7 +312,7 @@ func (bt *balanceTracker) setBalance(pos, neg uint64) error {
312312
defer bt.lock.Unlock()
313313

314314
now := bt.clock.Now()
315-
bt.updateBalance(now)
315+
bt.addBalance(now)
316316
bt.balance.pos = pos
317317
bt.balance.neg = neg
318318
bt.checkCallbacks(now)
@@ -329,7 +329,7 @@ func (bt *balanceTracker) setFactors(neg bool, timeFactor, requestFactor float64
329329
return
330330
}
331331
now := bt.clock.Now()
332-
bt.updateBalance(now)
332+
bt.addBalance(now)
333333
if neg {
334334
bt.negTimeFactor = timeFactor
335335
bt.negRequestFactor = requestFactor
@@ -360,7 +360,7 @@ func (bt *balanceTracker) addCallback(id int, threshold int64, callback func())
360360
bt.callbackIndex[id] = idx
361361
bt.callbacks[idx] = balanceCallback{id, threshold, callback}
362362
now := bt.clock.Now()
363-
bt.updateBalance(now)
363+
bt.addBalance(now)
364364
bt.checkCallbacks(now)
365365
}
366366

‎les/clientpool.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,9 @@ func (f *clientPool) getPosBalance(id enode.ID) posBalance {
553553
return f.ndb.getOrNewPB(id)
554554
}
555555

556-
// updateBalance updates the balance of a client (either overwrites it or adds to it).
556+
// addBalance updates the balance of a client (either overwrites it or adds to it).
557557
// It also updates the balance meta info string.
558-
func (f *clientPool) updateBalance(id enode.ID, amount int64, meta string) (uint64, uint64, error) {
558+
func (f *clientPool) addBalance(id enode.ID, amount int64, meta string) (uint64, uint64, error) {
559559
f.lock.Lock()
560560
defer f.lock.Unlock()
561561

‎les/clientpool_test.go‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func testClientPool(t *testing.T, connLimit, clientCount, paidCount int, randomD
111111
// give a positive balance to some of the peers
112112
amount := testClientPoolTicks / 2 * int64(time.Second) // enough for half of the simulation period
113113
for i := 0; i < paidCount; i++ {
114-
pool.updateBalance(poolTestPeer(i).ID(), amount, "")
114+
pool.addBalance(poolTestPeer(i).ID(), amount, "")
115115
}
116116
}
117117

@@ -178,7 +178,7 @@ func TestConnectPaidClient(t *testing.T) {
178178
pool.setDefaultFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1})
179179

180180
// Add balance for an external client and mark it as paid client
181-
pool.updateBalance(poolTestPeer(0).ID(), 1000, "")
181+
pool.addBalance(poolTestPeer(0).ID(), 1000, "")
182182

183183
if !pool.connect(poolTestPeer(0), 10) {
184184
t.Fatalf("Failed to connect paid client")
@@ -196,7 +196,7 @@ func TestConnectPaidClientToSmallPool(t *testing.T) {
196196
pool.setDefaultFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1})
197197

198198
// Add balance for an external client and mark it as paid client
199-
pool.updateBalance(poolTestPeer(0).ID(), 1000, "")
199+
pool.addBalance(poolTestPeer(0).ID(), 1000, "")
200200

201201
// Connect a fat paid client to pool, should reject it.
202202
if pool.connect(poolTestPeer(0), 100) {
@@ -216,15 +216,15 @@ func TestConnectPaidClientToFullPool(t *testing.T) {
216216
pool.setDefaultFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1})
217217

218218
for i := 0; i < 10; i++ {
219-
pool.updateBalance(poolTestPeer(i).ID(), 1000000000, "")
219+
pool.addBalance(poolTestPeer(i).ID(), 1000000000, "")
220220
pool.connect(poolTestPeer(i), 1)
221221
}
222-
pool.updateBalance(poolTestPeer(11).ID(), 1000, "") // Add low balance to new paid client
222+
pool.addBalance(poolTestPeer(11).ID(), 1000, "") // Add low balance to new paid client
223223
if pool.connect(poolTestPeer(11), 1) {
224224
t.Fatalf("Low balance paid client should be rejected")
225225
}
226226
clock.Run(time.Second)
227-
pool.updateBalance(poolTestPeer(12).ID(), 1000000000*60*3, "") // Add high balance to new paid client
227+
pool.addBalance(poolTestPeer(12).ID(), 1000000000*60*3, "") // Add high balance to new paid client
228228
if !pool.connect(poolTestPeer(12), 1) {
229229
t.Fatalf("High balance paid client should be accpected")
230230
}
@@ -243,7 +243,7 @@ func TestPaidClientKickedOut(t *testing.T) {
243243
pool.setDefaultFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1})
244244

245245
for i := 0; i < 10; i++ {
246-
pool.updateBalance(poolTestPeer(i).ID(), 1000000000, "") // 1 second allowance
246+
pool.addBalance(poolTestPeer(i).ID(), 1000000000, "") // 1 second allowance
247247
pool.connect(poolTestPeer(i), 1)
248248
clock.Run(time.Millisecond)
249249
}
@@ -351,7 +351,7 @@ func TestPositiveBalanceCalculation(t *testing.T) {
351351
pool.setLimits(10, uint64(10)) // Total capacity limit is 10
352352
pool.setDefaultFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1})
353353

354-
pool.updateBalance(poolTestPeer(0).ID(), int64(time.Minute*3), "")
354+
pool.addBalance(poolTestPeer(0).ID(), int64(time.Minute*3), "")
355355
pool.connect(poolTestPeer(0), 10)
356356
clock.Run(time.Minute)
357357

@@ -377,7 +377,7 @@ func TestDowngradePriorityClient(t *testing.T) {
377377
p := &poolTestPeerWithCap{
378378
poolTestPeer: poolTestPeer(0),
379379
}
380-
pool.updateBalance(p.ID(), int64(time.Minute), "")
380+
pool.addBalance(p.ID(), int64(time.Minute), "")
381381
pool.connect(p, 10)
382382
if p.cap != 10 {
383383
t.Fatalf("The capcacity of priority peer hasn't been updated, got: %d", p.cap)
@@ -393,7 +393,7 @@ func TestDowngradePriorityClient(t *testing.T) {
393393
t.Fatalf("Positive balance mismatch, want %v, got %v", 0, pb.value)
394394
}
395395

396-
pool.updateBalance(poolTestPeer(0).ID(), int64(time.Minute), "")
396+
pool.addBalance(poolTestPeer(0).ID(), int64(time.Minute), "")
397397
pb = pool.ndb.getOrNewPB(poolTestPeer(0).ID())
398398
if pb.value != uint64(time.Minute) {
399399
t.Fatalf("Positive balance mismatch, want %v, got %v", uint64(time.Minute), pb.value)

0 commit comments

Comments
 (0)