diff --git a/src/gridcoin/sidestake.cpp b/src/gridcoin/sidestake.cpp index 90a00f3c25..dc27fdce93 100644 --- a/src/gridcoin/sidestake.cpp +++ b/src/gridcoin/sidestake.cpp @@ -208,7 +208,7 @@ const std::vector SideStakeRegistry::SideStakeEntries() const return sidestakes; } -const std::vector SideStakeRegistry::ActiveSideStakeEntries() +const std::vector SideStakeRegistry::ActiveSideStakeEntries(const bool& local_only) { std::vector sidestakes; double allocation_sum = 0.0; @@ -221,11 +221,13 @@ const std::vector 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; + } } } @@ -263,7 +265,7 @@ std::vector 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); } @@ -368,12 +370,14 @@ 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); - + if (save_to_file) { + SaveLocalSideStakesToConfig(); + } } void SideStakeRegistry::Add(const ContractContext& ctx) @@ -381,13 +385,17 @@ 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) @@ -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])); + } + } } @@ -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. @@ -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); diff --git a/src/gridcoin/sidestake.h b/src/gridcoin/sidestake.h index 245873c211..54583e6858 100644 --- a/src/gridcoin/sidestake.h +++ b/src/gridcoin/sidestake.h @@ -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 ActiveSideStakeEntries(); + const std::vector ActiveSideStakeEntries(const bool& local_only = false); //! //! \brief Get the current sidestake entry for the specified key string. @@ -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 @@ -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