silkworm: internal refactorings (#8675)

This commit is contained in:
battlmonstr 2023-11-21 15:22:56 +01:00 committed by GitHub
parent 0aa06af226
commit 4abe1d59d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 53 additions and 51 deletions

View File

@ -846,8 +846,8 @@ var (
Usage: "Comma separated list of support session ids to connect to",
}
SilkwormPathFlag = cli.StringFlag{
Name: "silkworm.path",
SilkwormLibraryPathFlag = cli.StringFlag{
Name: "silkworm.libpath",
Usage: "Path to the Silkworm library",
Value: "",
}
@ -1528,7 +1528,7 @@ func setBeaconAPI(ctx *cli.Context, cfg *ethconfig.Config) {
}
func setSilkworm(ctx *cli.Context, cfg *ethconfig.Config) {
cfg.SilkwormPath = ctx.String(SilkwormPathFlag.Name)
cfg.SilkwormLibraryPath = ctx.String(SilkwormLibraryPathFlag.Name)
if ctx.IsSet(SilkwormExecutionFlag.Name) {
cfg.SilkwormExecution = ctx.Bool(SilkwormExecutionFlag.Name)
}

View File

@ -344,8 +344,8 @@ func New(ctx context.Context, stack *node.Node, config *ethconfig.Config, logger
backend.gasPrice, _ = uint256.FromBig(config.Miner.GasPrice)
if config.SilkwormPath != "" {
backend.silkworm, err = silkworm.New(config.SilkwormPath, config.Dirs.DataDir)
if config.SilkwormLibraryPath != "" {
backend.silkworm, err = silkworm.New(config.SilkwormLibraryPath, config.Dirs.DataDir)
if err != nil {
return nil, err
}

View File

@ -103,7 +103,7 @@ var Defaults = Config{
Produce: true,
},
// applies if SilkwormPath is set
// applies if SilkwormLibraryPath is set
SilkwormExecution: true,
}
@ -254,10 +254,10 @@ type Config struct {
ForcePartialCommit bool
// Embedded Silkworm support
SilkwormPath string
SilkwormExecution bool
SilkwormRpcDaemon bool
SilkwormSentry bool
SilkwormLibraryPath string
SilkwormExecution bool
SilkwormRpcDaemon bool
SilkwormSentry bool
}
type Sync struct {

View File

@ -165,7 +165,7 @@ var DefaultFlags = []cli.Flag{
&utils.OtsSearchMaxCapFlag,
&utils.SilkwormPathFlag,
&utils.SilkwormLibraryPathFlag,
&utils.SilkwormExecutionFlag,
&utils.SilkwormRpcDaemonFlag,
&utils.SilkwormSentryFlag,

View File

@ -54,7 +54,7 @@ var ErrInterrupted = errors.New("interrupted")
type Silkworm struct {
dllHandle unsafe.Pointer
instance *C.SilkwormHandle
handle C.SilkwormHandle
initFunc unsafe.Pointer
finiFunc unsafe.Pointer
addSnapshot unsafe.Pointer
@ -65,10 +65,10 @@ type Silkworm struct {
executeBlocks unsafe.Pointer
}
func New(dllPath string, dataDirPath string) (*Silkworm, error) {
dllHandle, err := OpenLibrary(dllPath)
func New(libraryPath string, dataDirPath string) (*Silkworm, error) {
dllHandle, err := OpenLibrary(libraryPath)
if err != nil {
return nil, fmt.Errorf("failed to load silkworm library from path %s: %w", dllPath, err)
return nil, fmt.Errorf("failed to load silkworm library from path %s: %w", libraryPath, err)
}
initFunc, err := LoadFunction(dllHandle, "silkworm_init")
@ -106,7 +106,7 @@ func New(dllPath string, dataDirPath string) (*Silkworm, error) {
silkworm := &Silkworm{
dllHandle: dllHandle,
instance: nil,
handle: nil,
initFunc: initFunc,
finiFunc: finiFunc,
addSnapshot: addSnapshot,
@ -123,7 +123,7 @@ func New(dllPath string, dataDirPath string) (*Silkworm, error) {
return nil, errors.New("silkworm.New failed to copy dataDirPath")
}
status := C.call_silkworm_init_func(silkworm.initFunc, &silkworm.instance, settings) //nolint:gocritic
status := C.call_silkworm_init_func(silkworm.initFunc, &silkworm.handle, settings) //nolint:gocritic
if status == SILKWORM_OK {
return silkworm, nil
}
@ -131,8 +131,8 @@ func New(dllPath string, dataDirPath string) (*Silkworm, error) {
}
func (s *Silkworm) Close() {
C.call_silkworm_fini_func(s.finiFunc, s.instance)
s.instance = nil
C.call_silkworm_fini_func(s.finiFunc, s.handle)
s.handle = nil
}
func (s *Silkworm) AddSnapshot(snapshot *MappedChainSnapshot) error {
@ -200,7 +200,7 @@ func (s *Silkworm) AddSnapshot(snapshot *MappedChainSnapshot) error {
transactions: cTxsSnapshot,
}
status := C.call_silkworm_add_snapshot_func(s.addSnapshot, s.instance, &cChainSnapshot) //nolint:gocritic
status := C.call_silkworm_add_snapshot_func(s.addSnapshot, s.handle, &cChainSnapshot) //nolint:gocritic
if status == SILKWORM_OK {
return nil
}
@ -209,7 +209,7 @@ func (s *Silkworm) AddSnapshot(snapshot *MappedChainSnapshot) error {
func (s *Silkworm) StartRpcDaemon(db kv.RoDB) error {
cEnv := (*C.MDBX_env)(db.CHandle())
status := C.call_silkworm_start_rpcdaemon_func(s.startRpcDaemon, s.instance, cEnv)
status := C.call_silkworm_start_rpcdaemon_func(s.startRpcDaemon, s.handle, cEnv)
// Handle successful execution
if status == SILKWORM_OK {
return nil
@ -218,7 +218,7 @@ func (s *Silkworm) StartRpcDaemon(db kv.RoDB) error {
}
func (s *Silkworm) StopRpcDaemon() error {
status := C.call_silkworm_stop_rpcdaemon_func(s.stopRpcDaemon, s.instance)
status := C.call_silkworm_stop_rpcdaemon_func(s.stopRpcDaemon, s.handle)
// Handle successful execution
if status == SILKWORM_OK {
return nil
@ -309,7 +309,7 @@ func (s *Silkworm) SentryStart(settings SentrySettings) error {
if err != nil {
return err
}
status := C.call_silkworm_sentry_start_func(s.sentryStart, s.instance, cSettings)
status := C.call_silkworm_sentry_start_func(s.sentryStart, s.handle, cSettings)
if status == SILKWORM_OK {
return nil
}
@ -317,7 +317,7 @@ func (s *Silkworm) SentryStart(settings SentrySettings) error {
}
func (s *Silkworm) SentryStop() error {
status := C.call_silkworm_stop_rpcdaemon_func(s.sentryStop, s.instance)
status := C.call_silkworm_stop_rpcdaemon_func(s.sentryStop, s.handle)
if status == SILKWORM_OK {
return nil
}
@ -359,7 +359,7 @@ func (s *Silkworm) ExecuteBlocks(txn kv.Tx, chainID *big.Int, startBlock uint64,
cWriteCallTraces := C._Bool(writeCallTraces)
cLastExecutedBlock := C.uint64_t(startBlock - 1)
cMdbxErrorCode := C.int(0)
status := C.call_silkworm_execute_blocks_func(s.executeBlocks, s.instance, cTxn, cChainId, cStartBlock,
status := C.call_silkworm_execute_blocks_func(s.executeBlocks, s.handle, cTxn, cChainId, cStartBlock,
cMaxBlock, cBatchSize, cWriteChangeSets, cWriteReceipts, cWriteCallTraces, &cLastExecutedBlock, &cMdbxErrorCode)
lastExecutedBlock = uint64(cLastExecutedBlock)
// Handle successful execution

View File

@ -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;
@ -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;
/**
@ -117,7 +119,7 @@ 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.
@ -125,7 +127,7 @@ SILKWORM_EXPORT int silkworm_build_recsplit_indexes(SilkwormHandle* handle, stru
* \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.
@ -133,7 +135,7 @@ SILKWORM_EXPORT int silkworm_add_snapshot(SilkwormHandle* handle, struct Silkwor
* \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.
@ -141,7 +143,7 @@ SILKWORM_EXPORT int silkworm_start_rpcdaemon(SilkwormHandle* handle, MDBX_env* e
* \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
@ -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.
@ -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;
@ -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
}

View File

@ -19,56 +19,56 @@
#include "silkworm_api.h"
typedef int (*silkworm_init_func)(SilkwormHandle** handle, const struct SilkwormSettings* settings);
typedef int (*silkworm_init_func)(SilkwormHandle* handle, const struct SilkwormSettings* settings);
int call_silkworm_init_func(void* func_ptr, SilkwormHandle** handle, const struct SilkwormSettings* settings) {
int call_silkworm_init_func(void* func_ptr, SilkwormHandle* handle, const struct SilkwormSettings* settings) {
return ((silkworm_init_func)func_ptr)(handle, settings);
}
typedef int (*silkworm_add_snapshot_func)(SilkwormHandle* handle, struct SilkwormChainSnapshot* snapshot);
typedef int (*silkworm_add_snapshot_func)(SilkwormHandle handle, struct SilkwormChainSnapshot* snapshot);
int call_silkworm_add_snapshot_func(void* func_ptr, SilkwormHandle* handle, struct SilkwormChainSnapshot* snapshot) {
int call_silkworm_add_snapshot_func(void* func_ptr, SilkwormHandle handle, struct SilkwormChainSnapshot* snapshot) {
return ((silkworm_add_snapshot_func)func_ptr)(handle, snapshot);
}
typedef int (*silkworm_start_rpcdaemon_func)(SilkwormHandle* handle, MDBX_env* env);
typedef int (*silkworm_start_rpcdaemon_func)(SilkwormHandle handle, MDBX_env* env);
int call_silkworm_start_rpcdaemon_func(void* func_ptr, SilkwormHandle* handle, MDBX_env* env) {
int call_silkworm_start_rpcdaemon_func(void* func_ptr, SilkwormHandle handle, MDBX_env* env) {
return ((silkworm_start_rpcdaemon_func)func_ptr)(handle, env);
}
typedef int (*silkworm_stop_rpcdaemon_func)(SilkwormHandle* handle);
typedef int (*silkworm_stop_rpcdaemon_func)(SilkwormHandle handle);
int call_silkworm_stop_rpcdaemon_func(void* func_ptr, SilkwormHandle* handle) {
int call_silkworm_stop_rpcdaemon_func(void* func_ptr, SilkwormHandle handle) {
return ((silkworm_stop_rpcdaemon_func)func_ptr)(handle);
}
typedef int (*silkworm_sentry_start_func)(SilkwormHandle* handle, const struct SilkwormSentrySettings* settings);
typedef int (*silkworm_sentry_start_func)(SilkwormHandle handle, const struct SilkwormSentrySettings* settings);
int call_silkworm_sentry_start_func(void* func_ptr, SilkwormHandle* handle, const struct SilkwormSentrySettings* settings) {
int call_silkworm_sentry_start_func(void* func_ptr, SilkwormHandle handle, const struct SilkwormSentrySettings* settings) {
return ((silkworm_sentry_start_func)func_ptr)(handle, settings);
}
typedef int (*silkworm_sentry_stop_func)(SilkwormHandle* handle);
typedef int (*silkworm_sentry_stop_func)(SilkwormHandle handle);
int call_silkworm_sentry_stop_func(void* func_ptr, SilkwormHandle* handle) {
int call_silkworm_sentry_stop_func(void* func_ptr, SilkwormHandle handle) {
return ((silkworm_sentry_stop_func)func_ptr)(handle);
}
typedef int (*silkworm_execute_blocks_func)(SilkwormHandle* handle, MDBX_txn* txn, uint64_t chain_id, uint64_t start_block,
typedef int (*silkworm_execute_blocks_func)(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);
int call_silkworm_execute_blocks_func(void* func_ptr, SilkwormHandle* handle, MDBX_txn* txn, uint64_t chain_id, uint64_t start_block,
int call_silkworm_execute_blocks_func(void* func_ptr, 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) {
return ((silkworm_execute_blocks_func)func_ptr)(handle, txn, chain_id, start_block, max_block, batch_size, write_change_sets,
write_receipts, write_call_traces, last_executed_block, mdbx_error_code);
}
typedef int (*silkworm_fini_func)(SilkwormHandle* handle);
typedef int (*silkworm_fini_func)(SilkwormHandle handle);
int call_silkworm_fini_func(void* func_ptr, SilkwormHandle* handle) {
int call_silkworm_fini_func(void* func_ptr, SilkwormHandle handle) {
return ((silkworm_fini_func)func_ptr)(handle);
}