Skip to content

Commit dec1258

Browse files
committed
db
1 parent 8bbbbd6 commit dec1258

File tree

11 files changed

+294
-209
lines changed

11 files changed

+294
-209
lines changed

silkworm/capi/common/instance.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
#include "common_component.hpp"
99

10+
namespace silkworm::db::capi {
11+
struct Component;
12+
}
13+
1014
namespace silkworm::rpc {
1115
class Daemon;
1216
}
@@ -19,6 +23,7 @@ namespace capi_todo {
1923

2024
struct SilkwormInstance {
2125
silkworm::capi::CommonComponent common;
26+
std::unique_ptr<silkworm::db::capi::Component> db;
2227
std::unique_ptr<silkworm::rpc::Daemon> rpcdaemon;
2328
std::unique_ptr<silkworm::sentry::capi::Component> sentry;
2429

silkworm/capi/instance.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "common/instance.hpp"
55

6+
#include <silkworm/db/capi/component.hpp>
67
#include <silkworm/rpc/daemon.hpp>
78
#include <silkworm/sentry/capi/component.hpp>
89

silkworm/capi/instance.hpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,7 @@
1010

1111
#include "common/instance.hpp"
1212

13-
namespace silkworm::snapshots {
14-
class SnapshotRepository;
15-
} // namespace silkworm::snapshots
16-
17-
namespace silkworm::datastore::kvdb {
18-
class DatabaseUnmanaged;
19-
} // namespace silkworm::datastore::kvdb
20-
2113
struct SilkwormInstance : public capi_todo::SilkwormInstance {
22-
std::unique_ptr<silkworm::datastore::kvdb::DatabaseUnmanaged> chaindata;
23-
std::unique_ptr<silkworm::snapshots::SnapshotRepository> blocks_repository;
24-
std::unique_ptr<silkworm::snapshots::SnapshotRepository> state_repository_latest;
25-
std::unique_ptr<silkworm::snapshots::SnapshotRepository> state_repository_historical;
26-
2714
std::optional<silkworm::ChainConfig> chain_config;
2815

2916
// TODO: This has to be changed and encapsulated by a proper block caching state

silkworm/capi/silkworm.cpp

Lines changed: 28 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <utility>
1313
#include <vector>
1414

15-
#include <absl/strings/str_split.h>
1615
#include <boost/thread/scoped_thread.hpp>
1716
#include <gsl/util>
1817
#include <nlohmann/json.hpp>
@@ -26,6 +25,8 @@
2625
#include <silkworm/db/blocks/schema_config.hpp>
2726
#include <silkworm/db/blocks/transactions/txn_to_block_index.hpp>
2827
#include <silkworm/db/buffer.hpp>
28+
#include <silkworm/db/capi/component.hpp>
29+
#include <silkworm/db/datastore/kvdb/mdbx_version.hpp>
2930
#include <silkworm/db/datastore/snapshots/index_builder.hpp>
3031
#include <silkworm/db/datastore/snapshots/segment/segment_reader.hpp>
3132
#include <silkworm/db/stages.hpp>
@@ -67,36 +68,6 @@ struct ExecutionProgress {
6768
float batch_progress_perc{0.0};
6869
};
6970

70-
//! Kind of match to perform between Erigon and Silkworm libmdbx versions
71-
enum class MdbxVersionCheck : uint8_t {
72-
kNone, /// no check at all
73-
kExact, /// git-describe versions must match perfectly
74-
kSemantic, /// compare semantic versions (<M1.m1.p1> == <M2.m2.p2>)
75-
};
76-
77-
static bool is_compatible_mdbx_version(std::string_view their_version, std::string_view our_version, MdbxVersionCheck check) {
78-
SILK_TRACE << "is_compatible_mdbx_version their_version: " << their_version << " our_version: " << our_version;
79-
bool compatible{false};
80-
switch (check) {
81-
case MdbxVersionCheck::kNone: {
82-
compatible = true;
83-
} break;
84-
case MdbxVersionCheck::kExact: {
85-
compatible = their_version == our_version;
86-
} break;
87-
case MdbxVersionCheck::kSemantic: {
88-
const std::vector<std::string> their_version_parts = absl::StrSplit(std::string(their_version), '.');
89-
const std::vector<std::string> our_version_parts = absl::StrSplit(std::string(our_version), '.');
90-
compatible = (their_version_parts.size() >= 3) &&
91-
(our_version_parts.size() >= 3) &&
92-
(their_version_parts[0] == our_version_parts[0]) &&
93-
(their_version_parts[1] == our_version_parts[1]) &&
94-
(their_version_parts[2] == our_version_parts[2]);
95-
}
96-
}
97-
return compatible;
98-
}
99-
10071
//! Generate log arguments for Silkworm library version
10172
static log::Args log_args_for_version() {
10273
const auto build_info{silkworm_get_buildinfo()};
@@ -191,6 +162,8 @@ class SignalHandlerGuard {
191162
static bool is_initialized{false};
192163

193164
SILKWORM_EXPORT int silkworm_init(SilkwormHandle* handle, const struct SilkwormSettings* settings) SILKWORM_NOEXCEPT {
165+
using namespace datastore::kvdb;
166+
194167
if (!handle) {
195168
return SILKWORM_INVALID_HANDLE;
196169
}
@@ -235,14 +208,17 @@ SILKWORM_EXPORT int silkworm_init(SilkwormHandle* handle, const struct SilkwormS
235208
snapshots_dir_path,
236209
/* open = */ false,
237210
settings->state_repo_index_salt);
211+
db::capi::Component db{
212+
.blocks_repository = std::move(blocks_repository),
213+
.state_repository_latest = std::move(state_repository_latest),
214+
.state_repository_historical = std::move(state_repository_historical),
215+
.chaindata = {},
216+
};
238217

239218
// NOLINTNEXTLINE(bugprone-unhandled-exception-at-new)
240-
*handle = new ::SilkwormInstance{
241-
.blocks_repository = std::make_unique<snapshots::SnapshotRepository>(std::move(blocks_repository)),
242-
.state_repository_latest = std::make_unique<snapshots::SnapshotRepository>(std::move(state_repository_latest)),
243-
.state_repository_historical = std::make_unique<snapshots::SnapshotRepository>(std::move(state_repository_historical)),
244-
};
219+
*handle = new ::SilkwormInstance{};
245220
(*handle)->common = std::move(common);
221+
(*handle)->db = std::make_unique<db::capi::Component>(std::move(db));
246222

247223
log::Info{"Silkworm build info", log_args_for_version()}; // NOLINT(*-unused-raii)
248224

@@ -255,10 +231,6 @@ SILKWORM_EXPORT int silkworm_init(SilkwormHandle* handle, const struct SilkwormS
255231
return SILKWORM_OK;
256232
}
257233

258-
SILKWORM_EXPORT const char* silkworm_libmdbx_version() SILKWORM_NOEXCEPT {
259-
return ::mdbx::get_version().git.describe;
260-
}
261-
262234
class BlockProvider {
263235
static constexpr size_t kTxnRefreshThreshold{100};
264236

@@ -348,8 +320,9 @@ int silkworm_execute_blocks_ephemeral(SilkwormHandle handle, MDBX_txn* mdbx_txn,
348320

349321
try {
350322
auto txn = datastore::kvdb::RWTxnUnmanaged{mdbx_txn};
323+
db::DataModel da_layer{txn, handle->db->blocks_repository};
351324

352-
db::Buffer state_buffer{txn, std::make_unique<db::BufferFullDataModel>(db::DataModel{txn, *handle->blocks_repository})};
325+
db::Buffer state_buffer{txn, std::make_unique<db::BufferFullDataModel>(da_layer)};
353326
state_buffer.set_memory_limit(batch_size);
354327

355328
const size_t max_batch_size{batch_size};
@@ -358,7 +331,6 @@ int silkworm_execute_blocks_ephemeral(SilkwormHandle handle, MDBX_txn* mdbx_txn,
358331
BlockNum block_num{start_block};
359332
BlockNum batch_start_block_num{start_block};
360333
BlockNum last_block_num = 0;
361-
db::DataModel da_layer{txn, *handle->blocks_repository};
362334

363335
AnalysisCache analysis_cache{execution::block::BlockExecutor::kDefaultAnalysisCacheSize};
364336
execution::block::BlockExecutor block_executor{*chain_info, write_receipts, write_call_traces, write_change_sets};
@@ -464,29 +436,22 @@ int silkworm_execute_blocks_perpetual(SilkwormHandle handle, MDBX_env* mdbx_env,
464436

465437
try {
466438
// Wrap MDBX env into an internal *unmanaged* env, i.e. MDBX env is only used but its lifecycle is untouched
467-
datastore::kvdb::EnvUnmanaged unmanaged_env{mdbx_env};
468-
const auto env_path = unmanaged_env.get_path();
469-
handle->chaindata = std::make_unique<datastore::kvdb::DatabaseUnmanaged>(
470-
db::DataStore::make_chaindata_database(std::move(unmanaged_env)));
471-
auto& chaindata = *handle->chaindata;
439+
if (!handle->db->chaindata) {
440+
handle->db->chaindata = std::make_unique<datastore::kvdb::DatabaseUnmanaged>(
441+
db::DataStore::make_chaindata_database(datastore::kvdb::EnvUnmanaged{mdbx_env}));
442+
}
443+
auto& chaindata = *handle->db->chaindata;
444+
db::DataModelFactory data_model_factory = handle->db->data_model_factory();
472445

473446
datastore::kvdb::RWAccess rw_access = chaindata.access_rw();
474447
auto txn = rw_access.start_rw_tx();
475448

476-
db::Buffer state_buffer{txn, std::make_unique<db::BufferFullDataModel>(db::DataModel{txn, *handle->blocks_repository})};
449+
db::Buffer state_buffer{txn, std::make_unique<db::BufferFullDataModel>(data_model_factory(txn))};
477450
state_buffer.set_memory_limit(batch_size);
478451

479452
BoundedBuffer<std::optional<Block>> block_buffer{kMaxBlockBufferSize};
480453
[[maybe_unused]] auto _ = gsl::finally([&block_buffer] { block_buffer.terminate_and_release_all(); });
481454

482-
db::DataStoreRef data_store{
483-
chaindata.ref(),
484-
*handle->blocks_repository,
485-
*handle->state_repository_latest,
486-
*handle->state_repository_historical,
487-
};
488-
db::DataModelFactory data_model_factory{std::move(data_store)};
489-
490455
BlockProvider block_provider{
491456
&block_buffer,
492457
chaindata.access_ro(),
@@ -556,7 +521,7 @@ int silkworm_execute_blocks_perpetual(SilkwormHandle handle, MDBX_env* mdbx_env,
556521
txn.commit_and_renew();
557522
const auto elapsed_time_and_duration = sw.stop();
558523
log::Info("[4/12 Execution] Commit state+history", // NOLINT(*-unused-raii)
559-
log_args_for_exec_commit(elapsed_time_and_duration.second, env_path));
524+
log_args_for_exec_commit(elapsed_time_and_duration.second, (*rw_access).get_path()));
560525

561526
if (last_executed_block) {
562527
*last_executed_block = last_block_num;
@@ -600,13 +565,8 @@ SILKWORM_EXPORT int silkworm_execute_txn(SilkwormHandle handle, MDBX_txn* mdbx_t
600565
*blob_gas_used = 0;
601566
}
602567

603-
if (!handle->blocks_repository) {
604-
SILK_ERROR << "Blocks repository not found";
605-
return SILKWORM_INVALID_HANDLE;
606-
}
607-
608-
if (!handle->state_repository_latest) {
609-
SILK_ERROR << "State repository latest not found";
568+
if (!handle->db) {
569+
SILK_ERROR << "Database component not initialized";
610570
return SILKWORM_INVALID_HANDLE;
611571
}
612572

@@ -625,8 +585,8 @@ SILKWORM_EXPORT int silkworm_execute_txn(SilkwormHandle handle, MDBX_txn* mdbx_t
625585
txn_id,
626586
unmanaged_tx,
627587
db_ref,
628-
*handle->blocks_repository,
629-
*handle->state_repository_latest,
588+
handle->db->blocks_repository,
589+
handle->db->state_repository_latest,
630590
};
631591
if (!handle->chain_config) {
632592
handle->chain_config = db::read_chain_config(unmanaged_tx);
@@ -771,8 +731,8 @@ SILKWORM_EXPORT int silkworm_block_exec_end(SilkwormHandle handle, MDBX_txn* mdb
771731
txn_id,
772732
unmanaged_tx,
773733
db_ref,
774-
*handle->blocks_repository,
775-
*handle->state_repository_latest,
734+
handle->db->blocks_repository,
735+
handle->db->state_repository_latest,
776736
};
777737

778738
const auto log_index = handle->executions_in_block[index].log_index;
@@ -787,9 +747,6 @@ SILKWORM_EXPORT int silkworm_fini(SilkwormHandle handle) SILKWORM_NOEXCEPT {
787747
if (!handle) {
788748
return SILKWORM_INVALID_HANDLE;
789749
}
790-
if (!handle->blocks_repository) {
791-
return SILKWORM_INVALID_HANDLE;
792-
}
793750
delete handle;
794751

795752
is_initialized = false;

silkworm/capi/silkworm.h

Lines changed: 4 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
#ifdef SILKWORM_CAPI_COMPONENT
1010
#include "common/preamble.h"
11+
#include <silkworm/db/capi/db.h>
1112
#include <silkworm/rpc/capi/rpcdaemon.h>
1213
#include <silkworm/sentry/capi/sentry.h>
1314
#else
1415
#include "preamble.h"
16+
#include "db.h"
1517
#include "rpcdaemon.h"
1618
#include "sentry.h"
1719
#endif
@@ -20,76 +22,6 @@
2022
extern "C" {
2123
#endif
2224

23-
typedef struct MDBX_env MDBX_env;
24-
typedef struct MDBX_txn MDBX_txn;
25-
26-
struct SilkwormMemoryMappedFile {
27-
const char* file_path;
28-
uint8_t* memory_address;
29-
uint64_t memory_length;
30-
};
31-
32-
struct SilkwormHeadersSnapshot {
33-
struct SilkwormMemoryMappedFile segment;
34-
struct SilkwormMemoryMappedFile header_hash_index;
35-
};
36-
37-
struct SilkwormBodiesSnapshot {
38-
struct SilkwormMemoryMappedFile segment;
39-
struct SilkwormMemoryMappedFile block_num_index;
40-
};
41-
42-
struct SilkwormTransactionsSnapshot {
43-
struct SilkwormMemoryMappedFile segment;
44-
struct SilkwormMemoryMappedFile tx_hash_index;
45-
struct SilkwormMemoryMappedFile tx_hash_2_block_index;
46-
};
47-
48-
struct SilkwormBlocksSnapshotBundle {
49-
struct SilkwormHeadersSnapshot headers;
50-
struct SilkwormBodiesSnapshot bodies;
51-
struct SilkwormTransactionsSnapshot transactions;
52-
};
53-
54-
struct SilkwormInvertedIndexSnapshot {
55-
struct SilkwormMemoryMappedFile segment; // .ef
56-
struct SilkwormMemoryMappedFile accessor_index; // .efi
57-
};
58-
59-
struct SilkwormHistorySnapshot {
60-
struct SilkwormMemoryMappedFile segment; // .v
61-
struct SilkwormMemoryMappedFile accessor_index; // .vi
62-
struct SilkwormInvertedIndexSnapshot inverted_index;
63-
};
64-
65-
struct SilkwormDomainSnapshot {
66-
struct SilkwormMemoryMappedFile segment; // .kv
67-
struct SilkwormMemoryMappedFile existence_index; // .kvei
68-
struct SilkwormMemoryMappedFile btree_index; // .bt
69-
bool has_accessor_index;
70-
struct SilkwormMemoryMappedFile accessor_index; // .kvi
71-
};
72-
73-
struct SilkwormStateSnapshotBundleLatest {
74-
struct SilkwormDomainSnapshot accounts;
75-
struct SilkwormDomainSnapshot storage;
76-
struct SilkwormDomainSnapshot code;
77-
struct SilkwormDomainSnapshot commitment;
78-
struct SilkwormDomainSnapshot receipts;
79-
};
80-
81-
struct SilkwormStateSnapshotBundleHistorical {
82-
struct SilkwormHistorySnapshot accounts;
83-
struct SilkwormHistorySnapshot storage;
84-
struct SilkwormHistorySnapshot code;
85-
struct SilkwormHistorySnapshot receipts;
86-
87-
struct SilkwormInvertedIndexSnapshot log_addresses;
88-
struct SilkwormInvertedIndexSnapshot log_topics;
89-
struct SilkwormInvertedIndexSnapshot traces_from;
90-
struct SilkwormInvertedIndexSnapshot traces_to;
91-
};
92-
9325
#define SILKWORM_GIT_VERSION_SIZE 32
9426

9527
//! Silkworm library logging level
@@ -128,44 +60,8 @@ struct SilkwormSettings {
12860
*/
12961
SILKWORM_EXPORT int silkworm_init(SilkwormHandle* handle, const struct SilkwormSettings* settings) SILKWORM_NOEXCEPT;
13062

131-
/**
132-
* \brief Build a set of indexes for the given snapshots.
133-
* \param[in] handle A valid Silkworm instance handle, got with silkworm_init.
134-
* \param[in] segments An array of segment files to index.
135-
* \param[in] len The number of segment files.
136-
* \return SILKWORM_OK (=0) on success, a non-zero error value on failure on some or all indexes.
137-
*/
138-
SILKWORM_EXPORT int silkworm_build_recsplit_indexes(SilkwormHandle handle, struct SilkwormMemoryMappedFile* segments[], size_t len) SILKWORM_NOEXCEPT;
139-
140-
/**
141-
* \brief Notify Silkworm about a new *block* snapshot bundle to use.
142-
* \param[in] handle A valid Silkworm instance handle, got with silkworm_init.
143-
* \param[in] bundle A *block* snapshot bundle to use.
144-
* \return SILKWORM_OK (=0) on success, a non-zero error value on failure.
145-
*/
146-
SILKWORM_EXPORT int silkworm_add_blocks_snapshot_bundle(SilkwormHandle handle, const struct SilkwormBlocksSnapshotBundle* bundle) SILKWORM_NOEXCEPT;
147-
148-
/**
149-
* \brief Notify Silkworm about a new *latest state* snapshot bundle to use.
150-
* \param[in] handle A valid Silkworm instance handle, got with silkworm_init.
151-
* \param[in] bundle A *latest state* snapshot bundle to use.
152-
* \return SILKWORM_OK (=0) on success, a non-zero error value on failure.
153-
*/
154-
SILKWORM_EXPORT int silkworm_add_state_snapshot_bundle_latest(SilkwormHandle handle, const struct SilkwormStateSnapshotBundleLatest* bundle) SILKWORM_NOEXCEPT;
155-
156-
/**
157-
* \brief Notify Silkworm about a new *historical state* snapshot bundle to use.
158-
* \param[in] handle A valid Silkworm instance handle, got with silkworm_init.
159-
* \param[in] bundle A *historical state* snapshot bundle to use.
160-
* \return SILKWORM_OK (=0) on success, a non-zero error value on failure.
161-
*/
162-
SILKWORM_EXPORT int silkworm_add_state_snapshot_bundle_historical(SilkwormHandle handle, const struct SilkwormStateSnapshotBundleHistorical* bundle) SILKWORM_NOEXCEPT;
163-
164-
/**
165-
* \brief Get libmdbx version for compatibility checks.
166-
* \return A string in git describe format.
167-
*/
168-
SILKWORM_EXPORT const char* silkworm_libmdbx_version(void) SILKWORM_NOEXCEPT;
63+
typedef struct MDBX_env MDBX_env;
64+
typedef struct MDBX_txn MDBX_txn;
16965

17066
struct SilkwormBytes32 {
17167
uint8_t bytes[32];

0 commit comments

Comments
 (0)