Skip to content

Commit 4bb83c9

Browse files
committed
init
1 parent 74b321b commit 4bb83c9

File tree

4 files changed

+169
-132
lines changed

4 files changed

+169
-132
lines changed

silkworm/capi/init.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright 2025 The Silkworm Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#include "init.h"
5+
6+
#include <silkworm/buildinfo.h>
7+
#include <silkworm/db/capi/component.hpp>
8+
#include <silkworm/db/capi/db.h>
9+
#include <silkworm/db/datastore/kvdb/mdbx_version.hpp>
10+
#include <silkworm/infra/common/log.hpp>
11+
12+
#include "common/instance.hpp"
13+
#include "common/parse_path.hpp"
14+
#include "instance.hpp"
15+
#include "make_log_settings.hpp"
16+
17+
using namespace silkworm;
18+
using namespace silkworm::capi;
19+
20+
static bool is_initialized{false};
21+
22+
//! Generate log arguments for Silkworm library version
23+
static log::Args log_args_for_version() {
24+
const auto build_info{silkworm_get_buildinfo()};
25+
return {
26+
"git_branch",
27+
std::string(build_info->git_branch),
28+
"git_tag",
29+
std::string(build_info->project_version),
30+
"git_commit",
31+
std::string(build_info->git_commit_hash)};
32+
}
33+
34+
SILKWORM_EXPORT int silkworm_init(SilkwormHandle* handle, const struct SilkwormSettings* settings) SILKWORM_NOEXCEPT {
35+
using namespace datastore::kvdb;
36+
37+
if (!handle) {
38+
return SILKWORM_INVALID_HANDLE;
39+
}
40+
if (!settings) {
41+
return SILKWORM_INVALID_SETTINGS;
42+
}
43+
if (std::strlen(settings->data_dir_path) == 0) {
44+
return SILKWORM_INVALID_PATH;
45+
}
46+
if (!is_compatible_mdbx_version(settings->libmdbx_version, silkworm_libmdbx_version(), MdbxVersionCheck::kExact)) {
47+
return SILKWORM_INCOMPATIBLE_LIBMDBX;
48+
}
49+
if (is_initialized) {
50+
return SILKWORM_TOO_MANY_INSTANCES;
51+
}
52+
53+
is_initialized = true;
54+
55+
log::Settings log_settings{make_log_settings(settings->log_verbosity)};
56+
log::init(log_settings);
57+
58+
auto data_dir_path = parse_path(settings->data_dir_path);
59+
60+
silkworm::capi::CommonComponent common{
61+
.log_settings = std::move(log_settings),
62+
.context_pool_settings = {
63+
.num_contexts = settings->num_contexts > 0 ? settings->num_contexts : silkworm::concurrency::kDefaultNumContexts,
64+
},
65+
.data_dir_path = data_dir_path,
66+
};
67+
68+
auto snapshots_dir_path = DataDirectory{data_dir_path}.snapshots().path();
69+
auto blocks_repository = db::blocks::make_blocks_repository(
70+
snapshots_dir_path,
71+
/* open = */ false,
72+
settings->blocks_repo_index_salt);
73+
auto state_repository_latest = db::state::make_state_repository_latest(
74+
snapshots_dir_path,
75+
/* open = */ false,
76+
settings->state_repo_index_salt);
77+
auto state_repository_historical = db::state::make_state_repository_historical(
78+
snapshots_dir_path,
79+
/* open = */ false,
80+
settings->state_repo_index_salt);
81+
db::capi::Component db{
82+
.blocks_repository = std::move(blocks_repository),
83+
.state_repository_latest = std::move(state_repository_latest),
84+
.state_repository_historical = std::move(state_repository_historical),
85+
.chaindata = {},
86+
};
87+
88+
// NOLINTNEXTLINE(bugprone-unhandled-exception-at-new)
89+
*handle = new ::SilkwormInstance{};
90+
(*handle)->common = std::move(common);
91+
(*handle)->db = std::make_unique<db::capi::Component>(std::move(db));
92+
93+
log::Info{"Silkworm build info", log_args_for_version()}; // NOLINT(*-unused-raii)
94+
95+
log::Debug{"[1/12] Silkworm initialized", // NOLINT(*-unused-raii)
96+
{"data_dir", data_dir_path.string(),
97+
"snapshots_dir", snapshots_dir_path.string(),
98+
"blocks_repo_index_salt", std::to_string(settings->blocks_repo_index_salt),
99+
"state_repo_index_salt", std::to_string(settings->state_repo_index_salt)}};
100+
101+
return SILKWORM_OK;
102+
}
103+
104+
SILKWORM_EXPORT int silkworm_fini(SilkwormHandle handle) SILKWORM_NOEXCEPT {
105+
if (!handle) {
106+
return SILKWORM_INVALID_HANDLE;
107+
}
108+
delete handle;
109+
110+
is_initialized = false;
111+
112+
return SILKWORM_OK;
113+
}

silkworm/capi/init.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2025 The Silkworm Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#ifndef SILKWORM_CAPI_INIT_H_
5+
#define SILKWORM_CAPI_INIT_H_
6+
7+
#ifdef SILKWORM_CAPI_COMPONENT
8+
#include "common/preamble.h"
9+
#else
10+
#include "preamble.h"
11+
#endif
12+
#include "log_level.h"
13+
14+
#if __cplusplus
15+
extern "C" {
16+
#endif
17+
18+
#define SILKWORM_GIT_VERSION_SIZE 32
19+
20+
//! Silkworm library general configuration options
21+
struct SilkwormSettings {
22+
//! Log verbosity level
23+
SilkwormLogLevel log_verbosity;
24+
//! Number of I/O contexts to use in concurrency mode
25+
uint32_t num_contexts;
26+
//! Data directory path in UTF-8.
27+
char data_dir_path[SILKWORM_PATH_SIZE];
28+
//! libmdbx version string in git describe format.
29+
char libmdbx_version[SILKWORM_GIT_VERSION_SIZE];
30+
//! Index salt for block snapshots
31+
uint32_t blocks_repo_index_salt;
32+
//! Index salt for state snapshots
33+
uint32_t state_repo_index_salt;
34+
};
35+
36+
/**
37+
* \brief Initialize the Silkworm C API library.
38+
* \param[in,out] handle Silkworm instance handle returned on successful initialization.
39+
* \param[in] settings General Silkworm settings.
40+
* \return SILKWORM_OK (=0) on success, a non-zero error value on failure.
41+
*/
42+
SILKWORM_EXPORT int silkworm_init(SilkwormHandle* handle, const struct SilkwormSettings* settings) SILKWORM_NOEXCEPT;
43+
44+
/**
45+
* \brief Finalize the Silkworm C API library.
46+
* \param[in] handle A valid Silkworm instance handle got with silkworm_init.
47+
* \return SILKWORM_OK (=0) on success, a non-zero error value on failure.
48+
*/
49+
SILKWORM_EXPORT int silkworm_fini(SilkwormHandle handle) SILKWORM_NOEXCEPT;
50+
51+
#if __cplusplus
52+
}
53+
#endif
54+
55+
#endif // SILKWORM_CAPI_INIT_H_

silkworm/capi/silkworm.cpp

Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <gsl/util>
1717
#include <nlohmann/json.hpp>
1818

19-
#include <silkworm/buildinfo.h>
2019
#include <silkworm/core/chain/config.hpp>
2120
#include <silkworm/core/execution/call_tracer.hpp>
2221
#include <silkworm/core/execution/execution.hpp>
@@ -26,7 +25,6 @@
2625
#include <silkworm/db/blocks/transactions/txn_to_block_index.hpp>
2726
#include <silkworm/db/buffer.hpp>
2827
#include <silkworm/db/capi/component.hpp>
29-
#include <silkworm/db/datastore/kvdb/mdbx_version.hpp>
3028
#include <silkworm/db/datastore/snapshots/index_builder.hpp>
3129
#include <silkworm/db/datastore/snapshots/segment/segment_reader.hpp>
3230
#include <silkworm/db/stages.hpp>
@@ -46,7 +44,6 @@
4644

4745
#include "common/parse_path.hpp"
4846
#include "instance.hpp"
49-
#include "make_log_settings.hpp"
5047

5148
using namespace std::chrono_literals;
5249
using namespace silkworm;
@@ -68,18 +65,6 @@ struct ExecutionProgress {
6865
float batch_progress_perc{0.0};
6966
};
7067

71-
//! Generate log arguments for Silkworm library version
72-
static log::Args log_args_for_version() {
73-
const auto build_info{silkworm_get_buildinfo()};
74-
return {
75-
"git_branch",
76-
std::string(build_info->git_branch),
77-
"git_tag",
78-
std::string(build_info->project_version),
79-
"git_commit",
80-
std::string(build_info->git_commit_hash)};
81-
}
82-
8368
//! Generate log arguments for execution flush at specified block
8469
static log::Args log_args_for_exec_flush(const db::Buffer& state_buffer, uint64_t max_batch_size, uint64_t current_block) {
8570
return {
@@ -159,78 +144,6 @@ class SignalHandlerGuard {
159144
~SignalHandlerGuard() { SignalHandler::reset(); }
160145
};
161146

162-
static bool is_initialized{false};
163-
164-
SILKWORM_EXPORT int silkworm_init(SilkwormHandle* handle, const struct SilkwormSettings* settings) SILKWORM_NOEXCEPT {
165-
using namespace datastore::kvdb;
166-
167-
if (!handle) {
168-
return SILKWORM_INVALID_HANDLE;
169-
}
170-
if (!settings) {
171-
return SILKWORM_INVALID_SETTINGS;
172-
}
173-
if (std::strlen(settings->data_dir_path) == 0) {
174-
return SILKWORM_INVALID_PATH;
175-
}
176-
if (!is_compatible_mdbx_version(settings->libmdbx_version, silkworm_libmdbx_version(), MdbxVersionCheck::kExact)) {
177-
return SILKWORM_INCOMPATIBLE_LIBMDBX;
178-
}
179-
if (is_initialized) {
180-
return SILKWORM_TOO_MANY_INSTANCES;
181-
}
182-
183-
is_initialized = true;
184-
185-
log::Settings log_settings{make_log_settings(settings->log_verbosity)};
186-
log::init(log_settings);
187-
188-
auto data_dir_path = parse_path(settings->data_dir_path);
189-
190-
silkworm::capi::CommonComponent common{
191-
.log_settings = std::move(log_settings),
192-
.context_pool_settings = {
193-
.num_contexts = settings->num_contexts > 0 ? settings->num_contexts : silkworm::concurrency::kDefaultNumContexts,
194-
},
195-
.data_dir_path = data_dir_path,
196-
};
197-
198-
auto snapshots_dir_path = DataDirectory{data_dir_path}.snapshots().path();
199-
auto blocks_repository = db::blocks::make_blocks_repository(
200-
snapshots_dir_path,
201-
/* open = */ false,
202-
settings->blocks_repo_index_salt);
203-
auto state_repository_latest = db::state::make_state_repository_latest(
204-
snapshots_dir_path,
205-
/* open = */ false,
206-
settings->state_repo_index_salt);
207-
auto state_repository_historical = db::state::make_state_repository_historical(
208-
snapshots_dir_path,
209-
/* open = */ false,
210-
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-
};
217-
218-
// NOLINTNEXTLINE(bugprone-unhandled-exception-at-new)
219-
*handle = new ::SilkwormInstance{};
220-
(*handle)->common = std::move(common);
221-
(*handle)->db = std::make_unique<db::capi::Component>(std::move(db));
222-
223-
log::Info{"Silkworm build info", log_args_for_version()}; // NOLINT(*-unused-raii)
224-
225-
log::Debug{"[1/12] Silkworm initialized", // NOLINT(*-unused-raii)
226-
{"data_dir", data_dir_path.string(),
227-
"snapshots_dir", snapshots_dir_path.string(),
228-
"blocks_repo_index_salt", std::to_string(settings->blocks_repo_index_salt),
229-
"state_repo_index_salt", std::to_string(settings->state_repo_index_salt)}};
230-
231-
return SILKWORM_OK;
232-
}
233-
234147
class BlockProvider {
235148
static constexpr size_t kTxnRefreshThreshold{100};
236149

@@ -742,14 +655,3 @@ SILKWORM_EXPORT int silkworm_block_exec_end(SilkwormHandle handle, MDBX_txn* mdb
742655

743656
return SILKWORM_OK;
744657
}
745-
746-
SILKWORM_EXPORT int silkworm_fini(SilkwormHandle handle) SILKWORM_NOEXCEPT {
747-
if (!handle) {
748-
return SILKWORM_INVALID_HANDLE;
749-
}
750-
delete handle;
751-
752-
is_initialized = false;
753-
754-
return SILKWORM_OK;
755-
}

silkworm/capi/silkworm.h

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,12 @@
1717
#include "rpcdaemon.h"
1818
#include "sentry.h"
1919
#endif
20-
#include "log_level.h"
20+
#include "init.h"
2121

2222
#if __cplusplus
2323
extern "C" {
2424
#endif
2525

26-
#define SILKWORM_GIT_VERSION_SIZE 32
27-
28-
//! Silkworm library general configuration options
29-
struct SilkwormSettings {
30-
//! Log verbosity level
31-
SilkwormLogLevel log_verbosity;
32-
//! Number of I/O contexts to use in concurrency mode
33-
uint32_t num_contexts;
34-
//! Data directory path in UTF-8.
35-
char data_dir_path[SILKWORM_PATH_SIZE];
36-
//! libmdbx version string in git describe format.
37-
char libmdbx_version[SILKWORM_GIT_VERSION_SIZE];
38-
//! Index salt for block snapshots
39-
uint32_t blocks_repo_index_salt;
40-
//! Index salt for state snapshots
41-
uint32_t state_repo_index_salt;
42-
};
43-
44-
/**
45-
* \brief Initialize the Silkworm C API library.
46-
* \param[in,out] handle Silkworm instance handle returned on successful initialization.
47-
* \param[in] settings General Silkworm settings.
48-
* \return SILKWORM_OK (=0) on success, a non-zero error value on failure.
49-
*/
50-
SILKWORM_EXPORT int silkworm_init(SilkwormHandle* handle, const struct SilkwormSettings* settings) SILKWORM_NOEXCEPT;
51-
5226
typedef struct MDBX_env MDBX_env;
5327
typedef struct MDBX_txn MDBX_txn;
5428

@@ -142,13 +116,6 @@ SILKWORM_EXPORT int silkworm_execute_txn(SilkwormHandle handle, MDBX_txn* mdbx_t
142116
*/
143117
SILKWORM_EXPORT int silkworm_block_exec_end(SilkwormHandle handle, MDBX_txn* mdbx_tx, MDBX_txn* mdbx_in_mem_temp_tx) SILKWORM_NOEXCEPT;
144118

145-
/**
146-
* \brief Finalize the Silkworm C API library.
147-
* \param[in] handle A valid Silkworm instance handle got with silkworm_init.
148-
* \return SILKWORM_OK (=0) on success, a non-zero error value on failure.
149-
*/
150-
SILKWORM_EXPORT int silkworm_fini(SilkwormHandle handle) SILKWORM_NOEXCEPT;
151-
152119
#if __cplusplus
153120
}
154121
#endif

0 commit comments

Comments
 (0)