| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- // Copyright 2019 The go-ethereum Authors
- // This file is part of the go-ethereum library.
- //
- // The go-ethereum library is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Lesser General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // The go-ethereum library is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Lesser General Public License for more details.
- //
- // You should have received a copy of the GNU Lesser General Public License
- // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
- package server
- import (
- "math"
- "math/rand"
- "reflect"
- "testing"
- "time"
- "github.com/ethereum/go-ethereum/common/mclock"
- "github.com/ethereum/go-ethereum/ethdb/memorydb"
- "github.com/ethereum/go-ethereum/les/utils"
- "github.com/ethereum/go-ethereum/p2p/enode"
- "github.com/ethereum/go-ethereum/p2p/enr"
- "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 balanceTestSetup struct {
- clock *mclock.Simulated
- ns *nodestate.NodeStateMachine
- bt *BalanceTracker
- }
- func newBalanceTestSetup() *balanceTestSetup {
- clock := &mclock.Simulated{}
- ns := nodestate.NewNodeStateMachine(nil, nil, clock, testSetup)
- db := memorydb.New()
- bt := NewBalanceTracker(ns, btTestSetup, db, clock, zeroExpirer{}, zeroExpirer{})
- ns.Start()
- return &balanceTestSetup{
- clock: clock,
- ns: ns,
- bt: bt,
- }
- }
- 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, "")
- if capacity != 0 {
- b.ns.SetField(node, ppTestSetup.CapacityField, capacity)
- }
- n, _ := b.ns.GetField(node, btTestSetup.BalanceField).(*NodeBalance)
- return n
- }
- func (b *balanceTestSetup) stop() {
- b.bt.Stop()
- b.ns.Stop()
- }
- func TestAddBalance(t *testing.T) {
- b := newBalanceTestSetup()
- defer b.stop()
- node := b.newNode(1000)
- var inputs = []struct {
- delta int64
- expect [2]uint64
- total uint64
- expectErr bool
- }{
- {100, [2]uint64{0, 100}, 100, false},
- {-100, [2]uint64{100, 0}, 0, false},
- {-100, [2]uint64{0, 0}, 0, false},
- {1, [2]uint64{0, 1}, 1, false},
- {maxBalance, [2]uint64{0, 0}, 0, true},
- }
- for _, i := range inputs {
- old, new, err := node.AddBalance(i.delta)
- if i.expectErr {
- if err == nil {
- t.Fatalf("Expect get error but nil")
- }
- continue
- } else if err != nil {
- t.Fatalf("Expect get no error but %v", err)
- }
- if old != i.expect[0] || new != i.expect[1] {
- t.Fatalf("Positive balance mismatch, got %v -> %v", old, new)
- }
- if b.bt.TotalTokenAmount() != i.total {
- t.Fatalf("Total positive balance mismatch, want %v, got %v", i.total, b.bt.TotalTokenAmount())
- }
- }
- }
- func TestSetBalance(t *testing.T) {
- b := newBalanceTestSetup()
- defer b.stop()
- node := b.newNode(1000)
- var inputs = []struct {
- pos, neg uint64
- }{
- {1000, 0},
- {0, 1000},
- {1000, 1000},
- }
- for _, i := range inputs {
- node.SetBalance(i.pos, i.neg)
- pos, neg := node.GetBalance()
- if pos != i.pos {
- t.Fatalf("Positive balance mismatch, want %v, got %v", i.pos, pos)
- }
- if neg != i.neg {
- t.Fatalf("Negative balance mismatch, want %v, got %v", i.neg, neg)
- }
- }
- }
- func TestBalanceTimeCost(t *testing.T) {
- b := newBalanceTestSetup()
- 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
- var inputs = []struct {
- runTime time.Duration
- expPos uint64
- expNeg uint64
- }{
- {time.Second, uint64(time.Second * 59), 0},
- {0, uint64(time.Second * 59), 0},
- {time.Second * 59, 0, 0},
- {time.Second, 0, uint64(time.Second)},
- }
- for _, i := range inputs {
- b.clock.Run(i.runTime)
- if pos, _ := node.GetBalance(); pos != i.expPos {
- t.Fatalf("Positive balance mismatch, want %v, got %v", i.expPos, pos)
- }
- if _, neg := node.GetBalance(); neg != i.expNeg {
- t.Fatalf("Negative balance mismatch, want %v, got %v", i.expNeg, neg)
- }
- }
- node.SetBalance(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 {
- t.Fatalf("Positive balance mismatch, want %v, got %v", i.expPos, pos)
- }
- if _, neg := node.GetBalance(); neg != i.expNeg {
- t.Fatalf("Negative balance mismatch, want %v, got %v", i.expNeg, neg)
- }
- }
- }
- func TestBalanceReqCost(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))
- node.SetBalance(uint64(time.Minute), 0) // 1 minute time serving time allowance
- var inputs = []struct {
- reqCost uint64
- expPos uint64
- expNeg uint64
- }{
- {uint64(time.Second), uint64(time.Second * 59), 0},
- {0, uint64(time.Second * 59), 0},
- {uint64(time.Second * 59), 0, 0},
- {uint64(time.Second), 0, uint64(time.Second)},
- }
- for _, i := range inputs {
- node.RequestServed(i.reqCost)
- if pos, _ := node.GetBalance(); pos != i.expPos {
- t.Fatalf("Positive balance mismatch, want %v, got %v", i.expPos, pos)
- }
- if _, neg := node.GetBalance(); neg != i.expNeg {
- t.Fatalf("Negative balance mismatch, want %v, got %v", i.expNeg, neg)
- }
- }
- }
- func TestBalanceToPriority(t *testing.T) {
- b := newBalanceTestSetup()
- defer b.stop()
- node := b.newNode(1000)
- node.SetPriceFactors(PriceFactors{1, 0, 1}, PriceFactors{1, 0, 1})
- var inputs = []struct {
- pos uint64
- neg uint64
- priority int64
- }{
- {1000, 0, 1},
- {2000, 0, 2}, // Higher balance, higher priority value
- {0, 0, 0},
- {0, 1000, -1000},
- }
- for _, i := range inputs {
- node.SetBalance(i.pos, i.neg)
- priority := node.Priority(b.clock.Now(), 1000)
- if priority != i.priority {
- t.Fatalf("Priority mismatch, want %v, got %v", i.priority, priority)
- }
- }
- }
- func TestEstimatedPriority(t *testing.T) {
- b := newBalanceTestSetup()
- 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)
- var inputs = []struct {
- runTime time.Duration // time cost
- futureTime time.Duration // diff of future time
- reqCost uint64 // single request cost
- priority int64 // expected estimated priority
- }{
- {time.Second, time.Second, 0, 58},
- {0, time.Second, 0, 58},
- // 2 seconds time cost, 1 second estimated time cost, 10^9 request cost,
- // 10^9 estimated request cost per second.
- {time.Second, time.Second, 1000000000, 55},
- // 3 seconds time cost, 3 second estimated time cost, 10^9*2 request cost,
- // 4*10^9 estimated request cost.
- {time.Second, 3 * time.Second, 1000000000, 48},
- // All positive balance is used up
- {time.Second * 55, 0, 0, 0},
- // 1 minute estimated time cost, 4/58 * 10^9 estimated request cost per sec.
- {0, time.Minute, 0, -int64(time.Minute) - int64(time.Second)*120/29},
- }
- for _, i := range inputs {
- b.clock.Run(i.runTime)
- node.RequestServed(i.reqCost)
- priority := node.EstMinPriority(b.clock.Now()+mclock.AbsTime(i.futureTime), 1000000000, 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)
- }
- }
- }
- func TestPostiveBalanceCounting(t *testing.T) {
- b := newBalanceTestSetup()
- defer b.stop()
- var nodes []*NodeBalance
- for i := 0; i < 100; i += 1 {
- node := b.newNode(1000000)
- node.SetPriceFactors(PriceFactors{1, 0, 1}, PriceFactors{1, 0, 1})
- nodes = append(nodes, node)
- }
- // Allocate service token
- var sum uint64
- for i := 0; i < 100; i += 1 {
- amount := int64(rand.Intn(100) + 100)
- nodes[i].AddBalance(amount)
- sum += uint64(amount)
- }
- if b.bt.TotalTokenAmount() != sum {
- t.Fatalf("Invalid token amount")
- }
- // 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))
- }
- }
- if b.bt.TotalTokenAmount() != sum {
- t.Fatalf("Invalid token amount")
- }
- for i := 0; i < 100; i += 1 {
- if rand.Intn(2) == 0 {
- b.ns.SetField(nodes[i].node, ppTestSetup.CapacityField, uint64(1))
- }
- }
- if b.bt.TotalTokenAmount() != sum {
- t.Fatalf("Invalid token amount")
- }
- }
- func TestCallbackChecking(t *testing.T) {
- b := newBalanceTestSetup()
- defer b.stop()
- node := b.newNode(1000000)
- node.SetPriceFactors(PriceFactors{1, 0, 1}, PriceFactors{1, 0, 1})
- var inputs = []struct {
- priority int64
- expDiff time.Duration
- }{
- {500, time.Millisecond * 500},
- {0, time.Second},
- {-int64(time.Second), 2 * time.Second},
- }
- node.SetBalance(uint64(time.Second), 0)
- for _, i := range inputs {
- diff, _ := node.timeUntil(i.priority)
- if diff != i.expDiff {
- t.Fatalf("Time difference mismatch, want %v, got %v", i.expDiff, diff)
- }
- }
- }
- func TestCallback(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))
- callCh := make(chan struct{}, 1)
- node.SetBalance(uint64(time.Minute), 0)
- node.addCallback(balanceCallbackZero, 0, func() { callCh <- struct{}{} })
- b.clock.Run(time.Minute)
- select {
- case <-callCh:
- case <-time.NewTimer(time.Second).C:
- t.Fatalf("Callback hasn't been called yet")
- }
- node.SetBalance(uint64(time.Minute), 0)
- node.addCallback(balanceCallbackZero, 0, func() { callCh <- struct{}{} })
- node.removeCallback(balanceCallbackZero)
- b.clock.Run(time.Minute)
- select {
- case <-callCh:
- t.Fatalf("Callback shouldn't be called")
- case <-time.NewTimer(time.Millisecond * 100).C:
- }
- }
- 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()
- if pos != expPos {
- t.Fatalf("Positive balance incorrect, want %v, got %v", expPos, pos)
- }
- if neg != expNeg {
- t.Fatalf("Positive balance incorrect, want %v, got %v", expPos, pos)
- }
- }
- expTotal := func(expTotal uint64) {
- total := bt.TotalTokenAmount()
- if total != expTotal {
- t.Fatalf("Total token amount incorrect, want %v, got %v", expTotal, total)
- }
- }
- expTotal(0)
- nb = bts.newNode(0)
- expTotal(0)
- nb.SetBalance(16000000000, 16000000000)
- exp(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,
- }
- expTotal(8000000000)
- nb = bts.newNode(0)
- exp(8000000000, 4000000000)
- expTotal(8000000000)
- clock.Run(time.Hour * 2)
- exp(4000000000, 1000000000)
- expTotal(4000000000)
- bt.Stop()
- ns.Stop()
- }
|