Add waitfor RPC methods
ADD
MagicalCryptoWallet.Tests/RpcMethodTests.cs
+using MagicalCryptoWallet.Tests.NodeBuilding;
+using NBitcoin.RPC;
+using System.Linq;
+using Xunit;
+
+namespace MagicalCryptoWallet.Tests
+{
+ public class RpcMethodTests : IClassFixture<SharedFixture>
+ {
+ private SharedFixture SharedFixture { get; }
+
+ public RpcMethodTests(SharedFixture fixture)
+ {
+ SharedFixture = fixture;
+ }
+
+ #region RPCMethodTests
+
+ [Fact]
+ public void CanWaitForNewBlockFromRPC()
+ {
+ using(var builder = NodeBuilder.Create())
+ {
+ var rpc = builder.CreateNode().CreateRPCClient();
+ builder.StartAll();
+ var latestBlockTask = rpc.WaitForNewBlockAsync(2 * 1000); // wait for 2 seconds
+ var generatedBlock = builder.Nodes.First().Generate(1);
+ latestBlockTask.Wait();
+ var latestBlock = latestBlockTask.Result;
+ Assert.True(latestBlockTask.IsCompleted && !latestBlockTask.IsFaulted);
+ Assert.Equal(generatedBlock[0].GetHash(), latestBlock.Hash);
+ }
+ }
+
+ [Fact]
+ public void CanWaitForBlockFromRPC()
+ {
+ using(var builder = NodeBuilder.Create())
+ {
+ var rpc = builder.CreateNode().CreateRPCClient();
+ builder.StartAll();
+ var generatedBlocks = builder.Nodes.First().Generate(10, true, false);
+ var latestBlockHash = generatedBlocks.Last().GetHash();
+ var latestBlockTask = rpc.WaitForBlockAsync(latestBlockHash);
+
+ builder.Nodes.First().BroadcastBlocks(generatedBlocks);
+ latestBlockTask.Wait();
+ var latestBlock = latestBlockTask.Result;
+ Assert.True(latestBlockTask.IsCompleted && !latestBlockTask.IsFaulted);
+ Assert.Equal(latestBlockHash, latestBlock.Hash);
+ }
+ }
+ #endregion
+ }
+}

ADD
MagicalCryptoWallet/Extensions/RPCClientExtensions.cs
+using System.Threading.Tasks;
+
+namespace NBitcoin.RPC
+{
+ public static class RPCClientExtensions
+ {
+ /// <summary>
+ /// Waits for a specific new block and returns useful info about it.
+ /// </summary>
+ /// <param name="timeout">(int, optional, default=0) Time in milliseconds to wait for a response. 0 indicates no timeout.</param>
+ /// <returns>Returns the current block on timeout or exit</returns>
+ public static async Task<BlockInfo> WaitForNewBlockAsync(this RPCClient rpc, long timeout = 0)
+ {
+ var resp = await rpc.SendCommandAsync("waitfornewblock", timeout).ConfigureAwait(false);
+ return new BlockInfo
+ {
+ Height = int.Parse(resp.Result["height"].ToString()),
+ Hash = uint256.Parse(resp.Result["hash"].ToString())
+ };
+ }
+
+ /// <summary>
+ /// Waits for a specific new block and returns useful info about it.
+ /// </summary>
+ /// <param name="blockhash">Block hash to wait for</param>
+ /// <param name="timeout">(int, optional, default=0) Time in milliseconds to wait for a response. 0 indicates no timeout.</param>
+ /// <returns>Returns the current block on timeout or exit</returns>
+ public static async Task<BlockInfo> WaitForBlockAsync(this RPCClient rpc, uint256 blockhash, long timeout = 0)
+ {
+ var resp = await rpc.SendCommandAsync("waitforblock", blockhash.ToString(), timeout).ConfigureAwait(false);
+ return new BlockInfo
+ {
+ Height = int.Parse(resp.Result["height"].ToString()),
+ Hash = uint256.Parse(resp.Result["hash"].ToString())
+ };
+ }
+ }
+
+ public class BlockInfo
+ {
+ public int Height { get; internal set; }
+ public uint256 Hash { get; internal set; }
+ }
+}
\ No newline at end of file

Add waitfor RPC methods