From 1962e64c4c626a915f1a709116217804bfb789e7 Mon Sep 17 00:00:00 2001 From: "James C. Owens" Date: Sat, 21 Oct 2023 19:49:42 -0400 Subject: [PATCH] Sidestake class cleanup. --- src/gridcoin/sidestake.cpp | 46 ++++++++--------- src/gridcoin/sidestake.h | 91 +++++++++++++++------------------- src/miner.cpp | 8 +-- src/qt/sidestaketablemodel.cpp | 18 +++---- src/rpc/mining.cpp | 2 +- 5 files changed, 77 insertions(+), 88 deletions(-) diff --git a/src/gridcoin/sidestake.cpp b/src/gridcoin/sidestake.cpp index 0220a15b87..fd4c9030e6 100644 --- a/src/gridcoin/sidestake.cpp +++ b/src/gridcoin/sidestake.cpp @@ -48,7 +48,7 @@ CBitcoinAddressForStorage::CBitcoinAddressForStorage(CBitcoinAddress address) // Class: SideStake // ----------------------------------------------------------------------------- SideStake::SideStake() - : m_key() + : m_address() , m_allocation() , m_description() , m_timestamp(0) @@ -58,7 +58,7 @@ SideStake::SideStake() {} SideStake::SideStake(CBitcoinAddressForStorage address, double allocation, std::string description) - : m_key(address) + : m_address(address) , m_allocation(allocation) , m_description(description) , m_timestamp(0) @@ -73,7 +73,7 @@ SideStake::SideStake(CBitcoinAddressForStorage address, int64_t timestamp, uint256 hash, SideStakeStatus status) - : m_key(address) + : m_address(address) , m_allocation(allocation) , m_description(description) , m_timestamp(timestamp) @@ -84,17 +84,17 @@ SideStake::SideStake(CBitcoinAddressForStorage address, bool SideStake::WellFormed() const { - return m_key.IsValid() && m_allocation >= 0.0 && m_allocation <= 1.0; + return m_address.IsValid() && m_allocation >= 0.0 && m_allocation <= 1.0; } CBitcoinAddressForStorage SideStake::Key() const { - return m_key; + return m_address; } std::pair SideStake::KeyValueToString() const { - return std::make_pair(m_key.ToString(), StatusToString()); + return std::make_pair(m_address.ToString(), StatusToString()); } std::string SideStake::StatusToString() const @@ -138,7 +138,7 @@ bool SideStake::operator==(SideStake b) { bool result = true; - result &= (m_key == b.m_key); + result &= (m_address == b.m_address); result &= (m_allocation == b.m_allocation); result &= (m_timestamp == b.m_timestamp); result &= (m_hash == b.m_hash); @@ -166,13 +166,13 @@ SideStakePayload::SideStakePayload(uint32_t version) } SideStakePayload::SideStakePayload(const uint32_t version, - CBitcoinAddressForStorage key, - double value, + CBitcoinAddressForStorage address, + double allocation, std::string description, SideStakeStatus status) : IContractPayload() , m_version(version) - , m_entry(SideStake(key, value, description, 0, uint256{}, status)) + , m_entry(SideStake(address, allocation, description, 0, uint256{}, status)) { } @@ -325,7 +325,7 @@ void SideStakeRegistry::AddDelete(const ContractContext& ctx) LOCK(cs_lock); - auto sidestake_entry_pair_iter = m_sidestake_entries.find(payload.m_entry.m_key); + auto sidestake_entry_pair_iter = m_sidestake_entries.find(payload.m_entry.m_address); SideStake_ptr current_sidestake_entry_ptr = nullptr; @@ -343,12 +343,12 @@ void SideStakeRegistry::AddDelete(const ContractContext& ctx) } LogPrint(LogFlags::CONTRACT, "INFO: %s: SideStake entry add/delete: contract m_version = %u, payload " - "m_version = %u, key = %s, value = %f, m_timestamp = %" PRId64 ", " + "m_version = %u, address = %s, allocation = %f, m_timestamp = %" PRId64 ", " "m_hash = %s, m_previous_hash = %s, m_status = %s", __func__, ctx->m_version, payload.m_version, - payload.m_entry.m_key.ToString(), + payload.m_entry.m_address.ToString(), payload.m_entry.m_allocation, payload.m_entry.m_timestamp, payload.m_entry.m_hash.ToString(), @@ -364,13 +364,13 @@ void SideStakeRegistry::AddDelete(const ContractContext& ctx) "the SideStake entry db record already exists. This can be expected on a restart " "of the wallet to ensure multiple contracts in the same block get stored/replayed.", __func__, - historical.m_key.ToString(), + historical.m_address.ToString(), historical.m_allocation, historical.m_hash.GetHex()); } // Finally, insert the new SideStake entry (payload) smart pointer into the m_sidestake_entries map. - m_sidestake_entries[payload.m_entry.m_key] = m_sidestake_db.find(ctx.m_tx.GetHash())->second; + m_sidestake_entries[payload.m_entry.m_address] = m_sidestake_db.find(ctx.m_tx.GetHash())->second; return; } @@ -380,7 +380,7 @@ void SideStakeRegistry::NonContractAdd(const SideStake& sidestake, const bool& s 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); + m_local_sidestake_entries[sidestake.m_address] = std::make_shared(sidestake); if (save_to_file) { SaveLocalSideStakesToConfig(); @@ -421,12 +421,12 @@ void SideStakeRegistry::Revert(const ContractContext& ctx) // resurrect. LOCK(cs_lock); - auto entry_to_revert = m_sidestake_entries.find(payload->m_entry.m_key); + auto entry_to_revert = m_sidestake_entries.find(payload->m_entry.m_address); if (entry_to_revert == m_sidestake_entries.end()) { error("%s: The SideStake entry for key %s to revert was not found in the SideStake entry map.", __func__, - entry_to_revert->second->m_key.ToString()); + entry_to_revert->second->m_address.ToString()); // If there is no record in the current m_sidestake_entries map, then there is nothing to do here. This // should not occur. @@ -434,13 +434,13 @@ void SideStakeRegistry::Revert(const ContractContext& ctx) } // If this is not a null hash, then there will be a prior entry to resurrect. - CBitcoinAddressForStorage key = entry_to_revert->second->m_key; + CBitcoinAddressForStorage key = entry_to_revert->second->m_address; uint256 resurrect_hash = entry_to_revert->second->m_previous_hash; // Revert the ADD or REMOVE action. Unlike the beacons, this is symmetric. if (ctx->m_action == ContractAction::ADD || ctx->m_action == ContractAction::REMOVE) { // Erase the record from m_sidestake_entries. - if (m_sidestake_entries.erase(payload->m_entry.m_key) == 0) { + if (m_sidestake_entries.erase(payload->m_entry.m_address) == 0) { error("%s: The SideStake entry to erase during a SideStake entry revert for key %s was not found.", __func__, key.ToString()); @@ -474,7 +474,7 @@ void SideStakeRegistry::Revert(const ContractContext& ctx) // Resurrect the entry prior to the reverted one. It is safe to use the bracket form here, because of the protection // of the logic above. There cannot be any entry in m_sidestake_entries with that key value left if we made it here. - m_sidestake_entries[resurrect_entry->second->m_key] = resurrect_entry->second; + m_sidestake_entries[resurrect_entry->second->m_address] = resurrect_entry->second; } } @@ -576,7 +576,7 @@ void SideStakeRegistry::LoadLocalSideStakesFromConfig() // If -sidestakeaddresses and -sidestakeallocations is set in either the config file or the r-w settings file // and the settings are not empty and they are the same size, this will take precedence over the multiple entry - // -sidestake format. Note that -descriptions is optional; however, if descriptions is used, the number must + // -sidestake format. Note that -descriptions is optional; however, if descriptions is used, the size must // match the other two if present. std::vector addresses; std::vector allocations; @@ -738,7 +738,7 @@ bool SideStakeRegistry::SaveLocalSideStakesToConfig() separator = ","; } - addresses += separator + iter.second->m_key.ToString(); + addresses += separator + iter.second->m_address.ToString(); allocations += separator + ToString(iter.second->m_allocation * 100.0); descriptions += separator + iter.second->m_description; diff --git a/src/gridcoin/sidestake.h b/src/gridcoin/sidestake.h index dccb6b216a..6705b1c527 100644 --- a/src/gridcoin/sidestake.h +++ b/src/gridcoin/sidestake.h @@ -15,6 +15,10 @@ namespace GRC { +//! +//! \brief The CBitcoinAddressForStorage class. This is a very small extension of the CBitcoinAddress class that +//! provides serialization/deserialization. +//! class CBitcoinAddressForStorage : public CBitcoinAddress { public: @@ -65,19 +69,19 @@ class SideStake //! using Status = EnumByte; - CBitcoinAddressForStorage m_key; //!< The key here is the Gridcoin Address of the sidestake destination. + CBitcoinAddressForStorage m_address; //!< The Gridcoin Address of the sidestake destination. - double m_allocation; //!< The allocation is a double precision floating point between 0.0 and 1.0 inclusive + double m_allocation; //!< The allocation is a double precision floating point between 0.0 and 1.0 inclusive - std::string m_description; //!< The description of the sidestake (optional) + std::string m_description; //!< The description of the sidestake (optional) - int64_t m_timestamp; //!< Time of the sidestake contract transaction. + int64_t m_timestamp; //!< Time of the sidestake contract transaction. - uint256 m_hash; //!< The hash of the transaction that contains a mandatory sidestake. + uint256 m_hash; //!< The hash of the transaction that contains a mandatory sidestake. - uint256 m_previous_hash; //!< The m_hash of the previous mandatory sidestake allocation with the same address. + uint256 m_previous_hash; //!< The m_hash of the previous mandatory sidestake allocation with the same address. - Status m_status; //!< The status of the sidestake. It is of type int instead of enum for serialization. + Status m_status; //!< The status of the sidestake. It is of type int instead of enum for serialization. //! //! \brief Initialize an empty, invalid sidestake instance. @@ -115,7 +119,8 @@ class SideStake //! \param hash //! \param status //! - SideStake(CBitcoinAddressForStorage address, double allocation, std::string description, int64_t timestamp, uint256 hash, SideStakeStatus status); + SideStake(CBitcoinAddressForStorage address, double allocation, std::string description, int64_t timestamp, + uint256 hash, SideStakeStatus status); //! //! \brief Determine whether a sidestake contains each of the required elements. @@ -124,7 +129,7 @@ class SideStake bool WellFormed() const; //! - //! \brief This is the standardized method that returns the key value for the sidestake entry (for + //! \brief This is the standardized method that returns the key value (in this case the address) for the sidestake entry (for //! the registry_db.h template.) //! //! \return CBitcoinAddress key value for the sidestake entry @@ -132,7 +137,7 @@ class SideStake CBitcoinAddressForStorage Key() const; //! - //! \brief Provides the sidestake address and status (value) as a pair of strings. + //! \brief Provides the sidestake address and status as a pair of strings. //! \return std::pair of strings //! std::pair KeyValueToString() const; @@ -161,7 +166,6 @@ class SideStake //! //! \return Equal or not. //! - bool operator==(SideStake b); //! @@ -171,7 +175,6 @@ class SideStake //! //! \return Equal or not. //! - bool operator!=(SideStake b); ADD_SERIALIZE_METHODS; @@ -179,7 +182,7 @@ class SideStake template inline void SerializationOp(Stream& s, Operation ser_action) { - READWRITE(m_key); + READWRITE(m_address); READWRITE(m_allocation); READWRITE(m_description); READWRITE(m_timestamp); @@ -195,23 +198,9 @@ class SideStake typedef std::shared_ptr SideStake_ptr; //! -//! \brief A type that either points to some sidestake or does not. -//! -//typedef const SideStake_ptr SideStakeOption; - -//! -//! \brief The body of a sidestake entry contract. Note that this body is bimodal. It -//! supports both the personality of the "LegacyPayload", and also the new native -//! sidestakeEntry format. In the Contract::Body::ConvertFromLegacy call, by the time -//! this call has been reached, the contract will have already been deserialized. -//! This will follow the legacy mode. For contracts at version 3+, the -//! Contract::SharePayload() will NOT call the ConvertFromLegacy. Note that because -//! the existing legacyPayloads are not versioned, the deserialization of -//! the payload first (de)serializes m_key, which is guaranteed to exist in either -//! legacy or native. If the key is empty, then payload v2+ is being deserialized -//! and the m_version and m_value are (de)serialized. This is ugly -//! but necessary to deal with the unversioned Legacy Payloads and maintain -//! compatibility. +//! \brief The body of a sidestake entry contract. This payload does NOT support +//! legacy payload formatting, as this contract/payload type is introduced after +//! legacy payloads are retired. //! class SideStakePayload : public IContractPayload { @@ -240,15 +229,15 @@ class SideStakePayload : public IContractPayload SideStakePayload(uint32_t version = CURRENT_VERSION); //! - //! \brief Initialize a sidestakeEntryPayload from a sidestake entry constructed from - //! string key and value. Not to be used for version 1 payloads. Will assert. Does NOT - //! initialize hash fields. + //! \brief Initialize a sidestakeEntryPayload from a sidestake address, allocation, + //! description, and status. //! - //! \param key. Key string for the sidestake entry - //! \param value. Value string for the sidestake entry + //! \param address. Address for the sidestake entry + //! \param allocation. Allocation for the sidestake entry + //! \param description. Description string for the sidstake entry //! \param status. Status of the sidestake entry //! - SideStakePayload(const uint32_t version, CBitcoinAddressForStorage key, double value, + SideStakePayload(const uint32_t version, CBitcoinAddressForStorage address, double allocation, std::string description, SideStakeStatus status); //! @@ -303,7 +292,7 @@ class SideStakePayload : public IContractPayload "m_entry.StatusToString() = %s", __func__, valid, - m_entry.m_key.ToString(), + m_entry.m_address.ToString(), m_entry.m_allocation, m_entry.StatusToString() ); @@ -319,7 +308,7 @@ class SideStakePayload : public IContractPayload //! std::string LegacyKeyString() const override { - return m_entry.m_key.ToString(); + return m_entry.m_address.ToString(); } //! @@ -366,7 +355,7 @@ class SideStakeRegistry : public IContractHandler //! sidestake entry db. This must be incremented when implementing format changes to the sidestake //! entries to force a reinit. //! - //! Version 1: TBD. + //! Version 1: 5.4.5.5+ //! SideStakeRegistry() : m_sidestake_db(1) @@ -374,7 +363,7 @@ class SideStakeRegistry : public IContractHandler }; //! - //! \brief The type that keys sidestake entries by their key strings. Note that the entries + //! \brief The type that keys sidestake entries by their addresses. Note that the entries //! in this map are actually smart shared pointer wrappers, so that the same actual object //! can be held by both this map and the historical map without object duplication. //! @@ -415,23 +404,23 @@ class SideStakeRegistry : public IContractHandler const std::vector ActiveSideStakeEntries(const bool& local_only, const bool& include_zero_alloc); //! - //! \brief Get the current sidestake entry for the specified key string. + //! \brief Get the current sidestake entry for the specified address. //! - //! \param key The key string of the sidestake entry. + //! \param key The address of the sidestake entry. //! \param local_only If true causes Try to only check the local sidestake map. Defaults to false. //! - //! \return A vector of smart pointers to entries matching the provided key (address). Up to two elements + //! \return A vector of smart pointers to entries matching the provided address. Up to two elements //! are returned, mandatory entry first, unless local only boolean is set true. //! std::vector Try(const CBitcoinAddressForStorage& key, const bool& local_only = false) const; //! - //! \brief Get the current sidestake entry for the specified key string if it has a status of ACTIVE or MANDATORY. + //! \brief Get the current sidestake entry for the specified address if it has a status of ACTIVE or MANDATORY. //! - //! \param key The key string of the sidestake entry. + //! \param key The address of the sidestake entry. //! \param local_only If true causes Try to only check the local sidestake map. Defaults to false. //! - //! \return A vector of smart pointers to entries matching the provided key (address) that are in status of + //! \return A vector of smart pointers to entries matching the provided address that are in status of //! MANDATORY or ACTIVE. Up to two elements are returned, mandatory entry first, unless local only boolean //! is set true. //! @@ -440,7 +429,7 @@ class SideStakeRegistry : public IContractHandler //! //! \brief Destroy the contract handler state in case of an error in loading //! the sidestake entry registry state from LevelDB to prepare for reload from contract - //! replay. This is not used for sidestake entries, unless -clearSideStakehistory is specified + //! replay. This is not used for sidestake entries, unless -clearsidestakehistory is specified //! as a startup argument, because contract replay storage and full reversion has //! been implemented for sidestake entries. //! @@ -460,7 +449,7 @@ class SideStakeRegistry : public IContractHandler //! //! \brief Determine whether a sidestake entry contract is valid including block context. This is used //! in ConnectBlock. Note that for sidestake entries this simply calls Validate as there is no - //! block level specific validation to be done. + //! block level specific validation to be done at the current time. //! //! \param ctx ContractContext containing the sidestake entry data to validate. //! \param DoS Misbehavior score out. @@ -470,7 +459,7 @@ class SideStakeRegistry : public IContractHandler bool BlockValidate(const ContractContext& ctx, int& DoS) const override; //! - //! \brief Allows local (voluntary) sidestakes to be added to the in-memory map and not persisted to + //! \brief Allows local (voluntary) sidestakes to be added to the in-memory local map and not persisted to //! the registry db. //! //! \param SideStake object to add @@ -488,7 +477,7 @@ class SideStakeRegistry : public IContractHandler void Add(const ContractContext& ctx) override; //! - //! \brief Provides for deletion of local (voluntary) sidestakes from the in-memory map that are not persisted + //! \brief Provides for deletion of local (voluntary) sidestakes from the in-memory local map that are not persisted //! to the registry db. Deletion is by the map key (CBitcoinAddress). //! //! \param address @@ -506,7 +495,7 @@ class SideStakeRegistry : public IContractHandler //! //! \brief Revert the registry state for the sidestake entry to the state prior - //! to this ContractContext application. This is typically an issue + //! to this ContractContext application. This is typically used //! during reorganizations, where blocks are disconnected. //! //! \param ctx References the sidestake entry contract and associated context. diff --git a/src/miner.cpp b/src/miner.cpp index c1225391a0..43a4f53fcd 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -922,11 +922,11 @@ void SplitCoinStakeOutput(CBlock &blocknew, int64_t &nReward, bool &fEnableStake (iterSideStake != vSideStakeAlloc.end()) && (nOutputsUsed <= nMaxSideStakeOutputs); ++iterSideStake) { - CBitcoinAddress& address = iterSideStake->get()->m_key; + CBitcoinAddress& address = iterSideStake->get()->m_address; if (!address.IsValid()) { LogPrintf("WARN: SplitCoinStakeOutput: ignoring sidestake invalid address %s.", - iterSideStake->get()->m_key.ToString()); + iterSideStake->get()->m_address.ToString()); continue; } @@ -936,7 +936,7 @@ void SplitCoinStakeOutput(CBlock &blocknew, int64_t &nReward, bool &fEnableStake { LogPrintf("WARN: SplitCoinStakeOutput: distribution %f too small to address %s.", CoinToDouble(nReward * iterSideStake->get()->m_allocation), - iterSideStake->get()->m_key.ToString() + iterSideStake->get()->m_address.ToString() ); continue; } @@ -978,7 +978,7 @@ void SplitCoinStakeOutput(CBlock &blocknew, int64_t &nReward, bool &fEnableStake LogPrintf("SplitCoinStakeOutput: create sidestake UTXO %i value %f to address %s", nOutputsUsed, CoinToDouble(nReward * iterSideStake->get()->m_allocation), - iterSideStake->get()->m_key.ToString() + iterSideStake->get()->m_address.ToString() ); dSumAllocation += iterSideStake->get()->m_allocation; nRemainingStakeOutputValue -= nSideStake; diff --git a/src/qt/sidestaketablemodel.cpp b/src/qt/sidestaketablemodel.cpp index 9beabbdf78..85d7ce9b9c 100644 --- a/src/qt/sidestaketablemodel.cpp +++ b/src/qt/sidestaketablemodel.cpp @@ -36,7 +36,7 @@ bool SideStakeLessThan::operator()(const GRC::SideStake& left, const GRC::SideSt switch (static_cast(m_column)) { case SideStakeTableModel::Address: - return pLeft->m_key < pRight->m_key; + return pLeft->m_address < pRight->m_address; case SideStakeTableModel::Allocation: return pLeft->m_allocation < pRight->m_allocation; case SideStakeTableModel::Description: @@ -131,7 +131,7 @@ QVariant SideStakeTableModel::data(const QModelIndex &index, int role) const if (role == Qt::DisplayRole || role == Qt::EditRole) { switch (column) { case Address: - return QString::fromStdString(rec->m_key.ToString()); + return QString::fromStdString(rec->m_address.ToString()); case Allocation: return rec->m_allocation * 100.0; case Description: @@ -182,7 +182,7 @@ bool SideStakeTableModel::setData(const QModelIndex &index, const QVariant &valu address.SetString(value.toString().toStdString()); - if (rec->m_key == address) { + if (rec->m_address == address) { m_edit_status = NO_CHANGES; return false; } else if (!address.IsValid()) { @@ -210,7 +210,7 @@ bool SideStakeTableModel::setData(const QModelIndex &index, const QVariant &valu GRC::SideStake orig_sidestake = *rec; for (const auto& entry : registry.ActiveSideStakeEntries(false, true)) { - if (entry->m_key == orig_sidestake.m_key) { + if (entry->m_address == orig_sidestake.m_address) { continue; } @@ -233,10 +233,10 @@ bool SideStakeTableModel::setData(const QModelIndex &index, const QVariant &valu } // Delete the original sidestake - registry.NonContractDelete(orig_sidestake.m_key, false); + registry.NonContractDelete(orig_sidestake.m_address, false); // Add back the sidestake with the modified allocation - registry.NonContractAdd(GRC::SideStake(orig_sidestake.m_key, + registry.NonContractAdd(GRC::SideStake(orig_sidestake.m_address, value.toDouble() / 100.0, orig_sidestake.m_description, int64_t {0}, @@ -264,10 +264,10 @@ bool SideStakeTableModel::setData(const QModelIndex &index, const QVariant &valu GRC::SideStake orig_sidestake = *rec; // Delete the original sidestake - registry.NonContractDelete(orig_sidestake.m_key, false); + registry.NonContractDelete(orig_sidestake.m_address, false); // Add back the sidestake with the modified allocation - registry.NonContractAdd(GRC::SideStake(orig_sidestake.m_key, + registry.NonContractAdd(GRC::SideStake(orig_sidestake.m_address, orig_sidestake.m_allocation, san_value, int64_t {0}, @@ -400,7 +400,7 @@ bool SideStakeTableModel::removeRows(int row, int count, const QModelIndex &pare return false; } - GRC::GetSideStakeRegistry().NonContractDelete(rec->m_key); + GRC::GetSideStakeRegistry().NonContractDelete(rec->m_address); updateSideStakeTableModel(); diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index dc0cc887ce..55cd761167 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -117,7 +117,7 @@ UniValue getstakinginfo(const UniValue& params, bool fHelp) // sidestakes are always included. for (const auto& alloc : vSideStakeAlloc) { - sidestakingalloc.pushKV("address", alloc->m_key.ToString()); + sidestakingalloc.pushKV("address", alloc->m_address.ToString()); sidestakingalloc.pushKV("allocation_pct", alloc->m_allocation * 100); sidestakingalloc.pushKV("status", alloc->StatusToString());