Skip to content

Commit

Permalink
Merge pull request #2740 from jamescowens/improve_allocation_class
Browse files Browse the repository at this point in the history
util: Improve allocation class
  • Loading branch information
jamescowens authored Feb 18, 2024
2 parents baa544b + 4c14a71 commit 871aad2
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 78 deletions.
148 changes: 148 additions & 0 deletions src/gridcoin/sidestake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ Allocation::Allocation(const Fraction& f)
: Fraction(f)
{}

Allocation::Allocation(const int64_t& numerator, const int64_t& denominator)
: Fraction(numerator, denominator)
{}

Allocation::Allocation(const int64_t& numerator, const int64_t& denominator, const bool& simplify)
: Fraction(numerator, denominator, simplify)
{}

CAmount Allocation::ToCAmount() const
{
return GetNumerator() / GetDenominator();
Expand All @@ -58,6 +66,146 @@ double Allocation::ToPercent() const
return ToDouble() * 100.0;
}

Allocation Allocation::operator+(const Allocation& rhs) const
{
return static_cast<Allocation>(Fraction::operator+(rhs));
}

Allocation Allocation::operator+(const int64_t& rhs) const
{
return static_cast<Allocation>(Fraction::operator+(rhs));
}

Allocation Allocation::operator-(const Allocation& rhs) const
{
return static_cast<Allocation>(Fraction::operator-(rhs));
}

Allocation Allocation::operator-(const int64_t& rhs) const
{
return static_cast<Allocation>(Fraction::operator-(rhs));
}

Allocation Allocation::operator*(const Allocation& rhs) const
{
return static_cast<Allocation>(Fraction::operator*(rhs));
}

Allocation Allocation::operator*(const int64_t& rhs) const
{
return static_cast<Allocation>(Fraction::operator*(rhs));
}

Allocation Allocation::operator/(const Allocation& rhs) const
{
return static_cast<Allocation>(Fraction::operator/(rhs));
}

Allocation Allocation::operator/(const int64_t& rhs) const
{
return static_cast<Allocation>(Fraction::operator/(rhs));
}

Allocation Allocation::operator+=(const Allocation& rhs)
{
return static_cast<Allocation>(Fraction::operator+=(rhs));
}

Allocation Allocation::operator+=(const int64_t& rhs)
{
return static_cast<Allocation>(Fraction::operator+=(rhs));
}

Allocation Allocation::operator-=(const Allocation& rhs)
{
return static_cast<Allocation>(Fraction::operator-=(rhs));
}

Allocation Allocation::operator-=(const int64_t& rhs)
{
return static_cast<Allocation>(Fraction::operator-=(rhs));
}

Allocation Allocation::operator*=(const Allocation& rhs)
{
return static_cast<Allocation>(Fraction::operator*=(rhs));
}

Allocation Allocation::operator*=(const int64_t& rhs)
{
return static_cast<Allocation>(Fraction::operator*=(rhs));
}

Allocation Allocation::operator/=(const Allocation& rhs)
{
return static_cast<Allocation>(Fraction::operator/=(rhs));
}

Allocation Allocation::operator/=(const int64_t& rhs)
{
return static_cast<Allocation>(Fraction::operator/=(rhs));
}

bool Allocation::operator==(const Allocation& rhs) const
{
return Fraction::operator==(rhs);
}

bool Allocation::operator!=(const Allocation& rhs) const
{
return Fraction::operator!=(rhs);
}

bool Allocation::operator<=(const Allocation& rhs) const
{
return Fraction::operator<=(rhs);
}

bool Allocation::operator>=(const Allocation& rhs) const
{
return Fraction::operator>=(rhs);
}

bool Allocation::operator<(const Allocation& rhs) const
{
return Fraction::operator<(rhs);
}

bool Allocation::operator>(const Allocation& rhs) const
{
return Fraction::operator>(rhs);
}

bool Allocation::operator==(const int64_t& rhs) const
{
return Fraction::operator==(rhs);
}

bool Allocation::operator!=(const int64_t& rhs) const
{
return Fraction::operator!=(rhs);
}

bool Allocation::operator<=(const int64_t& rhs) const
{
return Fraction::operator<=(rhs);
}

bool Allocation::operator>=(const int64_t& rhs) const
{
return Fraction::operator>=(rhs);
}

bool Allocation::operator<(const int64_t& rhs) const
{
return Fraction::operator<(rhs);
}

bool Allocation::operator>(const int64_t& rhs) const
{
return Fraction::operator>(rhs);
}

// -----------------------------------------------------------------------------
// Class: LocalSideStake
// -----------------------------------------------------------------------------
Expand Down
46 changes: 46 additions & 0 deletions src/gridcoin/sidestake.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ class Allocation : public Fraction
//!
Allocation(const Fraction& f);

//!
//! \brief Initialize an allocation directly from specifying a numerator and denominator
//! \param numerator
//! \param denominator
//!
Allocation(const int64_t& numerator, const int64_t& denominator);

//!
//! \brief Initialize an allocation directly from specifying a numerator and denominator, specifying the simplification
//! directive.
//!
//! \param numerator
//! \param denominator
//! \param simplify
//!
Allocation(const int64_t& numerator, const int64_t& denominator, const bool& simplify);

//!
//! \brief Allocations extend the Fraction class and can also represent the result of the allocation constructed fraction
//! and the result of the multiplication of that fraction times the reward, which is in CAmount (i.e. int64_t).
Expand All @@ -57,6 +74,35 @@ class Allocation : public Fraction
//! \return double percent representation of the allocation fraction.
//!
double ToPercent() const;

Allocation operator+(const Allocation& rhs) const;
Allocation operator+(const int64_t& rhs) const;
Allocation operator-(const Allocation& rhs) const;
Allocation operator-(const int64_t& rhs) const;
Allocation operator*(const Allocation& rhs) const;
Allocation operator*(const int64_t& rhs) const;
Allocation operator/(const Allocation& rhs) const;
Allocation operator/(const int64_t& rhs) const;
Allocation operator+=(const Allocation& rhs);
Allocation operator+=(const int64_t& rhs);
Allocation operator-=(const Allocation& rhs);
Allocation operator-=(const int64_t& rhs);
Allocation operator*=(const Allocation& rhs);
Allocation operator*=(const int64_t& rhs);
Allocation operator/=(const Allocation& rhs);
Allocation operator/=(const int64_t& rhs);
bool operator==(const Allocation& rhs) const;
bool operator!=(const Allocation& rhs) const;
bool operator<=(const Allocation& rhs) const;
bool operator>=(const Allocation& rhs) const;
bool operator<(const Allocation& rhs) const;
bool operator>(const Allocation& rhs) const;
bool operator==(const int64_t& rhs) const;
bool operator!=(const int64_t& rhs) const;
bool operator<=(const int64_t& rhs) const;
bool operator>=(const int64_t& rhs) const;
bool operator<(const int64_t& rhs) const;
bool operator>(const int64_t& rhs) const;
};

//!
Expand Down
6 changes: 3 additions & 3 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ void SplitCoinStakeOutput(CBlock &blocknew, int64_t &nReward, bool &fEnableStake
if (allocation * nReward < CENT)
{
LogPrintf("WARN: SplitCoinStakeOutput: distribution %f too small to address %s.",
CoinToDouble(static_cast<GRC::Allocation>(allocation * nReward).ToCAmount()),
CoinToDouble((allocation * nReward).ToCAmount()),
address.ToString()
);
continue;
Expand Down Expand Up @@ -998,7 +998,7 @@ void SplitCoinStakeOutput(CBlock &blocknew, int64_t &nReward, bool &fEnableStake

// For allocations ending less than 100% assign using sidestake allocation.
if (SumAllocation + allocation < 1)
nSideStake = static_cast<GRC::Allocation>(allocation * nReward).ToCAmount();
nSideStake = (allocation * nReward).ToCAmount();
// We need to handle the final sidestake differently in the case it brings the total allocation up to 100%,
// because testing showed in corner cases the output return to the staking address could be off by one Halford.
else if (SumAllocation + allocation == 1)
Expand All @@ -1010,7 +1010,7 @@ void SplitCoinStakeOutput(CBlock &blocknew, int64_t &nReward, bool &fEnableStake

LogPrintf("SplitCoinStakeOutput: create sidestake UTXO %i value %f to address %s",
nOutputsUsed,
CoinToDouble(static_cast<GRC::Allocation>(allocation * nReward).ToCAmount()),
CoinToDouble((allocation * nReward).ToCAmount()),
address.ToString()
);
SumAllocation += allocation;
Expand Down
2 changes: 1 addition & 1 deletion src/test/gridcoin/sidestake_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ BOOST_AUTO_TEST_CASE(sidestake_Allocation_multiplication_and_derivation_of_alloc

CAmount max_accrual = 16384 * COIN;

CAmount actual_output = static_cast<GRC::Allocation>(allocation * max_accrual).ToCAmount();
CAmount actual_output = (allocation * max_accrual).ToCAmount();
BOOST_CHECK_EQUAL(actual_output, int64_t {1638236160000});
}

Expand Down
Loading

0 comments on commit 871aad2

Please sign in to comment.