Skip to content

Commit

Permalink
Merge pull request #488 from evoskuil/master
Browse files Browse the repository at this point in the history
Add bypass flag to header table, queries, tests.
  • Loading branch information
evoskuil authored Jun 9, 2024
2 parents fc1931f + d3d9a19 commit feb78bb
Show file tree
Hide file tree
Showing 14 changed files with 380 additions and 267 deletions.
104 changes: 61 additions & 43 deletions include/bitcoin/database/impl/query/archive.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -131,27 +131,29 @@ inline bool CLASS::is_associated(const header_link& link) const NOEXCEPT
}

TEMPLATE
bool CLASS::set(const header& header, const chain_context& ctx) NOEXCEPT
bool CLASS::set(const header& header, const chain_context& ctx,
bool bypass) NOEXCEPT
{
return !set_link(header, ctx).is_terminal();
return !set_link(header, ctx, bypass).is_terminal();
}

TEMPLATE
bool CLASS::set(const header& header, const context& ctx) NOEXCEPT
bool CLASS::set(const header& header, const context& ctx, bool bypass) NOEXCEPT
{
return !set_link(header, ctx).is_terminal();
return !set_link(header, ctx, bypass).is_terminal();
}

TEMPLATE
bool CLASS::set(const block& block, const chain_context& ctx) NOEXCEPT
bool CLASS::set(const block& block, const chain_context& ctx,
bool bypass) NOEXCEPT
{
return !set_link(block, ctx).is_terminal();
return !set_link(block, ctx, bypass).is_terminal();
}

TEMPLATE
bool CLASS::set(const block& block, const context& ctx) NOEXCEPT
bool CLASS::set(const block& block, const context& ctx, bool bypass) NOEXCEPT
{
return !set_link(block, ctx).is_terminal();
return !set_link(block, ctx, bypass).is_terminal();
}

TEMPLATE
Expand All @@ -163,6 +165,7 @@ bool CLASS::set(const hash_digest& point_hash) NOEXCEPT
TEMPLATE
bool CLASS::set(const block& block) NOEXCEPT
{
// This sets only the txs of a block with header/context already archived.
return !set_link(block).is_terminal();
}

Expand Down Expand Up @@ -662,11 +665,14 @@ typename CLASS::inputs_ptr CLASS::get_spenders(const tx_link& link,
return get_spenders(to_output(link, output_index));
}

// set transaction
// ----------------------------------------------------------------------------

TEMPLATE
tx_link CLASS::set_link(const transaction& tx) NOEXCEPT
{
tx_link out{};
return set_code(out, tx) ? tx_link{} : out;
tx_link tx_fk{};
return set_code(tx_fk, tx) ? tx_link{} : tx_fk;
}

// The only multitable write query (except initialize/genesis).
Expand Down Expand Up @@ -855,34 +861,25 @@ code CLASS::set_code(tx_link& out_fk, const transaction& tx) NOEXCEPT
// ========================================================================
}

TEMPLATE
header_link CLASS::set_link(const block& block,
const chain_context& ctx) NOEXCEPT
{
// Map chain context into database context.
return set_link(block, context
{
system::possible_narrow_cast<context::flag::integer>(ctx.flags),
system::possible_narrow_cast<context::block::integer>(ctx.height),
ctx.median_time_past
});
}
// set header
// ----------------------------------------------------------------------------

TEMPLATE
header_link CLASS::set_link(const header& header,
const chain_context& ctx) NOEXCEPT
header_link CLASS::set_link(const header& header, const chain_context& ctx,
bool bypass) NOEXCEPT
{
// Map chain context into database context.
return set_link(header, context
{
system::possible_narrow_cast<context::flag::integer>(ctx.flags),
system::possible_narrow_cast<context::block::integer>(ctx.height),
ctx.median_time_past
});
}, bypass);
}

TEMPLATE
header_link CLASS::set_link(const header& header, const context& ctx) NOEXCEPT
header_link CLASS::set_link(const header& header, const context& ctx,
bool bypass) NOEXCEPT
{
const auto key = header.hash();

Expand All @@ -906,25 +903,46 @@ header_link CLASS::set_link(const header& header, const context& ctx) NOEXCEPT
{
{},
ctx,
bypass,
parent_fk,
header
});
// ========================================================================
}

// set block
// ----------------------------------------------------------------------------

TEMPLATE
header_link CLASS::set_link(const block& block, const context& ctx) NOEXCEPT
header_link CLASS::set_link(const block& block, const chain_context& ctx,
bool bypass) NOEXCEPT
{
const auto header_fk = set_link(block.header(), ctx);
// Map chain context into database context.
return set_link(block, context
{
system::possible_narrow_cast<context::flag::integer>(ctx.flags),
system::possible_narrow_cast<context::block::integer>(ctx.height),
ctx.median_time_past
}, bypass);
}

// Returns txs::link so translate to header::link.
if (set_link(*block.transactions_ptr(), header_fk,
block.serialized_size(true)).is_terminal())
TEMPLATE
header_link CLASS::set_link(const block& block, const context& ctx,
bool bypass) NOEXCEPT
{
const auto header_fk = set_link(block.header(), ctx, bypass);
if (header_fk.is_terminal())
return {};

return header_fk;
constexpr auto confirm = false;
const auto size = block.serialized_size(true);
return set_code(*block.transactions_ptr(), header_fk, size, confirm) ?
header_link{} : header_fk;
}

// set/unset txs
// ----------------------------------------------------------------------------

TEMPLATE
header_link CLASS::set_link(const block& block) NOEXCEPT
{
Expand All @@ -933,28 +951,27 @@ 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,
block.serialized_size(true)).is_terminal())
return {};

return header_fk;
constexpr auto confirm = false;
const auto size = block.serialized_size(true);
return set_code(*block.transactions_ptr(), header_fk, size, confirm) ?
header_link{} : header_fk;
}

TEMPLATE
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, false) ? txs_link{} : out;
constexpr auto confirm = false;
txs_link txs_fk{};
return set_code(txs_fk, txs, key, size, confirm) ? txs_link{} : txs_fk;
}

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

// protected
Expand Down Expand Up @@ -995,9 +1012,10 @@ code CLASS::set_code(txs_link& out_fk, const transactions& txs,

// ========================================================================
const auto scope = store_.get_transactor();
constexpr auto positive = true;

// Clean allocation failure (e.g. disk full), see set_strong() comments.
if (confirm && !set_strong(key, links, true))
if (confirm && !set_strong(key, links, positive))
return error::txs_confirm;

// Header link is the key for the txs table.
Expand Down
8 changes: 7 additions & 1 deletion include/bitcoin/database/impl/query/confirm.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ bool CLASS::initialize(const block& genesis) NOEXCEPT
// ========================================================================
const auto scope = store_.get_transactor();

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

const auto link = to_header(genesis.hash());
Expand All @@ -425,6 +425,9 @@ bool CLASS::initialize(const block& genesis) NOEXCEPT
TEMPLATE
bool CLASS::push_candidate(const header_link& link) NOEXCEPT
{
if (link.is_terminal())
return false;

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

Expand All @@ -437,6 +440,9 @@ bool CLASS::push_candidate(const header_link& link) NOEXCEPT
TEMPLATE
bool CLASS::push_confirmed(const header_link& link) NOEXCEPT
{
if (link.is_terminal())
return false;

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

Expand Down
17 changes: 13 additions & 4 deletions include/bitcoin/database/impl/query/validate.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ bool CLASS::get_version(uint32_t& version,
}

TEMPLATE
bool CLASS::get_bits(uint32_t& bits,
const header_link& link) const NOEXCEPT
bool CLASS::get_bits(uint32_t& bits, const header_link& link) const NOEXCEPT
{
table::header::get_bits header{};
if (!store_.header.get(link, header))
Expand All @@ -124,8 +123,7 @@ bool CLASS::get_bits(uint32_t& bits,
}

TEMPLATE
bool CLASS::get_context(context& ctx,
const header_link& link) const NOEXCEPT
bool CLASS::get_context(context& ctx, const header_link& link) const NOEXCEPT
{
table::header::record_context header{};
if (!store_.header.get(link, header))
Expand All @@ -144,6 +142,17 @@ bool CLASS::get_work(uint256_t& work, const header_link& link) const NOEXCEPT
return result;
}

TEMPLATE
bool CLASS::get_bypass(bool& bypass, const header_link& link) const NOEXCEPT
{
table::header::get_bypass header{};
if (!store_.header.get(link, header))
return false;

bypass = header.bypass;
return true;
}

////TEMPLATE
////bool CLASS::get_check_context(context& ctx, hash_digest& hash,
//// uint32_t& timestamp, const header_link& link) const NOEXCEPT
Expand Down
40 changes: 24 additions & 16 deletions include/bitcoin/database/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,10 @@ class query
inline bool is_malleable64(const header_link& link) const NOEXCEPT;
inline bool is_associated(const header_link& link) const NOEXCEPT;

bool set(const header& header, const chain_context& ctx) NOEXCEPT;
bool set(const header& header, const context& ctx) NOEXCEPT;
bool set(const block& block, const chain_context& ctx) NOEXCEPT;
bool set(const block& block, const context& ctx) NOEXCEPT;
bool set(const header& header, const chain_context& ctx, bool bypass) NOEXCEPT;
bool set(const header& header, const context& ctx, bool bypass) NOEXCEPT;
bool set(const block& block, const chain_context& ctx, bool bypass) NOEXCEPT;
bool set(const block& block, const context& ctx, bool bypass) NOEXCEPT;
bool set(const block& block) NOEXCEPT;
bool set(const hash_digest& point_hash) NOEXCEPT;
bool set(const transaction& tx) NOEXCEPT;
Expand Down Expand Up @@ -343,21 +343,28 @@ class query
input::cptr get_input(const tx_link& link, uint32_t input_index) const NOEXCEPT;
inputs_ptr get_spenders(const tx_link& link, uint32_t output_index) const NOEXCEPT;

// TODO: all except point expose idempotency guard option.
header_link set_link(const header& header, const chain_context& ctx) NOEXCEPT;
header_link set_link(const header& header, const context& ctx) NOEXCEPT;
header_link set_link(const block& block, const chain_context& ctx) NOEXCEPT;
header_link set_link(const block& block, const context& ctx) NOEXCEPT;
header_link set_link(const block& block) NOEXCEPT;

/// Set transaction.
tx_link set_link(const transaction& tx) 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;

/// Set header.
header_link set_link(const header& header, const context& ctx, bool bypass) NOEXCEPT;
header_link set_link(const header& header, const chain_context& ctx, bool bypass) NOEXCEPT;
////code set_code(header_link& out_fk, const block& block, const chain_context& ctx,
//// bool bypass) NOEXCEPT;

/// Set full block (header and all transactions).
header_link set_link(const block& block, const context& ctx, bool bypass) NOEXCEPT;
header_link set_link(const block& block, const chain_context& ctx, bool bypass) NOEXCEPT;
////code set_code(header_link& out_fk, const header& header, const chain_context& ctx,
//// bool bypass) NOEXCEPT;

/// Set txs of a block (with header/context already archived).
header_link set_link(const block& block) NOEXCEPT;
txs_link set_link(const transactions& txs, const header_link& key, size_t size) NOEXCEPT;
code set_code(const transactions& txs, const header_link& key, size_t size, bool confirm) NOEXCEPT;

/// Disassociate txs of block from its header (unset).
bool set_dissasociated(const header_link& key) NOEXCEPT;

/// Chain state.
Expand Down Expand Up @@ -390,6 +397,7 @@ class query
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_bypass(bool& bypass, const header_link& link) const NOEXCEPT;

bool set_block_confirmable(const header_link& link, uint64_t fees) NOEXCEPT;
bool set_block_valid(const header_link& link) NOEXCEPT;
Expand Down
Loading

0 comments on commit feb78bb

Please sign in to comment.