Skip to content

Commit 4452707

Browse files
committed
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.
1 parent 84d7145 commit 4452707

File tree

8 files changed

+37
-12
lines changed

8 files changed

+37
-12
lines changed

src/bitcoin-chainstate.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <functional>
3838
#include <iosfwd>
3939
#include <memory>
40+
#include <string>
4041

4142
int main(int argc, char* argv[])
4243
{
@@ -94,6 +95,10 @@ int main(int argc, char* argv[])
9495
{
9596
std::cout << "Header tip changed: " << height << ", " << timestamp << ", " << presync << std::endl;
9697
}
98+
void progress(const bilingual_str& title, int progress_percent, bool resume_possible) override
99+
{
100+
std::cout << "Progress: " << title.original << ", " << progress_percent << ", " << resume_possible << std::endl;
101+
}
97102
};
98103
auto notifications = std::make_unique<KernelNotifications>();
99104

src/kernel/notifications_interface.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
#define BITCOIN_KERNEL_NOTIFICATIONS_INTERFACE_H
77

88
#include <cstdint>
9+
#include <string>
910

1011
class CBlockIndex;
1112
enum class SynchronizationState;
13+
struct bilingual_str;
1214

1315
namespace kernel {
1416

@@ -23,6 +25,7 @@ class Notifications
2325

2426
virtual void blockTip(SynchronizationState state, CBlockIndex& index) {}
2527
virtual void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) {}
28+
virtual void progress(const bilingual_str& title, int progress_percent, bool resume_possible) {}
2629
};
2730
} // namespace kernel
2831

src/node/chainstate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ ChainstateLoadResult VerifyLoadedChainstate(ChainstateManager& chainman, const C
253253
"Only rebuild the block database if you are sure that your computer's date and time are correct")};
254254
}
255255

256-
VerifyDBResult result = CVerifyDB().VerifyDB(
256+
VerifyDBResult result = CVerifyDB(chainman.GetNotifications()).VerifyDB(
257257
*chainstate, chainman.GetConsensus(), chainstate->CoinsDB(),
258258
options.check_level,
259259
options.check_blocks);

src/node/kernel_notifications.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <node/kernel_notifications.h>
66

77
#include <node/interface_ui.h>
8+
#include <util/translation.h>
89

910
namespace node {
1011

@@ -18,4 +19,9 @@ void KernelNotifications::headerTip(SynchronizationState state, int64_t height,
1819
uiInterface.NotifyHeaderTip(state, height, timestamp, presync);
1920
}
2021

22+
void KernelNotifications::progress(const bilingual_str& title, int progress_percent, bool resume_possible)
23+
{
24+
uiInterface.ShowProgress(title.translated, progress_percent, resume_possible);
25+
}
26+
2127
} // namespace node

src/node/kernel_notifications.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
#include <kernel/notifications_interface.h>
99

1010
#include <cstdint>
11+
#include <string>
1112

1213
class CBlockIndex;
1314
enum class SynchronizationState;
15+
struct bilingual_str;
1416

1517
namespace node {
1618
class KernelNotifications : public kernel::Notifications
@@ -19,6 +21,8 @@ class KernelNotifications : public kernel::Notifications
1921
void blockTip(SynchronizationState state, CBlockIndex& index) override;
2022

2123
void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) override;
24+
25+
void progress(const bilingual_str& title, int progress_percent, bool resume_possible) override;
2226
};
2327
} // namespace node
2428

src/rpc/blockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ static RPCHelpMan verifychain()
11231123
LOCK(cs_main);
11241124

11251125
Chainstate& active_chainstate = chainman.ActiveChainstate();
1126-
return CVerifyDB().VerifyDB(
1126+
return CVerifyDB(chainman.GetNotifications()).VerifyDB(
11271127
active_chainstate, chainman.GetParams().GetConsensus(), active_chainstate.CoinsTip(), check_level, check_depth) == VerifyDBResult::SUCCESS;
11281128
},
11291129
};

src/validation.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <hash.h>
2424
#include <kernel/chainparams.h>
2525
#include <kernel/mempool_entry.h>
26+
#include <kernel/notifications_interface.h>
2627
#include <logging.h>
2728
#include <logging/timer.h>
2829
#include <node/blockstorage.h>
@@ -72,6 +73,7 @@ using kernel::CCoinsStats;
7273
using kernel::CoinStatsHashType;
7374
using kernel::ComputeUTXOStats;
7475
using kernel::LoadMempool;
76+
using kernel::Notifications;
7577

7678
using fsbridge::FopenFn;
7779
using node::BlockManager;
@@ -4145,14 +4147,15 @@ bool Chainstate::LoadChainTip()
41454147
return true;
41464148
}
41474149

4148-
CVerifyDB::CVerifyDB()
4150+
CVerifyDB::CVerifyDB(Notifications& notifications)
4151+
: m_notifications{notifications}
41494152
{
4150-
uiInterface.ShowProgress(_("Verifying blocks…").translated, 0, false);
4153+
m_notifications.progress(_("Verifying blocks…"), 0, false);
41514154
}
41524155

41534156
CVerifyDB::~CVerifyDB()
41544157
{
4155-
uiInterface.ShowProgress("", 100, false);
4158+
m_notifications.progress(bilingual_str{}, 100, false);
41564159
}
41574160

41584161
VerifyDBResult CVerifyDB::VerifyDB(
@@ -4192,7 +4195,7 @@ VerifyDBResult CVerifyDB::VerifyDB(
41924195
LogPrintf("Verification progress: %d%%\n", percentageDone);
41934196
reportDone = percentageDone / 10;
41944197
}
4195-
uiInterface.ShowProgress(_("Verifying blocks…").translated, percentageDone, false);
4198+
m_notifications.progress(_("Verifying blocks…"), percentageDone, false);
41964199
if (pindex->nHeight <= chainstate.m_chain.Height() - nCheckDepth) {
41974200
break;
41984201
}
@@ -4268,7 +4271,7 @@ VerifyDBResult CVerifyDB::VerifyDB(
42684271
LogPrintf("Verification progress: %d%%\n", percentageDone);
42694272
reportDone = percentageDone / 10;
42704273
}
4271-
uiInterface.ShowProgress(_("Verifying blocks…").translated, percentageDone, false);
4274+
m_notifications.progress(_("Verifying blocks…"), percentageDone, false);
42724275
pindex = chainstate.m_chain.Next(pindex);
42734276
CBlock block;
42744277
if (!chainstate.m_blockman.ReadBlockFromDisk(block, *pindex)) {
@@ -4327,7 +4330,7 @@ bool Chainstate::ReplayBlocks()
43274330
if (hashHeads.empty()) return true; // We're already in a consistent state.
43284331
if (hashHeads.size() != 2) return error("ReplayBlocks(): unknown inconsistent state");
43294332

4330-
uiInterface.ShowProgress(_("Replaying blocks…").translated, 0, false);
4333+
m_chainman.GetNotifications().progress(_("Replaying blocks…"), 0, false);
43314334
LogPrintf("Replaying blocks\n");
43324335

43334336
const CBlockIndex* pindexOld = nullptr; // Old tip during the interrupted flush.
@@ -4374,13 +4377,13 @@ bool Chainstate::ReplayBlocks()
43744377
const CBlockIndex& pindex{*Assert(pindexNew->GetAncestor(nHeight))};
43754378

43764379
LogPrintf("Rolling forward %s (%i)\n", pindex.GetBlockHash().ToString(), nHeight);
4377-
uiInterface.ShowProgress(_("Replaying blocks…").translated, (int) ((nHeight - nForkHeight) * 100.0 / (pindexNew->nHeight - nForkHeight)) , false);
4380+
m_chainman.GetNotifications().progress(_("Replaying blocks…"), (int)((nHeight - nForkHeight) * 100.0 / (pindexNew->nHeight - nForkHeight)), false);
43784381
if (!RollforwardBlock(&pindex, cache)) return false;
43794382
}
43804383

43814384
cache.SetBestBlock(pindexNew->GetBlockHash());
43824385
cache.Flush();
4383-
uiInterface.ShowProgress("", 100, false);
4386+
m_chainman.GetNotifications().progress(bilingual_str{}, 100, false);
43844387
return true;
43854388
}
43864389

src/validation.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,13 @@ enum class VerifyDBResult {
364364
};
365365

366366
/** RAII wrapper for VerifyDB: Verify consistency of the block and coin databases */
367-
class CVerifyDB {
367+
class CVerifyDB
368+
{
369+
private:
370+
kernel::Notifications& m_notifications;
371+
368372
public:
369-
CVerifyDB();
373+
explicit CVerifyDB(kernel::Notifications& notifications);
370374
~CVerifyDB();
371375
[[nodiscard]] VerifyDBResult VerifyDB(
372376
Chainstate& chainstate,

0 commit comments

Comments
 (0)