Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/wallet/bdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,12 +709,14 @@ void BerkeleyDatabase::ReloadDbEnv()
env->ReloadDbEnv();
}

BerkeleyCursor::BerkeleyCursor(BerkeleyDatabase& database)
BerkeleyCursor::BerkeleyCursor(BerkeleyDatabase& database, const BerkeleyBatch& batch)
{
if (!database.m_db.get()) {
throw std::runtime_error(STR_INTERNAL_BUG("BerkeleyDatabase does not exist"));
}
int ret = database.m_db->cursor(nullptr, &m_cursor, 0);
// Transaction argument to cursor is only needed when using the cursor to
// write to the database. Read-only cursors do not need a txn pointer.
int ret = database.m_db->cursor(batch.txn(), &m_cursor, 0);
if (ret != 0) {
throw std::runtime_error(STR_INTERNAL_BUG(strprintf("BDB Cursor could not be created. Returned %d", ret)));
}
Expand Down Expand Up @@ -752,7 +754,7 @@ BerkeleyCursor::~BerkeleyCursor()
std::unique_ptr<DatabaseCursor> BerkeleyBatch::GetNewCursor()
{
if (!pdb) return nullptr;
return std::make_unique<BerkeleyCursor>(m_database);
return std::make_unique<BerkeleyCursor>(m_database, *this);
}

bool BerkeleyBatch::TxnBegin()
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/bdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class BerkeleyCursor : public DatabaseCursor
Dbc* m_cursor;

public:
explicit BerkeleyCursor(BerkeleyDatabase& database);
explicit BerkeleyCursor(BerkeleyDatabase& database, const BerkeleyBatch& batch);
~BerkeleyCursor() override;

Status Next(DataStream& key, DataStream& value) override;
Expand Down
18 changes: 1 addition & 17 deletions src/wallet/scriptpubkeyman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2182,23 +2182,7 @@ bool LegacyScriptPubKeyMan::DeleteRecords()
{
LOCK(cs_KeyStore);
WalletBatch batch(m_storage.GetDatabase());
// Wrap the legacy-record deletion in a single SQLite transaction. Without
// this, EraseRecords cursor-walks the entire DB and every matching row's
// Erase auto-commits+fsyncs on its own — legacy wallets with thousands of
// KEY/HDPUBKEY/POOL/KEYMETA rows then take minutes instead of milliseconds.
if (!batch.TxnBegin()) {
WalletLogPrintf("DeleteRecords: TxnBegin failed\n");
return false;
}
if (!batch.EraseRecords(DBKeys::LEGACY_TYPES)) {
batch.TxnAbort();
return false;
}
if (!batch.TxnCommit()) {
WalletLogPrintf("DeleteRecords: TxnCommit failed\n");
return false;
}
return true;
return batch.EraseRecords(DBKeys::LEGACY_TYPES);
}

util::Result<CTxDestination> DescriptorScriptPubKeyMan::GetNewDestination()
Expand Down
9 changes: 9 additions & 0 deletions src/wallet/test/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ struct WalletContext;

const std::string ADDRESS_B58T_UNSPENDABLE = "yXXXXXXXXXXXXXXXXXXXXXXXXXXXVd2rXU";

static const DatabaseFormat DATABASE_FORMATS[] = {
#ifdef USE_SQLITE
DatabaseFormat::SQLITE,
#endif
#ifdef USE_BDB
DatabaseFormat::BERKELEY,
#endif
};

std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, ChainstateManager& chainman, const CKey& key);

std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context);
Expand Down
9 changes: 0 additions & 9 deletions src/wallet/test/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,15 +454,6 @@ BOOST_AUTO_TEST_CASE(ComputeTimeSmart)
BOOST_CHECK_EQUAL(AddTx(*m_node.chainman, m_wallet, 5, 50, 600), 300);
}

static const DatabaseFormat DATABASE_FORMATS[] = {
#ifdef USE_SQLITE
DatabaseFormat::SQLITE,
#endif
#ifdef USE_BDB
DatabaseFormat::BERKELEY,
#endif
};

void TestLoadWallet(const std::string& name, DatabaseFormat format, std::function<void(std::shared_ptr<CWallet>)> f)
{
DatabaseOptions options;
Expand Down
27 changes: 27 additions & 0 deletions src/wallet/test/walletdb_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <wallet/hdchain.h>
#include <wallet/test/util.h>
#include <wallet/test/wallet_test_fixture.h>
#include <wallet/wallet.h>
#include <wallet/walletdb.h>

#include <boost/test/unit_test.hpp>
Expand Down Expand Up @@ -149,5 +150,31 @@ BOOST_AUTO_TEST_CASE(walletdb_platform_data_corruption_policy)
BOOST_CHECK(platform_data.at("platform/intact") == std::vector<unsigned char>({0x01, 0x02}));
}

BOOST_AUTO_TEST_CASE(walletdb_read_write_deadlock)
{
// Exercises a db read write operation that shouldn't deadlock.
for (const DatabaseFormat& db_format : DATABASE_FORMATS) {
// Context setup
DatabaseOptions options;
options.require_format = db_format;
DatabaseStatus status;
bilingual_str error_string;
std::unique_ptr<WalletDatabase> db = MakeDatabase(m_path_root / strprintf("wallet_%d_.dat", db_format).c_str(), options, status, error_string);
BOOST_ASSERT(status == DatabaseStatus::SUCCESS);

std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", std::move(db)));
wallet->m_keypool_size = 4;

// Create legacy spkm
LOCK(wallet->cs_wallet);
auto legacy_spkm = wallet->GetOrCreateLegacyScriptPubKeyMan();
BOOST_CHECK(legacy_spkm->CanGenerateKeys() && legacy_spkm->TopUp());
wallet->Flush();

// Now delete all records, which performs a read write operation.
BOOST_CHECK(wallet->GetLegacyScriptPubKeyMan()->DeleteRecords());
}
}

BOOST_AUTO_TEST_SUITE_END()
} // namespace wallet
18 changes: 14 additions & 4 deletions src/wallet/walletdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,9 @@ bool WalletBatch::WriteWalletFlags(const uint64_t flags)

bool WalletBatch::EraseRecords(const std::unordered_set<std::string>& types)
{
// Begin db txn
if (!m_batch->TxnBegin()) return false;

// Get cursor
std::unique_ptr<DatabaseCursor> cursor = m_batch->GetNewCursor();
if (!cursor)
Expand All @@ -1256,15 +1259,16 @@ bool WalletBatch::EraseRecords(const std::unordered_set<std::string>& types)
}

// Iterate the DB and look for any records that have the type prefixes
while (true)
{
while (true) {
// Read next record
DataStream key{};
DataStream value{};
DatabaseCursor::Status status = cursor->Next(key, value);
if (status == DatabaseCursor::Status::DONE) {
break;
} else if (status == DatabaseCursor::Status::FAIL) {
cursor.reset(nullptr);
m_batch->TxnAbort(); // abort db txn
return false;
}

Expand All @@ -1275,10 +1279,16 @@ bool WalletBatch::EraseRecords(const std::unordered_set<std::string>& types)
key >> type;

if (types.count(type) > 0) {
m_batch->Erase(key_data);
if (!m_batch->Erase(key_data)) {
cursor.reset(nullptr);
m_batch->TxnAbort();
return false; // erase failed
}
}
}
return true;
// Finish db txn
cursor.reset(nullptr);
return m_batch->TxnCommit();
}

bool WalletBatch::TxnBegin()
Expand Down
Loading