Bitcoin Core  22.99.0
P2P Digital Currency
spend_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <consensus/amount.h>
6 #include <policy/fees.h>
7 #include <validation.h>
8 #include <wallet/coincontrol.h>
9 #include <wallet/spend.h>
10 #include <wallet/test/util.h>
12 
13 #include <boost/test/unit_test.hpp>
14 
15 namespace wallet {
16 BOOST_FIXTURE_TEST_SUITE(spend_tests, WalletTestingSetup)
17 
19 {
20  CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
21  auto wallet = CreateSyncedWallet(*m_node.chain, m_node.chainman->ActiveChain(), m_args, coinbaseKey);
22 
23  // Check that a subtract-from-recipient transaction slightly less than the
24  // coinbase input amount does not create a change output (because it would
25  // be uneconomical to add and spend the output), and make sure it pays the
26  // leftover input amount which would have been change to the recipient
27  // instead of the miner.
28  auto check_tx = [&wallet](CAmount leftover_input_amount) {
29  CRecipient recipient{GetScriptForRawPubKey({}), 50 * COIN - leftover_input_amount, true /* subtract fee */};
30  CTransactionRef tx;
31  CAmount fee;
32  int change_pos = -1;
34  CCoinControl coin_control;
35  coin_control.m_feerate.emplace(10000);
36  coin_control.fOverrideFeeRate = true;
37  // We need to use a change type with high cost of change so that the leftover amount will be dropped to fee instead of added as a change output
38  coin_control.m_change_type = OutputType::LEGACY;
39  FeeCalculation fee_calc;
40  BOOST_CHECK(CreateTransaction(*wallet, {recipient}, tx, fee, change_pos, error, coin_control, fee_calc));
41  BOOST_CHECK_EQUAL(tx->vout.size(), 1);
42  BOOST_CHECK_EQUAL(tx->vout[0].nValue, recipient.nAmount + leftover_input_amount - fee);
43  BOOST_CHECK_GT(fee, 0);
44  return fee;
45  };
46 
47  // Send full input amount to recipient, check that only nonzero fee is
48  // subtracted (to_reduce == fee).
49  const CAmount fee{check_tx(0)};
50 
51  // Send slightly less than full input amount to recipient, check leftover
52  // input amount is paid to recipient not the miner (to_reduce == fee - 123)
53  BOOST_CHECK_EQUAL(fee, check_tx(123));
54 
55  // Send full input minus fee amount to recipient, check leftover input
56  // amount is paid to recipient not the miner (to_reduce == 0)
57  BOOST_CHECK_EQUAL(fee, check_tx(fee));
58 
59  // Send full input minus more than the fee amount to recipient, check
60  // leftover input amount is paid to recipient not the miner (to_reduce ==
61  // -123). This overpays the recipient instead of overpaying the miner more
62  // than double the necessary fee.
63  BOOST_CHECK_EQUAL(fee, check_tx(fee + 123));
64 }
65 
67 } // namespace wallet
OutputType::LEGACY
@ LEGACY
bilingual_str
Bilingual messages:
Definition: translation.h:16
m_node
node::NodeContext m_node
Definition: bitcoin-gui.cpp:36
wallet::CRecipient
Definition: wallet.h:221
wallet
Definition: node.h:38
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
wallet
std::shared_ptr< CWallet > wallet
Definition: notifications.cpp:38
FeeCalculation
Definition: fees.h:75
GetScriptForRawPubKey
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:315
wallet::CCoinControl::m_change_type
std::optional< OutputType > m_change_type
Override the default change type if set, ignored if destChange is set.
Definition: coincontrol.h:35
wallet::CCoinControl::m_feerate
std::optional< CFeeRate > m_feerate
Override the wallet's m_pay_tx_fee if set.
Definition: coincontrol.h:47
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
TestChain100Setup
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:124
wallet_test_fixture.h
util.h
wallet::CreateTransaction
bool CreateTransaction(CWallet &wallet, const std::vector< CRecipient > &vecSend, CTransactionRef &tx, CAmount &nFeeRet, int &nChangePosInOut, bilingual_str &error, const CCoinControl &coin_control, FeeCalculation &fee_calc_out, bool sign)
Create a new transaction paying the recipients with a set of coins selected by SelectCoins(); Also cr...
Definition: spend.cpp:933
fees.h
wallet::CreateSyncedWallet
std::unique_ptr< CWallet > CreateSyncedWallet(interfaces::Chain &chain, CChain &cchain, ArgsManager &args, const CKey &key)
Definition: util.cpp:19
node::NodeContext::chainman
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:48
wallet::CCoinControl::fOverrideFeeRate
bool fOverrideFeeRate
Override automatic min/max checks on fee, m_feerate must be set if true.
Definition: coincontrol.h:45
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
COIN
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
coincontrol.h
amount.h
wallet::CCoinControl
Coin Control Features.
Definition: coincontrol.h:29
spend.h
wallet::BOOST_FIXTURE_TEST_CASE
BOOST_FIXTURE_TEST_CASE(SubtractFee, TestChain100Setup)
Definition: spend_tests.cpp:18
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
node::NodeContext::chain
std::unique_ptr< interfaces::Chain > chain
Definition: context.h:51