Skip to content

Commit 3f3d71b

Browse files
committed
merge bitcoin-core/gui#603: Add settings.json prune-prev, proxy-prev, onion-prev settings
1 parent 171c6a9 commit 3f3d71b

File tree

2 files changed

+77
-66
lines changed

2 files changed

+77
-66
lines changed

src/qt/optionsmodel.cpp

Lines changed: 75 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static bool RequiresNumWorkaround(OptionsModel::OptionID option)
9797
}
9898

9999
/** Call node.updateRwSetting() with Bitcoin 22.x workaround. */
100-
static void UpdateRwSetting(interfaces::Node& node, OptionsModel::OptionID option, const util::SettingsValue& value)
100+
static void UpdateRwSetting(interfaces::Node& node, OptionsModel::OptionID option, const std::string& suffix, const util::SettingsValue& value)
101101
{
102102
if (value.isNum() && RequiresNumWorkaround(option)) {
103103
// Write certain old settings as strings, even though they are numbers,
@@ -107,9 +107,9 @@ static void UpdateRwSetting(interfaces::Node& node, OptionsModel::OptionID optio
107107
// in later releases by https://github.com/bitcoin/bitcoin/pull/24498.
108108
// If new numeric settings are added, they can be written as numbers
109109
// instead of strings, because bitcoin 22.x will not try to read these.
110-
node.updateRwSetting(SettingName(option), value.getValStr());
110+
node.updateRwSetting(SettingName(option) + suffix, value.getValStr());
111111
} else {
112-
node.updateRwSetting(SettingName(option), value);
112+
node.updateRwSetting(SettingName(option) + suffix, value);
113113
}
114114
}
115115

@@ -188,13 +188,6 @@ void OptionsModel::addOverriddenOption(const std::string &option)
188188
bool OptionsModel::Init(bilingual_str& error)
189189
{
190190
// Initialize display settings from stored settings.
191-
m_prune_size_gb = PruneSizeGB(node().getPersistentSetting("prune"));
192-
ProxySetting proxy = ParseProxyString(SettingToString(node().getPersistentSetting("proxy"), GetDefaultProxyAddress().toStdString()));
193-
m_proxy_ip = proxy.ip;
194-
m_proxy_port = proxy.port;
195-
ProxySetting onion = ParseProxyString(SettingToString(node().getPersistentSetting("onion"), GetDefaultProxyAddress().toStdString()));
196-
m_onion_ip = onion.ip;
197-
m_onion_port = onion.port;
198191
language = QString::fromStdString(SettingToString(node().getPersistentSetting("lang"), ""));
199192

200193
checkAndMigrate();
@@ -437,8 +430,6 @@ void OptionsModel::SetPruneTargetGB(int prune_target_gb)
437430
const util::SettingsValue cur_value = node().getPersistentSetting("prune");
438431
const util::SettingsValue new_value = PruneSetting(prune_target_gb > 0, prune_target_gb);
439432

440-
m_prune_size_gb = prune_target_gb;
441-
442433
// Force setting to take effect. It is still safe to change the value at
443434
// this point because this function is only called after the intro screen is
444435
// shown, before the node starts.
@@ -457,7 +448,12 @@ void OptionsModel::SetPruneTargetGB(int prune_target_gb)
457448
PruneSizeGB(cur_value) != PruneSizeGB(new_value)) {
458449
// Call UpdateRwSetting() instead of setOption() to avoid setting
459450
// RestartRequired flag
460-
UpdateRwSetting(node(), Prune, new_value);
451+
UpdateRwSetting(node(), Prune, "", new_value);
452+
}
453+
454+
// Keep previous pruning size, if pruning was disabled.
455+
if (PruneEnabled(cur_value)) {
456+
UpdateRwSetting(node(), Prune, "-prev", PruneEnabled(new_value) ? util::SettingsValue{} : cur_value);
461457
}
462458
}
463459

@@ -485,9 +481,9 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in
485481
return successful;
486482
}
487483

488-
QVariant OptionsModel::getOption(OptionID option) const
484+
QVariant OptionsModel::getOption(OptionID option, const std::string& suffix) const
489485
{
490-
auto setting = [&]{ return node().getPersistentSetting(SettingName(option)); };
486+
auto setting = [&]{ return node().getPersistentSetting(SettingName(option) + suffix); };
491487

492488
QSettings settings;
493489
switch (option) {
@@ -514,19 +510,30 @@ QVariant OptionsModel::getOption(OptionID option) const
514510

515511
// default proxy
516512
case ProxyUse:
513+
case ProxyUseTor:
517514
return ParseProxyString(SettingToString(setting(), "")).is_set;
518515
case ProxyIP:
519-
return m_proxy_ip;
516+
case ProxyIPTor: {
517+
ProxySetting proxy = ParseProxyString(SettingToString(setting(), ""));
518+
if (proxy.is_set) {
519+
return proxy.ip;
520+
} else if (suffix.empty()) {
521+
return getOption(option, "-prev");
522+
} else {
523+
return ParseProxyString(GetDefaultProxyAddress().toStdString()).ip;
524+
}
525+
}
520526
case ProxyPort:
521-
return m_proxy_port;
522-
523-
// separate Tor proxy
524-
case ProxyUseTor:
525-
return ParseProxyString(SettingToString(setting(), "")).is_set;
526-
case ProxyIPTor:
527-
return m_onion_ip;
528-
case ProxyPortTor:
529-
return m_onion_port;
527+
case ProxyPortTor: {
528+
ProxySetting proxy = ParseProxyString(SettingToString(setting(), ""));
529+
if (proxy.is_set) {
530+
return proxy.port;
531+
} else if (suffix.empty()) {
532+
return getOption(option, "-prev");
533+
} else {
534+
return ParseProxyString(GetDefaultProxyAddress().toStdString()).port;
535+
}
536+
}
530537

531538
#ifdef ENABLE_WALLET
532539
case SpendZeroConfChange:
@@ -589,7 +596,9 @@ QVariant OptionsModel::getOption(OptionID option) const
589596
case Prune:
590597
return PruneEnabled(setting());
591598
case PruneSize:
592-
return m_prune_size_gb;
599+
return PruneEnabled(setting()) ? PruneSizeGB(setting()) :
600+
suffix.empty() ? getOption(option, "-prev") :
601+
DEFAULT_PRUNE_TARGET_GB;
593602
case DatabaseCache:
594603
return qlonglong(SettingToInt(setting(), nDefaultDbCache));
595604
case ThreadsScriptVerif:
@@ -605,10 +614,10 @@ QVariant OptionsModel::getOption(OptionID option) const
605614
}
606615
}
607616

608-
bool OptionsModel::setOption(OptionID option, const QVariant& value)
617+
bool OptionsModel::setOption(OptionID option, const QVariant& value, const std::string& suffix)
609618
{
610-
auto changed = [&] { return value.isValid() && value != getOption(option); };
611-
auto update = [&](const util::SettingsValue& value) { return UpdateRwSetting(node(), option, value); };
619+
auto changed = [&] { return value.isValid() && value != getOption(option, suffix); };
620+
auto update = [&](const util::SettingsValue& value) { return UpdateRwSetting(node(), option, suffix, value); };
612621

613622
bool successful = true; /* set to false on parse error */
614623
QSettings settings;
@@ -646,52 +655,60 @@ bool OptionsModel::setOption(OptionID option, const QVariant& value)
646655
// default proxy
647656
case ProxyUse:
648657
if (changed()) {
649-
update(ProxyString(value.toBool(), m_proxy_ip, m_proxy_port));
650-
setRestartRequired(true);
658+
if (suffix.empty() && !value.toBool()) setOption(option, true, "-prev");
659+
update(ProxyString(value.toBool(), getOption(ProxyIP).toString(), getOption(ProxyPort).toString()));
660+
if (suffix.empty() && value.toBool()) UpdateRwSetting(node(), option, "-prev", {});
661+
if (suffix.empty()) setRestartRequired(true);
651662
}
652663
break;
653664
case ProxyIP:
654665
if (changed()) {
655-
m_proxy_ip = value.toString();
656-
if (getOption(ProxyUse).toBool()) {
657-
update(ProxyString(true, m_proxy_ip, m_proxy_port));
658-
setRestartRequired(true);
666+
if (suffix.empty() && !getOption(ProxyUse).toBool()) {
667+
setOption(option, value, "-prev");
668+
} else {
669+
update(ProxyString(true, value.toString(), getOption(ProxyPort).toString()));
659670
}
671+
if (suffix.empty() && getOption(ProxyUse).toBool()) setRestartRequired(true);
660672
}
661673
break;
662674
case ProxyPort:
663675
if (changed()) {
664-
m_proxy_port = value.toString();
665-
if (getOption(ProxyUse).toBool()) {
666-
update(ProxyString(true, m_proxy_ip, m_proxy_port));
667-
setRestartRequired(true);
676+
if (suffix.empty() && !getOption(ProxyUse).toBool()) {
677+
setOption(option, value, "-prev");
678+
} else {
679+
update(ProxyString(true, getOption(ProxyIP).toString(), value.toString()));
668680
}
681+
if (suffix.empty() && getOption(ProxyUse).toBool()) setRestartRequired(true);
669682
}
670683
break;
671684

672685
// separate Tor proxy
673686
case ProxyUseTor:
674687
if (changed()) {
675-
update(ProxyString(value.toBool(), m_onion_ip, m_onion_port));
676-
setRestartRequired(true);
688+
if (suffix.empty() && !value.toBool()) setOption(option, true, "-prev");
689+
update(ProxyString(value.toBool(), getOption(ProxyIPTor).toString(), getOption(ProxyPortTor).toString()));
690+
if (suffix.empty() && value.toBool()) UpdateRwSetting(node(), option, "-prev", {});
691+
if (suffix.empty()) setRestartRequired(true);
677692
}
678693
break;
679694
case ProxyIPTor:
680695
if (changed()) {
681-
m_onion_ip = value.toString();
682-
if (getOption(ProxyUseTor).toBool()) {
683-
update(ProxyString(true, m_onion_ip, m_onion_port));
684-
setRestartRequired(true);
696+
if (suffix.empty() && !getOption(ProxyUseTor).toBool()) {
697+
setOption(option, value, "-prev");
698+
} else {
699+
update(ProxyString(true, value.toString(), getOption(ProxyPortTor).toString()));
685700
}
701+
if (suffix.empty() && getOption(ProxyUseTor).toBool()) setRestartRequired(true);
686702
}
687703
break;
688704
case ProxyPortTor:
689705
if (changed()) {
690-
m_onion_port = value.toString();
691-
if (getOption(ProxyUseTor).toBool()) {
692-
update(ProxyString(true, m_onion_ip, m_onion_port));
693-
setRestartRequired(true);
706+
if (suffix.empty() && !getOption(ProxyUseTor).toBool()) {
707+
setOption(option, value, "-prev");
708+
} else {
709+
update(ProxyString(true, getOption(ProxyIPTor).toString(), value.toString()));
694710
}
711+
if (suffix.empty() && getOption(ProxyUseTor).toBool()) setRestartRequired(true);
695712
}
696713
break;
697714

@@ -846,17 +863,20 @@ bool OptionsModel::setOption(OptionID option, const QVariant& value)
846863
#endif // ENABLE_WALLET
847864
case Prune:
848865
if (changed()) {
849-
update(PruneSetting(value.toBool(), m_prune_size_gb));
850-
setRestartRequired(true);
866+
if (suffix.empty() && !value.toBool()) setOption(option, true, "-prev");
867+
update(PruneSetting(value.toBool(), getOption(PruneSize).toInt()));
868+
if (suffix.empty() && value.toBool()) UpdateRwSetting(node(), option, "-prev", {});
869+
if (suffix.empty()) setRestartRequired(true);
851870
}
852871
break;
853872
case PruneSize:
854873
if (changed()) {
855-
m_prune_size_gb = ParsePruneSizeGB(value);
856-
if (getOption(Prune).toBool()) {
857-
update(PruneSetting(true, m_prune_size_gb));
858-
setRestartRequired(true);
874+
if (suffix.empty() && !getOption(Prune).toBool()) {
875+
setOption(option, value, "-prev");
876+
} else {
877+
update(PruneSetting(true, ParsePruneSizeGB(value)));
859878
}
879+
if (suffix.empty() && getOption(Prune).toBool()) setRestartRequired(true);
860880
}
861881
break;
862882
case DatabaseCache:

src/qt/optionsmodel.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ class OptionsModel : public QAbstractListModel
100100
int rowCount(const QModelIndex & parent = QModelIndex()) const override;
101101
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override;
102102
bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole) override;
103-
QVariant getOption(OptionID option) const;
104-
bool setOption(OptionID option, const QVariant& value);
103+
QVariant getOption(OptionID option, const std::string& suffix="") const;
104+
bool setOption(OptionID option, const QVariant& value, const std::string& suffix="");
105105
/** Updates current unit in memory, settings and emits displayUnitChanged(new_unit) signal */
106106
void setDisplayUnit(const QVariant& new_unit);
107107

@@ -145,15 +145,6 @@ class OptionsModel : public QAbstractListModel
145145
bool fKeepChangeAddress;
146146
bool fShowAdvancedCJUI;
147147

148-
//! In-memory settings for display. These are stored persistently by the
149-
//! bitcoin node but it's also nice to store them in memory to prevent them
150-
//! getting cleared when enable/disable toggles are used in the GUI.
151-
int m_prune_size_gb;
152-
QString m_proxy_ip;
153-
QString m_proxy_port;
154-
QString m_onion_ip;
155-
QString m_onion_port;
156-
157148
/* settings that were overridden by command-line */
158149
QString strOverriddenByCommandLine;
159150

0 commit comments

Comments
 (0)