From b9fcb0de08e5c3cdf93f3574721ffafc62b9a0cf Mon Sep 17 00:00:00 2001 From: Dan Gershony Date: Thu, 15 Jun 2017 01:36:49 +0100 Subject: [PATCH] Reset the puller when the chain has reorgenaized --- .../Notifications/BlockNotification.cs | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/Stratis.Bitcoin/Notifications/BlockNotification.cs b/Stratis.Bitcoin/Notifications/BlockNotification.cs index 1a64bbfdce7..f950c986fbb 100644 --- a/Stratis.Bitcoin/Notifications/BlockNotification.cs +++ b/Stratis.Bitcoin/Notifications/BlockNotification.cs @@ -12,6 +12,7 @@ namespace Stratis.Bitcoin.Notifications public class BlockNotification { private readonly ISignals signals; + private ChainedBlock tip; public BlockNotification(ConcurrentChain chain, ILookaheadBlockPuller puller, ISignals signals) { @@ -22,7 +23,7 @@ public BlockNotification(ConcurrentChain chain, ILookaheadBlockPuller puller, IS this.Chain = chain; this.Puller = puller; this.signals = signals; - + this.CointinueOnReorg = true; } public ILookaheadBlockPuller Puller { get; } @@ -32,6 +33,7 @@ public BlockNotification(ConcurrentChain chain, ILookaheadBlockPuller puller, IS public uint256 StartHash { get; private set; } private bool reSync; + public bool CointinueOnReorg { get; set; } public void SyncFrom(uint256 startHash) { @@ -43,6 +45,7 @@ public void SyncFrom(uint256 startHash) { // sets the location of the puller to the latest hash that was broadcasted this.Puller.SetLocation(startBlock); + this.tip = startBlock; } } @@ -73,6 +76,7 @@ public virtual void Notify(CancellationToken cancellationToken) // sets the location of the puller to the latest hash that was broadcasted this.Puller.SetLocation(startBlock); + this.tip = startBlock; // send notifications for all the following blocks while (!this.reSync) @@ -83,10 +87,23 @@ public virtual void Notify(CancellationToken cancellationToken) { // broadcast the block to the registered observers this.signals.Blocks.Broadcast(block); + this.tip = this.Chain.GetBlock(block.GetHash()); } else { - break; + // in reorg we reset the puller to the fork + // when a reorg happens the puller is pushed + // back and continues from the current fork + + // find the fork + while (this.Chain.GetBlock(this.tip.HashBlock) == null) + this.tip = this.tip.Previous; + + // set the puller to the fork location + this.Puller.SetLocation(this.tip); + + if (!this.CointinueOnReorg) + break; } }