Skip to content

Commit 42f44dd

Browse files
committed
eth, eth/downloader: handle header requests, table driven proto tests
1 parent c51e153 commit 42f44dd

14 files changed

Lines changed: 1209 additions & 254 deletions

File tree

‎cmd/geth/main.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
283283
utils.DataDirFlag,
284284
utils.BlockchainVersionFlag,
285285
utils.OlympicFlag,
286+
utils.EthVersionFlag,
286287
utils.CacheFlag,
287288
utils.JSpathFlag,
288289
utils.ListenPortFlag,
@@ -333,6 +334,7 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
333334
app.Before = func(ctx *cli.Context) error {
334335
utils.SetupLogger(ctx)
335336
utils.SetupVM(ctx)
337+
utils.SetupEth(ctx)
336338
if ctx.GlobalBool(utils.PProfEanbledFlag.Name) {
337339
utils.StartPProf(ctx)
338340
}

‎cmd/utils/flags.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ var (
138138
Name: "olympic",
139139
Usage: "Use olympic style protocol",
140140
}
141+
EthVersionFlag = cli.IntFlag{
142+
Name: "eth",
143+
Value: 61,
144+
Usage: "Highest eth protocol to advertise (temporary, dev option)",
145+
}
141146

142147
// miner settings
143148
MinerThreadsFlag = cli.IntFlag{
@@ -459,6 +464,18 @@ func SetupVM(ctx *cli.Context) {
459464
vm.SetJITCacheSize(ctx.GlobalInt(VMJitCacheFlag.Name))
460465
}
461466

467+
// SetupEth configures the eth packages global settings
468+
func SetupEth(ctx *cli.Context) {
469+
version := ctx.GlobalInt(EthVersionFlag.Name)
470+
for len(eth.ProtocolVersions) > 0 && eth.ProtocolVersions[0] > uint(version) {
471+
eth.ProtocolVersions = eth.ProtocolVersions[1:]
472+
eth.ProtocolLengths = eth.ProtocolLengths[1:]
473+
}
474+
if len(eth.ProtocolVersions) == 0 {
475+
Fatalf("No valid eth protocols remaining")
476+
}
477+
}
478+
462479
// MakeChain creates a chain manager from set command line flags.
463480
func MakeChain(ctx *cli.Context) (chain *core.ChainManager, chainDb common.Database) {
464481
datadir := ctx.GlobalString(DataDirFlag.Name)

‎eth/backend.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ func New(config *Config) (*Ethereum, error) {
373373

374374
eth.blockProcessor = core.NewBlockProcessor(chainDb, eth.pow, eth.chainManager, eth.EventMux())
375375
eth.chainManager.SetProcessor(eth.blockProcessor)
376-
eth.protocolManager = NewProtocolManager(config.NetworkId, eth.eventMux, eth.txPool, eth.pow, eth.chainManager)
376+
eth.protocolManager = NewProtocolManager(config.NetworkId, eth.eventMux, eth.txPool, eth.pow, eth.chainManager, chainDb)
377377

378378
eth.miner = miner.New(eth, eth.EventMux(), eth.pow)
379379
eth.miner.SetGasPrice(config.GasPrice)

‎eth/downloader/downloader.go‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ import (
3939
const (
4040
eth60 = 60 // Constant to check for old protocol support
4141
eth61 = 61 // Constant to check for new protocol support
42-
eth62 = 62 // Constant to check for experimental protocol support
4342
)
4443

4544
var (
46-
MinHashFetch = 512 // Minimum amount of hashes to not consider a peer stalling
47-
MaxHashFetch = 512 // Amount of hashes to be fetched per retrieval request
48-
MaxBlockFetch = 128 // Amount of blocks to be fetched per retrieval request
45+
MinHashFetch = 512 // Minimum amount of hashes to not consider a peer stalling
46+
MaxHashFetch = 512 // Amount of hashes to be fetched per retrieval request
47+
MaxBlockFetch = 128 // Amount of blocks to be fetched per retrieval request
48+
MaxHeaderFetch = 256 // Amount of block headers to be fetched per retrieval request
49+
MaxStateFetch = 384 // Amount of node state values to allow fetching per request
50+
MaxReceiptsFetch = 384 // Amount of transaction receipts to allow fetching per request
4951

5052
hashTTL = 5 * time.Second // Time it takes for a hash request to time out
5153
blockSoftTTL = 3 * time.Second // Request completion threshold for increasing or decreasing a peer's bandwidth
@@ -330,7 +332,7 @@ func (d *Downloader) syncWithPeer(p *peer, hash common.Hash, td *big.Int) (err e
330332
if err = d.fetchBlocks60(); err != nil {
331333
return err
332334
}
333-
case eth61, eth62:
335+
case eth61:
334336
// New eth/61, use forward, concurrent hash and block retrieval algorithm
335337
number, err := d.findAncestor(p)
336338
if err != nil {

‎eth/fetcher/fetcher.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ type peerDropFn func(id string)
6969
// announce is the hash notification of the availability of a new block in the
7070
// network.
7171
type announce struct {
72-
hash common.Hash // Hash of the block being announced
73-
time time.Time // Timestamp of the announcement
72+
hash common.Hash // Hash of the block being announced
73+
number uint64 // Number of the block being announced (0 = unknown | old protocol)
74+
time time.Time // Timestamp of the announcement
7475

7576
origin string // Identifier of the peer originating the notification
7677
fetch blockRequesterFn // Fetcher function to retrieve
@@ -152,9 +153,10 @@ func (f *Fetcher) Stop() {
152153

153154
// Notify announces the fetcher of the potential availability of a new block in
154155
// the network.
155-
func (f *Fetcher) Notify(peer string, hash common.Hash, time time.Time, fetcher blockRequesterFn) error {
156+
func (f *Fetcher) Notify(peer string, hash common.Hash, number uint64, time time.Time, fetcher blockRequesterFn) error {
156157
block := &announce{
157158
hash: hash,
159+
number: number,
158160
time: time,
159161
origin: peer,
160162
fetch: fetcher,

‎eth/fetcher/fetcher_test.go‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func TestSequentialAnnouncements(t *testing.T) {
194194
tester.fetcher.importedHook = func(block *types.Block) { imported <- block }
195195

196196
for i := len(hashes) - 2; i >= 0; i-- {
197-
tester.fetcher.Notify("valid", hashes[i], time.Now().Add(-arriveTimeout), fetcher)
197+
tester.fetcher.Notify("valid", hashes[i], 0, time.Now().Add(-arriveTimeout), fetcher)
198198
verifyImportEvent(t, imported)
199199
}
200200
verifyImportDone(t, imported)
@@ -221,9 +221,9 @@ func TestConcurrentAnnouncements(t *testing.T) {
221221
tester.fetcher.importedHook = func(block *types.Block) { imported <- block }
222222

223223
for i := len(hashes) - 2; i >= 0; i-- {
224-
tester.fetcher.Notify("first", hashes[i], time.Now().Add(-arriveTimeout), wrapper)
225-
tester.fetcher.Notify("second", hashes[i], time.Now().Add(-arriveTimeout+time.Millisecond), wrapper)
226-
tester.fetcher.Notify("second", hashes[i], time.Now().Add(-arriveTimeout-time.Millisecond), wrapper)
224+
tester.fetcher.Notify("first", hashes[i], 0, time.Now().Add(-arriveTimeout), wrapper)
225+
tester.fetcher.Notify("second", hashes[i], 0, time.Now().Add(-arriveTimeout+time.Millisecond), wrapper)
226+
tester.fetcher.Notify("second", hashes[i], 0, time.Now().Add(-arriveTimeout-time.Millisecond), wrapper)
227227

228228
verifyImportEvent(t, imported)
229229
}
@@ -252,7 +252,7 @@ func TestOverlappingAnnouncements(t *testing.T) {
252252
tester.fetcher.importedHook = func(block *types.Block) { imported <- block }
253253

254254
for i := len(hashes) - 2; i >= 0; i-- {
255-
tester.fetcher.Notify("valid", hashes[i], time.Now().Add(-arriveTimeout), fetcher)
255+
tester.fetcher.Notify("valid", hashes[i], 0, time.Now().Add(-arriveTimeout), fetcher)
256256
select {
257257
case <-fetching:
258258
case <-time.After(time.Second):
@@ -286,7 +286,7 @@ func TestPendingDeduplication(t *testing.T) {
286286
}
287287
// Announce the same block many times until it's fetched (wait for any pending ops)
288288
for tester.getBlock(hashes[0]) == nil {
289-
tester.fetcher.Notify("repeater", hashes[0], time.Now().Add(-arriveTimeout), wrapper)
289+
tester.fetcher.Notify("repeater", hashes[0], 0, time.Now().Add(-arriveTimeout), wrapper)
290290
time.Sleep(time.Millisecond)
291291
}
292292
time.Sleep(delay)
@@ -317,12 +317,12 @@ func TestRandomArrivalImport(t *testing.T) {
317317

318318
for i := len(hashes) - 1; i >= 0; i-- {
319319
if i != skip {
320-
tester.fetcher.Notify("valid", hashes[i], time.Now().Add(-arriveTimeout), fetcher)
320+
tester.fetcher.Notify("valid", hashes[i], 0, time.Now().Add(-arriveTimeout), fetcher)
321321
time.Sleep(time.Millisecond)
322322
}
323323
}
324324
// Finally announce the skipped entry and check full import
325-
tester.fetcher.Notify("valid", hashes[skip], time.Now().Add(-arriveTimeout), fetcher)
325+
tester.fetcher.Notify("valid", hashes[skip], 0, time.Now().Add(-arriveTimeout), fetcher)
326326
verifyImportCount(t, imported, len(hashes)-1)
327327
}
328328

@@ -343,7 +343,7 @@ func TestQueueGapFill(t *testing.T) {
343343

344344
for i := len(hashes) - 1; i >= 0; i-- {
345345
if i != skip {
346-
tester.fetcher.Notify("valid", hashes[i], time.Now().Add(-arriveTimeout), fetcher)
346+
tester.fetcher.Notify("valid", hashes[i], 0, time.Now().Add(-arriveTimeout), fetcher)
347347
time.Sleep(time.Millisecond)
348348
}
349349
}
@@ -374,7 +374,7 @@ func TestImportDeduplication(t *testing.T) {
374374
tester.fetcher.importedHook = func(block *types.Block) { imported <- block }
375375

376376
// Announce the duplicating block, wait for retrieval, and also propagate directly
377-
tester.fetcher.Notify("valid", hashes[0], time.Now().Add(-arriveTimeout), fetcher)
377+
tester.fetcher.Notify("valid", hashes[0], 0, time.Now().Add(-arriveTimeout), fetcher)
378378
<-fetching
379379

380380
tester.fetcher.Enqueue("valid", blocks[hashes[0]])
@@ -437,9 +437,9 @@ func TestHashMemoryExhaustionAttack(t *testing.T) {
437437
// Feed the tester a huge hashset from the attacker, and a limited from the valid peer
438438
for i := 0; i < len(attack); i++ {
439439
if i < maxQueueDist {
440-
tester.fetcher.Notify("valid", hashes[len(hashes)-2-i], time.Now(), valid)
440+
tester.fetcher.Notify("valid", hashes[len(hashes)-2-i], 0, time.Now(), valid)
441441
}
442-
tester.fetcher.Notify("attacker", attack[i], time.Now(), attacker)
442+
tester.fetcher.Notify("attacker", attack[i], 0, time.Now(), attacker)
443443
}
444444
if len(tester.fetcher.announced) != hashLimit+maxQueueDist {
445445
t.Fatalf("queued announce count mismatch: have %d, want %d", len(tester.fetcher.announced), hashLimit+maxQueueDist)
@@ -449,7 +449,7 @@ func TestHashMemoryExhaustionAttack(t *testing.T) {
449449

450450
// Feed the remaining valid hashes to ensure DOS protection state remains clean
451451
for i := len(hashes) - maxQueueDist - 2; i >= 0; i-- {
452-
tester.fetcher.Notify("valid", hashes[i], time.Now().Add(-arriveTimeout), valid)
452+
tester.fetcher.Notify("valid", hashes[i], 0, time.Now().Add(-arriveTimeout), valid)
453453
verifyImportEvent(t, imported)
454454
}
455455
verifyImportDone(t, imported)

0 commit comments

Comments
 (0)