Skip to content

Commit d2c2573

Browse files
authored
Merge pull request #723 from evoskuil/master
Add is_block_segregated and is_tx_segregated, test.
2 parents 47b28d6 + 4647338 commit d2c2573

5 files changed

Lines changed: 73 additions & 44 deletions

File tree

include/bitcoin/database/impl/query/archive_read.ipp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ inline bool CLASS::is_coinbase(const tx_link& link) const NOEXCEPT
5656
return store_.tx.get(link, tx) && tx.coinbase;
5757
}
5858

59+
TEMPLATE
60+
inline bool CLASS::is_tx_segregated(const tx_link& link) const NOEXCEPT
61+
{
62+
size_t light{}, heavy{};
63+
return get_tx_sizes(light, heavy, link) && heavy != light;
64+
}
65+
66+
TEMPLATE
67+
inline bool CLASS::is_block_segregated(const header_link& link) const NOEXCEPT
68+
{
69+
size_t light{}, heavy{};
70+
return get_block_sizes(light, heavy, link) && heavy != light;
71+
}
72+
5973
TEMPLATE
6074
inline bool CLASS::is_milestone(const header_link& link) const NOEXCEPT
6175
{

include/bitcoin/database/impl/query/fees.ipp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,6 @@
3333
namespace libbitcoin {
3434
namespace database {
3535

36-
// static/private
37-
TEMPLATE
38-
constexpr size_t CLASS::virtual_size(size_t light, size_t heavy) NOEXCEPT
39-
{
40-
using namespace system;
41-
using namespace system::chain;
42-
constexpr auto scale = base_size_contribution + total_size_contribution;
43-
44-
const auto weight = ceilinged_add(
45-
ceilinged_multiply(base_size_contribution, light),
46-
ceilinged_multiply(total_size_contribution, heavy));
47-
48-
// Block weight is 3 * nominal size * + 1 * witness size [bip141].
49-
return ceilinged_divide(weight, scale);
50-
}
51-
5236
TEMPLATE
5337
bool CLASS::get_tx_virtual_size(size_t& out,
5438
const tx_link& link) const NOEXCEPT
@@ -57,7 +41,7 @@ bool CLASS::get_tx_virtual_size(size_t& out,
5741
if (!get_tx_sizes(light, heavy, link))
5842
return false;
5943

60-
out = virtual_size(light, heavy);
44+
out = system::chain::virtual_size(light, heavy);
6145
return true;
6246
}
6347

@@ -69,7 +53,7 @@ bool CLASS::get_block_virtual_size(size_t& out,
6953
if (!get_block_sizes(light, heavy, link))
7054
return false;
7155

72-
out = virtual_size(light, heavy);
56+
out = system::chain::virtual_size(light, heavy);
7357
return true;
7458
}
7559

include/bitcoin/database/query.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ class query
326326
inline bool is_block(const hash_digest& key) const NOEXCEPT;
327327
inline bool is_tx(const hash_digest& key) const NOEXCEPT;
328328
inline bool is_coinbase(const tx_link& link) const NOEXCEPT;
329+
inline bool is_tx_segregated(const tx_link& link) const NOEXCEPT;
330+
inline bool is_block_segregated(const header_link& link) const NOEXCEPT;
329331
inline bool is_milestone(const header_link& link) const NOEXCEPT;
330332
inline bool is_associated(const header_link& link) const NOEXCEPT;
331333
inline bool is_confirmable(const header_link& link) const NOEXCEPT;
@@ -791,7 +793,6 @@ class query
791793
template <typename Functor>
792794
static inline code parallel_address_transform(std::atomic_bool& cancel,
793795
outpoints& out, const output_links& links, Functor&& functor) NOEXCEPT;
794-
static constexpr size_t virtual_size(size_t light, size_t heavy) NOEXCEPT;
795796

796797
// Not thread safe.
797798
size_t get_fork_() const NOEXCEPT;

test/query/archive_read.cpp

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ static_assert(is_same_type<database::context::flag_t::integer, decltype(system::
2828
// nop event handler.
2929
const auto events_handler = [](auto, auto) {};
3030

31-
// ----------------------------------------------------------------------------
32-
33-
// archive_write (foreign-keyed)
31+
// is_coinbase
3432

3533
BOOST_AUTO_TEST_CASE(query_archive_read__is_coinbase__coinbase__true)
3634
{
@@ -67,6 +65,40 @@ BOOST_AUTO_TEST_CASE(query_archive_read__is_coinbase__non_coinbase__false)
6765
BOOST_REQUIRE(!query.is_coinbase(42));
6866
}
6967

68+
// is_tx_segregated
69+
70+
BOOST_AUTO_TEST_CASE(query_archive_read__is_tx_segregated__always__expected)
71+
{
72+
settings settings{};
73+
settings.path = TEST_DIRECTORY;
74+
test::chunk_store store{ settings };
75+
test::query_accessor query{ store };
76+
BOOST_REQUIRE_EQUAL(store.create(events_handler), error::success);
77+
BOOST_REQUIRE(query.initialize(test::genesis));
78+
BOOST_REQUIRE(query.set(test::block1a, context{}, false, false));
79+
BOOST_REQUIRE(!query.is_tx_segregated(0));
80+
BOOST_REQUIRE( query.is_tx_segregated(1));
81+
BOOST_REQUIRE(!query.is_tx_segregated(2));
82+
}
83+
84+
// is_block_segregated
85+
86+
BOOST_AUTO_TEST_CASE(query_archive_read__is_block_segregated__always__expected)
87+
{
88+
settings settings{};
89+
settings.path = TEST_DIRECTORY;
90+
test::chunk_store store{ settings };
91+
test::query_accessor query{ store };
92+
BOOST_REQUIRE_EQUAL(store.create(events_handler), error::success);
93+
BOOST_REQUIRE(query.initialize(test::genesis));
94+
BOOST_REQUIRE(query.set(test::block1a, context{}, false, false));
95+
BOOST_REQUIRE(!query.is_block_segregated(0));
96+
BOOST_REQUIRE( query.is_block_segregated(1));
97+
BOOST_REQUIRE(!query.is_block_segregated(2));
98+
}
99+
100+
// is_milestone
101+
70102
BOOST_AUTO_TEST_CASE(query_archive_read__is_milestone__genesis__false)
71103
{
72104
settings settings{};
@@ -252,17 +284,17 @@ BOOST_AUTO_TEST_CASE(query_archive_read__get_point_key__always__expected)
252284
// tx4/5 prevouts are all block1a.tx1.
253285
BOOST_REQUIRE(query.set(test::tx4));
254286
BOOST_REQUIRE(query.set(test::tx5));
255-
//// BOOST_REQUIRE_EQUAL(query.get_point_hash(0), test::block1a.transactions_ptr()->front()->hash(false));
287+
////BOOST_REQUIRE_EQUAL(query.get_point_hash(0), test::block1a.transactions_ptr()->front()->hash(false));
256288
BOOST_REQUIRE_EQUAL(query.get_point_hash(1), test::block1a.transactions_ptr()->front()->hash(false));
257289
BOOST_REQUIRE_EQUAL(query.get_point_hash(2), test::block1a.transactions_ptr()->front()->hash(false));
258-
//// BOOST_REQUIRE_EQUAL(query.get_point_hash(3), system::null_hash);
290+
////BOOST_REQUIRE_EQUAL(query.get_point_hash(3), system::null_hash);
259291

260292
// block1a adds three prevouts of two txs.
261293
BOOST_REQUIRE(query.set(test::block1a, context{}, false, false));
262-
//// BOOST_REQUIRE_EQUAL(query.get_point_hash(3), system::one_hash);
294+
////BOOST_REQUIRE_EQUAL(query.get_point_hash(3), system::one_hash);
263295
BOOST_REQUIRE_EQUAL(query.get_point_hash(4), system::one_hash);
264-
//// BOOST_REQUIRE_EQUAL(query.get_point_hash(5), test::two_hash);
265-
//// BOOST_REQUIRE_EQUAL(query.get_point_hash(6), system::null_hash);
296+
////BOOST_REQUIRE_EQUAL(query.get_point_hash(5), test::two_hash);
297+
////BOOST_REQUIRE_EQUAL(query.get_point_hash(6), system::null_hash);
266298
}
267299

268300
BOOST_AUTO_TEST_CASE(query_archive_read__get_tx_key__always__expected)
@@ -656,28 +688,28 @@ BOOST_AUTO_TEST_CASE(query_archive_read__get_spenders__unspent_or_not_found__exp
656688

657689
// Caller should always test for nullptr.
658690
BOOST_REQUIRE(query.get_spenders(output_link::terminal, true)->empty());
659-
//BOOST_REQUIRE(query.get_spenders_index(tx_link::terminal, 0, true)->empty());
660-
//BOOST_REQUIRE(query.get_spenders_index(tx_link::terminal, 1, true)->empty());
691+
////BOOST_REQUIRE(query.get_spenders_index(tx_link::terminal, 0, true)->empty());
692+
////BOOST_REQUIRE(query.get_spenders_index(tx_link::terminal, 1, true)->empty());
661693

662694
BOOST_REQUIRE(query.get_spenders(query.to_output(0, 0), true)->empty());
663695
BOOST_REQUIRE(query.get_spenders(query.to_output(0, 1), true)->empty());
664-
//BOOST_REQUIRE(query.get_spenders_index(0, 0, true)->empty());
665-
//BOOST_REQUIRE(query.get_spenders_index(0, 1, true)->empty());
696+
///BOOST_REQUIRE(query.get_spenders_index(0, 0, true)->empty());
697+
////BOOST_REQUIRE(query.get_spenders_index(0, 1, true)->empty());
666698

667699
BOOST_REQUIRE(query.get_spenders(query.to_output(1, 0), true)->empty());
668700
BOOST_REQUIRE(query.get_spenders(query.to_output(1, 1), true)->empty());
669-
//BOOST_REQUIRE(query.get_spenders_index(1, 0, true)->empty());
670-
//BOOST_REQUIRE(query.get_spenders_index(1, 1, true)->empty());
701+
////BOOST_REQUIRE(query.get_spenders_index(1, 0, true)->empty());
702+
////BOOST_REQUIRE(query.get_spenders_index(1, 1, true)->empty());
671703

672704
BOOST_REQUIRE(query.get_spenders(query.to_output(2, 0), true)->empty());
673705
BOOST_REQUIRE(query.get_spenders(query.to_output(2, 1), true)->empty());
674-
//BOOST_REQUIRE(query.get_spenders_index(2, 0, true)->empty());
675-
//BOOST_REQUIRE(query.get_spenders_index(2, 1, true)->empty());
706+
////BOOST_REQUIRE(query.get_spenders_index(2, 0, true)->empty());
707+
////BOOST_REQUIRE(query.get_spenders_index(2, 1, true)->empty());
676708

677709
BOOST_REQUIRE(query.get_spenders(query.to_output(3, 0), true)->empty());
678710
BOOST_REQUIRE(query.get_spenders(query.to_output(3, 1), true)->empty());
679-
//BOOST_REQUIRE(query.get_spenders_index(3, 0, true)->empty());
680-
//BOOST_REQUIRE(query.get_spenders_index(3, 1, true)->empty());
711+
////BOOST_REQUIRE(query.get_spenders_index(3, 0, true)->empty());
712+
////BOOST_REQUIRE(query.get_spenders_index(3, 1, true)->empty());
681713
}
682714

683715
////BOOST_AUTO_TEST_CASE(query_archive_read__get_spenders__found_and_spent__expected)

test/query/archive_write.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ static_assert(is_same_type<database::context::flag_t::integer, decltype(system::
2828
// nop event handler.
2929
const auto events_handler = [](auto, auto) {};
3030

31-
// archive_write (natural-keyed)
32-
3331
// slow test (mmap)
3432
BOOST_AUTO_TEST_CASE(query_archive_write__set_header__mmap_get_header__expected)
3533
{
@@ -1145,17 +1143,17 @@ BOOST_AUTO_TEST_CASE(query_archive_write__get_point_key__always__expected)
11451143
// tx4/5 prevouts are all block1a.tx1.
11461144
BOOST_CHECK(query.set(test::tx4));
11471145
BOOST_CHECK(query.set(test::tx5));
1148-
//// BOOST_CHECK_EQUAL(query.get_point_hash(0), test::block1a.transactions_ptr()->front()->hash(false));
1146+
////BOOST_CHECK_EQUAL(query.get_point_hash(0), test::block1a.transactions_ptr()->front()->hash(false));
11491147
BOOST_CHECK_EQUAL(query.get_point_hash(1), test::block1a.transactions_ptr()->front()->hash(false));
11501148
BOOST_CHECK_EQUAL(query.get_point_hash(2), test::block1a.transactions_ptr()->front()->hash(false));
1151-
//// BOOST_CHECK_EQUAL(query.get_point_hash(3), system::null_hash);
1149+
////BOOST_CHECK_EQUAL(query.get_point_hash(3), system::null_hash);
11521150

11531151
// block1a adds three prevouts of two txs.
11541152
BOOST_CHECK(query.set(test::block1a, context{}, false, false));
1155-
//// BOOST_CHECK_EQUAL(query.get_point_hash(3), system::one_hash);
1153+
////BOOST_CHECK_EQUAL(query.get_point_hash(3), system::one_hash);
11561154
BOOST_CHECK_EQUAL(query.get_point_hash(4), system::one_hash);
1157-
//// BOOST_CHECK_EQUAL(query.get_point_hash(5), test::two_hash);
1158-
//// BOOST_CHECK_EQUAL(query.get_point_hash(6), system::null_hash);
1155+
////BOOST_CHECK_EQUAL(query.get_point_hash(5), test::two_hash);
1156+
////BOOST_CHECK_EQUAL(query.get_point_hash(6), system::null_hash);
11591157
}
11601158

11611159
BOOST_AUTO_TEST_CASE(query_archive_write__get_tx_key__always__expected)

0 commit comments

Comments
 (0)