From ed78e9df6eca427b38d4b711455c4d98d89f0058 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Tue, 13 Sep 2022 18:26:01 +0200 Subject: [PATCH] More reliable file load/save --- src/p2p_server.cpp | 3 ++- src/p2pool.cpp | 13 ++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/p2p_server.cpp b/src/p2p_server.cpp index c9b15a2..8323c54 100644 --- a/src/p2p_server.cpp +++ b/src/p2p_server.cpp @@ -477,6 +477,7 @@ void P2PServer::save_peer_list() } } + f.flush(); f.close(); LOGINFO(5, "peer list saved (" << peer_list.size() << " peers)"); @@ -557,7 +558,7 @@ void P2PServer::load_peer_list() std::ifstream f(saved_peer_list_file_name); if (f.is_open()) { std::string address; - while (!f.eof()) { + while (f.good()) { std::getline(f, address); if (!address.empty()) { if (!saved_list.empty()) { diff --git a/src/p2pool.cpp b/src/p2pool.cpp index 120e32a..dba128c 100644 --- a/src/p2pool.cpp +++ b/src/p2pool.cpp @@ -826,25 +826,26 @@ void p2pool::load_found_blocks() return; } - while (!f.eof()) { + while (f.good()) { time_t timestamp; f >> timestamp; - if (f.eof()) break; + if (!f.good()) break; uint64_t height; f >> height; - if (f.eof()) break; + if (!f.good()) break; hash id; f >> id; - if (f.eof()) break; + if (!f.good()) break; difficulty_type block_difficulty; f >> block_difficulty; - if (f.eof()) break; + if (!f.good()) break; difficulty_type cumulative_difficulty; f >> cumulative_difficulty; + if (!f.good() && !f.eof()) break; m_foundBlocks.emplace_back(timestamp, height, id, block_difficulty, cumulative_difficulty); } @@ -1333,6 +1334,8 @@ void p2pool::api_update_block_found(const ChainMain* data) std::ofstream f(FOUND_BLOCKS_FILE, std::ios::app); if (f.is_open()) { f << cur_time << ' ' << data->height << ' ' << data->id << ' ' << diff << ' ' << total_hashes << '\n'; + f.flush(); + f.close(); } }