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 <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>();

View File

@ -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

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")};
}
VerifyDBResult result = CVerifyDB().VerifyDB(
VerifyDBResult result = CVerifyDB(chainman.GetNotifications()).VerifyDB(
*chainstate, chainman.GetConsensus(), chainstate->CoinsDB(),
options.check_level,
options.check_blocks);

View File

@ -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

View File

@ -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

View File

@ -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;
},
};

View File

@ -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;
}

View File

@ -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,