From 1eacbcbe3c890238da49fcfdddc151a5b7748265 Mon Sep 17 00:00:00 2001 From: Sonic Date: Wed, 20 Mar 2019 14:48:21 +0800 Subject: dex: ignore acceptableDist when force synchronise (#285) --- dex/blockproposer.go | 12 ++++++++++++ dex/handler.go | 2 +- dex/sync.go | 14 ++++++++++---- 3 files changed, 23 insertions(+), 5 deletions(-) (limited to 'dex') diff --git a/dex/blockproposer.go b/dex/blockproposer.go index 5af08f6c4..63aaa5b51 100644 --- a/dex/blockproposer.go +++ b/dex/blockproposer.go @@ -18,6 +18,10 @@ import ( "github.com/dexon-foundation/dexon/rlp" ) +var ( + forceSyncTimeout = 20 * time.Second +) + type blockProposer struct { mu sync.Mutex running int32 @@ -223,6 +227,14 @@ ListenLoop: case <-b.stopCh: log.Debug("Early stop, before consensus core can run") return nil, errors.New("early stop") + case <-time.After(forceSyncTimeout): + log.Debug("no new chain head for a while") + if p := b.dex.protocolManager.peers.BestPeer(); p != nil { + log.Debug("try force sync with peer", "id", p.id) + b.dex.protocolManager.synchronise(p, true) + } else { + log.Debug("no peer to sync") + } case <-b.watchCat.Meow(): log.Info("WatchCat signaled to stop syncing") diff --git a/dex/handler.go b/dex/handler.go index 57315f90a..ea5c31b36 100644 --- a/dex/handler.go +++ b/dex/handler.go @@ -793,7 +793,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { // scenario should easily be covered by the fetcher. currentBlock := pm.blockchain.CurrentBlock() if trueNumber > currentBlock.NumberU64() { - go pm.synchronise(p) + go pm.synchronise(p, false) } } diff --git a/dex/sync.go b/dex/sync.go index 927c04bc3..93bed87c4 100644 --- a/dex/sync.go +++ b/dex/sync.go @@ -245,11 +245,11 @@ func (pm *ProtocolManager) syncer() { if pm.peers.Len() < minDesiredPeerCount { break } - go pm.synchronise(pm.peers.BestPeer()) + go pm.synchronise(pm.peers.BestPeer(), false) case <-forceSync.C: // Force a sync even if not enough peers are present - go pm.synchronise(pm.peers.BestPeer()) + go pm.synchronise(pm.peers.BestPeer(), false) case <-pm.noMorePeers: return @@ -258,7 +258,7 @@ func (pm *ProtocolManager) syncer() { } // synchronise tries to sync up our local block chain with a remote peer. -func (pm *ProtocolManager) synchronise(peer *peer) { +func (pm *ProtocolManager) synchronise(peer *peer, force bool) { // Short circuit if no peers are available if peer == nil { return @@ -271,9 +271,15 @@ func (pm *ProtocolManager) synchronise(peer *peer) { // If we are behind the peer, but not more than acceptable distance, don't // trigger a sync. Fetcher is able to cover this. - if pNumber <= number+acceptableDist { + var dist uint64 + if !force { + dist = acceptableDist + } + + if pNumber <= number+dist { return } + // Otherwise try to sync with the downloader mode := downloader.FullSync if atomic.LoadUint32(&pm.fastSync) == 1 { -- cgit v1.2.3