Skip to content

Commit eaab428

Browse files
authored
Merge pull request #653 from evoskuil/master
Add get_top_timestamp(bool) and tests.
2 parents 514ee1e + 953f31b commit eaab428

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

include/bitcoin/database/impl/query/validate.ipp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,18 @@ code CLASS::get_tx_state(uint64_t& fee, size_t& sigops, const tx_link& link,
204204
// Values.
205205
// ----------------------------------------------------------------------------
206206

207+
TEMPLATE
208+
uint32_t CLASS::get_top_timestamp(bool confirmed) const NOEXCEPT
209+
{
210+
const auto top = confirmed ? to_confirmed(get_top_confirmed()) :
211+
to_candidate(get_top_candidate());
212+
213+
// returns zero if read fails.
214+
uint32_t timestamp{};
215+
/* bool */ get_timestamp(timestamp, top);
216+
return timestamp;
217+
}
218+
207219
TEMPLATE
208220
bool CLASS::get_timestamp(uint32_t& timestamp,
209221
const header_link& link) const NOEXCEPT

include/bitcoin/database/query.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ class query
464464

465465
/// Values.
466466
// get_context(chain::context) sets only flags, median_time_past, height.
467+
uint32_t get_top_timestamp(bool confirmed) const NOEXCEPT;
467468
bool get_timestamp(uint32_t& timestamp, const header_link& link) const NOEXCEPT;
468469
bool get_version(uint32_t& version, const header_link& link) const NOEXCEPT;
469470
bool get_work(uint256_t& work, const header_link& link) const NOEXCEPT;

test/query/validate.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,31 @@ BOOST_FIXTURE_TEST_SUITE(query_validate_tests, query_validate_setup_fixture)
4343
// nop event handler.
4444
const auto events_handler = [](auto, auto) {};
4545

46+
BOOST_AUTO_TEST_CASE(query_validate__get_top_timestamp__always__expected)
47+
{
48+
settings settings{};
49+
settings.path = TEST_DIRECTORY;
50+
test::chunk_store store{ settings };
51+
test::query_accessor query{ store };
52+
BOOST_REQUIRE_EQUAL(store.create(events_handler), error::success);
53+
BOOST_REQUIRE(query.initialize(test::genesis));
54+
BOOST_REQUIRE(query.set(test::block1, context{}, false, false));
55+
BOOST_REQUIRE(query.set(test::block2, context{}, false, false));
56+
BOOST_REQUIRE(query.set(test::block3, context{}, false, false));
57+
BOOST_REQUIRE(query.push_candidate(1));
58+
BOOST_REQUIRE_EQUAL(query.get_top_timestamp(true), 0x495fab29_u32);
59+
BOOST_REQUIRE_EQUAL(query.get_top_timestamp(false), 0x4966bc61_u32);
60+
BOOST_REQUIRE(query.push_candidate(2));
61+
BOOST_REQUIRE_EQUAL(query.get_top_timestamp(true), 0x495fab29_u32);
62+
BOOST_REQUIRE_EQUAL(query.get_top_timestamp(false), 0x4966bcb0_u32);
63+
BOOST_REQUIRE(query.push_confirmed(1, false));
64+
BOOST_REQUIRE_EQUAL(query.get_top_timestamp(true), 0x4966bc61_u32);
65+
BOOST_REQUIRE_EQUAL(query.get_top_timestamp(false), 0x4966bcb0_u32);
66+
BOOST_REQUIRE(query.push_confirmed(2, false));
67+
BOOST_REQUIRE_EQUAL(query.get_top_timestamp(true), 0x4966bcb0_u32);
68+
BOOST_REQUIRE_EQUAL(query.get_top_timestamp(false), 0x4966bcb0_u32);
69+
}
70+
4671
BOOST_AUTO_TEST_CASE(query_validate__get_timestamp__genesis__expected)
4772
{
4873
settings settings{};

0 commit comments

Comments
 (0)