public void TestSegwitActivation()
        {
            using (NodeBuilder builder = NodeBuilder.Create(version: "0.15.1"))
            {
                CoreNode coreNode = builder.CreateNode(false);

                coreNode.ConfigParameters.AddOrReplace("debug", "1");
                coreNode.ConfigParameters.AddOrReplace("printtoconsole", "0");
                coreNode.Start();

                CoreNode node1 = builder.CreateStratisPowNode(true, fullNodeBuilder =>
                {
                    fullNodeBuilder
                    .UseConsensus()
                    .UseBlockStore()
                    .UseMempool()
                    .UseBlockNotification()
                    .UseTransactionNotification()
                    .AddMining()
                    .UseWallet()
                    .UseApi()
                    .AddRPC();
                });

                WalletManager wm1 = node1.FullNode.NodeService <IWalletManager>() as WalletManager;
                wm1.CreateWallet("Test1", "alice1");

                RPCClient rpc1    = node1.CreateRPCClient();
                RPCClient coreRpc = coreNode.CreateRPCClient();

                coreRpc.AddNode(node1.Endpoint, false);
                rpc1.AddNode(coreNode.Endpoint, false);

                coreRpc.Generate(450);

                BIP9DeploymentsArray bip9Constants = node1.FullNode.Network.Consensus.BIP9Deployments;
                ConsensusLoop        consensusLoop = node1.FullNode.NodeService <ConsensusLoop>();
                ThresholdState[]     bip9State     = consensusLoop.NodeDeployments.BIP9.GetStates(node1.FullNode.Chain.Tip.Previous);

                Money     amount       = new Money(5.0m, MoneyUnit.BTC);
                HdAddress destination1 = wm1.GetUnusedAddress(new WalletAccountReference("alice1", "account 0"));

                coreRpc.SendToAddress(BitcoinAddress.Create(destination1.Address, Network.RegTest), amount);

                coreRpc.Generate(1);

                Assert.Equal(ThresholdState.Active, bip9State.GetValue((int)BIP9Deployments.Segwit));
            }
        }
예제 #2
0
        public void TestSegwit_AlwaysActivatedOn_StratisNode()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                CoreNode coreNode = builder.CreateBitcoinCoreNode(version: "0.18.0", useNewConfigStyle: true);
                coreNode.Start();

                CoreNode stratisNode = builder.CreateStratisPowNode(KnownNetworks.RegTest).Start();

                RPCClient stratisNodeRpc = stratisNode.CreateRPCClient();
                RPCClient coreRpc        = coreNode.CreateRPCClient();

                coreRpc.AddNode(stratisNode.Endpoint, false);
                stratisNodeRpc.AddNode(coreNode.Endpoint, false);

                coreRpc.Generate(1);
                var cancellationToken = new CancellationTokenSource(TimeSpan.FromMinutes(1)).Token;
                TestBase.WaitLoop(() => stratisNode.CreateRPCClient().GetBestBlockHash() == coreNode.CreateRPCClient().GetBestBlockHash(), cancellationToken: cancellationToken);

                var consensusLoop = stratisNode.FullNode.NodeService <IConsensusRuleEngine>() as ConsensusRuleEngine;
                ThresholdState[] segwitActiveState = consensusLoop.NodeDeployments.BIP9.GetStates(stratisNode.FullNode.ChainIndexer.GetHeader(1));

                // Check that segwit got activated at genesis.
                Assert.Equal(ThresholdState.Active, segwitActiveState.GetValue((int)BitcoinBIP9Deployments.Segwit));
            }
        }
        public void CanGetRawTransaction()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                CoreNode node = builder.CreateBitcoinCoreNode(version: "0.18.0", useNewConfigStyle: true).Start();

                RPCClient rpcClient = node.CreateRPCClient();

                rpcClient.Generate(101);

                CoreNode sfn = builder.CreateStratisPowNode(this.regTest);
                sfn.Start();
                TestHelper.ConnectAndSync(node, sfn);

                uint256 txid = rpcClient.SendToAddress(new Key().PubKey.GetAddress(rpcClient.Network), Money.Coins(1.0m));

                TestBase.WaitLoop(() => node.CreateRPCClient().GetRawMempool().Length == 1);

                Transaction tx = rpcClient.GetRawTransaction(txid);

                RPCClient sfnRpc = sfn.CreateRPCClient();

                TestBase.WaitLoop(() => sfnRpc.GetRawMempool().Length == 1);

                Transaction tx2 = sfnRpc.GetRawTransaction(txid);

                Assert.Equal(tx.ToHex(), tx2.ToHex());
            }
        }
예제 #4
0
        public void CanUseMultipleWallets()
        {
            using (var builder = NodeBuilder.Create(version: "0.15.0"))
            {
                var node = builder.CreateNode();
                node.ConfigParameters.Add("wallet", "w1");
                //node.ConfigParameters.Add("wallet", "w2");
                node.Start();
                var rpc   = node.CreateRPCClient();
                var creds = RPCCredentialString.Parse(rpc.CredentialString.ToString());
                creds.Server     = rpc.Address.AbsoluteUri;
                creds.WalletName = "w1";
                rpc = new RPCClient(creds, Network.RegTest);
                rpc.SendCommandAsync(RPCOperations.getwalletinfo).GetAwaiter().GetResult().ThrowIfError();
                Assert.NotNull(rpc.GetBalance());
                Assert.NotNull(rpc.GetBestBlockHash());
                var block = rpc.GetBlock(rpc.Generate(1)[0]);

                rpc = rpc.PrepareBatch();
                var b  = rpc.GetBalanceAsync();
                var b2 = rpc.GetBestBlockHashAsync();
                var a  = rpc.SendCommandAsync("gettransaction", block.Transactions.First().GetHash().ToString());
                rpc.SendBatch();
                b.GetAwaiter().GetResult();
                b2.GetAwaiter().GetResult();
                a.GetAwaiter().GetResult();
            }
        }
예제 #5
0
        public void TestSegwit_MinedOnCore_ActivatedOn_StratisNode()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                CoreNode coreNode = builder.CreateBitcoinCoreNode(version: "0.15.1");

                coreNode.ConfigParameters.AddOrReplace("debug", "1");
                coreNode.ConfigParameters.AddOrReplace("printtoconsole", "0");
                coreNode.Start();

                CoreNode stratisNode = builder.CreateStratisPowNode(KnownNetworks.RegTest);
                stratisNode.Start();

                RPCClient stratisNodeRpc = stratisNode.CreateRPCClient();
                RPCClient coreRpc        = coreNode.CreateRPCClient();

                coreRpc.AddNode(stratisNode.Endpoint, false);
                stratisNodeRpc.AddNode(coreNode.Endpoint, false);

                // core (in version 0.15.1) only mines segwit blocks above a certain height on regtest
                // future versions of core will change that behaviour so this test may need to be changed in the future
                // see issue for more details https://github.com/stratisproject/StratisBitcoinFullNode/issues/1028
                BIP9DeploymentsParameters prevSegwitDeployment = KnownNetworks.RegTest.Consensus.BIP9Deployments[BIP9Deployments.Segwit];
                KnownNetworks.RegTest.Consensus.BIP9Deployments[BIP9Deployments.Segwit] = new BIP9DeploymentsParameters(1, 0, DateTime.Now.AddDays(50).ToUnixTimestamp());

                try
                {
                    // generate 450 blocks, block 431 will be segwit activated.
                    coreRpc.Generate(450);
                    var cancellationToken = new CancellationTokenSource(TimeSpan.FromMinutes(2)).Token;
                    TestHelper.WaitLoop(() => stratisNode.CreateRPCClient().GetBestBlockHash() == coreNode.CreateRPCClient().GetBestBlockHash(), cancellationToken: cancellationToken);

                    // segwit activation on Bitcoin regtest.
                    // - On regtest deployment state changes every 144 block, the threshold for activating a rule is 108 blocks.
                    // segwit deployment status should be:
                    // - Defined up to block 142.
                    // - Started at block 143 to block 286 .
                    // - LockedIn 287 (as segwit should already be signaled in blocks).
                    // - Active at block 431.

                    var consensusLoop = stratisNode.FullNode.NodeService <IConsensusRuleEngine>() as ConsensusRuleEngine;
                    ThresholdState[] segwitDefinedState  = consensusLoop.NodeDeployments.BIP9.GetStates(stratisNode.FullNode.Chain.GetBlock(142));
                    ThresholdState[] segwitStartedState  = consensusLoop.NodeDeployments.BIP9.GetStates(stratisNode.FullNode.Chain.GetBlock(143));
                    ThresholdState[] segwitLockedInState = consensusLoop.NodeDeployments.BIP9.GetStates(stratisNode.FullNode.Chain.GetBlock(287));
                    ThresholdState[] segwitActiveState   = consensusLoop.NodeDeployments.BIP9.GetStates(stratisNode.FullNode.Chain.GetBlock(431));

                    // check that segwit is got activated at block 431
                    Assert.Equal(ThresholdState.Defined, segwitDefinedState.GetValue((int)BIP9Deployments.Segwit));
                    Assert.Equal(ThresholdState.Started, segwitStartedState.GetValue((int)BIP9Deployments.Segwit));
                    Assert.Equal(ThresholdState.LockedIn, segwitLockedInState.GetValue((int)BIP9Deployments.Segwit));
                    Assert.Equal(ThresholdState.Active, segwitActiveState.GetValue((int)BIP9Deployments.Segwit));
                }
                finally
                {
                    KnownNetworks.RegTest.Consensus.BIP9Deployments[BIP9Deployments.Segwit] = prevSegwitDeployment;
                }
            }
        }
예제 #6
0
        public void TestSegwit_MinedOnCore_ActivatedOn_StratisNode()
        {
            // This test only verifies that the BIP9 machinery is operating correctly on the Stratis PoW node.
            // Since newer versions of Bitcoin Core have segwit always activated from genesis, there is no need to
            // perform the reverse version of this test. Much more important are the P2P and mempool tests for
            // segwit transactions.

            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                CoreNode coreNode = builder.CreateBitcoinCoreNode(version: "0.15.1");
                coreNode.Start();

                CoreNode stratisNode = builder.CreateStratisPowNode(KnownNetworks.RegTest).Start();

                RPCClient stratisNodeRpc = stratisNode.CreateRPCClient();
                RPCClient coreRpc        = coreNode.CreateRPCClient();

                coreRpc.AddNode(stratisNode.Endpoint, false);
                stratisNodeRpc.AddNode(coreNode.Endpoint, false);

                // Core (in version 0.15.1) only mines segwit blocks above a certain height on regtest
                // See issue for more details https://github.com/stratisproject/StratisBitcoinFullNode/issues/1028
                BIP9DeploymentsParameters prevSegwitDeployment = KnownNetworks.RegTest.Consensus.BIP9Deployments[BitcoinBIP9Deployments.Segwit];
                KnownNetworks.RegTest.Consensus.BIP9Deployments[BitcoinBIP9Deployments.Segwit] = new BIP9DeploymentsParameters("Test", 1, 0, DateTime.Now.AddDays(50).ToUnixTimestamp(), BIP9DeploymentsParameters.DefaultRegTestThreshold);

                try
                {
                    // Generate 450 blocks, block 431 will be segwit activated.
                    coreRpc.Generate(450);
                    var cancellationToken = new CancellationTokenSource(TimeSpan.FromMinutes(2)).Token;
                    TestBase.WaitLoop(() => stratisNode.CreateRPCClient().GetBestBlockHash() == coreNode.CreateRPCClient().GetBestBlockHash(), cancellationToken: cancellationToken);

                    // Segwit activation on Bitcoin regtest.
                    // - On regtest deployment state changes every 144 blocks, the threshold for activating a rule is 108 blocks.
                    // Segwit deployment status should be:
                    // - Defined up to block 142.
                    // - Started at block 143 to block 286.
                    // - LockedIn 287 (as segwit should already be signaled in blocks).
                    // - Active at block 431.

                    var consensusLoop = stratisNode.FullNode.NodeService <IConsensusRuleEngine>() as ConsensusRuleEngine;
                    ThresholdState[] segwitDefinedState  = consensusLoop.NodeDeployments.BIP9.GetStates(stratisNode.FullNode.ChainIndexer.GetHeader(142));
                    ThresholdState[] segwitStartedState  = consensusLoop.NodeDeployments.BIP9.GetStates(stratisNode.FullNode.ChainIndexer.GetHeader(143));
                    ThresholdState[] segwitLockedInState = consensusLoop.NodeDeployments.BIP9.GetStates(stratisNode.FullNode.ChainIndexer.GetHeader(287));
                    ThresholdState[] segwitActiveState   = consensusLoop.NodeDeployments.BIP9.GetStates(stratisNode.FullNode.ChainIndexer.GetHeader(431));

                    // Check that segwit got activated at block 431.
                    Assert.Equal(ThresholdState.Defined, segwitDefinedState.GetValue((int)BitcoinBIP9Deployments.Segwit));
                    Assert.Equal(ThresholdState.Started, segwitStartedState.GetValue((int)BitcoinBIP9Deployments.Segwit));
                    Assert.Equal(ThresholdState.LockedIn, segwitLockedInState.GetValue((int)BitcoinBIP9Deployments.Segwit));
                    Assert.Equal(ThresholdState.Active, segwitActiveState.GetValue((int)BitcoinBIP9Deployments.Segwit));
                }
                finally
                {
                    KnownNetworks.RegTest.Consensus.BIP9Deployments[BitcoinBIP9Deployments.Segwit] = prevSegwitDeployment;
                }
            }
        }
        public void CanSignRawTransaction()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                CoreNode node = builder.CreateBitcoinCoreNode(version: "0.18.0", useNewConfigStyle: true).Start();

                CoreNode sfn = builder.CreateStratisPowNode(this.regTest).WithWallet().Start();

                TestHelper.ConnectAndSync(node, sfn);

                RPCClient rpcClient = node.CreateRPCClient();
                RPCClient sfnRpc    = sfn.CreateRPCClient();

                // Need one block per node so they can each fund a transaction.
                rpcClient.Generate(1);

                TestHelper.ConnectAndSync(node, sfn);

                sfnRpc.Generate(1);

                TestHelper.ConnectAndSync(node, sfn);

                // And then enough blocks mined on top for the coinbases to mature.
                rpcClient.Generate(101);

                TestHelper.ConnectAndSync(node, sfn);

                var tx = new Transaction();
                tx.Outputs.Add(new TxOut(Money.Coins(1.0m), new Key()));
                FundRawTransactionResponse funded = rpcClient.FundRawTransaction(tx);

                // signrawtransaction was removed in 0.18. So just use its equivalent so that we can test SFN's ability to call signrawtransaction.
                RPCResponse response = rpcClient.SendCommand("signrawtransactionwithwallet", tx.ToHex());

                Assert.NotNull(response.Result["hex"]);

                sfnRpc.WalletPassphrase(sfn.WalletPassword, 60);

                tx = new Transaction();
                tx.Outputs.Add(new TxOut(Money.Coins(1.0m), new Key()));
                funded = sfnRpc.FundRawTransaction(tx);
                Transaction signed = sfnRpc.SignRawTransaction(funded.Transaction);
                rpcClient.SendRawTransaction(signed);
            }
        }
예제 #8
0
        public void TestRpcListUnspentForWatchOnlyIsSuccessful()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                CoreNode node  = builder.CreateStratisPowNode(new BitcoinRegTest()).AlwaysFlushBlocks().WithWallet().Start();
                CoreNode node2 = builder.CreateStratisPowNode(new BitcoinRegTest()).WithReadyBlockchainData(ReadyBlockchain.BitcoinRegTest10Miner).Start();

                TestHelper.ConnectAndSync(node, node2);

                UnspentOutputReference tx = node2.FullNode.WalletManager().GetUnspentTransactionsInWallet("mywallet", 0, Features.Wallet.Wallet.NormalAccounts).First();

                RPCClient rpc = node.CreateRPCClient();

                PubKey pubKey = PayToPubkeyTemplate.Instance.ExtractScriptPubKeyParameters(tx.Address.Pubkey);

                Assert.Throws <RPCException>(() => rpc.SendCommand(RPCOperations.gettransaction, tx.Transaction.Id.ToString(), true));

                rpc.ImportPubKey(pubKey.ToHex());

                // ListUnspent will not regard the outputs as spendable if they are not sufficiently mature.
                rpc.Generate((int)node.FullNode.Network.Consensus.CoinbaseMaturity);

                TestBase.WaitLoop(() => node.FullNode.WalletManager().WalletTipHeight == node2.FullNode.WalletManager().WalletTipHeight);

                TestBase.WaitLoop(() =>
                {
                    try
                    {
                        // Wait until gettransaction can find the transaction in the watch only account.
                        RPCResponse walletTx = rpc.SendCommand(RPCOperations.gettransaction, tx.Transaction.Id.ToString(), true);

                        return(walletTx != null);
                    }
                    catch (RPCException)
                    {
                        return(false);
                    }
                });

                UnspentCoin[] unspent = rpc.ListUnspent(1, 9999999);

                bool found = false;

                foreach (UnspentCoin coin in unspent)
                {
                    if (coin.OutPoint == tx.ToOutPoint())
                    {
                        found = true;
                    }

                    Assert.Equal(coin.Account, Features.Wallet.Wallet.WatchOnlyAccountName);
                }

                Assert.True(found);
            }
        }
        public void CanUseBatchedRequests()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15);
                builder.StartAll();

                RPCClient rpcClient = node.CreateRPCClient();

                uint256[] blocks = rpcClient.Generate(10);
                Assert.Throws <InvalidOperationException>(() => rpcClient.SendBatch());
                rpcClient = rpcClient.PrepareBatch();
                var requests = new List <Task <uint256> >();
                for (int i = 1; i < 11; i++)
                {
                    requests.Add(rpcClient.GetBlockHashAsync(i));
                }
                Thread.Sleep(1000);
                foreach (Task <uint256> req in requests)
                {
                    Assert.Equal(TaskStatus.WaitingForActivation, req.Status);
                }
                rpcClient.SendBatch();
                rpcClient = rpcClient.PrepareBatch();
                int blockIndex = 0;
                foreach (Task <uint256> req in requests)
                {
                    Assert.Equal(blocks[blockIndex], req.Result);
                    Assert.Equal(TaskStatus.RanToCompletion, req.Status);
                    blockIndex++;
                }
                requests.Clear();

                requests.Add(rpcClient.GetBlockHashAsync(10));
                requests.Add(rpcClient.GetBlockHashAsync(11));
                requests.Add(rpcClient.GetBlockHashAsync(9));
                requests.Add(rpcClient.GetBlockHashAsync(8));
                rpcClient.SendBatch();
                rpcClient = rpcClient.PrepareBatch();
                Assert.Equal(TaskStatus.RanToCompletion, requests[0].Status);
                Assert.Equal(TaskStatus.Faulted, requests[1].Status);
                Assert.Equal(TaskStatus.RanToCompletion, requests[2].Status);
                Assert.Equal(TaskStatus.RanToCompletion, requests[3].Status);
                requests.Clear();

                requests.Add(rpcClient.GetBlockHashAsync(10));
                requests.Add(rpcClient.GetBlockHashAsync(11));
                rpcClient.CancelBatch();
                rpcClient = rpcClient.PrepareBatch();
                Thread.Sleep(100);
                Assert.Equal(TaskStatus.Canceled, requests[0].Status);
                Assert.Equal(TaskStatus.Canceled, requests[1].Status);
            }
        }
예제 #10
0
        public void TestRpcGetTransactionIsSuccessful()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                CoreNode node = builder.CreateStratisPowNode(new BitcoinRegTest()).WithWallet().Start();

                RPCClient   rpc       = node.CreateRPCClient();
                uint256     blockHash = rpc.Generate(1)[0];
                Block       block     = rpc.GetBlock(blockHash);
                RPCResponse walletTx  = rpc.SendCommand(RPCOperations.gettransaction, block.Transactions[0].GetHash().ToString());
                walletTx.ThrowIfError();
            }
        }
예제 #11
0
        public void CanGetTxOutNoneFromRPC()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15).Start();

                RPCClient rpcClient = node.CreateRPCClient();

                uint256            txid        = rpcClient.Generate(1).Single();
                UnspentTransaction resultTxOut = rpcClient.GetTxOut(txid, 0, true);
                Assert.Null(resultTxOut);
            }
        }
예제 #12
0
        public void CanGetTxOutNoneFromRPC()
        {
            using (var builder = NodeBuilder.Create(this))
            {
                CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15);
                builder.StartAll();

                RPCClient rpcClient = node.CreateRPCClient();

                var txid        = rpcClient.Generate(1).Single();
                var resultTxOut = rpcClient.GetTxOut(txid, 0, true);
                Assert.Null(resultTxOut);
            }
        }
예제 #13
0
        public void CanSignRawTransaction()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                CoreNode node = builder.CreateBitcoinCoreNode(version: BitcoinCoreVersion15).Start();

                RPCClient rpcClient = node.CreateRPCClient();
                rpcClient.Generate(101);

                var tx = new Transaction();
                tx.Outputs.Add(new TxOut(Money.Coins(1.0m), new Key()));
                FundRawTransactionResponse funded = node.CreateRPCClient().FundRawTransaction(tx);
                Transaction signed = node.CreateRPCClient().SignRawTransaction(funded.Transaction);
                node.CreateRPCClient().SendRawTransaction(signed);
            }
        }
예제 #14
0
        static void Main(string[] args)
        {
            ExtKey         masterKey = new ExtKey();
            WalletCreation wc        = new WalletCreation
            {
                Network  = network,
                RootKeys = new [] { masterKey.Neuter() }
            };
            Wallet wallet = new Wallet(wc);

            wallet.Configure();

            NetworkAddress na = new NetworkAddress(IPAddress.Parse("127.0.0.1"), 18444);

            wallet.AddressManager.Add(na);
            wallet.Group.MaximumNodeConnection = 1;
            wallet.NewWalletTransaction       += (Wallet sender, WalletTransaction wtx) =>
            {
                WalletTransactionsCollection wtxc = wallet.GetTransactions();
                Console.WriteLine("wallet tx count => {0}", wtxc.Count);
                Console.WriteLine("immature => {0}", wtxc.Summary.Immature.Amount);
                Console.WriteLine("confirmed => {0}", wtxc.Summary.Confirmed.Amount);
                Console.WriteLine("unconfirmed => {0}", wtxc.Summary.UnConfirmed.Amount);
                Console.WriteLine("spendable => {0}", wtxc.Summary.Spendable.Amount);
            };
            wallet.Connect();

            BitcoinAddress addr = wallet.GetNextScriptPubKey().GetDestinationAddress(network);

            Console.WriteLine("receiver address => {0}", addr);

            Task.Run(() => {
                Console.WriteLine("wait a while before trigger tx ...");
                Thread.Sleep(2000);
                RPCClient client = new RPCClient("user:123456", "http://localhost:18443", network);
                uint256 txid     = client.SendToAddress(addr, Money.Coins(0.1m));
                Console.WriteLine("trigger txid => {0}", txid);
                client.Generate(1);
            });

            Console.ReadLine();
        }
예제 #15
0
        public void StakeSegwitBlock()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                // Even though we are mining, we still want to use PoS consensus rules.
                Network  network = KnownNetworks.StraxRegTest;
                CoreNode node    = builder.CreateStratisPosNode(network).WithWallet().Start();

                // Need the premine to be past coinbase maturity so that we can stake with it.
                RPCClient rpc = node.CreateRPCClient();
                int       minStakeConfirmations = ((PosConsensusOptions)network.Consensus.Options).GetStakeMinConfirmations(0, network);
                rpc.Generate(minStakeConfirmations + 2);

                var cancellationToken = new CancellationTokenSource(TimeSpan.FromMinutes(1)).Token;
                TestBase.WaitLoop(() => node.CreateRPCClient().GetBlockCount() >= (minStakeConfirmations + 2), cancellationToken: cancellationToken);

                // Now need to start staking.
                var staker = node.FullNode.NodeService <IPosMinting>() as StraxMinting;

                staker.Stake(new List <WalletSecret>()
                {
                    new WalletSecret()
                    {
                        WalletName = node.WalletName, WalletPassword = node.WalletPassword
                    }
                });

                // Wait for the chain height to increase.
                TestBase.WaitLoop(() => node.CreateRPCClient().GetBlockCount() >= (minStakeConfirmations + 3), cancellationToken: cancellationToken);

                // Get the last staked block.
                Block block = node.FullNode.ChainIndexer.Tip.Block;

                // Confirm that the staked block is Segwit-ted.
                Script commitment = WitnessCommitmentsRule.GetWitnessCommitment(node.FullNode.Network, block);

                // We presume that the consensus rules are checking the actual validity of the commitment, we just ensure that it exists here.
                Assert.NotNull(commitment);
            }
        }
        public void CanGetTxOutNoneFromRPC()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                CoreNode node = builder.CreateBitcoinCoreNode(version: "0.18.0", useNewConfigStyle: true).Start();
                CoreNode sfn  = builder.CreateStratisPowNode(this.regTest).Start();

                TestHelper.ConnectAndSync(node, sfn);

                RPCClient rpcClient = node.CreateRPCClient();
                RPCClient sfnRpc    = sfn.CreateRPCClient();

                uint256            txid        = rpcClient.Generate(1).Single();
                UnspentTransaction resultTxOut = rpcClient.GetTxOut(txid, 0, true);
                Assert.Null(resultTxOut);

                TestHelper.ConnectAndSync(node, sfn);

                resultTxOut = sfnRpc.GetTxOut(txid, 0, true);
                Assert.Null(resultTxOut);
            }
        }
예제 #17
0
        public void TestSegwit_MinedOnCore_ActivatedOn_XelsNode()
        {
            using (NodeBuilder builder = NodeBuilder.Create(version: "0.15.1"))
            {
                CoreNode coreNode = builder.CreateNode(false);

                coreNode.ConfigParameters.AddOrReplace("debug", "1");
                coreNode.ConfigParameters.AddOrReplace("printtoconsole", "0");
                coreNode.Start();

                CoreNode xelsNode = builder.CreateXelsPowNode(true, fullNodeBuilder =>
                {
                    fullNodeBuilder
                    .UsePowConsensus()
                    .UseBlockStore()
                    .UseWallet()
                    .UseMempool()
                    .AddMining()
                    .AddRPC();
                });

                RPCClient xelsNodeRpc = xelsNode.CreateRPCClient();
                RPCClient coreRpc     = coreNode.CreateRPCClient();

                coreRpc.AddNode(xelsNode.Endpoint, false);
                xelsNodeRpc.AddNode(coreNode.Endpoint, false);

                // core (in version 0.15.1) only mines segwit blocks above a certain height on regtest
                // future versions of core will change that behaviour so this test may need to be changed in the future
                // see issue for more details https://github.com/xelsproject/XelsBitcoinFullNode/issues/1028
                BIP9DeploymentsParameters prevSegwitDeployment = Network.RegTest.Consensus.BIP9Deployments[BIP9Deployments.Segwit];
                Network.RegTest.Consensus.BIP9Deployments[BIP9Deployments.Segwit] = new BIP9DeploymentsParameters(1, 0, DateTime.Now.AddDays(50).ToUnixTimestamp());

                try
                {
                    // generate 450 blocks, block 431 will be segwit activated.
                    coreRpc.Generate(450);

                    TestHelper.WaitLoop(() => xelsNode.CreateRPCClient().GetBestBlockHash() == coreNode.CreateRPCClient().GetBestBlockHash());

                    // segwit activation on Bitcoin regtest.
                    // - On regtest deployment state changes every 144 block, the threshold for activating a rule is 108 blocks.
                    // segwit deployment status should be:
                    // - Defined up to block 142.
                    // - Started at block 143 to block 286 .
                    // - LockedIn 287 (as segwit should already be signaled in blocks).
                    // - Active at block 431.

                    IConsensusLoop   consensusLoop       = xelsNode.FullNode.NodeService <IConsensusLoop>();
                    ThresholdState[] segwitDefinedState  = consensusLoop.NodeDeployments.BIP9.GetStates(xelsNode.FullNode.Chain.GetBlock(142));
                    ThresholdState[] segwitStartedState  = consensusLoop.NodeDeployments.BIP9.GetStates(xelsNode.FullNode.Chain.GetBlock(143));
                    ThresholdState[] segwitLockedInState = consensusLoop.NodeDeployments.BIP9.GetStates(xelsNode.FullNode.Chain.GetBlock(287));
                    ThresholdState[] segwitActiveState   = consensusLoop.NodeDeployments.BIP9.GetStates(xelsNode.FullNode.Chain.GetBlock(431));

                    // check that segwit is got activated at block 431
                    Assert.Equal(ThresholdState.Defined, segwitDefinedState.GetValue((int)BIP9Deployments.Segwit));
                    Assert.Equal(ThresholdState.Started, segwitStartedState.GetValue((int)BIP9Deployments.Segwit));
                    Assert.Equal(ThresholdState.LockedIn, segwitLockedInState.GetValue((int)BIP9Deployments.Segwit));
                    Assert.Equal(ThresholdState.Active, segwitActiveState.GetValue((int)BIP9Deployments.Segwit));
                }
                finally
                {
                    Network.RegTest.Consensus.BIP9Deployments[BIP9Deployments.Segwit] = prevSegwitDeployment;
                }
            }
        }