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

Add size argument to logdvmstate. Dump to data directory. #3082

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
28 changes: 22 additions & 6 deletions src/dfi/rpc_accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3550,8 +3550,10 @@ static std::string BytesToHex(const std::vector<unsigned char> &data) {
UniValue logdvmstate(const JSONRPCRequest &request) {
RPCHelpMan{
"logdvmstate",
"Log the full DVM state for debugging.\n",
{},
"Log the full DVM state for debugging in the datadir dumps directory.\n",
{
{"size", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "Size for each file in GBs, default 1024MB"},
},
RPCResult{"Generates logdvmstate-xxx.log files\n"},
RPCExamples{HelpExampleCli("logdvmstate", "")},
}
Expand All @@ -3568,8 +3570,21 @@ UniValue logdvmstate(const JSONRPCRequest &request) {
// Create a CDBIterator
auto pcursor = db->NewIterator(leveldb::ReadOptions());

const auto fileSize = request.params[0].isNull() ? 1 : request.params[0].get_int64();
if (fileSize <= 0) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Size must be more then zero");
}

const size_t MAX_FILE_SIZE = fileSize * 1024 * 1048576; // 1MB = 1048576 bytes

fs::path dumpsDir = GetDataDir() / "dumps";
if (!fs::exists(dumpsDir)) {
if (!fs::create_directory(dumpsDir)) {
throw JSONRPCError(RPC_MISC_ERROR, "Failed to create dumps directory");
}
}

// File handling variables
const size_t MAX_FILE_SIZE = 1ULL << 30; // 1 GB
size_t fileCounter = 0;
size_t bytesWritten = 0;
std::ofstream outFile;
Expand All @@ -3581,9 +3596,10 @@ UniValue logdvmstate(const JSONRPCRequest &request) {
}
std::ostringstream fileName;
fileName << "logdvmstate-" << std::setw(3) << std::setfill('0') << fileCounter << ".log";
outFile.open(fileName.str(), std::ios::out | std::ios::binary);
fs::path filePath = dumpsDir / fileName.str();
outFile.open(PathToString(filePath), std::ios::out | std::ios::binary);
if (!outFile) {
std::cerr << "Failed to open file: " << fileName.str() << std::endl;
std::cerr << "Failed to open file: " << PathToString(filePath) << std::endl;
return false;
}
bytesWritten = 0;
Expand Down Expand Up @@ -3684,7 +3700,7 @@ static const CRPCCommand commands[] = {
{"accounts", "listlockedtokens", &listlockedtokens, {} },
{"accounts", "getlockedtokens", &getlockedtokens, {"address"} },
{"accounts", "releaselockedtokens", &releaselockedtokens, {"releasePart"} },
{"hidden", "logdvmstate", &logdvmstate, {""} },
{"hidden", "logdvmstate", &logdvmstate, {"size"} },
};

void RegisterAccountsRPCCommands(CRPCTable &tableRPC) {
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ static const CRPCConvertParam vRPCConvertParams[] =

{ "releaselockedtokens", 0, "releasePart" },

{ "logdvmstate", 0, "size" },

{ "setgov", 0, "variables" },
{ "setgov", 1, "inputs" },

Expand Down
Loading