Keep uint256 in memory instead of the whole BlockHeader
MODIFY
WalletWasabi.Tests/UnitTests/BitcoinCore/P2pBasedTests.cs
WalletWasabi.Tests/UnitTests/BitcoinCore/P2pBasedTests.cs
@@ -160,7 +160,7 @@ namespace WalletWasabi.Tests.UnitTests.BitcoinCore
// Make sure we get reorg notifications.
var reorgNum = 3;
var newBlockNum = reorgNum + 1;
- var reorgEventsAwaiter = new EventsAwaiter<BlockHeader>(
+ var reorgEventsAwaiter = new EventsAwaiter<uint256>(
h => notifier.OnReorg += h,
h => notifier.OnReorg -= h,
reorgNum);
@@ -180,7 +180,7 @@ namespace WalletWasabi.Tests.UnitTests.BitcoinCore
for (int i = 0; i < reorgedHashes.Length; i++)
{
var expected = reorgedHashes[i];
- var actual = reorgedHeaders[i].GetHash();
+ var actual = reorgedHeaders[i];
Assert.Equal(expected, actual);
}

MODIFY
WalletWasabi.Tests/UnitTests/BlockNotifierTests.cs
WalletWasabi.Tests/UnitTests/BlockNotifierTests.cs
@@ -21,7 +21,7 @@ namespace WalletWasabi.Tests.UnitTests
var blockAwaiter = new EventAwaiter<Block>(
h => notifier.OnBlock += h,
h => notifier.OnBlock -= h);
- var reorgAwaiter = new EventAwaiter<BlockHeader>(
+ var reorgAwaiter = new EventAwaiter<uint256>(
h => notifier.OnReorg += h,
h => notifier.OnReorg -= h);
@@ -48,7 +48,7 @@ namespace WalletWasabi.Tests.UnitTests
var blockAwaiter = new EventAwaiter<Block>(
h => notifier.OnBlock += h,
h => notifier.OnBlock -= h);
- var reorgAwaiter = new EventAwaiter<BlockHeader>(
+ var reorgAwaiter = new EventAwaiter<uint256>(
h => notifier.OnReorg += h,
h => notifier.OnReorg -= h);
@@ -70,7 +70,7 @@ namespace WalletWasabi.Tests.UnitTests
using var notifier = CreateNotifier(chain);
var blockCount = 3;
- var reorgAwaiter = new EventAwaiter<BlockHeader>(
+ var reorgAwaiter = new EventAwaiter<uint256>(
h => notifier.OnReorg += h,
h => notifier.OnReorg -= h);
@@ -111,7 +111,7 @@ namespace WalletWasabi.Tests.UnitTests
h => notifier.OnBlock -= h,
5);
- var reorgAwaiter = new EventAwaiter<BlockHeader>(
+ var reorgAwaiter = new EventAwaiter<uint256>(
h => notifier.OnReorg += h,
h => notifier.OnReorg -= h);
@@ -132,8 +132,7 @@ namespace WalletWasabi.Tests.UnitTests
// No reorg notifications
var reorgedkBlock = await reorgAwaiter.WaitAsync(TimeSpan.FromSeconds(1));
- Assert.Equal(forkPoint.HashBlock, reorgedkBlock.HashPrevBlock);
- Assert.Equal(blockToBeReorged.HashBlock, reorgedkBlock.GetHash());
+ Assert.Equal(blockToBeReorged.HashBlock, reorgedkBlock);
Assert.Equal(chain.Tip.HashBlock, notifier.BestBlockHash);
await notifier.StopAsync(CancellationToken.None);
@@ -150,7 +149,7 @@ namespace WalletWasabi.Tests.UnitTests
h => notifier.OnBlock -= h,
11);
- var reorgAwaiter = new EventsAwaiter<BlockHeader>(
+ var reorgAwaiter = new EventsAwaiter<uint256>(
h => notifier.OnReorg += h,
h => notifier.OnReorg -= h,
3);
@@ -187,7 +186,7 @@ namespace WalletWasabi.Tests.UnitTests
// No reorg notifications
var reorgedkBlock = await reorgAwaiter.WaitAsync(TimeSpan.FromSeconds(1));
var expectedReorgedBlocks = firstReorgedChain.ToList().Concat(new[] { secondReorgedChain[2] });
- Assert.Subset(reorgedkBlock.Select(x => x.GetHash()).ToHashSet(), expectedReorgedBlocks.Select(x => x.Header.GetHash()).ToHashSet());
+ Assert.Subset(reorgedkBlock.ToHashSet(), expectedReorgedBlocks.Select(x => x.Header.GetHash()).ToHashSet());
Assert.Equal(chain.Tip.HashBlock, notifier.BestBlockHash);
await notifier.StopAsync(CancellationToken.None);

MODIFY
WalletWasabi/Blockchain/Blocks/BlockNotifier.cs
WalletWasabi/Blockchain/Blocks/BlockNotifier.cs
@@ -20,11 +20,13 @@ namespace WalletWasabi.Blockchain.Blocks
{
public event EventHandler<Block> OnBlock;
- public event EventHandler<BlockHeader> OnReorg;
+ public event EventHandler<uint256> OnReorg;
public IRPCClient RpcClient { get; set; }
public Network Network => RpcClient.Network;
- private List<BlockHeader> ProcessedBlocks { get; }
+
+ private List<uint256> ProcessedBlocks { get; }
+
public P2pNode P2pNode { get; }
public uint256 BestBlockHash { get; private set; }
@@ -32,7 +34,7 @@ namespace WalletWasabi.Blockchain.Blocks
{
RpcClient = Guard.NotNull(nameof(rpcClient), rpcClient);
P2pNode = p2pNode;
- ProcessedBlocks = new List<BlockHeader>();
+ ProcessedBlocks = new List<uint256>();
if (p2pNode is { })
{
p2pNode.BlockInv += P2pNode_BlockInv;
@@ -89,14 +91,14 @@ namespace WalletWasabi.Blockchain.Blocks
}
// If block was already processed return.
- if (ProcessedBlocks.Any(x => x.GetHash() == arrivedHeader.GetHash()))
+ if (ProcessedBlocks.Contains(arrivedHeader.GetHash()))
{
BestBlockHash = bestBlockHash;
return;
}
// If this block follows the proper order then add.
- if (ProcessedBlocks.Last().GetHash() == arrivedHeader.HashPrevBlock)
+ if (ProcessedBlocks.Last() == arrivedHeader.HashPrevBlock)
{
AddBlock(arrivedBlock);
BestBlockHash = bestBlockHash;
@@ -104,7 +106,7 @@ namespace WalletWasabi.Blockchain.Blocks
}
// Else let's sort out things.
- var foundPrevBlock = ProcessedBlocks.FirstOrDefault(x => x.GetHash() == arrivedHeader.HashPrevBlock);
+ var foundPrevBlock = ProcessedBlocks.FirstOrDefault(x => x == arrivedHeader.HashPrevBlock);
// Missed notifications on some previous blocks.
if (foundPrevBlock != null)
{
@@ -154,11 +156,11 @@ namespace WalletWasabi.Blockchain.Blocks
}
// If we found the proper chain.
- var foundPrevBlock = ProcessedBlocks.FirstOrDefault(x => x.GetHash() == currentHeader.HashPrevBlock);
+ var foundPrevBlock = ProcessedBlocks.FirstOrDefault(x => x == currentHeader.HashPrevBlock);
if (foundPrevBlock != null)
{
// If the last block hash is not what we found, then we missed a reorg also.
- if (foundPrevBlock.GetHash() != ProcessedBlocks.Last().GetHash())
+ if (foundPrevBlock != ProcessedBlocks.Last())
{
ReorgToBlock(foundPrevBlock);
}
@@ -182,10 +184,10 @@ namespace WalletWasabi.Blockchain.Blocks
private void AddHeader(BlockHeader block)
{
- ProcessedBlocks.Add(block);
+ ProcessedBlocks.Add(block.GetHash());
}
- private void ReorgToBlock(BlockHeader correctBlock)
+ private void ReorgToBlock(uint256 correctBlock)
{
var index = ProcessedBlocks.IndexOf(correctBlock);
int countToRemove = ProcessedBlocks.Count - (index + 1);

Keep uint256 in memory instead of the whole BlockHeader