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 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 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; } } }
public CoreNode CreateStratisPosNode(bool start = false, Action <IFullNodeBuilder> callback = null) { string child = this.CreateNewEmptyFolder(); var node = new CoreNode(child, new StratisBitcoinPosRunner(callback), this, Network.RegTest, configfile: "stratis.conf"); this.Nodes.Add(node); if (start) { node.Start(); } return(node); }
public CoreNode CreateNode(bool start = false) { string child = this.CreateNewEmptyFolder(); var node = new CoreNode(child, new BitcoinCoreRunner(this.BitcoinD), this, Network.RegTest); this.Nodes.Add(node); if (start) { node.Start(); } return(node); }
public CoreNode CreateStratisPowNode(bool start = false, Action <IFullNodeBuilder> callback = null) { string child = CreateNewEmptyFolder(); var node = new CoreNode(child, new StratisBitcoinPowRunner(callback), this); this.Nodes.Add(node); if (start) { node.Start(); } return(node); }
public CoreNode CreateStratisNode(bool start = false) { string child = CreateNewEmptyFolder(); var node = new CoreNode(child, new StratisBitcoinRunner(), this); this.Nodes.Add(node); if (start) { node.Start(); } return(node); }
public CoreNode CreateNode(bool start = false) { string child = CreateNewEmptyFolder(); var node = new CoreNode(child, new BitcoinCoreRunner(_Bitcoind), this); Nodes.Add(node); if (start) { node.Start(); } return(node); }
public void ColdStakingActivatedOnStratisNode() { using (NodeBuilder builder = NodeBuilder.Create(this)) { // Create separate network parameters for this test. var network = new StratisOverrideRegTest(); // Set the date ranges such that ColdStaking will 'Start' immediately after the initial confirmation window. network.Consensus.BIP9Deployments[StratisBIP9Deployments.ColdStaking] = new BIP9DeploymentsParameters(1, 0, DateTime.Now.AddDays(50).ToUnixTimestamp()); // Set a small confirmation window to reduce time taken by this test. network.Consensus.MinerConfirmationWindow = 10; // Minimum number of 'votes' required within the conformation window to reach 'LockedIn' state. network.Consensus.RuleChangeActivationThreshold = 8; CoreNode stratisNode = builder.CreateStratisPosNode(network).WithWallet(); stratisNode.Start(); // ColdStaking activation: // - Deployment state changes every 'MinerConfirmationWindow' blocks. // - Remains in 'Defined' state until 'startedHeight'. // - Changes to 'Started' state at 'startedHeight'. // - Changes to 'LockedIn' state at 'lockedInHeight' (as coldstaking should already be signaled in blocks). // - Changes to 'Active' state at 'activeHeight'. int startedHeight = network.Consensus.MinerConfirmationWindow - 1; int lockedInHeight = startedHeight + network.Consensus.MinerConfirmationWindow; int activeHeight = lockedInHeight + network.Consensus.MinerConfirmationWindow; // Generate enough blocks to cover all state changes. TestHelper.MineBlocks(stratisNode, activeHeight + 1); // Check that coldstaking states got updated as expected. ThresholdConditionCache cache = (stratisNode.FullNode.NodeService <IConsensusRuleEngine>() as ConsensusRuleEngine).NodeDeployments.BIP9; Assert.Equal(ThresholdState.Defined, cache.GetState(stratisNode.FullNode.Chain.GetBlock(startedHeight - 1), StratisBIP9Deployments.ColdStaking)); Assert.Equal(ThresholdState.Started, cache.GetState(stratisNode.FullNode.Chain.GetBlock(startedHeight), StratisBIP9Deployments.ColdStaking)); Assert.Equal(ThresholdState.LockedIn, cache.GetState(stratisNode.FullNode.Chain.GetBlock(lockedInHeight), StratisBIP9Deployments.ColdStaking)); Assert.Equal(ThresholdState.Active, cache.GetState(stratisNode.FullNode.Chain.GetBlock(activeHeight), StratisBIP9Deployments.ColdStaking)); // Verify that the block created before activation does not have the 'CheckColdStakeVerify' flag set. var rulesEngine = stratisNode.FullNode.NodeService <IConsensusRuleEngine>(); ChainedHeader prevHeader = stratisNode.FullNode.Chain.GetBlock(activeHeight - 1); DeploymentFlags flags1 = (rulesEngine as ConsensusRuleEngine).NodeDeployments.GetFlags(prevHeader); Assert.Equal(0, (int)(flags1.ScriptFlags & ScriptVerify.CheckColdStakeVerify)); // Verify that the block created after activation has the 'CheckColdStakeVerify' flag set. DeploymentFlags flags2 = (rulesEngine as ConsensusRuleEngine).NodeDeployments.GetFlags(stratisNode.FullNode.Chain.Tip); Assert.NotEqual(0, (int)(flags2.ScriptFlags & ScriptVerify.CheckColdStakeVerify)); } }
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)); } }
public void SegwitActivatedOnStraxNode() { using (NodeBuilder builder = NodeBuilder.Create(this)) { var network = new StraxOverrideRegTest(); // Set the date ranges such that segwit will 'Start' immediately after the initial confirmation window. network.Consensus.BIP9Deployments[StraxBIP9Deployments.Segwit] = new BIP9DeploymentsParameters("Test", 1, 0, DateTime.Now.AddDays(50).ToUnixTimestamp(), 8); // Set a small confirmation window to reduce time taken by this test. network.Consensus.MinerConfirmationWindow = 10; CoreNode stratisNode = builder.CreateStratisPosNode(network).WithWallet(); stratisNode.Start(); // Deployment activation: // - Deployment state changes every 'MinerConfirmationWindow' blocks. // - Remains in 'Defined' state until 'startedHeight'. // - Changes to 'Started' state at 'startedHeight'. // - Changes to 'LockedIn' state at 'lockedInHeight' (we are assuming that in this test every mined block will signal Segwit support) // - Changes to 'Active' state at 'activeHeight'. int startedHeight = network.Consensus.MinerConfirmationWindow - 1; int lockedInHeight = startedHeight + network.Consensus.MinerConfirmationWindow; int activeHeight = lockedInHeight + network.Consensus.MinerConfirmationWindow; // Generate enough blocks to cover all state changes. TestHelper.MineBlocks(stratisNode, activeHeight + 1); // Check that Segwit states got updated as expected. ThresholdConditionCache cache = (stratisNode.FullNode.NodeService <IConsensusRuleEngine>() as ConsensusRuleEngine).NodeDeployments.BIP9; Assert.Equal(ThresholdState.Defined, cache.GetState(stratisNode.FullNode.ChainIndexer.GetHeader(startedHeight - 1), StraxBIP9Deployments.Segwit)); Assert.Equal(ThresholdState.Started, cache.GetState(stratisNode.FullNode.ChainIndexer.GetHeader(startedHeight), StraxBIP9Deployments.Segwit)); Assert.Equal(ThresholdState.LockedIn, cache.GetState(stratisNode.FullNode.ChainIndexer.GetHeader(lockedInHeight), StraxBIP9Deployments.Segwit)); Assert.Equal(ThresholdState.Active, cache.GetState(stratisNode.FullNode.ChainIndexer.GetHeader(activeHeight), StraxBIP9Deployments.Segwit)); // Verify that the block created before activation does not have the 'Witness' script flag set. var rulesEngine = stratisNode.FullNode.NodeService <IConsensusRuleEngine>(); ChainedHeader prevHeader = stratisNode.FullNode.ChainIndexer.GetHeader(activeHeight - 1); DeploymentFlags flags1 = (rulesEngine as ConsensusRuleEngine).NodeDeployments.GetFlags(prevHeader); Assert.Equal(0, (int)(flags1.ScriptFlags & ScriptVerify.Witness)); // Verify that the block created after activation has the 'Witness' flag set. DeploymentFlags flags2 = (rulesEngine as ConsensusRuleEngine).NodeDeployments.GetFlags(stratisNode.FullNode.ChainIndexer.Tip); Assert.NotEqual(0, (int)(flags2.ScriptFlags & ScriptVerify.Witness)); } }
public void MiningNodeWithOneConnectionAlwaysSynced() { string testFolderPath = Path.Combine(this.GetType().Name, nameof(MiningNodeWithOneConnectionAlwaysSynced)); using (NodeBuilder nodeBuilder = NodeBuilder.Create(testFolderPath)) { CoreNode minerNode = nodeBuilder.CreateStratisPowNode(this.powNetwork).NotInIBD(); minerNode.Start(); minerNode.WithWallet(); CoreNode connectorNode = nodeBuilder.CreateStratisPowNode(this.powNetwork).NotInIBD(); connectorNode.Start(); connectorNode.WithWallet(); CoreNode firstNode = nodeBuilder.CreateStratisPowNode(this.powNetwork).NotInIBD(); firstNode.Start(); firstNode.WithWallet(); CoreNode secondNode = nodeBuilder.CreateStratisPowNode(this.powNetwork); secondNode.Start(); secondNode.WithWallet(); TestHelper.Connect(minerNode, connectorNode); TestHelper.Connect(connectorNode, firstNode); TestHelper.Connect(connectorNode, secondNode); TestHelper.Connect(firstNode, secondNode); List <CoreNode> nodes = new List <CoreNode> { minerNode, connectorNode, firstNode, secondNode }; nodes.ForEach(n => { TestHelper.MineBlocks(n, 1); TestHelper.WaitForNodeToSync(nodes.ToArray()); }); int networkHeight = minerNode.FullNode.Chain.Height; Assert.Equal(networkHeight, nodes.Count); // Random node on network generates a block. TestHelper.MineBlocks(firstNode, 1); // Wait until connector get the hash of network's block. while ((connectorNode.FullNode.ChainBehaviorState.ConsensusTip.HashBlock != firstNode.FullNode.ChainBehaviorState.ConsensusTip.HashBlock) || (firstNode.FullNode.ChainBehaviorState.ConsensusTip.Height == networkHeight)) { Thread.Sleep(1); } Assert.Equal(connectorNode.FullNode.Chain.Tip.HashBlock, firstNode.FullNode.Chain.Tip.HashBlock); Assert.Equal(minerNode.FullNode.Chain.Tip.Height, networkHeight); Assert.Equal(connectorNode.FullNode.Chain.Tip.Height, networkHeight + 1); // Miner mines the block. TestHelper.MineBlocks(minerNode, 1); networkHeight++; Assert.Equal(connectorNode.FullNode.Chain.Tip.HashBlock, firstNode.FullNode.Chain.Tip.HashBlock); Assert.Equal(minerNode.FullNode.Chain.Tip.Height, networkHeight); Assert.Equal(connectorNode.FullNode.Chain.Tip.Height, networkHeight); TestHelper.MineBlocks(connectorNode, 1); networkHeight++; TestHelper.WaitForNodeToSync(nodes.ToArray()); nodes.All(n => n.FullNode.Chain.Height == networkHeight).Should() .BeTrue(because: "all nodes have synced to chain height"); Assert.Equal(firstNode.FullNode.Chain.Tip.HashBlock, minerNode.FullNode.Chain.Tip.HashBlock); } }