Skip to content

Commit

Permalink
Instead of doubling code, use specific functions for matching state d…
Browse files Browse the repository at this point in the history
…efinitions and blockchain configuration
  • Loading branch information
asuch committed Dec 17, 2024
1 parent 5c7a9f7 commit dbf86e9
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 198 deletions.
1 change: 1 addition & 0 deletions libraries/chain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ add_library( hive_chain
util/owner_update_limit_mgr.cpp
util/operation_extractor.cpp
util/data_filter.cpp
util/state_checker_tools.cpp

rc/rc_curve.cpp
rc/rc_objects.cpp
Expand Down
102 changes: 4 additions & 98 deletions libraries/chain/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <hive/chain/util/dhf_processor.hpp>
#include <hive/chain/util/delayed_voting.hpp>
#include <hive/chain/util/decoded_types_data_storage.hpp>
#include <hive/chain/util/state_checker_tools.hpp>


#include <hive/chain/rc/rc_objects.hpp>
#include <hive/chain/rc/resource_count.hpp>
Expand Down Expand Up @@ -3411,38 +3413,7 @@ void database::verify_match_of_state_objects_definitions_from_shm(const bool thr
if (decoded_state_objects_data.empty())
set_decoded_state_objects_data(_my->_decoded_types_data_storage->generate_decoded_types_data_json_string());
else
{
auto result = _my->_decoded_types_data_storage->check_if_decoded_types_data_json_matches_with_current_decoded_data(decoded_state_objects_data);

if (!result.first)
{
std::fstream loaded_decoded_types_details, current_decoded_types_details;
constexpr char current_data_filename[] = "current_decoded_types_details.log";
constexpr char loaded_data_filename[] = "loaded_decoded_types_details.log";

loaded_decoded_types_details.open(loaded_data_filename, std::ios::out | std::ios::trunc);
if (loaded_decoded_types_details.good())
loaded_decoded_types_details << _my->_decoded_types_data_storage->generate_decoded_types_data_pretty_string(decoded_state_objects_data);
loaded_decoded_types_details.flush();
loaded_decoded_types_details.close();

current_decoded_types_details.open(current_data_filename, std::ios::out | std::ios::trunc);
if (current_decoded_types_details.good())
current_decoded_types_details << _my->_decoded_types_data_storage->generate_decoded_types_data_pretty_string();
current_decoded_types_details.flush();
current_decoded_types_details.close();

if (throw_an_error_on_state_definitions_mismatch)
FC_THROW_EXCEPTION(shm_state_definitions_mismatch_exception,
"Details:\n ${details}"
"\nFull data about decoded state objects are in files: ${current_data_filename}, ${loaded_data_filename}",
("details", result.second)(current_data_filename)(loaded_data_filename));
else
wlog("Mismatch between current and loaded state definitions. Details:\n ${details}"
"\nFull data about decoded state objects are in files: ${current_data_filename}, ${loaded_data_filename}",
("details", result.second)(current_data_filename)(loaded_data_filename));
}
}
util::verify_match_of_state_definitions(*(_my->_decoded_types_data_storage), decoded_state_objects_data, throw_an_error_on_state_definitions_mismatch, /* used in snapshot plugin*/ false);

_my->delete_decoded_types_data_storage();
}
Expand All @@ -3459,72 +3430,7 @@ void database::verify_match_of_blockchain_configuration()
if (full_stored_blockchain_config_json.empty())
set_blockchain_config(full_current_blockchain_config_as_json_string);
else if (full_stored_blockchain_config_json != full_current_blockchain_config_as_json_string)
{
constexpr char HIVE_TREASURY_ACCOUNT_KEY[] = "HIVE_TREASURY_ACCOUNT";
constexpr char HIVE_CHAIN_ID_KEY[] = "HIVE_CHAIN_ID";
constexpr char HIVE_BLOCKCHAIN_VERSION_KEY[] = "HIVE_BLOCKCHAIN_VERSION";

fc::mutable_variant_object stored_blockchain_config = fc::json::from_string(full_stored_blockchain_config_json, fc::json::format_validation_mode::full).get_object();
const std::string current_hive_treasury_account = current_blockchain_config[HIVE_TREASURY_ACCOUNT_KEY].as_string();
const std::string current_hive_chain_id = current_blockchain_config[HIVE_CHAIN_ID_KEY].as_string();

stored_blockchain_config.erase(HIVE_TREASURY_ACCOUNT_KEY);
current_blockchain_config.erase(HIVE_TREASURY_ACCOUNT_KEY);
stored_blockchain_config.erase(HIVE_CHAIN_ID_KEY);
current_blockchain_config.erase(HIVE_CHAIN_ID_KEY);
stored_blockchain_config.erase(HIVE_BLOCKCHAIN_VERSION_KEY);
current_blockchain_config.erase(HIVE_BLOCKCHAIN_VERSION_KEY);

bool throw_exception = false;

{
fc::variant modified_current_blockchain_config;
fc::to_variant(current_blockchain_config, modified_current_blockchain_config);
fc::variant modified_stored_blockchain_config;
fc::to_variant(stored_blockchain_config, modified_stored_blockchain_config);

if (fc::json::to_string(modified_current_blockchain_config) != fc::json::to_string(modified_stored_blockchain_config))
throw_exception = true;
}

if (!throw_exception)
{
if (get_hardfork() < HIVE_HARDFORK_1_24)
{
if (current_hive_treasury_account != OBSOLETE_TREASURY_ACCOUNT || current_hive_chain_id != std::string(OLD_CHAIN_ID))
throw_exception = true;
}
else
{
if (current_hive_treasury_account != NEW_HIVE_TREASURY_ACCOUNT || current_hive_chain_id != std::string(HIVE_CHAIN_ID))
throw_exception = true;
}
}

if (throw_exception)
{
std::fstream loaded_blockchain_config_file, current_blockchain_config_file;
constexpr char current_config_filename[] = "current_blockchain_config.log";
constexpr char loaded_config_filename[] = "loaded_blockchain_config.log";

loaded_blockchain_config_file.open(loaded_config_filename, std::ios::out | std::ios::trunc);
if (loaded_blockchain_config_file.good())
loaded_blockchain_config_file << fc::json::to_pretty_string(fc::json::from_string(full_stored_blockchain_config_json, fc::json::format_validation_mode::full));
loaded_blockchain_config_file.flush();
loaded_blockchain_config_file.close();

current_blockchain_config_file.open(current_config_filename, std::ios::out | std::ios::trunc);
if (current_blockchain_config_file.good())
current_blockchain_config_file << fc::json::to_pretty_string(full_current_blockchain_config_as_variant);
current_blockchain_config_file.flush();
current_blockchain_config_file.close();

FC_THROW_EXCEPTION(blockchain_config_mismatch_exception,
"Mismatch between blockchain configuration loaded from shared memory file and the current one"
"\nFull data about blockchain configuration are in files: ${current_config_filename}, ${loaded_config_filename}",
(current_config_filename)(loaded_config_filename));
}
}
util::verify_match_of_blockchain_configuration(current_blockchain_config, full_current_blockchain_config_as_variant, full_stored_blockchain_config_json, get_hardfork(), /* used_in_snapshot_plugin */ false);
}

std::string database::get_current_decoded_types_data_json()
Expand Down
15 changes: 15 additions & 0 deletions libraries/chain/include/hive/chain/util/state_checker_tools.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <hive/chain/util/decoded_types_data_storage.hpp>
#include <fc/variant_object.hpp>


namespace hive { namespace chain { namespace util {


// throws exception or log warning if doesn't match
void verify_match_of_state_definitions(const chain::util::decoded_types_data_storage& dtds, const std::string& decoded_state_objects_data, const bool throw_exception, const bool used_in_snapshot_plugin);

void verify_match_of_blockchain_configuration(fc::mutable_variant_object current_blockchain_config, const fc::variant& full_current_blockchain_config_as_variant, const std::string& full_stored_blockchain_config_json, const uint32_t hardfork, const bool used_in_snapshot_plugin);

} } } // hive::chain::util
135 changes: 135 additions & 0 deletions libraries/chain/util/state_checker_tools.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include <hive/chain/util/state_checker_tools.hpp>

#include <hive/chain/database_exceptions.hpp>
#include <hive/protocol/config.hpp>

#include <fc/exception/exception.hpp>

#include <fstream>

namespace hive { namespace chain { namespace util {

void verify_match_of_state_definitions(const chain::util::decoded_types_data_storage& dtds, const std::string& decoded_state_objects_data, const bool throw_exception, const bool used_in_snapshot_plugin)
{
auto result = dtds.check_if_decoded_types_data_json_matches_with_current_decoded_data(decoded_state_objects_data);

if (!result.first)
{
std::fstream loaded_decoded_types_details, current_decoded_types_details;
constexpr char current_data_filename[] = "current_decoded_types_details.log";
const std::string loaded_data_filename = used_in_snapshot_plugin ? "loaded_from_snapshot_decoded_types_details" : "loaded_from_shm_decoded_types_details.log";

loaded_decoded_types_details.open(loaded_data_filename, std::ios::out | std::ios::trunc);
if (loaded_decoded_types_details.good())
loaded_decoded_types_details << dtds.generate_decoded_types_data_pretty_string(decoded_state_objects_data);
loaded_decoded_types_details.flush();
loaded_decoded_types_details.close();

current_decoded_types_details.open(current_data_filename, std::ios::out | std::ios::trunc);
if (current_decoded_types_details.good())
current_decoded_types_details << dtds.generate_decoded_types_data_pretty_string();
current_decoded_types_details.flush();
current_decoded_types_details.close();

if (throw_exception)
{
if (used_in_snapshot_plugin)
FC_THROW_EXCEPTION(hive::chain::snapshot_state_definitions_mismatch_exception,
"Details:\n ${details}"
"\nFull data about decoded state objects are in files: ${current_data_filename}, ${loaded_data_filename}",
("details", result.second)(current_data_filename)(loaded_data_filename));
else
FC_THROW_EXCEPTION(hive::chain::shm_state_definitions_mismatch_exception,
"Details:\n ${details}"
"\nFull data about decoded state objects are in files: ${current_data_filename}, ${loaded_data_filename}",
("details", result.second)(current_data_filename)(loaded_data_filename));
}
else
{
if (used_in_snapshot_plugin)
wlog("Snapshot state definitions mismatch current hived version. Details:\n ${details}"
"\nFull data about decoded state objects are in files: ${current_data_filename}, ${loaded_data_filename}",
("details", result.second)(current_data_filename)(loaded_data_filename));
else
wlog("Mismatch between current and loaded from shm state definitions. Details:\n ${details}"
"\nFull data about decoded state objects are in files: ${current_data_filename}, ${loaded_data_filename}",
("details", result.second)(current_data_filename)(loaded_data_filename));
}
}
}

void verify_match_of_blockchain_configuration(fc::mutable_variant_object current_blockchain_config, const fc::variant& full_current_blockchain_config_as_variant, const std::string& full_stored_blockchain_config_json, const uint32_t hardfork, const bool used_in_snapshot_plugin)
{
constexpr char HIVE_TREASURY_ACCOUNT_KEY[] = "HIVE_TREASURY_ACCOUNT";
constexpr char HIVE_CHAIN_ID_KEY[] = "HIVE_CHAIN_ID";
constexpr char HIVE_BLOCKCHAIN_VERSION_KEY[] = "HIVE_BLOCKCHAIN_VERSION";

fc::mutable_variant_object stored_blockchain_config = fc::json::from_string(full_stored_blockchain_config_json, fc::json::format_validation_mode::full).get_object();
const std::string current_hive_treasury_account = current_blockchain_config[HIVE_TREASURY_ACCOUNT_KEY].as_string();
const std::string current_hive_chain_id = current_blockchain_config[HIVE_CHAIN_ID_KEY].as_string();

stored_blockchain_config.erase(HIVE_TREASURY_ACCOUNT_KEY);
current_blockchain_config.erase(HIVE_TREASURY_ACCOUNT_KEY);
stored_blockchain_config.erase(HIVE_CHAIN_ID_KEY);
current_blockchain_config.erase(HIVE_CHAIN_ID_KEY);
stored_blockchain_config.erase(HIVE_BLOCKCHAIN_VERSION_KEY);
current_blockchain_config.erase(HIVE_BLOCKCHAIN_VERSION_KEY);

bool throw_exception = false;

{
fc::variant modified_current_blockchain_config;
fc::to_variant(current_blockchain_config, modified_current_blockchain_config);
fc::variant modified_stored_blockchain_config;
fc::to_variant(stored_blockchain_config, modified_stored_blockchain_config);

if (fc::json::to_string(modified_current_blockchain_config) != fc::json::to_string(modified_stored_blockchain_config))
throw_exception = true;
}

if (!throw_exception)
{
if (hardfork < HIVE_HARDFORK_1_24)
{
if (current_hive_treasury_account != OBSOLETE_TREASURY_ACCOUNT || current_hive_chain_id != std::string(OLD_CHAIN_ID))
throw_exception = true;
}
else
{
if (current_hive_treasury_account != NEW_HIVE_TREASURY_ACCOUNT || current_hive_chain_id != std::string(HIVE_CHAIN_ID))
throw_exception = true;
}
}

if (throw_exception)
{
std::fstream loaded_blockchain_config_file, current_blockchain_config_file;
constexpr char current_config_filename[] = "current_blockchain_config.log";
const std::string loaded_config_filename = used_in_snapshot_plugin ? "loaded_from_snapshot_blockchain_config" : "loaded_from_shm_blockchain_config.log";

loaded_blockchain_config_file.open(loaded_config_filename, std::ios::out | std::ios::trunc);
if (loaded_blockchain_config_file.good())
loaded_blockchain_config_file << fc::json::to_pretty_string(fc::json::from_string(full_stored_blockchain_config_json, fc::json::format_validation_mode::full));
loaded_blockchain_config_file.flush();
loaded_blockchain_config_file.close();

current_blockchain_config_file.open(current_config_filename, std::ios::out | std::ios::trunc);
if (current_blockchain_config_file.good())
current_blockchain_config_file << fc::json::to_pretty_string(full_current_blockchain_config_as_variant);
current_blockchain_config_file.flush();
current_blockchain_config_file.close();

if (used_in_snapshot_plugin)
FC_THROW_EXCEPTION(blockchain_config_mismatch_exception,
"Mismatch between blockchain configuration loaded from snapshot and the current one"
"\nFull data about blockchain configuration are in files: ${current_config_filename}, ${loaded_config_filename}",
(current_config_filename)(loaded_config_filename));
else
FC_THROW_EXCEPTION(snapshot_blockchain_config_mismatch_exception,
"Mismatch between blockchain configuration loaded from shared memory file and the current one"
"\nFull data about blockchain configuration are in files: ${current_config_filename}, ${loaded_config_filename}",
(current_config_filename)(loaded_config_filename));
}
}

} } } // hive::chain::util
Loading

0 comments on commit dbf86e9

Please sign in to comment.