Skip to content

Commit

Permalink
Reorg rethinked
Browse files Browse the repository at this point in the history
  • Loading branch information
nopara73 committed Apr 17, 2017
1 parent 1fa5195 commit 62d9f69
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/HBitcoin/FullBlockSpv/Tracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,20 @@ public Tracker(Network network)

public void ReorgOne()
{
UnprocessedBlockBuffer.Clear();
// remove the last block
if (MerkleChain.Count != 0)
{
if(MerkleChain.TryRemove(MerkleChain.Max()))
var bestBlock = MerkleChain.Max();
if (MerkleChain.TryRemove(bestBlock))
{
List<SmartTransaction> affectedTxs = TrackedTransactions.Where(x => x.Height == bestBlock.Height).Select(x=>x).ToList();
foreach (var tx in affectedTxs)
{
TrackedTransactions.TryRemove(tx);
// add it back as a mempool transaction, it'll drop out anyway
TrackedTransactions.TryAdd(new SmartTransaction(tx.Transaction, Height.MemPool));
}
BestHeight = MerkleChain.Max().Height;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/HBitcoin/FullBlockSpv/WalletJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -674,9 +674,9 @@ private async Task BlockDownloadingJobAsync(CancellationToken ctsToken)

private void Reorg()
{
Tracker.UnprocessedBlockBuffer.Clear();
HeaderChain.SetTip(HeaderChain.Tip.Previous);
Tracker.ReorgOne();
SaveAllChangedAsync().Wait();
}
#endregion

Expand Down
4 changes: 3 additions & 1 deletion src/HBitcoin/MemPool/MemPoolJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ await Task.Run(() => node.GetMempoolTransactions(txIdsPiece.ToArray(), ctsToken)
System.Diagnostics.Debug.WriteLine(ex);
continue;
}
}

System.Diagnostics.Debug.WriteLine($"Mirrored a node's full MemPool. Local MemPool transaction count: {Transactions.Count}");
}
return txidsWeAlreadyHadAndFound;
}
}
Expand Down

0 comments on commit 62d9f69

Please sign in to comment.