1717package eth
1818
1919import (
20+ "bytes"
2021 "encoding/json"
2122 "errors"
2223 "fmt"
@@ -88,6 +89,8 @@ type ProtocolManager struct {
8889 txsSub event.Subscription
8990 minedBlockSub * event.TypeMuxSubscription
9091
92+ whitelist map [uint64 ]common.Hash
93+
9194 // channels for fetcher, syncer, txsyncLoop
9295 newPeerCh chan * peer
9396 txsyncCh chan * txsync
@@ -101,7 +104,7 @@ type ProtocolManager struct {
101104
102105// NewProtocolManager returns a new Ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
103106// with the Ethereum network.
104- func NewProtocolManager (config * params.ChainConfig , mode downloader.SyncMode , networkID uint64 , mux * event.TypeMux , txpool txPool , engine consensus.Engine , blockchain * core.BlockChain , chaindb ethdb.Database ) (* ProtocolManager , error ) {
107+ func NewProtocolManager (config * params.ChainConfig , mode downloader.SyncMode , networkID uint64 , mux * event.TypeMux , txpool txPool , engine consensus.Engine , blockchain * core.BlockChain , chaindb ethdb.Database , whitelist map [ uint64 ]common. Hash ) (* ProtocolManager , error ) {
105108 // Create the protocol manager with the base fields
106109 manager := & ProtocolManager {
107110 networkID : networkID ,
@@ -110,6 +113,7 @@ func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, ne
110113 blockchain : blockchain ,
111114 chainconfig : config ,
112115 peers : newPeerSet (),
116+ whitelist : whitelist ,
113117 newPeerCh : make (chan * peer ),
114118 noMorePeers : make (chan struct {}),
115119 txsyncCh : make (chan * txsync ),
@@ -307,6 +311,16 @@ func (pm *ProtocolManager) handle(p *peer) error {
307311 }
308312 }()
309313 }
314+
315+ // 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 )
320+ return err
321+ }
322+ }
323+
310324 // main loop. handle incoming messages.
311325 for {
312326 if err := pm .handleMsg (p ); err != nil {
@@ -452,6 +466,16 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
452466 // Filter out any explicitly requested headers, deliver the rest to the downloader
453467 filter := len (headers ) == 1
454468 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+
455479 // If it's a potential DAO fork check, validate against the rules
456480 if p .forkDrop != nil && pm .chainconfig .DAOForkBlock .Cmp (headers [0 ].Number ) == 0 {
457481 // Disable the fork drop timer
0 commit comments