Branch data Line data Source code
1 : : // Copyright (c) 2022 The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or https://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <test/util/setup_common.h>
6 : :
7 : : #include <wallet/coinselection.h>
8 : : #include <wallet/spend.h>
9 : : #include <wallet/test/util.h>
10 : : #include <wallet/wallet.h>
11 : :
12 : : #include <boost/test/unit_test.hpp>
13 : :
14 : : namespace wallet {
15 : : BOOST_FIXTURE_TEST_SUITE(group_outputs_tests, TestingSetup)
16 : :
17 : : static int nextLockTime = 0;
18 : :
19 : 1 : static std::shared_ptr<CWallet> NewWallet(const node::NodeContext& m_node)
20 : : {
21 [ + - + - ]: 1 : std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(m_node.chain.get(), "", CreateMockableWalletDatabase());
22 [ + - ]: 1 : wallet->LoadWallet();
23 [ + - ]: 1 : LOCK(wallet->cs_wallet);
24 [ + - ]: 1 : wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
25 [ + - ]: 1 : wallet->SetupDescriptorScriptPubKeyMans();
26 [ + - + - ]: 1 : return wallet;
27 [ - + ]: 1 : }
28 : :
29 : 124 : static void addCoin(CoinsResult& coins,
30 : : CWallet& wallet,
31 : : const CTxDestination& dest,
32 : : const CAmount& nValue,
33 : : bool is_from_me,
34 : : CFeeRate fee_rate = CFeeRate(0),
35 : : int depth = 6)
36 : : {
37 : 124 : CMutableTransaction tx;
38 : 124 : tx.nLockTime = nextLockTime++; // so all transactions get different hashes
39 [ + - ]: 124 : tx.vout.resize(1);
40 [ + - ]: 124 : tx.vout[0].nValue = nValue;
41 [ + - ]: 124 : tx.vout[0].scriptPubKey = GetScriptForDestination(dest);
42 : :
43 [ + - ]: 124 : const auto txid{tx.GetHash()};
44 [ + - ]: 124 : LOCK(wallet.cs_wallet);
45 [ + - + - : 248 : auto ret = wallet.mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(txid), std::forward_as_tuple(MakeTransactionRef(std::move(tx)), TxStateInactive{}));
- + ]
46 [ - + ]: 124 : assert(ret.second);
47 [ + - ]: 124 : CWalletTx& wtx = (*ret.first).second;
48 [ + - ]: 124 : const auto& txout = wtx.tx->vout.at(0);
49 [ + - + - : 248 : coins.Add(*Assert(OutputTypeFromDestination(dest)),
+ - + - +
- + - +
- ]
50 [ + - ]: 124 : {COutPoint(wtx.GetHash(), 0),
51 : : txout,
52 : : depth,
53 : : CalculateMaximumSignedInputSize(txout, &wallet, /*coin_control=*/nullptr),
54 : : /*solvable=*/ true,
55 : : /*safe=*/ true,
56 : : wtx.GetTxTime(),
57 : : is_from_me,
58 [ + - ]: 124 : fee_rate});
59 : 248 : }
60 : :
61 : 16 : CoinSelectionParams makeSelectionParams(FastRandomContext& rand, bool avoid_partial_spends)
62 : : {
63 : 16 : return CoinSelectionParams{
64 : : rand,
65 : : /*change_output_size=*/ 0,
66 : : /*change_spend_size=*/ 0,
67 : : /*min_change_target=*/ CENT,
68 : 16 : /*effective_feerate=*/ CFeeRate(0),
69 : 16 : /*long_term_feerate=*/ CFeeRate(0),
70 : 16 : /*discard_feerate=*/ CFeeRate(0),
71 : : /*tx_noinputs_size=*/ 0,
72 : : /*avoid_partial=*/ avoid_partial_spends,
73 : 16 : };
74 : : }
75 : :
76 : : class GroupVerifier
77 : : {
78 : : public:
79 : : std::shared_ptr<CWallet> wallet{nullptr};
80 : : CoinsResult coins_pool;
81 : : FastRandomContext rand;
82 : :
83 : 16 : void GroupVerify(const OutputType type,
84 : : const CoinEligibilityFilter& filter,
85 : : bool avoid_partial_spends,
86 : : bool positive_only,
87 : : int expected_size)
88 : : {
89 [ + - + - : 32 : OutputGroupTypeMap groups = GroupOutputs(*wallet, coins_pool, makeSelectionParams(rand, avoid_partial_spends), {{filter}})[filter];
+ - + + ]
90 [ + - ]: 6 : std::vector<OutputGroup>& groups_out = positive_only ? groups.groups_by_type[type].positive_group :
91 [ + + + - ]: 22 : groups.groups_by_type[type].mixed_group;
92 [ + - - + : 16 : BOOST_CHECK_EQUAL(groups_out.size(), expected_size);
+ - ]
93 : 16 : }
94 : :
95 : 8 : void GroupAndVerify(const OutputType type,
96 : : const CoinEligibilityFilter& filter,
97 : : int expected_with_partial_spends_size,
98 : : int expected_without_partial_spends_size,
99 : : bool positive_only)
100 : : {
101 : : // First avoid partial spends
102 : 8 : GroupVerify(type, filter, /*avoid_partial_spends=*/false, positive_only, expected_with_partial_spends_size);
103 : : // Second don't avoid partial spends
104 : 8 : GroupVerify(type, filter, /*avoid_partial_spends=*/true, positive_only, expected_without_partial_spends_size);
105 : 8 : }
106 : : };
107 : :
108 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(outputs_grouping_tests)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
109 : : {
110 : 1 : const auto& wallet = NewWallet(m_node);
111 : 1 : GroupVerifier group_verifier;
112 : 1 : group_verifier.wallet = wallet;
113 : :
114 [ + - ]: 1 : const CoinEligibilityFilter& BASIC_FILTER{1, 6, 0};
115 : :
116 : : // #################################################################################
117 : : // 10 outputs from different txs going to the same script
118 : : // 1) if partial spends is enabled --> must not be grouped
119 : : // 2) if partial spends is not enabled --> must be grouped into a single OutputGroup
120 : : // #################################################################################
121 : :
122 : 1 : unsigned long GROUP_SIZE = 10;
123 [ + - + - : 2 : const CTxDestination dest = *Assert(wallet->GetNewDestination(OutputType::BECH32, ""));
+ - + - ]
124 [ + + ]: 11 : for (unsigned long i = 0; i < GROUP_SIZE; i++) {
125 [ + - ]: 10 : addCoin(group_verifier.coins_pool, *wallet, dest, 10 * COIN, /*is_from_me=*/true);
126 : : }
127 : :
128 [ + - ]: 1 : group_verifier.GroupAndVerify(OutputType::BECH32,
129 : : BASIC_FILTER,
130 : : /*expected_with_partial_spends_size=*/ GROUP_SIZE,
131 : : /*expected_without_partial_spends_size=*/ 1,
132 : : /*positive_only=*/ true);
133 : :
134 : : // ####################################################################################
135 : : // 3) 10 more UTXO are added with a different script --> must be grouped into a single
136 : : // group for avoid partial spends and 10 different output groups for partial spends
137 : : // ####################################################################################
138 : :
139 [ + - + - : 2 : const CTxDestination dest2 = *Assert(wallet->GetNewDestination(OutputType::BECH32, ""));
+ - + - ]
140 [ + + ]: 11 : for (unsigned long i = 0; i < GROUP_SIZE; i++) {
141 [ + - ]: 10 : addCoin(group_verifier.coins_pool, *wallet, dest2, 5 * COIN, /*is_from_me=*/true);
142 : : }
143 : :
144 [ + - ]: 1 : group_verifier.GroupAndVerify(OutputType::BECH32,
145 : : BASIC_FILTER,
146 : : /*expected_with_partial_spends_size=*/ GROUP_SIZE * 2,
147 : : /*expected_without_partial_spends_size=*/ 2,
148 : : /*positive_only=*/ true);
149 : :
150 : : // ################################################################################
151 : : // 4) Now add a negative output --> which will be skipped if "positive_only" is set
152 : : // ################################################################################
153 : :
154 [ + - + - : 2 : const CTxDestination dest3 = *Assert(wallet->GetNewDestination(OutputType::BECH32, ""));
+ - + - ]
155 [ + - ]: 1 : addCoin(group_verifier.coins_pool, *wallet, dest3, 1, true, CFeeRate(100));
156 [ + - + - : 2 : BOOST_CHECK(group_verifier.coins_pool.coins[OutputType::BECH32].back().GetEffectiveValue() <= 0);
+ - + - ]
157 : :
158 : : // First expect no changes with "positive_only" enabled
159 [ + - ]: 1 : group_verifier.GroupAndVerify(OutputType::BECH32,
160 : : BASIC_FILTER,
161 : : /*expected_with_partial_spends_size=*/ GROUP_SIZE * 2,
162 : : /*expected_without_partial_spends_size=*/ 2,
163 : : /*positive_only=*/ true);
164 : :
165 : : // Then expect changes with "positive_only" disabled
166 [ + - ]: 1 : group_verifier.GroupAndVerify(OutputType::BECH32,
167 : : BASIC_FILTER,
168 : : /*expected_with_partial_spends_size=*/ GROUP_SIZE * 2 + 1,
169 : : /*expected_without_partial_spends_size=*/ 3,
170 : : /*positive_only=*/ false);
171 : :
172 : :
173 : : // ##############################################################################
174 : : // 5) Try to add a non-eligible UTXO (due not fulfilling the min depth target for
175 : : // "not mine" UTXOs) --> it must not be added to any group
176 : : // ##############################################################################
177 : :
178 [ + - + - : 2 : const CTxDestination dest4 = *Assert(wallet->GetNewDestination(OutputType::BECH32, ""));
+ - + - ]
179 [ + - ]: 1 : addCoin(group_verifier.coins_pool, *wallet, dest4, 6 * COIN,
180 [ + - ]: 1 : /*is_from_me=*/false, CFeeRate(0), /*depth=*/5);
181 : :
182 : : // Expect no changes from this round and the previous one (point 4)
183 [ + - ]: 1 : group_verifier.GroupAndVerify(OutputType::BECH32,
184 : : BASIC_FILTER,
185 : : /*expected_with_partial_spends_size=*/ GROUP_SIZE * 2 + 1,
186 : : /*expected_without_partial_spends_size=*/ 3,
187 : : /*positive_only=*/ false);
188 : :
189 : :
190 : : // ##############################################################################
191 : : // 6) Try to add a non-eligible UTXO (due not fulfilling the min depth target for
192 : : // "mine" UTXOs) --> it must not be added to any group
193 : : // ##############################################################################
194 : :
195 [ + - + - : 2 : const CTxDestination dest5 = *Assert(wallet->GetNewDestination(OutputType::BECH32, ""));
+ - + - ]
196 [ + - ]: 1 : addCoin(group_verifier.coins_pool, *wallet, dest5, 6 * COIN,
197 [ + - ]: 1 : /*is_from_me=*/true, CFeeRate(0), /*depth=*/0);
198 : :
199 : : // Expect no changes from this round and the previous one (point 5)
200 [ + - ]: 1 : group_verifier.GroupAndVerify(OutputType::BECH32,
201 : : BASIC_FILTER,
202 : : /*expected_with_partial_spends_size=*/ GROUP_SIZE * 2 + 1,
203 : : /*expected_without_partial_spends_size=*/ 3,
204 : : /*positive_only=*/ false);
205 : :
206 : : // ###########################################################################################
207 : : // 7) Surpass the OUTPUT_GROUP_MAX_ENTRIES and verify that a second partial group gets created
208 : : // ###########################################################################################
209 : :
210 [ + - + - : 2 : const CTxDestination dest7 = *Assert(wallet->GetNewDestination(OutputType::BECH32, ""));
+ - + - ]
211 : 1 : uint16_t NUM_SINGLE_ENTRIES = 101;
212 [ + + ]: 102 : for (unsigned long i = 0; i < NUM_SINGLE_ENTRIES; i++) { // OUTPUT_GROUP_MAX_ENTRIES{100}
213 [ + - ]: 101 : addCoin(group_verifier.coins_pool, *wallet, dest7, 9 * COIN, /*is_from_me=*/true);
214 : : }
215 : :
216 : : // Exclude partial groups only adds one more group to the previous test case (point 6)
217 : 1 : int PREVIOUS_ROUND_COUNT = GROUP_SIZE * 2 + 1;
218 [ + - ]: 1 : group_verifier.GroupAndVerify(OutputType::BECH32,
219 : : BASIC_FILTER,
220 : : /*expected_with_partial_spends_size=*/ PREVIOUS_ROUND_COUNT + NUM_SINGLE_ENTRIES,
221 : : /*expected_without_partial_spends_size=*/ 4,
222 : : /*positive_only=*/ false);
223 : :
224 : : // Include partial groups should add one more group inside the "avoid partial spends" count
225 [ + - ]: 1 : const CoinEligibilityFilter& avoid_partial_groups_filter{1, 6, 0, 0, /*include_partial=*/ true};
226 [ + - ]: 1 : group_verifier.GroupAndVerify(OutputType::BECH32,
227 : : avoid_partial_groups_filter,
228 : : /*expected_with_partial_spends_size=*/ PREVIOUS_ROUND_COUNT + NUM_SINGLE_ENTRIES,
229 : : /*expected_without_partial_spends_size=*/ 5,
230 : : /*positive_only=*/ false);
231 [ + - ]: 2 : }
232 : :
233 : : BOOST_AUTO_TEST_SUITE_END()
234 : : } // end namespace wallet
|