Skip to content

Commit

Permalink
[Refactoring] Move SST / custom fee handling to walletmodel
Browse files Browse the repository at this point in the history
- encapsulate SST settings, and save only in wallet DB
- emit signal when SST changes (to sync GUI with RPC)
  • Loading branch information
random-zebra committed Apr 11, 2021
1 parent 3b9c9e6 commit 9638333
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 151 deletions.
2 changes: 0 additions & 2 deletions src/qt/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ void OptionsDialog::setMapper()
/* Wallet */
mapper->addMapping(ui->spendZeroConfChange, OptionsModel::SpendZeroConfChange);
mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
mapper->addMapping(ui->spinBoxStakeSplitThreshold, OptionsModel::StakeSplitThreshold);

/* Network */
mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
Expand Down Expand Up @@ -264,7 +263,6 @@ void OptionsDialog::on_resetButton_clicked()
void OptionsDialog::on_okButton_clicked()
{
mapper->submit();
pwalletMain->MarkDirty();
accept();
}

Expand Down
91 changes: 0 additions & 91 deletions src/qt/optionsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ void OptionsModel::Init()

// Ensure restart flag is unset on client startup
setRestartRequired(false);
setSSTChanged(false);

// These are Qt-only settings:

Expand Down Expand Up @@ -127,8 +126,6 @@ void OptionsModel::setWalletDefaultOptions(QSettings& settings, bool reset)
if (!gArgs.SoftSetBoolArg("-spendzeroconfchange", settings.value("bSpendZeroConfChange").toBool()))
addOverriddenOption("-spendzeroconfchange");
if (reset) {
setStakeSplitThreshold(CWallet::DEFAULT_STAKE_SPLIT_THRESHOLD);
setUseCustomFee(false);
refreshDataView();
}
}
Expand Down Expand Up @@ -261,16 +258,6 @@ QVariant OptionsModel::data(const QModelIndex& index, int role) const
return settings.value("bSpendZeroConfChange");
case ShowMasternodesTab:
return settings.value("fShowMasternodesTab");
case StakeSplitThreshold:
{
// Return CAmount/qlonglong as double
const CAmount nStakeSplitThreshold = (pwalletMain) ? pwalletMain->nStakeSplitThreshold : CWallet::DEFAULT_STAKE_SPLIT_THRESHOLD;
return QVariant(static_cast<double>(nStakeSplitThreshold / static_cast<double>(COIN)));
}
case fUseCustomFee:
return QVariant((pwalletMain) ? pwalletMain->fUseCustomFee : false);
case nCustomFee:
return QVariant(static_cast<qlonglong>((pwalletMain) ? pwalletMain->nCustomFee : CWallet::GetRequiredFee(1000)));
#endif
case DisplayUnit:
return nDisplayUnit;
Expand Down Expand Up @@ -375,18 +362,7 @@ bool OptionsModel::setData(const QModelIndex& index, const QVariant& value, int
setRestartRequired(true);
}
break;
case fUseCustomFee:
setUseCustomFee(value.toBool());
break;
case nCustomFee:
setCustomFeeValue(value.toLongLong());
break;
#endif
case StakeSplitThreshold:
// Write double as qlonglong/CAmount
setStakeSplitThreshold(static_cast<CAmount>(value.toDouble() * COIN));
setSSTChanged(true);
break;
case DisplayUnit:
setDisplayUnit(value);
break;
Expand Down Expand Up @@ -478,61 +454,6 @@ void OptionsModel::setDisplayUnit(const QVariant& value)
}
}

/* Update StakeSplitThreshold's value in wallet */
void OptionsModel::setStakeSplitThreshold(const CAmount nStakeSplitThreshold)
{
if (pwalletMain && pwalletMain->nStakeSplitThreshold != nStakeSplitThreshold) {
CWalletDB walletdb(pwalletMain->GetDBHandle());
LOCK(pwalletMain->cs_wallet);
{
pwalletMain->nStakeSplitThreshold = nStakeSplitThreshold;
walletdb.WriteStakeSplitThreshold(nStakeSplitThreshold);
}
}
}

/* returns default minimum value for stake split threshold as doulbe */
double OptionsModel::getSSTMinimum() const
{
return static_cast<double>(CWallet::minStakeSplitThreshold / COIN);
}

/* Verify that StakeSplitThreshold's value is either 0 or above the min. Else reset */
bool OptionsModel::isSSTValid()
{
if (pwalletMain && pwalletMain->nStakeSplitThreshold &&
pwalletMain->nStakeSplitThreshold < CWallet::minStakeSplitThreshold) {
setStakeSplitThreshold(CWallet::minStakeSplitThreshold);
return false;
}
return true;
}

/* Update Custom Fee value in wallet */
void OptionsModel::setUseCustomFee(bool fUse)
{
if (pwalletMain && pwalletMain->fUseCustomFee != fUse) {
CWalletDB walletdb(pwalletMain->GetDBHandle());
{
LOCK(pwalletMain->cs_wallet);
pwalletMain->fUseCustomFee = fUse;
walletdb.WriteUseCustomFee(fUse);
}
}
}

void OptionsModel::setCustomFeeValue(const CAmount& value)
{
if (pwalletMain && pwalletMain->nCustomFee != value) {
CWalletDB walletdb(pwalletMain->GetDBHandle());
{
LOCK(pwalletMain->cs_wallet);
pwalletMain->nCustomFee = value;
walletdb.WriteCustomFeeValue(value);
}
}
}

bool OptionsModel::getProxySettings(QNetworkProxy& proxy) const
{
// Directly query current base proxy, because
Expand Down Expand Up @@ -561,15 +482,3 @@ bool OptionsModel::isRestartRequired()
QSettings settings;
return settings.value("fRestartRequired", false).toBool();
}

void OptionsModel::setSSTChanged(bool fChanged)
{
QSettings settings;
return settings.setValue("fSSTChanged", fChanged);
}

bool OptionsModel::isSSTChanged()
{
QSettings settings;
return settings.value("fSSTChanged", false).toBool();
}
12 changes: 0 additions & 12 deletions src/qt/optionsmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ class OptionsModel : public QAbstractListModel
AnonymizePivxAmount, //int
ShowMasternodesTab, // bool
Listen, // bool
StakeSplitThreshold, // CAmount (LongLong)
ShowColdStakingScreen, // bool
fUseCustomFee, // bool
nCustomFee, // CAmount (LongLong)
OptionIDRowCount,
};

Expand All @@ -71,13 +68,6 @@ class OptionsModel : public QAbstractListModel
void refreshDataView();
/** Updates current unit in memory, settings and emits displayUnitChanged(newUnit) signal */
void setDisplayUnit(const QVariant& value);
/* Update StakeSplitThreshold's value in wallet */
void setStakeSplitThreshold(const CAmount value);
double getSSTMinimum() const;
bool isSSTValid();
/* Update Custom Fee value in wallet */
void setUseCustomFee(bool fUse);
void setCustomFeeValue(const CAmount& value);

/* Explicit getters */
bool isHideCharts() { return fHideCharts; }
Expand All @@ -93,8 +83,6 @@ class OptionsModel : public QAbstractListModel
/* Restart flag helper */
void setRestartRequired(bool fRequired);
bool isRestartRequired();
void setSSTChanged(bool fChanged);
bool isSSTChanged();
bool resetSettings;

bool isColdStakingScreenEnabled() { return showColdStakingScreen; }
Expand Down
56 changes: 46 additions & 10 deletions src/qt/pivx/settings/settingswalletoptionswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,17 @@ SettingsWalletOptionsWidget::SettingsWalletOptionsWidget(PIVXGUI* _window, QWidg
connect(ui->pushButtonClean, &QPushButton::clicked, [this] { Q_EMIT discardSettings(); });
}

void SettingsWalletOptionsWidget::onResetClicked(){
if (clientModel) {
OptionsModel *optionsModel = clientModel->getOptionsModel();
QSettings settings;
optionsModel->setWalletDefaultOptions(settings, true);
optionsModel->setNetworkDefaultOptions(settings, true);
inform(tr("Options reset succeed"));
}
void SettingsWalletOptionsWidget::onResetClicked()
{
QSettings settings;
walletModel->resetWalletOptions(settings);
clientModel->getOptionsModel()->setNetworkDefaultOptions(settings, true);
inform(tr("Options reset succeed"));
}

void SettingsWalletOptionsWidget::setMapper(QDataWidgetMapper *mapper){
void SettingsWalletOptionsWidget::setMapper(QDataWidgetMapper *mapper)
{
mapper->addMapping(ui->radioButtonSpend, OptionsModel::SpendZeroConfChange);
mapper->addMapping(ui->spinBoxStakeSplitThreshold, OptionsModel::StakeSplitThreshold);

// Network
mapper->addMapping(ui->checkBoxMap, OptionsModel::MapPortUPnP);
Expand All @@ -79,11 +77,49 @@ void SettingsWalletOptionsWidget::setMapper(QDataWidgetMapper *mapper){
mapper->addMapping(ui->lineEditPort, OptionsModel::ProxyPort);
}

void SettingsWalletOptionsWidget::loadWalletModel()
{
reloadWalletOptions();
connect(walletModel, &WalletModel::notifySSTChanged, this, &SettingsWalletOptionsWidget::setSpinBoxStakeSplitThreshold);
}

void SettingsWalletOptionsWidget::reloadWalletOptions()
{
setSpinBoxStakeSplitThreshold(static_cast<double>(walletModel->getWalletStakeSplitThreshold()) / COIN);
}

void SettingsWalletOptionsWidget::setSpinBoxStakeSplitThreshold(double val)
{
ui->spinBoxStakeSplitThreshold->setValue(val);
}

double SettingsWalletOptionsWidget::getSpinBoxStakeSplitThreshold() const
{
return ui->spinBoxStakeSplitThreshold->value();
}

bool SettingsWalletOptionsWidget::saveWalletOnlyOptions()
{
// stake split threshold
const CAmount sstOld = walletModel->getWalletStakeSplitThreshold();
const CAmount sstNew = static_cast<CAmount>(getSpinBoxStakeSplitThreshold() * COIN);
if (sstNew != sstOld) {
const double stakeSplitMinimum = walletModel->getSSTMinimum();
if (sstNew != 0 && sstNew < static_cast<CAmount>(stakeSplitMinimum * COIN)) {
setSpinBoxStakeSplitThreshold(stakeSplitMinimum);
inform(tr("Stake Split too low, it shall be either >= %1 or equal to 0 (to disable stake splitting)").arg(stakeSplitMinimum));
return false;
}
walletModel->setWalletStakeSplitThreshold(sstNew);
}
return true;
}

void SettingsWalletOptionsWidget::discardWalletOnlyOptions()
{
reloadWalletOptions();
}

SettingsWalletOptionsWidget::~SettingsWalletOptionsWidget(){
delete ui;
}
10 changes: 9 additions & 1 deletion src/qt/pivx/settings/settingswalletoptionswidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class SettingsWalletOptionsWidget : public PWidget
~SettingsWalletOptionsWidget();

void setMapper(QDataWidgetMapper *mapper);
void setSpinBoxStakeSplitThreshold(double val);

void discardWalletOnlyOptions();
bool saveWalletOnlyOptions();

Q_SIGNALS:
void saveSettings();
Expand All @@ -32,6 +34,12 @@ public Q_SLOTS:

private:
Ui::SettingsWalletOptionsWidget *ui;

void loadWalletModel() override;
void reloadWalletOptions();

void setSpinBoxStakeSplitThreshold(double val);
double getSpinBoxStakeSplitThreshold() const;
};

#endif // SETTINGSWALLETOPTIONSWIDGET_H
14 changes: 7 additions & 7 deletions src/qt/pivx/settings/settingswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ void SettingsWidget::loadWalletModel()
this->settingsBitToolWidget->setWalletModel(this->walletModel);
//this->settingsMultisendWidget->setWalletModel(this->walletModel); no visible for now
this->settingsDisplayOptionsWidget->setWalletModel(this->walletModel);
this->settingsWalletOptionsWidget->setWalletModel(this->walletModel);
}

void SettingsWidget::onResetAction()
Expand All @@ -230,15 +231,13 @@ void SettingsWidget::onResetAction()

void SettingsWidget::onSaveOptionsClicked()
{
// Save settings that are stored inside the wallet only
if (!settingsWalletOptionsWidget->saveWalletOnlyOptions()) {
return;
}

if (mapper->submit()) {
OptionsModel* optionsModel = this->clientModel->getOptionsModel();
if (optionsModel->isSSTChanged() && !optionsModel->isSSTValid()) {
const double stakeSplitMinimum = optionsModel->getSSTMinimum();
settingsWalletOptionsWidget->setSpinBoxStakeSplitThreshold(stakeSplitMinimum);
inform(tr("Stake Split too low, it shall be either >= %1 or equal to 0 (to disable stake splitting)").arg(stakeSplitMinimum));
return;
}
pwalletMain->MarkDirty();
if (optionsModel->isRestartRequired()) {
bool fAcceptRestart = openStandardDialog(tr("Restart required"), tr("Your wallet needs to be restarted to apply the changes\n"), tr("Restart Now"), tr("Restart Later"));

Expand Down Expand Up @@ -422,6 +421,7 @@ void SettingsWidget::onDiscardChanges()
return;
clientModel->getOptionsModel()->refreshDataView();
}
settingsWalletOptionsWidget->discardWalletOnlyOptions();
}

void SettingsWidget::setMapper()
Expand Down
Loading

0 comments on commit 9638333

Please sign in to comment.