les: move client pool to les/vflux/server (#22495)

* les: move client pool to les/vflux/server

* les/vflux/server: un-expose NodeBalance, remove unused fn, fix bugs

* tests/fuzzers/vflux: add ClientPool fuzzer

* les/vflux/server: fixed balance tests

* les: rebase fix

* les/vflux/server: fixed more bugs

* les/vflux/server: unexported NodeStateMachine fields and flags

* les/vflux/server: unexport all internal components and functions

* les/vflux/server: fixed priorityPool test

* les/vflux/server: polish balance

* les/vflux/server: fixed mutex locking error

* les/vflux/server: priorityPool bug fixed

* common/prque: make Prque wrap-around priority handling optional

* les/vflux/server: rename funcs, small optimizations

* les/vflux/server: fixed timeUntil

* les/vflux/server: separated balance.posValue and negValue

* les/vflux/server: polish setup

* les/vflux/server: enforce capacity curve monotonicity

* les/vflux/server: simplified requestCapacity

* les/vflux/server: requestCapacity with target range, no iterations in SetCapacity

* les/vflux/server: minor changes

* les/vflux/server: moved default factors to balanceTracker

* les/vflux/server: set inactiveFlag in priorityPool

* les/vflux/server: moved related metrics to vfs package

* les/vflux/client: make priorityPool temp state logic cleaner

* les/vflux/server: changed log.Crit to log.Error

* add vflux fuzzer to oss-fuzz

Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
This commit is contained in:
Felföldi Zsolt
2021-04-06 20:42:50 +02:00
committed by GitHub
parent e275b1a293
commit 2d89fe0883
27 changed files with 1987 additions and 1545 deletions

View File

@@ -24,6 +24,7 @@ import (
"time"
"github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethdb/memorydb"
"github.com/ethereum/go-ethereum/les/utils"
"github.com/ethereum/go-ethereum/p2p/enode"
@@ -31,59 +32,82 @@ import (
"github.com/ethereum/go-ethereum/p2p/nodestate"
)
var (
testFlag = testSetup.NewFlag("testFlag")
connAddrFlag = testSetup.NewField("connAddr", reflect.TypeOf(""))
btTestSetup = NewBalanceTrackerSetup(testSetup)
)
func init() {
btTestSetup.Connect(connAddrFlag, ppTestSetup.CapacityField)
}
type zeroExpirer struct{}
func (z zeroExpirer) SetRate(now mclock.AbsTime, rate float64) {}
func (z zeroExpirer) SetLogOffset(now mclock.AbsTime, logOffset utils.Fixed64) {}
func (z zeroExpirer) LogOffset(now mclock.AbsTime) utils.Fixed64 { return 0 }
type balanceTestClient struct{}
func (client balanceTestClient) FreeClientId() string { return "" }
type balanceTestSetup struct {
clock *mclock.Simulated
db ethdb.KeyValueStore
ns *nodestate.NodeStateMachine
bt *BalanceTracker
setup *serverSetup
bt *balanceTracker
}
func newBalanceTestSetup() *balanceTestSetup {
func newBalanceTestSetup(db ethdb.KeyValueStore, posExp, negExp utils.ValueExpirer) *balanceTestSetup {
// Initialize and customize the setup for the balance testing
clock := &mclock.Simulated{}
ns := nodestate.NewNodeStateMachine(nil, nil, clock, testSetup)
db := memorydb.New()
bt := NewBalanceTracker(ns, btTestSetup, db, clock, zeroExpirer{}, zeroExpirer{})
setup := newServerSetup()
setup.clientField = setup.setup.NewField("balancTestClient", reflect.TypeOf(balanceTestClient{}))
ns := nodestate.NewNodeStateMachine(nil, nil, clock, setup.setup)
if posExp == nil {
posExp = zeroExpirer{}
}
if negExp == nil {
negExp = zeroExpirer{}
}
if db == nil {
db = memorydb.New()
}
bt := newBalanceTracker(ns, setup, db, clock, posExp, negExp)
ns.Start()
return &balanceTestSetup{
clock: clock,
db: db,
ns: ns,
setup: setup,
bt: bt,
}
}
func (b *balanceTestSetup) newNode(capacity uint64) *NodeBalance {
func (b *balanceTestSetup) newNode(capacity uint64) *nodeBalance {
node := enode.SignNull(&enr.Record{}, enode.ID{})
b.ns.SetState(node, testFlag, nodestate.Flags{}, 0)
b.ns.SetField(node, btTestSetup.connAddressField, "")
b.ns.SetField(node, b.setup.clientField, balanceTestClient{})
if capacity != 0 {
b.ns.SetField(node, ppTestSetup.CapacityField, capacity)
b.ns.SetField(node, b.setup.capacityField, capacity)
}
n, _ := b.ns.GetField(node, btTestSetup.BalanceField).(*NodeBalance)
n, _ := b.ns.GetField(node, b.setup.balanceField).(*nodeBalance)
return n
}
func (b *balanceTestSetup) setBalance(node *nodeBalance, pos, neg uint64) (err error) {
b.bt.BalanceOperation(node.node.ID(), node.connAddress, func(balance AtomicBalanceOperator) {
err = balance.SetBalance(pos, neg)
})
return
}
func (b *balanceTestSetup) addBalance(node *nodeBalance, add int64) (old, new uint64, err error) {
b.bt.BalanceOperation(node.node.ID(), node.connAddress, func(balance AtomicBalanceOperator) {
old, new, err = balance.AddBalance(add)
})
return
}
func (b *balanceTestSetup) stop() {
b.bt.Stop()
b.bt.stop()
b.ns.Stop()
}
func TestAddBalance(t *testing.T) {
b := newBalanceTestSetup()
b := newBalanceTestSetup(nil, nil, nil)
defer b.stop()
node := b.newNode(1000)
@@ -100,7 +124,7 @@ func TestAddBalance(t *testing.T) {
{maxBalance, [2]uint64{0, 0}, 0, true},
}
for _, i := range inputs {
old, new, err := node.AddBalance(i.delta)
old, new, err := b.addBalance(node, i.delta)
if i.expectErr {
if err == nil {
t.Fatalf("Expect get error but nil")
@@ -119,7 +143,7 @@ func TestAddBalance(t *testing.T) {
}
func TestSetBalance(t *testing.T) {
b := newBalanceTestSetup()
b := newBalanceTestSetup(nil, nil, nil)
defer b.stop()
node := b.newNode(1000)
@@ -130,9 +154,8 @@ func TestSetBalance(t *testing.T) {
{0, 1000},
{1000, 1000},
}
for _, i := range inputs {
node.SetBalance(i.pos, i.neg)
b.setBalance(node, i.pos, i.neg)
pos, neg := node.GetBalance()
if pos != i.pos {
t.Fatalf("Positive balance mismatch, want %v, got %v", i.pos, pos)
@@ -144,13 +167,12 @@ func TestSetBalance(t *testing.T) {
}
func TestBalanceTimeCost(t *testing.T) {
b := newBalanceTestSetup()
b := newBalanceTestSetup(nil, nil, nil)
defer b.stop()
node := b.newNode(1000)
b.ns.SetField(node.node, ppTestSetup.CapacityField, uint64(1))
node.SetPriceFactors(PriceFactors{1, 0, 1}, PriceFactors{1, 0, 1})
node.SetBalance(uint64(time.Minute), 0) // 1 minute time allowance
b.setBalance(node, uint64(time.Minute), 0) // 1 minute time allowance
var inputs = []struct {
runTime time.Duration
@@ -172,7 +194,7 @@ func TestBalanceTimeCost(t *testing.T) {
}
}
node.SetBalance(uint64(time.Minute), 0) // Refill 1 minute time allowance
b.setBalance(node, uint64(time.Minute), 0) // Refill 1 minute time allowance
for _, i := range inputs {
b.clock.Run(i.runTime)
if pos, _ := node.GetBalance(); pos != i.expPos {
@@ -185,13 +207,12 @@ func TestBalanceTimeCost(t *testing.T) {
}
func TestBalanceReqCost(t *testing.T) {
b := newBalanceTestSetup()
b := newBalanceTestSetup(nil, nil, nil)
defer b.stop()
node := b.newNode(1000)
node.SetPriceFactors(PriceFactors{1, 0, 1}, PriceFactors{1, 0, 1})
b.ns.SetField(node.node, ppTestSetup.CapacityField, uint64(1))
node.SetBalance(uint64(time.Minute), 0) // 1 minute time serving time allowance
b.setBalance(node, uint64(time.Minute), 0) // 1 minute time serving time allowance
var inputs = []struct {
reqCost uint64
expPos uint64
@@ -214,7 +235,7 @@ func TestBalanceReqCost(t *testing.T) {
}
func TestBalanceToPriority(t *testing.T) {
b := newBalanceTestSetup()
b := newBalanceTestSetup(nil, nil, nil)
defer b.stop()
node := b.newNode(1000)
node.SetPriceFactors(PriceFactors{1, 0, 1}, PriceFactors{1, 0, 1})
@@ -230,22 +251,20 @@ func TestBalanceToPriority(t *testing.T) {
{0, 1000, -1000},
}
for _, i := range inputs {
node.SetBalance(i.pos, i.neg)
priority := node.Priority(1000)
b.setBalance(node, i.pos, i.neg)
priority := node.priority(1000)
if priority != i.priority {
t.Fatalf("Priority mismatch, want %v, got %v", i.priority, priority)
t.Fatalf("priority mismatch, want %v, got %v", i.priority, priority)
}
}
}
func TestEstimatedPriority(t *testing.T) {
b := newBalanceTestSetup()
b := newBalanceTestSetup(nil, nil, nil)
defer b.stop()
node := b.newNode(1000000000)
node.SetPriceFactors(PriceFactors{1, 0, 1}, PriceFactors{1, 0, 1})
b.ns.SetField(node.node, ppTestSetup.CapacityField, uint64(1))
node.SetBalance(uint64(time.Minute), 0)
b.setBalance(node, uint64(time.Minute), 0)
var inputs = []struct {
runTime time.Duration // time cost
futureTime time.Duration // diff of future time
@@ -272,47 +291,18 @@ func TestEstimatedPriority(t *testing.T) {
for _, i := range inputs {
b.clock.Run(i.runTime)
node.RequestServed(i.reqCost)
priority := node.EstimatePriority(1000000000, 0, i.futureTime, 0, false)
if priority != i.priority {
t.Fatalf("Estimated priority mismatch, want %v, got %v", i.priority, priority)
}
}
}
func TestPosBalanceMissing(t *testing.T) {
b := newBalanceTestSetup()
defer b.stop()
node := b.newNode(1000)
node.SetPriceFactors(PriceFactors{1, 0, 1}, PriceFactors{1, 0, 1})
b.ns.SetField(node.node, ppTestSetup.CapacityField, uint64(1))
var inputs = []struct {
pos, neg uint64
priority int64
cap uint64
after time.Duration
expect uint64
}{
{uint64(time.Second * 2), 0, 0, 1, time.Second, 0},
{uint64(time.Second * 2), 0, 0, 1, 2 * time.Second, 1},
{uint64(time.Second * 2), 0, int64(time.Second), 1, 2 * time.Second, uint64(time.Second) + 1},
{0, 0, int64(time.Second), 1, time.Second, uint64(2*time.Second) + 1},
{0, 0, -int64(time.Second), 1, time.Second, 1},
}
for _, i := range inputs {
node.SetBalance(i.pos, i.neg)
got := node.PosBalanceMissing(i.priority, i.cap, i.after)
if got != i.expect {
t.Fatalf("Missing budget mismatch, want %v, got %v", i.expect, got)
priority := node.estimatePriority(1000000000, 0, i.futureTime, 0, false)
if priority != i.priority-1 {
t.Fatalf("Estimated priority mismatch, want %v, got %v", i.priority-1, priority)
}
}
}
func TestPostiveBalanceCounting(t *testing.T) {
b := newBalanceTestSetup()
b := newBalanceTestSetup(nil, nil, nil)
defer b.stop()
var nodes []*NodeBalance
var nodes []*nodeBalance
for i := 0; i < 100; i += 1 {
node := b.newNode(1000000)
node.SetPriceFactors(PriceFactors{1, 0, 1}, PriceFactors{1, 0, 1})
@@ -323,7 +313,7 @@ func TestPostiveBalanceCounting(t *testing.T) {
var sum uint64
for i := 0; i < 100; i += 1 {
amount := int64(rand.Intn(100) + 100)
nodes[i].AddBalance(amount)
b.addBalance(nodes[i], amount)
sum += uint64(amount)
}
if b.bt.TotalTokenAmount() != sum {
@@ -333,7 +323,7 @@ func TestPostiveBalanceCounting(t *testing.T) {
// Change client status
for i := 0; i < 100; i += 1 {
if rand.Intn(2) == 0 {
b.ns.SetField(nodes[i].node, ppTestSetup.CapacityField, uint64(1))
b.ns.SetField(nodes[i].node, b.setup.capacityField, uint64(1))
}
}
if b.bt.TotalTokenAmount() != sum {
@@ -341,7 +331,7 @@ func TestPostiveBalanceCounting(t *testing.T) {
}
for i := 0; i < 100; i += 1 {
if rand.Intn(2) == 0 {
b.ns.SetField(nodes[i].node, ppTestSetup.CapacityField, uint64(1))
b.ns.SetField(nodes[i].node, b.setup.capacityField, uint64(1))
}
}
if b.bt.TotalTokenAmount() != sum {
@@ -350,7 +340,7 @@ func TestPostiveBalanceCounting(t *testing.T) {
}
func TestCallbackChecking(t *testing.T) {
b := newBalanceTestSetup()
b := newBalanceTestSetup(nil, nil, nil)
defer b.stop()
node := b.newNode(1000000)
node.SetPriceFactors(PriceFactors{1, 0, 1}, PriceFactors{1, 0, 1})
@@ -363,7 +353,7 @@ func TestCallbackChecking(t *testing.T) {
{0, time.Second},
{-int64(time.Second), 2 * time.Second},
}
node.SetBalance(uint64(time.Second), 0)
b.setBalance(node, uint64(time.Second), 0)
for _, i := range inputs {
diff, _ := node.timeUntil(i.priority)
if diff != i.expDiff {
@@ -373,14 +363,13 @@ func TestCallbackChecking(t *testing.T) {
}
func TestCallback(t *testing.T) {
b := newBalanceTestSetup()
b := newBalanceTestSetup(nil, nil, nil)
defer b.stop()
node := b.newNode(1000)
node.SetPriceFactors(PriceFactors{1, 0, 1}, PriceFactors{1, 0, 1})
b.ns.SetField(node.node, ppTestSetup.CapacityField, uint64(1))
callCh := make(chan struct{}, 1)
node.SetBalance(uint64(time.Minute), 0)
b.setBalance(node, uint64(time.Minute), 0)
node.addCallback(balanceCallbackZero, 0, func() { callCh <- struct{}{} })
b.clock.Run(time.Minute)
@@ -390,7 +379,7 @@ func TestCallback(t *testing.T) {
t.Fatalf("Callback hasn't been called yet")
}
node.SetBalance(uint64(time.Minute), 0)
b.setBalance(node, uint64(time.Minute), 0)
node.addCallback(balanceCallbackZero, 0, func() { callCh <- struct{}{} })
node.removeCallback(balanceCallbackZero)
@@ -403,23 +392,14 @@ func TestCallback(t *testing.T) {
}
func TestBalancePersistence(t *testing.T) {
clock := &mclock.Simulated{}
ns := nodestate.NewNodeStateMachine(nil, nil, clock, testSetup)
db := memorydb.New()
posExp := &utils.Expirer{}
negExp := &utils.Expirer{}
posExp.SetRate(clock.Now(), math.Log(2)/float64(time.Hour*2)) // halves every two hours
negExp.SetRate(clock.Now(), math.Log(2)/float64(time.Hour)) // halves every hour
bt := NewBalanceTracker(ns, btTestSetup, db, clock, posExp, negExp)
ns.Start()
bts := &balanceTestSetup{
clock: clock,
ns: ns,
bt: bt,
}
var nb *NodeBalance
exp := func(expPos, expNeg uint64) {
pos, neg := nb.GetBalance()
posExp.SetRate(0, math.Log(2)/float64(time.Hour*2)) // halves every two hours
negExp.SetRate(0, math.Log(2)/float64(time.Hour)) // halves every hour
setup := newBalanceTestSetup(nil, posExp, negExp)
exp := func(balance *nodeBalance, expPos, expNeg uint64) {
pos, neg := balance.GetBalance()
if pos != expPos {
t.Fatalf("Positive balance incorrect, want %v, got %v", expPos, pos)
}
@@ -428,44 +408,32 @@ func TestBalancePersistence(t *testing.T) {
}
}
expTotal := func(expTotal uint64) {
total := bt.TotalTokenAmount()
total := setup.bt.TotalTokenAmount()
if total != expTotal {
t.Fatalf("Total token amount incorrect, want %v, got %v", expTotal, total)
}
}
expTotal(0)
nb = bts.newNode(0)
balance := setup.newNode(0)
expTotal(0)
nb.SetBalance(16000000000, 16000000000)
exp(16000000000, 16000000000)
setup.setBalance(balance, 16000000000, 16000000000)
exp(balance, 16000000000, 16000000000)
expTotal(16000000000)
clock.Run(time.Hour * 2)
exp(8000000000, 4000000000)
expTotal(8000000000)
bt.Stop()
ns.Stop()
clock = &mclock.Simulated{}
ns = nodestate.NewNodeStateMachine(nil, nil, clock, testSetup)
posExp = &utils.Expirer{}
negExp = &utils.Expirer{}
posExp.SetRate(clock.Now(), math.Log(2)/float64(time.Hour*2)) // halves every two hours
negExp.SetRate(clock.Now(), math.Log(2)/float64(time.Hour)) // halves every hour
bt = NewBalanceTracker(ns, btTestSetup, db, clock, posExp, negExp)
ns.Start()
bts = &balanceTestSetup{
clock: clock,
ns: ns,
bt: bt,
}
setup.clock.Run(time.Hour * 2)
exp(balance, 8000000000, 4000000000)
expTotal(8000000000)
nb = bts.newNode(0)
exp(8000000000, 4000000000)
setup.stop()
// Test the functionalities after restart
setup = newBalanceTestSetup(setup.db, posExp, negExp)
expTotal(8000000000)
clock.Run(time.Hour * 2)
exp(4000000000, 1000000000)
balance = setup.newNode(0)
exp(balance, 8000000000, 4000000000)
expTotal(8000000000)
setup.clock.Run(time.Hour * 2)
exp(balance, 4000000000, 1000000000)
expTotal(4000000000)
bt.Stop()
ns.Stop()
setup.stop()
}