Skip to content

Commit

Permalink
Initial skeletons for SideStake class
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Sep 28, 2023
1 parent 7548e6c commit a633217
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down
104 changes: 104 additions & 0 deletions src/gridcoin/sidestake.cpp
Original file line number Diff line number Diff line change
@@ -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<std::string, std::string> 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);
}
141 changes: 141 additions & 0 deletions src/gridcoin/sidestake.h
Original file line number Diff line number Diff line change
@@ -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<SideStakeStatus>;

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<std::string, std::string> 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 <typename Stream, typename Operation>
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> 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

0 comments on commit a633217

Please sign in to comment.