Skip to content

Commit 85592f5

Browse files
committed
wip: Add minimal support for loading signet UTXO snapshots
Adds minimal wiring to connect QML GUI to loading a signet UTXO snapshot via the connection settings. Modifies src/interfaces/node.h, src/node/interfaces.cpp and chainparams.cpp (temporarily for signet snapshot testing) to implement snapshot loading functionality through the node model. Current limitations: - Not integrated with onboarding process - Requires manual navigation to connection settings after initial startup - Snapshot verification progress is not working yet Testing: 1. Start the node 2. Complete onboarding 3. Navigate to connection settings 4. Load snapshot from provided interface
1 parent c97d2a1 commit 85592f5

File tree

10 files changed

+192
-23
lines changed

10 files changed

+192
-23
lines changed

Diff for: src/interfaces/node.h

+3
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ class Node
199199
//! List rpc commands.
200200
virtual std::vector<std::string> listRpcCommands() = 0;
201201

202+
//! Load UTXO Snapshot.
203+
virtual bool snapshotLoad(const std::string& path_string) = 0;
204+
202205
//! Set RPC timer interface if unset.
203206
virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface* iface) = 0;
204207

Diff for: 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);

Diff for: src/node/interfaces.cpp

+60
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>
@@ -395,6 +396,65 @@ class NodeImpl : public Node
395396
{
396397
m_context = context;
397398
}
399+
bool snapshotLoad(const std::string& path_string) override
400+
{
401+
const fs::path path = fs::u8path(path_string);
402+
if (!fs::exists(path)) {
403+
LogPrintf("[loadsnapshot] Snapshot file %s does not exist\n", path.u8string());
404+
return false;
405+
}
406+
407+
AutoFile afile{fsbridge::fopen(path, "rb")};
408+
if (afile.IsNull()) {
409+
LogPrintf("[loadsnapshot] Failed to open snapshot file %s\n", path.u8string());
410+
return false;
411+
}
412+
413+
SnapshotMetadata metadata;
414+
try {
415+
afile >> metadata;
416+
} catch (const std::exception& e) {
417+
LogPrintf("[loadsnapshot] Failed to read snapshot metadata: %s\n", e.what());
418+
return false;
419+
}
420+
421+
const uint256& base_blockhash = metadata.m_base_blockhash;
422+
LogPrintf("[loadsnapshot] Waiting for blockheader %s in headers chain before snapshot activation\n",
423+
base_blockhash.ToString());
424+
425+
if (!m_context->chainman) {
426+
LogPrintf("[loadsnapshot] Chainman is null\n");
427+
return false;
428+
}
429+
430+
ChainstateManager& chainman = *m_context->chainman;
431+
CBlockIndex* snapshot_start_block = nullptr;
432+
433+
// Wait for the block to appear in the block index
434+
constexpr int max_wait_seconds = 600; // 10 minutes
435+
for (int i = 0; i < max_wait_seconds; ++i) {
436+
snapshot_start_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(base_blockhash));
437+
if (snapshot_start_block) break;
438+
std::this_thread::sleep_for(std::chrono::seconds(1));
439+
}
440+
441+
if (!snapshot_start_block) {
442+
LogPrintf("[loadsnapshot] Timed out waiting for snapshot start blockheader %s\n", base_blockhash.ToString());
443+
return false;
444+
}
445+
446+
// Activate the snapshot
447+
if (!chainman.ActivateSnapshot(afile, metadata, false)) {
448+
LogPrintf("[loadsnapshot] Unable to load UTXO snapshot %s\n", path.u8string());
449+
return false;
450+
}
451+
452+
CBlockIndex* new_tip = WITH_LOCK(::cs_main, return chainman.ActiveTip());
453+
LogPrintf("[loadsnapshot] Loaded %d coins from snapshot %s at height %d\n",
454+
metadata.m_coins_count, new_tip->GetBlockHash().ToString(), new_tip->nHeight);
455+
456+
return true;
457+
}
398458
ArgsManager& args() { return *Assert(Assert(m_context)->args); }
399459
ChainstateManager& chainman() { return *Assert(m_context->chainman); }
400460
NodeContext* m_context{nullptr};

Diff for: src/qml/components/ConnectionSettings.qml

+16-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,24 @@ import "../controls"
1010
ColumnLayout {
1111
id: root
1212
signal next
13-
property bool snapshotImported: false
13+
property bool snapshotImported: onboarding ? false : chainModel.isSnapshotActive
14+
property bool onboarding: false
15+
16+
Component.onCompleted: {
17+
if (!onboarding) {
18+
snapshotImported = chainModel.isSnapshotActive
19+
} else {
20+
snapshotImported = false
21+
}
22+
}
23+
1424
function setSnapshotImported(imported) {
1525
snapshotImported = imported
1626
}
1727
spacing: 4
1828
Setting {
1929
id: gotoSnapshot
30+
visible: !root.onboarding
2031
Layout.fillWidth: true
2132
header: qsTr("Load snapshot")
2233
description: qsTr("Instant use with background sync")
@@ -40,7 +51,10 @@ ColumnLayout {
4051
connectionSwipe.incrementCurrentIndex()
4152
}
4253
}
43-
Separator { Layout.fillWidth: true }
54+
Separator {
55+
visible: !root.onboarding
56+
Layout.fillWidth: true
57+
}
4458
Setting {
4559
Layout.fillWidth: true
4660
header: qsTr("Enable listening")

Diff for: src/qml/components/SnapshotSettings.qml

+51-7
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@
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 {
1213
signal snapshotImportCompleted()
1314
property int snapshotVerificationCycles: 0
1415
property real snapshotVerificationProgress: 0
15-
property bool snapshotVerified: false
16+
property bool onboarding: false
17+
property bool snapshotVerified: onboarding ? false : chainModel.isSnapshotActive
1618

1719
id: columnLayout
1820
width: Math.min(parent.width, 450)
1921
anchors.horizontalCenter: parent.horizontalCenter
2022

21-
23+
// TODO: Remove this once the verification progress is available
2224
Timer {
2325
id: snapshotSimulationTimer
2426
interval: 50 // Update every 50ms
@@ -29,7 +31,7 @@ ColumnLayout {
2931
snapshotVerificationProgress += 0.01
3032
} else {
3133
snapshotVerificationCycles++
32-
if (snapshotVerificationCycles < 1) {
34+
if (snapshotVerificationCycles < 3) {
3335
snapshotVerificationProgress = 0
3436
} else {
3537
running = false
@@ -42,7 +44,7 @@ ColumnLayout {
4244

4345
StackLayout {
4446
id: settingsStack
45-
currentIndex: 0
47+
currentIndex: onboarding ? 0 : snapshotVerified ? 2 : 0
4648

4749
ColumnLayout {
4850
Layout.alignment: Qt.AlignHCenter
@@ -78,8 +80,22 @@ ColumnLayout {
7880
Layout.alignment: Qt.AlignCenter
7981
text: qsTr("Choose snapshot file")
8082
onClicked: {
81-
settingsStack.currentIndex = 1
82-
snapshotSimulationTimer.start()
83+
fileDialog.open()
84+
}
85+
}
86+
87+
FileDialog {
88+
id: fileDialog
89+
folder: shortcuts.home
90+
selectMultiple: false
91+
onAccepted: {
92+
console.log("File chosen:", fileDialog.fileUrls)
93+
var snapshotFileName = fileDialog.fileUrl.toString()
94+
console.log("Snapshot file name:", snapshotFileName)
95+
if (snapshotFileName.endsWith(".dat")) {
96+
nodeModel.initializeSnapshot(true, snapshotFileName)
97+
settingsStack.currentIndex = 1
98+
}
8399
}
84100
}
85101
}
@@ -109,10 +125,31 @@ ColumnLayout {
109125
Layout.topMargin: 20
110126
width: 200
111127
height: 20
112-
progress: snapshotVerificationProgress
128+
// TODO: uncomment this once the verification progress is available
129+
// progress: nodeModel.verificationProgress
130+
progress: 0
113131
Layout.alignment: Qt.AlignCenter
114132
progressColor: Theme.color.blue
115133
}
134+
135+
Connections {
136+
target: nodeModel
137+
// TODO: uncomment this once the verification progress is available
138+
// function onVerificationProgressChanged() {
139+
// progressIndicator.progress = nodeModel.verificationProgress
140+
// }
141+
function onSnapshotLoaded(success) {
142+
if (success) {
143+
chainModel.isSnapshotActiveChanged()
144+
snapshotVerified = chainModel.isSnapshotActive
145+
progressIndicator.progress = 1
146+
settingsStack.currentIndex = 2 // Move to the "Snapshot Loaded" page
147+
} else {
148+
// Handle snapshot loading failure
149+
console.error("Snapshot loading failed")
150+
}
151+
}
152+
}
116153
}
117154

118155
ColumnLayout {
@@ -137,6 +174,7 @@ ColumnLayout {
137174
descriptionColor: Theme.color.neutral6
138175
descriptionSize: 17
139176
descriptionLineHeight: 1.1
177+
// TODO: Update this description once the snapshot is verified
140178
description: qsTr("It contains transactions up to January 12, 2024. Newer transactions still need to be downloaded." +
141179
" The data will be verified in the background.")
142180
}
@@ -153,6 +191,9 @@ ColumnLayout {
153191
}
154192
}
155193

194+
// TODO: Update this with the actual snapshot details
195+
// TODO: uncomment this once the snapshot details are available
196+
/*
156197
Setting {
157198
id: viewDetails
158199
Layout.alignment: Qt.AlignCenter
@@ -188,17 +229,20 @@ ColumnLayout {
188229
font.pixelSize: 14
189230
}
190231
CoreText {
232+
// TODO: Update this with the actual block height
191233
text: qsTr("200,000")
192234
Layout.alignment: Qt.AlignRight
193235
font.pixelSize: 14
194236
}
195237
}
196238
Separator { Layout.fillWidth: true }
197239
CoreText {
240+
// TODO: Update this with the actual snapshot file hash
198241
text: qsTr("Hash: 0x1234567890abcdef...")
199242
font.pixelSize: 14
200243
}
201244
}
245+
*/
202246
}
203247
}
204248
}

Diff for: src/qml/models/chainmodel.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <QString>
1313
#include <QTimer>
1414
#include <QVariant>
15+
#include <QDebug>
1516

1617
namespace interfaces {
1718
class FoundBlock;
@@ -27,6 +28,7 @@ class ChainModel : public QObject
2728
Q_PROPERTY(quint64 assumedBlockchainSize READ assumedBlockchainSize CONSTANT)
2829
Q_PROPERTY(quint64 assumedChainstateSize READ assumedChainstateSize CONSTANT)
2930
Q_PROPERTY(QVariantList timeRatioList READ timeRatioList NOTIFY timeRatioListChanged)
31+
Q_PROPERTY(bool isSnapshotActive READ isSnapshotActive NOTIFY isSnapshotActiveChanged)
3032

3133
public:
3234
explicit ChainModel(interfaces::Chain& chain);
@@ -36,7 +38,7 @@ class ChainModel : public QObject
3638
quint64 assumedBlockchainSize() const { return m_assumed_blockchain_size; };
3739
quint64 assumedChainstateSize() const { return m_assumed_chainstate_size; };
3840
QVariantList timeRatioList() const { return m_time_ratio_list; };
39-
41+
bool isSnapshotActive() const { return m_chain.hasAssumedValidChain(); };
4042
int timestampAtMeridian();
4143

4244
void setCurrentTimeRatio();
@@ -48,6 +50,7 @@ public Q_SLOTS:
4850
Q_SIGNALS:
4951
void timeRatioListChanged();
5052
void currentNetworkNameChanged();
53+
void isSnapshotActiveChanged();
5154

5255
private:
5356
QString m_current_network_name;

Diff for: src/qml/models/nodemodel.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <interfaces/node.h>
88
#include <net.h>
99
#include <node/interface_ui.h>
10+
#include <node/utxo_snapshot.h>
1011
#include <validation.h>
1112

1213
#include <cassert>
@@ -16,6 +17,8 @@
1617
#include <QMetaObject>
1718
#include <QTimerEvent>
1819
#include <QString>
20+
#include <QUrl>
21+
#include <QThread>
1922

2023
NodeModel::NodeModel(interfaces::Node& node)
2124
: m_node{node}
@@ -166,3 +169,33 @@ void NodeModel::ConnectToNumConnectionsChangedSignal()
166169
setNumOutboundPeers(new_num_peers.outbound_full_relay + new_num_peers.block_relay);
167170
});
168171
}
172+
173+
// Loads a snapshot from a given path using FileDialog
174+
void NodeModel::initializeSnapshot(bool initLoadSnapshot, QString path_file) {
175+
if (initLoadSnapshot) {
176+
// TODO: this is to deal with FileDialog returning a QUrl
177+
path_file = QUrl(path_file).toLocalFile();
178+
// TODO: Remove this before release
179+
// qDebug() << "path_file: " << path_file;
180+
QThread* snapshot_thread = new QThread();
181+
182+
// Capture path_file by value
183+
auto lambda = [this, path_file]() {
184+
bool result = this->snapshotLoad(path_file);
185+
Q_EMIT snapshotLoaded(result);
186+
};
187+
188+
connect(snapshot_thread, &QThread::started, lambda);
189+
connect(snapshot_thread, &QThread::finished, snapshot_thread, &QThread::deleteLater);
190+
191+
snapshot_thread->start();
192+
}
193+
}
194+
195+
void NodeModel::setSnapshotProgress(double new_snapshot_progress)
196+
{
197+
if (new_snapshot_progress != m_snapshot_progress) {
198+
m_snapshot_progress = new_snapshot_progress;
199+
Q_EMIT snapshotProgressChanged();
200+
}
201+
}

0 commit comments

Comments
 (0)