Skip to content

Commit

Permalink
Migrate fc::static_variant to std::variant
Browse files Browse the repository at this point in the history
Cherry-picked from 9eaa571 (EOSIO/eos#9235) with additional fixes for later changes to 2.0.x.
  • Loading branch information
swatanabe committed Jan 28, 2022
1 parent 748131b commit 0c4f36d
Show file tree
Hide file tree
Showing 57 changed files with 531 additions and 368 deletions.
38 changes: 19 additions & 19 deletions libraries/chain/abi_serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,30 +662,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 @@ -805,13 +805,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 @@ -835,21 +835,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
25 changes: 19 additions & 6 deletions libraries/chain/apply_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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 @@ -122,12 +132,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 @@ -408,7 +421,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
2 changes: 1 addition & 1 deletion libraries/chain/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ namespace eosio { namespace chain {

}

} } /// namespace eosio::chain
} } /// namespace eosio::chain
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
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 0c4f36d

Please sign in to comment.