Skip to content

Commit 377711f

Browse files
committed
Merge pull request #5996
935bd0a Chainparams: Refactor: Decouple main::GetBlockValue() from Params() [renamed GetBlockSubsidy] (Jorge Timón)
2 parents e2e7f95 + 935bd0a commit 377711f

File tree

5 files changed

+47
-15
lines changed

5 files changed

+47
-15
lines changed

src/chainparams.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ class CChainParams
4949
const CMessageHeader::MessageStartChars& MessageStart() const { return pchMessageStart; }
5050
const std::vector<unsigned char>& AlertKey() const { return vAlertPubKey; }
5151
int GetDefaultPort() const { return nDefaultPort; }
52-
int SubsidyHalvingInterval() const { return consensus.nSubsidyHalvingInterval; }
5352

5453
/** Used if GenerateBitcoins is called with a negative number of threads */
5554
int DefaultMinerThreads() const { return nMinerThreads; }

src/main.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,19 +1188,17 @@ bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex)
11881188
return true;
11891189
}
11901190

1191-
CAmount GetBlockValue(int nHeight, const CAmount& nFees)
1191+
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
11921192
{
1193-
CAmount nSubsidy = 50 * COIN;
1194-
int halvings = nHeight / Params().SubsidyHalvingInterval();
1195-
1193+
int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
11961194
// Force block reward to zero when right shift is undefined.
11971195
if (halvings >= 64)
1198-
return nFees;
1196+
return 0;
11991197

1198+
CAmount nSubsidy = 50 * COIN;
12001199
// Subsidy is cut in half every 210,000 blocks which will occur approximately every 4 years.
12011200
nSubsidy >>= halvings;
1202-
1203-
return nSubsidy + nFees;
1201+
return nSubsidy;
12041202
}
12051203

12061204
bool IsInitialBlockDownload()
@@ -1809,10 +1807,11 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
18091807
int64_t nTime1 = GetTimeMicros(); nTimeConnect += nTime1 - nTimeStart;
18101808
LogPrint("bench", " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs]\n", (unsigned)block.vtx.size(), 0.001 * (nTime1 - nTimeStart), 0.001 * (nTime1 - nTimeStart) / block.vtx.size(), nInputs <= 1 ? 0 : 0.001 * (nTime1 - nTimeStart) / (nInputs-1), nTimeConnect * 0.000001);
18111809

1812-
if (block.vtx[0].GetValueOut() > GetBlockValue(pindex->nHeight, nFees))
1810+
CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, chainparams.GetConsensus());
1811+
if (block.vtx[0].GetValueOut() > blockReward)
18131812
return state.DoS(100,
18141813
error("ConnectBlock(): coinbase pays too much (actual=%d vs limit=%d)",
1815-
block.vtx[0].GetValueOut(), GetBlockValue(pindex->nHeight, nFees)),
1814+
block.vtx[0].GetValueOut(), blockReward),
18161815
REJECT_INVALID, "bad-cb-amount");
18171816

18181817
if (!control.Wait())

src/main.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ std::string GetWarnings(std::string strFor);
202202
bool GetTransaction(const uint256 &hash, CTransaction &tx, uint256 &hashBlock, bool fAllowSlow = false);
203203
/** Find the best known block, and make it the tip of the block chain */
204204
bool ActivateBestChain(CValidationState &state, CBlock *pblock = NULL);
205-
CAmount GetBlockValue(int nHeight, const CAmount& nFees);
205+
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);
206206

207207
/**
208208
* Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a user-defined target.

src/miner.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams,
9191

9292
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
9393
{
94+
const CChainParams& chainparams = Params();
9495
// Create new block
9596
auto_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
9697
if(!pblocktemplate.get())
@@ -320,7 +321,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
320321
LogPrintf("CreateNewBlock(): total size %u\n", nBlockSize);
321322

322323
// Compute final coinbase transaction.
323-
txNew.vout[0].nValue = GetBlockValue(nHeight, nFees);
324+
txNew.vout[0].nValue = nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus());
324325
txNew.vin[0].scriptSig = CScript() << nHeight << OP_0;
325326
pblock->vtx[0] = txNew;
326327
pblocktemplate->vTxFees[0] = -nFees;

src/test/main_tests.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,58 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include "primitives/transaction.h"
5+
#include "chainparams.h"
66
#include "main.h"
77

88
#include "test/test_bitcoin.h"
99

10+
#include <boost/signals2/signal.hpp>
1011
#include <boost/test/unit_test.hpp>
1112

1213
BOOST_FIXTURE_TEST_SUITE(main_tests, TestingSetup)
1314

15+
static void TestBlockSubsidyHalvings(const Consensus::Params& consensusParams)
16+
{
17+
int maxHalvings = 64;
18+
CAmount nInitialSubsidy = 50 * COIN;
19+
20+
CAmount nPreviousSubsidy = nInitialSubsidy * 2; // for height == 0
21+
BOOST_CHECK_EQUAL(nPreviousSubsidy, nInitialSubsidy * 2);
22+
for (int nHalvings = 0; nHalvings < maxHalvings; nHalvings++) {
23+
int nHeight = nHalvings * consensusParams.nSubsidyHalvingInterval;
24+
CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams);
25+
BOOST_CHECK(nSubsidy <= nInitialSubsidy);
26+
BOOST_CHECK_EQUAL(nSubsidy, nPreviousSubsidy / 2);
27+
nPreviousSubsidy = nSubsidy;
28+
}
29+
BOOST_CHECK_EQUAL(GetBlockSubsidy(maxHalvings * consensusParams.nSubsidyHalvingInterval, consensusParams), 0);
30+
}
31+
32+
static void TestBlockSubsidyHalvings(int nSubsidyHalvingInterval)
33+
{
34+
Consensus::Params consensusParams;
35+
consensusParams.nSubsidyHalvingInterval = nSubsidyHalvingInterval;
36+
TestBlockSubsidyHalvings(consensusParams);
37+
}
38+
39+
BOOST_AUTO_TEST_CASE(block_subsidy_test)
40+
{
41+
TestBlockSubsidyHalvings(Params(CBaseChainParams::MAIN).GetConsensus()); // As in main
42+
TestBlockSubsidyHalvings(150); // As in regtest
43+
TestBlockSubsidyHalvings(1000); // Just another interval
44+
}
45+
1446
BOOST_AUTO_TEST_CASE(subsidy_limit_test)
1547
{
48+
const Consensus::Params& consensusParams = Params(CBaseChainParams::MAIN).GetConsensus();
1649
CAmount nSum = 0;
1750
for (int nHeight = 0; nHeight < 14000000; nHeight += 1000) {
18-
CAmount nSubsidy = GetBlockValue(nHeight, 0);
51+
CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams);
1952
BOOST_CHECK(nSubsidy <= 50 * COIN);
2053
nSum += nSubsidy * 1000;
2154
BOOST_CHECK(MoneyRange(nSum));
2255
}
23-
BOOST_CHECK(nSum == 2099999997690000ULL);
56+
BOOST_CHECK_EQUAL(nSum, 2099999997690000ULL);
2457
}
2558

2659
bool ReturnFalse() { return false; }

0 commit comments

Comments
 (0)