8#define BOOST_TEST_MODULE Bitcoin Kernel Test Suite
9#include <boost/test/included/unit_test.hpp>
31 const std::string chars =
"0123456789"
32 "abcdefghijklmnopqrstuvwxyz"
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
35 static std::random_device rd;
36 static std::default_random_engine dre{rd()};
37 static std::uniform_int_distribution<> distribution(0, chars.size() - 1);
40 random.reserve(length);
41 for (uint32_t i = 0; i < length; i++) {
42 random += chars[distribution(dre)];
49 std::vector<std::byte> bytes;
50 bytes.reserve(hex.length() / 2);
52 for (
size_t i{0}; i < hex.length(); i += 2) {
54 auto [ptr, ec] = std::from_chars(hex.data() + i, hex.data() + i + 2, byte_value, 16);
56 if (ec != std::errc{} || ptr != hex.data() + i + 2) {
57 throw std::invalid_argument(
"Invalid hex character");
59 bytes.push_back(
static_cast<std::byte
>(byte_value));
66 std::ostringstream oss;
69 for (
auto it = bytes.rbegin(); it != bytes.rend(); ++it) {
70 oss << std::hex << std::setw(2) << std::setfill(
'0')
71 <<
static_cast<unsigned int>(
static_cast<uint8_t
>(*it));
78 ScriptVerificationFlags::NULLDUMMY | ScriptVerificationFlags::CHECKLOCKTIMEVERIFY |
79 ScriptVerificationFlags::CHECKSEQUENCEVERIFY};
82void check_equal(std::span<const std::byte> _actual, std::span<const std::byte> _expected,
bool equal =
true)
84 std::span<const uint8_t> actual{
reinterpret_cast<const unsigned char*
>(_actual.data()), _actual.size()};
85 std::span<const uint8_t> expected{
reinterpret_cast<const unsigned char*
>(_expected.data()), _expected.size()};
86 BOOST_CHECK_EQUAL_COLLECTIONS(
87 actual.begin(), actual.end(),
88 expected.begin(), expected.end());
96 std::cout <<
"kernel: " << message;
119 BOOST_CHECK_GT(timestamp, 0);
124 std::cout <<
"Kernel warning is set: " << message << std::endl;
129 std::cout <<
"Kernel warning was unset." << std::endl;
134 std::cout << error << std::endl;
139 std::cout << error << std::endl;
151 auto ser_block{block.
ToBytes()};
157 case ValidationMode::VALID: {
158 std::cout <<
"Valid block" << std::endl;
162 std::cout <<
"Invalid block: ";
165 case BlockValidationResult::UNSET:
166 std::cout <<
"initial value. Block has not yet been rejected" << std::endl;
168 case BlockValidationResult::HEADER_LOW_WORK:
169 std::cout <<
"the block header may be on a too-little-work chain" << std::endl;
171 case BlockValidationResult::CONSENSUS:
172 std::cout <<
"invalid by consensus rules (excluding any below reasons)" << std::endl;
174 case BlockValidationResult::CACHED_INVALID:
175 std::cout <<
"this block was cached as being invalid and we didn't store the reason why" << std::endl;
177 case BlockValidationResult::INVALID_HEADER:
178 std::cout <<
"invalid proof of work or time too old" << std::endl;
180 case BlockValidationResult::MUTATED:
181 std::cout <<
"the block's data didn't match the data committed to by the PoW" << std::endl;
183 case BlockValidationResult::MISSING_PREV:
184 std::cout <<
"We don't have the previous block the checked one is built on" << std::endl;
186 case BlockValidationResult::INVALID_PREV:
187 std::cout <<
"A block this one builds on is invalid" << std::endl;
189 case BlockValidationResult::TIME_FUTURE:
190 std::cout <<
"block timestamp was > 2 hours in the future (or our clock is bad)" << std::endl;
195 case ValidationMode::INTERNAL_ERROR: {
196 std::cout <<
"Internal error" << std::endl;
204 std::cout <<
"Block connected." << std::endl;
209 std::cout <<
"Block passed pow verification" << std::endl;
214 std::cout <<
"Block disconnected." << std::endl;
223 unsigned int input_index,
226 auto status = ScriptVerifyStatus::OK;
245 BOOST_CHECK(status == ScriptVerifyStatus::ERROR_SPENT_OUTPUTS_REQUIRED);
269 {
t.ToBytes() } -> std::convertible_to<std::vector<std::byte>>;
277 BOOST_CHECK(
object.get() != distinct_object.get());
280 BOOST_CHECK_NE(
object.ToBytes().size(), distinct_object.ToBytes().size());
284 T object2(distinct_object);
285 BOOST_CHECK_NE(distinct_object.get(), object2.get());
287 check_equal(distinct_object.ToBytes(), object2.ToBytes());
291 T object3{distinct_object};
293 BOOST_CHECK_NE(object3.get(), object2.get());
299 auto* original_ptr = object2.get();
300 T object4{std::move(object2)};
308 original_ptr = object4.get();
309 object2 = std::move(object4);
317template <
typename RangeType>
318 requires std::ranges::random_access_range<RangeType>
321 using value_type = std::ranges::range_value_t<RangeType>;
327 BOOST_CHECK_EQUAL(std::distance(range.begin(), range.end()),
static_cast<std::ptrdiff_t
>(expected_size));
331 for (
size_t i = 0; i < range.size(); ++i) {
335 BOOST_CHECK_NE(range.at(0).get(), range.at(expected_size - 1).get());
341 auto it = range.begin();
370 for (
auto rit = range.end(); rit != range.begin();) {
376 std::vector<value_type> collected;
377 for (
const auto& elem : range) {
378 collected.push_back(elem);
391 auto tx_data{
hex_string_to_byte_vec(
"02000000013f7cebd65c27431a90bba7f796914fe8cc2ddfc3f2cbd6f7e5f2fc854534da95000000006b483045022100de1ac3bcdfb0332207c4a91f3832bd2c2915840165f876ab47c5f8996b971c3602201c6c053d750fadde599e6f5c4e1963df0f01fc0d97815e8157e3d59fe09ca30d012103699b464d1d8bc9e47d4fb1cdaa89a1c5783d68363c4dbc4b524ed3d857148617feffffff02836d3c01000000001976a914fc25d6d5c94003bf5b0c7b640a248e2c637fcfb088ac7ada8202000000001976a914fbed3d9b11183209a57999d54d59f67c019e756c88ac6acb0700")};
393 auto tx_data_2{
hex_string_to_byte_vec(
"02000000000101904f4ee5c87d20090b642f116e458cd6693292ad9ece23e72f15fb6c05b956210500000000fdffffff02e2010000000000002251200839a723933b56560487ec4d67dda58f09bae518ffa7e148313c5696ac837d9f10060000000000002251205826bcdae7abfb1c468204170eab00d887b61ab143464a4a09e1450bdc59a3340140f26e7af574e647355830772946356c27e7bbc773c5293688890f58983499581be84de40be7311a14e6d6422605df086620e75adae84ff06b75ce5894de5e994a00000000")};
404 auto broken_tx_data{std::span<std::byte>{tx_data.begin(), tx_data.begin() + 10}};
406 auto output{tx.GetOutput(tx.CountOutputs() - 1)};
408 auto script_pubkey{output.GetScriptPubkey()};
416 BOOST_CHECK_NE(output.
get(), output2.
get());
419 BOOST_CHECK_NE(output3.
get(), output2.
get());
424 BOOST_CHECK_NE(
script.get(), script2.
get());
429 BOOST_CHECK_NE(script3.
get(), script2.
get());
447 int64_t total_amount{0};
448 for (
const auto output : tx.Outputs()) {
449 total_amount += output.Amount();
453 auto amount = *(tx.Outputs() | std::ranges::views::filter([](
const auto& output) {
454 return output.Amount() == 42130042;
456 std::views::transform([](
const auto& output) {
457 return output.Amount();
459 BOOST_REQUIRE(amount);
465 check_equal(script_pubkey_roundtrip.ToBytes(), script_pubkey.ToBytes());
471 std::vector<std::byte> script_data_2 = script_data;
472 script_data_2.push_back(std::byte{0x51});
477 std::span<std::byte> empty_data{};
492 Transaction tx{
hex_string_to_byte_vec(
"020000000248c03e66fd371c7033196ce24298628e59ebefa00363026044e0f35e0325a65d000000006a473044022004893432347f39beaa280e99da595681ddb20fc45010176897e6e055d716dbfa022040a9e46648a5d10c33ef7cee5e6cf4b56bd513eae3ae044f0039824b02d0f44c012102982331a52822fd9b62e9b5d120da1d248558fac3da3a3c51cd7d9c8ad3da760efeffffffb856678c6e4c3c84e39e2ca818807049d6fba274b42af3c6d3f9d4b6513212d2000000006a473044022068bcedc7fe39c9f21ad318df2c2da62c2dc9522a89c28c8420ff9d03d2e6bf7b0220132afd752754e5cb1ea2fd0ed6a38ec666781e34b0e93dc9a08f2457842cf5660121033aeb9c079ea3e08ea03556182ab520ce5c22e6b0cb95cee6435ee17144d860cdfeffffff0260d50b00000000001976a914363cc8d55ea8d0500de728ef6d63804ddddbdc9888ac67040f00000000001976a914c303bdc5064bf9c9a8b507b5496bd0987285707988ac6acb0700")};
503 auto tx_data{
hex_string_to_byte_vec(
"02000000013f7cebd65c27431a90bba7f796914fe8cc2ddfc3f2cbd6f7e5f2fc854534da95000000006b483045022100de1ac3bcdfb0332207c4a91f3832bd2c2915840165f876ab47c5f8996b971c3602201c6c053d750fadde599e6f5c4e1963df0f01fc0d97815e8157e3d59fe09ca30d012103699b464d1d8bc9e47d4fb1cdaa89a1c5783d68363c4dbc4b524ed3d857148617feffffff02836d3c01000000001976a914fc25d6d5c94003bf5b0c7b640a248e2c637fcfb088ac7ada8202000000001976a914fbed3d9b11183209a57999d54d59f67c019e756c88ac6acb0700")};
505 auto tx_data_2{
hex_string_to_byte_vec(
"02000000000101904f4ee5c87d20090b642f116e458cd6693292ad9ece23e72f15fb6c05b956210500000000fdffffff02e2010000000000002251200839a723933b56560487ec4d67dda58f09bae518ffa7e148313c5696ac837d9f10060000000000002251205826bcdae7abfb1c468204170eab00d887b61ab143464a4a09e1450bdc59a3340140f26e7af574e647355830772946356c27e7bbc773c5293688890f58983499581be84de40be7311a14e6d6422605df086620e75adae84ff06b75ce5894de5e994a00000000")};
515 CheckHandle(precomputed_txdata, precomputed_txdata_2);
522 auto legacy_spending_tx{
Transaction{
hex_string_to_byte_vec(
"02000000013f7cebd65c27431a90bba7f796914fe8cc2ddfc3f2cbd6f7e5f2fc854534da95000000006b483045022100de1ac3bcdfb0332207c4a91f3832bd2c2915840165f876ab47c5f8996b971c3602201c6c053d750fadde599e6f5c4e1963df0f01fc0d97815e8157e3d59fe09ca30d012103699b464d1d8bc9e47d4fb1cdaa89a1c5783d68363c4dbc4b524ed3d857148617feffffff02836d3c01000000001976a914fc25d6d5c94003bf5b0c7b640a248e2c637fcfb088ac7ada8202000000001976a914fbed3d9b11183209a57999d54d59f67c019e756c88ac6acb0700")}};
524 legacy_spent_script_pubkey,
537 legacy_spent_script_pubkey,
539 &legacy_precomputed_txdata,
546 auto segwit_spending_tx{
Transaction{
hex_string_to_byte_vec(
"010000000001011f97548fbbe7a0db7588a66e18d803d0089315aa7d4cc28360b6ec50ef36718a0100000000ffffffff02df1776000000000017a9146c002a686959067f4866b8fb493ad7970290ab728757d29f0000000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d04004730440220565d170eed95ff95027a69b313758450ba84a01224e1f7f130dda46e94d13f8602207bdd20e307f062594022f12ed5017bbf4a055a06aea91c10110a0e3bb23117fc014730440220647d2dc5b15f60bc37dc42618a370b2a1490293f9e5c8464f53ec4fe1dfe067302203598773895b4b16d37485cbe21b337f4e4b650739880098c592553add7dd4355016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000")}};
548 segwit_spent_script_pubkey,
561 segwit_spent_script_pubkey,
563 &segwit_precomputed_txdata,
570 auto taproot_spending_tx{
Transaction{
hex_string_to_byte_vec(
"01000000000101d1f1c1f8cdf6759167b90f52c9ad358a369f95284e841d7a2536cef31c0549580100000000fdffffff020000000000000000316a2f49206c696b65205363686e6f7272207369677320616e6420492063616e6e6f74206c69652e204062697462756734329e06010000000000225120a37c3903c8d0db6512e2b40b0dffa05e5a3ab73603ce8c9c4b7771e5412328f90140a60c383f71bac0ec919b1d7dbc3eb72dd56e7aa99583615564f9f99b8ae4e837b758773a5b2e4c51348854c8389f008e05029db7f464a5ff2e01d5e6e626174affd30a00")}};
571 std::vector<TransactionOutput> taproot_spent_outputs;
572 taproot_spent_outputs.emplace_back(taproot_spent_script_pubkey, 88480);
575 taproot_spent_outputs,
578 taproot_spent_script_pubkey,
580 &taproot_precomputed_txdata,
588 auto taproot2_spending_tx{
Transaction{
hex_string_to_byte_vec(
"02000000000102c0f01ead18750892c84b1d4f595149ad38f16847df1fbf490e235b3b78c1f98a0100000000ffffffff456764a19c2682bf5b1567119f06a421849ad1664cf42b5ef95b69d6e2159e9d0000000000ffffffff022202000000000000225120b6c0c2a8ee25a2ae0322ab7f1a06f01746f81f6b90d179c3c2a51a356e6188f1d70e020000000000225120b7da80f57e36930b0515eb09293e25858d13e6b91fee6184943f5a584cb4248e0141933fdc49eb1af1f08ed1e9cf5559259309a8acd25ff1e6999b6955124438aef4fceaa4e6a5f85286631e24837329563595bc3cf4b31e1c687442abb01c4206818101401c9620faf1e8c84187762ad14d04ae3857f59a2f03f1dcbb99290e16dfc572a63b4ea435780a5787af59beb5742fd71cda8a95381517a1ff14b4c67996c4bf8100000000")}};
589 std::vector<TransactionOutput> taproot2_spent_outputs;
590 taproot2_spent_outputs.emplace_back(taproot2_spent_script_pubkey0, 546);
591 taproot2_spent_outputs.emplace_back(taproot2_spent_script_pubkey1, 135125);
593 taproot2_spending_tx,
594 taproot2_spent_outputs,
597 taproot2_spent_script_pubkey0,
598 taproot2_spending_tx,
599 &taproot2_precomputed_txdata,
604 taproot2_spent_script_pubkey1,
605 taproot2_spending_tx,
606 &taproot2_precomputed_txdata,
616 .log_time_micros =
true,
617 .log_threadnames =
false,
618 .log_sourcelocations =
false,
619 .always_print_category_levels =
true,
632 Logger logger{std::make_unique<TestLog>()};
633 Logger logger_2{std::make_unique<TestLog>()};
635 Logger logger{std::make_unique<TestLog>()};
656 options.SetChainParams(params);
657 options.SetNotifications(std::make_shared<TestKernelNotifications>());
665 BlockHeader header_0{
hex_string_to_byte_vec(
"00e07a26beaaeee2e71d7eb19279545edbaf15de0999983626ec00000000000000000000579cf78b65229bfb93f4a11463af2eaa5ad91780f27f5d147a423bea5f7e4cdf2a47e268b4dd01173a9662ee")};
667 BlockHeader header_1{
hex_string_to_byte_vec(
"00c00020e7cb7b4de21d26d55bd384017b8bb9333ac3b2b55bed00000000000000000000d91b4484f801b99f03d36b9d26cfa83420b67f81da12d7e6c1e7f364e743c5ba9946e268b4dd011799c8533d")};
675 auto mainnet_block_1_header =
hex_string_to_byte_vec(
"010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e61bc6649ffff001d01e36299");
682 auto prev_hash = header.PrevHash();
685 auto raw_block =
hex_string_to_byte_vec(
"010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e61bc6649ffff001d01e362990101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac00000000");
686 Block block{raw_block};
701 CheckRange(block_tx.Transactions(), block_tx.CountTransactions());
708Context create_context(std::shared_ptr<TestKernelNotifications> notifications,
ChainType chain_type, std::shared_ptr<TestValidationInterface> validation_interface =
nullptr)
712 options.SetChainParams(params);
713 options.SetNotifications(notifications);
714 if (validation_interface) {
715 options.SetValidationInterface(validation_interface);
717 auto context{
Context{options}};
723 Logger logger{std::make_unique<TestLog>()};
724 auto test_directory{
TestDirectory{
"chainman_test_bitcoin_kernel"}};
728 ChainstateManagerOptions chainman_opts{context, test_directory.m_directory.string(), (test_directory.m_directory /
"blocks").
string()};
729 ChainMan chainman{context, chainman_opts};
735 ChainstateManagerOptions chainman_opts{context, test_directory.m_directory.string(), (test_directory.m_directory /
"blocks").
string()};
736 ChainMan chainman{context, chainman_opts};
740 auto valid_dir{test_directory.m_directory.string()};
741 std::vector<std::pair<std::string_view, std::string_view>> illegal_cases{
743 {valid_dir, {
nullptr, 0}},
745 {{
nullptr, 0}, {
nullptr, 0}},
747 for (
auto& [data_dir, blocks_dir] : illegal_cases) {
753 auto notifications{std::make_shared<TestKernelNotifications>()};
756 ChainstateManagerOptions chainman_opts{context, test_directory.m_directory.string(), (test_directory.m_directory /
"blocks").
string()};
758 BOOST_CHECK(!chainman_opts.SetWipeDbs(
true,
false));
760 BOOST_CHECK(chainman_opts.SetWipeDbs(
false,
true));
761 BOOST_CHECK(chainman_opts.SetWipeDbs(
false,
false));
762 ChainMan chainman{context, chainman_opts};
767 bool wipe_chainstate,
768 bool block_tree_db_in_memory,
769 bool chainstate_db_in_memory,
775 chainman_opts.SetWipeDbs(reindex, reindex);
777 if (wipe_chainstate) {
778 chainman_opts.SetWipeDbs(
false, wipe_chainstate);
780 if (block_tree_db_in_memory) {
781 chainman_opts.UpdateBlockTreeDbInMemory(block_tree_db_in_memory);
783 if (chainstate_db_in_memory) {
784 chainman_opts.UpdateChainstateDbInMemory(chainstate_db_in_memory);
787 auto chainman{std::make_unique<ChainMan>(context, chainman_opts)};
793 auto notifications{std::make_shared<TestKernelNotifications>()};
795 auto chainman{
create_chainman(test_directory,
true,
false,
false,
false, context)};
797 std::vector<std::string> import_files;
801 auto chain{chainman->GetChain()};
803 auto genesis_index{chain.Entries().front()};
805 auto genesis_block_raw{chainman->ReadBlock(genesis_index).value().ToBytes()};
806 auto first_index{chain.GetByHeight(0)};
807 auto first_block_raw{chainman->ReadBlock(genesis_index).value().ToBytes()};
809 auto height{first_index.GetHeight()};
812 auto next_index{chain.GetByHeight(first_index.GetHeight() + 1)};
814 auto next_block_data{chainman->ReadBlock(next_index).value().ToBytes()};
815 auto tip_index{chain.Entries().back()};
816 auto tip_block_data{chainman->ReadBlock(tip_index).value().ToBytes()};
817 auto second_index{chain.GetByHeight(1)};
818 auto second_block{chainman->ReadBlock(second_index).value()};
819 auto second_block_data{second_block.ToBytes()};
820 auto second_height{second_index.GetHeight()};
825 auto second_hash{second_index.GetHash()};
826 auto another_second_index{chainman->GetBlockTreeEntry(second_hash)};
828 auto another_second_height{another_second_index->GetHeight()};
829 auto second_block_hash{second_block.GetHash()};
830 check_equal(second_block_hash.ToBytes(), second_hash.ToBytes());
836 auto notifications{std::make_shared<TestKernelNotifications>()};
838 auto chainman{
create_chainman(test_directory,
false,
true,
false,
false, context)};
840 std::vector<std::string> import_files;
841 import_files.push_back((test_directory.
m_directory /
"blocks" /
"blk00000.dat").string());
847 auto notifications{std::make_shared<TestKernelNotifications>()};
848 auto validation_interface{std::make_shared<TestValidationInterface>()};
849 auto context{
create_context(notifications, ChainType::MAINNET, validation_interface)};
850 auto chainman{
create_chainman(test_directory,
false,
false,
false,
false, context)};
853 auto raw_block =
hex_string_to_byte_vec(
"010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e61bc6649ffff001d01e362990101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac00000000");
854 Block block{raw_block};
856 TransactionView tx{block.GetTransaction(block.CountTransactions() - 1)};
865 for (
auto transaction : block.Transactions()) {
868 auto output_counts = *(block.Transactions() | std::views::transform([](
const auto& tx) {
869 return tx.CountOutputs();
873 validation_interface->m_expected_valid_block.emplace(raw_block);
874 auto ser_block{block.ToBytes()};
876 bool new_block =
false;
877 BOOST_CHECK(chainman->ProcessBlock(block, &new_block));
880 validation_interface->m_expected_valid_block = std::nullopt;
883 BOOST_CHECK(!chainman->ProcessBlock(invalid_block, &new_block));
886 auto chain{chainman->GetChain()};
888 auto tip{chain.Entries().back()};
889 auto read_block{chainman->ReadBlock(tip)};
890 BOOST_REQUIRE(read_block);
891 check_equal(read_block.value().ToBytes(), raw_block);
895 Block read_block_2{*chainman->ReadBlock(tip_2)};
903 BOOST_CHECK(chainman->ProcessBlock(block, &new_block));
909 auto test_directory{
TestDirectory{
"mainnet_test_bitcoin_kernel"}};
917 std::array<std::byte, 32> test_hash;
918 std::array<std::byte, 32> test_hash_2;
919 for (
int i = 0; i < 32; ++i) {
920 test_hash[i] =
static_cast<std::byte
>(i);
921 test_hash_2[i] =
static_cast<std::byte
>(i + 1);
932 auto test_directory{
TestDirectory{
"block_tree_entry_test_bitcoin_kernel"}};
933 auto notifications{std::make_shared<TestKernelNotifications>()};
944 for (
size_t i{0}; i < 3; i++) {
946 bool new_block{
false};
947 chainman->ProcessBlock(block, &new_block);
951 auto chain{chainman->GetChain()};
952 auto entry_0{chain.GetByHeight(0)};
953 auto entry_1{chain.GetByHeight(1)};
954 auto entry_2{chain.GetByHeight(2)};
967 auto prev{entry_1.GetPrevious()};
974 auto in_memory_test_directory{
TestDirectory{
"in-memory_test_bitcoin_kernel"}};
976 auto notifications{std::make_shared<TestKernelNotifications>()};
978 auto chainman{
create_chainman(in_memory_test_directory,
false,
false,
true,
true, context)};
982 bool new_block{
false};
983 chainman->ProcessBlock(block, &new_block);
987 BOOST_CHECK(std::filesystem::exists(in_memory_test_directory.m_directory /
"blocks"));
988 BOOST_CHECK(!std::filesystem::exists(in_memory_test_directory.m_directory /
"blocks" /
"index"));
989 BOOST_CHECK(!std::filesystem::exists(in_memory_test_directory.m_directory /
"chainstate"));
996 auto test_directory{
TestDirectory{
"regtest_test_bitcoin_kernel"}};
998 auto notifications{std::make_shared<TestKernelNotifications>()};
1002 auto chainman{
create_chainman(test_directory,
false,
false,
false,
false, context)};
1007 BOOST_CHECK(state.GetBlockValidationResult() == BlockValidationResult::UNSET);
1008 BOOST_CHECK(chainman->ProcessBlockHeader(header, state));
1009 BOOST_CHECK(state.GetValidationMode() == ValidationMode::VALID);
1011 BOOST_CHECK(!chainman->GetChain().Contains(entry));
1014 BOOST_CHECK(hash == best_entry.GetHeader().Hash());
1024 auto chainman{
create_chainman(test_directory,
false,
false,
false,
false, context)};
1025 for (
size_t i{0}; i < mid; i++) {
1027 bool new_block{
false};
1028 BOOST_CHECK(chainman->ProcessBlock(block, &new_block));
1033 auto chainman{
create_chainman(test_directory,
false,
false,
false,
false, context)};
1037 bool new_block{
false};
1038 BOOST_CHECK(chainman->ProcessBlock(block, &new_block));
1042 auto chain = chainman->GetChain();
1043 auto tip = chain.Entries().back();
1044 auto read_block = chainman->ReadBlock(tip).value();
1047 auto tip_2 = tip.GetPrevious().value();
1048 auto read_block_2 = chainman->ReadBlock(tip_2).value();
1051 Txid txid = read_block.Transactions()[0].
Txid();
1052 Txid txid_2 = read_block_2.Transactions()[0].
Txid();
1057 auto find_transaction = [&chainman](
const TxidView& target_txid) -> std::optional<Transaction> {
1058 auto chain = chainman->GetChain();
1059 for (
const auto block_tree_entry : chain.Entries()) {
1060 auto block{chainman->ReadBlock(block_tree_entry)};
1062 if (transaction.Txid() == target_txid) {
1067 return std::nullopt;
1070 for (
const auto block_tree_entry : chain.Entries()) {
1071 auto block{chainman->ReadBlock(block_tree_entry)};
1072 for (
const auto transaction : block->Transactions()) {
1073 std::vector<TransactionInput> inputs;
1074 std::vector<TransactionOutput> spent_outputs;
1075 for (
const auto input : transaction.Inputs()) {
1077 if (point.
index() == std::numeric_limits<uint32_t>::max()) {
1080 inputs.emplace_back(input);
1082 std::optional<Transaction> tx = find_transaction(point.
Txid());
1085 spent_outputs.emplace_back(tx->GetOutput(point.
index()));
1087 BOOST_CHECK(inputs.size() == spent_outputs.size());
1090 for (
size_t i{0}; i < inputs.size(); ++i) {
1098 BlockSpentOutputs block_spent_outputs_prev{chainman->ReadBlockSpentOutputs(*tip.GetPrevious())};
1099 CheckHandle(block_spent_outputs, block_spent_outputs_prev);
1100 CheckRange(block_spent_outputs_prev.TxsSpentOutputs(), block_spent_outputs_prev.Count());
1106 TransactionSpentOutputs owned_transaction_spent_outputs_prev{block_spent_outputs_prev.GetTxSpentOutputs(block_spent_outputs_prev.Count() - 1)};
1107 CheckHandle(owned_transaction_spent_outputs, owned_transaction_spent_outputs_prev);
1108 CheckRange(transaction_spent_outputs.Coins(), transaction_spent_outputs.Count());
1111 CoinView coin{transaction_spent_outputs.GetCoin(transaction_spent_outputs.Count() - 1)};
1113 Coin owned_coin{coin};
1114 Coin owned_coin_prev{owned_transaction_spent_outputs_prev.GetCoin(owned_transaction_spent_outputs_prev.Count() - 1)};
1119 uint32_t coin_height = coin.GetConfirmationHeight();
1125 auto script_pubkey_bytes{script_pubkey.
ToBytes()};
1127 auto round_trip_script_pubkey{
ScriptPubkey(script_pubkey_bytes)};
1130 for (
const auto tx_spent_outputs : block_spent_outputs.TxsSpentOutputs()) {
1131 for (
const auto coins : tx_spent_outputs.Coins()) {
1132 BOOST_CHECK_GT(coins.GetOutput().Amount(), 1);
1136 CheckRange(chain.Entries(), chain.CountEntries());
1139 std::optional<Block> block{chainman->ReadBlock(entry)};
1147 BOOST_CHECK_GT(output.
Amount(), 1);
1154 for (
const auto entry : chain.Entries()) {
1161 std::filesystem::remove_all(test_directory.m_directory /
"blocks" /
"blk00000.dat");
1162 BOOST_CHECK(!chainman->ReadBlock(tip_2).has_value());
1163 std::filesystem::remove_all(test_directory.m_directory /
"blocks" /
"rev00000.dat");
constexpr std::array< std::string_view, 206 > REGTEST_BLOCK_DATA
void HeaderTipHandler(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) override
void FlushErrorHandler(std::string_view error) override
void FatalErrorHandler(std::string_view error) override
void WarningUnsetHandler(Warning warning) override
void WarningSetHandler(Warning warning, std::string_view message) override
void LogMessage(std::string_view message)
void PowValidBlock(BlockTreeEntry entry, Block block) override
void BlockDisconnected(Block block, BlockTreeEntry entry) override
std::optional< std::string > m_expected_valid_block
void BlockChecked(Block block, BlockValidationStateView state) override
void BlockConnected(Block block, BlockTreeEntry entry) override
std::vector< std::byte > ToBytes() const
std::optional< BlockTreeEntry > GetPrevious() const
ValidationMode GetValidationMode() const
BlockValidationResult GetBlockValidationResult() const
void SetWorkerThreads(int worker_threads)
std::vector< std::byte > ToBytes() const
bool Verify(int64_t amount, const Transaction &tx_to, const PrecomputedTransactionData *precomputed_txdata, unsigned int input_index, ScriptVerificationFlags flags, ScriptVerifyStatus &status) const
size_t CountInputs() const
std::vector< std::byte > ToBytes() const
ScriptPubkeyView GetScriptPubkey() const
Txid(const TxidView &view)
const CType * get() const
#define T(expected, seed, data)
void logging_set_options(const btck_LoggingOptions &logging_options)
void logging_set_level_category(LogCategory category, LogLevel level)
void logging_enable_category(LogCategory category)
void logging_disable_category(LogCategory category)
static const auto INVALID
A stack representing the lack of any (dis)satisfactions.
#define BOOST_CHECK_THROW(stmt, excMatch)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
static bool Verify(const CScript &scriptSig, const CScript &scriptPubKey, bool fStrict, ScriptError &err)
std::filesystem::path m_directory
TestDirectory(std::string directory_name)
Options controlling the format of log messages.
int log_timestamps
Prepend a timestamp to log messages.
std::string byte_span_to_hex_string_reversed(std::span< const std::byte > bytes)
void chainman_reindex_chainstate_test(TestDirectory &test_directory)
Context create_context(std::shared_ptr< TestKernelNotifications > notifications, ChainType chain_type, std::shared_ptr< TestValidationInterface > validation_interface=nullptr)
void run_verify_test(const ScriptPubkey &spent_script_pubkey, const Transaction &spending_tx, const PrecomputedTransactionData *precomputed_txdata, int64_t amount, unsigned int input_index, bool taproot)
std::vector< std::byte > hex_string_to_byte_vec(std::string_view hex)
void chainman_reindex_test(TestDirectory &test_directory)
BOOST_AUTO_TEST_CASE(btck_transaction_tests)
void check_equal(std::span< const std::byte > _actual, std::span< const std::byte > _expected, bool equal=true)
void CheckHandle(T object, T distinct_object)
std::unique_ptr< ChainMan > create_chainman(TestDirectory &test_directory, bool reindex, bool wipe_chainstate, bool block_tree_db_in_memory, bool chainstate_db_in_memory, Context &context)
constexpr auto VERIFY_ALL_PRE_SEGWIT
void CheckRange(const RangeType &range, size_t expected_size)
void chainman_mainnet_validation_test(TestDirectory &test_directory)
std::string random_string(uint32_t length)
constexpr auto VERIFY_ALL_PRE_TAPROOT