Skip to content

Commit

Permalink
Merge pull request dashpay#23 from PACGlobalOfficial/chainstats-rpc-c…
Browse files Browse the repository at this point in the history
…alls

Add some misc rpc calls from bitcoin upstream.
  • Loading branch information
barrystyle authored Oct 15, 2019
2 parents 33cef2a + 8a2b77b commit 885d511
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,85 @@ UniValue reconsiderblock(const JSONRPCRequest& request)
return NullUniValue;
}

UniValue getchaintxstats(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() > 2)
throw std::runtime_error(
"getchaintxstats ( nblocks blockhash )\n"
"\nCompute statistics about the total number and rate of transactions in the chain.\n"
"\nArguments:\n"
"1. nblocks (numeric, optional) Size of the window in number of blocks (default: one month).\n"
"2. \"blockhash\" (string, optional) The hash of the block that ends the window.\n"
"\nResult:\n"
"{\n"
" \"time\": xxxxx, (numeric) The timestamp for the final block in the window in UNIX format.\n"
" \"txcount\": xxxxx, (numeric) The total number of transactions in the chain up to that point.\n"
" \"window_block_count\": xxxxx, (numeric) Size of the window in number of blocks.\n"
" \"window_tx_count\": xxxxx, (numeric) The number of transactions in the window. Only returned if \"window_block_count\" is > 0.\n"
" \"window_interval\": xxxxx, (numeric) The elapsed time in the window in seconds. Only returned if \"window_block_count\" is > 0.\n"
" \"txrate\": x.xx, (numeric) The average rate of transactions per second in the window. Only returned if \"window_interval\" is > 0.\n"
"}\n"
"\nExamples:\n"
+ HelpExampleCli("getchaintxstats", "")
+ HelpExampleRpc("getchaintxstats", "2016")
);

const CBlockIndex* pindex;
int blockcount = 30 * 24 * 60 * 60 / Params().GetConsensus().nPosTargetSpacing; // By default: 1 month

bool havehash = !request.params[1].isNull();
uint256 hash;
if (havehash) {
hash = uint256S(request.params[1].get_str());
}

{
LOCK(cs_main);
if (havehash) {
auto it = mapBlockIndex.find(hash);
if (it == mapBlockIndex.end()) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
}
pindex = it->second;
if (!chainActive.Contains(pindex)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Block is not in main chain");
}
} else {
pindex = chainActive.Tip();
}
}

assert(pindex != nullptr);

if (request.params[0].isNull()) {
blockcount = std::max(0, std::min(blockcount, pindex->nHeight - 1));
} else {
blockcount = request.params[0].get_int();

if (blockcount < 0 || (blockcount > 0 && blockcount >= pindex->nHeight)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid block count: should be between 0 and the block's height - 1");
}
}

const CBlockIndex* pindexPast = pindex->GetAncestor(pindex->nHeight - blockcount);
int nTimeDiff = pindex->GetMedianTimePast() - pindexPast->GetMedianTimePast();
int nTxDiff = pindex->nChainTx - pindexPast->nChainTx;

UniValue ret(UniValue::VOBJ);
ret.push_back(Pair("time", (int64_t)pindex->nTime));
ret.push_back(Pair("txcount", (int64_t)pindex->nChainTx));
ret.push_back(Pair("window_block_count", blockcount));
if (blockcount > 0) {
ret.push_back(Pair("window_tx_count", nTxDiff));
ret.push_back(Pair("window_interval", nTimeDiff));
if (nTimeDiff > 0) {
ret.push_back(Pair("txrate", ((double)nTxDiff) / nTimeDiff));
}
}

return ret;
}

UniValue getspecialtxes(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() < 1 || request.params.size() > 5)
Expand Down Expand Up @@ -1756,6 +1835,7 @@ static const CRPCCommand commands[] =
{ // category name actor (function) okSafe argNames
// --------------------- ------------------------ ----------------------- ------ ----------
{ "blockchain", "getblockchaininfo", &getblockchaininfo, true, {} },
{ "blockchain", "getchaintxstats", &getchaintxstats, true, {"nblocks", "blockhash"} },
{ "blockchain", "getbestblockhash", &getbestblockhash, true, {} },
{ "blockchain", "getblockcount", &getblockcount, true, {} },
{ "blockchain", "getblock", &getblock, true, {"blockhash","verbosity|verbose"} },
Expand Down
17 changes: 17 additions & 0 deletions src/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,22 @@ UniValue stop(const JSONRPCRequest& jsonRequest)
return "PACGlobal Core server stopping";
}

UniValue uptime(const JSONRPCRequest& jsonRequest)
{
if (jsonRequest.fHelp || jsonRequest.params.size() > 1)
throw std::runtime_error(
"uptime\n"
"\nReturns the total uptime of the server.\n"
"\nResult:\n"
"ttt (numeric) The number of seconds that the server has been running\n"
"\nExamples:\n"
+ HelpExampleCli("uptime", "")
+ HelpExampleRpc("uptime", "")
);

return GetTime() - GetStartupTime();
}

/**
* Call Table
*/
Expand All @@ -325,6 +341,7 @@ static const CRPCCommand vRPCCommands[] =
/* Overall control/query calls */
{ "control", "help", &help, true, {"command"} },
{ "control", "stop", &stop, true, {} },
{ "control", "uptime", &uptime, true, {} },
};

CRPCTable::CRPCTable()
Expand Down
8 changes: 8 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
#include <openssl/rand.h>
#include <openssl/conf.h>

// Application startup time (used for uptime calculation)
const int64_t nStartupTime = GetTime();

// Work around clang compilation problem in Boost 1.46:
// /usr/include/boost/program_options/detail/config_file.hpp:163:17: error: call to function 'to_internal' that is neither visible in the template definition nor found by argument-dependent lookup
// See also: http://stackoverflow.com/questions/10020179/compilation-fail-in-boost-librairies-program-options
Expand Down Expand Up @@ -1068,3 +1071,8 @@ std::string SafeIntVersionToString(uint32_t nVersion)
}
}

// Obtain the application startup time (used for uptime calculation)
int64_t GetStartupTime()
{
return nStartupTime;
}
3 changes: 3 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ extern bool fMasternodeMode;
extern bool fLiteMode;
extern int nWalletBackups;

// Application startup time (used for uptime calculation)
int64_t GetStartupTime();

static const bool DEFAULT_LOGTIMEMICROS = false;
static const bool DEFAULT_LOGIPS = false;
static const bool DEFAULT_LOGTIMESTAMPS = true;
Expand Down

0 comments on commit 885d511

Please sign in to comment.