Skip to content
Bitcoin Roots

v31.1-roots.1 · Commit 6: [PATCH 06/17] feat(wallet): preserve Roots signing and spend controls on v31

src/wallet/test/spend_tests.cpp

Before: src/wallet/test/spend_tests.cpp · After: src/wallet/test/spend_tests.cpp

File metadata
index 218792ff0333936caa59e00024f9266d0dd2c6ce..c7f8f479f865f4fe93377df006837a79e8def727 100644

Download original patch · Release notes

Diff rows 1–268 of 268. Line numbers refer to the original before/after files.

+ Added− RemovedUnmarked lines are context.

Scroll the code area horizontally to read long lines. The two line-number columns refer to before and after.

src/wallet/test/spend_tests.cpp CHANGED
@@ -2,12 +2,24 @@
2
2
  // Distributed under the MIT software license, see the accompanying
3
3
  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
4
 
5
+ #include <algorithm>
6
+ #include <barrier>
7
+ #include <thread>
8
+ #include <util/rbf.h>
9
+ #include <wallet/fees.h>
10
+ #include <interfaces/wallet.h>
5
11
  #include <consensus/amount.h>
6
12
  #include <key.h>
13
+ #include <key_io.h>
7
14
  #include <policy/fees/block_policy_estimator.h>
15
+ #include <policy/policy.h>
16
+ #include <random.h>
8
17
  #include <script/solver.h>
18
+ #include <script/descriptor.h>
19
+ #include <util/strencodings.h>
9
20
  #include <validation.h>
10
21
  #include <wallet/coincontrol.h>
22
+ #include <wallet/context.h>
11
23
  #include <wallet/spend.h>
12
24
  #include <wallet/test/util.h>
13
25
  #include <wallet/test/wallet_test_fixture.h>
@@ -17,6 +29,60 @@
17
29
  namespace wallet {
18
30
  BOOST_FIXTURE_TEST_SUITE(spend_tests, WalletTestingSetup)
19
31
 
32
+ BOOST_AUTO_TEST_CASE(sweep_destination_requires_local_signature_satisfaction)
33
+ {
34
+ auto wallet{std::make_shared<CWallet>(m_node.chain.get(), "", CreateMockableWalletDatabase())};
35
+ const CKey local1{GenerateRandomKey()}, local2{GenerateRandomKey()}, foreign{GenerateRandomKey()};
36
+ const std::string private1{EncodeSecret(local1)}, private2{EncodeSecret(local2)}, public_foreign{HexStr(foreign.GetPubKey())};
37
+ const auto add_descriptor = [&](const std::string& text) {
38
+ FlatSigningProvider keys, public_data;
39
+ std::string error;
40
+ auto descriptors{Parse(text, keys, error, /*require_checksum=*/false)};
41
+ BOOST_REQUIRE_MESSAGE(descriptors.size() == 1, error);
42
+ std::vector<CScript> scripts;
43
+ BOOST_REQUIRE(descriptors[0]->Expand(0, keys, scripts, public_data));
44
+ BOOST_REQUIRE_EQUAL(scripts.size(), 1);
45
+ WalletDescriptor descriptor{std::move(descriptors[0]), 0, 0, 0, 0};
46
+ LOCK(wallet->cs_wallet);
47
+ BOOST_REQUIRE(wallet->AddWalletDescriptor(descriptor, keys, "", false));
48
+ return scripts[0];
49
+ };
50
+ WITH_LOCK(wallet->cs_wallet, wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS));
51
+ const auto incomplete{add_descriptor("wsh(multi(2," + private1 + "," + public_foreign + "))")};
52
+ const auto sufficient{add_descriptor("wsh(multi(2," + private1 + "," + private2 + "," + public_foreign + "))")};
53
+ const auto script_path{add_descriptor("tr(" + public_foreign.substr(2) + ",pk(" + private1 + "))")};
54
+ const auto absent_paths{add_descriptor("tr(" + public_foreign.substr(2) + ",pk(" + public_foreign.substr(2) + "))")};
55
+ const auto musig_partial{add_descriptor("tr(musig(" + private1 + "," + public_foreign + "))")};
56
+ const auto musig_complete{add_descriptor("tr(musig(" + private1 + "," + private2 + "))")};
57
+ const auto check = [&] {
58
+ LOCK(wallet->cs_wallet);
59
+ BOOST_CHECK(wallet->IsMine(incomplete));
60
+ BOOST_CHECK(!CanSweepTo(*wallet, incomplete));
61
+ BOOST_CHECK(CanSweepTo(*wallet, sufficient));
62
+ BOOST_CHECK(CanSweepTo(*wallet, script_path));
63
+ BOOST_CHECK(!CanSweepTo(*wallet, absent_paths));
64
+ BOOST_CHECK(!CanSweepTo(*wallet, musig_partial));
65
+ BOOST_CHECK(CanSweepTo(*wallet, musig_complete));
66
+ };
67
+ check();
68
+ BOOST_REQUIRE(wallet->EncryptWallet(SecureString{"test-passphrase"}));
69
+ BOOST_REQUIRE(wallet->IsLocked());
70
+ check();
71
+ WalletContext context;
72
+ auto interface{interfaces::MakeWallet(context, wallet)};
73
+ const SecureString supplied{private2.begin(), private2.end()};
74
+ CTxDestination destination;
75
+ BOOST_REQUIRE(ExtractDestination(incomplete, destination));
76
+ auto rejected{interface->sweepPrivateKey(supplied, destination, false, std::nullopt, std::make_shared<interfaces::SweepAuthorization>())};
77
+ BOOST_CHECK(!rejected);
78
+ BOOST_CHECK_EQUAL(util::ErrorString(rejected).original, "Destination is not controlled by this wallet");
79
+ BOOST_REQUIRE(ExtractDestination(sufficient, destination));
80
+ auto accepted{interface->sweepPrivateKey(supplied, destination, false, std::nullopt, std::make_shared<interfaces::SweepAuthorization>())};
81
+ BOOST_CHECK(!accepted);
82
+ BOOST_CHECK_EQUAL(util::ErrorString(accepted).original, "No spendable UTXOs found");
83
+ BOOST_CHECK(wallet->IsLocked());
84
+ }
85
+
20
86
  BOOST_AUTO_TEST_CASE(max_signed_input_size_uses_external_outpoint)
21
87
  {
22
88
  const CKey key{GenerateRandomKey()};
@@ -33,6 +99,141 @@ BOOST_AUTO_TEST_CASE(max_signed_input_size_uses_external_outpoint)
33
99
  BOOST_CHECK_EQUAL(high_r, low_r + 1);
34
100
  }
35
101
 
102
+ BOOST_AUTO_TEST_CASE(anti_fee_sniping_avoids_reserved_policy_locktime)
103
+ {
104
+ FastRandomContext rng{/*fDeterministic=*/true};
105
+ const int first_height{static_cast<int>(PARASITE_CAT21_LOCKTIME)};
106
+ for (int height{first_height}; height < first_height + 100; ++height) {
107
+ for (int sample = 0; sample < 1000; ++sample) {
108
+ const uint32_t lock_time{GetAntiFeeSnipingLockTime(height, rng)};
109
+ BOOST_CHECK_NE(lock_time, PARASITE_CAT21_LOCKTIME);
110
+ BOOST_CHECK_LE(lock_time, height);
111
+ }
112
+ }
113
+ }
114
+
115
+ BOOST_AUTO_TEST_CASE(sweep_cancel_and_broadcast_are_mutually_exclusive)
116
+ {
117
+ interfaces::SweepAuthorization authorization;
118
+ std::barrier start{3};
119
+ bool cancelled{false}, broadcast{false};
120
+ std::thread cancellation{[&] { start.arrive_and_wait(); cancelled = authorization.Cancel(); }};
121
+ std::thread submission{[&] { start.arrive_and_wait(); broadcast = authorization.BeginBroadcast(); }};
122
+ start.arrive_and_wait();
123
+ cancellation.join();
124
+ submission.join();
125
+ BOOST_CHECK(cancelled != broadcast);
126
+ BOOST_CHECK_EQUAL(authorization.IsCancelled(), cancelled);
127
+ BOOST_CHECK_EQUAL(authorization.BroadcastStarted(), broadcast);
128
+ }
129
+
130
+ BOOST_FIXTURE_TEST_CASE(sweep_transaction_failure_atomicity, TestChain100Setup)
131
+ {
132
+ CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
133
+ auto wallet = CreateSyncedWallet(*m_node.chain, WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain()), coinbaseKey);
134
+ const CKey key{GenerateRandomKey()};
135
+ const CScript script{GetScriptForDestination(PKHash{key.GetPubKey()})};
136
+ std::map<COutPoint, Coin> coins;
137
+ coins.emplace(COutPoint{Txid{}, 1}, Coin{CTxOut{COIN, script}, 1, false});
138
+ int broadcasts{0};
139
+ interfaces::SweepAuthorization signing_authorization;
140
+
141
+ FlatSigningProvider missing_key;
142
+ auto signing_failure{CreateSweepTransaction(*wallet, missing_key, coins, script, [&](const CTransactionRef&, std::string&) { ++broadcasts; return true; }, signing_authorization)};
143
+ BOOST_CHECK(!signing_failure);
144
+ BOOST_CHECK_EQUAL(broadcasts, 0);
145
+
146
+ FlatSigningProvider provider;
147
+ provider.keys.emplace(key.GetPubKey().GetID(), key);
148
+ provider.pubkeys.emplace(key.GetPubKey().GetID(), key.GetPubKey());
149
+ interfaces::SweepAuthorization success_authorization;
150
+ auto success{CreateSweepTransaction(*wallet, provider, coins, script, [&](const CTransactionRef&, std::string&) { ++broadcasts; return true; }, success_authorization)};
151
+ BOOST_REQUIRE(success);
152
+ BOOST_CHECK_GE(success->fee, GetMinimumFee(*wallet, GetVirtualTransactionSize(*success->tx), CCoinControl{}, nullptr));
153
+ BOOST_CHECK_EQUAL(broadcasts, 1);
154
+
155
+ std::string rejection;
156
+ interfaces::SweepAuthorization failure_authorization;
157
+ auto broadcast_failure{CreateSweepTransaction(*wallet, provider, coins, script, [&](const CTransactionRef&, std::string& error) {
158
+ ++broadcasts;
159
+ error = "txn-mempool-conflict";
160
+ return false;
161
+ }, failure_authorization)};
162
+ BOOST_CHECK(!broadcast_failure);
163
+ BOOST_CHECK(util::ErrorString(broadcast_failure).original.find("txn-mempool-conflict") != std::string::npos);
164
+ BOOST_CHECK_EQUAL(broadcasts, 2);
165
+
166
+ interfaces::SweepAuthorization cancelled_authorization;
167
+ BOOST_REQUIRE(cancelled_authorization.Cancel());
168
+ auto cancelled{CreateSweepTransaction(*wallet, provider, coins, script, [&](const CTransactionRef&, std::string&) {
169
+ ++broadcasts;
170
+ return true;
171
+ }, cancelled_authorization)};
172
+ BOOST_CHECK(!cancelled);
173
+ BOOST_CHECK_EQUAL(util::ErrorString(cancelled).original, "Sweep cancelled");
174
+ BOOST_CHECK_EQUAL(broadcasts, 2);
175
+
176
+ interfaces::SweepAuthorization committed_authorization;
177
+ BOOST_REQUIRE(committed_authorization.BeginBroadcast());
178
+ BOOST_CHECK(!committed_authorization.Cancel());
179
+ BOOST_CHECK(committed_authorization.BroadcastStarted());
180
+ }
181
+
182
+ BOOST_FIXTURE_TEST_CASE(sweep_transaction_uses_one_fee_snapshot, TestChain100Setup)
183
+ {
184
+ CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
185
+ auto wallet = CreateSyncedWallet(*m_node.chain, WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain()), coinbaseKey);
186
+ const CKey key{GenerateRandomKey()};
187
+ const CScript script{GetScriptForDestination(PKHash{key.GetPubKey()})};
188
+ const std::map<COutPoint, Coin> coins{{COutPoint{Txid{}, 1}, Coin{CTxOut{COIN, script}, 1, false}}};
189
+ const CFeeRate initial_rate{1000};
190
+ const CFeeRate changed_rate{10000};
191
+ WITH_LOCK(wallet->cs_wallet, wallet->m_fallback_fee = initial_rate);
192
+
193
+ // Change the wallet setting when signing the fee-adjusted transaction,
194
+ // after the operation has selected its fee rate. This deterministically
195
+ // models a a fee-settings change without timing-sensitive threads.
196
+ class FeeChangingProvider final : public SigningProvider
197
+ {
198
+ CWallet& m_wallet;
199
+ FlatSigningProvider m_provider;
200
+ CFeeRate m_changed_rate;
201
+
202
+ public:
203
+ mutable int key_requests{0};
204
+ FeeChangingProvider(CWallet& wallet, const CKey& key, CFeeRate changed_rate)
205
+ : m_wallet{wallet}, m_changed_rate{changed_rate}
206
+ {
207
+ m_provider.keys.emplace(key.GetPubKey().GetID(), key);
208
+ m_provider.pubkeys.emplace(key.GetPubKey().GetID(), key.GetPubKey());
209
+ }
210
+ bool GetKey(const CKeyID& id, CKey& key_out) const override
211
+ {
212
+ AssertLockNotHeld(m_wallet.cs_wallet);
213
+ if (++key_requests == 2) {
214
+ WITH_LOCK(m_wallet.cs_wallet, m_wallet.m_fallback_fee = m_changed_rate);
215
+ }
216
+ return m_provider.GetKey(id, key_out);
217
+ }
218
+ bool GetPubKey(const CKeyID& id, CPubKey& pubkey) const override
219
+ {
220
+ return m_provider.GetPubKey(id, pubkey);
221
+ }
222
+ } provider{*wallet, key, changed_rate};
223
+
224
+ interfaces::SweepAuthorization authorization;
225
+ auto result{CreateSweepTransaction(*wallet, provider, coins, script, [&](const CTransactionRef&, std::string&) {
226
+ AssertLockNotHeld(wallet->cs_wallet);
227
+ return true;
228
+ }, authorization)};
229
+ BOOST_REQUIRE(result);
230
+ BOOST_CHECK_GE(provider.key_requests, 2);
231
+ BOOST_CHECK(WITH_LOCK(wallet->cs_wallet, return wallet->m_fallback_fee == changed_rate));
232
+ const auto vsize{GetVirtualTransactionSize(*result->tx)};
233
+ BOOST_CHECK_GE(result->fee, initial_rate.GetFee(vsize));
234
+ BOOST_CHECK_LT(result->fee, changed_rate.GetFee(vsize));
235
+ }
236
+
36
237
  BOOST_FIXTURE_TEST_CASE(SubtractFee, TestChain100Setup)
37
238
  {
38
239
  CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
@@ -121,5 +322,43 @@ BOOST_FIXTURE_TEST_CASE(wallet_duplicated_preset_inputs_test, TestChain100Setup)
121
322
  BOOST_CHECK(!CreateTransaction(*wallet, recipients, /*change_pos=*/std::nullopt, coin_control));
122
323
  }
123
324
 
325
+ BOOST_FIXTURE_TEST_CASE(manual_coin_control_honors_selection_and_rbf, TestChain100Setup)
326
+ {
327
+ CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
328
+ auto wallet{CreateSyncedWallet(*m_node.chain, WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain()), coinbaseKey)};
329
+ const auto coins{WITH_LOCK(wallet->cs_wallet, return AvailableCoins(*wallet).All())};
330
+ BOOST_REQUIRE(!coins.empty());
331
+ const COutput selected_coin{coins.front()};
332
+ const CTxDestination destination{PKHash{GenerateRandomKey().GetPubKey()}};
333
+ const CRecipient recipient{destination, COIN, /*subtract_fee=*/false};
334
+
335
+ CCoinControl coin_control;
336
+ coin_control.m_allow_other_inputs = false;
337
+ coin_control.m_signal_bip125_rbf = false;
338
+ coin_control.Select(selected_coin.outpoint);
339
+
340
+ const auto no_rbf{CreateTransaction(*wallet, {recipient}, /*change_pos=*/std::nullopt, coin_control, /*sign=*/false)};
341
+ BOOST_REQUIRE(no_rbf);
342
+ BOOST_REQUIRE_EQUAL(no_rbf->tx->vin.size(), 1U);
343
+ BOOST_CHECK(no_rbf->tx->vin.front().prevout == selected_coin.outpoint);
344
+ BOOST_CHECK(no_rbf->tx->vin.front().nSequence == CTxIn::MAX_SEQUENCE_NONFINAL);
345
+ CAmount outputs{0};
346
+ for (const auto& output : no_rbf->tx->vout) outputs += output.nValue;
347
+ BOOST_CHECK_EQUAL(selected_coin.txout.nValue, outputs + no_rbf->fee);
348
+
349
+ coin_control.m_signal_bip125_rbf = true;
350
+ const auto rbf{CreateTransaction(*wallet, {recipient}, /*change_pos=*/std::nullopt, coin_control, /*sign=*/false)};
351
+ BOOST_REQUIRE(rbf);
352
+ BOOST_REQUIRE_EQUAL(rbf->tx->vin.size(), 1U);
353
+ BOOST_CHECK(rbf->tx->vin.front().prevout == selected_coin.outpoint);
354
+ BOOST_CHECK_EQUAL(rbf->tx->vin.front().nSequence, MAX_BIP125_RBF_SEQUENCE);
355
+
356
+ WITH_LOCK(wallet->cs_wallet, wallet->LockCoin(selected_coin.outpoint, /*persist=*/false));
357
+ const auto available_after_lock{WITH_LOCK(wallet->cs_wallet, return AvailableCoins(*wallet).All())};
358
+ BOOST_CHECK(std::none_of(available_after_lock.cbegin(), available_after_lock.cend(),
359
+ [&selected_coin](const COutput& coin) { return coin.outpoint == selected_coin.outpoint; }));
360
+ BOOST_CHECK(WITH_LOCK(wallet->cs_wallet, return wallet->IsLockedCoin(selected_coin.outpoint)));
361
+ }
362
+
124
363
  BOOST_AUTO_TEST_SUITE_END()
125
364
  } // namespace wallet