Exemplo n.º 1
0
 public void CanGetTransactionsFromMemPool()
 {
     using (var builder = NodeBuilderEx.Create())
     {
         var node = builder.CreateNode();
         node.ConfigParameters.Add("whitelist", "127.0.0.1");
         node.Start();
         var rpc = node.CreateRPCClient();
         rpc.Generate(101);
         rpc.SendCommand("sendtoaddress", new Key().PubKey.GetAddress(Network.RegTest).ToString(), "1.0");
         var client = node.CreateNodeClient();
         client.VersionHandshake();
         var transactions = client.GetMempoolTransactions();
         Assert.True(transactions.Length == 1);
     }
 }
Exemplo n.º 2
0
 public void CanDownloadHeaders()
 {
     using (var builder = NodeBuilderEx.Create())
     {
         var node = builder.CreateNode(true).CreateNodeClient();
         builder.Nodes[0].Generate(50);
         node.VersionHandshake();
         var begin  = node.Counter.Snapshot();
         var result = node.GetChain();
         var end    = node.Counter.Snapshot();
         var diff   = end - begin;
         Assert.True(node.PeerVersion.StartHeight <= result.Height);
         var subChain = node.GetChain(result.GetBlock(10).HashBlock);
         Assert.Equal(10, subChain.Height);
     }
 }
Exemplo n.º 3
0
        public void CanDownloadBlock()
        {
            using (var builder = NodeBuilderEx.Create())
            {
                var node = builder.CreateNode(true).CreateNodeClient();
                node.VersionHandshake();
                node.SendMessageAsync(new GetDataPayload(new InventoryVector
                {
                    Hash = Network.RegTest.GenesisHash,
                    Type = InventoryType.MSG_BLOCK
                }));

                var block = node.ReceiveMessage <BlockPayload>();
                Assert.True(block.Object.CheckMerkleRoot());
            }
        }
Exemplo n.º 4
0
        public void CanSyncWithPoW()
        {
            using (var builder = NodeBuilderEx.Create())
            {
                var node = builder.CreateNode();
                builder.StartAll();
                node.Generate(100);

                var nodeClient = node.CreateNodeClient();
                nodeClient.VersionHandshake();
                var chain = new ConcurrentChain(builder.Network);
                nodeClient.SynchronizeChain(chain, new SynchronizeChainOptions {
                    SkipPoWCheck = false
                });
                Assert.Equal(100, chain.Height);
            }
        }
Exemplo n.º 5
0
        public void CanDownloadBlocks()
        {
            using (var builder = NodeBuilderEx.Create())
            {
                var node = builder.CreateNode(true).CreateNodeClient();
                builder.Nodes[0].Generate(50);
                var chain = node.GetChain();
                chain.SetTip(chain.GetBlock(9));
                var blocks = node.GetBlocks(chain.ToEnumerable(true).Select(c => c.HashBlock)).ToList();
                foreach (var block in blocks)
                {
                    Assert.True(block.CheckMerkleRoot());
                }

                Assert.Equal(10, blocks.Count);
            }
        }
Exemplo n.º 6
0
        public void CanGetChainsConcurrenty()
        {
            using (var builder = NodeBuilderEx.Create())
            {
                var generating = true;
                var node       = builder.CreateNode(true);
                var rpc        = node.CreateRPCClient();
                Task.Run(() =>
                {
                    rpc.Generate(600);
                    generating = false;
                });
                var nodeClient = node.CreateNodeClient();
                nodeClient._pollHeaderDelay = TimeSpan.FromSeconds(2);
                nodeClient.VersionHandshake();
                var rand = new Random();
                Thread.Sleep(1000);
                var chains =
                    Enumerable.Range(0, 5)
                    .Select(_ => Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(rand.Next(0, 1000));
                    return(nodeClient.GetChain());
                }))
                    .Select(t => t.Result)
                    .ToArray();
                while (generating)
                {
                    SyncAll(nodeClient, rand, chains);
                }

                SyncAll(nodeClient, rand, chains);
                foreach (var c in chains)
                {
                    Assert.Equal(600, c.Height);
                }

                var chainNoHeader = nodeClient.GetChain(new SynchronizeChainOptions
                {
                    SkipPoWCheck = true, StripHeaders = true
                });
                Assert.False(chainNoHeader.Tip.HasHeader);
            }
        }
Exemplo n.º 7
0
        public void CanGetBlocksWithProtocol()
        {
            using (var builder = NodeBuilderEx.Create())
            {
                var node = builder.CreateNode(true);
                var rpc  = node.CreateRPCClient();
                rpc.Generate(50);
                var client = node.CreateNodeClient();
                var chain  = client.GetChain();
                var blocks = client.GetBlocks(chain.GetBlock(20).HashBlock).ToArray();
                Assert.Equal(20, blocks.Length);
                Assert.Equal(chain.GetBlock(20).HashBlock, blocks.Last().Header.GetHash());

                blocks = client.GetBlocksFromFork(chain.GetBlock(45)).ToArray();
                Assert.Equal(5, blocks.Length);
                Assert.Equal(chain.GetBlock(50).HashBlock, blocks.Last().Header.GetHash());
                Assert.Equal(chain.GetBlock(46).HashBlock, blocks.First().Header.GetHash());
            }
        }
Exemplo n.º 8
0
        public void SynchronizeChainSurviveReorg()
        {
            using (var builder = NodeBuilderEx.Create())
            {
                var chain = new ConcurrentChain(Network.RegTest);
                var node1 = builder.CreateNode(true);
                node1.Generate(10);
                node1.CreateNodeClient().SynchronizeChain(chain);
                Assert.Equal(10, chain.Height);


                var node2 = builder.CreateNode(true);
                node2.Generate(12);

                var node2c = node2.CreateNodeClient();
                node2c._pollHeaderDelay = TimeSpan.FromSeconds(2);
                node2c.SynchronizeChain(chain);
                Assert.Equal(12, chain.Height);
            }
        }
Exemplo n.º 9
0
        public void CanGetMemPool()
        {
            using (var builder = NodeBuilderEx.Create())
            {
                var node = builder.CreateNode();
                var rpc  = node.CreateRPCClient();
                node.ConfigParameters.Add("whitelist", "127.0.0.1");
                node.Start();
                rpc.Generate(102);
                for (var i = 0; i < 2; i++)
                {
                    node.CreateRPCClient().SendCommand("sendtoaddress",
                                                       new Key().PubKey.GetAddress(Network.RegTest).ToString(), "1.0");
                }

                var client = node.CreateNodeClient();
                var txIds  = client.GetMempool();
                Assert.True(txIds.Length == 2);
            }
        }
Exemplo n.º 10
0
 public void CanCancelConnection()
 {
     using (var builder = NodeBuilderEx.Create())
     {
         var node = builder.CreateNode(true);
         var cts  = new CancellationTokenSource();
         cts.Cancel();
         try
         {
             var client = Node.Connect(Network.RegTest, "127.0.0.1:" + node.ProtocolPort,
                                       new NodeConnectionParameters
             {
                 ConnectCancellation = cts.Token
             });
             Assert.False(true, "Should have thrown");
         }
         catch (OperationCanceledException)
         {
         }
     }
 }
Exemplo n.º 11
0
        public void CanMaintainChainWithChainBehavior()
        {
            using (var builder = NodeBuilderEx.Create())
            {
                var node = builder.CreateNode(true).CreateNodeClient();
                builder.Nodes[0].Generate(600);
                var rpc   = builder.Nodes[0].CreateRPCClient();
                var chain = node.GetChain(rpc.GetBlockHash(500));
                Assert.True(chain.Height == 500);
                using (var tester = new NodeServerTester())
                {
                    var n1 = tester.Node1;
                    n1.Behaviors.Add(new ChainBehavior(chain));
                    n1.VersionHandshake();
                    Assert.True(n1.MyVersion.StartHeight == 500);
                    var n2 = tester.Node2;
                    Assert.True(n2.MyVersion.StartHeight == 0);
                    Assert.True(n2.PeerVersion.StartHeight == 500);
                    Assert.True(n1.State == NodeState.HandShaked);
                    Assert.True(n2.State == NodeState.HandShaked);
                    var behavior = new ChainBehavior(new ConcurrentChain(Network.RegTest));
                    n2.Behaviors.Add(behavior);
                    TestUtils.Eventually(() => behavior.Chain.Height == 500);
                    var chain2 = n2.GetChain(rpc.GetBlockHash(500));
                    Assert.True(chain2.Height == 500);
                    var chain1 = n1.GetChain(rpc.GetBlockHash(500));
                    Assert.True(chain1.Height == 500);
                    chain1 = n1.GetChain(rpc.GetBlockHash(499));
                    Assert.True(chain1.Height == 499);

                    //Should not broadcast above HighestValidatorPoW
                    n1.Behaviors.Find <ChainBehavior>().SharedState.HighestValidatedPoW = chain1.GetBlock(300);
                    chain1 = n2.GetChain(rpc.GetBlockHash(499));
                    Assert.True(chain1.Height == 300);
                }
            }
        }
Exemplo n.º 12
0
        public void CanDownloadLastBlocks()
        {
            using (var builder = NodeBuilderEx.Create())
            {
                var node = builder.CreateNode(true).CreateNodeClient();
                builder.Nodes[0].Generate(150);
                var chain = node.GetChain();

                Assert.True(node.PeerVersion.StartHeight <= chain.Height);

                var subChain = chain.ToEnumerable(true).Take(100).Select(s => s.HashBlock).ToArray();

                var begin  = node.Counter.Snapshot();
                var blocks = node.GetBlocks(subChain).Select(_ => 1).ToList();
                var end    = node.Counter.Snapshot();
                var diff   = end - begin;
                Assert.True(diff.Start == begin.Taken);
                Assert.True(diff.Taken == end.Taken);
                Assert.True(diff.TotalBytesRead == end.TotalBytesRead - begin.TotalBytesRead);
                Assert.True(diff.TotalBytesWritten == end.TotalBytesWritten - begin.TotalBytesWritten);

                Assert.True(blocks.Count == 100);
            }
        }
Exemplo n.º 13
0
        public void CanGetMerkleRoot()
        {
            using (var builder = NodeBuilderEx.Create())
            {
                var node       = builder.CreateNode(true);
                var rpc        = node.CreateRPCClient();
                var nodeClient = node.CreateNodeClient();
                rpc.Generate(101);

                var knownAddresses = new List <TxDestination>();
                var batch          = rpc.PrepareBatch();
                for (var i = 0; i < 20; i++)
                {
                    var address = new Key().PubKey.GetAddress(rpc.Network);
                    knownAddresses.Add(address.Hash);
#pragma warning disable CS4014
                    batch.SendCommandAsync("sendtoaddress", address.ToString(), "0.5");
#pragma warning restore CS4014
                }

                batch.SendBatch();
                knownAddresses = knownAddresses.Take(10).ToList();
                var blockId = rpc.Generate(1)[0];
                var block   = rpc.GetBlock(blockId);
                Assert.Equal(21, block.Transactions.Count);
                var knownTx = block.Transactions[1].GetHash();
                nodeClient.VersionHandshake();
                using (var list = nodeClient.CreateListener()
                                  .Where(m => m.Message.Payload is MerkleBlockPayload || m.Message.Payload is TxPayload))
                {
                    var filter = new BloomFilter(1, 0.0001, 50, BloomFlags.UPDATE_NONE);
                    foreach (var a in knownAddresses)
                    {
                        filter.Insert(a.ToBytes());
                    }

                    nodeClient.SendMessageAsync(new FilterLoadPayload(filter));
                    nodeClient.SendMessageAsync(
                        new GetDataPayload(new InventoryVector(InventoryType.MSG_FILTERED_BLOCK, block.GetHash())));
                    var merkle = list.ReceivePayload <MerkleBlockPayload>();
                    var tree   = merkle.Object.PartialMerkleTree;
                    Assert.True(tree.Check(block.Header.HashMerkleRoot));
                    Assert.True(tree.GetMatchedTransactions().Count() >= 10);
                    Assert.True(tree.GetMatchedTransactions().Contains(knownTx));

                    var matched = new List <Transaction>();
                    for (var i = 0; i < tree.GetMatchedTransactions().Count(); i++)
                    {
                        matched.Add(list.ReceivePayload <TxPayload>().Object);
                    }

                    Assert.True(matched.Count >= 10);
                    tree = tree.Trim(knownTx);
                    Assert.True(tree.GetMatchedTransactions().Count() == 1);
                    Assert.True(tree.GetMatchedTransactions().Contains(knownTx));

                    Action act = () =>
                    {
                        foreach (var match in matched)
                        {
                            Assert.True(filter.IsRelevantAndUpdate(match));
                        }
                    };
                    act();
                    filter = filter.Clone();
                    act();

                    var unknownBlock =
                        uint256.Parse("00000000ad262227291eaf90cafdc56a8f8451e2d7653843122c5bb0bf2dfcdd");
                    nodeClient.SendMessageAsync(new GetDataPayload(new InventoryVector(InventoryType.MSG_FILTERED_BLOCK,
                                                                                       Network.RegTest.GetGenesis().GetHash())));

                    merkle = list.ReceivePayload <MerkleBlockPayload>();
                    tree   = merkle.Object.PartialMerkleTree;
                    Assert.True(tree.Check(merkle.Object.Header.HashMerkleRoot));
                    Assert.True(!tree.GetMatchedTransactions().Contains(knownTx));
                }
            }
        }