Skip to content

Commit

Permalink
Merge pull request #35 from jagerman/no-gmpxx
Browse files Browse the repository at this point in the history
Replace gmpxx usage with gmp
  • Loading branch information
jagerman authored Sep 26, 2024
2 parents d4eb24a + b038b6f commit ca215f6
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 ca215f6

Please sign in to comment.