Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CMerkleTx::IsImmatureCoinBase method #13631

Merged
merged 1 commit into from Aug 25, 2018

Conversation

Projects
None yet
8 participants
@Empact
Copy link
Member

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

This comment has been minimized.

Copy link
Member Author

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;

@251Labs
Copy link
Contributor

left a comment

Nice refactor, utACK.

src/wallet/wallet.h Outdated
@@ -277,6 +277,8 @@ class CMerkleTx

const uint256& GetHash() const { return tx->GetHash(); }
bool IsCoinBase() const { return tx->IsCoinBase(); }
// note GetBlocksToMaturity is 0 for non-coinbase tx

This comment has been minimized.

Copy link
@251Labs

251Labs Jul 11, 2018

Contributor

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".

@@ -1924,7 +1924,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())

This comment has been minimized.

Copy link
@251Labs

251Labs Jul 11, 2018

Contributor

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.

src/wallet/wallet.h Outdated
@@ -277,6 +277,8 @@ class CMerkleTx

const uint256& GetHash() const { return tx->GetHash(); }
bool IsCoinBase() const { return tx->IsCoinBase(); }
// note GetBlocksToMaturity is 0 for non-coinbase tx
bool IsImmatureCoinBase() const { return GetBlocksToMaturity() > 0; }

This comment has been minimized.

Copy link
@251Labs

251Labs Jul 11, 2018

Contributor

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

This comment has been minimized.

Copy link
Member

commented Jul 12, 2018

Concept ACK

@promag

This comment has been minimized.

Copy link
Member

commented Jul 12, 2018

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

@Empact Empact force-pushed the Empact:is-immature-coinbase branch 2 times, most recently Jul 12, 2018

@Empact

This comment has been minimized.

Copy link
Member Author

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 Empact:is-immature-coinbase branch Jul 13, 2018

@Empact

This comment has been minimized.

Copy link
Member Author

commented Jul 13, 2018

Moved the GetBlocksToMaturity assert out into #13657

@Empact Empact force-pushed the Empact:is-immature-coinbase branch 2 times, most recently Jul 13, 2018

@Empact Empact force-pushed the Empact:is-immature-coinbase branch Jul 14, 2018

@Empact

This comment has been minimized.

Copy link
Member Author

commented Jul 14, 2018

Rebased for #13630, #13072

@251Labs

This comment has been minimized.

Copy link
Contributor

commented Jul 19, 2018

Nice, utACK 860e214

@promag

This comment has been minimized.

Copy link
Member

commented Jul 19, 2018

utACK 860e214.

@MarcoFalke

This comment has been minimized.

Copy link
Member

commented Jul 19, 2018

utACK 860e214f7ba899efae397205891181056adf3fc2

@DrahtBot DrahtBot referenced this pull request Jul 22, 2018

Merged

PSBT key path cleanups #13723

@Empact Empact force-pushed the Empact:is-immature-coinbase branch Jul 25, 2018

@Empact

This comment has been minimized.

Copy link
Member Author

commented Jul 25, 2018

Rebased for #12257

@DrahtBot DrahtBot removed the Needs rebase label Jul 25, 2018

@MarcoFalke

This comment has been minimized.

Copy link
Member

commented Jul 27, 2018

re-utACK bb653872be8d251c21ee32c2948100b7febbd477

@promag

This comment has been minimized.

Copy link
Member

commented Jul 27, 2018

re-utACK bb65387.

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.

@Empact Empact force-pushed the Empact:is-immature-coinbase branch to 23f4343 Jul 29, 2018

@Empact

This comment has been minimized.

Copy link
Member Author

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 DrahtBot referenced this pull request Jul 31, 2018

Closed

[wallet] Kill accounts #13825

@DrahtBot

This comment has been minimized.

Copy link
Contributor

commented Aug 1, 2018

Note to reviewers: This pull request conflicts with the following ones:
  • #14023 (Remove accounts rpcs by jnewbery)
  • #13083 (Add compile time checking for cs_main runtime locking assertions by practicalswift)
  • #10973 (Refactor: separate wallet from node by ryanofsky)
  • #10102 ([experimental] Multiprocess bitcoin by ryanofsky)

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 referenced this pull request Aug 22, 2018

Merged

Remove accounts rpcs #14023

@laanwj

This comment has been minimized.

Copy link
Member

commented Aug 25, 2018

utACK 23f4343

@laanwj laanwj merged commit 23f4343 into bitcoin:master Aug 25, 2018

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

laanwj added a commit that referenced this pull request Aug 25, 2018

Merge #13631: Add CMerkleTx::IsImmatureCoinBase method
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.