Skip to content

Commit

Permalink
Merge pull request #514 from evoskuil/master
Browse files Browse the repository at this point in the history
Add get_context(system::chain::context, ...).
  • Loading branch information
evoskuil committed Sep 24, 2024
2 parents e94dd03 + d602bff commit 4576f39
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
23 changes: 23 additions & 0 deletions include/bitcoin/database/impl/query/validate.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,29 @@ bool CLASS::get_context(context& ctx, const header_link& link) const NOEXCEPT
return true;
}

TEMPLATE
bool CLASS::get_context(system::chain::context& ctx,
const header_link& link) const NOEXCEPT
{
table::header::record_context header{};
if (!store_.header.get(link, header))
return false;

// Context for block/header.check and header.accept are filled from
// chain_state, not from the store.
ctx =
{
header.ctx.flags, // [block.check, block.accept & block.connect]
{}, // [block.check] timestamp
header.ctx.mtp, // [block.check, header.accept]
header.ctx.height, // [block.check & block.accept]
{}, // [header.accept] minimum_block_version
{} // [header.accept] work_required
};

return true;
}

TEMPLATE
bool CLASS::get_work(uint256_t& work, const header_link& link) const NOEXCEPT
{
Expand Down
3 changes: 3 additions & 0 deletions include/bitcoin/database/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,12 @@ class query
code get_tx_state(uint64_t& fee, size_t& sigops, const tx_link& link,
const context& ctx) const NOEXCEPT;

/// get_context(chain::context) sets only flags, median_time_past, height.
bool get_bits(uint32_t& bits, const header_link& link) const NOEXCEPT;
bool get_work(uint256_t& work, const header_link& link) const NOEXCEPT;
bool get_context(context& ctx, const header_link& link) const NOEXCEPT;
bool get_context(system::chain::context& ctx,
const header_link& link) const NOEXCEPT;
bool get_version(uint32_t& version, const header_link& link) const NOEXCEPT;
bool get_timestamp(uint32_t& timestamp,
const header_link& link) const NOEXCEPT;
Expand Down
14 changes: 14 additions & 0 deletions test/query/validate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ BOOST_AUTO_TEST_CASE(query_validate__get_context__genesis__default)
context ctx{};
BOOST_REQUIRE(query.get_context(ctx, 0));
BOOST_REQUIRE(ctx == context{});

system::chain::context chain_ctx{};
BOOST_REQUIRE(query.get_context(chain_ctx, 0));
BOOST_REQUIRE(chain_ctx == system::chain::context{});
}

BOOST_AUTO_TEST_CASE(query_validate__get_context__invalid__default)
Expand All @@ -115,6 +119,11 @@ BOOST_AUTO_TEST_CASE(query_validate__get_context__invalid__default)
BOOST_REQUIRE(!query.get_context(ctx, header_link::terminal));
BOOST_REQUIRE(!query.get_context(ctx, 1));
BOOST_REQUIRE(ctx == context{});

system::chain::context chain_ctx{};
BOOST_REQUIRE(!query.get_context(chain_ctx, header_link::terminal));
BOOST_REQUIRE(!query.get_context(chain_ctx, 1));
BOOST_REQUIRE(chain_ctx == system::chain::context{});
}

BOOST_AUTO_TEST_CASE(query_validate__get_context__block1__expected)
Expand All @@ -132,6 +141,11 @@ BOOST_AUTO_TEST_CASE(query_validate__get_context__block1__expected)
context ctx{};
BOOST_REQUIRE(query.get_context(ctx, 1));
BOOST_REQUIRE(ctx == expected);

system::chain::context chain_ctx{};
const system::chain::context chain_expected{ expected.flags, 0, expected.mtp, expected.height, 0, 0 };
BOOST_REQUIRE(query.get_context(chain_ctx, 1));
BOOST_REQUIRE(chain_ctx == chain_expected);
}

BOOST_AUTO_TEST_CASE(query_validate__get_block_state__invalid_link__unassociated)
Expand Down

0 comments on commit 4576f39

Please sign in to comment.