Skip to content

Commit

Permalink
Implementation of EditSideStakeDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Oct 11, 2023
1 parent b51ce44 commit 65f1d88
Show file tree
Hide file tree
Showing 14 changed files with 770 additions and 52 deletions.
4 changes: 4 additions & 0 deletions src/Makefile.qt.include
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ QT_FORMS_UI = \
qt/forms/consolidateunspentwizardsendpage.ui \
qt/forms/diagnosticsdialog.ui \
qt/forms/editaddressdialog.ui \
qt/forms/editsidestakedialog.ui \
qt/forms/favoritespage.ui \
qt/forms/intro.ui \
qt/forms/mrcrequestpage.ui \
Expand Down Expand Up @@ -143,6 +144,7 @@ QT_MOC_CPP = \
qt/moc_csvmodelwriter.cpp \
qt/moc_diagnosticsdialog.cpp \
qt/moc_editaddressdialog.cpp \
qt/moc_editsidestakedialog.cpp \
qt/moc_favoritespage.cpp \
qt/moc_guiutil.cpp \
qt/moc_intro.cpp \
Expand Down Expand Up @@ -250,6 +252,7 @@ GRIDCOINRESEARCH_QT_H = \
qt/decoration.h \
qt/diagnosticsdialog.h \
qt/editaddressdialog.h \
qt/editsidestakedialog.h \
qt/favoritespage.h \
qt/guiconstants.h \
qt/guiutil.h \
Expand Down Expand Up @@ -342,6 +345,7 @@ GRIDCOINRESEARCH_QT_CPP = \
qt/decoration.cpp \
qt/diagnosticsdialog.cpp \
qt/editaddressdialog.cpp \
qt/editsidestakedialog.cpp \
qt/favoritespage.cpp \
qt/guiutil.cpp \
qt/intro.cpp \
Expand Down
80 changes: 52 additions & 28 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 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 @@ -221,11 +222,15 @@ const std::vector<SideStake_ptr> SideStakeRegistry::ActiveSideStakeEntries()
LOCK(cs_lock);

// Do mandatory sidestakes first.
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 (!local_only) {
for (const auto& entry : m_sidestake_entries)
{
if (entry.second->m_status == SideStakeStatus::MANDATORY && allocation_sum + entry.second->m_allocation <= 1.0) {
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 @@ -238,8 +243,10 @@ const std::vector<SideStake_ptr> SideStakeRegistry::ActiveSideStakeEntries()
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 All @@ -263,7 +270,7 @@ std::vector<SideStake_ptr> SideStakeRegistry::Try(const CBitcoinAddressForStorag

const auto local_entry = m_local_sidestake_entries.find(key);

if (local_entry != m_sidestake_entries.end()) {
if (local_entry != m_local_sidestake_entries.end()) {
result.push_back(local_entry->second);
}

Expand Down Expand Up @@ -368,26 +375,36 @@ void SideStakeRegistry::AddDelete(const ContractContext& ctx)
return;
}

void SideStakeRegistry::NonContractAdd(SideStake& sidestake)
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);


if (save_to_file) {
SaveLocalSideStakesToConfig();
}
}

void SideStakeRegistry::Add(const ContractContext& ctx)
{
AddDelete(ctx);
}

void SideStakeRegistry::NonContractDelete(CBitcoinAddressForStorage& address)
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()) {
m_local_sidestake_entries.erase(sidestake_entry_pair_iter);
}

if (save_to_file) {
SaveLocalSideStakesToConfig();
}
}

void SideStakeRegistry::Delete(const ContractContext& ctx)
Expand Down Expand Up @@ -571,17 +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)
{
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 @@ -639,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 All @@ -665,7 +687,7 @@ void SideStakeRegistry::LoadLocalSideStakesFromConfig()
SideStakeStatus::ACTIVE);

// This will add or update (replace) a non-contract entry in the registry for the local sidestake.
NonContractAdd(sidestake);
NonContractAdd(sidestake, false);

// This is needed because we need to detect entries in the registry map that are no longer in the config file to mark
// them deleted.
Expand Down Expand Up @@ -708,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 All @@ -721,9 +745,9 @@ bool SideStakeRegistry::SaveLocalSideStakesToConfig()
++i;
}

settings.push_back(std::make_pair("addresses", addresses));
settings.push_back(std::make_pair("allocations", allocations));
settings.push_back(std::make_pair("descriptions", descriptions));
settings.push_back(std::make_pair("sidestakeaddresses", addresses));
settings.push_back(std::make_pair("sidestakeallocations", allocations));
settings.push_back(std::make_pair("sidestakedescriptions", descriptions));

status = updateRwSettings(settings);

Expand Down
11 changes: 8 additions & 3 deletions src/gridcoin/sidestake.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,11 @@ class SideStakeRegistry : public IContractHandler
//! Mandatory sidestakes come before local ones, and the method ensures that the sidestakes
//! returned do not total an allocation greater than 1.0.
//!
//! \param bool true to return local sidestakes only
//!
//! \return A vector of smart pointers to sidestake entries.
//!
const std::vector<SideStake_ptr> ActiveSideStakeEntries();
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 Expand Up @@ -472,8 +474,9 @@ class SideStakeRegistry : public IContractHandler
//! the registry db.
//!
//! \param SideStake object to add
//! \param bool save_to_file if true causes SaveLocalSideStakesToConfig() to be called.
//!
void NonContractAdd(SideStake& sidestake);
void NonContractAdd(const SideStake& sidestake, const bool& save_to_file = true);

//!
//! \brief Add a sidestake entry to the registry from contract data. For the sidestake registry
Expand All @@ -487,9 +490,11 @@ class SideStakeRegistry : public IContractHandler
//!
//! \brief Provides for deletion of local (voluntary) sidestakes from the in-memory map that are not persisted
//! to the registry db. Deletion is by the map key (CBitcoinAddress).
//!
//! \param address
//! \param bool save_to_file if true causes SaveLocalSideStakesToConfig() to be called.
//!
void NonContractDelete(CBitcoinAddressForStorage& address);
void NonContractDelete(const CBitcoinAddressForStorage& address, const bool& save_to_file = true);

//!
//! \brief Mark a sidestake entry deleted in the registry from contract data. For the sidestake registry
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
1 change: 1 addition & 0 deletions src/qt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ add_library(gridcoinqt STATIC
decoration.cpp
diagnosticsdialog.cpp
editaddressdialog.cpp
editsidestakedialog.cpp
favoritespage.cpp
guiutil.cpp
intro.cpp
Expand Down
Loading

0 comments on commit 65f1d88

Please sign in to comment.