Add test for chain reogs
MODIFY
MagicalCryptoWallet.Tests/NodeBuilding/CoreNode.cs
MagicalCryptoWallet.Tests/NodeBuilding/CoreNode.cs
@@ -80,20 +80,22 @@ namespace MagicalCryptoWallet.Tests.NodeBuilding
}
catch (DirectoryNotFoundException) { }
}
-#if !NOSOCKET
- public void Sync(CoreNode node, bool keepConnection = false)
+
+ public async Task SyncAsync(CoreNode node, bool keepConnection = false)
{
var rpc = CreateRPCClient();
var rpc1 = node.CreateRPCClient();
rpc.AddNode(node.Endpoint, true);
- while (rpc.GetBestBlockHash() != rpc1.GetBestBlockHash())
+ while (await rpc.GetBestBlockHashAsync() != await rpc1.GetBestBlockHashAsync())
{
- Thread.Sleep(200);
+ await Task.Delay(200);
}
if (!keepConnection)
+ {
rpc.RemoveNode(node.Endpoint);
}
-#endif
+ }
+
private CoreNodeState _state;
public CoreNodeState State
{
@@ -144,6 +146,7 @@ namespace MagicalCryptoWallet.Tests.NodeBuilding
{
{"regtest", "1"},
{"rest", "1"},
+ {"listenonion", "0"},
{"server", "1"},
{"txindex", "0"},
{"rpcuser", Creds.UserName},
@@ -157,7 +160,7 @@ namespace MagicalCryptoWallet.Tests.NodeBuilding
File.WriteAllText(_config, config.ToString());
lock (_l)
{
- _process = Process.Start(new FileInfo(_Builder.BitcoinD).FullName, "-conf=bitcoin.conf" + " -datadir=" + DataDir + " -debug=net");
+ _process = Process.Start(new FileInfo(_Builder.BitcoinD).FullName, "-conf=bitcoin.conf" + " -datadir=" + DataDir + " -debug=1");
_state = CoreNodeState.Starting;
}
while (true)
@@ -365,7 +368,7 @@ namespace MagicalCryptoWallet.Tests.NodeBuilding
return new TransactionSignature(ecdsa, sig.SigHash);
}
- public void BroadcastBlocks(Block[] blocks)
+ public void BroadcastBlocks(IEnumerable<Block> blocks)
{
using (var node = CreateNodeClient())
{
@@ -374,7 +377,7 @@ namespace MagicalCryptoWallet.Tests.NodeBuilding
}
}
- public void BroadcastBlocks(Block[] blocks, Node node)
+ public void BroadcastBlocks(IEnumerable<Block> blocks, Node node)
{
foreach (var block in blocks)
{
@@ -390,6 +393,17 @@ namespace MagicalCryptoWallet.Tests.NodeBuilding
return Generate(blockCount, includeMempool);
}
+ public void MineBlock(Block block)
+ {
+ block.UpdateMerkleRoot();
+ uint nonce = 0;
+ while (!block.CheckProofOfWork(Network.RegTest.Consensus))
+ {
+ block.Header.Nonce = ++nonce;
+ }
+ }
+
+
private class TransactionNode
{
public TransactionNode(Transaction tx)

MODIFY
MagicalCryptoWallet.Tests/RegTests.cs
MagicalCryptoWallet.Tests/RegTests.cs
@@ -323,6 +323,68 @@ namespace MagicalCryptoWallet.Tests
}
}
+
+
+ [Fact]
+ public async Task FilterReorgTestAsync()
+ {
+ await AssertFiltersInitializedAsync();
+
+ var node0 = Fixture.BackendRegTestNode;
+ var indexFilePath = Path.Combine(SharedFixture.DataDir, nameof(FilterDownloaderTestAsync), $"Index{Global.RpcClient.Network}.dat");
+
+ var downloader = new IndexDownloader(Global.RpcClient.Network, indexFilePath, new Uri(Fixture.BackendEndPoint));
+ try
+ {
+ downloader.Syncronize(requestInterval: TimeSpan.FromSeconds(1));
+
+ // Test initial syncronization.
+
+ var times = 0;
+ int filterCount;
+ while ((filterCount = downloader.GetFiltersIncluding(Network.RegTest.GenesisHash).Count()) < 102)
+ {
+ if (times > 500) // 30 sec
+ {
+ throw new TimeoutException($"{nameof(IndexDownloader)} test timed out. Needed filters: {102}, got only: {filterCount}.");
+ }
+ await Task.Delay(100);
+ times++;
+ }
+
+ // Test syncronization after fork.
+ var node1 = Fixture.BackendNodeBuilder.CreateNode(start: true);
+ await node1.CreateRPCClient().GenerateAsync(120);
+ await node1.SyncAsync(node0);
+
+ times = 0;
+ while ((filterCount = downloader.GetFiltersIncluding(new Height(0)).Count()) < 120)
+ {
+ if (times > 240) // 2 min
+ {
+ throw new TimeoutException($"{nameof(IndexDownloader)} test timed out. Needed filters: {120}, got only: {filterCount}.");
+ }
+ await Task.Delay(500);
+ times++;
+ }
+
+ // Test filter block hashes are correct after fork.
+ var filters = downloader.GetFiltersIncluding(Network.RegTest.GenesisHash).ToArray();
+ for (int i = 0; i < 120; i++)
+ {
+ var expectedHash = await Global.RpcClient.GetBlockHashAsync(i);
+ var filter = filters[i];
+ Assert.Equal(i, filter.BlockHeight.Value);
+ Assert.Equal(expectedHash, filter.BlockHash);
+ Assert.Null(filter.Filter);
+ }
+ }
+ finally
+ {
+ downloader.Stop();
+ }
+ }
+
#endregion
#region ClientTests

Add test for chain reogs