diff --git a/src/main.cpp b/src/main.cpp index b5302a9bf..38f567661 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -932,11 +932,21 @@ int CMerkleTx::GetDepthInMainChain(CBlockIndex* &pindexRet) const return nResult; } +int CMerkleTx::GetHeightInMainChain(CBlockIndex* &pindexRet) const +{ + return chainActive.Height() - GetDepthInMainChain(pindexRet) + 1; +} + int CMerkleTx::GetBlocksToMaturity() const { if (!IsCoinBase()) return 0; - return max(0, (COINBASE_MATURITY+1) - GetDepthInMainChain()); + + int nHeight = GetHeightInMainChain(); + int nMaturity = GetRequiredMaturityDepth(nHeight); + + return max(0, (nMaturity+20) - GetDepthInMainChain()); + } @@ -1530,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 *pvChecks) { if (!tx.IsCoinBase()) @@ -1555,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), diff --git a/src/main.h b/src/main.h index 155ad413d..542252051 100644 --- a/src/main.h +++ b/src/main.h @@ -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. @@ -476,6 +483,8 @@ public: // >=1 : this many blocks deep in the main chain int GetDepthInMainChain(CBlockIndex* &pindexRet) const; int GetDepthInMainChain() const { CBlockIndex *pindexRet; return GetDepthInMainChain(pindexRet); } + int GetHeightInMainChain(CBlockIndex* &pindexRet) const; + int GetHeightInMainChain() const { CBlockIndex *pindexRet; return GetHeightInMainChain(pindexRet); } bool IsInMainChain() const { CBlockIndex *pindexRet; return GetDepthInMainChainINTERNAL(pindexRet) > 0; } int GetBlocksToMaturity() const; bool AcceptToMemoryPool(bool fLimitFree=true);