Commit bf5c6b29 authored by Felföldi Zsolt's avatar Felföldi Zsolt Committed by Felix Lange

les: implement server priority API (#20070)

This PR implements the LES server RPC API. Methods for server
capacity, client balance and client priority management are provided.
parent 22e3bbbf
...@@ -445,6 +445,11 @@ web3._extend({ ...@@ -445,6 +445,11 @@ web3._extend({
params: 2, params: 2,
inputFormatter:[null, null], inputFormatter:[null, null],
}), }),
new web3._extend.Method({
name: 'freezeClient',
call: 'debug_freezeClient',
params: 1,
}),
], ],
properties: [] properties: []
}); });
...@@ -798,6 +803,31 @@ web3._extend({ ...@@ -798,6 +803,31 @@ web3._extend({
call: 'les_getCheckpoint', call: 'les_getCheckpoint',
params: 1 params: 1
}), }),
new web3._extend.Method({
name: 'clientInfo',
call: 'les_clientInfo',
params: 1
}),
new web3._extend.Method({
name: 'priorityClientInfo',
call: 'les_priorityClientInfo',
params: 3
}),
new web3._extend.Method({
name: 'setClientParams',
call: 'les_setClientParams',
params: 2
}),
new web3._extend.Method({
name: 'setDefaultParams',
call: 'les_setDefaultParams',
params: 1
}),
new web3._extend.Method({
name: 'updateBalance',
call: 'les_updateBalance',
params: 3
}),
], ],
properties: properties:
[ [
...@@ -809,6 +839,10 @@ web3._extend({ ...@@ -809,6 +839,10 @@ web3._extend({
name: 'checkpointContractAddress', name: 'checkpointContractAddress',
getter: 'les_getCheckpointContractAddress' getter: 'les_getCheckpointContractAddress'
}), }),
new web3._extend.Property({
name: 'serverInfo',
getter: 'les_serverInfo'
}),
] ]
}); });
` `
This diff is collapsed.
...@@ -160,6 +160,14 @@ func (bt *balanceTracker) timeUntil(priority int64) (time.Duration, bool) { ...@@ -160,6 +160,14 @@ func (bt *balanceTracker) timeUntil(priority int64) (time.Duration, bool) {
return time.Duration(dt), true return time.Duration(dt), true
} }
// setCapacity updates the capacity value used for priority calculation
func (bt *balanceTracker) setCapacity(capacity uint64) {
bt.lock.Lock()
defer bt.lock.Unlock()
bt.capacity = capacity
}
// getPriority returns the actual priority based on the current balance // getPriority returns the actual priority based on the current balance
func (bt *balanceTracker) getPriority(now mclock.AbsTime) int64 { func (bt *balanceTracker) getPriority(now mclock.AbsTime) int64 {
bt.lock.Lock() bt.lock.Lock()
......
This diff is collapsed.
...@@ -76,6 +76,8 @@ type poolTestPeerWithCap struct { ...@@ -76,6 +76,8 @@ type poolTestPeerWithCap struct {
func (i *poolTestPeerWithCap) updateCapacity(cap uint64) { i.cap = cap } func (i *poolTestPeerWithCap) updateCapacity(cap uint64) { i.cap = cap }
func (i poolTestPeer) freezeClient() {}
func testClientPool(t *testing.T, connLimit, clientCount, paidCount int, randomDisconnect bool) { func testClientPool(t *testing.T, connLimit, clientCount, paidCount int, randomDisconnect bool) {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
var ( var (
...@@ -91,7 +93,7 @@ func testClientPool(t *testing.T, connLimit, clientCount, paidCount int, randomD ...@@ -91,7 +93,7 @@ func testClientPool(t *testing.T, connLimit, clientCount, paidCount int, randomD
) )
pool.disableBias = true pool.disableBias = true
pool.setLimits(connLimit, uint64(connLimit)) pool.setLimits(connLimit, uint64(connLimit))
pool.setPriceFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1}) pool.setDefaultFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1})
// pool should accept new peers up to its connected limit // pool should accept new peers up to its connected limit
for i := 0; i < connLimit; i++ { for i := 0; i < connLimit; i++ {
...@@ -107,9 +109,9 @@ func testClientPool(t *testing.T, connLimit, clientCount, paidCount int, randomD ...@@ -107,9 +109,9 @@ func testClientPool(t *testing.T, connLimit, clientCount, paidCount int, randomD
if tickCounter == testClientPoolTicks/4 { if tickCounter == testClientPoolTicks/4 {
// give a positive balance to some of the peers // give a positive balance to some of the peers
amount := uint64(testClientPoolTicks / 2 * 1000000000) // enough for half of the simulation period amount := testClientPoolTicks / 2 * int64(time.Second) // enough for half of the simulation period
for i := 0; i < paidCount; i++ { for i := 0; i < paidCount; i++ {
pool.addBalance(poolTestPeer(i).ID(), amount, false) pool.updateBalance(poolTestPeer(i).ID(), amount, "")
} }
} }
...@@ -173,10 +175,10 @@ func TestConnectPaidClient(t *testing.T) { ...@@ -173,10 +175,10 @@ func TestConnectPaidClient(t *testing.T) {
pool := newClientPool(db, 1, &clock, nil) pool := newClientPool(db, 1, &clock, nil)
defer pool.stop() defer pool.stop()
pool.setLimits(10, uint64(10)) pool.setLimits(10, uint64(10))
pool.setPriceFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1}) pool.setDefaultFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1})
// Add balance for an external client and mark it as paid client // Add balance for an external client and mark it as paid client
pool.addBalance(poolTestPeer(0).ID(), 1000, false) pool.updateBalance(poolTestPeer(0).ID(), 1000, "")
if !pool.connect(poolTestPeer(0), 10) { if !pool.connect(poolTestPeer(0), 10) {
t.Fatalf("Failed to connect paid client") t.Fatalf("Failed to connect paid client")
...@@ -191,10 +193,10 @@ func TestConnectPaidClientToSmallPool(t *testing.T) { ...@@ -191,10 +193,10 @@ func TestConnectPaidClientToSmallPool(t *testing.T) {
pool := newClientPool(db, 1, &clock, nil) pool := newClientPool(db, 1, &clock, nil)
defer pool.stop() defer pool.stop()
pool.setLimits(10, uint64(10)) // Total capacity limit is 10 pool.setLimits(10, uint64(10)) // Total capacity limit is 10
pool.setPriceFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1}) pool.setDefaultFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1})
// Add balance for an external client and mark it as paid client // Add balance for an external client and mark it as paid client
pool.addBalance(poolTestPeer(0).ID(), 1000, false) pool.updateBalance(poolTestPeer(0).ID(), 1000, "")
// Connect a fat paid client to pool, should reject it. // Connect a fat paid client to pool, should reject it.
if pool.connect(poolTestPeer(0), 100) { if pool.connect(poolTestPeer(0), 100) {
...@@ -211,18 +213,18 @@ func TestConnectPaidClientToFullPool(t *testing.T) { ...@@ -211,18 +213,18 @@ func TestConnectPaidClientToFullPool(t *testing.T) {
pool := newClientPool(db, 1, &clock, removeFn) pool := newClientPool(db, 1, &clock, removeFn)
defer pool.stop() defer pool.stop()
pool.setLimits(10, uint64(10)) // Total capacity limit is 10 pool.setLimits(10, uint64(10)) // Total capacity limit is 10
pool.setPriceFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1}) pool.setDefaultFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1})
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
pool.addBalance(poolTestPeer(i).ID(), 1000000000, false) pool.updateBalance(poolTestPeer(i).ID(), 1000000000, "")
pool.connect(poolTestPeer(i), 1) pool.connect(poolTestPeer(i), 1)
} }
pool.addBalance(poolTestPeer(11).ID(), 1000, false) // Add low balance to new paid client pool.updateBalance(poolTestPeer(11).ID(), 1000, "") // Add low balance to new paid client
if pool.connect(poolTestPeer(11), 1) { if pool.connect(poolTestPeer(11), 1) {
t.Fatalf("Low balance paid client should be rejected") t.Fatalf("Low balance paid client should be rejected")
} }
clock.Run(time.Second) clock.Run(time.Second)
pool.addBalance(poolTestPeer(12).ID(), 1000000000*60*3, false) // Add high balance to new paid client pool.updateBalance(poolTestPeer(12).ID(), 1000000000*60*3, "") // Add high balance to new paid client
if !pool.connect(poolTestPeer(12), 1) { if !pool.connect(poolTestPeer(12), 1) {
t.Fatalf("High balance paid client should be accpected") t.Fatalf("High balance paid client should be accpected")
} }
...@@ -238,10 +240,10 @@ func TestPaidClientKickedOut(t *testing.T) { ...@@ -238,10 +240,10 @@ func TestPaidClientKickedOut(t *testing.T) {
pool := newClientPool(db, 1, &clock, removeFn) pool := newClientPool(db, 1, &clock, removeFn)
defer pool.stop() defer pool.stop()
pool.setLimits(10, uint64(10)) // Total capacity limit is 10 pool.setLimits(10, uint64(10)) // Total capacity limit is 10
pool.setPriceFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1}) pool.setDefaultFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1})
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
pool.addBalance(poolTestPeer(i).ID(), 1000000000, false) // 1 second allowance pool.updateBalance(poolTestPeer(i).ID(), 1000000000, "") // 1 second allowance
pool.connect(poolTestPeer(i), 1) pool.connect(poolTestPeer(i), 1)
clock.Run(time.Millisecond) clock.Run(time.Millisecond)
} }
...@@ -268,7 +270,7 @@ func TestConnectFreeClient(t *testing.T) { ...@@ -268,7 +270,7 @@ func TestConnectFreeClient(t *testing.T) {
pool := newClientPool(db, 1, &clock, nil) pool := newClientPool(db, 1, &clock, nil)
defer pool.stop() defer pool.stop()
pool.setLimits(10, uint64(10)) pool.setLimits(10, uint64(10))
pool.setPriceFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1}) pool.setDefaultFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1})
if !pool.connect(poolTestPeer(0), 10) { if !pool.connect(poolTestPeer(0), 10) {
t.Fatalf("Failed to connect free client") t.Fatalf("Failed to connect free client")
} }
...@@ -283,7 +285,7 @@ func TestConnectFreeClientToFullPool(t *testing.T) { ...@@ -283,7 +285,7 @@ func TestConnectFreeClientToFullPool(t *testing.T) {
pool := newClientPool(db, 1, &clock, removeFn) pool := newClientPool(db, 1, &clock, removeFn)
defer pool.stop() defer pool.stop()
pool.setLimits(10, uint64(10)) // Total capacity limit is 10 pool.setLimits(10, uint64(10)) // Total capacity limit is 10
pool.setPriceFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1}) pool.setDefaultFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1})
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
pool.connect(poolTestPeer(i), 1) pool.connect(poolTestPeer(i), 1)
...@@ -312,7 +314,7 @@ func TestFreeClientKickedOut(t *testing.T) { ...@@ -312,7 +314,7 @@ func TestFreeClientKickedOut(t *testing.T) {
pool := newClientPool(db, 1, &clock, removeFn) pool := newClientPool(db, 1, &clock, removeFn)
defer pool.stop() defer pool.stop()
pool.setLimits(10, uint64(10)) // Total capacity limit is 10 pool.setLimits(10, uint64(10)) // Total capacity limit is 10
pool.setPriceFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1}) pool.setDefaultFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1})
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
pool.connect(poolTestPeer(i), 1) pool.connect(poolTestPeer(i), 1)
...@@ -347,9 +349,9 @@ func TestPositiveBalanceCalculation(t *testing.T) { ...@@ -347,9 +349,9 @@ func TestPositiveBalanceCalculation(t *testing.T) {
pool := newClientPool(db, 1, &clock, removeFn) pool := newClientPool(db, 1, &clock, removeFn)
defer pool.stop() defer pool.stop()
pool.setLimits(10, uint64(10)) // Total capacity limit is 10 pool.setLimits(10, uint64(10)) // Total capacity limit is 10
pool.setPriceFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1}) pool.setDefaultFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1})
pool.addBalance(poolTestPeer(0).ID(), uint64(time.Minute*3), false) pool.updateBalance(poolTestPeer(0).ID(), int64(time.Minute*3), "")
pool.connect(poolTestPeer(0), 10) pool.connect(poolTestPeer(0), 10)
clock.Run(time.Minute) clock.Run(time.Minute)
...@@ -370,12 +372,12 @@ func TestDowngradePriorityClient(t *testing.T) { ...@@ -370,12 +372,12 @@ func TestDowngradePriorityClient(t *testing.T) {
pool := newClientPool(db, 1, &clock, removeFn) pool := newClientPool(db, 1, &clock, removeFn)
defer pool.stop() defer pool.stop()
pool.setLimits(10, uint64(10)) // Total capacity limit is 10 pool.setLimits(10, uint64(10)) // Total capacity limit is 10
pool.setPriceFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1}) pool.setDefaultFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1})
p := &poolTestPeerWithCap{ p := &poolTestPeerWithCap{
poolTestPeer: poolTestPeer(0), poolTestPeer: poolTestPeer(0),
} }
pool.addBalance(p.ID(), uint64(time.Minute), false) pool.updateBalance(p.ID(), int64(time.Minute), "")
pool.connect(p, 10) pool.connect(p, 10)
if p.cap != 10 { if p.cap != 10 {
t.Fatalf("The capcacity of priority peer hasn't been updated, got: %d", p.cap) t.Fatalf("The capcacity of priority peer hasn't been updated, got: %d", p.cap)
...@@ -391,7 +393,7 @@ func TestDowngradePriorityClient(t *testing.T) { ...@@ -391,7 +393,7 @@ func TestDowngradePriorityClient(t *testing.T) {
t.Fatalf("Positive balance mismatch, want %v, got %v", 0, pb.value) t.Fatalf("Positive balance mismatch, want %v, got %v", 0, pb.value)
} }
pool.addBalance(poolTestPeer(0).ID(), uint64(time.Minute), false) pool.updateBalance(poolTestPeer(0).ID(), int64(time.Minute), "")
pb = pool.ndb.getOrNewPB(poolTestPeer(0).ID()) pb = pool.ndb.getOrNewPB(poolTestPeer(0).ID())
if pb.value != uint64(time.Minute) { if pb.value != uint64(time.Minute) {
t.Fatalf("Positive balance mismatch, want %v, got %v", uint64(time.Minute), pb.value) t.Fatalf("Positive balance mismatch, want %v, got %v", uint64(time.Minute), pb.value)
...@@ -408,7 +410,7 @@ func TestNegativeBalanceCalculation(t *testing.T) { ...@@ -408,7 +410,7 @@ func TestNegativeBalanceCalculation(t *testing.T) {
pool := newClientPool(db, 1, &clock, removeFn) pool := newClientPool(db, 1, &clock, removeFn)
defer pool.stop() defer pool.stop()
pool.setLimits(10, uint64(10)) // Total capacity limit is 10 pool.setLimits(10, uint64(10)) // Total capacity limit is 10
pool.setPriceFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1}) pool.setDefaultFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1})
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
pool.connect(poolTestPeer(i), 1) pool.connect(poolTestPeer(i), 1)
...@@ -442,8 +444,8 @@ func TestNodeDB(t *testing.T) { ...@@ -442,8 +444,8 @@ func TestNodeDB(t *testing.T) {
ndb := newNodeDB(rawdb.NewMemoryDatabase(), mclock.System{}) ndb := newNodeDB(rawdb.NewMemoryDatabase(), mclock.System{})
defer ndb.close() defer ndb.close()
if !bytes.Equal(ndb.verbuf[:], []byte{0x00, 0x00}) { if !bytes.Equal(ndb.verbuf[:], []byte{0x00, nodeDBVersion}) {
t.Fatalf("version buffer mismatch, want %v, got %v", []byte{0x00, 0x00}, ndb.verbuf) t.Fatalf("version buffer mismatch, want %v, got %v", []byte{0x00, nodeDBVersion}, ndb.verbuf)
} }
var cases = []struct { var cases = []struct {
id enode.ID id enode.ID
...@@ -451,8 +453,8 @@ func TestNodeDB(t *testing.T) { ...@@ -451,8 +453,8 @@ func TestNodeDB(t *testing.T) {
balance interface{} balance interface{}
positive bool positive bool
}{ }{
{enode.ID{0x00, 0x01, 0x02}, "", posBalance{value: 100, lastTotal: 200}, true}, {enode.ID{0x00, 0x01, 0x02}, "", posBalance{value: 100}, true},
{enode.ID{0x00, 0x01, 0x02}, "", posBalance{value: 200, lastTotal: 300}, true}, {enode.ID{0x00, 0x01, 0x02}, "", posBalance{value: 200}, true},
{enode.ID{}, "127.0.0.1", negBalance{logValue: 10}, false}, {enode.ID{}, "127.0.0.1", negBalance{logValue: 10}, false},
{enode.ID{}, "127.0.0.1", negBalance{logValue: 20}, false}, {enode.ID{}, "127.0.0.1", negBalance{logValue: 20}, false},
} }
......
...@@ -50,9 +50,9 @@ type LesServer struct { ...@@ -50,9 +50,9 @@ type LesServer struct {
servingQueue *servingQueue servingQueue *servingQueue
clientPool *clientPool clientPool *clientPool
freeCapacity uint64 // The minimal client capacity used for free client. minCapacity, maxCapacity, freeCapacity uint64
threadsIdle int // Request serving threads count when system is idle. threadsIdle int // Request serving threads count when system is idle.
threadsBusy int // Request serving threads count when system is busy(block insertion). threadsBusy int // Request serving threads count when system is busy(block insertion).
} }
func NewLesServer(e *eth.Ethereum, config *eth.Config) (*LesServer, error) { func NewLesServer(e *eth.Ethereum, config *eth.Config) (*LesServer, error) {
...@@ -88,7 +88,8 @@ func NewLesServer(e *eth.Ethereum, config *eth.Config) (*LesServer, error) { ...@@ -88,7 +88,8 @@ func NewLesServer(e *eth.Ethereum, config *eth.Config) (*LesServer, error) {
threadsIdle: threads, threadsIdle: threads,
} }
srv.handler = newServerHandler(srv, e.BlockChain(), e.ChainDb(), e.TxPool(), e.Synced) srv.handler = newServerHandler(srv, e.BlockChain(), e.ChainDb(), e.TxPool(), e.Synced)
srv.costTracker, srv.freeCapacity = newCostTracker(e.ChainDb(), config) srv.costTracker, srv.minCapacity = newCostTracker(e.ChainDb(), config)
srv.freeCapacity = srv.minCapacity
// Set up checkpoint oracle. // Set up checkpoint oracle.
oracle := config.CheckpointOracle oracle := config.CheckpointOracle
...@@ -108,13 +109,13 @@ func NewLesServer(e *eth.Ethereum, config *eth.Config) (*LesServer, error) { ...@@ -108,13 +109,13 @@ func NewLesServer(e *eth.Ethereum, config *eth.Config) (*LesServer, error) {
// to send requests most of the time. Our goal is to serve as many clients as // to send requests most of the time. Our goal is to serve as many clients as
// possible while the actually used server capacity does not exceed the limits // possible while the actually used server capacity does not exceed the limits
totalRecharge := srv.costTracker.totalRecharge() totalRecharge := srv.costTracker.totalRecharge()
maxCapacity := srv.freeCapacity * uint64(srv.config.LightPeers) srv.maxCapacity = srv.freeCapacity * uint64(srv.config.LightPeers)
if totalRecharge > maxCapacity { if totalRecharge > srv.maxCapacity {
maxCapacity = totalRecharge srv.maxCapacity = totalRecharge
} }
srv.fcManager.SetCapacityLimits(srv.freeCapacity, maxCapacity, srv.freeCapacity*2) srv.fcManager.SetCapacityLimits(srv.freeCapacity, srv.maxCapacity, srv.freeCapacity*2)
srv.clientPool = newClientPool(srv.chainDb, srv.freeCapacity, mclock.System{}, func(id enode.ID) { go srv.peers.Unregister(peerIdToString(id)) }) srv.clientPool = newClientPool(srv.chainDb, srv.freeCapacity, mclock.System{}, func(id enode.ID) { go srv.peers.Unregister(peerIdToString(id)) })
srv.clientPool.setPriceFactors(priceFactors{0, 1, 1}, priceFactors{0, 1, 1}) srv.clientPool.setDefaultFactors(priceFactors{0, 1, 1}, priceFactors{0, 1, 1})
checkpoint := srv.latestLocalCheckpoint() checkpoint := srv.latestLocalCheckpoint()
if !checkpoint.Empty() { if !checkpoint.Empty() {
...@@ -133,6 +134,18 @@ func (s *LesServer) APIs() []rpc.API { ...@@ -133,6 +134,18 @@ func (s *LesServer) APIs() []rpc.API {
Service: NewPrivateLightAPI(&s.lesCommons), Service: NewPrivateLightAPI(&s.lesCommons),
Public: false, Public: false,
}, },
{
Namespace: "les",
Version: "1.0",
Service: NewPrivateLightServerAPI(s),
Public: false,
},
{
Namespace: "debug",
Version: "1.0",
Service: NewPrivateDebugAPI(s),
Public: false,
},
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment