Skip to content

Commit

Permalink
Merge pull request #512 from evoskuil/master
Browse files Browse the repository at this point in the history
Add get_tx_count and get_tx_sizes, tests.
  • Loading branch information
evoskuil committed Aug 27, 2024
2 parents e260e9d + 1138604 commit e94dd03
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 1 deletion.
23 changes: 23 additions & 0 deletions include/bitcoin/database/impl/query/archive.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ hashes CLASS::get_tx_keys(const header_link& link) const NOEXCEPT
return hashes;
}

TEMPLATE
size_t CLASS::get_tx_count(const header_link& link) const NOEXCEPT
{
table::txs::get_tx_quantity txs{};
if (!store_.txs.find(link, txs))
return {};

return txs.quantity;
}

TEMPLATE
inline hash_digest CLASS::get_header_key(const header_link& link) const NOEXCEPT
{
Expand Down Expand Up @@ -364,6 +374,19 @@ bool CLASS::get_tx_position(size_t& out, const tx_link& link) const NOEXCEPT
return true;
}

TEMPLATE
bool CLASS::get_tx_sizes(size_t& light, size_t& heavy,
const tx_link& link) const NOEXCEPT
{
table::transaction::get_sizes sizes{};
if (!store_.tx.get(link, sizes))
return false;

light = sizes.light;
heavy = sizes.heavy;
return true;
}

TEMPLATE
bool CLASS::get_value(uint64_t& out, const output_link& link) const NOEXCEPT
{
Expand Down
5 changes: 4 additions & 1 deletion include/bitcoin/database/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,18 @@ class query
/// Archival (surrogate-keyed).
/// -----------------------------------------------------------------------

/// Empty/null_hash implies fault.
/// Empty/null_hash implies fault, zero count implies unassociated.
hashes get_tx_keys(const header_link& link) const NOEXCEPT;
size_t get_tx_count(const header_link& link) const NOEXCEPT;
inline hash_digest get_header_key(const header_link& link) const NOEXCEPT;
inline hash_digest get_point_key(const point_link& link) const NOEXCEPT;
inline hash_digest get_tx_key(const tx_link& link) const NOEXCEPT;

/// False implies not confirmed.
bool get_tx_height(size_t& out, const tx_link& link) const NOEXCEPT;
bool get_tx_position(size_t& out, const tx_link& link) const NOEXCEPT;
bool get_tx_sizes(size_t& light, size_t& heavy,
const tx_link& link) const NOEXCEPT;

/// Terminal implies not found, false implies fault.
height_link get_height(const hash_digest& key) const NOEXCEPT;
Expand Down
16 changes: 16 additions & 0 deletions include/bitcoin/database/tables/archives/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,22 @@ struct transaction

bool coinbase{};
};

struct get_sizes
: public schema::transaction
{
inline bool from_data(reader& source) NOEXCEPT
{
source.skip_byte();
light = source.read_little_endian<bytes::integer, bytes::size>();
heavy = source.read_little_endian<bytes::integer, bytes::size>();
return source;
}

size_t light{};
size_t heavy{};
};

};

} // namespace table
Expand Down
12 changes: 12 additions & 0 deletions include/bitcoin/database/tables/archives/txs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,18 @@ struct txs

keys tx_fks{};
};

struct get_tx_quantity
: public schema::txs
{
inline bool from_data(reader& source) NOEXCEPT
{
quantity = source.read_little_endian<tx::integer, schema::count_>();
return source;
}

size_t quantity{};
};
};

} // namespace table
Expand Down
27 changes: 27 additions & 0 deletions test/query/archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,33 @@ BOOST_AUTO_TEST_CASE(query_archive__get_tx_position__always__expected)
BOOST_REQUIRE(!query.get_tx_position(out, 5));
}

BOOST_AUTO_TEST_CASE(query_archive__get_tx_sizes__coinbase__204)
{
settings settings{};
settings.path = TEST_DIRECTORY;
test::chunk_store store{ settings };
test::query_accessor query{ store };
BOOST_REQUIRE(!store.create(events_handler));
BOOST_REQUIRE(query.initialize(test::genesis));

size_t light{};
size_t heavy{};
BOOST_REQUIRE(query.get_tx_sizes(light, heavy, 0));
BOOST_REQUIRE_EQUAL(light, 204u);
BOOST_REQUIRE_EQUAL(heavy, 204u);
}

BOOST_AUTO_TEST_CASE(query_archive__get_tx_count__coinbase__1)
{
settings settings{};
settings.path = TEST_DIRECTORY;
test::chunk_store store{ settings };
test::query_accessor query{ store };
BOOST_REQUIRE(!store.create(events_handler));
BOOST_REQUIRE(query.initialize(test::genesis));
BOOST_REQUIRE_EQUAL(query.get_tx_count(0), 1u);
}

BOOST_AUTO_TEST_CASE(query_archive__get_input__not_found__nullptr)
{
settings settings{};
Expand Down

0 comments on commit e94dd03

Please sign in to comment.