Skip to content

Commit

Permalink
dynamically calculates how many blocks the chain is producing per day
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro-at-decenomy committed Jul 18, 2024
1 parent 596da5b commit 8acbd85
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,8 @@ UniValue getrewardsinfo(const JSONRPCRequest& request)
const auto& params = Params();
const auto& consensus = params.GetConsensus();

const auto tip = chainActive.Tip();
const auto nHeight = tip->nHeight;
const auto pTip = chainActive.Tip();
const auto nHeight = pTip->nHeight;

// Fetch consensus parameters
const auto nTargetSpacing = consensus.nTargetSpacing;
Expand All @@ -865,15 +865,28 @@ UniValue getrewardsinfo(const JSONRPCRequest& request)
const auto nMNReward = CMasternode::GetMasternodePayment(nHeight);
const auto nStakeReward = nBlockValue - nMNReward;

const auto nBlocksPerDay = DAY_IN_SECONDS / nTargetSpacing;
const auto nBlocksPerWeek = WEEK_IN_SECONDS / nTargetSpacing;
const auto nBlocksPerMonth = MONTH_IN_SECONDS / nTargetSpacing;
const auto nBlocksPerYear = YEAR_IN_SECONDS / nTargetSpacing;
int64_t nBlocksPerDay = DAY_IN_SECONDS / nTargetSpacing;
CBlockIndex* BlockReading = pTip;

if(nHeight > nBlocksPerDay) {
for (unsigned int i = 0; BlockReading && BlockReading->nHeight > 0; i++) {
if(BlockReading->nTime < (pTip->nTime - DAY_IN_SECONDS)) {
nBlocksPerDay = i;
break;
}

BlockReading = BlockReading->pprev;
}
}

int64_t nBlocksPerWeek = nBlocksPerDay * 7;
int64_t nBlocksPerMonth = nBlocksPerDay * 30;
int64_t nBlocksPerYear = nBlocksPerDay * 365;

// Fetch the network generated hashes per second
const auto nBlocks = static_cast<int>(nTargetTimespan / nTargetSpacing);
const auto startBlock = chainActive[nHeight - std::min(nBlocks, nHeight)];
const auto endBlock = tip;
const auto endBlock = pTip;
const auto nTimeDiff = endBlock->GetBlockTime() - startBlock->GetBlockTime();
const auto nWorkDiff = endBlock->nChainWork - startBlock->nChainWork;
const auto nNetworkHashPS = static_cast<int64_t>(nWorkDiff.getdouble() / nTimeDiff);
Expand All @@ -887,7 +900,7 @@ UniValue getrewardsinfo(const JSONRPCRequest& request)
// Calculate how many coins are allocated in the entire staking algorithm
const auto nStakedCoins = static_cast<double>(nNetworkHashPS * nTimeSlotLength * 100);
const auto nSmoothStakedCoins = static_cast<double>(nSmoothNetworkHashPS * nTimeSlotLength * 100);
const auto nStakingAllocation = static_cast<double>(nSmoothStakedCoins) / tip->nMoneySupply.get();
const auto nStakingAllocation = static_cast<double>(nSmoothStakedCoins) / pTip->nMoneySupply.get();
const auto nYearlyStakingRewards = nStakeReward * nBlocksPerYear;
auto nStakingROI = nYearlyStakingRewards / nStakedCoins;
auto nSmoothStakingROI = nYearlyStakingRewards / nSmoothStakedCoins;
Expand All @@ -897,7 +910,7 @@ UniValue getrewardsinfo(const JSONRPCRequest& request)
const auto nMNNextWeekCollateral = CMasternode::GetNextWeekMasternodeCollateral();
const auto nMNEnabled = mnodeman.CountEnabled();
const auto nMNCoins = nMNCollateral * nMNEnabled;
const auto nMNAllocation = static_cast<double>(nMNCoins) / tip->nMoneySupply.get();
const auto nMNAllocation = static_cast<double>(nMNCoins) / pTip->nMoneySupply.get();

const auto nTotalAllocationCoins = nSmoothStakedCoins + nMNCoins;
const auto nTotalAllocation = nStakingAllocation + nMNAllocation;
Expand Down

0 comments on commit 8acbd85

Please sign in to comment.