Skip to content

Commit 8233f0f

Browse files
authored
Merge pull request #717 from evoskuil/master
Add comprehensive fee methods tests.
2 parents 6a8ed10 + 56be34f commit 8233f0f

12 files changed

Lines changed: 555 additions & 72 deletions

File tree

Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ test_libbitcoin_database_test_SOURCES = \
9191
test/query/confirm.cpp \
9292
test/query/consensus.cpp \
9393
test/query/context.cpp \
94-
test/query/estimate.cpp \
9594
test/query/extent.cpp \
95+
test/query/fees.cpp \
9696
test/query/height.cpp \
9797
test/query/initialize.cpp \
9898
test/query/merkle.cpp \
@@ -182,8 +182,8 @@ include_bitcoin_database_impl_query_HEADERS = \
182182
include/bitcoin/database/impl/query/confirm.ipp \
183183
include/bitcoin/database/impl/query/consensus.ipp \
184184
include/bitcoin/database/impl/query/context.ipp \
185-
include/bitcoin/database/impl/query/estimate.ipp \
186185
include/bitcoin/database/impl/query/extent.ipp \
186+
include/bitcoin/database/impl/query/fees.ipp \
187187
include/bitcoin/database/impl/query/height.ipp \
188188
include/bitcoin/database/impl/query/initialize.ipp \
189189
include/bitcoin/database/impl/query/merkle.ipp \

builds/cmake/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ if (with-tests)
301301
"../../test/query/confirm.cpp"
302302
"../../test/query/consensus.cpp"
303303
"../../test/query/context.cpp"
304-
"../../test/query/estimate.cpp"
305304
"../../test/query/extent.cpp"
305+
"../../test/query/fees.cpp"
306306
"../../test/query/height.cpp"
307307
"../../test/query/initialize.cpp"
308308
"../../test/query/merkle.cpp"

builds/msvc/vs2022/libbitcoin-database-test/libbitcoin-database-test.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@
152152
<ClCompile Include="..\..\..\..\test\query\confirm.cpp" />
153153
<ClCompile Include="..\..\..\..\test\query\consensus.cpp" />
154154
<ClCompile Include="..\..\..\..\test\query\context.cpp" />
155-
<ClCompile Include="..\..\..\..\test\query\estimate.cpp" />
156155
<ClCompile Include="..\..\..\..\test\query\extent.cpp" />
156+
<ClCompile Include="..\..\..\..\test\query\fees.cpp" />
157157
<ClCompile Include="..\..\..\..\test\query\height.cpp">
158158
<ObjectFileName>$(IntDir)test_query_height.obj</ObjectFileName>
159159
</ClCompile>

builds/msvc/vs2022/libbitcoin-database-test/libbitcoin-database-test.vcxproj.filters

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@
123123
<ClCompile Include="..\..\..\..\test\query\context.cpp">
124124
<Filter>src\query</Filter>
125125
</ClCompile>
126-
<ClCompile Include="..\..\..\..\test\query\estimate.cpp">
126+
<ClCompile Include="..\..\..\..\test\query\extent.cpp">
127127
<Filter>src\query</Filter>
128128
</ClCompile>
129-
<ClCompile Include="..\..\..\..\test\query\extent.cpp">
129+
<ClCompile Include="..\..\..\..\test\query\fees.cpp">
130130
<Filter>src\query</Filter>
131131
</ClCompile>
132132
<ClCompile Include="..\..\..\..\test\query\height.cpp">

builds/msvc/vs2022/libbitcoin-database/libbitcoin-database.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@
219219
<None Include="..\..\..\..\include\bitcoin\database\impl\query\confirm.ipp" />
220220
<None Include="..\..\..\..\include\bitcoin\database\impl\query\consensus.ipp" />
221221
<None Include="..\..\..\..\include\bitcoin\database\impl\query\context.ipp" />
222-
<None Include="..\..\..\..\include\bitcoin\database\impl\query\estimate.ipp" />
223222
<None Include="..\..\..\..\include\bitcoin\database\impl\query\extent.ipp" />
223+
<None Include="..\..\..\..\include\bitcoin\database\impl\query\fees.ipp" />
224224
<None Include="..\..\..\..\include\bitcoin\database\impl\query\height.ipp" />
225225
<None Include="..\..\..\..\include\bitcoin\database\impl\query\initialize.ipp" />
226226
<None Include="..\..\..\..\include\bitcoin\database\impl\query\merkle.ipp" />

builds/msvc/vs2022/libbitcoin-database/libbitcoin-database.vcxproj.filters

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,10 @@
346346
<None Include="..\..\..\..\include\bitcoin\database\impl\query\context.ipp">
347347
<Filter>include\bitcoin\database\impl\query</Filter>
348348
</None>
349-
<None Include="..\..\..\..\include\bitcoin\database\impl\query\estimate.ipp">
349+
<None Include="..\..\..\..\include\bitcoin\database\impl\query\extent.ipp">
350350
<Filter>include\bitcoin\database\impl\query</Filter>
351351
</None>
352-
<None Include="..\..\..\..\include\bitcoin\database\impl\query\extent.ipp">
352+
<None Include="..\..\..\..\include\bitcoin\database\impl\query\fees.ipp">
353353
<Filter>include\bitcoin\database\impl\query</Filter>
354354
</None>
355355
<None Include="..\..\..\..\include\bitcoin\database\impl\query\height.ipp">

include/bitcoin/database/impl/query/estimate.ipp renamed to include/bitcoin/database/impl/query/fees.ipp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,35 @@
2929

3030
namespace libbitcoin {
3131
namespace database {
32-
32+
33+
// TODO: optimize.
34+
35+
TEMPLATE
36+
uint64_t CLASS::get_tx_fee(const tx_link& link) const NOEXCEPT
37+
{
38+
const auto tx = get_transaction(link, false);
39+
if (!tx)
40+
return max_uint64;
41+
42+
if (tx->is_coinbase())
43+
return zero;
44+
45+
return populate_without_metadata(*tx) ? tx->fee() : max_uint64;
46+
}
47+
48+
TEMPLATE
49+
uint64_t CLASS::get_block_fee(const header_link& link) const NOEXCEPT
50+
{
51+
const auto block = get_block(link, false);
52+
return block && populate_without_metadata(*block) ? block->fees() :
53+
max_uint64;
54+
}
55+
3356
TEMPLATE
3457
bool CLASS::get_tx_fees(fee_rate& out, const tx_link& link) const NOEXCEPT
3558
{
3659
const auto tx = get_transaction(link, false);
37-
if (!tx || !populate_without_metadata(*tx))
60+
if (!tx || tx->is_coinbase() || !populate_without_metadata(*tx))
3861
return false;
3962

4063
out.bytes = tx->virtual_size();
@@ -68,17 +91,17 @@ bool CLASS::get_block_fees(fee_rates& out,
6891

6992
TEMPLATE
7093
bool CLASS::get_branch_fees(std::atomic_bool& cancel, fee_rate_sets& out,
71-
size_t top, size_t count) const NOEXCEPT
94+
size_t start, size_t count) const NOEXCEPT
7295
{
7396
out.clear();
7497
if (is_zero(count))
7598
return true;
7699

77-
if (top > get_top_confirmed())
100+
if (system::is_add_overflow(start, sub1(count)))
78101
return false;
79102

80-
const auto start = top - sub1(count);
81-
if (system::is_subtract_overflow(top, sub1(count)))
103+
const auto last = start + sub1(count);
104+
if (last > get_top_confirmed())
82105
return false;
83106

84107
out.resize(count);

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -140,26 +140,6 @@ code CLASS::get_block_state(const header_link& link) const NOEXCEPT
140140
return to_block_code(valid.code);
141141
}
142142

143-
TEMPLATE
144-
uint64_t CLASS::get_block_fee(const header_link& link) const NOEXCEPT
145-
{
146-
// TODO: optimize.
147-
const auto block = get_block(link, false);
148-
return block && populate_without_metadata(*block) ? block->fees() :
149-
max_uint64;
150-
}
151-
152-
TEMPLATE
153-
uint64_t CLASS::get_tx_fee(const tx_link& link) const NOEXCEPT
154-
{
155-
// TODO: optimize.
156-
const auto tx = get_transaction(link, false);
157-
if (is_coinbase(link))
158-
return {};
159-
160-
return tx && populate_without_metadata(*tx) ? tx->fee() : max_uint64;
161-
}
162-
163143
TEMPLATE
164144
inline bool CLASS::is_validated(const header_link& link) const NOEXCEPT
165145
{

include/bitcoin/database/query.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,17 +398,21 @@ class query
398398
bool populate_without_metadata(const block& block) const NOEXCEPT;
399399
bool populate_without_metadata(const transaction& tx) const NOEXCEPT;
400400

401-
/// Services.
401+
/// Fees.
402402
/// -----------------------------------------------------------------------
403403

404+
/// Total fee value by tx or block.
404405
uint64_t get_tx_fee(const tx_link& link) const NOEXCEPT;
405406
uint64_t get_block_fee(const header_link& link) const NOEXCEPT;
406407

407-
/// Gether fee rate tuples by tx, block or branch.
408+
/// Fee rate tuples by tx, block or branch.
408409
bool get_tx_fees(fee_rate& out, const tx_link& link) const NOEXCEPT;
409410
bool get_block_fees(fee_rates& out, const header_link& link) const NOEXCEPT;
410411
bool get_branch_fees(std::atomic_bool& cancel, fee_rate_sets& out,
411-
size_t top, size_t count) const NOEXCEPT;
412+
size_t start, size_t count) const NOEXCEPT;
413+
414+
/// Merkle.
415+
/// -----------------------------------------------------------------------
412416

413417
/// Merkle computations over the index of confirmed headers.
414418
hash_digest get_merkle_root(size_t height) const NOEXCEPT;
@@ -786,13 +790,13 @@ BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
786790
#include <bitcoin/database/impl/query/consensus.ipp>
787791
#include <bitcoin/database/impl/query/context.ipp>
788792
#include <bitcoin/database/impl/query/extent.ipp>
793+
#include <bitcoin/database/impl/query/fees.ipp>
789794
#include <bitcoin/database/impl/query/height.ipp>
790795
#include <bitcoin/database/impl/query/initialize.ipp>
791796
#include <bitcoin/database/impl/query/network.ipp>
792797
#include <bitcoin/database/impl/query/objects.ipp>
793798
#include <bitcoin/database/impl/query/optional.ipp>
794799
#include <bitcoin/database/impl/query/merkle.ipp>
795-
#include <bitcoin/database/impl/query/estimate.ipp>
796800
#include <bitcoin/database/impl/query/translate.ipp>
797801
#include <bitcoin/database/impl/query/validate.ipp>
798802

test/mocks/blocks.hpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,115 @@ const block block_spend_internal_2b
522522
}
523523
}
524524
};
525+
const block block_missing_prevout_2b
526+
{
527+
header
528+
{
529+
0x31323334, // version
530+
block1b.hash(), // previous_block_hash
531+
system::one_hash, // merkle_root
532+
0x41424344, // timestamp
533+
0x51525354, // bits
534+
0x61626364 // nonce
535+
},
536+
transactions
537+
{
538+
tx2b,
539+
transaction
540+
{
541+
0xb2,
542+
inputs
543+
{
544+
input
545+
{
546+
// missing prevout index.
547+
point{ tx2b.hash(false), 0x01 },
548+
script{ { { opcode::checkmultisig }, { opcode::size } } },
549+
witness{},
550+
0xb2
551+
}
552+
},
553+
outputs
554+
{
555+
output
556+
{
557+
0xb0, // fee will be 0x01
558+
script{ { { opcode::pick } } }
559+
}
560+
},
561+
0xb2
562+
}
563+
}
564+
};
565+
const block block_valid_spend_internal_2b
566+
{
567+
header
568+
{
569+
0x31323334, // version
570+
block1b.hash(), // previous_block_hash
571+
system::one_hash, // merkle_root
572+
0x41424344, // timestamp
573+
0x51525354, // bits
574+
0x61626364 // nonce
575+
},
576+
transactions
577+
{
578+
tx2b,
579+
transaction
580+
{
581+
0xb2,
582+
inputs
583+
{
584+
input
585+
{
586+
point{ tx2b.hash(false), 0x00 },
587+
script{ { { opcode::checkmultisig }, { opcode::size } } },
588+
witness{},
589+
0xb2
590+
}
591+
},
592+
outputs
593+
{
594+
output
595+
{
596+
0xb0, // fee will be 0x01
597+
script{ { { opcode::pick } } }
598+
}
599+
},
600+
0xb2
601+
},
602+
transaction
603+
{
604+
0xb2,
605+
inputs
606+
{
607+
input
608+
{
609+
point{ block1b.transactions_ptr()->front()->hash(false), 0x00 },
610+
script{ { { opcode::checkmultisig }, { opcode::size } } },
611+
witness{},
612+
0xb2
613+
},
614+
input
615+
{
616+
point{ block1b.transactions_ptr()->front()->hash(false), 0x01 },
617+
script{ { { opcode::checkmultisig } } },
618+
witness{},
619+
0xb2
620+
}
621+
},
622+
outputs
623+
{
624+
output
625+
{
626+
0xb2, // fee will be 0xb1 + 0xb1 - 0xb2 = 0xb0
627+
script{ { { opcode::pick }, { opcode::roll }, { opcode::pick } } }
628+
}
629+
},
630+
0xb2
631+
}
632+
}
633+
};
525634

526635
} // namespace test
527636

0 commit comments

Comments
 (0)