Move COINBASE_MATURITY to the consensus parameters (#1426)

This commit is contained in:
Ross Nicoll 2018-01-08 19:39:10 +00:00
parent ec40df46b5
commit 7b81f4de0a
8 changed files with 28 additions and 27 deletions

View File

@ -7,6 +7,7 @@
#include "auxpow.h"
#include "chainparams.h"
#include "compat/endian.h"
#include "consensus/consensus.h"
#include "consensus/merkle.h"
@ -64,7 +65,8 @@ int CMerkleTx::GetBlocksToMaturity() const
{
if (!IsCoinBase())
return 0;
return std::max(0, (COINBASE_MATURITY+1) - GetDepthInMainChain());
int nCoinbaseMaturity = Params().GetConsensus(chainActive.Height()).nCoinbaseMaturity;
return std::max(0, (nCoinbaseMaturity + 1) - GetDepthInMainChain());
}

View File

@ -88,6 +88,7 @@ public:
consensus.nPowTargetTimespan = 4 * 60 * 60; // pre-digishield: 4 hours
consensus.nPowTargetSpacing = 60; // 1 minute
consensus.fDigishieldDifficultyCalculation = false;
consensus.nCoinbaseMaturity = 30;
consensus.fPowAllowMinDifficultyBlocks = false;
consensus.fPowAllowDigishieldMinDifficultyBlocks = false;
consensus.fPowNoRetargeting = false;
@ -467,6 +468,20 @@ const CChainParams &Params() {
return *pCurrentParams;
}
const Consensus::Params *Consensus::Params::GetConsensus(uint32_t nTargetHeight) const {
if (nTargetHeight < this -> nHeightEffective && this -> pLeft != NULL) {
return this -> pLeft -> GetConsensus(nTargetHeight);
} else if (nTargetHeight > this -> nHeightEffective && this -> pRight != NULL) {
const Consensus::Params *pCandidate = this -> pRight -> GetConsensus(nTargetHeight);
if (pCandidate->nHeightEffective <= nTargetHeight) {
return pCandidate;
}
}
// No better match below the target height
return this;
}
CChainParams& Params(const std::string& chain)
{
if (chain == CBaseChainParams::MAIN)

View File

@ -57,21 +57,7 @@ public:
};
const Consensus::Params& GetConsensus(uint32_t nTargetHeight) const {
return *GetConsensus(nTargetHeight, pConsensusRoot);
}
Consensus::Params *GetConsensus(uint32_t nTargetHeight, Consensus::Params *pRoot) const {
if (nTargetHeight < pRoot -> nHeightEffective && pRoot -> pLeft != NULL) {
return GetConsensus(nTargetHeight, pRoot -> pLeft);
} else if (nTargetHeight > pRoot -> nHeightEffective && pRoot -> pRight != NULL) {
Consensus::Params *pCandidate = GetConsensus(nTargetHeight, pRoot -> pRight);
if (pCandidate->nHeightEffective <= nTargetHeight) {
return pCandidate;
}
}
// No better match below the target height
return pRoot;
return *(pConsensusRoot -> GetConsensus(nTargetHeight));
}
const CMessageHeader::MessageStartChars& MessageStart() const { return pchMessageStart; }

View File

@ -18,10 +18,6 @@ static const unsigned int MAX_BLOCK_BASE_SIZE = 1000000;
static const int64_t MAX_BLOCK_SIGOPS_COST = 80000;
/** Coinbase transaction outputs can only be spent after this number of new blocks (network rule) */
static const int COINBASE_MATURITY = 60*4; // 4 hours of blocks
/** Coinbase maturity before block 145000 **/
static const int COINBASE_MATURITY_OLD = 30;
/** Block at which COINBASE_MATURITY_OLD was deprecated **/
static const int COINBASE_MATURITY_SWITCH = 145000;
/** Flags for nSequence and nLockTime locks */
enum {

View File

@ -43,7 +43,6 @@ struct Params {
int nMajorityEnforceBlockUpgrade;
int nMajorityRejectBlockOutdated;
int nMajorityWindow;
int nCoinbaseMaturity;
/** Block height and hash at which BIP34 becomes active */
int BIP34Height;
uint256 BIP34Hash;
@ -59,6 +58,7 @@ struct Params {
uint32_t nRuleChangeActivationThreshold;
uint32_t nMinerConfirmationWindow;
BIP9Deployment vDeployments[MAX_VERSION_BITS_DEPLOYMENTS];
uint32_t nCoinbaseMaturity;
/** Proof of work parameters */
uint256 powLimit;
bool fPowAllowMinDifficultyBlocks;
@ -84,6 +84,7 @@ struct Params {
uint32_t nHeightEffective; // When these parameters come into use
struct Params *pLeft; // Left hand branch
struct Params *pRight; // Right hand branch
const Consensus::Params *GetConsensus(uint32_t nTargetHeight) const;
};
} // namespace Consensus

View File

@ -10,6 +10,7 @@
#include "transactionrecord.h"
#include "base58.h"
#include "chainparams.h"
#include "consensus/consensus.h"
#include "validation.h"
#include "script/script.h"
@ -266,8 +267,8 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx, TransactionReco
if (wtx.IsCoinBase())
{
quint32 numBlocksToMaturity = COINBASE_MATURITY + 1;
strHTML += "<br>" + tr("Generated coins must mature %1 blocks before they can be spent. When you generated this block, it was broadcast to the network to be added to the block chain. If it fails to get into the chain, its state will change to \"not accepted\" and it won't be spendable. This may occasionally happen if another node generates a block within a few seconds of yours.").arg(QString::number(numBlocksToMaturity)) + "<br>";
quint32 nCoinbaseMaturity = Params().GetConsensus(chainActive.Height()).nCoinbaseMaturity + 1;
strHTML += "<br>" + tr("Generated coins must mature %1 blocks before they can be spent. When you generated this block, it was broadcast to the network to be added to the block chain. If it fails to get into the chain, its state will change to \"not accepted\" and it won't be spendable. This may occasionally happen if another node generates a block within a few seconds of yours.").arg(QString::number(nCoinbaseMaturity)) + "<br>";
}
//

View File

@ -5,6 +5,7 @@
#include "txmempool.h"
#include "chainparams.h"
#include "clientversion.h"
#include "consensus/consensus.h"
#include "consensus/validation.h"
@ -556,7 +557,8 @@ void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMem
continue;
const CCoins *coins = pcoins->AccessCoins(txin.prevout.hash);
if (nCheckFrequency != 0) assert(coins);
if (!coins || (coins->IsCoinBase() && ((signed long)nMemPoolHeight) - coins->nHeight < COINBASE_MATURITY)) {
int nCoinbaseMaturity = Params().GetConsensus(coins->nHeight).nCoinbaseMaturity;
if (!coins || (coins->IsCoinBase() && ((signed long)nMemPoolHeight) - coins->nHeight < nCoinbaseMaturity)) {
txToRemove.insert(it);
break;
}

View File

@ -1414,9 +1414,7 @@ bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoins
// If prev is coinbase, check that it's matured
if (coins->IsCoinBase()) {
// Dogecoin: Switch maturity at depth 145,000
int nCoinbaseMaturity = coins->nHeight < COINBASE_MATURITY_SWITCH
? COINBASE_MATURITY_OLD
: COINBASE_MATURITY;
int nCoinbaseMaturity = Params().GetConsensus(coins->nHeight)->nCoinbaseMaturity;
if (nSpendHeight - coins->nHeight < nCoinbaseMaturity)
return state.Invalid(false,
REJECT_INVALID, "bad-txns-premature-spend-of-coinbase",