Skip to content

Commit

Permalink
GetBlockSubsidy current height as param
Browse files Browse the repository at this point in the history
  • Loading branch information
Krekeler committed Dec 21, 2018
1 parent 24a1c63 commit e5f2b79
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/governance-classes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ 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 nSuperblockPartOfSubsidy = GetBlockSubsidy(nBits, nBlockHeight, consensusParams, true);
CAmount nPaymentsLimit = nSuperblockPartOfSubsidy * consensusParams.nSuperblockCycle;
LogPrint("gobject", "CSuperblock::GetPaymentsLimit -- Valid superblock height %d, payments max %lld\n", nBlockHeight, nPaymentsLimit);

Expand Down
4 changes: 2 additions & 2 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
coinbaseTx.vout.resize(1);
coinbaseTx.vout[0].scriptPubKey = scriptPubKeyIn;

// NOTE: unlike in bitcoin, we need to pass PREVIOUS block height here
CAmount blockReward = nFees + GetBlockSubsidy(pindexPrev->nBits, pindexPrev->nHeight, Params().GetConsensus());
// NOTE: unlike in Dash, we need to pass CURRENT block height here (like Bitcoin)
CAmount blockReward = nFees + GetBlockSubsidy(pindexPrev->nBits, nHeight, Params().GetConsensus());

// Compute regular coinbase transaction.
coinbaseTx.vout[0].nValue = blockReward;
Expand Down
16 changes: 8 additions & 8 deletions src/test/subsidy_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,51 +24,51 @@ BOOST_AUTO_TEST_CASE(block_subsidy_test)
// details for block 4249 (subsidy returned will be for block 4250)
nPrevBits = 0x1c4a47c4;
nPrevHeight = 4249;
nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight, consensusParams, false);
nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight + 1, consensusParams, false);
BOOST_CHECK_EQUAL(nSubsidy, 50000000000ULL);

// details for block 4501 (subsidy returned will be for block 4502)
nPrevBits = 0x1c4a47c4;
nPrevHeight = 4501;
nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight, consensusParams, false);
nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight + 1, consensusParams, false);
BOOST_CHECK_EQUAL(nSubsidy, 5600000000ULL);

// details for block 5464 (subsidy returned will be for block 5465)
nPrevBits = 0x1c29ec00;
nPrevHeight = 5464;
nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight, consensusParams, false);
nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight + 1, consensusParams, false);
BOOST_CHECK_EQUAL(nSubsidy, 2100000000ULL);

// details for block 5465 (subsidy returned will be for block 5466)
nPrevBits = 0x1c29ec00;
nPrevHeight = 5465;
nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight, consensusParams, false);
nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight + 1, consensusParams, false);
BOOST_CHECK_EQUAL(nSubsidy, 12200000000ULL);

// details for block 17588 (subsidy returned will be for block 17589)
nPrevBits = 0x1c08ba34;
nPrevHeight = 17588;
nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight, consensusParams, false);
nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight + 1, consensusParams, false);
BOOST_CHECK_EQUAL(nSubsidy, 6100000000ULL);

// details for block 99999 (subsidy returned will be for block 100000)
nPrevBits = 0x1b10cf42;
nPrevHeight = 99999;
nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight, consensusParams, false);
nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight + 1, consensusParams, false);
BOOST_CHECK_EQUAL(nSubsidy, 500000000ULL);

// details for block 210239 (subsidy returned will be for block 210240)
nPrevBits = 0x1b11548e;
nPrevHeight = 210239;
nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight, consensusParams, false);
nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight + 1, consensusParams, false);
BOOST_CHECK_EQUAL(nSubsidy, 500000000ULL);

// 1st subsidy reduction happens here

// details for block 210240 (subsidy returned will be for block 210241)
nPrevBits = 0x1b10d50b;
nPrevHeight = 210240;
nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight, consensusParams, false);
nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight + 1, consensusParams, false);
BOOST_CHECK_EQUAL(nSubsidy, 464285715ULL);
}

Expand Down
19 changes: 7 additions & 12 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1284,15 +1284,10 @@ double ConvertBitsToDouble(unsigned int nBits)
return dDiff;
}

/*
NOTE: unlike bitcoin we are using PREVIOUS block height here,
might be a good idea to change this to use prev bits
but current height to avoid confusion.
*/
CAmount GetBlockSubsidy(int nPrevBits, int nPrevHeight, const Consensus::Params& consensusParams, bool fSuperblockPartOnly)
// unlike Dash we are using current block height and previous nBits here, Dash uses previous height
// DMS uses only nHeight, but the params are not removed yet because they could be of interest for future purposes
CAmount GetBlockSubsidy(int nPrevBits, int nHeight, const Consensus::Params& consensusParams, bool fSuperblockPartOnly)
{
int nHeight = nPrevHeight + 1; // TODO: use nHeight as param like bitcoin, remove unused params

// 10 blocks/hour, 240/day, 7200/month, 87600/year
int nReward;
if (nHeight < 44001) { nReward = 50; } // 2 200 000 / 6 month
Expand Down Expand Up @@ -2292,15 +2287,15 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd
int64_t nTime3 = GetTimeMicros(); nTimeConnect += nTime3 - nTime2;
LogPrint("bench", " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs]\n", (unsigned)block.vtx.size(), 0.001 * (nTime3 - nTime2), 0.001 * (nTime3 - nTime2) / block.vtx.size(), nInputs <= 1 ? 0 : 0.001 * (nTime3 - nTime2) / (nInputs-1), nTimeConnect * 0.000001);

// DMS : MODIFIED TO CHECK MASTERNODE PAYMENTS AND SUPERBLOCKS

// Dash : MODIFIED TO CHECK MASTERNODE PAYMENTS AND SUPERBLOCKS
// It's possible that we simply don't have enough data and this could fail
// (i.e. block itself could be a correct one and we need to store it),
// that's why this is in ConnectBlock. Could be the other way around however -
// the peer who sent us this block is missing some data and wasn't able
// to recognize that block is actually invalid.
// TODO: resync data (both ways?) and try to reprocess this block later.
CAmount blockReward = nFees + GetBlockSubsidy(pindex->pprev->nBits, pindex->pprev->nHeight, chainparams.GetConsensus());
// DMS: GetBlockSubsidy uses previus nBits and current nHeight
CAmount blockReward = nFees + GetBlockSubsidy(pindex->pprev->nBits, pindex->nHeight, chainparams.GetConsensus());
std::string strError = "";
if (!IsBlockValueValid(block, pindex->nHeight, blockReward, strError)) {
return state.DoS(0, error("ConnectBlock(DMS): %s", strError), REJECT_INVALID, "bad-cb-amount");
Expand All @@ -2311,7 +2306,7 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd
return state.DoS(0, error("ConnectBlock(DMS): couldn't find masternode or superblock payments"),
REJECT_INVALID, "bad-cb-payee");
}
// END DMS
// END Dash

if (!control.Wait())
return state.DoS(100, false);
Expand Down
2 changes: 1 addition & 1 deletion src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ bool GetTransaction(const uint256 &hash, CTransactionRef &tx, const Consensus::P
bool ActivateBestChain(CValidationState& state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock = std::shared_ptr<const CBlock>());

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

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

0 comments on commit e5f2b79

Please sign in to comment.