Skip to content

Commit de55304

Browse files
committed
[refactor] Add versionbits deployments to deploymentstatus.h
Adds support for versionbits deployments to DeploymentEnabled, DeploymentActiveAfter and DeploymentActiveAt. Also moves versionbitscache from validation to deploymentstatus.
1 parent 2b0d291 commit de55304

File tree

9 files changed

+50
-12
lines changed

9 files changed

+50
-12
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ libbitcoin_server_a_SOURCES = \
329329
chain.cpp \
330330
consensus/tx_verify.cpp \
331331
dbwrapper.cpp \
332+
deploymentstatus.cpp \
332333
flatfile.cpp \
333334
httprpc.cpp \
334335
httpserver.cpp \

src/consensus/params.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ enum BuriedDeployment : int16_t
2222
};
2323
constexpr bool ValidDeployment(BuriedDeployment dep) { return DEPLOYMENT_HEIGHTINCB <= dep && dep <= DEPLOYMENT_SEGWIT; }
2424

25-
enum DeploymentPos
25+
enum DeploymentPos : uint16_t
2626
{
2727
DEPLOYMENT_TESTDUMMY,
2828
DEPLOYMENT_TAPROOT, // Deployment of Schnorr/Taproot (BIPs 340-342)
2929
// NOTE: Also add new deployments to VersionBitsDeploymentInfo in versionbits.cpp
3030
MAX_VERSION_BITS_DEPLOYMENTS
3131
};
32+
constexpr bool ValidDeployment(DeploymentPos dep) { return DEPLOYMENT_TESTDUMMY <= dep && dep <= DEPLOYMENT_TAPROOT; }
3233

3334
/**
3435
* Struct for each individual consensus rule change using BIP9.

src/deploymentstatus.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2020 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <deploymentstatus.h>
6+
7+
#include <consensus/params.h>
8+
#include <versionbits.h>
9+
10+
VersionBitsCache versionbitscache;
11+
12+
/* Basic sanity checking for BuriedDeployment/DeploymentPos enums and
13+
* ValidDeployment check */
14+
15+
static_assert(ValidDeployment(Consensus::DEPLOYMENT_TESTDUMMY), "sanity check of DeploymentPos failed (TESTDUMMY not valid)");
16+
static_assert(!ValidDeployment(Consensus::MAX_VERSION_BITS_DEPLOYMENTS), "sanity check of DeploymentPos failed (MAX value considered valid)");
17+
static_assert(!ValidDeployment(static_cast<Consensus::BuriedDeployment>(Consensus::DEPLOYMENT_TESTDUMMY)), "sanity check of BuriedDeployment failed (overlaps with DeploymentPos)");

src/deploymentstatus.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,50 @@
66
#define BITCOIN_DEPLOYMENTSTATUS_H
77

88
#include <chain.h>
9+
#include <versionbits.h>
910

1011
#include <limits>
1112

13+
/** Global cache for versionbits deployment status */
14+
extern VersionBitsCache versionbitscache;
15+
1216
/** Determine if a deployment is active for the next block */
1317
inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::BuriedDeployment dep)
1418
{
1519
assert(Consensus::ValidDeployment(dep));
1620
return (pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1) >= params.DeploymentHeight(dep);
1721
}
1822

23+
inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos dep)
24+
{
25+
assert(Consensus::ValidDeployment(dep));
26+
return ThresholdState::ACTIVE == VersionBitsState(pindexPrev, params, dep, versionbitscache);
27+
}
28+
1929
/** Determine if a deployment is active for this block */
2030
inline bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params& params, Consensus::BuriedDeployment dep)
2131
{
2232
assert(Consensus::ValidDeployment(dep));
2333
return index.nHeight >= params.DeploymentHeight(dep);
2434
}
2535

36+
inline bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params& params, Consensus::DeploymentPos dep)
37+
{
38+
assert(Consensus::ValidDeployment(dep));
39+
return DeploymentActiveAfter(index.pprev, params, dep);
40+
}
41+
2642
/** Determine if a deployment is enabled (can ever be active) */
2743
inline bool DeploymentEnabled(const Consensus::Params& params, Consensus::BuriedDeployment dep)
2844
{
2945
assert(Consensus::ValidDeployment(dep));
3046
return params.DeploymentHeight(dep) != std::numeric_limits<int>::max();
3147
}
3248

49+
inline bool DeploymentEnabled(const Consensus::Params& params, Consensus::DeploymentPos dep)
50+
{
51+
assert(Consensus::ValidDeployment(dep));
52+
return params.vDeployments[dep].nTimeout != 0;
53+
}
54+
3355
#endif // BITCOIN_DEPLOYMENTSTATUS_H

src/node/interfaces.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <banman.h>
77
#include <chain.h>
88
#include <chainparams.h>
9+
#include <deploymentstatus.h>
910
#include <external_signer.h>
1011
#include <init.h>
1112
#include <interfaces/chain.h>
@@ -692,7 +693,7 @@ class ChainImpl : public Chain
692693
{
693694
LOCK(::cs_main);
694695
const CBlockIndex* tip = Assert(m_node.chainman)->ActiveChain().Tip();
695-
return VersionBitsState(tip, Params().GetConsensus(), Consensus::DEPLOYMENT_TAPROOT, versionbitscache) == ThresholdState::ACTIVE;
696+
return DeploymentActiveAfter(tip, Params().GetConsensus(), Consensus::DEPLOYMENT_TAPROOT);
696697
}
697698
NodeContext& m_node;
698699
};

src/rpc/blockchain.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <coins.h>
1313
#include <consensus/validation.h>
1414
#include <core_io.h>
15+
#include <deploymentstatus.h>
1516
#include <hash.h>
1617
#include <index/blockfilterindex.h>
1718
#include <index/coinstatsindex.h>

src/test/versionbits_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <chain.h>
66
#include <chainparams.h>
77
#include <consensus/params.h>
8+
#include <deploymentstatus.h>
89
#include <test/util/setup_common.h>
910
#include <validation.h>
1011
#include <versionbits.h>

src/validation.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -684,9 +684,8 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
684684
}
685685

686686
// Check for non-standard pay-to-script-hash in inputs
687-
const auto& params = args.m_chainparams.GetConsensus();
688-
auto taproot_state = VersionBitsState(m_active_chainstate.m_chain.Tip(), params, Consensus::DEPLOYMENT_TAPROOT, versionbitscache);
689-
if (fRequireStandard && !AreInputsStandard(tx, m_view, taproot_state == ThresholdState::ACTIVE)) {
687+
const bool taproot_active = DeploymentActiveAfter(m_active_chainstate.m_chain.Tip(), args.m_chainparams.GetConsensus(), Consensus::DEPLOYMENT_TAPROOT);
688+
if (fRequireStandard && !AreInputsStandard(tx, m_view, taproot_active)) {
690689
return state.Invalid(TxValidationResult::TX_INPUTS_NOT_STANDARD, "bad-txns-nonstandard-inputs");
691690
}
692691

@@ -1607,8 +1606,6 @@ void StopScriptCheckWorkerThreads()
16071606
scriptcheckqueue.StopWorkerThreads();
16081607
}
16091608

1610-
VersionBitsCache versionbitscache;
1611-
16121609
int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params)
16131610
{
16141611
int32_t nVersion = VERSIONBITS_TOP_BITS;
@@ -1687,8 +1684,8 @@ static unsigned int GetBlockScriptFlags(const CBlockIndex* pindex, const Consens
16871684
flags |= SCRIPT_VERIFY_CHECKSEQUENCEVERIFY;
16881685
}
16891686

1690-
// Start enforcing Taproot using versionbits logic.
1691-
if (VersionBitsState(pindex->pprev, consensusparams, Consensus::DEPLOYMENT_TAPROOT, versionbitscache) == ThresholdState::ACTIVE) {
1687+
// Enforce Taproot (BIP340-BIP342)
1688+
if (DeploymentActiveAt(*pindex, consensusparams, Consensus::DEPLOYMENT_TAPROOT)) {
16921689
flags |= SCRIPT_VERIFY_TAPROOT;
16931690
}
16941691

src/validation.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <sync.h>
2525
#include <txmempool.h> // For CTxMemPool::cs
2626
#include <txdb.h>
27-
#include <versionbits.h>
2827
#include <serialize.h>
2928
#include <util/check.h>
3029
#include <util/hasher.h>
@@ -1020,8 +1019,6 @@ class ChainstateManager
10201019
/** Global variable that points to the active block tree (protected by cs_main) */
10211020
extern std::unique_ptr<CBlockTreeDB> pblocktree;
10221021

1023-
extern VersionBitsCache versionbitscache;
1024-
10251022
/**
10261023
* Determine what nVersion a new block should use.
10271024
*/

0 commit comments

Comments
 (0)