Skip to content

Commit 7c08d81

Browse files
committed
Merge bitcoin/bitcoin#23536: Enforce Taproot script flags whenever WITNESS is set
cccc1e7 Enforce Taproot script flags whenever WITNESS is set (MarcoFalke) fa42299 Remove nullptr check in GetBlockScriptFlags (MarcoFalke) faadc60 refactor: Pass const reference instead of pointer to GetBlockScriptFlags (MarcoFalke) Pull request description: Now that Taproot is active, it makes sense to enforce its rules on all blocks, even historic ones, regardless of the deployment status. ### Benefits: (With "script flags" I mean "taproot script verification flags".) * Script flags are known ahead for all blocks (even blocks not yet created) and do not change. This may benefit static analysis, code review, and development of new script features that build on Taproot. * Any future bugs introduced in the deployment code won't have any effect on the script flags, as they are independent of deployment. * Enforcing the taproot rules regardless of the deployment status makes testing easier because invalid blocks after activation are also invalid before activation. So there is no need to differentiate the two cases. * It gives belt-and-suspenders protection against a practically expensive and theoretically impossible IBD reorg attack where the node is eclipsed. While `nMinimumChainWork` already protects against this, the cost for a few months worth of POW might be lowered until a major version release of Bitcoin Core reaches EOL. The needed work for the attack is the difference between `nMinimumChainWork` and the work at block 709632. For reference, previously the same was done for P2SH and WITNESS in commit 0a8b7b4. ### Implementation: I found one block which fails verification with the flags applied, so I added a `TaprootException`, similar to the `BIP16Exception`. For reference, the debug log: ``` ERROR: ConnectBlock(): CheckInputScripts on b10c007c60e14f9d087e0291d4d0c7869697c6681d979c6639dbd960792b4d41 failed with non-mandatory-script-verify-flag (Witness program was passed an empty witness) BlockChecked: block hash=0000000000000000000f14c35b2d841e986ab5441de8c585d5ffe55ea1e395ad state=non-mandatory-script-verify-flag (Witness program was passed an empty witness) InvalidChainFound: invalid block=0000000000000000000f14c35b2d841e986ab5441de8c585d5ffe55ea1e395ad height=692261 log2_work=92.988459 date=2021-07-23T08:24:20Z InvalidChainFound: current best=0000000000000000000067b17a4c0ffd77c29941b15ad356ca8f980af137a25d height=692260 log2_work=92.988450 date=2021-07-23T07:47:31Z ERROR: ConnectTip: ConnectBlock 0000000000000000000f14c35b2d841e986ab5441de8c585d5ffe55ea1e395ad failed, non-mandatory-script-verify-flag (Witness program was passed an empty witness) ``` Hint for testing, make sure to set `-noassumevalid`. ### Considerations Obviously this change can lead to consensus splits on the network in light of massive reorgs. Currently the last block before Taproot activation, that is the last block without the Taproot script flags set, is only buried by a few days of POW. However, when and if this patch is included in the next major release, it will be buried by a few months of POW. BIP90 considerations apply when looking at reorgs this large. ACKs for top commit: Sjors: tACK cccc1e7 achow101: ACK cccc1e7 laanwj: Code review ACK cccc1e7 ajtowns: ACK cccc1e7 ; code review; wrote a "getblockscriptflags" rpc to quickly check that blocks just had bit 17 (taproot) added; review of earlier revisions had established non-exception blocks do validate with taproot rules enabled. jamesob: ACK cccc1e7 ([`jamesob/ackr/23536.1.MarcoFalke.enforce_taproot_script_f`](https://github.com/jamesob/bitcoin/tree/ackr/23536.1.MarcoFalke.enforce_taproot_script_f)) Tree-SHA512: 00044de68939caef6420ffd588c1291c041a8b397c80a3df1e3e3487fbeae1821d23975c51c95e44e774558db76f943b00b4e27cbd0213f64a9253116dc6edde
2 parents d261531 + cccc1e7 commit 7c08d81

File tree

4 files changed

+36
-37
lines changed

4 files changed

+36
-37
lines changed

src/chainparams.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <consensus/merkle.h>
1010
#include <deploymentinfo.h>
1111
#include <hash.h> // for signet block challenge hash
12+
#include <script/interpreter.h>
1213
#include <util/system.h>
1314

1415
#include <assert.h>
@@ -65,7 +66,10 @@ class CMainParams : public CChainParams {
6566
consensus.signet_blocks = false;
6667
consensus.signet_challenge.clear();
6768
consensus.nSubsidyHalvingInterval = 210000;
68-
consensus.BIP16Exception = uint256S("0x00000000000002dc756eebf4f49723ed8d30cc28a5f108eb94b1ba88ac4f9c22");
69+
consensus.script_flag_exceptions.emplace( // BIP16 exception
70+
uint256S("0x00000000000002dc756eebf4f49723ed8d30cc28a5f108eb94b1ba88ac4f9c22"), SCRIPT_VERIFY_NONE);
71+
consensus.script_flag_exceptions.emplace( // Taproot exception
72+
uint256S("0x0000000000000000000f14c35b2d841e986ab5441de8c585d5ffe55ea1e395ad"), SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS);
6973
consensus.BIP34Height = 227931;
7074
consensus.BIP34Hash = uint256S("0x000000000000024b89b42a942fe0d9fea3bb44ab7bd1b19115dd6a759c0808b8");
7175
consensus.BIP65Height = 388381; // 000000000000000004c2b624ed5d7756c508d90fd0da2c7c679febfa6c4735f0
@@ -184,7 +188,8 @@ class CTestNetParams : public CChainParams {
184188
consensus.signet_blocks = false;
185189
consensus.signet_challenge.clear();
186190
consensus.nSubsidyHalvingInterval = 210000;
187-
consensus.BIP16Exception = uint256S("0x00000000dd30457c001f4095d208cc1296b0eed002427aa599874af7a432b105");
191+
consensus.script_flag_exceptions.emplace( // BIP16 exception
192+
uint256S("0x00000000dd30457c001f4095d208cc1296b0eed002427aa599874af7a432b105"), SCRIPT_VERIFY_NONE);
188193
consensus.BIP34Height = 21111;
189194
consensus.BIP34Hash = uint256S("0x0000000023b3a96d3484e5abb3755c413e7d41500f8e2a5c3f0dd01299cd8ef8");
190195
consensus.BIP65Height = 581885; // 00000000007f6655f22f98e72ed80d8b06dc761d5da09df0fa1dc4be4f861eb6
@@ -323,7 +328,6 @@ class SigNetParams : public CChainParams {
323328
consensus.signet_blocks = true;
324329
consensus.signet_challenge.assign(bin.begin(), bin.end());
325330
consensus.nSubsidyHalvingInterval = 210000;
326-
consensus.BIP16Exception = uint256{};
327331
consensus.BIP34Height = 1;
328332
consensus.BIP34Hash = uint256{};
329333
consensus.BIP65Height = 1;
@@ -391,7 +395,6 @@ class CRegTestParams : public CChainParams {
391395
consensus.signet_blocks = false;
392396
consensus.signet_challenge.clear();
393397
consensus.nSubsidyHalvingInterval = 150;
394-
consensus.BIP16Exception = uint256();
395398
consensus.BIP34Height = 1; // Always active unless overridden
396399
consensus.BIP34Hash = uint256();
397400
consensus.BIP65Height = 1; // Always active unless overridden

src/consensus/params.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#define BITCOIN_CONSENSUS_PARAMS_H
88

99
#include <uint256.h>
10+
1011
#include <limits>
12+
#include <map>
1113

1214
namespace Consensus {
1315

@@ -70,8 +72,13 @@ struct BIP9Deployment {
7072
struct Params {
7173
uint256 hashGenesisBlock;
7274
int nSubsidyHalvingInterval;
73-
/* Block hash that is excepted from BIP16 enforcement */
74-
uint256 BIP16Exception;
75+
/**
76+
* Hashes of blocks that
77+
* - are known to be consensus valid, and
78+
* - buried in the chain, and
79+
* - fail if the default script verify flags are applied.
80+
*/
81+
std::map<uint256, uint32_t> script_flag_exceptions;
7582
/** Block height and hash at which BIP34 becomes active */
7683
int BIP34Height;
7784
uint256 BIP34Hash;

src/validation.cpp

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ bool CheckSequenceLocksAtTip(CBlockIndex* tip,
255255
}
256256

257257
// Returns the script flags which should be checked for a given block
258-
static unsigned int GetBlockScriptFlags(const CBlockIndex* pindex, const Consensus::Params& chainparams);
258+
static unsigned int GetBlockScriptFlags(const CBlockIndex& block_index, const Consensus::Params& chainparams);
259259

260260
static void LimitMempoolSize(CTxMemPool& pool, CCoinsViewCache& coins_cache, size_t limit, std::chrono::seconds age)
261261
EXCLUSIVE_LOCKS_REQUIRED(::cs_main, pool.cs)
@@ -1013,7 +1013,7 @@ bool MemPoolAccept::ConsensusScriptChecks(const ATMPArgs& args, Workspace& ws)
10131013
// There is a similar check in CreateNewBlock() to prevent creating
10141014
// invalid blocks (using TestBlockValidity), however allowing such
10151015
// transactions into the mempool can be exploited as a DoS attack.
1016-
unsigned int currentBlockScriptVerifyFlags = GetBlockScriptFlags(m_active_chainstate.m_chain.Tip(), chainparams.GetConsensus());
1016+
unsigned int currentBlockScriptVerifyFlags{GetBlockScriptFlags(*m_active_chainstate.m_chain.Tip(), chainparams.GetConsensus())};
10171017
if (!CheckInputsFromMempoolAndCache(tx, state, m_view, m_pool, currentBlockScriptVerifyFlags,
10181018
ws.m_precomputed_txdata, m_active_chainstate.CoinsTip())) {
10191019
LogPrintf("BUG! PLEASE REPORT THIS! CheckInputScripts failed against latest-block but not STANDARD flags %s, %s\n", hash.ToString(), state.ToString());
@@ -1854,53 +1854,46 @@ class WarningBitsConditionChecker : public AbstractThresholdConditionChecker
18541854

18551855
static ThresholdConditionCache warningcache[VERSIONBITS_NUM_BITS] GUARDED_BY(cs_main);
18561856

1857-
static unsigned int GetBlockScriptFlags(const CBlockIndex* pindex, const Consensus::Params& consensusparams)
1857+
static unsigned int GetBlockScriptFlags(const CBlockIndex& block_index, const Consensus::Params& consensusparams)
18581858
{
1859-
unsigned int flags = SCRIPT_VERIFY_NONE;
1860-
18611859
// BIP16 didn't become active until Apr 1 2012 (on mainnet, and
18621860
// retroactively applied to testnet)
18631861
// However, only one historical block violated the P2SH rules (on both
1864-
// mainnet and testnet), so for simplicity, always leave P2SH
1865-
// on except for the one violating block.
1866-
if (consensusparams.BIP16Exception.IsNull() || // no bip16 exception on this chain
1867-
pindex->phashBlock == nullptr || // this is a new candidate block, eg from TestBlockValidity()
1868-
*pindex->phashBlock != consensusparams.BIP16Exception) // this block isn't the historical exception
1869-
{
1870-
// Enforce WITNESS rules whenever P2SH is in effect
1871-
flags |= SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS;
1862+
// mainnet and testnet).
1863+
// Similarly, only one historical block violated the TAPROOT rules on
1864+
// mainnet.
1865+
// For simplicity, always leave P2SH+WITNESS+TAPROOT on except for the two
1866+
// violating blocks.
1867+
uint32_t flags{SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_TAPROOT};
1868+
const auto it{consensusparams.script_flag_exceptions.find(*Assert(block_index.phashBlock))};
1869+
if (it != consensusparams.script_flag_exceptions.end()) {
1870+
flags = it->second;
18721871
}
18731872

18741873
// Enforce the DERSIG (BIP66) rule
1875-
if (DeploymentActiveAt(*pindex, consensusparams, Consensus::DEPLOYMENT_DERSIG)) {
1874+
if (DeploymentActiveAt(block_index, consensusparams, Consensus::DEPLOYMENT_DERSIG)) {
18761875
flags |= SCRIPT_VERIFY_DERSIG;
18771876
}
18781877

18791878
// Enforce CHECKLOCKTIMEVERIFY (BIP65)
1880-
if (DeploymentActiveAt(*pindex, consensusparams, Consensus::DEPLOYMENT_CLTV)) {
1879+
if (DeploymentActiveAt(block_index, consensusparams, Consensus::DEPLOYMENT_CLTV)) {
18811880
flags |= SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY;
18821881
}
18831882

18841883
// Enforce CHECKSEQUENCEVERIFY (BIP112)
1885-
if (DeploymentActiveAt(*pindex, consensusparams, Consensus::DEPLOYMENT_CSV)) {
1884+
if (DeploymentActiveAt(block_index, consensusparams, Consensus::DEPLOYMENT_CSV)) {
18861885
flags |= SCRIPT_VERIFY_CHECKSEQUENCEVERIFY;
18871886
}
18881887

1889-
// Enforce Taproot (BIP340-BIP342)
1890-
if (DeploymentActiveAt(*pindex, consensusparams, Consensus::DEPLOYMENT_TAPROOT)) {
1891-
flags |= SCRIPT_VERIFY_TAPROOT;
1892-
}
1893-
18941888
// Enforce BIP147 NULLDUMMY (activated simultaneously with segwit)
1895-
if (DeploymentActiveAt(*pindex, consensusparams, Consensus::DEPLOYMENT_SEGWIT)) {
1889+
if (DeploymentActiveAt(block_index, consensusparams, Consensus::DEPLOYMENT_SEGWIT)) {
18961890
flags |= SCRIPT_VERIFY_NULLDUMMY;
18971891
}
18981892

18991893
return flags;
19001894
}
19011895

19021896

1903-
19041897
static int64_t nTimeCheck = 0;
19051898
static int64_t nTimeForks = 0;
19061899
static int64_t nTimeVerify = 0;
@@ -2088,7 +2081,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
20882081
}
20892082

20902083
// Get the script flags for this block
2091-
unsigned int flags = GetBlockScriptFlags(pindex, m_params.GetConsensus());
2084+
unsigned int flags{GetBlockScriptFlags(*pindex, m_params.GetConsensus())};
20922085

20932086
int64_t nTime2 = GetTimeMicros(); nTimeForks += nTime2 - nTime1;
20942087
LogPrint(BCLog::BENCH, " - Fork checks: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime2 - nTime1), nTimeForks * MICRO, nTimeForks * MILLI / nBlocksTotal);

test/functional/feature_taproot.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,15 +1182,11 @@ def spenders_taproot_inactive():
11821182
]
11831183
tap = taproot_construct(pub, scripts)
11841184

1185-
# Test that keypath spending is valid & non-standard, regardless of validity.
1185+
# Test that valid spending is standard.
11861186
add_spender(spenders, "inactive/keypath_valid", key=sec, tap=tap, standard=Standard.V23)
1187-
add_spender(spenders, "inactive/keypath_invalidsig", key=sec, tap=tap, standard=False, sighash=bitflipper(default_sighash))
1188-
add_spender(spenders, "inactive/keypath_empty", key=sec, tap=tap, standard=False, witness=[])
1189-
1190-
# Same for scriptpath spending (and features like annex, leaf versions, or OP_SUCCESS don't change this)
11911187
add_spender(spenders, "inactive/scriptpath_valid", key=sec, tap=tap, leaf="pk", standard=Standard.V23, inputs=[getter("sign")])
1192-
add_spender(spenders, "inactive/scriptpath_invalidsig", key=sec, tap=tap, leaf="pk", standard=False, inputs=[getter("sign")], sighash=bitflipper(default_sighash))
1193-
add_spender(spenders, "inactive/scriptpath_invalidcb", key=sec, tap=tap, leaf="pk", standard=False, inputs=[getter("sign")], controlblock=bitflipper(default_controlblock))
1188+
1189+
# Test that features like annex, leaf versions, or OP_SUCCESS are valid but non-standard
11941190
add_spender(spenders, "inactive/scriptpath_valid_unkleaf", key=sec, tap=tap, leaf="future_leaf", standard=False, inputs=[getter("sign")])
11951191
add_spender(spenders, "inactive/scriptpath_invalid_unkleaf", key=sec, tap=tap, leaf="future_leaf", standard=False, inputs=[getter("sign")], sighash=bitflipper(default_sighash))
11961192
add_spender(spenders, "inactive/scriptpath_valid_opsuccess", key=sec, tap=tap, leaf="op_success", standard=False, inputs=[getter("sign")])

0 commit comments

Comments
 (0)