Skip to content

Commit

Permalink
Replace gmpxx usage with gmp
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
jagerman committed Sep 26, 2024
1 parent d4eb24a commit b038b6f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions external/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
29 changes: 15 additions & 14 deletions src/provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "ethyl/provider.hpp"
#include "ethyl/utils.hpp"

#include <gmpxx.h>
#include <gmp.h>

namespace
{
Expand Down Expand Up @@ -704,22 +704,23 @@ void Provider::getBalanceAsync(std::string_view address, optional_callback<std::
return;
}

try
{
std::string balanceHex = r->get<std::string>();
std::string balanceHex = r->get<std::string>();

// 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<std::string>());
user_cb(std::nullopt);
std::optional<std::string> 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));
}
Expand Down

0 comments on commit b038b6f

Please sign in to comment.