Skip to content

Commit

Permalink
Merge pull request #109 from AntelopeIO/rm_kv_db_config_from_snapshots
Browse files Browse the repository at this point in the history
Remove `kv_database_config` from snapshots starting in Spring 1.0 (snapshot V7)
  • Loading branch information
linh2931 authored May 6, 2024
2 parents e8453ac + 05faab3 commit d411eea
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 36 deletions.
27 changes: 23 additions & 4 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2205,6 +2205,7 @@ struct controller_impl {
using v2 = legacy::snapshot_global_property_object_v2;
using v3 = legacy::snapshot_global_property_object_v3;
using v4 = legacy::snapshot_global_property_object_v4;
using v5 = legacy::snapshot_global_property_object_v5;

if (std::clamp(header.version, v2::minimum_version, v2::maximum_version) == header.version ) {
std::optional<genesis_state> genesis = extract_legacy_genesis_state(*snapshot, header.version);
Expand All @@ -2216,8 +2217,7 @@ struct controller_impl {
section.read_row(legacy_global_properties, db);

db.create<global_property_object>([&legacy_global_properties,&gs_chain_id](auto& gpo ){
gpo.initialize_from(legacy_global_properties, gs_chain_id, kv_database_config{},
genesis_state::default_initial_wasm_configuration);
gpo.initialize_from(legacy_global_properties, gs_chain_id);
});
});
return; // early out to avoid default processing
Expand All @@ -2229,8 +2229,7 @@ struct controller_impl {
section.read_row(legacy_global_properties, db);

db.create<global_property_object>([&legacy_global_properties](auto& gpo ){
gpo.initialize_from(legacy_global_properties, kv_database_config{},
genesis_state::default_initial_wasm_configuration);
gpo.initialize_from(legacy_global_properties);
});
});
return; // early out to avoid default processing
Expand All @@ -2247,6 +2246,18 @@ struct controller_impl {
});
return; // early out to avoid default processing
}

if (std::clamp(header.version, v5::minimum_version, v5::maximum_version) == header.version) {
snapshot->read_section<global_property_object>([&db = this->db](auto& section) {
v5 legacy_global_properties;
section.read_row(legacy_global_properties, db);

db.create<global_property_object>([&legacy_global_properties](auto& gpo) {
gpo.initialize_from(legacy_global_properties);
});
});
return; // early out to avoid default processing
}
}

snapshot->read_section<value_t>([this]( auto& section ) {
Expand Down Expand Up @@ -5669,13 +5680,21 @@ chain_id_type controller::extract_chain_id(snapshot_reader& snapshot) {
chain_id_type chain_id;

using v4 = legacy::snapshot_global_property_object_v4;
using v5 = legacy::snapshot_global_property_object_v5;
if (header.version <= v4::maximum_version) {
snapshot.read_section<global_property_object>([&chain_id]( auto &section ){
v4 global_properties;
section.read_row(global_properties);
chain_id = global_properties.chain_id;
});
}
else if (header.version <= v5::maximum_version) {
snapshot.read_section<global_property_object>([&chain_id]( auto &section ){
v5 global_properties;
section.read_row(global_properties);
chain_id = global_properties.chain_id;
});
}
else {
snapshot.read_section<global_property_object>([&chain_id]( auto &section ){
snapshot_global_property_object global_properties;
Expand Down
2 changes: 2 additions & 0 deletions libraries/chain/include/eosio/chain/chain_id_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace chain {
namespace legacy {
struct snapshot_global_property_object_v3;
struct snapshot_global_property_object_v4;
struct snapshot_global_property_object_v5;
}

struct chain_id_type : public fc::sha256 {
Expand Down Expand Up @@ -60,6 +61,7 @@ namespace chain {
friend struct snapshot_global_property_object;
friend struct legacy::snapshot_global_property_object_v3;
friend struct legacy::snapshot_global_property_object_v4;
friend struct legacy::snapshot_global_property_object_v5;
};

} } // namespace eosio::chain
Expand Down
73 changes: 43 additions & 30 deletions libraries/chain/include/eosio/chain/global_property_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <eosio/chain/producer_schedule.hpp>
#include <eosio/chain/finality/finalizer_policy.hpp>
#include <eosio/chain/snapshot.hpp>
#include <eosio/chain/genesis_state.hpp>
#include <chainbase/chainbase.hpp>
#include "multi_index_includes.hpp"

Expand Down Expand Up @@ -55,6 +56,21 @@ namespace eosio::chain {
kv_database_config kv_configuration;
wasm_config wasm_configuration;
};
struct snapshot_global_property_object_v5 {
// minimum_version and maximum_version refer to chain_snapshot_header
// version. snapshot_global_property_object_v5 applies to version 5 & 6
static constexpr uint32_t minimum_version = 5;
static constexpr uint32_t maximum_version = 6;
static_assert(chain_snapshot_header::minimum_compatible_version <= maximum_version,
"snapshot_global_property_object_v5 is no longer needed");

std::optional<block_num_type> proposed_schedule_block_num;
producer_authority_schedule proposed_schedule;
chain_config configuration;
chain_id_type chain_id;
kv_database_config kv_configuration;
wasm_config wasm_configuration;
};
}

/**
Expand All @@ -79,43 +95,39 @@ namespace eosio::chain {
std::optional<block_num_type> proposed_fin_pol_block_num;
finalizer_policy proposed_fin_pol;

void initialize_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) {
// For snapshot_global_property_object_v2 and initialize_from( const T& legacy )
template<typename T>
void initialize_from( const T& legacy, const chain_id_type& chain_id_val) {
proposed_schedule_block_num = legacy.proposed_schedule_block_num;
proposed_schedule = producer_authority_schedule(legacy.proposed_schedule);

if constexpr (std::is_same_v<T, legacy::snapshot_global_property_object_v2>) {
proposed_schedule = producer_authority_schedule(legacy.proposed_schedule);
} else {
proposed_schedule = legacy.proposed_schedule;
}

configuration = legacy.configuration;
chain_id = chain_id_val;
wasm_configuration = wasm_config_val;
// proposed_fin_pol_block_num and proposed_fin_pol are set to default values.
proposed_fin_pol_block_num = std::nullopt;
proposed_fin_pol = finalizer_policy{};
}

void initialize_from( const legacy::snapshot_global_property_object_v3& legacy,
const kv_database_config& kv_config_val, const wasm_config& wasm_config_val) {
proposed_schedule_block_num = legacy.proposed_schedule_block_num;
proposed_schedule = legacy.proposed_schedule;
configuration = legacy.configuration;
chain_id = legacy.chain_id;
wasm_configuration = wasm_config_val;
if constexpr (std::is_same_v<T, legacy::snapshot_global_property_object_v2> ||
std::is_same_v<T, legacy::snapshot_global_property_object_v3>) {
wasm_configuration = genesis_state::default_initial_wasm_configuration;
} else {
wasm_configuration = legacy.wasm_configuration;
}

// proposed_fin_pol_block_num and proposed_fin_pol are set to default values.
proposed_fin_pol_block_num = std::nullopt;
proposed_fin_pol = finalizer_policy{};
}

void initialize_from( const legacy::snapshot_global_property_object_v4& legacy ) {
proposed_schedule_block_num = legacy.proposed_schedule_block_num;
proposed_schedule = legacy.proposed_schedule;
configuration = legacy.configuration;
chain_id = legacy.chain_id;
wasm_configuration = legacy.wasm_configuration;
// proposed_fin_pol_block_num and proposed_fin_pol are set to default values.
proposed_fin_pol_block_num = std::nullopt;
proposed_fin_pol = finalizer_policy{};
// For snapshot_global_property_object v3, v4, and v5
template<typename T>
void initialize_from( const T& legacy ) {
initialize_from(legacy, legacy.chain_id);
}
};


using global_property_multi_index = chainbase::shared_multi_index_container<
global_property_object,
indexed_by<
Expand All @@ -130,7 +142,6 @@ 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;
};

Expand All @@ -142,9 +153,7 @@ 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),
// global_property_object does not have kv_database_config.
// Set kv_database_config to ddefault value in snapshot
value.configuration, value.chain_id, kv_database_config{}, value.wasm_configuration};
value.configuration, value.chain_id, value.wasm_configuration};
}

static void from_snapshot_row( snapshot_global_property_object&& row, global_property_object& value, chainbase::database& ) {
Expand Down Expand Up @@ -206,10 +215,14 @@ FC_REFLECT(eosio::chain::legacy::snapshot_global_property_object_v4,
(proposed_schedule_block_num)(proposed_schedule)(configuration)(chain_id)(kv_configuration)(wasm_configuration)
)

FC_REFLECT(eosio::chain::snapshot_global_property_object,
FC_REFLECT(eosio::chain::legacy::snapshot_global_property_object_v5,
(proposed_schedule_block_num)(proposed_schedule)(configuration)(chain_id)(kv_configuration)(wasm_configuration)
)

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

FC_REFLECT(eosio::chain::dynamic_global_property_object,
(global_action_sequence)
)
8 changes: 6 additions & 2 deletions libraries/chain/include/eosio/chain/kv_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ namespace eosio { namespace chain {
* The key and value limits apply when adding or modifying elements. They may be reduced
* below existing database entries.
*
* This has been marked for removal at a later date, when v7 snapshots are deemed necessary.
* kv_database_config will be removed as part of that effort.
* Current uses of kv_database_config were removed in Spring 1.0.
* Do not use it in the future.
*
* The file is kept for backward compatibilities as kv_database_config was used
* in snapshots containing legacy::snapshot_global_property_object_v4 and
* legacy::snapshot_global_property_object_v5.
*/
struct kv_database_config {
std::uint32_t max_key_size = 0; ///< the maximum size in bytes of a key
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 d411eea

Please sign in to comment.