From f207523421b22d0eddbb68faa4275c41de77600f Mon Sep 17 00:00:00 2001 From: canepat <16927169+canepat@users.noreply.github.com> Date: Wed, 18 Oct 2023 22:25:04 +0200 Subject: [PATCH] api: handle RPCDaemon not started or already started (#1591) --- silkworm/api/silkworm_api.cpp | 9 ++++++++- silkworm/api/silkworm_api.h | 23 +++++++++++++---------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/silkworm/api/silkworm_api.cpp b/silkworm/api/silkworm_api.cpp index b2aad21b19..758b63bb71 100644 --- a/silkworm/api/silkworm_api.cpp +++ b/silkworm/api/silkworm_api.cpp @@ -277,6 +277,9 @@ SILKWORM_EXPORT int silkworm_start_rpcdaemon(SilkwormHandle* handle, MDBX_env* e if (handle != instance.handle) { return SILKWORM_INSTANCE_NOT_FOUND; } + if (instance.rpcdaemon) { + return SILKWORM_RPCDAEMON_ALREADY_STARTED; + } struct EnvUnmanaged : public ::mdbx::env { explicit EnvUnmanaged(MDBX_env* ptr) : ::mdbx::env{ptr} {} @@ -293,7 +296,7 @@ SILKWORM_EXPORT int silkworm_start_rpcdaemon(SilkwormHandle* handle, MDBX_env* e instance.rpcdaemon = std::make_unique(settings, std::make_optional(unmanaged_env)); // Check protocol version compatibility with Core Services - if (not settings.skip_protocol_check) { + if (!settings.skip_protocol_check) { SILK_INFO << "[Silkworm RPC] Checking protocol version compatibility with core services..."; const auto checklist = instance.rpcdaemon->run_checklist(); @@ -315,11 +318,15 @@ SILKWORM_EXPORT int silkworm_stop_rpcdaemon(SilkwormHandle* handle) SILKWORM_NOE if (handle != instance.handle) { return SILKWORM_INSTANCE_NOT_FOUND; } + if (!instance.rpcdaemon) { + return SILKWORM_RPCDAEMON_NOT_STARTED; + } instance.rpcdaemon->stop(); SILK_INFO << "[Silkworm RPC] Exiting..."; instance.rpcdaemon->join(); SILK_INFO << "[Silkworm RPC] Stopped"; + instance.rpcdaemon.reset(); return SILKWORM_OK; } diff --git a/silkworm/api/silkworm_api.h b/silkworm/api/silkworm_api.h index b79a7e2960..184f3c4939 100644 --- a/silkworm/api/silkworm_api.h +++ b/silkworm/api/silkworm_api.h @@ -39,10 +39,9 @@ extern "C" { #endif -typedef struct MDBX_env MDBX_env; -typedef struct MDBX_txn MDBX_txn; +// Silkworm library error codes (SILKWORM_OK indicates no error, i.e. success) -#define SILKWORM_OK 0 /* Successful result */ +#define SILKWORM_OK 0 #define SILKWORM_INTERNAL_ERROR 1 #define SILKWORM_UNKNOWN_ERROR 2 #define SILKWORM_INVALID_HANDLE 3 @@ -58,16 +57,13 @@ typedef struct MDBX_txn MDBX_txn; #define SILKWORM_TOO_MANY_INSTANCES 13 #define SILKWORM_INSTANCE_NOT_FOUND 14 #define SILKWORM_TERMINATION_SIGNAL 15 +#define SILKWORM_RPCDAEMON_ALREADY_STARTED 16 +#define SILKWORM_RPCDAEMON_NOT_STARTED 17 +typedef struct MDBX_env MDBX_env; +typedef struct MDBX_txn MDBX_txn; typedef struct SilkwormHandle SilkwormHandle; -/** - * \brief Initialize the Silkworm C API library. - * \param[in,out] handle Silkworm instance handle returned on successful initialization. - * \return SILKWORM_OK (=0) on success, a non-zero error value on failure. - */ -SILKWORM_EXPORT int silkworm_init(SilkwormHandle** handle) SILKWORM_NOEXCEPT; - struct SilkwormMemoryMappedFile { const char* file_path; uint8_t* memory_address; @@ -96,6 +92,13 @@ struct SilkwormChainSnapshot { struct SilkwormTransactionsSnapshot transactions; }; +/** + * \brief Initialize the Silkworm C API library. + * \param[in,out] handle Silkworm instance handle returned on successful initialization. + * \return SILKWORM_OK (=0) on success, a non-zero error value on failure. + */ +SILKWORM_EXPORT int silkworm_init(SilkwormHandle** handle) SILKWORM_NOEXCEPT; + /** * \brief Build a set of indexes for the given snapshots. * \param[in] handle A valid Silkworm instance handle, got with silkworm_init.