github.com/shyftnetwork/go-empyrean@v1.8.3-0.20191127201940-fbfca9338f04/eth/downloader/downloader_test.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package downloader 18 19 import ( 20 "errors" 21 "fmt" 22 "math/big" 23 "strings" 24 "sync" 25 "sync/atomic" 26 "testing" 27 "time" 28 29 ethereum "github.com/ShyftNetwork/go-empyrean" 30 "github.com/ShyftNetwork/go-empyrean/common" 31 "github.com/ShyftNetwork/go-empyrean/core/types" 32 "github.com/ShyftNetwork/go-empyrean/ethdb" 33 "github.com/ShyftNetwork/go-empyrean/event" 34 "github.com/ShyftNetwork/go-empyrean/trie" 35 ) 36 37 // Reduce some of the parameters to make the tester faster. 38 func init() { 39 MaxForkAncestry = uint64(10000) 40 blockCacheItems = 1024 41 fsHeaderContCheck = 500 * time.Millisecond 42 } 43 44 // downloadTester is a test simulator for mocking out local block chain. 45 type downloadTester struct { 46 downloader *Downloader 47 48 genesis *types.Block // Genesis blocks used by the tester and peers 49 stateDb ethdb.Database // Database used by the tester for syncing from peers 50 peerDb ethdb.Database // Database of the peers containing all data 51 shyftDb ethdb.SDatabase //Shyft postgres instance 52 peers map[string]*downloadTesterPeer 53 54 ownHashes []common.Hash // Hash chain belonging to the tester 55 ownHeaders map[common.Hash]*types.Header // Headers belonging to the tester 56 ownBlocks map[common.Hash]*types.Block // Blocks belonging to the tester 57 ownReceipts map[common.Hash]types.Receipts // Receipts belonging to the tester 58 ownChainTd map[common.Hash]*big.Int // Total difficulties of the blocks in the local chain 59 60 lock sync.RWMutex 61 } 62 63 // newTester creates a new downloader test mocker. 64 func newTester() *downloadTester { 65 tester := &downloadTester{ 66 genesis: testGenesis, 67 peerDb: testDB, 68 shyftDb: nil, 69 peers: make(map[string]*downloadTesterPeer), 70 ownHashes: []common.Hash{testGenesis.Hash()}, 71 ownHeaders: map[common.Hash]*types.Header{testGenesis.Hash(): testGenesis.Header()}, 72 ownBlocks: map[common.Hash]*types.Block{testGenesis.Hash(): testGenesis}, 73 ownReceipts: map[common.Hash]types.Receipts{testGenesis.Hash(): nil}, 74 ownChainTd: map[common.Hash]*big.Int{testGenesis.Hash(): testGenesis.Difficulty()}, 75 } 76 tester.stateDb = ethdb.NewMemDatabase() 77 tester.stateDb.Put(testGenesis.Root().Bytes(), []byte{0x00}) 78 tester.downloader = New(FullSync, tester.stateDb, tester.shyftDb, new(event.TypeMux), tester, nil, tester.dropPeer) 79 return tester 80 } 81 82 // terminate aborts any operations on the embedded downloader and releases all 83 // held resources. 84 func (dl *downloadTester) terminate() { 85 dl.downloader.Terminate() 86 } 87 88 // sync starts synchronizing with a remote peer, blocking until it completes. 89 func (dl *downloadTester) sync(id string, td *big.Int, mode SyncMode) error { 90 dl.lock.RLock() 91 hash := dl.peers[id].chain.headBlock().Hash() 92 // If no particular TD was requested, load from the peer's blockchain 93 if td == nil { 94 td = dl.peers[id].chain.td(hash) 95 } 96 dl.lock.RUnlock() 97 98 // Synchronise with the chosen peer and ensure proper cleanup afterwards 99 err := dl.downloader.synchronise(id, hash, td, mode) 100 select { 101 case <-dl.downloader.cancelCh: 102 // Ok, downloader fully cancelled after sync cycle 103 default: 104 // Downloader is still accepting packets, can block a peer up 105 panic("downloader active post sync cycle") // panic will be caught by tester 106 } 107 return err 108 } 109 110 // HasHeader checks if a header is present in the testers canonical chain. 111 func (dl *downloadTester) HasHeader(hash common.Hash, number uint64) bool { 112 return dl.GetHeaderByHash(hash) != nil 113 } 114 115 // HasBlock checks if a block is present in the testers canonical chain. 116 func (dl *downloadTester) HasBlock(hash common.Hash, number uint64) bool { 117 return dl.GetBlockByHash(hash) != nil 118 } 119 120 // HasFastBlock checks if a block is present in the testers canonical chain. 121 func (dl *downloadTester) HasFastBlock(hash common.Hash, number uint64) bool { 122 dl.lock.RLock() 123 defer dl.lock.RUnlock() 124 125 _, ok := dl.ownReceipts[hash] 126 return ok 127 } 128 129 // GetHeader retrieves a header from the testers canonical chain. 130 func (dl *downloadTester) GetHeaderByHash(hash common.Hash) *types.Header { 131 dl.lock.RLock() 132 defer dl.lock.RUnlock() 133 134 return dl.ownHeaders[hash] 135 } 136 137 // GetBlock retrieves a block from the testers canonical chain. 138 func (dl *downloadTester) GetBlockByHash(hash common.Hash) *types.Block { 139 dl.lock.RLock() 140 defer dl.lock.RUnlock() 141 142 return dl.ownBlocks[hash] 143 } 144 145 // CurrentHeader retrieves the current head header from the canonical chain. 146 func (dl *downloadTester) CurrentHeader() *types.Header { 147 dl.lock.RLock() 148 defer dl.lock.RUnlock() 149 150 for i := len(dl.ownHashes) - 1; i >= 0; i-- { 151 if header := dl.ownHeaders[dl.ownHashes[i]]; header != nil { 152 return header 153 } 154 } 155 return dl.genesis.Header() 156 } 157 158 // CurrentBlock retrieves the current head block from the canonical chain. 159 func (dl *downloadTester) CurrentBlock() *types.Block { 160 dl.lock.RLock() 161 defer dl.lock.RUnlock() 162 163 for i := len(dl.ownHashes) - 1; i >= 0; i-- { 164 if block := dl.ownBlocks[dl.ownHashes[i]]; block != nil { 165 if _, err := dl.stateDb.Get(block.Root().Bytes()); err == nil { 166 return block 167 } 168 } 169 } 170 return dl.genesis 171 } 172 173 // CurrentFastBlock retrieves the current head fast-sync block from the canonical chain. 174 func (dl *downloadTester) CurrentFastBlock() *types.Block { 175 dl.lock.RLock() 176 defer dl.lock.RUnlock() 177 178 for i := len(dl.ownHashes) - 1; i >= 0; i-- { 179 if block := dl.ownBlocks[dl.ownHashes[i]]; block != nil { 180 return block 181 } 182 } 183 return dl.genesis 184 } 185 186 // FastSyncCommitHead manually sets the head block to a given hash. 187 func (dl *downloadTester) FastSyncCommitHead(hash common.Hash) error { 188 // For now only check that the state trie is correct 189 if block := dl.GetBlockByHash(hash); block != nil { 190 _, err := trie.NewSecure(block.Root(), trie.NewDatabase(dl.stateDb), 0) 191 return err 192 } 193 return fmt.Errorf("non existent block: %x", hash[:4]) 194 } 195 196 // GetTd retrieves the block's total difficulty from the canonical chain. 197 func (dl *downloadTester) GetTd(hash common.Hash, number uint64) *big.Int { 198 dl.lock.RLock() 199 defer dl.lock.RUnlock() 200 201 return dl.ownChainTd[hash] 202 } 203 204 // InsertHeaderChain injects a new batch of headers into the simulated chain. 205 func (dl *downloadTester) InsertHeaderChain(headers []*types.Header, checkFreq int) (i int, err error) { 206 dl.lock.Lock() 207 defer dl.lock.Unlock() 208 209 // Do a quick check, as the blockchain.InsertHeaderChain doesn't insert anything in case of errors 210 if _, ok := dl.ownHeaders[headers[0].ParentHash]; !ok { 211 return 0, errors.New("unknown parent") 212 } 213 for i := 1; i < len(headers); i++ { 214 if headers[i].ParentHash != headers[i-1].Hash() { 215 return i, errors.New("unknown parent") 216 } 217 } 218 // Do a full insert if pre-checks passed 219 for i, header := range headers { 220 if _, ok := dl.ownHeaders[header.Hash()]; ok { 221 continue 222 } 223 if _, ok := dl.ownHeaders[header.ParentHash]; !ok { 224 return i, errors.New("unknown parent") 225 } 226 dl.ownHashes = append(dl.ownHashes, header.Hash()) 227 dl.ownHeaders[header.Hash()] = header 228 dl.ownChainTd[header.Hash()] = new(big.Int).Add(dl.ownChainTd[header.ParentHash], header.Difficulty) 229 } 230 return len(headers), nil 231 } 232 233 // InsertChain injects a new batch of blocks into the simulated chain. 234 func (dl *downloadTester) InsertChain(blocks types.Blocks) (i int, err error) { 235 dl.lock.Lock() 236 defer dl.lock.Unlock() 237 238 for i, block := range blocks { 239 if parent, ok := dl.ownBlocks[block.ParentHash()]; !ok { 240 return i, errors.New("unknown parent") 241 } else if _, err := dl.stateDb.Get(parent.Root().Bytes()); err != nil { 242 return i, fmt.Errorf("unknown parent state %x: %v", parent.Root(), err) 243 } 244 if _, ok := dl.ownHeaders[block.Hash()]; !ok { 245 dl.ownHashes = append(dl.ownHashes, block.Hash()) 246 dl.ownHeaders[block.Hash()] = block.Header() 247 } 248 dl.ownBlocks[block.Hash()] = block 249 dl.ownReceipts[block.Hash()] = make(types.Receipts, 0) 250 dl.stateDb.Put(block.Root().Bytes(), []byte{0x00}) 251 dl.ownChainTd[block.Hash()] = new(big.Int).Add(dl.ownChainTd[block.ParentHash()], block.Difficulty()) 252 } 253 return len(blocks), nil 254 } 255 256 // InsertReceiptChain injects a new batch of receipts into the simulated chain. 257 func (dl *downloadTester) InsertReceiptChain(blocks types.Blocks, receipts []types.Receipts) (i int, err error) { 258 dl.lock.Lock() 259 defer dl.lock.Unlock() 260 261 for i := 0; i < len(blocks) && i < len(receipts); i++ { 262 if _, ok := dl.ownHeaders[blocks[i].Hash()]; !ok { 263 return i, errors.New("unknown owner") 264 } 265 if _, ok := dl.ownBlocks[blocks[i].ParentHash()]; !ok { 266 return i, errors.New("unknown parent") 267 } 268 dl.ownBlocks[blocks[i].Hash()] = blocks[i] 269 dl.ownReceipts[blocks[i].Hash()] = receipts[i] 270 } 271 return len(blocks), nil 272 } 273 274 // Rollback removes some recently added elements from the chain. 275 func (dl *downloadTester) Rollback(hashes []common.Hash) { 276 dl.lock.Lock() 277 defer dl.lock.Unlock() 278 279 for i := len(hashes) - 1; i >= 0; i-- { 280 if dl.ownHashes[len(dl.ownHashes)-1] == hashes[i] { 281 dl.ownHashes = dl.ownHashes[:len(dl.ownHashes)-1] 282 } 283 delete(dl.ownChainTd, hashes[i]) 284 delete(dl.ownHeaders, hashes[i]) 285 delete(dl.ownReceipts, hashes[i]) 286 delete(dl.ownBlocks, hashes[i]) 287 } 288 } 289 290 // newPeer registers a new block download source into the downloader. 291 func (dl *downloadTester) newPeer(id string, version int, chain *testChain) error { 292 dl.lock.Lock() 293 defer dl.lock.Unlock() 294 295 peer := &downloadTesterPeer{dl: dl, id: id, chain: chain} 296 dl.peers[id] = peer 297 return dl.downloader.RegisterPeer(id, version, peer) 298 } 299 300 // dropPeer simulates a hard peer removal from the connection pool. 301 func (dl *downloadTester) dropPeer(id string) { 302 dl.lock.Lock() 303 defer dl.lock.Unlock() 304 305 delete(dl.peers, id) 306 dl.downloader.UnregisterPeer(id) 307 } 308 309 type downloadTesterPeer struct { 310 dl *downloadTester 311 id string 312 lock sync.RWMutex 313 chain *testChain 314 missingStates map[common.Hash]bool // State entries that fast sync should not return 315 } 316 317 // Head constructs a function to retrieve a peer's current head hash 318 // and total difficulty. 319 func (dlp *downloadTesterPeer) Head() (common.Hash, *big.Int) { 320 b := dlp.chain.headBlock() 321 return b.Hash(), dlp.chain.td(b.Hash()) 322 } 323 324 // RequestHeadersByHash constructs a GetBlockHeaders function based on a hashed 325 // origin; associated with a particular peer in the download tester. The returned 326 // function can be used to retrieve batches of headers from the particular peer. 327 func (dlp *downloadTesterPeer) RequestHeadersByHash(origin common.Hash, amount int, skip int, reverse bool) error { 328 if reverse { 329 panic("reverse header requests not supported") 330 } 331 332 result := dlp.chain.headersByHash(origin, amount, skip) 333 go dlp.dl.downloader.DeliverHeaders(dlp.id, result) 334 return nil 335 } 336 337 // RequestHeadersByNumber constructs a GetBlockHeaders function based on a numbered 338 // origin; associated with a particular peer in the download tester. The returned 339 // function can be used to retrieve batches of headers from the particular peer. 340 func (dlp *downloadTesterPeer) RequestHeadersByNumber(origin uint64, amount int, skip int, reverse bool) error { 341 if reverse { 342 panic("reverse header requests not supported") 343 } 344 345 result := dlp.chain.headersByNumber(origin, amount, skip) 346 go dlp.dl.downloader.DeliverHeaders(dlp.id, result) 347 return nil 348 } 349 350 // RequestBodies constructs a getBlockBodies method associated with a particular 351 // peer in the download tester. The returned function can be used to retrieve 352 // batches of block bodies from the particularly requested peer. 353 func (dlp *downloadTesterPeer) RequestBodies(hashes []common.Hash) error { 354 txs, uncles := dlp.chain.bodies(hashes) 355 go dlp.dl.downloader.DeliverBodies(dlp.id, txs, uncles) 356 return nil 357 } 358 359 // RequestReceipts constructs a getReceipts method associated with a particular 360 // peer in the download tester. The returned function can be used to retrieve 361 // batches of block receipts from the particularly requested peer. 362 func (dlp *downloadTesterPeer) RequestReceipts(hashes []common.Hash) error { 363 receipts := dlp.chain.receipts(hashes) 364 go dlp.dl.downloader.DeliverReceipts(dlp.id, receipts) 365 return nil 366 } 367 368 // RequestNodeData constructs a getNodeData method associated with a particular 369 // peer in the download tester. The returned function can be used to retrieve 370 // batches of node state data from the particularly requested peer. 371 func (dlp *downloadTesterPeer) RequestNodeData(hashes []common.Hash) error { 372 dlp.dl.lock.RLock() 373 defer dlp.dl.lock.RUnlock() 374 375 results := make([][]byte, 0, len(hashes)) 376 for _, hash := range hashes { 377 if data, err := dlp.dl.peerDb.Get(hash.Bytes()); err == nil { 378 if !dlp.missingStates[hash] { 379 results = append(results, data) 380 } 381 } 382 } 383 go dlp.dl.downloader.DeliverNodeData(dlp.id, results) 384 return nil 385 } 386 387 // assertOwnChain checks if the local chain contains the correct number of items 388 // of the various chain components. 389 func assertOwnChain(t *testing.T, tester *downloadTester, length int) { 390 // Mark this method as a helper to report errors at callsite, not in here 391 t.Helper() 392 393 assertOwnForkedChain(t, tester, 1, []int{length}) 394 } 395 396 // assertOwnForkedChain checks if the local forked chain contains the correct 397 // number of items of the various chain components. 398 func assertOwnForkedChain(t *testing.T, tester *downloadTester, common int, lengths []int) { 399 // Mark this method as a helper to report errors at callsite, not in here 400 t.Helper() 401 402 // Initialize the counters for the first fork 403 headers, blocks, receipts := lengths[0], lengths[0], lengths[0] 404 405 // Update the counters for each subsequent fork 406 for _, length := range lengths[1:] { 407 headers += length - common 408 blocks += length - common 409 receipts += length - common 410 } 411 if tester.downloader.mode == LightSync { 412 blocks, receipts = 1, 1 413 } 414 if hs := len(tester.ownHeaders); hs != headers { 415 t.Fatalf("synchronised headers mismatch: have %v, want %v", hs, headers) 416 } 417 if bs := len(tester.ownBlocks); bs != blocks { 418 t.Fatalf("synchronised blocks mismatch: have %v, want %v", bs, blocks) 419 } 420 if rs := len(tester.ownReceipts); rs != receipts { 421 t.Fatalf("synchronised receipts mismatch: have %v, want %v", rs, receipts) 422 } 423 } 424 425 // Tests that simple synchronization against a canonical chain works correctly. 426 // In this test common ancestor lookup should be short circuited and not require 427 // binary searching. 428 func TestCanonicalSynchronisation62(t *testing.T) { testCanonicalSynchronisation(t, 62, FullSync) } 429 func TestCanonicalSynchronisation63Full(t *testing.T) { testCanonicalSynchronisation(t, 63, FullSync) } 430 func TestCanonicalSynchronisation63Fast(t *testing.T) { testCanonicalSynchronisation(t, 63, FastSync) } 431 func TestCanonicalSynchronisation64Full(t *testing.T) { testCanonicalSynchronisation(t, 64, FullSync) } 432 func TestCanonicalSynchronisation64Fast(t *testing.T) { testCanonicalSynchronisation(t, 64, FastSync) } 433 func TestCanonicalSynchronisation64Light(t *testing.T) { testCanonicalSynchronisation(t, 64, LightSync) } 434 435 func testCanonicalSynchronisation(t *testing.T, protocol int, mode SyncMode) { 436 t.Parallel() 437 438 tester := newTester() 439 defer tester.terminate() 440 441 // Create a small enough block chain to download 442 chain := testChainBase.shorten(blockCacheItems - 15) 443 tester.newPeer("peer", protocol, chain) 444 445 // Synchronise with the peer and make sure all relevant data was retrieved 446 if err := tester.sync("peer", nil, mode); err != nil { 447 t.Fatalf("failed to synchronise blocks: %v", err) 448 } 449 assertOwnChain(t, tester, chain.len()) 450 } 451 452 // Tests that if a large batch of blocks are being downloaded, it is throttled 453 // until the cached blocks are retrieved. 454 func TestThrottling62(t *testing.T) { testThrottling(t, 62, FullSync) } 455 func TestThrottling63Full(t *testing.T) { testThrottling(t, 63, FullSync) } 456 func TestThrottling63Fast(t *testing.T) { testThrottling(t, 63, FastSync) } 457 func TestThrottling64Full(t *testing.T) { testThrottling(t, 64, FullSync) } 458 func TestThrottling64Fast(t *testing.T) { testThrottling(t, 64, FastSync) } 459 460 func testThrottling(t *testing.T, protocol int, mode SyncMode) { 461 t.Parallel() 462 tester := newTester() 463 defer tester.terminate() 464 465 // Create a long block chain to download and the tester 466 targetBlocks := testChainBase.len() - 1 467 tester.newPeer("peer", protocol, testChainBase) 468 469 // Wrap the importer to allow stepping 470 blocked, proceed := uint32(0), make(chan struct{}) 471 tester.downloader.chainInsertHook = func(results []*fetchResult) { 472 atomic.StoreUint32(&blocked, uint32(len(results))) 473 <-proceed 474 } 475 // Start a synchronisation concurrently 476 errc := make(chan error) 477 go func() { 478 errc <- tester.sync("peer", nil, mode) 479 }() 480 // Iteratively take some blocks, always checking the retrieval count 481 for { 482 // Check the retrieval count synchronously (! reason for this ugly block) 483 tester.lock.RLock() 484 retrieved := len(tester.ownBlocks) 485 tester.lock.RUnlock() 486 if retrieved >= targetBlocks+1 { 487 break 488 } 489 // Wait a bit for sync to throttle itself 490 var cached, frozen int 491 for start := time.Now(); time.Since(start) < 3*time.Second; { 492 time.Sleep(25 * time.Millisecond) 493 494 tester.lock.Lock() 495 tester.downloader.queue.lock.Lock() 496 cached = len(tester.downloader.queue.blockDonePool) 497 if mode == FastSync { 498 if receipts := len(tester.downloader.queue.receiptDonePool); receipts < cached { 499 cached = receipts 500 } 501 } 502 frozen = int(atomic.LoadUint32(&blocked)) 503 retrieved = len(tester.ownBlocks) 504 tester.downloader.queue.lock.Unlock() 505 tester.lock.Unlock() 506 507 if cached == blockCacheItems || cached == blockCacheItems-reorgProtHeaderDelay || retrieved+cached+frozen == targetBlocks+1 || retrieved+cached+frozen == targetBlocks+1-reorgProtHeaderDelay { 508 break 509 } 510 } 511 // Make sure we filled up the cache, then exhaust it 512 time.Sleep(25 * time.Millisecond) // give it a chance to screw up 513 514 tester.lock.RLock() 515 retrieved = len(tester.ownBlocks) 516 tester.lock.RUnlock() 517 if cached != blockCacheItems && cached != blockCacheItems-reorgProtHeaderDelay && retrieved+cached+frozen != targetBlocks+1 && retrieved+cached+frozen != targetBlocks+1-reorgProtHeaderDelay { 518 t.Fatalf("block count mismatch: have %v, want %v (owned %v, blocked %v, target %v)", cached, blockCacheItems, retrieved, frozen, targetBlocks+1) 519 } 520 // Permit the blocked blocks to import 521 if atomic.LoadUint32(&blocked) > 0 { 522 atomic.StoreUint32(&blocked, uint32(0)) 523 proceed <- struct{}{} 524 } 525 } 526 // Check that we haven't pulled more blocks than available 527 assertOwnChain(t, tester, targetBlocks+1) 528 if err := <-errc; err != nil { 529 t.Fatalf("block synchronization failed: %v", err) 530 } 531 } 532 533 // Tests that simple synchronization against a forked chain works correctly. In 534 // this test common ancestor lookup should *not* be short circuited, and a full 535 // binary search should be executed. 536 func TestForkedSync62(t *testing.T) { testForkedSync(t, 62, FullSync) } 537 func TestForkedSync63Full(t *testing.T) { testForkedSync(t, 63, FullSync) } 538 func TestForkedSync63Fast(t *testing.T) { testForkedSync(t, 63, FastSync) } 539 func TestForkedSync64Full(t *testing.T) { testForkedSync(t, 64, FullSync) } 540 func TestForkedSync64Fast(t *testing.T) { testForkedSync(t, 64, FastSync) } 541 func TestForkedSync64Light(t *testing.T) { testForkedSync(t, 64, LightSync) } 542 543 func testForkedSync(t *testing.T, protocol int, mode SyncMode) { 544 t.Parallel() 545 546 tester := newTester() 547 defer tester.terminate() 548 549 chainA := testChainForkLightA.shorten(testChainBase.len() + 80) 550 chainB := testChainForkLightB.shorten(testChainBase.len() + 80) 551 tester.newPeer("fork A", protocol, chainA) 552 tester.newPeer("fork B", protocol, chainB) 553 554 // Synchronise with the peer and make sure all blocks were retrieved 555 if err := tester.sync("fork A", nil, mode); err != nil { 556 t.Fatalf("failed to synchronise blocks: %v", err) 557 } 558 assertOwnChain(t, tester, chainA.len()) 559 560 // Synchronise with the second peer and make sure that fork is pulled too 561 if err := tester.sync("fork B", nil, mode); err != nil { 562 t.Fatalf("failed to synchronise blocks: %v", err) 563 } 564 assertOwnForkedChain(t, tester, testChainBase.len(), []int{chainA.len(), chainB.len()}) 565 } 566 567 // Tests that synchronising against a much shorter but much heavyer fork works 568 // corrently and is not dropped. 569 func TestHeavyForkedSync62(t *testing.T) { testHeavyForkedSync(t, 62, FullSync) } 570 func TestHeavyForkedSync63Full(t *testing.T) { testHeavyForkedSync(t, 63, FullSync) } 571 func TestHeavyForkedSync63Fast(t *testing.T) { testHeavyForkedSync(t, 63, FastSync) } 572 func TestHeavyForkedSync64Full(t *testing.T) { testHeavyForkedSync(t, 64, FullSync) } 573 func TestHeavyForkedSync64Fast(t *testing.T) { testHeavyForkedSync(t, 64, FastSync) } 574 func TestHeavyForkedSync64Light(t *testing.T) { testHeavyForkedSync(t, 64, LightSync) } 575 576 func testHeavyForkedSync(t *testing.T, protocol int, mode SyncMode) { 577 t.Parallel() 578 579 tester := newTester() 580 defer tester.terminate() 581 582 chainA := testChainForkLightA.shorten(testChainBase.len() + 80) 583 chainB := testChainForkHeavy.shorten(testChainBase.len() + 80) 584 tester.newPeer("light", protocol, chainA) 585 tester.newPeer("heavy", protocol, chainB) 586 587 // Synchronise with the peer and make sure all blocks were retrieved 588 if err := tester.sync("light", nil, mode); err != nil { 589 t.Fatalf("failed to synchronise blocks: %v", err) 590 } 591 assertOwnChain(t, tester, chainA.len()) 592 593 // Synchronise with the second peer and make sure that fork is pulled too 594 if err := tester.sync("heavy", nil, mode); err != nil { 595 t.Fatalf("failed to synchronise blocks: %v", err) 596 } 597 assertOwnForkedChain(t, tester, testChainBase.len(), []int{chainA.len(), chainB.len()}) 598 } 599 600 // Tests that chain forks are contained within a certain interval of the current 601 // chain head, ensuring that malicious peers cannot waste resources by feeding 602 // long dead chains. 603 func TestBoundedForkedSync62(t *testing.T) { testBoundedForkedSync(t, 62, FullSync) } 604 func TestBoundedForkedSync63Full(t *testing.T) { testBoundedForkedSync(t, 63, FullSync) } 605 func TestBoundedForkedSync63Fast(t *testing.T) { testBoundedForkedSync(t, 63, FastSync) } 606 func TestBoundedForkedSync64Full(t *testing.T) { testBoundedForkedSync(t, 64, FullSync) } 607 func TestBoundedForkedSync64Fast(t *testing.T) { testBoundedForkedSync(t, 64, FastSync) } 608 func TestBoundedForkedSync64Light(t *testing.T) { testBoundedForkedSync(t, 64, LightSync) } 609 610 func testBoundedForkedSync(t *testing.T, protocol int, mode SyncMode) { 611 t.Parallel() 612 613 tester := newTester() 614 defer tester.terminate() 615 616 chainA := testChainForkLightA 617 chainB := testChainForkLightB 618 tester.newPeer("original", protocol, chainA) 619 tester.newPeer("rewriter", protocol, chainB) 620 621 // Synchronise with the peer and make sure all blocks were retrieved 622 if err := tester.sync("original", nil, mode); err != nil { 623 t.Fatalf("failed to synchronise blocks: %v", err) 624 } 625 assertOwnChain(t, tester, chainA.len()) 626 627 // Synchronise with the second peer and ensure that the fork is rejected to being too old 628 if err := tester.sync("rewriter", nil, mode); err != errInvalidAncestor { 629 t.Fatalf("sync failure mismatch: have %v, want %v", err, errInvalidAncestor) 630 } 631 } 632 633 // Tests that chain forks are contained within a certain interval of the current 634 // chain head for short but heavy forks too. These are a bit special because they 635 // take different ancestor lookup paths. 636 func TestBoundedHeavyForkedSync62(t *testing.T) { testBoundedHeavyForkedSync(t, 62, FullSync) } 637 func TestBoundedHeavyForkedSync63Full(t *testing.T) { testBoundedHeavyForkedSync(t, 63, FullSync) } 638 func TestBoundedHeavyForkedSync63Fast(t *testing.T) { testBoundedHeavyForkedSync(t, 63, FastSync) } 639 func TestBoundedHeavyForkedSync64Full(t *testing.T) { testBoundedHeavyForkedSync(t, 64, FullSync) } 640 func TestBoundedHeavyForkedSync64Fast(t *testing.T) { testBoundedHeavyForkedSync(t, 64, FastSync) } 641 func TestBoundedHeavyForkedSync64Light(t *testing.T) { testBoundedHeavyForkedSync(t, 64, LightSync) } 642 643 func testBoundedHeavyForkedSync(t *testing.T, protocol int, mode SyncMode) { 644 t.Parallel() 645 646 tester := newTester() 647 defer tester.terminate() 648 649 // Create a long enough forked chain 650 chainA := testChainForkLightA 651 chainB := testChainForkHeavy 652 tester.newPeer("original", protocol, chainA) 653 tester.newPeer("heavy-rewriter", protocol, chainB) 654 655 // Synchronise with the peer and make sure all blocks were retrieved 656 if err := tester.sync("original", nil, mode); err != nil { 657 t.Fatalf("failed to synchronise blocks: %v", err) 658 } 659 assertOwnChain(t, tester, chainA.len()) 660 661 // Synchronise with the second peer and ensure that the fork is rejected to being too old 662 if err := tester.sync("heavy-rewriter", nil, mode); err != errInvalidAncestor { 663 t.Fatalf("sync failure mismatch: have %v, want %v", err, errInvalidAncestor) 664 } 665 } 666 667 // Tests that an inactive downloader will not accept incoming block headers and 668 // bodies. 669 func TestInactiveDownloader62(t *testing.T) { 670 t.Parallel() 671 672 tester := newTester() 673 defer tester.terminate() 674 675 // Check that neither block headers nor bodies are accepted 676 if err := tester.downloader.DeliverHeaders("bad peer", []*types.Header{}); err != errNoSyncActive { 677 t.Errorf("error mismatch: have %v, want %v", err, errNoSyncActive) 678 } 679 if err := tester.downloader.DeliverBodies("bad peer", [][]*types.Transaction{}, [][]*types.Header{}); err != errNoSyncActive { 680 t.Errorf("error mismatch: have %v, want %v", err, errNoSyncActive) 681 } 682 } 683 684 // Tests that an inactive downloader will not accept incoming block headers, 685 // bodies and receipts. 686 func TestInactiveDownloader63(t *testing.T) { 687 t.Parallel() 688 689 tester := newTester() 690 defer tester.terminate() 691 692 // Check that neither block headers nor bodies are accepted 693 if err := tester.downloader.DeliverHeaders("bad peer", []*types.Header{}); err != errNoSyncActive { 694 t.Errorf("error mismatch: have %v, want %v", err, errNoSyncActive) 695 } 696 if err := tester.downloader.DeliverBodies("bad peer", [][]*types.Transaction{}, [][]*types.Header{}); err != errNoSyncActive { 697 t.Errorf("error mismatch: have %v, want %v", err, errNoSyncActive) 698 } 699 if err := tester.downloader.DeliverReceipts("bad peer", [][]*types.Receipt{}); err != errNoSyncActive { 700 t.Errorf("error mismatch: have %v, want %v", err, errNoSyncActive) 701 } 702 } 703 704 // Tests that a canceled download wipes all previously accumulated state. 705 func TestCancel62(t *testing.T) { testCancel(t, 62, FullSync) } 706 func TestCancel63Full(t *testing.T) { testCancel(t, 63, FullSync) } 707 func TestCancel63Fast(t *testing.T) { testCancel(t, 63, FastSync) } 708 func TestCancel64Full(t *testing.T) { testCancel(t, 64, FullSync) } 709 func TestCancel64Fast(t *testing.T) { testCancel(t, 64, FastSync) } 710 func TestCancel64Light(t *testing.T) { testCancel(t, 64, LightSync) } 711 712 func testCancel(t *testing.T, protocol int, mode SyncMode) { 713 t.Parallel() 714 715 tester := newTester() 716 defer tester.terminate() 717 718 chain := testChainBase.shorten(MaxHeaderFetch) 719 tester.newPeer("peer", protocol, chain) 720 721 // Make sure canceling works with a pristine downloader 722 tester.downloader.Cancel() 723 if !tester.downloader.queue.Idle() { 724 t.Errorf("download queue not idle") 725 } 726 // Synchronise with the peer, but cancel afterwards 727 if err := tester.sync("peer", nil, mode); err != nil { 728 t.Fatalf("failed to synchronise blocks: %v", err) 729 } 730 tester.downloader.Cancel() 731 if !tester.downloader.queue.Idle() { 732 t.Errorf("download queue not idle") 733 } 734 } 735 736 // Tests that synchronisation from multiple peers works as intended (multi thread sanity test). 737 func TestMultiSynchronisation62(t *testing.T) { testMultiSynchronisation(t, 62, FullSync) } 738 func TestMultiSynchronisation63Full(t *testing.T) { testMultiSynchronisation(t, 63, FullSync) } 739 func TestMultiSynchronisation63Fast(t *testing.T) { testMultiSynchronisation(t, 63, FastSync) } 740 func TestMultiSynchronisation64Full(t *testing.T) { testMultiSynchronisation(t, 64, FullSync) } 741 func TestMultiSynchronisation64Fast(t *testing.T) { testMultiSynchronisation(t, 64, FastSync) } 742 func TestMultiSynchronisation64Light(t *testing.T) { testMultiSynchronisation(t, 64, LightSync) } 743 744 func testMultiSynchronisation(t *testing.T, protocol int, mode SyncMode) { 745 t.Parallel() 746 747 tester := newTester() 748 defer tester.terminate() 749 750 // Create various peers with various parts of the chain 751 targetPeers := 8 752 chain := testChainBase.shorten(targetPeers * 100) 753 754 for i := 0; i < targetPeers; i++ { 755 id := fmt.Sprintf("peer #%d", i) 756 tester.newPeer(id, protocol, chain.shorten(chain.len()/(i+1))) 757 } 758 if err := tester.sync("peer #0", nil, mode); err != nil { 759 t.Fatalf("failed to synchronise blocks: %v", err) 760 } 761 assertOwnChain(t, tester, chain.len()) 762 } 763 764 // Tests that synchronisations behave well in multi-version protocol environments 765 // and not wreak havoc on other nodes in the network. 766 func TestMultiProtoSynchronisation62(t *testing.T) { testMultiProtoSync(t, 62, FullSync) } 767 func TestMultiProtoSynchronisation63Full(t *testing.T) { testMultiProtoSync(t, 63, FullSync) } 768 func TestMultiProtoSynchronisation63Fast(t *testing.T) { testMultiProtoSync(t, 63, FastSync) } 769 func TestMultiProtoSynchronisation64Full(t *testing.T) { testMultiProtoSync(t, 64, FullSync) } 770 func TestMultiProtoSynchronisation64Fast(t *testing.T) { testMultiProtoSync(t, 64, FastSync) } 771 func TestMultiProtoSynchronisation64Light(t *testing.T) { testMultiProtoSync(t, 64, LightSync) } 772 773 func testMultiProtoSync(t *testing.T, protocol int, mode SyncMode) { 774 t.Parallel() 775 776 tester := newTester() 777 defer tester.terminate() 778 779 // Create a small enough block chain to download 780 chain := testChainBase.shorten(blockCacheItems - 15) 781 782 // Create peers of every type 783 tester.newPeer("peer 62", 62, chain) 784 tester.newPeer("peer 63", 63, chain) 785 tester.newPeer("peer 64", 64, chain) 786 787 // Synchronise with the requested peer and make sure all blocks were retrieved 788 if err := tester.sync(fmt.Sprintf("peer %d", protocol), nil, mode); err != nil { 789 t.Fatalf("failed to synchronise blocks: %v", err) 790 } 791 assertOwnChain(t, tester, chain.len()) 792 793 // Check that no peers have been dropped off 794 for _, version := range []int{62, 63, 64} { 795 peer := fmt.Sprintf("peer %d", version) 796 if _, ok := tester.peers[peer]; !ok { 797 t.Errorf("%s dropped", peer) 798 } 799 } 800 } 801 802 // Tests that if a block is empty (e.g. header only), no body request should be 803 // made, and instead the header should be assembled into a whole block in itself. 804 func TestEmptyShortCircuit62(t *testing.T) { testEmptyShortCircuit(t, 62, FullSync) } 805 func TestEmptyShortCircuit63Full(t *testing.T) { testEmptyShortCircuit(t, 63, FullSync) } 806 func TestEmptyShortCircuit63Fast(t *testing.T) { testEmptyShortCircuit(t, 63, FastSync) } 807 func TestEmptyShortCircuit64Full(t *testing.T) { testEmptyShortCircuit(t, 64, FullSync) } 808 func TestEmptyShortCircuit64Fast(t *testing.T) { testEmptyShortCircuit(t, 64, FastSync) } 809 func TestEmptyShortCircuit64Light(t *testing.T) { testEmptyShortCircuit(t, 64, LightSync) } 810 811 func testEmptyShortCircuit(t *testing.T, protocol int, mode SyncMode) { 812 t.Parallel() 813 814 tester := newTester() 815 defer tester.terminate() 816 817 // Create a block chain to download 818 chain := testChainBase 819 tester.newPeer("peer", protocol, chain) 820 821 // Instrument the downloader to signal body requests 822 bodiesHave, receiptsHave := int32(0), int32(0) 823 tester.downloader.bodyFetchHook = func(headers []*types.Header) { 824 atomic.AddInt32(&bodiesHave, int32(len(headers))) 825 } 826 tester.downloader.receiptFetchHook = func(headers []*types.Header) { 827 atomic.AddInt32(&receiptsHave, int32(len(headers))) 828 } 829 // Synchronise with the peer and make sure all blocks were retrieved 830 if err := tester.sync("peer", nil, mode); err != nil { 831 t.Fatalf("failed to synchronise blocks: %v", err) 832 } 833 assertOwnChain(t, tester, chain.len()) 834 835 // Validate the number of block bodies that should have been requested 836 bodiesNeeded, receiptsNeeded := 0, 0 837 for _, block := range chain.blockm { 838 if mode != LightSync && block != tester.genesis && (len(block.Transactions()) > 0 || len(block.Uncles()) > 0) { 839 bodiesNeeded++ 840 } 841 } 842 for _, receipt := range chain.receiptm { 843 if mode == FastSync && len(receipt) > 0 { 844 receiptsNeeded++ 845 } 846 } 847 if int(bodiesHave) != bodiesNeeded { 848 t.Errorf("body retrieval count mismatch: have %v, want %v", bodiesHave, bodiesNeeded) 849 } 850 if int(receiptsHave) != receiptsNeeded { 851 t.Errorf("receipt retrieval count mismatch: have %v, want %v", receiptsHave, receiptsNeeded) 852 } 853 } 854 855 // Tests that headers are enqueued continuously, preventing malicious nodes from 856 // stalling the downloader by feeding gapped header chains. 857 func TestMissingHeaderAttack62(t *testing.T) { testMissingHeaderAttack(t, 62, FullSync) } 858 func TestMissingHeaderAttack63Full(t *testing.T) { testMissingHeaderAttack(t, 63, FullSync) } 859 func TestMissingHeaderAttack63Fast(t *testing.T) { testMissingHeaderAttack(t, 63, FastSync) } 860 func TestMissingHeaderAttack64Full(t *testing.T) { testMissingHeaderAttack(t, 64, FullSync) } 861 func TestMissingHeaderAttack64Fast(t *testing.T) { testMissingHeaderAttack(t, 64, FastSync) } 862 func TestMissingHeaderAttack64Light(t *testing.T) { testMissingHeaderAttack(t, 64, LightSync) } 863 864 func testMissingHeaderAttack(t *testing.T, protocol int, mode SyncMode) { 865 t.Parallel() 866 867 tester := newTester() 868 defer tester.terminate() 869 870 chain := testChainBase.shorten(blockCacheItems - 15) 871 brokenChain := chain.shorten(chain.len()) 872 delete(brokenChain.headerm, brokenChain.chain[brokenChain.len()/2]) 873 tester.newPeer("attack", protocol, brokenChain) 874 875 if err := tester.sync("attack", nil, mode); err == nil { 876 t.Fatalf("succeeded attacker synchronisation") 877 } 878 // Synchronise with the valid peer and make sure sync succeeds 879 tester.newPeer("valid", protocol, chain) 880 if err := tester.sync("valid", nil, mode); err != nil { 881 t.Fatalf("failed to synchronise blocks: %v", err) 882 } 883 assertOwnChain(t, tester, chain.len()) 884 } 885 886 // Tests that if requested headers are shifted (i.e. first is missing), the queue 887 // detects the invalid numbering. 888 func TestShiftedHeaderAttack62(t *testing.T) { testShiftedHeaderAttack(t, 62, FullSync) } 889 func TestShiftedHeaderAttack63Full(t *testing.T) { testShiftedHeaderAttack(t, 63, FullSync) } 890 func TestShiftedHeaderAttack63Fast(t *testing.T) { testShiftedHeaderAttack(t, 63, FastSync) } 891 func TestShiftedHeaderAttack64Full(t *testing.T) { testShiftedHeaderAttack(t, 64, FullSync) } 892 func TestShiftedHeaderAttack64Fast(t *testing.T) { testShiftedHeaderAttack(t, 64, FastSync) } 893 func TestShiftedHeaderAttack64Light(t *testing.T) { testShiftedHeaderAttack(t, 64, LightSync) } 894 895 func testShiftedHeaderAttack(t *testing.T, protocol int, mode SyncMode) { 896 t.Parallel() 897 898 tester := newTester() 899 defer tester.terminate() 900 901 chain := testChainBase.shorten(blockCacheItems - 15) 902 903 // Attempt a full sync with an attacker feeding shifted headers 904 brokenChain := chain.shorten(chain.len()) 905 delete(brokenChain.headerm, brokenChain.chain[1]) 906 delete(brokenChain.blockm, brokenChain.chain[1]) 907 delete(brokenChain.receiptm, brokenChain.chain[1]) 908 tester.newPeer("attack", protocol, brokenChain) 909 if err := tester.sync("attack", nil, mode); err == nil { 910 t.Fatalf("succeeded attacker synchronisation") 911 } 912 913 // Synchronise with the valid peer and make sure sync succeeds 914 tester.newPeer("valid", protocol, chain) 915 if err := tester.sync("valid", nil, mode); err != nil { 916 t.Fatalf("failed to synchronise blocks: %v", err) 917 } 918 assertOwnChain(t, tester, chain.len()) 919 } 920 921 // Tests that upon detecting an invalid header, the recent ones are rolled back 922 // for various failure scenarios. Afterwards a full sync is attempted to make 923 // sure no state was corrupted. 924 func TestInvalidHeaderRollback63Fast(t *testing.T) { testInvalidHeaderRollback(t, 63, FastSync) } 925 func TestInvalidHeaderRollback64Fast(t *testing.T) { testInvalidHeaderRollback(t, 64, FastSync) } 926 func TestInvalidHeaderRollback64Light(t *testing.T) { testInvalidHeaderRollback(t, 64, LightSync) } 927 928 func testInvalidHeaderRollback(t *testing.T, protocol int, mode SyncMode) { 929 t.Parallel() 930 931 tester := newTester() 932 defer tester.terminate() 933 934 // Create a small enough block chain to download 935 targetBlocks := 3*fsHeaderSafetyNet + 256 + fsMinFullBlocks 936 chain := testChainBase.shorten(targetBlocks) 937 938 // Attempt to sync with an attacker that feeds junk during the fast sync phase. 939 // This should result in the last fsHeaderSafetyNet headers being rolled back. 940 missing := fsHeaderSafetyNet + MaxHeaderFetch + 1 941 fastAttackChain := chain.shorten(chain.len()) 942 delete(fastAttackChain.headerm, fastAttackChain.chain[missing]) 943 tester.newPeer("fast-attack", protocol, fastAttackChain) 944 945 if err := tester.sync("fast-attack", nil, mode); err == nil { 946 t.Fatalf("succeeded fast attacker synchronisation") 947 } 948 if head := tester.CurrentHeader().Number.Int64(); int(head) > MaxHeaderFetch { 949 t.Errorf("rollback head mismatch: have %v, want at most %v", head, MaxHeaderFetch) 950 } 951 952 // Attempt to sync with an attacker that feeds junk during the block import phase. 953 // This should result in both the last fsHeaderSafetyNet number of headers being 954 // rolled back, and also the pivot point being reverted to a non-block status. 955 missing = 3*fsHeaderSafetyNet + MaxHeaderFetch + 1 956 blockAttackChain := chain.shorten(chain.len()) 957 delete(fastAttackChain.headerm, fastAttackChain.chain[missing]) // Make sure the fast-attacker doesn't fill in 958 delete(blockAttackChain.headerm, blockAttackChain.chain[missing]) 959 tester.newPeer("block-attack", protocol, blockAttackChain) 960 961 if err := tester.sync("block-attack", nil, mode); err == nil { 962 t.Fatalf("succeeded block attacker synchronisation") 963 } 964 if head := tester.CurrentHeader().Number.Int64(); int(head) > 2*fsHeaderSafetyNet+MaxHeaderFetch { 965 t.Errorf("rollback head mismatch: have %v, want at most %v", head, 2*fsHeaderSafetyNet+MaxHeaderFetch) 966 } 967 if mode == FastSync { 968 if head := tester.CurrentBlock().NumberU64(); head != 0 { 969 t.Errorf("fast sync pivot block #%d not rolled back", head) 970 } 971 } 972 973 // Attempt to sync with an attacker that withholds promised blocks after the 974 // fast sync pivot point. This could be a trial to leave the node with a bad 975 // but already imported pivot block. 976 withholdAttackChain := chain.shorten(chain.len()) 977 tester.newPeer("withhold-attack", protocol, withholdAttackChain) 978 tester.downloader.syncInitHook = func(uint64, uint64) { 979 for i := missing; i < withholdAttackChain.len(); i++ { 980 delete(withholdAttackChain.headerm, withholdAttackChain.chain[i]) 981 } 982 tester.downloader.syncInitHook = nil 983 } 984 if err := tester.sync("withhold-attack", nil, mode); err == nil { 985 t.Fatalf("succeeded withholding attacker synchronisation") 986 } 987 if head := tester.CurrentHeader().Number.Int64(); int(head) > 2*fsHeaderSafetyNet+MaxHeaderFetch { 988 t.Errorf("rollback head mismatch: have %v, want at most %v", head, 2*fsHeaderSafetyNet+MaxHeaderFetch) 989 } 990 if mode == FastSync { 991 if head := tester.CurrentBlock().NumberU64(); head != 0 { 992 t.Errorf("fast sync pivot block #%d not rolled back", head) 993 } 994 } 995 996 // synchronise with the valid peer and make sure sync succeeds. Since the last rollback 997 // should also disable fast syncing for this process, verify that we did a fresh full 998 // sync. Note, we can't assert anything about the receipts since we won't purge the 999 // database of them, hence we can't use assertOwnChain. 1000 tester.newPeer("valid", protocol, chain) 1001 if err := tester.sync("valid", nil, mode); err != nil { 1002 t.Fatalf("failed to synchronise blocks: %v", err) 1003 } 1004 if hs := len(tester.ownHeaders); hs != chain.len() { 1005 t.Fatalf("synchronised headers mismatch: have %v, want %v", hs, chain.len()) 1006 } 1007 if mode != LightSync { 1008 if bs := len(tester.ownBlocks); bs != chain.len() { 1009 t.Fatalf("synchronised blocks mismatch: have %v, want %v", bs, chain.len()) 1010 } 1011 } 1012 } 1013 1014 // Tests that a peer advertising an high TD doesn't get to stall the downloader 1015 // afterwards by not sending any useful hashes. 1016 func TestHighTDStarvationAttack62(t *testing.T) { testHighTDStarvationAttack(t, 62, FullSync) } 1017 func TestHighTDStarvationAttack63Full(t *testing.T) { testHighTDStarvationAttack(t, 63, FullSync) } 1018 func TestHighTDStarvationAttack63Fast(t *testing.T) { testHighTDStarvationAttack(t, 63, FastSync) } 1019 func TestHighTDStarvationAttack64Full(t *testing.T) { testHighTDStarvationAttack(t, 64, FullSync) } 1020 func TestHighTDStarvationAttack64Fast(t *testing.T) { testHighTDStarvationAttack(t, 64, FastSync) } 1021 func TestHighTDStarvationAttack64Light(t *testing.T) { testHighTDStarvationAttack(t, 64, LightSync) } 1022 1023 func testHighTDStarvationAttack(t *testing.T, protocol int, mode SyncMode) { 1024 t.Parallel() 1025 1026 tester := newTester() 1027 defer tester.terminate() 1028 1029 chain := testChainBase.shorten(1) 1030 tester.newPeer("attack", protocol, chain) 1031 if err := tester.sync("attack", big.NewInt(1000000), mode); err != errStallingPeer { 1032 t.Fatalf("synchronisation error mismatch: have %v, want %v", err, errStallingPeer) 1033 } 1034 } 1035 1036 // Tests that misbehaving peers are disconnected, whilst behaving ones are not. 1037 func TestBlockHeaderAttackerDropping62(t *testing.T) { testBlockHeaderAttackerDropping(t, 62) } 1038 func TestBlockHeaderAttackerDropping63(t *testing.T) { testBlockHeaderAttackerDropping(t, 63) } 1039 func TestBlockHeaderAttackerDropping64(t *testing.T) { testBlockHeaderAttackerDropping(t, 64) } 1040 1041 func testBlockHeaderAttackerDropping(t *testing.T, protocol int) { 1042 t.Parallel() 1043 1044 // Define the disconnection requirement for individual hash fetch errors 1045 tests := []struct { 1046 result error 1047 drop bool 1048 }{ 1049 {nil, false}, // Sync succeeded, all is well 1050 {errBusy, false}, // Sync is already in progress, no problem 1051 {errUnknownPeer, false}, // Peer is unknown, was already dropped, don't double drop 1052 {errBadPeer, true}, // Peer was deemed bad for some reason, drop it 1053 {errStallingPeer, true}, // Peer was detected to be stalling, drop it 1054 {errNoPeers, false}, // No peers to download from, soft race, no issue 1055 {errTimeout, true}, // No hashes received in due time, drop the peer 1056 {errEmptyHeaderSet, true}, // No headers were returned as a response, drop as it's a dead end 1057 {errPeersUnavailable, true}, // Nobody had the advertised blocks, drop the advertiser 1058 {errInvalidAncestor, true}, // Agreed upon ancestor is not acceptable, drop the chain rewriter 1059 {errInvalidChain, true}, // Hash chain was detected as invalid, definitely drop 1060 {errInvalidBlock, false}, // A bad peer was detected, but not the sync origin 1061 {errInvalidBody, false}, // A bad peer was detected, but not the sync origin 1062 {errInvalidReceipt, false}, // A bad peer was detected, but not the sync origin 1063 {errCancelBlockFetch, false}, // Synchronisation was canceled, origin may be innocent, don't drop 1064 {errCancelHeaderFetch, false}, // Synchronisation was canceled, origin may be innocent, don't drop 1065 {errCancelBodyFetch, false}, // Synchronisation was canceled, origin may be innocent, don't drop 1066 {errCancelReceiptFetch, false}, // Synchronisation was canceled, origin may be innocent, don't drop 1067 {errCancelHeaderProcessing, false}, // Synchronisation was canceled, origin may be innocent, don't drop 1068 {errCancelContentProcessing, false}, // Synchronisation was canceled, origin may be innocent, don't drop 1069 } 1070 // Run the tests and check disconnection status 1071 tester := newTester() 1072 defer tester.terminate() 1073 chain := testChainBase.shorten(1) 1074 1075 for i, tt := range tests { 1076 // Register a new peer and ensure it's presence 1077 id := fmt.Sprintf("test %d", i) 1078 if err := tester.newPeer(id, protocol, chain); err != nil { 1079 t.Fatalf("test %d: failed to register new peer: %v", i, err) 1080 } 1081 if _, ok := tester.peers[id]; !ok { 1082 t.Fatalf("test %d: registered peer not found", i) 1083 } 1084 // Simulate a synchronisation and check the required result 1085 tester.downloader.synchroniseMock = func(string, common.Hash) error { return tt.result } 1086 1087 tester.downloader.Synchronise(id, tester.genesis.Hash(), big.NewInt(1000), FullSync) 1088 if _, ok := tester.peers[id]; !ok != tt.drop { 1089 t.Errorf("test %d: peer drop mismatch for %v: have %v, want %v", i, tt.result, !ok, tt.drop) 1090 } 1091 } 1092 } 1093 1094 // Tests that synchronisation progress (origin block number, current block number 1095 // and highest block number) is tracked and updated correctly. 1096 func TestSyncProgress62(t *testing.T) { testSyncProgress(t, 62, FullSync) } 1097 func TestSyncProgress63Full(t *testing.T) { testSyncProgress(t, 63, FullSync) } 1098 func TestSyncProgress63Fast(t *testing.T) { testSyncProgress(t, 63, FastSync) } 1099 func TestSyncProgress64Full(t *testing.T) { testSyncProgress(t, 64, FullSync) } 1100 func TestSyncProgress64Fast(t *testing.T) { testSyncProgress(t, 64, FastSync) } 1101 func TestSyncProgress64Light(t *testing.T) { testSyncProgress(t, 64, LightSync) } 1102 1103 func testSyncProgress(t *testing.T, protocol int, mode SyncMode) { 1104 t.Parallel() 1105 1106 tester := newTester() 1107 defer tester.terminate() 1108 chain := testChainBase.shorten(blockCacheItems - 15) 1109 1110 // Set a sync init hook to catch progress changes 1111 starting := make(chan struct{}) 1112 progress := make(chan struct{}) 1113 1114 tester.downloader.syncInitHook = func(origin, latest uint64) { 1115 starting <- struct{}{} 1116 <-progress 1117 } 1118 checkProgress(t, tester.downloader, "pristine", ethereum.SyncProgress{}) 1119 1120 // Synchronise half the blocks and check initial progress 1121 tester.newPeer("peer-half", protocol, chain.shorten(chain.len()/2)) 1122 pending := new(sync.WaitGroup) 1123 pending.Add(1) 1124 1125 go func() { 1126 defer pending.Done() 1127 if err := tester.sync("peer-half", nil, mode); err != nil { 1128 panic(fmt.Sprintf("failed to synchronise blocks: %v", err)) 1129 } 1130 }() 1131 <-starting 1132 checkProgress(t, tester.downloader, "initial", ethereum.SyncProgress{ 1133 HighestBlock: uint64(chain.len()/2 - 1), 1134 }) 1135 progress <- struct{}{} 1136 pending.Wait() 1137 1138 // Synchronise all the blocks and check continuation progress 1139 tester.newPeer("peer-full", protocol, chain) 1140 pending.Add(1) 1141 go func() { 1142 defer pending.Done() 1143 if err := tester.sync("peer-full", nil, mode); err != nil { 1144 panic(fmt.Sprintf("failed to synchronise blocks: %v", err)) 1145 } 1146 }() 1147 <-starting 1148 checkProgress(t, tester.downloader, "completing", ethereum.SyncProgress{ 1149 StartingBlock: uint64(chain.len()/2 - 1), 1150 CurrentBlock: uint64(chain.len()/2 - 1), 1151 HighestBlock: uint64(chain.len() - 1), 1152 }) 1153 1154 // Check final progress after successful sync 1155 progress <- struct{}{} 1156 pending.Wait() 1157 checkProgress(t, tester.downloader, "final", ethereum.SyncProgress{ 1158 StartingBlock: uint64(chain.len()/2 - 1), 1159 CurrentBlock: uint64(chain.len() - 1), 1160 HighestBlock: uint64(chain.len() - 1), 1161 }) 1162 } 1163 1164 func checkProgress(t *testing.T, d *Downloader, stage string, want ethereum.SyncProgress) { 1165 // Mark this method as a helper to report errors at callsite, not in here 1166 t.Helper() 1167 1168 p := d.Progress() 1169 p.KnownStates, p.PulledStates = 0, 0 1170 want.KnownStates, want.PulledStates = 0, 0 1171 if p != want { 1172 t.Fatalf("%s progress mismatch:\nhave %+v\nwant %+v", stage, p, want) 1173 } 1174 } 1175 1176 // Tests that synchronisation progress (origin block number and highest block 1177 // number) is tracked and updated correctly in case of a fork (or manual head 1178 // revertal). 1179 func TestForkedSyncProgress62(t *testing.T) { testForkedSyncProgress(t, 62, FullSync) } 1180 func TestForkedSyncProgress63Full(t *testing.T) { testForkedSyncProgress(t, 63, FullSync) } 1181 func TestForkedSyncProgress63Fast(t *testing.T) { testForkedSyncProgress(t, 63, FastSync) } 1182 func TestForkedSyncProgress64Full(t *testing.T) { testForkedSyncProgress(t, 64, FullSync) } 1183 func TestForkedSyncProgress64Fast(t *testing.T) { testForkedSyncProgress(t, 64, FastSync) } 1184 func TestForkedSyncProgress64Light(t *testing.T) { testForkedSyncProgress(t, 64, LightSync) } 1185 1186 func testForkedSyncProgress(t *testing.T, protocol int, mode SyncMode) { 1187 t.Parallel() 1188 1189 tester := newTester() 1190 defer tester.terminate() 1191 chainA := testChainForkLightA.shorten(testChainBase.len() + MaxHashFetch) 1192 chainB := testChainForkLightB.shorten(testChainBase.len() + MaxHashFetch) 1193 1194 // Set a sync init hook to catch progress changes 1195 starting := make(chan struct{}) 1196 progress := make(chan struct{}) 1197 1198 tester.downloader.syncInitHook = func(origin, latest uint64) { 1199 starting <- struct{}{} 1200 <-progress 1201 } 1202 checkProgress(t, tester.downloader, "pristine", ethereum.SyncProgress{}) 1203 1204 // Synchronise with one of the forks and check progress 1205 tester.newPeer("fork A", protocol, chainA) 1206 pending := new(sync.WaitGroup) 1207 pending.Add(1) 1208 go func() { 1209 defer pending.Done() 1210 if err := tester.sync("fork A", nil, mode); err != nil { 1211 panic(fmt.Sprintf("failed to synchronise blocks: %v", err)) 1212 } 1213 }() 1214 <-starting 1215 1216 checkProgress(t, tester.downloader, "initial", ethereum.SyncProgress{ 1217 HighestBlock: uint64(chainA.len() - 1), 1218 }) 1219 progress <- struct{}{} 1220 pending.Wait() 1221 1222 // Simulate a successful sync above the fork 1223 tester.downloader.syncStatsChainOrigin = tester.downloader.syncStatsChainHeight 1224 1225 // Synchronise with the second fork and check progress resets 1226 tester.newPeer("fork B", protocol, chainB) 1227 pending.Add(1) 1228 go func() { 1229 defer pending.Done() 1230 if err := tester.sync("fork B", nil, mode); err != nil { 1231 panic(fmt.Sprintf("failed to synchronise blocks: %v", err)) 1232 } 1233 }() 1234 <-starting 1235 checkProgress(t, tester.downloader, "forking", ethereum.SyncProgress{ 1236 StartingBlock: uint64(testChainBase.len()) - 1, 1237 CurrentBlock: uint64(chainA.len() - 1), 1238 HighestBlock: uint64(chainB.len() - 1), 1239 }) 1240 1241 // Check final progress after successful sync 1242 progress <- struct{}{} 1243 pending.Wait() 1244 checkProgress(t, tester.downloader, "final", ethereum.SyncProgress{ 1245 StartingBlock: uint64(testChainBase.len()) - 1, 1246 CurrentBlock: uint64(chainB.len() - 1), 1247 HighestBlock: uint64(chainB.len() - 1), 1248 }) 1249 } 1250 1251 // Tests that if synchronisation is aborted due to some failure, then the progress 1252 // origin is not updated in the next sync cycle, as it should be considered the 1253 // continuation of the previous sync and not a new instance. 1254 func TestFailedSyncProgress62(t *testing.T) { testFailedSyncProgress(t, 62, FullSync) } 1255 func TestFailedSyncProgress63Full(t *testing.T) { testFailedSyncProgress(t, 63, FullSync) } 1256 func TestFailedSyncProgress63Fast(t *testing.T) { testFailedSyncProgress(t, 63, FastSync) } 1257 func TestFailedSyncProgress64Full(t *testing.T) { testFailedSyncProgress(t, 64, FullSync) } 1258 func TestFailedSyncProgress64Fast(t *testing.T) { testFailedSyncProgress(t, 64, FastSync) } 1259 func TestFailedSyncProgress64Light(t *testing.T) { testFailedSyncProgress(t, 64, LightSync) } 1260 1261 func testFailedSyncProgress(t *testing.T, protocol int, mode SyncMode) { 1262 t.Parallel() 1263 1264 tester := newTester() 1265 defer tester.terminate() 1266 chain := testChainBase.shorten(blockCacheItems - 15) 1267 1268 // Set a sync init hook to catch progress changes 1269 starting := make(chan struct{}) 1270 progress := make(chan struct{}) 1271 1272 tester.downloader.syncInitHook = func(origin, latest uint64) { 1273 starting <- struct{}{} 1274 <-progress 1275 } 1276 checkProgress(t, tester.downloader, "pristine", ethereum.SyncProgress{}) 1277 1278 // Attempt a full sync with a faulty peer 1279 brokenChain := chain.shorten(chain.len()) 1280 missing := brokenChain.len() / 2 1281 delete(brokenChain.headerm, brokenChain.chain[missing]) 1282 delete(brokenChain.blockm, brokenChain.chain[missing]) 1283 delete(brokenChain.receiptm, brokenChain.chain[missing]) 1284 tester.newPeer("faulty", protocol, brokenChain) 1285 1286 pending := new(sync.WaitGroup) 1287 pending.Add(1) 1288 go func() { 1289 defer pending.Done() 1290 if err := tester.sync("faulty", nil, mode); err == nil { 1291 panic("succeeded faulty synchronisation") 1292 } 1293 }() 1294 <-starting 1295 checkProgress(t, tester.downloader, "initial", ethereum.SyncProgress{ 1296 HighestBlock: uint64(brokenChain.len() - 1), 1297 }) 1298 progress <- struct{}{} 1299 pending.Wait() 1300 afterFailedSync := tester.downloader.Progress() 1301 1302 // Synchronise with a good peer and check that the progress origin remind the same 1303 // after a failure 1304 tester.newPeer("valid", protocol, chain) 1305 pending.Add(1) 1306 go func() { 1307 defer pending.Done() 1308 if err := tester.sync("valid", nil, mode); err != nil { 1309 panic(fmt.Sprintf("failed to synchronise blocks: %v", err)) 1310 } 1311 }() 1312 <-starting 1313 checkProgress(t, tester.downloader, "completing", afterFailedSync) 1314 1315 // Check final progress after successful sync 1316 progress <- struct{}{} 1317 pending.Wait() 1318 checkProgress(t, tester.downloader, "final", ethereum.SyncProgress{ 1319 CurrentBlock: uint64(chain.len() - 1), 1320 HighestBlock: uint64(chain.len() - 1), 1321 }) 1322 } 1323 1324 // Tests that if an attacker fakes a chain height, after the attack is detected, 1325 // the progress height is successfully reduced at the next sync invocation. 1326 func TestFakedSyncProgress62(t *testing.T) { testFakedSyncProgress(t, 62, FullSync) } 1327 func TestFakedSyncProgress63Full(t *testing.T) { testFakedSyncProgress(t, 63, FullSync) } 1328 func TestFakedSyncProgress63Fast(t *testing.T) { testFakedSyncProgress(t, 63, FastSync) } 1329 func TestFakedSyncProgress64Full(t *testing.T) { testFakedSyncProgress(t, 64, FullSync) } 1330 func TestFakedSyncProgress64Fast(t *testing.T) { testFakedSyncProgress(t, 64, FastSync) } 1331 func TestFakedSyncProgress64Light(t *testing.T) { testFakedSyncProgress(t, 64, LightSync) } 1332 1333 func testFakedSyncProgress(t *testing.T, protocol int, mode SyncMode) { 1334 t.Parallel() 1335 1336 tester := newTester() 1337 defer tester.terminate() 1338 chain := testChainBase.shorten(blockCacheItems - 15) 1339 1340 // Set a sync init hook to catch progress changes 1341 starting := make(chan struct{}) 1342 progress := make(chan struct{}) 1343 tester.downloader.syncInitHook = func(origin, latest uint64) { 1344 starting <- struct{}{} 1345 <-progress 1346 } 1347 checkProgress(t, tester.downloader, "pristine", ethereum.SyncProgress{}) 1348 1349 // Create and sync with an attacker that promises a higher chain than available. 1350 brokenChain := chain.shorten(chain.len()) 1351 numMissing := 5 1352 for i := brokenChain.len() - 2; i > brokenChain.len()-numMissing; i-- { 1353 delete(brokenChain.headerm, brokenChain.chain[i]) 1354 } 1355 tester.newPeer("attack", protocol, brokenChain) 1356 1357 pending := new(sync.WaitGroup) 1358 pending.Add(1) 1359 go func() { 1360 defer pending.Done() 1361 if err := tester.sync("attack", nil, mode); err == nil { 1362 panic("succeeded attacker synchronisation") 1363 } 1364 }() 1365 <-starting 1366 checkProgress(t, tester.downloader, "initial", ethereum.SyncProgress{ 1367 HighestBlock: uint64(brokenChain.len() - 1), 1368 }) 1369 progress <- struct{}{} 1370 pending.Wait() 1371 afterFailedSync := tester.downloader.Progress() 1372 1373 // Synchronise with a good peer and check that the progress height has been reduced to 1374 // the true value. 1375 validChain := chain.shorten(chain.len() - numMissing) 1376 tester.newPeer("valid", protocol, validChain) 1377 pending.Add(1) 1378 1379 go func() { 1380 defer pending.Done() 1381 if err := tester.sync("valid", nil, mode); err != nil { 1382 panic(fmt.Sprintf("failed to synchronise blocks: %v", err)) 1383 } 1384 }() 1385 <-starting 1386 checkProgress(t, tester.downloader, "completing", ethereum.SyncProgress{ 1387 CurrentBlock: afterFailedSync.CurrentBlock, 1388 HighestBlock: uint64(validChain.len() - 1), 1389 }) 1390 1391 // Check final progress after successful sync. 1392 progress <- struct{}{} 1393 pending.Wait() 1394 checkProgress(t, tester.downloader, "final", ethereum.SyncProgress{ 1395 CurrentBlock: uint64(validChain.len() - 1), 1396 HighestBlock: uint64(validChain.len() - 1), 1397 }) 1398 } 1399 1400 // This test reproduces an issue where unexpected deliveries would 1401 // block indefinitely if they arrived at the right time. 1402 func TestDeliverHeadersHang(t *testing.T) { 1403 t.Parallel() 1404 1405 testCases := []struct { 1406 protocol int 1407 syncMode SyncMode 1408 }{ 1409 {62, FullSync}, 1410 {63, FullSync}, 1411 {63, FastSync}, 1412 {64, FullSync}, 1413 {64, FastSync}, 1414 {64, LightSync}, 1415 } 1416 for _, tc := range testCases { 1417 t.Run(fmt.Sprintf("protocol %d mode %v", tc.protocol, tc.syncMode), func(t *testing.T) { 1418 t.Parallel() 1419 testDeliverHeadersHang(t, tc.protocol, tc.syncMode) 1420 }) 1421 } 1422 } 1423 1424 func testDeliverHeadersHang(t *testing.T, protocol int, mode SyncMode) { 1425 master := newTester() 1426 defer master.terminate() 1427 chain := testChainBase.shorten(15) 1428 1429 for i := 0; i < 200; i++ { 1430 tester := newTester() 1431 tester.peerDb = master.peerDb 1432 tester.newPeer("peer", protocol, chain) 1433 1434 // Whenever the downloader requests headers, flood it with 1435 // a lot of unrequested header deliveries. 1436 tester.downloader.peers.peers["peer"].peer = &floodingTestPeer{ 1437 peer: tester.downloader.peers.peers["peer"].peer, 1438 tester: tester, 1439 } 1440 if err := tester.sync("peer", nil, mode); err != nil { 1441 t.Errorf("test %d: sync failed: %v", i, err) 1442 } 1443 tester.terminate() 1444 } 1445 } 1446 1447 type floodingTestPeer struct { 1448 peer Peer 1449 tester *downloadTester 1450 } 1451 1452 func (ftp *floodingTestPeer) Head() (common.Hash, *big.Int) { return ftp.peer.Head() } 1453 func (ftp *floodingTestPeer) RequestHeadersByHash(hash common.Hash, count int, skip int, reverse bool) error { 1454 return ftp.peer.RequestHeadersByHash(hash, count, skip, reverse) 1455 } 1456 func (ftp *floodingTestPeer) RequestBodies(hashes []common.Hash) error { 1457 return ftp.peer.RequestBodies(hashes) 1458 } 1459 func (ftp *floodingTestPeer) RequestReceipts(hashes []common.Hash) error { 1460 return ftp.peer.RequestReceipts(hashes) 1461 } 1462 func (ftp *floodingTestPeer) RequestNodeData(hashes []common.Hash) error { 1463 return ftp.peer.RequestNodeData(hashes) 1464 } 1465 1466 func (ftp *floodingTestPeer) RequestHeadersByNumber(from uint64, count, skip int, reverse bool) error { 1467 deliveriesDone := make(chan struct{}, 500) 1468 for i := 0; i < cap(deliveriesDone)-1; i++ { 1469 peer := fmt.Sprintf("fake-peer%d", i) 1470 go func() { 1471 ftp.tester.downloader.DeliverHeaders(peer, []*types.Header{{}, {}, {}, {}}) 1472 deliveriesDone <- struct{}{} 1473 }() 1474 } 1475 1476 // None of the extra deliveries should block. 1477 timeout := time.After(60 * time.Second) 1478 launched := false 1479 for i := 0; i < cap(deliveriesDone); i++ { 1480 select { 1481 case <-deliveriesDone: 1482 if !launched { 1483 // Start delivering the requested headers 1484 // after one of the flooding responses has arrived. 1485 go func() { 1486 ftp.peer.RequestHeadersByNumber(from, count, skip, reverse) 1487 deliveriesDone <- struct{}{} 1488 }() 1489 launched = true 1490 } 1491 case <-timeout: 1492 panic("blocked") 1493 } 1494 } 1495 return nil 1496 } 1497 1498 func TestRemoteHeaderRequestSpan(t *testing.T) { 1499 testCases := []struct { 1500 remoteHeight uint64 1501 localHeight uint64 1502 expected []int 1503 }{ 1504 // Remote is way higher. We should ask for the remote head and go backwards 1505 {1500, 1000, 1506 []int{1323, 1339, 1355, 1371, 1387, 1403, 1419, 1435, 1451, 1467, 1483, 1499}, 1507 }, 1508 {15000, 13006, 1509 []int{14823, 14839, 14855, 14871, 14887, 14903, 14919, 14935, 14951, 14967, 14983, 14999}, 1510 }, 1511 //Remote is pretty close to us. We don't have to fetch as many 1512 {1200, 1150, 1513 []int{1149, 1154, 1159, 1164, 1169, 1174, 1179, 1184, 1189, 1194, 1199}, 1514 }, 1515 // Remote is equal to us (so on a fork with higher td) 1516 // We should get the closest couple of ancestors 1517 {1500, 1500, 1518 []int{1497, 1499}, 1519 }, 1520 // We're higher than the remote! Odd 1521 {1000, 1500, 1522 []int{997, 999}, 1523 }, 1524 // Check some weird edgecases that it behaves somewhat rationally 1525 {0, 1500, 1526 []int{0, 2}, 1527 }, 1528 {6000000, 0, 1529 []int{5999823, 5999839, 5999855, 5999871, 5999887, 5999903, 5999919, 5999935, 5999951, 5999967, 5999983, 5999999}, 1530 }, 1531 {0, 0, 1532 []int{0, 2}, 1533 }, 1534 } 1535 reqs := func(from, count, span int) []int { 1536 var r []int 1537 num := from 1538 for len(r) < count { 1539 r = append(r, num) 1540 num += span + 1 1541 } 1542 return r 1543 } 1544 for i, tt := range testCases { 1545 from, count, span, max := calculateRequestSpan(tt.remoteHeight, tt.localHeight) 1546 data := reqs(int(from), count, span) 1547 1548 if max != uint64(data[len(data)-1]) { 1549 t.Errorf("test %d: wrong last value %d != %d", i, data[len(data)-1], max) 1550 } 1551 failed := false 1552 if len(data) != len(tt.expected) { 1553 failed = true 1554 t.Errorf("test %d: length wrong, expected %d got %d", i, len(tt.expected), len(data)) 1555 } else { 1556 for j, n := range data { 1557 if n != tt.expected[j] { 1558 failed = true 1559 break 1560 } 1561 } 1562 } 1563 if failed { 1564 res := strings.Replace(fmt.Sprint(data), " ", ",", -1) 1565 exp := strings.Replace(fmt.Sprint(tt.expected), " ", ",", -1) 1566 fmt.Printf("got: %v\n", res) 1567 fmt.Printf("exp: %v\n", exp) 1568 t.Errorf("test %d: wrong values", i) 1569 } 1570 } 1571 }