From 12335d09c40f26aea948822d299b6f0cc4045650 Mon Sep 17 00:00:00 2001 From: Chalith Desaman Date: Mon, 30 Nov 2020 22:21:56 +0530 Subject: [PATCH] Skip ledger truncation when full history is enabled (#177) * Skip ledger truncation when full history is enabled. * Skip clearing ledger if full history is enabled. --- src/ledger.cpp | 32 +++++++++++++++++++------------- src/ledger.hpp | 2 +- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/ledger.cpp b/src/ledger.cpp index 4fabec10..3436a781 100644 --- a/src/ledger.cpp +++ b/src/ledger.cpp @@ -197,6 +197,7 @@ namespace ledger if (!sync_ctx.target_lcl.empty()) { + // If target lcl is genesis lcl, Clear the ledger history and reset target sequence number. if (sync_ctx.target_lcl == GENESIS_LEDGER) { LOG_INFO << "lcl sync: Target is GENESIS. Clearing our history."; @@ -205,9 +206,10 @@ namespace ledger sync_ctx.target_lcl_seq_no = 0; sync_ctx.is_syncing = false; } - // Check the target lcl seq no. to see whether it's too far ahead. That means no one probably has our + // If full history mode is not enabled check the target lcl seq no + // to see whether it's too far ahead. That means no one probably has our // lcl in their ledgers. So we should clear our entire ledger history before requesting from peers. - else if (current_lcl != GENESIS_LEDGER && sync_ctx.target_lcl_seq_no > (ctx.get_seq_no() + MAX_LEDGER_SEQUENCE)) + else if (!conf::cfg.fullhistory && current_lcl != GENESIS_LEDGER && sync_ctx.target_lcl_seq_no > (ctx.get_seq_no() + MAX_LEDGER_SEQUENCE)) { LOG_INFO << "lcl sync: Target " << sync_ctx.target_lcl.substr(0, 15) << " is too far ahead. Clearing our history."; clear_ledger(); @@ -354,20 +356,24 @@ namespace ledger */ void remove_old_ledgers(const uint64_t led_seq_no) { - std::map::iterator itr; - - for (itr = ctx.cache.begin(); - itr != ctx.cache.lower_bound(led_seq_no + 1); - itr++) + // Remove old ledgers if full history mode is not enabled. + if (!conf::cfg.fullhistory) { - const std::string file_path = conf::ctx.hist_dir + "/" + itr->second + ".lcl"; + std::map::iterator itr; - if (util::is_file_exists(file_path)) - util::remove_file(file_path); + for (itr = ctx.cache.begin(); + itr != ctx.cache.lower_bound(led_seq_no + 1); + itr++) + { + const std::string file_path = conf::ctx.hist_dir + "/" + itr->second + ".lcl"; + + if (util::is_file_exists(file_path)) + util::remove_file(file_path); + } + + if (!ctx.cache.empty()) + ctx.cache.erase(ctx.cache.begin(), ctx.cache.lower_bound(led_seq_no + 1)); } - - if (!ctx.cache.empty()) - ctx.cache.erase(ctx.cache.begin(), ctx.cache.lower_bound(led_seq_no + 1)); } /** diff --git a/src/ledger.hpp b/src/ledger.hpp index b6b7449a..afdc65fc 100644 --- a/src/ledger.hpp +++ b/src/ledger.hpp @@ -38,7 +38,7 @@ namespace ledger // Map of closed ledgers (lcl string) with sequence number as map key. // Contains closed ledgers from oldest to latest - MAX_LEDGER_SEQUENCE. // This is loaded when node started and updated throughout consensus. - // Deletes ledgers that falls behind MAX_LEDGER_SEQUENCE range. + // If full history mode is not enabled, deletes ledgers that falls behind MAX_LEDGER_SEQUENCE range. std::map cache; std::mutex ledger_mutex;