17
17
package eth
18
18
19
19
import (
20
- "bytes"
21
20
"encoding/json"
22
21
"errors"
23
22
"fmt"
@@ -311,17 +310,13 @@ func (pm *ProtocolManager) handle(p *peer) error {
311
310
}
312
311
}()
313
312
}
314
-
315
313
// If we have any explicit whitelist block hashes, request them
316
- for bn := range pm .whitelist {
317
- p .Log ().Debug ("Requesting whitelist block" , "number" , bn )
318
- if err := p .RequestHeadersByNumber (bn , 1 , 0 , false ); err != nil {
319
- p .Log ().Error ("whitelist request failed" , "err" , err , "number" , bn , "peer" , p .id )
314
+ for number := range pm .whitelist {
315
+ if err := p .RequestHeadersByNumber (number , 1 , 0 , false ); err != nil {
320
316
return err
321
317
}
322
318
}
323
-
324
- // main loop. handle incoming messages.
319
+ // Handle incoming messages until the connection is torn down
325
320
for {
326
321
if err := pm .handleMsg (p ); err != nil {
327
322
p .Log ().Debug ("Ethereum message handling failed" , "err" , err )
@@ -466,16 +461,6 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
466
461
// Filter out any explicitly requested headers, deliver the rest to the downloader
467
462
filter := len (headers ) == 1
468
463
if filter {
469
- // Check for any responses not matching our whitelist
470
- if expected , ok := pm .whitelist [headers [0 ].Number .Uint64 ()]; ok {
471
- actual := headers [0 ].Hash ()
472
- if ! bytes .Equal (expected .Bytes (), actual .Bytes ()) {
473
- p .Log ().Info ("Dropping peer with non-matching whitelist block" , "number" , headers [0 ].Number .Uint64 (), "hash" , actual , "expected" , expected )
474
- return errors .New ("whitelist block mismatch" )
475
- }
476
- p .Log ().Debug ("Whitelist block verified" , "number" , headers [0 ].Number .Uint64 (), "hash" , expected )
477
- }
478
-
479
464
// If it's a potential DAO fork check, validate against the rules
480
465
if p .forkDrop != nil && pm .chainconfig .DAOForkBlock .Cmp (headers [0 ].Number ) == 0 {
481
466
// Disable the fork drop timer
@@ -490,6 +475,14 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
490
475
p .Log ().Debug ("Verified to be on the same side of the DAO fork" )
491
476
return nil
492
477
}
478
+ // Otherwise if it's a whitelisted block, validate against the set
479
+ if want , ok := pm .whitelist [headers [0 ].Number .Uint64 ()]; ok {
480
+ if hash := headers [0 ].Hash (); want != hash {
481
+ p .Log ().Info ("Whitelist mismatch, dropping peer" , "number" , headers [0 ].Number .Uint64 (), "hash" , hash , "want" , want )
482
+ return errors .New ("whitelist block mismatch" )
483
+ }
484
+ p .Log ().Debug ("Whitelist block verified" , "number" , headers [0 ].Number .Uint64 (), "hash" , want )
485
+ }
493
486
// Irrelevant of the fork checks, send the header to the fetcher just in case
494
487
headers = pm .fetcher .FilterHeaders (p .id , headers , time .Now ())
495
488
}
0 commit comments