Skip to content

Commit

Permalink
wip: Add minimal support for loading signet UTXO snapshots
Browse files Browse the repository at this point in the history
        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
  • Loading branch information
D33r-Gee committed Oct 31, 2024
1 parent 1d028d7 commit 90dc0dd
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/interfaces/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ class Node
//! List rpc commands.
virtual std::vector<std::string> listRpcCommands() = 0;

//! Load UTXO Snapshot.
virtual bool snapshotLoad(const std::string& path_string) = 0;

//! Set RPC timer interface if unset.
virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface* iface) = 0;

Expand Down
7 changes: 7 additions & 0 deletions src/kernel/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,13 @@ class SigNetParams : public CChainParams {

vFixedSeeds.clear();

m_assumeutxo_data = MapAssumeutxo{
{
160000,
{AssumeutxoHash{uint256S("0x5225141cb62dee63ab3be95f9b03d60801f264010b1816d4bd00618b2736e7be")}, 1278002},
},
};

base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,111);
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,196);
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,239);
Expand Down
60 changes: 60 additions & 0 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <node/context.h>
#include <node/interface_ui.h>
#include <node/transaction.h>
#include <node/utxo_snapshot.h>
#include <policy/feerate.h>
#include <policy/fees.h>
#include <policy/policy.h>
Expand Down Expand Up @@ -395,6 +396,65 @@ class NodeImpl : public Node
{
m_context = context;
}
bool snapshotLoad(const std::string& path_string) override
{
const fs::path path = fs::u8path(path_string);
if (!fs::exists(path)) {
LogPrintf("[loadsnapshot] Snapshot file %s does not exist\n", path.u8string());
return false;
}

AutoFile afile{fsbridge::fopen(path, "rb")};
if (afile.IsNull()) {
LogPrintf("[loadsnapshot] Failed to open snapshot file %s\n", path.u8string());
return false;
}

SnapshotMetadata metadata;
try {
afile >> metadata;
} catch (const std::exception& e) {
LogPrintf("[loadsnapshot] Failed to read snapshot metadata: %s\n", e.what());
return false;
}

const uint256& base_blockhash = metadata.m_base_blockhash;
LogPrintf("[loadsnapshot] Waiting for blockheader %s in headers chain before snapshot activation\n",
base_blockhash.ToString());

if (!m_context->chainman) {
LogPrintf("[loadsnapshot] Chainman is null\n");
return false;
}

ChainstateManager& chainman = *m_context->chainman;
CBlockIndex* snapshot_start_block = nullptr;

// Wait for the block to appear in the block index
constexpr int max_wait_seconds = 600; // 10 minutes
for (int i = 0; i < max_wait_seconds; ++i) {
snapshot_start_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(base_blockhash));
if (snapshot_start_block) break;
std::this_thread::sleep_for(std::chrono::seconds(1));
}

if (!snapshot_start_block) {
LogPrintf("[loadsnapshot] Timed out waiting for snapshot start blockheader %s\n", base_blockhash.ToString());
return false;
}

// Activate the snapshot
if (!chainman.ActivateSnapshot(afile, metadata, false)) {
LogPrintf("[loadsnapshot] Unable to load UTXO snapshot %s\n", path.u8string());
return false;
}

CBlockIndex* new_tip = WITH_LOCK(::cs_main, return chainman.ActiveTip());
LogPrintf("[loadsnapshot] Loaded %d coins from snapshot %s at height %d\n",
metadata.m_coins_count, new_tip->GetBlockHash().ToString(), new_tip->nHeight);

return true;
}
ArgsManager& args() { return *Assert(Assert(m_context)->args); }
ChainstateManager& chainman() { return *Assert(m_context->chainman); }
NodeContext* m_context{nullptr};
Expand Down
12 changes: 11 additions & 1 deletion src/qml/components/ConnectionSettings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ ColumnLayout {
id: root
signal next
property bool snapshotImported: false
property bool onboarding: false

Component.onCompleted: {
snapshotImported = false
}

function setSnapshotImported(imported) {
snapshotImported = imported
}
spacing: 4
Setting {
id: gotoSnapshot
visible: !root.onboarding
Layout.fillWidth: true
header: qsTr("Load snapshot")
description: qsTr("Instant use with background sync")
Expand All @@ -39,7 +46,10 @@ ColumnLayout {
connectionSwipe.incrementCurrentIndex()
}
}
Separator { Layout.fillWidth: true }
Separator {
visible: !root.onboarding
Layout.fillWidth: true
}
Setting {
Layout.fillWidth: true
header: qsTr("Enable listening")
Expand Down
55 changes: 49 additions & 6 deletions src/qml/components/SnapshotSettings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Dialogs 1.3

import "../controls"

Expand All @@ -18,7 +19,7 @@ ColumnLayout {
width: Math.min(parent.width, 450)
anchors.horizontalCenter: parent.horizontalCenter


// TODO: Remove this once the verification progress is available
Timer {
id: snapshotSimulationTimer
interval: 50 // Update every 50ms
Expand All @@ -29,7 +30,7 @@ ColumnLayout {
snapshotVerificationProgress += 0.01
} else {
snapshotVerificationCycles++
if (snapshotVerificationCycles < 1) {
if (snapshotVerificationCycles < 3) {
snapshotVerificationProgress = 0
} else {
running = false
Expand All @@ -42,7 +43,7 @@ ColumnLayout {

StackLayout {
id: settingsStack
currentIndex: 0
currentIndex: snapshotVerified ? 2 : 0

ColumnLayout {
Layout.alignment: Qt.AlignHCenter
Expand Down Expand Up @@ -78,8 +79,24 @@ ColumnLayout {
Layout.alignment: Qt.AlignCenter
text: qsTr("Choose snapshot file")
onClicked: {
settingsStack.currentIndex = 1
snapshotSimulationTimer.start()
fileDialog.open()
}
}

FileDialog {
id: fileDialog
folder: shortcuts.home
selectMultiple: false
onAccepted: {
console.log("File chosen:", fileDialog.fileUrls)
var snapshotFileName = fileDialog.fileUrl.toString()
console.log("Snapshot file name:", snapshotFileName)
if (snapshotFileName.endsWith(".dat")) {
// optionsModel.setSnapshotDirectory(snapshotFileName)
// console.log("Snapshot directory set:", optionsModel.getSnapshotDirectory())
nodeModel.initializeSnapshot(true, snapshotFileName)
settingsStack.currentIndex = 1
}
}
}
}
Expand Down Expand Up @@ -109,10 +126,29 @@ ColumnLayout {
Layout.topMargin: 20
width: 200
height: 20
progress: snapshotVerificationProgress
// TODO: uncomment this once the verification progress is available
// progress: nodeModel.verificationProgress
progress: 0
Layout.alignment: Qt.AlignCenter
progressColor: Theme.color.blue
}

Connections {
target: nodeModel
// TODO: uncomment this once the verification progress is available
// function onVerificationProgressChanged() {
// progressIndicator.progress = nodeModel.verificationProgress
// }
function onSnapshotLoaded(success) {
if (success) {
progressIndicator.progress = 1
settingsStack.currentIndex = 2 // Move to the "Snapshot Loaded" page
} else {
// Handle snapshot loading failure
console.error("Snapshot loading failed")
}
}
}
}

ColumnLayout {
Expand All @@ -137,6 +173,7 @@ ColumnLayout {
descriptionColor: Theme.color.neutral6
descriptionSize: 17
descriptionLineHeight: 1.1
// TODO: Update this description once the snapshot is verified
description: qsTr("It contains transactions up to January 12, 2024. Newer transactions still need to be downloaded." +
" The data will be verified in the background.")
}
Expand All @@ -153,6 +190,9 @@ ColumnLayout {
}
}

// TODO: Update this with the actual snapshot details
// TODO: uncomment this once the snapshot details are available
/*
Setting {
id: viewDetails
Layout.alignment: Qt.AlignCenter
Expand Down Expand Up @@ -188,17 +228,20 @@ ColumnLayout {
font.pixelSize: 14
}
CoreText {
// TODO: Update this with the actual block height
text: qsTr("200,000")
Layout.alignment: Qt.AlignRight
font.pixelSize: 14
}
}
Separator { Layout.fillWidth: true }
CoreText {
// TODO: Update this with the actual snapshot file hash
text: qsTr("Hash: 0x1234567890abcdef...")
font.pixelSize: 14
}
}
*/
}
}
}
33 changes: 33 additions & 0 deletions src/qml/models/nodemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <interfaces/node.h>
#include <net.h>
#include <node/interface_ui.h>
#include <node/utxo_snapshot.h>
#include <validation.h>

#include <cassert>
Expand All @@ -16,6 +17,8 @@
#include <QMetaObject>
#include <QTimerEvent>
#include <QString>
#include <QUrl>
#include <QThread>

NodeModel::NodeModel(interfaces::Node& node)
: m_node{node}
Expand Down Expand Up @@ -166,3 +169,33 @@ void NodeModel::ConnectToNumConnectionsChangedSignal()
setNumOutboundPeers(new_num_peers.outbound_full_relay + new_num_peers.block_relay);
});
}

// Loads a snapshot from a given path using FileDialog
void NodeModel::initializeSnapshot(bool initLoadSnapshot, QString path_file) {
if (initLoadSnapshot) {
// TODO: this is to deal with FileDialog returning a QUrl
path_file = QUrl(path_file).toLocalFile();
// TODO: Remove this before release
// qDebug() << "path_file: " << path_file;
QThread* snapshot_thread = new QThread();

// Capture path_file by value
auto lambda = [this, path_file]() {
bool result = this->snapshotLoad(path_file);
Q_EMIT snapshotLoaded(result);
};

connect(snapshot_thread, &QThread::started, lambda);
connect(snapshot_thread, &QThread::finished, snapshot_thread, &QThread::deleteLater);

snapshot_thread->start();
}
}

void NodeModel::setSnapshotProgress(double new_snapshot_progress)
{
if (new_snapshot_progress != m_snapshot_progress) {
m_snapshot_progress = new_snapshot_progress;
Q_EMIT snapshotProgressChanged();
}
}
12 changes: 11 additions & 1 deletion src/qml/models/nodemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class NodeModel : public QObject
Q_PROPERTY(double verificationProgress READ verificationProgress NOTIFY verificationProgressChanged)
Q_PROPERTY(bool pause READ pause WRITE setPause NOTIFY pauseChanged)
Q_PROPERTY(bool faulted READ errorState WRITE setErrorState NOTIFY errorStateChanged)
Q_PROPERTY(double snapshotProgress READ snapshotProgress NOTIFY snapshotProgressChanged)

public:
explicit NodeModel(interfaces::Node& node);
Expand All @@ -52,13 +53,19 @@ class NodeModel : public QObject
void setPause(bool new_pause);
bool errorState() const { return m_faulted; }
void setErrorState(bool new_error);
double snapshotProgress() const { return m_snapshot_progress; }
void setSnapshotProgress(double new_snapshot_progress);
bool isSnapshotLoaded() const;

Q_INVOKABLE float getTotalBytesReceived() const { return (float)m_node.getTotalBytesRecv(); }
Q_INVOKABLE float getTotalBytesSent() const { return (float)m_node.getTotalBytesSent(); }

Q_INVOKABLE void startNodeInitializionThread();
Q_INVOKABLE void requestShutdown();

Q_INVOKABLE void initializeSnapshot(bool initLoadSnapshot, QString path_file);
Q_INVOKABLE bool snapshotLoad(QString path_file) const { return m_node.snapshotLoad(path_file.toStdString()); }

void startShutdownPolling();
void stopShutdownPolling();

Expand All @@ -77,6 +84,9 @@ public Q_SLOTS:

void setTimeRatioList(int new_time);
void setTimeRatioListInitial();
void initializationFinished();
void snapshotLoaded(bool result);
void snapshotProgressChanged();

protected:
void timerEvent(QTimerEvent* event) override;
Expand All @@ -90,7 +100,7 @@ public Q_SLOTS:
double m_verification_progress{0.0};
bool m_pause{false};
bool m_faulted{false};

double m_snapshot_progress{0.0};
int m_shutdown_polling_timer_id{0};

QVector<QPair<int, double>> m_block_process_time;
Expand Down
1 change: 1 addition & 0 deletions src/qml/pages/settings/SettingsConnection.qml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Item {
detailActive: true
detailItem: ConnectionSettings {
onNext: connectionSwipe.incrementCurrentIndex()
onboarding: root.onboarding
}

states: [
Expand Down

0 comments on commit 90dc0dd

Please sign in to comment.