Skip to content

Commit

Permalink
Add size argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar committed Oct 4, 2024
1 parent af38693 commit 898b86a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/dfi/rpc_accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3291,8 +3291,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 @@ -3309,8 +3311,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 @@ -3322,9 +3337,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 @@ -3422,7 +3438,7 @@ static const CRPCCommand commands[] = {
{"accounts", "listpendingdusdswaps", &listpendingdusdswaps, {} },
{"accounts", "getpendingdusdswaps", &getpendingdusdswaps, {"address"} },
{"hidden", "logaccountbalances", &logaccountbalances, {"logfile", "rpcresult"} },
{"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 @@ -320,6 +320,8 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "listburnhistory", 0, "options" },
{ "accounthistorycount", 1, "options" },

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

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

Expand Down

0 comments on commit 898b86a

Please sign in to comment.