public void CanDownloadLastBlocks()
        {
            using (var builder = NodeBuilder.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.TotalReadenBytes == end.TotalReadenBytes - begin.TotalReadenBytes);
                Assert.True(diff.TotalWrittenBytes == end.TotalWrittenBytes - begin.TotalWrittenBytes);

                Assert.True(blocks.Count == 100);
            }
        }
 public void CanGetChainsConcurrenty()
 {
     using (var builder = NodeBuilder.Create())
     {
         bool generating = true;
         builder.CreateNode(true);
         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());
         }))
             .Select(t => t.Result)
             .ToArray();
         while (generating)
         {
             SyncAll(node, rand, chains);
         }
         SyncAll(node, rand, chains);
         foreach (var c in chains)
         {
             Assert.Equal(600, c.Height);
         }
     }
 }
Exemple #3
0
        public void CanSyncWalletCore(NodeBuilder builder, CoreNode walletNode, WalletCreation creation)
        {
            var        rpc = builder.Nodes[0].CreateRPCClient();
            var        notifiedTransactions = new List <WalletTransaction>();
            NodesGroup connected            = CreateGroup(builder, new List <CoreNode>(new[] { walletNode }), 1);
            Wallet     wallet = new Wallet(creation, keyPoolSize: 11);

            wallet.NewWalletTransaction += (s, a) => notifiedTransactions.Add(a);
            Assert.True(wallet.State == WalletState.Created);
            wallet.Configure(connected);
            wallet.Connect();
            Assert.True(wallet.State == WalletState.Disconnected);
            TestUtils.Eventually(() => connected.ConnectedNodes.Count == 1);
            Assert.True(wallet.State == WalletState.Connected);

            TestUtils.Eventually(() => wallet.Chain.Height == rpc.GetBlockCount());
            for (int i = 0; i < 9; i++)
            {
                wallet.GetNextScriptPubKey();
            }
            wallet.GetNextScriptPubKey();             //Should provoke purge
            TestUtils.Eventually(() => wallet.State == WalletState.Disconnected && wallet.ConnectedNodes == 0);
            Thread.Sleep(100);
            TestUtils.Eventually(() => wallet.ConnectedNodes == 1);

            var k = wallet.GetNextScriptPubKey();

            Assert.NotNull(wallet.GetKeyPath(k));
            if (creation.UseP2SH)
            {
                var p2sh = k.GetDestinationAddress(Network.TestNet) as BitcoinScriptAddress;
                Assert.NotNull(p2sh);
                var redeem = wallet.GetRedeemScript(p2sh);
                Assert.NotNull(redeem);
                Assert.Equal(redeem.Hash, p2sh.Hash);
            }

            Assert.Equal(creation.UseP2SH, k.GetDestinationAddress(Network.TestNet) is BitcoinScriptAddress);
            builder.Nodes[0].GiveMoney(k, Money.Coins(1.0m));
            TestUtils.Eventually(() => wallet.GetTransactions().Count == 1 &&
                                 notifiedTransactions.Count == 1);
            builder.Nodes[0].FindBlock();
            TestUtils.Eventually(() => wallet.GetTransactions().Where(t => t.BlockInformation != null).Count() == 1 &&
                                 notifiedTransactions.Count == 2);
            builder.Nodes[0].GiveMoney(k, Money.Coins(1.5m), false);
            builder.Nodes[0].FindBlock();
            TestUtils.Eventually(() => wallet.GetTransactions().Summary.Confirmed.TransactionCount == 2 &&
                                 notifiedTransactions.Count == 3);

            builder.Nodes[0].FindBlock(30);
            Assert.True(wallet.GetTransactions().Summary.Confirmed.TransactionCount == 2);
            builder.Nodes[0].GiveMoney(k, Money.Coins(0.001m), false);
            Assert.True(wallet.GetTransactions().Summary.Confirmed.TransactionCount == 2);
            builder.Nodes[0].FindBlock(1, false);
            Assert.True(wallet.GetTransactions().Summary.Confirmed.TransactionCount == 2);
            builder.Nodes[0].FindBlock();
            //Sync automatically
            TestUtils.Eventually(() => wallet.GetTransactions().Summary.Confirmed.TransactionCount == 3);

            //Save and restore wallet
            MemoryStream ms = new MemoryStream();

            wallet.Save(ms);
            ms.Position = 0;
            var wallet2 = Wallet.Load(ms);

            //////

            //Save and restore tracker
            ms = new MemoryStream();
            var tracker = connected.NodeConnectionParameters.TemplateBehaviors.Find <TrackerBehavior>();

            tracker.Tracker.Save(ms);
            ms.Position = 0;
            connected   = CreateGroup(builder, new List <CoreNode>(new[] { walletNode }), 1);
            tracker     = new TrackerBehavior(Tracker.Load(ms), wallet.Chain);
            connected.NodeConnectionParameters.TemplateBehaviors.Add(tracker);
            //////

            wallet2.Configure(connected);
            wallet2.Connect();
            Assert.Equal(wallet.Created, wallet2.Created);
            Assert.Equal(wallet.GetNextScriptPubKey(), wallet2.GetNextScriptPubKey());
            Assert.True(wallet.GetKnownScripts().Length == wallet2.GetKnownScripts().Length);
            TestUtils.Eventually(() => wallet2.GetTransactions().Summary.Confirmed.TransactionCount == 3);

            //TestUtils.Eventually(() =>
            //{
            //    var fork = wallet.Chain.FindFork(wallet2._ScanLocation);
            //    return fork.Height == rpc.GetBlockCount();
            //});

            wallet2.Disconnect();
            wallet.Disconnect();
            connected.Disconnect();
        }
        public void CanGetMerkleRoot()
        {
            using (var builder = NodeBuilder.Create())
            {
                var node = builder.CreateNode(true).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));
                }
            }
        }
Exemple #5
0
        public void CanSyncWallet2()
        {
            using (var builder = NodeBuilder.Create())
            {
                NodesGroup aliceConnection = CreateGroup(builder, 1);
                NodesGroup bobConnection   = CreateGroup(builder, new[] { builder.Nodes[0] }, 1);
                builder.Nodes[0].Generate(101);
                var    rpc      = builder.Nodes[0].CreateRPCClient();
                var    aliceKey = new ExtKey();
                Wallet alice    = new Wallet(new WalletCreation()
                {
                    Network           = Network.RegTest,
                    RootKeys          = new[] { aliceKey.Neuter() },
                    SignatureRequired = 1,
                    UseP2SH           = false
                }, 11);
                Wallet bob = new Wallet(new WalletCreation()
                {
                    Network           = Network.RegTest,
                    RootKeys          = new[] { new ExtKey().Neuter() },
                    SignatureRequired = 1,
                    UseP2SH           = false
                }, 11);

                alice.Configure(aliceConnection);
                alice.Connect();

                bob.Configure(bobConnection);
                bob.Connect();

                TestUtils.Eventually(() => aliceConnection.ConnectedNodes.Count == 1);

                //New address tracked
                var addressAlice = alice.GetNextScriptPubKey();
                builder.Nodes[0].GiveMoney(addressAlice, Money.Coins(1.0m));
                TestUtils.Eventually(() => aliceConnection.ConnectedNodes.Count == 1);                 //Reconnect
                //////

                TestUtils.Eventually(() => alice.GetTransactions().Count == 1);

                //Alice send tx to bob
                var coins = alice.GetTransactions().GetSpendableCoins();
                var keys  = coins.Select(c => alice.GetKeyPath(c.ScriptPubKey))
                            .Select(k => aliceKey.Derive(k))
                            .ToArray();
                var txBuilder = new TransactionBuilder();
                var tx        =
                    txBuilder
                    .SetTransactionPolicy(new Policy.StandardTransactionPolicy()
                {
                    MinRelayTxFee = new FeeRate(0)
                })
                    .AddCoins(coins)
                    .AddKeys(keys)
                    .Send(bob.GetNextScriptPubKey(), Money.Coins(0.4m))
                    .SetChange(alice.GetNextScriptPubKey(true))
                    .SendFees(Money.Coins(0.0001m))
                    .BuildTransaction(true);

                Assert.True(txBuilder.Verify(tx));

                builder.Nodes[0].Broadcast(tx);

                //Alice get change
                TestUtils.Eventually(() => alice.GetTransactions().Count == 2);
                coins = alice.GetTransactions().GetSpendableCoins();
                Assert.True(coins.Single().Amount == Money.Coins(0.5999m));
                //////

                //Bob get coins
                TestUtils.Eventually(() => bob.GetTransactions().Count == 1);
                coins = bob.GetTransactions().GetSpendableCoins();
                Assert.True(coins.Single().Amount == Money.Coins(0.4m));
                //////


                MemoryStream bobWalletBackup = new MemoryStream();
                bob.Save(bobWalletBackup);
                bobWalletBackup.Position = 0;

                MemoryStream bobTrakerBackup = new MemoryStream();
                bob.Tracker.Save(bobTrakerBackup);
                bobTrakerBackup.Position = 0;

                bob.Disconnect();

                //Restore bob
                bob = Wallet.Load(bobWalletBackup);
                bobConnection.NodeConnectionParameters.TemplateBehaviors.Remove <TrackerBehavior>();
                bobConnection.NodeConnectionParameters.TemplateBehaviors.Add(new TrackerBehavior(Tracker.Load(bobTrakerBackup), alice.Chain));
                /////

                bob.Configure(bobConnection);

                //Bob still has coins
                TestUtils.Eventually(() => bob.GetTransactions().Count == 1);
                coins = bob.GetTransactions().GetSpendableCoins();
                Assert.True(coins.Single().Amount == Money.Coins(0.4m));
                //////

                bob.Connect();
                TestUtils.Eventually(() => bobConnection.ConnectedNodes.Count == 1);

                //New block found !
                builder.Nodes[0].SelectMempoolTransactions();
                builder.Nodes[0].Generate(1);

                //Alice send tx to bob
                coins = alice.GetTransactions().GetSpendableCoins();
                keys  = coins.Select(c => alice.GetKeyPath(c.ScriptPubKey))
                        .Select(k => aliceKey.Derive(k))
                        .ToArray();
                txBuilder = new TransactionBuilder();
                tx        =
                    txBuilder
                    .SetTransactionPolicy(new Policy.StandardTransactionPolicy()
                {
                    MinRelayTxFee = new FeeRate(0)
                })
                    .AddCoins(coins)
                    .AddKeys(keys)
                    .Send(bob.GetNextScriptPubKey(), Money.Coins(0.1m))
                    .SetChange(alice.GetNextScriptPubKey(true))
                    .SendFees(Money.Coins(0.0001m))
                    .BuildTransaction(true);

                Assert.True(txBuilder.Verify(tx));

                builder.Nodes[0].Broadcast(tx);

                //Bob still has coins
                TestUtils.Eventually(() => bob.GetTransactions().Count == 2);                 //Bob has both, old and new tx
                coins = bob.GetTransactions().GetSpendableCoins();
                //////
            }
        }