mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-01 16:53:52 +02:00
Merge bitcoin/bitcoin#27790: walletdb: Add PrefixCursor
ba616b932cwallet: Add GetPrefixCursor to DatabaseBatch (Andrew Chow)1d858b055dwalletdb: Handle when database keys are empty (Ryan Ofsky)84b2f353bbwalletdb: Consistently clear key and value streams before writing (Andrew Chow) Pull request description: Split from #24914 as suggested in https://github.com/bitcoin/bitcoin/pull/24914#pullrequestreview-1442091917 This PR adds a wallet database cursor that gives a view over all of the records beginning with the same prefix. ACKs for top commit: ryanofsky: Code review ACKba616b932c. Just suggested changes since last review furszy: ACKba616b93Tree-SHA512: 38a61849f108d8003d28c599b1ad0421ac9beb3afe14c02f1253e7b4efc3d4eef483e32647a820fc6636bca3f9efeff9fe062b6b602e0cded69f21f8b26af544
This commit is contained in:
@@ -6,13 +6,56 @@
|
||||
|
||||
#include <test/util/setup_common.h>
|
||||
#include <util/fs.h>
|
||||
#include <util/translation.h>
|
||||
#ifdef USE_BDB
|
||||
#include <wallet/bdb.h>
|
||||
#endif
|
||||
#ifdef USE_SQLITE
|
||||
#include <wallet/sqlite.h>
|
||||
#endif
|
||||
#include <wallet/test/util.h>
|
||||
#include <wallet/walletutil.h> // for WALLET_FLAG_DESCRIPTORS
|
||||
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, const std::pair<const SerializeData, SerializeData>& kv)
|
||||
{
|
||||
Span key{kv.first}, value{kv.second};
|
||||
os << "(\"" << std::string_view{reinterpret_cast<const char*>(key.data()), key.size()} << "\", \""
|
||||
<< std::string_view{reinterpret_cast<const char*>(key.data()), key.size()} << "\")";
|
||||
return os;
|
||||
}
|
||||
|
||||
namespace wallet {
|
||||
|
||||
static Span<const std::byte> StringBytes(std::string_view str)
|
||||
{
|
||||
return AsBytes<const char>({str.data(), str.size()});
|
||||
}
|
||||
|
||||
static SerializeData StringData(std::string_view str)
|
||||
{
|
||||
auto bytes = StringBytes(str);
|
||||
return SerializeData{bytes.begin(), bytes.end()};
|
||||
}
|
||||
|
||||
static void CheckPrefix(DatabaseBatch& batch, Span<const std::byte> prefix, MockableData expected)
|
||||
{
|
||||
std::unique_ptr<DatabaseCursor> cursor = batch.GetNewPrefixCursor(prefix);
|
||||
MockableData actual;
|
||||
while (true) {
|
||||
DataStream key, value;
|
||||
DatabaseCursor::Status status = cursor->Next(key, value);
|
||||
if (status == DatabaseCursor::Status::DONE) break;
|
||||
BOOST_CHECK(status == DatabaseCursor::Status::MORE);
|
||||
BOOST_CHECK(
|
||||
actual.emplace(SerializeData(key.begin(), key.end()), SerializeData(value.begin(), value.end())).second);
|
||||
}
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end());
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(db_tests, BasicTestingSetup)
|
||||
|
||||
static std::shared_ptr<BerkeleyEnvironment> GetWalletEnv(const fs::path& path, fs::path& database_filename)
|
||||
@@ -78,5 +121,90 @@ BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_free_instance)
|
||||
BOOST_CHECK(env_2_a == env_2_b);
|
||||
}
|
||||
|
||||
static std::vector<std::unique_ptr<WalletDatabase>> TestDatabases(const fs::path& path_root)
|
||||
{
|
||||
std::vector<std::unique_ptr<WalletDatabase>> dbs;
|
||||
DatabaseOptions options;
|
||||
DatabaseStatus status;
|
||||
bilingual_str error;
|
||||
#ifdef USE_BDB
|
||||
dbs.emplace_back(MakeBerkeleyDatabase(path_root / "bdb", options, status, error));
|
||||
#endif
|
||||
#ifdef USE_SQLITE
|
||||
dbs.emplace_back(MakeSQLiteDatabase(path_root / "sqlite", options, status, error));
|
||||
#endif
|
||||
dbs.emplace_back(CreateMockableWalletDatabase());
|
||||
return dbs;
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(db_cursor_prefix_range_test)
|
||||
{
|
||||
// Test each supported db
|
||||
for (const auto& database : TestDatabases(m_path_root)) {
|
||||
BOOST_ASSERT(database);
|
||||
|
||||
std::vector<std::string> prefixes = {"", "FIRST", "SECOND", "P\xfe\xff", "P\xff\x01", "\xff\xff"};
|
||||
|
||||
// Write elements to it
|
||||
std::unique_ptr<DatabaseBatch> handler = database->MakeBatch();
|
||||
for (unsigned int i = 0; i < 10; i++) {
|
||||
for (const auto& prefix : prefixes) {
|
||||
BOOST_CHECK(handler->Write(std::make_pair(prefix, i), i));
|
||||
}
|
||||
}
|
||||
|
||||
// Now read all the items by prefix and verify that each element gets parsed correctly
|
||||
for (const auto& prefix : prefixes) {
|
||||
DataStream s_prefix;
|
||||
s_prefix << prefix;
|
||||
std::unique_ptr<DatabaseCursor> cursor = handler->GetNewPrefixCursor(s_prefix);
|
||||
DataStream key;
|
||||
DataStream value;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
DatabaseCursor::Status status = cursor->Next(key, value);
|
||||
BOOST_ASSERT(status == DatabaseCursor::Status::MORE);
|
||||
|
||||
std::string key_back;
|
||||
unsigned int i_back;
|
||||
key >> key_back >> i_back;
|
||||
BOOST_CHECK_EQUAL(key_back, prefix);
|
||||
|
||||
unsigned int value_back;
|
||||
value >> value_back;
|
||||
BOOST_CHECK_EQUAL(value_back, i_back);
|
||||
}
|
||||
|
||||
// Let's now read it once more, it should return DONE
|
||||
BOOST_CHECK(cursor->Next(key, value) == DatabaseCursor::Status::DONE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Lower level DatabaseBase::GetNewPrefixCursor test, to cover cases that aren't
|
||||
// covered in the higher level test above. The higher level test uses
|
||||
// serialized strings which are prefixed with string length, so it doesn't test
|
||||
// truly empty prefixes or prefixes that begin with \xff
|
||||
BOOST_AUTO_TEST_CASE(db_cursor_prefix_byte_test)
|
||||
{
|
||||
const MockableData::value_type
|
||||
e{StringData(""), StringData("e")},
|
||||
p{StringData("prefix"), StringData("p")},
|
||||
ps{StringData("prefixsuffix"), StringData("ps")},
|
||||
f{StringData("\xff"), StringData("f")},
|
||||
fs{StringData("\xffsuffix"), StringData("fs")},
|
||||
ff{StringData("\xff\xff"), StringData("ff")},
|
||||
ffs{StringData("\xff\xffsuffix"), StringData("ffs")};
|
||||
for (const auto& database : TestDatabases(m_path_root)) {
|
||||
std::unique_ptr<DatabaseBatch> batch = database->MakeBatch();
|
||||
for (const auto& [k, v] : {e, p, ps, f, fs, ff, ffs}) {
|
||||
batch->Write(MakeUCharSpan(k), MakeUCharSpan(v));
|
||||
}
|
||||
CheckPrefix(*batch, StringBytes(""), {e, p, ps, f, fs, ff, ffs});
|
||||
CheckPrefix(*batch, StringBytes("prefix"), {p, ps});
|
||||
CheckPrefix(*batch, StringBytes("\xff"), {f, fs, ff, ffs});
|
||||
CheckPrefix(*batch, StringBytes("\xff\xff"), {ff, ffs});
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
} // namespace wallet
|
||||
|
||||
@@ -92,6 +92,17 @@ CTxDestination getNewDestination(CWallet& w, OutputType output_type)
|
||||
return *Assert(w.GetNewDestination(output_type, ""));
|
||||
}
|
||||
|
||||
// BytePrefix compares equality with other byte spans that begin with the same prefix.
|
||||
struct BytePrefix { Span<const std::byte> prefix; };
|
||||
bool operator<(BytePrefix a, Span<const std::byte> b) { return a.prefix < b.subspan(0, std::min(a.prefix.size(), b.size())); }
|
||||
bool operator<(Span<const std::byte> a, BytePrefix b) { return a.subspan(0, std::min(a.size(), b.prefix.size())) < b.prefix; }
|
||||
|
||||
MockableCursor::MockableCursor(const MockableData& records, bool pass, Span<const std::byte> prefix)
|
||||
{
|
||||
m_pass = pass;
|
||||
std::tie(m_cursor, m_cursor_end) = records.equal_range(BytePrefix{prefix});
|
||||
}
|
||||
|
||||
DatabaseCursor::Status MockableCursor::Next(DataStream& key, DataStream& value)
|
||||
{
|
||||
if (!m_pass) {
|
||||
@@ -100,6 +111,8 @@ DatabaseCursor::Status MockableCursor::Next(DataStream& key, DataStream& value)
|
||||
if (m_cursor == m_cursor_end) {
|
||||
return Status::DONE;
|
||||
}
|
||||
key.clear();
|
||||
value.clear();
|
||||
const auto& [key_data, value_data] = *m_cursor;
|
||||
key.write(key_data);
|
||||
value.write(value_data);
|
||||
@@ -117,6 +130,7 @@ bool MockableBatch::ReadKey(DataStream&& key, DataStream& value)
|
||||
if (it == m_records.end()) {
|
||||
return false;
|
||||
}
|
||||
value.clear();
|
||||
value.write(it->second);
|
||||
return true;
|
||||
}
|
||||
@@ -172,7 +186,7 @@ bool MockableBatch::ErasePrefix(Span<const std::byte> prefix)
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(std::map<SerializeData, SerializeData> records)
|
||||
std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(MockableData records)
|
||||
{
|
||||
return std::make_unique<MockableDatabase>(records);
|
||||
}
|
||||
|
||||
@@ -48,14 +48,17 @@ std::string getnewaddress(CWallet& w);
|
||||
/** Returns a new destination, of an specific type, from the wallet */
|
||||
CTxDestination getNewDestination(CWallet& w, OutputType output_type);
|
||||
|
||||
using MockableData = std::map<SerializeData, SerializeData, std::less<>>;
|
||||
|
||||
class MockableCursor: public DatabaseCursor
|
||||
{
|
||||
public:
|
||||
std::map<SerializeData, SerializeData>::const_iterator m_cursor;
|
||||
std::map<SerializeData, SerializeData>::const_iterator m_cursor_end;
|
||||
MockableData::const_iterator m_cursor;
|
||||
MockableData::const_iterator m_cursor_end;
|
||||
bool m_pass;
|
||||
|
||||
explicit MockableCursor(const std::map<SerializeData, SerializeData>& records, bool pass) : m_cursor(records.begin()), m_cursor_end(records.end()), m_pass(pass) {}
|
||||
explicit MockableCursor(const MockableData& records, bool pass) : m_cursor(records.begin()), m_cursor_end(records.end()), m_pass(pass) {}
|
||||
MockableCursor(const MockableData& records, bool pass, Span<const std::byte> prefix);
|
||||
~MockableCursor() {}
|
||||
|
||||
Status Next(DataStream& key, DataStream& value) override;
|
||||
@@ -64,7 +67,7 @@ public:
|
||||
class MockableBatch : public DatabaseBatch
|
||||
{
|
||||
private:
|
||||
std::map<SerializeData, SerializeData>& m_records;
|
||||
MockableData& m_records;
|
||||
bool m_pass;
|
||||
|
||||
bool ReadKey(DataStream&& key, DataStream& value) override;
|
||||
@@ -74,7 +77,7 @@ private:
|
||||
bool ErasePrefix(Span<const std::byte> prefix) override;
|
||||
|
||||
public:
|
||||
explicit MockableBatch(std::map<SerializeData, SerializeData>& records, bool pass) : m_records(records), m_pass(pass) {}
|
||||
explicit MockableBatch(MockableData& records, bool pass) : m_records(records), m_pass(pass) {}
|
||||
~MockableBatch() {}
|
||||
|
||||
void Flush() override {}
|
||||
@@ -84,6 +87,9 @@ public:
|
||||
{
|
||||
return std::make_unique<MockableCursor>(m_records, m_pass);
|
||||
}
|
||||
std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(Span<const std::byte> prefix) override {
|
||||
return std::make_unique<MockableCursor>(m_records, m_pass, prefix);
|
||||
}
|
||||
bool TxnBegin() override { return m_pass; }
|
||||
bool TxnCommit() override { return m_pass; }
|
||||
bool TxnAbort() override { return m_pass; }
|
||||
@@ -94,10 +100,10 @@ public:
|
||||
class MockableDatabase : public WalletDatabase
|
||||
{
|
||||
public:
|
||||
std::map<SerializeData, SerializeData> m_records;
|
||||
MockableData m_records;
|
||||
bool m_pass{true};
|
||||
|
||||
MockableDatabase(std::map<SerializeData, SerializeData> records = {}) : WalletDatabase(), m_records(records) {}
|
||||
MockableDatabase(MockableData records = {}) : WalletDatabase(), m_records(records) {}
|
||||
~MockableDatabase() {};
|
||||
|
||||
void Open() override {}
|
||||
@@ -117,7 +123,7 @@ public:
|
||||
std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override { return std::make_unique<MockableBatch>(m_records, m_pass); }
|
||||
};
|
||||
|
||||
std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(std::map<SerializeData, SerializeData> records = {});
|
||||
std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(MockableData records = {});
|
||||
|
||||
MockableDatabase& GetMockableDatabase(CWallet& wallet);
|
||||
} // namespace wallet
|
||||
|
||||
@@ -83,7 +83,7 @@ BOOST_FIXTURE_TEST_CASE(wallet_load_ckey, TestingSetup)
|
||||
{
|
||||
SerializeData ckey_record_key;
|
||||
SerializeData ckey_record_value;
|
||||
std::map<SerializeData, SerializeData> records;
|
||||
MockableData records;
|
||||
|
||||
{
|
||||
// Context setup.
|
||||
|
||||
Reference in New Issue
Block a user