Skip to content

Commit abe7b3d

Browse files
committed
Don't require segwit in getblocktemplate for segwit signalling or mining
Segwit's version bit will be signalled for all invocations of CreateNewBlock, and not specifying segwit only will cause CreateNewBlock to skip transactions with witness from being selected.
1 parent 3cc13ea commit abe7b3d

File tree

6 files changed

+29
-14
lines changed

6 files changed

+29
-14
lines changed

qa/rpc-tests/p2p-segwit.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,9 +1698,11 @@ def test_getblocktemplate_before_lockin(self):
16981698
for node in [self.nodes[0], self.nodes[2]]:
16991699
gbt_results = node.getblocktemplate()
17001700
block_version = gbt_results['version']
1701-
# If we're not indicating segwit support, we should not be signalling
1702-
# for segwit activation, nor should we get a witness commitment.
1703-
assert_equal(block_version & (1 << VB_WITNESS_BIT), 0)
1701+
# If we're not indicating segwit support, we will still be
1702+
# signalling for segwit activation.
1703+
assert_equal((block_version & (1 << VB_WITNESS_BIT) != 0), node == self.nodes[0])
1704+
# If we don't specify the segwit rule, then we won't get a default
1705+
# commitment.
17041706
assert('default_witness_commitment' not in gbt_results)
17051707

17061708
# Workaround:

qa/rpc-tests/segwit.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,12 @@ def run_test(self):
250250
assert(tmpl['transactions'][0]['txid'] == txid)
251251
assert(tmpl['transactions'][0]['sigops'] == 8)
252252

253-
self.log.info("Non-segwit miners are not able to use GBT response after activation.")
254-
send_to_witness(1, self.nodes[0], find_unspent(self.nodes[0], 50), self.pubkey[0], False, Decimal("49.998"))
255-
assert_raises_jsonrpc(-8, "Support for 'segwit' rule requires explicit client support", self.nodes[0].getblocktemplate, {})
253+
self.log.info("Non-segwit miners are able to use GBT response after activation.")
254+
txid = send_to_witness(1, self.nodes[0], find_unspent(self.nodes[0], 50), self.pubkey[0], False, Decimal("49.998"))
255+
#assert_raises_jsonrpc(-8, "Support for 'segwit' rule requires explicit client support", self.nodes[0].getblocktemplate, {})
256+
tmpl = self.nodes[0].getblocktemplate()
257+
# TODO: add a transaction with witness to mempool, and verify it's not
258+
# selected for mining.
256259

257260
self.log.info("Verify behaviour of importaddress, addwitnessaddress and listunspent")
258261

src/miner.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void BlockAssembler::resetBlock()
137137
nFees = 0;
138138
}
139139

140-
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
140+
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn, bool fMineWitnessTx)
141141
{
142142
resetBlock();
143143

@@ -175,7 +175,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
175175
// -promiscuousmempoolflags is used.
176176
// TODO: replace this with a call to main to assess validity of a mempool
177177
// transaction (which in most cases can be a no-op).
178-
fIncludeWitness = IsWitnessEnabled(pindexPrev, chainparams.GetConsensus());
178+
fIncludeWitness = IsWitnessEnabled(pindexPrev, chainparams.GetConsensus()) && fMineWitnessTx;
179179

180180
addPackageTxs();
181181

src/miner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class BlockAssembler
170170
BlockAssembler(const CChainParams& params, const Options& options);
171171

172172
/** Construct a new block template with coinbase to scriptPubKeyIn */
173-
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
173+
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn, bool fMineWitnessTx=true);
174174

175175
private:
176176
// utility functions

src/rpc/mining.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,22 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
514514
// TODO: Maybe recheck connections/IBD and (if something wrong) send an expires-immediately template to stop miners?
515515
}
516516

517+
const struct BIP9DeploymentInfo& segwit_info = VersionBitsDeploymentInfo[Consensus::DEPLOYMENT_SEGWIT];
518+
// If the caller is indicating segwit support, then allow CreateNewBlock()
519+
// to select witness transactions, after segwit activates (otherwise
520+
// don't).
521+
bool fSupportsSegwit = setClientRules.find(segwit_info.name) != setClientRules.end();
522+
517523
// Update block
518524
static CBlockIndex* pindexPrev;
519525
static int64_t nStart;
520526
static std::unique_ptr<CBlockTemplate> pblocktemplate;
527+
// Cache whether the last invocation was with segwit support, to avoid returning
528+
// a segwit-block to a non-segwit caller.
529+
static bool fLastTemplateSupportsSegwit = true;
521530
if (pindexPrev != chainActive.Tip() ||
522-
(mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 5))
531+
(mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 5) ||
532+
fLastTemplateSupportsSegwit != fSupportsSegwit)
523533
{
524534
// Clear pindexPrev so future calls make a new block, despite any failures from here on
525535
pindexPrev = nullptr;
@@ -528,10 +538,11 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
528538
nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
529539
CBlockIndex* pindexPrevNew = chainActive.Tip();
530540
nStart = GetTime();
541+
fLastTemplateSupportsSegwit = fSupportsSegwit;
531542

532543
// Create new block
533544
CScript scriptDummy = CScript() << OP_TRUE;
534-
pblocktemplate = BlockAssembler(Params()).CreateNewBlock(scriptDummy);
545+
pblocktemplate = BlockAssembler(Params()).CreateNewBlock(scriptDummy, fSupportsSegwit);
535546
if (!pblocktemplate)
536547
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
537548

@@ -681,8 +692,7 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
681692
result.push_back(Pair("bits", strprintf("%08x", pblock->nBits)));
682693
result.push_back(Pair("height", (int64_t)(pindexPrev->nHeight+1)));
683694

684-
const struct BIP9DeploymentInfo& segwit_info = VersionBitsDeploymentInfo[Consensus::DEPLOYMENT_SEGWIT];
685-
if (!pblocktemplate->vchCoinbaseCommitment.empty() && setClientRules.find(segwit_info.name) != setClientRules.end()) {
695+
if (!pblocktemplate->vchCoinbaseCommitment.empty() && fSupportsSegwit) {
686696
result.push_back(Pair("default_witness_commitment", HexStr(pblocktemplate->vchCoinbaseCommitment.begin(), pblocktemplate->vchCoinbaseCommitment.end())));
687697
}
688698

src/versionbits.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const struct BIP9DeploymentInfo VersionBitsDeploymentInfo[Consensus::MAX_VERSION
1717
},
1818
{
1919
/*.name =*/ "segwit",
20-
/*.gbt_force =*/ false,
20+
/*.gbt_force =*/ true,
2121
}
2222
};
2323

0 commit comments

Comments
 (0)