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; } } }
private static ConcurrentChain GenerateChainWithHeightAndActivatedBip9(int blockAmount, Network network, Key key, BIP9DeploymentsParameters parameter, Target bits = null) { var chain = new ConcurrentChain(network); uint nonce = RandomUtils.GetUInt32(); uint256 prevBlockHash = chain.Genesis.HashBlock; for (int i = 0; i < blockAmount; i++) { Block block = network.Consensus.ConsensusFactory.CreateBlock(); Transaction coinbase = CreateCoinbaseTransaction(network, key, chain.Height + 1); block.AddTransaction(coinbase); block.UpdateMerkleRoot(); block.Header.BlockTime = new DateTimeOffset(new DateTime(2017, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddDays(i)); block.Header.HashPrevBlock = prevBlockHash; block.Header.Nonce = nonce; if (bits != null) { block.Header.Bits = bits; } if (parameter != null) { uint version = ThresholdConditionCache.VersionbitsTopBits; version |= ((uint)1) << parameter.Bit; block.Header.Version = (int)version; } chain.SetTip(block.Header); prevBlockHash = block.GetHash(); } return(chain); }
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; } } }