Skip to content

Commit

Permalink
wallet test: Add test for subtract fee from recipient behavior
Browse files Browse the repository at this point in the history
Behavior might have recently changed in bitcoin#17331 (it is not clear) but not
noticed because there is no test coverage.

This adds test coverage for current subtract from recipient behavior
without changing it.
  • Loading branch information
ryanofsky committed Jun 4, 2021
1 parent db35760 commit e459d01
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ BITCOIN_TESTS =\
if ENABLE_WALLET
BITCOIN_TESTS += \
wallet/test/psbt_wallet_tests.cpp \
wallet/test/spend_tests.cpp \
wallet/test/wallet_tests.cpp \
wallet/test/walletdb_tests.cpp \
wallet/test/wallet_crypto_tests.cpp \
Expand Down
48 changes: 48 additions & 0 deletions src/wallet/test/spend_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <policy/fees.h>
#include <validation.h>
#include <wallet/coincontrol.h>
#include <wallet/test/util.h>
#include <wallet/test/wallet_test_fixture.h>

#include <boost/test/unit_test.hpp>

BOOST_FIXTURE_TEST_SUITE(spend_tests, WalletTestingSetup)

BOOST_FIXTURE_TEST_CASE(SubtractFee, TestChain100Setup)
{
CChain& cchain = m_node.chainman->ActiveChain();
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
auto wallet = CreateSyncedWallet(*m_node.chain, cchain, coinbaseKey);

// Check subtract from recipient transaction equal to coinbase amount does
// not create change output.
CRecipient recipient{GetScriptForRawPubKey({}), 50 * COIN, true /* subtract fee */};
CTransactionRef tx;
CAmount fee;
int change_pos = -1;
bilingual_str error;
CCoinControl coin_control;
FeeCalculation fee_calc;
BOOST_CHECK(wallet->CreateTransaction({recipient}, tx, fee, change_pos, error, coin_control, fee_calc));
BOOST_CHECK_EQUAL(tx->vout.size(), 1);
BOOST_CHECK_EQUAL(tx->vout[0].nValue, recipient.nAmount - fee);
tx.reset();

// Check subtract from recipient transaction slightly less than coinbase
// amount also does not create change output and pays extra dust amount to
// recipient instead of miner
const CAmount dust_amount = 123;
const CAmount expected_fee = fee;
recipient.nAmount -= dust_amount;
BOOST_CHECK(wallet->CreateTransaction({recipient}, tx, fee, change_pos, error, coin_control, fee_calc));
BOOST_CHECK_EQUAL(tx->vout.size(), 1);
BOOST_CHECK_EQUAL(tx->vout[0].nValue, recipient.nAmount - fee + dust_amount);
BOOST_CHECK_EQUAL(fee, expected_fee);
tx.reset();
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit e459d01

Please sign in to comment.