Skip to content

Commit

Permalink
Deprecate HexTo & fix SafeAddress copy ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Lessa committed Feb 7, 2024
1 parent f29e192 commit 09130d4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
4 changes: 1 addition & 3 deletions src/contract/variables/safeaddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ class SafeAddress : public SafeBase {

/// Copy constructor.
SafeAddress(const SafeAddress& other) : SafeBase(nullptr) {
check();
address_ = other.address_;
addressPtr_ = std::make_unique<Address>(*other.addressPtr_);
other.check(); addressPtr_ = std::make_unique<Address>(*other.addressPtr_);
}

/// Getter for the value. Returns the value from the pointer.
Expand Down
27 changes: 10 additions & 17 deletions src/utils/hex.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,8 @@ using BytesArr = std::array<Byte, N>;
using BytesArrView = std::span<const Byte, std::dynamic_extent>;
using BytesArrMutableView = std::span<Byte, std::dynamic_extent>;


using uint256_t = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::cpp_int_check_type::checked, void>>;

/**
* Helper struct for use with Boost's lexical_cast to convert hex strings
* to a given type (`boost::lexical_cast<%HexTo<uint256_t>>(hexStr)`).
*/
template <typename ElemT> struct HexTo {
ElemT value; ///< The value to hold.
operator ElemT() const { return value; } ///< Operator to get the value.
/// Stream operator.
friend std::istream& operator>>(std::istream& in, HexTo& out) {
in >> std::hex >> out.value;
return in;
}
};

/// Abstraction of a strictly hex-formatted string (`(0x)[1-9][a-f][A-F]`).
class Hex {
private:
Expand Down Expand Up @@ -125,9 +110,17 @@ class Hex {
/// Getter for `hex`.
inline const std::string& get() const { return this->hex_; }

/// Getter for `hex`, but converts it back to an unsigned integer.
/**
* Getter for `hex`, but converts it back to an unsigned 256-bit integer.
* @throw std::length_error if hex is too big to be converted to uint256_t.
*/
inline uint256_t getUint() const {
return boost::lexical_cast<HexTo<uint256_t>>(this->hex_);
Bytes b = Hex::toBytes(this->hex_);
if (b.size() > 32) throw std::length_error("Hex too big for uint conversion");
BytesArrView bV(b.data(), b.size());
uint256_t ret;
boost::multiprecision::import_bits(ret, bV.begin(), bV.end(), 8);
return ret;
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/utils/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ See the LICENSE.txt file in the project root for more information.
* This class is used as a base for both classes inheriting it
* (e.g. Hash, Signature, etc.) and aliases (e.g. PrivKey, PubKey, etc.).
*/

template <unsigned N> class FixedBytes {
protected:
BytesArr<N> data_; ///< Internal string data.
Expand Down
2 changes: 1 addition & 1 deletion src/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ See the LICENSE.txt file in the project root for more information.
#include <array>
#include <span>
#include <cxxabi.h>
#include <variant>

#include <boost/lexical_cast.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
Expand All @@ -33,7 +34,6 @@ See the LICENSE.txt file in the project root for more information.
#include "src/libs/json.hpp"
#include "src/contract/variables/safeuint.h"
#include "src/contract/variables/safeint.h"
#include <variant>

/// @file utils.h

Expand Down
13 changes: 13 additions & 0 deletions tests/utils/hex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,23 @@ namespace THex {

SECTION("Hex GetUint") {
std::string hexStr = "0x1234";
std::string oddHexStr = "0xfffff";
std::string evenHexStr = "0x0fffff";
std::string tooBigHexStr = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"; // 33 bytes
Hex hex(hexStr, false);
Hex hexStrict(hexStr, true);
Hex oddHex(oddHexStr, true);
Hex evenHex(evenHexStr, true);
Hex tooBigHex(tooBigHexStr, true);
REQUIRE(hex.getUint() == uint256_t(4660));
REQUIRE(hexStrict.getUint() == uint256_t(4660));
REQUIRE(oddHex.getUint() == uint256_t(1048575));
REQUIRE(evenHex.getUint() == uint256_t(1048575));
try {
uint256_t wrongNumber = tooBigHex.getUint();
} catch (std::length_error& e) {
REQUIRE(e.what() == std::string("Hex too big for uint conversion"));
}
}

SECTION("Hex Substr") {
Expand Down

0 comments on commit 09130d4

Please sign in to comment.