@@ -10,7 +10,10 @@ import (
1010 "github.com/ethereum/go-ethereum/core/types"
1111)
1212
13- var knownHash = common.Hash {1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }
13+ var (
14+ knownHash = common.Hash {1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }
15+ unknownHash = common.Hash {9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 }
16+ )
1417
1518func createHashes (start , amount int ) (hashes []common.Hash ) {
1619 hashes = make ([]common.Hash , amount + 1 )
@@ -27,7 +30,7 @@ func createBlock(i int, prevHash, hash common.Hash) *types.Block {
2730 header := & types.Header {Number : big .NewInt (int64 (i ))}
2831 block := types .NewBlockWithHeader (header )
2932 block .HeaderHash = hash
30- block .ParentHeaderHash = knownHash
33+ block .ParentHeaderHash = prevHash
3134 return block
3235}
3336
@@ -42,17 +45,28 @@ func createBlocksFromHashes(hashes []common.Hash) map[common.Hash]*types.Block {
4245}
4346
4447type downloadTester struct {
45- downloader * Downloader
46- hashes []common.Hash
47- blocks map [common.Hash ]* types.Block
48+ downloader * Downloader
49+
50+ hashes []common.Hash // Chain of hashes simulating
51+ blocks map [common.Hash ]* types.Block // Blocks associated with the hashes
52+ chain []common.Hash // Block-chain being constructed
53+
4854 t * testing.T
4955 pcount int
5056 done chan bool
5157 activePeerId string
5258}
5359
5460func newTester (t * testing.T , hashes []common.Hash , blocks map [common.Hash ]* types.Block ) * downloadTester {
55- tester := & downloadTester {t : t , hashes : hashes , blocks : blocks , done : make (chan bool )}
61+ tester := & downloadTester {
62+ t : t ,
63+
64+ hashes : hashes ,
65+ blocks : blocks ,
66+ chain : []common.Hash {knownHash },
67+
68+ done : make (chan bool ),
69+ }
5670 downloader := New (tester .hasBlock , tester .getBlock )
5771 tester .downloader = downloader
5872
@@ -64,9 +78,17 @@ func (dl *downloadTester) sync(peerId string, hash common.Hash) error {
6478 return dl .downloader .Synchronise (peerId , hash )
6579}
6680
81+ func (dl * downloadTester ) insertBlocks (blocks types.Blocks ) {
82+ for _ , block := range blocks {
83+ dl .chain = append (dl .chain , block .Hash ())
84+ }
85+ }
86+
6787func (dl * downloadTester ) hasBlock (hash common.Hash ) bool {
68- if knownHash == hash {
69- return true
88+ for _ , h := range dl .chain {
89+ if h == hash {
90+ return true
91+ }
7092 }
7193 return false
7294}
@@ -175,10 +197,9 @@ func TestTaking(t *testing.T) {
175197 if err != nil {
176198 t .Error ("download error" , err )
177199 }
178-
179- bs1 := tester .downloader .TakeBlocks ()
180- if len (bs1 ) != 1000 {
181- t .Error ("expected to take 1000, got" , len (bs1 ))
200+ bs := tester .downloader .TakeBlocks ()
201+ if len (bs ) != targetBlocks {
202+ t .Error ("retrieved block mismatch: have %v, want %v" , len (bs ), targetBlocks )
182203 }
183204}
184205
@@ -248,17 +269,17 @@ func TestThrottling(t *testing.T) {
248269 done := make (chan struct {})
249270 took := []* types.Block {}
250271 go func () {
251- for {
272+ for running := true ; running ; {
252273 select {
253274 case <- done :
254- took = append (took , tester .downloader .TakeBlocks ()... )
255- done <- struct {}{}
256- return
275+ running = false
257276 default :
258- took = append (took , tester .downloader .TakeBlocks ()... )
259277 time .Sleep (time .Millisecond )
260278 }
279+ // Take a batch of blocks and accumulate
280+ took = append (took , tester .downloader .TakeBlocks ()... )
261281 }
282+ done <- struct {}{}
262283 }()
263284
264285 // Synchronise the two threads and verify
@@ -273,3 +294,43 @@ func TestThrottling(t *testing.T) {
273294 t .Fatalf ("downloaded block mismatch: have %v, want %v" , len (took ), targetBlocks )
274295 }
275296}
297+
298+ // Tests that if a peer returns an invalid chain with a block pointing to a non-
299+ // existing parent, it is correctly detected and handled.
300+ func TestNonExistingParentAttack (t * testing.T ) {
301+ // Forge a single-link chain with a forged header
302+ hashes := createHashes (0 , 1 )
303+ blocks := createBlocksFromHashes (hashes )
304+
305+ forged := blocks [hashes [0 ]]
306+ forged .ParentHeaderHash = unknownHash
307+
308+ // Try and sync with the malicious node and check that it fails
309+ tester := newTester (t , hashes , blocks )
310+ tester .newPeer ("attack" , big .NewInt (10000 ), hashes [0 ])
311+ if err := tester .sync ("attack" , hashes [0 ]); err != nil {
312+ t .Fatalf ("failed to synchronise blocks: %v" , err )
313+ }
314+ bs := tester .downloader .TakeBlocks ()
315+ if len (bs ) != 1 {
316+ t .Fatalf ("retrieved block mismatch: have %v, want %v" , len (bs ), 1 )
317+ }
318+ if tester .hasBlock (bs [0 ].ParentHash ()) {
319+ t .Fatalf ("tester knows about the unknown hash" )
320+ }
321+ tester .downloader .Cancel ()
322+
323+ // Reconstruct a valid chain, and try to synchronize with it
324+ forged .ParentHeaderHash = knownHash
325+ tester .newPeer ("valid" , big .NewInt (20000 ), hashes [0 ])
326+ if err := tester .sync ("valid" , hashes [0 ]); err != nil {
327+ t .Fatalf ("failed to synchronise blocks: %v" , err )
328+ }
329+ bs = tester .downloader .TakeBlocks ()
330+ if len (bs ) != 1 {
331+ t .Fatalf ("retrieved block mismatch: have %v, want %v" , len (bs ), 1 )
332+ }
333+ if ! tester .hasBlock (bs [0 ].ParentHash ()) {
334+ t .Fatalf ("tester doesn't know about the origin hash" )
335+ }
336+ }
0 commit comments