Skip to content
Bitcoin Roots

v29.4-roots.4 · Commit 28: [PATCH 28/37] fix(wallet): secure asynchronous sweep cancellation

src/wallet/test/spend_tests.cpp

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

File metadata
index b0ffdcc26c42bd1d3d09990b3c18f4c44b296d16..b3c12a3a4ed9853e80e504db9e4b3c99f5fb1f5c 100644

Download original patch · Release notes

Diff rows 1–57 of 57. 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
@@ -3,6 +3,7 @@
3
3
  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
4
 
5
5
  #include <consensus/amount.h>
6
+ #include <interfaces/wallet.h>
6
7
  #include <key.h>
7
8
  #include <policy/fees.h>
8
9
  #include <policy/policy.h>
@@ -61,29 +62,47 @@ BOOST_FIXTURE_TEST_CASE(sweep_transaction_failure_atomicity, TestChain100Setup)
61
62
  std::map<COutPoint, Coin> coins;
62
63
  coins.emplace(COutPoint{Txid{}, 1}, Coin{CTxOut{COIN, script}, 1, false});
63
64
  int broadcasts{0};
65
+ interfaces::SweepAuthorization signing_authorization;
64
66
 
65
67
  FlatSigningProvider missing_key;
66
- auto signing_failure{CreateSweepTransaction(*wallet, missing_key, coins, script, [&](const CTransactionRef&, std::string&) { ++broadcasts; return true; })};
68
+ auto signing_failure{CreateSweepTransaction(*wallet, missing_key, coins, script, [&](const CTransactionRef&, std::string&) { ++broadcasts; return true; }, signing_authorization)};
67
69
  BOOST_CHECK(!signing_failure);
68
70
  BOOST_CHECK_EQUAL(broadcasts, 0);
69
71
 
70
72
  FlatSigningProvider provider;
71
73
  provider.keys.emplace(key.GetPubKey().GetID(), key);
72
74
  provider.pubkeys.emplace(key.GetPubKey().GetID(), key.GetPubKey());
73
- auto success{CreateSweepTransaction(*wallet, provider, coins, script, [&](const CTransactionRef&, std::string&) { ++broadcasts; return true; })};
75
+ interfaces::SweepAuthorization success_authorization;
76
+ auto success{CreateSweepTransaction(*wallet, provider, coins, script, [&](const CTransactionRef&, std::string&) { ++broadcasts; return true; }, success_authorization)};
74
77
  BOOST_REQUIRE(success);
75
78
  BOOST_CHECK_GE(success->fee, GetMinimumFee(*wallet, GetVirtualTransactionSize(*success->tx), CCoinControl{}, nullptr));
76
79
  BOOST_CHECK_EQUAL(broadcasts, 1);
77
80
 
78
81
  std::string rejection;
82
+ interfaces::SweepAuthorization failure_authorization;
79
83
  auto broadcast_failure{CreateSweepTransaction(*wallet, provider, coins, script, [&](const CTransactionRef&, std::string& error) {
80
84
  ++broadcasts;
81
85
  error = "txn-mempool-conflict";
82
86
  return false;
83
- })};
87
+ }, failure_authorization)};
84
88
  BOOST_CHECK(!broadcast_failure);
85
89
  BOOST_CHECK(util::ErrorString(broadcast_failure).original.find("txn-mempool-conflict") != std::string::npos);
86
90
  BOOST_CHECK_EQUAL(broadcasts, 2);
91
+
92
+ interfaces::SweepAuthorization cancelled_authorization;
93
+ BOOST_REQUIRE(cancelled_authorization.Cancel());
94
+ auto cancelled{CreateSweepTransaction(*wallet, provider, coins, script, [&](const CTransactionRef&, std::string&) {
95
+ ++broadcasts;
96
+ return true;
97
+ }, cancelled_authorization)};
98
+ BOOST_CHECK(!cancelled);
99
+ BOOST_CHECK_EQUAL(util::ErrorString(cancelled).original, "Sweep cancelled");
100
+ BOOST_CHECK_EQUAL(broadcasts, 2);
101
+
102
+ interfaces::SweepAuthorization committed_authorization;
103
+ BOOST_REQUIRE(committed_authorization.BeginBroadcast());
104
+ BOOST_CHECK(!committed_authorization.Cancel());
105
+ BOOST_CHECK(committed_authorization.BroadcastStarted());
87
106
  }
88
107
 
89
108
  BOOST_FIXTURE_TEST_CASE(SubtractFee, TestChain100Setup)