Ejemplo n.º 1
0
        public void CanDownloadLastBlocks()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                var node = builder.CreateNode(false).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.TotalReadenBytes == end.TotalReadenBytes - begin.TotalReadenBytes);
                Assert.True(diff.TotalWrittenBytes == end.TotalWrittenBytes - begin.TotalWrittenBytes);

                Assert.True(blocks.Count == 100);
            }
        }
Ejemplo n.º 2
0
        public void CanAddNodes()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                var nodeA = builder.CreateNode();
                var nodeB = builder.CreateNode();
                builder.StartAll();
                var rpc = nodeA.CreateRPCClient();
                rpc.RemoveNode(nodeA.Endpoint);
                rpc.AddNode(nodeB.Endpoint);
                Thread.Sleep(500);
                var info = rpc.GetAddedNodeInfo(true);
                Assert.NotNull(info);
                Assert.NotEmpty(info);
                //For some reason this one does not pass anymore in 0.13.1
                //Assert.Equal(nodeB.Endpoint, info.First().Addresses.First().Address);
                var oneInfo = rpc.GetAddedNodeInfo(true, nodeB.Endpoint);
                Assert.NotNull(oneInfo);
                Assert.True(oneInfo.AddedNode.ToString() == nodeB.Endpoint.ToString());
                oneInfo = rpc.GetAddedNodeInfo(true, nodeA.Endpoint);
                Assert.Null(oneInfo);
                //rpc.RemoveNode(nodeB.Endpoint);
                //Thread.Sleep(500);
                //info = rpc.GetAddedNodeInfo(true);
                //Assert.Equal(0, info.Count());
            }
        }
Ejemplo n.º 3
0
        public void RPCSendRPCException()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.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");
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public void CanCancelConnection()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                var node = builder.CreateNode(false);
                CancellationTokenSource cts = new CancellationTokenSource();
                cts.Cancel();
                try
                {
                    var client = Node.Connect(Network.StratisMain, "127.0.0.1:" + node.ProtocolPort.ToString(), new NodeConnectionParameters()
                    {
                        ConnectCancellation = cts.Token
                    });
                    Assert.False(true, "Should have thrown");
                }
                catch (OperationCanceledException)
                {
                }
            }
        }
Ejemplo n.º 5
0
        public void RawTransactionIsConformsToRPC()
        {
            using (var builder = NodeBuilderStratis.Create())
            {
                var rpc = builder.CreateNode().CreateRPCClient();
                builder.StartAll();
                var tx = Transaction.Parse("01000000ac55a957010000000000000000000000000000000000000000000000000000000000000000ffffffff0401320103ffffffff010084d717000000001976a9143ac0dad2ad42e35fcd745d7511d47c24ad6580b588ac00000000");

                var tx2 = rpc.GetRawTransaction(uint256.Parse("a6783a0933942d37dcb5fb923ddd343522036de23fbc658f2ad2a9f1428ca19d"));
                Assert.Equal(tx.GetHash(), tx2.GetHash());
            }
        }
Ejemplo n.º 6
0
        public void CanSendCommand()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                var rpc = builder.CreateNode().CreateRPCClient();
                builder.StartAll();
                var response = rpc.SendCommand(RPCOperations.getinfo);
                Assert.NotNull(response.Result);
            }
        }
Ejemplo n.º 7
0
        public void CanGetTransactionBlockFromRPC()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                var rpc = builder.CreateNode().CreateRPCClient();
                builder.StartAll();
                var blockId = rpc.GetBestBlockHash();
                var block   = rpc.GetRPCBlock(blockId).Result;
                Assert.NotNull(block);
            }
        }
Ejemplo n.º 8
0
        public void CanGetTransactionBlockFromRPC()
        {
            if (noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                RPCClient rpc = builder.CreateNode().CreateRPCClient();
                builder.StartAll();
                uint256  blockId = rpc.GetBestBlockHash();
                RPCBlock block   = rpc.GetRPCBlockAsync(blockId).Result;
                Assert.NotNull(block);
            }
        }
Ejemplo n.º 9
0
        public void CanUseAsyncRPC()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                var node = builder.CreateNode();
                var rpc  = node.CreateRPCClient();
                builder.StartAll();
                ////node.Generate(10);
                var blkCount = rpc.GetBlockCountAsync().Result;
                Assert.True(blkCount > 10);
            }
        }
Ejemplo n.º 10
0
        public void InvalidCommandSendRPCException()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                var rpc = builder.CreateNode().CreateRPCClient();
                builder.StartAll();
                AssertException <RPCException>(() => rpc.SendCommand("donotexist"), (ex) =>
                {
                    Assert.True(ex.RPCCode == RPCErrorCode.RPC_METHOD_NOT_FOUND);
                });
            }
        }
Ejemplo n.º 11
0
        public void CanGetGenesisFromRPC()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                var rpc = builder.CreateNode().CreateRPCClient();
                builder.StartAll();
                var response      = rpc.SendCommand(RPCOperations.getblockhash, 0);
                var actualGenesis = (string)response.Result;
                Assert.Equal(Network.StratisMain.GetGenesis().GetHash().ToString(), actualGenesis);
                //Assert.Equal(Network.StratisMain.GetGenesis().GetHash(), rpc.GetBestBlockHash());
            }
        }
Ejemplo n.º 12
0
        public void CanHandshake()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                var seed = builder.CreateNode(false).CreateNodeClient();
                Assert.True(seed.State == NodeState.Connected);
                seed.VersionHandshake();
                Assert.True(seed.State == NodeState.HandShaked);
                seed.Disconnect();
                Assert.True(seed.State == NodeState.Offline);
            }
        }
 public CoreNodeStratis(string folder, NodeBuilderStratis builder)
 {
     this._Builder = builder;
     this._Folder  = folder;
     //_State = CoreNodeState.Stopped;
     //CleanFolder();
     //Directory.CreateDirectory(folder);
     //dataDir = Path.Combine(folder, "data");
     //Directory.CreateDirectory(dataDir);
     //var pass = Encoders.Hex.EncodeData(RandomUtils.GetBytes(20));
     //creds = new NetworkCredential(pass, pass);
     //_Config = Path.Combine(dataDir, "bitcoin.conf");
     //ConfigParameters.Import(builder.ConfigParameters);
     ports = new int[2];
     FindPorts(ports);
     //ports[1] = Network.StratisMain.RPCPort;
     //ports[0] = Network.StratisMain.DefaultPort;
 }
Ejemplo n.º 14
0
        public void CanGetBlockFromRPC()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                var rpc = builder.CreateNode().CreateRPCClient();
                builder.StartAll();
                var response = rpc.GetBlockHeader(0);
                AssertEx.CollectionEquals(Network.StratisMain.GetGenesis().Header.ToBytes(), response.ToBytes());

                response = rpc.GetBlockHeader(0);
                Assert.Equal(Network.StratisMain.GenesisHash, response.GetHash());
            }
        }
Ejemplo n.º 15
0
        public void CanGetRawMemPool()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                var node = builder.CreateNode();
                var rpc  = node.CreateRPCClient();
                builder.StartAll();
                ////node.Generate(101);
                //var txid = rpc.SendToAddress(new Key().PubKey.GetAddress(rpc.Network), Money.Coins(1.0m), "hello", "world");
                var ids = rpc.GetRawMempool();
                Assert.NotNull(ids);
                //Assert.Equal(txid, ids[0]);
            }
        }
Ejemplo n.º 16
0
        public void CanGetChainsConcurrently()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            var stop = uint256.Parse("c9920baf967a314bd123efa184d54d4b9e7460301e3f2e059bafc77c45d03017");

            using (var builder = NodeBuilderStratis.Create())
            {
                //bool generating = true;
                builder.CreateNode(false);
                //Task.Run(() =>
                //{
                //	builder.Nodes[0].Generate(600);
                //	generating = false;
                //});
                var node = builder.Nodes[0].CreateNodeClient();
                node.PollHeaderDelay = TimeSpan.FromSeconds(2);
                node.VersionHandshake();
                Random rand = new Random();
                Thread.Sleep(1000);
                var chains =
                    Enumerable.Range(0, 5)
                    .Select(_ => Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(rand.Next(0, 1000));
                    return(node.GetChain(stop));
                }))
                    .Select(t => t.Result)
                    .ToArray();
                //while (generating)
                //{
                //	SyncAll(node, rand, chains);
                //}
                SyncAll(node, rand, chains);
                foreach (var c in chains)
                {
                    Assert.Equal(500, c.Height);
                }
            }
        }
Ejemplo n.º 17
0
        public void CanGetTransactionsFromMemPool()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                var node = builder.CreateNode();
                node.ConfigParameters.Add("whitelist", "127.0.0.1");
                //node.Start();
                //node.Generate(101);
                //node.CreateRPCClient().SendToAddress(new Key().PubKey.GetAddress(Network.StratisMain), Money.Coins(1.0m));
                var client = node.CreateNodeClient();
                client.VersionHandshake();
                var transactions = client.GetMempoolTransactions();
                Assert.NotNull(transactions);                //.True(transactions.Length == 1);
            }
        }
Ejemplo n.º 18
0
        public void CanGetMemPool()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                var node = builder.CreateNode();
                node.ConfigParameters.Add("whitelist", "127.0.0.1");
                //node.Start();
                //node.Generate(102);
                //for (int i = 0; i < 2; i++)
                //	node.CreateRPCClient().SendToAddress(new Key().PubKey.GetAddress(Network.RegTest), Money.Coins(1.0m));
                var client = node.CreateNodeClient();
                var txIds  = client.GetMempool();
                Assert.NotNull(txIds);                 //.True(txIds.Length == 2);
            }
        }
Ejemplo n.º 19
0
        public void CanGetPeersInfo()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                var nodeA = builder.CreateNode();
                builder.StartAll();
                var rpc = nodeA.CreateRPCClient();
                using (var node = nodeA.CreateNodeClient())
                {
                    node.VersionHandshake();
                    var peers = rpc.GetStratisPeersInfoAsync().Result;
                    Assert.NotEmpty(peers);
                }
            }
        }
Ejemplo n.º 20
0
        public void CanDownloadBlock()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                var node = builder.CreateNode(false).CreateNodeClient();
                node.VersionHandshake();
                node.SendMessageAsync(new GetDataPayload(new InventoryVector()
                {
                    Hash = Network.StratisMain.GenesisHash,
                    Type = InventoryType.MSG_BLOCK
                }));

                var block = node.ReceiveMessage <BlockPayload>();
                Assert.True(block.Object.CheckMerkleRoot());
            }
        }
Ejemplo n.º 21
0
        public void CanDownloadHeaders()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                var node = builder.CreateNode(false).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);
            }
        }
Ejemplo n.º 22
0
        public void CanDownloadBlocks()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                var node = builder.CreateNode(false).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);
            }
        }
Ejemplo n.º 23
0
        public void CanMaintainChainWithChainBehavior()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                var node = builder.CreateNode(false).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(Network.StratisMain))
                {
                    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.StratisMain));
                    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);
                    Thread.Sleep(5000);
                }
            }
        }
Ejemplo n.º 24
0
        public void CanGetBlocksWithProtocol()
        {
            if (pos_RPCClientTests.noClient)
            {
                return;
            }

            using (var builder = NodeBuilderStratis.Create())
            {
                var node = builder.CreateNode(false);
                //node.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), chain.GetBlock(50).HashBlock).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());
            }
        }
Ejemplo n.º 25
0
        //[Fact]
        //[Trait("Protocol", "Protocol")]
        public void CanGetMerkleRoot()
        {
            using (var builder = NodeBuilderStratis.Create())
            {
                var node = builder.CreateNode(false).CreateNodeClient();
                //builder.Nodes[0].Generate(101);
                var rpc = builder.Nodes[0].CreateRPCClient();
                builder.Nodes[0].Split(Money.Coins(50m), 50);
                builder.Nodes[0].SelectMempoolTransactions();
                builder.Nodes[0].Generate(1);
                for (int i = 0; i < 20; i++)
                {
                    rpc.SendToAddress(new Key().PubKey.GetAddress(rpc.Network), Money.Coins(0.5m));
                }
                builder.Nodes[0].SelectMempoolTransactions();
                builder.Nodes[0].Generate(1);
                var block        = builder.Nodes[0].CreateRPCClient().GetBlock(103);
                var knownTx      = block.Transactions[0].GetHash();
                var knownAddress = block.Transactions[0].Outputs[0].ScriptPubKey.GetDestination();
                node.VersionHandshake();
                using (var list = node.CreateListener()
                                  .Where(m => m.Message.Payload is MerkleBlockPayload || m.Message.Payload is TxPayload))
                {
                    BloomFilter filter = new BloomFilter(1, 0.005, 50, BloomFlags.UPDATE_NONE);
                    filter.Insert(knownAddress.ToBytes());
                    node.SendMessageAsync(new FilterLoadPayload(filter));
                    node.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() > 1);
                    Assert.True(tree.GetMatchedTransactions().Contains(knownTx));

                    List <Transaction> matched = new List <Transaction>();
                    for (int i = 0; i < tree.GetMatchedTransactions().Count(); i++)
                    {
                        matched.Add(list.ReceivePayload <TxPayload>().Object);
                    }
                    Assert.True(matched.Count > 1);
                    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");
                    node.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));
                }
            }
        }