Merge bitcoin/bitcoin#28924: refactor: Remove unused and fragile string interface from arith_uint256

fa63f16018 test: Add uint256 string parse tests (MarcoFalke)
facf629ce8 refactor: Remove unused and fragile string interface from arith_uint256 (MarcoFalke)

Pull request description:

  The string interface (`base_uint(const std::string&)`, as well as `base_uint::SetHex`) is problematic for many reasons:

  * It is unused (except in test-only code).
  * It is redundant with the `uint256` string interface: `std::string -> uint256 -> UintToArith256`.
  * It is brittle, because it inherits the brittle `uint256` string interface, which is brittle due to the use of `c_str()` (embedded null will be treated as end-of string), etc ...

  Instead of fixing the interface, remove it since it is unused and redundant with `UintToArith256`.

ACKs for top commit:
  ajtowns:
    ACK fa63f16018
  TheCharlatan:
    ACK fa63f16018

Tree-SHA512: a95d5b938ffd0473361336bbf6be093d01265a626c50be1345ce2c5e582c0f3f73eb11af5fd1884019f59d7ba27e670ecffdb41d2c624ffb9aa63bd52b780e62
This commit is contained in:
fanquake
2023-12-07 15:44:38 +00:00
5 changed files with 56 additions and 52 deletions

View File

@@ -259,8 +259,8 @@ BOOST_AUTO_TEST_CASE( conversion )
BOOST_CHECK(UintToArith256(OneL) == 1);
BOOST_CHECK(ArithToUint256(0) == ZeroL);
BOOST_CHECK(ArithToUint256(1) == OneL);
BOOST_CHECK(arith_uint256(R1L.GetHex()) == UintToArith256(R1L));
BOOST_CHECK(arith_uint256(R2L.GetHex()) == UintToArith256(R2L));
BOOST_CHECK(arith_uint256(UintToArith256(uint256S(R1L.GetHex()))) == UintToArith256(R1L));
BOOST_CHECK(arith_uint256(UintToArith256(uint256S(R2L.GetHex()))) == UintToArith256(R2L));
BOOST_CHECK(R1L.GetHex() == UintToArith256(R1L).GetHex());
BOOST_CHECK(R2L.GetHex() == UintToArith256(R2L).GetHex());
}
@@ -278,6 +278,34 @@ BOOST_AUTO_TEST_CASE( operator_with_self )
BOOST_CHECK(v == UintToArith256(uint256S("0")));
}
BOOST_AUTO_TEST_CASE(parse)
{
{
std::string s_12{"0000000000000000000000000000000000000000000000000000000000000012"};
BOOST_CHECK_EQUAL(uint256S("12\0").GetHex(), s_12);
BOOST_CHECK_EQUAL(uint256S(std::string{"12\0", 3}).GetHex(), s_12);
BOOST_CHECK_EQUAL(uint256S("0x12").GetHex(), s_12);
BOOST_CHECK_EQUAL(uint256S(" 0x12").GetHex(), s_12);
BOOST_CHECK_EQUAL(uint256S(" 12").GetHex(), s_12);
}
{
std::string s_1{uint256::ONE.GetHex()};
BOOST_CHECK_EQUAL(uint256S("1\0").GetHex(), s_1);
BOOST_CHECK_EQUAL(uint256S(std::string{"1\0", 2}).GetHex(), s_1);
BOOST_CHECK_EQUAL(uint256S("0x1").GetHex(), s_1);
BOOST_CHECK_EQUAL(uint256S(" 0x1").GetHex(), s_1);
BOOST_CHECK_EQUAL(uint256S(" 1").GetHex(), s_1);
}
{
std::string s_0{uint256::ZERO.GetHex()};
BOOST_CHECK_EQUAL(uint256S("\0").GetHex(), s_0);
BOOST_CHECK_EQUAL(uint256S(std::string{"\0", 1}).GetHex(), s_0);
BOOST_CHECK_EQUAL(uint256S("0x").GetHex(), s_0);
BOOST_CHECK_EQUAL(uint256S(" 0x").GetHex(), s_0);
BOOST_CHECK_EQUAL(uint256S(" ").GetHex(), s_0);
}
}
BOOST_AUTO_TEST_CASE( check_ONE )
{
uint256 one = uint256S("0000000000000000000000000000000000000000000000000000000000000001");