Skip to content

Commit

Permalink
c api: refactor SilkwormHandle (erigontech#1634)
Browse files Browse the repository at this point in the history
  • Loading branch information
battlmonstr authored Nov 21, 2023
1 parent 4aec5f3 commit c4ee339
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 34 deletions.
22 changes: 11 additions & 11 deletions cmd/api/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,26 @@ const char* kSilkwormFiniSymbol = "silkworm_fini";
auto kSilkwormApiLibPath{boost::dll::shared_library::decorate(kSilkwormApiLibUndecoratedPath)};

//! Function signature for silkworm_init C API
using SilkwormInitSig = int(SilkwormHandle**, const struct SilkwormSettings* settings);
using SilkwormInitSig = int(SilkwormHandle*, const struct SilkwormSettings* settings);

//! Function signature for silkworm_build_recsplit_indexes C API
using SilkwormBuildRecSplitIndexes = int(SilkwormHandle*, SilkwormMemoryMappedFile*[], int len);
using SilkwormBuildRecSplitIndexes = int(SilkwormHandle, SilkwormMemoryMappedFile*[], int len);

//! Function signature for silkworm_add_snapshot C API
using SilkwormAddSnapshotSig = int(SilkwormHandle*, SilkwormChainSnapshot*);
using SilkwormAddSnapshotSig = int(SilkwormHandle, SilkwormChainSnapshot*);

//! Function signature for silkworm_start_rpcdaemon C API
using SilkwormStartRpcDaemonSig = int(SilkwormHandle*, MDBX_env*);
using SilkwormStartRpcDaemonSig = int(SilkwormHandle, MDBX_env*);

//! Function signature for silkworm_stop_rpcdaemon C API
using SilkwormStopRpcDaemonSig = int(SilkwormHandle*);
using SilkwormStopRpcDaemonSig = int(SilkwormHandle);

//! Function signature for silkworm_execute_blocks C API
using SilkwormExecuteBlocksSig =
int(SilkwormHandle*, MDBX_txn*, uint64_t, uint64_t, uint64_t, uint64_t, bool, bool, bool, uint64_t*, int*);
int(SilkwormHandle, MDBX_txn*, uint64_t, uint64_t, uint64_t, uint64_t, bool, bool, bool, uint64_t*, int*);

//! Function signature for silkworm_fini C API
using SilkwormFiniSig = int(SilkwormHandle*);
using SilkwormFiniSig = int(SilkwormHandle);

struct ExecuteBlocksSettings {
BlockNum start_block{1};
Expand Down Expand Up @@ -247,7 +247,7 @@ std::vector<SilkwormChainSnapshot> collect_all_snapshots(const SnapshotRepositor
return snapshot_sequence;
}

int execute_blocks(SilkwormHandle* handle, ExecuteBlocksSettings settings, const SnapshotRepository& repository, const DataDirectory& data_dir) {
int execute_blocks(SilkwormHandle handle, ExecuteBlocksSettings settings, const SnapshotRepository& repository, const DataDirectory& data_dir) {
// Import the silkworm_add_snapshot symbol from Silkworm API library
const auto silkworm_add_snapshot{
boost::dll::import_symbol<SilkwormAddSnapshotSig>(kSilkwormApiLibPath, kSilkwormAddSnapshotSymbol)};
Expand Down Expand Up @@ -316,7 +316,7 @@ int execute_blocks(SilkwormHandle* handle, ExecuteBlocksSettings settings, const
return status_code;
}

int build_indices(SilkwormHandle* handle, BuildIdxesSettings settings, const SnapshotRepository& repository, const DataDirectory& data_dir) {
int build_indices(SilkwormHandle handle, BuildIdxesSettings settings, const SnapshotRepository& repository, const DataDirectory& data_dir) {
// Import the silkworm_build_recsplit_indexes symbol from Silkworm API library
const auto silkworm_build_recsplit_indexes{
boost::dll::import_symbol<SilkwormBuildRecSplitIndexes>(kSilkwormApiLibPath, kSilkwormBuildRecSplitIndexes)};
Expand Down Expand Up @@ -376,7 +376,7 @@ int build_indices(SilkwormHandle* handle, BuildIdxesSettings settings, const Sna
return SILKWORM_OK;
}

int start_rpcdaemon(SilkwormHandle* handle, rpc::DaemonSettings /*settings*/, const DataDirectory& data_dir) {
int start_rpcdaemon(SilkwormHandle handle, rpc::DaemonSettings /*settings*/, const DataDirectory& data_dir) {
// Import the silkworm_start_rpcdaemon symbol from Silkworm API library
const auto silkworm_start_rpcdaemon{
boost::dll::import_symbol<SilkwormStartRpcDaemonSig>(kSilkwormApiLibPath, kSilkwormStartRpcDaemonSymbol)};
Expand Down Expand Up @@ -444,7 +444,7 @@ int main(int argc, char* argv[]) {
boost::dll::import_symbol<SilkwormFiniSig>(kSilkwormApiLibPath, kSilkwormFiniSymbol)};

// Initialize Silkworm API library
SilkwormHandle* handle{nullptr};
SilkwormHandle handle{nullptr};

SilkwormSettings silkworm_settings;
std::string data_dir_path = data_dir.path().string();
Expand Down
2 changes: 1 addition & 1 deletion silkworm/api/handle.hpp → silkworm/api/instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <silkworm/node/snapshot/repository.hpp>
#include <silkworm/silkrpc/daemon.hpp>

struct SilkwormHandle {
struct SilkwormInstance {
silkworm::concurrency::ContextPoolSettings context_pool_settings;
std::filesystem::path data_dir_path;
std::unique_ptr<silkworm::snapshot::SnapshotRepository> snapshot_repository;
Expand Down
6 changes: 3 additions & 3 deletions silkworm/api/sentry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <silkworm/sentry/sentry.hpp>
#include <silkworm/sentry/settings.hpp>

#include "handle.hpp"
#include "instance.hpp"
#include "silkworm_api.h"

using namespace silkworm;
Expand Down Expand Up @@ -102,7 +102,7 @@ static void sentry_run(
const boost::asio::cancellation_slot& sentry_stop_signal_slot,
std::latch& sentry_started);

SILKWORM_EXPORT int silkworm_sentry_start(SilkwormHandle* handle, const struct SilkwormSentrySettings* c_settings) SILKWORM_NOEXCEPT {
SILKWORM_EXPORT int silkworm_sentry_start(SilkwormHandle handle, const struct SilkwormSentrySettings* c_settings) SILKWORM_NOEXCEPT {
try {
if (!handle) {
return SILKWORM_INVALID_HANDLE;
Expand Down Expand Up @@ -181,7 +181,7 @@ static void sentry_run(
context_pool.join();
}

SILKWORM_EXPORT int silkworm_sentry_stop(SilkwormHandle* handle) SILKWORM_NOEXCEPT {
SILKWORM_EXPORT int silkworm_sentry_stop(SilkwormHandle handle) SILKWORM_NOEXCEPT {
try {
if (!handle) {
return SILKWORM_INVALID_HANDLE;
Expand Down
18 changes: 9 additions & 9 deletions silkworm/api/silkworm_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include <silkworm/node/snapshot/index.hpp>
#include <silkworm/silkrpc/daemon.hpp>

#include "handle.hpp"
#include "instance.hpp"

using namespace std::chrono_literals;
using namespace silkworm;
Expand Down Expand Up @@ -131,7 +131,7 @@ class SignalHandlerGuard {
};

SILKWORM_EXPORT int silkworm_init(
SilkwormHandle** handle,
SilkwormHandle* handle,
const struct SilkwormSettings* settings) SILKWORM_NOEXCEPT {
if (!handle) {
return SILKWORM_INVALID_HANDLE;
Expand All @@ -153,7 +153,7 @@ SILKWORM_EXPORT int silkworm_init(
auto snapshot_repository = std::make_unique<snapshot::SnapshotRepository>();
db::DataModel::set_snapshot_repository(snapshot_repository.get());

*handle = new SilkwormHandle{
*handle = new SilkwormInstance{
{}, // context_pool_settings
make_path(settings->data_dir_path),
std::move(snapshot_repository),
Expand All @@ -164,7 +164,7 @@ SILKWORM_EXPORT int silkworm_init(
return SILKWORM_OK;
}

SILKWORM_EXPORT int silkworm_build_recsplit_indexes(SilkwormHandle* handle, struct SilkwormMemoryMappedFile* snapshots[], int len) SILKWORM_NOEXCEPT {
SILKWORM_EXPORT int silkworm_build_recsplit_indexes(SilkwormHandle handle, struct SilkwormMemoryMappedFile* snapshots[], int len) SILKWORM_NOEXCEPT {
const int kNeededIndexesToBuildInParallel = 2;

if (!handle) {
Expand Down Expand Up @@ -236,7 +236,7 @@ SILKWORM_EXPORT int silkworm_build_recsplit_indexes(SilkwormHandle* handle, stru
return SILKWORM_OK;
}

SILKWORM_EXPORT int silkworm_add_snapshot(SilkwormHandle* handle, SilkwormChainSnapshot* snapshot) SILKWORM_NOEXCEPT {
SILKWORM_EXPORT int silkworm_add_snapshot(SilkwormHandle handle, SilkwormChainSnapshot* snapshot) SILKWORM_NOEXCEPT {
if (!handle || !handle->snapshot_repository) {
return SILKWORM_INVALID_HANDLE;
}
Expand Down Expand Up @@ -291,7 +291,7 @@ SILKWORM_EXPORT int silkworm_add_snapshot(SilkwormHandle* handle, SilkwormChainS
return SILKWORM_OK;
}

SILKWORM_EXPORT int silkworm_start_rpcdaemon(SilkwormHandle* handle, MDBX_env* env) SILKWORM_NOEXCEPT {
SILKWORM_EXPORT int silkworm_start_rpcdaemon(SilkwormHandle handle, MDBX_env* env) SILKWORM_NOEXCEPT {
if (!handle) {
return SILKWORM_INVALID_HANDLE;
}
Expand Down Expand Up @@ -332,7 +332,7 @@ SILKWORM_EXPORT int silkworm_start_rpcdaemon(SilkwormHandle* handle, MDBX_env* e
return SILKWORM_OK;
}

SILKWORM_EXPORT int silkworm_stop_rpcdaemon(SilkwormHandle* handle) SILKWORM_NOEXCEPT {
SILKWORM_EXPORT int silkworm_stop_rpcdaemon(SilkwormHandle handle) SILKWORM_NOEXCEPT {
if (!handle) {
return SILKWORM_INVALID_HANDLE;
}
Expand All @@ -350,7 +350,7 @@ SILKWORM_EXPORT int silkworm_stop_rpcdaemon(SilkwormHandle* handle) SILKWORM_NOE
}

SILKWORM_EXPORT
int silkworm_execute_blocks(SilkwormHandle* handle, MDBX_txn* mdbx_txn, uint64_t chain_id, uint64_t start_block, uint64_t max_block,
int silkworm_execute_blocks(SilkwormHandle handle, MDBX_txn* mdbx_txn, uint64_t chain_id, uint64_t start_block, uint64_t max_block,
uint64_t batch_size, bool write_change_sets, bool write_receipts, bool write_call_traces,
uint64_t* last_executed_block, int* mdbx_error_code) SILKWORM_NOEXCEPT {
if (!handle) {
Expand Down Expand Up @@ -491,7 +491,7 @@ int silkworm_execute_blocks(SilkwormHandle* handle, MDBX_txn* mdbx_txn, uint64_t
}
}

SILKWORM_EXPORT int silkworm_fini(SilkwormHandle* handle) SILKWORM_NOEXCEPT {
SILKWORM_EXPORT int silkworm_fini(SilkwormHandle handle) SILKWORM_NOEXCEPT {
if (!handle) {
return SILKWORM_INVALID_HANDLE;
}
Expand Down
22 changes: 12 additions & 10 deletions silkworm/api/silkworm_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ extern "C" {

typedef struct MDBX_env MDBX_env;
typedef struct MDBX_txn MDBX_txn;
typedef struct SilkwormHandle SilkwormHandle;

struct SilkwormInstance;
typedef struct SilkwormInstance* SilkwormHandle;

struct SilkwormMemoryMappedFile {
const char* file_path;
Expand Down Expand Up @@ -105,7 +107,7 @@ struct SilkwormSettings {
* \return SILKWORM_OK (=0) on success, a non-zero error value on failure.
*/
SILKWORM_EXPORT int silkworm_init(
SilkwormHandle** handle,
SilkwormHandle* handle,
const struct SilkwormSettings* settings) SILKWORM_NOEXCEPT;

/**
Expand All @@ -117,31 +119,31 @@ SILKWORM_EXPORT int silkworm_init(
* \param[in] len The number of snapshots and paths.
* \return SILKWORM_OK (=0) on success, a non-zero error value on failure on some or all indexes.
*/
SILKWORM_EXPORT int silkworm_build_recsplit_indexes(SilkwormHandle* handle, struct SilkwormMemoryMappedFile* snapshots[], int len) SILKWORM_NOEXCEPT;
SILKWORM_EXPORT int silkworm_build_recsplit_indexes(SilkwormHandle handle, struct SilkwormMemoryMappedFile* snapshots[], int len) SILKWORM_NOEXCEPT;

/**
* \brief Notify Silkworm about a new snapshot to use.
* \param[in] handle A valid Silkworm instance handle, got with silkworm_init.
* \param[in] snapshot A snapshot to use.
* \return SILKWORM_OK (=0) on success, a non-zero error value on failure.
*/
SILKWORM_EXPORT int silkworm_add_snapshot(SilkwormHandle* handle, struct SilkwormChainSnapshot* snapshot) SILKWORM_NOEXCEPT;
SILKWORM_EXPORT int silkworm_add_snapshot(SilkwormHandle handle, struct SilkwormChainSnapshot* snapshot) SILKWORM_NOEXCEPT;

/**
* \brief Start Silkworm RPC daemon.
* \param[in] handle A valid Silkworm instance handle, got with silkworm_init.Must not be zero.
* \param[in] env An valid MDBX environment. Must not be zero.
* \return SILKWORM_OK (=0) on success, a non-zero error value on failure.
*/
SILKWORM_EXPORT int silkworm_start_rpcdaemon(SilkwormHandle* handle, MDBX_env* env) SILKWORM_NOEXCEPT;
SILKWORM_EXPORT int silkworm_start_rpcdaemon(SilkwormHandle handle, MDBX_env* env) SILKWORM_NOEXCEPT;

/**
* \brief Stop Silkworm RPC daemon and wait for its termination.
* \param[in] handle A valid Silkworm instance handle, got with silkworm_init. Must not be zero.
* \param[in] snapshot A snapshot to use.
* \return SILKWORM_OK (=0) on success, a non-zero error value on failure.
*/
SILKWORM_EXPORT int silkworm_stop_rpcdaemon(SilkwormHandle* handle) SILKWORM_NOEXCEPT;
SILKWORM_EXPORT int silkworm_stop_rpcdaemon(SilkwormHandle handle) SILKWORM_NOEXCEPT;

#define SILKWORM_SENTRY_SETTINGS_CLIENT_ID_SIZE 128
#define SILKWORM_SENTRY_SETTINGS_NAT_SIZE 50
Expand All @@ -162,8 +164,8 @@ struct SilkwormSentrySettings {
size_t max_peers;
};

SILKWORM_EXPORT int silkworm_sentry_start(SilkwormHandle* handle, const struct SilkwormSentrySettings* settings) SILKWORM_NOEXCEPT;
SILKWORM_EXPORT int silkworm_sentry_stop(SilkwormHandle* handle) SILKWORM_NOEXCEPT;
SILKWORM_EXPORT int silkworm_sentry_start(SilkwormHandle handle, const struct SilkwormSentrySettings* settings) SILKWORM_NOEXCEPT;
SILKWORM_EXPORT int silkworm_sentry_stop(SilkwormHandle handle) SILKWORM_NOEXCEPT;

/**
* \brief Execute a batch of blocks and write resulting changes into the database.
Expand All @@ -188,7 +190,7 @@ SILKWORM_EXPORT int silkworm_sentry_stop(SilkwormHandle* handle) SILKWORM_NOEXCE
* (blocks up to and incl. last_executed_block were still executed).
*/
SILKWORM_EXPORT int silkworm_execute_blocks(
SilkwormHandle* handle, MDBX_txn* txn, uint64_t chain_id, uint64_t start_block, uint64_t max_block,
SilkwormHandle handle, MDBX_txn* txn, uint64_t chain_id, uint64_t start_block, uint64_t max_block,
uint64_t batch_size, bool write_change_sets, bool write_receipts, bool write_call_traces,
uint64_t* last_executed_block, int* mdbx_error_code) SILKWORM_NOEXCEPT;

Expand All @@ -197,7 +199,7 @@ SILKWORM_EXPORT int silkworm_execute_blocks(
* \param[in] handle A valid Silkworm instance handle got with silkworm_init.
* \return SILKWORM_OK (=0) on success, a non-zero error value on failure.
*/
SILKWORM_EXPORT int silkworm_fini(SilkwormHandle* handle) SILKWORM_NOEXCEPT;
SILKWORM_EXPORT int silkworm_fini(SilkwormHandle handle) SILKWORM_NOEXCEPT;

#if __cplusplus
}
Expand Down

0 comments on commit c4ee339

Please sign in to comment.