Skip to content

Commit

Permalink
Merge pull request #1463 from evoskuil/master
Browse files Browse the repository at this point in the history
Replace !is_coinbase() assertions with runtime bypass.
  • Loading branch information
evoskuil authored May 27, 2024
2 parents 267ea9b + 232e934 commit c145a5b
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 16 deletions.
3 changes: 2 additions & 1 deletion include/bitcoin/system/chain/header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ class BC_API header
uint32_t nonce_;
bool valid_;

// Identity hash cashing.
// Identity hash caching.
// TODO: use std::optional to avoid this pointer allocation.
mutable std::shared_ptr<const hash_digest> hash_{};
};

Expand Down
5 changes: 3 additions & 2 deletions include/bitcoin/system/chain/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,9 @@ class BC_API transaction

void initialize_hash_cache() const NOEXCEPT;

// Signature and identity hash cashing (witness hash if witnessed).
mutable std::unique_ptr<hash_cache> cache_{};
// TODO: use std::optional to avoid these pointer allocations (0.16%).
// Signature and identity hash caching (witness hash if witnessed).
mutable std::unique_ptr<const hash_cache> cache_{};
mutable std::unique_ptr<const hash_digest> nominal_hash_{};
mutable std::unique_ptr<const hash_digest> witness_hash_{};
};
Expand Down
2 changes: 2 additions & 0 deletions src/chain/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ hash_digest block::hash() const NOEXCEPT
return header_->hash();
}

// TODO: this is expensive.
size_t block::serialized_size(bool witness) const NOEXCEPT
{
// Overflow returns max_size_t.
Expand Down Expand Up @@ -350,6 +351,7 @@ size_t block::non_coinbase_inputs() const NOEXCEPT
return std::accumulate(std::next(txs_->begin()), txs_->end(), zero, inputs);
}

// TODO: this is expensive (1.85%).
// This also precludes the block merkle calculation DoS exploit.
// bitcointalk.org/?topic=102395
bool block::is_internal_double_spend() const NOEXCEPT
Expand Down
1 change: 1 addition & 0 deletions src/chain/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ void input::to_data(writer& sink) const NOEXCEPT
sink.write_4_bytes_little_endian(sequence_);
}

// TODO: this is expensive.
size_t input::serialized_size(bool witness) const NOEXCEPT
{
// input.serialized_size(witness) provides sizing for witness, however
Expand Down
1 change: 1 addition & 0 deletions src/chain/operation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ const chunk_cptr& operation::data_ptr() const NOEXCEPT
return data_;
}

// TODO: this is expensive.
size_t operation::serialized_size() const NOEXCEPT
{
static constexpr auto op_size = sizeof(uint8_t);
Expand Down
1 change: 1 addition & 0 deletions src/chain/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ void output::to_data(writer& sink) const NOEXCEPT
script_->to_data(sink, true);
}

// TODO: this is expensive.
size_t output::serialized_size() const NOEXCEPT
{
return sizeof(value_) + script_->serialized_size(true);
Expand Down
2 changes: 2 additions & 0 deletions src/chain/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ size_t script::op_count(reader& source) NOEXCEPT
const auto start = source.get_read_position();
auto count = zero;

// TODO: this is expensive (0.83%).
while (operation::count_op(source))
++count;

Expand Down Expand Up @@ -358,6 +359,7 @@ hash_digest script::hash() const NOEXCEPT
return sha256;
}

// TODO: this is expensive.
size_t script::serialized_size(bool prefix) const NOEXCEPT
{
const auto op_size = [](size_t total, const operation& op) NOEXCEPT
Expand Down
25 changes: 13 additions & 12 deletions src/chain/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ void transaction::to_data(writer& sink, bool witness) const NOEXCEPT
sink.write_4_bytes_little_endian(locktime_);
}

// TODO: this is expensive.
size_t transaction::serialized_size(bool witness) const NOEXCEPT
{
witness &= segregated_;
Expand Down Expand Up @@ -1281,14 +1282,14 @@ code transaction::check(const context& ctx) const NOEXCEPT
return error::transaction_success;
}

// Do NOT invoke on coinbase.
// Do not need to invoke on coinbase.
// This assumes that prevout caching is completed on all inputs.
code transaction::accept(const context&) const NOEXCEPT
{
BC_ASSERT(!is_coinbase());
////BC_ASSERT(!is_coinbase());

////if (is_coinbase())
//// return error::transaction_success;
if (is_coinbase())
return error::transaction_success;
if (is_missing_prevouts())
return error::missing_previous_output;
if (is_overspent())
Expand All @@ -1301,16 +1302,16 @@ code transaction::accept(const context&) const NOEXCEPT
// height
// median_time_past

// Do NOT invoke on coinbase.
// Do not need to invoke on coinbase.
// Node performs these checks through database query.
// This assumes that prevout and metadata caching are completed on all inputs.
code transaction::confirm(const context& ctx) const NOEXCEPT
{
BC_ASSERT(!is_coinbase());
////BC_ASSERT(!is_coinbase());
const auto bip68 = ctx.is_enabled(bip68_rule);

////if (is_coinbase())
//// return error::transaction_success;
if (is_coinbase())
return error::transaction_success;
if (bip68 && is_locked(ctx.height, ctx.median_time_past))
return error::relative_time_locked;
if (is_immature(ctx.height))
Expand All @@ -1328,13 +1329,13 @@ code transaction::confirm(const context& ctx) const NOEXCEPT

// forks

// Do NOT invoke on coinbase.
// Do not need to invoke on coinbase.
code transaction::connect(const context& ctx) const NOEXCEPT
{
BC_ASSERT(!is_coinbase());
////BC_ASSERT(!is_coinbase());

////if (is_coinbase())
//// return error::transaction_success;
if (is_coinbase())
return error::transaction_success;

code ec;
using namespace machine;
Expand Down
1 change: 1 addition & 0 deletions src/chain/witness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ size_t witness::serialized_size() const NOEXCEPT
return std::accumulate(stack_.begin(), stack_.end(), zero, sum);
}

// TODO: this is expensive.
size_t witness::serialized_size(bool prefix) const NOEXCEPT
{
// Witness prefix is an element count, not a byte length (unlike script).
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/neutrino_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ bool compute_filter(data_chunk& out, const chain::block& block) NOEXCEPT
const auto& script = output->script();

// bip138: any "nil" items MUST NOT be included.
// bip138:exclude all outputs that start with OP_RETURN.
// bip138: exclude all outputs that start with OP_RETURN.
if (!script.ops().empty() &&
!chain::script::is_pay_op_return_pattern(script.ops()))
scripts.push_back(script.to_data(false));
Expand Down

0 comments on commit c145a5b

Please sign in to comment.