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

Dump CCustomCSView DB to files #3081

Merged
merged 1 commit into from
Oct 4, 2024
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
8 changes: 8 additions & 0 deletions src/dbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ class CDBIterator
return true;
}

leveldb::Slice GetKey() {
return piter->key();
}

leveldb::Slice GetValue() {
return piter->value();
}

unsigned int GetValueSize() {
return piter->value().size();
}
Expand Down
117 changes: 117 additions & 0 deletions src/dfi/rpc_accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <ffi/ffihelpers.h>
#include <boost/asio.hpp>

#include <fstream>

static bool DEFAULT_DVM_OWNERSHIP_CHECK = true;

std::string tokenAmountString(const CCustomCSView &view,
Expand Down Expand Up @@ -3537,6 +3539,120 @@ UniValue getpendingdusdswaps(const JSONRPCRequest &request) {
return GetRPCResultCache().Set(request, obj);
}

static std::string BytesToHex(const std::vector<unsigned char> &data) {
std::ostringstream oss;
for (auto byte : data) {
oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte);
}
return oss.str();
}

UniValue logdvmstate(const JSONRPCRequest &request) {
RPCHelpMan{
"logdvmstate",
"Log the full DVM state for debugging.\n",
{},
RPCResult{"Generates logdvmstate-xxx.log files\n"},
RPCExamples{HelpExampleCli("logdvmstate", "")},
}
.Check(request);

LOCK(cs_main);

// Flush any pending changes to the DB. Not always written to disk.
pcustomcsview->Flush();

// Get the CDBWrapper instance from CCustomCSView
auto db = pcustomcsview->GetStorage().GetStorageLevelDB()->GetDB();

// Create a CDBIterator
auto pcursor = db->NewIterator(leveldb::ReadOptions());

// File handling variables
const size_t MAX_FILE_SIZE = 1ULL << 30; // 1 GB
size_t fileCounter = 0;
size_t bytesWritten = 0;
std::ofstream outFile;

// Function to open a new file
auto openNewFile = [&]() -> bool {
if (outFile.is_open()) {
outFile.close();
}
std::ostringstream fileName;
fileName << "logdvmstate-" << std::setw(3) << std::setfill('0') << fileCounter << ".log";
outFile.open(fileName.str(), std::ios::out | std::ios::binary);
if (!outFile) {
std::cerr << "Failed to open file: " << fileName.str() << std::endl;
return false;
}
bytesWritten = 0;
fileCounter++;
return true;
};

// Open the first file
if (!openNewFile()) {
return {};
}

// Seek to the beginning of the database
pcursor->SeekToFirst();

// Iterate over all key-value pairs
while (pcursor->Valid()) {
// Get the key and value slices
auto keySlice = pcursor->GetKey();
auto valueSlice = pcursor->GetValue();

// Convert key and value to byte vectors
std::vector<unsigned char> vKey(keySlice.data(), keySlice.data() + keySlice.size());
std::vector<unsigned char> vValue(valueSlice.data(), valueSlice.data() + valueSlice.size());

if (!vKey.empty()) {
auto &prefix = vKey[0];
std::string keyPrefixName = BytesToHex({prefix});

// Convert the rest of the key
std::string keyRestHex;
if (vKey.size() > 1) {
keyRestHex = BytesToHex(std::vector<unsigned char>(vKey.begin() + 1, vKey.end()));
}

// Convert value
std::string valueHex = BytesToHex(vValue);

// Prepare output
std::ostringstream oss;
oss << keyPrefixName << " ";
if (!keyRestHex.empty()) {
oss << keyRestHex << " ";
}
oss << valueHex << "\n";
std::string outputStr = oss.str();

// Write to file
outFile << outputStr;
bytesWritten += outputStr.size();

// Check file size limit
if (bytesWritten >= MAX_FILE_SIZE) {
if (!openNewFile()) {
return {};
}
}
}

pcursor->Next();
}

if (outFile.is_open()) {
outFile.close();
}

return {};
}

static const CRPCCommand commands[] = {
// category name actor (function) params
// ------------- ------------------------ ---------------------- ----------
Expand Down Expand Up @@ -3568,6 +3684,7 @@ static const CRPCCommand commands[] = {
{"accounts", "listlockedtokens", &listlockedtokens, {} },
{"accounts", "getlockedtokens", &getlockedtokens, {"address"} },
{"accounts", "releaselockedtokens", &releaselockedtokens, {"releasePart"} },
{"hidden", "logdvmstate", &logdvmstate, {""} },
};

void RegisterAccountsRPCCommands(CRPCTable &tableRPC) {
Expand Down
Loading