DRY calculation for required maturity depth

Having this defined in multiple places is suboptimal and may lead
to porting issues, so I extracted it into it's own function and
let both implementations call that instead.
This commit is contained in:
Patrick Lodder 2014-04-20 17:14:13 +04:00
parent 85392ed8bb
commit d0957dc745
2 changed files with 29 additions and 8 deletions

View file

@ -941,11 +941,12 @@ int CMerkleTx::GetBlocksToMaturity() const
{
if (!IsCoinBase())
return 0;
if(GetHeightInMainChain() >= COINBASE_MATURITY_SWITCH)
return max(0, (COINBASE_MATURITY_NEW+20) - GetDepthInMainChain());
else
return max(0, (COINBASE_MATURITY+20) - GetDepthInMainChain());
int nHeight = GetHeightInMainChain();
int nMaturity = GetRequiredMaturityDepth(nHeight);
return max(0, (nMaturity+20) - GetDepthInMainChain());
}
@ -1539,6 +1540,19 @@ bool VerifySignature(const CCoins& txFrom, const CTransaction& txTo, unsigned in
return CScriptCheck(txFrom, txTo, nIn, flags, nHashType)();
}
int GetRequiredMaturityDepth(int nHeight)
{
if (nHeight >= COINBASE_MATURITY_SWITCH)
{
return COINBASE_MATURITY_NEW;
}
else
{
return COINBASE_MATURITY;
}
}
bool CheckInputs(const CTransaction& tx, CValidationState &state, CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, std::vector<CScriptCheck> *pvChecks)
{
if (!tx.IsCoinBase())
@ -1564,9 +1578,9 @@ bool CheckInputs(const CTransaction& tx, CValidationState &state, CCoinsViewCach
// If prev is coinbase, check that it's matured
if (coins.IsCoinBase()) {
int minDepth = COINBASE_MATURITY;
if(coins.nHeight >= COINBASE_MATURITY_SWITCH)
minDepth = COINBASE_MATURITY_NEW;
int minDepth = GetRequiredMaturityDepth(coins.nHeight);
if (nSpendHeight - coins.nHeight < minDepth)
return state.Invalid(
error("CheckInputs() : tried to spend coinbase at depth %d", nSpendHeight - coins.nHeight),

View file

@ -307,6 +307,13 @@ inline bool AllowFree(double dPriority)
return dPriority > 100 * COIN * 1440 / 250; // Dogecoin: 1440 blocks found a day. Priority cutoff is 100 dogecoin day / 250 bytes.
}
/** Get the maturity depth for coinbase transactions at a given height.
@param[in] nHeight The height at which to check maturity for
@return the depth at which the coinbase transaction matures
*/
// Dogecoin specific implementation, standardizes checks for the hard maturity change at block 145k
int GetRequiredMaturityDepth(int nHeight);
// Check whether all inputs of this transaction are valid (no double spends, scripts & sigs, amounts)
// This does not modify the UTXO set. If pvChecks is not NULL, script checks are pushed onto it
// instead of being performed inline.