Skip to content

Commit

Permalink
Added --data-dir command line option
Browse files Browse the repository at this point in the history
  • Loading branch information
SChernykh committed Dec 4, 2024
1 parent fb8e2a7 commit 0908375
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 21 deletions.
3 changes: 2 additions & 1 deletion docs/COMMAND_LINE.MD
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
--addpeers Comma-separated list of IP:port of other p2pool nodes to connect to
--light-mode Don't allocate RandomX dataset, saves 2GB of RAM
--loglevel Verbosity of the log, integer number between 0 and 6
--data-dir Path to store general p2pool files (log, cache, peer data, etc.), default is current directory
--config Name of p2pool sidechain's config file (don't use it unless you want to mine to a different p2pool chain)
--data-api Path to the p2pool JSON data (use it in tandem with an external web-server)
--data-api Path to the p2pool JSON data (use it in tandem with an external web-server). Not affected by --data-dir setting!
--local-api Enable /local/ path in api path for Stratum Server and built-in miner statistics
--stratum-api An alias for --local-api
--no-cache Disable p2pool.cache
Expand Down
12 changes: 8 additions & 4 deletions src/block_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ struct BlockCache::Impl : public nocopy_nomove

Impl()
{
m_fd = open(cache_name, O_RDWR | O_CREAT, static_cast<mode_t>(0600));
const std::string cache_path = DATA_DIR + cache_name;

m_fd = open(cache_path.c_str(), O_RDWR | O_CREAT, static_cast<mode_t>(0600));
if (m_fd == -1) {
LOGERR(1, "couldn't open/create " << cache_name);
LOGERR(1, "couldn't open/create " << cache_path << ": error " << errno);
return;
}

Expand Down Expand Up @@ -87,11 +89,12 @@ struct BlockCache::Impl : public nocopy_nomove
#elif defined(_WIN32)

Impl()
: m_file(CreateFile(cache_name, GENERIC_ALL, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL))
: m_cachePath(DATA_DIR + cache_name)
, m_file(CreateFile(m_cachePath.c_str(), GENERIC_ALL, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL))
, m_map(0)
{
if (m_file == INVALID_HANDLE_VALUE) {
LOGERR(1, "couldn't open " << cache_name << ", error " << static_cast<uint32_t>(GetLastError()));
LOGERR(1, "couldn't open " << m_cachePath << ": error " << static_cast<uint32_t>(GetLastError()));
return;
}

Expand Down Expand Up @@ -142,6 +145,7 @@ struct BlockCache::Impl : public nocopy_nomove
}
}

std::string m_cachePath;
HANDLE m_file;
HANDLE m_map;

Expand Down
20 changes: 12 additions & 8 deletions src/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ class Worker

std::setlocale(LC_ALL, "en_001");

m_logFile.open(log_file_name, std::ios::app | std::ios::binary);
m_logFilePath = DATA_DIR + log_file_name;
m_logFile.open(m_logFilePath, std::ios::app | std::ios::binary);

if (!m_logFile.is_open()) {
fprintf(stderr, "failed to open %s: error %d\n", m_logFilePath.c_str(), errno);
}

m_buf.resize(BUF_SIZE);

Expand All @@ -148,10 +153,6 @@ class Worker
CONSOLE_COLORS = false;
}

if (!m_logFile.is_open()) {
fprintf(stderr, "failed to open %s\n", log_file_name);
}

init_uv_threadpool();

const int err = uv_thread_create(&m_worker, run_wrapper, this);
Expand Down Expand Up @@ -216,6 +217,8 @@ class Worker
uv_cond_signal(&m_cond);
}

const std::string& log_file_path() const { return m_logFilePath; }

private:
static void init_uv_threadpool()
{
Expand Down Expand Up @@ -324,9 +327,9 @@ class Worker

// Reopen the log file if it's been moved (logrotate support)
struct stat buf;
if (stat(log_file_name, &buf) != 0) {
if (stat(m_logFilePath.c_str(), &buf) != 0) {
m_logFile.close();
m_logFile.open(log_file_name, std::ios::app | std::ios::binary);
m_logFile.open(m_logFilePath, std::ios::app | std::ios::binary);
}
}

Expand Down Expand Up @@ -373,6 +376,7 @@ class Worker
bool m_stopped;

std::ofstream m_logFile;
std::string m_logFilePath;
};

static Worker* worker = nullptr;
Expand Down Expand Up @@ -438,7 +442,7 @@ void start()
void reopen()
{
// This will trigger the worker thread which will then reopen log file if it's been moved
LOGINFO(0, "reopening " << log_file_name);
LOGINFO(0, "reopening " << worker->log_file_path());
}

void stop()
Expand Down
17 changes: 16 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ void p2pool_usage()
"--addpeers Comma-separated list of IP:port of other p2pool nodes to connect to\n"
"--light-mode Don't allocate RandomX dataset, saves 2GB of RAM\n"
"--loglevel Verbosity of the log, integer number between 0 and %d\n"
"--data-dir Path to store general p2pool files (log, cache, peer data, etc.), default is current directory\n"
"--config Name of the p2pool config file\n"
"--data-api Path to the p2pool JSON data (use it in tandem with an external web-server)\n"
"--data-api Path to the p2pool JSON data (use it in tandem with an external web-server). Not affected by --data-dir setting!\n"
"--local-api Enable /local/ path in api path for Stratum Server and built-in miner statistics\n"
"--stratum-api An alias for --local-api\n"
"--no-cache Disable p2pool.cache\n"
Expand Down Expand Up @@ -194,6 +195,20 @@ int main(int argc, char* argv[])
if (!strcmp(argv[i], "--test")) {
return p2pool_test();
}

if ((strcmp(argv[i], "--data-dir") == 0) && (i + 1 < argc)) {
std::string path = argv[++i];

if (!path.empty() && (path.back() != '/')
#ifdef _WIN32
&& (path.back() != '\\')
#endif
) {
path.append(1, '/');
}

p2pool::DATA_DIR = std::move(path);
}
}

#if defined(_WIN32) && defined(_MSC_VER) && !defined(NDEBUG)
Expand Down
1 change: 0 additions & 1 deletion src/mempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#include "common.h"
#include "mempool.h"
#include "util.h"

LOG_CATEGORY(Mempool)

Expand Down
10 changes: 7 additions & 3 deletions src/p2p_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,10 +447,12 @@ void P2PServer::save_peer_list_async()

void P2PServer::save_peer_list()
{
std::ofstream f(saved_peer_list_file_name, std::ios::binary);
const std::string path = DATA_DIR + saved_peer_list_file_name;

std::ofstream f(path, std::ios::binary);

if (!f.is_open()) {
LOGERR(1, "failed to save peer list " << saved_peer_list_file_name << ", error " << errno);
LOGERR(1, "failed to save peer list " << path << ": error " << errno);
return;
}

Expand Down Expand Up @@ -579,7 +581,9 @@ void P2PServer::load_peer_list()
}

// Finally load peers from p2pool_peers.txt
std::ifstream f(saved_peer_list_file_name);
const std::string path = DATA_DIR + saved_peer_list_file_name;

std::ifstream f(path);
if (f.is_open()) {
std::string address;
while (f.good()) {
Expand Down
7 changes: 4 additions & 3 deletions src/p2pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@ void p2pool::load_found_blocks()
return;
}

std::ifstream f(FOUND_BLOCKS_FILE);
std::ifstream f(DATA_DIR + FOUND_BLOCKS_FILE);
if (!f.is_open()) {
return;
}
Expand Down Expand Up @@ -1721,14 +1721,15 @@ void p2pool::api_update_block_found(const ChainMain* data, const PoolBlock* bloc
difficulty_type diff;

if (data && get_difficulty_at_height(data->height, diff)) {
std::ofstream f(FOUND_BLOCKS_FILE, std::ios::app);
const std::string path = DATA_DIR + FOUND_BLOCKS_FILE;
std::ofstream f(path, std::ios::app);
if (f.is_open()) {
f << cur_time << ' ' << data->height << ' ' << data->id << ' ' << diff << ' ' << total_hashes << '\n';
f.flush();
f.close();
}
else {
LOGERR(1, "Failed to update " << FOUND_BLOCKS_FILE << ", error " << errno);
LOGERR(1, "Failed to update " << path << ": error " << errno);
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ Params::Params(int argc, char* const argv[])
ok = true;
}

if ((strcmp(argv[i], "--data-dir") == 0) && (i + 1 < argc)) {
// Processed in main.cpp
++i;
ok = true;
}

if ((strcmp(argv[i], "--config") == 0) && (i + 1 < argc)) {
m_config = argv[++i];
ok = true;
Expand Down
2 changes: 2 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ const char* VERSION = "v" STR2(P2POOL_VERSION_MAJOR) "." STR2(P2POOL_VERSION_MIN
#endif
" on " __DATE__ ")";

std::string DATA_DIR;

SoftwareID get_software_id(uint32_t value)
{
switch (value) {
Expand Down
2 changes: 2 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ constexpr uint32_t P2POOL_VERSION = (P2POOL_VERSION_MAJOR << 16) | (P2POOL_VERSI

extern const char* VERSION;

extern std::string DATA_DIR;

enum class SoftwareID : uint32_t {
P2Pool = 0,
GoObserver = 0x624F6F47UL,
Expand Down

0 comments on commit 0908375

Please sign in to comment.