Skip to content

Commit

Permalink
Merge bitcoin#13631: Add CMerkleTx::IsImmatureCoinBase method
Browse files Browse the repository at this point in the history
23f4343 Add CMerkleTx::IsImmatureCoinBase method (Ben Woosley)

Pull request description:

  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.

Tree-SHA512: 4470d07404a0707144f9827b9a94c5c4905f23ee6f9248edc5df599a59d28e21ea0201d8abe5d5d73b39cb05b60c861ea8e04767eef04433e2ee95dcfed653ee

# Conflicts:
#	src/wallet/wallet.h
  • Loading branch information
laanwj authored and Munkybooty committed Jun 30, 2021
1 parent e64ed43 commit 119f6b4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1785,7 +1785,7 @@ static void ListTransactions(CWallet * const pwallet, const CWalletTx& wtx, cons
{
if (wtx.GetDepthInMainChain() < 1)
entry.pushKV("category", "orphan");
else if (wtx.GetBlocksToMaturity() > 0)
else if (wtx.IsImmatureCoinBase())
entry.pushKV("category", "immature");
else
entry.pushKV("category", "generate");
Expand Down Expand Up @@ -2098,7 +2098,7 @@ static UniValue listaccounts(const JSONRPCRequest& request)
std::list<COutputEntry> listReceived;
std::list<COutputEntry> listSent;
int nDepth = wtx.GetDepthInMainChain();
if (wtx.GetBlocksToMaturity() > 0 || nDepth < 0)
if (wtx.IsImmatureCoinBase() || nDepth < 0)
continue;
wtx.GetAmounts(listReceived, listSent, nFee, strSentAccount, includeWatchonly);
mapAccountBalances[strSentAccount] -= nFee;
Expand Down
21 changes: 12 additions & 9 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2347,7 +2347,7 @@ CAmount CWalletTx::GetDebit(const isminefilter& filter) const
CAmount CWalletTx::GetCredit(const isminefilter& filter) const
{
// Must wait until coinbase is safely deep enough in the chain before valuing it
if (IsCoinBase() && GetBlocksToMaturity() > 0)
if (IsImmatureCoinBase())
return 0;

CAmount credit = 0;
Expand Down Expand Up @@ -2379,8 +2379,7 @@ CAmount CWalletTx::GetCredit(const isminefilter& filter) const

CAmount CWalletTx::GetImmatureCredit(bool fUseCache) const
{
if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
{
if (IsImmatureCoinBase() && IsInMainChain()) {
if (fUseCache && fImmatureCreditCached)
return nImmatureCreditCached;
nImmatureCreditCached = pwallet->GetCredit(*tx, ISMINE_SPENDABLE);
Expand All @@ -2397,7 +2396,7 @@ CAmount CWalletTx::GetAvailableCredit(bool fUseCache, const isminefilter& filter
return 0;

// Must wait until coinbase is safely deep enough in the chain before valuing it
if (IsCoinBase() && GetBlocksToMaturity() > 0)
if (IsImmatureCoinBase())
return 0;

CAmount* cache = nullptr;
Expand Down Expand Up @@ -2438,8 +2437,7 @@ CAmount CWalletTx::GetAvailableCredit(bool fUseCache, const isminefilter& filter

CAmount CWalletTx::GetImmatureWatchOnlyCredit(const bool fUseCache) const
{
if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
{
if (IsImmatureCoinBase() && IsInMainChain()) {
if (fUseCache && fImmatureWatchCreditCached)
return nImmatureWatchCreditCached;
nImmatureWatchCreditCached = pwallet->GetCredit(*tx, ISMINE_WATCH_ONLY);
Expand Down Expand Up @@ -2848,7 +2846,7 @@ CAmount CWallet::GetLegacyBalance(const isminefilter& filter, int minDepth, cons
for (const auto& entry : mapWallet) {
const CWalletTx& wtx = entry.second;
const int depth = wtx.GetDepthInMainChain();
if (depth < 0 || !CheckFinalTx(*wtx.tx) || wtx.GetBlocksToMaturity() > 0) {
if (depth < 0 || !CheckFinalTx(*wtx.tx) || wtx.IsImmatureCoinBase()) {
continue;
}

Expand Down Expand Up @@ -2908,7 +2906,7 @@ void CWallet::AvailableCoins(std::vector<COutput> &vCoins, bool fOnlySafe, const
if (!CheckFinalTx(*pcoin->tx))
continue;

if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
if (pcoin->IsImmatureCoinBase())
continue;

int nDepth = pcoin->GetDepthInMainChain();
Expand Down Expand Up @@ -4455,7 +4453,7 @@ std::map<CTxDestination, CAmount> CWallet::GetAddressBalances()
if (!pcoin->IsTrusted())
continue;

if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
if (pcoin->IsImmatureCoinBase())
continue;

int nDepth = pcoin->GetDepthInMainChain();
Expand Down Expand Up @@ -5549,6 +5547,11 @@ int CMerkleTx::GetBlocksToMaturity() const
return std::max(0, (COINBASE_MATURITY+1) - chain_depth);
}

bool CMerkleTx::IsImmatureCoinBase() const
{
// note GetBlocksToMaturity is 0 for non-coinbase tx
return GetBlocksToMaturity() > 0;
}

bool CWalletTx::AcceptToMemoryPool(const CAmount& nAbsurdFee, CValidationState& state)
{
Expand Down
7 changes: 7 additions & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,20 @@ class CMerkleTx
bool IsInMainChain() const { return GetDepthInMainChain() > 0; }
bool IsLockedByInstantSend() const;
bool IsChainLocked() const;

/**
* @return number of blocks to maturity for this transaction:
* 0 : is not a coinbase transaction, or is a mature coinbase transaction
* >0 : is a coinbase transaction which matures in this many blocks
*/
int GetBlocksToMaturity() const;
bool hashUnset() const { return (hashBlock.IsNull() || hashBlock == ABANDON_HASH); }
bool isAbandoned() const { return (hashBlock == ABANDON_HASH); }
void setAbandoned() { hashBlock = ABANDON_HASH; }

const uint256& GetHash() const { return tx->GetHash(); }
bool IsCoinBase() const { return tx->IsCoinBase(); }
bool IsImmatureCoinBase() const;
};

//Get the marginal bytes of spending the specified output
Expand Down

0 comments on commit 119f6b4

Please sign in to comment.