Skip to content

Commit

Permalink
Merge #14244: amount: Move CAmount CENT to unit test header
Browse files Browse the repository at this point in the history
Summary:
fa84723e73 amount: Move CAmount CENT to unit test header (MarcoFalke)

Pull request description:

  `CAmount` is currently not type-safe. Exporting a constant (`CENT`) that is commonly not referred to by that name might be confusing. `CENT` is only used in two places prior to this commit (`ParseMoney` and `MIN_CHANGE`). So replace these with constants relative to `COIN` and move `CENT` to the unit test header.

Tree-SHA512: 5273e96d8664ced6ae211abde2e20bc763e6e99f89404eec02c621f29e1d235e5f9b1ade933743843fae16fc24b643f883deda9221e3d9fd31229d2ab63a914f

Backport of Core [[bitcoin/bitcoin#14244 | PR14244]]

Test Plan:
  ninja check
  ninja check-functional

Reviewers: O1 Bitcoin ABC, #bitcoin_abc, deadalnix

Reviewed By: O1 Bitcoin ABC, #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D5489
  • Loading branch information
ftrader authored and cculianu committed Apr 23, 2020
1 parent 7905cb4 commit fde4619
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/amount.h
Expand Up @@ -151,7 +151,6 @@ struct Amount {
static constexpr Amount SATOSHI = Amount::satoshi();
static constexpr Amount CASH = 100 * SATOSHI;
static constexpr Amount COIN = 100000000 * SATOSHI;
static constexpr Amount CENT = COIN / 100;

extern const std::string CURRENCY_UNIT;

Expand Down
16 changes: 8 additions & 8 deletions src/bench/ccoins_caching.cpp
Expand Up @@ -12,8 +12,8 @@
// FIXME: Dedup with SetupDummyInputs in test/transaction_tests.cpp.
//
// Helper: create two dummy transactions, each with
// two outputs. The first has 11 and 50 CENT outputs
// paid to a TX_PUBKEY, the second 21 and 22 CENT outputs
// two outputs. The first has 11 and 50 COIN outputs
// paid to a TX_PUBKEY, the second 21 and 22 COIN outputs
// paid to a TX_PUBKEYHASH.
//
static std::vector<CMutableTransaction>
Expand All @@ -30,19 +30,19 @@ SetupDummyInputs(CBasicKeyStore &keystoreRet, CCoinsViewCache &coinsRet) {

// Create some dummy input transactions
dummyTransactions[0].vout.resize(2);
dummyTransactions[0].vout[0].nValue = 11 * CENT;
dummyTransactions[0].vout[0].nValue = 11 * COIN;
dummyTransactions[0].vout[0].scriptPubKey
<< ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG;
dummyTransactions[0].vout[1].nValue = 50 * CENT;
dummyTransactions[0].vout[1].nValue = 50 * COIN;
dummyTransactions[0].vout[1].scriptPubKey
<< ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG;
AddCoins(coinsRet, CTransaction(dummyTransactions[0]), 0);

dummyTransactions[1].vout.resize(2);
dummyTransactions[1].vout[0].nValue = 21 * CENT;
dummyTransactions[1].vout[0].nValue = 21 * COIN;
dummyTransactions[1].vout[0].scriptPubKey =
GetScriptForDestination(key[2].GetPubKey().GetID());
dummyTransactions[1].vout[1].nValue = 22 * CENT;
dummyTransactions[1].vout[1].nValue = 22 * COIN;
dummyTransactions[1].vout[1].scriptPubKey =
GetScriptForDestination(key[3].GetPubKey().GetID());
AddCoins(coinsRet, CTransaction(dummyTransactions[1]), 0);
Expand Down Expand Up @@ -74,7 +74,7 @@ static void CCoinsCaching(benchmark::State &state) {
t1.vin[2].scriptSig << std::vector<uint8_t>(65, 0)
<< std::vector<uint8_t>(33, 4);
t1.vout.resize(2);
t1.vout[0].nValue = 90 * CENT;
t1.vout[0].nValue = 90 * COIN;
t1.vout[0].scriptPubKey << OP_1;

// Benchmark.
Expand All @@ -84,7 +84,7 @@ static void CCoinsCaching(benchmark::State &state) {
AreInputsStandard(t, coins, STANDARD_SCRIPT_VERIFY_FLAGS);
assert(success);
Amount value = coins.GetValueIn(t);
assert(value == (50 + 21 + 22) * CENT);
assert(value == (50 + 21 + 22) * COIN);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/bench/rpc_mempool.cpp
Expand Up @@ -31,9 +31,9 @@ static void RpcMempool(benchmark::State &state) {
tx.vin[0].scriptSig = CScript() << OP_1;
tx.vout.resize(1);
tx.vout[0].scriptPubKey = CScript() << OP_1 << OP_EQUAL;
tx.vout[0].nValue = i * CENT;
tx.vout[0].nValue = i * COIN;
const CTransactionRef tx_r{MakeTransactionRef(tx)};
AddTx(tx_r, /* fee */ i * CENT, pool);
AddTx(tx_r, /* fee */ i * COIN, pool);
}

while (state.KeepRunning()) {
Expand All @@ -57,10 +57,10 @@ static void RpcMempool10k(benchmark::State &state) {
for (size_t j = 0; j < nOuts; ++j) {
tx.vin[j].scriptSig = CScript() << OP_1;
tx.vout[j].scriptPubKey = CScript() << OP_1 << OP_EQUAL;
tx.vout[j].nValue = int64_t(i*j) * CENT;
tx.vout[j].nValue = int64_t(i*j) * COIN;
}
const CTransactionRef tx_r{MakeTransactionRef(tx)};
AddTx(tx_r, /* fee */ int64_t(i) * CENT, pool);
AddTx(tx_r, /* fee */ int64_t(i) * COIN, pool);
}

while (state.KeepRunning()) {
Expand Down
2 changes: 2 additions & 0 deletions src/test/test_bitcoin.h
Expand Up @@ -57,6 +57,8 @@ static inline bool InsecureRandBool() {
return g_insecure_rand_ctx.randbool();
}

static constexpr Amount CENT(COIN / 100);

/**
* Basic testing setup.
* This just configures logging and chain parameters.
Expand Down
2 changes: 1 addition & 1 deletion src/util/moneystr.cpp
Expand Up @@ -45,7 +45,7 @@ bool ParseMoney(const char *pszIn, Amount &nRet) {
for (; *p; p++) {
if (*p == '.') {
p++;
Amount nMult = 10 * CENT;
Amount nMult = COIN / 10;
while (isdigit(*p) && (nMult > Amount::zero())) {
nUnits += (*p++ - '0') * nMult;
nMult /= 10;
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/coinselection.h
Expand Up @@ -10,7 +10,7 @@
#include <random.h>

//! target minimum change amount
static constexpr Amount MIN_CHANGE = CENT;
static constexpr Amount MIN_CHANGE{COIN / 100};
//! final minimum change amount after paying for fees
static constexpr Amount MIN_FINAL_CHANGE = MIN_CHANGE / 2;

Expand Down

0 comments on commit fde4619

Please sign in to comment.