Add CMerkleTx::IsImmatureCoinBase method #13631

pull Empact wants to merge 1 commits into bitcoin:master from Empact:is-immature-coinbase changing 3 files +21 −11
  1. Empact commented at 5:20 am on July 11, 2018: member

    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.

  2. fanquake added the label Refactoring on Jul 11, 2018
  3. fanquake added the label Wallet on Jul 11, 2018
  4. in src/wallet/wallet.h:280 in e7a1d43644 outdated
    276@@ -277,6 +277,8 @@ class CMerkleTx
    277 
    278     const uint256& GetHash() const { return tx->GetHash(); }
    279     bool IsCoinBase() const { return tx->IsCoinBase(); }
    280+    // note GetBlocksToMaturity is 0 for non-coinbase tx
    


    l2a5b1 commented at 2:59 pm on July 11, 2018:
    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”.
  5. in src/wallet/wallet.cpp:1897 in e7a1d43644 outdated
    1923@@ -1924,7 +1924,7 @@ CAmount CWalletTx::GetDebit(const isminefilter& filter) const
    1924 CAmount CWalletTx::GetCredit(const isminefilter& filter) const
    1925 {
    1926     // Must wait until coinbase is safely deep enough in the chain before valuing it
    1927-    if (IsCoinBase() && GetBlocksToMaturity() > 0)
    1928+    if (IsImmatureCoinBase())
    


    l2a5b1 commented at 5:18 pm on July 11, 2018:

    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.

  6. in src/wallet/wallet.h:281 in e7a1d43644 outdated
    276@@ -277,6 +277,8 @@ class CMerkleTx
    277 
    278     const uint256& GetHash() const { return tx->GetHash(); }
    279     bool IsCoinBase() const { return tx->IsCoinBase(); }
    280+    // note GetBlocksToMaturity is 0 for non-coinbase tx
    281+    bool IsImmatureCoinBase() const { return GetBlocksToMaturity() > 0; }
    


    l2a5b1 commented at 5:33 pm on July 11, 2018:
    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.
  7. l2a5b1 commented at 5:35 pm on July 11, 2018: contributor
    Nice refactor, utACK.
  8. practicalswift commented at 2:28 pm on July 12, 2018: contributor
    Concept ACK
  9. promag commented at 3:30 pm on July 12, 2018: member
    Concept ACK. Makes the intention clear. Agree with @251Labs points.
  10. Empact force-pushed on Jul 12, 2018
  11. Empact force-pushed on Jul 12, 2018
  12. Empact commented at 6:13 pm on July 12, 2018: member

    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

  13. Empact force-pushed on Jul 13, 2018
  14. Empact commented at 4:48 pm on July 13, 2018: member
    Moved the GetBlocksToMaturity assert out into #13657
  15. Empact force-pushed on Jul 13, 2018
  16. Empact force-pushed on Jul 13, 2018
  17. Empact force-pushed on Jul 14, 2018
  18. Empact commented at 3:49 am on July 14, 2018: member
    Rebased for #13630, #13072
  19. l2a5b1 commented at 10:09 pm on July 19, 2018: contributor
    Nice, utACK 860e214
  20. promag commented at 10:24 pm on July 19, 2018: member
    utACK 860e214.
  21. MarcoFalke commented at 11:36 pm on July 19, 2018: member
    utACK 860e214f7ba899efae397205891181056adf3fc2
  22. DrahtBot added the label Needs rebase on Jul 24, 2018
  23. Empact force-pushed on Jul 25, 2018
  24. Empact commented at 2:57 pm on July 25, 2018: member
    Rebased for #12257
  25. DrahtBot removed the label Needs rebase on Jul 25, 2018
  26. MarcoFalke commented at 3:55 pm on July 27, 2018: member
    re-utACK bb653872be8d251c21ee32c2948100b7febbd477
  27. promag commented at 6:26 pm on July 27, 2018: member
    re-utACK bb65387.
  28. 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.
    23f4343781
  29. Empact force-pushed on Jul 29, 2018
  30. Empact commented at 11:51 pm on July 29, 2018: member

    Noticed the whitespace was off. Diff:

     0diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
     1index fc6f03a16..540a7b0fc 100644
     2--- a/src/wallet/wallet.cpp
     3+++ b/src/wallet/wallet.cpp
     4@@ -1926,8 +1926,7 @@ CAmount CWalletTx::GetCredit(const isminefilter& filter) const
     5 
     6 CAmount CWalletTx::GetImmatureCredit(bool fUseCache) const
     7 {
     8-    if (IsImmatureCoinBase() && IsInMainChain())
     9-    {
    10+    if (IsImmatureCoinBase() && IsInMainChain()) {
    11         if (fUseCache && fImmatureCreditCached)
    12             return nImmatureCreditCached;
    13         nImmatureCreditCached = pwallet->GetCredit(*tx, ISMINE_SPENDABLE);
    14@@ -1985,8 +1984,7 @@ CAmount CWalletTx::GetAvailableCredit(bool fUseCache, const isminefilter& filter
    15 
    16 CAmount CWalletTx::GetImmatureWatchOnlyCredit(const bool fUseCache) const
    17 {
    18-    if (IsImmatureCoinBase() && IsInMainChain())
    19-    {
    20+    if (IsImmatureCoinBase() && IsInMainChain()) {
    21         if (fUseCache && fImmatureWatchCreditCached)
    22             return nImmatureWatchCreditCached;
    23         nImmatureWatchCreditCached = pwallet->GetCredit(*tx, ISMINE_WATCH_ONLY);
    24@@ -4399,8 +4397,8 @@ int CMerkleTx::GetBlocksToMaturity() const
    25 
    26 bool CMerkleTx::IsImmatureCoinBase() const
    27 {
    28-   // note GetBlocksToMaturity is 0 for non-coinbase tx
    29-   return GetBlocksToMaturity() > 0;
    30+    // note GetBlocksToMaturity is 0 for non-coinbase tx
    31+    return GetBlocksToMaturity() > 0;
    32 }
    33 
    34 bool CWalletTx::AcceptToMemoryPool(const CAmount& nAbsurdFee, CValidationState& state)
    
  31. DrahtBot commented at 0:27 am on August 1, 2018: member
    • #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.

  32. laanwj commented at 2:53 pm on August 25, 2018: member
    utACK 23f434378153cf764230066662f3ec3ad614ff30
  33. laanwj merged this on Aug 25, 2018
  34. laanwj closed this on Aug 25, 2018

  35. laanwj referenced this in commit 776fa60c4b on Aug 25, 2018
  36. Munkybooty referenced this in commit 119f6b42b7 on Jun 30, 2021
  37. Munkybooty referenced this in commit 0ee85e49ba on Jun 30, 2021
  38. Munkybooty referenced this in commit e8a5281918 on Jul 1, 2021
  39. Munkybooty referenced this in commit 527007bf01 on Jul 1, 2021
  40. Munkybooty referenced this in commit d48320aa4d on Jul 2, 2021
  41. Munkybooty referenced this in commit 97470e86c2 on Jul 2, 2021
  42. Munkybooty referenced this in commit 19b9e74026 on Jul 2, 2021
  43. MarcoFalke locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-11-17 06:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me