From b038b6f9c517dabb095cd2ec66a11dd464bd0810 Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Thu, 26 Sep 2024 20:11:11 -0300 Subject: [PATCH] Replace gmpxx usage with gmp The packaging isn't looking for gmpxx, and so fails when gmp is installed but gmpxx isn't. Rather than add a dependency on both gmpxx and gmp, this just replaces the small amount of gmpxx usage with gmp and drops the gmpxx linking. Also fixes cmake target names to be less error prone (using gmp::gmp rather than bare gmp or gmpxx ensures cmake sees a missing target rather than it failing later at build time), and to not do anything if a gmp::gmp target is already available (e.g. from a parent project). --- CMakeLists.txt | 3 +-- external/CMakeLists.txt | 9 +++++---- src/provider.cpp | 29 +++++++++++++++-------------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d3e32e..c4b1e83 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -106,8 +106,7 @@ target_link_libraries( PRIVATE cpr::cpr oxen::logging - gmp - gmpxx + gmp::gmp ) if(${PROJECT_NAME}_ENABLE_SIGNER) target_link_libraries(${PROJECT_NAME} PUBLIC secp256k1) diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index 219471a..a0a30e3 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -62,7 +62,8 @@ endif() # GMP # -pkg_check_modules(GMP gmp IMPORTED_TARGET REQUIRED) -add_library(gmp INTERFACE) -target_link_libraries(gmp INTERFACE PkgConfig::GMP) -message(STATUS "Found gmp ${GMP_VERSION}") +if(NOT TARGET gmp::gmp) + pkg_check_modules(GMP gmp IMPORTED_TARGET REQUIRED GLOBAL) + add_library(gmp::gmp ALIAS PkgConfig::GMP) + message(STATUS "Found gmp ${GMP_VERSION}") +endif() diff --git a/src/provider.cpp b/src/provider.cpp index b0c5a24..f715359 100644 --- a/src/provider.cpp +++ b/src/provider.cpp @@ -15,7 +15,7 @@ #include "ethyl/provider.hpp" #include "ethyl/utils.hpp" -#include +#include namespace { @@ -704,22 +704,23 @@ void Provider::getBalanceAsync(std::string_view address, optional_callbackget(); + std::string balanceHex = r->get(); - // Convert balance from hex to GMP multi-precision integer - mpz_class balance; - balance.set_str(balanceHex, 0); // 0 as base to automatically pick up hex from the prepended 0x of our balanceHex string + // Convert balance from hex to GMP multi-precision integer - user_cb(balance.get_str()); - return; - } - catch (const std::exception& e) - { - log::warning(logcat, "eth_getBalance response, failed to parse bigint: {}", r->get()); - user_cb(std::nullopt); + std::optional bal10; + mpz_t balance; + // 0 as base to automatically pick up hex from the prepended 0x of our balanceHex string + if (int rc = mpz_init_set_str(balance, balanceHex.c_str(), 0); rc == 0) { + bal10.emplace(); + bal10->resize(mpz_sizeinbase(balance, 10) + 1); + mpz_get_str(bal10->data(), 10, balance); + bal10->resize(std::strlen(bal10->c_str())); + } else { + log::warning(logcat, "eth_getBalance response, failed to parse bigint: {}", balanceHex); } + mpz_clear(balance); + user_cb(std::move(bal10)); }; makeJsonRpcRequest("eth_getBalance", params, std::move(cb)); }