Skip to content

Commit

Permalink
RPC/Mining: Abstract TemplateToJSON out of getblocktemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-jr committed Jan 31, 2025
1 parent 53f05f4 commit 1ea4569
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,8 @@ static std::string gbt_vb_name(const Consensus::DeploymentPos pos) {
return s;
}

static UniValue TemplateToJSON(const Consensus::Params&, const ChainstateManager&, const CBlockTemplate*, const CBlockIndex*, const std::set<std::string>& setClientRules, unsigned int nTransactionsUpdatedLast);

static RPCHelpMan getblocktemplate()
{
return RPCHelpMan{"getblocktemplate",
Expand Down Expand Up @@ -830,6 +832,14 @@ static RPCHelpMan getblocktemplate()
UpdateTime(pblock, consensusParams, pindexPrev);
pblock->nNonce = 0;

return TemplateToJSON(consensusParams, chainman, &*pblocktemplate, pindexPrev, setClientRules, nTransactionsUpdatedLast);
},
};
}

static UniValue TemplateToJSON(const Consensus::Params& consensusParams, const ChainstateManager& chainman, const CBlockTemplate* const pblocktemplate, const CBlockIndex* const pindexPrev, const std::set<std::string>& setClientRules, const unsigned int nTransactionsUpdatedLast) {
const CBlock* const pblock = &pblocktemplate->block;

// NOTE: If at some point we support pre-segwit miners post-segwit-activation, this needs to take segwit support into consideration
const bool fPreSegWit = !DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_SEGWIT);

Expand Down Expand Up @@ -894,6 +904,7 @@ static RPCHelpMan getblocktemplate()
aRules.push_back("!signet");
}

auto block_version = pblock->nVersion;
UniValue vbavailable(UniValue::VOBJ);
for (int j = 0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
Consensus::DeploymentPos pos = Consensus::DeploymentPos(j);
Expand All @@ -905,7 +916,7 @@ static RPCHelpMan getblocktemplate()
break;
case ThresholdState::LOCKED_IN:
// Ensure bit is set in block version
pblock->nVersion |= chainman.m_versionbitscache.Mask(consensusParams, pos);
block_version |= chainman.m_versionbitscache.Mask(consensusParams, pos);
[[fallthrough]];
case ThresholdState::STARTED:
{
Expand All @@ -914,7 +925,7 @@ static RPCHelpMan getblocktemplate()
if (setClientRules.find(vbinfo.name) == setClientRules.end()) {
if (!vbinfo.gbt_force) {
// If the client doesn't support this, don't indicate it in the [default] version
pblock->nVersion &= ~chainman.m_versionbitscache.Mask(consensusParams, pos);
block_version &= ~chainman.m_versionbitscache.Mask(consensusParams, pos);
}
}
break;
Expand All @@ -934,7 +945,7 @@ static RPCHelpMan getblocktemplate()
}
}
}
result.pushKV("version", pblock->nVersion);
result.pushKV("version", block_version);
result.pushKV("rules", std::move(aRules));
result.pushKV("vbavailable", std::move(vbavailable));
result.pushKV("vbrequired", int(0));
Expand All @@ -943,7 +954,7 @@ static RPCHelpMan getblocktemplate()
result.pushKV("transactions", std::move(transactions));
result.pushKV("coinbaseaux", std::move(aux));
result.pushKV("coinbasevalue", (int64_t)pblock->vtx[0]->vout[0].nValue);
result.pushKV("longpollid", tip.GetHex() + ToString(nTransactionsUpdatedLast));
result.pushKV("longpollid", pindexPrev->GetBlockHash().GetHex() + ToString(nTransactionsUpdatedLast));
result.pushKV("target", hashTarget.GetHex());
result.pushKV("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1);
result.pushKV("mutable", std::move(aMutable));
Expand Down Expand Up @@ -974,8 +985,6 @@ static RPCHelpMan getblocktemplate()
}

return result;
},
};
}

class submitblock_StateCatcher final : public CValidationInterface
Expand Down

0 comments on commit 1ea4569

Please sign in to comment.