|
| 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 <policy/fees.h> |
| 6 | +#include <validation.h> |
| 7 | +#include <wallet/coincontrol.h> |
| 8 | +#include <wallet/test/util.h> |
| 9 | +#include <wallet/test/wallet_test_fixture.h> |
| 10 | + |
| 11 | +#include <boost/test/unit_test.hpp> |
| 12 | + |
| 13 | +BOOST_FIXTURE_TEST_SUITE(spend_tests, WalletTestingSetup) |
| 14 | + |
| 15 | +BOOST_FIXTURE_TEST_CASE(SubtractFee, TestChain100Setup) |
| 16 | +{ |
| 17 | + CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())); |
| 18 | + auto wallet = CreateSyncedWallet(*m_node.chain, m_node.chainman->ActiveChain(), coinbaseKey); |
| 19 | + |
| 20 | + // Check that a subtract-from-recipient transaction slightly less than the |
| 21 | + // coinbase input amount does not create a change output (because it would |
| 22 | + // be uneconomical to add and spend the output), and make sure it pays the |
| 23 | + // leftover input amount which would have been change to the recipient |
| 24 | + // instead of the miner. |
| 25 | + auto check_tx = [&wallet](CAmount leftover_input_amount) { |
| 26 | + CRecipient recipient{GetScriptForRawPubKey({}), 50 * COIN - leftover_input_amount, true /* subtract fee */}; |
| 27 | + CTransactionRef tx; |
| 28 | + CAmount fee; |
| 29 | + int change_pos = -1; |
| 30 | + bilingual_str error; |
| 31 | + CCoinControl coin_control; |
| 32 | + coin_control.m_feerate.emplace(10000); |
| 33 | + coin_control.fOverrideFeeRate = true; |
| 34 | + FeeCalculation fee_calc; |
| 35 | + BOOST_CHECK(wallet->CreateTransaction({recipient}, tx, fee, change_pos, error, coin_control, fee_calc)); |
| 36 | + BOOST_CHECK_EQUAL(tx->vout.size(), 1); |
| 37 | + BOOST_CHECK_EQUAL(tx->vout[0].nValue, recipient.nAmount + leftover_input_amount - fee); |
| 38 | + BOOST_CHECK_GT(fee, 0); |
| 39 | + return fee; |
| 40 | + }; |
| 41 | + |
| 42 | + // Send full input amount to recipient, check that only nonzero fee is |
| 43 | + // subtracted (to_reduce == fee). |
| 44 | + const CAmount fee{check_tx(0)}; |
| 45 | + |
| 46 | + // Send slightly less than full input amount to recipient, check leftover |
| 47 | + // input amount is paid to recipient not the miner (to_reduce == fee - 123) |
| 48 | + BOOST_CHECK_EQUAL(fee, check_tx(123)); |
| 49 | + |
| 50 | + // Send full input minus fee amount to recipient, check leftover input |
| 51 | + // amount is paid to recipient not the miner (to_reduce == 0) |
| 52 | + BOOST_CHECK_EQUAL(fee, check_tx(fee)); |
| 53 | + |
| 54 | + // Send full input minus more than the fee amount to recipient, check |
| 55 | + // leftover input amount is paid to recipient not the miner (to_reduce == |
| 56 | + // -123). This overpays the recipient instead of overpaying the miner more |
| 57 | + // than double the neccesary fee. |
| 58 | + BOOST_CHECK_EQUAL(fee, check_tx(fee + 123)); |
| 59 | +} |
| 60 | + |
| 61 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments