Skip to content

Commit

Permalink
les, tests: fix les clientpool (#22756)
Browse files Browse the repository at this point in the history
* les, tests: fix les clientpool

* tests: disable debug mode

* les: polish code
  • Loading branch information
rjl493456442 committed Apr 28, 2021
1 parent 9e5bb84 commit 6d7c956
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 19 deletions.
7 changes: 3 additions & 4 deletions les/vflux/server/clientpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,11 @@ func (cp *ClientPool) SetCapacity(node *enode.Node, reqCap uint64, bias time.Dur
maxTarget = curve.maxCapacity(func(capacity uint64) int64 {
return balance.estimatePriority(capacity, 0, 0, bias, false)
})
if maxTarget <= capacity {
if maxTarget < reqCap {
return
}
if maxTarget > reqCap {
maxTarget = reqCap
}
maxTarget = reqCap

// Specify a narrow target range that allows a limited number of fine step
// iterations
minTarget = maxTarget - maxTarget/20
Expand Down
76 changes: 61 additions & 15 deletions tests/fuzzers/vflux/clientpool-fuzzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,22 @@ import (
"github.com/ethereum/go-ethereum/ethdb/memorydb"
"github.com/ethereum/go-ethereum/les/vflux"
vfs "github.com/ethereum/go-ethereum/les/vflux/server"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/ethereum/go-ethereum/rlp"
)

var (
debugMode = false
doLog = func(msg string, ctx ...interface{}) {
if !debugMode {
return
}
log.Info(msg, ctx...)
}
)

type fuzzer struct {
peers [256]*clientPeer
disconnectList []*clientPeer
Expand Down Expand Up @@ -65,6 +76,7 @@ func (p *clientPeer) InactiveAllowance() time.Duration {
}

func (p *clientPeer) UpdateCapacity(newCap uint64, requested bool) {
origin, originTotal := p.capacity, p.fuzzer.activeCap
p.fuzzer.activeCap -= p.capacity
if p.capacity != 0 {
p.fuzzer.activeCount--
Expand All @@ -74,16 +86,19 @@ func (p *clientPeer) UpdateCapacity(newCap uint64, requested bool) {
if p.capacity != 0 {
p.fuzzer.activeCount++
}
doLog("Update capacity", "peer", p.node.ID(), "origin", origin, "cap", newCap, "origintotal", originTotal, "total", p.fuzzer.activeCap, "requested", requested)
}

func (p *clientPeer) Disconnect() {
origin, originTotal := p.capacity, p.fuzzer.activeCap
p.fuzzer.disconnectList = append(p.fuzzer.disconnectList, p)
p.fuzzer.activeCap -= p.capacity
if p.capacity != 0 {
p.fuzzer.activeCount--
}
p.capacity = 0
p.balance = nil
doLog("Disconnect", "peer", p.node.ID(), "origin", origin, "origintotal", originTotal, "total", p.fuzzer.activeCap)
}

func newFuzzer(input []byte) *fuzzer {
Expand Down Expand Up @@ -165,25 +180,33 @@ func (f *fuzzer) randomFactors() vfs.PriceFactors {
}
}

func (f *fuzzer) connectedBalanceOp(balance vfs.ConnectedBalance) {
func (f *fuzzer) connectedBalanceOp(balance vfs.ConnectedBalance, id enode.ID) {
switch f.randomInt(3) {
case 0:
balance.RequestServed(uint64(f.randomTokenAmount(false)))
cost := uint64(f.randomTokenAmount(false))
balance.RequestServed(cost)
doLog("Serve request cost", "id", id, "amount", cost)
case 1:
balance.SetPriceFactors(f.randomFactors(), f.randomFactors())
posFactor, negFactor := f.randomFactors(), f.randomFactors()
balance.SetPriceFactors(posFactor, negFactor)
doLog("Set price factor", "pos", posFactor, "neg", negFactor)
case 2:
balance.GetBalance()
balance.GetRawBalance()
balance.GetPriceFactors()
}
}

func (f *fuzzer) atomicBalanceOp(balance vfs.AtomicBalanceOperator) {
func (f *fuzzer) atomicBalanceOp(balance vfs.AtomicBalanceOperator, id enode.ID) {
switch f.randomInt(3) {
case 0:
balance.AddBalance(f.randomTokenAmount(true))
amount := f.randomTokenAmount(true)
balance.AddBalance(amount)
doLog("Add balance", "id", id, "amount", amount)
case 1:
balance.SetBalance(uint64(f.randomTokenAmount(false)), uint64(f.randomTokenAmount(false)))
pos, neg := uint64(f.randomTokenAmount(false)), uint64(f.randomTokenAmount(false))
balance.SetBalance(pos, neg)
doLog("Set balance", "id", id, "pos", pos, "neg", neg)
case 2:
balance.GetBalance()
balance.GetRawBalance()
Expand Down Expand Up @@ -212,33 +235,53 @@ func FuzzClientPool(input []byte) int {
case 0:
i := int(f.randomByte())
f.peers[i].balance = pool.Register(f.peers[i])
doLog("Register peer", "id", f.peers[i].node.ID())
case 1:
i := int(f.randomByte())
f.peers[i].Disconnect()
doLog("Disconnect peer", "id", f.peers[i].node.ID())
case 2:
f.maxCount = uint64(f.randomByte())
f.maxCap = uint64(f.randomByte())
f.maxCap *= f.maxCap

count, cap := pool.Limits()
pool.SetLimits(f.maxCount, f.maxCap)
doLog("Set limits", "maxcount", f.maxCount, "maxcap", f.maxCap, "origincount", count, "oricap", cap)
case 3:
bias := f.randomDelay()
pool.SetConnectedBias(f.randomDelay())
doLog("Set connection bias", "bias", bias)
case 4:
pool.SetDefaultFactors(f.randomFactors(), f.randomFactors())
pos, neg := f.randomFactors(), f.randomFactors()
pool.SetDefaultFactors(pos, neg)
doLog("Set default factors", "pos", pos, "neg", neg)
case 5:
pool.SetExpirationTCs(uint64(f.randomInt(50000)), uint64(f.randomInt(50000)))
pos, neg := uint64(f.randomInt(50000)), uint64(f.randomInt(50000))
pool.SetExpirationTCs(pos, neg)
doLog("Set expiration constants", "pos", pos, "neg", neg)
case 6:
if _, err := pool.SetCapacity(f.peers[f.randomByte()].node, uint64(f.randomByte()), f.randomDelay(), f.randomBool()); err == vfs.ErrCantFindMaximum {
var (
index = f.randomByte()
reqCap = uint64(f.randomByte())
bias = f.randomDelay()
requested = f.randomBool()
)
if _, err := pool.SetCapacity(f.peers[index].node, reqCap, bias, requested); err == vfs.ErrCantFindMaximum {
panic(nil)
}
doLog("Set capacity", "id", f.peers[index].node.ID(), "reqcap", reqCap, "bias", bias, "requested", requested)
case 7:
if balance := f.peers[f.randomByte()].balance; balance != nil {
f.connectedBalanceOp(balance)
index := f.randomByte()
if balance := f.peers[index].balance; balance != nil {
f.connectedBalanceOp(balance, f.peers[index].node.ID())
}
case 8:
pool.BalanceOperation(f.peers[f.randomByte()].node.ID(), f.peers[f.randomByte()].freeID, func(balance vfs.AtomicBalanceOperator) {
index := f.randomByte()
pool.BalanceOperation(f.peers[index].node.ID(), f.peers[index].freeID, func(balance vfs.AtomicBalanceOperator) {
count := f.randomInt(4)
for i := 0; i < count; i++ {
f.atomicBalanceOp(balance)
f.atomicBalanceOp(balance, f.peers[index].node.ID())
}
})
case 9:
Expand Down Expand Up @@ -272,13 +315,16 @@ func FuzzClientPool(input []byte) int {

for _, peer := range f.disconnectList {
pool.Unregister(peer)
doLog("Unregister peer", "id", peer.node.ID())
}
f.disconnectList = nil
if d := f.randomDelay(); d > 0 {
clock.Run(d)
}
//fmt.Println(f.activeCount, f.maxCount, f.activeCap, f.maxCap)
if activeCount, activeCap := pool.Active(); activeCount != f.activeCount || activeCap != f.activeCap {
doLog("Clientpool stats in fuzzer", "count", f.activeCap, "maxcount", f.maxCount, "cap", f.activeCap, "maxcap", f.maxCap)
activeCount, activeCap := pool.Active()
doLog("Clientpool stats in pool", "count", activeCount, "cap", activeCap)
if activeCount != f.activeCount || activeCap != f.activeCap {
panic(nil)
}
if f.activeCount > f.maxCount || f.activeCap > f.maxCap {
Expand Down
3 changes: 3 additions & 0 deletions tests/fuzzers/vflux/debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ import (
"io/ioutil"
"os"

"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/tests/fuzzers/vflux"
)

func main() {
log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))

if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: debug <file>\n")
fmt.Fprintf(os.Stderr, "Example\n")
Expand Down

0 comments on commit 6d7c956

Please sign in to comment.