From 66e6af89ce413ed7bb9f427d64250c6cc4bc3120 Mon Sep 17 00:00:00 2001 From: kenshi84 Date: Fri, 16 Dec 2016 18:10:03 +0900 Subject: added experimental boost::archive::portable_binary_{i|o}archive --- .../cryptonote_boost_serialization.h | 8 +++-- src/simplewallet/simplewallet.cpp | 4 +-- src/wallet/CMakeLists.txt | 1 + src/wallet/wallet2.cpp | 34 +++++++++++++++++----- src/wallet/wallet2.h | 7 ++--- 5 files changed, 39 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/cryptonote_core/cryptonote_boost_serialization.h b/src/cryptonote_core/cryptonote_boost_serialization.h index 663ef5070..9a81ac307 100644 --- a/src/cryptonote_core/cryptonote_boost_serialization.h +++ b/src/cryptonote_core/cryptonote_boost_serialization.h @@ -39,6 +39,8 @@ #include #include #include +#include +#include #include "cryptonote_basic.h" #include "common/unordered_containers_boost_serialization.h" #include "crypto/crypto.h" @@ -230,7 +232,8 @@ namespace boost // a & x.senderPk; // not serialized, as we do not use it in monero currently } - inline void serializeOutPk(boost::archive::binary_iarchive &a, rct::ctkeyV &outPk_, const boost::serialization::version_type ver) + template + inline typename std::enable_if::type serializeOutPk(Archive &a, rct::ctkeyV &outPk_, const boost::serialization::version_type ver) { rct::keyV outPk; a & outPk; @@ -242,7 +245,8 @@ namespace boost } } - inline void serializeOutPk(boost::archive::binary_oarchive &a, rct::ctkeyV &outPk_, const boost::serialization::version_type ver) + template + inline typename std::enable_if::type serializeOutPk(Archive &a, rct::ctkeyV &outPk_, const boost::serialization::version_type ver) { rct::keyV outPk(outPk_.size()); for (size_t n = 0; n < outPk_.size(); ++n) diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index b46447975..222998dfd 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -3882,7 +3882,7 @@ bool simple_wallet::export_outputs(const std::vector &args) std::vector outs = m_wallet->export_outputs(); std::stringstream oss; - boost::archive::binary_oarchive ar(oss); + boost::archive::portable_binary_oarchive ar(oss); ar << outs; std::string magic(OUTPUT_EXPORT_FILE_MAGIC, strlen(OUTPUT_EXPORT_FILE_MAGIC)); @@ -3962,7 +3962,7 @@ bool simple_wallet::import_outputs(const std::vector &args) std::string body(data, headerlen); std::stringstream iss; iss << body; - boost::archive::binary_iarchive ar(iss); + boost::archive::portable_binary_iarchive ar(iss); std::vector outputs; ar >> outputs; diff --git a/src/wallet/CMakeLists.txt b/src/wallet/CMakeLists.txt index 056a1ca10..e34b7b579 100644 --- a/src/wallet/CMakeLists.txt +++ b/src/wallet/CMakeLists.txt @@ -73,6 +73,7 @@ target_link_libraries(wallet cryptonote_core mnemonics p2p + boost_extra ${Boost_CHRONO_LIBRARY} ${Boost_SERIALIZATION_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index db4fae557..f22410563 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -30,8 +30,6 @@ #include #include -#include -#include #include #include #include @@ -2313,16 +2311,38 @@ void wallet2::load(const std::string& wallet_, const std::string& password) std::stringstream iss; iss << cache_data; - boost::archive::binary_iarchive ar(iss); - ar >> *this; + try { + boost::archive::portable_binary_iarchive ar(iss); + ar >> *this; + } + 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); + iss.str(""); + iss << cache_data; + boost::archive::binary_iarchive ar(iss); + ar >> *this; + } } catch (...) { LOG_PRINT_L1("Failed to load encrypted cache, trying unencrypted"); std::stringstream iss; iss << buf; - boost::archive::binary_iarchive ar(iss); - ar >> *this; + try { + boost::archive::portable_binary_iarchive ar(iss); + ar >> *this; + } + 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); + iss.str(""); + iss << buf; + boost::archive::binary_iarchive ar(iss); + ar >> *this; + } } THROW_WALLET_EXCEPTION_IF( m_account_public_address.m_spend_public_key != m_account.get_keys().m_account_address.m_spend_public_key || @@ -2396,7 +2416,7 @@ void wallet2::store_to(const std::string &path, const std::string &password) } // preparing wallet data std::stringstream oss; - boost::archive::binary_oarchive ar(oss); + boost::archive::portable_binary_oarchive ar(oss); ar << *this; wallet2::cache_file_data cache_file_data = boost::value_initialized(); diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index c3381730b..9f4180c0d 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -31,7 +31,6 @@ #pragma once #include -#include #include #include @@ -676,11 +675,11 @@ namespace boost namespace serialization { template - inline void initialize_transfer_details(Archive &a, tools::wallet2::transfer_details &x, const boost::serialization::version_type ver) + inline typename std::enable_if::type initialize_transfer_details(Archive &a, tools::wallet2::transfer_details &x, const boost::serialization::version_type ver) { } - template<> - inline void initialize_transfer_details(boost::archive::binary_iarchive &a, tools::wallet2::transfer_details &x, const boost::serialization::version_type ver) + template + inline typename std::enable_if::type initialize_transfer_details(Archive &a, tools::wallet2::transfer_details &x, const boost::serialization::version_type ver) { if (ver < 1) { -- cgit v1.2.3 From d1d6e27ab661f71d90fb6530db84d5a2b92550a8 Mon Sep 17 00:00:00 2001 From: kenshi84 Date: Tue, 20 Dec 2016 11:51:22 +0900 Subject: moved boost cpp into hpp since they're supposed to be header only --- src/wallet/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/wallet/CMakeLists.txt b/src/wallet/CMakeLists.txt index e34b7b579..056a1ca10 100644 --- a/src/wallet/CMakeLists.txt +++ b/src/wallet/CMakeLists.txt @@ -73,7 +73,6 @@ target_link_libraries(wallet cryptonote_core mnemonics p2p - boost_extra ${Boost_CHRONO_LIBRARY} ${Boost_SERIALIZATION_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} -- cgit v1.2.3 From 2ac80075444ebac2feef59f98c912342a708606d Mon Sep 17 00:00:00 2001 From: kenshi84 Date: Tue, 20 Dec 2016 13:04:19 +0900 Subject: 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(-) (limited to 'src') diff --git a/src/common/boost_serialization_helper.h b/src/common/boost_serialization_helper.h index c640a1705..fe01532d8 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 c3f2b4446..89ad4184c 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 70b2ccc79..ba369afbd 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 9a81ac307..7423b222a 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 442c42517..f32e7a435 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 c19ecf65f..fa69abd6e 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 f22410563..e58c31156 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); -- cgit v1.2.3 From 07b9138cad7d5e8aa046f2e555563760c11a9e37 Mon Sep 17 00:00:00 2001 From: kenshi84 Date: Tue, 20 Dec 2016 13:26:39 +0900 Subject: support importing unportable outputs --- src/simplewallet/simplewallet.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 222998dfd..251f9c231 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -3962,10 +3962,19 @@ bool simple_wallet::import_outputs(const std::vector &args) std::string body(data, headerlen); std::stringstream iss; iss << body; - boost::archive::portable_binary_iarchive ar(iss); std::vector outputs; - ar >> outputs; - + try + { + boost::archive::portable_binary_iarchive ar(iss); + ar >> outputs; + } + catch (...) + { + iss.str(""); + iss << body; + boost::archive::binary_iarchive ar(iss); + ar >> outputs; + } size_t n_outputs = m_wallet->import_outputs(outputs); success_msg_writer() << boost::lexical_cast(n_outputs) << " outputs imported"; } -- cgit v1.2.3