Add block remove so we can handle reorgs
MODIFY
MagicalCryptoWallet.Tests/WalletTests.cs
MagicalCryptoWallet.Tests/WalletTests.cs
@@ -87,7 +87,7 @@ namespace MagicalCryptoWallet.Tests
downloader.Start();
foreach(var hash in blocksToDownload)
{
- downloader.TryQueToDownload(hash);
+ downloader.QueToDownload(hash);
}
var wallet = new WalletService(dataFolder, network, manager, nodes, memPoolService);
try
@@ -124,7 +124,7 @@ namespace MagicalCryptoWallet.Tests
foreach(var hash in blocksToDownload)
{
times = 0;
- while (downloader.TryGetBlock(hash) == null)
+ while (downloader.GetBlock(hash) == null)
{
if (times > 1800) // 3 minutes
{
@@ -148,7 +148,14 @@ namespace MagicalCryptoWallet.Tests
finally
{
downloader?.Stop();
+
+ // So next test will download the block.
+ foreach (var hash in blocksToDownload)
+ {
+ downloader.TryRemove(hash);
+ }
Directory.Delete(blocksFolderPath, recursive: true);
+
addressManager?.SavePeerFile(addressManagerFilePath, network);
Logger.LogInfo<WalletTests>($"Saved {nameof(AddressManager)} to `{addressManagerFilePath}`.");
}

MODIFY
MagicalCryptoWallet/Services/BlockDownloader.cs
MagicalCryptoWallet/Services/BlockDownloader.cs
@@ -188,7 +188,7 @@ namespace MagicalCryptoWallet.Services
Interlocked.Exchange(ref _running, 0);
}
- public void TryQueToDownload(uint256 hash)
+ public void QueToDownload(uint256 hash)
{
using (BlocksFolderLock.Lock())
using (BlocksToDownloadLock.Lock())
@@ -207,8 +207,32 @@ namespace MagicalCryptoWallet.Services
}
}
+ /// <remarks>
+ /// Use it at reorgs.
+ /// </remarks>
+ public void TryRemove(uint256 hash)
+ {
+ using (BlocksFolderLock.Lock())
+ using (BlocksToDownloadLock.Lock())
+ {
+ if(BlocksToDownload.Contains(hash))
+ {
+ BlocksToDownload.Remove(hash);
+ }
+
+ var filePaths = Directory.EnumerateFiles(BlocksFolderPath);
+ var fileNames = filePaths.Select(x => Path.GetFileName(x));
+ var hashes = fileNames.Select(x => new uint256(x));
+
+ if (hashes.Contains(hash))
+ {
+ File.Delete(Path.Combine(BlocksFolderPath, hash.ToString()));
+ }
+ }
+ }
+
/// <returns>null if don't have, ques it if not qued</returns>
- public Block TryGetBlock(uint256 hash)
+ public Block GetBlock(uint256 hash)
{
using (BlocksFolderLock.Lock())
{
@@ -224,7 +248,7 @@ namespace MagicalCryptoWallet.Services
}
}
- TryQueToDownload(hash);
+ QueToDownload(hash);
return default;
}
}

Add block remove so we can handle reorgs