aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/test/wallet_tests.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/wallet/test/wallet_tests.cpp')
-rw-r--r--src/wallet/test/wallet_tests.cpp42
1 files changed, 38 insertions, 4 deletions
diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp
index 53f3bcc421..b5de4b4b3d 100644
--- a/src/wallet/test/wallet_tests.cpp
+++ b/src/wallet/test/wallet_tests.cpp
@@ -65,8 +65,9 @@ static void AddKey(CWallet& wallet, const CKey& key)
LOCK(wallet.cs_wallet);
FlatSigningProvider provider;
std::string error;
- std::unique_ptr<Descriptor> desc = Parse("combo(" + EncodeSecret(key) + ")", provider, error, /* require_checksum=*/ false);
- assert(desc);
+ auto descs = Parse("combo(" + EncodeSecret(key) + ")", provider, error, /* require_checksum=*/ false);
+ assert(descs.size() == 1);
+ auto& desc = descs.at(0);
WalletDescriptor w_desc(std::move(desc), 0, 0, 1, 1);
if (!wallet.AddWalletDescriptor(w_desc, provider, "", false)) assert(false);
}
@@ -329,6 +330,39 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
}
}
+// This test verifies that wallet settings can be added and removed
+// concurrently, ensuring no race conditions occur during either process.
+BOOST_FIXTURE_TEST_CASE(write_wallet_settings_concurrently, TestingSetup)
+{
+ auto chain = m_node.chain.get();
+ const auto NUM_WALLETS{5};
+
+ // Since we're counting the number of wallets, ensure we start without any.
+ BOOST_REQUIRE(chain->getRwSetting("wallet").isNull());
+
+ const auto& check_concurrent_wallet = [&](const auto& settings_function, int num_expected_wallets) {
+ std::vector<std::thread> threads;
+ threads.reserve(NUM_WALLETS);
+ for (auto i{0}; i < NUM_WALLETS; ++i) threads.emplace_back(settings_function, i);
+ for (auto& t : threads) t.join();
+
+ auto wallets = chain->getRwSetting("wallet");
+ BOOST_CHECK_EQUAL(wallets.getValues().size(), num_expected_wallets);
+ };
+
+ // Add NUM_WALLETS wallets concurrently, ensure we end up with NUM_WALLETS stored.
+ check_concurrent_wallet([&chain](int i) {
+ Assert(AddWalletSetting(*chain, strprintf("wallet_%d", i)));
+ },
+ /*num_expected_wallets=*/NUM_WALLETS);
+
+ // Remove NUM_WALLETS wallets concurrently, ensure we end up with 0 wallets.
+ check_concurrent_wallet([&chain](int i) {
+ Assert(RemoveWalletSetting(*chain, strprintf("wallet_%d", i)));
+ },
+ /*num_expected_wallets=*/0);
+}
+
// Check that GetImmatureCredit() returns a newly calculated value instead of
// the cached value after a MarkDirty() call.
//
@@ -889,7 +923,7 @@ BOOST_FIXTURE_TEST_CASE(CreateWalletWithoutChain, BasicTestingSetup)
context.args = &m_args;
auto wallet = TestLoadWallet(context);
BOOST_CHECK(wallet);
- UnloadWallet(std::move(wallet));
+ WaitForDeleteWallet(std::move(wallet));
}
BOOST_FIXTURE_TEST_CASE(RemoveTxs, TestChain100Setup)
@@ -945,7 +979,7 @@ BOOST_FIXTURE_TEST_CASE(wallet_sync_tx_invalid_state_test, TestingSetup)
CMutableTransaction mtx;
mtx.vout.emplace_back(COIN, GetScriptForDestination(op_dest));
- mtx.vin.emplace_back(Txid::FromUint256(g_insecure_rand_ctx.rand256()), 0);
+ mtx.vin.emplace_back(Txid::FromUint256(m_rng.rand256()), 0);
const auto& tx_id_to_spend = wallet.AddToWallet(MakeTransactionRef(mtx), TxStateInMempool{})->GetHash();
{