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

Reset the puller when the chain has reorgenaized #161

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is an extra i in cointinue..


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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure this is necessary

break;
}
}

Expand Down