Skip to content

Commit 32daff9

Browse files
committed
[mining] Several segwit2x seasoning differential related fixes.
Fixes #28
1 parent d2431c0 commit 32daff9

File tree

5 files changed

+32
-11
lines changed

5 files changed

+32
-11
lines changed

qa/rpc-tests/segwit.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def run_test(self):
135135
assert(tmpl['sigoplimit'] == 20000)
136136
assert(tmpl['transactions'][0]['hash'] == txid)
137137
assert(tmpl['transactions'][0]['sigops'] == 2)
138-
tmpl = self.nodes[0].getblocktemplate({'rules':['segwit','segwit2x']})
138+
tmpl = self.nodes[0].getblocktemplate({'rules':['segwit']})
139139
assert(tmpl['sizelimit'] == 1000000)
140140
assert('weightlimit' not in tmpl)
141141
assert(tmpl['sigoplimit'] == 20000)
@@ -244,10 +244,21 @@ def run_test(self):
244244

245245
print("Verify sigops are counted in GBT with BIP141 rules after the fork")
246246
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
247+
tmpl = self.nodes[0].getblocktemplate({'rules':['segwit']})
248+
assert(tmpl['sizelimit'] >= 3999577) # actual maximum size is lower due to minimum mandatory non-witness data
249+
assert(tmpl['weightlimit'] == 4000000)
250+
assert(tmpl['sigoplimit'] == 80000)
251+
assert(tmpl['transactions'][0]['txid'] == txid)
252+
assert(tmpl['transactions'][0]['sigops'] == 8)
253+
254+
self.nodes[0].generate(1) # Mine a block to clear the gbt cache
255+
256+
print("Verify sigops are counted in GBT with BIP141 rules after the 2x fork")
257+
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
247258
tmpl = self.nodes[0].getblocktemplate({'rules':['segwit','segwit2x']})
248259
assert(tmpl['sizelimit'] >= 3999577) # actual maximum size is lower due to minimum mandatory non-witness data
249-
assert(tmpl['weightlimit'] == 4000000) # limit at first step
250-
assert(tmpl['sigoplimit'] == 160000) # limit at first step
260+
assert(tmpl['weightlimit'] == (4000000*2))
261+
assert(tmpl['sigoplimit'] == (80000*2))
251262
assert(tmpl['transactions'][0]['txid'] == txid)
252263
assert(tmpl['transactions'][0]['sigops'] == 8)
253264

src/consensus/consensus.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ inline unsigned int MaxBlockWeight()
6464
return MaxBlockWeight(true);
6565
}
6666

67+
inline unsigned int MaxBlockSerSize(bool fSegwitSeasoned)
68+
{
69+
return (MaxBlockBaseSize(fSegwitSeasoned) * 4);
70+
}
71+
6772
/** The maximum allowed number of transactions per block */
6873
static const unsigned int MAX_BLOCK_VTX_SIZE = 1000000;
6974

src/miner.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ BlockAssembler::BlockAssembler(const CChainParams& _chainparams)
8484
bool fWeightSet = false;
8585
if (IsArgSet("-blockmaxweight")) {
8686
nBlockMaxWeight = GetArg("-blockmaxweight", DEFAULT_BLOCK_MAX_WEIGHT);
87-
nBlockMaxSize = MAX_BLOCK_SERIALIZED_SIZE;
87+
nBlockMaxSize = MaxBlockSerSize(fWitnessSeasoned);
8888
fWeightSet = true;
8989
}
9090
if (IsArgSet("-blockmaxsize")) {
@@ -102,11 +102,11 @@ BlockAssembler::BlockAssembler(const CChainParams& _chainparams)
102102
}
103103

104104
// Limit weight to between 4K and MAX_BLOCK_WEIGHT-4K for sanity:
105-
nBlockMaxWeight = std::max((unsigned int)4000, std::min((unsigned int)(MaxBlockWeight(false)-4000), nBlockMaxWeight));
105+
nBlockMaxWeight = std::max((unsigned int)4000, std::min((unsigned int)(MaxBlockWeight(fWitnessSeasoned)-4000), nBlockMaxWeight));
106106
// Limit size to between 1K and MAX_BLOCK_SERIALIZED_SIZE-1K for sanity:
107-
nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MAX_BLOCK_SERIALIZED_SIZE-1000), nBlockMaxSize));
107+
nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MaxBlockSerSize(fWitnessSeasoned)-1000), nBlockMaxSize));
108108
// Whether we need to account for byte usage (in addition to weight usage)
109-
fNeedSizeAccounting = (nBlockMaxSize < MAX_BLOCK_SERIALIZED_SIZE-1000);
109+
fNeedSizeAccounting = (nBlockMaxSize < MaxBlockSerSize(fWitnessSeasoned)-1000);
110110
}
111111

112112
void BlockAssembler::resetBlock()
@@ -118,6 +118,7 @@ void BlockAssembler::resetBlock()
118118
nBlockWeight = 4000;
119119
nBlockSigOpsCost = 400;
120120
fIncludeWitness = false;
121+
fWitnessSeasoned = false;
121122

122123
// These counters do not include coinbase tx
123124
nBlockTx = 0;
@@ -168,6 +169,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
168169
// TODO: replace this with a call to main to assess validity of a mempool
169170
// transaction (which in most cases can be a no-op).
170171
fIncludeWitness = IsWitnessEnabled(pindexPrev, chainparams.GetConsensus()) && fMineWitnessTx;
172+
fWitnessSeasoned = IsWitnessSeasoned(pindexPrev, chainparams.GetConsensus());
171173

172174
addPriorityTxs();
173175
int nPackagesSelected = 0;
@@ -242,7 +244,7 @@ bool BlockAssembler::TestPackage(uint64_t packageSize, int64_t packageSigOpsCost
242244
// TODO: switch to weight-based accounting for packages instead of vsize-based accounting.
243245
if (nBlockWeight + WITNESS_SCALE_FACTOR * packageSize >= nBlockMaxWeight)
244246
return false;
245-
if (nBlockSigOpsCost + packageSigOpsCost >= (uint64_t)MaxBlockSigOpsCost(fIncludeWitness)) // note - excludes bip102 buffer
247+
if (nBlockSigOpsCost + packageSigOpsCost >= (uint64_t)MaxBlockSigOpsCost(fWitnessSeasoned))
246248
return false;
247249
return true;
248250
}
@@ -302,7 +304,7 @@ bool BlockAssembler::TestForBlock(CTxMemPool::txiter iter)
302304
}
303305
}
304306

305-
uint64_t sigOpMax = MaxBlockSigOpsCost(fIncludeWitness); // excludes bip102 buffer
307+
uint64_t sigOpMax = MaxBlockSigOpsCost(fWitnessSeasoned);
306308
if (nBlockSigOpsCost + iter->GetSigOpCost() >= sigOpMax) {
307309
// If the block has room for no more sig ops then
308310
// flag that the block is finished

src/miner.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class BlockAssembler
141141

142142
// Configuration parameters for the block size
143143
bool fIncludeWitness;
144+
bool fWitnessSeasoned;
144145
unsigned int nBlockMaxWeight, nBlockMaxSize;
145146
bool fNeedSizeAccounting;
146147
CFeeRate blockMinFeeRate;

src/rpc/mining.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,10 +520,12 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
520520
}
521521

522522
const struct BIP9DeploymentInfo& segwit_info = VersionBitsDeploymentInfo[Consensus::DEPLOYMENT_SEGWIT];
523+
const struct BIP9DeploymentInfo& segwit2x_info = VersionBitsDeploymentInfo[Consensus::DEPLOYMENT_SEGWIT2X];
523524
// If the caller is indicating segwit support, then allow CreateNewBlock()
524525
// to select witness transactions, after segwit activates (otherwise
525526
// don't).
526527
bool fSupportsSegwit = setClientRules.find(segwit_info.name) != setClientRules.end();
528+
bool fSupportsSegwit2x = setClientRules.find(segwit2x_info.name) != setClientRules.end();
527529

528530
// Update block
529531
static CBlockIndex* pindexPrev;
@@ -681,7 +683,7 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
681683
result.push_back(Pair("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1));
682684
result.push_back(Pair("mutable", aMutable));
683685
result.push_back(Pair("noncerange", "00000000ffffffff"));
684-
int64_t nSigOpLimit = MaxBlockSigOpsCost(fPreSegWit?false:true); // excl bip102 buffer
686+
int64_t nSigOpLimit = MaxBlockSigOpsCost(fSupportsSegwit2x); // excl bip102 buffer
685687
if (fPreSegWit) {
686688
assert(nSigOpLimit % WITNESS_SCALE_FACTOR == 0);
687689
nSigOpLimit /= WITNESS_SCALE_FACTOR;
@@ -691,7 +693,7 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
691693
result.push_back(Pair("sizelimit", (int64_t)MAX_LEGACY_BLOCK_SIZE));
692694
} else {
693695
result.push_back(Pair("sizelimit", (int64_t)MAX_BLOCK_SERIALIZED_SIZE));
694-
result.push_back(Pair("weightlimit", (int64_t)MaxBlockWeight(false)));
696+
result.push_back(Pair("weightlimit", (int64_t)MaxBlockWeight(fSupportsSegwit2x)));
695697
}
696698
result.push_back(Pair("curtime", pblock->GetBlockTime()));
697699
result.push_back(Pair("bits", strprintf("%08x", pblock->nBits)));

0 commit comments

Comments
 (0)