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 #9235 from EOSIO/EPE-17-develop-variant
Browse files Browse the repository at this point in the history
Migrate fc::static_variant to std::variant
  • Loading branch information
Timothy Banks authored Aug 25, 2020
2 parents 9a10d21 + f1a24af commit 9eaa571
Show file tree
Hide file tree
Showing 98 changed files with 1,244 additions and 2,663 deletions.
38 changes: 19 additions & 19 deletions libraries/chain/abi_serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,30 +675,30 @@ namespace eosio { namespace chain {

auto& b = path.back();

EOS_ASSERT( b.contains<array_index_path_item>(), abi_exception, "trying to set array index without first pushing new array index item" );
EOS_ASSERT( std::holds_alternative<array_index_path_item>(b), abi_exception, "trying to set array index without first pushing new array index item" );

b.get<array_index_path_item>().array_index = i;
std::get<array_index_path_item>(b).array_index = i;
}

void abi_traverse_context_with_path::hint_array_type_if_in_array() {
if( path.size() == 0 || !path.back().contains<array_index_path_item>() )
if( path.size() == 0 || !std::holds_alternative<array_index_path_item>(path.back()) )
return;

path.back().get<array_index_path_item>().type_hint = array_type_path_root{};
std::get<array_index_path_item>(path.back()).type_hint = array_type_path_root{};
}

void abi_traverse_context_with_path::hint_struct_type_if_in_array( const map<type_name, struct_def>::const_iterator& itr ) {
if( path.size() == 0 || !path.back().contains<array_index_path_item>() )
if( path.size() == 0 || !std::holds_alternative<array_index_path_item>(path.back()) )
return;

path.back().get<array_index_path_item>().type_hint = struct_type_path_root{ .struct_itr = itr };
std::get<array_index_path_item>(path.back()).type_hint = struct_type_path_root{ .struct_itr = itr };
}

void abi_traverse_context_with_path::hint_variant_type_if_in_array( const map<type_name, variant_def>::const_iterator& itr ) {
if( path.size() == 0 || !path.back().contains<array_index_path_item>() )
if( path.size() == 0 || !std::holds_alternative<array_index_path_item>(path.back()) )
return;

path.back().get<array_index_path_item>().type_hint = variant_type_path_root{ .variant_itr = itr };
std::get<array_index_path_item>(path.back()).type_hint = variant_type_path_root{ .variant_itr = itr };
}

constexpr size_t const_strlen( const char* str )
Expand Down Expand Up @@ -818,13 +818,13 @@ namespace eosio { namespace chain {

void operator()( const array_index_path_item& item ) {
const auto& th = item.type_hint;
if( th.contains<struct_type_path_root>() ) {
const auto& str = th.get<struct_type_path_root>().struct_itr->first;
if( std::holds_alternative<struct_type_path_root>(th) ) {
const auto& str = std::get<struct_type_path_root>(th).struct_itr->first;
output_name( s, str, shorten_names );
} else if( th.contains<variant_type_path_root>() ) {
const auto& str = th.get<variant_type_path_root>().variant_itr->first;
} else if( std::holds_alternative<variant_type_path_root>(th) ) {
const auto& str = std::get<variant_type_path_root>(th).variant_itr->first;
output_name( s, str, shorten_names );
} else if( th.contains<array_type_path_root>() ) {
} else if( std::holds_alternative<array_type_path_root>(th) ) {
s << "ARRAY";
} else {
s << "UNKNOWN";
Expand All @@ -848,21 +848,21 @@ namespace eosio { namespace chain {

generate_path_string_visitor visitor(shorten_names, !full_path);
if( full_path )
root_of_path.visit( visitor );
std::visit( visitor, root_of_path );
for( size_t i = 0, n = path.size(); i < n; ++i ) {
if( full_path && !path[i].contains<array_index_path_item>() )
if( full_path && !std::holds_alternative<array_index_path_item>(path[i]) )
visitor.add_dot();

path[i].visit( visitor );
std::visit( visitor, path[i] );

}

if( !full_path ) {
if( visitor.last_path_item.contains<empty_path_item>() ) {
root_of_path.visit( visitor );
if( std::holds_alternative<empty_path_item>(visitor.last_path_item) ) {
std::visit( visitor, root_of_path );
} else {
path_item_type_visitor vis2(visitor.s, shorten_names);
visitor.last_path_item.visit(vis2);
std::visit(vis2, visitor.last_path_item);
}
}

Expand Down
37 changes: 25 additions & 12 deletions libraries/chain/apply_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ void apply_context::exec_one()

const auto& cfg = control.get_global_properties().configuration;
const account_metadata_object* receiver_account = nullptr;

auto handle_exception = [&](const auto& e)
{
action_trace& trace = trx_context.get_action_trace( action_ordinal );
trace.error_code = controller::convert_exception_to_error_code( e );
trace.except = e;
finalize_trace( trace, start );
throw;
};

try {
try {
action_return_value.clear();
Expand Down Expand Up @@ -139,12 +149,15 @@ void apply_context::exec_one()
} else {
act_digest = digest_type::hash(*act);
}
} catch( const fc::exception& e ) {
action_trace& trace = trx_context.get_action_trace( action_ordinal );
trace.error_code = controller::convert_exception_to_error_code( e );
trace.except = e;
finalize_trace( trace, start );
} catch ( const std::bad_alloc& ) {
throw;
} catch ( const boost::interprocess::bad_alloc& ) {
throw;
} catch( const fc::exception& e ) {
handle_exception(e);
} catch ( const std::exception& e ) {
auto wrapper = fc::std_exception_wrapper::from_current_exception(e);
handle_exception(wrapper);
}

// Note: It should not be possible for receiver_account to be invalidated because:
Expand Down Expand Up @@ -430,7 +443,7 @@ void apply_context::schedule_deferred_transaction( const uint128_t& sender_id, a
"only the deferred_transaction_generation_context extension is currently supported for deferred transactions"
);

const auto& context = itr->second.get<deferred_transaction_generation_context>();
const auto& context = std::get<deferred_transaction_generation_context>(itr->second);

EOS_ASSERT( context.sender == receiver, ill_formed_deferred_transaction_generation_context,
"deferred transaction generaction context contains mismatching sender",
Expand Down Expand Up @@ -826,16 +839,16 @@ int apply_context::get_context_free_data( uint32_t index, char* buffer, size_t b
{
const packed_transaction::prunable_data_type::prunable_data_t& data = trx_context.packed_trx.get_prunable_data().prunable_data;
const bytes* cfd = nullptr;
if( data.contains<packed_transaction::prunable_data_type::none>() ) {
} else if( data.contains<packed_transaction::prunable_data_type::partial>() ) {
if( index >= data.get<packed_transaction::prunable_data_type::partial>().context_free_segments.size() ) return -1;
if( std::holds_alternative<packed_transaction::prunable_data_type::none>(data) ) {
} else if( std::holds_alternative<packed_transaction::prunable_data_type::partial>(data) ) {
if( index >= std::get<packed_transaction::prunable_data_type::partial>(data).context_free_segments.size() ) return -1;

cfd = trx_context.packed_trx.get_context_free_data(index);
} else {
const std::vector<bytes>& context_free_data =
data.contains<packed_transaction::prunable_data_type::full_legacy>() ?
data.get<packed_transaction::prunable_data_type::full_legacy>().context_free_segments :
data.get<packed_transaction::prunable_data_type::full>().context_free_segments;
std::holds_alternative<packed_transaction::prunable_data_type::full_legacy>(data) ?
std::get<packed_transaction::prunable_data_type::full_legacy>(data).context_free_segments :
std::get<packed_transaction::prunable_data_type::full>(data).context_free_segments;
if( index >= context_free_data.size() ) return -1;

cfd = &context_free_data[index];
Expand Down
16 changes: 8 additions & 8 deletions libraries/chain/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ namespace eosio { namespace chain {

struct transaction_receipt_translator {
bool legacy = true;
fc::static_variant<transaction_id_type, packed_transaction> operator()(const transaction_id_type& tid) const {
std::variant<transaction_id_type, packed_transaction> operator()(const transaction_id_type& tid) const {
return tid;
}
fc::static_variant<transaction_id_type, packed_transaction> operator()(const packed_transaction_v0& ptrx) const {
std::variant<transaction_id_type, packed_transaction> operator()(const packed_transaction_v0& ptrx) const {
return packed_transaction(ptrx, legacy);
}
fc::static_variant<transaction_id_type, packed_transaction> operator()(packed_transaction_v0&& ptrx) const {
std::variant<transaction_id_type, packed_transaction> operator()(packed_transaction_v0&& ptrx) const {
return packed_transaction(std::move(ptrx), legacy);
}
};

transaction_receipt::transaction_receipt(const transaction_receipt_v0& other, bool legacy)
: transaction_receipt_header(static_cast<const transaction_receipt_header&>(other)),
trx( other.trx.visit(transaction_receipt_translator{legacy}))
trx(std::visit(transaction_receipt_translator{legacy}, other.trx))
{}

transaction_receipt::transaction_receipt(transaction_receipt_v0&& other, bool legacy)
: transaction_receipt_header(std::move(static_cast<transaction_receipt_header&>(other))),
trx( std::move(other.trx).visit(transaction_receipt_translator{legacy}))
trx(std::visit(transaction_receipt_translator{legacy}, std::move(other.trx)))
{}

static flat_multimap<uint16_t, block_extension> validate_and_extract_block_extensions(const extensions_type& block_extensions) {
Expand Down Expand Up @@ -118,7 +118,7 @@ namespace eosio { namespace chain {

std::size_t transaction_receipt::maximum_pruned_pack_size( packed_transaction::cf_compression_type segment_compression ) const {
return fc::raw::pack_size(*static_cast<const transaction_receipt_header*>(this)) + 1 +
trx.visit([&](const auto& obj){ return pruned_trx_receipt_packed_size(obj, segment_compression); });
std::visit([&](const auto& obj){ return pruned_trx_receipt_packed_size(obj, segment_compression); }, trx);
}

std::size_t signed_block::maximum_pruned_pack_size( packed_transaction::cf_compression_type segment_compression ) const {
Expand All @@ -143,12 +143,12 @@ namespace eosio { namespace chain {
auto visitor = overloaded{
[](const transaction_id_type &id) -> transaction_receipt_v0::trx_type { return id; },
[](const packed_transaction &trx) -> transaction_receipt_v0::trx_type {
const auto& legacy = trx.get_prunable_data().prunable_data.get<packed_transaction::prunable_data_type::full_legacy>();
const auto& legacy = std::get<packed_transaction::prunable_data_type::full_legacy>(trx.get_prunable_data().prunable_data);
return packed_transaction_v0(trx.get_packed_transaction(), legacy.signatures, legacy.packed_context_free_data, trx.get_compression());
}};

for (const transaction_receipt &r : transactions){
result.transactions.emplace_back(transaction_receipt_v0{r, r.trx.visit(visitor)});
result.transactions.emplace_back(transaction_receipt_v0{r, std::visit(visitor, r.trx)});
}
return result;
}
Expand Down
13 changes: 7 additions & 6 deletions libraries/chain/block_header_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,11 @@ namespace eosio { namespace chain {
legacy::producer_schedule_type downgraded_producers;
downgraded_producers.version = new_producers->version;
for (const auto &p : new_producers->producers) {
p.authority.visit([&downgraded_producers, &p](const auto& auth){
std::visit([&downgraded_producers, &p](const auto& auth)
{
EOS_ASSERT(auth.keys.size() == 1 && auth.keys.front().weight == auth.threshold, producer_schedule_exception, "multisig block signing present before enabled!");
downgraded_producers.producers.emplace_back(legacy::producer_key{p.producer_name, auth.keys.front().key});
});
}, p.authority);
}
h.new_producers = std::move(downgraded_producers);
}
Expand Down Expand Up @@ -263,7 +264,7 @@ namespace eosio { namespace chain {
EOS_ASSERT(wtmsig_enabled, producer_schedule_exception, "Block header producer_schedule_change_extension before activation of WTMsig Block Signatures" );
EOS_ASSERT( !was_pending_promoted, producer_schedule_exception, "cannot set pending producer schedule in the same block in which pending was promoted to active" );

const auto& new_producer_schedule = exts.lower_bound(producer_schedule_change_extension::extension_id())->second.get<producer_schedule_change_extension>();
const auto& new_producer_schedule = std::get<producer_schedule_change_extension>(exts.lower_bound(producer_schedule_change_extension::extension_id())->second);

EOS_ASSERT( new_producer_schedule.version == active_schedule.version + 1, producer_schedule_exception, "wrong producer schedule version specified" );
EOS_ASSERT( prev_pending_schedule.schedule.producers.empty(), producer_schedule_exception,
Expand All @@ -276,7 +277,7 @@ namespace eosio { namespace chain {
protocol_feature_activation_set_ptr new_activated_protocol_features;
{ // handle protocol_feature_activation
if( exts.count(protocol_feature_activation::extension_id() > 0) ) {
const auto& new_protocol_features = exts.lower_bound(protocol_feature_activation::extension_id())->second.get<protocol_feature_activation>().protocol_features;
const auto& new_protocol_features = std::get<protocol_feature_activation>(exts.lower_bound(protocol_feature_activation::extension_id())->second).protocol_features;
validator( timestamp, prev_activated_protocol_features->protocol_features, new_protocol_features );

new_activated_protocol_features = std::make_shared<protocol_feature_activation_set>(
Expand Down Expand Up @@ -409,7 +410,7 @@ namespace eosio { namespace chain {

void block_header_state::verify_signee( )const {

size_t num_keys_in_authority = valid_block_signing_authority.visit([](const auto &a){ return a.keys.size(); });
auto num_keys_in_authority = std::visit([](const auto &a){ return a.keys.size(); }, valid_block_signing_authority);
EOS_ASSERT(1 + additional_signatures.size() <= num_keys_in_authority, wrong_signing_key,
"number of block signatures (${num_block_signatures}) exceeds number of keys in block signing authority (${num_keys})",
("num_block_signatures", 1 + additional_signatures.size())
Expand Down Expand Up @@ -449,7 +450,7 @@ namespace eosio { namespace chain {
if( header_exts.count(protocol_feature_activation::extension_id()) == 0 )
return no_activations;

return header_exts.lower_bound(protocol_feature_activation::extension_id())->second.get<protocol_feature_activation>().protocol_features;
return std::get<protocol_feature_activation>(header_exts.lower_bound(protocol_feature_activation::extension_id())->second).protocol_features;
}

block_header_state::block_header_state( legacy::snapshot_block_header_state_v2&& snapshot )
Expand Down
12 changes: 6 additions & 6 deletions libraries/chain/block_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ namespace eosio { namespace chain {
ds.write(reinterpret_cast<const char*>(&first_block_num), sizeof(first_block_num));

std::visit(overloaded{[&ds](const chain_id_type& id) { ds << id; },
[&ds](const genesis_state& state) {
auto data = fc::raw::pack(state);
ds.write(data.data(), data.size());
}},
chain_context);
[&ds](const genesis_state& state) {
auto data = fc::raw::pack(state);
ds.write(data.data(), data.size());
}},
chain_context);

auto totem = block_log::npos;
ds.write(reinterpret_cast<const char*>(&totem), sizeof(totem));
Expand Down Expand Up @@ -988,7 +988,7 @@ namespace eosio { namespace chain {

size_t num_trx_pruned = 0;
for (auto& trx : entry.block.transactions) {
num_trx_pruned += trx.trx.visit(pruner);
num_trx_pruned += std::visit(pruner, trx.trx);
}
strm.skip(offset_to_block_start(version));
entry.block.pack(strm, entry.meta.compression);
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/block_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace eosio { namespace chain {
auto exts = b->validate_and_extract_extensions();

if ( exts.count(additional_sigs_eid) > 0 ) {
auto& additional_sigs = exts.lower_bound(additional_sigs_eid)->second.get<additional_block_signatures_extension>();
auto& additional_sigs = std::get<additional_block_signatures_extension>(exts.lower_bound(additional_sigs_eid)->second);

return std::move(additional_sigs.signatures);
}
Expand Down
Loading

0 comments on commit 9eaa571

Please sign in to comment.