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
58 changes: 58 additions & 0 deletions include/bitcoin/database/impl/query/consensus/consensus_forks.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,64 @@ size_t CLASS::get_fork() const NOEXCEPT
///////////////////////////////////////////////////////////////////////////
}

// server/native
TEMPLATE
header_links CLASS::get_branch_tops() const NOEXCEPT
{
using namespace system;
using link_t = header_link::integer;
const auto top = std::max(get_top_candidate(), get_top_confirmed());
const auto records = header_records();

// A height of more than one header is forked, as is any above the tops.
size_t height{};
std::vector<bool> seen(add1(top), false);
std::vector<bool> forked(add1(top), false);
for (size_t record{}; record < records; ++record)
{
if (!get_height(height, possible_narrow_cast<link_t>(record)) ||
height > top)
continue;

if (seen.at(height))
forked.at(height) = true;
else
seen.at(height) = true;
}

std::unordered_set<link_t> forks{};
for (size_t record{}; record < records; ++record)
{
const auto link = possible_narrow_cast<link_t>(record);
if (get_height(height, link) && (height > top || forked.at(height)))
forks.insert(link);
}

// A child follows its parent, so the last link of a branch is its top.
header_links tops{};
for (auto record = records; is_nonzero(record) && !forks.empty();)
{
const auto link = possible_narrow_cast<link_t>(--record);
if (!forks.contains(link))
continue;

tops.push_back(link);
for (auto next = link; is_nonzero(forks.erase(next));)
next = to_parent(next).value;
}

///////////////////////////////////////////////////////////////////////////
std::shared_lock interlock1{ candidate_reorganization_mutex_ };
std::shared_lock interlock2{ confirmed_reorganization_mutex_ };
std::erase_if(tops, [&](const auto& link) NOEXCEPT
{
return is_candidate_header(link) || is_confirmed_block(link);
});
///////////////////////////////////////////////////////////////////////////

return tops;
}

// node/organizer
TEMPLATE
header_links CLASS::get_candidate_fork(size_t& fork_point) const NOEXCEPT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ code CLASS::get_confirmable_fees(fee_rates& out) const NOEXCEPT

const auto totals = algorithm::from_column(columns.totals[row]);
const size_t height = algorithm::from_column(columns.heights[row]);
out.push_back({ totals.size, totals.fee, height });
const auto bytes = ceilinged_divide<size_t>(totals.weight,
chain::light_weight_factor);
out.push_back({ bytes, totals.fee, height });
}

return error::success;
Expand Down
14 changes: 7 additions & 7 deletions include/bitcoin/database/impl/query/pool/pool_coverage.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ code CLASS::get_pool_candidate(pool_graph& out, const transaction& tx,
out.totals.push_back(algorithm::to_column(algorithm::totals_t
{
tx.fee(),
possible_narrow_cast<uint32_t>(tx.virtual_size()),
possible_narrow_cast<uint32_t>(tx.weight()),
possible_narrow_cast<uint32_t>(sigops)
}));

Expand Down Expand Up @@ -236,18 +236,18 @@ code CLASS::get_pool_coverage(bool& out, const pool_graph& graph,
return error::pool_limit;

const auto best = algorithm::evaluate(masks, component, g);
const auto bytes = algorithm::chargeable(component, g, bytes_per_sigop);
each = each && algorithm::covered(best, bytes, rate);
const auto weight = algorithm::chargeable(component, g, bytes_per_sigop);
each = each && algorithm::covered(best, weight, rate);
components.push_back(component);
}

// Pooled parent's surplus pays for its child (parent pays for child), in a
// family within a block, beyond which each component pays its own.
uint64_t fee{}, bytes{};
const auto bounded = algorithm::family(fee, bytes, g, measured,
limit.size, rate, bytes_per_sigop, maximum_outcomes);
uint64_t fee{}, weight{};
const auto bounded = algorithm::family(fee, weight, g, measured,
limit.weight, rate, bytes_per_sigop, maximum_outcomes);

out = bounded ? algorithm::covered(fee, bytes, rate) : each;
out = bounded ? algorithm::covered(fee, weight, rate) : each;

return error::success;
}
Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/database/impl/query/pool/pool_estimate.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ code CLASS::get_pool_arrivals(pool_arrivals& out, size_t top, size_t count,

const auto totals = algorithm::from_column(columns.totals[at]);
const auto rate = algorithm::fee_rate(totals, bytes_per_sigop);
const auto size = algorithm::chargeable(totals, bytes_per_sigop);
out.at(sub1(top - height)).push_back({ rate, size });
const auto weight = algorithm::chargeable(totals, bytes_per_sigop);
out.at(sub1(top - height)).push_back({ rate, weight });
}

return error::success;
Expand Down
10 changes: 5 additions & 5 deletions include/bitcoin/database/impl/query/pool/pool_reader.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ bool CLASS::get_pooled_totals(pool_totals& out,
if (!store_.pool.totals.get(fk, total))
return false;

out = { total.fee, total.bytes, total.sigops };
out = { total.fee, total.weight, total.sigops };
return true;
}

Expand All @@ -138,12 +138,12 @@ TEMPLATE
bool CLASS::get_pooled_fee(uint64_t& fee, size_t& height,
const tx_link& link) const NOEXCEPT
{
size_t size{};
return get_pooled_fee(fee, size, height, link);
size_t weight{};
return get_pooled_fee(fee, weight, height, link);
}

TEMPLATE
bool CLASS::get_pooled_fee(uint64_t& fee, size_t& size, size_t& height,
bool CLASS::get_pooled_fee(uint64_t& fee, size_t& weight, size_t& height,
const tx_link& link) const NOEXCEPT
{
if (!store_.pool.enabled())
Expand All @@ -160,7 +160,7 @@ bool CLASS::get_pooled_fee(uint64_t& fee, size_t& size, size_t& height,
return false;

fee = total.fee;
size = total.bytes;
weight = total.weight;
height = pooled.value;
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/database/impl/query/pool/pool_writer.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ bool CLASS::set_pool_row(const tx_link& link, const transaction& tx,
{
{},
tx.fee(),
possible_narrow_cast<uint32_t>(tx.virtual_size()),
possible_narrow_cast<uint32_t>(tx.weight()),
possible_narrow_cast<uint32_t>(sigops)
};
const table::pool_edge edge
Expand Down
36 changes: 31 additions & 5 deletions include/bitcoin/database/impl/store/store_open.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ code CLASS::migrate(system::config::version& original,
{
schema = { 4, 0, 34, 0 };
}
else if (schema == system::config::version{ 4, 0, 34, 0 })
{
ec = migrate_4_0_34_to_4_0_35(stored, configured, handler);
schema = { 4, 0, 35, 0 };
}
else
{
ec = error::schema_version;
Expand Down Expand Up @@ -327,11 +332,34 @@ code CLASS::purge_snapshots() NOEXCEPT
return error::success;
}

// The pool tables are discarded and created empty at configured sizing.
TEMPLATE
code CLASS::migrate_4_0_30_to_4_0_31(database::envelope& stored,
const database::envelope& configured,
const event_handler& handler) NOEXCEPT
{
const auto obsolete = body(configuration_.path,
std::string{ schema::caches::pool } + "_context");

if (const auto ec = file::remove_ex(obsolete))
return ec;

return recreate_pool(stored, configured, handler);
}

TEMPLATE
code CLASS::migrate_4_0_34_to_4_0_35(database::envelope& stored,
const database::envelope& configured,
const event_handler& handler) NOEXCEPT
{
return recreate_pool(stored, configured, handler);
}

// The pool tables are discarded and created empty at configured sizing, and
// the dropped pool has no top.
TEMPLATE
code CLASS::recreate_pool(database::envelope& stored,
const database::envelope& configured,
const event_handler& handler) NOEXCEPT
{
using namespace system;
const auto pool_buckets = configured.pool_buckets;
Expand Down Expand Up @@ -396,10 +424,7 @@ code CLASS::migrate_4_0_30_to_4_0_31(database::envelope& stored,
if (const auto closed = file.close(); !ec) ec = closed;
};

const auto obsolete = body(configuration_.path,
std::string{ schema::caches::pool } + "_context");

auto ec = file::remove_ex(obsolete);
code ec{};
create(ec, pool_head_, table_t::pool_head);
create(ec, pool_body_, table_t::pool_body);
create(ec, spends_head_, table_t::spends_head);
Expand Down Expand Up @@ -458,6 +483,7 @@ code CLASS::migrate_4_0_30_to_4_0_31(database::envelope& stored,
stored.wtxid_buckets = wtxid_buckets;
stored.pool_k = possible_narrow_cast<uint8_t>(pool_k);
stored.wtxid_k = possible_narrow_cast<uint8_t>(wtxid_k);
stored.pool_top = schema::header::link::terminal;
return error::success;
}

Expand Down
5 changes: 3 additions & 2 deletions include/bitcoin/database/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ class query
inline size_t get_top_candidate() const NOEXCEPT;
inline size_t get_top_confirmed() const NOEXCEPT;
size_t get_fork() const NOEXCEPT;
header_links get_branch_tops() const NOEXCEPT;
size_t get_top_associated() const NOEXCEPT;
size_t get_top_associated_from(size_t height) const NOEXCEPT;
associations get_all_unassociated() const NOEXCEPT;
Expand Down Expand Up @@ -897,7 +898,7 @@ class query
const pool_totals& budget, size_t depth,
uint32_t bytes_per_sigop) const NOEXCEPT;

/// Rate and size of rows pooled at each height below top, top first.
/// Rate and weight of rows pooled at each height below top, top first.
code get_pool_arrivals(pool_arrivals& out, size_t top, size_t count,
uint32_t bytes_per_sigop) const NOEXCEPT;

Expand Down Expand Up @@ -1264,7 +1265,7 @@ class query
bool get_pooled_fee(uint64_t& out, const tx_link& link) const NOEXCEPT;
bool get_pooled_fee(uint64_t& fee, size_t& height,
const tx_link& link) const NOEXCEPT;
bool get_pooled_fee(uint64_t& fee, size_t& size, size_t& height,
bool get_pooled_fee(uint64_t& fee, size_t& weight, size_t& height,
const tx_link& link) const NOEXCEPT;
hash_digest get_pooled_wtxid(const table::pool::link& link) const NOEXCEPT;

Expand Down
6 changes: 6 additions & 0 deletions include/bitcoin/database/store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ class store
code migrate_4_0_31_to_4_0_32(database::envelope& stored,
const database::envelope& configured) NOEXCEPT;
code migrate_4_0_32_to_4_0_33(const event_handler& handler) NOEXCEPT;
code migrate_4_0_34_to_4_0_35(database::envelope& stored,
const database::envelope& configured,
const event_handler& handler) NOEXCEPT;
code recreate_pool(database::envelope& stored,
const database::envelope& configured,
const event_handler& handler) NOEXCEPT;
code get_pruned(bool& pruned,
const table::transaction::record& record) const NOEXCEPT;

Expand Down
10 changes: 5 additions & 5 deletions include/bitcoin/database/tables/caches/pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct pool_word
lane word{};
};

/// Fee, virtual size and sigops of the tx (totals column).
/// Fee, weight and sigops of the tx (totals column).
struct pool_total
: public schema::pool_totals
{
Expand All @@ -78,7 +78,7 @@ struct pool_total
inline bool from_data(reader& source) NOEXCEPT
{
fee = source.read_little_endian<uint64_t>();
bytes = source.read_little_endian<uint32_t>();
weight = source.read_little_endian<uint32_t>();
sigops = source.read_little_endian<uint32_t>();
BC_ASSERT(!source || source.get_read_position() == minrow);
return source;
Expand All @@ -87,7 +87,7 @@ struct pool_total
inline bool to_data(flipper& sink) const NOEXCEPT
{
sink.write_little_endian<uint64_t>(fee);
sink.write_little_endian<uint32_t>(bytes);
sink.write_little_endian<uint32_t>(weight);
sink.write_little_endian<uint32_t>(sigops);
BC_ASSERT(!sink || sink.get_write_position() == minrow);
return sink;
Expand All @@ -96,7 +96,7 @@ struct pool_total
inline bool operator==(const pool_total&) const NOEXCEPT = default;

uint64_t fee{};
uint32_t bytes{};
uint32_t weight{};
uint32_t sigops{};
};

Expand Down Expand Up @@ -186,7 +186,7 @@ struct pool_id3
using no_map<schema::pool_id3>::nomap;
};

/// Scan columns (fee, size, sigops and edges), words aligned for lanes.
/// Scan columns (fee, weight, sigops and edges), words aligned for lanes.
struct pool_totals
: public no_map<schema::pool_totals>
{
Expand Down
3 changes: 3 additions & 0 deletions include/bitcoin/database/tables/envelope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ struct BCD_API envelope
/// 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 a serialized 4.0.34 envelope to 4.0.35.
static bool migrate_4_0_34_to_4_0_35(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 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 @@ -78,7 +78,8 @@ namespace schema {
/// 4.0.32.0 Require witness in the envelope.
/// 4.0.33.0 Pruned flags merged into the tx and txs heavy sizes.
/// 4.0.34.0 Pool top in the envelope.
constexpr std::array<uint32_t, 4> version{ 4, 0, 34, 0 };
/// 4.0.35.0 Pool totals weight in place of virtual size.
constexpr std::array<uint32_t, 4> version{ 4, 0, 35, 0 };

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

// The envelope layout is unchanged.
bool envelope::migrate_4_0_34_to_4_0_35(data_chunk& data) NOEXCEPT
{
set_schema(data, { 4, 0, 35, 0 });
return true;
}

bool envelope::migrate_step(data_chunk& data,
const envelope& configured) NOEXCEPT
{
Expand All @@ -139,6 +146,9 @@ bool envelope::migrate_step(data_chunk& data,
if (schema == config::version{ 4, 0, 33, 0 })
return migrate_4_0_33_to_4_0_34(data);

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

return false;
}

Expand Down
4 changes: 2 additions & 2 deletions test/mocks/chunk_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,10 @@ class chunk_store
using query_accessor = query<store<chunk_storages>>;

inline bool put_pooled(chunk_store& store, const tx_link& tx, uint64_t fee,
size_t height, uint32_t bytes=0) NOEXCEPT
size_t height, uint32_t weight=0) NOEXCEPT
{
const auto link = store.pool.allocate(1);
const table::pool_total total{ {}, fee, bytes, 0 };
const table::pool_total total{ {}, fee, weight, 0 };
const auto high = system::possible_narrow_cast<uint32_t>(height);
auto guard = store.pool.get_memory();
if (!guard || !store.pool.totals.put(link, total) ||
Expand Down
2 changes: 1 addition & 1 deletion test/mocks/pooling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ using pool_totals = system::pool::algorithm::totals_t;
using pool_projection = system::pool::algorithm::projection_t;
using pool_arrivals = std::vector<system::pool::algorithm::takes_t>;
constexpr auto bip113 = system::chain::flags::bip113_rule;
constexpr pool_totals block_limit{ 0, 1'000'000, 80'000 };
constexpr pool_totals block_limit{ 0, 4'000'000, 80'000 };
constexpr pool_totals tight_limit{ 0, 10, 80'000 };

/// A tx of one output spending the first output of the hash.
Expand Down
2 changes: 1 addition & 1 deletion test/query/amounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ BC_AUTO_TEST_CASE(query_amounts__get_tx_fee__pooled_missing_prevouts__pooled_fee
constexpr size_t expected_height = 7;
size_t bytes{};
BC_CHECK(query.get_tx_virtual_size(bytes, 3));
BC_CHECK(test::put_pooled(store, tx_link{ 3 }, expected_fee, expected_height, system::possible_narrow_cast<uint32_t>(bytes)));
BC_CHECK(test::put_pooled(store, tx_link{ 3 }, expected_fee, expected_height));
BC_CHECK(!query.get_tx_value(out, 3));
BC_CHECK(query.get_tx_fee(out, 3));
BC_CHECK_EQUAL(out, expected_fee);
Expand Down
Loading
Loading