Skip to content

Commit

Permalink
Merge pull request #482 from evoskuil/master
Browse files Browse the repository at this point in the history
Simplify genesis commit.
  • Loading branch information
evoskuil authored Jun 1, 2024
2 parents 173c72c + 1abaa4a commit f0f0570
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 37 deletions.
3 changes: 2 additions & 1 deletion include/bitcoin/database/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ enum error_t : uint8_t

/// txs archive
txs_header,
txs_txs_put
txs_txs_put,
txs_confirm
};

// No current need for error_code equivalence mapping.
Expand Down
17 changes: 12 additions & 5 deletions include/bitcoin/database/impl/query/archive.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -941,20 +941,21 @@ txs_link CLASS::set_link(const transactions& txs, const header_link& key,
size_t size) NOEXCEPT
{
txs_link out{};
return set_code(out, txs, key, size) ? txs_link{} : out;
return set_code(out, txs, key, size, false) ? txs_link{} : out;
}

TEMPLATE
code CLASS::set_code(const transactions& txs, const header_link& key,
size_t size) NOEXCEPT
size_t size, bool confirm) NOEXCEPT
{
txs_link out_fk{};
return set_code(out_fk, txs, key, size);
return set_code(out_fk, txs, key, size, confirm);
}

// protected
TEMPLATE
code CLASS::set_code(txs_link& out_fk, const transactions& txs,
const header_link& key, size_t size) NOEXCEPT
const header_link& key, size_t size, bool confirm) NOEXCEPT
{
if (key.is_terminal())
return error::txs_header;
Expand Down Expand Up @@ -994,7 +995,13 @@ code CLASS::set_code(txs_link& out_fk, const transactions& txs,
links
});

return out_fk.is_terminal() ? error::txs_txs_put : error::success;
if (out_fk.is_terminal())
return error::txs_txs_put;

if (confirm && !set_strong(key, links, true))
return error::txs_confirm;

return error::success;
// ========================================================================
}

Expand Down
42 changes: 21 additions & 21 deletions include/bitcoin/database/impl/query/confirm.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -344,23 +344,34 @@ code CLASS::block_confirmable(const header_link& link) const NOEXCEPT
return error::success;
}

// protected
TEMPLATE
bool CLASS::set_strong(const header_link& link, const tx_links& txs,
bool positive) NOEXCEPT
{
return std::all_of(txs.begin(), txs.end(), [&](const tx_link& fk) NOEXCEPT
{
// Clean allocation failure (e.g. disk full), block not confirmed.
return store_.strong_tx.put(fk, table::strong_tx::record
{
{},
link,
positive
});
});
}

TEMPLATE
bool CLASS::set_strong(const header_link& link) NOEXCEPT
{
const auto txs = to_txs(link);
if (txs.empty())
return false;

const table::strong_tx::record strong{ {}, link, true };

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

// Clean allocation failure (e.g. disk full), block not confirmed.
return std::all_of(txs.begin(), txs.end(), [&](const tx_link& fk) NOEXCEPT
{
return store_.strong_tx.put(fk, strong);
});
return set_strong(link, txs, true);
// ========================================================================
}

Expand All @@ -371,16 +382,10 @@ bool CLASS::set_unstrong(const header_link& link) NOEXCEPT
if (txs.empty())
return false;

const table::strong_tx::record strong{ {}, link, false };

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

// Clean allocation failure (e.g. disk full), block not unconfirmed.
return std::all_of(txs.begin(), txs.end(), [&](const tx_link& fk) NOEXCEPT
{
return store_.strong_tx.put(fk, strong);
});
return set_strong(link, txs, false);
// ========================================================================
}

Expand All @@ -393,18 +398,13 @@ bool CLASS::initialize(const block& genesis) NOEXCEPT
// ========================================================================
const auto scope = store_.get_transactor();

const context ctx{};
if (!set(genesis, ctx))
if (!set(genesis, context{}))
return false;

constexpr auto fees = 0u;
constexpr auto sigops = 0u;
const auto link = to_header(genesis.hash());

// Unsafe for allocation failure, but only used in store creation.
return set_strong(header_link{ 0 })
&& set_tx_connected(tx_link{ 0 }, ctx, fees, sigops) // tx valid.
&& set_block_confirmable(link, fees) // rename, block valid step.
return set_strong(link)
&& push_candidate(link)
&& push_confirmed(link);
// ========================================================================
Expand Down
12 changes: 8 additions & 4 deletions include/bitcoin/database/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,13 @@ class query
header_link set_link(const block& block, const context& ctx) NOEXCEPT;
header_link set_link(const block& block) NOEXCEPT;

txs_link set_link(const transactions& txs, const header_link& key, size_t size) NOEXCEPT;
tx_link set_link(const transaction& tx) NOEXCEPT;

code set_code(const transactions& txs, const header_link& key, size_t size) NOEXCEPT;
txs_link set_link(const transactions& txs, const header_link& key,
size_t size) NOEXCEPT;

code set_code(tx_link& out_fk, const transaction& tx) NOEXCEPT;
code set_code(const transactions& txs, const header_link& key, size_t size,
bool confirm) NOEXCEPT;

bool set_dissasociated(const header_link& key) NOEXCEPT;

Expand Down Expand Up @@ -450,7 +452,9 @@ class query

protected:
code set_code(txs_link& out_fk, const transactions& txs,
const header_link& key, size_t size) NOEXCEPT;
const header_link& key, size_t size, bool confirm) NOEXCEPT;
bool set_strong(const header_link& link, const tx_links& txs,
bool positive) NOEXCEPT;

/// Translate.
/// -----------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion src/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ DEFINE_ERROR_T_MESSAGE_MAP(error)

// tx archive
{ txs_header, "txs_header" },
{ txs_txs_put, "txs_txs_put" }
{ txs_txs_put, "txs_txs_put" },
{ txs_confirm, "txs_confirm" }
};

DEFINE_ERROR_T_CATEGORY(error, "database", "database code")
Expand Down
9 changes: 9 additions & 0 deletions test/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,4 +612,13 @@ BOOST_AUTO_TEST_CASE(error_t__code__txs_txs_put__true_exected_message)
BOOST_REQUIRE_EQUAL(ec.message(), "txs_txs_put");
}

BOOST_AUTO_TEST_CASE(error_t__code__txs_confirm__true_exected_message)
{
constexpr auto value = error::txs_confirm;
const auto ec = code(value);
BOOST_REQUIRE(ec);
BOOST_REQUIRE(ec == value);
BOOST_REQUIRE_EQUAL(ec.message(), "txs_confirm");
}

BOOST_AUTO_TEST_SUITE_END()
4 changes: 2 additions & 2 deletions test/query/extent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ BOOST_AUTO_TEST_CASE(query_extent__body_sizes__genesis__expected)
BOOST_REQUIRE_EQUAL(query.candidate_body_size(), schema::height::minrow);
BOOST_REQUIRE_EQUAL(query.confirmed_body_size(), schema::height::minrow);
BOOST_REQUIRE_EQUAL(query.strong_tx_body_size(), schema::strong_tx::minrow);
BOOST_REQUIRE_EQUAL(query.validated_tx_body_size(), schema::validated_tx::minrow);
BOOST_REQUIRE_EQUAL(query.validated_bk_body_size(), schema::validated_bk::minrow);
BOOST_REQUIRE_EQUAL(query.validated_tx_body_size(), 0u);
BOOST_REQUIRE_EQUAL(query.validated_bk_body_size(), 0u);

BOOST_REQUIRE_EQUAL(query.address_body_size(), schema::address::minrow);
BOOST_REQUIRE_EQUAL(query.neutrino_body_size(), 0u);
Expand Down
6 changes: 3 additions & 3 deletions test/query/validate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ BOOST_AUTO_TEST_CASE(query_validate__get_block_state__confirmable__block_confirm
BOOST_REQUIRE(query.set(test::block1, context{}));

uint64_t fees{};
BOOST_REQUIRE_EQUAL(query.get_header_state(0), error::block_confirmable);
BOOST_REQUIRE_EQUAL(query.get_block_state(0), error::block_confirmable);
BOOST_REQUIRE_EQUAL(query.get_block_state(fees, 0), error::block_confirmable);
BOOST_REQUIRE_EQUAL(query.get_header_state(0), error::unvalidated);
BOOST_REQUIRE_EQUAL(query.get_block_state(0), error::unvalidated);
BOOST_REQUIRE_EQUAL(query.get_block_state(fees, 0), error::unvalidated);
BOOST_REQUIRE_EQUAL(fees, 0u);

BOOST_REQUIRE(query.set_block_confirmable(1, 42));
Expand Down

0 comments on commit f0f0570

Please sign in to comment.