From a6332179c8e93601d95fe56fd484838401fa8480 Mon Sep 17 00:00:00 2001 From: "James C. Owens" Date: Thu, 28 Sep 2023 17:59:54 -0400 Subject: [PATCH] Initial skeletons for SideStake class --- src/Makefile.am | 2 + src/gridcoin/sidestake.cpp | 104 +++++++++++++++++++++++++++ src/gridcoin/sidestake.h | 141 +++++++++++++++++++++++++++++++++++++ 3 files changed, 247 insertions(+) create mode 100644 src/gridcoin/sidestake.cpp create mode 100644 src/gridcoin/sidestake.h diff --git a/src/Makefile.am b/src/Makefile.am index 4a6883ec0c..949e9cd715 100755 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -130,6 +130,7 @@ GRIDCOIN_CORE_H = \ gridcoin/scraper/scraper.h \ gridcoin/scraper/scraper_net.h \ gridcoin/scraper/scraper_registry.h \ + gridcoin/sidestake.h \ gridcoin/staking/chain_trust.h \ gridcoin/staking/difficulty.h \ gridcoin/staking/exceptions.h \ @@ -259,6 +260,7 @@ GRIDCOIN_CORE_CPP = addrdb.cpp \ gridcoin/scraper/scraper.cpp \ gridcoin/scraper/scraper_net.cpp \ gridcoin/scraper/scraper_registry.cpp \ + gridcoin/sidestake.cpp \ gridcoin/staking/difficulty.cpp \ gridcoin/staking/exceptions.cpp \ gridcoin/staking/kernel.cpp \ diff --git a/src/gridcoin/sidestake.cpp b/src/gridcoin/sidestake.cpp new file mode 100644 index 0000000000..4301ca53a6 --- /dev/null +++ b/src/gridcoin/sidestake.cpp @@ -0,0 +1,104 @@ +// Copyright (c) 2014-2023 The Gridcoin developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or https://opensource.org/licenses/mit-license.php. + +#include "sidestake.h" +#include "node/ui_interface.h" + +using namespace GRC; + +// ----------------------------------------------------------------------------- +// Class: SideStake +// ----------------------------------------------------------------------------- +SideStake::SideStake() + : m_address() + , m_allocation() + , m_timestamp(0) + , m_hash() + , m_previous_hash() + , m_status(SideStakeStatus::UNKNOWN) +{} + +SideStake::SideStake(CBitcoinAddress address, double allocation) + : m_address(address) + , m_allocation(allocation) + , m_timestamp(0) + , m_hash() + , m_previous_hash() + , m_status(SideStakeStatus::UNKNOWN) +{} + +SideStake::SideStake(CBitcoinAddress address, double allocation, int64_t timestamp, uint256 hash) + : m_address(address) + , m_allocation(allocation) + , m_timestamp(timestamp) + , m_hash(hash) + , m_previous_hash() + , m_status(SideStakeStatus::UNKNOWN) +{} + +bool SideStake::WellFormed() const +{ + return m_address.IsValid() && m_allocation >= 0.0 && m_allocation <= 1.0; +} + +std::pair SideStake::KeyValueToString() const +{ + return std::make_pair(m_address.ToString(), StatusToString()); +} + +std::string SideStake::StatusToString() const +{ + return StatusToString(m_status.Value()); +} + +std::string SideStake::StatusToString(const SideStakeStatus& status, const bool& translated) const +{ + if (translated) { + switch(status) { + case SideStakeStatus::UNKNOWN: return _("Unknown"); + case SideStakeStatus::ACTIVE: return _("Active"); + case SideStakeStatus::INACTIVE: return _("Inactive"); + case SideStakeStatus::DELETED: return _("Deleted"); + case SideStakeStatus::MANDATORY: return _("Mandatory"); + case SideStakeStatus::OUT_OF_BOUND: break; + } + + assert(false); // Suppress warning + } else { + // The untranslated versions are really meant to serve as the string equivalent of the enum values. + switch(status) { + case SideStakeStatus::UNKNOWN: return "Unknown"; + case SideStakeStatus::ACTIVE: return "Active"; + case SideStakeStatus::INACTIVE: return "Inactive"; + case SideStakeStatus::DELETED: return "Deleted"; + case SideStakeStatus::MANDATORY: return "Mandatory"; + case SideStakeStatus::OUT_OF_BOUND: break; + } + + assert(false); // Suppress warning + } + + // This will never be reached. Put it in anyway to prevent control reaches end of non-void function warning + // from some compiler versions. + return std::string{}; +} + +bool SideStake::operator==(SideStake b) +{ + bool result = true; + + result &= (m_address == b.m_address); + result &= (m_allocation == b.m_allocation); + result &= (m_timestamp == b.m_timestamp); + result &= (m_hash == b.m_hash); + result &= (m_previous_hash == b.m_previous_hash); + result &= (m_status == b.m_status); + + return result; +} + +bool SideStake::operator!=(SideStake b) +{ + return !(*this == b); +} diff --git a/src/gridcoin/sidestake.h b/src/gridcoin/sidestake.h new file mode 100644 index 0000000000..d32bd7214c --- /dev/null +++ b/src/gridcoin/sidestake.h @@ -0,0 +1,141 @@ +// Copyright (c) 2014-2023 The Gridcoin developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or https://opensource.org/licenses/mit-license.php. + +#ifndef GRIDCOIN_SIDESTAKE_H +#define GRIDCOIN_SIDESTAKE_H + +#include "base58.h" +#include "gridcoin/support/enumbytes.h" +#include "serialize.h" + +namespace GRC { + +enum class SideStakeStatus +{ + UNKNOWN, + ACTIVE, //!< A user specified sidestake that is active + INACTIVE, //!< A user specified sidestake that is inactive + DELETED, //!< A mandatory sidestake that has been deleted by contract + MANDATORY, //!< An active mandatory sidetake by contract + OUT_OF_BOUND +}; + +class SideStake +{ + using Status = EnumByte; + + CBitcoinAddress m_address; + + double m_allocation; + + 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_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. + + //! + //! \brief Initialize an empty, invalid sidestake instance. + //! + SideStake(); + + //! + //! \brief Initialize a sidestake instance with the provided address and allocation. This is used to construct a user + //! specified sidestake. + //! + //! \param address + //! \param allocation + //! + SideStake(CBitcoinAddress address, double allocation); + + //! + //! \brief Initial a sidestake instance with the provided parameters. This form is normally used to construct a + //! mandatory sidestake from a contract. + //! + //! \param address + //! \param allocation + //! \param timestamp + //! \param hash + //! + SideStake(CBitcoinAddress address, double allocation, int64_t timestamp, uint256 hash); + + //! + //! \brief Determine whether a sidestake contains each of the required elements. + //! \return true if the sidestake is well-formed. + //! + bool WellFormed() const; + + //! + //! \brief Provides the sidestake address and status (value) as a pair of strings. + //! \return std::pair of strings + //! + std::pair KeyValueToString() const; + + //! + //! \brief Returns the string representation of the current sidestake status + //! + //! \return Translated string representation of sidestake status + //! + std::string StatusToString() const; + + //! + //! \brief Returns the translated or untranslated string of the input sidestake status + //! + //! \param status. SideStake status + //! \param translated. True for translated, false for not translated. Defaults to true. + //! + //! \return SideStake status string. + //! + std::string StatusToString(const SideStakeStatus& status, const bool& translated = true) const; + + //! + //! \brief Comparison operator overload used in the unit test harness. + //! + //! \param b The right hand side sidestake to compare for equality. + //! + //! \return Equal or not. + //! + + bool operator==(SideStake b); + + //! + //! \brief Comparison operator overload used in the unit test harness. + //! + //! \param b The right hand side sidestake to compare for equality. + //! + //! \return Equal or not. + //! + + bool operator!=(SideStake b); + + ADD_SERIALIZE_METHODS; + + template + inline void SerializationOp(Stream& s, Operation ser_action) + { + READWRITE(m_address); + READWRITE(m_allocation); + READWRITE(m_timestamp); + READWRITE(m_hash); + READWRITE(m_previous_hash); + READWRITE(m_status); + } +}; + +//! +//! \brief The type that defines a shared pointer to a sidestake +//! +typedef std::shared_ptr SideStake_ptr; + +//! +//! \brief A type that either points to some sidestake or does not. +//! +typedef const SideStake_ptr SideStakeOption; + + +} // namespace GRC + +#endif // GRIDCOIN_SIDESTAKE_H