Skip to content

Commit e43ff4e

Browse files
committed
Merge #603: Add settings.json prune-prev, proxy-prev, onion-prev settings
9d3127b Add settings.json prune-prev, proxy-prev, onion-prev settings (Ryan Ofsky) Pull request description: With #602, if proxy and pruning settings are disabled in the GUI and the GUI is restarted, proxy and prune values are not stored anywhere. So if these settings are enabled in the future, default values will be shown, not previous values. This PR stores previous values so they will preserved across restarts. I'm not sure I like this behavior because showing default values seems simpler and safer to me. Previous values may just have been set temporarily and may have never actually worked, and it adds some code complexity to store them. This PR is one way of resolving #596. Other solutions are possible and could be implemented as alternatives. ACKs for top commit: hebasto: ACK 9d3127b, tested on Ubuntu 22.04. vasild: ACK 9d3127b jarolrod: tACK 9d3127b Tree-SHA512: 1778d1819443490c880cfd5c1711d9c5ac75ea3ee8440e2f0ced81d293247163a78ae8aba6027215110aec6533bd7dc6472aeead6796bfbd51bf2354e28f24a9
2 parents 68e484a + 9d3127b commit e43ff4e

File tree

2 files changed

+77
-66
lines changed

2 files changed

+77
-66
lines changed

src/qt/optionsmodel.cpp

+75-55
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static const char* SettingName(OptionsModel::OptionID option)
5959
}
6060

6161
/** Call node.updateRwSetting() with Bitcoin 22.x workaround. */
62-
static void UpdateRwSetting(interfaces::Node& node, OptionsModel::OptionID option, const util::SettingsValue& value)
62+
static void UpdateRwSetting(interfaces::Node& node, OptionsModel::OptionID option, const std::string& suffix, const util::SettingsValue& value)
6363
{
6464
if (value.isNum() &&
6565
(option == OptionsModel::DatabaseCache ||
@@ -73,9 +73,9 @@ static void UpdateRwSetting(interfaces::Node& node, OptionsModel::OptionID optio
7373
// in later releases by https://github.com/bitcoin/bitcoin/pull/24498.
7474
// If new numeric settings are added, they can be written as numbers
7575
// instead of strings, because bitcoin 22.x will not try to read these.
76-
node.updateRwSetting(SettingName(option), value.getValStr());
76+
node.updateRwSetting(SettingName(option) + suffix, value.getValStr());
7777
} else {
78-
node.updateRwSetting(SettingName(option), value);
78+
node.updateRwSetting(SettingName(option) + suffix, value);
7979
}
8080
}
8181

@@ -131,13 +131,6 @@ void OptionsModel::addOverriddenOption(const std::string &option)
131131
bool OptionsModel::Init(bilingual_str& error)
132132
{
133133
// Initialize display settings from stored settings.
134-
m_prune_size_gb = PruneSizeGB(node().getPersistentSetting("prune"));
135-
ProxySetting proxy = ParseProxyString(SettingToString(node().getPersistentSetting("proxy"), GetDefaultProxyAddress().toStdString()));
136-
m_proxy_ip = proxy.ip;
137-
m_proxy_port = proxy.port;
138-
ProxySetting onion = ParseProxyString(SettingToString(node().getPersistentSetting("onion"), GetDefaultProxyAddress().toStdString()));
139-
m_onion_ip = onion.ip;
140-
m_onion_port = onion.port;
141134
language = QString::fromStdString(SettingToString(node().getPersistentSetting("lang"), ""));
142135

143136
checkAndMigrate();
@@ -320,8 +313,6 @@ void OptionsModel::SetPruneTargetGB(int prune_target_gb)
320313
const util::SettingsValue cur_value = node().getPersistentSetting("prune");
321314
const util::SettingsValue new_value = PruneSetting(prune_target_gb > 0, prune_target_gb);
322315

323-
m_prune_size_gb = prune_target_gb;
324-
325316
// Force setting to take effect. It is still safe to change the value at
326317
// this point because this function is only called after the intro screen is
327318
// shown, before the node starts.
@@ -334,7 +325,12 @@ void OptionsModel::SetPruneTargetGB(int prune_target_gb)
334325
PruneSizeGB(cur_value) != PruneSizeGB(new_value)) {
335326
// Call UpdateRwSetting() instead of setOption() to avoid setting
336327
// RestartRequired flag
337-
UpdateRwSetting(node(), Prune, new_value);
328+
UpdateRwSetting(node(), Prune, "", new_value);
329+
}
330+
331+
// Keep previous pruning size, if pruning was disabled.
332+
if (PruneEnabled(cur_value)) {
333+
UpdateRwSetting(node(), Prune, "-prev", PruneEnabled(new_value) ? util::SettingsValue{} : cur_value);
338334
}
339335
}
340336

@@ -362,9 +358,9 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in
362358
return successful;
363359
}
364360

365-
QVariant OptionsModel::getOption(OptionID option) const
361+
QVariant OptionsModel::getOption(OptionID option, const std::string& suffix) const
366362
{
367-
auto setting = [&]{ return node().getPersistentSetting(SettingName(option)); };
363+
auto setting = [&]{ return node().getPersistentSetting(SettingName(option) + suffix); };
368364

369365
QSettings settings;
370366
switch (option) {
@@ -391,19 +387,30 @@ QVariant OptionsModel::getOption(OptionID option) const
391387

392388
// default proxy
393389
case ProxyUse:
390+
case ProxyUseTor:
394391
return ParseProxyString(SettingToString(setting(), "")).is_set;
395392
case ProxyIP:
396-
return m_proxy_ip;
393+
case ProxyIPTor: {
394+
ProxySetting proxy = ParseProxyString(SettingToString(setting(), ""));
395+
if (proxy.is_set) {
396+
return proxy.ip;
397+
} else if (suffix.empty()) {
398+
return getOption(option, "-prev");
399+
} else {
400+
return ParseProxyString(GetDefaultProxyAddress().toStdString()).ip;
401+
}
402+
}
397403
case ProxyPort:
398-
return m_proxy_port;
399-
400-
// separate Tor proxy
401-
case ProxyUseTor:
402-
return ParseProxyString(SettingToString(setting(), "")).is_set;
403-
case ProxyIPTor:
404-
return m_onion_ip;
405-
case ProxyPortTor:
406-
return m_onion_port;
404+
case ProxyPortTor: {
405+
ProxySetting proxy = ParseProxyString(SettingToString(setting(), ""));
406+
if (proxy.is_set) {
407+
return proxy.port;
408+
} else if (suffix.empty()) {
409+
return getOption(option, "-prev");
410+
} else {
411+
return ParseProxyString(GetDefaultProxyAddress().toStdString()).port;
412+
}
413+
}
407414

408415
#ifdef ENABLE_WALLET
409416
case SpendZeroConfChange:
@@ -428,7 +435,9 @@ QVariant OptionsModel::getOption(OptionID option) const
428435
case Prune:
429436
return PruneEnabled(setting());
430437
case PruneSize:
431-
return m_prune_size_gb;
438+
return PruneEnabled(setting()) ? PruneSizeGB(setting()) :
439+
suffix.empty() ? getOption(option, "-prev") :
440+
DEFAULT_PRUNE_TARGET_GB;
432441
case DatabaseCache:
433442
return qlonglong(SettingToInt(setting(), nDefaultDbCache));
434443
case ThreadsScriptVerif:
@@ -444,10 +453,10 @@ QVariant OptionsModel::getOption(OptionID option) const
444453
}
445454
}
446455

447-
bool OptionsModel::setOption(OptionID option, const QVariant& value)
456+
bool OptionsModel::setOption(OptionID option, const QVariant& value, const std::string& suffix)
448457
{
449-
auto changed = [&] { return value.isValid() && value != getOption(option); };
450-
auto update = [&](const util::SettingsValue& value) { return UpdateRwSetting(node(), option, value); };
458+
auto changed = [&] { return value.isValid() && value != getOption(option, suffix); };
459+
auto update = [&](const util::SettingsValue& value) { return UpdateRwSetting(node(), option, suffix, value); };
451460

452461
bool successful = true; /* set to false on parse error */
453462
QSettings settings;
@@ -485,52 +494,60 @@ bool OptionsModel::setOption(OptionID option, const QVariant& value)
485494
// default proxy
486495
case ProxyUse:
487496
if (changed()) {
488-
update(ProxyString(value.toBool(), m_proxy_ip, m_proxy_port));
489-
setRestartRequired(true);
497+
if (suffix.empty() && !value.toBool()) setOption(option, true, "-prev");
498+
update(ProxyString(value.toBool(), getOption(ProxyIP).toString(), getOption(ProxyPort).toString()));
499+
if (suffix.empty() && value.toBool()) UpdateRwSetting(node(), option, "-prev", {});
500+
if (suffix.empty()) setRestartRequired(true);
490501
}
491502
break;
492503
case ProxyIP:
493504
if (changed()) {
494-
m_proxy_ip = value.toString();
495-
if (getOption(ProxyUse).toBool()) {
496-
update(ProxyString(true, m_proxy_ip, m_proxy_port));
497-
setRestartRequired(true);
505+
if (suffix.empty() && !getOption(ProxyUse).toBool()) {
506+
setOption(option, value, "-prev");
507+
} else {
508+
update(ProxyString(true, value.toString(), getOption(ProxyPort).toString()));
498509
}
510+
if (suffix.empty() && getOption(ProxyUse).toBool()) setRestartRequired(true);
499511
}
500512
break;
501513
case ProxyPort:
502514
if (changed()) {
503-
m_proxy_port = value.toString();
504-
if (getOption(ProxyUse).toBool()) {
505-
update(ProxyString(true, m_proxy_ip, m_proxy_port));
506-
setRestartRequired(true);
515+
if (suffix.empty() && !getOption(ProxyUse).toBool()) {
516+
setOption(option, value, "-prev");
517+
} else {
518+
update(ProxyString(true, getOption(ProxyIP).toString(), value.toString()));
507519
}
520+
if (suffix.empty() && getOption(ProxyUse).toBool()) setRestartRequired(true);
508521
}
509522
break;
510523

511524
// separate Tor proxy
512525
case ProxyUseTor:
513526
if (changed()) {
514-
update(ProxyString(value.toBool(), m_onion_ip, m_onion_port));
515-
setRestartRequired(true);
527+
if (suffix.empty() && !value.toBool()) setOption(option, true, "-prev");
528+
update(ProxyString(value.toBool(), getOption(ProxyIPTor).toString(), getOption(ProxyPortTor).toString()));
529+
if (suffix.empty() && value.toBool()) UpdateRwSetting(node(), option, "-prev", {});
530+
if (suffix.empty()) setRestartRequired(true);
516531
}
517532
break;
518533
case ProxyIPTor:
519534
if (changed()) {
520-
m_onion_ip = value.toString();
521-
if (getOption(ProxyUseTor).toBool()) {
522-
update(ProxyString(true, m_onion_ip, m_onion_port));
523-
setRestartRequired(true);
535+
if (suffix.empty() && !getOption(ProxyUseTor).toBool()) {
536+
setOption(option, value, "-prev");
537+
} else {
538+
update(ProxyString(true, value.toString(), getOption(ProxyPortTor).toString()));
524539
}
540+
if (suffix.empty() && getOption(ProxyUseTor).toBool()) setRestartRequired(true);
525541
}
526542
break;
527543
case ProxyPortTor:
528544
if (changed()) {
529-
m_onion_port = value.toString();
530-
if (getOption(ProxyUseTor).toBool()) {
531-
update(ProxyString(true, m_onion_ip, m_onion_port));
532-
setRestartRequired(true);
545+
if (suffix.empty() && !getOption(ProxyUseTor).toBool()) {
546+
setOption(option, value, "-prev");
547+
} else {
548+
update(ProxyString(true, getOption(ProxyIPTor).toString(), value.toString()));
533549
}
550+
if (suffix.empty() && getOption(ProxyUseTor).toBool()) setRestartRequired(true);
534551
}
535552
break;
536553

@@ -584,17 +601,20 @@ bool OptionsModel::setOption(OptionID option, const QVariant& value)
584601
break;
585602
case Prune:
586603
if (changed()) {
587-
update(PruneSetting(value.toBool(), m_prune_size_gb));
588-
setRestartRequired(true);
604+
if (suffix.empty() && !value.toBool()) setOption(option, true, "-prev");
605+
update(PruneSetting(value.toBool(), getOption(PruneSize).toInt()));
606+
if (suffix.empty() && value.toBool()) UpdateRwSetting(node(), option, "-prev", {});
607+
if (suffix.empty()) setRestartRequired(true);
589608
}
590609
break;
591610
case PruneSize:
592611
if (changed()) {
593-
m_prune_size_gb = ParsePruneSizeGB(value);
594-
if (getOption(Prune).toBool()) {
595-
update(PruneSetting(true, m_prune_size_gb));
596-
setRestartRequired(true);
612+
if (suffix.empty() && !getOption(Prune).toBool()) {
613+
setOption(option, value, "-prev");
614+
} else {
615+
update(PruneSetting(true, ParsePruneSizeGB(value)));
597616
}
617+
if (suffix.empty() && getOption(Prune).toBool()) setRestartRequired(true);
598618
}
599619
break;
600620
case DatabaseCache:

src/qt/optionsmodel.h

+2-11
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ class OptionsModel : public QAbstractListModel
8282
int rowCount(const QModelIndex & parent = QModelIndex()) const override;
8383
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override;
8484
bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole) override;
85-
QVariant getOption(OptionID option) const;
86-
bool setOption(OptionID option, const QVariant& value);
85+
QVariant getOption(OptionID option, const std::string& suffix="") const;
86+
bool setOption(OptionID option, const QVariant& value, const std::string& suffix="");
8787
/** Updates current unit in memory, settings and emits displayUnitChanged(new_unit) signal */
8888
void setDisplayUnit(const QVariant& new_unit);
8989

@@ -123,15 +123,6 @@ class OptionsModel : public QAbstractListModel
123123
bool m_enable_psbt_controls;
124124
bool m_mask_values;
125125

126-
//! In-memory settings for display. These are stored persistently by the
127-
//! bitcoin node but it's also nice to store them in memory to prevent them
128-
//! getting cleared when enable/disable toggles are used in the GUI.
129-
int m_prune_size_gb;
130-
QString m_proxy_ip;
131-
QString m_proxy_port;
132-
QString m_onion_ip;
133-
QString m_onion_port;
134-
135126
/* settings that were overridden by command-line */
136127
QString strOverriddenByCommandLine;
137128

0 commit comments

Comments
 (0)