Bitcoin Core  24.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, WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return 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  constexpr int RANDOM_CHANGE_POSITION = -1;
31  CCoinControl coin_control;
32  coin_control.m_feerate.emplace(10000);
33  coin_control.fOverrideFeeRate = true;
34  // 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
35  coin_control.m_change_type = OutputType::LEGACY;
36  auto res = CreateTransaction(*wallet, {recipient}, RANDOM_CHANGE_POSITION, coin_control);
37  BOOST_CHECK(res);
38  const auto& txr = *res;
39  BOOST_CHECK_EQUAL(txr.tx->vout.size(), 1);
40  BOOST_CHECK_EQUAL(txr.tx->vout[0].nValue, recipient.nAmount + leftover_input_amount - txr.fee);
41  BOOST_CHECK_GT(txr.fee, 0);
42  return txr.fee;
43  };
44 
45  // Send full input amount to recipient, check that only nonzero fee is
46  // subtracted (to_reduce == fee).
47  const CAmount fee{check_tx(0)};
48 
49  // Send slightly less than full input amount to recipient, check leftover
50  // input amount is paid to recipient not the miner (to_reduce == fee - 123)
51  BOOST_CHECK_EQUAL(fee, check_tx(123));
52 
53  // Send full input minus fee amount to recipient, check leftover input
54  // amount is paid to recipient not the miner (to_reduce == 0)
55  BOOST_CHECK_EQUAL(fee, check_tx(fee));
56 
57  // Send full input minus more than the fee amount to recipient, check
58  // leftover input amount is paid to recipient not the miner (to_reduce ==
59  // -123). This overpays the recipient instead of overpaying the miner more
60  // than double the necessary fee.
61  BOOST_CHECK_EQUAL(fee, check_tx(fee + 123));
62 }
63 
64 static void TestFillInputToWeight(int64_t additional_weight, std::vector<int64_t> expected_stack_sizes)
65 {
66  static const int64_t EMPTY_INPUT_WEIGHT = GetTransactionInputWeight(CTxIn());
67 
68  CTxIn input;
69  int64_t target_weight = EMPTY_INPUT_WEIGHT + additional_weight;
70  BOOST_CHECK(FillInputToWeight(input, target_weight));
71  BOOST_CHECK_EQUAL(GetTransactionInputWeight(input), target_weight);
72  BOOST_CHECK_EQUAL(input.scriptWitness.stack.size(), expected_stack_sizes.size());
73  for (unsigned int i = 0; i < expected_stack_sizes.size(); ++i) {
74  BOOST_CHECK_EQUAL(input.scriptWitness.stack[i].size(), expected_stack_sizes[i]);
75  }
76 }
77 
79 {
80  {
81  // Less than or equal minimum of 165 should not add any witness data
82  CTxIn input;
83  BOOST_CHECK(!FillInputToWeight(input, -1));
85  BOOST_CHECK_EQUAL(input.scriptWitness.stack.size(), 0);
86  BOOST_CHECK(!FillInputToWeight(input, 0));
88  BOOST_CHECK_EQUAL(input.scriptWitness.stack.size(), 0);
89  BOOST_CHECK(!FillInputToWeight(input, 164));
91  BOOST_CHECK_EQUAL(input.scriptWitness.stack.size(), 0);
92  BOOST_CHECK(FillInputToWeight(input, 165));
94  BOOST_CHECK_EQUAL(input.scriptWitness.stack.size(), 0);
95  }
96 
97  // Make sure we can add at least one weight
98  TestFillInputToWeight(1, {0});
99 
100  // 1 byte compact size uint boundary
101  TestFillInputToWeight(252, {251});
102  TestFillInputToWeight(253, {83, 168});
103  TestFillInputToWeight(262, {86, 174});
104  TestFillInputToWeight(263, {260});
105 
106  // 3 byte compact size uint boundary
107  TestFillInputToWeight(65535, {65532});
108  TestFillInputToWeight(65536, {21842, 43688});
109  TestFillInputToWeight(65545, {21845, 43694});
110  TestFillInputToWeight(65546, {65541});
111 
112  // Note: We don't test the next boundary because of memory allocation constraints.
113 }
114 
116 } // namespace wallet
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
node::NodeContext m_node
Definition: bitcoin-gui.cpp:37
#define Assert(val)
Identity function.
Definition: check.h:74
An input of a transaction.
Definition: transaction.h:74
CScriptWitness scriptWitness
Only serialized through CTransaction.
Definition: transaction.h:79
Coin Control Features.
Definition: coincontrol.h:30
std::optional< OutputType > m_change_type
Override the default change type if set, ignored if destChange is set.
Definition: coincontrol.h:35
bool fOverrideFeeRate
Override automatic min/max checks on fee, m_feerate must be set if true.
Definition: coincontrol.h:44
std::optional< CFeeRate > m_feerate
Override the wallet's m_pay_tx_fee if set.
Definition: coincontrol.h:46
static int64_t GetTransactionInputWeight(const CTxIn &txin)
Definition: validation.h:156
BOOST_AUTO_TEST_SUITE_END()
bool FillInputToWeight(CTxIn &txin, int64_t target_weight)
Definition: wallet.cpp:1570
Definition: node.h:39
static void TestFillInputToWeight(int64_t additional_weight, std::vector< int64_t > expected_stack_sizes)
Definition: spend_tests.cpp:64
std::unique_ptr< CWallet > CreateSyncedWallet(interfaces::Chain &chain, CChain &cchain, ArgsManager &args, const CKey &key)
Definition: util.cpp:19
util::Result< CreatedTransactionResult > CreateTransaction(CWallet &wallet, const std::vector< CRecipient > &vecSend, int change_pos, const CCoinControl &coin_control, bool sign)
Create a new transaction paying the recipients with a set of coins selected by SelectCoins(); Also cr...
Definition: spend.cpp:1038
BOOST_FIXTURE_TEST_CASE(BasicOutputTypesTest, AvailableCoinsTestingSetup)
std::shared_ptr< CWallet > wallet
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
#define BOOST_CHECK(expr)
Definition: object.cpp:16
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:339
Basic testing setup.
Definition: setup_common.h:83
std::vector< std::vector< unsigned char > > stack
Definition: script.h:566
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:126
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:54
std::unique_ptr< interfaces::Chain > chain
Definition: context.h:57
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:305