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.
This commit is contained in:
TheCharlatan 2023-05-10 22:36:04 +02:00
parent 84d71457e7
commit 4452707ede
No known key found for this signature in database
GPG key ID: 9B79B45691DB4173
8 changed files with 37 additions and 12 deletions

View file

@ -37,6 +37,7 @@
#include <functional> #include <functional>
#include <iosfwd> #include <iosfwd>
#include <memory> #include <memory>
#include <string>
int main(int argc, char* argv[]) 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; 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>(); auto notifications = std::make_unique<KernelNotifications>();

View file

@ -6,9 +6,11 @@
#define BITCOIN_KERNEL_NOTIFICATIONS_INTERFACE_H #define BITCOIN_KERNEL_NOTIFICATIONS_INTERFACE_H
#include <cstdint> #include <cstdint>
#include <string>
class CBlockIndex; class CBlockIndex;
enum class SynchronizationState; enum class SynchronizationState;
struct bilingual_str;
namespace kernel { namespace kernel {
@ -23,6 +25,7 @@ public:
virtual void blockTip(SynchronizationState state, CBlockIndex& index) {} virtual void blockTip(SynchronizationState state, CBlockIndex& index) {}
virtual void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) {} 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 } // namespace kernel

View file

@ -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")}; "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(), *chainstate, chainman.GetConsensus(), chainstate->CoinsDB(),
options.check_level, options.check_level,
options.check_blocks); options.check_blocks);

View file

@ -5,6 +5,7 @@
#include <node/kernel_notifications.h> #include <node/kernel_notifications.h>
#include <node/interface_ui.h> #include <node/interface_ui.h>
#include <util/translation.h>
namespace node { namespace node {
@ -18,4 +19,9 @@ void KernelNotifications::headerTip(SynchronizationState state, int64_t height,
uiInterface.NotifyHeaderTip(state, height, timestamp, presync); 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 } // namespace node

View file

@ -8,9 +8,11 @@
#include <kernel/notifications_interface.h> #include <kernel/notifications_interface.h>
#include <cstdint> #include <cstdint>
#include <string>
class CBlockIndex; class CBlockIndex;
enum class SynchronizationState; enum class SynchronizationState;
struct bilingual_str;
namespace node { namespace node {
class KernelNotifications : public kernel::Notifications class KernelNotifications : public kernel::Notifications
@ -19,6 +21,8 @@ public:
void blockTip(SynchronizationState state, CBlockIndex& index) override; void blockTip(SynchronizationState state, CBlockIndex& index) override;
void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) 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 } // namespace node

View file

@ -1123,7 +1123,7 @@ static RPCHelpMan verifychain()
LOCK(cs_main); LOCK(cs_main);
Chainstate& active_chainstate = chainman.ActiveChainstate(); 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; active_chainstate, chainman.GetParams().GetConsensus(), active_chainstate.CoinsTip(), check_level, check_depth) == VerifyDBResult::SUCCESS;
}, },
}; };

View file

@ -23,6 +23,7 @@
#include <hash.h> #include <hash.h>
#include <kernel/chainparams.h> #include <kernel/chainparams.h>
#include <kernel/mempool_entry.h> #include <kernel/mempool_entry.h>
#include <kernel/notifications_interface.h>
#include <logging.h> #include <logging.h>
#include <logging/timer.h> #include <logging/timer.h>
#include <node/blockstorage.h> #include <node/blockstorage.h>
@ -72,6 +73,7 @@ using kernel::CCoinsStats;
using kernel::CoinStatsHashType; using kernel::CoinStatsHashType;
using kernel::ComputeUTXOStats; using kernel::ComputeUTXOStats;
using kernel::LoadMempool; using kernel::LoadMempool;
using kernel::Notifications;
using fsbridge::FopenFn; using fsbridge::FopenFn;
using node::BlockManager; using node::BlockManager;
@ -4145,14 +4147,15 @@ bool Chainstate::LoadChainTip()
return true; 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() CVerifyDB::~CVerifyDB()
{ {
uiInterface.ShowProgress("", 100, false); m_notifications.progress(bilingual_str{}, 100, false);
} }
VerifyDBResult CVerifyDB::VerifyDB( VerifyDBResult CVerifyDB::VerifyDB(
@ -4192,7 +4195,7 @@ VerifyDBResult CVerifyDB::VerifyDB(
LogPrintf("Verification progress: %d%%\n", percentageDone); LogPrintf("Verification progress: %d%%\n", percentageDone);
reportDone = percentageDone / 10; 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) { if (pindex->nHeight <= chainstate.m_chain.Height() - nCheckDepth) {
break; break;
} }
@ -4268,7 +4271,7 @@ VerifyDBResult CVerifyDB::VerifyDB(
LogPrintf("Verification progress: %d%%\n", percentageDone); LogPrintf("Verification progress: %d%%\n", percentageDone);
reportDone = percentageDone / 10; reportDone = percentageDone / 10;
} }
uiInterface.ShowProgress(_("Verifying blocks…").translated, percentageDone, false); m_notifications.progress(_("Verifying blocks…"), percentageDone, false);
pindex = chainstate.m_chain.Next(pindex); pindex = chainstate.m_chain.Next(pindex);
CBlock block; CBlock block;
if (!chainstate.m_blockman.ReadBlockFromDisk(block, *pindex)) { 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.empty()) return true; // We're already in a consistent state.
if (hashHeads.size() != 2) return error("ReplayBlocks(): unknown inconsistent 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"); LogPrintf("Replaying blocks\n");
const CBlockIndex* pindexOld = nullptr; // Old tip during the interrupted flush. const CBlockIndex* pindexOld = nullptr; // Old tip during the interrupted flush.
@ -4374,13 +4377,13 @@ bool Chainstate::ReplayBlocks()
const CBlockIndex& pindex{*Assert(pindexNew->GetAncestor(nHeight))}; const CBlockIndex& pindex{*Assert(pindexNew->GetAncestor(nHeight))};
LogPrintf("Rolling forward %s (%i)\n", pindex.GetBlockHash().ToString(), 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; if (!RollforwardBlock(&pindex, cache)) return false;
} }
cache.SetBestBlock(pindexNew->GetBlockHash()); cache.SetBestBlock(pindexNew->GetBlockHash());
cache.Flush(); cache.Flush();
uiInterface.ShowProgress("", 100, false); m_chainman.GetNotifications().progress(bilingual_str{}, 100, false);
return true; return true;
} }

View file

@ -364,9 +364,13 @@ enum class VerifyDBResult {
}; };
/** RAII wrapper for VerifyDB: Verify consistency of the block and coin databases */ /** RAII wrapper for VerifyDB: Verify consistency of the block and coin databases */
class CVerifyDB { class CVerifyDB
{
private:
kernel::Notifications& m_notifications;
public: public:
CVerifyDB(); explicit CVerifyDB(kernel::Notifications& notifications);
~CVerifyDB(); ~CVerifyDB();
[[nodiscard]] VerifyDBResult VerifyDB( [[nodiscard]] VerifyDBResult VerifyDB(
Chainstate& chainstate, Chainstate& chainstate,