Skip to content

Commit 66ed450

Browse files
committed
Merge #7935: Versionbits: GBT support
12c708a getblocktemplate: Use version/force mutation to support pre-BIP9 clients (Luke Dashjr) 9879060 getblocktemplate: Explicitly handle the distinction between GBT-affecting softforks vs not (Luke Dashjr) 72cd6b2 qa/rpc-tests: bip9-softforks: Add tests for getblocktemplate versionbits updates (Luke Dashjr) d3df40e Implement BIP 9 GBT changes (Luke Dashjr)
2 parents 6a034ed + 12c708a commit 66ed450

File tree

7 files changed

+150
-14
lines changed

7 files changed

+150
-14
lines changed

qa/rpc-tests/bip9-softforks.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def get_bip9_status(self, key):
8080
info = self.nodes[0].getblockchaininfo()
8181
return info['bip9_softforks'][key]
8282

83-
def test_BIP(self, bipName, activated_version, invalidate, invalidatePostSignature):
83+
def test_BIP(self, bipName, activated_version, invalidate, invalidatePostSignature, bitno):
8484
# generate some coins for later
8585
self.coinbase_blocks = self.nodes[0].generate(2)
8686
self.height = 3 # height of the next block to build
@@ -89,13 +89,23 @@ def test_BIP(self, bipName, activated_version, invalidate, invalidatePostSignatu
8989
self.last_block_time = int(time.time())
9090

9191
assert_equal(self.get_bip9_status(bipName)['status'], 'defined')
92+
tmpl = self.nodes[0].getblocktemplate({})
93+
assert(bipName not in tmpl['rules'])
94+
assert(bipName not in tmpl['vbavailable'])
95+
assert_equal(tmpl['vbrequired'], 0)
96+
assert_equal(tmpl['version'], 0x20000000)
9297

9398
# Test 1
9499
# Advance from DEFINED to STARTED
95100
test_blocks = self.generate_blocks(141, 4)
96101
yield TestInstance(test_blocks, sync_every_block=False)
97102

98103
assert_equal(self.get_bip9_status(bipName)['status'], 'started')
104+
tmpl = self.nodes[0].getblocktemplate({})
105+
assert(bipName not in tmpl['rules'])
106+
assert_equal(tmpl['vbavailable'][bipName], bitno)
107+
assert_equal(tmpl['vbrequired'], 0)
108+
assert(tmpl['version'] & activated_version)
99109

100110
# Test 2
101111
# Fail to achieve LOCKED_IN 100 out of 144 signal bit 1
@@ -107,6 +117,11 @@ def test_BIP(self, bipName, activated_version, invalidate, invalidatePostSignatu
107117
yield TestInstance(test_blocks, sync_every_block=False)
108118

109119
assert_equal(self.get_bip9_status(bipName)['status'], 'started')
120+
tmpl = self.nodes[0].getblocktemplate({})
121+
assert(bipName not in tmpl['rules'])
122+
assert_equal(tmpl['vbavailable'][bipName], bitno)
123+
assert_equal(tmpl['vbrequired'], 0)
124+
assert(tmpl['version'] & activated_version)
110125

111126
# Test 3
112127
# 108 out of 144 signal bit 1 to achieve LOCKED_IN
@@ -118,13 +133,17 @@ def test_BIP(self, bipName, activated_version, invalidate, invalidatePostSignatu
118133
yield TestInstance(test_blocks, sync_every_block=False)
119134

120135
assert_equal(self.get_bip9_status(bipName)['status'], 'locked_in')
136+
tmpl = self.nodes[0].getblocktemplate({})
137+
assert(bipName not in tmpl['rules'])
121138

122139
# Test 4
123140
# 143 more version 536870913 blocks (waiting period-1)
124141
test_blocks = self.generate_blocks(143, 4)
125142
yield TestInstance(test_blocks, sync_every_block=False)
126143

127144
assert_equal(self.get_bip9_status(bipName)['status'], 'locked_in')
145+
tmpl = self.nodes[0].getblocktemplate({})
146+
assert(bipName not in tmpl['rules'])
128147

129148
# Test 5
130149
# Check that the new rule is enforced
@@ -148,6 +167,11 @@ def test_BIP(self, bipName, activated_version, invalidate, invalidatePostSignatu
148167
yield TestInstance([[block, True]])
149168

150169
assert_equal(self.get_bip9_status(bipName)['status'], 'active')
170+
tmpl = self.nodes[0].getblocktemplate({})
171+
assert(bipName in tmpl['rules'])
172+
assert(bipName not in tmpl['vbavailable'])
173+
assert_equal(tmpl['vbrequired'], 0)
174+
assert(not (tmpl['version'] & (1 << bitno)))
151175

152176
# Test 6
153177
# Check that the new sequence lock rules are enforced
@@ -183,9 +207,9 @@ def test_BIP(self, bipName, activated_version, invalidate, invalidatePostSignatu
183207

184208
def get_tests(self):
185209
for test in itertools.chain(
186-
self.test_BIP('csv', 536870913, self.sequence_lock_invalidate, self.donothing),
187-
self.test_BIP('csv', 536870913, self.mtp_invalidate, self.donothing),
188-
self.test_BIP('csv', 536870913, self.donothing, self.csv_invalidate)
210+
self.test_BIP('csv', 0x20000001, self.sequence_lock_invalidate, self.donothing, 0),
211+
self.test_BIP('csv', 0x20000001, self.mtp_invalidate, self.donothing, 0),
212+
self.test_BIP('csv', 0x20000001, self.donothing, self.csv_invalidate, 0)
189213
):
190214
yield test
191215

src/consensus/params.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ enum DeploymentPos
1616
{
1717
DEPLOYMENT_TESTDUMMY,
1818
DEPLOYMENT_CSV, // Deployment of BIP68, BIP112, and BIP113.
19+
// NOTE: Also add new deployments to VersionBitsDeploymentInfo in versionbits.cpp
1920
MAX_VERSION_BITS_DEPLOYMENTS
2021
};
2122

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2181,7 +2181,7 @@ void PartitionCheck(bool (*initialDownloadCheck)(), CCriticalSection& cs, const
21812181
}
21822182

21832183
// Protected by cs_main
2184-
static VersionBitsCache versionbitscache;
2184+
VersionBitsCache versionbitscache;
21852185

21862186
int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params)
21872187
{

src/main.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,8 @@ extern CBlockTreeDB *pblocktree;
482482
*/
483483
int GetSpendHeight(const CCoinsViewCache& inputs);
484484

485+
extern VersionBitsCache versionbitscache;
486+
485487
/**
486488
* Determine what nVersion a new block should use.
487489
*/

src/rpc/mining.cpp

Lines changed: 96 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "chain.h"
99
#include "chainparams.h"
1010
#include "consensus/consensus.h"
11+
#include "consensus/params.h"
1112
#include "consensus/validation.h"
1213
#include "core_io.h"
1314
#include "init.h"
@@ -303,14 +304,25 @@ static UniValue BIP22ValidationResult(const CValidationState& state)
303304
return "valid?";
304305
}
305306

307+
std::string gbt_vb_name(const Consensus::DeploymentPos pos) {
308+
const struct BIP9DeploymentInfo& vbinfo = VersionBitsDeploymentInfo[pos];
309+
std::string s = vbinfo.name;
310+
if (!vbinfo.gbt_force) {
311+
s.insert(s.begin(), '!');
312+
}
313+
return s;
314+
}
315+
306316
UniValue getblocktemplate(const UniValue& params, bool fHelp)
307317
{
308318
if (fHelp || params.size() > 1)
309319
throw runtime_error(
310320
"getblocktemplate ( \"jsonrequestobject\" )\n"
311321
"\nIf the request parameters include a 'mode' key, that is used to explicitly select between the default 'template' request or a 'proposal'.\n"
312322
"It returns data needed to construct a block to work on.\n"
313-
"See https://en.bitcoin.it/wiki/BIP_0022 for full specification.\n"
323+
"For full specification, see BIPs 22 and 9:\n"
324+
" https://github.com/bitcoin/bips/blob/master/bip-0022.mediawiki\n"
325+
" https://github.com/bitcoin/bips/blob/master/bip-0009.mediawiki#getblocktemplate_changes\n"
314326

315327
"\nArguments:\n"
316328
"1. \"jsonrequestobject\" (string, optional) A json object in the following spec\n"
@@ -326,6 +338,12 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
326338
"\nResult:\n"
327339
"{\n"
328340
" \"version\" : n, (numeric) The block version\n"
341+
" \"rules\" : [ \"rulename\", ... ], (array of strings) specific block rules that are to be enforced\n"
342+
" \"vbavailable\" : { (json object) set of pending, supported versionbit (BIP 9) softfork deployments\n"
343+
" \"rulename\" : bitnumber (numeric) identifies the bit number as indicating acceptance and readiness for the named softfork rule\n"
344+
" ,...\n"
345+
" },\n"
346+
" \"vbrequired\" : n, (numeric) bit mask of versionbits the server requires set in submissions\n"
329347
" \"previousblockhash\" : \"xxxx\", (string) The hash of current highest block\n"
330348
" \"transactions\" : [ (array) contents of non-coinbase transactions that should be included in the next block\n"
331349
" {\n"
@@ -369,6 +387,8 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
369387

370388
std::string strMode = "template";
371389
UniValue lpval = NullUniValue;
390+
std::set<std::string> setClientRules;
391+
int64_t nMaxVersionPreVB = -1;
372392
if (params.size() > 0)
373393
{
374394
const UniValue& oparam = params[0].get_obj();
@@ -412,6 +432,20 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
412432
TestBlockValidity(state, Params(), block, pindexPrev, false, true);
413433
return BIP22ValidationResult(state);
414434
}
435+
436+
const UniValue& aClientRules = find_value(oparam, "rules");
437+
if (aClientRules.isArray()) {
438+
for (unsigned int i = 0; i < aClientRules.size(); ++i) {
439+
const UniValue& v = aClientRules[i];
440+
setClientRules.insert(v.get_str());
441+
}
442+
} else {
443+
// NOTE: It is important that this NOT be read if versionbits is supported
444+
const UniValue& uvMaxVersion = find_value(oparam, "maxversion");
445+
if (uvMaxVersion.isNum()) {
446+
nMaxVersionPreVB = uvMaxVersion.get_int64();
447+
}
448+
}
415449
}
416450

417451
if (strMode != "template")
@@ -501,9 +535,10 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
501535
pindexPrev = pindexPrevNew;
502536
}
503537
CBlock* pblock = &pblocktemplate->block; // pointer for convenience
538+
const Consensus::Params& consensusParams = Params().GetConsensus();
504539

505540
// Update nTime
506-
UpdateTime(pblock, Params().GetConsensus(), pindexPrev);
541+
UpdateTime(pblock, consensusParams, pindexPrev);
507542
pblock->nNonce = 0;
508543

509544
UniValue aCaps(UniValue::VARR); aCaps.push_back("proposal");
@@ -544,17 +579,69 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
544579

545580
arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits);
546581

547-
static UniValue aMutable(UniValue::VARR);
548-
if (aMutable.empty())
549-
{
550-
aMutable.push_back("time");
551-
aMutable.push_back("transactions");
552-
aMutable.push_back("prevblock");
553-
}
582+
UniValue aMutable(UniValue::VARR);
583+
aMutable.push_back("time");
584+
aMutable.push_back("transactions");
585+
aMutable.push_back("prevblock");
554586

555587
UniValue result(UniValue::VOBJ);
556588
result.push_back(Pair("capabilities", aCaps));
589+
590+
UniValue aRules(UniValue::VARR);
591+
UniValue vbavailable(UniValue::VOBJ);
592+
for (int i = 0; i < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++i) {
593+
Consensus::DeploymentPos pos = Consensus::DeploymentPos(i);
594+
ThresholdState state = VersionBitsState(pindexPrev, consensusParams, pos, versionbitscache);
595+
switch (state) {
596+
case THRESHOLD_DEFINED:
597+
case THRESHOLD_FAILED:
598+
// Not exposed to GBT at all
599+
break;
600+
case THRESHOLD_LOCKED_IN:
601+
// Ensure bit is set in block version
602+
pblock->nVersion |= VersionBitsMask(consensusParams, pos);
603+
// FALL THROUGH to get vbavailable set...
604+
case THRESHOLD_STARTED:
605+
{
606+
const struct BIP9DeploymentInfo& vbinfo = VersionBitsDeploymentInfo[pos];
607+
vbavailable.push_back(Pair(gbt_vb_name(pos), consensusParams.vDeployments[pos].bit));
608+
if (setClientRules.find(vbinfo.name) == setClientRules.end()) {
609+
if (!vbinfo.gbt_force) {
610+
// If the client doesn't support this, don't indicate it in the [default] version
611+
pblock->nVersion &= ~VersionBitsMask(consensusParams, pos);
612+
}
613+
}
614+
break;
615+
}
616+
case THRESHOLD_ACTIVE:
617+
{
618+
// Add to rules only
619+
const struct BIP9DeploymentInfo& vbinfo = VersionBitsDeploymentInfo[pos];
620+
aRules.push_back(gbt_vb_name(pos));
621+
if (setClientRules.find(vbinfo.name) == setClientRules.end()) {
622+
// Not supported by the client; make sure it's safe to proceed
623+
if (!vbinfo.gbt_force) {
624+
// If we do anything other than throw an exception here, be sure version/force isn't sent to old clients
625+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Support for '%s' rule requires explicit client support", vbinfo.name));
626+
}
627+
}
628+
break;
629+
}
630+
}
631+
}
557632
result.push_back(Pair("version", pblock->nVersion));
633+
result.push_back(Pair("rules", aRules));
634+
result.push_back(Pair("vbavailable", vbavailable));
635+
result.push_back(Pair("vbrequired", int(0)));
636+
637+
if (nMaxVersionPreVB >= 2) {
638+
// If VB is supported by the client, nMaxVersionPreVB is -1, so we won't get here
639+
// Because BIP 34 changed how the generation transaction is serialised, we can only use version/force back to v2 blocks
640+
// This is safe to do [otherwise-]unconditionally only because we are throwing an exception above if a non-force deployment gets activated
641+
// Note that this can probably also be removed entirely after the first BIP9 non-force deployment (ie, probably segwit) gets activated
642+
aMutable.push_back("version/force");
643+
}
644+
558645
result.push_back(Pair("previousblockhash", pblock->hashPrevBlock.GetHex()));
559646
result.push_back(Pair("transactions", transactions));
560647
result.push_back(Pair("coinbaseaux", aux));

src/versionbits.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44

55
#include "versionbits.h"
66

7+
#include "consensus/params.h"
8+
9+
const struct BIP9DeploymentInfo VersionBitsDeploymentInfo[Consensus::MAX_VERSION_BITS_DEPLOYMENTS] = {
10+
{
11+
/*.name =*/ "testdummy",
12+
/*.gbt_force =*/ true,
13+
},
14+
{
15+
/*.name =*/ "csv",
16+
/*.gbt_force =*/ true,
17+
}
18+
};
19+
720
ThresholdState AbstractThresholdConditionChecker::GetStateFor(const CBlockIndex* pindexPrev, const Consensus::Params& params, ThresholdConditionCache& cache) const
821
{
922
int nPeriod = Period(params);

src/versionbits.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ enum ThresholdState {
3030
// will either be NULL or a block with (height + 1) % Period() == 0.
3131
typedef std::map<const CBlockIndex*, ThresholdState> ThresholdConditionCache;
3232

33+
struct BIP9DeploymentInfo {
34+
/** Deployment name */
35+
const char *name;
36+
/** Whether GBT clients can safely ignore this rule in simplified usage */
37+
bool gbt_force;
38+
};
39+
40+
extern const struct BIP9DeploymentInfo VersionBitsDeploymentInfo[];
41+
3342
/**
3443
* Abstract class that implements BIP9-style threshold logic, and caches results.
3544
*/

0 commit comments

Comments
 (0)