mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-26 03:03:22 -03:00
7c08d81e11
cccc1e70b8
Enforce Taproot script flags whenever WITNESS is set (MarcoFalke)fa42299411
Remove nullptr check in GetBlockScriptFlags (MarcoFalke)faadc606c7
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 commit0a8b7b4b33
. ### 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: tACKcccc1e70b8
achow101: ACKcccc1e70b8
laanwj: Code review ACKcccc1e70b8
ajtowns: ACKcccc1e70b8
; 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: ACKcccc1e70b8
([`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
145 lines
5.4 KiB
C++
145 lines
5.4 KiB
C++
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-2021 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#ifndef BITCOIN_CONSENSUS_PARAMS_H
|
|
#define BITCOIN_CONSENSUS_PARAMS_H
|
|
|
|
#include <uint256.h>
|
|
|
|
#include <limits>
|
|
#include <map>
|
|
|
|
namespace Consensus {
|
|
|
|
/**
|
|
* A buried deployment is one where the height of the activation has been hardcoded into
|
|
* the client implementation long after the consensus change has activated. See BIP 90.
|
|
*/
|
|
enum BuriedDeployment : int16_t {
|
|
// buried deployments get negative values to avoid overlap with DeploymentPos
|
|
DEPLOYMENT_HEIGHTINCB = std::numeric_limits<int16_t>::min(),
|
|
DEPLOYMENT_CLTV,
|
|
DEPLOYMENT_DERSIG,
|
|
DEPLOYMENT_CSV,
|
|
DEPLOYMENT_SEGWIT,
|
|
};
|
|
constexpr bool ValidDeployment(BuriedDeployment dep) { return dep <= DEPLOYMENT_SEGWIT; }
|
|
|
|
enum DeploymentPos : uint16_t {
|
|
DEPLOYMENT_TESTDUMMY,
|
|
DEPLOYMENT_TAPROOT, // Deployment of Schnorr/Taproot (BIPs 340-342)
|
|
// NOTE: Also add new deployments to VersionBitsDeploymentInfo in deploymentinfo.cpp
|
|
MAX_VERSION_BITS_DEPLOYMENTS
|
|
};
|
|
constexpr bool ValidDeployment(DeploymentPos dep) { return dep < MAX_VERSION_BITS_DEPLOYMENTS; }
|
|
|
|
/**
|
|
* Struct for each individual consensus rule change using BIP9.
|
|
*/
|
|
struct BIP9Deployment {
|
|
/** Bit position to select the particular bit in nVersion. */
|
|
int bit;
|
|
/** Start MedianTime for version bits miner confirmation. Can be a date in the past */
|
|
int64_t nStartTime;
|
|
/** Timeout/expiry MedianTime for the deployment attempt. */
|
|
int64_t nTimeout;
|
|
/** If lock in occurs, delay activation until at least this block
|
|
* height. Note that activation will only occur on a retarget
|
|
* boundary.
|
|
*/
|
|
int min_activation_height{0};
|
|
|
|
/** Constant for nTimeout very far in the future. */
|
|
static constexpr int64_t NO_TIMEOUT = std::numeric_limits<int64_t>::max();
|
|
|
|
/** Special value for nStartTime indicating that the deployment is always active.
|
|
* This is useful for testing, as it means tests don't need to deal with the activation
|
|
* process (which takes at least 3 BIP9 intervals). Only tests that specifically test the
|
|
* behaviour during activation cannot use this. */
|
|
static constexpr int64_t ALWAYS_ACTIVE = -1;
|
|
|
|
/** Special value for nStartTime indicating that the deployment is never active.
|
|
* This is useful for integrating the code changes for a new feature
|
|
* prior to deploying it on some or all networks. */
|
|
static constexpr int64_t NEVER_ACTIVE = -2;
|
|
};
|
|
|
|
/**
|
|
* Parameters that influence chain consensus.
|
|
*/
|
|
struct Params {
|
|
uint256 hashGenesisBlock;
|
|
int nSubsidyHalvingInterval;
|
|
/**
|
|
* Hashes of blocks that
|
|
* - are known to be consensus valid, and
|
|
* - buried in the chain, and
|
|
* - fail if the default script verify flags are applied.
|
|
*/
|
|
std::map<uint256, uint32_t> script_flag_exceptions;
|
|
/** Block height and hash at which BIP34 becomes active */
|
|
int BIP34Height;
|
|
uint256 BIP34Hash;
|
|
/** Block height at which BIP65 becomes active */
|
|
int BIP65Height;
|
|
/** Block height at which BIP66 becomes active */
|
|
int BIP66Height;
|
|
/** Block height at which CSV (BIP68, BIP112 and BIP113) becomes active */
|
|
int CSVHeight;
|
|
/** Block height at which Segwit (BIP141, BIP143 and BIP147) becomes active.
|
|
* Note that segwit v0 script rules are enforced on all blocks except the
|
|
* BIP 16 exception blocks. */
|
|
int SegwitHeight;
|
|
/** Don't warn about unknown BIP 9 activations below this height.
|
|
* This prevents us from warning about the CSV and segwit activations. */
|
|
int MinBIP9WarningHeight;
|
|
/**
|
|
* Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period,
|
|
* (nPowTargetTimespan / nPowTargetSpacing) which is also used for BIP9 deployments.
|
|
* Examples: 1916 for 95%, 1512 for testchains.
|
|
*/
|
|
uint32_t nRuleChangeActivationThreshold;
|
|
uint32_t nMinerConfirmationWindow;
|
|
BIP9Deployment vDeployments[MAX_VERSION_BITS_DEPLOYMENTS];
|
|
/** Proof of work parameters */
|
|
uint256 powLimit;
|
|
bool fPowAllowMinDifficultyBlocks;
|
|
bool fPowNoRetargeting;
|
|
int64_t nPowTargetSpacing;
|
|
int64_t nPowTargetTimespan;
|
|
int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespan / nPowTargetSpacing; }
|
|
/** The best chain should have at least this much work */
|
|
uint256 nMinimumChainWork;
|
|
/** By default assume that the signatures in ancestors of this block are valid */
|
|
uint256 defaultAssumeValid;
|
|
|
|
/**
|
|
* If true, witness commitments contain a payload equal to a Bitcoin Script solution
|
|
* to the signet challenge. See BIP325.
|
|
*/
|
|
bool signet_blocks{false};
|
|
std::vector<uint8_t> signet_challenge;
|
|
|
|
int DeploymentHeight(BuriedDeployment dep) const
|
|
{
|
|
switch (dep) {
|
|
case DEPLOYMENT_HEIGHTINCB:
|
|
return BIP34Height;
|
|
case DEPLOYMENT_CLTV:
|
|
return BIP65Height;
|
|
case DEPLOYMENT_DERSIG:
|
|
return BIP66Height;
|
|
case DEPLOYMENT_CSV:
|
|
return CSVHeight;
|
|
case DEPLOYMENT_SEGWIT:
|
|
return SegwitHeight;
|
|
} // no default case, so the compiler can warn about missing cases
|
|
return std::numeric_limits<int>::max();
|
|
}
|
|
};
|
|
|
|
} // namespace Consensus
|
|
|
|
#endif // BITCOIN_CONSENSUS_PARAMS_H
|