Skip to content

Commit 18b5805

Browse files
PastaPastaPastaogabrielides
authored andcommitted
Merge pull request dashpay#5740 from knst/bp-versionbits
backport: bitcoin#19438 Introduce deploymentstatus (versionbits improvements)
1 parent 5e8140d commit 18b5805

23 files changed

+378
-183
lines changed

src/Makefile.am

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ BITCOIN_CORE_H = \
169169
cuckoocache.h \
170170
ctpl_stl.h \
171171
cxxtimer.hpp \
172+
deploymentinfo.h \
173+
deploymentstatus.h \
172174
evo/assetlocktx.h \
173175
evo/dmn_types.h \
174176
evo/cbtx.h \
@@ -349,7 +351,6 @@ BITCOIN_CORE_H = \
349351
validation.h \
350352
validationinterface.h \
351353
versionbits.h \
352-
versionbitsinfo.h \
353354
walletinitinterface.h \
354355
wallet/bdb.h \
355356
wallet/coincontrol.h \
@@ -403,6 +404,7 @@ libbitcoin_server_a_SOURCES = \
403404
coinjoin/server.cpp \
404405
consensus/tx_verify.cpp \
405406
dbwrapper.cpp \
407+
deploymentstatus.cpp \
406408
dsnotificationinterface.cpp \
407409
evo/assetlocktx.cpp \
408410
evo/cbtx.cpp \
@@ -696,6 +698,7 @@ libbitcoin_common_a_SOURCES = \
696698
compressor.cpp \
697699
core_read.cpp \
698700
core_write.cpp \
701+
deploymentinfo.cpp \
699702
key.cpp \
700703
key_io.cpp \
701704
merkleblock.cpp \
@@ -715,7 +718,6 @@ libbitcoin_common_a_SOURCES = \
715718
script/sign.cpp \
716719
script/signingprovider.cpp \
717720
script/standard.cpp \
718-
versionbitsinfo.cpp \
719721
warnings.cpp \
720722
$(BITCOIN_CORE_H)
721723

src/chainparams.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
#include <chainparamsseeds.h>
1010
#include <consensus/merkle.h>
11+
#include <deploymentinfo.h>
1112
#include <llmq/params.h>
1213
#include <util/ranges.h>
1314
#include <util/system.h>
1415
#include <util/underlying.h>
1516
#include <versionbits.h>
16-
#include <versionbitsinfo.h>
1717

1818
#include <arith_uint256.h>
1919

src/consensus/params.h

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,32 @@
1414

1515
namespace Consensus {
1616

17-
enum DeploymentPos {
17+
enum BuriedDeployment : int16_t
18+
{
19+
DEPLOYMENT_HEIGHTINCB = std::numeric_limits<int16_t>::min(),
20+
DEPLOYMENT_DERSIG,
21+
DEPLOYMENT_CLTV,
22+
DEPLOYMENT_BIP147,
23+
DEPLOYMENT_CSV,
24+
DEPLOYMENT_DIP0001,
25+
DEPLOYMENT_DIP0003,
26+
DEPLOYMENT_DIP0008,
27+
DEPLOYMENT_DIP0020,
28+
DEPLOYMENT_DIP0024,
29+
DEPLOYMENT_BRR,
30+
DEPLOYMENT_V19,
31+
};
32+
constexpr bool ValidDeployment(BuriedDeployment dep) { return DEPLOYMENT_HEIGHTINCB <= dep && dep <= DEPLOYMENT_V19; }
33+
34+
enum DeploymentPos : uint16_t
35+
{
1836
DEPLOYMENT_TESTDUMMY,
1937
DEPLOYMENT_V20, // Deployment of EHF, LLMQ Randomness Beacon
2038
DEPLOYMENT_MN_RR, // Deployment of Masternode Reward Location Reallocation
21-
// NOTE: Also add new deployments to VersionBitsDeploymentInfo in versionbits.cpp
39+
// NOTE: Also add new deployments to VersionBitsDeploymentInfo in deploymentinfo.cpp
2240
MAX_VERSION_BITS_DEPLOYMENTS
2341
};
42+
constexpr bool ValidDeployment(DeploymentPos dep) { return DEPLOYMENT_TESTDUMMY <= dep && dep <= DEPLOYMENT_MN_RR; }
2443

2544
/**
2645
* Struct for each individual consensus rule change using BIP9.
@@ -145,7 +164,39 @@ struct Params {
145164
LLMQType llmqTypePlatform{LLMQType::LLMQ_NONE};
146165
LLMQType llmqTypeMnhf{LLMQType::LLMQ_NONE};
147166
LLMQType llmqTypeAssetLocks{LLMQType::LLMQ_NONE};
167+
168+
int DeploymentHeight(BuriedDeployment dep) const
169+
{
170+
switch (dep) {
171+
case DEPLOYMENT_HEIGHTINCB:
172+
return BIP34Height;
173+
case DEPLOYMENT_DERSIG:
174+
return BIP66Height;
175+
case DEPLOYMENT_CLTV:
176+
return BIP65Height;
177+
case DEPLOYMENT_BIP147:
178+
return BIP147Height;
179+
case DEPLOYMENT_CSV:
180+
return CSVHeight;
181+
case DEPLOYMENT_DIP0001:
182+
return DIP0001Height;
183+
case DEPLOYMENT_DIP0003:
184+
return DIP0003Height;
185+
case DEPLOYMENT_DIP0008:
186+
return DIP0008Height;
187+
case DEPLOYMENT_DIP0020:
188+
return DIP0020Height;
189+
case DEPLOYMENT_DIP0024:
190+
return DIP0024Height;
191+
case DEPLOYMENT_BRR:
192+
return BRRHeight;
193+
case DEPLOYMENT_V19:
194+
return V19Height;
195+
} // no default case, so the compiler can warn about missing cases
196+
return std::numeric_limits<int>::max();
197+
}
148198
};
199+
149200
} // namespace Consensus
150201

151202
#endif // BITCOIN_CONSENSUS_PARAMS_H

src/deploymentinfo.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) 2016-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 <deploymentinfo.h>
6+
7+
#include <consensus/params.h>
8+
9+
const struct VBDeploymentInfo VersionBitsDeploymentInfo[Consensus::MAX_VERSION_BITS_DEPLOYMENTS] = {
10+
{
11+
/*.name =*/ "testdummy",
12+
/*.gbt_force =*/ true,
13+
},
14+
{
15+
/*.name =*/"v20",
16+
/*.gbt_force =*/true,
17+
},
18+
{
19+
/*.name =*/"mn_rr",
20+
/*.gbt_force =*/true,
21+
},
22+
};
23+
24+
std::string DeploymentName(Consensus::BuriedDeployment dep)
25+
{
26+
assert(ValidDeployment(dep));
27+
switch (dep) {
28+
case Consensus::DEPLOYMENT_HEIGHTINCB:
29+
return "bip34";
30+
case Consensus::DEPLOYMENT_CLTV:
31+
return "bip65";
32+
case Consensus::DEPLOYMENT_DERSIG:
33+
return "bip66";
34+
case Consensus::DEPLOYMENT_BIP147:
35+
return "bip147";
36+
case Consensus::DEPLOYMENT_CSV:
37+
return "csv";
38+
case Consensus::DEPLOYMENT_DIP0001:
39+
return "dip0001";
40+
case Consensus::DEPLOYMENT_DIP0003:
41+
return "dip0003";
42+
case Consensus::DEPLOYMENT_DIP0008:
43+
return "dip0008";
44+
case Consensus::DEPLOYMENT_DIP0020:
45+
return "dip0020";
46+
case Consensus::DEPLOYMENT_DIP0024:
47+
return "dip0024";
48+
case Consensus::DEPLOYMENT_BRR:
49+
return "realloc";
50+
case Consensus::DEPLOYMENT_V19:
51+
return "v19";
52+
} // no default case, so the compiler can warn about missing cases
53+
return "";
54+
}

src/deploymentinfo.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2016-2018 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+
#ifndef BITCOIN_DEPLOYMENTINFO_H
6+
#define BITCOIN_DEPLOYMENTINFO_H
7+
8+
#include <consensus/params.h>
9+
10+
#include <string>
11+
12+
struct VBDeploymentInfo {
13+
/** Deployment name */
14+
const char *name;
15+
/** Whether GBT clients can safely ignore this rule in simplified usage */
16+
bool gbt_force;
17+
};
18+
19+
extern const VBDeploymentInfo VersionBitsDeploymentInfo[Consensus::MAX_VERSION_BITS_DEPLOYMENTS];
20+
21+
std::string DeploymentName(Consensus::BuriedDeployment dep);
22+
23+
inline std::string DeploymentName(Consensus::DeploymentPos pos)
24+
{
25+
assert(Consensus::ValidDeployment(pos));
26+
return VersionBitsDeploymentInfo[pos].name;
27+
}
28+
29+
#endif // BITCOIN_DEPLOYMENTINFO_H

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 g_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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
#ifndef BITCOIN_DEPLOYMENTSTATUS_H
6+
#define BITCOIN_DEPLOYMENTSTATUS_H
7+
8+
#include <chain.h>
9+
#include <versionbits.h>
10+
11+
#include <limits>
12+
13+
/** Global cache for versionbits deployment status */
14+
extern VersionBitsCache g_versionbitscache;
15+
16+
/** Determine if a deployment is active for the next block */
17+
inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::BuriedDeployment dep)
18+
{
19+
assert(Consensus::ValidDeployment(dep));
20+
return (pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1) >= params.DeploymentHeight(dep);
21+
}
22+
23+
inline bool DeploymentActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos dep)
24+
{
25+
assert(Consensus::ValidDeployment(dep));
26+
return ThresholdState::ACTIVE == g_versionbitscache.State(pindexPrev, params, dep);
27+
}
28+
29+
/** Determine if a deployment is active for this block */
30+
inline bool DeploymentActiveAt(const CBlockIndex& index, const Consensus::Params& params, Consensus::BuriedDeployment dep)
31+
{
32+
assert(Consensus::ValidDeployment(dep));
33+
return index.nHeight >= params.DeploymentHeight(dep);
34+
}
35+
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+
42+
/** Determine if a deployment is enabled (can ever be active) */
43+
inline bool DeploymentEnabled(const Consensus::Params& params, Consensus::BuriedDeployment dep)
44+
{
45+
assert(Consensus::ValidDeployment(dep));
46+
return params.DeploymentHeight(dep) != std::numeric_limits<int>::max();
47+
}
48+
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+
55+
#endif // BITCOIN_DEPLOYMENTSTATUS_H

src/evo/mnhftx.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <consensus/validation.h>
6+
#include <deploymentstatus.h>
67
#include <evo/mnhftx.h>
78
#include <evo/specialtx.h>
89
#include <llmq/commitment.h>
@@ -278,10 +279,12 @@ CMNHFManager::Signals CMNHFManager::GetFromCache(const CBlockIndex* const pindex
278279
return signals;
279280
}
280281
}
281-
if (VersionBitsState(pindex->pprev, Params().GetConsensus(), Consensus::DEPLOYMENT_V20, versionbitscache) != ThresholdState::ACTIVE) {
282+
{
282283
LOCK(cs_cache);
283-
mnhfCache.insert(blockHash, {});
284-
return {};
284+
if (ThresholdState::ACTIVE != v20_activation.State(pindex->pprev, Params().GetConsensus(), Consensus::DEPLOYMENT_V20)) {
285+
mnhfCache.insert(blockHash, {});
286+
return {};
287+
}
285288
}
286289
bool ok = m_evoDb.Read(std::make_pair(DB_SIGNALS, blockHash), signals);
287290
assert(ok);
@@ -297,8 +300,10 @@ void CMNHFManager::AddToCache(const Signals& signals, const CBlockIndex* const p
297300
LOCK(cs_cache);
298301
mnhfCache.insert(blockHash, signals);
299302
}
300-
if (VersionBitsState(pindex->pprev, Params().GetConsensus(), Consensus::DEPLOYMENT_V20, versionbitscache) != ThresholdState::ACTIVE) {
301-
return;
303+
assert(pindex != nullptr);
304+
{
305+
LOCK(cs_cache);
306+
if (ThresholdState::ACTIVE != v20_activation.State(pindex->pprev, Params().GetConsensus(), Consensus::DEPLOYMENT_V20)) return;
302307
}
303308
m_evoDb.Write(std::make_pair(DB_SIGNALS, blockHash), signals);
304309
}

src/evo/mnhftx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ class CMNHFManager : public AbstractEHFManager
102102
// versionBit <-> height
103103
unordered_lru_cache<uint256, Signals, StaticSaltedHasher> mnhfCache GUARDED_BY(cs_cache) {MNHFCacheSize};
104104

105+
// This cache is used only for v20 activation to avoid double lock throught VersionBitsConditionChecker::SignalHeight
106+
VersionBitsCache v20_activation GUARDED_BY(cs_cache);
105107
public:
106108
explicit CMNHFManager(CEvoDB& evoDb);
107109
~CMNHFManager();

src/governance/classes.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <util/time.h>
1818
#include <util/underlying.h>
1919
#include <validation.h>
20+
#include <versionbits.h>
2021

2122
#include <univalue.h>
2223

0 commit comments

Comments
 (0)