Skip to content

Commit

Permalink
db: fix datastore namespaces
Browse files Browse the repository at this point in the history
This is a preparation before adding DHII into datastore/mdbx.
Everything inside datastore/mdbx should have the same namespace independent of silkworm::db.
The silkworm::db contains DAL while datastore/mdbx contains the datastore implementation.

* move top level datastore items to silkworm::datastore
* move items in datastore/mdbx from silkworm::db to silkworm::sw_mdbx
* move items in datastore/etl to silkworm::etl

Note 1: a name "sw_mdbx" is chosen to avoid a conflict with ::mdbx own namespace.
Note 2: code that uses access_layer.hpp or stage.hpp will use aliases of ROTxn/RWTxn inside silkworm::db namespace.
    This is done to avoid extra churn in code that we don't actively support.
  • Loading branch information
battlmonstr committed Dec 4, 2024
1 parent a66c6e7 commit 7fdc968
Show file tree
Hide file tree
Showing 171 changed files with 660 additions and 554 deletions.
12 changes: 6 additions & 6 deletions cmd/capi/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ std::vector<SilkwormChainSnapshot> collect_all_snapshots(const SnapshotRepositor
return snapshot_sequence;
}

int execute_with_internal_txn(SilkwormHandle handle, ExecuteBlocksSettings settings, db::RWAccess chaindata) {
db::ROTxnManaged ro_txn = chaindata.start_ro_tx();
int execute_with_internal_txn(SilkwormHandle handle, ExecuteBlocksSettings settings, sw_mdbx::RWAccess chaindata) {
sw_mdbx::ROTxnManaged ro_txn = chaindata.start_ro_tx();
const auto chain_config{db::read_chain_config(ro_txn)};
ensure(chain_config.has_value(), "no chain configuration in database");
const auto chain_id{chain_config->chain_id};
Expand Down Expand Up @@ -252,7 +252,7 @@ int execute_with_internal_txn(SilkwormHandle handle, ExecuteBlocksSettings setti
return status_code;
}

int execute_with_external_txn(SilkwormHandle handle, ExecuteBlocksSettings settings, db::RWTxnManaged rw_txn) {
int execute_with_external_txn(SilkwormHandle handle, ExecuteBlocksSettings settings, sw_mdbx::RWTxnManaged rw_txn) {
const auto chain_config{db::read_chain_config(rw_txn)};
ensure(chain_config.has_value(), "no chain configuration in database");
const auto chain_id{chain_config->chain_id};
Expand Down Expand Up @@ -283,7 +283,7 @@ int execute_with_external_txn(SilkwormHandle handle, ExecuteBlocksSettings setti

int execute_blocks(SilkwormHandle handle, ExecuteBlocksSettings settings, const DataDirectory& data_dir) {
// Open chain database
silkworm::db::EnvConfig config{
silkworm::sw_mdbx::EnvConfig config{
.path = data_dir.chaindata().path().string(),
.readonly = false,
.exclusive = true};
Expand Down Expand Up @@ -376,11 +376,11 @@ int start_rpcdaemon(SilkwormHandle handle, const rpc::DaemonSettings& /*settings
});

// Open chain database
silkworm::db::EnvConfig config{
silkworm::sw_mdbx::EnvConfig config{
.path = data_dir.chaindata().path().string(),
.readonly = false,
.exclusive = true};
::mdbx::env_managed env{silkworm::db::open_env(config)};
::mdbx::env_managed env{silkworm::sw_mdbx::open_env(config)};

SilkwormRpcSettings settings{};
const int status_code{silkworm_start_rpcdaemon(handle, &*env, &settings)};
Expand Down
2 changes: 1 addition & 1 deletion cmd/common/db_max_readers_option.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace silkworm::cmd::common {

void add_option_db_max_readers(CLI::App& cli, uint32_t& max_readers) {
cli.add_option("--mdbx.max.readers", max_readers, "The maximum number of MDBX readers")
->default_val(silkworm::db::EnvConfig{}.max_readers)
->default_val(silkworm::sw_mdbx::EnvConfig{}.max_readers)
->check(CLI::Range(1, 32767));
}

Expand Down
14 changes: 7 additions & 7 deletions cmd/dev/backend_kv_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,16 @@ void parse_command_line(int argc, char* argv[], CLI::App& app, StandaloneBackEnd
}

node_settings.data_directory = std::make_unique<DataDirectory>(data_dir, /*create=*/false);
node_settings.chaindata_env_config = db::EnvConfig{node_settings.data_directory->chaindata().path().string(),
/*create=*/false,
/*readonly=*/true};
node_settings.chaindata_env_config = sw_mdbx::EnvConfig{node_settings.data_directory->chaindata().path().string(),
/*create=*/false,
/*readonly=*/true};
node_settings.chaindata_env_config.max_readers = max_readers;
}

std::shared_ptr<silkworm::sentry::api::SentryClient> make_sentry_client(
const NodeSettings& node_settings,
rpc::ClientContextPool& context_pool,
db::ROAccess db_access) {
sw_mdbx::ROAccess db_access) {
std::shared_ptr<silkworm::sentry::api::SentryClient> sentry_client;

auto chain_head_provider = [db_access = std::move(db_access)]() {
Expand Down Expand Up @@ -178,12 +178,12 @@ int main(int argc, char* argv[]) {
<< " address: " << server_settings.address_uri
<< " contexts: " << server_settings.context_pool_settings.num_contexts;

auto chaindata_env = db::open_env(node_settings.chaindata_env_config);
auto chaindata_env = sw_mdbx::open_env(node_settings.chaindata_env_config);
SILK_INFO << "BackEndKvServer MDBX max readers: " << chaindata_env.max_readers();

// Read chain config from database (this allows for custom config)
db::ROAccess chaindata{chaindata_env};
db::ROTxnManaged ro_txn = chaindata.start_ro_tx();
sw_mdbx::ROAccess chaindata{chaindata_env};
sw_mdbx::ROTxnManaged ro_txn = chaindata.start_ro_tx();
node_settings.chain_config = db::read_chain_config(ro_txn);
if (!node_settings.chain_config.has_value()) {
throw std::runtime_error("invalid chain config in database");
Expand Down
10 changes: 5 additions & 5 deletions cmd/dev/check_blockhashes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ int main(int argc, char* argv[]) {
try {
auto data_dir{DataDirectory::from_chaindata(chaindata)};
data_dir.deploy();
db::EnvConfig db_config{data_dir.chaindata().path().string()};
auto env{db::open_env(db_config)};
sw_mdbx::EnvConfig db_config{data_dir.chaindata().path().string()};
auto env{sw_mdbx::open_env(db_config)};
auto txn{env.start_read()};

auto canonical_hashes_table{db::open_cursor(txn, db::table::kCanonicalHashes)};
auto blockhashes_table{db::open_cursor(txn, db::table::kHeaderNumbers)};
auto canonical_hashes_table = sw_mdbx::open_cursor(txn, db::table::kCanonicalHashes);
auto blockhashes_table = sw_mdbx::open_cursor(txn, db::table::kHeaderNumbers);
uint32_t scanned_headers{0};

SILK_INFO << "Checking Block Hashes...";
Expand All @@ -54,7 +54,7 @@ int main(int argc, char* argv[]) {

// Check if each hash has the correct number according to the header table
while (canonical_hashes_data) {
ByteView hash_data_view{db::from_slice(canonical_hashes_data.value)}; // Canonical Hash
ByteView hash_data_view{sw_mdbx::from_slice(canonical_hashes_data.value)}; // Canonical Hash
auto block_hashes_data{blockhashes_table.find(canonical_hashes_data.value, /*throw_notfound*/ false)};
if (!block_hashes_data) {
uint64_t hash_block_num{
Expand Down
4 changes: 2 additions & 2 deletions cmd/dev/check_changes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ int main(int argc, char* argv[]) {

auto data_dir{DataDirectory::from_chaindata(chaindata)};
data_dir.deploy();
db::EnvConfig db_config{data_dir.chaindata().path().string()};
sw_mdbx::EnvConfig db_config{data_dir.chaindata().path().string()};

db::DataStore data_store{
db_config,
data_dir.snapshots().path(),
};

db::RWTxnManaged txn = data_store.chaindata_rw().start_rw_tx();
auto txn = data_store.chaindata_rw().start_rw_tx();
auto chain_config{db::read_chain_config(txn)};
if (!chain_config) {
throw std::runtime_error("Unable to retrieve chain config");
Expand Down
1 change: 1 addition & 0 deletions cmd/dev/check_hashstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

using namespace silkworm;
using namespace silkworm::db;
using namespace silkworm::sw_mdbx;

enum Operation {
kHashAccount,
Expand Down
1 change: 1 addition & 0 deletions cmd/dev/check_log_indices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
using Roaring = roaring::Roaring;
using namespace silkworm;
using namespace silkworm::db;
using namespace silkworm::sw_mdbx;
using namespace silkworm::cmd::common;

enum class TargetIndex {
Expand Down
12 changes: 6 additions & 6 deletions cmd/dev/check_pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ int main(int argc, char* argv[]) {
options.datadir = data_dir.chaindata().path().string();

// Set database parameters
db::EnvConfig db_config{options.datadir};
auto env{db::open_env(db_config)};
db::ROTxnManaged txn{env};
sw_mdbx::EnvConfig db_config{options.datadir};
auto env{sw_mdbx::open_env(db_config)};
sw_mdbx::ROTxnManaged txn{env};

auto config{db::read_chain_config(txn)};
if (!config.has_value()) {
Expand All @@ -99,7 +99,7 @@ int main(int argc, char* argv[]) {
SILK_INFO << "Initializing Light Cache for DAG epoch " << epoch_num;
auto epoch_context{ethash::create_epoch_context(static_cast<int>(epoch_num))};

auto canonical_hashes{db::open_cursor(txn, db::table::kCanonicalHashes)};
auto canonical_hashes = sw_mdbx::open_cursor(txn, db::table::kCanonicalHashes);

// Loop blocks
StopWatch sw;
Expand All @@ -113,12 +113,12 @@ int main(int argc, char* argv[]) {
}

auto block_key{db::block_key(block_num)};
auto data{canonical_hashes.find(db::to_slice(block_key), /*throw_notfound*/ false)};
auto data{canonical_hashes.find(sw_mdbx::to_slice(block_key), /*throw_notfound*/ false)};
if (!data) {
throw std::runtime_error("Can't retrieve canonical hash for block " + std::to_string(block_num));
}

auto header_key{to_bytes32(db::from_slice(data.value))};
auto header_key{to_bytes32(sw_mdbx::from_slice(data.value))};
auto header{db::read_header(txn, block_num, header_key.bytes)};
if (!header.has_value()) {
throw std::runtime_error("Can't retrieve header for block " + std::to_string(block_num));
Expand Down
1 change: 1 addition & 0 deletions cmd/dev/check_senders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

using namespace silkworm;
using namespace silkworm::db;
using namespace silkworm::sw_mdbx;
using namespace silkworm::cmd::common;

int main(int argc, char* argv[]) {
Expand Down
22 changes: 11 additions & 11 deletions cmd/dev/check_tx_lookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ int main(int argc, char* argv[]) {

auto data_dir{DataDirectory::from_chaindata(chaindata)};
data_dir.deploy();
db::EnvConfig db_config{data_dir.chaindata().path().string()};
db::etl::Collector collector(data_dir.temp().path().string().c_str(), /* flush size */ 512 * kMebi);
sw_mdbx::EnvConfig db_config{data_dir.chaindata().path().string()};
etl::Collector collector(data_dir.temp().path().string().c_str(), /* flush size */ 512 * kMebi);

auto env{db::open_env(db_config)};
auto env{sw_mdbx::open_env(db_config)};
auto txn{env.start_read()};

auto bodies_table{db::open_cursor(txn, db::table::kBlockBodies)};
auto tx_lookup_table{db::open_cursor(txn, db::table::kTxLookup)};
auto transactions_table{db::open_cursor(txn, db::table::kBlockTransactions)};
auto bodies_table = sw_mdbx::open_cursor(txn, db::table::kBlockBodies);
auto tx_lookup_table = sw_mdbx::open_cursor(txn, db::table::kTxLookup);
auto transactions_table = sw_mdbx::open_cursor(txn, db::table::kBlockTransactions);

uint64_t expected_block_num{0};

Expand All @@ -64,15 +64,15 @@ int main(int argc, char* argv[]) {
auto bodies_data{bodies_table.to_first(false)};
while (bodies_data) {
auto block_num(endian::load_big_u64(static_cast<uint8_t*>(bodies_data.key.data())));
auto body_rlp{db::from_slice(bodies_data.value)};
auto body_rlp{sw_mdbx::from_slice(bodies_data.value)};
auto body{unwrap_or_throw(decode_stored_block_body(body_rlp))};

if (body.txn_count > 0) {
Bytes transaction_key(8, '\0');
endian::store_big_u64(transaction_key.data(), body.base_txn_id);

uint64_t i{0};
auto transaction_data{transactions_table.find(db::to_slice(transaction_key), false)};
auto transaction_data{transactions_table.find(sw_mdbx::to_slice(transaction_key), false)};
for (; i < body.txn_count && transaction_data.done;
++i, transaction_data = transactions_table.to_next(false)) {
if (!transaction_data) {
Expand All @@ -81,10 +81,10 @@ int main(int argc, char* argv[]) {
continue;
}

ByteView transaction_rlp{db::from_slice(transaction_data.value)};
ByteView transaction_rlp{sw_mdbx::from_slice(transaction_data.value)};
auto transaction_hash{keccak256(transaction_rlp)};
ByteView transaction_view{transaction_hash.bytes};
auto lookup_data{tx_lookup_table.find(db::to_slice(transaction_view), false)};
auto lookup_data{tx_lookup_table.find(sw_mdbx::to_slice(transaction_view), false)};
if (!lookup_data) {
SILK_ERROR << "Block " << block_num << " transaction " << i << " with hash "
<< to_hex(transaction_view) << " not found in " << db::table::kTxLookup.name
Expand All @@ -93,7 +93,7 @@ int main(int argc, char* argv[]) {
}

// Erigon stores block_num as compact (no leading zeroes)
auto lookup_block_value{db::from_slice(lookup_data.value)};
auto lookup_block_value{sw_mdbx::from_slice(lookup_data.value)};
uint64_t actual_block_num{0};
if (!endian::from_big_compact(lookup_block_value, actual_block_num)) {
SILK_ERROR << "Failed to read expected block number from: " << to_hex(lookup_block_value);
Expand Down
3 changes: 2 additions & 1 deletion cmd/dev/db_toolbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
namespace fs = std::filesystem;
using namespace silkworm;
using namespace silkworm::db;
using namespace silkworm::sw_mdbx;

class Progress {
public:
Expand Down Expand Up @@ -1426,7 +1427,7 @@ void do_extract_headers(EnvConfig& config, const std::string& file_name, uint32_
void do_freeze(EnvConfig& config, const DataDirectory& data_dir, bool keep_blocks) {
using namespace concurrency::awaitable_wait_for_one;

class StageSchedulerAdapter : public stagedsync::StageScheduler, public ActiveComponent {
class StageSchedulerAdapter : public datastore::StageScheduler, public ActiveComponent {
public:
explicit StageSchedulerAdapter(RWAccess db_access)
: db_access_(std::move(db_access)) {}
Expand Down
6 changes: 3 additions & 3 deletions cmd/dev/scan_txs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ int main(int argc, char* argv[]) {
try {
auto data_dir{DataDirectory::from_chaindata(chaindata)};
data_dir.deploy();
db::EnvConfig db_config{data_dir.chaindata().path().string()};
auto env{db::open_env(db_config)};
db::RWTxnManaged txn{env};
sw_mdbx::EnvConfig db_config{data_dir.chaindata().path().string()};
auto env{sw_mdbx::open_env(db_config)};
sw_mdbx::RWTxnManaged txn{env};
auto chain_config{db::read_chain_config(txn)};
if (!chain_config) {
throw std::runtime_error("Unable to retrieve chain config");
Expand Down
6 changes: 3 additions & 3 deletions cmd/dev/snapshots.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,13 +1001,13 @@ void lookup_transaction(const SnapshotSubcommandSettings& settings) {
void merge(const SnapshotSettings& settings) {
auto repository = make_repository(settings);
TemporaryDirectory tmp_dir;
db::SnapshotMerger merger{repository, tmp_dir.path()};
datastore::SnapshotMerger merger{repository, tmp_dir.path()};
test_util::TaskRunner runner;
runner.run(merger.exec());
}

void sync(const SnapshotSettings& settings) {
class NoopStageSchedulerAdapter : public stagedsync::StageScheduler {
class NoopStageSchedulerAdapter : public datastore::StageScheduler {
public:
explicit NoopStageSchedulerAdapter() = default;
~NoopStageSchedulerAdapter() override = default;
Expand All @@ -1019,7 +1019,7 @@ void sync(const SnapshotSettings& settings) {
std::chrono::time_point start{std::chrono::steady_clock::now()};

TemporaryDirectory tmp_dir;
db::EnvConfig chaindata_env_config{tmp_dir.path()};
sw_mdbx::EnvConfig chaindata_env_config{tmp_dir.path()};

db::DataStore data_store{
chaindata_env_config,
Expand Down
Loading

0 comments on commit 7fdc968

Please sign in to comment.