Skip to content

Commit 184a9b8

Browse files
committed
cpp: Implement UTXO snapshot loading interface and progress tracking
- Extend node interface with virtual functions for UTXO snapshot loading - Add signal mechanism to monitor snapshot loading progress - Include predefined signet UTXO dataset in chainparams for validation
1 parent 0d9ea87 commit 184a9b8

9 files changed

+43
-1
lines changed

src/interfaces/node.h

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <net_types.h> // For banmap_t
1212
#include <netaddress.h> // For Network
1313
#include <netbase.h> // For ConnectionDirection
14+
#include <node/utxo_snapshot.h> // For SnapshotMetadata
1415
#include <support/allocators/secure.h> // For SecureString
1516
#include <util/translation.h>
1617

@@ -208,6 +209,9 @@ class Node
208209
//! List rpc commands.
209210
virtual std::vector<std::string> listRpcCommands() = 0;
210211

212+
//! Load UTXO Snapshot.
213+
virtual bool loadSnapshot(AutoFile& afile, const node::SnapshotMetadata& metadata, bool in_memory) = 0;
214+
211215
//! Set RPC timer interface if unset.
212216
virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface* iface) = 0;
213217

@@ -243,6 +247,10 @@ class Node
243247
using ShowProgressFn = std::function<void(const std::string& title, int progress, bool resume_possible)>;
244248
virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
245249

250+
//! Register handler for snapshot load progress.
251+
using SnapshotLoadProgressFn = std::function<void(double progress)>;
252+
virtual std::unique_ptr<Handler> handleSnapshotLoadProgress(SnapshotLoadProgressFn fn) = 0;
253+
246254
//! Register handler for wallet loader constructed messages.
247255
using InitWalletFn = std::function<void()>;
248256
virtual std::unique_ptr<Handler> handleInitWallet(InitWalletFn fn) = 0;

src/kernel/chainparams.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,13 @@ class SigNetParams : public CChainParams {
371371

372372
vFixedSeeds.clear();
373373

374+
m_assumeutxo_data = MapAssumeutxo{
375+
{
376+
160000,
377+
{AssumeutxoHash{uint256S("0x5225141cb62dee63ab3be95f9b03d60801f264010b1816d4bd00618b2736e7be")}, 1278002},
378+
},
379+
};
380+
374381
base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,111);
375382
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,196);
376383
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,239);

src/kernel/notifications_interface.h

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Notifications
4040
[[nodiscard]] virtual InterruptResult blockTip(SynchronizationState state, CBlockIndex& index) { return {}; }
4141
virtual void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) {}
4242
virtual void progress(const bilingual_str& title, int progress_percent, bool resume_possible) {}
43+
virtual void snapshotLoadProgress(double progress) {}
4344
virtual void warning(const bilingual_str& warning) {}
4445

4546
//! The flush error notification is sent to notify the user that an error

src/node/interface_ui.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct UISignals {
2121
boost::signals2::signal<CClientUIInterface::NotifyNetworkActiveChangedSig> NotifyNetworkActiveChanged;
2222
boost::signals2::signal<CClientUIInterface::NotifyAlertChangedSig> NotifyAlertChanged;
2323
boost::signals2::signal<CClientUIInterface::ShowProgressSig> ShowProgress;
24+
boost::signals2::signal<CClientUIInterface::SnapshotLoadProgressSig> SnapshotLoadProgress;
2425
boost::signals2::signal<CClientUIInterface::NotifyBlockTipSig> NotifyBlockTip;
2526
boost::signals2::signal<CClientUIInterface::NotifyHeaderTipSig> NotifyHeaderTip;
2627
boost::signals2::signal<CClientUIInterface::BannedListChangedSig> BannedListChanged;
@@ -44,6 +45,7 @@ ADD_SIGNALS_IMPL_WRAPPER(ShowProgress);
4445
ADD_SIGNALS_IMPL_WRAPPER(NotifyBlockTip);
4546
ADD_SIGNALS_IMPL_WRAPPER(NotifyHeaderTip);
4647
ADD_SIGNALS_IMPL_WRAPPER(BannedListChanged);
48+
ADD_SIGNALS_IMPL_WRAPPER(SnapshotLoadProgress);
4749

4850
bool CClientUIInterface::ThreadSafeMessageBox(const bilingual_str& message, const std::string& caption, unsigned int style) { return g_ui_signals.ThreadSafeMessageBox(message, caption, style).value_or(false);}
4951
bool CClientUIInterface::ThreadSafeQuestion(const bilingual_str& message, const std::string& non_interactive_message, const std::string& caption, unsigned int style) { return g_ui_signals.ThreadSafeQuestion(message, non_interactive_message, caption, style).value_or(false);}
@@ -53,6 +55,7 @@ void CClientUIInterface::NotifyNumConnectionsChanged(PeersNumByType newNumConnec
5355
void CClientUIInterface::NotifyNetworkActiveChanged(bool networkActive) { return g_ui_signals.NotifyNetworkActiveChanged(networkActive); }
5456
void CClientUIInterface::NotifyAlertChanged() { return g_ui_signals.NotifyAlertChanged(); }
5557
void CClientUIInterface::ShowProgress(const std::string& title, int nProgress, bool resume_possible) { return g_ui_signals.ShowProgress(title, nProgress, resume_possible); }
58+
void CClientUIInterface::SnapshotLoadProgress(double progress) { return g_ui_signals.SnapshotLoadProgress(progress); }
5659
void CClientUIInterface::NotifyBlockTip(SynchronizationState s, const CBlockIndex* i) { return g_ui_signals.NotifyBlockTip(s, i); }
5760
void CClientUIInterface::NotifyHeaderTip(SynchronizationState s, int64_t height, int64_t timestamp, bool presync) { return g_ui_signals.NotifyHeaderTip(s, height, timestamp, presync); }
5861
void CClientUIInterface::BannedListChanged() { return g_ui_signals.BannedListChanged(); }

src/node/interface_ui.h

+3
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ class CClientUIInterface
109109
*/
110110
ADD_SIGNALS_DECL_WRAPPER(ShowProgress, void, const std::string& title, int nProgress, bool resume_possible);
111111

112+
/** Snapshot load progress. */
113+
ADD_SIGNALS_DECL_WRAPPER(SnapshotLoadProgress, void, double progress);
114+
112115
/** New block has been accepted */
113116
ADD_SIGNALS_DECL_WRAPPER(NotifyBlockTip, void, SynchronizationState, const CBlockIndex*);
114117

src/node/interfaces.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <node/context.h>
3030
#include <node/interface_ui.h>
3131
#include <node/transaction.h>
32+
#include <node/utxo_snapshot.h>
3233
#include <policy/feerate.h>
3334
#include <policy/fees.h>
3435
#include <policy/policy.h>
@@ -378,6 +379,10 @@ class NodeImpl : public Node
378379
{
379380
return MakeSignalHandler(::uiInterface.ShowProgress_connect(fn));
380381
}
382+
std::unique_ptr<Handler> handleSnapshotLoadProgress(SnapshotLoadProgressFn fn) override
383+
{
384+
return MakeSignalHandler(::uiInterface.SnapshotLoadProgress_connect(fn));
385+
}
381386
std::unique_ptr<Handler> handleInitWallet(InitWalletFn fn) override
382387
{
383388
return MakeSignalHandler(::uiInterface.InitWallet_connect(fn));
@@ -417,6 +422,10 @@ class NodeImpl : public Node
417422
{
418423
m_context = context;
419424
}
425+
bool loadSnapshot(AutoFile& afile, const node::SnapshotMetadata& metadata, bool in_memory) override
426+
{
427+
return chainman().ActivateSnapshot(afile, metadata, in_memory);
428+
}
420429
ArgsManager& args() { return *Assert(Assert(m_context)->args); }
421430
ChainstateManager& chainman() { return *Assert(m_context->chainman); }
422431
NodeContext* m_context{nullptr};
@@ -532,7 +541,7 @@ class RpcHandlerImpl : public Handler
532541
class ChainImpl : public Chain
533542
{
534543
public:
535-
explicit ChainImpl(NodeContext& node) : m_node(node) {}
544+
explicit ChainImpl(node::NodeContext& node) : m_node(node) {}
536545
std::optional<int> getHeight() override
537546
{
538547
const int height{WITH_LOCK(::cs_main, return chainman().ActiveChain().Height())};

src/node/kernel_notifications.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ void KernelNotifications::progress(const bilingual_str& title, int progress_perc
7878
uiInterface.ShowProgress(title.translated, progress_percent, resume_possible);
7979
}
8080

81+
void KernelNotifications::snapshotLoadProgress(double progress)
82+
{
83+
uiInterface.SnapshotLoadProgress(progress);
84+
}
85+
8186
void KernelNotifications::warning(const bilingual_str& warning)
8287
{
8388
DoWarning(warning);

src/node/kernel_notifications.h

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class KernelNotifications : public kernel::Notifications
3131

3232
void progress(const bilingual_str& title, int progress_percent, bool resume_possible) override;
3333

34+
void snapshotLoadProgress(double progress) override;
35+
3436
void warning(const bilingual_str& warning) override;
3537

3638
void flushError(const std::string& debug_message) override;

src/validation.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -5261,6 +5261,10 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
52615261
--coins_left;
52625262
++coins_processed;
52635263

5264+
// Show Snapshot Loading progress
5265+
double progress = static_cast<double>(coins_processed) / static_cast<double>(coins_count);
5266+
GetNotifications().snapshotLoadProgress(progress);
5267+
52645268
if (coins_processed % 1000000 == 0) {
52655269
LogPrintf("[snapshot] %d coins loaded (%.2f%%, %.2f MB)\n",
52665270
coins_processed,

0 commit comments

Comments
 (0)