Skip to content

Commit eba5b1c

Browse files
committed
[test] remove or move tests using -segwitheight=-1
1 parent 4129134 commit eba5b1c

File tree

4 files changed

+75
-79
lines changed

4 files changed

+75
-79
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2017-2020 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test a pre-segwit node upgrading to segwit consensus"""
6+
7+
from test_framework.test_framework import BitcoinTestFramework
8+
from test_framework.util import (
9+
assert_equal,
10+
softfork_active,
11+
)
12+
13+
class SegwitUpgradeTest(BitcoinTestFramework):
14+
def set_test_params(self):
15+
self.setup_clean_chain = True
16+
self.num_nodes = 1
17+
self.extra_args = [["-segwitheight=10"]]
18+
19+
def run_test(self):
20+
"""A pre-segwit node with insufficiently validated blocks needs to redownload blocks"""
21+
22+
self.log.info("Testing upgrade behaviour for pre-segwit node to segwit rules")
23+
node = self.nodes[0]
24+
25+
# Node hasn't been used or connected yet
26+
assert_equal(node.getblockcount(), 0)
27+
28+
assert not softfork_active(node, "segwit")
29+
30+
# Generate 8 blocks without witness data
31+
node.generate(8)
32+
assert_equal(node.getblockcount(), 8)
33+
34+
self.stop_node(0)
35+
# Restarting the node (with segwit activation height set to 5) should result in a shutdown
36+
# because the blockchain consists of 3 insufficiently validated blocks per segwit consensus rules.
37+
node.assert_start_raises_init_error(
38+
extra_args=["-segwitheight=5"],
39+
expected_msg=": Witness data for blocks after height 5 requires validation. Please restart with -reindex..\nPlease restart with -reindex or -reindex-chainstate to recover.")
40+
41+
# As directed, the user restarts the node with -reindex
42+
self.start_node(0, extra_args=["-reindex", "-segwitheight=5"])
43+
44+
# With the segwit consensus rules, the node is able to validate only up to block 4
45+
assert_equal(node.getblockcount(), 4)
46+
47+
# The upgraded node should now have segwit activated
48+
assert softfork_active(node, "segwit")
49+
50+
51+
if __name__ == '__main__':
52+
SegwitUpgradeTest().main()

test/functional/mining_basic.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313

1414
from test_framework.blocktools import (
1515
create_coinbase,
16+
get_witness_script,
1617
NORMAL_GBT_REQUEST_PARAMS,
1718
TIME_GENESIS_BLOCK,
1819
)
1920
from test_framework.messages import (
2021
CBlock,
2122
CBlockHeader,
2223
BLOCK_HEADER_SIZE,
24+
ser_uint256,
2325
)
2426
from test_framework.p2p import P2PDataStore
2527
from test_framework.test_framework import BitcoinTestFramework
@@ -49,6 +51,9 @@ def set_test_params(self):
4951
self.setup_clean_chain = True
5052
self.supports_cli = False
5153

54+
def skip_test_if_missing_module(self):
55+
self.skip_if_no_wallet()
56+
5257
def mine_chain(self):
5358
self.log.info('Create some old blocks')
5459
for t in range(TIME_GENESIS_BLOCK, TIME_GENESIS_BLOCK + 200 * 600, 600):
@@ -89,7 +94,21 @@ def assert_submitblock(block, result_str_1, result_str_2=None):
8994
assert_equal(mining_info['networkhashps'], Decimal('0.003333333333333334'))
9095
assert_equal(mining_info['pooledtx'], 0)
9196

92-
# Mine a block to leave initial block download
97+
self.log.info("getblocktemplate: Test default witness commitment")
98+
txid = int(node.sendtoaddress(node.getnewaddress(), 1), 16)
99+
tmpl = node.getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)
100+
101+
# Check that default_witness_commitment is present.
102+
assert 'default_witness_commitment' in tmpl
103+
witness_commitment = tmpl['default_witness_commitment']
104+
105+
# Check that default_witness_commitment is correct.
106+
witness_root = CBlock.get_merkle_root([ser_uint256(0),
107+
ser_uint256(txid)])
108+
script = get_witness_script(witness_root, 0)
109+
assert_equal(witness_commitment, script.hex())
110+
111+
# Mine a block to leave initial block download and clear the mempool
93112
node.generatetoaddress(1, node.get_deterministic_priv_key().address)
94113
tmpl = node.getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)
95114
self.log.info("getblocktemplate: Test capability advertised")

test/functional/p2p_segwit.py

Lines changed: 2 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
import struct
1010
import time
1111

12-
from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment, get_witness_script, WITNESS_COMMITMENT_HEADER
12+
from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment, WITNESS_COMMITMENT_HEADER
1313
from test_framework.key import ECKey
1414
from test_framework.messages import (
1515
BIP125_SEQUENCE_NUMBER,
16-
CBlock,
1716
CBlockHeader,
1817
CInv,
1918
COutPoint,
@@ -209,24 +208,17 @@ def request_block(self, blockhash, inv_type, timeout=60):
209208
class SegWitTest(BitcoinTestFramework):
210209
def set_test_params(self):
211210
self.setup_clean_chain = True
212-
self.num_nodes = 3
211+
self.num_nodes = 2
213212
# This test tests SegWit both pre and post-activation, so use the normal BIP9 activation.
214213
self.extra_args = [
215214
["-acceptnonstdtxn=1", "-segwitheight={}".format(SEGWIT_HEIGHT), "[email protected]"],
216215
["-acceptnonstdtxn=0", "-segwitheight={}".format(SEGWIT_HEIGHT)],
217-
["-acceptnonstdtxn=1", "-segwitheight=-1"],
218216
]
219217
self.supports_cli = False
220218

221219
def skip_test_if_missing_module(self):
222220
self.skip_if_no_wallet()
223221

224-
def setup_network(self):
225-
self.setup_nodes()
226-
self.connect_nodes(0, 1)
227-
self.connect_nodes(0, 2)
228-
self.sync_all()
229-
230222
# Helper functions
231223

232224
def build_next_block(self, version=4):
@@ -267,7 +259,6 @@ def run_test(self):
267259
self.test_non_witness_transaction()
268260
self.test_v0_outputs_arent_spendable()
269261
self.test_block_relay()
270-
self.test_getblocktemplate_before_lockin()
271262
self.test_unnecessary_witness_before_segwit_activation()
272263
self.test_witness_tx_relay_before_segwit_activation()
273264
self.test_standardness_v0()
@@ -295,7 +286,6 @@ def run_test(self):
295286
self.test_signature_version_1()
296287
self.test_non_standard_witness_blinding()
297288
self.test_non_standard_witness()
298-
self.test_upgrade_after_activation()
299289
self.test_witness_sigops()
300290
self.test_superfluous_witness()
301291
self.test_wtxid_relay()
@@ -485,11 +475,6 @@ def test_v0_outputs_arent_spendable(self):
485475
witness, and so can't be spent before segwit activation (the point at which
486476
blocks are permitted to contain witnesses)."""
487477

488-
# node2 doesn't need to be connected for this test.
489-
# (If it's connected, node0 may propagate an invalid block to it over
490-
# compact blocks and the nodes would have inconsistent tips.)
491-
self.disconnect_nodes(0, 2)
492-
493478
# Create two outputs, a p2wsh and p2sh-p2wsh
494479
witness_program = CScript([OP_TRUE])
495480
witness_hash = sha256(witness_program)
@@ -550,37 +535,9 @@ def test_v0_outputs_arent_spendable(self):
550535
# TODO: support multiple acceptable reject reasons.
551536
test_witness_block(self.nodes[0], self.test_node, block, accepted=False, with_witness=False)
552537

553-
self.connect_nodes(0, 2)
554-
555538
self.utxo.pop(0)
556539
self.utxo.append(UTXO(txid, 2, value))
557540

558-
@subtest # type: ignore
559-
def test_getblocktemplate_before_lockin(self):
560-
txid = int(self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1), 16)
561-
562-
for node in [self.nodes[0], self.nodes[2]]:
563-
gbt_results = node.getblocktemplate({"rules": ["segwit"]})
564-
if node == self.nodes[2]:
565-
# If this is a non-segwit node, we should not get a witness
566-
# commitment.
567-
assert 'default_witness_commitment' not in gbt_results
568-
else:
569-
# For segwit-aware nodes, check the witness
570-
# commitment is correct.
571-
assert 'default_witness_commitment' in gbt_results
572-
witness_commitment = gbt_results['default_witness_commitment']
573-
574-
# Check that default_witness_commitment is present.
575-
witness_root = CBlock.get_merkle_root([ser_uint256(0),
576-
ser_uint256(txid)])
577-
script = get_witness_script(witness_root, 0)
578-
assert_equal(witness_commitment, script.hex())
579-
580-
# Clear out the mempool
581-
self.nodes[0].generate(1)
582-
self.sync_blocks()
583-
584541
@subtest # type: ignore
585542
def test_witness_tx_relay_before_segwit_activation(self):
586543

@@ -1952,39 +1909,6 @@ def test_non_standard_witness(self):
19521909

19531910
self.utxo.pop(0)
19541911

1955-
@subtest # type: ignore
1956-
def test_upgrade_after_activation(self):
1957-
"""Test the behavior of starting up a segwit-aware node after the softfork has activated."""
1958-
1959-
# All nodes are caught up and node 2 is a pre-segwit node that will soon upgrade.
1960-
for n in range(2):
1961-
assert_equal(self.nodes[n].getblockcount(), self.nodes[2].getblockcount())
1962-
assert softfork_active(self.nodes[n], "segwit")
1963-
assert SEGWIT_HEIGHT < self.nodes[2].getblockcount()
1964-
assert 'segwit' not in self.nodes[2].getblockchaininfo()['softforks']
1965-
1966-
# Restarting node 2 should result in a shutdown because the blockchain consists of
1967-
# insufficiently validated blocks per segwit consensus rules.
1968-
self.stop_node(2)
1969-
self.nodes[2].assert_start_raises_init_error(
1970-
extra_args=[f"-segwitheight={SEGWIT_HEIGHT}"],
1971-
expected_msg=f": Witness data for blocks after height {SEGWIT_HEIGHT} requires validation. Please restart with -reindex..\nPlease restart with -reindex or -reindex-chainstate to recover.",
1972-
)
1973-
1974-
# As directed, the user restarts the node with -reindex
1975-
self.start_node(2, extra_args=["-reindex", f"-segwitheight={SEGWIT_HEIGHT}"])
1976-
1977-
# With the segwit consensus rules, the node is able to validate only up to SEGWIT_HEIGHT - 1
1978-
assert_equal(self.nodes[2].getblockcount(), SEGWIT_HEIGHT - 1)
1979-
self.connect_nodes(0, 2)
1980-
1981-
# We reconnect more than 100 blocks, give it plenty of time
1982-
# sync_blocks() also verifies the best block hash is the same for all nodes
1983-
self.sync_blocks(timeout=240)
1984-
1985-
# The upgraded node should now have segwit activated
1986-
assert softfork_active(self.nodes[2], "segwit")
1987-
19881912
@subtest # type: ignore
19891913
def test_witness_sigops(self):
19901914
"""Test sigop counting is correct inside witnesses."""

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@
292292
'feature_blocksdir.py',
293293
'wallet_startup.py',
294294
'feature_config_args.py',
295+
'feature_presegwit_node_upgrade.py',
295296
'feature_settings.py',
296297
'rpc_getdescriptorinfo.py',
297298
'rpc_addresses_deprecation.py',

0 commit comments

Comments
 (0)