From 2ac80075444ebac2feef59f98c912342a708606d Mon Sep 17 00:00:00 2001 From: kenshi84 Date: Tue, 20 Dec 2016 13:04:19 +0900 Subject: [PATCH] also use portable serializer for boost_serialization_helper.h and net_node.inl, completely adandon boost/archive/binary_oarchive.hpp --- src/common/boost_serialization_helper.h | 25 ++++++++++++--- src/cryptonote_core/account.cpp | 2 -- src/cryptonote_core/blockchain.cpp | 2 -- .../cryptonote_boost_serialization.h | 1 - src/p2p/net_node.inl | 32 +++++++++++++++---- src/p2p/net_peerlist.h | 3 +- src/wallet/wallet2.cpp | 4 +-- 7 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/common/boost_serialization_helper.h b/src/common/boost_serialization_helper.h index c640a170..fe01532d 100644 --- a/src/common/boost_serialization_helper.h +++ b/src/common/boost_serialization_helper.h @@ -30,8 +30,9 @@ #pragma once -#include #include +#include +#include namespace tools @@ -76,7 +77,7 @@ namespace tools return false; #endif - boost::archive::binary_oarchive a(data_file); + boost::archive::portable_binary_oarchive a(data_file); a << obj; if (data_file.fail()) return false; @@ -101,9 +102,23 @@ namespace tools data_file.open( file_path, std::ios_base::binary | std::ios_base::in); if(data_file.fail()) return false; - boost::archive::binary_iarchive a(data_file); - - a >> obj; + try + { + // first try reading in portable mode + boost::archive::portable_binary_iarchive a(data_file); + a >> obj; + } + catch(...) + { + // if failed, try reading in unportable mode + boost::filesystem::copy_file(file_path, file_path + ".unportable", boost::filesystem::copy_option::overwrite_if_exists); + data_file.close(); + data_file.open( file_path, std::ios_base::binary | std::ios_base::in); + if(data_file.fail()) + return false; + boost::archive::binary_iarchive a(data_file); + a >> obj; + } return !data_file.fail(); CATCH_ENTRY_L0("unserialize_obj_from_file", false); } diff --git a/src/cryptonote_core/account.cpp b/src/cryptonote_core/account.cpp index c3f2b444..89ad4184 100644 --- a/src/cryptonote_core/account.cpp +++ b/src/cryptonote_core/account.cpp @@ -28,8 +28,6 @@ // // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers -#include -#include #include #include "include_base_utils.h" diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 70b2ccc7..ba369afb 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -30,8 +30,6 @@ #include #include -#include -#include #include #include "include_base_utils.h" diff --git a/src/cryptonote_core/cryptonote_boost_serialization.h b/src/cryptonote_core/cryptonote_boost_serialization.h index 9a81ac30..7423b222 100644 --- a/src/cryptonote_core/cryptonote_boost_serialization.h +++ b/src/cryptonote_core/cryptonote_boost_serialization.h @@ -38,7 +38,6 @@ #include #include #include -#include #include #include #include "cryptonote_basic.h" diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index 442c4251..f32e7a43 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -137,14 +137,34 @@ namespace nodetool { try { - boost::archive::binary_iarchive a(p2p_data); + // first try reading in portable mode + boost::archive::portable_binary_iarchive a(p2p_data); a >> *this; } - catch (const std::exception &e) + catch (...) { - LOG_ERROR("Failed to load p2p config file, falling back to default config"); - m_peerlist = peerlist_manager(); // it was probably half clobbered by the failed load - make_default_config(); + // if failed, try reading in unportable mode + boost::filesystem::copy_file(state_file_path, state_file_path + ".unportable", boost::filesystem::copy_option::overwrite_if_exists); + p2p_data.close(); + p2p_data.open( state_file_path , std::ios_base::binary | std::ios_base::in); + if(!p2p_data.fail()) + { + try + { + boost::archive::binary_iarchive a(p2p_data); + a >> *this; + } + catch (const std::exception &e) + { + LOG_ERROR("Failed to load p2p config file, falling back to default config"); + m_peerlist = peerlist_manager(); // it was probably half clobbered by the failed load + make_default_config(); + } + } + else + { + make_default_config(); + } } }else { @@ -645,7 +665,7 @@ namespace nodetool return false; }; - boost::archive::binary_oarchive a(p2p_data); + boost::archive::portable_binary_oarchive a(p2p_data); a << *this; return true; CATCH_ENTRY_L0("blockchain_storage::save", false); diff --git a/src/p2p/net_peerlist.h b/src/p2p/net_peerlist.h index c19ecf65..fa69abd6 100644 --- a/src/p2p/net_peerlist.h +++ b/src/p2p/net_peerlist.h @@ -36,8 +36,9 @@ #include //#include //#include -#include #include +#include +#include #include #include diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index f2241056..e58c3115 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -2318,7 +2318,7 @@ void wallet2::load(const std::string& wallet_, const std::string& password) catch (...) { LOG_PRINT_L0("Failed to open portable binary, trying unportable"); - boost::filesystem::copy_file(m_wallet_file, m_wallet_file + ".old", boost::filesystem::copy_option::overwrite_if_exists); + boost::filesystem::copy_file(m_wallet_file, m_wallet_file + ".unportable", boost::filesystem::copy_option::overwrite_if_exists); iss.str(""); iss << cache_data; boost::archive::binary_iarchive ar(iss); @@ -2337,7 +2337,7 @@ void wallet2::load(const std::string& wallet_, const std::string& password) catch (...) { LOG_PRINT_L0("Failed to open portable binary, trying unportable"); - boost::filesystem::copy_file(m_wallet_file, m_wallet_file + ".old", boost::filesystem::copy_option::overwrite_if_exists); + boost::filesystem::copy_file(m_wallet_file, m_wallet_file + ".unportable", boost::filesystem::copy_option::overwrite_if_exists); iss.str(""); iss << buf; boost::archive::binary_iarchive ar(iss);