From 296236412f311de7e889a4c37c3ee545b358dd12 Mon Sep 17 00:00:00 2001 From: Pedro Pinheiro Date: Mon, 22 Feb 2016 14:15:19 +0000 Subject: [PATCH] Add hard fork information to getblockchaininfo RPC endpoint --- src/main.h | 1 + src/rpcblockchain.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/main.h b/src/main.h index 93b412b08db..7ae8eec4df0 100644 --- a/src/main.h +++ b/src/main.h @@ -124,6 +124,7 @@ struct BlockHasher extern CScript COINBASE_FLAGS; extern CCriticalSection cs_main; extern CTxMemPool mempool; +extern boost::atomic sizeForkTime; typedef boost::unordered_map BlockMap; extern BlockMap mapBlockIndex; extern uint64_t nLastBlockTx; diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index f5b16bc7c49..1c6d25cdf28 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -16,6 +16,8 @@ #include "streams.h" #include "sync.h" #include "txmempool.h" +#include "txdb.h" +#include "timedata.h" #include "util.h" #include "utilstrencodings.h" @@ -604,6 +606,46 @@ static UniValue SoftForkDesc(const std::string &name, int version, CBlockIndex* return rv; } +static UniValue HardForkMajorityDesc(int minVersion, CBlockIndex* pindex, int nRequired, const Consensus::Params& consensusParams) +{ + uint32_t forkTime = sizeForkTime.load(); + bool isHardForkActive = forkTime < std::numeric_limits::max(); + int nFound = 0; + UniValue gracePeriodEnds; + UniValue triggeredAtBlock; + CBlockIndex* pstart = pindex; + if (isHardForkActive) { + // Always report blocks found as over the threshold once the fork is active + nFound = nRequired + 1; + gracePeriodEnds = static_cast(forkTime); + triggeredAtBlock = pblocktree->ForkBitActivated(FORK_BIT_2MB).GetHex(); + } else { + for (int i = 0; i < consensusParams.nMajorityWindow && pstart != NULL; i++) + { + if (pstart->nVersion >= minVersion) + ++nFound; + pstart = pstart->pprev; + } + } + + UniValue rv(UniValue::VOBJ); + rv.push_back(Pair("triggeredatblock", triggeredAtBlock)); + rv.push_back(Pair("earliestforktime", gracePeriodEnds)); + rv.push_back(Pair("found", nFound)); + rv.push_back(Pair("required", nRequired)); + rv.push_back(Pair("window", consensusParams.nMajorityWindow)); + return rv; +} + +static UniValue HardForkDesc(const std::string &name, int version, CBlockIndex* pindex, const Consensus::Params& consensusParams) +{ + UniValue rv(UniValue::VOBJ); + rv.push_back(Pair("id", name)); + rv.push_back(Pair("version", version)); + rv.push_back(Pair("status", HardForkMajorityDesc(version, pindex, consensusParams.nMajorityEnforceBlockUpgrade, consensusParams))); + return rv; +} + UniValue getblockchaininfo(const UniValue& params, bool fHelp) { if (fHelp || params.size() != 0) @@ -662,6 +704,10 @@ UniValue getblockchaininfo(const UniValue& params, bool fHelp) softforks.push_back(SoftForkDesc("bip65", 4, tip, consensusParams)); obj.push_back(Pair("softforks", softforks)); + UniValue hardforks(UniValue::VARR); + hardforks.push_back(HardForkDesc("bip109", 0x01000000, tip, consensusParams)); + obj.push_back(Pair("hardforks", hardforks)); + if (fPruneMode) { CBlockIndex *block = chainActive.Tip();