diff --git a/src/Makefile.qt.include b/src/Makefile.qt.include index 8217d04798..8cbf44785d 100644 --- a/src/Makefile.qt.include +++ b/src/Makefile.qt.include @@ -43,6 +43,7 @@ QT_MOC_CPP = \ qml/models/moc_options_model.cpp \ qml/models/moc_peerdetailsmodel.cpp \ qml/models/moc_peerlistsortproxy.cpp \ + qml/models/moc_snapshotqml.cpp \ qml/models/moc_walletlistmodel.cpp \ qml/models/moc_walletqmlmodel.cpp \ qml/moc_appmode.cpp \ @@ -126,6 +127,7 @@ BITCOIN_QT_H = \ qml/models/options_model.h \ qml/models/peerdetailsmodel.h \ qml/models/peerlistsortproxy.h \ + qml/models/snapshotqml.h \ qml/models/walletlistmodel.h \ qml/models/walletqmlmodel.h \ qml/appmode.h \ @@ -318,6 +320,7 @@ BITCOIN_QML_BASE_CPP = \ qml/models/options_model.cpp \ qml/models/peerdetailsmodel.cpp \ qml/models/peerlistsortproxy.cpp \ + qml/models/snapshotqml.cpp \ qml/models/walletlistmodel.cpp \ qml/models/walletqmlmodel.cpp \ qml/imageprovider.cpp \ @@ -340,11 +343,14 @@ QML_RES_ICONS = \ qml/res/icons/caret-left.png \ qml/res/icons/caret-right.png \ qml/res/icons/check.png \ + qml/res/icons/circle-file.png \ + qml/res/icons/circle-green-check.png \ qml/res/icons/cross.png \ qml/res/icons/error.png \ qml/res/icons/export.png \ qml/res/icons/gear.png \ qml/res/icons/gear-outline.png \ + qml/res/icons/green-check.png \ qml/res/icons/hidden.png \ qml/res/icons/info.png \ qml/res/icons/minus.png \ @@ -376,6 +382,7 @@ QML_RES_QML = \ qml/components/NetworkIndicator.qml \ qml/components/ProxySettings.qml \ qml/components/Separator.qml \ + qml/components/SnapshotSettings.qml \ qml/components/StorageLocations.qml \ qml/components/StorageOptions.qml \ qml/components/StorageSettings.qml \ @@ -431,6 +438,7 @@ QML_RES_QML = \ qml/pages/settings/SettingsDeveloper.qml \ qml/pages/settings/SettingsDisplay.qml \ qml/pages/settings/SettingsProxy.qml \ + qml/pages/settings/SettingsSnapshot.qml \ qml/pages/settings/SettingsStorage.qml \ qml/pages/settings/SettingsTheme.qml \ qml/pages/wallet/CreateBackup.qml \ diff --git a/src/interfaces/node.h b/src/interfaces/node.h index f6c79f0c1b..80902b9645 100644 --- a/src/interfaces/node.h +++ b/src/interfaces/node.h @@ -11,6 +11,7 @@ #include // For banmap_t #include // For Network #include // For ConnectionDirection +#include // For SnapshotMetadata #include // For SecureString #include @@ -199,6 +200,9 @@ class Node //! List rpc commands. virtual std::vector listRpcCommands() = 0; + //! Load UTXO Snapshot. + virtual bool loadSnapshot(AutoFile& afile, const node::SnapshotMetadata& metadata, bool in_memory) = 0; + //! Set RPC timer interface if unset. virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface* iface) = 0; @@ -234,6 +238,10 @@ class Node using ShowProgressFn = std::function; virtual std::unique_ptr handleShowProgress(ShowProgressFn fn) = 0; + //! Register handler for snapshot load progress. + using SnapshotLoadProgressFn = std::function; + virtual std::unique_ptr handleSnapshotLoadProgress(SnapshotLoadProgressFn fn) = 0; + //! Register handler for wallet loader constructed messages. using InitWalletFn = std::function; virtual std::unique_ptr handleInitWallet(InitWalletFn fn) = 0; diff --git a/src/kernel/chainparams.cpp b/src/kernel/chainparams.cpp index 733a3339b3..635dc40f9f 100644 --- a/src/kernel/chainparams.cpp +++ b/src/kernel/chainparams.cpp @@ -371,6 +371,13 @@ class SigNetParams : public CChainParams { vFixedSeeds.clear(); + m_assumeutxo_data = MapAssumeutxo{ + { + 160000, + {AssumeutxoHash{uint256S("0x5225141cb62dee63ab3be95f9b03d60801f264010b1816d4bd00618b2736e7be")}, 1278002}, + }, + }; + base58Prefixes[PUBKEY_ADDRESS] = std::vector(1,111); base58Prefixes[SCRIPT_ADDRESS] = std::vector(1,196); base58Prefixes[SECRET_KEY] = std::vector(1,239); diff --git a/src/kernel/notifications_interface.h b/src/kernel/notifications_interface.h index c5e77b0df9..b8002dedd2 100644 --- a/src/kernel/notifications_interface.h +++ b/src/kernel/notifications_interface.h @@ -40,6 +40,7 @@ class Notifications [[nodiscard]] virtual InterruptResult blockTip(SynchronizationState state, CBlockIndex& index) { return {}; } virtual void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) {} virtual void progress(const bilingual_str& title, int progress_percent, bool resume_possible) {} + virtual void snapshotLoadProgress(double progress) {} virtual void warning(const bilingual_str& warning) {} //! The flush error notification is sent to notify the user that an error diff --git a/src/node/interface_ui.cpp b/src/node/interface_ui.cpp index a1664f1bad..a57a559146 100644 --- a/src/node/interface_ui.cpp +++ b/src/node/interface_ui.cpp @@ -21,6 +21,7 @@ struct UISignals { boost::signals2::signal NotifyNetworkActiveChanged; boost::signals2::signal NotifyAlertChanged; boost::signals2::signal ShowProgress; + boost::signals2::signal SnapshotLoadProgress; boost::signals2::signal NotifyBlockTip; boost::signals2::signal NotifyHeaderTip; boost::signals2::signal BannedListChanged; @@ -44,6 +45,7 @@ ADD_SIGNALS_IMPL_WRAPPER(ShowProgress); ADD_SIGNALS_IMPL_WRAPPER(NotifyBlockTip); ADD_SIGNALS_IMPL_WRAPPER(NotifyHeaderTip); ADD_SIGNALS_IMPL_WRAPPER(BannedListChanged); +ADD_SIGNALS_IMPL_WRAPPER(SnapshotLoadProgress); 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);} 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 void CClientUIInterface::NotifyNetworkActiveChanged(bool networkActive) { return g_ui_signals.NotifyNetworkActiveChanged(networkActive); } void CClientUIInterface::NotifyAlertChanged() { return g_ui_signals.NotifyAlertChanged(); } void CClientUIInterface::ShowProgress(const std::string& title, int nProgress, bool resume_possible) { return g_ui_signals.ShowProgress(title, nProgress, resume_possible); } +void CClientUIInterface::SnapshotLoadProgress(double progress) { return g_ui_signals.SnapshotLoadProgress(progress); } void CClientUIInterface::NotifyBlockTip(SynchronizationState s, const CBlockIndex* i) { return g_ui_signals.NotifyBlockTip(s, i); } void CClientUIInterface::NotifyHeaderTip(SynchronizationState s, int64_t height, int64_t timestamp, bool presync) { return g_ui_signals.NotifyHeaderTip(s, height, timestamp, presync); } void CClientUIInterface::BannedListChanged() { return g_ui_signals.BannedListChanged(); } diff --git a/src/node/interface_ui.h b/src/node/interface_ui.h index cb5a9cbafd..14d3761898 100644 --- a/src/node/interface_ui.h +++ b/src/node/interface_ui.h @@ -109,6 +109,9 @@ class CClientUIInterface */ ADD_SIGNALS_DECL_WRAPPER(ShowProgress, void, const std::string& title, int nProgress, bool resume_possible); + /** Snapshot load progress. */ + ADD_SIGNALS_DECL_WRAPPER(SnapshotLoadProgress, void, double progress); + /** New block has been accepted */ ADD_SIGNALS_DECL_WRAPPER(NotifyBlockTip, void, SynchronizationState, const CBlockIndex*); diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index f1fe42206e..3824b54fa2 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -356,6 +357,10 @@ class NodeImpl : public Node { return MakeSignalHandler(::uiInterface.ShowProgress_connect(fn)); } + std::unique_ptr handleSnapshotLoadProgress(SnapshotLoadProgressFn fn) override + { + return MakeSignalHandler(::uiInterface.SnapshotLoadProgress_connect(fn)); + } std::unique_ptr handleInitWallet(InitWalletFn fn) override { return MakeSignalHandler(::uiInterface.InitWallet_connect(fn)); @@ -395,6 +400,10 @@ class NodeImpl : public Node { m_context = context; } + bool loadSnapshot(AutoFile& afile, const node::SnapshotMetadata& metadata, bool in_memory) override + { + return chainman().ActivateSnapshot(afile, metadata, in_memory); + } ArgsManager& args() { return *Assert(Assert(m_context)->args); } ChainstateManager& chainman() { return *Assert(m_context->chainman); } NodeContext* m_context{nullptr}; @@ -510,7 +519,7 @@ class RpcHandlerImpl : public Handler class ChainImpl : public Chain { public: - explicit ChainImpl(NodeContext& node) : m_node(node) {} + explicit ChainImpl(node::NodeContext& node) : m_node(node) {} std::optional getHeight() override { const int height{WITH_LOCK(::cs_main, return chainman().ActiveChain().Height())}; diff --git a/src/node/kernel_notifications.cpp b/src/node/kernel_notifications.cpp index 7224127c72..e41308dd50 100644 --- a/src/node/kernel_notifications.cpp +++ b/src/node/kernel_notifications.cpp @@ -78,6 +78,11 @@ void KernelNotifications::progress(const bilingual_str& title, int progress_perc uiInterface.ShowProgress(title.translated, progress_percent, resume_possible); } +void KernelNotifications::snapshotLoadProgress(double progress) +{ + uiInterface.SnapshotLoadProgress(progress); +} + void KernelNotifications::warning(const bilingual_str& warning) { DoWarning(warning); diff --git a/src/node/kernel_notifications.h b/src/node/kernel_notifications.h index b2dfc03398..4e9b9f77db 100644 --- a/src/node/kernel_notifications.h +++ b/src/node/kernel_notifications.h @@ -31,6 +31,8 @@ class KernelNotifications : public kernel::Notifications void progress(const bilingual_str& title, int progress_percent, bool resume_possible) override; + void snapshotLoadProgress(double progress) override; + void warning(const bilingual_str& warning) override; void flushError(const std::string& debug_message) override; diff --git a/src/qml/bitcoin_qml.qrc b/src/qml/bitcoin_qml.qrc index 61d2607fca..f11a9bad5d 100644 --- a/src/qml/bitcoin_qml.qrc +++ b/src/qml/bitcoin_qml.qrc @@ -15,6 +15,7 @@ components/ProxySettings.qml components/StorageLocations.qml components/Separator.qml + components/SnapshotSettings.qml components/StorageOptions.qml components/StorageSettings.qml components/ThemeSettings.qml @@ -25,6 +26,7 @@ controls/CoreTextField.qml controls/ExternalLink.qml controls/FocusBorder.qml + controls/GreenCheckIcon.qml controls/Header.qml controls/Icon.qml controls/InformationPage.qml @@ -68,6 +70,7 @@ pages/settings/SettingsDeveloper.qml pages/settings/SettingsDisplay.qml pages/settings/SettingsProxy.qml + pages/settings/SettingsSnapshot.qml pages/settings/SettingsStorage.qml pages/settings/SettingsTheme.qml pages/wallet/CreateBackup.qml @@ -93,11 +96,14 @@ res/icons/caret-left.png res/icons/caret-right.png res/icons/check.png + res/icons/circle-file.png + res/icons/circle-green-check.png res/icons/cross.png res/icons/error.png res/icons/export.png res/icons/gear.png res/icons/gear-outline.png + res/icons/green-check.png res/icons/hidden.png res/icons/info.png res/icons/minus.png diff --git a/src/qml/components/ConnectionSettings.qml b/src/qml/components/ConnectionSettings.qml index 35e78b7a6e..b3bc837789 100644 --- a/src/qml/components/ConnectionSettings.qml +++ b/src/qml/components/ConnectionSettings.qml @@ -10,7 +10,38 @@ import "../controls" ColumnLayout { id: root signal next + signal gotoSnapshot + property bool snapshotImportCompleted: chainModel.isSnapshotActive + property bool onboarding: false + spacing: 4 + Setting { + id: gotoSnapshot + visible: !root.onboarding + Layout.fillWidth: true + header: qsTr("Load snapshot") + description: qsTr("Instant use with background sync") + actionItem: Item { + width: 26 + height: 26 + CaretRightIcon { + anchors.centerIn: parent + visible: !snapshotImportCompleted + color: gotoSnapshot.stateColor + } + GreenCheckIcon { + anchors.centerIn: parent + visible: snapshotImportCompleted + color: Theme.color.transparent + size: 30 + } + } + onClicked: root.gotoSnapshot() + } + Separator { + visible: !root.onboarding + Layout.fillWidth: true + } Setting { Layout.fillWidth: true header: qsTr("Enable listening") diff --git a/src/qml/components/SnapshotSettings.qml b/src/qml/components/SnapshotSettings.qml new file mode 100644 index 0000000000..48b49e2110 --- /dev/null +++ b/src/qml/components/SnapshotSettings.qml @@ -0,0 +1,205 @@ +// Copyright (c) 2023-present The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +import QtQuick 2.15 +import QtQuick.Controls 2.15 +import QtQuick.Layouts 1.15 +import QtQuick.Dialogs 1.3 + +import "../controls" + +ColumnLayout { + id: columnLayout + signal back + property bool snapshotLoading: nodeModel.snapshotLoading + property bool snapshotLoaded: nodeModel.isSnapshotLoaded + property bool snapshotImportCompleted: chainModel.isSnapshotActive + property bool onboarding: false + property bool snapshotVerified: onboarding ? false : chainModel.isSnapshotActive + property string snapshotFileName: "" + property var snapshotInfo: (snapshotVerified || snapshotLoaded) ? chainModel.getSnapshotInfo() : ({}) + property string selectedFile: "" + + width: Math.min(parent.width, 450) + anchors.horizontalCenter: parent.horizontalCenter + + StackLayout { + id: settingsStack + currentIndex: onboarding ? 0 : snapshotLoaded ? 2 : snapshotVerified ? 2 : snapshotLoading ? 1 : 0 + + ColumnLayout { + Layout.alignment: Qt.AlignHCenter + Layout.preferredWidth: Math.min(parent.width, 450) + + Image { + Layout.alignment: Qt.AlignCenter + source: "image://images/circle-file" + + sourceSize.width: 200 + sourceSize.height: 200 + } + + Header { + Layout.fillWidth: true + Layout.topMargin: 20 + headerBold: true + header: qsTr("Load snapshot") + descriptionBold: false + descriptionColor: Theme.color.neutral6 + descriptionSize: 17 + descriptionLineHeight: 1.1 + description: qsTr("You can start using the application more quickly by loading a recent transaction snapshot." + + " It will be automatically verified in the background.") + } + + ContinueButton { + Layout.preferredWidth: Math.min(300, columnLayout.width - 2 * Layout.leftMargin) + Layout.topMargin: 40 + Layout.leftMargin: 20 + Layout.rightMargin: Layout.leftMargin + Layout.bottomMargin: 20 + Layout.alignment: Qt.AlignCenter + text: qsTr("Choose snapshot file") + onClicked: fileDialog.open() + } + + FileDialog { + id: fileDialog + folder: shortcuts.home + selectMultiple: false + selectExisting: true + nameFilters: ["Snapshot files (*.dat)", "All files (*)"] + onAccepted: { + selectedFile = fileUrl.toString() + snapshotFileName = selectedFile + nodeModel.snapshotLoadThread(snapshotFileName) + } + } + // TODO: Handle file error signal + } + + ColumnLayout { + Layout.alignment: Qt.AlignHCenter + Layout.preferredWidth: Math.min(parent.width, 450) + + Image { + Layout.alignment: Qt.AlignCenter + source: "image://images/circle-file" + + sourceSize.width: 200 + sourceSize.height: 200 + } + + Header { + Layout.fillWidth: true + Layout.topMargin: 20 + Layout.leftMargin: 20 + Layout.rightMargin: 20 + header: qsTr("Loading Snapshot") + description: qsTr("This might take a while...") + } + + ProgressIndicator { + id: progressIndicator + Layout.topMargin: 20 + width: 200 + height: 20 + progress: nodeModel.snapshotProgress + Layout.alignment: Qt.AlignCenter + progressColor: Theme.color.blue + } + } + + ColumnLayout { + id: loadedSnapshotColumn + Layout.alignment: Qt.AlignHCenter + Layout.preferredWidth: Math.min(parent.width, 450) + + Image { + Layout.alignment: Qt.AlignCenter + source: "image://images/circle-green-check" + + sourceSize.width: 60 + sourceSize.height: 60 + } + + Header { + Layout.fillWidth: true + Layout.topMargin: 20 + headerBold: true + header: qsTr("Snapshot Loaded") + descriptionBold: false + descriptionColor: Theme.color.neutral6 + descriptionSize: 17 + descriptionLineHeight: 1.1 + description: snapshotInfo && snapshotInfo["date"] ? + qsTr("It contains transactions up to %1. Newer transactions still need to be downloaded." + + " The data will be verified in the background.").arg(snapshotInfo["date"]) : + qsTr("It contains transactions up to DEBUG. Newer transactions still need to be downloaded." + + " The data will be verified in the background.") + } + + ContinueButton { + Layout.preferredWidth: Math.min(300, columnLayout.width - 2 * Layout.leftMargin) + Layout.topMargin: 40 + Layout.alignment: Qt.AlignCenter + text: qsTr("Done") + onClicked: { + chainModel.isSnapshotActiveChanged() + back() + } + } + + Setting { + id: viewDetails + Layout.alignment: Qt.AlignCenter + header: qsTr("View details") + actionItem: CaretRightIcon { + id: caretIcon + color: viewDetails.stateColor + rotation: viewDetails.expanded ? 90 : 0 + Behavior on rotation { NumberAnimation { duration: 200 } } + } + + property bool expanded: false + + onClicked: { + expanded = !expanded + } + } + + ColumnLayout { + id: detailsContent + visible: viewDetails.expanded + Layout.preferredWidth: Math.min(300, parent.width - 2 * Layout.leftMargin) + Layout.alignment: Qt.AlignCenter + Layout.leftMargin: 80 + Layout.rightMargin: 80 + Layout.topMargin: 10 + spacing: 10 + // TODO: make sure the block height number aligns right + RowLayout { + CoreText { + text: qsTr("Block Height:") + Layout.alignment: Qt.AlignLeft + font.pixelSize: 14 + } + CoreText { + text: snapshotInfo && snapshotInfo["height"] ? + snapshotInfo["height"] : qsTr("DEBUG") + Layout.alignment: Qt.AlignRight + font.pixelSize: 14 + } + } + Separator { Layout.fillWidth: true } + CoreText { + text: snapshotInfo && snapshotInfo["hashSerialized"] ? + qsTr("Hash: %1").arg(snapshotInfo["hashSerialized"].substring(0, 13) + "...") : + qsTr("Hash: DEBUG") + font.pixelSize: 14 + } + } + } + } +} diff --git a/src/qml/controls/GreenCheckIcon.qml b/src/qml/controls/GreenCheckIcon.qml new file mode 100644 index 0000000000..02977857b2 --- /dev/null +++ b/src/qml/controls/GreenCheckIcon.qml @@ -0,0 +1,11 @@ +// Copyright (c) 2023 - present The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +import QtQuick 2.15 +import QtQuick.Controls 2.15 + +Icon { + source: "image://images/green-check" + size: 30 +} diff --git a/src/qml/controls/Header.qml b/src/qml/controls/Header.qml index f3c4c0c3e3..ece49234d2 100644 --- a/src/qml/controls/Header.qml +++ b/src/qml/controls/Header.qml @@ -25,6 +25,7 @@ ColumnLayout { property int subtextSize: 15 property color subtextColor: Theme.color.neutral9 property bool wrap: true + property real descriptionLineHeight: 1 spacing: 0 Loader { @@ -60,6 +61,7 @@ ColumnLayout { text: root.description horizontalAlignment: root.center ? Text.AlignHCenter : Text.AlignLeft wrapMode: wrap ? Text.WordWrap : Text.NoWrap + lineHeight: root.descriptionLineHeight Behavior on color { ColorAnimation { duration: 150 } diff --git a/src/qml/controls/ProgressIndicator.qml b/src/qml/controls/ProgressIndicator.qml index 117a4baebb..9d6d62d329 100644 --- a/src/qml/controls/ProgressIndicator.qml +++ b/src/qml/controls/ProgressIndicator.qml @@ -7,6 +7,7 @@ import QtQuick.Controls 2.15 Control { property real progress: 0 + property color progressColor: Theme.color.orange Behavior on progress { NumberAnimation { easing.type: Easing.Bezier @@ -26,7 +27,7 @@ Control { width: contentItem.width height: contentItem.height radius: contentItem.radius - color: Theme.color.orange + color: progressColor } } } diff --git a/src/qml/controls/Theme.qml b/src/qml/controls/Theme.qml index f57e152cbd..3c7621c2b5 100644 --- a/src/qml/controls/Theme.qml +++ b/src/qml/controls/Theme.qml @@ -27,6 +27,7 @@ Control { required property color blue required property color amber required property color purple + required property color transparent required property color neutral0 required property color neutral1 required property color neutral2 @@ -59,6 +60,7 @@ Control { blue: "#3CA3DE" amber: "#C9B500" purple: "#C075DC" + transparent: "#00000000" neutral0: "#000000" neutral1: "#1A1A1A" neutral2: "#2D2D2D" @@ -91,6 +93,7 @@ Control { blue: "#2D9CDB" amber: "#C9B500" purple: "#BB6BD9" + transparent: "#00000000" neutral0: "#FFFFFF" neutral1: "#F8F8F8" neutral2: "#F4F4F4" diff --git a/src/qml/imageprovider.cpp b/src/qml/imageprovider.cpp index daf2feeae2..abd387f3b4 100644 --- a/src/qml/imageprovider.cpp +++ b/src/qml/imageprovider.cpp @@ -77,6 +77,16 @@ QPixmap ImageProvider::requestPixmap(const QString& id, QSize* size, const QSize return QIcon(":/icons/check").pixmap(requested_size); } + if (id == "circle-file") { + *size = requested_size; + return QIcon(":/icons/circle-file").pixmap(requested_size); + } + + if (id == "circle-green-check") { + *size = requested_size; + return QIcon(":/icons/circle-green-check").pixmap(requested_size); + } + if (id == "cross") { *size = requested_size; return QIcon(":/icons/cross").pixmap(requested_size); @@ -102,6 +112,11 @@ QPixmap ImageProvider::requestPixmap(const QString& id, QSize* size, const QSize return QIcon(":/icons/gear-outline").pixmap(requested_size); } + if (id == "green-check") { + *size = requested_size; + return QIcon(":/icons/green-check").pixmap(requested_size); + } + if (id == "info") { *size = requested_size; return QIcon(":/icons/info").pixmap(requested_size); diff --git a/src/qml/models/chainmodel.cpp b/src/qml/models/chainmodel.cpp index aeffe99599..290837c3bd 100644 --- a/src/qml/models/chainmodel.cpp +++ b/src/qml/models/chainmodel.cpp @@ -9,9 +9,13 @@ #include #include #include +#include +#include +#include ChainModel::ChainModel(interfaces::Chain& chain) : m_chain{chain} + // m_params{Params()} { QTimer* timer = new QTimer(); connect(timer, &QTimer::timeout, this, &ChainModel::setCurrentTimeRatio); @@ -101,3 +105,22 @@ void ChainModel::setCurrentTimeRatio() Q_EMIT timeRatioListChanged(); } + +// TODO: Change this once a better solution has been found. +// Using hardcoded snapshot info to display in SnapshotSettings.qml +QVariantMap ChainModel::getSnapshotInfo() { + QVariantMap snapshot_info; + + const MapAssumeutxo& valid_assumeutxos_map = Params().Assumeutxo(); + if (!valid_assumeutxos_map.empty()) { + const int height = valid_assumeutxos_map.rbegin()->first; + const auto& hash_serialized = valid_assumeutxos_map.rbegin()->second.hash_serialized; + int64_t date = m_chain.getBlockTime(height); + + snapshot_info["height"] = height; + snapshot_info["hashSerialized"] = QString::fromStdString(hash_serialized.ToString()); + snapshot_info["date"] = QDateTime::fromSecsSinceEpoch(date).toString("MMMM d yyyy"); + } + + return snapshot_info; +} diff --git a/src/qml/models/chainmodel.h b/src/qml/models/chainmodel.h index 9318510eda..6a5124be7f 100644 --- a/src/qml/models/chainmodel.h +++ b/src/qml/models/chainmodel.h @@ -27,6 +27,7 @@ class ChainModel : public QObject Q_PROPERTY(quint64 assumedBlockchainSize READ assumedBlockchainSize CONSTANT) Q_PROPERTY(quint64 assumedChainstateSize READ assumedChainstateSize CONSTANT) Q_PROPERTY(QVariantList timeRatioList READ timeRatioList NOTIFY timeRatioListChanged) + Q_PROPERTY(bool isSnapshotActive READ isSnapshotActive NOTIFY isSnapshotActiveChanged) public: explicit ChainModel(interfaces::Chain& chain); @@ -36,11 +37,13 @@ class ChainModel : public QObject quint64 assumedBlockchainSize() const { return m_assumed_blockchain_size; }; quint64 assumedChainstateSize() const { return m_assumed_chainstate_size; }; QVariantList timeRatioList() const { return m_time_ratio_list; }; - + bool isSnapshotActive() const { return m_chain.hasAssumedValidChain(); }; int timestampAtMeridian(); void setCurrentTimeRatio(); + Q_INVOKABLE QVariantMap getSnapshotInfo(); + public Q_SLOTS: void setTimeRatioList(int new_time); void setTimeRatioListInitial(); @@ -48,6 +51,7 @@ public Q_SLOTS: Q_SIGNALS: void timeRatioListChanged(); void currentNetworkNameChanged(); + void isSnapshotActiveChanged(); private: QString m_current_network_name; diff --git a/src/qml/models/nodemodel.cpp b/src/qml/models/nodemodel.cpp index 521e5fa1c5..c6497aefab 100644 --- a/src/qml/models/nodemodel.cpp +++ b/src/qml/models/nodemodel.cpp @@ -3,6 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include +#include #include #include @@ -14,14 +15,19 @@ #include #include +#include #include #include +#include +#include +#include NodeModel::NodeModel(interfaces::Node& node) : m_node{node} { ConnectToBlockTipSignal(); ConnectToNumConnectionsChangedSignal(); + ConnectToSnapshotLoadProgressSignal(); } void NodeModel::setBlockTipHeight(int new_height) @@ -166,3 +172,44 @@ void NodeModel::ConnectToNumConnectionsChangedSignal() setNumOutboundPeers(new_num_peers.outbound_full_relay + new_num_peers.block_relay); }); } + +void NodeModel::ConnectToSnapshotLoadProgressSignal() +{ + assert(!m_handler_snapshot_load_progress); + + m_handler_snapshot_load_progress = m_node.handleSnapshotLoadProgress( + [this](double progress) { + setSnapshotProgress(progress); + }); +} + +void NodeModel::snapshotLoadThread(QString path_file) { + m_snapshot_loading = true; + Q_EMIT snapshotLoadingChanged(); + + path_file = QUrl(path_file).toLocalFile(); + + QThread* snapshot_thread = QThread::create([this, path_file]() { + SnapshotQml loader(m_node, path_file); + bool result = loader.processPath(); + if (!result) { + m_snapshot_loading = false; + Q_EMIT snapshotLoadingChanged(); + } else { + m_snapshot_loaded = true; + Q_EMIT snapshotLoaded(result); + Q_EMIT snapshotLoadingChanged(); + } + }); + + connect(snapshot_thread, &QThread::finished, snapshot_thread, &QThread::deleteLater); + + snapshot_thread->start(); +} + +void NodeModel::setSnapshotProgress(double new_progress) { + if (new_progress != m_snapshot_progress) { + m_snapshot_progress = new_progress; + Q_EMIT snapshotProgressChanged(); + } +} diff --git a/src/qml/models/nodemodel.h b/src/qml/models/nodemodel.h index a17f9b0833..7ea84a5496 100644 --- a/src/qml/models/nodemodel.h +++ b/src/qml/models/nodemodel.h @@ -34,6 +34,9 @@ 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 WRITE setSnapshotProgress NOTIFY snapshotProgressChanged) + Q_PROPERTY(bool snapshotLoading READ snapshotLoading NOTIFY snapshotLoadingChanged) + Q_PROPERTY(bool isSnapshotLoaded READ isSnapshotLoaded NOTIFY snapshotLoaded) public: explicit NodeModel(interfaces::Node& node); @@ -52,6 +55,10 @@ class NodeModel : public QObject void setPause(bool new_pause); bool errorState() const { return m_faulted; } void setErrorState(bool new_error); + bool isSnapshotLoaded() const { return m_snapshot_loaded; } + double snapshotProgress() const { return m_snapshot_progress; } + void setSnapshotProgress(double new_progress); + bool snapshotLoading() const { return m_snapshot_loading; } Q_INVOKABLE float getTotalBytesReceived() const { return (float)m_node.getTotalBytesRecv(); } Q_INVOKABLE float getTotalBytesSent() const { return (float)m_node.getTotalBytesSent(); } @@ -59,6 +66,8 @@ class NodeModel : public QObject Q_INVOKABLE void startNodeInitializionThread(); Q_INVOKABLE void requestShutdown(); + Q_INVOKABLE void snapshotLoadThread(QString path_file); + void startShutdownPolling(); void stopShutdownPolling(); @@ -77,7 +86,11 @@ public Q_SLOTS: void setTimeRatioList(int new_time); void setTimeRatioListInitial(); - + void initializationFinished(); + void snapshotLoaded(bool result); + void snapshotProgressChanged(); + void snapshotLoadingChanged(); + void showProgress(const QString& title, int progress); protected: void timerEvent(QTimerEvent* event) override; @@ -90,17 +103,20 @@ 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}; - + int m_snapshot_timer_id{0}; + bool m_snapshot_loading{false}; + bool m_snapshot_loaded{false}; QVector> m_block_process_time; interfaces::Node& m_node; std::unique_ptr m_handler_notify_block_tip; std::unique_ptr m_handler_notify_num_peers_changed; - + std::unique_ptr m_handler_snapshot_load_progress; void ConnectToBlockTipSignal(); void ConnectToNumConnectionsChangedSignal(); + void ConnectToSnapshotLoadProgressSignal(); }; #endif // BITCOIN_QML_MODELS_NODEMODEL_H diff --git a/src/qml/models/snapshotqml.cpp b/src/qml/models/snapshotqml.cpp new file mode 100644 index 0000000000..6265aba417 --- /dev/null +++ b/src/qml/models/snapshotqml.cpp @@ -0,0 +1,40 @@ +// Copyright (c) 2024 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +#include +#include +#include +#include + +SnapshotQml::SnapshotQml(interfaces::Node& node, QString path) + : m_node(node), m_path(path) {} + +bool SnapshotQml::processPath() +{ + const fs::path path = fs::u8path(m_path.toStdString()); + if (!fs::exists(path)) { + return false; + } + + FILE* snapshot_file{fsbridge::fopen(path, "rb")}; + AutoFile afile{snapshot_file}; + if (afile.IsNull()) { + return false; + } + + node::SnapshotMetadata metadata; + try { + afile >> metadata; + } catch (const std::exception& e) { + return false; + } + + bool result = m_node.loadSnapshot(afile, metadata, false); + if (!result) { + return false; + } + return true; +} \ No newline at end of file diff --git a/src/qml/models/snapshotqml.h b/src/qml/models/snapshotqml.h new file mode 100644 index 0000000000..75e4758d7e --- /dev/null +++ b/src/qml/models/snapshotqml.h @@ -0,0 +1,26 @@ +// Copyright (c) 2024 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_QML_MODELS_SNAPSHOTQML_H +#define BITCOIN_QML_MODELS_SNAPSHOTQML_H + +#include +#include + +#include + +class SnapshotQml : public QObject +{ + Q_OBJECT +public: + SnapshotQml(interfaces::Node& node, QString path); + + bool processPath(); + +private: + interfaces::Node& m_node; + QString m_path; +}; + +#endif // BITCOIN_QML_MODELS_SNAPSHOTQML_H diff --git a/src/qml/pages/settings/SettingsConnection.qml b/src/qml/pages/settings/SettingsConnection.qml index d180fa2ff2..7d5c00496a 100644 --- a/src/qml/pages/settings/SettingsConnection.qml +++ b/src/qml/pages/settings/SettingsConnection.qml @@ -12,6 +12,7 @@ Page { id: root signal back property bool onboarding: false + property bool snapshotImportCompleted: chainModel.isSnapshotActive background: null PageStack { id: stack @@ -31,6 +32,9 @@ Page { detailActive: true detailItem: ConnectionSettings { onNext: stack.push(proxySettings) + onGotoSnapshot: stack.push(snapshotSettings) + snapshotImportCompleted: root.snapshotImportCompleted + onboarding: root.onboarding } states: [ @@ -87,5 +91,13 @@ Page { onBack: stack.pop() } } + Component { + id: snapshotSettings + SettingsSnapshot { + onboarding: root.onboarding + snapshotImportCompleted: root.snapshotImportCompleted + onBack: stack.pop() + } + } } } diff --git a/src/qml/pages/settings/SettingsSnapshot.qml b/src/qml/pages/settings/SettingsSnapshot.qml new file mode 100644 index 0000000000..2392dfb9c7 --- /dev/null +++ b/src/qml/pages/settings/SettingsSnapshot.qml @@ -0,0 +1,38 @@ +// Copyright (c) 2024-present The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +import QtQuick 2.15 +import QtQuick.Controls 2.15 +import QtQuick.Layouts 1.15 +import "../../controls" +import "../../components" + +Page { + signal back + property bool snapshotImportCompleted: chainModel.isSnapshotActive + property bool onboarding: false + + id: root + + background: null + implicitWidth: 450 + leftPadding: 20 + rightPadding: 20 + topPadding: 30 + + header: NavigationBar2 { + leftItem: NavButton { + iconSource: "image://images/caret-left" + text: qsTr("Back") + onClicked: root.back() + } + } + SnapshotSettings { + width: Math.min(parent.width, 450) + anchors.horizontalCenter: parent.horizontalCenter + onboarding: root.onboarding + snapshotImportCompleted: root.snapshotImportCompleted + onBack: root.back() + } +} diff --git a/src/qml/res/icons/circle-file.png b/src/qml/res/icons/circle-file.png new file mode 100644 index 0000000000..14a776e6d5 Binary files /dev/null and b/src/qml/res/icons/circle-file.png differ diff --git a/src/qml/res/icons/circle-green-check.png b/src/qml/res/icons/circle-green-check.png new file mode 100644 index 0000000000..25bb20e00f Binary files /dev/null and b/src/qml/res/icons/circle-green-check.png differ diff --git a/src/qml/res/icons/green-check.png b/src/qml/res/icons/green-check.png new file mode 100644 index 0000000000..acce5e1a21 Binary files /dev/null and b/src/qml/res/icons/green-check.png differ diff --git a/src/qml/res/src/circle-file.svg b/src/qml/res/src/circle-file.svg new file mode 100644 index 0000000000..d8af3949d8 --- /dev/null +++ b/src/qml/res/src/circle-file.svg @@ -0,0 +1,18 @@ + + circle-file-svg + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/qml/res/src/circle-green-check.svg b/src/qml/res/src/circle-green-check.svg new file mode 100644 index 0000000000..d56c175fd4 --- /dev/null +++ b/src/qml/res/src/circle-green-check.svg @@ -0,0 +1,18 @@ + + Circle arrow up-svg + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/qml/res/src/green-check.svg b/src/qml/res/src/green-check.svg new file mode 100644 index 0000000000..95f7c80ead --- /dev/null +++ b/src/qml/res/src/green-check.svg @@ -0,0 +1,9 @@ + + green-check-svg + + + + \ No newline at end of file diff --git a/src/validation.cpp b/src/validation.cpp index ed9889d9dd..c67cb91178 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -5261,6 +5261,10 @@ bool ChainstateManager::PopulateAndValidateSnapshot( --coins_left; ++coins_processed; + // Show Snapshot Loading progress + double progress = static_cast(coins_processed) / static_cast(coins_count); + GetNotifications().snapshotLoadProgress(progress); + if (coins_processed % 1000000 == 0) { LogPrintf("[snapshot] %d coins loaded (%.2f%%, %.2f MB)\n", coins_processed,