Skip to content

Commit

Permalink
Implementation of add, edit, and delete sidestake buttons in options
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Oct 10, 2023
1 parent e5299bc commit 3d85fdd
Show file tree
Hide file tree
Showing 12 changed files with 457 additions and 171 deletions.
53 changes: 32 additions & 21 deletions src/gridcoin/sidestake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ const std::vector<SideStake_ptr> SideStakeRegistry::SideStakeEntries() const
return sidestakes;
}

const std::vector<SideStake_ptr> SideStakeRegistry::ActiveSideStakeEntries(const bool& local_only)
const std::vector<SideStake_ptr> SideStakeRegistry::ActiveSideStakeEntries(const bool& local_only,
const bool& include_zero_alloc)
{
std::vector<SideStake_ptr> sidestakes;
double allocation_sum = 0.0;
Expand All @@ -225,8 +226,10 @@ const std::vector<SideStake_ptr> SideStakeRegistry::ActiveSideStakeEntries(const
for (const auto& entry : m_sidestake_entries)
{
if (entry.second->m_status == SideStakeStatus::MANDATORY && allocation_sum + entry.second->m_allocation <= 1.0) {
sidestakes.push_back(entry.second);
allocation_sum += entry.second->m_allocation;
if ((include_zero_alloc && entry.second->m_allocation == 0.0) || entry.second->m_allocation > 0.0) {
sidestakes.push_back(entry.second);
allocation_sum += entry.second->m_allocation;
}
}
}
}
Expand All @@ -240,8 +243,10 @@ const std::vector<SideStake_ptr> SideStakeRegistry::ActiveSideStakeEntries(const
for (const auto& entry : m_local_sidestake_entries)
{
if (entry.second->m_status == SideStakeStatus::ACTIVE && allocation_sum + entry.second->m_allocation <= 1.0) {
sidestakes.push_back(entry.second);
allocation_sum += entry.second->m_allocation;
if ((include_zero_alloc && entry.second->m_allocation == 0.0) || entry.second->m_allocation > 0.0) {
sidestakes.push_back(entry.second);
allocation_sum += entry.second->m_allocation;
}
}
}
}
Expand Down Expand Up @@ -372,6 +377,8 @@ void SideStakeRegistry::AddDelete(const ContractContext& ctx)

void SideStakeRegistry::NonContractAdd(const SideStake& sidestake, const bool& save_to_file)
{
LOCK(cs_lock);

// Using this form of insert because we want the latest record with the same key to override any previous one.
m_local_sidestake_entries[sidestake.m_key] = std::make_shared<SideStake>(sidestake);

Expand All @@ -387,6 +394,8 @@ void SideStakeRegistry::Add(const ContractContext& ctx)

void SideStakeRegistry::NonContractDelete(const CBitcoinAddressForStorage& address, const bool& save_to_file)
{
LOCK(cs_lock);

auto sidestake_entry_pair_iter = m_local_sidestake_entries.find(address);

if (sidestake_entry_pair_iter != m_local_sidestake_entries.end()) {
Expand Down Expand Up @@ -579,22 +588,22 @@ void SideStakeRegistry::LoadLocalSideStakesFromConfig()

bool new_format_valid = false;

if (addresses.size() != allocations.size() || (!descriptions.empty() && descriptions.size() != addresses.size()))
{
LogPrintf("WARN: %s: Malformed new style sidestaking configuration entries. Reverting to original format in read only "
"gridcoinresearch.conf file.",
__func__);
} else {
new_format_valid = true;
if (!addresses.empty()) {
if (addresses.size() != allocations.size() || (!descriptions.empty() && addresses.size() != descriptions.size())) {
LogPrintf("WARN: %s: Malformed new style sidestaking configuration entries. "
"Reverting to original format in read only gridcoinresearch.conf file.",
__func__);
} else {
new_format_valid = true;

for (unsigned int i = 0; i < addresses.size(); ++i)
{
if (descriptions.empty()) {
raw_vSideStakeAlloc.push_back(std::make_tuple(addresses[i], allocations[i], ""));
} else {
raw_vSideStakeAlloc.push_back(std::make_tuple(addresses[i], allocations[i], descriptions[i]));
for (unsigned int i = 0; i < addresses.size(); ++i)
{
if (descriptions.empty()) {
raw_vSideStakeAlloc.push_back(std::make_tuple(addresses[i], allocations[i], ""));
} else {
raw_vSideStakeAlloc.push_back(std::make_tuple(addresses[i], allocations[i], descriptions[i]));
}
}

}
}

Expand Down Expand Up @@ -652,9 +661,9 @@ void SideStakeRegistry::LoadLocalSideStakesFromConfig()

dAllocation /= 100.0;

if (dAllocation <= 0)
if (dAllocation < 0)
{
LogPrintf("WARN: %s: Negative or zero allocation provided. Skipping allocation.", __func__);
LogPrintf("WARN: %s: Negative allocation provided. Skipping allocation.", __func__);
continue;
}

Expand Down Expand Up @@ -721,6 +730,8 @@ bool SideStakeRegistry::SaveLocalSideStakesToConfig()

std::vector<std::pair<std::string, util::SettingsValue>> settings;

LOCK(cs_lock);

unsigned int i = 0;
for (const auto& iter : m_local_sidestake_entries) {
if (i) {
Expand Down
2 changes: 1 addition & 1 deletion src/gridcoin/sidestake.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ class SideStakeRegistry : public IContractHandler
//!
//! \return A vector of smart pointers to sidestake entries.
//!
const std::vector<SideStake_ptr> ActiveSideStakeEntries(const bool& local_only = false);
const std::vector<SideStake_ptr> ActiveSideStakeEntries(const bool& local_only, const bool& include_zero_alloc);

//!
//! \brief Get the current sidestake entry for the specified key string.
Expand Down
2 changes: 1 addition & 1 deletion src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ void StakeMiner(CWallet *pwallet)

// Note that fEnableSideStaking is now processed internal to ActiveSideStakeEntries. The sidestaking flag only
// controls local sidestakes. If there exists mandatory sidestakes, they occur regardless of the flag.
vSideStakeAlloc = GRC::GetSideStakeRegistry().ActiveSideStakeEntries();
vSideStakeAlloc = GRC::GetSideStakeRegistry().ActiveSideStakeEntries(false, false);

// wait for next round
if (!MilliSleep(nMinerSleep)) return;
Expand Down
69 changes: 39 additions & 30 deletions src/qt/editsidestakedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "editsidestakedialog.h"
#include "ui_editsidestakedialog.h"
#include "optionsmodel.h"
#include "sidestaketablemodel.h"
#include "guiutil.h"
#include "qt/decoration.h"
Expand All @@ -15,7 +14,6 @@
EditSideStakeDialog::EditSideStakeDialog(Mode mode, QWidget* parent)
: QDialog(parent)
, ui(new Ui::EditSideStakeDialog)
, mapper(nullptr)
, mode(mode)
, model(nullptr)
{
Expand All @@ -29,38 +27,43 @@ EditSideStakeDialog::EditSideStakeDialog(Mode mode, QWidget* parent)
{
case NewSideStake:
setWindowTitle(tr("New SideStake"));
ui->statusLineEdit->setEnabled(false);
ui->statusLabel->setHidden(true);
ui->statusLineEdit->setHidden(true);
break;
case EditSideStake:
setWindowTitle(tr("Edit SideStake"));
ui->addressLineEdit->setEnabled(false);
ui->statusLabel->setHidden(false);
ui->statusLineEdit->setHidden(false);
ui->statusLineEdit->setEnabled(false);
break;
}

mapper = new QDataWidgetMapper(this);
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
}

EditSideStakeDialog::~EditSideStakeDialog()
{
delete ui;
}

void EditSideStakeDialog::setModel(OptionsModel *model)
void EditSideStakeDialog::setModel(SideStakeTableModel* model)
{
this->model = model;
if (!model) {
return;
}

mapper->setModel(model);
mapper->addMapping(ui->addressLineEdit, SideStakeTableModel::Address);
mapper->addMapping(ui->allocationLineEdit, SideStakeTableModel::Allocation);
mapper->addMapping(ui->descriptionLineEdit, SideStakeTableModel::Description);
}

void EditSideStakeDialog::loadRow(int row)
{
mapper->setCurrentIndex(row);
m_row = row;

ui->addressLineEdit->setText(model->index(row, SideStakeTableModel::Address, QModelIndex()).data().toString());
ui->allocationLineEdit->setText(model->index(row, SideStakeTableModel::Allocation, QModelIndex()).data().toString());
ui->descriptionLineEdit->setText(model->index(row, SideStakeTableModel::Description, QModelIndex()).data().toString());
ui->statusLineEdit->setText(model->index(row, SideStakeTableModel::Status, QModelIndex()).data().toString());
}

bool EditSideStakeDialog::saveCurrentRow()
Expand All @@ -69,20 +72,35 @@ bool EditSideStakeDialog::saveCurrentRow()
return false;
}

bool success = true;

switch (mode)
{
case NewSideStake:
address = model->getSideStakeTableModel()->addRow(ui->addressLineEdit->text(),
ui->allocationLineEdit->text(),
ui->descriptionLineEdit->text());
address = model->addRow(ui->addressLineEdit->text(),
ui->allocationLineEdit->text(),
ui->descriptionLineEdit->text());

break;
case EditSideStake:
if (mapper->submit()) {
address = ui->addressLineEdit->text();
QModelIndex index = model->index(m_row, SideStakeTableModel::Allocation, QModelIndex());
model->setData(index, ui->allocationLineEdit->text(), Qt::EditRole);

if (model->getEditStatus() == SideStakeTableModel::OK || model->getEditStatus() == SideStakeTableModel::NO_CHANGES) {
index = model->index(m_row, SideStakeTableModel::Description, QModelIndex());
model->setData(index, ui->descriptionLineEdit->text(), Qt::EditRole);

if (model->getEditStatus() == SideStakeTableModel::OK || model->getEditStatus() == SideStakeTableModel::NO_CHANGES) {
break;
}
}

success = false;

break;
}
return !address.isEmpty();

return success;
}

void EditSideStakeDialog::accept()
Expand All @@ -93,7 +111,7 @@ void EditSideStakeDialog::accept()

if (!saveCurrentRow())
{
switch(model->getSideStakeTableModel()->getEditStatus())
switch (model->getEditStatus())
{
case SideStakeTableModel::OK:
// Failed with unknown reason. Just reject.
Expand All @@ -115,23 +133,14 @@ void EditSideStakeDialog::accept()
break;
case SideStakeTableModel::INVALID_ALLOCATION:
QMessageBox::warning(this, windowTitle(),
tr("The entered allocation is not valid.").arg(ui->allocationLineEdit->text()),
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()),
QMessageBox::Ok, QMessageBox::Ok);

}

return;
}

QDialog::accept();
}

QString EditSideStakeDialog::getAddress() const
{
return address;
}

void EditSideStakeDialog::setAddress(const QString &address)
{
this->address = address;
ui->addressLineEdit->setText(address);
}
11 changes: 4 additions & 7 deletions src/qt/editsidestakedialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ QT_END_NAMESPACE
namespace Ui {
class EditSideStakeDialog;
}
class OptionsModel;
class SideStakeTableModel;

/** Dialog for editing an address and associated information.
*/
Expand All @@ -31,22 +31,19 @@ class EditSideStakeDialog : public QDialog
explicit EditSideStakeDialog(Mode mode, QWidget* parent = nullptr);
~EditSideStakeDialog();

void setModel(OptionsModel *model);
void setModel(SideStakeTableModel* model);
void loadRow(int row);

QString getAddress() const;
void setAddress(const QString &address);

public slots:
void accept();

private:
bool saveCurrentRow();

Ui::EditSideStakeDialog *ui;
QDataWidgetMapper *mapper;
Mode mode;
OptionsModel *model;
SideStakeTableModel *model;
int m_row;

QString address;
};
Expand Down
Loading

0 comments on commit 3d85fdd

Please sign in to comment.