aboutsummaryrefslogtreecommitdiff
path: root/src/versionbits.cpp
diff options
context:
space:
mode:
authorMatthew Zipkin <pinheadmz@gmail.com>2017-01-13 17:06:50 -0800
committerMatthew Zipkin <pinheadmz@gmail.com>2017-03-24 16:57:05 -0700
commit557c9a68fb72aeb535f2efe3cc82d3f5e66c6ad3 (patch)
treeb347a1975468bcae82ee6c1bb051f001973c6408 /src/versionbits.cpp
parenta0b1e57b20a17177ed5a9a54e4a8aab597a546b4 (diff)
downloadbitcoin-557c9a68fb72aeb535f2efe3cc82d3f5e66c6ad3.tar.xz
RPC: getblockchaininfo: BIP9 stats
add RPC tests for BIP9 counting stats
Diffstat (limited to 'src/versionbits.cpp')
-rw-r--r--src/versionbits.cpp36
1 files changed, 35 insertions, 1 deletions
diff --git a/src/versionbits.cpp b/src/versionbits.cpp
index 8a7cce7485..80786233f5 100644
--- a/src/versionbits.cpp
+++ b/src/versionbits.cpp
@@ -3,7 +3,6 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "versionbits.h"
-
#include "consensus/params.h"
const struct BIP9DeploymentInfo VersionBitsDeploymentInfo[Consensus::MAX_VERSION_BITS_DEPLOYMENTS] = {
@@ -105,6 +104,36 @@ ThresholdState AbstractThresholdConditionChecker::GetStateFor(const CBlockIndex*
return state;
}
+// return the numerical statistics of blocks signalling the specified BIP9 condition in this current period
+BIP9Stats AbstractThresholdConditionChecker::GetStateStatisticsFor(const CBlockIndex* pindex, const Consensus::Params& params) const
+{
+ BIP9Stats stats;
+
+ stats.period = Period(params);
+ stats.threshold = Threshold(params);
+
+ if (pindex == NULL)
+ return stats;
+
+ // Find beginning of period
+ const CBlockIndex* pindexEndOfPrevPeriod = pindex->GetAncestor(pindex->nHeight - ((pindex->nHeight + 1) % stats.period));
+ stats.elapsed = pindex->nHeight - pindexEndOfPrevPeriod->nHeight;
+
+ // Count from current block to beginning of period
+ int count = 0;
+ const CBlockIndex* currentIndex = pindex;
+ while (pindexEndOfPrevPeriod->nHeight != currentIndex->nHeight){
+ if (Condition(currentIndex, params))
+ count++;
+ currentIndex = currentIndex->pprev;
+ }
+
+ stats.count = count;
+ stats.possible = (stats.period - stats.threshold ) >= (stats.elapsed - count);
+
+ return stats;
+}
+
int AbstractThresholdConditionChecker::GetStateSinceHeightFor(const CBlockIndex* pindexPrev, const Consensus::Params& params, ThresholdConditionCache& cache) const
{
const ThresholdState initialState = GetStateFor(pindexPrev, params, cache);
@@ -167,6 +196,11 @@ ThresholdState VersionBitsState(const CBlockIndex* pindexPrev, const Consensus::
return VersionBitsConditionChecker(pos).GetStateFor(pindexPrev, params, cache.caches[pos]);
}
+BIP9Stats VersionBitsStatistics(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos)
+{
+ return VersionBitsConditionChecker(pos).GetStateStatisticsFor(pindexPrev, params);
+}
+
int VersionBitsStateSinceHeight(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos, VersionBitsCache& cache)
{
return VersionBitsConditionChecker(pos).GetStateSinceHeightFor(pindexPrev, params, cache.caches[pos]);