Skip to content

Commit

Permalink
Rename :is_malleable32_size to :is_malleable32, style.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Apr 11, 2024
1 parent 8f50b17 commit e9e5e61
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 110 deletions.
5 changes: 2 additions & 3 deletions include/bitcoin/system/chain/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,12 @@ class BC_API block

size_t malleated32_size() const NOEXCEPT;
bool is_malleated32(size_t width) const NOEXCEPT;
static constexpr bool is_malleable32_size(size_t set, size_t width) NOEXCEPT
static constexpr bool is_malleable32(size_t set, size_t width) NOEXCEPT
{
// Malleable when set is odd at width depth and not before and not one.
// This is the only case in which Merkle clones the last item in a set.
for (auto depth = one; depth <= width; depth *= two, set = to_half(set))
if (is_odd(set))
return depth == width && !is_one(set);
if (is_odd(set)) return depth == width && !is_one(set);

return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/chain/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ bool block::is_malleable32() const NOEXCEPT
{
const auto unmalleated = txs_->size();
for (auto mally = one; mally <= unmalleated; mally *= two)
if (is_malleable32_size(unmalleated, mally))
if (is_malleable32(unmalleated, mally))
return true;

return false;
Expand All @@ -464,7 +464,7 @@ size_t block::malleated32_size() const NOEXCEPT
{
const auto malleated = txs_->size();
for (auto mally = one; mally <= to_half(malleated); mally *= two)
if (is_malleable32_size(malleated - mally, mally) &&
if (is_malleable32(malleated - mally, mally) &&
is_malleated32(mally))
return mally;

Expand Down
210 changes: 105 additions & 105 deletions test/chain/block_malleable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ class accessor
return block::is_malleated32(width);
}

static constexpr bool is_malleable32_size(size_t set,
static constexpr bool is_malleable32_(size_t set,
size_t width) NOEXCEPT
{
return block::is_malleable32_size(set, width);
return block::is_malleable32(set, width);
}
};

Expand Down Expand Up @@ -132,7 +132,7 @@ BOOST_AUTO_TEST_CASE(block__malleable__empty__false)
BOOST_REQUIRE(!instance.is_malleable());
BOOST_REQUIRE(!instance.is_malleable64());
BOOST_REQUIRE(!instance.is_malleable32());
BOOST_REQUIRE(!instance.is_malleable32_size(0, 0));
BOOST_REQUIRE(!instance.is_malleable32_(0, 0));
BOOST_REQUIRE(!instance.is_malleated32());
BOOST_REQUIRE(!instance.is_malleated32_(0));
}
Expand Down Expand Up @@ -299,112 +299,112 @@ BOOST_AUTO_TEST_CASE(block__is_malleable64__three_64_64_64__true)
BOOST_REQUIRE(instance.is_malleable64());
}

// is_malleable32_size
// is_malleable32

BOOST_AUTO_TEST_CASE(block__is_malleable32_size__overflow__false)
BOOST_AUTO_TEST_CASE(block__is_malleable32__overflow__false)
{
BOOST_REQUIRE(!accessor::is_malleable32_size(0, 1));
BOOST_REQUIRE(!accessor::is_malleable32_size(1, 50));
BOOST_REQUIRE(!accessor::is_malleable32_size(2, 100));
BOOST_REQUIRE(!accessor::is_malleable32_(0, 1));
BOOST_REQUIRE(!accessor::is_malleable32_(1, 50));
BOOST_REQUIRE(!accessor::is_malleable32_(2, 100));
}

BOOST_AUTO_TEST_CASE(block__is_malleable32_size__various__expected)
{
BOOST_REQUIRE(!accessor::is_malleable32_size(1, 1));

BOOST_REQUIRE(!accessor::is_malleable32_size(2, 1));
BOOST_REQUIRE(!accessor::is_malleable32_size(2, 2));

BOOST_REQUIRE(accessor::is_malleable32_size(3, 1)); // 4:1
BOOST_REQUIRE(!accessor::is_malleable32_size(3, 2));
BOOST_REQUIRE(!accessor::is_malleable32_size(3, 3));

BOOST_REQUIRE(!accessor::is_malleable32_size(4, 1));
BOOST_REQUIRE(!accessor::is_malleable32_size(4, 2));
BOOST_REQUIRE(!accessor::is_malleable32_size(4, 3));
BOOST_REQUIRE(!accessor::is_malleable32_size(4, 4));

BOOST_REQUIRE(accessor::is_malleable32_size(5, 1)); // 6:1
BOOST_REQUIRE(!accessor::is_malleable32_size(5, 2));
BOOST_REQUIRE(!accessor::is_malleable32_size(5, 3));
BOOST_REQUIRE(!accessor::is_malleable32_size(5, 4));
BOOST_REQUIRE(!accessor::is_malleable32_size(5, 5));

BOOST_REQUIRE(!accessor::is_malleable32_size(6, 1));
BOOST_REQUIRE(accessor::is_malleable32_size(6, 2)); // 8:2
BOOST_REQUIRE(!accessor::is_malleable32_size(6, 3));
BOOST_REQUIRE(!accessor::is_malleable32_size(6, 4));
BOOST_REQUIRE(!accessor::is_malleable32_size(6, 5));
BOOST_REQUIRE(!accessor::is_malleable32_size(6, 6));

BOOST_REQUIRE(accessor::is_malleable32_size(7, 1)); // 8:1
BOOST_REQUIRE(!accessor::is_malleable32_size(7, 2));
BOOST_REQUIRE(!accessor::is_malleable32_size(7, 3));
BOOST_REQUIRE(!accessor::is_malleable32_size(7, 4));
BOOST_REQUIRE(!accessor::is_malleable32_size(7, 5));
BOOST_REQUIRE(!accessor::is_malleable32_size(7, 6));
BOOST_REQUIRE(!accessor::is_malleable32_size(7, 7));

BOOST_REQUIRE(!accessor::is_malleable32_size(8, 1));
BOOST_REQUIRE(!accessor::is_malleable32_size(8, 2));
BOOST_REQUIRE(!accessor::is_malleable32_size(8, 3));
BOOST_REQUIRE(!accessor::is_malleable32_size(8, 4));
BOOST_REQUIRE(!accessor::is_malleable32_size(8, 5));
BOOST_REQUIRE(!accessor::is_malleable32_size(8, 6));
BOOST_REQUIRE(!accessor::is_malleable32_size(8, 7));
BOOST_REQUIRE(!accessor::is_malleable32_size(8, 8));

BOOST_REQUIRE(accessor::is_malleable32_size(9, 1)); // 10:1
BOOST_REQUIRE(!accessor::is_malleable32_size(9, 2));
BOOST_REQUIRE(!accessor::is_malleable32_size(9, 3));
BOOST_REQUIRE(!accessor::is_malleable32_size(9, 4));
BOOST_REQUIRE(!accessor::is_malleable32_size(9, 5));
BOOST_REQUIRE(!accessor::is_malleable32_size(9, 6));
BOOST_REQUIRE(!accessor::is_malleable32_size(9, 7));
BOOST_REQUIRE(!accessor::is_malleable32_size(9, 8));
BOOST_REQUIRE(!accessor::is_malleable32_size(9, 9));

BOOST_REQUIRE(!accessor::is_malleable32_size(10, 1));
BOOST_REQUIRE(accessor::is_malleable32_size(10, 2)); // 12:2
BOOST_REQUIRE(!accessor::is_malleable32_size(10, 3));
BOOST_REQUIRE(accessor::is_malleable32_size(11, 1)); // 12:1
BOOST_REQUIRE(!accessor::is_malleable32_size(11, 2));
BOOST_REQUIRE(!accessor::is_malleable32_size(12, 1));
BOOST_REQUIRE(!accessor::is_malleable32_size(12, 2));
BOOST_REQUIRE(!accessor::is_malleable32_size(12, 3));
BOOST_REQUIRE(accessor::is_malleable32_size(12, 4)); // 16:4
BOOST_REQUIRE(accessor::is_malleable32_size(13, 1)); // 14:1
BOOST_REQUIRE(!accessor::is_malleable32_size(13, 2));
BOOST_REQUIRE(!accessor::is_malleable32_size(14, 1));
BOOST_REQUIRE(accessor::is_malleable32_size(14, 2)); // 16:2
BOOST_REQUIRE(!accessor::is_malleable32_size(14, 3));
BOOST_REQUIRE(accessor::is_malleable32_size(15, 1)); // 16:1
BOOST_REQUIRE(!accessor::is_malleable32_size(16, 1));
BOOST_REQUIRE(accessor::is_malleable32_size(17, 1)); // 18:1
BOOST_REQUIRE(!accessor::is_malleable32_size(18, 1));
BOOST_REQUIRE(accessor::is_malleable32_size(18, 2)); // 20:2
BOOST_REQUIRE(accessor::is_malleable32_size(19, 1)); // 20:1
BOOST_REQUIRE(!accessor::is_malleable32_size(20, 1));
BOOST_REQUIRE(!accessor::is_malleable32_size(20, 2));
BOOST_REQUIRE(!accessor::is_malleable32_size(20, 3));
BOOST_REQUIRE(accessor::is_malleable32_size(20, 4)); // 24:4
BOOST_REQUIRE(accessor::is_malleable32_size(21, 1)); // 22:1
BOOST_REQUIRE(!accessor::is_malleable32_size(22, 1));
BOOST_REQUIRE(accessor::is_malleable32_size(22, 2)); // 24:2
BOOST_REQUIRE(accessor::is_malleable32_size(23, 1)); // 24:1
BOOST_REQUIRE(!accessor::is_malleable32_size(24, 1));
BOOST_REQUIRE(accessor::is_malleable32_size(25, 1)); // 26:1
BOOST_REQUIRE(!accessor::is_malleable32_size(26, 1));
BOOST_REQUIRE(accessor::is_malleable32_size(26, 2)); // 28:2
BOOST_REQUIRE(accessor::is_malleable32_size(27, 1)); // 28:1
BOOST_REQUIRE(!accessor::is_malleable32_size(28, 1));
BOOST_REQUIRE(!accessor::is_malleable32_size(28, 2));
BOOST_REQUIRE(!accessor::is_malleable32_size(28, 3));
BOOST_REQUIRE(accessor::is_malleable32_size(28, 4)); // 32:4
BOOST_REQUIRE(accessor::is_malleable32_size(29, 1)); // 30:1
BOOST_REQUIRE(!accessor::is_malleable32_size(30, 1));
BOOST_REQUIRE(accessor::is_malleable32_size(30, 2)); // 32:2
BOOST_REQUIRE(accessor::is_malleable32_size(31, 1)); // 32:1
BOOST_AUTO_TEST_CASE(block__is_malleable32__various__expected)
{
BOOST_REQUIRE(!accessor::is_malleable32_(1, 1));

BOOST_REQUIRE(!accessor::is_malleable32_(2, 1));
BOOST_REQUIRE(!accessor::is_malleable32_(2, 2));

BOOST_REQUIRE( accessor::is_malleable32_(3, 1)); // 4:1
BOOST_REQUIRE(!accessor::is_malleable32_(3, 2));
BOOST_REQUIRE(!accessor::is_malleable32_(3, 3));

BOOST_REQUIRE(!accessor::is_malleable32_(4, 1));
BOOST_REQUIRE(!accessor::is_malleable32_(4, 2));
BOOST_REQUIRE(!accessor::is_malleable32_(4, 3));
BOOST_REQUIRE(!accessor::is_malleable32_(4, 4));

BOOST_REQUIRE( accessor::is_malleable32_(5, 1)); // 6:1
BOOST_REQUIRE(!accessor::is_malleable32_(5, 2));
BOOST_REQUIRE(!accessor::is_malleable32_(5, 3));
BOOST_REQUIRE(!accessor::is_malleable32_(5, 4));
BOOST_REQUIRE(!accessor::is_malleable32_(5, 5));

BOOST_REQUIRE(!accessor::is_malleable32_(6, 1));
BOOST_REQUIRE( accessor::is_malleable32_(6, 2)); // 8:2
BOOST_REQUIRE(!accessor::is_malleable32_(6, 3));
BOOST_REQUIRE(!accessor::is_malleable32_(6, 4));
BOOST_REQUIRE(!accessor::is_malleable32_(6, 5));
BOOST_REQUIRE(!accessor::is_malleable32_(6, 6));

BOOST_REQUIRE( accessor::is_malleable32_(7, 1)); // 8:1
BOOST_REQUIRE(!accessor::is_malleable32_(7, 2));
BOOST_REQUIRE(!accessor::is_malleable32_(7, 3));
BOOST_REQUIRE(!accessor::is_malleable32_(7, 4));
BOOST_REQUIRE(!accessor::is_malleable32_(7, 5));
BOOST_REQUIRE(!accessor::is_malleable32_(7, 6));
BOOST_REQUIRE(!accessor::is_malleable32_(7, 7));

BOOST_REQUIRE(!accessor::is_malleable32_(8, 1));
BOOST_REQUIRE(!accessor::is_malleable32_(8, 2));
BOOST_REQUIRE(!accessor::is_malleable32_(8, 3));
BOOST_REQUIRE(!accessor::is_malleable32_(8, 4));
BOOST_REQUIRE(!accessor::is_malleable32_(8, 5));
BOOST_REQUIRE(!accessor::is_malleable32_(8, 6));
BOOST_REQUIRE(!accessor::is_malleable32_(8, 7));
BOOST_REQUIRE(!accessor::is_malleable32_(8, 8));

BOOST_REQUIRE( accessor::is_malleable32_(9, 1)); // 10:1
BOOST_REQUIRE(!accessor::is_malleable32_(9, 2));
BOOST_REQUIRE(!accessor::is_malleable32_(9, 3));
BOOST_REQUIRE(!accessor::is_malleable32_(9, 4));
BOOST_REQUIRE(!accessor::is_malleable32_(9, 5));
BOOST_REQUIRE(!accessor::is_malleable32_(9, 6));
BOOST_REQUIRE(!accessor::is_malleable32_(9, 7));
BOOST_REQUIRE(!accessor::is_malleable32_(9, 8));
BOOST_REQUIRE(!accessor::is_malleable32_(9, 9));

BOOST_REQUIRE(!accessor::is_malleable32_(10, 1));
BOOST_REQUIRE( accessor::is_malleable32_(10, 2)); // 12:2
BOOST_REQUIRE(!accessor::is_malleable32_(10, 3));
BOOST_REQUIRE( accessor::is_malleable32_(11, 1)); // 12:1
BOOST_REQUIRE(!accessor::is_malleable32_(11, 2));
BOOST_REQUIRE(!accessor::is_malleable32_(12, 1));
BOOST_REQUIRE(!accessor::is_malleable32_(12, 2));
BOOST_REQUIRE(!accessor::is_malleable32_(12, 3));
BOOST_REQUIRE( accessor::is_malleable32_(12, 4)); // 16:4
BOOST_REQUIRE( accessor::is_malleable32_(13, 1)); // 14:1
BOOST_REQUIRE(!accessor::is_malleable32_(13, 2));
BOOST_REQUIRE(!accessor::is_malleable32_(14, 1));
BOOST_REQUIRE( accessor::is_malleable32_(14, 2)); // 16:2
BOOST_REQUIRE(!accessor::is_malleable32_(14, 3));
BOOST_REQUIRE( accessor::is_malleable32_(15, 1)); // 16:1
BOOST_REQUIRE(!accessor::is_malleable32_(16, 1));
BOOST_REQUIRE( accessor::is_malleable32_(17, 1)); // 18:1
BOOST_REQUIRE(!accessor::is_malleable32_(18, 1));
BOOST_REQUIRE( accessor::is_malleable32_(18, 2)); // 20:2
BOOST_REQUIRE( accessor::is_malleable32_(19, 1)); // 20:1
BOOST_REQUIRE(!accessor::is_malleable32_(20, 1));
BOOST_REQUIRE(!accessor::is_malleable32_(20, 2));
BOOST_REQUIRE(!accessor::is_malleable32_(20, 3));
BOOST_REQUIRE( accessor::is_malleable32_(20, 4)); // 24:4
BOOST_REQUIRE( accessor::is_malleable32_(21, 1)); // 22:1
BOOST_REQUIRE(!accessor::is_malleable32_(22, 1));
BOOST_REQUIRE( accessor::is_malleable32_(22, 2)); // 24:2
BOOST_REQUIRE( accessor::is_malleable32_(23, 1)); // 24:1
BOOST_REQUIRE(!accessor::is_malleable32_(24, 1));
BOOST_REQUIRE( accessor::is_malleable32_(25, 1)); // 26:1
BOOST_REQUIRE(!accessor::is_malleable32_(26, 1));
BOOST_REQUIRE( accessor::is_malleable32_(26, 2)); // 28:2
BOOST_REQUIRE( accessor::is_malleable32_(27, 1)); // 28:1
BOOST_REQUIRE(!accessor::is_malleable32_(28, 1));
BOOST_REQUIRE(!accessor::is_malleable32_(28, 2));
BOOST_REQUIRE(!accessor::is_malleable32_(28, 3));
BOOST_REQUIRE( accessor::is_malleable32_(28, 4)); // 32:4
BOOST_REQUIRE( accessor::is_malleable32_(29, 1)); // 30:1
BOOST_REQUIRE(!accessor::is_malleable32_(30, 1));
BOOST_REQUIRE( accessor::is_malleable32_(30, 2)); // 32:2
BOOST_REQUIRE( accessor::is_malleable32_(31, 1)); // 32:1
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit e9e5e61

Please sign in to comment.