Skip to content

Commit 95f4a03

Browse files
sdaftuarsipa
authored andcommitted
[qa] Test getblocktemplate default_witness_commitment
1 parent ad04d1c commit 95f4a03

File tree

1 file changed

+47
-23
lines changed

1 file changed

+47
-23
lines changed

qa/rpc-tests/p2p-segwit.py

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -195,24 +195,16 @@ def __init__(self):
195195
self.setup_clean_chain = True
196196
self.num_nodes = 3
197197

198-
def add_options(self, parser):
199-
parser.add_option("--oldbinary", dest="oldbinary",
200-
default=None,
201-
help="pre-segwit bitcoind binary for upgrade testing")
202-
203198
def setup_network(self):
204199
self.nodes = []
205200
self.nodes.append(start_node(0, self.options.tmpdir, ["-debug", "-logtimemicros=1", "-whitelist=127.0.0.1"]))
206201
# Start a node for testing IsStandard rules.
207202
self.nodes.append(start_node(1, self.options.tmpdir, ["-debug", "-logtimemicros=1", "-whitelist=127.0.0.1", "-acceptnonstdtxn=0"]))
208203
connect_nodes(self.nodes[0], 1)
209204

210-
# If an old bitcoind is given, do the upgrade-after-activation test.
211-
self.test_upgrade = False
212-
if (self.options.oldbinary != None):
213-
self.nodes.append(start_node(2, self.options.tmpdir, ["-debug", "-whitelist=127.0.0.1"], binary=self.options.oldbinary))
214-
connect_nodes(self.nodes[0], 2)
215-
self.test_upgrade = True
205+
# Disable segwit's bip9 parameter to simulate upgrading after activation.
206+
self.nodes.append(start_node(2, self.options.tmpdir, ["-debug", "-whitelist=127.0.0.1", "-bip9params=segwit:0:0"]))
207+
connect_nodes(self.nodes[0], 2)
216208

217209
''' Helpers '''
218210
# Build a block on top of node0's tip.
@@ -1177,7 +1169,7 @@ def test_standardness_v0(self, segwit_activated):
11771169
if segwit_activated:
11781170
# tx and tx2 were both accepted. Don't bother trying to reclaim the
11791171
# P2PKH output; just send tx's first output back to an anyone-can-spend.
1180-
sync_mempools(self.nodes)
1172+
sync_mempools([self.nodes[0], self.nodes[1]])
11811173
tx3.vin = [CTxIn(COutPoint(tx.sha256, 0), b"")]
11821174
tx3.vout = [CTxOut(tx.vout[0].nValue-1000, CScript([OP_TRUE]))]
11831175
tx3.wit.vtxinwit.append(CTxInWitness())
@@ -1706,19 +1698,53 @@ def test_witness_sigops(self):
17061698

17071699
def test_getblocktemplate_before_lockin(self):
17081700
print("\tTesting getblocktemplate setting of segwit versionbit (before lockin)")
1709-
block_version = (self.nodes[0].getblocktemplate())['version']
1710-
assert_equal(block_version & (1 << VB_WITNESS_BIT), 0)
1701+
# Node0 is segwit aware, node2 is not.
1702+
for node in [self.nodes[0], self.nodes[2]]:
1703+
gbt_results = node.getblocktemplate()
1704+
block_version = gbt_results['version']
1705+
# If we're not indicating segwit support, we should not be signalling
1706+
# for segwit activation, nor should we get a witness commitment.
1707+
assert_equal(block_version & (1 << VB_WITNESS_BIT), 0)
1708+
assert('default_witness_commitment' not in gbt_results)
17111709

17121710
# Workaround:
17131711
# Can either change the tip, or change the mempool and wait 5 seconds
17141712
# to trigger a recomputation of getblocktemplate.
1715-
self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
1713+
txid = int(self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1), 16)
17161714
# Using mocktime lets us avoid sleep()
1715+
sync_mempools(self.nodes)
17171716
self.nodes[0].setmocktime(int(time.time())+10)
1718-
1719-
block_version = self.nodes[0].getblocktemplate({"rules" : ["segwit"]})['version']
1720-
assert(block_version & (1 << VB_WITNESS_BIT) != 0)
1721-
self.nodes[0].setmocktime(0) # undo mocktime
1717+
self.nodes[2].setmocktime(int(time.time())+10)
1718+
1719+
for node in [self.nodes[0], self.nodes[2]]:
1720+
gbt_results = node.getblocktemplate({"rules" : ["segwit"]})
1721+
block_version = gbt_results['version']
1722+
if node == self.nodes[2]:
1723+
# If this is a non-segwit node, we should still not get a witness
1724+
# commitment, nor a version bit signalling segwit.
1725+
assert_equal(block_version & (1 << VB_WITNESS_BIT), 0)
1726+
assert('default_witness_commitment' not in gbt_results)
1727+
else:
1728+
# For segwit-aware nodes, check the version bit and the witness
1729+
# commitment are correct.
1730+
assert(block_version & (1 << VB_WITNESS_BIT) != 0)
1731+
assert('default_witness_commitment' in gbt_results)
1732+
witness_commitment = gbt_results['default_witness_commitment']
1733+
1734+
# TODO: this duplicates some code from blocktools.py, would be nice
1735+
# to refactor.
1736+
# Check that default_witness_commitment is present.
1737+
block = CBlock()
1738+
witness_root = block.get_merkle_root([ser_uint256(0), ser_uint256(txid)])
1739+
check_commitment = uint256_from_str(hash256(ser_uint256(witness_root)+ser_uint256(0)))
1740+
from test_framework.blocktools import WITNESS_COMMITMENT_HEADER
1741+
output_data = WITNESS_COMMITMENT_HEADER + ser_uint256(check_commitment)
1742+
script = CScript([OP_RETURN, output_data])
1743+
assert_equal(witness_commitment, bytes_to_hex_str(script))
1744+
1745+
# undo mocktime
1746+
self.nodes[0].setmocktime(0)
1747+
self.nodes[2].setmocktime(0)
17221748

17231749
# Uncompressed pubkeys are no longer supported in default relay policy,
17241750
# but (for now) are still valid in blocks.
@@ -1958,6 +1984,7 @@ def run_test(self):
19581984

19591985
# Advance to segwit being 'started'
19601986
self.advance_to_segwit_started()
1987+
sync_blocks(self.nodes)
19611988
self.test_getblocktemplate_before_lockin()
19621989

19631990
sync_blocks(self.nodes)
@@ -2000,10 +2027,7 @@ def run_test(self):
20002027
self.test_signature_version_1()
20012028
self.test_non_standard_witness()
20022029
sync_blocks(self.nodes)
2003-
if self.test_upgrade:
2004-
self.test_upgrade_after_activation(self.nodes[2], 2)
2005-
else:
2006-
print("\tSkipping upgrade-after-activation test (use --oldbinary to enable)")
2030+
self.test_upgrade_after_activation(self.nodes[2], 2)
20072031
self.test_witness_sigops()
20082032

20092033

0 commit comments

Comments
 (0)