Skip to content

Conversation

@Empact
Copy link
Contributor

@Empact Empact commented Jul 11, 2018

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.

@Empact
Copy link
Contributor Author

Empact commented Jul 11, 2018

For reference:

bitcoin/src/wallet/wallet.cpp

Lines 4435 to 4438 in 062738c

int CMerkleTx::GetBlocksToMaturity() const
{
if (!IsCoinBase())
return 0;

Copy link
Contributor

@l2a5b1 l2a5b1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice refactor, utACK.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nits: Maybe move the comment to the function body, because it's an implementation detail of IsImmatureCoinbase; and update the comment by replacing "is" with "returns".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems safe to optimize this boolean expression and remove the IsCoinBase() condition in IsImmatureCoinBase, because GetBlocksToMaturity already calls IsCoinBase, which effectively makes this condition redundant.

I do however appreciate the defensiveness of having the redundantIsCoinBase() condition; it is not specified that GetBlocksToMaturity must return 0 if it is not a coinbase transaction.

Therefore, if we optimize the boolean expression in IsImmatureCoinBase and fully rely on the result of GetBlocksToMaturity, I would like to suggest to add a comment to the interface documentation of the CMerkleTx::GetBlocksToMaturity method that explicitly states the postcondition that GetBlocksToMaturity must return 0 if it is not a coinbase transaction.

Copy link
Contributor

@l2a5b1 l2a5b1 Jul 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are going to rely on the result of GetBlocksToMaturity, and not defensively call IsCoinBase(), a unit test would be nice to verify the expected behaviour.

@practicalswift
Copy link
Contributor

Concept ACK

@promag
Copy link
Contributor

promag commented Jul 12, 2018

Concept ACK. Makes the intention clear. Agree with @251labs points.

@Empact Empact force-pushed the is-immature-coinbase branch 2 times, most recently from d0fe31e to 1503a9a Compare July 12, 2018 18:10
@Empact
Copy link
Contributor Author

Empact commented Jul 12, 2018

Added docs, moved the implementations together.

I also noticed that the result of GetBlocksToMaturity would be inaccurate if the TX was marked as conflicted - my impression is that coinbase transactions can't be conflicting, so I added an assert to make that expectation explicit:
https://github.com/bitcoin/bitcoin/pull/13631/files#diff-b2bb174788c7409b671c46ccc86034bdR4440

@Empact Empact force-pushed the is-immature-coinbase branch from 1503a9a to 192075f Compare July 13, 2018 16:44
@Empact
Copy link
Contributor Author

Empact commented Jul 13, 2018

Moved the GetBlocksToMaturity assert out into #13657

@Empact Empact force-pushed the is-immature-coinbase branch 2 times, most recently from 86a1caf to 328d830 Compare July 13, 2018 16:50
@Empact Empact force-pushed the is-immature-coinbase branch from 328d830 to 860e214 Compare July 14, 2018 03:44
@Empact
Copy link
Contributor Author

Empact commented Jul 14, 2018

Rebased for #13630, #13072

@l2a5b1
Copy link
Contributor

l2a5b1 commented Jul 19, 2018

Nice, utACK 860e214

@promag
Copy link
Contributor

promag commented Jul 19, 2018

utACK 860e214.

@maflcko
Copy link
Member

maflcko commented Jul 19, 2018

utACK 860e214f7ba899efae397205891181056adf3fc2

@Empact
Copy link
Contributor Author

Empact commented Jul 25, 2018

Rebased for #12257

@maflcko
Copy link
Member

maflcko commented Jul 27, 2018

re-utACK bb653872be8d251c21ee32c2948100b7febbd477

@promag
Copy link
Contributor

promag commented Jul 27, 2018

re-utACK bb65387.

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.
@Empact Empact force-pushed the is-immature-coinbase branch from bb65387 to 23f4343 Compare July 29, 2018 23:50
@Empact
Copy link
Contributor Author

Empact commented Jul 29, 2018

Noticed the whitespace was off. Diff:

diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index fc6f03a16..540a7b0fc 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -1926,8 +1926,7 @@ CAmount CWalletTx::GetCredit(const isminefilter& filter) const
 
 CAmount CWalletTx::GetImmatureCredit(bool fUseCache) const
 {
-    if (IsImmatureCoinBase() && IsInMainChain())
-    {
+    if (IsImmatureCoinBase() && IsInMainChain()) {
         if (fUseCache && fImmatureCreditCached)
             return nImmatureCreditCached;
         nImmatureCreditCached = pwallet->GetCredit(*tx, ISMINE_SPENDABLE);
@@ -1985,8 +1984,7 @@ CAmount CWalletTx::GetAvailableCredit(bool fUseCache, const isminefilter& filter
 
 CAmount CWalletTx::GetImmatureWatchOnlyCredit(const bool fUseCache) const
 {
-    if (IsImmatureCoinBase() && IsInMainChain())
-    {
+    if (IsImmatureCoinBase() && IsInMainChain()) {
         if (fUseCache && fImmatureWatchCreditCached)
             return nImmatureWatchCreditCached;
         nImmatureWatchCreditCached = pwallet->GetCredit(*tx, ISMINE_WATCH_ONLY);
@@ -4399,8 +4397,8 @@ int CMerkleTx::GetBlocksToMaturity() const
 
 bool CMerkleTx::IsImmatureCoinBase() const
 {
-   // note GetBlocksToMaturity is 0 for non-coinbase tx
-   return GetBlocksToMaturity() > 0;
+    // note GetBlocksToMaturity is 0 for non-coinbase tx
+    return GetBlocksToMaturity() > 0;
 }
 
 bool CWalletTx::AcceptToMemoryPool(const CAmount& nAbsurdFee, CValidationState& state)

@DrahtBot
Copy link
Contributor

DrahtBot commented Aug 1, 2018

Note to reviewers: This pull request conflicts with the following ones:

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@DrahtBot DrahtBot mentioned this pull request Aug 22, 2018
@laanwj
Copy link
Member

laanwj commented Aug 25, 2018

utACK 23f4343

@laanwj laanwj merged commit 23f4343 into bitcoin:master Aug 25, 2018
laanwj added a commit that referenced this pull request Aug 25, 2018
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
Munkybooty pushed a commit to Munkybooty/dash that referenced this pull request Jul 2, 2021
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
Munkybooty pushed a commit to Munkybooty/dash that referenced this pull request Jul 2, 2021
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
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants