Skip to content

Commit

Permalink
Add snapshot support for the new version of global_property_object
Browse files Browse the repository at this point in the history
  • Loading branch information
linh2931 committed May 1, 2024
1 parent ac8e153 commit 4f85bee
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 25 deletions.
25 changes: 15 additions & 10 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2207,7 +2207,9 @@ struct controller_impl {

db.create<global_property_object>([&legacy_global_properties,&gs_chain_id](auto& gpo ){
gpo.initalize_from(legacy_global_properties, gs_chain_id, kv_database_config{},
genesis_state::default_initial_wasm_configuration);
genesis_state::default_initial_wasm_configuration,
std::nullopt,
finalizer_policy{});
});
});
return; // early out to avoid default processing
Expand All @@ -2220,7 +2222,9 @@ struct controller_impl {

db.create<global_property_object>([&legacy_global_properties](auto& gpo ){
gpo.initalize_from(legacy_global_properties, kv_database_config{},
genesis_state::default_initial_wasm_configuration);
genesis_state::default_initial_wasm_configuration,
std::nullopt,
finalizer_policy{});
});
});
return; // early out to avoid default processing
Expand All @@ -2232,7 +2236,9 @@ struct controller_impl {
section.read_row(legacy_global_properties, db);

db.create<global_property_object>([&legacy_global_properties](auto& gpo) {
gpo.initalize_from(legacy_global_properties);
gpo.initalize_from(legacy_global_properties,
std::nullopt,
finalizer_policy{});
});
});
return; // early out to avoid default processing
Expand Down Expand Up @@ -3161,12 +3167,12 @@ struct controller_impl {
// other transition blocks ignore set_finalizers
auto process_new_finalizer_policy_legacy = [&](building_block::building_block_legacy& bb_legacy) -> void {
const auto& gpo = db.get<global_property_object>();
if (gpo.proposed_fin_pol_block_num && !bb_legacy.pending_block_header_state.is_if_transition_block()) {
if (gpo.proposed_fin_pol_block_num.has_value() && !bb_legacy.pending_block_header_state.is_if_transition_block()) {
new_finalizer_policy = gpo.proposed_fin_pol;
new_finalizer_policy->generation = 1; // only allowed to be set once in legacy mode

db.modify( gpo, [&]( auto& gp ) {
gp.proposed_fin_pol_block_num = std::optional<block_num_type>();
gp.proposed_fin_pol_block_num = std::nullopt;
gp.proposed_fin_pol.generation = 0;
gp.proposed_fin_pol.finalizers.clear();
});
Expand All @@ -3176,12 +3182,12 @@ struct controller_impl {
// Process set_finalizers after transition to Savanna
auto process_new_finalizer_policy_savanna = [&](building_block::building_block_if& bb_savanna) -> void {
const auto& gpo = db.get<global_property_object>();
if (gpo.proposed_fin_pol_block_num) {
if (gpo.proposed_fin_pol_block_num.has_value()) {
new_finalizer_policy = gpo.proposed_fin_pol;
new_finalizer_policy->generation = bb_savanna.parent.finalizer_policy_generation + 1;

db.modify( gpo, [&]( auto& gp ) {
gp.proposed_fin_pol_block_num = std::optional<block_num_type>();
gp.proposed_fin_pol_block_num = std::nullopt;
gp.proposed_fin_pol.generation = 0;
gp.proposed_fin_pol.finalizers.clear();
});
Expand Down Expand Up @@ -3311,8 +3317,7 @@ struct controller_impl {
auto& bb = std::get<building_block>(pending->_block_stage);

// Use global_property_object instead of building_block so that if the
// transaction fails it is rolledback.
// Overwrite any existing finalizer policy set earlier in this block.
// transaction fails it will be rolledback.
auto cur_block_num = chain_head.block_num() + 1;
auto& gpo = db.get<global_property_object>();
db.modify( gpo, [&]( auto& gp ) {
Expand Down Expand Up @@ -5324,7 +5329,7 @@ int64_t controller_impl::set_proposed_producers_legacy( vector<producer_authorit
assert(pending);
auto& bb = std::get<building_block>(pending->_block_stage);
bool transition_block = bb.apply_l<bool>([&](building_block::building_block_legacy& bl) {
return bl.pending_block_header_state.is_if_transition_block() || gpo.proposed_fin_pol_block_num > 0;
return bl.pending_block_header_state.is_if_transition_block() || gpo.proposed_fin_pol_block_num.has_value();
});
if (transition_block)
return -1;
Expand Down
42 changes: 27 additions & 15 deletions libraries/chain/include/eosio/chain/global_property_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ namespace eosio::chain {
};
struct snapshot_global_property_object_v4 {
static constexpr uint32_t minimum_version = 4;
static constexpr uint32_t maximum_version = 4;
// snapshot_global_property_object_v4 is valid up to snapshot header version 6
static constexpr uint32_t maximum_version = 6;
static_assert(chain_snapshot_header::minimum_compatible_version <= maximum_version,
"snapshot_global_property_object_v4 is no longer needed");

Expand All @@ -71,40 +72,48 @@ namespace eosio::chain {
id_type id;
std::optional<block_num_type> proposed_schedule_block_num;
shared_producer_authority_schedule proposed_schedule;
std::optional<block_num_type> proposed_fin_pol_block_num;
finalizer_policy proposed_fin_pol;
chain_config configuration;
chain_id_type chain_id;
kv_database_config kv_configuration;
wasm_config wasm_configuration;
std::optional<block_num_type> proposed_fin_pol_block_num;
finalizer_policy proposed_fin_pol;

void initalize_from( const legacy::snapshot_global_property_object_v2& legacy, const chain_id_type& chain_id_val,
const kv_database_config& kv_config_val, const wasm_config& wasm_config_val ) {
const kv_database_config& kv_config_val, const wasm_config& wasm_config_val,
const std::optional<block_num_type>& proposed_fin_pol_block_num_val,
const finalizer_policy& proposed_fin_pol_val) {
proposed_schedule_block_num = legacy.proposed_schedule_block_num;
proposed_schedule = producer_authority_schedule(legacy.proposed_schedule);
configuration = legacy.configuration;
chain_id = chain_id_val;
kv_configuration = kv_config_val;
wasm_configuration = wasm_config_val;
proposed_fin_pol_block_num = proposed_fin_pol_block_num_val;
proposed_fin_pol = proposed_fin_pol_val;
}

void initalize_from( const legacy::snapshot_global_property_object_v3& legacy,
const kv_database_config& kv_config_val, const wasm_config& wasm_config_val ) {
const kv_database_config& kv_config_val, const wasm_config& wasm_config_val,
const std::optional<block_num_type>& proposed_fin_pol_block_num_val,
const finalizer_policy& proposed_fin_pol_val) {
proposed_schedule_block_num = legacy.proposed_schedule_block_num;
proposed_schedule = legacy.proposed_schedule;
configuration = legacy.configuration;
chain_id = legacy.chain_id;
kv_configuration = kv_config_val;
wasm_configuration = wasm_config_val;
proposed_fin_pol_block_num = proposed_fin_pol_block_num_val;
proposed_fin_pol = proposed_fin_pol_val;
}

void initalize_from( const legacy::snapshot_global_property_object_v4& legacy ) {
void initalize_from( const legacy::snapshot_global_property_object_v4& legacy,
const std::optional<block_num_type>& proposed_fin_pol_block_num_val,
const finalizer_policy& proposed_fin_pol_val) {
proposed_schedule_block_num = legacy.proposed_schedule_block_num;
proposed_schedule = legacy.proposed_schedule;
configuration = legacy.configuration;
chain_id = legacy.chain_id;
kv_configuration = legacy.kv_configuration;
wasm_configuration = legacy.wasm_configuration;
proposed_fin_pol_block_num = proposed_fin_pol_block_num_val;
proposed_fin_pol = proposed_fin_pol_val;
}
};

Expand All @@ -123,8 +132,9 @@ namespace eosio::chain {
producer_authority_schedule proposed_schedule;
chain_config configuration;
chain_id_type chain_id;
kv_database_config kv_configuration;
wasm_config wasm_configuration;
std::optional<block_num_type> proposed_fin_pol_block_num;
finalizer_policy proposed_fin_pol;
};

namespace detail {
Expand All @@ -135,16 +145,18 @@ namespace eosio::chain {

static snapshot_global_property_object to_snapshot_row( const global_property_object& value, const chainbase::database& ) {
return {value.proposed_schedule_block_num, producer_authority_schedule::from_shared(value.proposed_schedule),
value.configuration, value.chain_id, value.kv_configuration, value.wasm_configuration};
value.configuration, value.chain_id, value.wasm_configuration,
value.proposed_fin_pol_block_num, value.proposed_fin_pol};
}

static void from_snapshot_row( snapshot_global_property_object&& row, global_property_object& value, chainbase::database& ) {
value.proposed_schedule_block_num = row.proposed_schedule_block_num;
value.proposed_schedule = row.proposed_schedule;
value.configuration = row.configuration;
value.chain_id = row.chain_id;
value.kv_configuration = row.kv_configuration;
value.wasm_configuration = row.wasm_configuration;
value.proposed_fin_pol_block_num = row.proposed_fin_pol_block_num;
value.proposed_fin_pol = row.proposed_fin_pol;
}
};
}
Expand Down Expand Up @@ -179,7 +191,7 @@ CHAINBASE_SET_INDEX_TYPE(eosio::chain::dynamic_global_property_object,
eosio::chain::dynamic_global_property_multi_index)

FC_REFLECT(eosio::chain::global_property_object,
(proposed_schedule_block_num)(proposed_schedule)(proposed_fin_pol_block_num)(proposed_fin_pol)(configuration)(chain_id)(kv_configuration)(wasm_configuration)
(proposed_schedule_block_num)(proposed_schedule)(configuration)(chain_id)(wasm_configuration)(proposed_fin_pol_block_num)(proposed_fin_pol)
)

FC_REFLECT(eosio::chain::legacy::snapshot_global_property_object_v2,
Expand All @@ -195,7 +207,7 @@ FC_REFLECT(eosio::chain::legacy::snapshot_global_property_object_v4,
)

FC_REFLECT(eosio::chain::snapshot_global_property_object,
(proposed_schedule_block_num)(proposed_schedule)(configuration)(chain_id)(kv_configuration)(wasm_configuration)
(proposed_schedule_block_num)(proposed_schedule)(configuration)(chain_id)(wasm_configuration)(proposed_fin_pol_block_num)(proposed_fin_pol)
)

FC_REFLECT(eosio::chain::dynamic_global_property_object,
Expand Down
Binary file modified unittests/snapshots/snap_v7.bin.gz
Binary file not shown.
Binary file modified unittests/snapshots/snap_v7.bin.json.gz
Binary file not shown.
Binary file modified unittests/snapshots/snap_v7.json.gz
Binary file not shown.

0 comments on commit 4f85bee

Please sign in to comment.