Remove CCoinsViewCache::GetValueIn(…) #18859

pull practicalswift wants to merge 1 commits into bitcoin:master from practicalswift:GetValueIn changing 5 files +0 −27
  1. practicalswift commented at 6:20 pm on May 3, 2020: contributor

    Remove CCoinsViewCache::GetValueIn(...).

    Fixes #18858.

    It seems like GetValueIn was added in #748 (“Pay-to-script-hash (OP_EVAL replacement)”, merged in 2012) and the last use in validation code was removed in #8498 (“Near-Bugfix: Optimization: Minimize the number of times it is checked that no money…”, merged in 2017).

    CCoinsViewCache::GetValueIn(…) performs money summation like this:

     0CAmount CCoinsViewCache::GetValueIn(const CTransaction& tx) const
     1{
     2    if (tx.IsCoinBase())
     3        return 0;
     4
     5    CAmount nResult = 0;
     6    for (unsigned int i = 0; i < tx.vin.size(); i++)
     7        nResult += AccessCoin(tx.vin[i].prevout).out.nValue;
     8
     9    return nResult;
    10}
    

    Note that no check is done to make sure that the resulting nResult is such that it stays within the money bounds (MoneyRange(nResult)), or that the summation does not trigger a signed integer overflow.

    Proof of concept output:

    0coins.cpp:243:17: runtime error: signed integer overflow: 9223200000000000000 + 2100000000000000 cannot be represented in type 'long'
    1GetValueIn = -9221444073709551616
    

    Proof of concept code:

     0CMutableTransaction mutable_transaction;
     1mutable_transaction.vin.resize(4393);
     2
     3Coin coin;
     4coin.out.nValue = MAX_MONEY;
     5assert(MoneyRange(coin.out.nValue));
     6
     7CCoinsCacheEntry coins_cache_entry;
     8coins_cache_entry.coin = coin;
     9coins_cache_entry.flags = CCoinsCacheEntry::DIRTY;
    10
    11CCoinsView backend_coins_view;
    12CCoinsViewCache coins_view_cache{&backend_coins_view};
    13CCoinsMap coins_map;
    14coins_map.emplace(COutPoint{}, std::move(coins_cache_entry));
    15coins_view_cache.BatchWrite(coins_map, {});
    16
    17const CAmount total_value_in = coins_view_cache.GetValueIn(CTransaction{mutable_transaction});
    18std::cout << "GetValueIn = " << total_value_in << std::endl;
    
  2. practicalswift force-pushed on May 3, 2020
  3. Remove CCoinsViewCache::GetValueIn(...) b56607a89b
  4. practicalswift force-pushed on May 3, 2020
  5. DrahtBot added the label Tests on May 3, 2020
  6. DrahtBot added the label UTXO Db and Indexes on May 3, 2020
  7. MarcoFalke commented at 8:33 pm on May 3, 2020: member
    Please don’t remove the tests and benchmarks. Just because some imaginary input causes an integer overflow in a helper function doesn’t mean all tests using it should be removed. You can inline the function where it is used of move it to the test library in src/test/util
  8. practicalswift commented at 8:49 pm on May 3, 2020: contributor

    Just because some imaginary input causes an integer overflow in a helper function […]

    Leaving this one as up for grabs.

  9. practicalswift closed this on May 3, 2020

  10. sipa commented at 9:08 pm on May 3, 2020: member
    @MarcoFalke What is the point of keeping a function around that is not used outside of tests?
  11. MarcoFalke commented at 9:14 pm on May 3, 2020: member

    The function calls AccessCoin, which is used to benchmark coin access in the coins_caching benchmark.

    See also the docstring // Microbenchmark for simple accesses to a CCoinsViewCache database

    If the call to AccessCoin is removed, the you might as well remove the whole benchmark

  12. practicalswift commented at 9:14 pm on May 3, 2020: contributor
    @MarcoFalke Just so that I understand: would you be fine with anyone using CCoinsViewCache::GetValueIn(...) as it is currently formulated within Bitcoin Core?
  13. MarcoFalke commented at 9:16 pm on May 3, 2020: member
    Oh, wait the benchmark does the coins access via AreInputsStandard. My bad.
  14. MarcoFalke reopened this on May 3, 2020

  15. MarcoFalke commented at 9:17 pm on May 3, 2020: member
    ACK b56607a89ba112083f2b0a7b64ab18d66b26e2be
  16. MarcoFalke added this to the milestone 0.21.0 on May 3, 2020
  17. promag commented at 10:55 pm on May 3, 2020: member

    Code review ACK b56607a89ba112083f2b0a7b64ab18d66b26e2be.

    The removed CCoinsViewCache::GetValueIn is similar to AreInputsStandard and like @practicalswift mentions the summation is meh.

  18. DrahtBot commented at 11:04 pm on May 3, 2020: member

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #13525 (policy: Report reason inputs are nonstandard from AreInputsStandard by Empact)

    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.

  19. hebasto approved
  20. hebasto commented at 11:14 pm on May 3, 2020: member
    ACK b56607a89ba112083f2b0a7b64ab18d66b26e2be, I have not tested the code, but I have reviewed it and it looks OK, I agree it can be merged.
  21. jb55 commented at 0:52 am on May 4, 2020: member
    ACK b56607a89ba112083f2b0a7b64ab18d66b26e2be
  22. MarcoFalke commented at 11:49 am on May 4, 2020: member
    @practicalswift Sorry again that I misread the diff. Looks good to merge now, and thanks for removing the code.
  23. MarcoFalke merged this on May 4, 2020
  24. MarcoFalke closed this on May 4, 2020

  25. ComputerCraftr referenced this in commit 04dcc6ec3e on Jun 10, 2020
  26. ComputerCraftr referenced this in commit 514b330e83 on Jun 10, 2020
  27. Fabcien referenced this in commit 96fc9f405d on Jan 27, 2021
  28. practicalswift deleted the branch on Apr 10, 2021
  29. DrahtBot locked this on Aug 16, 2022

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-12-21 15:12 UTC

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