Skip to content

Commit 3fd192f

Browse files
committed
Merge pull request #4234
c122f55 qt: Register CAmount metatype (Wladimir J. van der Laan) a372168 Use a typedef for monetary values (Mark Friedenbach)
2 parents b255511 + c122f55 commit 3fd192f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+402
-357
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ BITCOIN_CORE_H = \
6565
addrman.h \
6666
alert.h \
6767
allocators.h \
68+
amount.h \
6869
base58.h \
6970
bloom.h \
7071
chain.h \

src/amount.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2014 The Bitcoin developers
3+
// Distributed under the MIT/X11 software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#ifndef BITCOIN_AMOUNT_H
7+
#define BITCOIN_AMOUNT_H
8+
9+
#include <stdint.h>
10+
11+
typedef int64_t CAmount;
12+
13+
#endif

src/bitcoin-tx.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ static void MutateTxAddOutAddr(CMutableTransaction& tx, const string& strInput)
214214

215215
// extract and validate VALUE
216216
string strValue = strInput.substr(0, pos);
217-
int64_t value;
217+
CAmount value;
218218
if (!ParseMoney(strValue, value))
219219
throw runtime_error("invalid TX output value");
220220

@@ -242,7 +242,7 @@ static void MutateTxAddOutScript(CMutableTransaction& tx, const string& strInput
242242

243243
// extract and validate VALUE
244244
string strValue = strInput.substr(0, pos);
245-
int64_t value;
245+
CAmount value;
246246
if (!ParseMoney(strValue, value))
247247
throw runtime_error("invalid TX output value");
248248

src/coins.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,12 @@ const CTxOut &CCoinsViewCache::GetOutputFor(const CTxIn& input) const
171171
return coins->vout[input.prevout.n];
172172
}
173173

174-
int64_t CCoinsViewCache::GetValueIn(const CTransaction& tx) const
174+
CAmount CCoinsViewCache::GetValueIn(const CTransaction& tx) const
175175
{
176176
if (tx.IsCoinBase())
177177
return 0;
178178

179-
int64_t nResult = 0;
179+
CAmount nResult = 0;
180180
for (unsigned int i = 0; i < tx.vin.size(); i++)
181181
nResult += GetOutputFor(tx.vin[i]).nValue;
182182

src/coins.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ struct CCoinsStats
266266
uint64_t nTransactionOutputs;
267267
uint64_t nSerializedSize;
268268
uint256 hashSerialized;
269-
int64_t nTotalAmount;
269+
CAmount nTotalAmount;
270270

271271
CCoinsStats() : nHeight(0), hashBlock(0), nTransactions(0), nTransactionOutputs(0), nSerializedSize(0), hashSerialized(0), nTotalAmount(0) {}
272272
};
@@ -367,7 +367,7 @@ class CCoinsViewCache : public CCoinsViewBacked
367367
@param[in] tx transaction for which we are checking input total
368368
@return Sum of value of all inputs (scriptSigs)
369369
*/
370-
int64_t GetValueIn(const CTransaction& tx) const;
370+
CAmount GetValueIn(const CTransaction& tx) const;
371371

372372
// Check whether all prevouts of the transaction are present in the UTXO set represented by this view
373373
bool HaveInputs(const CTransaction& tx) const;

src/core.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ std::string CTxIn::ToString() const
4343
return str;
4444
}
4545

46-
CTxOut::CTxOut(int64_t nValueIn, CScript scriptPubKeyIn)
46+
CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
4747
{
4848
nValue = nValueIn;
4949
scriptPubKey = scriptPubKeyIn;
@@ -59,17 +59,17 @@ std::string CTxOut::ToString() const
5959
return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, scriptPubKey.ToString().substr(0,30));
6060
}
6161

62-
CFeeRate::CFeeRate(int64_t nFeePaid, size_t nSize)
62+
CFeeRate::CFeeRate(const CAmount& nFeePaid, size_t nSize)
6363
{
6464
if (nSize > 0)
6565
nSatoshisPerK = nFeePaid*1000/nSize;
6666
else
6767
nSatoshisPerK = 0;
6868
}
6969

70-
int64_t CFeeRate::GetFee(size_t nSize) const
70+
CAmount CFeeRate::GetFee(size_t nSize) const
7171
{
72-
int64_t nFee = nSatoshisPerK*nSize / 1000;
72+
CAmount nFee = nSatoshisPerK*nSize / 1000;
7373

7474
if (nFee == 0 && nSatoshisPerK > 0)
7575
nFee = nSatoshisPerK;
@@ -110,9 +110,9 @@ CTransaction& CTransaction::operator=(const CTransaction &tx) {
110110
return *this;
111111
}
112112

113-
int64_t CTransaction::GetValueOut() const
113+
CAmount CTransaction::GetValueOut() const
114114
{
115-
int64_t nValueOut = 0;
115+
CAmount nValueOut = 0;
116116
BOOST_FOREACH(const CTxOut& txout, vout)
117117
{
118118
nValueOut += txout.nValue;

src/core.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef BITCOIN_CORE_H
77
#define BITCOIN_CORE_H
88

9+
#include "amount.h"
910
#include "script/compressor.h"
1011
#include "script/script.h"
1112
#include "serialize.h"
@@ -19,8 +20,8 @@ static const int64_t COIN = 100000000;
1920
static const int64_t CENT = 1000000;
2021

2122
/** No amount larger than this (in satoshi) is valid */
22-
static const int64_t MAX_MONEY = 21000000 * COIN;
23-
inline bool MoneyRange(int64_t nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
23+
static const CAmount MAX_MONEY = 21000000 * COIN;
24+
inline bool MoneyRange(const CAmount& nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
2425

2526
/** An outpoint - a combination of a transaction hash and an index n into its vout */
2627
class COutPoint
@@ -129,15 +130,15 @@ class CTxIn
129130
class CFeeRate
130131
{
131132
private:
132-
int64_t nSatoshisPerK; // unit is satoshis-per-1,000-bytes
133+
CAmount nSatoshisPerK; // unit is satoshis-per-1,000-bytes
133134
public:
134135
CFeeRate() : nSatoshisPerK(0) { }
135-
explicit CFeeRate(int64_t _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) { }
136-
CFeeRate(int64_t nFeePaid, size_t nSize);
136+
explicit CFeeRate(const CAmount& _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) { }
137+
CFeeRate(const CAmount& nFeePaid, size_t nSize);
137138
CFeeRate(const CFeeRate& other) { nSatoshisPerK = other.nSatoshisPerK; }
138139

139-
int64_t GetFee(size_t size) const; // unit returned is satoshis
140-
int64_t GetFeePerK() const { return GetFee(1000); } // satoshis-per-1000-bytes
140+
CAmount GetFee(size_t size) const; // unit returned is satoshis
141+
CAmount GetFeePerK() const { return GetFee(1000); } // satoshis-per-1000-bytes
141142

142143
friend bool operator<(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK < b.nSatoshisPerK; }
143144
friend bool operator>(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK > b.nSatoshisPerK; }
@@ -161,15 +162,15 @@ class CFeeRate
161162
class CTxOut
162163
{
163164
public:
164-
int64_t nValue;
165+
CAmount nValue;
165166
CScript scriptPubKey;
166167

167168
CTxOut()
168169
{
169170
SetNull();
170171
}
171172

172-
CTxOut(int64_t nValueIn, CScript scriptPubKeyIn);
173+
CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn);
173174

174175
ADD_SERIALIZE_METHODS;
175176

@@ -276,7 +277,7 @@ class CTransaction
276277
}
277278

278279
// Return sum of txouts.
279-
int64_t GetValueOut() const;
280+
CAmount GetValueOut() const;
280281
// GetValueIn() is a method on CCoinsViewCache, because
281282
// inputs must be known to compute value in.
282283

src/init.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ bool AppInit2(boost::thread_group& threadGroup)
662662
// cost to you of processing a transaction.
663663
if (mapArgs.count("-minrelaytxfee"))
664664
{
665-
int64_t n = 0;
665+
CAmount n = 0;
666666
if (ParseMoney(mapArgs["-minrelaytxfee"], n) && n > 0)
667667
::minRelayTxFee = CFeeRate(n);
668668
else
@@ -672,15 +672,15 @@ bool AppInit2(boost::thread_group& threadGroup)
672672
#ifdef ENABLE_WALLET
673673
if (mapArgs.count("-mintxfee"))
674674
{
675-
int64_t n = 0;
675+
CAmount n = 0;
676676
if (ParseMoney(mapArgs["-mintxfee"], n) && n > 0)
677677
CWallet::minTxFee = CFeeRate(n);
678678
else
679679
return InitError(strprintf(_("Invalid amount for -mintxfee=<amount>: '%s'"), mapArgs["-mintxfee"]));
680680
}
681681
if (mapArgs.count("-paytxfee"))
682682
{
683-
int64_t nFeePerK = 0;
683+
CAmount nFeePerK = 0;
684684
if (!ParseMoney(mapArgs["-paytxfee"], nFeePerK))
685685
return InitError(strprintf(_("Invalid amount for -paytxfee=<amount>: '%s'"), mapArgs["-paytxfee"]));
686686
if (nFeePerK > nHighTransactionFeeWarning)

src/main.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ bool CheckTransaction(const CTransaction& tx, CValidationState &state)
728728
REJECT_INVALID, "bad-txns-oversize");
729729

730730
// Check for negative or overflow output values
731-
int64_t nValueOut = 0;
731+
CAmount nValueOut = 0;
732732
BOOST_FOREACH(const CTxOut& txout, tx.vout)
733733
{
734734
if (txout.nValue < 0)
@@ -770,19 +770,19 @@ bool CheckTransaction(const CTransaction& tx, CValidationState &state)
770770
return true;
771771
}
772772

773-
int64_t GetMinRelayFee(const CTransaction& tx, unsigned int nBytes, bool fAllowFree)
773+
CAmount GetMinRelayFee(const CTransaction& tx, unsigned int nBytes, bool fAllowFree)
774774
{
775775
{
776776
LOCK(mempool.cs);
777777
uint256 hash = tx.GetHash();
778778
double dPriorityDelta = 0;
779-
int64_t nFeeDelta = 0;
779+
CAmount nFeeDelta = 0;
780780
mempool.ApplyDeltas(hash, dPriorityDelta, nFeeDelta);
781781
if (dPriorityDelta > 0 || nFeeDelta > 0)
782782
return 0;
783783
}
784784

785-
int64_t nMinFee = ::minRelayTxFee.GetFee(nBytes);
785+
CAmount nMinFee = ::minRelayTxFee.GetFee(nBytes);
786786

787787
if (fAllowFree)
788788
{
@@ -845,7 +845,7 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
845845
CCoinsView dummy;
846846
CCoinsViewCache view(dummy);
847847

848-
int64_t nValueIn = 0;
848+
CAmount nValueIn = 0;
849849
{
850850
LOCK(pool.cs);
851851
CCoinsViewMemPool viewMemPool(*pcoinsTip, pool);
@@ -897,15 +897,15 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
897897
hash.ToString(), nSigOps, MAX_TX_SIGOPS),
898898
REJECT_NONSTANDARD, "bad-txns-too-many-sigops");
899899

900-
int64_t nValueOut = tx.GetValueOut();
901-
int64_t nFees = nValueIn-nValueOut;
900+
CAmount nValueOut = tx.GetValueOut();
901+
CAmount nFees = nValueIn-nValueOut;
902902
double dPriority = view.GetPriority(tx, chainActive.Height());
903903

904904
CTxMemPoolEntry entry(tx, nFees, GetTime(), dPriority, chainActive.Height());
905905
unsigned int nSize = entry.GetTxSize();
906906

907907
// Don't accept it if it can't get into a block
908-
int64_t txMinFee = GetMinRelayFee(tx, nSize, true);
908+
CAmount txMinFee = GetMinRelayFee(tx, nSize, true);
909909
if (fLimitFree && nFees < txMinFee)
910910
return state.DoS(0, error("AcceptToMemoryPool : not enough fees %s, %d < %d",
911911
hash.ToString(), nFees, txMinFee),
@@ -1125,7 +1125,7 @@ void static PruneOrphanBlocks()
11251125
mapOrphanBlocks.erase(hash);
11261126
}
11271127

1128-
int64_t GetBlockValue(int nHeight, int64_t nFees)
1128+
CAmount GetBlockValue(int nHeight, const CAmount& nFees)
11291129
{
11301130
int64_t nSubsidy = 50 * COIN;
11311131
int halvings = nHeight / Params().SubsidyHalvingInterval();
@@ -1336,8 +1336,8 @@ bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsVi
13361336
// This is also true for mempool checks.
13371337
CBlockIndex *pindexPrev = mapBlockIndex.find(inputs.GetBestBlock())->second;
13381338
int nSpendHeight = pindexPrev->nHeight + 1;
1339-
int64_t nValueIn = 0;
1340-
int64_t nFees = 0;
1339+
CAmount nValueIn = 0;
1340+
CAmount nFees = 0;
13411341
for (unsigned int i = 0; i < tx.vin.size(); i++)
13421342
{
13431343
const COutPoint &prevout = tx.vin[i].prevout;
@@ -1365,7 +1365,7 @@ bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsVi
13651365
REJECT_INVALID, "bad-txns-in-belowout");
13661366

13671367
// Tally transaction fees
1368-
int64_t nTxFee = nValueIn - tx.GetValueOut();
1368+
CAmount nTxFee = nValueIn - tx.GetValueOut();
13691369
if (nTxFee < 0)
13701370
return state.DoS(100, error("CheckInputs() : %s nTxFee < 0", tx.GetHash().ToString()),
13711371
REJECT_INVALID, "bad-txns-fee-negative");
@@ -1605,7 +1605,7 @@ bool ConnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, C
16051605
CCheckQueueControl<CScriptCheck> control(fScriptChecks && nScriptCheckThreads ? &scriptcheckqueue : NULL);
16061606

16071607
int64_t nTimeStart = GetTimeMicros();
1608-
int64_t nFees = 0;
1608+
CAmount nFees = 0;
16091609
int nInputs = 0;
16101610
unsigned int nSigOps = 0;
16111611
CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));

src/main.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ std::string GetWarnings(std::string strFor);
172172
bool GetTransaction(const uint256 &hash, CTransaction &tx, uint256 &hashBlock, bool fAllowSlow = false);
173173
/** Find the best known block, and make it the tip of the block chain */
174174
bool ActivateBestChain(CValidationState &state, CBlock *pblock = NULL);
175-
int64_t GetBlockValue(int nHeight, int64_t nFees);
175+
CAmount GetBlockValue(int nHeight, const CAmount& nFees);
176176

177177
/** Create a new block index entry for a given block hash */
178178
CBlockIndex * InsertBlockIndex(uint256 hash);
@@ -220,7 +220,7 @@ struct CDiskTxPos : public CDiskBlockPos
220220
};
221221

222222

223-
int64_t GetMinRelayFee(const CTransaction& tx, unsigned int nBytes, bool fAllowFree);
223+
CAmount GetMinRelayFee(const CTransaction& tx, unsigned int nBytes, bool fAllowFree);
224224

225225
//
226226
// Check transaction inputs, and make sure any
@@ -591,7 +591,7 @@ extern CBlockTreeDB *pblocktree;
591591
struct CBlockTemplate
592592
{
593593
CBlock block;
594-
std::vector<int64_t> vTxFees;
594+
std::vector<CAmount> vTxFees;
595595
std::vector<int64_t> vTxSigOps;
596596
};
597597

0 commit comments

Comments
 (0)