Skip to content
Permalink
Browse files

SimpleMode: automatic public nodes discovering and switching

  • Loading branch information...
xiphon committed Sep 2, 2019
1 parent a2d30b9 commit 34468b08b5c1bbd621f20bdb4191285a0d330c72
Showing with 64 additions and 29 deletions.
  1. +0 −4 js/Utils.js
  2. +46 −20 main.qml
  3. +15 −4 src/daemon/DaemonManager.cpp
  4. +3 −1 src/daemon/DaemonManager.h
@@ -94,10 +94,6 @@ function netTypeToString(){
return nettype == 1 ? qsTr("Testnet") : nettype == 2 ? qsTr("Stagenet") : qsTr("Mainnet");
}

function randomChoice(arr){
return arr[Math.floor(Math.random() * arr.length)];
}

function epoch(){
return Math.floor((new Date).getTime()/1000);
}
@@ -692,7 +692,8 @@ ApplicationWindow {
simpleModeConnectionTimer.stop();

appWindow.showProcessingSplash(qsTr("Waiting for daemon to start..."))
daemonManager.start(flags, persistentSettings.nettype, persistentSettings.blockchainDataDir, persistentSettings.bootstrapNodeAddress);
const noSync = appWindow.walletMode === 0;
daemonManager.start(flags, persistentSettings.nettype, persistentSettings.blockchainDataDir, persistentSettings.bootstrapNodeAddress, noSync);
persistentSettings.daemonFlags = flags
}

@@ -2008,30 +2009,45 @@ ApplicationWindow {
onTriggered: appWindow.themeTransition = true;
}

function checkNoSyncFlag() {
if (!appWindow.daemonRunning) {
return true;
}
if (appWindow.walletMode == 0 && !daemonManager.noSync()) {
return false;
}
if (appWindow.walletMode == 1 && daemonManager.noSync()) {
return false;
}
return true;
}

function checkSimpleModeConnection(){
// auto-connection mechanism for simple mode
if(persistentSettings.nettype != NetworkType.MAINNET) return;
if(appWindow.walletMode >= 2) return;

var disconnected = leftPanel.networkStatus.connected === Wallet.ConnectionStatus_Disconnected;
var disconnectedEpoch = appWindow.disconnectedEpoch;
if(disconnectedEpoch === 0){
const disconnectedTimeoutSec = 30;
const firstCheckDelaySec = 2;

const connected = leftPanel.networkStatus.connected === Wallet.ConnectionStatus_Connected;
const firstRun = appWindow.disconnectedEpoch == 0;
if (firstRun) {
appWindow.disconnectedEpoch = Utils.epoch() + firstCheckDelaySec - disconnectedTimeoutSec;
} else if (connected) {
appWindow.disconnectedEpoch = Utils.epoch();
}

// disconnected longer than 5 seconds?
if(disconnected && disconnectedEpoch > 0 && (Utils.epoch() - disconnectedEpoch) >= 5){
// for bootstrap mode, first wait until daemon is killed
if(appWindow.walletMode === 1 && appWindow.daemonRunning) {
appWindow.stopDaemon();
return;
}

appWindow.startDaemon(persistentSettings.daemonFlags);
const sinceLastConnect = Utils.epoch() - appWindow.disconnectedEpoch;
if (sinceLastConnect < disconnectedTimeoutSec && checkNoSyncFlag()) {
console.error('<-checkSimpleModeConnection1');
return;
}

// reset state
appWindow.disconnectedEpoch = 0;
if (appWindow.daemonRunning) {
appWindow.stopDaemon();
}
appWindow.startDaemon(persistentSettings.daemonFlags);
appWindow.disconnectedEpoch = Utils.epoch();
}

Timer {
@@ -2110,7 +2126,12 @@ ApplicationWindow {
closeAccepted();
};

confirmationDialog.open()
if (appWindow.walletMode == 0) {
stopDaemon();
closeAccepted();
} else {
confirmationDialog.open();
}

} else {
closeAccepted();
@@ -2222,11 +2243,16 @@ ApplicationWindow {
}

function changeWalletMode(mode){
appWindow.disconnectedEpoch = 0;
appWindow.walletMode = mode;
persistentSettings.walletMode = mode;
persistentSettings.useRemoteNode = mode === 0 ? true : false;
if (mode < 2 && (middlePanel.settingsView.settingsStateViewState === "Node" || middlePanel.settingsView.settingsStateViewState === "Log")) {
middlePanel.settingsView.settingsStateViewState = "Wallet"
if (mode < 2) {
persistentSettings.useRemoteNode = false;
persistentSettings.bootstrapNodeAddress = "auto";

if (middlePanel.settingsView.settingsStateViewState === "Node" || middlePanel.settingsView.settingsStateViewState === "Log") {
middlePanel.settingsView.settingsStateViewState = "Wallet"
}
}
console.log("walletMode changed: " + (mode === 0 ? "simple": mode === 1 ? "simple (bootstrap)" : "Advanced"));
}
@@ -61,7 +61,7 @@ DaemonManager *DaemonManager::instance(const QStringList *args)
return m_instance;
}

bool DaemonManager::start(const QString &flags, NetworkType::Type nettype, const QString &dataDir, const QString &bootstrapNodeAddress)
bool DaemonManager::start(const QString &flags, NetworkType::Type nettype, const QString &dataDir, const QString &bootstrapNodeAddress, bool noSync /* = false*/)
{
// prepare command line arguments and pass to monerod
QStringList arguments;
@@ -99,6 +99,10 @@ bool DaemonManager::start(const QString &flags, NetworkType::Type nettype, const
arguments << "--bootstrap-daemon-address" << bootstrapNodeAddress;
}

if (noSync) {
arguments << "--no-sync";
}

arguments << "--check-updates" << "disabled";

// --max-concurrency based on threads available. max: 6
@@ -131,11 +135,13 @@ bool DaemonManager::start(const QString &flags, NetworkType::Type nettype, const
}

// Start start watcher
m_scheduler.run([this, nettype] {
if (startWatcher(nettype))
m_scheduler.run([this, nettype, noSync] {
if (startWatcher(nettype)) {
emit daemonStarted();
else
m_noSync = noSync;
} else {
emit daemonStartFailure();
}
});

return true;
@@ -244,6 +250,11 @@ bool DaemonManager::running(NetworkType::Type nettype) const
return false;
}

bool DaemonManager::noSync() const noexcept
{
return m_noSync;
}

void DaemonManager::runningAsync(NetworkType::Type nettype, const QJSValue& callback) const
{
m_scheduler.run([this, nettype] {
@@ -44,9 +44,10 @@ class DaemonManager : public QObject

static DaemonManager * instance(const QStringList *args);

Q_INVOKABLE bool start(const QString &flags, NetworkType::Type nettype, const QString &dataDir = "", const QString &bootstrapNodeAddress = "");
Q_INVOKABLE bool start(const QString &flags, NetworkType::Type nettype, const QString &dataDir = "", const QString &bootstrapNodeAddress = "", bool noSync = false);
Q_INVOKABLE bool stop(NetworkType::Type nettype);

Q_INVOKABLE bool noSync() const noexcept;
// return true if daemon process is started
Q_INVOKABLE void runningAsync(NetworkType::Type nettype, const QJSValue& callback) const;
// Send daemon command from qml and prints output in console window.
@@ -82,6 +83,7 @@ public slots:
QString m_monerod;
bool m_has_daemon = true;
bool m_app_exit = false;
bool m_noSync = false;

mutable FutureScheduler m_scheduler;
};

0 comments on commit 34468b0

Please sign in to comment.
You can’t perform that action at this time.