Skip to content

Commit

Permalink
third_party: use erigontech/mdbx-go for libmdbx sources (#1741)
Browse files Browse the repository at this point in the history
Co-authored-by: canepat <[email protected]>
  • Loading branch information
battlmonstr and canepat authored Feb 2, 2024
1 parent f0edd23 commit 0cdd97b
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 7 deletions.
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
[submodule "cbor-cpp"]
path = third_party/cbor-cpp/cbor-cpp
url = https://github.com/torquem-ch/cbor-cpp.git
[submodule "libmdbx"]
path = third_party/libmdbx/libmdbx
url = https://github.com/torquem-ch/libmdbx.git
[submodule "interfaces"]
path = third_party/erigon-interfaces
url = https://github.com/ledgerwatch/interfaces.git
branch = stable
[submodule "stbrumme-crc32"]
path = third_party/stbrumme-crc32/stbrumme-crc32
url = https://github.com/battlmonstr/stbrumme-crc32.git
[submodule "erigon-mdbx-go"]
path = third_party/erigon-mdbx-go/mdbx-go
url = https://github.com/erigontech/mdbx-go.git
[submodule "erigon-snapshot"]
path = third_party/erigon-snapshot
url = https://github.com/ledgerwatch/erigon-snapshot
Expand Down
3 changes: 3 additions & 0 deletions cmd/capi/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ int main(int argc, char* argv[]) {
}
strncpy(silkworm_settings.data_dir_path, data_dir_path.c_str(), SILKWORM_PATH_SIZE - 1);

SILK_INFO << "libmdbx version: " << silkworm_libmdbx_version();
strncpy(silkworm_settings.libmdbx_version, ::mdbx::get_version().git.describe, sizeof(silkworm_settings.libmdbx_version) - 1);

const int init_status_code = silkworm_init(&handle, &silkworm_settings);
if (init_status_code != SILKWORM_OK) {
SILK_ERROR << "silkworm_init failed [code=" << std::to_string(init_status_code) << "]";
Expand Down
41 changes: 41 additions & 0 deletions silkworm/capi/silkworm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
#include <charconv>
#include <chrono>
#include <memory>
#include <string>
#include <string_view>
#include <vector>

#include <absl/strings/str_split.h>
#include <boost/circular_buffer.hpp>

#include <silkworm/buildinfo.h>
Expand Down Expand Up @@ -67,6 +70,36 @@ struct ExecutionProgress {
float gas_state_perc{0.0};
};

//! Kind of match to perform between Erigon and Silkworm libmdbx versions
enum class MdbxVersionCheck : uint8_t {
kNone, /// no check at all
kExact, /// git-describe versions must match perfectly
kSemantic, /// compare semantic versions (<M1.m1.p1> == <M2.m2.p2>)
};

static bool is_compatible_mdbx_version(std::string_view their_version, std::string_view our_version, MdbxVersionCheck check) {
SILK_TRACE << "is_compatible_mdbx_version their_version: " << their_version << " our_version: " << our_version;
bool compatible{false};
switch (check) {
case MdbxVersionCheck::kNone: {
compatible = true;
} break;
case MdbxVersionCheck::kExact: {
compatible = their_version == our_version;
} break;
case MdbxVersionCheck::kSemantic: {
const std::vector<std::string> their_version_parts = absl::StrSplit(std::string(their_version), '.');
const std::vector<std::string> our_version_parts = absl::StrSplit(std::string(our_version), '.');
compatible = (their_version_parts.size() >= 3) &&
(our_version_parts.size() >= 3) &&
(their_version_parts[0] == our_version_parts[0]) &&
(their_version_parts[1] == our_version_parts[1]) &&
(their_version_parts[2] == our_version_parts[2]);
}
}
return compatible;
}

//! Generate log arguments for Silkworm library version
static log::Args log_args_for_version() {
const auto build_info{silkworm_get_buildinfo()};
Expand Down Expand Up @@ -158,6 +191,10 @@ SILKWORM_EXPORT int silkworm_init(
return SILKWORM_INVALID_SETTINGS;
}

if (!is_compatible_mdbx_version(settings->libmdbx_version, silkworm_libmdbx_version(), MdbxVersionCheck::kExact)) {
return SILKWORM_INCOMPATIBLE_LIBMDBX;
}

static bool is_initialized = false;
if (is_initialized) {
return SILKWORM_TOO_MANY_INSTANCES;
Expand Down Expand Up @@ -310,6 +347,10 @@ SILKWORM_EXPORT int silkworm_add_snapshot(SilkwormHandle handle, SilkwormChainSn
return SILKWORM_OK;
}

SILKWORM_EXPORT const char* silkworm_libmdbx_version() SILKWORM_NOEXCEPT {
return ::mdbx::get_version().git.describe;
}

SILKWORM_EXPORT int silkworm_start_rpcdaemon(SilkwormHandle handle, MDBX_env* env) SILKWORM_NOEXCEPT {
if (!handle) {
return SILKWORM_INVALID_HANDLE;
Expand Down
9 changes: 9 additions & 0 deletions silkworm/capi/silkworm.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ extern "C" {
#define SILKWORM_INVALID_SETTINGS 14
#define SILKWORM_TERMINATION_SIGNAL 15
#define SILKWORM_SERVICE_ALREADY_STARTED 16
#define SILKWORM_INCOMPATIBLE_LIBMDBX 17

typedef struct MDBX_env MDBX_env;
typedef struct MDBX_txn MDBX_txn;
Expand Down Expand Up @@ -98,6 +99,8 @@ struct SilkwormChainSnapshot {
struct SilkwormSettings {
//! Data directory path in UTF-8.
char data_dir_path[SILKWORM_PATH_SIZE];
//! libmdbx version string in git describe format.
char libmdbx_version[32];
};

/**
Expand Down Expand Up @@ -129,6 +132,12 @@ SILKWORM_EXPORT int silkworm_build_recsplit_indexes(SilkwormHandle handle, struc
*/
SILKWORM_EXPORT int silkworm_add_snapshot(SilkwormHandle handle, struct SilkwormChainSnapshot* snapshot) SILKWORM_NOEXCEPT;

/**
* \brief Get libmdbx version for compatibility checks.
* \return A string in git describe format.
*/
SILKWORM_EXPORT const char* silkworm_libmdbx_version() SILKWORM_NOEXCEPT;

/**
* \brief Start Silkworm RPC daemon.
* \param[in] handle A valid Silkworm instance handle, got with silkworm_init.Must not be zero.
Expand Down
2 changes: 1 addition & 1 deletion third_party/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ add_subdirectory(evmone) # depends on ethash, intx
if(NOT SILKWORM_CORE_ONLY)
add_subdirectory(cbor-cpp)
add_subdirectory(cpp-base64)
add_subdirectory(erigon-mdbx-go)
add_subdirectory(glaze)
add_subdirectory(libmdbx)
add_subdirectory(libtorrent)
add_subdirectory(picohttpparser)
add_subdirectory(sais-lite)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@
]]

set(MDBX_ENABLE_TESTS OFF)
add_subdirectory(libmdbx)
target_include_directories(mdbx-static INTERFACE "libmdbx")
add_subdirectory(mdbx-go/mdbxdist)
target_compile_definitions(mdbx-static PUBLIC CONSTEXPR_ASSERT=assert)
1 change: 1 addition & 0 deletions third_party/erigon-mdbx-go/mdbx-go
Submodule mdbx-go added at 839cb7
1 change: 0 additions & 1 deletion third_party/libmdbx/libmdbx
Submodule libmdbx deleted from 1cac65

0 comments on commit 0cdd97b

Please sign in to comment.