Skip to content

Commit

Permalink
Bypass metadata population when filter with no validate.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Jan 13, 2025
1 parent 37f0680 commit 0185357
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 37 deletions.
9 changes: 8 additions & 1 deletion include/bitcoin/node/chasers/chaser_validate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ class BCN_API chaser_validate
virtual void do_checked(height_t height) NOEXCEPT;
virtual void do_bump(height_t height) NOEXCEPT;

virtual void validate_block(const database::header_link& link) NOEXCEPT;
virtual void validate_block(const database::header_link& link,
bool bypass) NOEXCEPT;
virtual code validate(bool bypass, const system::chain::block& block,
const database::header_link& link,
const system::chain::context& ctx) NOEXCEPT;
virtual code populate(bool bypass, const system::chain::block& block,
const system::chain::context& ctx) NOEXCEPT;
virtual void complete_block(const code& ec,
const database::header_link& link, size_t height) NOEXCEPT;

Expand All @@ -77,6 +83,7 @@ class BCN_API chaser_validate
const uint64_t initial_subsidy_;
const size_t maximum_backlog_;
const bool concurrent_;
const bool filter_;
};

} // namespace node
Expand Down
2 changes: 0 additions & 2 deletions include/bitcoin/node/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ enum error_t : uint8_t
validate5,
validate6,
validate7,
validate8,
validate9,
confirm1,
confirm2,
confirm3,
Expand Down
94 changes: 62 additions & 32 deletions src/chasers/chaser_validate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ chaser_validate::chaser_validate(full_node& node) NOEXCEPT
subsidy_interval_(node.config().bitcoin.subsidy_interval_blocks),
initial_subsidy_(node.config().bitcoin.initial_subsidy()),
maximum_backlog_(node.config().node.maximum_concurrency_()),
concurrent_(node.config().node.concurrent_validation)
concurrent_(node.config().node.concurrent_validation),
filter_(node.archive().neutrino_enabled())
{
}

Expand Down Expand Up @@ -187,20 +188,24 @@ void chaser_validate::do_bump(height_t) NOEXCEPT

set_position(height);

if ((ec == database::error::block_valid) ||
const auto bypass =
(ec == database::error::block_valid) ||
(ec == database::error::block_confirmable) ||
is_under_checkpoint(height) || query.is_milestone(link))
is_under_checkpoint(height) || query.is_milestone(link);

if (bypass && !filter_)
{
complete_block(error::success, link, height);
continue;
}

PARALLEL(validate_block, link);
PARALLEL(validate_block, link, bypass);
}
}

// Unstranded (concurrent by block).
void chaser_validate::validate_block(const header_link& link) NOEXCEPT
void chaser_validate::validate_block(const header_link& link,
bool bypass) NOEXCEPT
{
if (closed())
return;
Expand All @@ -218,40 +223,19 @@ void chaser_validate::validate_block(const header_link& link) NOEXCEPT
{
ec = error::validate2;
}
else if (!block->populate(ctx))
else if ((ec = populate(bypass, *block, ctx)))
{
ec = system::error::relative_time_locked;
if (!query.set_block_unconfirmable(link))
ec = error::validate3;
}
else if (!query.populate(*block))
else if ((ec = validate(bypass, *block, link, ctx)))
{
ec = system::error::missing_previous_output;
if (!query.set_block_unconfirmable(link))
ec = error::validate4;
}
else if ((ec = block->accept(ctx, subsidy_interval_, initial_subsidy_)))
{
if (!query.set_block_unconfirmable(link))
ec = error::validate5;
}
else if ((ec = block->connect(ctx)))
{
if (!query.set_block_unconfirmable(link))
ec = error::validate6;
}
else if (!query.set_block_valid(link, block->fees()))
{
ec = error::validate7;
}
else if (!query.set_prevouts(link, *block))
{
ec = error::validate8;
}
else if (!query.set_filter_body(link, *block))
{
// TODO: this should not bypass checkpoint/milestone if enabled.
ec = error::validate9;
ec = error::validate5;
}
else
{
Expand All @@ -262,6 +246,54 @@ void chaser_validate::validate_block(const header_link& link) NOEXCEPT
POST(complete_block, ec, link, ctx.height);
}

code chaser_validate::populate(bool bypass, const system::chain::block& block,
const system::chain::context& ctx) NOEXCEPT
{
const auto& query = archive();

// Relative locktime check is unnecessary under bypass, but cheap.
if (!block.populate(ctx))
return system::error::relative_time_locked;

if (bypass)
{
if (!query.populate_without_metadata(block))
return system::error::missing_previous_output;
}
else
{
if (!query.populate(block))
return system::error::missing_previous_output;
}

return error::success;
}

code chaser_validate::validate(bool bypass, const system::chain::block& block,
const database::header_link& link,
const system::chain::context& ctx) NOEXCEPT
{
code ec{};
if (bypass)
return ec;

auto& query = archive();

if ((ec = block.accept(ctx, subsidy_interval_, initial_subsidy_)))
return ec;

if ((ec = block.connect(ctx)))
return ec;

if (!query.set_block_valid(link, block.fees()))
return error::validate6;

if (!query.set_prevouts(link, block))
return error::validate7;

return ec;
}

void chaser_validate::complete_block(const code& ec, const header_link& link,
size_t height) NOEXCEPT
{
Expand All @@ -279,9 +311,7 @@ void chaser_validate::complete_block(const code& ec, const header_link& link,
ec == error::validate4 ||
ec == error::validate5 ||
ec == error::validate6 ||
ec == error::validate7 ||
ec == error::validate8 ||
ec == error::validate9)
ec == error::validate7)
{
fault(ec);
return;
Expand Down
2 changes: 0 additions & 2 deletions src/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ DEFINE_ERROR_T_MESSAGE_MAP(error)
{ validate5, "validate5" },
{ validate6, "validate6" },
{ validate7, "validate7" },
{ validate8, "validate8" },
{ validate9, "validate9" },
{ confirm1, "confirm1" },
{ confirm2, "confirm2" },
{ confirm3, "confirm3" },
Expand Down

0 comments on commit 0185357

Please sign in to comment.