diff options
author | Sonic <sonic@dexon.org> | 2019-03-27 20:02:55 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@byzantine-lab.io> | 2019-06-13 18:11:44 +0800 |
commit | c52c9e04a916fac3550b0a3c3d8cdf979ab70bb8 (patch) | |
tree | aa9e20f32fa084fd9c5e2fbfcee295d5d63b1d48 /dex/handler.go | |
parent | 7b8b4fcb0e8fd411bf523d06492e966e20e1b613 (diff) | |
download | go-tangerine-c52c9e04a916fac3550b0a3c3d8cdf979ab70bb8.tar go-tangerine-c52c9e04a916fac3550b0a3c3d8cdf979ab70bb8.tar.gz go-tangerine-c52c9e04a916fac3550b0a3c3d8cdf979ab70bb8.tar.bz2 go-tangerine-c52c9e04a916fac3550b0a3c3d8cdf979ab70bb8.tar.lz go-tangerine-c52c9e04a916fac3550b0a3c3d8cdf979ab70bb8.tar.xz go-tangerine-c52c9e04a916fac3550b0a3c3d8cdf979ab70bb8.tar.zst go-tangerine-c52c9e04a916fac3550b0a3c3d8cdf979ab70bb8.zip |
backport from v1.8.23 (#304)
* dex: backport f6193ad
* dex/downloader: backport accc0fa accc0fab 174083c3
* dex: backport 434dd5b
* dex: backport 42a914a 0983d02
* dex: backport 48b70ec 31b3334 and some modification
* dex/downloader: backport 5f251a6
* dex/downloader: backport 81c3dc7
* dex, dex/downloader: fix typos
Diffstat (limited to 'dex/handler.go')
-rw-r--r-- | dex/handler.go | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/dex/handler.go b/dex/handler.go index ec5704103..20df41709 100644 --- a/dex/handler.go +++ b/dex/handler.go @@ -124,6 +124,8 @@ type ProtocolManager struct { recordsCh chan newRecordsEvent recordsSub event.Subscription + whitelist map[uint64]common.Hash + // channels for fetcher, syncer, txsyncLoop newPeerCh chan *peer txsyncCh chan *txsync @@ -161,7 +163,7 @@ type ProtocolManager struct { func NewProtocolManager( config *params.ChainConfig, mode downloader.SyncMode, networkID uint64, mux *event.TypeMux, txpool txPool, engine consensus.Engine, - blockchain *core.BlockChain, chaindb ethdb.Database, + blockchain *core.BlockChain, chaindb ethdb.Database, whitelist map[uint64]common.Hash, isBlockProposer bool, gov governance, app dexconApp) (*ProtocolManager, error) { tab := newNodeTable() // Create the protocol manager with the base fields @@ -175,6 +177,7 @@ func NewProtocolManager( cache: newCache(5120, dexDB.NewDatabase(chaindb)), nextPullVote: &sync.Map{}, chainconfig: config, + whitelist: whitelist, newPeerCh: make(chan *peer), noMorePeers: make(chan struct{}), txsyncCh: make(chan *txsync), @@ -388,7 +391,13 @@ func (pm *ProtocolManager) handle(p *peer) error { pm.syncTransactions(p) pm.syncNodeRecords(p) - // main loop. handle incoming messages. + // If we have any explicit whitelist block hashes, request them + for number := range pm.whitelist { + if err := p.RequestWhitelistHeader(number); err != nil { + return err + } + } + // Handle incoming messages until the connection is torn down for { if err := pm.handleMsg(p); err != nil { p.Log().Debug("Ethereum message handling failed", "err", err) @@ -597,6 +606,14 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { if err != nil { log.Debug("Failed to deliver headers", "err", err) } + case whitelistReq: + if want, ok := pm.whitelist[data.Headers[0].Number.Uint64()]; ok { + if hash := data.Headers[0].Hash(); want != hash { + p.Log().Info("Whitelist mismatch, dropping peer", "number", data.Headers[0].Number.Uint64(), "hash", hash, "want", want) + return errors.New("whitelist block mismatch") + } + p.Log().Debug("Whitelist block verified", "number", data.Headers[0].Number.Uint64(), "hash", want) + } default: log.Debug("Got headers with unexpected flag", "flag", data.Flag) } @@ -791,7 +808,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { p.SetHead(trueHead, trueNumber) // Schedule a sync if above ours. Note, this will not fire a sync for a gap of - // a singe block (as the true number is below the propagated block), however this + // a single block (as the true number is below the propagated block), however this // scenario should easily be covered by the fetcher. currentBlock := pm.blockchain.CurrentBlock() if trueNumber > currentBlock.NumberU64() { |