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
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

# gnu
.deps
.dirstamp
Makefile
Makefile.in
.libs
Expand All @@ -28,7 +29,10 @@ builds/gnu/build-aux
builds/gnu/config.*
builds/gnu/configure
builds/gnu/libtool
builds/gnu/.dirstamp
builds/gnu/console
builds/gnu/test
builds/gnu/*.log
builds/gnu/*.trs

# pkg-config pc file from .pc.in
builds/gnu/*.pc
Expand Down
16 changes: 16 additions & 0 deletions include/bitcoin/database/impl/query/envelope_state.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ size_t CLASS::get_latch_tx() const NOEXCEPT
return store_.get_latch_tx();
}

TEMPLATE
header_link CLASS::get_pool_top() const NOEXCEPT
{
return store_.get_pool_top();
}

TEMPLATE
bool CLASS::set_pool_top(const header_link& link) NOEXCEPT
{
// ========================================================================
const auto scope = get_transactor();

return store_.set_pool_top(link);
// ========================================================================
}

TEMPLATE
bool CLASS::get_activation(size_t& out, uint32_t flag) const NOEXCEPT
{
Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/database/impl/query/pool/pool_index.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ bool CLASS::set_pool_block(const header_link& link, bool confirmed) NOEXCEPT

const auto count = store_.pool.count().value;
if (is_zero(count))
return true;
return !confirmed || store_.set_pool_top(link);

table::txs::get_txs txs{};
if (!store_.txs.at(to_txs(link), txs) || txs.tx_fks.empty())
Expand Down Expand Up @@ -436,7 +436,7 @@ bool CLASS::set_pool_block(const header_link& link, bool confirmed) NOEXCEPT
if (!complete)
system::pool::label(columns);

return true;
return !confirmed || store_.set_pool_top(link);
// ========================================================================
}

Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/database/impl/query/pool/pool_suffice.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ bool CLASS::set_pool_unvalidated(size_t& out,
}
}

return true;
return store_.set_pool_top(to_parent(link));
// ========================================================================
}

Expand Down
19 changes: 19 additions & 0 deletions include/bitcoin/database/impl/store/store.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,25 @@ void CLASS::set_pooling() NOEXCEPT
set_dirty();
}

TEMPLATE
header_link CLASS::get_pool_top() const NOEXCEPT
{
return pool_top_.load(std::memory_order_relaxed);
}

TEMPLATE
bool CLASS::set_pool_top(const header_link& link) NOEXCEPT
{
envelope_.pool_top = link.value;
pool_top_.store(link.value, std::memory_order_relaxed);

using integer = table::envelope::link::integer;
const auto position = envelope_.pool_top_offset();
const auto offset = system::possible_narrow_cast<integer>(position);
const table::envelope::put_pool_top record{ link.value };
return envelope.put(offset, record);
}

TEMPLATE
database::envelope::activation CLASS::get_activation(
size_t index) const NOEXCEPT
Expand Down
5 changes: 5 additions & 0 deletions include/bitcoin/database/impl/store/store_open.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ code CLASS::migrate(system::config::version& original,
ec = migrate_4_0_32_to_4_0_33(handler);
schema = { 4, 0, 33, 0 };
}
else if (schema == system::config::version{ 4, 0, 33, 0 })
{
schema = { 4, 0, 34, 0 };
}
else
{
ec = error::schema_version;
Expand Down Expand Up @@ -803,6 +807,7 @@ code CLASS::load_envelope() NOEXCEPT
// The stored latch governs, it is one-way and survives the session.
scan_frontier_.store(envelope_.scan_frontier, std::memory_order_relaxed);
latch_tx_.store(envelope_.latch_tx, std::memory_order_relaxed);
pool_top_.store(envelope_.pool_top, std::memory_order_relaxed);
for (size_t index{}; index < activation_count; ++index)
{
const auto& activation = envelope_.activations.at(index);
Expand Down
4 changes: 4 additions & 0 deletions include/bitcoin/database/impl/store/store_restore.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ code CLASS::restore(const event_handler& handler) NOEXCEPT
dropped(ec, spends_index, table_t::spends_index_table);
dropped(ec, wtxid, table_t::wtxid_table);

// Dropped pool has no top.
if (!ec && !set_pool_top(header_link::terminal))
ec = error::restore_table;

restore(ec, filter_bk, table_t::filter_bk_table);
restore(ec, filter_tx, table_t::filter_tx_table);

Expand Down
8 changes: 6 additions & 2 deletions include/bitcoin/database/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ class query
size_t get_scan_frontier() const NOEXCEPT;
size_t get_latch_tx() const NOEXCEPT;

/// Last block applied to pool, terminal if none.
header_link get_pool_top() const NOEXCEPT;
bool set_pool_top(const header_link& link) NOEXCEPT;

/// Height at which forks of flag activated, if confirmed active.
bool get_activation(size_t& out, uint32_t flag) const NOEXCEPT;

Expand Down Expand Up @@ -790,7 +794,7 @@ class query
/// Rebuild pool index heads from pool (after compaction).
bool set_pool_index() NOEXCEPT;

/// Apply confirmation (or reorganization) of block to index.
/// Apply confirmation (pool top) or reorganization of block to index.
bool set_pool_block(const header_link& link, bool confirmed) NOEXCEPT;

/// Apply pool context to index, out rows it does not satisfy.
Expand All @@ -800,7 +804,7 @@ class query
bool set_pooled_context(const tx_link& link, const context& ctx,
size_t sigops) NOEXCEPT;

/// Set each pooled spender of block's txs not validated, out count.
/// Set pooled spenders of block's txs not validated, parent pool top.
bool set_pool_unvalidated(size_t& out, const header_link& link) NOEXCEPT;

/// Coverage.
Expand Down
5 changes: 5 additions & 0 deletions include/bitcoin/database/store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ class store
size_t get_scan_frontier() const NOEXCEPT;
size_t get_latch_tx() const NOEXCEPT;

/// Last block applied to pool, terminal if none (set under transactor).
header_link get_pool_top() const NOEXCEPT;
bool set_pool_top(const header_link& link) NOEXCEPT;

/// Fork activation slot, flags activated together and their header.
database::envelope::activation get_activation(size_t index) const NOEXCEPT;
bool set_activation(size_t index, uint32_t flags,
Expand Down Expand Up @@ -356,6 +360,7 @@ class store
stopper pooling_{ false };
std::atomic<uint32_t> scan_frontier_{};
std::atomic<uint32_t> latch_tx_{};
std::atomic<header_link::integer> pool_top_{ header_link::terminal };
std::array<std::atomic<uint32_t>, activation_count> activation_flags_{};
std::array<activation_link, activation_count> activation_links_{};

Expand Down
20 changes: 20 additions & 0 deletions include/bitcoin/database/tables/caches/envelope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ struct envelope
uint32_t latch_tx{};
};

struct put_pool_top
{
static constexpr size_t size = max_size_t;

inline link count() const NOEXCEPT
{
return system::possible_narrow_cast<link::integer>(
database::envelope::pool_top_size);
}

inline bool to_data(flipper& sink) const NOEXCEPT
{
using header = schema::header::link;
sink.write_little_endian<header::integer, header::size>(header_fk);
return sink;
}

schema::header::link::integer header_fk{};
};

struct put_activation
{
static constexpr size_t size = max_size_t;
Expand Down
6 changes: 6 additions & 0 deletions include/bitcoin/database/tables/envelope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ struct BCD_API envelope
/// Migrate a serialized 4.0.32 envelope to 4.0.33.
static bool migrate_4_0_32_to_4_0_33(system::data_chunk& data) NOEXCEPT;

/// Migrate a serialized 4.0.33 envelope to 4.0.34.
static bool migrate_4_0_33_to_4_0_34(system::data_chunk& data) NOEXCEPT;

/// Migrate serialized migratable envelope to next schema, taking values
/// prior schema does not store from configured.
static bool migrate_step(system::data_chunk& data,
Expand All @@ -77,6 +80,7 @@ struct BCD_API envelope
schema::header::pk;
static constexpr size_t pooling_size = sizeof(uint8_t) +
(2 * sizeof(uint32_t));
static constexpr size_t pool_top_size = schema::header::pk;

envelope() NOEXCEPT;
envelope(const system::settings& bitcoin, const settings& database,
Expand All @@ -96,6 +100,7 @@ struct BCD_API envelope
size_t serialized_size() const NOEXCEPT;

size_t pooling_offset() const NOEXCEPT;
size_t pool_top_offset() const NOEXCEPT;
size_t activation_offset(size_t index) const NOEXCEPT;
bool operator==(const envelope& other) const NOEXCEPT = default;

Expand Down Expand Up @@ -169,6 +174,7 @@ struct BCD_API envelope
bool pooling{};
uint32_t scan_frontier{};
uint32_t latch_tx{};
schema::header::link::integer pool_top{ schema::header::link::terminal };
std::array<activation, activation_count> activations{};
};

Expand Down
3 changes: 2 additions & 1 deletion include/bitcoin/database/tables/schema.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ namespace schema {
/// 4.0.31.0 Pool flags, heights and medians columns (from the pool record).
/// 4.0.32.0 Require witness in the envelope.
/// 4.0.33.0 Pruned flags merged into the tx and txs heavy sizes.
constexpr std::array<uint32_t, 4> version{ 4, 0, 33, 0 };
/// 4.0.34.0 Pool top in the envelope.
constexpr std::array<uint32_t, 4> version{ 4, 0, 34, 0 };

/// The oldest store schema migrated to version.
constexpr std::array<uint32_t, 4> migratable{ 4, 0, 30, 0 };
Expand Down
28 changes: 27 additions & 1 deletion src/tables/envelope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,21 @@ bool envelope::migrate_4_0_32_to_4_0_33(data_chunk& data) NOEXCEPT
return true;
}

// pool_top precedes activations.
bool envelope::migrate_4_0_33_to_4_0_34(data_chunk& data) NOEXCEPT
{
constexpr size_t link = 3;
constexpr size_t activations = 32 * (sizeof(uint32_t) + link);
constexpr data_array<link> terminal{ 0xff, 0xff, 0x7f };
if (data.size() < activations)
return false;

const auto at = std::prev(data.end(), activations);
data.insert(at, terminal.begin(), terminal.end());
set_schema(data, { 4, 0, 34, 0 });
return true;
}

bool envelope::migrate_step(data_chunk& data,
const envelope& configured) NOEXCEPT
{
Expand All @@ -121,6 +136,9 @@ bool envelope::migrate_step(data_chunk& data,
if (schema == config::version{ 4, 0, 32, 0 })
return migrate_4_0_32_to_4_0_33(data);

if (schema == config::version{ 4, 0, 33, 0 })
return migrate_4_0_33_to_4_0_34(data);

return false;
}

Expand Down Expand Up @@ -401,6 +419,7 @@ bool envelope::from_data(reader& source) NOEXCEPT

using integer = schema::header::link::integer;
constexpr auto size = schema::header::link::size;
pool_top = source.read_little_endian<integer, size>();
for (auto& activation: activations)
{
activation.flags = source.read_little_endian<uint32_t>();
Expand Down Expand Up @@ -528,6 +547,7 @@ bool envelope::to_data(flipper& sink) const NOEXCEPT

using integer = schema::header::link::integer;
constexpr auto size = schema::header::link::size;
sink.write_little_endian<integer, size>(pool_top);
for (const auto& activation: activations)
{
sink.write_little_endian<uint32_t>(activation.flags);
Expand All @@ -538,9 +558,14 @@ bool envelope::to_data(flipper& sink) const NOEXCEPT
}

size_t envelope::pooling_offset() const NOEXCEPT
{
return pool_top_offset() - pooling_size;
}

size_t envelope::pool_top_offset() const NOEXCEPT
{
constexpr auto activations_size = activation_count * activation_size;
return serialized_size() - pooling_size - activations_size;
return serialized_size() - pool_top_size - activations_size;
}

size_t envelope::activation_offset(size_t index) const NOEXCEPT
Expand All @@ -557,6 +582,7 @@ size_t envelope::serialized_size() const NOEXCEPT
forks_count +
( 1 * sizeof(uint64_t)) +
(18 * sizeof(uint32_t)) +
pool_top_size +
(activation_count * activation_size);

const auto checkpoint_size = [](const chain::checkpoint& in) NOEXCEPT
Expand Down
Loading
Loading