Skip to content

Commit

Permalink
Corrections to sidestake class.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Oct 8, 2023
1 parent bf4d620 commit e5299bc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
43 changes: 28 additions & 15 deletions src/gridcoin/sidestake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ 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)
{
std::vector<SideStake_ptr> sidestakes;
double allocation_sum = 0.0;
Expand All @@ -221,11 +221,13 @@ 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) {
sidestakes.push_back(entry.second);
allocation_sum += entry.second->m_allocation;
}
}
}

Expand Down Expand Up @@ -263,7 +265,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 +370,32 @@ void SideStakeRegistry::AddDelete(const ContractContext& ctx)
return;
}

void SideStakeRegistry::NonContractAdd(SideStake& sidestake)
void SideStakeRegistry::NonContractAdd(const SideStake& sidestake, const bool& save_to_file)
{
// 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)
{
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 @@ -581,7 +589,12 @@ void SideStakeRegistry::LoadLocalSideStakesFromConfig()

for (unsigned int i = 0; i < addresses.size(); ++i)
{
raw_vSideStakeAlloc.push_back(std::make_tuple(addresses[i], allocations[i], descriptions[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 @@ -665,7 +678,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 @@ -721,9 +734,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 = false);

//!
//! \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

0 comments on commit e5299bc

Please sign in to comment.