Skip to content

Commit e9690cf

Browse files
MarcoFalkevijaydasmp
authored andcommitted
Merge bitcoin#22155: wallet test: Add test for subtract fee from recipient behavior
fe6dc76 wallet test: Add test for subtract fee from recipient behavior (Russell Yanofsky) 2565478 wallet test refactor: add CreateSyncedWallet function (Russell Yanofsky) Pull request description: This adds test coverage for wallet subtract from recipient behavior without changing it. Behavior seems to have changed recently in a minor way in bitcoin#17331 without being noticed. ACKs for top commit: achow101: ACK fe6dc76 glozow: ACK fe6dc76 promag: Code review ACK fe6dc76. Tree-SHA512: e00c5dfe467e4ccef5edb0dd4fff6c53f35a37828a4327bea2e166751e5ef971d519ffca7b8f735b12912bb4a547980626356bc1855981005aed1a6c2a57be0b
1 parent 0c23f20 commit e9690cf

File tree

5 files changed

+124
-15
lines changed

5 files changed

+124
-15
lines changed

src/Makefile.test.include

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ BITCOIN_TESTS += \
185185
wallet/test/bip39_tests.cpp \
186186
wallet/test/coinjoin_tests.cpp \
187187
wallet/test/psbt_wallet_tests.cpp \
188+
wallet/test/spend_tests.cpp \
188189
wallet/test/wallet_tests.cpp \
189190
wallet/test/walletdb_tests.cpp \
190191
wallet/test/wallet_crypto_tests.cpp \
@@ -204,6 +205,8 @@ endif
204205

205206

206207
BITCOIN_TEST_SUITE += \
208+
wallet/test/util.cpp \
209+
wallet/test/util.h \
207210
wallet/test/wallet_test_fixture.cpp \
208211
wallet/test/wallet_test_fixture.h \
209212
wallet/test/init_test_fixture.cpp \

src/wallet/test/spend_tests.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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()

src/wallet/test/util.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 <wallet/test/util.h>
6+
7+
#include <chain.h>
8+
#include <key.h>
9+
#include <test/util/setup_common.h>
10+
#include <wallet/wallet.h>
11+
#include <wallet/walletdb.h>
12+
13+
#include <boost/test/unit_test.hpp>
14+
15+
#include <memory>
16+
17+
std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key)
18+
{
19+
auto wallet = std::make_unique<CWallet>(&chain, /*coinjoin_loader=*/ nullptr, "", CreateMockWalletDatabase());
20+
{
21+
LOCK2(wallet->cs_wallet, ::cs_main);
22+
wallet->SetLastBlockProcessed(cchain.Height(), cchain.Tip()->GetBlockHash());
23+
}
24+
bool firstRun;
25+
wallet->LoadWallet(firstRun);
26+
{
27+
auto spk_man = wallet->GetOrCreateLegacyScriptPubKeyMan();
28+
LOCK2(wallet->cs_wallet, spk_man->cs_KeyStore);
29+
spk_man->AddKeyPubKey(key, key.GetPubKey());
30+
}
31+
WalletRescanReserver reserver(*wallet);
32+
reserver.reserve();
33+
CWallet::ScanResult result = wallet->ScanForWalletTransactions(cchain.Genesis()->GetBlockHash(), 0 /* start_height */, {} /* max_height */, reserver, false /* update */);
34+
BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS);
35+
BOOST_CHECK_EQUAL(result.last_scanned_block, cchain.Tip()->GetBlockHash());
36+
BOOST_CHECK_EQUAL(*result.last_scanned_height, cchain.Height());
37+
BOOST_CHECK(result.last_failed_block.IsNull());
38+
return wallet;
39+
}

src/wallet/test/util.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
#ifndef BITCOIN_WALLET_TEST_UTIL_H
6+
#define BITCOIN_WALLET_TEST_UTIL_H
7+
8+
#include <memory>
9+
10+
class CChain;
11+
class CKey;
12+
class CWallet;
13+
namespace interfaces {
14+
class Chain;
15+
} // namespace interfaces
16+
17+
std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
18+
19+
#endif // BITCOIN_WALLET_TEST_UTIL_H

src/wallet/test/wallet_tests.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <util/translation.h>
2525
#include <validation.h>
2626
#include <wallet/coincontrol.h>
27+
#include <wallet/test/util.h>
2728
#include <wallet/test/wallet_test_fixture.h>
2829

2930
#include <boost/test/unit_test.hpp>
@@ -570,21 +571,7 @@ class ListCoinsTestingSetup : public TestChain100Setup
570571
ListCoinsTestingSetup()
571572
{
572573
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
573-
wallet = std::make_unique<CWallet>(m_node.chain.get(), m_node.coinjoin_loader.get(), "", CreateMockWalletDatabase());
574-
{
575-
LOCK(wallet->cs_wallet);
576-
wallet->SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
577-
}
578-
bool firstRun;
579-
wallet->LoadWallet(firstRun);
580-
AddKey(*wallet, coinbaseKey);
581-
WalletRescanReserver reserver(*wallet);
582-
reserver.reserve();
583-
CWallet::ScanResult result = wallet->ScanForWalletTransactions(m_node.chainman->ActiveChain().Genesis()->GetBlockHash(), 0 /* start_height */, {} /* max_height */, reserver, false /* update */);
584-
BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS);
585-
BOOST_CHECK_EQUAL(result.last_scanned_block, m_node.chainman->ActiveChain().Tip()->GetBlockHash());
586-
BOOST_CHECK_EQUAL(*result.last_scanned_height, m_node.chainman->ActiveChain().Height());
587-
BOOST_CHECK(result.last_failed_block.IsNull());
574+
wallet = CreateSyncedWallet(*m_node.chain, m_node.chainman->ActiveChain(), coinbaseKey);
588575
}
589576

590577
~ListCoinsTestingSetup()

0 commit comments

Comments
 (0)