From 62d9f691c8f7485bf9e0469186cccfc1a4f0cf70 Mon Sep 17 00:00:00 2001 From: nopara73 Date: Mon, 17 Apr 2017 20:08:40 +0800 Subject: [PATCH] Reorg rethinked --- src/HBitcoin/FullBlockSpv/Tracker.cs | 11 ++++++++++- src/HBitcoin/FullBlockSpv/WalletJob.cs | 2 +- src/HBitcoin/MemPool/MemPoolJob.cs | 4 +++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/HBitcoin/FullBlockSpv/Tracker.cs b/src/HBitcoin/FullBlockSpv/Tracker.cs index 2336dc2..967dcf1 100644 --- a/src/HBitcoin/FullBlockSpv/Tracker.cs +++ b/src/HBitcoin/FullBlockSpv/Tracker.cs @@ -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 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; } } diff --git a/src/HBitcoin/FullBlockSpv/WalletJob.cs b/src/HBitcoin/FullBlockSpv/WalletJob.cs index 9a6f411..1436ad1 100644 --- a/src/HBitcoin/FullBlockSpv/WalletJob.cs +++ b/src/HBitcoin/FullBlockSpv/WalletJob.cs @@ -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 diff --git a/src/HBitcoin/MemPool/MemPoolJob.cs b/src/HBitcoin/MemPool/MemPoolJob.cs index b9b21dc..cc638dc 100644 --- a/src/HBitcoin/MemPool/MemPoolJob.cs +++ b/src/HBitcoin/MemPool/MemPoolJob.cs @@ -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; } }