Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions include/bitcoin/database/impl/query/validate.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@ code CLASS::get_tx_state(uint64_t& fee, size_t& sigops, const tx_link& link,
// Values.
// ----------------------------------------------------------------------------

TEMPLATE
uint32_t CLASS::get_top_timestamp(bool confirmed) const NOEXCEPT
{
const auto top = confirmed ? to_confirmed(get_top_confirmed()) :
to_candidate(get_top_candidate());

// returns zero if read fails.
uint32_t timestamp{};
/* bool */ get_timestamp(timestamp, top);
return timestamp;
}

TEMPLATE
bool CLASS::get_timestamp(uint32_t& timestamp,
const header_link& link) const NOEXCEPT
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/database/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ class query

/// Values.
// get_context(chain::context) sets only flags, median_time_past, height.
uint32_t get_top_timestamp(bool confirmed) const NOEXCEPT;
bool get_timestamp(uint32_t& timestamp, const header_link& link) const NOEXCEPT;
bool get_version(uint32_t& version, const header_link& link) const NOEXCEPT;
bool get_work(uint256_t& work, const header_link& link) const NOEXCEPT;
Expand Down
25 changes: 25 additions & 0 deletions test/query/validate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,31 @@ BOOST_FIXTURE_TEST_SUITE(query_validate_tests, query_validate_setup_fixture)
// nop event handler.
const auto events_handler = [](auto, auto) {};

BOOST_AUTO_TEST_CASE(query_validate__get_top_timestamp__always__expected)
{
settings settings{};
settings.path = TEST_DIRECTORY;
test::chunk_store store{ settings };
test::query_accessor query{ store };
BOOST_REQUIRE_EQUAL(store.create(events_handler), error::success);
BOOST_REQUIRE(query.initialize(test::genesis));
BOOST_REQUIRE(query.set(test::block1, context{}, false, false));
BOOST_REQUIRE(query.set(test::block2, context{}, false, false));
BOOST_REQUIRE(query.set(test::block3, context{}, false, false));
BOOST_REQUIRE(query.push_candidate(1));
BOOST_REQUIRE_EQUAL(query.get_top_timestamp(true), 0x495fab29_u32);
BOOST_REQUIRE_EQUAL(query.get_top_timestamp(false), 0x4966bc61_u32);
BOOST_REQUIRE(query.push_candidate(2));
BOOST_REQUIRE_EQUAL(query.get_top_timestamp(true), 0x495fab29_u32);
BOOST_REQUIRE_EQUAL(query.get_top_timestamp(false), 0x4966bcb0_u32);
BOOST_REQUIRE(query.push_confirmed(1, false));
BOOST_REQUIRE_EQUAL(query.get_top_timestamp(true), 0x4966bc61_u32);
BOOST_REQUIRE_EQUAL(query.get_top_timestamp(false), 0x4966bcb0_u32);
BOOST_REQUIRE(query.push_confirmed(2, false));
BOOST_REQUIRE_EQUAL(query.get_top_timestamp(true), 0x4966bcb0_u32);
BOOST_REQUIRE_EQUAL(query.get_top_timestamp(false), 0x4966bcb0_u32);
}

BOOST_AUTO_TEST_CASE(query_validate__get_timestamp__genesis__expected)
{
settings settings{};
Expand Down
Loading