Skip to content

Commit

Permalink
Implement sidestake description validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Oct 25, 2023
1 parent ee6e264 commit 990b29a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 10 deletions.
9 changes: 8 additions & 1 deletion src/qt/editsidestakedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,14 @@ void EditSideStakeDialog::accept()
QMessageBox::warning(this, windowTitle(),
tr("The entered allocation is not valid. Check to make sure that the "
"allocation is greater than zero and when added to the other allocations "
"totals less than 100.").arg(ui->allocationLineEdit->text()),
"totals less than 100."),
QMessageBox::Ok, QMessageBox::Ok);
break;
case SideStakeTableModel::INVALID_DESCRIPTION:
QMessageBox::warning(this, windowTitle(),
tr("The entered description is not valid. Check to make sure that the "
"description only contains letters, numbers, spaces, periods, or "
"underscores."),
QMessageBox::Ok, QMessageBox::Ok);
}

Expand Down
17 changes: 16 additions & 1 deletion src/qt/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ void OptionsDialog::setModel(OptionsModel *model)
ui->sidestakingTableView->installEventFilter(this);

connect(this, &OptionsDialog::sidestakeAllocationInvalid, this, &OptionsDialog::handleSideStakeAllocationInvalid);

connect(this, &OptionsDialog::sidestakeDescriptionInvalid, this, &OptionsDialog::handleSideStakeDescriptionInvalid);
}

/* update the display unit, to not use the default ("BTC") */
Expand Down Expand Up @@ -564,6 +564,10 @@ bool OptionsDialog::eventFilter(QObject *object, QEvent *event)
if (model->getSideStakeTableModel()->getEditStatus() == SideStakeTableModel::INVALID_ALLOCATION) {
emit sidestakeAllocationInvalid();
}

if (model->getSideStakeTableModel()->getEditStatus() == SideStakeTableModel::INVALID_DESCRIPTION) {
emit sidestakeDescriptionInvalid();
}
}

return QDialog::eventFilter(object, event);
Expand Down Expand Up @@ -601,6 +605,17 @@ void OptionsDialog::handleSideStakeAllocationInvalid()
QMessageBox::Ok, QMessageBox::Ok);
}

void OptionsDialog::handleSideStakeDescriptionInvalid()
{
model->getSideStakeTableModel()->refresh();

QMessageBox::warning(this, windowTitle(),
tr("The entered description is not valid. Check to make sure that the "
"description only contains letters, numbers, spaces, periods, or "
"underscores."),
QMessageBox::Ok, QMessageBox::Ok);
}

void OptionsDialog::updateSideStakeTableView()
{
ui->sidestakingTableView->update();
Expand Down
6 changes: 4 additions & 2 deletions src/qt/optionsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ private slots:
void handleStakingEfficiencyValid(QValidatedLineEdit *object, bool fState);
void handleMinStakeSplitValueValid(QValidatedLineEdit *object, bool fState);
void handleSideStakeAllocationInvalid();
void handleSideStakeDescriptionInvalid();

void refreshSideStakeTableModel();

Expand All @@ -71,6 +72,7 @@ private slots:
void stakingEfficiencyValid(QValidatedLineEdit *object, bool fValid);
void minStakeSplitValueValid(QValidatedLineEdit *object, bool fValid);
void sidestakeAllocationInvalid();
void sidestakeDescriptionInvalid();

private:
Ui::OptionsDialog *ui;
Expand All @@ -89,8 +91,8 @@ private slots:
enum SideStakeTableColumnWidths
{
ADDRESS_COLUMN_WIDTH = 200,
ALLOCATION_COLUMN_WIDTH = 50,
DESCRIPTION_COLUMN_WIDTH = 130,
ALLOCATION_COLUMN_WIDTH = 60,
DESCRIPTION_COLUMN_WIDTH = 150,
STATUS_COLUMN_WIDTH = 50
};

Expand Down
24 changes: 19 additions & 5 deletions src/qt/sidestaketablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,19 @@ bool SideStakeTableModel::setData(const QModelIndex &index, const QVariant &valu
}
case Description:
{
if (rec->m_description == value.toString().toStdString()) {
std::string orig_value = value.toString().toStdString();
std::string san_value = SanitizeString(orig_value, SAFE_CHARS_CSV);

if (rec->m_description == orig_value) {
m_edit_status = NO_CHANGES;
return false;
}

if (san_value != orig_value) {
m_edit_status = INVALID_DESCRIPTION;
return false;
}

// Save the original local sidestake (also in the core).
GRC::SideStake orig_sidestake = *rec;

Expand All @@ -261,7 +269,7 @@ bool SideStakeTableModel::setData(const QModelIndex &index, const QVariant &valu
// Add back the sidestake with the modified allocation
registry.NonContractAdd(GRC::SideStake(orig_sidestake.m_key,
orig_sidestake.m_allocation,
value.toString().toStdString(),
san_value,
int64_t {0},
uint256 {},
orig_sidestake.m_status.Value()), true);
Expand Down Expand Up @@ -327,8 +335,6 @@ QString SideStakeTableModel::addRow(const QString &address, const QString &alloc

double sidestake_allocation = 0.0;

std::string sidestake_description = description.toStdString();

m_edit_status = OK;

if (!sidestake_address.IsValid()) {
Expand Down Expand Up @@ -362,9 +368,17 @@ QString SideStakeTableModel::addRow(const QString &address, const QString &alloc

sidestake_allocation /= 100.0;

std::string sidestake_description = description.toStdString();
std::string sanitized_description = SanitizeString(sidestake_description, SAFE_CHARS_CSV);

if (sanitized_description != sidestake_description) {
m_edit_status = INVALID_DESCRIPTION;
return QString();
}

registry.NonContractAdd(GRC::SideStake(sidestake_address,
sidestake_allocation,
sidestake_description,
sanitized_description,
int64_t {0},
uint256 {},
GRC::SideStakeStatus::ACTIVE));
Expand Down
3 changes: 2 additions & 1 deletion src/qt/sidestaketablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ class SideStakeTableModel : public QAbstractTableModel
NO_CHANGES, /**< No changes were made during edit operation */
INVALID_ADDRESS, /**< Unparseable address */
DUPLICATE_ADDRESS, /**< Address already in sidestake registry */
INVALID_ALLOCATION /**< Allocation is invalid (i.e. not parseable or not between 0.0 and 100.0) */
INVALID_ALLOCATION, /**< Allocation is invalid (i.e. not parseable or not between 0.0 and 100.0) */
INVALID_DESCRIPTION /**< Description contains an invalid character */
};

/** @name Methods overridden from QAbstractTableModel
Expand Down
1 change: 1 addition & 0 deletions src/util/strencodings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ static const std::string SAFE_CHARS[] =
CHARS_ALPHA_NUM + " .,;-_?@", // SAFE_CHARS_UA_COMMENT
CHARS_ALPHA_NUM + ".-_", // SAFE_CHARS_FILENAME
CHARS_ALPHA_NUM + "!*'();:@&=+$,/?#[]-_.~%", // SAFE_CHARS_URI
CHARS_ALPHA_NUM + " .-_" // SAFE_CHARS_CSV
};

std::string SanitizeString(const std::string& str, int rule)
Expand Down
1 change: 1 addition & 0 deletions src/util/strencodings.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ enum SafeChars
SAFE_CHARS_UA_COMMENT, //!< BIP-0014 subset
SAFE_CHARS_FILENAME, //!< Chars allowed in filenames
SAFE_CHARS_URI, //!< Chars allowed in URIs (RFC 3986)
SAFE_CHARS_CSV //!< Chars allowed in fields stored as comma separated values
};

/**
Expand Down

0 comments on commit 990b29a

Please sign in to comment.