Skip to content

rpc: Add a call to dump contracts in binary form #2011

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

Merged
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
85 changes: 85 additions & 0 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,91 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool fP
return result;
}

UniValue dumpcontracts(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 2)
throw runtime_error(
"dumpcontracts <contract_type> <file>\n"
"\n"
"<contract_type> Contract type to gather data from."
"<file> Output file.\n"
"\n"
"Dump serialized contract data gathered from the chain to a specified file.\n");

GRC::Contract::Type contract_type = GRC::Contract::Type::Parse(params[0].get_str());
if (contract_type == GRC::ContractType::UNKNOWN)
throw runtime_error("Invalid contract type");
std::string path = params[1].get_str();
if (path.empty() || fs::exists(path))
throw runtime_error("Invalid path");

UniValue report(UniValue::VOBJ);
int height = 0;
int64_t now = GetAdjustedTime();

CBlock block;
CAutoFile file(fsbridge::fopen(path, "wb"), SER_DISK, PROTOCOL_VERSION);

std::vector<CTransaction> vtx;
std::vector<std::pair<uint160, int64_t>> beacon_activations;
std::vector<GRC::Cpid> active_beacons;
std::vector<CKeyID> pending_beacons;

file << now;

{
LOCK(cs_main);

CBlockIndex* pblockindex = pindexGenesisBlock;
while (pblockindex != nullptr) {
block.ReadFromDisk(pblockindex);
for (auto& tx : block.vtx) {
for (const auto& contract : tx.GetContracts()) {
if (contract.m_type == contract_type) {
vtx.push_back(tx);
break;
}
}
}

if (pblockindex->IsSuperblock() && contract_type == GRC::ContractType::BEACON) {
GRC::SuperblockPtr psuperblock = block.GetSuperblock();
for (const auto& key : psuperblock->m_verified_beacons.m_verified) {
beacon_activations.push_back(std::make_pair(key, psuperblock.m_timestamp));
}
}

height = pblockindex->nHeight;
pblockindex = pblockindex->pnext;
}

if (contract_type == GRC::ContractType::BEACON) {
for (const auto& x : GRC::GetBeaconRegistry().Beacons()) {
if (!x.second->Expired(now))
active_beacons.push_back(x.first);
}

for (const auto& x : GRC::GetBeaconRegistry().PendingBeacons()) {
if (!x.second.Expired(now))
pending_beacons.push_back(x.first);
}
}
}

file << vtx;
report.pushKV("height", height);
report.pushKV("tx_count", (uint64_t)vtx.size());
if (contract_type == GRC::ContractType::BEACON) {
file << beacon_activations;
report.pushKV("activation_count", (uint64_t)beacon_activations.size());
file << active_beacons;
report.pushKV("active_beacon_count", (uint64_t)active_beacons.size());
file << pending_beacons;
report.pushKV("pending_beacon_count", (uint64_t)pending_beacons.size());
}

return report;
}

UniValue showblock(const UniValue& params, bool fHelp)
{
Expand Down
1 change: 1 addition & 0 deletions src/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ static const CRPCCommand vRPCCommands[] =
{ "currentcontractaverage", &currentcontractaverage, cat_developer },
{ "debug", &debug, cat_developer },
{ "debug10", &debug10, cat_developer },
{ "dumpcontracts", &dumpcontracts, cat_developer },
{ "exportstats1", &rpc_exportstats, cat_developer },
{ "getblockstats", &rpc_getblockstats, cat_developer },
{ "getlistof", &getlistof, cat_developer },
Expand Down
1 change: 1 addition & 0 deletions src/rpc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ extern UniValue currentcontractaverage(const UniValue& params, bool fHelp);
extern UniValue debug(const UniValue& params, bool fHelp);
extern UniValue debug10(const UniValue& params, bool fHelp);
extern UniValue debug2(const UniValue& params, bool fHelp);
extern UniValue dumpcontracts(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);
Expand Down