Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented rollover treasury #5

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/governance-classes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,48 @@ CAmount CSuperblock::GetPaymentsLimit(int nBlockHeight)
// min subsidy for high diff networks and vice versa
int nBits = consensusParams.fPowAllowMinDifficultyBlocks ? UintToArith256(consensusParams.powLimit).GetCompact() : 1;
// some part of all blocks issued during the cycle goes to superblock, see GetBlockSubsidy
CAmount nSuperblockPartOfSubsidy = GetBlockSubsidy(nBits, nBlockHeight - 1, consensusParams, true);
CAmount nPaymentsLimit = nSuperblockPartOfSubsidy * consensusParams.nSuperblockCycle;
LOCK(cs_main);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the necessity of this. It looked like it was used in another instance similar to this


CCoinsView *view = (CCoinsView*)pcoinsdbview;
std::unique_ptr<CCoinsViewCursor> pcursor(view->Cursor());


uint256 prevkey;
std::map<uint32_t, Coin> outputs;
while (pcursor->Valid()) {
boost::this_thread::interruption_point();
COutPoint key;
Coin coin;
if (pcursor->GetKey(key) && pcursor->GetValue(coin)) {
if (!outputs.empty() && key.hash != prevkey) {
outputs.clear();
}
prevkey = key.hash;
outputs[key.n] = std::move(coin);
}
else {
return error("%s: unable to read value", __func__);
}
pcursor->Next();
}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of that was based on another location where the set of outputs is created, likely from the total supply field in the rpc command gettxoutsetinfo


// Current supply
CAmount nCirculatingSupply = 0;
for (const auto output : outputs) {
nCirculatingSupply += output.second.out.nValue;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on total supply field in the rpc command gettxoutsetinfo

}

// Theoretical Maximum Supply at given block
CAmount nMaxSupply = 0;
for (auto it = 0; it < nBlockHeight; it++) {

// Get CBlockIndex pointer based on iterator
CBlockIndex *pblock = chainActive[it];

// Add to the Max Supply the subsidy at each block
nMaxSupply += GetCompleteBlockSubsidy(pblock->pprev->nBits, pblock->pprev->nHeight, Params().GetConsensus);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on other locations (I think) uses previous blocks since that is how our GetBlockSubsidy methods work

}
CAmount nPaymentsLimit = nMaxSupply - nCirculatingSupply;
LogPrint("gobject", "CSuperblock::GetPaymentsLimit -- Valid superblock height %d, payments max %lld\n", nBlockHeight, nPaymentsLimit);

return nPaymentsLimit;
Expand Down
12 changes: 6 additions & 6 deletions src/masternode-payments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,20 @@ bool IsBlockValueValid(const CBlock& block, int nBlockHeight, CAmount blockRewar
bool isBlockRewardValueMet = (block.vtx[0]->GetValueOut() <= blockReward);
if(fDebug) LogPrintf("block.vtx[0]->GetValueOut() %lld <= blockReward %lld\n", block.vtx[0]->GetValueOut(), blockReward);

CAmount nSuperblockMaxValue = blockReward + CSuperblock::GetPaymentsLimit(nBlockHeight);
bool isSuperblockMaxValueMet = (block.vtx[0]->GetValueOut() <= nSuperblockMaxValue);

LogPrint("gobject", "block.vtx[0]->GetValueOut() %lld <= nSuperblockMaxValue %lld\n", block.vtx[0]->GetValueOut(), nSuperblockMaxValue);

if (!CSuperblock::IsValidBlockHeight(nBlockHeight)) {
// can't possibly be a superblock, so lets just check for block reward limits
if (!isBlockRewardValueMet) {
strErrorRet = strprintf("coinbase pays too much at height %d (actual=%d vs limit=%d), exceeded block reward, only regular blocks are allowed at this height",
nBlockHeight, block.vtx[0]->GetValueOut(), blockReward);
nBlockHeight, block.vtx[0]->GetValueOut(), blockReward);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto formatting based on clang file since I initially edited this line and then reverted

}
return isBlockRewardValueMet;
}

CAmount nSuperblockMaxValue = blockReward + CSuperblock::GetPaymentsLimit(nBlockHeight);
bool isSuperblockMaxValueMet = (block.vtx[0]->GetValueOut() <= nSuperblockMaxValue);

LogPrint("gobject", "block.vtx[0]->GetValueOut() %lld <= nSuperblockMaxValue %lld\n", block.vtx[0]->GetValueOut(), nSuperblockMaxValue);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to increase efficiency since the new superblock limit calculations are more demanding and there is no point in calculating the limit if it is not a superblock


// bail out in case superblock limits were exceeded
if (!isSuperblockMaxValueMet) {
strErrorRet = strprintf("coinbase pays too much at height %d (actual=%d vs limit=%d), exceeded superblock max value",
Expand Down
46 changes: 29 additions & 17 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1318,49 +1318,61 @@ NOTE: unlike bitcoin we are using PREVIOUS block height here,
but current height to avoid confusion.
*/
CAmount GetBlockSubsidy(int nPrevBits, int nPrevHeight, const Consensus::Params& consensusParams, bool fSuperblockPartOnly)
{

CAmount nSubsidy = GetCompleteBlockSubsidy(nPrevBits, nPrevHeight, consensusParams);

// Hard fork to reduce the block reward by 10 extra percent (allowing budget/superblocks)
CAmount nSuperblockPart = (nPrevHeight > consensusParams.nBudgetPaymentsStartBlock) ? nSubsidy / 10 : 0;

return fSuperblockPartOnly ? nSuperblockPart : nSubsidy - nSuperblockPart;
}

// Returns the total subsidy, includes the miners, masternodes and treasury
CAmount GetCompleteBlockSubsidy(int nPrevBits, int nPrevHeight, const Consensus::Params& consensusParams)
{
double dDiff;
CAmount nSubsidyBase;

if (nPrevHeight <= 4500 && Params().NetworkIDString() == CBaseChainParams::MAIN) {
/* a bug which caused diff to not be correctly calculated */
dDiff = (double)0x0000ffff / (double)(nPrevBits & 0x00ffffff);
} else {
}
else {
dDiff = ConvertBitsToDouble(nPrevBits);
}

if (nPrevHeight < 5465) {
// Early ages...
// 1111/((x+1)^2)
nSubsidyBase = (1111.0 / (pow((dDiff+1.0),2.0)));
if(nSubsidyBase > 500) nSubsidyBase = 500;
else if(nSubsidyBase < 1) nSubsidyBase = 1;
} else if (nPrevHeight < 17000 || (dDiff <= 75 && nPrevHeight < 24000)) {
nSubsidyBase = (1111.0 / (pow((dDiff + 1.0), 2.0)));
if (nSubsidyBase > 500) nSubsidyBase = 500;
else if (nSubsidyBase < 1) nSubsidyBase = 1;
}
else if (nPrevHeight < 17000 || (dDiff <= 75 && nPrevHeight < 24000)) {
// CPU mining era
// 11111/(((x+51)/6)^2)
nSubsidyBase = (11111.0 / (pow((dDiff+51.0)/6.0,2.0)));
if(nSubsidyBase > 500) nSubsidyBase = 500;
else if(nSubsidyBase < 25) nSubsidyBase = 25;
} else {
nSubsidyBase = (11111.0 / (pow((dDiff + 51.0) / 6.0, 2.0)));
if (nSubsidyBase > 500) nSubsidyBase = 500;
else if (nSubsidyBase < 25) nSubsidyBase = 25;
}
else {
// GPU/ASIC mining era
// 2222222/(((x+2600)/9)^2)
nSubsidyBase = (2222222.0 / (pow((dDiff+2600.0)/9.0,2.0)));
if(nSubsidyBase > 25) nSubsidyBase = 25;
else if(nSubsidyBase < 5) nSubsidyBase = 5;
nSubsidyBase = (2222222.0 / (pow((dDiff + 2600.0) / 9.0, 2.0)));
if (nSubsidyBase > 25) nSubsidyBase = 25;
else if (nSubsidyBase < 5) nSubsidyBase = 5;
}

// LogPrintf("height %u diff %4.2f reward %d\n", nPrevHeight, dDiff, nSubsidyBase);
CAmount nSubsidy = nSubsidyBase * COIN;

// yearly decline of production by ~7.1% per year, projected ~18M coins max by year 2050+.
for (int i = consensusParams.nSubsidyHalvingInterval; i <= nPrevHeight; i += consensusParams.nSubsidyHalvingInterval) {
nSubsidy -= nSubsidy/14;
nSubsidy -= nSubsidy / 14;
}

// Hard fork to reduce the block reward by 10 extra percent (allowing budget/superblocks)
CAmount nSuperblockPart = (nPrevHeight > consensusParams.nBudgetPaymentsStartBlock) ? nSubsidy/10 : 0;

return fSuperblockPartOnly ? nSuperblockPart : nSubsidy - nSuperblockPart;
return nSubsidy;
}

CAmount GetMasternodePayment(int nHeight, CAmount blockValue)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about the formatting changes in this file, again, due to copy/paste auto format was enabled and formatted based on clang file

Expand Down
1 change: 1 addition & 0 deletions src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ bool ActivateBestChain(CValidationState& state, const CChainParams& chainparams,

double ConvertBitsToDouble(unsigned int nBits);
CAmount GetBlockSubsidy(int nBits, int nHeight, const Consensus::Params& consensusParams, bool fSuperblockPartOnly = false);
CAmount GetCompleteBlockSubsidy(int nPrevBits, int nPrevHeight, const Consensus::Params& consensusParams);
CAmount GetMasternodePayment(int nHeight, CAmount blockValue);

/** Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip). */
Expand Down