Skip to content
Bitcoin Roots

v30.3-roots.1 · Commit 28: [PATCH 28/68] 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 93f992b3e16d1114f66071c679135cbf7dfc7457..dd1ac13a193f2c3d88b9992dc9d407d2887a1093 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. Before is on the left; after is on the right. Unified view fits narrow screens more easily.

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