Skip to content

Commit 6d7c956

Browse files
authored
les, tests: fix les clientpool (#22756)
* les, tests: fix les clientpool * tests: disable debug mode * les: polish code
1 parent 9e5bb84 commit 6d7c956

3 files changed

Lines changed: 67 additions & 19 deletions

File tree

‎les/vflux/server/clientpool.go‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,11 @@ func (cp *ClientPool) SetCapacity(node *enode.Node, reqCap uint64, bias time.Dur
239239
maxTarget = curve.maxCapacity(func(capacity uint64) int64 {
240240
return balance.estimatePriority(capacity, 0, 0, bias, false)
241241
})
242-
if maxTarget <= capacity {
242+
if maxTarget < reqCap {
243243
return
244244
}
245-
if maxTarget > reqCap {
246-
maxTarget = reqCap
247-
}
245+
maxTarget = reqCap
246+
248247
// Specify a narrow target range that allows a limited number of fine step
249248
// iterations
250249
minTarget = maxTarget - maxTarget/20

‎tests/fuzzers/vflux/clientpool-fuzzer.go‎

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,22 @@ import (
2828
"github.com/ethereum/go-ethereum/ethdb/memorydb"
2929
"github.com/ethereum/go-ethereum/les/vflux"
3030
vfs "github.com/ethereum/go-ethereum/les/vflux/server"
31+
"github.com/ethereum/go-ethereum/log"
3132
"github.com/ethereum/go-ethereum/p2p/enode"
3233
"github.com/ethereum/go-ethereum/p2p/enr"
3334
"github.com/ethereum/go-ethereum/rlp"
3435
)
3536

37+
var (
38+
debugMode = false
39+
doLog = func(msg string, ctx ...interface{}) {
40+
if !debugMode {
41+
return
42+
}
43+
log.Info(msg, ctx...)
44+
}
45+
)
46+
3647
type fuzzer struct {
3748
peers [256]*clientPeer
3849
disconnectList []*clientPeer
@@ -65,6 +76,7 @@ func (p *clientPeer) InactiveAllowance() time.Duration {
6576
}
6677

6778
func (p *clientPeer) UpdateCapacity(newCap uint64, requested bool) {
79+
origin, originTotal := p.capacity, p.fuzzer.activeCap
6880
p.fuzzer.activeCap -= p.capacity
6981
if p.capacity != 0 {
7082
p.fuzzer.activeCount--
@@ -74,16 +86,19 @@ func (p *clientPeer) UpdateCapacity(newCap uint64, requested bool) {
7486
if p.capacity != 0 {
7587
p.fuzzer.activeCount++
7688
}
89+
doLog("Update capacity", "peer", p.node.ID(), "origin", origin, "cap", newCap, "origintotal", originTotal, "total", p.fuzzer.activeCap, "requested", requested)
7790
}
7891

7992
func (p *clientPeer) Disconnect() {
93+
origin, originTotal := p.capacity, p.fuzzer.activeCap
8094
p.fuzzer.disconnectList = append(p.fuzzer.disconnectList, p)
8195
p.fuzzer.activeCap -= p.capacity
8296
if p.capacity != 0 {
8397
p.fuzzer.activeCount--
8498
}
8599
p.capacity = 0
86100
p.balance = nil
101+
doLog("Disconnect", "peer", p.node.ID(), "origin", origin, "origintotal", originTotal, "total", p.fuzzer.activeCap)
87102
}
88103

89104
func newFuzzer(input []byte) *fuzzer {
@@ -165,25 +180,33 @@ func (f *fuzzer) randomFactors() vfs.PriceFactors {
165180
}
166181
}
167182

168-
func (f *fuzzer) connectedBalanceOp(balance vfs.ConnectedBalance) {
183+
func (f *fuzzer) connectedBalanceOp(balance vfs.ConnectedBalance, id enode.ID) {
169184
switch f.randomInt(3) {
170185
case 0:
171-
balance.RequestServed(uint64(f.randomTokenAmount(false)))
186+
cost := uint64(f.randomTokenAmount(false))
187+
balance.RequestServed(cost)
188+
doLog("Serve request cost", "id", id, "amount", cost)
172189
case 1:
173-
balance.SetPriceFactors(f.randomFactors(), f.randomFactors())
190+
posFactor, negFactor := f.randomFactors(), f.randomFactors()
191+
balance.SetPriceFactors(posFactor, negFactor)
192+
doLog("Set price factor", "pos", posFactor, "neg", negFactor)
174193
case 2:
175194
balance.GetBalance()
176195
balance.GetRawBalance()
177196
balance.GetPriceFactors()
178197
}
179198
}
180199

181-
func (f *fuzzer) atomicBalanceOp(balance vfs.AtomicBalanceOperator) {
200+
func (f *fuzzer) atomicBalanceOp(balance vfs.AtomicBalanceOperator, id enode.ID) {
182201
switch f.randomInt(3) {
183202
case 0:
184-
balance.AddBalance(f.randomTokenAmount(true))
203+
amount := f.randomTokenAmount(true)
204+
balance.AddBalance(amount)
205+
doLog("Add balance", "id", id, "amount", amount)
185206
case 1:
186-
balance.SetBalance(uint64(f.randomTokenAmount(false)), uint64(f.randomTokenAmount(false)))
207+
pos, neg := uint64(f.randomTokenAmount(false)), uint64(f.randomTokenAmount(false))
208+
balance.SetBalance(pos, neg)
209+
doLog("Set balance", "id", id, "pos", pos, "neg", neg)
187210
case 2:
188211
balance.GetBalance()
189212
balance.GetRawBalance()
@@ -212,33 +235,53 @@ func FuzzClientPool(input []byte) int {
212235
case 0:
213236
i := int(f.randomByte())
214237
f.peers[i].balance = pool.Register(f.peers[i])
238+
doLog("Register peer", "id", f.peers[i].node.ID())
215239
case 1:
216240
i := int(f.randomByte())
217241
f.peers[i].Disconnect()
242+
doLog("Disconnect peer", "id", f.peers[i].node.ID())
218243
case 2:
219244
f.maxCount = uint64(f.randomByte())
220245
f.maxCap = uint64(f.randomByte())
221246
f.maxCap *= f.maxCap
247+
248+
count, cap := pool.Limits()
222249
pool.SetLimits(f.maxCount, f.maxCap)
250+
doLog("Set limits", "maxcount", f.maxCount, "maxcap", f.maxCap, "origincount", count, "oricap", cap)
223251
case 3:
252+
bias := f.randomDelay()
224253
pool.SetConnectedBias(f.randomDelay())
254+
doLog("Set connection bias", "bias", bias)
225255
case 4:
226-
pool.SetDefaultFactors(f.randomFactors(), f.randomFactors())
256+
pos, neg := f.randomFactors(), f.randomFactors()
257+
pool.SetDefaultFactors(pos, neg)
258+
doLog("Set default factors", "pos", pos, "neg", neg)
227259
case 5:
228-
pool.SetExpirationTCs(uint64(f.randomInt(50000)), uint64(f.randomInt(50000)))
260+
pos, neg := uint64(f.randomInt(50000)), uint64(f.randomInt(50000))
261+
pool.SetExpirationTCs(pos, neg)
262+
doLog("Set expiration constants", "pos", pos, "neg", neg)
229263
case 6:
230-
if _, err := pool.SetCapacity(f.peers[f.randomByte()].node, uint64(f.randomByte()), f.randomDelay(), f.randomBool()); err == vfs.ErrCantFindMaximum {
264+
var (
265+
index = f.randomByte()
266+
reqCap = uint64(f.randomByte())
267+
bias = f.randomDelay()
268+
requested = f.randomBool()
269+
)
270+
if _, err := pool.SetCapacity(f.peers[index].node, reqCap, bias, requested); err == vfs.ErrCantFindMaximum {
231271
panic(nil)
232272
}
273+
doLog("Set capacity", "id", f.peers[index].node.ID(), "reqcap", reqCap, "bias", bias, "requested", requested)
233274
case 7:
234-
if balance := f.peers[f.randomByte()].balance; balance != nil {
235-
f.connectedBalanceOp(balance)
275+
index := f.randomByte()
276+
if balance := f.peers[index].balance; balance != nil {
277+
f.connectedBalanceOp(balance, f.peers[index].node.ID())
236278
}
237279
case 8:
238-
pool.BalanceOperation(f.peers[f.randomByte()].node.ID(), f.peers[f.randomByte()].freeID, func(balance vfs.AtomicBalanceOperator) {
280+
index := f.randomByte()
281+
pool.BalanceOperation(f.peers[index].node.ID(), f.peers[index].freeID, func(balance vfs.AtomicBalanceOperator) {
239282
count := f.randomInt(4)
240283
for i := 0; i < count; i++ {
241-
f.atomicBalanceOp(balance)
284+
f.atomicBalanceOp(balance, f.peers[index].node.ID())
242285
}
243286
})
244287
case 9:
@@ -272,13 +315,16 @@ func FuzzClientPool(input []byte) int {
272315

273316
for _, peer := range f.disconnectList {
274317
pool.Unregister(peer)
318+
doLog("Unregister peer", "id", peer.node.ID())
275319
}
276320
f.disconnectList = nil
277321
if d := f.randomDelay(); d > 0 {
278322
clock.Run(d)
279323
}
280-
//fmt.Println(f.activeCount, f.maxCount, f.activeCap, f.maxCap)
281-
if activeCount, activeCap := pool.Active(); activeCount != f.activeCount || activeCap != f.activeCap {
324+
doLog("Clientpool stats in fuzzer", "count", f.activeCap, "maxcount", f.maxCount, "cap", f.activeCap, "maxcap", f.maxCap)
325+
activeCount, activeCap := pool.Active()
326+
doLog("Clientpool stats in pool", "count", activeCount, "cap", activeCap)
327+
if activeCount != f.activeCount || activeCap != f.activeCap {
282328
panic(nil)
283329
}
284330
if f.activeCount > f.maxCount || f.activeCap > f.maxCap {

‎tests/fuzzers/vflux/debug/main.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ import (
2121
"io/ioutil"
2222
"os"
2323

24+
"github.com/ethereum/go-ethereum/log"
2425
"github.com/ethereum/go-ethereum/tests/fuzzers/vflux"
2526
)
2627

2728
func main() {
29+
log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
30+
2831
if len(os.Args) != 2 {
2932
fmt.Fprintf(os.Stderr, "Usage: debug <file>\n")
3033
fmt.Fprintf(os.Stderr, "Example\n")

0 commit comments

Comments
 (0)