Branch data Line data Source code
1 : : // Copyright (c) 2021-present 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 <interfaces/chain.h>
7 : : #include <key.h>
8 : : #include <primitives/transaction.h>
9 : : #include <random.h>
10 : : #include <script/solver.h>
11 : : #include <test/util/setup_common.h>
12 : : #include <uint256.h>
13 : : #include <util/time.h>
14 : : #include <validation.h>
15 : : #include <wallet/coincontrol.h>
16 : : #include <wallet/spend.h>
17 : : #include <wallet/test/util.h>
18 : : #include <wallet/test/wallet_test_fixture.h>
19 : :
20 : : #include <boost/test/unit_test.hpp>
21 : :
22 : : namespace wallet {
23 : : BOOST_FIXTURE_TEST_SUITE(spend_tests, WalletTestingSetup)
24 : :
25 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(max_signed_input_size_uses_external_outpoint)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
26 : : {
27 : 1 : const CKey key{GenerateRandomKey()};
28 : 1 : FillableSigningProvider provider;
29 [ + - + - : 2 : BOOST_REQUIRE(provider.AddKey(key));
+ - + - ]
30 : :
31 [ + - + - : 2 : const CTxOut txout{COIN, GetScriptForDestination(PKHash{key.GetPubKey()})};
+ - + - ]
32 [ + - ]: 1 : const COutPoint outpoint{Txid{}, 0};
33 [ + - ]: 1 : CCoinControl coin_control;
34 [ + - + - ]: 1 : coin_control.Select(outpoint).SetTxOut(txout);
35 : :
36 [ + - ]: 1 : const int low_r{CalculateMaximumSignedInputSize(txout, COutPoint{}, &provider, /*can_grind_r=*/true, &coin_control)};
37 [ + - ]: 1 : const int high_r{CalculateMaximumSignedInputSize(txout, outpoint, &provider, /*can_grind_r=*/true, &coin_control)};
38 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(high_r, low_r + 1);
39 : 1 : }
40 : :
41 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(SubtractFee, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
42 : : {
43 [ + - ]: 2 : CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
44 [ + - + - ]: 3 : auto wallet = CreateSyncedWallet(*m_node.chain, WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain()), coinbaseKey);
45 : :
46 : : // Check that a subtract-from-recipient transaction slightly less than the
47 : : // coinbase input amount does not create a change output (because it would
48 : : // be uneconomical to add and spend the output), and make sure it pays the
49 : : // leftover input amount which would have been change to the recipient
50 : : // instead of the miner.
51 : 5 : auto check_tx = [&wallet](CAmount leftover_input_amount) {
52 [ + - ]: 4 : CRecipient recipient{PubKeyDestination({}), 50 * COIN - leftover_input_amount, /*subtract_fee=*/true};
53 [ + - ]: 4 : CCoinControl coin_control;
54 [ - + ]: 4 : coin_control.m_feerate.emplace(10000);
55 : 4 : coin_control.fOverrideFeeRate = true;
56 : : // 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
57 : 4 : coin_control.m_change_type = OutputType::LEGACY;
58 [ + - + + : 8 : auto res = CreateTransaction(*wallet, {recipient}, /*change_pos=*/std::nullopt, coin_control);
- - ]
59 [ + - + - ]: 8 : BOOST_CHECK(res);
60 : 4 : const auto& txr = *res;
61 [ + - - + : 4 : BOOST_CHECK_EQUAL(txr.tx->vout.size(), 1);
+ - ]
62 [ + - + - ]: 4 : BOOST_CHECK_EQUAL(txr.tx->vout[0].nValue, recipient.nAmount + leftover_input_amount - txr.fee);
63 [ + - + - ]: 4 : BOOST_CHECK_GT(txr.fee, 0);
64 : 4 : return txr.fee;
65 [ + - + - ]: 9 : };
66 : :
67 : : // Send full input amount to recipient, check that only nonzero fee is
68 : : // subtracted (to_reduce == fee).
69 [ + - ]: 1 : const CAmount fee{check_tx(0)};
70 : :
71 : : // Send slightly less than full input amount to recipient, check leftover
72 : : // input amount is paid to recipient not the miner (to_reduce == fee - 123)
73 [ + - + - : 1 : BOOST_CHECK_EQUAL(fee, check_tx(123));
+ - ]
74 : :
75 : : // Send full input minus fee amount to recipient, check leftover input
76 : : // amount is paid to recipient not the miner (to_reduce == 0)
77 [ + - + - : 1 : BOOST_CHECK_EQUAL(fee, check_tx(fee));
+ - ]
78 : :
79 : : // Send full input minus more than the fee amount to recipient, check
80 : : // leftover input amount is paid to recipient not the miner (to_reduce ==
81 : : // -123). This overpays the recipient instead of overpaying the miner more
82 : : // than double the necessary fee.
83 [ + - + - : 1 : BOOST_CHECK_EQUAL(fee, check_tx(fee + 123));
+ - + - +
- ]
84 : 1 : }
85 : :
86 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(wallet_duplicated_preset_inputs_test, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
87 : : {
88 : : // Verify that the wallet's Coin Selection process does not include pre-selected inputs twice in a transaction.
89 : :
90 : : // Add 4 spendable UTXO, 50 BTC each, to the wallet (total balance 200 BTC)
91 [ + - + + ]: 5 : for (int i = 0; i < 4; i++) CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
92 [ + - + - ]: 3 : auto wallet = CreateSyncedWallet(*m_node.chain, WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain()), coinbaseKey);
93 : :
94 [ + - ]: 1 : LOCK(wallet->cs_wallet);
95 [ + - ]: 1 : auto available_coins = AvailableCoins(*wallet);
96 [ + - ]: 1 : std::vector<COutput> coins = available_coins.All();
97 : : // Preselect the first 3 UTXO (150 BTC total)
98 [ + - ]: 1 : std::set<COutPoint> preset_inputs = {coins[0].outpoint, coins[1].outpoint, coins[2].outpoint};
99 : :
100 : : // Try to create a tx that spends more than what preset inputs + wallet selected inputs are covering for.
101 : : // The wallet can cover up to 200 BTC, and the tx target is 299 BTC.
102 [ + - + - : 2 : std::vector<CRecipient> recipients{{*Assert(wallet->GetNewDestination(OutputType::BECH32, "dummy")),
+ - ]
103 [ + + - - ]: 2 : /*nAmount=*/299 * COIN, /*fSubtractFeeFromAmount=*/true}};
104 [ + - ]: 1 : CCoinControl coin_control;
105 : 1 : coin_control.m_allow_other_inputs = true;
106 [ + + ]: 4 : for (const auto& outpoint : preset_inputs) {
107 [ + - ]: 3 : coin_control.Select(outpoint);
108 : : }
109 : :
110 : : // Attempt to send 299 BTC from a wallet that only has 200 BTC. The wallet should exclude
111 : : // the preset inputs from the pool of available coins, realize that there is not enough
112 : : // money to fund the 299 BTC payment, and fail with "Insufficient funds".
113 : : //
114 : : // Even with SFFO, the wallet can only afford to send 200 BTC.
115 : : // If the wallet does not properly exclude preset inputs from the pool of available coins
116 : : // prior to coin selection, it may create a transaction that does not fund the full payment
117 : : // amount or, through SFFO, incorrectly reduce the recipient's amount by the difference
118 : : // between the original target and the wrongly counted inputs (in this case 99 BTC)
119 : : // so that the recipient's amount is no longer equal to the user's selected target of 299 BTC.
120 : :
121 : : // First case, use 'subtract_fee_from_outputs=true'
122 [ + - + - : 2 : BOOST_CHECK(!CreateTransaction(*wallet, recipients, /*change_pos=*/std::nullopt, coin_control));
+ - + - ]
123 : :
124 : : // Second case, don't use 'subtract_fee_from_outputs'.
125 [ + - ]: 1 : recipients[0].fSubtractFeeFromAmount = false;
126 [ + - + - : 2 : BOOST_CHECK(!CreateTransaction(*wallet, recipients, /*change_pos=*/std::nullopt, coin_control));
+ - ]
127 [ + - - + : 5 : }
+ - + - ]
128 : :
129 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(discourage_fee_sniping_backdating_bounds, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
130 : : {
131 : : // Verify that DiscourageFeeSniping works as expected when called with the minimum_height
132 : : // parameter. This only happens while replacing a transaction (bumpfee).
133 : 1 : FastRandomContext rng_fast(true);
134 : :
135 : : // Mine 11 more blocks, so that the tip ends up above height 110.
136 [ + + ]: 12 : for (int i{0}; i < 11; ++i) {
137 [ + - + - : 22 : CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
+ - ]
138 : : }
139 [ + - + + : 4 : const CBlockIndex* tip{WITH_LOCK(Assert(m_node.chainman)->GetMutex(),
+ - ]
140 : : return m_node.chainman->ActiveChain().Tip())};
141 : 1 : const int block_height{tip->nHeight};
142 : 1 : const uint256 block_hash{tip->GetBlockHash()};
143 [ + - ]: 1 : auto& chain{*m_node.chain};
144 : :
145 [ + - ]: 1 : CMutableTransaction mtx;
146 : 1 : mtx.vin.emplace_back(COutPoint{Txid::FromUint256(uint256::ONE), 0},
147 [ + - ]: 1 : CScript(), CTxIn::MAX_SEQUENCE_NONFINAL);
148 : :
149 : : // Backdating is constrained to [block_height - 99, block_height].
150 : 1 : bool backdated{false};
151 : 1 : bool saw_tip_locktime{false};
152 : : // Require a tip above 110 so that block_height - 110 cannot underflow when
153 : : // converted to the uint32_t minimum_height parameter.
154 [ + - + - ]: 2 : BOOST_REQUIRE(block_height > 110);
155 [ + + ]: 201 : for (int i{0}; i < 200; ++i) {
156 [ + - ]: 200 : DiscourageFeeSniping(mtx, rng_fast, chain, block_hash, block_height, block_height - 110);
157 [ + - + - ]: 200 : BOOST_CHECK_LE(mtx.nLockTime, static_cast<uint32_t>(block_height));
158 [ + - + - ]: 200 : BOOST_CHECK_GE(mtx.nLockTime, static_cast<uint32_t>(block_height - 99));
159 [ + + ]: 200 : if (mtx.nLockTime == static_cast<uint32_t>(block_height)) {
160 : : saw_tip_locktime = true;
161 : : } else {
162 : 25 : backdated = true;
163 : : }
164 : : }
165 [ + - + - : 2 : BOOST_CHECK(backdated);
+ - ]
166 [ + - + - ]: 2 : BOOST_CHECK(saw_tip_locktime);
167 : :
168 : : // Check that backdating can reach the minimum height.
169 : 1 : backdated = false;
170 [ + + ]: 201 : for (int i{0}; i < 200; ++i) {
171 [ + - ]: 200 : DiscourageFeeSniping(mtx, rng_fast, chain, block_hash, block_height, block_height - 1);
172 [ + - + - ]: 200 : BOOST_CHECK_LE(mtx.nLockTime, static_cast<uint32_t>(block_height));
173 [ + - + - ]: 200 : BOOST_CHECK_GE(mtx.nLockTime, static_cast<uint32_t>(block_height - 1));
174 [ + + ]: 200 : if (mtx.nLockTime < static_cast<uint32_t>(block_height)) {
175 [ + - + - ]: 7 : BOOST_CHECK_EQUAL(mtx.nLockTime, static_cast<uint32_t>(block_height - 1));
176 : 7 : backdated = true;
177 : : }
178 : : }
179 [ + - + - : 2 : BOOST_CHECK(backdated);
+ - ]
180 : :
181 : : // A minimum at the tip leaves no room for backdating.
182 [ + - ]: 1 : DiscourageFeeSniping(mtx, rng_fast, chain, block_hash, block_height, block_height);
183 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(mtx.nLockTime, static_cast<uint32_t>(block_height));
184 : :
185 : : // If minimum_height > block_height the locktime is set to 0.
186 [ + - ]: 1 : DiscourageFeeSniping(mtx, rng_fast, chain, block_hash, block_height, block_height + 1);
187 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(mtx.nLockTime, 0);
188 : 1 : }
189 : :
190 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(discourage_fee_sniping_stale_tip, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
191 : : {
192 : : // Verify DiscourageFeeSniping on a stale tip when minimum_height is set.
193 : 1 : FastRandomContext rng_fast;
194 : :
195 [ + - + + : 4 : const CBlockIndex* tip{WITH_LOCK(Assert(m_node.chainman)->GetMutex(),
+ - ]
196 : : return m_node.chainman->ActiveChain().Tip())};
197 : 1 : const int block_height{tip->nHeight};
198 : 1 : const uint256 block_hash{tip->GetBlockHash()};
199 [ + - ]: 1 : auto& chain{*m_node.chain};
200 : :
201 [ + - ]: 1 : CMutableTransaction mtx;
202 : 1 : mtx.vin.emplace_back(COutPoint{Txid::FromUint256(uint256::ONE), 0},
203 [ + - ]: 1 : CScript(), CTxIn::MAX_SEQUENCE_NONFINAL);
204 : :
205 : : // Make the tip stale by moving the time 9 hours forward, so that it is
206 : : // older than the 8 hours IsCurrentForAntiFeeSniping allows
207 [ + - ]: 1 : SetMockTime(std::chrono::seconds{tip->GetBlockTime()} + std::chrono::hours{9});
208 : : // Stale chain with a minimum_height lower than the block_height
209 : : // should set the locktime to minimum_height
210 [ + - ]: 1 : DiscourageFeeSniping(mtx, rng_fast, chain, block_hash, block_height, block_height - 10);
211 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(mtx.nLockTime, static_cast<uint32_t>(block_height - 10));
212 : : // Stale chain with a minimum_height higher than the block_height
213 : : // should set the locktime to 0
214 [ + - ]: 1 : DiscourageFeeSniping(mtx, rng_fast, chain, block_hash, block_height, block_height + 1);
215 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(mtx.nLockTime, 0);
216 : 1 : }
217 : :
218 : : BOOST_AUTO_TEST_SUITE_END()
219 : : } // namespace wallet
|