versionbits: Move getdeploymentinfo logic to versionbits

Rather than having the RPC code have knowledge about how BIP9 is
implemented, create a reporting function in the versionbits code, and
limit the RPC code to coverting the result of that into Univalue/JSON.
This commit is contained in:
Anthony Towns
2023-12-08 14:20:19 +10:00
parent 3bd32c2055
commit b1e967c3ec
3 changed files with 85 additions and 46 deletions

View File

@@ -9,6 +9,18 @@
using enum ThresholdState;
static std::string StateName(ThresholdState state)
{
switch (state) {
case DEFINED: return "defined";
case STARTED: return "started";
case LOCKED_IN: return "locked_in";
case ACTIVE: return "active";
case FAILED: return "failed";
}
return "invalid";
}
ThresholdState AbstractThresholdConditionChecker::GetStateFor(const CBlockIndex* pindexPrev, ThresholdConditionCache& cache) const
{
int nPeriod = Period();
@@ -205,6 +217,35 @@ public:
} // namespace
BIP9Info VersionBitsCache::Info(const CBlockIndex& block_index, const Consensus::Params& params, Consensus::DeploymentPos id)
{
BIP9Info result;
const auto current_state = State(block_index.pprev, params, id);
result.current_state = StateName(current_state);
result.since = StateSinceHeight(block_index.pprev, params, id);
const auto next_state = State(&block_index, params, id);
result.next_state = StateName(next_state);
const bool has_signal = (STARTED == current_state || LOCKED_IN == current_state);
if (has_signal) {
result.stats.emplace(Statistics(&block_index, params, id, &result.signalling_blocks));
if (LOCKED_IN == current_state) {
result.stats->threshold = 0;
result.stats->possible = false;
}
}
if (current_state == ACTIVE) {
result.active_since = result.since;
} else if (next_state == ACTIVE) {
result.active_since = block_index.nHeight + 1;
}
return result;
}
ThresholdState VersionBitsCache::State(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos)
{
LOCK(m_mutex);