Skip to content

Commit

Permalink
Merge pull request digibyte#33 from SmartArray/refactor/subsidy
Browse files Browse the repository at this point in the history
Refactor Subsidy Code
  • Loading branch information
JaredTate authored Apr 27, 2021
2 parents 5799e15 + 55ad95e commit c554b8c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 68 deletions.
141 changes: 73 additions & 68 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1171,76 +1171,81 @@ bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const CBlockIndex* pindex
return ReadRawBlockFromDisk(block, block_pos, message_start);
}

CAmount GetDGBSubsidy(int nHeight, const Consensus::Params& consensusParams) {
// thanks to RealSolid & WDC for helping out with this code
CAmount qSubsidy;

if (nHeight < consensusParams.alwaysUpdateDiffChangeTarget)
{
qSubsidy = 8000*COIN;
int blocks = nHeight - consensusParams.nDiffChangeTarget;
int weeks = (blocks / consensusParams.patchBlockRewardDuration)+1;
//decrease reward by 0.5% every 10080 blocks
for(int i = 0; i < weeks; i++) qSubsidy -= (qSubsidy/200);
}
else if(nHeight<consensusParams.workComputationChangeTarget)
{
qSubsidy = 2459*COIN;
int blocks = nHeight - consensusParams.alwaysUpdateDiffChangeTarget;
int weeks = (blocks / consensusParams.patchBlockRewardDuration2)+1;
//decrease reward by 1% every month
for(int i = 0; i < weeks; i++) qSubsidy -= (qSubsidy/100);
}
else
{
//hard fork point: 1.43M
//subsidy at hard fork: 2157
//monthly decay factor: 98884/100000
//last block number: 41668798
//expected years after hard fork: 19.1395

qSubsidy = 2157*COIN/2;
int64_t blocks = nHeight - consensusParams.workComputationChangeTarget;
int64_t months = blocks*15/(3600*24*365/12);
for(int64_t i = 0; i < months; i++)
{
qSubsidy*=98884;
qSubsidy/=100000;
}
}

return qSubsidy;

}

CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
{
CAmount nSubsidy = COIN;
LogPrintf("block height for reward is %d\n", nHeight);
if(nHeight < consensusParams.nDiffChangeTarget) {
//this is pre-patch, reward is 8000.
nSubsidy = 8000 * COIN;

if(nHeight < 1440) //1440
{
nSubsidy = 72000 * COIN;
}
else if(nHeight < 5760) //5760
{
nSubsidy = 16000 * COIN;
}

} else {
//patch takes effect after 67,200 blocks solved
nSubsidy = GetDGBSubsidy(nHeight, consensusParams);
}

//make sure the reward is at least 1 DGB
if(nSubsidy < COIN) {
nSubsidy = COIN;
}

return nSubsidy;
CAmount nSubsidy = COIN;
LogPrintf("block height for reward is %d\n", nHeight);

if (nHeight < consensusParams.nDiffChangeTarget) { // < 67200
if (nHeight < 1440)
{
// (Period I)
nSubsidy = 72000 * COIN;
}
else if (nHeight < 5760)
{
// (Period II)
nSubsidy = 16000 * COIN;
}
else
{
// (Period III)
// This is pre-patch, reward is 8000.
nSubsidy = 8000 * COIN;
}
}
else if (nHeight < consensusParams.alwaysUpdateDiffChangeTarget) // < 400000
{
// (Period IV)
nSubsidy = 8000 * COIN;
int blocks = nHeight - consensusParams.nDiffChangeTarget;
int weeks = (blocks / consensusParams.patchBlockRewardDuration) + 1;

// Decrease reward by 0.5% every 10080 blocks
for (int i = 0; i < weeks; i++)
{
nSubsidy -= (nSubsidy / 200);
}
}
else if (nHeight < consensusParams.workComputationChangeTarget) // < 1430000
{
// (Period V)
nSubsidy = 2459 * COIN;
int blocks = nHeight - consensusParams.alwaysUpdateDiffChangeTarget;
int weeks = (blocks / consensusParams.patchBlockRewardDuration2) + 1;
// decrease reward by 1% every month
for(int i = 0; i < weeks; i++)
{
nSubsidy -= (nSubsidy / 100);
}
}
else // < ∞
{
// (Period VI)
// Hard Fork Point: 1.43M
// Subsidy at Hard Fork: 2157
// Monthly Decay Factor: 98884/100000
// Last Block Number: 41668798
// Expected years after Hard Fork: 19.1395
nSubsidy = 2157 * COIN / 2;
int64_t blocks = nHeight - consensusParams.workComputationChangeTarget;
int64_t months = blocks * BLOCK_TIME_SECONDS / SECONDS_PER_MONTH;

for (int64_t i = 0; i < months; i++)
{
nSubsidy *= 98884;
nSubsidy /= 100000;
}
}

// Make sure the reward is at least 1 DGB
// ToDo: Remove this statement in a future release, along
// with a fixed supply curve.
if (nSubsidy < COIN) {
nSubsidy = COIN;
}

return nSubsidy;
}

bool IsInitialBlockDownload()
Expand Down
9 changes: 9 additions & 0 deletions src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@

#include <atomic>

// The following constants are used in GetDGBSubsidy()
#define BLOCK_TIME_SECONDS 15
#define MINUTES 60
#define SECONDS 60
#define HOURS 24
#define MONTHS_PER_YEAR 12
#define DAYS_PER_YEAR 365
#define SECONDS_PER_MONTH (SECONDS * MINUTES * HOURS * DAYS_PER_YEAR / MONTHS_PER_YEAR);

class CBlockIndex;
class CBlockTreeDB;
class CChainParams;
Expand Down

0 comments on commit c554b8c

Please sign in to comment.