diff options
Diffstat (limited to 'src')
34 files changed, 1099 insertions, 737 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 7f0b0c18f..cf38466fe 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -81,6 +81,9 @@ target_link_libraries(common PRIVATE ${OPENSSL_LIBRARIES} ${EXTRA_LIBRARIES}) +target_include_directories(common + PRIVATE + ${Boost_INCLUDE_DIRS}) #monero_install_headers(common # ${common_headers}) diff --git a/src/common/container_helpers.h b/src/common/container_helpers.h new file mode 100644 index 000000000..5824c9c37 --- /dev/null +++ b/src/common/container_helpers.h @@ -0,0 +1,170 @@ +// Copyright (c) 2022, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Miscellaneous container helpers. + +#pragma once + +//local headers + +//third party headers + +//standard headers +#include <algorithm> +#include <unordered_map> +#include <utility> + +//forward declarations + + +namespace tools +{ + +/// convert an arbitrary function to functor +template <typename F> +inline auto as_functor(F f) +{ + return [f = std::move(f)](auto&&... args) { return f(std::forward<decltype(args)>(args)...); }; +} +/// convert a binary comparison function to a functor +/// note: for most use-cases 'const T&' will work because only non-trivial types need a user-defined comparison operation +template <typename T, typename ComparisonOpT = bool(const T&, const T&)> +inline auto compare_func(ComparisonOpT comparison_op_func) +{ + static_assert( + std::is_same< + bool, + decltype( + comparison_op_func( + std::declval<std::remove_cv_t<T>>(), + std::declval<std::remove_cv_t<T>>() + ) + ) + >::value, + "invalid callable - expected callable in form bool(T, T)" + ); + + return as_functor(std::move(comparison_op_func)); +} +/// test if a container is sorted and unique according to a comparison criteria (defaults to operator<) +/// NOTE: ComparisonOpT must establish 'strict weak ordering' https://en.cppreference.com/w/cpp/named_req/Compare +template <typename T, typename ComparisonOpT = std::less<typename T::value_type>> +bool is_sorted_and_unique(const T &container, ComparisonOpT comparison_op = ComparisonOpT{}) +{ + using ValueT = typename T::value_type; + static_assert( + std::is_same< + bool, + decltype( + comparison_op( + std::declval<std::remove_cv_t<ValueT>>(), + std::declval<std::remove_cv_t<ValueT>>() + ) + ) + >::value, + "invalid callable - expected callable in form bool(ValueT, ValueT)" + ); + + if (!std::is_sorted(container.begin(), container.end(), comparison_op)) + return false; + + if (std::adjacent_find(container.begin(), + container.end(), + [comparison_op = std::move(comparison_op)](const ValueT &a, const ValueT &b) -> bool + { + return !comparison_op(a, b) && !comparison_op(b, a); + }) + != container.end()) + return false; + + return true; +} +/// specialization for raw function pointers +template <typename T> +bool is_sorted_and_unique(const T &container, + bool (*const comparison_op_func)(const typename T::value_type&, const typename T::value_type&)) +{ + return is_sorted_and_unique(container, compare_func<typename T::value_type>(comparison_op_func)); +} +/// convenience wrapper for checking if the key to a mapped object is correct for that object +/// example: std::unorderd_map<rct::key, std::pair<rct::key, rct::xmr_amount>> where the map key is supposed to +/// reproduce the pair's rct::key; use the predicate to check that relationship +template <typename KeyT, typename ValueT, typename PredT> +bool keys_match_internal_values(const std::unordered_map<KeyT, ValueT> &map, PredT check_key_func) +{ + static_assert( + std::is_same< + bool, + decltype( + check_key_func( + std::declval<std::remove_cv_t<KeyT>>(), + std::declval<std::remove_cv_t<ValueT>>() + ) + ) + >::value, + "invalid callable - expected callable in form bool(KeyT, ValueT)" + ); + + for (const auto &map_element : map) + { + if (!check_key_func(map_element.first, map_element.second)) + return false; + } + + return true; +} +/// convenience wrapper for getting the last element after emplacing back +template <typename ContainerT> +typename ContainerT::value_type& add_element(ContainerT &container) +{ + container.emplace_back(); + return container.back(); +} +/// convenience erasor for unordered maps: std::erase_if(std::unordered_map) is C++20 +template <typename KeyT, typename ValueT, typename PredT> +void for_all_in_map_erase_if(std::unordered_map<KeyT, ValueT> &map_inout, PredT predicate) +{ + using MapValueT = typename std::unordered_map<KeyT, ValueT>::value_type; + static_assert( + std::is_same< + bool, + decltype(predicate(std::declval<std::remove_cv_t<MapValueT>>())) + >::value, + "invalid callable - expected callable in form bool(Value)" + ); + + for (auto map_it = map_inout.begin(); map_it != map_inout.end();) + { + if (predicate(*map_it)) + map_it = map_inout.erase(map_it); + else + ++map_it; + } +} + +} //namespace tools diff --git a/src/common/dns_utils.cpp b/src/common/dns_utils.cpp index 324566afd..ce4555ae3 100644 --- a/src/common/dns_utils.cpp +++ b/src/common/dns_utils.cpp @@ -340,6 +340,7 @@ std::vector<std::string> DNSResolver::get_record(const std::string& url, int rec dnssec_valid = result->secure && !result->bogus; if (dnssec_available && !dnssec_valid) MWARNING("Invalid DNSSEC " << get_record_name(record_type) << " record signature for " << url << ": " << result->why_bogus); + MWARNING("Possibly your DNS service is problematic. You can have monerod use an alternate via env variable DNS_PUBLIC. Example: DNS_PUBLIC=tcp://9.9.9.9"); if (result->havedata) { for (size_t i=0; result->data[i] != NULL; i++) diff --git a/src/common/util.cpp b/src/common/util.cpp index a0074f44c..b4f3360ef 100644 --- a/src/common/util.cpp +++ b/src/common/util.cpp @@ -78,7 +78,6 @@ using namespace epee; #include <strsafe.h> #else #include <sys/file.h> - #include <sys/utsname.h> #include <sys/stat.h> #endif #include <boost/filesystem.hpp> @@ -302,277 +301,6 @@ namespace tools #endif } -#ifdef WIN32 - std::string get_windows_version_display_string() - { - typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO); - typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD); -#define BUFSIZE 10000 - - char pszOS[BUFSIZE] = {0}; - OSVERSIONINFOEX osvi; - SYSTEM_INFO si; - PGNSI pGNSI; - PGPI pGPI; - BOOL bOsVersionInfoEx; - DWORD dwType; - - ZeroMemory(&si, sizeof(SYSTEM_INFO)); - ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); - - osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); - bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*) &osvi); - - if(!bOsVersionInfoEx) return pszOS; - - // Call GetNativeSystemInfo if supported or GetSystemInfo otherwise. - - pGNSI = (PGNSI) GetProcAddress( - GetModuleHandle(TEXT("kernel32.dll")), - "GetNativeSystemInfo"); - if(NULL != pGNSI) - pGNSI(&si); - else GetSystemInfo(&si); - - if ( VER_PLATFORM_WIN32_NT==osvi.dwPlatformId && - osvi.dwMajorVersion > 4 ) - { - StringCchCopy(pszOS, BUFSIZE, TEXT("Microsoft ")); - - // Test for the specific product. - if ( osvi.dwMajorVersion == 10 ) - { - if ( osvi.dwMinorVersion == 0 ) - { - if( osvi.wProductType == VER_NT_WORKSTATION ) - StringCchCat(pszOS, BUFSIZE, TEXT("Windows 10 ")); - else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2016 " )); - } - } - - if ( osvi.dwMajorVersion == 6 ) - { - if ( osvi.dwMinorVersion == 0 ) - { - if( osvi.wProductType == VER_NT_WORKSTATION ) - StringCchCat(pszOS, BUFSIZE, TEXT("Windows Vista ")); - else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 " )); - } - - if ( osvi.dwMinorVersion == 1 ) - { - if( osvi.wProductType == VER_NT_WORKSTATION ) - StringCchCat(pszOS, BUFSIZE, TEXT("Windows 7 ")); - else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 R2 " )); - } - - if ( osvi.dwMinorVersion == 2 ) - { - if( osvi.wProductType == VER_NT_WORKSTATION ) - StringCchCat(pszOS, BUFSIZE, TEXT("Windows 8 ")); - else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2012 " )); - } - - if ( osvi.dwMinorVersion == 3 ) - { - if( osvi.wProductType == VER_NT_WORKSTATION ) - StringCchCat(pszOS, BUFSIZE, TEXT("Windows 8.1 ")); - else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2012 R2 " )); - } - - pGPI = (PGPI) GetProcAddress( - GetModuleHandle(TEXT("kernel32.dll")), - "GetProductInfo"); - - pGPI( osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType); - - switch( dwType ) - { - case PRODUCT_ULTIMATE: - StringCchCat(pszOS, BUFSIZE, TEXT("Ultimate Edition" )); - break; - case PRODUCT_PROFESSIONAL: - StringCchCat(pszOS, BUFSIZE, TEXT("Professional" )); - break; - case PRODUCT_HOME_PREMIUM: - StringCchCat(pszOS, BUFSIZE, TEXT("Home Premium Edition" )); - break; - case PRODUCT_HOME_BASIC: - StringCchCat(pszOS, BUFSIZE, TEXT("Home Basic Edition" )); - break; - case PRODUCT_ENTERPRISE: - StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition" )); - break; - case PRODUCT_BUSINESS: - StringCchCat(pszOS, BUFSIZE, TEXT("Business Edition" )); - break; - case PRODUCT_STARTER: - StringCchCat(pszOS, BUFSIZE, TEXT("Starter Edition" )); - break; - case PRODUCT_CLUSTER_SERVER: - StringCchCat(pszOS, BUFSIZE, TEXT("Cluster Server Edition" )); - break; - case PRODUCT_DATACENTER_SERVER: - StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition" )); - break; - case PRODUCT_DATACENTER_SERVER_CORE: - StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition (core installation)" )); - break; - case PRODUCT_ENTERPRISE_SERVER: - StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition" )); - break; - case PRODUCT_ENTERPRISE_SERVER_CORE: - StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition (core installation)" )); - break; - case PRODUCT_ENTERPRISE_SERVER_IA64: - StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems" )); - break; - case PRODUCT_SMALLBUSINESS_SERVER: - StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server" )); - break; - case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM: - StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server Premium Edition" )); - break; - case PRODUCT_STANDARD_SERVER: - StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition" )); - break; - case PRODUCT_STANDARD_SERVER_CORE: - StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition (core installation)" )); - break; - case PRODUCT_WEB_SERVER: - StringCchCat(pszOS, BUFSIZE, TEXT("Web Server Edition" )); - break; - } - } - - if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 ) - { - if( GetSystemMetrics(SM_SERVERR2) ) - StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Server 2003 R2, ")); - else if ( osvi.wSuiteMask & VER_SUITE_STORAGE_SERVER ) - StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Storage Server 2003")); - else if ( osvi.wSuiteMask & VER_SUITE_WH_SERVER ) - StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Home Server")); - else if( osvi.wProductType == VER_NT_WORKSTATION && - si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64) - { - StringCchCat(pszOS, BUFSIZE, TEXT( "Windows XP Professional x64 Edition")); - } - else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2003, ")); - - // Test for the server type. - if ( osvi.wProductType != VER_NT_WORKSTATION ) - { - if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64 ) - { - if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) - StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Edition for Itanium-based Systems" )); - else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) - StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise Edition for Itanium-based Systems" )); - } - - else if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 ) - { - if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) - StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter x64 Edition" )); - else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) - StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise x64 Edition" )); - else StringCchCat(pszOS, BUFSIZE, TEXT( "Standard x64 Edition" )); - } - - else - { - if ( osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER ) - StringCchCat(pszOS, BUFSIZE, TEXT( "Compute Cluster Edition" )); - else if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) - StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Edition" )); - else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) - StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise Edition" )); - else if ( osvi.wSuiteMask & VER_SUITE_BLADE ) - StringCchCat(pszOS, BUFSIZE, TEXT( "Web Edition" )); - else StringCchCat(pszOS, BUFSIZE, TEXT( "Standard Edition" )); - } - } - } - - if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 ) - { - StringCchCat(pszOS, BUFSIZE, TEXT("Windows XP ")); - if( osvi.wSuiteMask & VER_SUITE_PERSONAL ) - StringCchCat(pszOS, BUFSIZE, TEXT( "Home Edition" )); - else StringCchCat(pszOS, BUFSIZE, TEXT( "Professional" )); - } - - if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 ) - { - StringCchCat(pszOS, BUFSIZE, TEXT("Windows 2000 ")); - - if ( osvi.wProductType == VER_NT_WORKSTATION ) - { - StringCchCat(pszOS, BUFSIZE, TEXT( "Professional" )); - } - else - { - if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) - StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Server" )); - else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) - StringCchCat(pszOS, BUFSIZE, TEXT( "Advanced Server" )); - else StringCchCat(pszOS, BUFSIZE, TEXT( "Server" )); - } - } - - // Include service pack (if any) and build number. - - if( strlen(osvi.szCSDVersion) > 0 ) - { - StringCchCat(pszOS, BUFSIZE, TEXT(" ") ); - StringCchCat(pszOS, BUFSIZE, osvi.szCSDVersion); - } - - TCHAR buf[80]; - - StringCchPrintf( buf, 80, TEXT(" (build %d)"), osvi.dwBuildNumber); - StringCchCat(pszOS, BUFSIZE, buf); - - if ( osvi.dwMajorVersion >= 6 ) - { - if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 ) - StringCchCat(pszOS, BUFSIZE, TEXT( ", 64-bit" )); - else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_INTEL ) - StringCchCat(pszOS, BUFSIZE, TEXT(", 32-bit")); - } - - return pszOS; - } - else - { - printf( "This sample does not support this version of Windows.\n"); - return pszOS; - } - } -#else -std::string get_nix_version_display_string() -{ - struct utsname un; - - if(uname(&un) < 0) - return std::string("*nix: failed to get os version"); - return std::string() + un.sysname + " " + un.version + " " + un.release; -} -#endif - - - - std::string get_os_version_string() - { -#ifdef WIN32 - return get_windows_version_display_string(); -#else - return get_nix_version_display_string(); -#endif - } - - #ifdef WIN32 std::string get_special_folder_path(int nfolder, bool iscreate) @@ -882,13 +610,6 @@ std::string get_nix_version_display_string() bool is_local_address(const std::string &address) { - // always assume Tor/I2P addresses to be untrusted by default - if (is_privacy_preserving_network(address)) - { - MDEBUG("Address '" << address << "' is Tor/I2P, non local"); - return false; - } - // extract host epee::net_utils::http::url_content u_c; if (!epee::net_utils::parse_url(address, u_c)) @@ -902,20 +623,22 @@ std::string get_nix_version_display_string() return false; } - // resolve to IP - boost::asio::io_service io_service; - boost::asio::ip::tcp::resolver resolver(io_service); - boost::asio::ip::tcp::resolver::query query(u_c.host, ""); - boost::asio::ip::tcp::resolver::iterator i = resolver.resolve(query); - while (i != boost::asio::ip::tcp::resolver::iterator()) + if (u_c.host == "localhost" || boost::ends_with(u_c.host, ".localhost")) { // RFC 6761 (6.3) + MDEBUG("Address '" << address << "' is local"); + return true; + } + + boost::system::error_code ec; + const auto parsed_ip = boost::asio::ip::address::from_string(u_c.host, ec); + if (ec) { + MDEBUG("Failed to parse '" << address << "' as IP address: " << ec.message() << ". Considering it not local"); + return false; + } + + if (parsed_ip.is_loopback()) { - const boost::asio::ip::tcp::endpoint &ep = *i; - if (ep.address().is_loopback()) - { - MDEBUG("Address '" << address << "' is local"); - return true; - } - ++i; + MDEBUG("Address '" << address << "' is local"); + return true; } MDEBUG("Address '" << address << "' is not local"); diff --git a/src/common/variant.h b/src/common/variant.h new file mode 100644 index 000000000..ffb34e40a --- /dev/null +++ b/src/common/variant.h @@ -0,0 +1,167 @@ +// Copyright (c) 2022, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Variant wrapper class. + +#pragma once + +//local headers + +//third party headers +#include <boost/blank.hpp> +#include <boost/mpl/begin_end.hpp> +#include <boost/mpl/distance.hpp> +#include <boost/mpl/find.hpp> +#include <boost/mpl/vector.hpp> +#include <boost/none_t.hpp> +#include <boost/variant/apply_visitor.hpp> +#include <boost/variant/get.hpp> +#include <boost/variant/static_visitor.hpp> +#include <boost/variant/variant.hpp> + +//standard headers +#include <stdexcept> +#include <type_traits> +#include <utility> + +//forward declarations + + +namespace tools +{ + +[[noreturn]] inline void variant_static_visitor_blank_err() +{ throw std::runtime_error("variant: tried to visit an empty variant."); } +[[noreturn]] inline void variant_unwrap_err() +{ throw std::runtime_error("variant: tried to access value of incorrect type."); } + +//// +// variant: convenience wrapper around boost::variant with a cleaner interface +// - the variant is 'optional' - an empty variant will evaluate to 'false' and an initialized variant will be 'true' +/// +template <typename ResultT> +struct variant_static_visitor : public boost::static_visitor<ResultT> +{ + /// provide visitation for empty variants + /// - add this to your visitor with: using variant_static_visitor::operator(); + [[noreturn]] ResultT operator()(const boost::blank) { variant_static_visitor_blank_err(); } + [[noreturn]] ResultT operator()(const boost::blank) const { variant_static_visitor_blank_err(); } +}; + +template <typename... Types> +class variant final +{ + using VType = boost::variant<boost::blank, Types...>; + +public: +//constructors + /// default constructor + variant() = default; + variant(boost::none_t) : variant{} {} //act like boost::optional + + /// construct from variant type (use enable_if to avoid issues with copy/move constructor) + template <typename T, + typename std::enable_if< + !std::is_same< + std::remove_cv_t<std::remove_reference_t<T>>, + variant<Types...> + >::value, + bool + >::type = true> + variant(T &&value) : m_value{std::forward<T>(value)} {} + +//overloaded operators + /// boolean operator: true if the variant isn't empty/uninitialized + explicit operator bool() const noexcept { return !this->is_empty(); } + +//member functions + /// check if empty/uninitialized + bool is_empty() const noexcept { return m_value.which() == 0; } + + /// check the variant type + template <typename T> + bool is_type() const noexcept { return this->index() == this->type_index_of<T>(); } + + /// try to get a handle to the embedded value (return nullptr on failure) + template <typename T> + T* try_unwrap() noexcept { return boost::strict_get< T>(&m_value); } + template <typename T> + const T* try_unwrap() const noexcept { return boost::strict_get<const T>(&m_value); } + + /// get a handle to the embedded value + template <typename T> + T& unwrap() + { + T *value_ptr{this->try_unwrap<T>()}; + if (!value_ptr) variant_unwrap_err(); + return *value_ptr; + } + template <typename T> + const T& unwrap() const + { + const T *value_ptr{this->try_unwrap<T>()}; + if (!value_ptr) variant_unwrap_err(); + return *value_ptr; + } + + /// get the type index of the currently stored type + int index() const noexcept { return m_value.which(); } + + /// get the type index of a requested type (compile error for invalid types) (boost::mp11 is boost 1.66.0) + template <typename T> + static constexpr int type_index_of() noexcept + { + using types = boost::mpl::vector<boost::blank, Types...>; + using elem = typename boost::mpl::find<types, T>::type; + using begin = typename boost::mpl::begin<types>::type; + return boost::mpl::distance<begin, elem>::value; + } + + /// check if two variants have the same type + static bool same_type(const variant<Types...> &v1, const variant<Types...> &v2) noexcept + { return v1.index() == v2.index(); } + + /// apply a visitor to the variant + template <typename VisitorT> + typename VisitorT::result_type visit(VisitorT &&visitor) + { + return boost::apply_visitor(std::forward<VisitorT>(visitor), m_value); + } + template <typename VisitorT> + typename VisitorT::result_type visit(VisitorT &&visitor) const + { + return boost::apply_visitor(std::forward<VisitorT>(visitor), m_value); + } + +private: +//member variables + /// variant of all value types + VType m_value; +}; + +} //namespace tools diff --git a/src/crypto/CMakeLists.txt b/src/crypto/CMakeLists.txt index e17584c90..5dfc121ce 100644 --- a/src/crypto/CMakeLists.txt +++ b/src/crypto/CMakeLists.txt @@ -33,6 +33,7 @@ set(crypto_sources crypto-ops-data.c crypto-ops.c crypto.cpp + generators.cpp groestl.c hash-extra-blake.c hash-extra-groestl.c diff --git a/src/crypto/generators.cpp b/src/crypto/generators.cpp new file mode 100644 index 000000000..a4539f473 --- /dev/null +++ b/src/crypto/generators.cpp @@ -0,0 +1,186 @@ +// Copyright (c) 2022, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "generators.h" + +#include "crypto.h" +extern "C" +{ +#include "crypto-ops.h" +} +#include "hash.h" + +#include <cassert> +#include <cstddef> +#include <cstdint> +#include <mutex> + +namespace crypto +{ + +/// constexpr assert for old gcc bug: https://stackoverflow.com/questions/34280729/throw-in-constexpr-function +/// - this function won't compile in a constexpr context if b == false +constexpr void constexpr_assert(const bool b) { b ? 0 : throw std::runtime_error("constexpr assert failed"); }; + +/// constexpr paste bytes into an array-of-bytes type +template<typename T> +constexpr T bytes_to(const std::initializer_list<unsigned char> bytes) +{ + T out{}; // zero-initialize trailing bytes + + auto current = std::begin(out.data); + constexpr_assert(static_cast<long>(bytes.size()) <= std::end(out.data) - current); + + for (const unsigned char byte : bytes) + *current++ = byte; + return out; +} + +// generators +//standard ed25519 generator G: {x, 4/5} (positive x when decompressing y = 4/5) +constexpr public_key G = bytes_to<public_key>({ 0x58, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, + 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66 }); +//pedersen commitment generator H: toPoint(cn_fast_hash(G)) +constexpr public_key H = bytes_to<public_key>({ 0x8b, 0x65, 0x59, 0x70, 0x15, 0x37, 0x99, 0xaf, 0x2a, 0xea, 0xdc, 0x9f, 0xf1, + 0xad, 0xd0, 0xea, 0x6c, 0x72, 0x51, 0xd5, 0x41, 0x54, 0xcf, 0xa9, 0x2c, 0x17, 0x3a, 0x0d, 0xd3, 0x9c, 0x1f, 0x94 }); +static ge_p3 G_p3; +static ge_p3 H_p3; +static ge_cached G_cached; +static ge_cached H_cached; + +// misc +static std::once_flag init_gens_once_flag; + +//------------------------------------------------------------------------------------------------------------------- +//------------------------------------------------------------------------------------------------------------------- +static public_key reproduce_generator_G() +{ + // G = {x, 4/5 mod q} + fe four, five, inv_five, y; + fe_0(four); + fe_0(five); + four[0] = 4; + five[0] = 5; + fe_invert(inv_five, five); + fe_mul(y, four, inv_five); + + public_key reproduced_G; + fe_tobytes(to_bytes(reproduced_G), y); + + return reproduced_G; +} +//------------------------------------------------------------------------------------------------------------------- +//------------------------------------------------------------------------------------------------------------------- +static public_key reproduce_generator_H() +{ + // H = 8*to_point(keccak(G)) + // note: this does not use the point_from_bytes() function found in H_p(), instead directly interpreting the + // input bytes as a compressed point (this can fail, so should not be used generically) + // note2: to_point(keccak(G)) is known to succeed for the canonical value of G (it will fail 7/8ths of the time + // normally) + ge_p3 temp_p3; + ge_p2 temp_p2; + ge_p1p1 temp_p1p1; + + hash H_temp_hash{cn_fast_hash(to_bytes(G), sizeof(ec_point))}; + (void)H_temp_hash; //suppress unused warning + assert(ge_frombytes_vartime(&temp_p3, reinterpret_cast<const unsigned char*>(&H_temp_hash)) == 0); + ge_p3_to_p2(&temp_p2, &temp_p3); + ge_mul8(&temp_p1p1, &temp_p2); + ge_p1p1_to_p3(&temp_p3, &temp_p1p1); + + public_key reproduced_H; + ge_p3_tobytes(to_bytes(reproduced_H), &temp_p3); + + return reproduced_H; +} +//------------------------------------------------------------------------------------------------------------------- +// Make generators, but only once +//------------------------------------------------------------------------------------------------------------------- +static void init_gens() +{ + std::call_once(init_gens_once_flag, + [&](){ + + // sanity check the generators + static_assert(static_cast<unsigned char>(G.data[0]) == 0x58, "compile-time constant sanity check"); + static_assert(static_cast<unsigned char>(H.data[0]) == 0x8b, "compile-time constant sanity check"); + + // build ge_p3 representations of generators + const int G_deserialize = ge_frombytes_vartime(&G_p3, to_bytes(G)); + const int H_deserialize = ge_frombytes_vartime(&H_p3, to_bytes(H)); + + (void)G_deserialize; assert(G_deserialize == 0); + (void)H_deserialize; assert(H_deserialize == 0); + + // get cached versions + ge_p3_to_cached(&G_cached, &G_p3); + ge_p3_to_cached(&H_cached, &H_p3); + + // in debug mode, check that generators are reproducible + (void)reproduce_generator_G; assert(reproduce_generator_G() == G); + (void)reproduce_generator_H; assert(reproduce_generator_H() == H); + + }); +} +//------------------------------------------------------------------------------------------------------------------- +public_key get_G() +{ + return G; +} +//------------------------------------------------------------------------------------------------------------------- +public_key get_H() +{ + return H; +} +//------------------------------------------------------------------------------------------------------------------- +ge_p3 get_G_p3() +{ + init_gens(); + return G_p3; +} +//------------------------------------------------------------------------------------------------------------------- +ge_p3 get_H_p3() +{ + init_gens(); + return H_p3; +} +//------------------------------------------------------------------------------------------------------------------- +ge_cached get_G_cached() +{ + init_gens(); + return G_cached; +} +//------------------------------------------------------------------------------------------------------------------- +ge_cached get_H_cached() +{ + init_gens(); + return H_cached; +} +//------------------------------------------------------------------------------------------------------------------- +} //namespace crypto diff --git a/src/platform/msc/stdbool.h b/src/crypto/generators.h index 1bfd78f57..797336203 100644 --- a/src/platform/msc/stdbool.h +++ b/src/crypto/generators.h @@ -1,4 +1,4 @@ -// Copyright (c) 2014-2023, The Monero Project +// Copyright (c) 2022, The Monero Project // // All rights reserved. // @@ -25,15 +25,23 @@ // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers #pragma once -#if !defined(__cplusplus) +extern "C" +{ +#include "crypto-ops.h" +} +#include "crypto.h" + +namespace crypto +{ -typedef int bool; -#define true 1 -#define false 0 +public_key get_G(); +public_key get_H(); +ge_p3 get_G_p3(); +ge_p3 get_H_p3(); +ge_cached get_G_cached(); +ge_cached get_H_cached(); -#endif +} //namespace crypto diff --git a/src/cryptonote_basic/cryptonote_format_utils.cpp b/src/cryptonote_basic/cryptonote_format_utils.cpp index cdbcf48c2..9aeee3d55 100644 --- a/src/cryptonote_basic/cryptonote_format_utils.cpp +++ b/src/cryptonote_basic/cryptonote_format_utils.cpp @@ -1229,7 +1229,7 @@ namespace cryptonote char *end = NULL; errno = 0; const unsigned long long ull = strtoull(buf, &end, 10); - CHECK_AND_ASSERT_THROW_MES(ull != ULONG_MAX || errno == 0, "Failed to parse rounded amount: " << buf); + CHECK_AND_ASSERT_THROW_MES(ull != ULLONG_MAX || errno == 0, "Failed to parse rounded amount: " << buf); CHECK_AND_ASSERT_THROW_MES(ull != 0 || amount == 0, "Overflow in rounding"); return ull; } diff --git a/src/cryptonote_basic/miner.cpp b/src/cryptonote_basic/miner.cpp index 249896ee8..91ee86d60 100644 --- a/src/cryptonote_basic/miner.cpp +++ b/src/cryptonote_basic/miner.cpp @@ -523,7 +523,7 @@ namespace cryptonote bool miner::worker_thread() { const uint32_t th_local_index = m_thread_index++; // atomically increment, getting value before increment - crypto::rx_set_miner_thread(th_local_index, tools::get_max_concurrency()); + bool rx_set = false; MLOG_SET_THREAD_NAME(std::string("[miner ") + std::to_string(th_local_index) + "]"); MGINFO("Miner thread was started ["<< th_local_index << "]"); @@ -575,6 +575,13 @@ namespace cryptonote b.nonce = nonce; crypto::hash h; + + if ((b.major_version >= RX_BLOCK_VERSION) && !rx_set) + { + crypto::rx_set_miner_thread(th_local_index, tools::get_max_concurrency()); + rx_set = true; + } + m_gbh(b, height, NULL, tools::get_max_concurrency(), h); if(check_hash(h, local_diff)) diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index dc20bd1ff..8036c84cd 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -3709,7 +3709,7 @@ uint64_t Blockchain::get_dynamic_base_fee(uint64_t block_reward, size_t median_b div128_64(hi, lo, median_block_weight, &hi, &lo, NULL, NULL); assert(hi == 0); lo -= lo / 20; - return lo; + return lo == 0 ? 1 : lo; } else { diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp index 72ddd0dc9..7b0c9e495 100644 --- a/src/cryptonote_core/cryptonote_core.cpp +++ b/src/cryptonote_core/cryptonote_core.cpp @@ -173,11 +173,6 @@ namespace cryptonote , "Check for new versions of monero: [disabled|notify|download|update]" , "notify" }; - static const command_line::arg_descriptor<bool> arg_fluffy_blocks = { - "fluffy-blocks" - , "Relay blocks as fluffy blocks (obsolete, now default)" - , true - }; static const command_line::arg_descriptor<bool> arg_no_fluffy_blocks = { "no-fluffy-blocks" , "Relay blocks as normal blocks" @@ -340,7 +335,6 @@ namespace cryptonote command_line::add_arg(desc, arg_show_time_stats); command_line::add_arg(desc, arg_block_sync_size); command_line::add_arg(desc, arg_check_updates); - command_line::add_arg(desc, arg_fluffy_blocks); command_line::add_arg(desc, arg_no_fluffy_blocks); command_line::add_arg(desc, arg_test_dbg_lock_sleep); command_line::add_arg(desc, arg_offline); @@ -393,9 +387,6 @@ namespace cryptonote m_offline = get_arg(vm, arg_offline); m_disable_dns_checkpoints = get_arg(vm, arg_disable_dns_checkpoints); - if (!command_line::is_arg_defaulted(vm, arg_fluffy_blocks)) - MWARNING(arg_fluffy_blocks.name << " is obsolete, it is now default"); - if (command_line::get_arg(vm, arg_test_drop_download) == true) test_drop_download(); @@ -576,7 +567,11 @@ namespace cryptonote else if(options[0] == "fastest") { db_flags = DBF_FASTEST; +#ifdef _WIN32 sync_threshold = 1000; // default to fastest:async:1000 +#else + sync_threshold = 100000; // default to fastest:async:100000 +#endif sync_mode = db_sync_mode_is_default ? db_defaultsync : db_async; } else diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.inl b/src/cryptonote_protocol/cryptonote_protocol_handler.inl index 325737789..af667dc0c 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.inl +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.inl @@ -281,6 +281,11 @@ namespace cryptonote cnx.ip = cnx.host; cnx.port = std::to_string(cntxt.m_remote_address.as<epee::net_utils::ipv4_network_address>().port()); } + else if (cntxt.m_remote_address.get_type_id() == epee::net_utils::ipv6_network_address::get_type_id()) + { + cnx.ip = cnx.host; + cnx.port = std::to_string(cntxt.m_remote_address.as<epee::net_utils::ipv6_network_address>().port()); + } cnx.rpc_port = cntxt.m_rpc_port; cnx.rpc_credits_per_hash = cntxt.m_rpc_credits_per_hash; @@ -979,8 +984,18 @@ namespace cryptonote int t_cryptonote_protocol_handler<t_core>::handle_notify_new_transactions(int command, NOTIFY_NEW_TRANSACTIONS::request& arg, cryptonote_connection_context& context) { MLOG_P2P_MESSAGE("Received NOTIFY_NEW_TRANSACTIONS (" << arg.txs.size() << " txes)"); + std::unordered_set<blobdata> seen; for (const auto &blob: arg.txs) + { MLOGIF_P2P_MESSAGE(cryptonote::transaction tx; crypto::hash hash; bool ret = cryptonote::parse_and_validate_tx_from_blob(blob, tx, hash);, ret, "Including transaction " << hash); + if (seen.find(blob) != seen.end()) + { + LOG_PRINT_CCONTEXT_L1("Duplicate transaction in notification, dropping connection"); + drop_connection(context, false, false); + return 1; + } + seen.insert(blob); + } if(context.m_state != cryptonote_connection_context::state_normal) return 1; diff --git a/src/daemon/command_line_args.h b/src/daemon/command_line_args.h index 506d75490..44505eb8f 100644 --- a/src/daemon/command_line_args.h +++ b/src/daemon/command_line_args.h @@ -86,10 +86,6 @@ namespace daemon_args "daemon_command" , "Hidden" }; - const command_line::arg_descriptor<bool> arg_os_version = { - "os-version" - , "OS for which this executable was compiled" - }; const command_line::arg_descriptor<unsigned> arg_max_concurrency = { "max-concurrency" , "Max number of threads to use for a parallel job" diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index 23c313c9d..e2ff27daa 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -143,7 +143,6 @@ int main(int argc, char const * argv[]) command_line::add_arg(visible_options, command_line::arg_help); command_line::add_arg(visible_options, command_line::arg_version); - command_line::add_arg(visible_options, daemon_args::arg_os_version); command_line::add_arg(visible_options, daemon_args::arg_config_file); // Settings @@ -159,6 +158,7 @@ int main(int argc, char const * argv[]) command_line::add_arg(core_settings, daemon_args::arg_zmq_rpc_bind_port); command_line::add_arg(core_settings, daemon_args::arg_zmq_pub); command_line::add_arg(core_settings, daemon_args::arg_zmq_rpc_disabled); + command_line::add_arg(core_settings, daemonizer::arg_non_interactive); daemonizer::init_options(hidden_options, visible_options); daemonize::t_executor::init_options(core_settings); @@ -203,13 +203,6 @@ int main(int argc, char const * argv[]) return 0; } - // OS - if (command_line::get_arg(vm, daemon_args::arg_os_version)) - { - std::cout << "OS: " << tools::get_os_version_string() << ENDL; - return 0; - } - std::string config = command_line::get_arg(vm, daemon_args::arg_config_file); boost::filesystem::path config_path(config); boost::system::error_code ec; diff --git a/src/daemon/rpc_command_executor.cpp b/src/daemon/rpc_command_executor.cpp index 0dcfc9d53..fbf26db85 100644 --- a/src/daemon/rpc_command_executor.cpp +++ b/src/daemon/rpc_command_executor.cpp @@ -644,7 +644,14 @@ bool t_rpc_command_executor::print_connections() { } } - tools::msg_writer() << std::setw(30) << std::left << "Remote Host" + auto longest_host = *std::max_element(res.connections.begin(), res.connections.end(), + [](const auto &info1, const auto &info2) + { + return info1.address.length() < info2.address.length(); + }); + int host_field_width = std::max(15, 8 + (int) longest_host.address.length()); + + tools::msg_writer() << std::setw(host_field_width) << std::left << "Remote Host" << std::setw(8) << "Type" << std::setw(6) << "SSL" << std::setw(20) << "Peer id" @@ -661,11 +668,11 @@ bool t_rpc_command_executor::print_connections() { for (auto & info : res.connections) { std::string address = info.incoming ? "INC " : "OUT "; - address += info.ip + ":" + info.port; + address += info.address; //std::string in_out = info.incoming ? "INC " : "OUT "; tools::msg_writer() //<< std::setw(30) << std::left << in_out - << std::setw(30) << std::left << address + << std::setw(host_field_width) << std::left << address << std::setw(8) << (get_address_type_name((epee::net_utils::address_type)info.address_type)) << std::setw(6) << (info.ssl ? "yes" : "no") << std::setw(20) << info.peer_id diff --git a/src/daemonizer/daemonizer.h b/src/daemonizer/daemonizer.h index d313ff3f1..8c76c0004 100644 --- a/src/daemonizer/daemonizer.h +++ b/src/daemonizer/daemonizer.h @@ -57,6 +57,11 @@ namespace daemonizer , T_executor && executor // universal ref , boost::program_options::variables_map const & vm ); + + const command_line::arg_descriptor<bool> arg_non_interactive = { + "non-interactive" + , "Run non-interactive" + }; } #ifdef WIN32 diff --git a/src/daemonizer/posix_daemonizer.inl b/src/daemonizer/posix_daemonizer.inl index 5defd1e69..82b87cc97 100644 --- a/src/daemonizer/posix_daemonizer.inl +++ b/src/daemonizer/posix_daemonizer.inl @@ -47,10 +47,6 @@ namespace daemonizer "pidfile" , "File path to write the daemon's PID to (optional, requires --detach)" }; - const command_line::arg_descriptor<bool> arg_non_interactive = { - "non-interactive" - , "Run non-interactive" - }; } inline void init_options( @@ -60,7 +56,6 @@ namespace daemonizer { command_line::add_arg(normal_options, arg_detach); command_line::add_arg(normal_options, arg_pidfile); - command_line::add_arg(normal_options, arg_non_interactive); } inline boost::filesystem::path get_default_data_dir() diff --git a/src/daemonizer/windows_daemonizer.inl b/src/daemonizer/windows_daemonizer.inl index d3e5d66dd..dbe8e4a02 100644 --- a/src/daemonizer/windows_daemonizer.inl +++ b/src/daemonizer/windows_daemonizer.inl @@ -61,11 +61,6 @@ namespace daemonizer "run-as-service" , "Hidden -- true if running as windows service" }; - const command_line::arg_descriptor<bool> arg_non_interactive = { - "non-interactive" - , "Run non-interactive" - }; - std::string get_argument_string(int argc, char const * argv[]) { std::string result = ""; @@ -87,7 +82,6 @@ namespace daemonizer command_line::add_arg(normal_options, arg_start_service); command_line::add_arg(normal_options, arg_stop_service); command_line::add_arg(hidden_options, arg_is_service); - command_line::add_arg(hidden_options, arg_non_interactive); } inline boost::filesystem::path get_default_data_dir() diff --git a/src/gen_ssl_cert/gen_ssl_cert.cpp b/src/gen_ssl_cert/gen_ssl_cert.cpp index e695df727..b25d9a73d 100644 --- a/src/gen_ssl_cert/gen_ssl_cert.cpp +++ b/src/gen_ssl_cert/gen_ssl_cert.cpp @@ -65,29 +65,6 @@ namespace const command_line::arg_descriptor<bool> arg_prompt_for_passphrase = {"prompt-for-passphrase", gencert::tr("Prompt for a passphrase with which to encrypt the private key"), false}; } -// adapted from openssl's apps/x509.c -static std::string get_fingerprint(X509 *cert, const EVP_MD *fdig) -{ - unsigned int j; - unsigned int n; - unsigned char md[EVP_MAX_MD_SIZE]; - std::string fingerprint; - - if (!X509_digest(cert, fdig, md, &n)) - { - tools::fail_msg_writer() << tr("Failed to create fingerprint: ") << ERR_reason_error_string(ERR_get_error()); - return fingerprint; - } - fingerprint.resize(n * 3 - 1); - char *out = &fingerprint[0]; - for (j = 0; j < n; ++j) - { - snprintf(out, 3 + (j + 1 < n), "%02X%s", md[j], (j + 1 == n) ? "" : ":"); - out += 3; - } - return fingerprint; -} - int main(int argc, char* argv[]) { TRY_ENTRY(); @@ -246,7 +223,7 @@ int main(int argc, char* argv[]) tools::success_msg_writer() << tr("New certificate created:"); tools::success_msg_writer() << tr("Certificate: ") << certificate_filename; - tools::success_msg_writer() << tr("SHA-256 Fingerprint: ") << get_fingerprint(cert, EVP_sha256()); + tools::success_msg_writer() << tr("SHA-256 Fingerprint: ") << epee::net_utils::get_hr_ssl_fingerprint(cert); tools::success_msg_writer() << tr("Private key: ") << private_key_filename << " (" << (private_key_passphrase.empty() ? "unencrypted" : "encrypted") << ")"; return 0; diff --git a/src/platform/mingw/alloca.h b/src/platform/mingw/alloca.h deleted file mode 100644 index 8ed1c6514..000000000 --- a/src/platform/mingw/alloca.h +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) 2014-2023, The Monero Project -// -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without modification, are -// permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of -// conditions and the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list -// of conditions and the following disclaimer in the documentation and/or other -// materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be -// used to endorse or promote products derived from this software without specific -// prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY -// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL -// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, -// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF -// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers - -#pragma once - -#include <malloc.h> diff --git a/src/platform/msc/alloca.h b/src/platform/msc/alloca.h deleted file mode 100644 index 8c4701b78..000000000 --- a/src/platform/msc/alloca.h +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) 2014-2023, The Monero Project -// -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without modification, are -// permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of -// conditions and the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list -// of conditions and the following disclaimer in the documentation and/or other -// materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be -// used to endorse or promote products derived from this software without specific -// prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY -// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL -// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, -// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF -// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers - -#pragma once - -#define alloca(size) _alloca(size) diff --git a/src/platform/msc/inline_c.h b/src/platform/msc/inline_c.h deleted file mode 100644 index 72bad9223..000000000 --- a/src/platform/msc/inline_c.h +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) 2014-2023, The Monero Project -// -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without modification, are -// permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of -// conditions and the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list -// of conditions and the following disclaimer in the documentation and/or other -// materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be -// used to endorse or promote products derived from this software without specific -// prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY -// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL -// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, -// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF -// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers - -#pragma once - -#ifndef __cplusplus -#define inline __inline -#endif diff --git a/src/platform/msc/sys/param.h b/src/platform/msc/sys/param.h deleted file mode 100644 index 1f1950ac6..000000000 --- a/src/platform/msc/sys/param.h +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2014-2023, The Monero Project -// -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without modification, are -// permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of -// conditions and the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list -// of conditions and the following disclaimer in the documentation and/or other -// materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be -// used to endorse or promote products derived from this software without specific -// prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY -// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL -// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, -// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF -// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers - -#pragma once - -#define LITTLE_ENDIAN 1234 -#define BIG_ENDIAN 4321 -#define PDP_ENDIAN 3412 -#define BYTE_ORDER LITTLE_ENDIAN diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp index cb347110d..0adf0b65e 100644 --- a/src/rpc/core_rpc_server.cpp +++ b/src/rpc/core_rpc_server.cpp @@ -352,6 +352,7 @@ namespace cryptonote const auto ssl_base_path = (boost::filesystem::path{data_dir} / "rpc_ssl").string(); const bool ssl_cert_file_exists = boost::filesystem::exists(ssl_base_path + ".crt"); const bool ssl_pkey_file_exists = boost::filesystem::exists(ssl_base_path + ".key"); + const bool ssl_fp_file_exists = boost::filesystem::exists(ssl_base_path + ".fingerprint"); if (store_ssl_key) { // .key files are often given different read permissions as their corresponding .crt files. @@ -361,13 +362,39 @@ namespace cryptonote MFATAL("Certificate (.crt) and private key (.key) files must both exist or both not exist at path: " << ssl_base_path); return false; } + else if (!ssl_cert_file_exists && ssl_fp_file_exists) // only fingerprint file is present + { + MFATAL("Fingerprint file is present while certificate (.crt) and private key (.key) files are not at path: " << ssl_base_path); + return false; + } else if (ssl_cert_file_exists) { // and ssl_pkey_file_exists // load key from previous run, password prompted by OpenSSL store_ssl_key = false; rpc_config->ssl_options.auth = epee::net_utils::ssl_authentication_t{ssl_base_path + ".key", ssl_base_path + ".crt"}; + + // Since the .fingerprint file was added afterwards, sometimes the other 2 are present, and .fingerprint isn't + // In that case, generate the .fingerprint file from the existing .crt file + if (!ssl_fp_file_exists) + { + try + { + std::string fingerprint = epee::net_utils::get_hr_ssl_fingerprint_from_file(ssl_base_path + ".crt"); + if (!epee::file_io_utils::save_string_to_file(ssl_base_path + ".fingerprint", fingerprint)) + { + MWARNING("Could not save SSL fingerprint to file '" << ssl_base_path << ".fingerprint'"); + } + const auto fp_perms = boost::filesystem::owner_read | boost::filesystem::group_read | boost::filesystem::others_read; + boost::filesystem::permissions(ssl_base_path + ".fingerprint", fp_perms); + } + catch (const std::exception& e) + { + // Do nothing. The fingerprint file is helpful, but not at all necessary. + MWARNING("While trying to store SSL fingerprint file, got error (ignoring): " << e.what()); + } + } } - } + } // if (store_ssl_key) auto rng = [](size_t len, uint8_t *ptr){ return crypto::rand(len, ptr); }; const bool inited = epee::http_server_impl_base<core_rpc_server, connection_context>::init( diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 9031f8aa1..e41a66d24 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -3011,7 +3011,6 @@ bool simple_wallet::scan_tx(const std::vector<std::string> &args) } txids.insert(txid); } - std::vector<crypto::hash> txids_v(txids.begin(), txids.end()); if (!m_wallet->is_trusted_daemon()) { message_writer(console_color_red, true) << tr("WARNING: this operation may reveal the txids to the remote node and affect your privacy"); @@ -3024,7 +3023,9 @@ bool simple_wallet::scan_tx(const std::vector<std::string> &args) LOCK_IDLE_SCOPE(); m_in_manual_refresh.store(true); try { - m_wallet->scan_tx(txids_v); + m_wallet->scan_tx(txids); + } catch (const tools::error::wont_reprocess_recent_txs_via_untrusted_daemon &e) { + fail_msg_writer() << e.what() << ". Either connect to a trusted daemon by passing --trusted-daemon when starting the wallet, or use rescan_bc to rescan the chain."; } catch (const std::exception &e) { fail_msg_writer() << e.what(); } @@ -6583,23 +6584,7 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri } if (!process_ring_members(ptx_vector, prompt, m_wallet->print_ring_members())) return false; - bool default_ring_size = true; - for (const auto &ptx: ptx_vector) - { - for (const auto &vin: ptx.tx.vin) - { - if (vin.type() == typeid(txin_to_key)) - { - const txin_to_key& in_to_key = boost::get<txin_to_key>(vin); - if (in_to_key.key_offsets.size() != min_ring_size) - default_ring_size = false; - } - } - } - if (m_wallet->confirm_non_default_ring_size() && !default_ring_size) - { - prompt << tr("WARNING: this is a non default ring size, which may harm your privacy. Default is recommended."); - } + prompt << ENDL << tr("Is this okay?"); std::string accepted = input_line(prompt.str(), true); diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index 3f192b11b..0c3aaf853 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -1230,11 +1230,15 @@ bool WalletImpl::scanTransactions(const std::vector<std::string> &txids) } txids_u.insert(txid); } - std::vector<crypto::hash> txids_v(txids_u.begin(), txids_u.end()); try { - m_wallet->scan_tx(txids_v); + m_wallet->scan_tx(txids_u); + } + catch (const tools::error::wont_reprocess_recent_txs_via_untrusted_daemon &e) + { + setStatusError(e.what()); + return false; } catch (const std::exception &e) { diff --git a/src/wallet/node_rpc_proxy.cpp b/src/wallet/node_rpc_proxy.cpp index dd0f6eb0c..1142e46ce 100644 --- a/src/wallet/node_rpc_proxy.cpp +++ b/src/wallet/node_rpc_proxy.cpp @@ -328,4 +328,23 @@ boost::optional<std::string> NodeRPCProxy::get_transactions(const std::vector<cr return boost::optional<std::string>(); } +boost::optional<std::string> NodeRPCProxy::get_block_header_by_height(uint64_t height, cryptonote::block_header_response &block_header) +{ + if (m_offline) + return boost::optional<std::string>("offline"); + + cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::request req_t = AUTO_VAL_INIT(req_t); + cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::response resp_t = AUTO_VAL_INIT(resp_t); + req_t.height = height; + + { + const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex}; + bool r = net_utils::invoke_http_json_rpc("/json_rpc", "getblockheaderbyheight", req_t, resp_t, m_http_client, rpc_timeout); + RETURN_ON_RPC_RESPONSE_ERROR(r, epee::json_rpc::error{}, resp_t, "getblockheaderbyheight"); + } + + block_header = std::move(resp_t.block_header); + return boost::optional<std::string>(); +} + } diff --git a/src/wallet/node_rpc_proxy.h b/src/wallet/node_rpc_proxy.h index 3ec96baaa..0dcfd0f83 100644 --- a/src/wallet/node_rpc_proxy.h +++ b/src/wallet/node_rpc_proxy.h @@ -57,6 +57,7 @@ public: boost::optional<std::string> get_dynamic_base_fee_estimate_2021_scaling(uint64_t grace_blocks, std::vector<uint64_t> &fees); boost::optional<std::string> get_fee_quantization_mask(uint64_t &fee_quantization_mask); boost::optional<std::string> get_transactions(const std::vector<crypto::hash> &txids, const std::function<void(const cryptonote::COMMAND_RPC_GET_TRANSACTIONS::request&, const cryptonote::COMMAND_RPC_GET_TRANSACTIONS::response&, bool)> &f); + boost::optional<std::string> get_block_header_by_height(uint64_t height, cryptonote::block_header_response &block_header); private: boost::optional<std::string> get_info(); diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 378fccfab..a8b732017 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -1168,7 +1168,7 @@ wallet2::wallet2(network_type nettype, uint64_t kdf_rounds, bool unattended, std m_first_refresh_done(false), m_refresh_from_block_height(0), m_explicit_refresh_from_block_height(true), - m_confirm_non_default_ring_size(true), + m_skip_to_height(0), m_ask_password(AskPasswordToDecrypt), m_max_reorg_depth(ORPHANED_BLOCKS_MAX_COUNT), m_min_output_count(0), @@ -1609,14 +1609,13 @@ std::string wallet2::get_subaddress_label(const cryptonote::subaddress_index& in return m_subaddress_labels[index.major][index.minor]; } //---------------------------------------------------------------------------------------------------- -void wallet2::scan_tx(const std::vector<crypto::hash> &txids) +wallet2::tx_entry_data wallet2::get_tx_entries(const std::unordered_set<crypto::hash> &txids) { - // Get the transactions from daemon in batches and add them to a priority queue ordered in chronological order - auto cmp_tx_entry = [](const cryptonote::COMMAND_RPC_GET_TRANSACTIONS::entry& l, const cryptonote::COMMAND_RPC_GET_TRANSACTIONS::entry& r) - { return l.block_height > r.block_height; }; + tx_entry_data tx_entries; + tx_entries.tx_entries.reserve(txids.size()); - std::priority_queue<cryptonote::COMMAND_RPC_GET_TRANSACTIONS::entry, std::vector<COMMAND_RPC_GET_TRANSACTIONS::entry>, decltype(cmp_tx_entry)> txq(cmp_tx_entry); const size_t SLICE_SIZE = 100; // RESTRICTED_TRANSACTIONS_COUNT as defined in rpc/core_rpc_server.cpp, hardcoded in daemon code + std::unordered_set<crypto::hash>::const_iterator it = txids.begin(); for(size_t slice = 0; slice < txids.size(); slice += SLICE_SIZE) { cryptonote::COMMAND_RPC_GET_TRANSACTIONS::request req = AUTO_VAL_INIT(req); cryptonote::COMMAND_RPC_GET_TRANSACTIONS::response res = AUTO_VAL_INIT(res); @@ -1625,7 +1624,10 @@ void wallet2::scan_tx(const std::vector<crypto::hash> &txids) size_t ntxes = slice + SLICE_SIZE > txids.size() ? txids.size() - slice : SLICE_SIZE; for (size_t i = slice; i < slice + ntxes; ++i) - req.txs_hashes.push_back(epee::string_tools::pod_to_hex(txids[i])); + { + req.txs_hashes.push_back(epee::string_tools::pod_to_hex(*it)); + ++it; + } { const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex}; @@ -1635,17 +1637,254 @@ void wallet2::scan_tx(const std::vector<crypto::hash> &txids) } for (auto& tx_info : res.txs) - txq.push(tx_info); + { + if (!tx_info.in_pool) + { + tx_entries.lowest_height = std::min(tx_info.block_height, tx_entries.lowest_height); + tx_entries.highest_height = std::max(tx_info.block_height, tx_entries.highest_height); + } + + cryptonote::transaction tx; + crypto::hash tx_hash; + THROW_WALLET_EXCEPTION_IF(!get_pruned_tx(tx_info, tx, tx_hash), error::wallet_internal_error, "Failed to get transaction from daemon"); + tx_entries.tx_entries.emplace_back(process_tx_entry_t{ std::move(tx_info), std::move(tx), std::move(tx_hash) }); + } } - // Process the transactions in chronologically ascending order - while(!txq.empty()) { - auto& tx_info = txq.top(); - cryptonote::transaction tx; - crypto::hash tx_hash; - THROW_WALLET_EXCEPTION_IF(!get_pruned_tx(tx_info, tx, tx_hash), error::wallet_internal_error, "Failed to get transaction from daemon (2)"); - process_new_transaction(tx_hash, tx, tx_info.output_indices, tx_info.block_height, 0, tx_info.block_timestamp, false, tx_info.in_pool, tx_info.double_spend_seen, {}, {}); - txq.pop(); + return tx_entries; +} +//---------------------------------------------------------------------------------------------------- +void wallet2::sort_scan_tx_entries(std::vector<process_tx_entry_t> &unsorted_tx_entries) +{ + // If any txs we're scanning have the same height, then we need to request the + // blocks those txs are in to see what order they appear in the chain. We + // need to scan txs in the same order they appear in the chain so that the + // `m_transfers` container holds entries in a consistently sorted order. + // This ensures that hot wallets <> cold wallets both maintain the same order + // of m_transfers, which they rely on when importing/exporting. Same goes + // for multisig wallets when they synchronize. + std::set<uint64_t> entry_heights; + std::set<uint64_t> entry_heights_requested; + COMMAND_RPC_GET_BLOCKS_BY_HEIGHT::request req; + COMMAND_RPC_GET_BLOCKS_BY_HEIGHT::response res; + for (const auto & tx_info : unsorted_tx_entries) + { + if (!tx_info.tx_entry.in_pool && !cryptonote::is_coinbase(tx_info.tx)) + { + const uint64_t height = tx_info.tx_entry.block_height; + if (entry_heights.find(height) == entry_heights.end()) + { + entry_heights.insert(height); + } + else if (entry_heights_requested.find(height) == entry_heights_requested.end()) + { + req.heights.push_back(height); + entry_heights_requested.insert(height); + } + } + } + + { + const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex}; + bool r = net_utils::invoke_http_bin("/getblocks_by_height.bin", req, res, *m_http_client, rpc_timeout); + THROW_WALLET_EXCEPTION_IF(!r, error::wallet_internal_error, "Failed to get blocks by height from daemon"); + THROW_WALLET_EXCEPTION_IF(res.blocks.size() != req.heights.size(), error::wallet_internal_error, "Failed to get blocks by height from daemon"); + } + + std::unordered_map<uint64_t, cryptonote::block> parsed_blocks; + for (size_t i = 0; i < res.blocks.size(); ++i) + { + const auto &blk = res.blocks[i]; + cryptonote::block parsed_block; + THROW_WALLET_EXCEPTION_IF(!cryptonote::parse_and_validate_block_from_blob(blk.block, parsed_block), + error::wallet_internal_error, "Failed to parse block"); + parsed_blocks[req.heights[i]] = std::move(parsed_block); + } + + // sort tx_entries in chronologically ascending order; pool txs to the back + auto cmp_tx_entry = [&](const process_tx_entry_t& l, const process_tx_entry_t& r) + { + if (l.tx_entry.in_pool) + return false; + else if (r.tx_entry.in_pool) + return true; + else if (l.tx_entry.block_height > r.tx_entry.block_height) + return false; + else if (l.tx_entry.block_height < r.tx_entry.block_height) + return true; + else // l.tx_entry.block_height == r.tx_entry.block_height + { + // coinbase tx is the first tx in a block + if (cryptonote::is_coinbase(l.tx)) + return true; + if (cryptonote::is_coinbase(r.tx)) + return false; + + // see which tx hash comes first in the block + THROW_WALLET_EXCEPTION_IF(parsed_blocks.find(l.tx_entry.block_height) == parsed_blocks.end(), + error::wallet_internal_error, "Expected block not returned by daemon"); + const auto &blk = parsed_blocks[l.tx_entry.block_height]; + for (const auto &tx_hash : blk.tx_hashes) + { + if (tx_hash == l.tx_hash) + return true; + if (tx_hash == r.tx_hash) + return false; + } + THROW_WALLET_EXCEPTION(error::wallet_internal_error, "Tx hashes not found in block"); + return false; + } + }; + std::sort(unsorted_tx_entries.begin(), unsorted_tx_entries.end(), cmp_tx_entry); +} +//---------------------------------------------------------------------------------------------------- +void wallet2::process_scan_txs(const tx_entry_data &txs_to_scan, const tx_entry_data &txs_to_reprocess, const std::unordered_set<crypto::hash> &tx_hashes_to_reprocess, detached_blockchain_data &dbd) +{ + LOG_PRINT_L0("Processing " << txs_to_scan.tx_entries.size() << " txs, re-processing " + << txs_to_reprocess.tx_entries.size() << " txs"); + + // Sort the txs in chronologically ascending order they appear in the chain + std::vector<process_tx_entry_t> process_txs; + process_txs.reserve(txs_to_scan.tx_entries.size() + txs_to_reprocess.tx_entries.size()); + process_txs.insert(process_txs.end(), txs_to_scan.tx_entries.begin(), txs_to_scan.tx_entries.end()); + process_txs.insert(process_txs.end(), txs_to_reprocess.tx_entries.begin(), txs_to_reprocess.tx_entries.end()); + sort_scan_tx_entries(process_txs); + + for (const auto &tx_info : process_txs) + { + const auto &tx_entry = tx_info.tx_entry; + + // Ignore callbacks when re-processing a tx to avoid confusing feedback to user + bool ignore_callbacks = tx_hashes_to_reprocess.find(tx_info.tx_hash) != tx_hashes_to_reprocess.end(); + process_new_transaction( + tx_info.tx_hash, + tx_info.tx, + tx_entry.output_indices, + tx_entry.block_height, + 0, + tx_entry.block_timestamp, + cryptonote::is_coinbase(tx_info.tx), + tx_entry.in_pool, + tx_entry.double_spend_seen, + {}, {}, // unused caches + ignore_callbacks); + + // Re-set destination addresses if they were previously set + if (m_confirmed_txs.find(tx_info.tx_hash) != m_confirmed_txs.end() && + dbd.detached_confirmed_txs_dests.find(tx_info.tx_hash) != dbd.detached_confirmed_txs_dests.end()) + { + m_confirmed_txs[tx_info.tx_hash].m_dests = std::move(dbd.detached_confirmed_txs_dests[tx_info.tx_hash]); + } + } + + LOG_PRINT_L0("Done processing " << txs_to_scan.tx_entries.size() << " txs and re-processing " + << txs_to_reprocess.tx_entries.size() << " txs"); +} +//---------------------------------------------------------------------------------------------------- +void reattach_blockchain(hashchain &blockchain, wallet2::detached_blockchain_data &dbd) +{ + if (!dbd.detached_blockchain.empty()) + { + LOG_PRINT_L0("Re-attaching " << dbd.detached_blockchain.size() << " blocks"); + for (size_t i = 0; i < dbd.detached_blockchain.size(); ++i) + blockchain.push_back(dbd.detached_blockchain[i]); + } + + THROW_WALLET_EXCEPTION_IF(blockchain.size() != dbd.original_chain_size, + error::wallet_internal_error, "Unexpected blockchain size after re-attaching"); +} +//---------------------------------------------------------------------------------------------------- +bool has_nonrequested_tx_at_height_or_above_requested(uint64_t height, const std::unordered_set<crypto::hash> &requested_txids, const wallet2::transfer_container &transfers, + const wallet2::payment_container &payments, const serializable_unordered_map<crypto::hash, wallet2::confirmed_transfer_details> &confirmed_txs) +{ + for (const auto &td : transfers) + if (td.m_block_height >= height && requested_txids.find(td.m_txid) == requested_txids.end()) + return true; + + for (const auto &pmt : payments) + if (pmt.second.m_block_height >= height && requested_txids.find(pmt.second.m_tx_hash) == requested_txids.end()) + return true; + + for (const auto &ct : confirmed_txs) + if (ct.second.m_block_height >= height && requested_txids.find(ct.first) == requested_txids.end()) + return true; + + return false; +} +//---------------------------------------------------------------------------------------------------- +void wallet2::scan_tx(const std::unordered_set<crypto::hash> &txids) +{ + // Get the transactions from daemon in batches sorted lowest height to highest + tx_entry_data txs_to_scan = get_tx_entries(txids); + if (txs_to_scan.tx_entries.empty()) + return; + + // Re-process wallet's txs >= lowest scan_tx height. Re-processing ensures + // process_new_transaction is called with txs in chronological order. Say that + // tx2 spends an output from tx1, and the user calls scan_tx(tx1) *after* tx2 + // has already been scanned. In this case, we will "re-process" tx2 *after* + // processing tx1 to ensure the wallet picks up that tx2 spends the output + // from tx1, and to ensure transfers are placed in the sorted transfers + // container in chronological order. Note: in the above example, if tx2 is + // a sweep to a different wallet's address, the wallet will not be able to + // detect tx2. The wallet would need to scan tx1 first in that case. + // TODO: handle this sweep case + detached_blockchain_data dbd; + dbd.original_chain_size = m_blockchain.size(); + if (m_blockchain.size() > txs_to_scan.lowest_height) + { + // When connected to an untrusted daemon, if we will need to re-process 1+ + // tx that the user did not request to scan, then we fail out because + // re-requesting those unexpected txs from the daemon poses a more severe + // and unintuitive privacy risk to the user + THROW_WALLET_EXCEPTION_IF(!is_trusted_daemon() && + has_nonrequested_tx_at_height_or_above_requested(txs_to_scan.lowest_height, txids, m_transfers, m_payments, m_confirmed_txs), + error::wont_reprocess_recent_txs_via_untrusted_daemon + ); + + LOG_PRINT_L0("Re-processing wallet's existing txs (if any) starting from height " << txs_to_scan.lowest_height); + dbd = detach_blockchain(txs_to_scan.lowest_height); + } + std::unordered_set<crypto::hash> tx_hashes_to_reprocess; + tx_hashes_to_reprocess.reserve(dbd.detached_tx_hashes.size()); + for (const auto &tx_hash : dbd.detached_tx_hashes) + { + if (txids.find(tx_hash) == txids.end()) + tx_hashes_to_reprocess.insert(tx_hash); + } + // re-request txs from daemon to re-process with all tx data needed + tx_entry_data txs_to_reprocess = get_tx_entries(tx_hashes_to_reprocess); + + process_scan_txs(txs_to_scan, txs_to_reprocess, tx_hashes_to_reprocess, dbd); + reattach_blockchain(m_blockchain, dbd); + + // If the highest scan_tx height exceeds the wallet's known scan height, then + // the wallet should skip ahead to the scan_tx's height in order to service + // the request in a timely manner. Skipping unrequested transactions avoids + // generating sequences of calls to process_new_transaction which process + // transactions out-of-order, relative to their order in the blockchain, as + // the process_new_transaction implementation requires transactions to be + // processed in blockchain order. If a user misses a tx, they should either + // use rescan_bc, or manually scan missed txs with scan_tx. + uint64_t skip_to_height = txs_to_scan.highest_height + 1; + if (skip_to_height > m_blockchain.size()) + { + m_skip_to_height = skip_to_height; + LOG_PRINT_L0("Skipping refresh to height " << skip_to_height); + + // update last block reward here because the refresh loop won't necessarily set it + try + { + cryptonote::block_header_response block_header; + if (m_node_rpc_proxy.get_block_header_by_height(txs_to_scan.highest_height, block_header)) + throw std::runtime_error("Failed to request block header by height"); + m_last_block_reward = block_header.reward; + } + catch (...) { MERROR("Failed getting block header at height " << txs_to_scan.highest_height); } + + // TODO: use fast_refresh instead of refresh to update m_blockchain. It needs refactoring to work correctly here. + // Or don't refresh at all, and let it update on the next refresh loop. + refresh(is_trusted_daemon()); } } //---------------------------------------------------------------------------------------------------- @@ -1946,7 +2185,7 @@ bool wallet2::spends_one_of_ours(const cryptonote::transaction &tx) const return false; } //---------------------------------------------------------------------------------------------------- -void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote::transaction& tx, const std::vector<uint64_t> &o_indices, uint64_t height, uint8_t block_version, uint64_t ts, bool miner_tx, bool pool, bool double_spend_seen, const tx_cache_data &tx_cache_data, std::map<std::pair<uint64_t, uint64_t>, size_t> *output_tracker_cache) +void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote::transaction& tx, const std::vector<uint64_t> &o_indices, uint64_t height, uint8_t block_version, uint64_t ts, bool miner_tx, bool pool, bool double_spend_seen, const tx_cache_data &tx_cache_data, std::map<std::pair<uint64_t, uint64_t>, size_t> *output_tracker_cache, bool ignore_callbacks) { PERF_TIMER(process_new_transaction); // In this function, tx (probably) only contains the base information @@ -1988,7 +2227,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote if (pk_index > 1) break; LOG_PRINT_L0("Public key wasn't found in the transaction extra. Skipping transaction " << txid); - if(0 != m_callback) + if(!ignore_callbacks && 0 != m_callback) m_callback->on_skip_transaction(height, txid, tx); break; } @@ -2201,7 +2440,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote update_multisig_rescan_info(*m_multisig_rescan_k, *m_multisig_rescan_info, m_transfers.size() - 1); } LOG_PRINT_L0("Received money: " << print_money(td.amount()) << ", with tx: " << txid); - if (0 != m_callback) + if (!ignore_callbacks && 0 != m_callback) m_callback->on_money_received(height, txid, tx, td.m_amount, 0, td.m_subaddr_index, spends_one_of_ours(tx), td.m_tx.unlock_time); } total_received_1 += amount; @@ -2279,7 +2518,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote THROW_WALLET_EXCEPTION_IF(td.m_spent, error::wallet_internal_error, "Inconsistent spent status"); LOG_PRINT_L0("Received money: " << print_money(td.amount()) << ", with tx: " << txid); - if (0 != m_callback) + if (!ignore_callbacks && 0 != m_callback) m_callback->on_money_received(height, txid, tx, td.m_amount, burnt, td.m_subaddr_index, spends_one_of_ours(tx), td.m_tx.unlock_time); } total_received_1 += extra_amount; @@ -2333,7 +2572,7 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote { LOG_PRINT_L0("Spent money: " << print_money(amount) << ", with tx: " << txid); set_spent(it->second, height); - if (0 != m_callback) + if (!ignore_callbacks && 0 != m_callback) m_callback->on_money_spent(height, txid, tx, amount, tx, td.m_subaddr_index); } } @@ -2568,7 +2807,7 @@ void wallet2::process_outgoing(const crypto::hash &txid, const cryptonote::trans bool wallet2::should_skip_block(const cryptonote::block &b, uint64_t height) const { // seeking only for blocks that are not older then the wallet creation time plus 1 day. 1 day is for possible user incorrect time setup - return !(b.timestamp + 60*60*24 > m_account.get_createtime() && height >= m_refresh_from_block_height); + return !(b.timestamp + 60*60*24 > m_account.get_createtime() && height >= m_refresh_from_block_height && height >= m_skip_to_height); } //---------------------------------------------------------------------------------------------------- void wallet2::process_new_blockchain_entry(const cryptonote::block& b, const cryptonote::block_complete_entry& bche, const parsed_block &parsed_block, const crypto::hash& bl_id, uint64_t height, const std::vector<tx_cache_data> &tx_cache_data, size_t tx_cache_data_offset, std::map<std::pair<uint64_t, uint64_t>, size_t> *output_tracker_cache) @@ -2976,7 +3215,7 @@ void wallet2::process_parsed_blocks(uint64_t start_height, const std::vector<cry tr("reorg exceeds maximum allowed depth, use 'set max-reorg-depth N' to allow it, reorg depth: ") + std::to_string(reorg_depth)); - detach_blockchain(current_index, output_tracker_cache); + handle_reorg(current_index, output_tracker_cache); process_new_blockchain_entry(bl, blocks[i], parsed_blocks[i], bl_id, current_index, tx_cache_data, tx_cache_data_offset, output_tracker_cache); } else @@ -3621,9 +3860,9 @@ void wallet2::refresh(bool trusted_daemon, uint64_t start_height, uint64_t & blo // pull the first set of blocks get_short_chain_history(short_chain_history, (m_first_refresh_done || trusted_daemon) ? 1 : FIRST_REFRESH_GRANULARITY); m_run.store(true, std::memory_order_relaxed); - if (start_height > m_blockchain.size() || m_refresh_from_block_height > m_blockchain.size()) { + if (start_height > m_blockchain.size() || m_refresh_from_block_height > m_blockchain.size() || m_skip_to_height > m_blockchain.size()) { if (!start_height) - start_height = m_refresh_from_block_height; + start_height = std::max(m_refresh_from_block_height, m_skip_to_height);; // we can shortcut by only pulling hashes up to the start_height fast_refresh(start_height, blocks_start_height, short_chain_history); // regenerate the history now that we've got a full set of hashes @@ -3712,6 +3951,7 @@ void wallet2::refresh(bool trusted_daemon, uint64_t start_height, uint64_t & blo catch (const std::exception &e) { MERROR("Error parsing blocks: " << e.what()); + exception = std::current_exception(); error = true; } blocks_fetched += added_blocks; @@ -3776,6 +4016,7 @@ void wallet2::refresh(bool trusted_daemon, uint64_t start_height, uint64_t & blo { LOG_PRINT_L1("Another try pull_blocks (try_count=" << try_count << ")..."); first = true; + last = false; start_height = 0; blocks.clear(); parsed_blocks.clear(); @@ -3863,15 +4104,10 @@ bool wallet2::get_rct_distribution(uint64_t &start_height, std::vector<uint64_t> return true; } //---------------------------------------------------------------------------------------------------- -void wallet2::detach_blockchain(uint64_t height, std::map<std::pair<uint64_t, uint64_t>, size_t> *output_tracker_cache) +wallet2::detached_blockchain_data wallet2::detach_blockchain(uint64_t height, std::map<std::pair<uint64_t, uint64_t>, size_t> *output_tracker_cache) { LOG_PRINT_L0("Detaching blockchain on height " << height); - - // size 1 2 3 4 5 6 7 8 9 - // block 0 1 2 3 4 5 6 7 8 - // C - THROW_WALLET_EXCEPTION_IF(height < m_blockchain.offset() && m_blockchain.size() > m_blockchain.offset(), - error::wallet_internal_error, "Daemon claims reorg below last checkpoint"); + detached_blockchain_data dbd; size_t transfers_detached = 0; @@ -3913,16 +4149,32 @@ void wallet2::detach_blockchain(uint64_t height, std::map<std::pair<uint64_t, ui THROW_WALLET_EXCEPTION_IF(it_pk == m_pub_keys.end(), error::wallet_internal_error, "public key not found"); m_pub_keys.erase(it_pk); } + transfers_detached = std::distance(it, m_transfers.end()); + dbd.detached_tx_hashes.reserve(transfers_detached); + for (size_t i = i_start; i!=m_transfers.size();i++) + dbd.detached_tx_hashes.insert(std::move(m_transfers[i].m_txid)); + MDEBUG(transfers_detached << " transfers detached / expected " << dbd.detached_tx_hashes.size()); m_transfers.erase(it, m_transfers.end()); - const uint64_t blocks_detached = m_blockchain.size() - height; - m_blockchain.crop(height); + uint64_t blocks_detached = 0; + dbd.original_chain_size = m_blockchain.size(); + if (height >= m_blockchain.offset()) + { + for (uint64_t i = height; i < m_blockchain.size(); ++i) + dbd.detached_blockchain.push_back(m_blockchain[i]); + blocks_detached = m_blockchain.size() - height; + m_blockchain.crop(height); + MDEBUG(blocks_detached << " blocks detached / expected " << dbd.detached_blockchain.size()); + } for (auto it = m_payments.begin(); it != m_payments.end(); ) { if(height <= it->second.m_block_height) + { + dbd.detached_tx_hashes.insert(it->second.m_tx_hash); it = m_payments.erase(it); + } else ++it; } @@ -3930,7 +4182,11 @@ void wallet2::detach_blockchain(uint64_t height, std::map<std::pair<uint64_t, ui for (auto it = m_confirmed_txs.begin(); it != m_confirmed_txs.end(); ) { if(height <= it->second.m_block_height) + { + dbd.detached_tx_hashes.insert(it->first); + dbd.detached_confirmed_txs_dests[it->first] = std::move(it->second.m_dests); it = m_confirmed_txs.erase(it); + } else ++it; } @@ -3939,6 +4195,17 @@ void wallet2::detach_blockchain(uint64_t height, std::map<std::pair<uint64_t, ui m_callback->on_reorg(height, blocks_detached, transfers_detached); LOG_PRINT_L0("Detached blockchain on height " << height << ", transfers detached " << transfers_detached << ", blocks detached " << blocks_detached); + return dbd; +} +//---------------------------------------------------------------------------------------------------- +void wallet2::handle_reorg(uint64_t height, std::map<std::pair<uint64_t, uint64_t>, size_t> *output_tracker_cache) +{ + // size 1 2 3 4 5 6 7 8 9 + // block 0 1 2 3 4 5 6 7 8 + // C + THROW_WALLET_EXCEPTION_IF(height < m_blockchain.offset() && m_blockchain.size() > m_blockchain.offset(), + error::wallet_internal_error, "Daemon claims reorg below last checkpoint"); + detach_blockchain(height, output_tracker_cache); } //---------------------------------------------------------------------------------------------------- bool wallet2::deinit() @@ -3971,6 +4238,7 @@ bool wallet2::clear() m_multisig_rounds_passed = 0; m_device_last_key_image_sync = 0; m_pool_info_query_time = 0; + m_skip_to_height = 0; return true; } //---------------------------------------------------------------------------------------------------- @@ -3988,6 +4256,7 @@ void wallet2::clear_soft(bool keep_key_images) m_scanned_pool_txs[0].clear(); m_scanned_pool_txs[1].clear(); m_pool_info_query_time = 0; + m_skip_to_height = 0; cryptonote::block b; generate_genesis(b); @@ -4117,7 +4386,10 @@ boost::optional<wallet2::keys_file_data> wallet2::get_keys_file_data(const epee: value2.SetUint64(m_refresh_from_block_height); json.AddMember("refresh_height", value2, json.GetAllocator()); - value2.SetInt(m_confirm_non_default_ring_size ? 1 :0); + value2.SetUint64(m_skip_to_height); + json.AddMember("skip_to_height", value2, json.GetAllocator()); + + value2.SetInt(1); // exists for deserialization backwards compatibility json.AddMember("confirm_non_default_ring_size", value2, json.GetAllocator()); value2.SetInt(m_ask_password); @@ -4349,7 +4621,7 @@ bool wallet2::load_keys_buf(const std::string& keys_buf, const epee::wipeable_st m_auto_refresh = true; m_refresh_type = RefreshType::RefreshDefault; m_refresh_from_block_height = 0; - m_confirm_non_default_ring_size = true; + m_skip_to_height = 0; m_ask_password = AskPasswordToDecrypt; cryptonote::set_default_decimal_point(CRYPTONOTE_DISPLAY_DECIMAL_POINT); m_max_reorg_depth = ORPHANED_BLOCKS_MAX_COUNT; @@ -4499,8 +4771,8 @@ bool wallet2::load_keys_buf(const std::string& keys_buf, const epee::wipeable_st } GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, refresh_height, uint64_t, Uint64, false, 0); m_refresh_from_block_height = field_refresh_height; - GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, confirm_non_default_ring_size, int, Int, false, true); - m_confirm_non_default_ring_size = field_confirm_non_default_ring_size; + GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, skip_to_height, uint64_t, Uint64, false, 0); + m_skip_to_height = field_skip_to_height; GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, ask_password, AskPasswordType, Int, false, AskPasswordToDecrypt); m_ask_password = field_ask_password; GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, default_decimal_point, int, Int, false, CRYPTONOTE_DISPLAY_DECIMAL_POINT); @@ -5874,23 +6146,16 @@ void wallet2::trim_hashchain() if (!m_blockchain.empty() && m_blockchain.size() == m_blockchain.offset()) { MINFO("Fixing empty hashchain"); - cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::request req = AUTO_VAL_INIT(req); - cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::response res = AUTO_VAL_INIT(res); - - bool r; - { - const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex}; - req.height = m_blockchain.size() - 1; - r = net_utils::invoke_http_json_rpc("/json_rpc", "getblockheaderbyheight", req, res, *m_http_client, rpc_timeout); - } - - if (r && res.status == CORE_RPC_STATUS_OK) + try { + cryptonote::block_header_response block_header; + if (m_node_rpc_proxy.get_block_header_by_height(m_blockchain.size() - 1, block_header)) + throw std::runtime_error("Failed to request block header by height"); crypto::hash hash; - epee::string_tools::hex_to_pod(res.block_header.hash, hash); + epee::string_tools::hex_to_pod(block_header.hash, hash); m_blockchain.refill(hash); } - else + catch(...) { MERROR("Failed to request block header from daemon, hash chain may be unable to sync till the wallet is loaded with a usable daemon"); } @@ -13337,7 +13602,7 @@ size_t wallet2::import_multisig(std::vector<cryptonote::blobdata> blobs) if (!td.m_key_image_partial) continue; MINFO("Multisig info importing from block height " << td.m_block_height); - detach_blockchain(td.m_block_height); + handle_reorg(td.m_block_height); break; } diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 5e922494b..e57163a73 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -810,6 +810,30 @@ private: bool empty() const { return tx_extra_fields.empty() && primary.empty() && additional.empty(); } }; + struct detached_blockchain_data + { + hashchain detached_blockchain; + size_t original_chain_size; + std::unordered_set<crypto::hash> detached_tx_hashes; + std::unordered_map<crypto::hash, std::vector<cryptonote::tx_destination_entry>> detached_confirmed_txs_dests; + }; + + struct process_tx_entry_t + { + cryptonote::COMMAND_RPC_GET_TRANSACTIONS::entry tx_entry; + cryptonote::transaction tx; + crypto::hash tx_hash; + }; + + struct tx_entry_data + { + std::vector<process_tx_entry_t> tx_entries; + uint64_t lowest_height; + uint64_t highest_height; + + tx_entry_data(): lowest_height((uint64_t)-1), highest_height(0) {} + }; + /*! * \brief Generates a wallet or restores one. Assumes the multisig setup * has already completed for the provided multisig info. @@ -1318,8 +1342,6 @@ private: void segregation_height(uint64_t height) { m_segregation_height = height; } bool ignore_fractional_outputs() const { return m_ignore_fractional_outputs; } void ignore_fractional_outputs(bool value) { m_ignore_fractional_outputs = value; } - bool confirm_non_default_ring_size() const { return m_confirm_non_default_ring_size; } - void confirm_non_default_ring_size(bool always) { m_confirm_non_default_ring_size = always; } uint64_t ignore_outputs_above() const { return m_ignore_outputs_above; } void ignore_outputs_above(uint64_t value) { m_ignore_outputs_above = value; } uint64_t ignore_outputs_below() const { return m_ignore_outputs_below; } @@ -1360,7 +1382,7 @@ private: std::string get_spend_proof(const crypto::hash &txid, const std::string &message); bool check_spend_proof(const crypto::hash &txid, const std::string &message, const std::string &sig_str); - void scan_tx(const std::vector<crypto::hash> &txids); + void scan_tx(const std::unordered_set<crypto::hash> &txids); /*! * \brief Generates a proof that proves the reserve of unspent funds @@ -1644,10 +1666,11 @@ private: */ bool load_keys_buf(const std::string& keys_buf, const epee::wipeable_string& password); bool load_keys_buf(const std::string& keys_buf, const epee::wipeable_string& password, boost::optional<crypto::chacha_key>& keys_to_encrypt); - void process_new_transaction(const crypto::hash &txid, const cryptonote::transaction& tx, const std::vector<uint64_t> &o_indices, uint64_t height, uint8_t block_version, uint64_t ts, bool miner_tx, bool pool, bool double_spend_seen, const tx_cache_data &tx_cache_data, std::map<std::pair<uint64_t, uint64_t>, size_t> *output_tracker_cache = NULL); + void process_new_transaction(const crypto::hash &txid, const cryptonote::transaction& tx, const std::vector<uint64_t> &o_indices, uint64_t height, uint8_t block_version, uint64_t ts, bool miner_tx, bool pool, bool double_spend_seen, const tx_cache_data &tx_cache_data, std::map<std::pair<uint64_t, uint64_t>, size_t> *output_tracker_cache = NULL, bool ignore_callbacks = false); bool should_skip_block(const cryptonote::block &b, uint64_t height) const; void process_new_blockchain_entry(const cryptonote::block& b, const cryptonote::block_complete_entry& bche, const parsed_block &parsed_block, const crypto::hash& bl_id, uint64_t height, const std::vector<tx_cache_data> &tx_cache_data, size_t tx_cache_data_offset, std::map<std::pair<uint64_t, uint64_t>, size_t> *output_tracker_cache = NULL); - void detach_blockchain(uint64_t height, std::map<std::pair<uint64_t, uint64_t>, size_t> *output_tracker_cache = NULL); + detached_blockchain_data detach_blockchain(uint64_t height, std::map<std::pair<uint64_t, uint64_t>, size_t> *output_tracker_cache = NULL); + void handle_reorg(uint64_t height, std::map<std::pair<uint64_t, uint64_t>, size_t> *output_tracker_cache = NULL); void get_short_chain_history(std::list<crypto::hash>& ids, uint64_t granularity = 1) const; bool clear(); void clear_soft(bool keep_key_images=false); @@ -1703,6 +1726,9 @@ private: crypto::chacha_key get_ringdb_key(); void setup_keys(const epee::wipeable_string &password); size_t get_transfer_details(const crypto::key_image &ki) const; + tx_entry_data get_tx_entries(const std::unordered_set<crypto::hash> &txids); + void sort_scan_tx_entries(std::vector<process_tx_entry_t> &unsorted_tx_entries); + void process_scan_txs(const tx_entry_data &txs_to_scan, const tx_entry_data &txs_to_reprocess, const std::unordered_set<crypto::hash> &tx_hashes_to_reprocess, detached_blockchain_data &dbd); void register_devices(); hw::device& lookup_device(const std::string & device_descriptor); @@ -1793,6 +1819,9 @@ private: // m_refresh_from_block_height was defaulted to zero.*/ bool m_explicit_refresh_from_block_height; uint64_t m_pool_info_query_time; + uint64_t m_skip_to_height; + // m_skip_to_height is useful when we don't want to modify the wallet's restore height. + // m_refresh_from_block_height is also a wallet's restore height which should remain constant unless explicitly modified by the user. bool m_confirm_non_default_ring_size; AskPasswordType m_ask_password; uint64_t m_max_reorg_depth; diff --git a/src/wallet/wallet_errors.h b/src/wallet/wallet_errors.h index a2b248434..6706e77ff 100644 --- a/src/wallet/wallet_errors.h +++ b/src/wallet/wallet_errors.h @@ -93,6 +93,8 @@ namespace tools // get_output_distribution // deprecated_rpc_access // wallet_files_doesnt_correspond + // scan_tx_error * + // wont_reprocess_recent_txs_via_untrusted_daemon // // * - class with protected ctor @@ -916,6 +918,23 @@ namespace tools } }; //---------------------------------------------------------------------------------------------------- + struct scan_tx_error : public wallet_logic_error + { + protected: + explicit scan_tx_error(std::string&& loc, const std::string& message) + : wallet_logic_error(std::move(loc), message) + { + } + }; + //---------------------------------------------------------------------------------------------------- + struct wont_reprocess_recent_txs_via_untrusted_daemon : public scan_tx_error + { + explicit wont_reprocess_recent_txs_via_untrusted_daemon(std::string&& loc) + : scan_tx_error(std::move(loc), "The wallet has already seen 1 or more recent transactions than the scanned tx") + { + } + }; + //---------------------------------------------------------------------------------------------------- #if !defined(_MSC_VER) diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp index 81e26c120..7c46d9887 100644 --- a/src/wallet/wallet_rpc_server.cpp +++ b/src/wallet/wallet_rpc_server.cpp @@ -3173,7 +3173,7 @@ namespace tools return false; } - std::vector<crypto::hash> txids; + std::unordered_set<crypto::hash> txids; std::list<std::string>::const_iterator i = req.txids.begin(); while (i != req.txids.end()) { @@ -3186,11 +3186,15 @@ namespace tools } crypto::hash txid = *reinterpret_cast<const crypto::hash*>(txid_blob.data()); - txids.push_back(txid); + txids.insert(txid); } try { m_wallet->scan_tx(txids); + } catch (const tools::error::wont_reprocess_recent_txs_via_untrusted_daemon &e) { + er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR; + er.message = e.what() + std::string(". Either connect to a trusted daemon or rescan the chain."); + return false; } catch (const std::exception &e) { handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR); return false; @@ -4694,6 +4698,7 @@ int main(int argc, char** argv) { command_line::add_arg(desc_params, arg_wallet_dir); command_line::add_arg(desc_params, arg_prompt_for_password); command_line::add_arg(desc_params, arg_no_initial_sync); + command_line::add_arg(hidden_options, daemonizer::arg_non_interactive); daemonizer::init_options(hidden_options, desc_params); desc_params.add(hidden_options); diff --git a/src/wallet/wallet_rpc_server_commands_defs.h b/src/wallet/wallet_rpc_server_commands_defs.h index 72719e982..002db4289 100644 --- a/src/wallet/wallet_rpc_server_commands_defs.h +++ b/src/wallet/wallet_rpc_server_commands_defs.h @@ -530,6 +530,33 @@ namespace wallet_rpc END_KV_SERIALIZE_MAP() }; + struct single_transfer_response + { + std::string tx_hash; + std::string tx_key; + uint64_t amount; + uint64_t fee; + uint64_t weight; + std::string tx_blob; + std::string tx_metadata; + std::string multisig_txset; + std::string unsigned_txset; + key_image_list spent_key_images; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(tx_hash) + KV_SERIALIZE(tx_key) + KV_SERIALIZE(amount) + KV_SERIALIZE(fee) + KV_SERIALIZE(weight) + KV_SERIALIZE(tx_blob) + KV_SERIALIZE(tx_metadata) + KV_SERIALIZE(multisig_txset) + KV_SERIALIZE(unsigned_txset) + KV_SERIALIZE(spent_key_images) + END_KV_SERIALIZE_MAP() + }; + struct COMMAND_RPC_TRANSFER { struct request_t @@ -562,35 +589,37 @@ namespace wallet_rpc }; typedef epee::misc_utils::struct_init<request_t> request; - struct response_t - { - std::string tx_hash; - std::string tx_key; - uint64_t amount; - uint64_t fee; - uint64_t weight; - std::string tx_blob; - std::string tx_metadata; - std::string multisig_txset; - std::string unsigned_txset; - key_image_list spent_key_images; - - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(tx_hash) - KV_SERIALIZE(tx_key) - KV_SERIALIZE(amount) - KV_SERIALIZE(fee) - KV_SERIALIZE(weight) - KV_SERIALIZE(tx_blob) - KV_SERIALIZE(tx_metadata) - KV_SERIALIZE(multisig_txset) - KV_SERIALIZE(unsigned_txset) - KV_SERIALIZE(spent_key_images) - END_KV_SERIALIZE_MAP() - }; + typedef single_transfer_response response_t; typedef epee::misc_utils::struct_init<response_t> response; }; + struct split_transfer_response + { + std::list<std::string> tx_hash_list; + std::list<std::string> tx_key_list; + std::list<uint64_t> amount_list; + std::list<uint64_t> fee_list; + std::list<uint64_t> weight_list; + std::list<std::string> tx_blob_list; + std::list<std::string> tx_metadata_list; + std::string multisig_txset; + std::string unsigned_txset; + std::list<key_image_list> spent_key_images_list; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(tx_hash_list) + KV_SERIALIZE(tx_key_list) + KV_SERIALIZE(amount_list) + KV_SERIALIZE(fee_list) + KV_SERIALIZE(weight_list) + KV_SERIALIZE(tx_blob_list) + KV_SERIALIZE(tx_metadata_list) + KV_SERIALIZE(multisig_txset) + KV_SERIALIZE(unsigned_txset) + KV_SERIALIZE(spent_key_images_list) + END_KV_SERIALIZE_MAP() + }; + struct COMMAND_RPC_TRANSFER_SPLIT { struct request_t @@ -623,41 +652,7 @@ namespace wallet_rpc }; typedef epee::misc_utils::struct_init<request_t> request; - struct key_list - { - std::list<std::string> keys; - - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(keys) - END_KV_SERIALIZE_MAP() - }; - - struct response_t - { - std::list<std::string> tx_hash_list; - std::list<std::string> tx_key_list; - std::list<uint64_t> amount_list; - std::list<uint64_t> fee_list; - std::list<uint64_t> weight_list; - std::list<std::string> tx_blob_list; - std::list<std::string> tx_metadata_list; - std::string multisig_txset; - std::string unsigned_txset; - std::list<key_image_list> spent_key_images_list; - - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(tx_hash_list) - KV_SERIALIZE(tx_key_list) - KV_SERIALIZE(amount_list) - KV_SERIALIZE(fee_list) - KV_SERIALIZE(weight_list) - KV_SERIALIZE(tx_blob_list) - KV_SERIALIZE(tx_metadata_list) - KV_SERIALIZE(multisig_txset) - KV_SERIALIZE(unsigned_txset) - KV_SERIALIZE(spent_key_images_list) - END_KV_SERIALIZE_MAP() - }; + typedef split_transfer_response response_t; typedef epee::misc_utils::struct_init<response_t> response; }; @@ -821,41 +816,7 @@ namespace wallet_rpc }; typedef epee::misc_utils::struct_init<request_t> request; - struct key_list - { - std::list<std::string> keys; - - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(keys) - END_KV_SERIALIZE_MAP() - }; - - struct response_t - { - std::list<std::string> tx_hash_list; - std::list<std::string> tx_key_list; - std::list<uint64_t> amount_list; - std::list<uint64_t> fee_list; - std::list<uint64_t> weight_list; - std::list<std::string> tx_blob_list; - std::list<std::string> tx_metadata_list; - std::string multisig_txset; - std::string unsigned_txset; - std::list<key_image_list> spent_key_images_list; - - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(tx_hash_list) - KV_SERIALIZE(tx_key_list) - KV_SERIALIZE(amount_list) - KV_SERIALIZE(fee_list) - KV_SERIALIZE(weight_list) - KV_SERIALIZE(tx_blob_list) - KV_SERIALIZE(tx_metadata_list) - KV_SERIALIZE(multisig_txset) - KV_SERIALIZE(unsigned_txset) - KV_SERIALIZE(spent_key_images_list) - END_KV_SERIALIZE_MAP() - }; + typedef split_transfer_response response_t; typedef epee::misc_utils::struct_init<response_t> response; }; @@ -897,41 +858,7 @@ namespace wallet_rpc }; typedef epee::misc_utils::struct_init<request_t> request; - struct key_list - { - std::list<std::string> keys; - - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(keys) - END_KV_SERIALIZE_MAP() - }; - - struct response_t - { - std::list<std::string> tx_hash_list; - std::list<std::string> tx_key_list; - std::list<uint64_t> amount_list; - std::list<uint64_t> fee_list; - std::list<uint64_t> weight_list; - std::list<std::string> tx_blob_list; - std::list<std::string> tx_metadata_list; - std::string multisig_txset; - std::string unsigned_txset; - std::list<key_image_list> spent_key_images_list; - - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(tx_hash_list) - KV_SERIALIZE(tx_key_list) - KV_SERIALIZE(amount_list) - KV_SERIALIZE(fee_list) - KV_SERIALIZE(weight_list) - KV_SERIALIZE(tx_blob_list) - KV_SERIALIZE(tx_metadata_list) - KV_SERIALIZE(multisig_txset) - KV_SERIALIZE(unsigned_txset) - KV_SERIALIZE(spent_key_images_list) - END_KV_SERIALIZE_MAP() - }; + typedef split_transfer_response response_t; typedef epee::misc_utils::struct_init<response_t> response; }; @@ -967,32 +894,7 @@ namespace wallet_rpc }; typedef epee::misc_utils::struct_init<request_t> request; - struct response_t - { - std::string tx_hash; - std::string tx_key; - uint64_t amount; - uint64_t fee; - uint64_t weight; - std::string tx_blob; - std::string tx_metadata; - std::string multisig_txset; - std::string unsigned_txset; - key_image_list spent_key_images; - - BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(tx_hash) - KV_SERIALIZE(tx_key) - KV_SERIALIZE(amount) - KV_SERIALIZE(fee) - KV_SERIALIZE(weight) - KV_SERIALIZE(tx_blob) - KV_SERIALIZE(tx_metadata) - KV_SERIALIZE(multisig_txset) - KV_SERIALIZE(unsigned_txset) - KV_SERIALIZE(spent_key_images) - END_KV_SERIALIZE_MAP() - }; + typedef single_transfer_response response_t; typedef epee::misc_utils::struct_init<response_t> response; }; |