Skip to content

Commit 74a1152

Browse files
author
MarcoFalke
committed
Merge #18859: Remove CCoinsViewCache::GetValueIn(...)
b56607a Remove CCoinsViewCache::GetValueIn(...) (practicalswift) Pull request description: Remove `CCoinsViewCache::GetValueIn(...)`. Fixes #18858. It seems like `GetValueIn` was added in bitcoin-core#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: ```c++ CAmount CCoinsViewCache::GetValueIn(const CTransaction& tx) const { if (tx.IsCoinBase()) return 0; CAmount nResult = 0; for (unsigned int i = 0; i < tx.vin.size(); i++) nResult += AccessCoin(tx.vin[i].prevout).out.nValue; return nResult; } ``` 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: ``` coins.cpp:243:17: runtime error: signed integer overflow: 9223200000000000000 + 2100000000000000 cannot be represented in type 'long' GetValueIn = -9221444073709551616 ``` Proof of concept code: ```c++ CMutableTransaction mutable_transaction; mutable_transaction.vin.resize(4393); Coin coin; coin.out.nValue = MAX_MONEY; assert(MoneyRange(coin.out.nValue)); CCoinsCacheEntry coins_cache_entry; coins_cache_entry.coin = coin; coins_cache_entry.flags = CCoinsCacheEntry::DIRTY; CCoinsView backend_coins_view; CCoinsViewCache coins_view_cache{&backend_coins_view}; CCoinsMap coins_map; coins_map.emplace(COutPoint{}, std::move(coins_cache_entry)); coins_view_cache.BatchWrite(coins_map, {}); const CAmount total_value_in = coins_view_cache.GetValueIn(CTransaction{mutable_transaction}); std::cout << "GetValueIn = " << total_value_in << std::endl; ``` ACKs for top commit: MarcoFalke: ACK b56607a promag: Code review ACK b56607a. jb55: ACK b56607a hebasto: ACK b56607a, I have not tested the code, but I have reviewed it and it looks OK, I agree it can be merged. Tree-SHA512: 2c8402b5753ec96703d12c57c3eda8eccf999ed3519134a87faaf0838cfe44b94ef384296af2a524c06c8756c0245418d181af9083548e360905fac9d79215e6
2 parents afa577c + b56607a commit 74a1152

File tree

5 files changed

+0
-27
lines changed

5 files changed

+0
-27
lines changed

src/bench/ccoins_caching.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ static void CCoinsCaching(benchmark::State& state)
4747
while (state.KeepRunning()) {
4848
bool success = AreInputsStandard(tx_1, coins);
4949
assert(success);
50-
CAmount value = coins.GetValueIn(tx_1);
51-
assert(value == (50 + 21 + 22) * COIN);
5250
}
5351
ECC_Stop();
5452
}

src/coins.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,6 @@ unsigned int CCoinsViewCache::GetCacheSize() const {
233233
return cacheCoins.size();
234234
}
235235

236-
CAmount CCoinsViewCache::GetValueIn(const CTransaction& tx) const
237-
{
238-
if (tx.IsCoinBase())
239-
return 0;
240-
241-
CAmount nResult = 0;
242-
for (unsigned int i = 0; i < tx.vin.size(); i++)
243-
nResult += AccessCoin(tx.vin[i].prevout).out.nValue;
244-
245-
return nResult;
246-
}
247-
248236
bool CCoinsViewCache::HaveInputs(const CTransaction& tx) const
249237
{
250238
if (!tx.IsCoinBase()) {

src/coins.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -315,16 +315,6 @@ class CCoinsViewCache : public CCoinsViewBacked
315315
//! Calculate the size of the cache (in bytes)
316316
size_t DynamicMemoryUsage() const;
317317

318-
/**
319-
* Amount of bitcoins coming in to a transaction
320-
* Note that lightweight clients may not know anything besides the hash of previous transactions,
321-
* so may not be able to calculate this.
322-
*
323-
* @param[in] tx transaction for which we are checking input total
324-
* @return Sum of value of all inputs (scriptSigs)
325-
*/
326-
CAmount GetValueIn(const CTransaction& tx) const;
327-
328318
//! Check whether all prevouts of the transaction are present in the UTXO set represented by this view
329319
bool HaveInputs(const CTransaction& tx) const;
330320

src/primitives/transaction.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,6 @@ class CTransaction
324324

325325
// Return sum of txouts.
326326
CAmount GetValueOut() const;
327-
// GetValueIn() is a method on CCoinsViewCache, because
328-
// inputs must be known to compute value in.
329327

330328
/**
331329
* Get the total transaction size in bytes, including witness data.

src/test/transaction_tests.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ BOOST_AUTO_TEST_CASE(test_Get)
305305
t1.vout[0].scriptPubKey << OP_1;
306306

307307
BOOST_CHECK(AreInputsStandard(CTransaction(t1), coins));
308-
BOOST_CHECK_EQUAL(coins.GetValueIn(CTransaction(t1)), (50+21+22)*CENT);
309308
}
310309

311310
static void CreateCreditAndSpend(const FillableSigningProvider& keystore, const CScript& outscript, CTransactionRef& output, CMutableTransaction& input, bool success = true)

0 commit comments

Comments
 (0)