-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instead of doubling code, use specific functions for matching state d…
…efinitions and blockchain configuration
- Loading branch information
asuch
committed
Dec 17, 2024
1 parent
5c7a9f7
commit dbf86e9
Showing
5 changed files
with
158 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
libraries/chain/include/hive/chain/util/state_checker_tools.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.