Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update finalizer_safety_file_versioning test to not need are_equivalent. #502

Merged
merged 6 commits into from
Aug 8, 2024
14 changes: 1 addition & 13 deletions libraries/chain/finality/finalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ my_finalizers_t::fsi_map my_finalizers_t::load_finalizer_safety_info() {
("p", persist_file_path));

if (!std::filesystem::exists(persist_file_path)) {
fc_elog(vote_logger, "unable to open finalizer safety persistence file ${p}, file doesn't exist",
fc_ilog(vote_logger, "unable to open finalizer safety persistence file ${p}, file doesn't exist (which is expected on the first use of a BLS finalizer key)",
("p", persist_file_path));
return res;
}
Expand Down Expand Up @@ -323,16 +323,4 @@ void my_finalizers_t::set_default_safety_information(const fsi_t& fsi) {
default_fsi = fsi;
}

bool my_finalizers_t::are_equivalent(uint32_t version, const fsi_map& old, const fsi_map& current) {
assert(version < current_safety_file_version); // this function compares an older version with the current one
switch(version) {
case 0:
return old == current;
break;
default:
assert(0);
return false;
}
}

} // namespace eosio::chain
1 change: 0 additions & 1 deletion libraries/chain/include/eosio/chain/finality/finalizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ namespace eosio::chain {
// for testing purposes only, not thread safe
const fsi_t& get_fsi(const bls_public_key& k) { return finalizers[k].fsi; }
void set_fsi(const bls_public_key& k, const fsi_t& fsi) { finalizers[k].fsi = fsi; }
static bool are_equivalent(uint32_t version, const fsi_map& old, const fsi_map& current);
};

}
Expand Down
46 changes: 35 additions & 11 deletions unittests/finalizer_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ BOOST_AUTO_TEST_CASE( finalizer_safety_file_io ) try {


BOOST_AUTO_TEST_CASE( finalizer_safety_file_versioning ) try {
std::filesystem::path test_data_path { UNITTEST_TEST_DATA_DIR };
namespace fs = std::filesystem;

fs::path test_data_path { UNITTEST_TEST_DATA_DIR };
auto fsi_reference_dir = test_data_path / "fsi";

auto create_fsi_reference = [&](my_finalizers_t& fset) {
Expand All @@ -239,7 +241,7 @@ BOOST_AUTO_TEST_CASE( finalizer_safety_file_versioning ) try {
set_fsi<decltype(fsi), 0, 1, 2>(fset, keys, fsi);
};

auto create_fsi_reference_file = [&](const std::filesystem::path& safety_file_path) {
auto create_fsi_reference_file = [&](const fs::path& safety_file_path) {
my_finalizers_t fset{safety_file_path};
create_fsi_reference(fset);
fset.save_finalizer_safety_info();
Expand All @@ -262,20 +264,42 @@ BOOST_AUTO_TEST_CASE( finalizer_safety_file_versioning ) try {
if (save_fsi_reference_file)
create_fsi_reference_file(mk_versioned_fsi_file_path(current_version));

auto load_fsi_map = [&](const std::filesystem::path& safety_file_path) {
BOOST_REQUIRE(std::filesystem::exists(safety_file_path));
my_finalizers_t fset{safety_file_path};
auto map = fset.load_finalizer_safety_info();
return map;
auto load_fsi_map = [&](const fs::path& safety_file_path, bool save_after_load) {
BOOST_REQUIRE(fs::exists(safety_file_path));
my_finalizers_t fset{safety_file_path};
auto map = fset.load_finalizer_safety_info();
if (save_after_load) {
bls_pub_priv_key_map_t local_finalizers = create_local_finalizers<0, 1, 2>(create_keys(3));
fset.set_keys(local_finalizers); // need to call set_keys otherwise inactive keys not saved.
fset.save_finalizer_safety_info();

}
return map;
};

// make sure we can read previous versions of the safety file correctly
// Make sure we can read previous versions of the safety file correctly.
// --------------------------------------------------------------------
auto fsi_map_current = load_fsi_map(mk_versioned_fsi_file_path(current_version));
fc::temp_directory tempdir;

for (size_t i=0; i<current_version; ++i) {
auto fsi_map_vi = load_fsi_map(mk_versioned_fsi_file_path(i));
BOOST_REQUIRE(my_finalizers_t::are_equivalent(i, fsi_map_vi, fsi_map_current));
auto ref_path = mk_versioned_fsi_file_path(i);
auto copy_path = tempdir.path() / ref_path.filename();
fs::copy_file(ref_path, copy_path, fs::copy_options::none);
std::this_thread::sleep_for(std::chrono::milliseconds{10});

// first load the reference file in the old format, and then save it in the new version format
// -------------------------------------------------------------------------------------------
auto last_write = fs::last_write_time(copy_path);
auto last_size = fs::file_size(copy_path);
auto fsi_map_vi = load_fsi_map(copy_path, true);

BOOST_REQUIRE_GT(fs::last_write_time(copy_path), last_write); // just a sanity check.
BOOST_REQUIRE_NE(fs::file_size(copy_path), last_size); // we expect the size to be different if the format changes

// then load it again as the new version
auto fsi_map_vn = load_fsi_map(copy_path, false);

BOOST_REQUIRE(fsi_map_vi == fsi_map_vn);
}

} FC_LOG_AND_RETHROW()
Expand Down
Loading