diff --git a/libraries/chain/block_log.cpp b/libraries/chain/block_log.cpp index 946e4764b8..d68a54e039 100644 --- a/libraries/chain/block_log.cpp +++ b/libraries/chain/block_log.cpp @@ -661,10 +661,11 @@ namespace eosio { namespace chain { signed_block_ptr read_block_by_num(uint32_t block_num) final { try { - uint64_t pos = get_block_pos(block_num); + auto [ pos, size ] = get_block_position_and_size(block_num); if (pos != block_log::npos) { block_file.seek(pos); - return read_block(block_file, block_num); + fc::datastream_mirror ds(block_file, size); + return read_block(ds, block_num); } return retry_read_block_by_num(block_num); } @@ -801,7 +802,7 @@ namespace eosio { namespace chain { void reset(const genesis_state& gs, const signed_block_ptr& first_block) override { this->reset(1, gs, default_initial_version); - this->append(first_block, first_block->calculate_id(), fc::raw::pack(*first_block)); + this->append(first_block, first_block->calculate_id(), first_block->packed_signed_block()); } void reset(const chain_id_type& chain_id, uint32_t first_block_num) override { @@ -1094,9 +1095,13 @@ namespace eosio { namespace chain { } signed_block_ptr retry_read_block_by_num(uint32_t block_num) final { - auto ds = catalog.ro_stream_for_block(block_num); - if (ds) - return read_block(*ds, block_num); + uint64_t block_size = 0; + + auto ds = catalog.ro_stream_and_size_for_block(block_num, block_size); + if (ds) { + fc::datastream_mirror dsm(*ds, block_size); + return read_block(dsm, block_num); + } return {}; } @@ -1277,9 +1282,8 @@ namespace eosio { namespace chain { } void block_log::append(const signed_block_ptr& b, const block_id_type& id) { - std::vector packed_block = fc::raw::pack(*b); std::lock_guard g(my->mtx); - my->append(b, id, packed_block); + my->append(b, id, b->packed_signed_block()); } void block_log::append(const signed_block_ptr& b, const block_id_type& id, const std::vector& packed_block) { diff --git a/libraries/chain/block_state.cpp b/libraries/chain/block_state.cpp index 722f15016e..26e48ef57d 100644 --- a/libraries/chain/block_state.cpp +++ b/libraries/chain/block_state.cpp @@ -114,7 +114,7 @@ block_state::block_state(const block_header_state& bhs, , cached_trxs(std::move(trx_metas)) , action_mroot(action_mroot) { - mutable_signed_block_ptr new_block = std::make_shared(signed_block_header{bhs.header}); + mutable_block_ptr new_block = signed_block::create_mutable_block(signed_block_header{bhs.header}); new_block->transactions = std::move(trx_receipts); if( qc ) { @@ -125,7 +125,7 @@ block_state::block_state(const block_header_state& bhs, sign(*new_block, block_id, signer, valid_block_signing_authority); - block = std::move(new_block); + block = signed_block::create_signed_block(std::move(new_block)); } // Used for transition from dpos to Savanna. diff --git a/libraries/chain/block_state_legacy.cpp b/libraries/chain/block_state_legacy.cpp index cd681cc085..9152140f1e 100644 --- a/libraries/chain/block_state_legacy.cpp +++ b/libraries/chain/block_state_legacy.cpp @@ -64,7 +64,7 @@ namespace eosio::chain { {} block_state_legacy::block_state_legacy( pending_block_header_state_legacy&& cur, - mutable_signed_block_ptr&& b, + mutable_block_ptr&& b, deque&& trx_metas, const std::optional& action_receipt_digests_savanna, const protocol_feature_set& pfs, @@ -72,7 +72,7 @@ namespace eosio::chain { const signer_callback_type& signer ) :block_header_state_legacy( inject_additional_signatures( std::move(cur), *b, pfs, validator, signer ) ) - ,block( std::move(b) ) + ,block( signed_block::create_signed_block(std::move(b)) ) ,_pub_keys_recovered( true ) // called by produce_block so signature recovery of trxs must have been done ,_cached_trxs( std::move(trx_metas) ) ,action_mroot_savanna( action_receipt_digests_savanna ? std::optional(calculate_merkle(*action_receipt_digests_savanna)) : std::nullopt ) diff --git a/libraries/chain/controller.cpp b/libraries/chain/controller.cpp index fb877b0201..8bf0b981b4 100644 --- a/libraries/chain/controller.cpp +++ b/libraries/chain/controller.cpp @@ -231,7 +231,7 @@ struct assembled_block { block_id_type id; pending_block_header_state_legacy pending_block_header_state; deque trx_metas; - mutable_signed_block_ptr unsigned_block; + mutable_block_ptr unsigned_block; // if the unsigned_block pre-dates block-signing authorities this may be present. std::optional new_producer_authority_cache; @@ -728,7 +728,7 @@ struct building_block { } // in dpos, we create a signed_block here. In IF mode, we do it later (when we are ready to sign it) - auto block_ptr = std::make_shared(bb.pending_block_header_state.make_block_header( + auto block_ptr = signed_block::create_mutable_block(bb.pending_block_header_state.make_block_header( transaction_mroot, action_mroot, bb.new_pending_producer_schedule, std::move(new_finalizer_policy), vector(bb.new_protocol_feature_activations), pfs)); @@ -1567,20 +1567,8 @@ struct controller_impl { return irreversible_mode() || bsp->is_valid(); }; - using packed_block_future = std::future>; - std::vector v; - if (!irreversible_mode()) { - v.reserve( branch.size() ); - for( auto bitr = branch.rbegin(); bitr != branch.rend() && should_process(*bitr); ++bitr ) { - v.emplace_back( post_async_task( thread_pool.get_executor(), [b=(*bitr)->block]() { return fc::raw::pack(*b); } ) ); - } - } - auto it = v.begin(); - for( auto bitr = branch.rbegin(); bitr != branch.rend() && should_process(*bitr); ++bitr ) { - packed_block_future f; if (irreversible_mode()) { - f = post_async_task( thread_pool.get_executor(), [b=(*bitr)->block]() { return fc::raw::pack(*b); } ); result = apply_irreversible_block(fork_db, *bitr); if (result != controller::apply_blocks_result::complete) break; @@ -1590,7 +1578,7 @@ struct controller_impl { // blog.append could fail due to failures like running out of space. // Do it before commit so that in case it throws, DB can be rolled back. - blog.append( (*bitr)->block, (*bitr)->id(), irreversible_mode() ? f.get() : it++->get() ); + blog.append( (*bitr)->block, (*bitr)->id(), (*bitr)->block->packed_signed_block() ); db.commit( (*bitr)->block_num() ); root_id = (*bitr)->id(); @@ -1659,7 +1647,7 @@ struct controller_impl { auto head = std::make_shared(); static_cast(*head) = genheader; head->activated_protocol_features = std::make_shared(); // no activated protocol features in genesis - head->block = std::make_shared(genheader.header); + head->block = signed_block::create_signed_block(signed_block::create_mutable_block(genheader.header)); chain_head = block_handle{head}; db.set_revision( chain_head.block_num() ); @@ -5531,7 +5519,7 @@ signed_block_ptr controller::fetch_block_by_number( uint32_t block_num )const { std::vector controller::fetch_serialized_block_by_number( uint32_t block_num)const { try { if (signed_block_ptr b = my->fork_db_fetch_block_on_best_branch_by_num(block_num)) { - return fc::raw::pack(*b); + return b->packed_signed_block(); } return my->blog.read_serialized_block_by_num(block_num); diff --git a/libraries/chain/deep_mind.cpp b/libraries/chain/deep_mind.cpp index 91f55b62cd..ed868133ef 100644 --- a/libraries/chain/deep_mind.cpp +++ b/libraries/chain/deep_mind.cpp @@ -91,7 +91,6 @@ namespace eosio::chain { { assert(b); assert(active_proposer_policy); - auto packed_blk = fc::raw::pack(*b); auto finality_data = fc::raw::pack(fd); auto packed_proposer_policy = fc::raw::pack(*active_proposer_policy); auto packed_finalizer_policy = fc::raw::pack(active_finalizer_policy); @@ -100,7 +99,7 @@ namespace eosio::chain { ("id", id) ("num", b->block_num()) ("lib", lib) - ("blk", fc::to_hex(packed_blk)) + ("blk", fc::to_hex(b->packed_signed_block())) ("fd", fc::to_hex(finality_data)) ("pp", fc::to_hex(packed_proposer_policy)) ("fp", fc::to_hex(packed_finalizer_policy)) diff --git a/libraries/chain/include/eosio/chain/block.hpp b/libraries/chain/include/eosio/chain/block.hpp index a427db8806..19618650b2 100644 --- a/libraries/chain/include/eosio/chain/block.hpp +++ b/libraries/chain/include/eosio/chain/block.hpp @@ -85,21 +85,26 @@ namespace eosio { namespace chain { using block_extension = block_extension_types::block_extension_t; + using signed_block_ptr = std::shared_ptr; + using mutable_block_ptr = std::unique_ptr; + /** */ struct signed_block : public signed_block_header{ private: signed_block( const signed_block& ) = default; + explicit signed_block( const signed_block_header& h ):signed_block_header(h){} public: signed_block() = default; - explicit signed_block( const signed_block_header& h ):signed_block_header(h){} signed_block( signed_block&& ) = default; signed_block& operator=(const signed_block&) = delete; signed_block& operator=(signed_block&&) = default; - signed_block clone() const { return *this; } + mutable_block_ptr clone() const { return std::unique_ptr(new signed_block(*this)); } + static mutable_block_ptr create_mutable_block(const signed_block_header& h) { return std::unique_ptr(new signed_block(h)); } + static signed_block_ptr create_signed_block(mutable_block_ptr&& b) { b->pack(); return b; } deque transactions; /// new or generated transactions - extensions_type block_extensions; + extensions_type block_extensions; flat_multimap validate_and_extract_extensions()const; std::optional extract_extension(uint16_t extension_id)const; @@ -108,9 +113,17 @@ namespace eosio { namespace chain { return std::get(*extract_extension(Ext::extension_id())); } bool contains_extension(uint16_t extension_id)const; + + const bytes& packed_signed_block() const { assert(!packed_block.empty()); return packed_block; } + + private: + friend struct block_state; + friend struct block_state_legacy; + template friend void fc::raw::unpack(Stream& s, eosio::chain::signed_block& v); + void pack() { packed_block = fc::raw::pack( *this ); } + + bytes packed_block; // packed this }; - using signed_block_ptr = std::shared_ptr; - using mutable_signed_block_ptr = std::shared_ptr; struct producer_confirmation { block_id_type block_id; @@ -129,3 +142,20 @@ FC_REFLECT_DERIVED(eosio::chain::transaction_receipt, (eosio::chain::transaction FC_REFLECT(eosio::chain::additional_block_signatures_extension, (signatures)); FC_REFLECT(eosio::chain::quorum_certificate_extension, (qc)); FC_REFLECT_DERIVED(eosio::chain::signed_block, (eosio::chain::signed_block_header), (transactions)(block_extensions) ) + +namespace fc::raw { + template + void unpack(Stream& s, eosio::chain::signed_block& v) { + try { + if constexpr (requires { s.extract_mirror(); }) { + fc::reflector::visit( fc::raw::detail::unpack_object_visitor( v, s ) ); + v.packed_block = s.extract_mirror(); + } else { + fc::datastream_mirror ds(s, sizeof(eosio::chain::signed_block) + 4096); + fc::reflector::visit( fc::raw::detail::unpack_object_visitor, eosio::chain::signed_block>( v, ds ) ); + v.packed_block = ds.extract_mirror(); + } + } FC_RETHROW_EXCEPTIONS(warn, "error unpacking signed_block") + } +} + diff --git a/libraries/chain/include/eosio/chain/block_state_legacy.hpp b/libraries/chain/include/eosio/chain/block_state_legacy.hpp index 4484db593e..1e98d6f2e2 100644 --- a/libraries/chain/include/eosio/chain/block_state_legacy.hpp +++ b/libraries/chain/include/eosio/chain/block_state_legacy.hpp @@ -21,7 +21,7 @@ namespace eosio::chain { ); block_state_legacy( pending_block_header_state_legacy&& cur, - mutable_signed_block_ptr&& b, // unsigned block + mutable_block_ptr&& b, // unsigned block deque&& trx_metas, const std::optional& action_receipt_digests_savanna, const protocol_feature_set& pfs, diff --git a/libraries/libfc/include/fc/io/raw_fwd.hpp b/libraries/libfc/include/fc/io/raw_fwd.hpp index 8f159718ee..fa9a2cf456 100644 --- a/libraries/libfc/include/fc/io/raw_fwd.hpp +++ b/libraries/libfc/include/fc/io/raw_fwd.hpp @@ -13,6 +13,10 @@ #include #include +namespace eosio::chain { + struct signed_block; +} + namespace fc { class time_point; class time_point_sec; @@ -32,6 +36,8 @@ namespace fc { template inline size_t pack_size( const T& v ); + template void unpack(Stream& s, eosio::chain::signed_block& v); + template inline void pack( Stream& s, const fc::fixed_string& u ); template inline void unpack( Stream& s, fc::fixed_string& u ); diff --git a/plugins/net_plugin/net_plugin.cpp b/plugins/net_plugin/net_plugin.cpp index 805f5ef63e..bd649a3d35 100644 --- a/plugins/net_plugin/net_plugin.cpp +++ b/plugins/net_plugin/net_plugin.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -3190,8 +3191,10 @@ namespace eosio { return true; } - auto ds = pending_message_buffer.create_datastream(); - fc::raw::unpack( ds, which ); + auto mb_ds = pending_message_buffer.create_datastream(); + fc::raw::unpack( mb_ds, which ); + + fc::datastream_mirror ds(mb_ds, message_length); shared_ptr ptr = std::make_shared(); fc::raw::unpack( ds, *ptr ); diff --git a/plugins/test_control_plugin/test_control_plugin.cpp b/plugins/test_control_plugin/test_control_plugin.cpp index 4859403b7a..281de917bb 100644 --- a/plugins/test_control_plugin/test_control_plugin.cpp +++ b/plugins/test_control_plugin/test_control_plugin.cpp @@ -126,7 +126,7 @@ void test_control_plugin_impl::swap_action_in_block(const chain::signed_block_pt return; } - auto copy_b = std::make_shared(b->clone()); + auto copy_b = b->clone(); copy_b->previous = b->calculate_id(); copy_b->block_extensions.clear(); // remove QC extension since header will claim same as previous block copy_b->timestamp = b->timestamp.next(); @@ -159,12 +159,13 @@ void test_control_plugin_impl::swap_action_in_block(const chain::signed_block_pt copy_b->transaction_mroot = chain::calculate_merkle( std::move(trx_digests) ); // Re-sign the block copy_b->producer_signature = _swap_on_options.blk_priv_key.sign(copy_b->calculate_id()); + auto copy_b_signed = signed_block::create_signed_block(std::move(copy_b)); // will be processed on the next start_block if is_new_best_head - const auto&[add_result, bh] = _chain.accept_block(copy_b->calculate_id(), copy_b); + const auto&[add_result, bh] = _chain.accept_block(copy_b_signed->calculate_id(), copy_b_signed); ilog("Swapped action ${f} to ${t}, add_result ${a}, block ${bn}", ("f", _swap_on_options.from)("t", _swap_on_options.to)("a", add_result)("bn", bh ? bh->block_num() : 0)); - app().find_plugin()->broadcast_block(copy_b, copy_b->calculate_id()); + app().find_plugin()->broadcast_block(copy_b_signed, copy_b_signed->calculate_id()); if (_swap_on_options.shutdown) app().quit(); reset_swap_action(); diff --git a/unittests/block_log_extract.cpp b/unittests/block_log_extract.cpp index 32bba2cb95..c98b2f12fe 100644 --- a/unittests/block_log_extract.cpp +++ b/unittests/block_log_extract.cpp @@ -11,7 +11,7 @@ using namespace eosio::chain; struct block_log_extract_fixture { block_log_extract_fixture() { log.emplace(dir.path()); - log->reset(genesis_state(), std::make_shared()); + log->reset(genesis_state(), signed_block::create_signed_block(signed_block::create_mutable_block({}))); BOOST_REQUIRE_EQUAL(log->first_block_num(), 1u); BOOST_REQUIRE_EQUAL(log->head()->block_num(), 1u); for(uint32_t i = 2; i < 13; ++i) { @@ -21,9 +21,10 @@ struct block_log_extract_fixture { }; void add(uint32_t index) { - auto p = std::make_shared(); + auto p = signed_block::create_mutable_block({}); p->previous._hash[0] = fc::endian_reverse_u32(index-1); - log->append(p, p->calculate_id()); + auto sp = signed_block::create_signed_block(std::move(p)); + log->append(sp, sp->calculate_id()); } static void rename_blocks_files(std::filesystem::path dir) { diff --git a/unittests/block_log_get_block_tests.cpp b/unittests/block_log_get_block_tests.cpp index 61019859af..792d80e394 100644 --- a/unittests/block_log_get_block_tests.cpp +++ b/unittests/block_log_get_block_tests.cpp @@ -14,14 +14,15 @@ struct block_log_get_block_fixture { log.emplace(block_dir); - log->reset(genesis_state(), std::make_shared()); + log->reset(genesis_state(), signed_block::create_signed_block(signed_block::create_mutable_block({}))); BOOST_REQUIRE_EQUAL(log->first_block_num(), 1u); BOOST_REQUIRE_EQUAL(log->head()->block_num(), 1u); for(uint32_t i = 2; i < last_block_num + 1; ++i) { - auto p = std::make_shared(); + auto p = signed_block::create_mutable_block({}); p->previous._hash[0] = fc::endian_reverse_u32(i-1); - log->append(p, p->calculate_id()); + auto sp = signed_block::create_signed_block(std::move(p)); + log->append(sp, sp->calculate_id()); } BOOST_REQUIRE_EQUAL(log->head()->block_num(), last_block_num); }; diff --git a/unittests/block_tests.cpp b/unittests/block_tests.cpp index b03f89080d..faa0c4939f 100644 --- a/unittests/block_tests.cpp +++ b/unittests/block_tests.cpp @@ -17,7 +17,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE( block_with_invalid_tx_test, T, testers ) auto b = main.produce_block(); // Make a copy of the valid block and corrupt the transaction - auto copy_b = std::make_shared(b->clone()); + auto copy_b = b->clone(); auto signed_tx = std::get(copy_b->transactions.back().trx).get_signed_transaction(); auto& act = signed_tx.actions.back(); auto act_data = act.template data_as(); @@ -53,7 +53,8 @@ BOOST_AUTO_TEST_CASE_TEMPLATE( block_with_invalid_tx_test, T, testers ) // Push block with invalid transaction to other chain T validator; - auto [best_head, obh] = validator.control->accept_block( copy_b->calculate_id(), copy_b ); + auto signed_copy_b = signed_block::create_signed_block(std::move(copy_b)); + auto [best_head, obh] = validator.control->accept_block( signed_copy_b->calculate_id(), signed_copy_b ); BOOST_REQUIRE(obh); validator.control->abort_block(); BOOST_REQUIRE_EXCEPTION(validator.control->apply_blocks( {}, trx_meta_cache_lookup{} ), fc::exception , @@ -72,7 +73,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE( block_with_invalid_tx_mroot_test, T, testers ) auto b = main.produce_block(); // Make a copy of the valid block and corrupt the transaction - auto copy_b = std::make_shared(b->clone()); + auto copy_b = b->clone(); const auto& packed_trx = std::get(copy_b->transactions.back().trx); auto signed_tx = packed_trx.get_signed_transaction(); @@ -96,7 +97,8 @@ BOOST_AUTO_TEST_CASE_TEMPLATE( block_with_invalid_tx_mroot_test, T, testers ) // Push block with invalid transaction to other chain T validator; - BOOST_REQUIRE_EXCEPTION(validator.control->accept_block( copy_b->calculate_id(), copy_b ), fc::exception, + auto signed_copy_b = signed_block::create_signed_block(std::move(copy_b)); + BOOST_REQUIRE_EXCEPTION(validator.control->accept_block( signed_copy_b->calculate_id(), signed_copy_b ), fc::exception, [] (const fc::exception &e)->bool { return e.code() == block_validate_exception::code_value && e.to_detail_string().find("invalid block transaction merkle root") != std::string::npos; @@ -110,7 +112,7 @@ std::pair corrupt_trx_in_block(T& main, acco signed_block_ptr b = main.produce_block_no_validation(); // Make a copy of the valid block and corrupt the transaction - auto copy_b = std::make_shared(b->clone()); + auto copy_b = b->clone(); const auto& packed_trx = std::get(copy_b->transactions.back().trx); auto signed_tx = packed_trx.get_signed_transaction(); // Corrupt one signature @@ -141,7 +143,7 @@ std::pair corrupt_trx_in_block(T& main, acco copy_b->producer_signature = main.get_private_key(b->producer, "active").sign(sig_digest); } - return std::pair(b, copy_b); + return std::pair(b, signed_block::create_signed_block(std::move(copy_b))); } // verify that a block with a transaction with an incorrect signature, is blindly accepted from a trusted producer @@ -389,7 +391,7 @@ BOOST_FIXTURE_TEST_CASE( invalid_qc_claim_block_num_test, validating_tester ) { auto b = produce_block_no_validation(); // Make a copy of the valid block - auto copy_b = std::make_shared(b->clone()); + auto copy_b = b->clone(); // Retrieve finality extension auto fin_ext_id = finality_extension::extension_id(); @@ -412,7 +414,7 @@ BOOST_FIXTURE_TEST_CASE( invalid_qc_claim_block_num_test, validating_tester ) { copy_b->producer_signature = get_private_key(config::system_account_name, "active").sign(copy_b->calculate_id()); // Push the corrupted block. It must be rejected. - BOOST_REQUIRE_EXCEPTION(validate_push_block(copy_b), fc::exception, + BOOST_REQUIRE_EXCEPTION(validate_push_block(signed_block::create_signed_block(std::move(copy_b))), fc::exception, [] (const fc::exception &e)->bool { return e.code() == invalid_qc_claim::code_value && e.to_detail_string().find("that is greater than the previous block number") != std::string::npos; diff --git a/unittests/checktime_tests.cpp b/unittests/checktime_tests.cpp index 591bf1dc0d..07fa7f0352 100644 --- a/unittests/checktime_tests.cpp +++ b/unittests/checktime_tests.cpp @@ -104,7 +104,7 @@ BOOST_AUTO_TEST_CASE( checktime_interrupt_test) { try { BOOST_REQUIRE_EQUAL( b->transactions.size(), 1 ); // Make a copy of the valid block and swicth the checktime_pass transaction with checktime_failure - auto copy_b = std::make_shared(b->clone()); + auto copy_b = b->clone(); auto signed_tx = std::get(copy_b->transactions.back().trx).get_signed_transaction(); auto& act = signed_tx.actions.back(); constexpr chain::name checktime_fail_n{WASM_TEST_ACTION("test_checktime", "checktime_failure")}; @@ -132,7 +132,7 @@ BOOST_AUTO_TEST_CASE( checktime_interrupt_test) { try { } ); // apply block, caught in an "infinite" loop - BOOST_CHECK_EXCEPTION( other.push_block(copy_b), fc::exception, + BOOST_CHECK_EXCEPTION( other.push_block(signed_block::create_signed_block(std::move(copy_b))), fc::exception, [](const fc::exception& e) { return e.code() == interrupt_exception::code_value; } ); th.join(); diff --git a/unittests/forked_tests.cpp b/unittests/forked_tests.cpp index 4a8dedd7f4..8979ade3ce 100644 --- a/unittests/forked_tests.cpp +++ b/unittests/forked_tests.cpp @@ -66,7 +66,7 @@ BOOST_AUTO_TEST_CASE( fork_with_bad_block ) try { auto& fork = forks.at(j); if (j <= i) { - auto copy_b = std::make_shared(b->clone()); + auto copy_b = b->clone(); if (j == i) { // corrupt this block fork.block_merkle = remote.control->head_block_state_legacy()->blockroot_merkle; @@ -82,8 +82,9 @@ BOOST_AUTO_TEST_CASE( fork_with_bad_block ) try { copy_b->producer_signature = remote.get_private_key("b"_n, "active").sign(sig_digest); // add this new block to our corrupted block merkle - fork.block_merkle.append(copy_b->calculate_id()); - fork.blocks.emplace_back(copy_b); + auto signed_copy_b = signed_block::create_signed_block(std::move(copy_b)); + fork.block_merkle.append(signed_copy_b->calculate_id()); + fork.blocks.emplace_back(signed_copy_b); } else { fork.blocks.emplace_back(b); } @@ -242,10 +243,10 @@ BOOST_AUTO_TEST_CASE( forking ) try { } wlog( "end push c2 blocks to c1" ); wlog( "now push dan's block to c1 but first corrupt it so it is a bad block" ); - signed_block bad_block{b->clone()}; - bad_block.action_mroot = bad_block.previous; - auto bad_id = bad_block.calculate_id(); - BOOST_REQUIRE_EXCEPTION(c.control->accept_block(bad_id, std::make_shared(bad_block.clone())), + auto bad_block = b->clone(); + bad_block->action_mroot = bad_block->previous; + auto bad_id = bad_block->calculate_id(); + BOOST_REQUIRE_EXCEPTION(c.control->accept_block(bad_id, signed_block::create_signed_block(std::move(bad_block))), fc::exception, [] (const fc::exception& ex)->bool { return ex.to_detail_string().find("block signed by unexpected key") != std::string::npos; }); diff --git a/unittests/forked_tests_savanna.cpp b/unittests/forked_tests_savanna.cpp index 9600517241..a185d1b413 100644 --- a/unittests/forked_tests_savanna.cpp +++ b/unittests/forked_tests_savanna.cpp @@ -113,7 +113,7 @@ BOOST_FIXTURE_TEST_CASE(fork_with_bad_block_savanna, savanna_cluster::cluster_t) auto& fork = forks.at(j); if (j <= i) { - auto copy_b = std::make_shared(b->clone()); + auto copy_b = b->clone(); if (j == i) { // corrupt this block (forks[j].blocks[j] is corrupted) copy_b->action_mroot._hash[0] ^= 0x1ULL; @@ -126,7 +126,7 @@ BOOST_FIXTURE_TEST_CASE(fork_with_bad_block_savanna, savanna_cluster::cluster_t) copy_b->producer_signature = pk.sign(copy_b->calculate_id()); // add this new block to our corrupted block merkle - fork.blocks.emplace_back(copy_b); + fork.blocks.emplace_back(signed_block::create_signed_block(std::move(copy_b))); } else { fork.blocks.emplace_back(b); } diff --git a/unittests/producer_schedule_tests.cpp b/unittests/producer_schedule_tests.cpp index ebf46f3661..0679e85b12 100644 --- a/unittests/producer_schedule_tests.cpp +++ b/unittests/producer_schedule_tests.cpp @@ -678,7 +678,7 @@ BOOST_AUTO_TEST_CASE( extra_signatures_test ) try { main.produce_blocks(3); BOOST_REQUIRE( main.control->pending_block_producer() == "alice"_n ); - mutable_signed_block_ptr b; + mutable_block_ptr b; // Generate a valid block and then corrupt it by adding an extra signature. { @@ -694,7 +694,7 @@ BOOST_AUTO_TEST_CASE( extra_signatures_test ) try { BOOST_REQUIRE( valid_block->producer == "alice"_n ); // Make a copy of pointer to the valid block. - b = std::make_shared(valid_block->clone()); + b = valid_block->clone(); BOOST_REQUIRE_EQUAL( b->block_extensions.size(), 1u ); // Extract the existing signatures. @@ -717,7 +717,8 @@ BOOST_AUTO_TEST_CASE( extra_signatures_test ) try { } // Push block with extra signature to the main chain. - BOOST_REQUIRE_EXCEPTION( main.push_block(b), wrong_signing_key, fc_exception_message_starts_with("number of block signatures") ); + auto sb = signed_block::create_signed_block(std::move(b)); + BOOST_REQUIRE_EXCEPTION( main.push_block(sb), wrong_signing_key, fc_exception_message_starts_with("number of block signatures") ); } FC_LOG_AND_RETHROW() diff --git a/unittests/protocol_feature_tests.cpp b/unittests/protocol_feature_tests.cpp index 1c9f66a57e..d6f48de8bf 100644 --- a/unittests/protocol_feature_tests.cpp +++ b/unittests/protocol_feature_tests.cpp @@ -1595,7 +1595,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(producer_schedule_change_extension_test, T, tester { // ensure producer_schedule_change_extension is rejected // create a bad block that has the producer schedule change extension before the feature upgrade - auto bad_block = std::make_shared(last_legacy_block->clone()); + auto bad_block = last_legacy_block->clone(); emplace_extension( bad_block->header_extensions, producer_schedule_change_extension::extension_id(), @@ -1609,14 +1609,14 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(producer_schedule_change_extension_test, T, tester // ensure it is rejected as an unknown extension BOOST_REQUIRE_EXCEPTION( - remote.push_block(bad_block), producer_schedule_exception, + remote.push_block(signed_block::create_signed_block(std::move(bad_block))), producer_schedule_exception, fc_exception_message_is( "Block header producer_schedule_change_extension before activation of WTMsig Block Signatures" ) ); } { // ensure that non-null new_producers is accepted (and fails later in validation) // create a bad block that has the producer schedule change extension before the feature upgrade - auto bad_block = std::make_shared(last_legacy_block->clone()); + auto bad_block = last_legacy_block->clone(); bad_block->new_producers = legacy::producer_schedule_type{remote.control->active_producers().version + 1, {}}; // re-sign the bad block @@ -1626,7 +1626,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(producer_schedule_change_extension_test, T, tester // ensure it is accepted (but rejected because it doesn't match expected state) BOOST_REQUIRE_EXCEPTION( - remote.push_block(bad_block), wrong_signing_key, + remote.push_block(signed_block::create_signed_block(std::move(bad_block))), wrong_signing_key, fc_exception_message_starts_with( "block signed by unexpected key" ) ); } @@ -1638,7 +1638,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(producer_schedule_change_extension_test, T, tester { // create a bad block that has the producer schedule change extension that is valid but not warranted by actions in the block - auto bad_block = std::make_shared(first_new_block->clone()); + auto bad_block = first_new_block->clone(); emplace_extension( bad_block->header_extensions, producer_schedule_change_extension::extension_id(), @@ -1652,14 +1652,14 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(producer_schedule_change_extension_test, T, tester // ensure it is rejected because it doesn't match expected state (but the extention was accepted) BOOST_REQUIRE_EXCEPTION( - remote.push_block(bad_block), wrong_signing_key, + remote.push_block(signed_block::create_signed_block(std::move(bad_block))), wrong_signing_key, fc_exception_message_starts_with( "block signed by unexpected key" ) ); } { // ensure that non-null new_producers is rejected // create a bad block that has the producer schedule change extension before the feature upgrade - auto bad_block = std::make_shared(first_new_block->clone()); + auto bad_block = first_new_block->clone(); bad_block->new_producers = legacy::producer_schedule_type{remote.control->active_producers().version + 1, {}}; // re-sign the bad block @@ -1669,7 +1669,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(producer_schedule_change_extension_test, T, tester // ensure it is rejected because the new_producers field is not null BOOST_REQUIRE_EXCEPTION( - remote.push_block(bad_block), producer_schedule_exception, + remote.push_block(signed_block::create_signed_block(std::move(bad_block))), producer_schedule_exception, fc_exception_message_is( "Block header contains legacy producer schedule outdated by activation of WTMsig Block Signatures" ) ); } @@ -2219,7 +2219,7 @@ BOOST_AUTO_TEST_CASE( block_validation_after_stage_1_test ) { try { auto b = tester1.produce_block(); // Make a copy of the block - auto copy_b = std::make_shared(b->clone()); + auto copy_b = b->clone(); // Retrieve the last transaction auto signed_tx = std::get(copy_b->transactions.back().trx).get_signed_transaction(); // Make a delayed transaction by forcing delay_sec greater than 0 @@ -2253,7 +2253,8 @@ BOOST_AUTO_TEST_CASE( block_validation_after_stage_1_test ) { try { tester2.produce_block(); // Push the block with delayed transaction to the second chain - auto [best_head, obh] = tester2.control->accept_block( copy_b->calculate_id(), copy_b ); + auto signed_copy_b = signed_block::create_signed_block(std::move(copy_b)); + auto [best_head, obh] = tester2.control->accept_block( signed_copy_b->calculate_id(), signed_copy_b ); BOOST_REQUIRE(obh); tester2.control->abort_block(); diff --git a/unittests/replay_block_invariants_tests.cpp b/unittests/replay_block_invariants_tests.cpp index f192fc4978..5d6b301ba6 100644 --- a/unittests/replay_block_invariants_tests.cpp +++ b/unittests/replay_block_invariants_tests.cpp @@ -62,7 +62,7 @@ struct test_fixture { } // clone the QC block - auto qc_block = std::make_shared(blog.read_block_by_num(block_num)->clone()); + auto qc_block = blog.read_block_by_num(block_num)->clone(); BOOST_TEST(qc_block); // remove qc block until the end from block log @@ -92,7 +92,8 @@ struct test_fixture { // add the corrupted block back to block log block_log new_blog(blocks_dir, config.blog); - new_blog.append(qc_block, qc_block->calculate_id()); + auto qc_signed_block = signed_block::create_signed_block(std::move(qc_block)); + new_blog.append(qc_signed_block, qc_signed_block->calculate_id()); } // Corrupts finality_extension in the last block of the blocks log @@ -106,7 +107,7 @@ struct test_fixture { // retrieve the last block in block log uint32_t last_block_num = blog.head()->block_num(); - auto last_block = std::make_shared(blog.read_block_by_num(last_block_num)->clone()); + auto last_block = blog.read_block_by_num(last_block_num)->clone(); BOOST_TEST(last_block); // remove last block from block log @@ -133,7 +134,8 @@ struct test_fixture { // add the corrupted block to block log. Need to instantiate a // new_blog as a block has been removed from blocks log. block_log new_blog(blocks_dir, config.blog); - new_blog.append(last_block, last_block->calculate_id()); + auto last_signed_block = signed_block::create_signed_block(std::move(last_block)); + new_blog.append(last_signed_block, last_signed_block->calculate_id()); } }; diff --git a/unittests/unapplied_transaction_queue_tests.cpp b/unittests/unapplied_transaction_queue_tests.cpp index ac2a54ac5b..e6ce5c00ca 100644 --- a/unittests/unapplied_transaction_queue_tests.cpp +++ b/unittests/unapplied_transaction_queue_tests.cpp @@ -34,7 +34,7 @@ auto next( unapplied_transaction_queue& q ) { } auto create_test_block_state( deque trx_metas ) { - auto block = std::make_shared(); + auto block = std::make_unique(); for( auto& trx_meta : trx_metas ) { block->transactions.emplace_back( *trx_meta->packed_trx() ); } diff --git a/unittests/vote_processor_tests.cpp b/unittests/vote_processor_tests.cpp index 1871342746..29f555a764 100644 --- a/unittests/vote_processor_tests.cpp +++ b/unittests/vote_processor_tests.cpp @@ -35,7 +35,7 @@ bls_private_key bls_priv_key_2 = bls_private_key::generate(); std::vector bls_priv_keys{bls_priv_key_0, bls_priv_key_1, bls_priv_key_2}; auto create_genesis_block_state() { // block 2 - auto block = std::make_shared(); + auto block = signed_block::create_mutable_block({}); block->producer = eosio::chain::config::system_account_name; auto pub_key = eosio::testing::base_tester::get_public_key( block->producer, "active" ); @@ -54,7 +54,7 @@ auto create_genesis_block_state() { // block 2 producer_authority_schedule schedule = { 0, { producer_authority{block->producer, block_signing_authority_v0{ 1, {{pub_key, 1}} } } } }; auto genesis = std::make_shared(); block->previous = make_block_id(1); - genesis->block = block; + genesis->block = signed_block::create_signed_block(std::move(block)); genesis->activated_protocol_features = std::make_shared(); genesis->active_finalizer_policy = std::make_shared(new_finalizer_policy); genesis->active_proposer_policy = std::make_shared(proposer_policy{.proposer_schedule = schedule}); @@ -66,7 +66,7 @@ auto create_genesis_block_state() { // block 2 auto create_test_block_state(const block_state_ptr& prev) { static block_timestamp_type timestamp; timestamp = timestamp.next(); // each test block state will be unique - auto block = std::make_shared(prev->block->clone()); + auto block = prev->block->clone(); block->producer = eosio::chain::config::system_account_name; block->previous = prev->id(); block->timestamp = timestamp;