Пример #1
0
        public async Task CanGetBlockchainInfo()
        {
            using (var builder = NodeBuilderEx.Create())
            {
                var rpc = builder.CreateNode().CreateRPCClient();
                builder.StartAll();
                var response = await rpc.GetBlockchainInfoAsync();

                Assert.Equal(builder.Network, response.Chain);
                Assert.Equal(builder.Network.GetGenesis().GetHash(), response.BestBlockHash);
                Assert.True(response.SoftForks.Any(x => x.Bip == "csv"));
                Assert.True(response.SoftForks.Any(x => x.Bip == "bip34"));
                Assert.True(response.SoftForks.Any(x => x.Bip == "bip65"));
                Assert.True(response.SoftForks.Any(x => x.Bip == "bip66"));
            }
        }
Пример #2
0
 public void CanGetBlockHeader()
 {
     using (var builder = NodeBuilderEx.Create())
     {
         var client = builder.CreateNode().CreateRESTClient();
         var rpc    = builder.Nodes[0].CreateRPCClient();
         builder.StartAll();
         rpc.Generate(2);
         var result  = client.GetBlockHeadersAsync(RegNetGenesisBlock.GetHash(), 3).Result;
         var headers = result.ToArray();
         var last    = headers.Last();
         Assert.Equal(3, headers.Length);
         Assert.Equal(rpc.GetBestBlockHash(), last.GetHash());
         Assert.Equal(headers[1].GetHash(), last.HashPrevBlock);
         Assert.Equal(RegNetGenesisBlock.GetHash(), headers[1].HashPrevBlock);
     }
 }
Пример #3
0
        public void CanGetRawMemPool()
        {
            using (var builder = NodeBuilderEx.Create())
            {
                var node = builder.CreateNode();
                var rpc  = node.CreateRPCClient();
                builder.StartAll();
                node.Generate(101);

                var txid = new uint256(rpc.SendCommand("sendtoaddress",
                                                       new Key().PubKey.GetAddress(rpc.Network).ToString(),
                                                       "1.0").Result.ToString());
                var ids = rpc.GetRawMempool();
                Assert.Equal(1, ids.Length);
                Assert.Equal(txid, ids[0]);
            }
        }
Пример #4
0
 public void CanCalculateChainWork()
 {
     using (var builder = NodeBuilderEx.Create())
     {
         var node   = builder.CreateNode();
         var client = node.CreateRESTClient();
         var rpc    = node.CreateRPCClient();
         builder.StartAll();
         var info = client.GetChainInfoAsync().Result;
         Assert.Equal("regtest", info.Chain);
         Assert.Equal(new ChainedBlock(Network.RegTest.GetGenesis().Header, 0).GetChainWork(true),
                      info.ChainWork);
         rpc.Generate(10);
         var chain = node.CreateNodeClient().GetChain();
         info = client.GetChainInfoAsync().Result;
         Assert.Equal(info.ChainWork, chain.Tip.GetChainWork(true));
     }
 }
Пример #5
0
        public void ThrowsRestApiClientException()
        {
            using (var builder = NodeBuilderEx.Create())
            {
                var client = builder.CreateNode().CreateRESTClient();
                builder.StartAll();
                var unexistingBlockId =
                    uint256.Parse("100000006c02c8ea6e4ff69651f7fcde348fb9d557a06e6957b65552002a7820");
                Assert.Throws <RestApiException>(() => client.GetBlock(unexistingBlockId));

                var txId = uint256.Parse("7569ce92f93f9afd51ffae243e04076be4e5088cf69501aab6de9ede5c331402");
                Assert.Throws <RestApiException>(() => client.GetTransaction(txId));

                var result  = client.GetBlockHeaders(unexistingBlockId, 3);
                var headers = result.ToArray();
                Assert.Empty(headers);
            }
        }
Пример #6
0
        public void CanSignRawTransaction()
        {
            using (var builder = NodeBuilderEx.Create())
            {
                var node = builder.CreateNode();
                var rpc  = node.CreateRPCClient();
                builder.StartAll();
                node.Generate(101);

                var tx = rpc.Network.CreateTransaction();
                tx.Outputs.Add(new TxOut(Money.Coins(1.0m), new Key()));
                var funded = Transaction.Parse(
                    rpc.SendCommand("fundrawtransaction", tx.ToString())
                    .Result["hex"].ToString(), rpc.Network);
                var signed = rpc.SignRawTransaction(funded);
                rpc.SendRawTransaction(signed);
            }
        }
Пример #7
0
        public void InvalidateBlockToRPC()
        {
            using (var builder = NodeBuilderEx.Create())
            {
                var rpc = builder.CreateNode().CreateRPCClient();
                builder.StartAll();
                var generatedBlockHashes = rpc.Generate(2);
                var tip = rpc.GetBestBlockHash();

                var bestBlockHash = generatedBlockHashes.Last();
                Assert.Equal(tip, bestBlockHash);

                rpc.InvalidateBlock(bestBlockHash);
                tip = rpc.GetBestBlockHash();
                Assert.NotEqual(tip, bestBlockHash);

                bestBlockHash = generatedBlockHashes.First();
                Assert.Equal(tip, bestBlockHash);
            }
        }
Пример #8
0
 public void RPCSendRPCException()
 {
     using (var builder = NodeBuilderEx.Create())
     {
         var node = builder.CreateNode();
         builder.StartAll();
         var rpcClient = node.CreateRPCClient();
         try
         {
             rpcClient.SendCommand("whatever");
             Assert.False(true, "Should have thrown");
         }
         catch (RPCException ex)
         {
             if (ex.RPCCode != RPCErrorCode.RPC_METHOD_NOT_FOUND)
             {
                 Assert.False(true, "Should have thrown RPC_METHOD_NOT_FOUND");
             }
         }
     }
 }
Пример #9
0
        public void CanAuthWithCookieFile()
        {
            using (var builder = NodeBuilderEx.Create())
            {
                //Sanity check that it does not throw
#pragma warning disable CS0618
                new RPCClient("toto:tata:blah", "localhost:10393", Network.Main);

                var node = builder.CreateNode();
                node.CookieAuth = true;
                node.Start();
                var rpc = node.CreateRPCClient();
                rpc.GetBlockCount();
                node.Restart();
                rpc.GetBlockCount();
                new RPCClient("cookiefile=data/tx_valid.json", new Uri("http://localhost/"), Network.RegTest);
                new RPCClient("cookiefile=data/efpwwie.json", new Uri("http://localhost/"), Network.RegTest);

                rpc = new RPCClient("bla:bla", null as Uri, Network.RegTest);
                Assert.Equal("http://127.0.0.1:" + Network.RegTest.RPCPort + "/", rpc.Address.AbsoluteUri);

                rpc = node.CreateRPCClient();
                rpc = rpc.PrepareBatch();
                var blockCountAsync = rpc.GetBlockCountAsync();
                rpc.SendBatch();
                var blockCount = blockCountAsync.GetAwaiter().GetResult();

                node.Restart();

                rpc             = rpc.PrepareBatch();
                blockCountAsync = rpc.GetBlockCountAsync();
                rpc.SendBatch();
                blockCount = blockCountAsync.GetAwaiter().GetResult();

                rpc = new RPCClient("bla:bla", "http://toto/", Network.RegTest);
            }
        }