Skip to content

Commit

Permalink
Merge d97cf40 into 2683067
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Mar 6, 2024
2 parents 2683067 + d97cf40 commit 5c52b78
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 36 deletions.
2 changes: 2 additions & 0 deletions include/bitcoin/database/association.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
#include <bitcoin/system.hpp>
#include <bitcoin/database/boost.hpp>
#include <bitcoin/database/define.hpp>
#include <bitcoin/database/tables/tables.hpp>

namespace libbitcoin {
namespace database {

/// Association between block hash and context.
struct association
{
table::height::link link;
system::hash_digest hash;
system::chain::context context;

Expand Down
48 changes: 24 additions & 24 deletions include/bitcoin/database/impl/query/archive.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -694,26 +694,12 @@ TEMPLATE
header_link CLASS::set_link(const block& block, const context& ctx) NOEXCEPT
{
const auto header_fk = set_link(block.header(), ctx);
if (header_fk.is_terminal())
return {};

// GUARDED (block (txs) redundancy)
// This guard is only effective if there is a single database thread.
if (is_associated(header_fk))
return header_fk;

tx_links links{};
links.reserve(block.transactions_ptr()->size());
for (const auto& tx: *block.transactions_ptr())
if (!push_link_value(links, set_link(*tx)))
return {};

// ========================================================================
const auto scope = store_.get_transactor();
// Returns txs::link so translate to header::link.
if (set_link(*block.transactions_ptr(), header_fk).is_terminal())
return {};

return store_.txs.put(header_fk, table::txs::slab{ {}, links }) ?
header_fk : table::header::link{};
// ========================================================================
return header_fk;
}

TEMPLATE
Expand All @@ -724,22 +710,36 @@ header_link CLASS::set_link(const block& block) NOEXCEPT
if (header_fk.is_terminal())
return {};

// Returns txs::link so translate to header::link.
if (set_link(*block.transactions_ptr(), header_fk).is_terminal())
return {};

return header_fk;
}

TEMPLATE
txs_link CLASS::set_link(const transactions& txs,
const header_link& link) NOEXCEPT
{
if (link.is_terminal())
return{};

// GUARDED (block (txs) redundancy)
// This guard is only effective if there is a single database thread.
if (is_associated(header_fk))
return header_fk;
const auto txs_link = to_txs_link(link);
if (!txs_link.is_terminal())
return txs_link;

tx_links links{};
links.reserve(block.transactions_ptr()->size());
for (const auto& tx: *block.transactions_ptr())
links.reserve(txs.size());
for (const auto& tx: txs)
if (!push_link_value(links, set_link(*tx)))
return {};

// ========================================================================
const auto scope = store_.get_transactor();

return store_.txs.put(header_fk, table::txs::slab{ {}, links }) ?
header_fk : table::header::link{};
return store_.txs.put_link(link, table::txs::slab{ {}, links });
// ========================================================================
}

Expand Down
11 changes: 1 addition & 10 deletions include/bitcoin/database/impl/query/initialize.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ associations CLASS::get_all_unassociated_above(size_t height) const NOEXCEPT
table::header::get_check_context context{};
if (store_.header.get(header_fk, context))
{
// boost::multi_index_container
out.insert(association
{
header_fk,
context.key,
system::chain::context
{
Expand All @@ -106,15 +106,6 @@ associations CLASS::get_all_unassociated_above(size_t height) const NOEXCEPT
system::possible_wide_cast<size_t>(context.ctx.height)
}
});

// std::unordered_map
////out.emplace(context.key, system::chain::context
////{
//// context.ctx.flags,
//// context.timestamp,
//// context.ctx.mtp,
//// system::possible_wide_cast<size_t>(context.ctx.height)
////});
}
}
}
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 @@ -252,6 +252,7 @@ class query
header_link set_link(const block& block, const context& ctx) NOEXCEPT;
header_link set_link(const block& block) NOEXCEPT;
point_link set_link(const hash_digest& point_hash) NOEXCEPT;
txs_link set_link(const transactions& txs, const header_link& link) NOEXCEPT;
tx_link set_link(const transaction& tx) NOEXCEPT;

/// Chain state.
Expand Down
4 changes: 2 additions & 2 deletions test/query/initialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,15 @@ BOOST_AUTO_TEST_CASE(query_initialize__get_all_unassociated_above__gapped_candid

const auto it2 = unassociated0.find(test::block2.hash());
BOOST_REQUIRE(it2 != unassociated0.end());
BOOST_REQUIRE(it2 != unassociated0.end());
BOOST_REQUIRE_EQUAL(it2->link, 2u);
BOOST_REQUIRE_EQUAL(it2->context.forks, context2.flags);
BOOST_REQUIRE_EQUAL(it2->context.timestamp, test::block2.header().timestamp());
BOOST_REQUIRE_EQUAL(it2->context.median_time_past, context2.mtp);
BOOST_REQUIRE_EQUAL(it2->context.height, context2.height);

const auto it3 = unassociated0.find(test::block3.hash());
BOOST_REQUIRE(it3 != unassociated0.end());
BOOST_REQUIRE(it3 != unassociated0.end());
BOOST_REQUIRE_EQUAL(it3->link, 3u);
BOOST_REQUIRE_EQUAL(it3->context.forks, context3.flags);
BOOST_REQUIRE_EQUAL(it3->context.timestamp, test::block3.header().timestamp());
BOOST_REQUIRE_EQUAL(it3->context.median_time_past, context3.mtp);
Expand Down

0 comments on commit 5c52b78

Please sign in to comment.