diff options
author | TheCharlatan <seb.kung@gmail.com> | 2023-05-10 22:36:04 +0200 |
---|---|---|
committer | TheCharlatan <seb.kung@gmail.com> | 2023-05-20 12:03:26 +0200 |
commit | 4452707edec91c7d7991f486dd41ef3edb4f7fbf (patch) | |
tree | 4f06c4ad2e74d53af4ed1bd5f2325a5602ba0ee6 | |
parent | 84d71457e7250ab25c0a11d1ad1c7657197ffd90 (diff) |
kernel: Add progress method to notifications
This commit is part of the libbitcoinkernel project and seeks to remove
the ChainstateManager's and, more generally, the kernel library's
dependency on interface_ui with options methods in this and the
following few commits. By removing interface_ui from the kernel library,
its dependency on boost is reduced to just boost::multi_index.
-rw-r--r-- | src/bitcoin-chainstate.cpp | 5 | ||||
-rw-r--r-- | src/kernel/notifications_interface.h | 3 | ||||
-rw-r--r-- | src/node/chainstate.cpp | 2 | ||||
-rw-r--r-- | src/node/kernel_notifications.cpp | 6 | ||||
-rw-r--r-- | src/node/kernel_notifications.h | 4 | ||||
-rw-r--r-- | src/rpc/blockchain.cpp | 2 | ||||
-rw-r--r-- | src/validation.cpp | 19 | ||||
-rw-r--r-- | src/validation.h | 8 |
8 files changed, 37 insertions, 12 deletions
diff --git a/src/bitcoin-chainstate.cpp b/src/bitcoin-chainstate.cpp index a184d64ff0..b949bafcc8 100644 --- a/src/bitcoin-chainstate.cpp +++ b/src/bitcoin-chainstate.cpp @@ -37,6 +37,7 @@ #include <functional> #include <iosfwd> #include <memory> +#include <string> int main(int argc, char* argv[]) { @@ -94,6 +95,10 @@ int main(int argc, char* argv[]) { std::cout << "Header tip changed: " << height << ", " << timestamp << ", " << presync << std::endl; } + void progress(const bilingual_str& title, int progress_percent, bool resume_possible) override + { + std::cout << "Progress: " << title.original << ", " << progress_percent << ", " << resume_possible << std::endl; + } }; auto notifications = std::make_unique<KernelNotifications>(); diff --git a/src/kernel/notifications_interface.h b/src/kernel/notifications_interface.h index 7f88176d03..1d5643ca07 100644 --- a/src/kernel/notifications_interface.h +++ b/src/kernel/notifications_interface.h @@ -6,9 +6,11 @@ #define BITCOIN_KERNEL_NOTIFICATIONS_INTERFACE_H #include <cstdint> +#include <string> class CBlockIndex; enum class SynchronizationState; +struct bilingual_str; namespace kernel { @@ -23,6 +25,7 @@ public: virtual void blockTip(SynchronizationState state, CBlockIndex& index) {} virtual void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) {} + virtual void progress(const bilingual_str& title, int progress_percent, bool resume_possible) {} }; } // namespace kernel diff --git a/src/node/chainstate.cpp b/src/node/chainstate.cpp index 40b609d069..8f997b0594 100644 --- a/src/node/chainstate.cpp +++ b/src/node/chainstate.cpp @@ -253,7 +253,7 @@ ChainstateLoadResult VerifyLoadedChainstate(ChainstateManager& chainman, const C "Only rebuild the block database if you are sure that your computer's date and time are correct")}; } - VerifyDBResult result = CVerifyDB().VerifyDB( + VerifyDBResult result = CVerifyDB(chainman.GetNotifications()).VerifyDB( *chainstate, chainman.GetConsensus(), chainstate->CoinsDB(), options.check_level, options.check_blocks); diff --git a/src/node/kernel_notifications.cpp b/src/node/kernel_notifications.cpp index 1e4bc20f2f..9efbc6c4ca 100644 --- a/src/node/kernel_notifications.cpp +++ b/src/node/kernel_notifications.cpp @@ -5,6 +5,7 @@ #include <node/kernel_notifications.h> #include <node/interface_ui.h> +#include <util/translation.h> namespace node { @@ -18,4 +19,9 @@ void KernelNotifications::headerTip(SynchronizationState state, int64_t height, uiInterface.NotifyHeaderTip(state, height, timestamp, presync); } +void KernelNotifications::progress(const bilingual_str& title, int progress_percent, bool resume_possible) +{ + uiInterface.ShowProgress(title.translated, progress_percent, resume_possible); +} + } // namespace node diff --git a/src/node/kernel_notifications.h b/src/node/kernel_notifications.h index 1507c3214a..ecd21c3a58 100644 --- a/src/node/kernel_notifications.h +++ b/src/node/kernel_notifications.h @@ -8,9 +8,11 @@ #include <kernel/notifications_interface.h> #include <cstdint> +#include <string> class CBlockIndex; enum class SynchronizationState; +struct bilingual_str; namespace node { class KernelNotifications : public kernel::Notifications @@ -19,6 +21,8 @@ public: void blockTip(SynchronizationState state, CBlockIndex& index) override; void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) override; + + void progress(const bilingual_str& title, int progress_percent, bool resume_possible) override; }; } // namespace node diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 72866532d2..88370b5925 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1123,7 +1123,7 @@ static RPCHelpMan verifychain() LOCK(cs_main); Chainstate& active_chainstate = chainman.ActiveChainstate(); - return CVerifyDB().VerifyDB( + return CVerifyDB(chainman.GetNotifications()).VerifyDB( active_chainstate, chainman.GetParams().GetConsensus(), active_chainstate.CoinsTip(), check_level, check_depth) == VerifyDBResult::SUCCESS; }, }; diff --git a/src/validation.cpp b/src/validation.cpp index 2159270943..46b5b3e8ef 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -23,6 +23,7 @@ #include <hash.h> #include <kernel/chainparams.h> #include <kernel/mempool_entry.h> +#include <kernel/notifications_interface.h> #include <logging.h> #include <logging/timer.h> #include <node/blockstorage.h> @@ -72,6 +73,7 @@ using kernel::CCoinsStats; using kernel::CoinStatsHashType; using kernel::ComputeUTXOStats; using kernel::LoadMempool; +using kernel::Notifications; using fsbridge::FopenFn; using node::BlockManager; @@ -4145,14 +4147,15 @@ bool Chainstate::LoadChainTip() return true; } -CVerifyDB::CVerifyDB() +CVerifyDB::CVerifyDB(Notifications& notifications) + : m_notifications{notifications} { - uiInterface.ShowProgress(_("Verifying blocks…").translated, 0, false); + m_notifications.progress(_("Verifying blocks…"), 0, false); } CVerifyDB::~CVerifyDB() { - uiInterface.ShowProgress("", 100, false); + m_notifications.progress(bilingual_str{}, 100, false); } VerifyDBResult CVerifyDB::VerifyDB( @@ -4192,7 +4195,7 @@ VerifyDBResult CVerifyDB::VerifyDB( LogPrintf("Verification progress: %d%%\n", percentageDone); reportDone = percentageDone / 10; } - uiInterface.ShowProgress(_("Verifying blocks…").translated, percentageDone, false); + m_notifications.progress(_("Verifying blocks…"), percentageDone, false); if (pindex->nHeight <= chainstate.m_chain.Height() - nCheckDepth) { break; } @@ -4268,7 +4271,7 @@ VerifyDBResult CVerifyDB::VerifyDB( LogPrintf("Verification progress: %d%%\n", percentageDone); reportDone = percentageDone / 10; } - uiInterface.ShowProgress(_("Verifying blocks…").translated, percentageDone, false); + m_notifications.progress(_("Verifying blocks…"), percentageDone, false); pindex = chainstate.m_chain.Next(pindex); CBlock block; if (!chainstate.m_blockman.ReadBlockFromDisk(block, *pindex)) { @@ -4327,7 +4330,7 @@ bool Chainstate::ReplayBlocks() if (hashHeads.empty()) return true; // We're already in a consistent state. if (hashHeads.size() != 2) return error("ReplayBlocks(): unknown inconsistent state"); - uiInterface.ShowProgress(_("Replaying blocks…").translated, 0, false); + m_chainman.GetNotifications().progress(_("Replaying blocks…"), 0, false); LogPrintf("Replaying blocks\n"); const CBlockIndex* pindexOld = nullptr; // Old tip during the interrupted flush. @@ -4374,13 +4377,13 @@ bool Chainstate::ReplayBlocks() const CBlockIndex& pindex{*Assert(pindexNew->GetAncestor(nHeight))}; LogPrintf("Rolling forward %s (%i)\n", pindex.GetBlockHash().ToString(), nHeight); - uiInterface.ShowProgress(_("Replaying blocks…").translated, (int) ((nHeight - nForkHeight) * 100.0 / (pindexNew->nHeight - nForkHeight)) , false); + m_chainman.GetNotifications().progress(_("Replaying blocks…"), (int)((nHeight - nForkHeight) * 100.0 / (pindexNew->nHeight - nForkHeight)), false); if (!RollforwardBlock(&pindex, cache)) return false; } cache.SetBestBlock(pindexNew->GetBlockHash()); cache.Flush(); - uiInterface.ShowProgress("", 100, false); + m_chainman.GetNotifications().progress(bilingual_str{}, 100, false); return true; } diff --git a/src/validation.h b/src/validation.h index 352056f063..444fe72db4 100644 --- a/src/validation.h +++ b/src/validation.h @@ -364,9 +364,13 @@ enum class VerifyDBResult { }; /** RAII wrapper for VerifyDB: Verify consistency of the block and coin databases */ -class CVerifyDB { +class CVerifyDB +{ +private: + kernel::Notifications& m_notifications; + public: - CVerifyDB(); + explicit CVerifyDB(kernel::Notifications& notifications); ~CVerifyDB(); [[nodiscard]] VerifyDBResult VerifyDB( Chainstate& chainstate, |