Skip to content

Commit 23f4343

Browse files
committed
Add CMerkleTx::IsImmatureCoinBase method
All but one call to GetBlocksToMaturity is testing it relative to 0 for the purposes of determining whether the coinbase tx is immature. In such case, the value greater than 0 implies that the tx is coinbase, so there is no need to separately test that status. This names the concept for easy singular use.
1 parent 29b4ee6 commit 23f4343

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,7 +1851,7 @@ static void ListTransactions(CWallet* const pwallet, const CWalletTx& wtx, const
18511851
{
18521852
if (wtx.GetDepthInMainChain() < 1)
18531853
entry.pushKV("category", "orphan");
1854-
else if (wtx.GetBlocksToMaturity() > 0)
1854+
else if (wtx.IsImmatureCoinBase())
18551855
entry.pushKV("category", "immature");
18561856
else
18571857
entry.pushKV("category", "generate");
@@ -2147,7 +2147,7 @@ static UniValue listaccounts(const JSONRPCRequest& request)
21472147
std::list<COutputEntry> listReceived;
21482148
std::list<COutputEntry> listSent;
21492149
int nDepth = wtx.GetDepthInMainChain();
2150-
if (wtx.GetBlocksToMaturity() > 0 || nDepth < 0)
2150+
if (wtx.IsImmatureCoinBase() || nDepth < 0)
21512151
continue;
21522152
wtx.GetAmounts(listReceived, listSent, nFee, strSentAccount, includeWatchonly);
21532153
mapAccountBalances[strSentAccount] -= nFee;

src/wallet/wallet.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,7 +1894,7 @@ CAmount CWalletTx::GetDebit(const isminefilter& filter) const
18941894
CAmount CWalletTx::GetCredit(const isminefilter& filter) const
18951895
{
18961896
// Must wait until coinbase is safely deep enough in the chain before valuing it
1897-
if (IsCoinBase() && GetBlocksToMaturity() > 0)
1897+
if (IsImmatureCoinBase())
18981898
return 0;
18991899

19001900
CAmount credit = 0;
@@ -1926,8 +1926,7 @@ CAmount CWalletTx::GetCredit(const isminefilter& filter) const
19261926

19271927
CAmount CWalletTx::GetImmatureCredit(bool fUseCache) const
19281928
{
1929-
if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
1930-
{
1929+
if (IsImmatureCoinBase() && IsInMainChain()) {
19311930
if (fUseCache && fImmatureCreditCached)
19321931
return nImmatureCreditCached;
19331932
nImmatureCreditCached = pwallet->GetCredit(*tx, ISMINE_SPENDABLE);
@@ -1944,7 +1943,7 @@ CAmount CWalletTx::GetAvailableCredit(bool fUseCache, const isminefilter& filter
19441943
return 0;
19451944

19461945
// Must wait until coinbase is safely deep enough in the chain before valuing it
1947-
if (IsCoinBase() && GetBlocksToMaturity() > 0)
1946+
if (IsImmatureCoinBase())
19481947
return 0;
19491948

19501949
CAmount* cache = nullptr;
@@ -1985,8 +1984,7 @@ CAmount CWalletTx::GetAvailableCredit(bool fUseCache, const isminefilter& filter
19851984

19861985
CAmount CWalletTx::GetImmatureWatchOnlyCredit(const bool fUseCache) const
19871986
{
1988-
if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
1989-
{
1987+
if (IsImmatureCoinBase() && IsInMainChain()) {
19901988
if (fUseCache && fImmatureWatchCreditCached)
19911989
return nImmatureWatchCreditCached;
19921990
nImmatureWatchCreditCached = pwallet->GetCredit(*tx, ISMINE_WATCH_ONLY);
@@ -2199,7 +2197,7 @@ CAmount CWallet::GetLegacyBalance(const isminefilter& filter, int minDepth, cons
21992197
for (const auto& entry : mapWallet) {
22002198
const CWalletTx& wtx = entry.second;
22012199
const int depth = wtx.GetDepthInMainChain();
2202-
if (depth < 0 || !CheckFinalTx(*wtx.tx) || wtx.GetBlocksToMaturity() > 0) {
2200+
if (depth < 0 || !CheckFinalTx(*wtx.tx) || wtx.IsImmatureCoinBase()) {
22032201
continue;
22042202
}
22052203

@@ -2259,7 +2257,7 @@ void CWallet::AvailableCoins(std::vector<COutput> &vCoins, bool fOnlySafe, const
22592257
if (!CheckFinalTx(*pcoin->tx))
22602258
continue;
22612259

2262-
if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
2260+
if (pcoin->IsImmatureCoinBase())
22632261
continue;
22642262

22652263
int nDepth = pcoin->GetDepthInMainChain();
@@ -3521,7 +3519,7 @@ std::map<CTxDestination, CAmount> CWallet::GetAddressBalances()
35213519
if (!pcoin->IsTrusted())
35223520
continue;
35233521

3524-
if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
3522+
if (pcoin->IsImmatureCoinBase())
35253523
continue;
35263524

35273525
int nDepth = pcoin->GetDepthInMainChain();
@@ -4397,6 +4395,11 @@ int CMerkleTx::GetBlocksToMaturity() const
43974395
return std::max(0, (COINBASE_MATURITY+1) - GetDepthInMainChain());
43984396
}
43994397

4398+
bool CMerkleTx::IsImmatureCoinBase() const
4399+
{
4400+
// note GetBlocksToMaturity is 0 for non-coinbase tx
4401+
return GetBlocksToMaturity() > 0;
4402+
}
44004403

44014404
bool CWalletTx::AcceptToMemoryPool(const CAmount& nAbsurdFee, CValidationState& state)
44024405
{

src/wallet/wallet.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,20 @@ class CMerkleTx
266266
*/
267267
int GetDepthInMainChain() const;
268268
bool IsInMainChain() const { return GetDepthInMainChain() > 0; }
269+
270+
/**
271+
* @return number of blocks to maturity for this transaction:
272+
* 0 : is not a coinbase transaction, or is a mature coinbase transaction
273+
* >0 : is a coinbase transaction which matures in this many blocks
274+
*/
269275
int GetBlocksToMaturity() const;
270276
bool hashUnset() const { return (hashBlock.IsNull() || hashBlock == ABANDON_HASH); }
271277
bool isAbandoned() const { return (hashBlock == ABANDON_HASH); }
272278
void setAbandoned() { hashBlock = ABANDON_HASH; }
273279

274280
const uint256& GetHash() const { return tx->GetHash(); }
275281
bool IsCoinBase() const { return tx->IsCoinBase(); }
282+
bool IsImmatureCoinBase() const;
276283
};
277284

278285
//Get the marginal bytes of spending the specified output

0 commit comments

Comments
 (0)