Skip to content

Commit 5d2ffb0

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. Uses SnapshotSettings.qml to allow user interaction. Modifies src/interfaces/node.h, src/node/interfaces.cpp and chainparams.cpp (temporarily for signet snapshot testing) to expose 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 implemented yet Testing: 1. Start the node 2. Complete onboarding 3. Navigate to connection settings 4. Load snapshot from provided interface
1 parent c97d2a1 commit 5d2ffb0

File tree

11 files changed

+205
-79
lines changed

11 files changed

+205
-79
lines changed

src/interfaces/node.h

+6
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ 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+
205+
//! Get snapshot progress.
206+
virtual double getSnapshotProgress() = 0;
207+
202208
//! Set RPC timer interface if unset.
203209
virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface* iface) = 0;
204210

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/node/interfaces.cpp

+35-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>
@@ -395,9 +396,42 @@ class NodeImpl : public Node
395396
{
396397
m_context = context;
397398
}
399+
double getSnapshotProgress() override { return m_snapshot_progress.load(); }
400+
bool snapshotLoad(const std::string& path_string) override
401+
{
402+
const fs::path path = fs::u8path(path_string);
403+
if (!fs::exists(path)) { return false; }
404+
405+
AutoFile afile{fsbridge::fopen(path, "rb")};
406+
if (afile.IsNull()) { return false; }
407+
408+
SnapshotMetadata metadata;
409+
try {
410+
afile >> metadata;
411+
} catch (const std::exception& e) { return false; }
412+
413+
const uint256& base_blockhash = metadata.m_base_blockhash;
414+
415+
if (!m_context->chainman) { return false; }
416+
417+
ChainstateManager& chainman = *m_context->chainman;
418+
CBlockIndex* snapshot_start_block = nullptr;
419+
420+
// Wait for the block to appear in the block index
421+
//TODO: remove this once another method is implemented
422+
constexpr int max_wait_seconds = 600; // 10 minutes
423+
for (int i = 0; i < max_wait_seconds; ++i) {
424+
snapshot_start_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(base_blockhash));
425+
if (snapshot_start_block) break;
426+
std::this_thread::sleep_for(std::chrono::seconds(1));
427+
}
428+
429+
return chainman.ActivateSnapshot(afile, metadata, false);
430+
}
398431
ArgsManager& args() { return *Assert(Assert(m_context)->args); }
399432
ChainstateManager& chainman() { return *Assert(m_context->chainman); }
400433
NodeContext* m_context{nullptr};
434+
std::atomic<double> m_snapshot_progress{0.0};
401435
};
402436

403437
bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<RecursiveMutex>& lock, const CChain& active, const BlockManager& blockman)
@@ -510,7 +544,7 @@ class RpcHandlerImpl : public Handler
510544
class ChainImpl : public Chain
511545
{
512546
public:
513-
explicit ChainImpl(NodeContext& node) : m_node(node) {}
547+
explicit ChainImpl(node::NodeContext& node) : m_node(node) {}
514548
std::optional<int> getHeight() override
515549
{
516550
const int height{WITH_LOCK(::cs_main, return chainman().ActiveChain().Height())};

src/qml/components/ConnectionSettings.qml

+17-6
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@ import "../controls"
1010
ColumnLayout {
1111
id: root
1212
signal next
13-
property bool snapshotImported: false
14-
function setSnapshotImported(imported) {
15-
snapshotImported = imported
13+
property bool snapshotImportCompleted: false
14+
property bool onboarding: false
15+
16+
Component.onCompleted: {
17+
if (!onboarding) {
18+
snapshotImportCompleted = chainModel.isSnapshotActive
19+
} else {
20+
snapshotImportCompleted = false
21+
}
1622
}
23+
1724
spacing: 4
1825
Setting {
1926
id: gotoSnapshot
27+
visible: !root.onboarding
2028
Layout.fillWidth: true
2129
header: qsTr("Load snapshot")
2230
description: qsTr("Instant use with background sync")
@@ -25,12 +33,12 @@ ColumnLayout {
2533
height: 26
2634
CaretRightIcon {
2735
anchors.centerIn: parent
28-
visible: !snapshotImported
36+
visible: !snapshotImportCompleted
2937
color: gotoSnapshot.stateColor
3038
}
3139
GreenCheckIcon {
3240
anchors.centerIn: parent
33-
visible: snapshotImported
41+
visible: snapshotImportCompleted
3442
color: Theme.color.transparent
3543
size: 30
3644
}
@@ -40,7 +48,10 @@ ColumnLayout {
4048
connectionSwipe.incrementCurrentIndex()
4149
}
4250
}
43-
Separator { Layout.fillWidth: true }
51+
Separator {
52+
visible: !root.onboarding
53+
Layout.fillWidth: true
54+
}
4455
Setting {
4556
Layout.fillWidth: true
4657
header: qsTr("Enable listening")

src/qml/components/SnapshotSettings.qml

+43-45
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,27 @@
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+
property bool snapshotLoading: nodeModel.snapshotLoading
15+
property bool snapshotLoaded: nodeModel.isSnapshotLoaded
16+
property bool snapshotImportCompleted: chainModel.isSnapshotActive
17+
property bool onboarding: false
18+
property bool snapshotVerified: onboarding ? false : chainModel.isSnapshotActive
19+
property string snapshotFileName: ""
20+
property var snapshotInfo: ({})
21+
property string selectedFile: ""
22+
1823
width: Math.min(parent.width, 450)
1924
anchors.horizontalCenter: parent.horizontalCenter
2025

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-
4326
StackLayout {
4427
id: settingsStack
45-
currentIndex: 0
28+
currentIndex: onboarding ? 0 : snapshotLoaded ? 2 : snapshotVerified ? 2 : snapshotLoading ? 1 : 0
4629

4730
ColumnLayout {
4831
Layout.alignment: Qt.AlignHCenter
@@ -77,11 +60,22 @@ ColumnLayout {
7760
Layout.bottomMargin: 20
7861
Layout.alignment: Qt.AlignCenter
7962
text: qsTr("Choose snapshot file")
80-
onClicked: {
81-
settingsStack.currentIndex = 1
82-
snapshotSimulationTimer.start()
63+
onClicked: fileDialog.open()
64+
}
65+
66+
FileDialog {
67+
id: fileDialog
68+
folder: shortcuts.home
69+
selectMultiple: false
70+
selectExisting: true
71+
nameFilters: ["Snapshot files (*.dat)", "All files (*)"]
72+
onAccepted: {
73+
selectedFile = fileUrl.toString()
74+
snapshotFileName = selectedFile
75+
nodeModel.initializeSnapshot(true, snapshotFileName)
8376
}
8477
}
78+
// TODO: Handle file error signal
8579
}
8680

8781
ColumnLayout {
@@ -102,17 +96,10 @@ ColumnLayout {
10296
Layout.leftMargin: 20
10397
Layout.rightMargin: 20
10498
header: qsTr("Loading Snapshot")
99+
description: qsTr("This might take a while...")
105100
}
106101

107-
ProgressIndicator {
108-
id: progressIndicator
109-
Layout.topMargin: 20
110-
width: 200
111-
height: 20
112-
progress: snapshotVerificationProgress
113-
Layout.alignment: Qt.AlignCenter
114-
progressColor: Theme.color.blue
115-
}
102+
// TODO: add progress indicator once the snapshot progress is implemented
116103
}
117104

118105
ColumnLayout {
@@ -137,8 +124,11 @@ ColumnLayout {
137124
descriptionColor: Theme.color.neutral6
138125
descriptionSize: 17
139126
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.")
127+
description: snapshotInfo && snapshotInfo["date"] ?
128+
qsTr("It contains transactions up to %1. Newer transactions still need to be downloaded." +
129+
" The data will be verified in the background.").arg(snapshotInfo["date"]) :
130+
qsTr("It contains transactions up to DEBUG. Newer transactions still need to be downloaded." +
131+
" The data will be verified in the background.")
142132
}
143133

144134
ContinueButton {
@@ -147,7 +137,6 @@ ColumnLayout {
147137
Layout.alignment: Qt.AlignCenter
148138
text: qsTr("Done")
149139
onClicked: {
150-
snapshotImportCompleted()
151140
connectionSwipe.decrementCurrentIndex()
152141
connectionSwipe.decrementCurrentIndex()
153142
}
@@ -188,16 +177,25 @@ ColumnLayout {
188177
font.pixelSize: 14
189178
}
190179
CoreText {
191-
text: qsTr("200,000")
180+
text: snapshotInfo && snapshotInfo["height"] ?
181+
snapshotInfo["height"] : qsTr("DEBUG")
192182
Layout.alignment: Qt.AlignRight
193183
font.pixelSize: 14
194184
}
195185
}
196186
Separator { Layout.fillWidth: true }
197187
CoreText {
198-
text: qsTr("Hash: 0x1234567890abcdef...")
188+
text: snapshotInfo && snapshotInfo["hashSerialized"] ?
189+
qsTr("Hash: %1").arg(snapshotInfo["hashSerialized"].substring(0, 13) + "...") :
190+
qsTr("Hash: DEBUG")
199191
font.pixelSize: 14
200192
}
193+
194+
Component.onCompleted: {
195+
if (snapshotVerified || snapshotLoaded) {
196+
snapshotInfo = chainModel.getSnapshotInfo()
197+
}
198+
}
201199
}
202200
}
203201
}

src/qml/models/chainmodel.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
#include <QThread>
1010
#include <QTime>
1111
#include <interfaces/chain.h>
12+
#include <node/utxo_snapshot.h>
13+
#include <kernel/chainparams.h>
14+
#include <validation.h>
1215

1316
ChainModel::ChainModel(interfaces::Chain& chain)
1417
: m_chain{chain}
18+
// m_params{Params()}
1519
{
1620
QTimer* timer = new QTimer();
1721
connect(timer, &QTimer::timeout, this, &ChainModel::setCurrentTimeRatio);
@@ -101,3 +105,21 @@ void ChainModel::setCurrentTimeRatio()
101105

102106
Q_EMIT timeRatioListChanged();
103107
}
108+
109+
// Using hardcoded snapshot info to display in SnapshotSettings.qml
110+
QVariantMap ChainModel::getSnapshotInfo() {
111+
QVariantMap snapshot_info;
112+
113+
const MapAssumeutxo& valid_assumeutxos_map = Params().Assumeutxo();
114+
if (!valid_assumeutxos_map.empty()) {
115+
const int height = valid_assumeutxos_map.rbegin()->first;
116+
const auto& hash_serialized = valid_assumeutxos_map.rbegin()->second.hash_serialized;
117+
int64_t date = m_chain.getBlockTime(height);
118+
119+
snapshot_info["height"] = height;
120+
snapshot_info["hashSerialized"] = QString::fromStdString(hash_serialized.ToString());
121+
snapshot_info["date"] = QDateTime::fromSecsSinceEpoch(date).toString("MMMM d yyyy");
122+
}
123+
124+
return snapshot_info;
125+
}

src/qml/models/chainmodel.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class ChainModel : public QObject
2727
Q_PROPERTY(quint64 assumedBlockchainSize READ assumedBlockchainSize CONSTANT)
2828
Q_PROPERTY(quint64 assumedChainstateSize READ assumedChainstateSize CONSTANT)
2929
Q_PROPERTY(QVariantList timeRatioList READ timeRatioList NOTIFY timeRatioListChanged)
30+
Q_PROPERTY(bool isSnapshotActive READ isSnapshotActive NOTIFY isSnapshotActiveChanged)
3031

3132
public:
3233
explicit ChainModel(interfaces::Chain& chain);
@@ -36,18 +37,21 @@ class ChainModel : public QObject
3637
quint64 assumedBlockchainSize() const { return m_assumed_blockchain_size; };
3738
quint64 assumedChainstateSize() const { return m_assumed_chainstate_size; };
3839
QVariantList timeRatioList() const { return m_time_ratio_list; };
39-
40+
bool isSnapshotActive() const { return m_chain.hasAssumedValidChain(); };
4041
int timestampAtMeridian();
4142

4243
void setCurrentTimeRatio();
4344

45+
Q_INVOKABLE QVariantMap getSnapshotInfo();
46+
4447
public Q_SLOTS:
4548
void setTimeRatioList(int new_time);
4649
void setTimeRatioListInitial();
4750

4851
Q_SIGNALS:
4952
void timeRatioListChanged();
5053
void currentNetworkNameChanged();
54+
void isSnapshotActiveChanged();
5155

5256
private:
5357
QString m_current_network_name;

0 commit comments

Comments
 (0)