Skip to content

Commit d932159

Browse files
committed
Merge #9189: Always add default_witness_commitment with GBT client support
95f4a03 [qa] Test getblocktemplate default_witness_commitment (Suhas Daftuar) ad04d1c Always add default_witness_commitment with GBT client support (Pieter Wuille)
2 parents bc121b0 + 95f4a03 commit d932159

File tree

4 files changed

+52
-32
lines changed

4 files changed

+52
-32
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

src/main.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3511,15 +3511,8 @@ std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBloc
35113511
{
35123512
std::vector<unsigned char> commitment;
35133513
int commitpos = GetWitnessCommitmentIndex(block);
3514-
bool fHaveWitness = false;
3515-
for (size_t t = 1; t < block.vtx.size(); t++) {
3516-
if (!block.vtx[t]->wit.IsNull()) {
3517-
fHaveWitness = true;
3518-
break;
3519-
}
3520-
}
35213514
std::vector<unsigned char> ret(32, 0x00);
3522-
if (fHaveWitness && IsWitnessEnabled(pindexPrev, consensusParams)) {
3515+
if (consensusParams.vDeployments[Consensus::DEPLOYMENT_SEGWIT].nTimeout != 0) {
35233516
if (commitpos == -1) {
35243517
uint256 witnessroot = BlockWitnessMerkleRoot(block, NULL);
35253518
CHash256().Write(witnessroot.begin(), 32).Write(&ret[0], 32).Finalize(witnessroot.begin());

src/rpc/mining.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,9 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
680680
result.push_back(Pair("curtime", pblock->GetBlockTime()));
681681
result.push_back(Pair("bits", strprintf("%08x", pblock->nBits)));
682682
result.push_back(Pair("height", (int64_t)(pindexPrev->nHeight+1)));
683-
if (!pblocktemplate->vchCoinbaseCommitment.empty()) {
683+
684+
const struct BIP9DeploymentInfo& segwit_info = VersionBitsDeploymentInfo[Consensus::DEPLOYMENT_SEGWIT];
685+
if (!pblocktemplate->vchCoinbaseCommitment.empty() && setClientRules.find(segwit_info.name) != setClientRules.end()) {
684686
result.push_back(Pair("default_witness_commitment", HexStr(pblocktemplate->vchCoinbaseCommitment.begin(), pblocktemplate->vchCoinbaseCommitment.end())));
685687
}
686688

src/test/miner_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
214214
txCoinbase.vin[0].scriptSig = CScript();
215215
txCoinbase.vin[0].scriptSig.push_back(blockinfo[i].extranonce);
216216
txCoinbase.vin[0].scriptSig.push_back(chainActive.Height());
217+
txCoinbase.vout.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this)
217218
txCoinbase.vout[0].scriptPubKey = CScript();
218219
pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
219220
if (txFirst.size() == 0)

0 commit comments

Comments
 (0)