Skip to content

Commit 0733c1b

Browse files
committed
Refactor: move GetValueIn(tx) to tx.GetValueIn()
GetValueIn makes more sense as a CTransaction member.
1 parent 98c7c8f commit 0733c1b

File tree

10 files changed

+33
-36
lines changed

10 files changed

+33
-36
lines changed

src/coins.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ class CCoinsViewCache : public CCoinsViewBacked
340340
341341
@param[in] tx transaction for which we are checking input total
342342
@return Sum of value of all inputs (scriptSigs)
343-
@see CTransaction::FetchInputs
344343
*/
345344
int64_t GetValueIn(const CTransaction& tx);
346345

src/core.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ bool CTransaction::IsNewerThan(const CTransaction& old) const
106106
return fNewer;
107107
}
108108

109+
int64_t CTransaction::GetValueOut() const
110+
{
111+
int64_t nValueOut = 0;
112+
BOOST_FOREACH(const CTxOut& txout, vout)
113+
{
114+
nValueOut += txout.nValue;
115+
if (!MoneyRange(txout.nValue) || !MoneyRange(nValueOut))
116+
throw std::runtime_error("CTransaction::GetValueOut() : value out of range");
117+
}
118+
return nValueOut;
119+
}
120+
109121
std::string CTransaction::ToString() const
110122
{
111123
std::string str;

src/core.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
class CTransaction;
1616

17+
/** No amount larger than this (in satoshi) is valid */
18+
static const int64_t MAX_MONEY = 21000000 * COIN;
19+
inline bool MoneyRange(int64_t nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
20+
1721
/** An outpoint - a combination of a transaction hash and an index n into its vout */
1822
class COutPoint
1923
{
@@ -217,6 +221,11 @@ class CTransaction
217221
uint256 GetHash() const;
218222
bool IsNewerThan(const CTransaction& old) const;
219223

224+
// Return sum of txouts.
225+
int64_t GetValueOut() const;
226+
// GetValueIn() is a method on CCoinsViewCache, because
227+
// inputs must be known to compute value in.
228+
220229
bool IsCoinBase() const
221230
{
222231
return (vin.size() == 1 && vin[0].prevout.IsNull());

src/main.cpp

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -379,21 +379,6 @@ bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
379379
return true;
380380
}
381381

382-
/** Amount of bitcoins spent by the transaction.
383-
@return sum of all outputs (note: does not include fees)
384-
*/
385-
int64_t GetValueOut(const CTransaction& tx)
386-
{
387-
int64_t nValueOut = 0;
388-
BOOST_FOREACH(const CTxOut& txout, tx.vout)
389-
{
390-
nValueOut += txout.nValue;
391-
if (!MoneyRange(txout.nValue) || !MoneyRange(nValueOut))
392-
throw std::runtime_error("GetValueOut() : value out of range");
393-
}
394-
return nValueOut;
395-
}
396-
397382
//
398383
// Check transaction inputs, and make sure any
399384
// pay-to-script-hash transactions are evaluating IsStandard scripts
@@ -717,7 +702,7 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
717702
// you should add code here to check that the transaction does a
718703
// reasonable number of ECDSA signature verifications.
719704

720-
int64_t nFees = view.GetValueIn(tx)-GetValueOut(tx);
705+
int64_t nFees = view.GetValueIn(tx)-tx.GetValueOut();
721706
unsigned int nSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
722707

723708
// Don't accept it if it can't get into a block
@@ -1342,12 +1327,12 @@ bool CheckInputs(const CTransaction& tx, CValidationState &state, CCoinsViewCach
13421327

13431328
}
13441329

1345-
if (nValueIn < GetValueOut(tx))
1330+
if (nValueIn < tx.GetValueOut())
13461331
return state.DoS(100, error("CheckInputs() : %s value in < value out", tx.GetHash().ToString().c_str()),
13471332
REJECT_INVALID, "in < out");
13481333

13491334
// Tally transaction fees
1350-
int64_t nTxFee = nValueIn - GetValueOut(tx);
1335+
int64_t nTxFee = nValueIn - tx.GetValueOut();
13511336
if (nTxFee < 0)
13521337
return state.DoS(100, error("CheckInputs() : %s nTxFee < 0", tx.GetHash().ToString().c_str()),
13531338
REJECT_INVALID, "fee < 0");
@@ -1600,7 +1585,7 @@ bool ConnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, C
16001585
REJECT_INVALID, "too many sigops");
16011586
}
16021587

1603-
nFees += view.GetValueIn(tx)-GetValueOut(tx);
1588+
nFees += view.GetValueIn(tx)-tx.GetValueOut();
16041589

16051590
std::vector<CScriptCheck> vChecks;
16061591
if (!CheckInputs(tx, state, view, fScriptChecks, flags, nScriptCheckThreads ? &vChecks : NULL))
@@ -1620,10 +1605,10 @@ bool ConnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, C
16201605
if (fBenchmark)
16211606
LogPrintf("- Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin)\n", (unsigned)block.vtx.size(), 0.001 * nTime, 0.001 * nTime / block.vtx.size(), nInputs <= 1 ? 0 : 0.001 * nTime / (nInputs-1));
16221607

1623-
if (GetValueOut(block.vtx[0]) > GetBlockValue(pindex->nHeight, nFees))
1608+
if (block.vtx[0].GetValueOut() > GetBlockValue(pindex->nHeight, nFees))
16241609
return state.DoS(100,
16251610
error("ConnectBlock() : coinbase pays too much (actual=%"PRId64" vs limit=%"PRId64")",
1626-
GetValueOut(block.vtx[0]), GetBlockValue(pindex->nHeight, nFees)),
1611+
block.vtx[0].GetValueOut(), GetBlockValue(pindex->nHeight, nFees)),
16271612
REJECT_INVALID, "coinbase too large");
16281613

16291614
if (!control.Wait())

src/main.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
4949
static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB
5050
/** The pre-allocation chunk size for rev?????.dat files (since 0.8) */
5151
static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
52-
/** No amount larger than this (in satoshi) is valid */
53-
static const int64_t MAX_MONEY = 21000000 * COIN;
54-
inline bool MoneyRange(int64_t nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
5552
/** Coinbase transaction outputs can only be spent after this number of new blocks (network rule) */
5653
static const int COINBASE_MATURITY = 100;
5754
/** Threshold for nLockTime: below this value it is interpreted as block number, otherwise as UNIX timestamp. */
@@ -320,11 +317,6 @@ bool IsStandardTx(const CTransaction& tx, std::string& reason);
320317

321318
bool IsFinalTx(const CTransaction &tx, int nBlockHeight = 0, int64_t nBlockTime = 0);
322319

323-
/** Amount of bitcoins spent by the transaction.
324-
@return sum of all outputs (note: does not include fees)
325-
*/
326-
int64_t GetValueOut(const CTransaction& tx);
327-
328320
/** Undo information for a CBlock */
329321
class CBlockUndo
330322
{

src/miner.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
261261
// This is a more accurate fee-per-kilobyte than is used by the client code, because the
262262
// client code rounds up the size to the nearest 1K. That's good, because it gives an
263263
// incentive to create smaller transactions.
264-
double dFeePerKb = double(nTotalIn-GetValueOut(tx)) / (double(nTxSize)/1000.0);
264+
double dFeePerKb = double(nTotalIn-tx.GetValueOut()) / (double(nTxSize)/1000.0);
265265

266266
if (porphan)
267267
{
@@ -318,7 +318,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
318318
if (!view.HaveInputs(tx))
319319
continue;
320320

321-
int64_t nTxFees = view.GetValueIn(tx)-GetValueOut(tx);
321+
int64_t nTxFees = view.GetValueIn(tx)-tx.GetValueOut();
322322

323323
nTxSigOps += GetP2SHSigOpCount(tx, view);
324324
if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)

src/qt/transactiondesc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx, int vout, int u
194194
strHTML += "<b>" + tr("Credit") + ":</b> " + BitcoinUnits::formatWithUnit(unit, nValue) + "<br>";
195195
}
196196

197-
int64_t nTxFee = nDebit - GetValueOut(wtx);
197+
int64_t nTxFee = nDebit - wtx.GetValueOut();
198198
if (nTxFee > 0)
199199
strHTML += "<b>" + tr("Transaction fee") + ":</b> " + BitcoinUnits::formatWithUnit(unit, -nTxFee) + "<br>";
200200
}

src/qt/transactionrecord.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *
9595
//
9696
// Debit
9797
//
98-
int64_t nTxFee = nDebit - GetValueOut(wtx);
98+
int64_t nTxFee = nDebit - wtx.GetValueOut();
9999

100100
for (unsigned int nOut = 0; nOut < wtx.vout.size(); nOut++)
101101
{

src/rpcwallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1673,7 +1673,7 @@ Value gettransaction(const Array& params, bool fHelp)
16731673
int64_t nCredit = wtx.GetCredit();
16741674
int64_t nDebit = wtx.GetDebit();
16751675
int64_t nNet = nCredit - nDebit;
1676-
int64_t nFee = (wtx.IsFromMe() ? GetValueOut(wtx) - nDebit : 0);
1676+
int64_t nFee = (wtx.IsFromMe() ? wtx.GetValueOut() - nDebit : 0);
16771677

16781678
entry.push_back(Pair("amount", ValueFromAmount(nNet - nFee)));
16791679
if (wtx.IsFromMe())

src/wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ void CWalletTx::GetAmounts(list<pair<CTxDestination, int64_t> >& listReceived,
655655
int64_t nDebit = GetDebit();
656656
if (nDebit > 0) // debit>0 means we signed/sent this transaction
657657
{
658-
int64_t nValueOut = GetValueOut(*this);
658+
int64_t nValueOut = GetValueOut();
659659
nFee = nDebit - nValueOut;
660660
}
661661

0 commit comments

Comments
 (0)