Skip to content

Commit 58497f4

Browse files
authored
les, les/flowcontrol: implement LES/3 (#19329)
les, les/flowcontrol: implement LES/3
1 parent 3d58268 commit 58497f4

22 files changed

Lines changed: 1533 additions & 608 deletions

‎cmd/utils/flags.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,13 @@ var (
205205
}
206206
LightBandwidthInFlag = cli.IntFlag{
207207
Name: "lightbwin",
208-
Usage: "Incoming bandwidth limit for light server (1000 bytes/sec, 0 = unlimited)",
209-
Value: 1000,
208+
Usage: "Incoming bandwidth limit for light server (kilobytes/sec, 0 = unlimited)",
209+
Value: 0,
210210
}
211211
LightBandwidthOutFlag = cli.IntFlag{
212212
Name: "lightbwout",
213-
Usage: "Outgoing bandwidth limit for light server (1000 bytes/sec, 0 = unlimited)",
214-
Value: 5000,
213+
Usage: "Outgoing bandwidth limit for light server (kilobytes/sec, 0 = unlimited)",
214+
Value: 0,
215215
}
216216
LightPeersFlag = cli.IntFlag{
217217
Name: "lightpeers",

‎core/blockchain.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const (
7474
maxFutureBlocks = 256
7575
maxTimeFutureBlocks = 30
7676
badBlockLimit = 10
77-
triesInMemory = 128
77+
TriesInMemory = 128
7878

7979
// BlockChainVersion ensures that an incompatible database forces a resync from scratch.
8080
//
@@ -799,7 +799,7 @@ func (bc *BlockChain) Stop() {
799799
if !bc.cacheConfig.TrieDirtyDisabled {
800800
triedb := bc.stateCache.TrieDB()
801801

802-
for _, offset := range []uint64{0, 1, triesInMemory - 1} {
802+
for _, offset := range []uint64{0, 1, TriesInMemory - 1} {
803803
if number := bc.CurrentBlock().NumberU64(); number > offset {
804804
recent := bc.GetBlockByNumber(number - offset)
805805

@@ -1224,7 +1224,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
12241224
triedb.Reference(root, common.Hash{}) // metadata reference to keep trie alive
12251225
bc.triegc.Push(root, -int64(block.NumberU64()))
12261226

1227-
if current := block.NumberU64(); current > triesInMemory {
1227+
if current := block.NumberU64(); current > TriesInMemory {
12281228
// If we exceeded our memory allowance, flush matured singleton nodes to disk
12291229
var (
12301230
nodes, imgs = triedb.Size()
@@ -1234,7 +1234,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
12341234
triedb.Cap(limit - ethdb.IdealBatchSize)
12351235
}
12361236
// Find the next state trie we need to commit
1237-
chosen := current - triesInMemory
1237+
chosen := current - TriesInMemory
12381238

12391239
// If we exceeded out time allowance, flush an entire trie to disk
12401240
if bc.gcproc > bc.cacheConfig.TrieTimeLimit {
@@ -1246,8 +1246,8 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
12461246
} else {
12471247
// If we're exceeding limits but haven't reached a large enough memory gap,
12481248
// warn the user that the system is becoming unstable.
1249-
if chosen < lastWrite+triesInMemory && bc.gcproc >= 2*bc.cacheConfig.TrieTimeLimit {
1250-
log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", bc.cacheConfig.TrieTimeLimit, "optimum", float64(chosen-lastWrite)/triesInMemory)
1249+
if chosen < lastWrite+TriesInMemory && bc.gcproc >= 2*bc.cacheConfig.TrieTimeLimit {
1250+
log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", bc.cacheConfig.TrieTimeLimit, "optimum", float64(chosen-lastWrite)/TriesInMemory)
12511251
}
12521252
// Flush an entire trie and restart the counters
12531253
triedb.Commit(header.Root, true)

‎core/blockchain_test.go‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,7 +1534,7 @@ func TestTrieForkGC(t *testing.T) {
15341534

15351535
db := rawdb.NewMemoryDatabase()
15361536
genesis := new(Genesis).MustCommit(db)
1537-
blocks, _ := GenerateChain(params.TestChainConfig, genesis, engine, db, 2*triesInMemory, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) })
1537+
blocks, _ := GenerateChain(params.TestChainConfig, genesis, engine, db, 2*TriesInMemory, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) })
15381538

15391539
// Generate a bunch of fork blocks, each side forking from the canonical chain
15401540
forks := make([]*types.Block, len(blocks))
@@ -1563,7 +1563,7 @@ func TestTrieForkGC(t *testing.T) {
15631563
}
15641564
}
15651565
// Dereference all the recent tries and ensure no past trie is left in
1566-
for i := 0; i < triesInMemory; i++ {
1566+
for i := 0; i < TriesInMemory; i++ {
15671567
chain.stateCache.TrieDB().Dereference(blocks[len(blocks)-1-i].Root())
15681568
chain.stateCache.TrieDB().Dereference(forks[len(blocks)-1-i].Root())
15691569
}
@@ -1582,8 +1582,8 @@ func TestLargeReorgTrieGC(t *testing.T) {
15821582
genesis := new(Genesis).MustCommit(db)
15831583

15841584
shared, _ := GenerateChain(params.TestChainConfig, genesis, engine, db, 64, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) })
1585-
original, _ := GenerateChain(params.TestChainConfig, shared[len(shared)-1], engine, db, 2*triesInMemory, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{2}) })
1586-
competitor, _ := GenerateChain(params.TestChainConfig, shared[len(shared)-1], engine, db, 2*triesInMemory+1, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{3}) })
1585+
original, _ := GenerateChain(params.TestChainConfig, shared[len(shared)-1], engine, db, 2*TriesInMemory, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{2}) })
1586+
competitor, _ := GenerateChain(params.TestChainConfig, shared[len(shared)-1], engine, db, 2*TriesInMemory+1, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{3}) })
15871587

15881588
// Import the shared chain and the original canonical one
15891589
diskdb := rawdb.NewMemoryDatabase()
@@ -1618,7 +1618,7 @@ func TestLargeReorgTrieGC(t *testing.T) {
16181618
if _, err := chain.InsertChain(competitor[len(competitor)-2:]); err != nil {
16191619
t.Fatalf("failed to finalize competitor chain: %v", err)
16201620
}
1621-
for i, block := range competitor[:len(competitor)-triesInMemory] {
1621+
for i, block := range competitor[:len(competitor)-TriesInMemory] {
16221622
if node, _ := chain.stateCache.TrieDB().Node(block.Root()); node != nil {
16231623
t.Fatalf("competitor %d: competing chain state missing", i)
16241624
}
@@ -1753,7 +1753,7 @@ func TestLowDiffLongChain(t *testing.T) {
17531753

17541754
// We must use a pretty long chain to ensure that the fork doesn't overtake us
17551755
// until after at least 128 blocks post tip
1756-
blocks, _ := GenerateChain(params.TestChainConfig, genesis, engine, db, 6*triesInMemory, func(i int, b *BlockGen) {
1756+
blocks, _ := GenerateChain(params.TestChainConfig, genesis, engine, db, 6*TriesInMemory, func(i int, b *BlockGen) {
17571757
b.SetCoinbase(common.Address{1})
17581758
b.OffsetTime(-9)
17591759
})
@@ -1771,7 +1771,7 @@ func TestLowDiffLongChain(t *testing.T) {
17711771
}
17721772
// Generate fork chain, starting from an early block
17731773
parent := blocks[10]
1774-
fork, _ := GenerateChain(params.TestChainConfig, parent, engine, db, 8*triesInMemory, func(i int, b *BlockGen) {
1774+
fork, _ := GenerateChain(params.TestChainConfig, parent, engine, db, 8*TriesInMemory, func(i int, b *BlockGen) {
17751775
b.SetCoinbase(common.Address{2})
17761776
})
17771777

@@ -1806,7 +1806,7 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon
18061806
genesis := new(Genesis).MustCommit(db)
18071807

18081808
// Generate and import the canonical chain
1809-
blocks, _ := GenerateChain(params.TestChainConfig, genesis, engine, db, 2*triesInMemory, nil)
1809+
blocks, _ := GenerateChain(params.TestChainConfig, genesis, engine, db, 2*TriesInMemory, nil)
18101810
diskdb := rawdb.NewMemoryDatabase()
18111811
new(Genesis).MustCommit(diskdb)
18121812
chain, err := NewBlockChain(diskdb, nil, params.TestChainConfig, engine, vm.Config{}, nil)
@@ -1817,9 +1817,9 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon
18171817
t.Fatalf("block %d: failed to insert into chain: %v", n, err)
18181818
}
18191819

1820-
lastPrunedIndex := len(blocks) - triesInMemory - 1
1820+
lastPrunedIndex := len(blocks) - TriesInMemory - 1
18211821
lastPrunedBlock := blocks[lastPrunedIndex]
1822-
firstNonPrunedBlock := blocks[len(blocks)-triesInMemory]
1822+
firstNonPrunedBlock := blocks[len(blocks)-TriesInMemory]
18231823

18241824
// Verify pruning of lastPrunedBlock
18251825
if chain.HasBlockAndState(lastPrunedBlock.Hash(), lastPrunedBlock.NumberU64()) {
@@ -1836,7 +1836,7 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon
18361836
// Generate fork chain, make it longer than canon
18371837
parentIndex := lastPrunedIndex + blocksBetweenCommonAncestorAndPruneblock
18381838
parent := blocks[parentIndex]
1839-
fork, _ := GenerateChain(params.TestChainConfig, parent, engine, db, 2*triesInMemory, func(i int, b *BlockGen) {
1839+
fork, _ := GenerateChain(params.TestChainConfig, parent, engine, db, 2*TriesInMemory, func(i int, b *BlockGen) {
18401840
b.SetCoinbase(common.Address{2})
18411841
})
18421842
// Prepend the parent(s)

‎les/api.go‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ package les
1919
import (
2020
"context"
2121
"errors"
22+
"fmt"
2223
"sync"
2324
"time"
2425

2526
"github.com/ethereum/go-ethereum/common/hexutil"
2627
"github.com/ethereum/go-ethereum/common/mclock"
28+
"github.com/ethereum/go-ethereum/les/csvlogger"
2729
"github.com/ethereum/go-ethereum/p2p/enode"
2830
"github.com/ethereum/go-ethereum/rpc"
2931
)
@@ -99,7 +101,7 @@ func (s tcSubs) send(tc uint64, underrun bool) {
99101

100102
// MinimumCapacity queries minimum assignable capacity for a single client
101103
func (api *PrivateLightServerAPI) MinimumCapacity() hexutil.Uint64 {
102-
return hexutil.Uint64(minCapacity)
104+
return hexutil.Uint64(api.server.minCapacity)
103105
}
104106

105107
// FreeClientCapacity queries the capacity provided for free clients
@@ -115,7 +117,7 @@ func (api *PrivateLightServerAPI) FreeClientCapacity() hexutil.Uint64 {
115117
// Note: assigned capacity can be changed while the client is connected with
116118
// immediate effect.
117119
func (api *PrivateLightServerAPI) SetClientCapacity(id enode.ID, cap uint64) error {
118-
if cap != 0 && cap < minCapacity {
120+
if cap != 0 && cap < api.server.minCapacity {
119121
return ErrMinCap
120122
}
121123
return api.server.priorityClientPool.setClientCapacity(id, cap)
@@ -144,6 +146,8 @@ type priorityClientPool struct {
144146
totalCap, totalCapAnnounced uint64
145147
totalConnectedCap, freeClientCap uint64
146148
maxPeers, priorityCount int
149+
logger *csvlogger.Logger
150+
logTotalPriConn *csvlogger.Channel
147151

148152
subs tcSubs
149153
updateSchedule []scheduledUpdate
@@ -164,12 +168,14 @@ type priorityClientInfo struct {
164168
}
165169

166170
// newPriorityClientPool creates a new priority client pool
167-
func newPriorityClientPool(freeClientCap uint64, ps *peerSet, child clientPool) *priorityClientPool {
171+
func newPriorityClientPool(freeClientCap uint64, ps *peerSet, child clientPool, metricsLogger, eventLogger *csvlogger.Logger) *priorityClientPool {
168172
return &priorityClientPool{
169-
clients: make(map[enode.ID]priorityClientInfo),
170-
freeClientCap: freeClientCap,
171-
ps: ps,
172-
child: child,
173+
clients: make(map[enode.ID]priorityClientInfo),
174+
freeClientCap: freeClientCap,
175+
ps: ps,
176+
child: child,
177+
logger: eventLogger,
178+
logTotalPriConn: metricsLogger.NewChannel("totalPriConn", 0),
173179
}
174180
}
175181

@@ -185,13 +191,15 @@ func (v *priorityClientPool) registerPeer(p *peer) {
185191

186192
id := p.ID()
187193
c := v.clients[id]
194+
v.logger.Event(fmt.Sprintf("priorityClientPool: registerPeer cap=%d connected=%v, %x", c.cap, c.connected, id.Bytes()))
188195
if c.connected {
189196
return
190197
}
191198
if c.cap == 0 && v.child != nil {
192199
v.child.registerPeer(p)
193200
}
194201
if c.cap != 0 && v.totalConnectedCap+c.cap > v.totalCap {
202+
v.logger.Event(fmt.Sprintf("priorityClientPool: rejected, %x", id.Bytes()))
195203
go v.ps.Unregister(p.id)
196204
return
197205
}
@@ -202,6 +210,8 @@ func (v *priorityClientPool) registerPeer(p *peer) {
202210
if c.cap != 0 {
203211
v.priorityCount++
204212
v.totalConnectedCap += c.cap
213+
v.logger.Event(fmt.Sprintf("priorityClientPool: accepted with %d capacity, %x", c.cap, id.Bytes()))
214+
v.logTotalPriConn.Update(float64(v.totalConnectedCap))
205215
if v.child != nil {
206216
v.child.setLimits(v.maxPeers-v.priorityCount, v.totalCap-v.totalConnectedCap)
207217
}
@@ -217,6 +227,7 @@ func (v *priorityClientPool) unregisterPeer(p *peer) {
217227

218228
id := p.ID()
219229
c := v.clients[id]
230+
v.logger.Event(fmt.Sprintf("priorityClientPool: unregisterPeer cap=%d connected=%v, %x", c.cap, c.connected, id.Bytes()))
220231
if !c.connected {
221232
return
222233
}
@@ -225,6 +236,7 @@ func (v *priorityClientPool) unregisterPeer(p *peer) {
225236
v.clients[id] = c
226237
v.priorityCount--
227238
v.totalConnectedCap -= c.cap
239+
v.logTotalPriConn.Update(float64(v.totalConnectedCap))
228240
if v.child != nil {
229241
v.child.setLimits(v.maxPeers-v.priorityCount, v.totalCap-v.totalConnectedCap)
230242
}
@@ -299,8 +311,10 @@ func (v *priorityClientPool) setLimitsNow(count int, totalCap uint64) {
299311
if v.priorityCount > count || v.totalConnectedCap > totalCap {
300312
for id, c := range v.clients {
301313
if c.connected {
314+
v.logger.Event(fmt.Sprintf("priorityClientPool: setLimitsNow kicked out, %x", id.Bytes()))
302315
c.connected = false
303316
v.totalConnectedCap -= c.cap
317+
v.logTotalPriConn.Update(float64(v.totalConnectedCap))
304318
v.priorityCount--
305319
v.clients[id] = c
306320
go v.ps.Unregister(c.peer.id)
@@ -356,6 +370,7 @@ func (v *priorityClientPool) setClientCapacity(id enode.ID, cap uint64) error {
356370
v.priorityCount--
357371
}
358372
v.totalConnectedCap += cap - c.cap
373+
v.logTotalPriConn.Update(float64(v.totalConnectedCap))
359374
if v.child != nil {
360375
v.child.setLimits(v.maxPeers-v.priorityCount, v.totalCap-v.totalConnectedCap)
361376
}
@@ -374,6 +389,9 @@ func (v *priorityClientPool) setClientCapacity(id enode.ID, cap uint64) error {
374389
} else {
375390
delete(v.clients, id)
376391
}
392+
if c.connected {
393+
v.logger.Event(fmt.Sprintf("priorityClientPool: changed capacity to %d, %x", cap, id.Bytes()))
394+
}
377395
return nil
378396
}
379397

0 commit comments

Comments
 (0)