Exemple #1
0
        public void StakeSegwitBlock_UsingOnlySegwitUTXOs()
        {
            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();

                var address = BitcoinWitPubKeyAddress.Create(node.FullNode.WalletManager().GetUnusedAddress().Bech32Address, network);

                // A P2WPKH scriptPubKey - so that funds get mined into the node's wallet as segwit UTXOs
                var script = address.ScriptPubKey;

                // Need the premine to be past coinbase maturity so that we can stake with it.
                var            miner = node.FullNode.NodeService <IPowMining>() as PowMining;
                int            minStakeConfirmations = ((PosConsensusOptions)network.Consensus.Options).GetStakeMinConfirmations(0, network);
                List <uint256> res = miner.GenerateBlocks(new ReserveScript(script), (ulong)minStakeConfirmations + 2, int.MaxValue);

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

                var scriptPubKeys = node.FullNode.WalletManager().GetAccounts(node.WalletName).SelectMany(a => a.GetCombinedAddresses()).SelectMany(b => b.Transactions).Select(c => c.ScriptPubKey);

                // The wallet must have correctly determined that the block rewards have been mined into addresses it is aware of.
                Assert.NotEmpty(scriptPubKeys);

                // Check that every UTXO in the wallet has a Segwit scriptPubKey.
                foreach (Script scriptPubKey in scriptPubKeys)
                {
                    Assert.True(scriptPubKey.IsScriptType(ScriptType.P2WPKH));
                }

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

                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 static Transaction SetupValidSegwitTransaction(Features.Wallet.Wallet wallet, string password, HdAddress spendingAddress, HdAddress destinationAddress, HdAddress changeAddress, Money amount, Money fee)
        {
            Script scriptPubKey = BitcoinWitPubKeyAddress.Create(destinationAddress.Bech32Address, wallet.Network).ScriptPubKey;

            return(SetupValidTransaction(wallet, password, spendingAddress, scriptPubKey, changeAddress, amount, fee));
        }