Skip to content

Commit

Permalink
Rem get_hash() where result copied, check malleated32 in bypass.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Jun 2, 2024
1 parent b70d518 commit 5f216f4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
2 changes: 0 additions & 2 deletions include/bitcoin/system/chain/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ class BC_API block
using hash_cref = std::reference_wrapper<const hash_digest>;
using hash_hash = unique_hash_t<>;

using unordered_map_of_constant_referenced_points =
std::unordered_map<point_cref, output::cptr, point_hash>;
using unordered_set_of_constant_referenced_points =
std::unordered_set<point_cref, point_hash>;
using unordered_set_of_constant_referenced_hashes =
Expand Down
36 changes: 23 additions & 13 deletions src/chain/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ bool block::is_hash_limit_exceeded() const NOEXCEPT
return false;

// A set is used to collapse duplicates.
unordered_set_of_constant_referenced_hashes hashes;
unordered_set_of_constant_referenced_hashes hashes{};

// Just the coinbase tx hash, skip its null input hashes.
hashes.emplace(txs_->front()->get_hash(false));
Expand Down Expand Up @@ -471,8 +471,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(malleated - mally, mally) &&
is_malleated32(mally))
if (is_malleable32(malleated - mally, mally) && is_malleated32(mally))
return mally;

return zero;
Expand Down Expand Up @@ -632,21 +631,21 @@ bool block::is_unspent_coinbase_collision() const NOEXCEPT
// Search is not ordered, forward references are caught by block.check.
void block::populate() const NOEXCEPT
{
unordered_map_of_constant_referenced_points points{};
std::unordered_map<point, output::cptr> points{};
uint32_t index{};

// Populate outputs hash table.
for (auto tx = txs_->begin(); tx != txs_->end(); ++tx, index = 0)
for (const auto& out: *(*tx)->outputs_ptr())
points.emplace(std::pair{ point{ (*tx)->get_hash(false),
index++ }, out });
points.emplace(std::pair{ point{ (*tx)->hash(false), index++ },
out });

// Populate input prevouts from hash table.
for (auto tx = txs_->begin(); tx != txs_->end(); ++tx)
{
for (const auto& in: *(*tx)->inputs_ptr())
{
const auto point = points.find(std::cref(in->point()));
const auto point = points.find(in->point());
if (point != points.end())
in->prevout = point->second;
}
Expand Down Expand Up @@ -723,15 +722,22 @@ code block::confirm_transactions(const context& ctx) const NOEXCEPT
// ----------------------------------------------------------------------------
// The block header is checked/accepted independently.

// context free.
// TODO: use of get_hash() in is_forward_reference makes this thread unsafe.
code block::check(bool bypass) const NOEXCEPT
{
if (bypass)
{
return is_invalid_merkle_root() ? error::merkle_mismatch :
error::block_success;
// type32_malleated is subset of is_internal_double_spend, assuming
// otherwise valid txs, as that will catch any duplicated transaction.
if (is_invalid_merkle_root())
return error::merkle_mismatch;
if (is_malleated32())
return error::type32_malleated;

return error::block_success;
}

// context free.
// empty_block is redundant with first_not_coinbase.
//if (is_empty())
// return error::empty_block;
Expand All @@ -756,20 +762,24 @@ code block::check(bool bypass) const NOEXCEPT
// timestamp
// median_time_past

// context required.
// TODO: use of get_hash() in is_hash_limit_exceeded makes this thread unsafe.
code block::check(const context& ctx, bool bypass) const NOEXCEPT
{
if (bypass)
{
const auto bip141 = ctx.is_enabled(bip141_rule);
return bip141 && is_invalid_witness_commitment() ?
error::invalid_witness_commitment : error::block_success;

if (bip141 && is_invalid_witness_commitment())
return error::invalid_witness_commitment;

return error::block_success;
}

const auto bip34 = ctx.is_enabled(bip34_rule);
const auto bip50 = ctx.is_enabled(bip50_rule);
const auto bip141 = ctx.is_enabled(bip141_rule);

// context required.
if (bip141 && is_overweight())
return error::block_weight_limit;
if (bip34 && is_invalid_coinbase_script(ctx.height))
Expand Down

0 comments on commit 5f216f4

Please sign in to comment.