Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc: Implement inspectaccrualsnapshot and parseaccrualsnapshotfile #1744

Merged
merged 6 commits into from
Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/neuralnet/accrual/snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,23 @@ fs::path SnapshotPath(const uint64_t height)
class AccrualSnapshot
{
public:
using AccrualMap = std::unordered_map<Cpid, int64_t>;

//!
//! \brief Version number of the current format for a serialized snapshot.
//!
static constexpr uint32_t CURRENT_VERSION = 1;

uint32_t m_version; //!< Version of the serialized snapshot format.
uint64_t m_height; //!< Block height of the snapshot.

//!
//! \brief Maps CPIDs to rewards accrued at the time of the snapshot.
//!
//! Accrual values stored in units of of 1/100000000 GRC.
//!
AccrualMap m_records;

//!
//! \brief Initialize an empty accrual snapshot.
//!
Expand Down Expand Up @@ -422,17 +434,6 @@ class AccrualSnapshot

return iter->second;
}

private:
uint32_t m_version; //!< Version of the serialized snapshot format.
uint64_t m_height; //!< Block height of the snapshot.

//!
//! \brief Maps CPIDs to rewards accrued at the time of the snapshot.
//!
//! Accrual values stored in units of of 1/100000000 GRC.
//!
std::unordered_map<Cpid, int64_t> m_records;
}; // AccrualSnapshot

constexpr uint32_t AccrualSnapshot::CURRENT_VERSION; // for clang
Expand Down
1 change: 1 addition & 0 deletions src/rpcblockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,7 @@ UniValue beaconstatus(const UniValue& params, bool fHelp)
return res;
}


UniValue explainmagnitude(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() > 1)
Expand Down
1 change: 1 addition & 0 deletions src/rpcclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "getblockstats" , 0 },
{ "getblockstats" , 1 },
{ "getblockstats" , 2 },
{ "inspectaccrualsnapshot" , 0 },
{ "listmanifests" , 0 },
{ "sendalert" , 2 },
{ "sendalert" , 3 },
Expand Down
76 changes: 76 additions & 0 deletions src/rpcmining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "init.h"
#include "main.h"
#include "miner.h"
#include "neuralnet/accrual/snapshot.h"
#include "neuralnet/quorum.h"
#include "neuralnet/researcher.h"
#include "neuralnet/superblock.h"
Expand Down Expand Up @@ -466,3 +467,78 @@ UniValue comparesnapshotaccrual(const UniValue& params, bool fHelp)

return result;
}

UniValue inspectaccrualsnapshot(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() > 1)
throw runtime_error(
"inspectaccrualsnapshot <height>\n"
"\n"
"<height> --> block height (and file name) of the snapshot"
"\n"
"Display the contents of an accrual snapshot from accrual repository on disk.\n");


const fs::path snapshot_path = SnapshotPath(params[0].get_int());
const AccrualSnapshot snapshot = AccrualSnapshotReader(snapshot_path).Read();

UniValue result(UniValue::VOBJ);

result.pushKV("version", (uint64_t)snapshot.m_version);
result.pushKV("height", snapshot.m_height);

const AccrualSnapshot::AccrualMap& records = snapshot.m_records;
const std::map<Cpid, int64_t> sorted_records(records.begin(), records.end());

UniValue records_out(UniValue::VOBJ);

for (const auto& record_pair : sorted_records) {
const Cpid& cpid = record_pair.first;
const int64_t accrual = record_pair.second;

records_out.pushKV(cpid.ToString(), ValueFromAmount(accrual));
}

result.pushKV("records", records_out);

return result;
}

UniValue parseaccrualsnapshotfile(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 1)
throw runtime_error(
"parseaccrualsnapshot <filespec>\n"
"\n"
"<filespec> -> String - path to file."
"\n"
"Parses accrual snapshot from a valid snapshot file.\n");

UniValue res(UniValue::VOBJ);

boost::filesystem::path snapshot_path = params[0].get_str();

if (!boost::filesystem::is_regular_file(snapshot_path))
{
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid snapshot file specified.");
}

const AccrualSnapshot snapshot = AccrualSnapshotReader(snapshot_path).Read();
const AccrualSnapshot::AccrualMap& records = snapshot.m_records;
const std::map<Cpid, int64_t> sorted_records(records.begin(), records.end());

UniValue accruals(UniValue::VOBJ);

for (const auto& iter : sorted_records)
{
UniValue entry(UniValue::VOBJ);

accruals.pushKV(iter.first.ToString(), ValueFromAmount(iter.second));
}

res.pushKV("version", (uint64_t) snapshot.m_version);
res.pushKV("height", snapshot.m_height);
res.pushKV("records", accruals);

return res;
}
2 changes: 2 additions & 0 deletions src/rpcserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,12 @@ static const CRPCCommand vRPCCommands[] =
{ "getlistof", &getlistof, cat_developer },
{ "getrecentblocks", &rpc_getrecentblocks, cat_developer },
{ "getsupervotes", &rpc_getsupervotes, cat_developer },
{ "inspectaccrualsnapshot", &inspectaccrualsnapshot, cat_developer },
{ "listdata", &listdata, cat_developer },
{ "listprojects", &listprojects, cat_developer },
{ "logging", &logging, cat_developer },
{ "network", &network, cat_developer },
{ "parseaccrualsnapshotfile",&parseaccrualsnapshotfile,cat_developer },
{ "parselegacysb", &parselegacysb, cat_developer },
{ "projects", &projects, cat_developer },
{ "readconfig", &readconfig, cat_developer },
Expand Down
2 changes: 2 additions & 0 deletions src/rpcserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,12 @@ extern UniValue debug10(const UniValue& params, bool fHelp);
extern UniValue debug2(const UniValue& params, bool fHelp);
extern UniValue rpc_getblockstats(const UniValue& params, bool fHelp);
extern UniValue getlistof(const UniValue& params, bool fHelp);
extern UniValue inspectaccrualsnapshot(const UniValue& params, bool fHelp);
extern UniValue listdata(const UniValue& params, bool fHelp);
extern UniValue listprojects(const UniValue& params, bool fHelp);
extern UniValue logging(const UniValue& params, bool fHelp);
extern UniValue network(const UniValue& params, bool fHelp);
extern UniValue parseaccrualsnapshotfile(const UniValue& params, bool fHelp);
extern UniValue parselegacysb(const UniValue& params, bool fHelp);
extern UniValue projects(const UniValue& params, bool fHelp);
extern UniValue readconfig(const UniValue& params, bool fHelp);
Expand Down