Skip to content

Commit 6a782f7

Browse files
committed
rpc: add dumpbeaconcontracts
1 parent b88f766 commit 6a782f7

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/rpc/blockchain.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,56 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool fP
201201
return result;
202202
}
203203

204+
UniValue dumpbeaconcontracts(const UniValue& params, bool fHelp)
205+
{
206+
if (fHelp || params.size() != 1)
207+
throw runtime_error(
208+
"dumpbeaconcontracts <file>\n"
209+
"\n"
210+
"<file> Output file.\n"
211+
"\n"
212+
"Dumps current beacon storage and all beacon contracts in the chain to a file.\n");
213+
214+
std::string path = params[0].get_str();
215+
if (path.empty() || fs::exists(path))
216+
throw runtime_error("Invalid path");
217+
218+
CBlock block;
219+
std::vector<CTransaction> vtx;
220+
std::vector<GRC::Cpid> active_beacons;
221+
std::vector<CKeyID> pending_beacons;
222+
CAutoFile file(fsbridge::fopen(path, "wb"), SER_DISK, PROTOCOL_VERSION);
223+
224+
LOCK(cs_main);
225+
226+
CBlockIndex* pblockindex = pindexGenesisBlock;
227+
while (pblockindex != nullptr) {
228+
block.ReadFromDisk(pblockindex);
229+
for (auto& tx : block.vtx) {
230+
for (const auto& contract : tx.GetContracts()) {
231+
if (contract.m_type == GRC::ContractType::BEACON) {
232+
vtx.push_back(tx);
233+
break;
234+
}
235+
}
236+
}
237+
pblockindex = pblockindex->pnext;
238+
}
239+
240+
for (const auto& x : GRC::GetBeaconRegistry().Beacons()) {
241+
active_beacons.push_back(x.first);
242+
}
243+
244+
for (const auto& x : GRC::GetBeaconRegistry().PendingBeacons()) {
245+
pending_beacons.push_back(x.first);
246+
}
247+
248+
file << vtx;
249+
file << active_beacons;
250+
file << pending_beacons;
251+
252+
return UniValue(UniValue::VOBJ);
253+
}
204254

205255
UniValue showblock(const UniValue& params, bool fHelp)
206256
{

src/rpc/server.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ static const CRPCCommand vRPCCommands[] =
369369
{ "currentcontractaverage", &currentcontractaverage, cat_developer },
370370
{ "debug", &debug, cat_developer },
371371
{ "debug10", &debug10, cat_developer },
372+
{ "dumpbeaconcontracts", &dumpbeaconcontracts, cat_developer },
372373
{ "exportstats1", &rpc_exportstats, cat_developer },
373374
{ "getblockstats", &rpc_getblockstats, cat_developer },
374375
{ "getlistof", &getlistof, cat_developer },

src/rpc/server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ extern UniValue currentcontractaverage(const UniValue& params, bool fHelp);
191191
extern UniValue debug(const UniValue& params, bool fHelp);
192192
extern UniValue debug10(const UniValue& params, bool fHelp);
193193
extern UniValue debug2(const UniValue& params, bool fHelp);
194+
extern UniValue dumpbeaconcontracts(const UniValue& params, bool fHelp);
194195
extern UniValue rpc_getblockstats(const UniValue& params, bool fHelp);
195196
extern UniValue getlistof(const UniValue& params, bool fHelp);
196197
extern UniValue inspectaccrualsnapshot(const UniValue& params, bool fHelp);

0 commit comments

Comments
 (0)