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 9, 2023
1 parent e5299bc commit 34d31ce
Show file tree
Hide file tree
Showing 12 changed files with 403 additions and 152 deletions.
47 changes: 26 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 @@ -579,22 +584,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 +657,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
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
35 changes: 16 additions & 19 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 @@ -29,10 +28,16 @@ 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;
}

Expand All @@ -45,7 +50,7 @@ EditSideStakeDialog::~EditSideStakeDialog()
delete ui;
}

void EditSideStakeDialog::setModel(OptionsModel *model)
void EditSideStakeDialog::setModel(SideStakeTableModel* model)
{
this->model = model;
if (!model) {
Expand All @@ -56,6 +61,7 @@ void EditSideStakeDialog::setModel(OptionsModel *model)
mapper->addMapping(ui->addressLineEdit, SideStakeTableModel::Address);
mapper->addMapping(ui->allocationLineEdit, SideStakeTableModel::Allocation);
mapper->addMapping(ui->descriptionLineEdit, SideStakeTableModel::Description);
mapper->addMapping(ui->statusLineEdit, SideStakeTableModel::Status);
}

void EditSideStakeDialog::loadRow(int row)
Expand All @@ -72,9 +78,9 @@ bool EditSideStakeDialog::saveCurrentRow()
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()) {
Expand All @@ -93,7 +99,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 +121,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);
}
9 changes: 3 additions & 6 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,12 +31,9 @@ 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();

Expand All @@ -46,7 +43,7 @@ public slots:
Ui::EditSideStakeDialog *ui;
QDataWidgetMapper *mapper;
Mode mode;
OptionsModel *model;
SideStakeTableModel *model;

QString address;
};
Expand Down
Loading

0 comments on commit 34d31ce

Please sign in to comment.