Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #8880 from EOSIO/pruned_block-to-signed_block
Browse files Browse the repository at this point in the history
add pruned_block to signed_block conversion
  • Loading branch information
heifner authored Mar 26, 2020
2 parents 0f8e58f + 717282e commit 9851b21
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
20 changes: 20 additions & 0 deletions libraries/chain/block.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <eosio/chain/block.hpp>
#include <eosio/chain/types.hpp>

namespace eosio { namespace chain {
void additional_block_signatures_extension::reflector_init() {
Expand Down Expand Up @@ -111,4 +112,23 @@ namespace eosio { namespace chain {
return validate_and_extract_block_extensions( block_extensions );
}

signed_block_ptr pruned_block::to_signed_block() const {
if (prune_state != prune_state_type::complete_legacy)
return signed_block_ptr{};

auto result = std::make_shared<signed_block>(*static_cast<const signed_block_header*>(this));
result->block_extensions = this->block_extensions;

auto visitor = overloaded{
[](const transaction_id_type &id) -> transaction_receipt::trx_type { return id; },
[](const pruned_transaction &trx) -> transaction_receipt::trx_type {
const auto& legacy = trx.get_prunable_data().prunable_data.get<prunable_transaction_data::full_legacy>();
return packed_transaction(trx.get_packed_transaction(), legacy.signatures, legacy.packed_context_free_data, trx.get_compression());
}};

for (const pruned_transaction_receipt &r : transactions){
result->transactions.emplace_back(transaction_receipt{r, r.trx.visit(visitor)});
}
return result;
}
} } /// namespace eosio::chain
8 changes: 5 additions & 3 deletions libraries/chain/include/eosio/chain/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ namespace eosio { namespace chain {
};

struct transaction_receipt : public transaction_receipt_header {

transaction_receipt():transaction_receipt_header(){}
using trx_type = fc::static_variant<transaction_id_type, packed_transaction>;
transaction_receipt() : transaction_receipt_header() {}
transaction_receipt(const transaction_receipt_header& header, trx_type&& t): transaction_receipt_header(header), trx(std::move(t)){}
explicit transaction_receipt( const transaction_id_type& tid ):transaction_receipt_header(executed),trx(tid){}
explicit transaction_receipt( const packed_transaction& ptrx ):transaction_receipt_header(executed),trx(ptrx){}

fc::static_variant<transaction_id_type, packed_transaction> trx;
trx_type trx;

digest_type digest()const {
digest_type::encoder enc;
Expand Down Expand Up @@ -139,6 +140,7 @@ namespace eosio { namespace chain {
pruned_block( pruned_block&& ) = default;
pruned_block& operator=(const pruned_block&) = delete;
pruned_block clone() const { return *this; }
signed_block_ptr to_signed_block() const;

fc::enum_type<uint8_t,prune_state_type> prune_state{prune_state_type::complete_legacy};
deque<pruned_transaction_receipt> transactions; /// new or generated transactions
Expand Down
3 changes: 3 additions & 0 deletions libraries/chain/include/eosio/chain/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ namespace eosio { namespace chain {
local_pack_context_free_data();
}

packed_transaction(const bytes& packed_txn, const vector<signature_type>& sigs, const bytes& packed_cfd, compression_type _compression);

// used by abi_serializer
packed_transaction( bytes&& packed_txn, vector<signature_type>&& sigs, bytes&& packed_cfd, compression_type _compression );
packed_transaction( bytes&& packed_txn, vector<signature_type>&& sigs, vector<bytes>&& cfd, compression_type _compression );
Expand Down Expand Up @@ -290,6 +292,7 @@ namespace eosio { namespace chain {
const bytes* get_context_free_data(std::size_t segment_ordinal);
const fc::enum_type<uint8_t,compression_type>& get_compression()const { return compression; }
const bytes& get_packed_transaction()const { return packed_trx; }
const prunable_transaction_data& get_prunable_data() const { return prunable_data; }

void prune_all();

Expand Down
12 changes: 12 additions & 0 deletions libraries/chain/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,18 @@ bytes packed_transaction::get_raw_transaction() const
} FC_CAPTURE_AND_RETHROW((compression)(packed_trx))
}

packed_transaction::packed_transaction(const bytes& packed_txn, const vector<signature_type>& sigs, const bytes& packed_cfd, compression_type _compression )
:signatures(sigs)
,compression(_compression)
,packed_context_free_data(packed_cfd)
,packed_trx(packed_txn)
{
local_unpack_transaction({});
if( !packed_context_free_data.empty() ) {
local_unpack_context_free_data();
}
}

packed_transaction::packed_transaction( bytes&& packed_txn, vector<signature_type>&& sigs, bytes&& packed_cfd, compression_type _compression )
:signatures(std::move(sigs))
,compression(_compression)
Expand Down
6 changes: 6 additions & 0 deletions unittests/misc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,10 @@ BOOST_AUTO_TEST_CASE(pruned_block_test) {
BOOST_TEST(basic.transaction_mroot.str() == original->transaction_mroot.str());
BOOST_TEST(basic.transaction_mroot.str() == calculate_trx_merkle(basic.transactions).str());

signed_block_ptr recovered = basic.to_signed_block();
BOOST_REQUIRE(recovered);
BOOST_TEST(fc::raw::pack(*original) == fc::raw::pack(*recovered));

fc::datastream<std::size_t> size_stream;
std::size_t padded_size = basic.pack(size_stream, pruned_transaction::cf_compression_type::none);
BOOST_TEST(size_stream.tellp() <= padded_size);
Expand All @@ -1037,6 +1041,8 @@ BOOST_AUTO_TEST_CASE(pruned_block_test) {
fc::datastream<char*> out(buffer.data(), buffer.size());
deserialized.pack(out, pruned_transaction::cf_compression_type::none);
BOOST_TEST(out.tellp() <= buffer.size());

BOOST_TEST(!deserialized.to_signed_block());
}

BOOST_AUTO_TEST_CASE(reflector_init_test) {
Expand Down

0 comments on commit 9851b21

Please sign in to comment.