Skip to content

Commit

Permalink
miner: always treat SegWit as active
Browse files Browse the repository at this point in the history
The getblocktemplate RPC would check if SegWit has activated yet at the tip. This has been the case for more than five years.
  • Loading branch information
Sjors committed Sep 17, 2024
1 parent 9fc563b commit 9fd56f0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 35 deletions.
24 changes: 4 additions & 20 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,9 +826,6 @@ static RPCHelpMan getblocktemplate()
UpdateTime(&block, consensusParams, pindexPrev);
block.nNonce = 0;

// NOTE: If at some point we support pre-segwit miners post-segwit-activation, this needs to take segwit support into consideration
const bool fPreSegWit = !DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_SEGWIT);

UniValue aCaps(UniValue::VARR); aCaps.push_back("proposal");

UniValue transactions(UniValue::VARR);
Expand Down Expand Up @@ -862,10 +859,6 @@ static RPCHelpMan getblocktemplate()
int index_in_template = i - 1;
entry.pushKV("fee", tx_fees.at(index_in_template));
int64_t nTxSigOps{tx_sigops.at(index_in_template)};
if (fPreSegWit) {
CHECK_NONFATAL(nTxSigOps % WITNESS_SCALE_FACTOR == 0);
nTxSigOps /= WITNESS_SCALE_FACTOR;
}
entry.pushKV("sigops", nTxSigOps);
entry.pushKV("weight", GetTransactionWeight(tx));

Expand All @@ -889,10 +882,9 @@ static RPCHelpMan getblocktemplate()
// ! indicates a more subtle change to the block structure or generation transaction
// Otherwise clients may assume the rule will not impact usage of the template as-is.
aRules.push_back("csv");
if (!fPreSegWit) {
aRules.push_back("!segwit");
aRules.push_back("taproot");
}
// BIP 145: the '!' rule prefix MUST be enabled on the "segwit" rule for templates including transactions with witness data.
aRules.push_back("!segwit");
aRules.push_back("taproot");
if (consensusParams.signet_blocks) {
// indicate to miner that they must understand signet rules
// when attempting to mine with this template
Expand Down Expand Up @@ -955,17 +947,9 @@ static RPCHelpMan getblocktemplate()
result.pushKV("noncerange", "00000000ffffffff");
int64_t nSigOpLimit = MAX_BLOCK_SIGOPS_COST;
int64_t nSizeLimit = MAX_BLOCK_SERIALIZED_SIZE;
if (fPreSegWit) {
CHECK_NONFATAL(nSigOpLimit % WITNESS_SCALE_FACTOR == 0);
nSigOpLimit /= WITNESS_SCALE_FACTOR;
CHECK_NONFATAL(nSizeLimit % WITNESS_SCALE_FACTOR == 0);
nSizeLimit /= WITNESS_SCALE_FACTOR;
}
result.pushKV("sigoplimit", nSigOpLimit);
result.pushKV("sizelimit", nSizeLimit);
if (!fPreSegWit) {
result.pushKV("weightlimit", (int64_t)MAX_BLOCK_WEIGHT);
}
result.pushKV("weightlimit", (int64_t)MAX_BLOCK_WEIGHT);
result.pushKV("curtime", block.GetBlockTime());
result.pushKV("bits", strprintf("%08x", block.nBits));
result.pushKV("height", (int64_t)(pindexPrev->nHeight+1));
Expand Down
16 changes: 1 addition & 15 deletions test/functional/feature_segwit.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,18 @@ def add_options(self, parser):
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 3
# This test tests SegWit both pre and post-activation, so use the normal BIP9 activation.
# This test tests SegWit post-activation
self.extra_args = [
[
"-acceptnonstdtxn=1",
"-testactivationheight=segwit@165",
"-addresstype=legacy",
],
[
"-acceptnonstdtxn=1",
"-testactivationheight=segwit@165",
"-addresstype=legacy",
],
[
"-acceptnonstdtxn=1",
"-testactivationheight=segwit@165",
"-addresstype=legacy",
],
]
Expand All @@ -124,17 +121,6 @@ def fail_accept(self, node, error_msg, txid, sign, redeem_script=""):
def run_test(self):
self.generate(self.nodes[0], 161) # block 161

self.log.info("Verify sigops are counted in GBT with pre-BIP141 rules before the fork")
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
tmpl = self.nodes[0].getblocktemplate({'rules': ['segwit']})
assert_equal(tmpl['sizelimit'], 1000000)
assert 'weightlimit' not in tmpl
assert_equal(tmpl['sigoplimit'], 20000)
assert_equal(tmpl['transactions'][0]['hash'], txid)
assert_equal(tmpl['transactions'][0]['sigops'], 2)
assert '!segwit' not in tmpl['rules']
self.generate(self.nodes[0], 1) # block 162

balance_presetup = self.nodes[0].getbalance()
self.pubkey = []
p2sh_ids = [] # p2sh_ids[NODE][TYPE] is an array of txids that spend to P2WPKH (TYPE=0) or P2WSH (TYPE=1) scripts to an address for NODE embedded in p2sh
Expand Down

0 comments on commit 9fd56f0

Please sign in to comment.