Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #161 from dangershony/notification-reset-on-reorg
Browse files Browse the repository at this point in the history
Reset the puller when the chain has reorgenaized
  • Loading branch information
dangershony authored Jun 15, 2017
2 parents c3b231d + b9fcb0d commit 4dd674d
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions Stratis.Bitcoin/Notifications/BlockNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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; }
Expand All @@ -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)
{
Expand All @@ -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;
}

}
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
}

Expand Down

0 comments on commit 4dd674d

Please sign in to comment.