Skip to content

Commit b720cde

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 ab5635c commit b720cde

20 files changed

+322
-61
lines changed

src/Makefile.qt.include

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ QT_MOC_CPP = \
4646
qml/models/moc_peerlistsortproxy.cpp \
4747
qml/models/moc_transaction.cpp \
4848
qml/models/moc_sendrecipient.cpp \
49+
qml/models/moc_snapshotqml.cpp \
4950
qml/models/moc_walletlistmodel.cpp \
5051
qml/models/moc_walletqmlmodel.cpp \
5152
qml/models/moc_walletqmlmodel.cpp \
@@ -136,6 +137,7 @@ BITCOIN_QT_H = \
136137
qml/models/peerlistsortproxy.h \
137138
qml/models/transaction.h \
138139
qml/models/sendrecipient.h \
140+
qml/models/snapshotqml.h \
139141
qml/models/walletlistmodel.h \
140142
qml/models/walletqmlmodel.h \
141143
qml/models/walletqmlmodeltransaction.h \
@@ -335,6 +337,7 @@ BITCOIN_QML_BASE_CPP = \
335337
qml/models/peerlistsortproxy.cpp \
336338
qml/models/transaction.cpp \
337339
qml/models/sendrecipient.cpp \
340+
qml/models/snapshotqml.cpp \
338341
qml/models/walletlistmodel.cpp \
339342
qml/models/walletqmlmodel.cpp \
340343
qml/models/walletqmlmodeltransaction.cpp \

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/qml/components/ConnectionSettings.qml

+10-7
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ ColumnLayout {
1111
id: root
1212
signal next
1313
signal gotoSnapshot
14-
property bool snapshotImported: false
15-
function setSnapshotImported(imported) {
16-
snapshotImported = imported
17-
}
14+
property bool snapshotImportCompleted: onboarding ? false : chainModel.isSnapshotActive
15+
property bool onboarding: false
16+
1817
spacing: 4
1918
Setting {
2019
id: gotoSnapshot
20+
visible: !root.onboarding
2121
Layout.fillWidth: true
2222
header: qsTr("Load snapshot")
2323
description: qsTr("Instant use with background sync")
@@ -26,19 +26,22 @@ ColumnLayout {
2626
height: 26
2727
CaretRightIcon {
2828
anchors.centerIn: parent
29-
visible: !snapshotImported
29+
visible: !snapshotImportCompleted
3030
color: gotoSnapshot.stateColor
3131
}
3232
GreenCheckIcon {
3333
anchors.centerIn: parent
34-
visible: snapshotImported
34+
visible: snapshotImportCompleted
3535
color: Theme.color.transparent
3636
size: 30
3737
}
3838
}
3939
onClicked: root.gotoSnapshot()
4040
}
41-
Separator { Layout.fillWidth: true }
41+
Separator {
42+
visible: !root.onboarding
43+
Layout.fillWidth: true
44+
}
4245
Setting {
4346
Layout.fillWidth: true
4447
header: qsTr("Enable listening")

src/qml/components/SnapshotSettings.qml

+66-41
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,29 @@
55
import QtQuick 2.15
66
import QtQuick.Controls 2.15
77
import QtQuick.Layouts 1.15
8+
import QtQuick.Dialogs 1.3
89

910
import "../controls"
1011

1112
ColumnLayout {
12-
signal snapshotImportCompleted()
13-
property int snapshotVerificationCycles: 0
14-
property real snapshotVerificationProgress: 0
15-
property bool snapshotVerified: false
16-
1713
id: columnLayout
14+
signal back
15+
property bool snapshotLoading: nodeModel.snapshotLoading
16+
property bool snapshotLoaded: nodeModel.isSnapshotLoaded
17+
property bool snapshotImportCompleted: onboarding ? false : chainModel.isSnapshotActive
18+
property bool onboarding: false
19+
property bool snapshotVerified: onboarding ? false : chainModel.isSnapshotActive
20+
property string snapshotFileName: ""
21+
property var snapshotInfo: (snapshotVerified || snapshotLoaded) ? chainModel.getSnapshotInfo() : ({})
22+
property string selectedFile: ""
23+
property bool headersSynced: nodeModel.headersSynced
24+
1825
width: Math.min(parent.width, 450)
1926
anchors.horizontalCenter: parent.horizontalCenter
2027

21-
22-
Timer {
23-
id: snapshotSimulationTimer
24-
interval: 50 // Update every 50ms
25-
running: false
26-
repeat: true
27-
onTriggered: {
28-
if (snapshotVerificationProgress < 1) {
29-
snapshotVerificationProgress += 0.01
30-
} else {
31-
snapshotVerificationCycles++
32-
if (snapshotVerificationCycles < 1) {
33-
snapshotVerificationProgress = 0
34-
} else {
35-
running = false
36-
snapshotVerified = true
37-
settingsStack.currentIndex = 2
38-
}
39-
}
40-
}
41-
}
42-
4328
StackLayout {
4429
id: settingsStack
45-
currentIndex: 0
30+
currentIndex: onboarding ? 0 : snapshotLoaded ? 2 : snapshotVerified ? 2 : snapshotLoading ? 1 : 0
4631

4732
ColumnLayout {
4833
Layout.alignment: Qt.AlignHCenter
@@ -69,6 +54,19 @@ ColumnLayout {
6954
" It will be automatically verified in the background.")
7055
}
7156

57+
CoreText {
58+
Layout.fillWidth: true
59+
Layout.topMargin: 20
60+
color: Theme.color.neutral6
61+
font.pixelSize: 17
62+
visible: !headersSynced
63+
text: !headersSynced
64+
? qsTr("Please wait for headers to sync before loading a snapshot.")
65+
: qsTr("")
66+
wrap: true
67+
wrapMode: Text.WordWrap
68+
}
69+
7270
ContinueButton {
7371
Layout.preferredWidth: Math.min(300, columnLayout.width - 2 * Layout.leftMargin)
7472
Layout.topMargin: 40
@@ -77,11 +75,23 @@ ColumnLayout {
7775
Layout.bottomMargin: 20
7876
Layout.alignment: Qt.AlignCenter
7977
text: qsTr("Choose snapshot file")
80-
onClicked: {
81-
settingsStack.currentIndex = 1
82-
snapshotSimulationTimer.start()
78+
enabled: headersSynced
79+
onClicked: fileDialog.open()
80+
}
81+
82+
FileDialog {
83+
id: fileDialog
84+
folder: shortcuts.home
85+
selectMultiple: false
86+
selectExisting: true
87+
nameFilters: ["Snapshot files (*.dat)", "All files (*)"]
88+
onAccepted: {
89+
selectedFile = fileUrl.toString()
90+
snapshotFileName = selectedFile
91+
nodeModel.snapshotLoadThread(snapshotFileName)
8392
}
8493
}
94+
// TODO: Handle file error signal
8595
}
8696

8797
ColumnLayout {
@@ -102,14 +112,15 @@ ColumnLayout {
102112
Layout.leftMargin: 20
103113
Layout.rightMargin: 20
104114
header: qsTr("Loading Snapshot")
115+
description: qsTr("This might take a while...")
105116
}
106117

107118
ProgressIndicator {
108119
id: progressIndicator
109120
Layout.topMargin: 20
110121
width: 200
111122
height: 20
112-
progress: snapshotVerificationProgress
123+
progress: nodeModel.snapshotProgress
113124
Layout.alignment: Qt.AlignCenter
114125
progressColor: Theme.color.blue
115126
}
@@ -137,8 +148,10 @@ ColumnLayout {
137148
descriptionColor: Theme.color.neutral6
138149
descriptionSize: 17
139150
descriptionLineHeight: 1.1
140-
description: qsTr("It contains transactions up to January 12, 2024. Newer transactions still need to be downloaded." +
141-
" The data will be verified in the background.")
151+
description: snapshotInfo && snapshotInfo["date"] ?
152+
qsTr("It contains unspent transactions up to %1. Next, transactions will be verified and newer transactions downloaded.").arg(snapshotInfo["date"]) :
153+
qsTr("It contains transactions up to DEBUG. Newer transactions still need to be downloaded." +
154+
" The data will be verified in the background.")
142155
}
143156

144157
ContinueButton {
@@ -147,9 +160,8 @@ ColumnLayout {
147160
Layout.alignment: Qt.AlignCenter
148161
text: qsTr("Done")
149162
onClicked: {
150-
snapshotImportCompleted()
151-
connectionSwipe.decrementCurrentIndex()
152-
connectionSwipe.decrementCurrentIndex()
163+
chainModel.isSnapshotActiveChanged()
164+
back()
153165
}
154166
}
155167

@@ -188,15 +200,28 @@ ColumnLayout {
188200
font.pixelSize: 14
189201
}
190202
CoreText {
191-
text: qsTr("200,000")
203+
text: snapshotInfo && snapshotInfo["height"] ?
204+
snapshotInfo["height"] : qsTr("DEBUG")
192205
Layout.alignment: Qt.AlignRight
193206
font.pixelSize: 14
194207
}
195208
}
196209
Separator { Layout.fillWidth: true }
197-
CoreText {
198-
text: qsTr("Hash: 0x1234567890abcdef...")
199-
font.pixelSize: 14
210+
ColumnLayout {
211+
Layout.fillWidth: true
212+
spacing: 5
213+
CoreText {
214+
text: qsTr("Hash:")
215+
font.pixelSize: 14
216+
}
217+
CoreText {
218+
text: snapshotInfo && snapshotInfo["hashSerializedFirstHalf"] ?
219+
snapshotInfo["hashSerializedFirstHalf"] + "\n" + snapshotInfo["hashSerializedSecondHalf"] :
220+
qsTr("DEBUG")
221+
Layout.fillWidth: true
222+
font.pixelSize: 14
223+
textFormat: Text.PlainText
224+
}
200225
}
201226
}
202227
}

0 commit comments

Comments
 (0)