aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.github/workflows/build.yml15
-rw-r--r--contrib/depends/packages/openssl.mk2
-rw-r--r--contrib/depends/packages/qt.mk2
-rw-r--r--contrib/epee/include/net/abstract_http_client.h87
-rw-r--r--contrib/epee/include/net/http_client.h176
-rw-r--r--contrib/epee/include/storages/http_abstract_invoke.h10
-rw-r--r--contrib/epee/include/syncobj.h2
-rw-r--r--contrib/epee/src/CMakeLists.txt2
-rw-r--r--contrib/epee/src/abstract_http_client.cpp142
-rw-r--r--contrib/epee/src/hex.cpp6
-rw-r--r--src/blockchain_db/blockchain_db.cpp26
-rw-r--r--src/blockchain_db/blockchain_db.h27
-rw-r--r--src/blockchain_db/lmdb/db_lmdb.cpp98
-rw-r--r--src/blockchain_db/lmdb/db_lmdb.h1
-rw-r--r--src/blockchain_db/testdb.h1
-rw-r--r--src/common/util.cpp114
-rw-r--r--src/common/util.h3
-rw-r--r--src/crypto/duration.h70
-rw-r--r--src/cryptonote_basic/verification_context.h7
-rw-r--r--src/cryptonote_config.h7
-rw-r--r--src/cryptonote_core/blockchain.cpp32
-rw-r--r--src/cryptonote_core/cryptonote_core.cpp5
-rw-r--r--src/cryptonote_core/tx_pool.cpp138
-rw-r--r--src/cryptonote_protocol/cryptonote_protocol_defs.h2
-rw-r--r--src/cryptonote_protocol/cryptonote_protocol_handler.h10
-rw-r--r--src/cryptonote_protocol/cryptonote_protocol_handler.inl145
-rw-r--r--src/cryptonote_protocol/cryptonote_protocol_handler_common.h4
-rw-r--r--src/cryptonote_protocol/enums.h5
-rw-r--r--src/cryptonote_protocol/levin_notify.cpp210
-rw-r--r--src/cryptonote_protocol/levin_notify.h3
-rw-r--r--src/daemon/rpc_command_executor.cpp4
-rw-r--r--src/p2p/net_node.h2
-rw-r--r--src/p2p/net_node.inl12
-rw-r--r--src/p2p/net_node_common.h4
-rw-r--r--src/rpc/core_rpc_server.cpp10
-rw-r--r--src/rpc/daemon_handler.cpp10
-rw-r--r--src/rpc/daemon_handler.h2
-rw-r--r--src/simplewallet/simplewallet.cpp34
-rw-r--r--src/wallet/api/wallet2_api.h6
-rw-r--r--src/wallet/api/wallet_manager.cpp20
-rw-r--r--src/wallet/message_store.cpp2
-rw-r--r--src/wallet/message_store.h4
-rw-r--r--src/wallet/message_transporter.cpp8
-rw-r--r--src/wallet/message_transporter.h6
-rw-r--r--src/wallet/node_rpc_proxy.cpp2
-rw-r--r--src/wallet/node_rpc_proxy.h6
-rw-r--r--src/wallet/wallet2.cpp354
-rw-r--r--src/wallet/wallet2.h41
-rw-r--r--src/wallet/wallet_rpc_payments.cpp2
-rw-r--r--tests/core_tests/chaingen.h7
-rw-r--r--tests/core_tests/chaingen_main.cpp2
-rw-r--r--tests/core_tests/tx_pool.cpp81
-rw-r--r--tests/core_tests/tx_pool.h20
-rw-r--r--tests/fuzz/http-client.cpp5
-rw-r--r--tests/unit_tests/epee_utils.cpp3
-rw-r--r--tests/unit_tests/levin.cpp1101
56 files changed, 2545 insertions, 555 deletions
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 9e1aade2d..3e4ee7dce 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -36,6 +36,11 @@ jobs:
submodules: recursive
- name: remove bundled boost
run: sudo rm -rf /usr/local/share/boost
+ - name: set apt conf
+ run: |
+ echo "Acquire::Retries \"3\";" | sudo tee -a /etc/apt/apt.conf.d/80-custom
+ echo "Acquire::http::Timeout \"120\";" | sudo tee -a /etc/apt/apt.conf.d/80-custom
+ echo "Acquire::ftp::Timeout \"120\";" | sudo tee -a /etc/apt/apt.conf.d/80-custom
- name: update apt
run: sudo apt update
- name: install monero dependencies
@@ -51,6 +56,11 @@ jobs:
submodules: recursive
- name: remove bundled boost
run: sudo rm -rf /usr/local/share/boost
+ - name: set apt conf
+ run: |
+ echo "Acquire::Retries \"3\";" | sudo tee -a /etc/apt/apt.conf.d/80-custom
+ echo "Acquire::http::Timeout \"120\";" | sudo tee -a /etc/apt/apt.conf.d/80-custom
+ echo "Acquire::ftp::Timeout \"120\";" | sudo tee -a /etc/apt/apt.conf.d/80-custom
- name: update apt
run: sudo apt update
- name: install monero dependencies
@@ -67,6 +77,11 @@ jobs:
submodules: recursive
- name: remove bundled boost
run: sudo rm -rf /usr/local/share/boost
+ - name: set apt conf
+ run: |
+ echo "Acquire::Retries \"3\";" | sudo tee -a /etc/apt/apt.conf.d/80-custom
+ echo "Acquire::http::Timeout \"120\";" | sudo tee -a /etc/apt/apt.conf.d/80-custom
+ echo "Acquire::ftp::Timeout \"120\";" | sudo tee -a /etc/apt/apt.conf.d/80-custom
- name: update apt
run: sudo apt update
- name: install monero dependencies
diff --git a/contrib/depends/packages/openssl.mk b/contrib/depends/packages/openssl.mk
index 4b07d08ba..9d3c28465 100644
--- a/contrib/depends/packages/openssl.mk
+++ b/contrib/depends/packages/openssl.mk
@@ -1,6 +1,6 @@
package=openssl
$(package)_version=1.0.2r
-$(package)_download_path=https://www.openssl.org/source
+$(package)_download_path=https://ftp.openssl.org/source/old/1.0.2
$(package)_file_name=$(package)-$($(package)_version).tar.gz
$(package)_sha256_hash=ae51d08bba8a83958e894946f15303ff894d75c2b8bbd44a852b64e3fe11d0d6
$(package)_patches=fix_arflags.patch
diff --git a/contrib/depends/packages/qt.mk b/contrib/depends/packages/qt.mk
index 6a6f0e0f7..98cef4631 100644
--- a/contrib/depends/packages/qt.mk
+++ b/contrib/depends/packages/qt.mk
@@ -1,6 +1,6 @@
PACKAGE=qt
$(package)_version=5.7.1
-$(package)_download_path=https://download.qt.io/archive/qt/5.7/5.7.1/submodules
+$(package)_download_path=http://linorg.usp.br/Qt/archive/qt/5.7/5.7.1/submodules
$(package)_suffix=opensource-src-$($(package)_version).tar.gz
$(package)_file_name=qtbase-$($(package)_suffix)
$(package)_sha256_hash=95f83e532d23b3ddbde7973f380ecae1bac13230340557276f75f2e37984e410
diff --git a/contrib/epee/include/net/abstract_http_client.h b/contrib/epee/include/net/abstract_http_client.h
new file mode 100644
index 000000000..787ae2667
--- /dev/null
+++ b/contrib/epee/include/net/abstract_http_client.h
@@ -0,0 +1,87 @@
+// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * 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.
+// * Neither the name of the Andrey N. Sabelnikov 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 OWNER 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.
+
+#pragma once
+
+#include <string>
+#include <boost/optional/optional.hpp>
+#include "http_auth.h"
+#include "net/net_ssl.h"
+
+namespace epee
+{
+namespace net_utils
+{
+ inline const char* get_hex_vals()
+ {
+ static constexpr const char hexVals[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
+ return hexVals;
+ }
+
+ inline const char* get_unsave_chars()
+ {
+ //static constexpr char unsave_chars[] = "\"<>%\\^[]`+$,@:;/!#?=&";
+ static constexpr const char unsave_chars[] = "\"<>%\\^[]`+$,@:;!#&";
+ return unsave_chars;
+ }
+
+ bool is_unsafe(unsigned char compare_char);
+ std::string dec_to_hex(char num, int radix);
+ int get_index(const char *s, char c);
+ std::string hex_to_dec_2bytes(const char *s);
+ std::string convert(char val);
+ std::string conver_to_url_format(const std::string& uri);
+ std::string convert_from_url_format(const std::string& uri);
+ std::string convert_to_url_format_force_all(const std::string& uri);
+
+namespace http
+{
+ class abstract_http_client
+ {
+ public:
+ abstract_http_client() {}
+ virtual ~abstract_http_client() {}
+ bool set_server(const std::string& address, boost::optional<login> user, ssl_options_t ssl_options = ssl_support_t::e_ssl_support_autodetect);
+ virtual void set_server(std::string host, std::string port, boost::optional<login> user, ssl_options_t ssl_options = ssl_support_t::e_ssl_support_autodetect) = 0;
+ virtual void set_auto_connect(bool auto_connect) = 0;
+ virtual bool connect(std::chrono::milliseconds timeout) = 0;
+ virtual bool disconnect() = 0;
+ virtual bool is_connected(bool *ssl = NULL) = 0;
+ virtual bool invoke(const boost::string_ref uri, const boost::string_ref method, const std::string& body, std::chrono::milliseconds timeout, const http_response_info** ppresponse_info = NULL, const fields_list& additional_params = fields_list()) = 0;
+ virtual bool invoke_get(const boost::string_ref uri, std::chrono::milliseconds timeout, const std::string& body = std::string(), const http_response_info** ppresponse_info = NULL, const fields_list& additional_params = fields_list()) = 0;
+ virtual bool invoke_post(const boost::string_ref uri, const std::string& body, std::chrono::milliseconds timeout, const http_response_info** ppresponse_info = NULL, const fields_list& additional_params = fields_list()) = 0;
+ virtual uint64_t get_bytes_sent() const = 0;
+ virtual uint64_t get_bytes_received() const = 0;
+ };
+
+ class http_client_factory
+ {
+ public:
+ virtual ~http_client_factory() {}
+ virtual std::unique_ptr<abstract_http_client> create() = 0;
+ };
+}
+}
+}
diff --git a/contrib/epee/include/net/http_client.h b/contrib/epee/include/net/http_client.h
index 588d5f0e3..d329b8cf2 100644
--- a/contrib/epee/include/net/http_client.h
+++ b/contrib/epee/include/net/http_client.h
@@ -47,6 +47,7 @@
#include "string_tools.h"
#include "reg_exp_definer.h"
+#include "abstract_http_client.h"
#include "http_base.h"
#include "http_auth.h"
#include "to_nonconst_iterator.h"
@@ -105,140 +106,11 @@ namespace net_utils
//---------------------------------------------------------------------------
- static inline const char* get_hex_vals()
- {
- static const char hexVals[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
- return hexVals;
- }
-
- static inline const char* get_unsave_chars()
- {
- //static char unsave_chars[] = "\"<>%\\^[]`+$,@:;/!#?=&";
- static const char unsave_chars[] = "\"<>%\\^[]`+$,@:;!#&";
- return unsave_chars;
- }
-
- static inline bool is_unsafe(unsigned char compare_char)
- {
- if(compare_char <= 32 || compare_char >= 123)
- return true;
-
- const char* punsave = get_unsave_chars();
-
- for(int ichar_pos = 0; 0!=punsave[ichar_pos] ;ichar_pos++)
- if(compare_char == punsave[ichar_pos])
- return true;
-
- return false;
- }
-
- static inline
- std::string dec_to_hex(char num, int radix)
- {
- int temp=0;
- std::string csTmp;
- int num_char;
-
- num_char = (int) num;
- if (num_char < 0)
- num_char = 256 + num_char;
-
- while (num_char >= radix)
- {
- temp = num_char % radix;
- num_char = (int)floor((float)num_char / (float)radix);
- csTmp = get_hex_vals()[temp];
- }
-
- csTmp += get_hex_vals()[num_char];
-
- if(csTmp.size() < 2)
- {
- csTmp += '0';
- }
-
- std::reverse(csTmp.begin(), csTmp.end());
- //_mbsrev((unsigned char*)csTmp.data());
-
- return csTmp;
- }
- static inline int get_index(const char *s, char c) { const char *ptr = (const char*)memchr(s, c, 16); return ptr ? ptr-s : -1; }
- static inline
- std::string hex_to_dec_2bytes(const char *s)
- {
- const char *hex = get_hex_vals();
- int i0 = get_index(hex, toupper(s[0]));
- int i1 = get_index(hex, toupper(s[1]));
- if (i0 < 0 || i1 < 0)
- return std::string("%") + std::string(1, s[0]) + std::string(1, s[1]);
- return std::string(1, i0 * 16 | i1);
- }
-
- static inline std::string convert(char val)
- {
- std::string csRet;
- csRet += "%";
- csRet += dec_to_hex(val, 16);
- return csRet;
- }
- static inline std::string conver_to_url_format(const std::string& uri)
- {
-
- std::string result;
-
- for(size_t i = 0; i!= uri.size(); i++)
- {
- if(is_unsafe(uri[i]))
- result += convert(uri[i]);
- else
- result += uri[i];
-
- }
-
- return result;
- }
- static inline std::string convert_from_url_format(const std::string& uri)
- {
-
- std::string result;
-
- for(size_t i = 0; i!= uri.size(); i++)
- {
- if(uri[i] == '%' && i + 2 < uri.size())
- {
- result += hex_to_dec_2bytes(uri.c_str() + i + 1);
- i += 2;
- }
- else
- result += uri[i];
-
- }
-
- return result;
- }
-
- static inline std::string convert_to_url_format_force_all(const std::string& uri)
- {
-
- std::string result;
-
- for(size_t i = 0; i!= uri.size(); i++)
- {
- result += convert(uri[i]);
- }
-
- return result;
- }
-
-
-
-
-
namespace http
{
template<typename net_client_type>
- class http_simple_client_template: public i_target_handler
+ class http_simple_client_template : public i_target_handler, public abstract_http_client
{
private:
enum reciev_machine_state
@@ -279,7 +151,7 @@ namespace net_utils
public:
explicit http_simple_client_template()
- : i_target_handler()
+ : i_target_handler(), abstract_http_client()
, m_net_client()
, m_host_buff()
, m_port()
@@ -299,26 +171,19 @@ namespace net_utils
const std::string &get_host() const { return m_host_buff; };
const std::string &get_port() const { return m_port; };
- bool set_server(const std::string& address, boost::optional<login> user, ssl_options_t ssl_options = ssl_support_t::e_ssl_support_autodetect)
- {
- http::url_content parsed{};
- const bool r = parse_url(address, parsed);
- CHECK_AND_ASSERT_MES(r, false, "failed to parse url: " << address);
- set_server(std::move(parsed.host), std::to_string(parsed.port), std::move(user), std::move(ssl_options));
- return true;
- }
+ using abstract_http_client::set_server;
- void set_server(std::string host, std::string port, boost::optional<login> user, ssl_options_t ssl_options = ssl_support_t::e_ssl_support_autodetect)
+ void set_server(std::string host, std::string port, boost::optional<login> user, ssl_options_t ssl_options = ssl_support_t::e_ssl_support_autodetect) override
{
CRITICAL_REGION_LOCAL(m_lock);
disconnect();
m_host_buff = std::move(host);
m_port = std::move(port);
- m_auth = user ? http_client_auth{std::move(*user)} : http_client_auth{};
+ m_auth = user ? http_client_auth{std::move(*user)} : http_client_auth{};
m_net_client.set_ssl(std::move(ssl_options));
}
- void set_auto_connect(bool auto_connect)
+ void set_auto_connect(bool auto_connect) override
{
m_auto_connect = auto_connect;
}
@@ -330,25 +195,25 @@ namespace net_utils
m_net_client.set_connector(std::move(connector));
}
- bool connect(std::chrono::milliseconds timeout)
+ bool connect(std::chrono::milliseconds timeout) override
{
CRITICAL_REGION_LOCAL(m_lock);
return m_net_client.connect(m_host_buff, m_port, timeout);
}
//---------------------------------------------------------------------------
- bool disconnect()
+ bool disconnect() override
{
CRITICAL_REGION_LOCAL(m_lock);
return m_net_client.disconnect();
}
//---------------------------------------------------------------------------
- bool is_connected(bool *ssl = NULL)
+ bool is_connected(bool *ssl = NULL) override
{
CRITICAL_REGION_LOCAL(m_lock);
return m_net_client.is_connected(ssl);
}
//---------------------------------------------------------------------------
- virtual bool handle_target_data(std::string& piece_of_transfer)
+ virtual bool handle_target_data(std::string& piece_of_transfer) override
{
CRITICAL_REGION_LOCAL(m_lock);
m_response_info.m_body += piece_of_transfer;
@@ -361,15 +226,14 @@ namespace net_utils
return true;
}
//---------------------------------------------------------------------------
- inline
- bool invoke_get(const boost::string_ref uri, std::chrono::milliseconds timeout, const std::string& body = std::string(), const http_response_info** ppresponse_info = NULL, const fields_list& additional_params = fields_list())
+ inline bool invoke_get(const boost::string_ref uri, std::chrono::milliseconds timeout, const std::string& body = std::string(), const http_response_info** ppresponse_info = NULL, const fields_list& additional_params = fields_list()) override
{
CRITICAL_REGION_LOCAL(m_lock);
return invoke(uri, "GET", body, timeout, ppresponse_info, additional_params);
}
//---------------------------------------------------------------------------
- inline bool invoke(const boost::string_ref uri, const boost::string_ref method, const std::string& body, std::chrono::milliseconds timeout, const http_response_info** ppresponse_info = NULL, const fields_list& additional_params = fields_list())
+ inline bool invoke(const boost::string_ref uri, const boost::string_ref method, const std::string& body, std::chrono::milliseconds timeout, const http_response_info** ppresponse_info = NULL, const fields_list& additional_params = fields_list()) override
{
CRITICAL_REGION_LOCAL(m_lock);
if(!is_connected())
@@ -442,7 +306,7 @@ namespace net_utils
return false;
}
//---------------------------------------------------------------------------
- inline bool invoke_post(const boost::string_ref uri, const std::string& body, std::chrono::milliseconds timeout, const http_response_info** ppresponse_info = NULL, const fields_list& additional_params = fields_list())
+ inline bool invoke_post(const boost::string_ref uri, const std::string& body, std::chrono::milliseconds timeout, const http_response_info** ppresponse_info = NULL, const fields_list& additional_params = fields_list()) override
{
CRITICAL_REGION_LOCAL(m_lock);
return invoke(uri, "POST", body, timeout, ppresponse_info, additional_params);
@@ -456,12 +320,12 @@ namespace net_utils
return handle_reciev(timeout);
}
//---------------------------------------------------------------------------
- uint64_t get_bytes_sent() const
+ uint64_t get_bytes_sent() const override
{
return m_net_client.get_bytes_sent();
}
//---------------------------------------------------------------------------
- uint64_t get_bytes_received() const
+ uint64_t get_bytes_received() const override
{
return m_net_client.get_bytes_received();
}
@@ -1016,6 +880,14 @@ namespace net_utils
}
};
typedef http_simple_client_template<blocked_mode_client> http_simple_client;
+
+ class http_simple_client_factory : public http_client_factory
+ {
+ public:
+ std::unique_ptr<abstract_http_client> create() override {
+ return std::unique_ptr<epee::net_utils::http::abstract_http_client>(new epee::net_utils::http::http_simple_client());
+ }
+ };
}
}
}
diff --git a/contrib/epee/include/storages/http_abstract_invoke.h b/contrib/epee/include/storages/http_abstract_invoke.h
index 78e02c93a..a8bc945a8 100644
--- a/contrib/epee/include/storages/http_abstract_invoke.h
+++ b/contrib/epee/include/storages/http_abstract_invoke.h
@@ -38,7 +38,7 @@ namespace epee
namespace net_utils
{
template<class t_request, class t_response, class t_transport>
- bool invoke_http_json(const boost::string_ref uri, const t_request& out_struct, t_response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref method = "GET")
+ bool invoke_http_json(const boost::string_ref uri, const t_request& out_struct, t_response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref method = "POST")
{
std::string req_param;
if(!serialization::store_t_to_json(out_struct, req_param))
@@ -72,7 +72,7 @@ namespace epee
template<class t_request, class t_response, class t_transport>
- bool invoke_http_bin(const boost::string_ref uri, const t_request& out_struct, t_response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref method = "GET")
+ bool invoke_http_bin(const boost::string_ref uri, const t_request& out_struct, t_response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref method = "POST")
{
std::string req_param;
if(!serialization::store_t_to_binary(out_struct, req_param))
@@ -101,7 +101,7 @@ namespace epee
}
template<class t_request, class t_response, class t_transport>
- bool invoke_http_json_rpc(const boost::string_ref uri, std::string method_name, const t_request& out_struct, t_response& result_struct, epee::json_rpc::error &error_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "GET", const std::string& req_id = "0")
+ bool invoke_http_json_rpc(const boost::string_ref uri, std::string method_name, const t_request& out_struct, t_response& result_struct, epee::json_rpc::error &error_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "POST", const std::string& req_id = "0")
{
epee::json_rpc::request<t_request> req_t = AUTO_VAL_INIT(req_t);
req_t.jsonrpc = "2.0";
@@ -125,14 +125,14 @@ namespace epee
}
template<class t_request, class t_response, class t_transport>
- bool invoke_http_json_rpc(const boost::string_ref uri, std::string method_name, const t_request& out_struct, t_response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "GET", const std::string& req_id = "0")
+ bool invoke_http_json_rpc(const boost::string_ref uri, std::string method_name, const t_request& out_struct, t_response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "POST", const std::string& req_id = "0")
{
epee::json_rpc::error error_struct;
return invoke_http_json_rpc(uri, method_name, out_struct, result_struct, error_struct, transport, timeout, http_method, req_id);
}
template<class t_command, class t_transport>
- bool invoke_http_json_rpc(const boost::string_ref uri, typename t_command::request& out_struct, typename t_command::response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "GET", const std::string& req_id = "0")
+ bool invoke_http_json_rpc(const boost::string_ref uri, typename t_command::request& out_struct, typename t_command::response& result_struct, t_transport& transport, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "POST", const std::string& req_id = "0")
{
return invoke_http_json_rpc(uri, t_command::methodname(), out_struct, result_struct, transport, timeout, http_method, req_id);
}
diff --git a/contrib/epee/include/syncobj.h b/contrib/epee/include/syncobj.h
index dba02f270..804bafda7 100644
--- a/contrib/epee/include/syncobj.h
+++ b/contrib/epee/include/syncobj.h
@@ -150,7 +150,7 @@ namespace epee
};
-#define CRITICAL_REGION_LOCAL(x) {boost::this_thread::sleep_for(boost::chrono::milliseconds(epee::debug::g_test_dbg_lock_sleep()));} epee::critical_region_t<decltype(x)> critical_region_var(x)
+#define CRITICAL_REGION_LOCAL(x) {} epee::critical_region_t<decltype(x)> critical_region_var(x)
#define CRITICAL_REGION_BEGIN(x) { boost::this_thread::sleep_for(boost::chrono::milliseconds(epee::debug::g_test_dbg_lock_sleep())); epee::critical_region_t<decltype(x)> critical_region_var(x)
#define CRITICAL_REGION_LOCAL1(x) {boost::this_thread::sleep_for(boost::chrono::milliseconds(epee::debug::g_test_dbg_lock_sleep()));} epee::critical_region_t<decltype(x)> critical_region_var1(x)
#define CRITICAL_REGION_BEGIN1(x) { boost::this_thread::sleep_for(boost::chrono::milliseconds(epee::debug::g_test_dbg_lock_sleep())); epee::critical_region_t<decltype(x)> critical_region_var1(x)
diff --git a/contrib/epee/src/CMakeLists.txt b/contrib/epee/src/CMakeLists.txt
index 5201f59c2..88018d71a 100644
--- a/contrib/epee/src/CMakeLists.txt
+++ b/contrib/epee/src/CMakeLists.txt
@@ -26,7 +26,7 @@
# 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.
-add_library(epee STATIC byte_slice.cpp hex.cpp http_auth.cpp mlog.cpp net_helper.cpp net_utils_base.cpp string_tools.cpp wipeable_string.cpp
+add_library(epee STATIC byte_slice.cpp hex.cpp abstract_http_client.cpp http_auth.cpp mlog.cpp net_helper.cpp net_utils_base.cpp string_tools.cpp wipeable_string.cpp
levin_base.cpp memwipe.c connection_basic.cpp network_throttle.cpp network_throttle-detail.cpp mlocker.cpp buffer.cpp net_ssl.cpp
int-util.cpp)
diff --git a/contrib/epee/src/abstract_http_client.cpp b/contrib/epee/src/abstract_http_client.cpp
new file mode 100644
index 000000000..98b5b67d9
--- /dev/null
+++ b/contrib/epee/src/abstract_http_client.cpp
@@ -0,0 +1,142 @@
+#include "net/abstract_http_client.h"
+#include "net/http_base.h"
+#include "net/net_parse_helpers.h"
+
+#undef MONERO_DEFAULT_LOG_CATEGORY
+#define MONERO_DEFAULT_LOG_CATEGORY "net.http"
+
+namespace epee
+{
+namespace net_utils
+{
+ //----------------------------------------------------------------------------------------------------
+ bool is_unsafe(unsigned char compare_char)
+ {
+ if(compare_char <= 32 || compare_char >= 123)
+ return true;
+
+ const char* punsave = get_unsave_chars();
+
+ for(int ichar_pos = 0; 0!=punsave[ichar_pos] ;ichar_pos++)
+ if(compare_char == punsave[ichar_pos])
+ return true;
+
+ return false;
+ }
+ //----------------------------------------------------------------------------------------------------
+ std::string dec_to_hex(char num, int radix)
+ {
+ int temp=0;
+ std::string csTmp;
+ int num_char;
+
+ num_char = (int) num;
+ if (num_char < 0)
+ num_char = 256 + num_char;
+
+ while (num_char >= radix)
+ {
+ temp = num_char % radix;
+ num_char = (int)floor((float)num_char / (float)radix);
+ csTmp = get_hex_vals()[temp];
+ }
+
+ csTmp += get_hex_vals()[num_char];
+
+ if(csTmp.size() < 2)
+ {
+ csTmp += '0';
+ }
+
+ std::reverse(csTmp.begin(), csTmp.end());
+ //_mbsrev((unsigned char*)csTmp.data());
+
+ return csTmp;
+ }
+ //----------------------------------------------------------------------------------------------------
+ int get_index(const char *s, char c)
+ {
+ const char *ptr = (const char*)memchr(s, c, 16);
+ return ptr ? ptr-s : -1;
+ }
+ //----------------------------------------------------------------------------------------------------
+ std::string hex_to_dec_2bytes(const char *s)
+ {
+ const char *hex = get_hex_vals();
+ int i0 = get_index(hex, toupper(s[0]));
+ int i1 = get_index(hex, toupper(s[1]));
+ if (i0 < 0 || i1 < 0)
+ return std::string("%") + std::string(1, s[0]) + std::string(1, s[1]);
+ return std::string(1, i0 * 16 | i1);
+ }
+ //----------------------------------------------------------------------------------------------------
+ std::string convert(char val)
+ {
+ std::string csRet;
+ csRet += "%";
+ csRet += dec_to_hex(val, 16);
+ return csRet;
+ }
+ //----------------------------------------------------------------------------------------------------
+ std::string conver_to_url_format(const std::string& uri)
+ {
+
+ std::string result;
+
+ for(size_t i = 0; i!= uri.size(); i++)
+ {
+ if(is_unsafe(uri[i]))
+ result += convert(uri[i]);
+ else
+ result += uri[i];
+
+ }
+
+ return result;
+ }
+ //----------------------------------------------------------------------------------------------------
+ std::string convert_from_url_format(const std::string& uri)
+ {
+
+ std::string result;
+
+ for(size_t i = 0; i!= uri.size(); i++)
+ {
+ if(uri[i] == '%' && i + 2 < uri.size())
+ {
+ result += hex_to_dec_2bytes(uri.c_str() + i + 1);
+ i += 2;
+ }
+ else
+ result += uri[i];
+
+ }
+
+ return result;
+ }
+ //----------------------------------------------------------------------------------------------------
+ std::string convert_to_url_format_force_all(const std::string& uri)
+ {
+ std::string result;
+
+ for(size_t i = 0; i!= uri.size(); i++)
+ {
+ result += convert(uri[i]);
+ }
+ return result;
+ }
+
+namespace http
+{
+ //----------------------------------------------------------------------------------------------------
+ bool epee::net_utils::http::abstract_http_client::set_server(const std::string& address, boost::optional<login> user, ssl_options_t ssl_options)
+ {
+ http::url_content parsed{};
+ const bool r = parse_url(address, parsed);
+ CHECK_AND_ASSERT_MES(r, false, "failed to parse url: " << address);
+ set_server(std::move(parsed.host), std::to_string(parsed.port), std::move(user), std::move(ssl_options));
+ return true;
+ }
+}
+}
+}
diff --git a/contrib/epee/src/hex.cpp b/contrib/epee/src/hex.cpp
index b25ce5421..b53efe6b8 100644
--- a/contrib/epee/src/hex.cpp
+++ b/contrib/epee/src/hex.cpp
@@ -141,7 +141,7 @@ namespace epee
result.reserve(count / 2);
// the data to work with (std::string is always null-terminated)
- auto data = src.data();
+ auto data = src.begin();
// convert a single hex character to an unsigned integer
auto char_to_int = [](const char *input) {
@@ -167,9 +167,9 @@ namespace epee
};
// keep going until we reach the end
- while (data[0] != '\0') {
+ while (data != src.end()) {
// skip unwanted characters
- if (!include(data[0])) {
+ if (!include(*data)) {
++data;
continue;
}
diff --git a/src/blockchain_db/blockchain_db.cpp b/src/blockchain_db/blockchain_db.cpp
index 1a6a19da5..01faf43c4 100644
--- a/src/blockchain_db/blockchain_db.cpp
+++ b/src/blockchain_db/blockchain_db.cpp
@@ -53,9 +53,7 @@ bool matches_category(relay_method method, relay_category category) noexcept
case relay_category::all:
return true;
case relay_category::relayable:
- if (method == relay_method::none)
- return false;
- return true;
+ return method != relay_method::none;
case relay_category::broadcasted:
case relay_category::legacy:
break;
@@ -65,6 +63,7 @@ bool matches_category(relay_method method, relay_category category) noexcept
{
default:
case relay_method::local:
+ case relay_method::stem:
return false;
case relay_method::block:
case relay_method::fluff:
@@ -80,6 +79,7 @@ void txpool_tx_meta_t::set_relay_method(relay_method method) noexcept
kept_by_block = 0;
do_not_relay = 0;
is_local = 0;
+ dandelionpp_stem = 0;
switch (method)
{
@@ -92,6 +92,9 @@ void txpool_tx_meta_t::set_relay_method(relay_method method) noexcept
default:
case relay_method::fluff:
break;
+ case relay_method::stem:
+ dandelionpp_stem = 1;
+ break;
case relay_method::block:
kept_by_block = 1;
break;
@@ -106,9 +109,26 @@ relay_method txpool_tx_meta_t::get_relay_method() const noexcept
return relay_method::none;
if (is_local)
return relay_method::local;
+ if (dandelionpp_stem)
+ return relay_method::stem;
return relay_method::fluff;
}
+bool txpool_tx_meta_t::upgrade_relay_method(relay_method method) noexcept
+{
+ static_assert(relay_method::none < relay_method::local, "bad relay_method value");
+ static_assert(relay_method::local < relay_method::stem, "bad relay_method value");
+ static_assert(relay_method::stem < relay_method::fluff, "bad relay_method value");
+ static_assert(relay_method::fluff < relay_method::block, "bad relay_method value");
+
+ if (get_relay_method() < method)
+ {
+ set_relay_method(method);
+ return true;
+ }
+ return false;
+}
+
const command_line::arg_descriptor<std::string> arg_db_sync_mode = {
"db-sync-mode"
, "Specify sync option, using format [safe|fast|fastest]:[sync|async]:[<nblocks_per_sync>[blocks]|<nbytes_per_sync>[bytes]]."
diff --git a/src/blockchain_db/blockchain_db.h b/src/blockchain_db/blockchain_db.h
index e9fc85803..3e2387da4 100644
--- a/src/blockchain_db/blockchain_db.h
+++ b/src/blockchain_db/blockchain_db.h
@@ -160,7 +160,7 @@ struct txpool_tx_meta_t
uint64_t max_used_block_height;
uint64_t last_failed_height;
uint64_t receive_time;
- uint64_t last_relayed_time;
+ uint64_t last_relayed_time; //!< If Dandelion++ stem, randomized embargo timestamp. Otherwise, last relayed timestmap.
// 112 bytes
uint8_t kept_by_block;
uint8_t relayed;
@@ -168,13 +168,17 @@ struct txpool_tx_meta_t
uint8_t double_spend_seen: 1;
uint8_t pruned: 1;
uint8_t is_local: 1;
- uint8_t bf_padding: 5;
+ uint8_t dandelionpp_stem : 1;
+ uint8_t bf_padding: 4;
uint8_t padding[76]; // till 192 bytes
void set_relay_method(relay_method method) noexcept;
relay_method get_relay_method() const noexcept;
+ //! \return True if `get_relay_method()` now returns `method`.
+ bool upgrade_relay_method(relay_method method) noexcept;
+
//! See `relay_category` description
bool matches(const relay_category category) const noexcept
{
@@ -1291,6 +1295,25 @@ public:
virtual bool get_pruned_tx_blobs_from(const crypto::hash& h, size_t count, std::vector<cryptonote::blobdata> &bd) const = 0;
/**
+ * @brief fetches a variable number of blocks and transactions from the given height, in canonical blockchain order
+ *
+ * The subclass should return the blocks and transactions stored from the one with the given
+ * height. The number of blocks returned is variable, based on the max_size passed.
+ *
+ * @param start_height the height of the first block
+ * @param min_count the minimum number of blocks to return, if they exist
+ * @param max_count the maximum number of blocks to return
+ * @param max_size the maximum size of block/transaction data to return (will be exceeded by one blocks's worth at most, if min_count is met)
+ * @param blocks the returned block/transaction data
+ * @param pruned whether to return full or pruned tx data
+ * @param skip_coinbase whether to return or skip coinbase transactions (they're in blocks regardless)
+ * @param get_miner_tx_hash whether to calculate and return the miner (coinbase) tx hash
+ *
+ * @return true iff the blocks and transactions were found
+ */
+ virtual bool get_blocks_from(uint64_t start_height, size_t min_count, size_t max_count, size_t max_size, std::vector<std::pair<std::pair<cryptonote::blobdata, crypto::hash>, std::vector<std::pair<crypto::hash, cryptonote::blobdata>>>>& blocks, bool pruned, bool skip_coinbase, bool get_miner_tx_hash) const = 0;
+
+ /**
* @brief fetches the prunable transaction blob with the given hash
*
* The subclass should return the prunable transaction stored which has the given
diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp
index 1103899d5..5cec8879d 100644
--- a/src/blockchain_db/lmdb/db_lmdb.cpp
+++ b/src/blockchain_db/lmdb/db_lmdb.cpp
@@ -3108,6 +3108,104 @@ bool BlockchainLMDB::get_pruned_tx_blobs_from(const crypto::hash& h, size_t coun
return true;
}
+bool BlockchainLMDB::get_blocks_from(uint64_t start_height, size_t min_count, size_t max_count, size_t max_size, std::vector<std::pair<std::pair<cryptonote::blobdata, crypto::hash>, std::vector<std::pair<crypto::hash, cryptonote::blobdata>>>>& blocks, bool pruned, bool skip_coinbase, bool get_miner_tx_hash) const
+{
+ LOG_PRINT_L3("BlockchainLMDB::" << __func__);
+ check_open();
+
+ TXN_PREFIX_RDONLY();
+ RCURSOR(blocks);
+ RCURSOR(tx_indices);
+ RCURSOR(txs_pruned);
+ if (!pruned)
+ {
+ RCURSOR(txs_prunable);
+ }
+
+ blocks.reserve(std::min<size_t>(max_count, 10000)); // guard against very large max count if only checking bytes
+ const uint64_t blockchain_height = height();
+ uint64_t size = 0;
+ MDB_val_copy<uint64_t> key(start_height);
+ MDB_val k, v, val_tx_id;
+ uint64_t tx_id = ~0;
+ MDB_cursor_op op = MDB_SET;
+ for (uint64_t h = start_height; h < blockchain_height && blocks.size() < max_count && (size < max_size || blocks.size() < min_count); ++h)
+ {
+ MDB_cursor_op op = h == start_height ? MDB_SET : MDB_NEXT;
+ int result = mdb_cursor_get(m_cur_blocks, &key, &v, op);
+ if (result == MDB_NOTFOUND)
+ throw0(BLOCK_DNE(std::string("Attempt to get block from height ").append(boost::lexical_cast<std::string>(h)).append(" failed -- block not in db").c_str()));
+ else if (result)
+ throw0(DB_ERROR(lmdb_error("Error attempting to retrieve a block from the db", result).c_str()));
+
+ blocks.resize(blocks.size() + 1);
+ auto &current_block = blocks.back();
+
+ current_block.first.first.assign(reinterpret_cast<char*>(v.mv_data), v.mv_size);
+ size += v.mv_size;
+
+ cryptonote::block b;
+ if (!parse_and_validate_block_from_blob(current_block.first.first, b))
+ throw0(DB_ERROR("Invalid block"));
+ current_block.first.second = get_miner_tx_hash ? cryptonote::get_transaction_hash(b.miner_tx) : crypto::null_hash;
+
+ // get the tx_id for the first tx (the first block's coinbase tx)
+ if (h == start_height)
+ {
+ crypto::hash hash = cryptonote::get_transaction_hash(b.miner_tx);
+ MDB_val_set(v, hash);
+ result = mdb_cursor_get(m_cur_tx_indices, (MDB_val *)&zerokval, &v, MDB_GET_BOTH);
+ if (result)
+ throw0(DB_ERROR(lmdb_error("Error attempting to retrieve block coinbase transaction from the db: ", result).c_str()));
+
+ const txindex *tip = (const txindex *)v.mv_data;
+ tx_id = tip->data.tx_id;
+ val_tx_id.mv_data = &tx_id;
+ val_tx_id.mv_size = sizeof(tx_id);
+ }
+
+ if (skip_coinbase)
+ {
+ result = mdb_cursor_get(m_cur_txs_pruned, &val_tx_id, &v, op);
+ if (result)
+ throw0(DB_ERROR(lmdb_error("Error attempting to retrieve transaction data from the db: ", result).c_str()));
+ if (!pruned)
+ {
+ result = mdb_cursor_get(m_cur_txs_prunable, &val_tx_id, &v, op);
+ if (result)
+ throw0(DB_ERROR(lmdb_error("Error attempting to retrieve transaction data from the db: ", result).c_str()));
+ }
+ }
+
+ op = MDB_NEXT;
+
+ current_block.second.reserve(b.tx_hashes.size());
+ for (const auto &tx_hash: b.tx_hashes)
+ {
+ // get pruned data
+ cryptonote::blobdata tx_blob;
+ result = mdb_cursor_get(m_cur_txs_pruned, &val_tx_id, &v, op);
+ if (result)
+ throw0(DB_ERROR(lmdb_error("Error attempting to retrieve transaction data from the db: ", result).c_str()));
+ tx_blob.assign((const char*)v.mv_data, v.mv_size);
+
+ if (!pruned)
+ {
+ result = mdb_cursor_get(m_cur_txs_prunable, &val_tx_id, &v, op);
+ if (result)
+ throw0(DB_ERROR(lmdb_error("Error attempting to retrieve transaction data from the db: ", result).c_str()));
+ tx_blob.append(reinterpret_cast<const char*>(v.mv_data), v.mv_size);
+ }
+ current_block.second.push_back(std::make_pair(tx_hash, std::move(tx_blob)));
+ size += current_block.second.back().second.size();
+ }
+ }
+
+ TXN_POSTFIX_RDONLY();
+
+ return true;
+}
+
bool BlockchainLMDB::get_prunable_tx_blob(const crypto::hash& h, cryptonote::blobdata &bd) const
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
diff --git a/src/blockchain_db/lmdb/db_lmdb.h b/src/blockchain_db/lmdb/db_lmdb.h
index 7c0b4c72c..6ddeed671 100644
--- a/src/blockchain_db/lmdb/db_lmdb.h
+++ b/src/blockchain_db/lmdb/db_lmdb.h
@@ -255,6 +255,7 @@ public:
virtual bool get_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const;
virtual bool get_pruned_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const;
virtual bool get_pruned_tx_blobs_from(const crypto::hash& h, size_t count, std::vector<cryptonote::blobdata> &bd) const;
+ virtual bool get_blocks_from(uint64_t start_height, size_t min_count, size_t max_count, size_t max_size, std::vector<std::pair<std::pair<cryptonote::blobdata, crypto::hash>, std::vector<std::pair<crypto::hash, cryptonote::blobdata>>>>& blocks, bool pruned, bool skip_coinbase, bool get_miner_tx_hash) const;
virtual bool get_prunable_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const;
virtual bool get_prunable_tx_hash(const crypto::hash& tx_hash, crypto::hash &prunable_hash) const;
diff --git a/src/blockchain_db/testdb.h b/src/blockchain_db/testdb.h
index 46de38c7e..638dd3b37 100644
--- a/src/blockchain_db/testdb.h
+++ b/src/blockchain_db/testdb.h
@@ -70,6 +70,7 @@ public:
virtual bool get_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const override { return false; }
virtual bool get_pruned_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const override { return false; }
virtual bool get_pruned_tx_blobs_from(const crypto::hash& h, size_t count, std::vector<cryptonote::blobdata> &bd) const { return false; }
+ virtual bool get_blocks_from(uint64_t start_height, size_t min_count, size_t max_count, size_t max_size, std::vector<std::pair<std::pair<cryptonote::blobdata, crypto::hash>, std::vector<std::pair<crypto::hash, cryptonote::blobdata>>>>& blocks, bool pruned, bool skip_coinbase, bool get_miner_tx_hash) const { return false; }
virtual bool get_prunable_tx_blob(const crypto::hash& h, cryptonote::blobdata &tx) const override { return false; }
virtual bool get_prunable_tx_hash(const crypto::hash& tx_hash, crypto::hash &prunable_hash) const override { return false; }
virtual uint64_t get_block_height(const crypto::hash& h) const override { return 0; }
diff --git a/src/common/util.cpp b/src/common/util.cpp
index 747235646..f1140d1d5 100644
--- a/src/common/util.cpp
+++ b/src/common/util.cpp
@@ -66,7 +66,6 @@ using namespace epee;
#include "util.h"
#include "stack_trace.h"
#include "memwipe.h"
-#include "cryptonote_config.h"
#include "net/http_client.h" // epee::net_utils::...
#include "readline_buffer.h"
@@ -1074,16 +1073,33 @@ std::string get_nix_version_display_string()
{
if (seconds < 60)
return std::to_string(seconds) + " seconds";
+ std::stringstream ss;
+ ss << std::fixed << std::setprecision(1);
if (seconds < 3600)
- return std::to_string((uint64_t)(seconds / 60)) + " minutes";
+ {
+ ss << seconds / 60.f;
+ return ss.str() + " minutes";
+ }
if (seconds < 3600 * 24)
- return std::to_string((uint64_t)(seconds / 3600)) + " hours";
- if (seconds < 3600 * 24 * 30.5)
- return std::to_string((uint64_t)(seconds / (3600 * 24))) + " days";
- if (seconds < 3600 * 24 * 365.25)
- return std::to_string((uint64_t)(seconds / (3600 * 24 * 30.5))) + " months";
- if (seconds < 3600 * 24 * 365.25 * 100)
- return std::to_string((uint64_t)(seconds / (3600 * 24 * 30.5 * 365.25))) + " years";
+ {
+ ss << seconds / 3600.f;
+ return ss.str() + " hours";
+ }
+ if (seconds < 3600 * 24 * 30.5f)
+ {
+ ss << seconds / (3600 * 24.f);
+ return ss.str() + " days";
+ }
+ if (seconds < 3600 * 24 * 365.25f)
+ {
+ ss << seconds / (3600 * 24 * 30.5f);
+ return ss.str() + " months";
+ }
+ if (seconds < 3600 * 24 * 365.25f * 100)
+ {
+ ss << seconds / (3600 * 24 * 365.25f);
+ return ss.str() + " years";
+ }
return "a long time";
}
@@ -1279,6 +1295,86 @@ std::string get_nix_version_display_string()
return lines;
}
+ // Calculate a "sync weight" over ranges of blocks in the blockchain, suitable for
+ // calculating sync time estimates
+ uint64_t cumulative_block_sync_weight(cryptonote::network_type nettype, uint64_t start_block, uint64_t num_blocks)
+ {
+ if (nettype != cryptonote::MAINNET)
+ {
+ // No detailed data available except for Mainnet: Give back the number of blocks
+ // as a very simple and non-varying block sync weight for ranges of Testnet and
+ // Stagenet blocks
+ return num_blocks;
+ }
+
+ // The following is a table of average blocks sizes in bytes over the Monero mainnet
+ // blockchain, where the block size is averaged over ranges of 10,000 blocks
+ // (about 2 weeks worth of blocks each).
+ // The first array entry of 442 thus means "The average byte size of the blocks
+ // 0 .. 9,999 is 442". The info "block_size" from the "get_block_header_by_height"
+ // RPC call was used for calculating this. This table (and the whole mechanism
+ // of calculating a "sync weight") is most important when estimating times for
+ // syncing from scratch. Without it the fast progress through the (in comparison)
+ // rather small blocks in the early blockchain) would lead to vastly underestimated
+ // total sync times.
+ // It's no big problem for estimates that this table will, over time, and if not
+ // updated, miss larger and larger parts at the top of the blockchain, as long
+ // as block size averages there do not differ wildly.
+ // Without time-consuming tests it's hard to say how much the estimates would
+ // improve if one would not only take block sizes into account, but also varying
+ // verification times i.e. the different CPU effort needed for the different
+ // transaction types (pre / post RingCT, pre / post Bulletproofs).
+ // Testnet and Stagenet are neglected here because of their much smaller
+ // importance.
+ static const uint32_t average_block_sizes[] =
+ {
+ 442, 1211, 1445, 1763, 2272, 8217, 5603, 9999, 16358, 10805, 5290, 4362,
+ 4325, 5584, 4515, 5008, 4789, 5196, 7660, 3829, 6034, 2925, 3762, 2545,
+ 2437, 2553, 2167, 2761, 2015, 1969, 2350, 1731, 2367, 2078, 2026, 3518,
+ 2214, 1908, 1780, 1640, 1976, 1647, 1921, 1716, 1895, 2150, 2419, 2451,
+ 2147, 2327, 2251, 1644, 1750, 1481, 1570, 1524, 1562, 1668, 1386, 1494,
+ 1637, 1880, 1431, 1472, 1637, 1363, 1762, 1597, 1999, 1564, 1341, 1388,
+ 1530, 1476, 1617, 1488, 1368, 1906, 1403, 1695, 1535, 1598, 1318, 1234,
+ 1358, 1406, 1698, 1554, 1591, 1758, 1426, 2389, 1946, 1533, 1308, 2701,
+ 1525, 1653, 3580, 1889, 2913, 8164, 5154, 3762, 3356, 4360, 3589, 4844,
+ 4232, 3781, 3882, 5924, 10790, 7185, 7442, 8214, 8509, 7484, 6939, 7391,
+ 8210, 15572, 39680, 44810, 53873, 54639, 68227, 63428, 62386, 68504,
+ 83073, 103858, 117573, 98089, 96793, 102337, 94714, 129568, 251584,
+ 132026, 94579, 94516, 95722, 106495, 121824, 153983, 162338, 136608,
+ 137104, 109872, 91114, 84757, 96339, 74251, 94314, 143216, 155837,
+ 129968, 120201, 109913, 101588, 97332, 104611, 95310, 93419, 113345,
+ 100743, 92152, 57565, 22533, 37564, 21823, 19980, 18277, 18402, 14344,
+ 12142, 15842, 13677, 17631, 18294, 22270, 41422, 39296, 36688, 33512,
+ 33831, 27582, 22276, 27516, 27317, 25505, 24426, 20566, 23045, 26766,
+ 28185, 26169, 27011,
+ 28642 // Blocks 1,990,000 to 1,999,999 in December 2019
+ };
+ const uint64_t block_range_size = 10000;
+
+ uint64_t num_block_sizes = sizeof(average_block_sizes) / sizeof(average_block_sizes[0]);
+ uint64_t weight = 0;
+ uint64_t table_index = start_block / block_range_size;
+ for (;;) {
+ if (num_blocks == 0)
+ {
+ break;
+ }
+ if (table_index >= num_block_sizes)
+ {
+ // Take all blocks beyond our table as having the size of the blocks
+ // in the last table entry i.e. in the most recent known block range
+ weight += num_blocks * average_block_sizes[num_block_sizes - 1];
+ break;
+ }
+ uint64_t portion_size = std::min(num_blocks, block_range_size - start_block % block_range_size);
+ weight += portion_size * average_block_sizes[table_index];
+ table_index++;
+ num_blocks -= portion_size;
+ start_block += portion_size;
+ }
+ return weight;
+ }
+
std::vector<std::pair<std::string, size_t>> split_string_by_width(const std::string &s, size_t columns)
{
std::vector<std::string> lines;
diff --git a/src/common/util.h b/src/common/util.h
index b794d7908..25137ab64 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -46,6 +46,7 @@
#endif
#include "crypto/hash.h"
+#include "cryptonote_config.h"
/*! \brief Various Tools
*
@@ -252,4 +253,6 @@ namespace tools
void clear_screen();
std::vector<std::pair<std::string, size_t>> split_string_by_width(const std::string &s, size_t columns);
+
+ uint64_t cumulative_block_sync_weight(cryptonote::network_type nettype, uint64_t start_block, uint64_t num_blocks);
}
diff --git a/src/crypto/duration.h b/src/crypto/duration.h
new file mode 100644
index 000000000..493874288
--- /dev/null
+++ b/src/crypto/duration.h
@@ -0,0 +1,70 @@
+// Copyright (c) 2020, 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.
+
+#pragma once
+
+#include <chrono>
+#include "crypto/crypto.h"
+
+namespace crypto
+{
+ //! Generate poisson distributed values in discrete `D` time units.
+ template<typename D>
+ struct random_poisson_duration
+ {
+ using result_type = D; //!< std::chrono::duration time unit precision
+ using rep = typename result_type::rep; //!< Type used to represent duration value
+
+ //! \param average for generated durations
+ explicit random_poisson_duration(result_type average)
+ : dist(average.count() < 0 ? 0 : average.count())
+ {}
+
+ //! Generate a crypto-secure random duration
+ result_type operator()()
+ {
+ crypto::random_device rand{};
+ return result_type{dist(rand)};
+ }
+
+ private:
+ std::poisson_distribution<rep> dist;
+ };
+
+ /* A custom duration is used for subsecond precision because of the
+ variance. If 5000 milliseconds is given, 95% of the values fall between
+ 4859ms-5141ms in 1ms increments (not enough time variance). Providing 1/4
+ seconds would yield 95% of the values between 3s-7.25s in 1/4s
+ increments. */
+
+ //! Generate random durations with 1 second precision
+ using random_poisson_seconds = random_poisson_duration<std::chrono::seconds>;
+ //! Generate random duration with 1/4 second precision
+ using random_poisson_subseconds =
+ random_poisson_duration<std::chrono::duration<std::chrono::milliseconds::rep, std::ratio<1, 4>>>;
+}
diff --git a/src/cryptonote_basic/verification_context.h b/src/cryptonote_basic/verification_context.h
index 4d49b692c..ec5f604a5 100644
--- a/src/cryptonote_basic/verification_context.h
+++ b/src/cryptonote_basic/verification_context.h
@@ -29,6 +29,9 @@
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#pragma once
+
+#include "cryptonote_protocol/enums.h"
+
namespace cryptonote
{
/************************************************************************/
@@ -36,7 +39,9 @@ namespace cryptonote
/************************************************************************/
struct tx_verification_context
{
- bool m_should_be_relayed;
+ static_assert(unsigned(relay_method::none) == 0, "default m_relay initialization is not to relay_method::none");
+
+ relay_method m_relay; // gives indication on how tx should be relayed (if at all)
bool m_verifivation_failed; //bad tx, should drop connection
bool m_verifivation_impossible; //the transaction is related with an alternative blockchain
bool m_added_to_pool;
diff --git a/src/cryptonote_config.h b/src/cryptonote_config.h
index 66af46a5f..81dc15dee 100644
--- a/src/cryptonote_config.h
+++ b/src/cryptonote_config.h
@@ -102,7 +102,12 @@
#define CRYPTONOTE_MEMPOOL_TX_FROM_ALT_BLOCK_LIVETIME 604800 //seconds, one week
-#define CRYPTONOTE_DANDELIONPP_FLUSH_AVERAGE 5 // seconds
+#define CRYPTONOTE_DANDELIONPP_STEMS 2 // number of outgoing stem connections per epoch
+#define CRYPTONOTE_DANDELIONPP_FLUFF_PROBABILITY 10 // out of 100
+#define CRYPTONOTE_DANDELIONPP_MIN_EPOCH 10 // minutes
+#define CRYPTONOTE_DANDELIONPP_EPOCH_RANGE 30 // seconds
+#define CRYPTONOTE_DANDELIONPP_FLUSH_AVERAGE 5 // seconds average for poisson distributed fluff flush
+#define CRYPTONOTE_DANDELIONPP_EMBARGO_AVERAGE 173 // seconds (see tx_pool.cpp for more info)
// see src/cryptonote_protocol/levin_notify.cpp
#define CRYPTONOTE_NOISE_MIN_EPOCH 5 // minutes
diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp
index 7a13705a3..2571e4203 100644
--- a/src/cryptonote_core/blockchain.cpp
+++ b/src/cryptonote_core/blockchain.cpp
@@ -2493,38 +2493,10 @@ bool Blockchain::find_blockchain_supplement(const uint64_t req_start_block, cons
}
db_rtxn_guard rtxn_guard(m_db);
- total_height = get_current_blockchain_height();
- size_t count = 0, size = 0;
blocks.reserve(std::min(std::min(max_count, (size_t)10000), (size_t)(total_height - start_height)));
- for(uint64_t i = start_height; i < total_height && count < max_count && (size < FIND_BLOCKCHAIN_SUPPLEMENT_MAX_SIZE || count < 3); i++, count++)
- {
- blocks.resize(blocks.size()+1);
- blocks.back().first.first = m_db->get_block_blob_from_height(i);
- block b;
- CHECK_AND_ASSERT_MES(parse_and_validate_block_from_blob(blocks.back().first.first, b), false, "internal error, invalid block");
- blocks.back().first.second = get_miner_tx_hash ? cryptonote::get_transaction_hash(b.miner_tx) : crypto::null_hash;
- std::vector<cryptonote::blobdata> txs;
- if (pruned)
- {
- CHECK_AND_ASSERT_MES(m_db->get_pruned_tx_blobs_from(b.tx_hashes.front(), b.tx_hashes.size(), txs), false, "Failed to retrieve all transactions needed");
- }
- else
- {
- std::vector<crypto::hash> mis;
- get_transactions_blobs(b.tx_hashes, txs, mis, pruned);
- CHECK_AND_ASSERT_MES(!mis.size(), false, "internal error, transaction from block not found");
- }
- size += blocks.back().first.first.size();
- for (const auto &t: txs)
- size += t.size();
+ CHECK_AND_ASSERT_MES(m_db->get_blocks_from(start_height, 3, max_count, FIND_BLOCKCHAIN_SUPPLEMENT_MAX_SIZE, blocks, pruned, true, get_miner_tx_hash),
+ false, "Error getting blocks");
- CHECK_AND_ASSERT_MES(txs.size() == b.tx_hashes.size(), false, "mismatched sizes of b.tx_hashes and txs");
- blocks.back().second.reserve(txs.size());
- for (size_t i = 0; i < txs.size(); ++i)
- {
- blocks.back().second.push_back(std::make_pair(b.tx_hashes[i], std::move(txs[i])));
- }
- }
return true;
}
//------------------------------------------------------------------
diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp
index 7fb232ad2..10bbff457 100644
--- a/src/cryptonote_core/cryptonote_core.cpp
+++ b/src/cryptonote_core/cryptonote_core.cpp
@@ -1284,6 +1284,7 @@ namespace cryptonote
break;
case relay_method::block:
case relay_method::fluff:
+ case relay_method::stem:
public_req.txs.push_back(std::move(std::get<1>(tx)));
break;
}
@@ -1295,9 +1296,9 @@ namespace cryptonote
re-relaying public and private _should_ be acceptable here. */
const boost::uuids::uuid source = boost::uuids::nil_uuid();
if (!public_req.txs.empty())
- get_protocol()->relay_transactions(public_req, source, epee::net_utils::zone::public_);
+ get_protocol()->relay_transactions(public_req, source, epee::net_utils::zone::public_, relay_method::fluff);
if (!private_req.txs.empty())
- get_protocol()->relay_transactions(private_req, source, epee::net_utils::zone::invalid);
+ get_protocol()->relay_transactions(private_req, source, epee::net_utils::zone::invalid, relay_method::local);
}
return true;
}
diff --git a/src/cryptonote_core/tx_pool.cpp b/src/cryptonote_core/tx_pool.cpp
index 53a6ce579..c49a3dabc 100644
--- a/src/cryptonote_core/tx_pool.cpp
+++ b/src/cryptonote_core/tx_pool.cpp
@@ -46,6 +46,7 @@
#include "warnings.h"
#include "common/perf_timer.h"
#include "crypto/hash.h"
+#include "crypto/duration.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "txpool"
@@ -58,6 +59,29 @@ namespace cryptonote
{
namespace
{
+ /*! The Dandelion++ has formula for calculating the average embargo timeout:
+ (-k*(k-1)*hop)/(2*log(1-ep))
+ where k is the number of hops before this node and ep is the probability
+ that one of the k hops hits their embargo timer, and hop is the average
+ time taken between hops. So decreasing ep will make it more probable
+ that "this" node is the first to expire the embargo timer. Increasing k
+ will increase the number of nodes that will be "hidden" as a prior
+ recipient of the tx.
+
+ As example, k=5 and ep=0.1 means "this" embargo timer has a 90%
+ probability of being the first to expire amongst 5 nodes that saw the
+ tx before "this" one. These values are independent to the fluff
+ probability, but setting a low k with a low p (fluff probability) is
+ not ideal since a blackhole is more likely to reveal earlier nodes in
+ the chain.
+
+ This value was calculated with k=10, ep=0.10, and hop = 175 ms. A
+ testrun from a recent Intel laptop took ~80ms to
+ receive+parse+proces+send transaction. At least 50ms will be added to
+ the latency if crossing an ocean. So 175ms is the fudge factor for
+ a single hop with 173s being the embargo timer. */
+ constexpr const std::chrono::seconds dandelionpp_embargo_average{CRYPTONOTE_DANDELIONPP_EMBARGO_AVERAGE};
+
//TODO: constants such as these should at least be in the header,
// but probably somewhere more accessible to the rest of the
// codebase. As it stands, it is at best nontrivial to test
@@ -262,34 +286,51 @@ namespace cryptonote
}
}else
{
- //update transactions container
- meta.weight = tx_weight;
- meta.fee = fee;
- meta.max_used_block_id = max_used_block_id;
- meta.max_used_block_height = max_used_block_height;
- meta.last_failed_height = 0;
- meta.last_failed_id = null_hash;
- meta.receive_time = receive_time;
- meta.last_relayed_time = time(NULL);
- meta.relayed = relayed;
- meta.set_relay_method(tx_relay);
- meta.double_spend_seen = false;
- meta.pruned = tx.pruned;
- meta.bf_padding = 0;
- memset(meta.padding, 0, sizeof(meta.padding));
-
try
{
if (kept_by_block)
m_parsed_tx_cache.insert(std::make_pair(id, tx));
CRITICAL_REGION_LOCAL1(m_blockchain);
LockedTXN lock(m_blockchain.get_db());
- m_blockchain.remove_txpool_tx(id);
- if (!insert_key_images(tx, id, tx_relay))
- return false;
- m_blockchain.add_txpool_tx(id, blob, meta);
- m_txs_by_fee_and_receive_time.emplace(std::pair<double, std::time_t>(fee / (double)(tx_weight ? tx_weight : 1), receive_time), id);
+ const bool existing_tx = m_blockchain.get_txpool_tx_meta(id, meta);
+ if (existing_tx)
+ {
+ /* If Dandelion++ loop. Do not use txes in the `local` state in the
+ loop detection - txes in that state should be outgoing over i2p/tor
+ then routed back via public dandelion++ stem. Pretend to be
+ another stem node in that situation, a loop over the public
+ network hasn't been hit yet. */
+ if (tx_relay == relay_method::stem && meta.dandelionpp_stem)
+ tx_relay = relay_method::fluff;
+ }
+ else
+ meta.set_relay_method(relay_method::none);
+
+ if (meta.upgrade_relay_method(tx_relay) || !existing_tx) // synchronize with embargo timer or stem/fluff out-of-order messages
+ {
+ //update transactions container
+ meta.last_relayed_time = std::numeric_limits<decltype(meta.last_relayed_time)>::max();
+ meta.receive_time = receive_time;
+ meta.weight = tx_weight;
+ meta.fee = fee;
+ meta.max_used_block_id = max_used_block_id;
+ meta.max_used_block_height = max_used_block_height;
+ meta.last_failed_height = 0;
+ meta.last_failed_id = null_hash;
+ meta.relayed = relayed;
+ meta.double_spend_seen = false;
+ meta.pruned = tx.pruned;
+ meta.bf_padding = 0;
+ memset(meta.padding, 0, sizeof(meta.padding));
+
+ if (!insert_key_images(tx, id, tx_relay))
+ return false;
+
+ m_blockchain.remove_txpool_tx(id);
+ m_blockchain.add_txpool_tx(id, blob, meta);
+ m_txs_by_fee_and_receive_time.emplace(std::pair<double, std::time_t>(fee / (double)(tx_weight ? tx_weight : 1), receive_time), id);
+ }
lock.commit();
}
catch (const std::exception &e)
@@ -299,8 +340,9 @@ namespace cryptonote
}
tvc.m_added_to_pool = true;
- if(meta.fee > 0 && tx_relay != relay_method::none)
- tvc.m_should_be_relayed = true;
+ static_assert(unsigned(relay_method::none) == 0, "expected relay_method::none value to be zero");
+ if(meta.fee > 0)
+ tvc.m_relay = tx_relay;
}
tvc.m_verifivation_failed = false;
@@ -404,27 +446,18 @@ namespace cryptonote
CHECKED_GET_SPECIFIC_VARIANT(in, const txin_to_key, txin, false);
std::unordered_set<crypto::hash>& kei_image_set = m_spent_key_images[txin.k_image];
- /* If any existing key-image in the set is publicly visible AND this is
- not forcibly "kept_by_block", then fail (duplicate key image). If all
- existing key images are supposed to be hidden, we silently allow so
- that the node doesn't leak knowledge of a local/stem tx. */
- bool visible = false;
+ // Only allow multiple txes per key-image if kept-by-block. Only allow
+ // the same txid if going from local/stem->fluff.
+
if (tx_relay != relay_method::block)
{
- for (const crypto::hash& other_id : kei_image_set)
- visible |= m_blockchain.txpool_tx_matches_category(other_id, relay_category::legacy);
- }
-
- CHECK_AND_ASSERT_MES(!visible, false, "internal error: tx_relay=" << unsigned(tx_relay)
+ const bool one_txid =
+ (kei_image_set.empty() || (kei_image_set.size() == 1 && *(kei_image_set.cbegin()) == id));
+ CHECK_AND_ASSERT_MES(one_txid, false, "internal error: tx_relay=" << unsigned(tx_relay)
<< ", kei_image_set.size()=" << kei_image_set.size() << ENDL << "txin.k_image=" << txin.k_image << ENDL
<< "tx_id=" << id);
+ }
- /* If adding a tx (hash) that already exists, fail only if the tx has
- been publicly "broadcast" previously. This way, when a private tx is
- received for the first time from a remote node, "this" node will
- respond as-if it were seen for the first time. LMDB does the
- "hard-check" on key-images, so the effect is overwriting the existing
- tx_pool metadata and "first seen" time. */
const bool new_or_previously_private =
kei_image_set.insert(id).second ||
!m_blockchain.txpool_tx_matches_category(id, relay_category::legacy);
@@ -562,7 +595,7 @@ namespace cryptonote
td.last_failed_height = meta.last_failed_height;
td.last_failed_id = meta.last_failed_id;
td.receive_time = meta.receive_time;
- td.last_relayed_time = meta.last_relayed_time;
+ td.last_relayed_time = meta.dandelionpp_stem ? 0 : meta.last_relayed_time;
td.relayed = meta.relayed;
td.do_not_relay = meta.do_not_relay;
td.double_spend_seen = meta.double_spend_seen;
@@ -695,8 +728,13 @@ namespace cryptonote
txs.reserve(m_blockchain.get_txpool_tx_count());
m_blockchain.for_all_txpool_txes([this, now, &txs](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *){
// 0 fee transactions are never relayed
- if(!meta.pruned && meta.fee > 0 && !meta.do_not_relay && now - meta.last_relayed_time > get_relay_delay(now, meta.receive_time))
+ if(!meta.pruned && meta.fee > 0 && !meta.do_not_relay)
{
+ if (!meta.dandelionpp_stem && now - meta.last_relayed_time <= get_relay_delay(now, meta.receive_time))
+ return true;
+ if (meta.dandelionpp_stem && meta.last_relayed_time < now) // for dandelion++ stem, this value is the embargo timeout
+ return true;
+
// if the tx is older than half the max lifetime, we don't re-relay it, to avoid a problem
// mentioned by smooth where nodes would flush txes at slightly different times, causing
// flushed txes to be re-added when received from a node which was just about to flush it
@@ -721,9 +759,11 @@ namespace cryptonote
//---------------------------------------------------------------------------------
void tx_memory_pool::set_relayed(const epee::span<const crypto::hash> hashes, const relay_method method)
{
+ crypto::random_poisson_seconds embargo_duration{dandelionpp_embargo_average};
+ const auto now = std::chrono::system_clock::now();
+
CRITICAL_REGION_LOCAL(m_transactions_lock);
CRITICAL_REGION_LOCAL1(m_blockchain);
- const time_t now = time(NULL);
LockedTXN lock(m_blockchain.get_db());
for (const auto& hash : hashes)
{
@@ -732,9 +772,15 @@ namespace cryptonote
txpool_tx_meta_t meta;
if (m_blockchain.get_txpool_tx_meta(hash, meta))
{
+ // txes can be received as "stem" or "fluff" in either order
+ meta.upgrade_relay_method(method);
meta.relayed = true;
- meta.last_relayed_time = now;
- meta.set_relay_method(method);
+
+ if (meta.dandelionpp_stem)
+ meta.last_relayed_time = std::chrono::system_clock::to_time_t(now + embargo_duration());
+ else
+ meta.last_relayed_time = std::chrono::system_clock::to_time_t(now);
+
m_blockchain.update_txpool_tx(hash, meta);
}
}
@@ -919,7 +965,7 @@ namespace cryptonote
txi.receive_time = include_sensitive_data ? meta.receive_time : 0;
txi.relayed = meta.relayed;
// In restricted mode we do not include this data:
- txi.last_relayed_time = include_sensitive_data ? meta.last_relayed_time : 0;
+ txi.last_relayed_time = (include_sensitive_data && !meta.dandelionpp_stem) ? meta.last_relayed_time : 0;
txi.do_not_relay = meta.do_not_relay;
txi.double_spend_seen = meta.double_spend_seen;
tx_infos.push_back(std::move(txi));
@@ -971,7 +1017,7 @@ namespace cryptonote
txi.last_failed_block_hash = meta.last_failed_id;
txi.receive_time = meta.receive_time;
txi.relayed = meta.relayed;
- txi.last_relayed_time = meta.last_relayed_time;
+ txi.last_relayed_time = meta.dandelionpp_stem ? 0 : meta.last_relayed_time;
txi.do_not_relay = meta.do_not_relay;
txi.double_spend_seen = meta.double_spend_seen;
tx_infos.push_back(txi);
diff --git a/src/cryptonote_protocol/cryptonote_protocol_defs.h b/src/cryptonote_protocol/cryptonote_protocol_defs.h
index f809bff74..76b57afd3 100644
--- a/src/cryptonote_protocol/cryptonote_protocol_defs.h
+++ b/src/cryptonote_protocol/cryptonote_protocol_defs.h
@@ -197,10 +197,12 @@ namespace cryptonote
{
std::vector<blobdata> txs;
std::string _; // padding
+ bool dandelionpp_fluff; //zero initialization defaults to stem mode
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(txs)
KV_SERIALIZE(_)
+ KV_SERIALIZE_OPT(dandelionpp_fluff, true) // backwards compatible mode is fluff
END_KV_SERIALIZE_MAP()
};
typedef epee::misc_utils::struct_init<request_t> request;
diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.h b/src/cryptonote_protocol/cryptonote_protocol_handler.h
index 2664716a8..e2ad3727f 100644
--- a/src/cryptonote_protocol/cryptonote_protocol_handler.h
+++ b/src/cryptonote_protocol/cryptonote_protocol_handler.h
@@ -129,7 +129,7 @@ namespace cryptonote
//----------------- i_bc_protocol_layout ---------------------------------------
virtual bool relay_block(NOTIFY_NEW_BLOCK::request& arg, cryptonote_connection_context& exclude_context);
- virtual bool relay_transactions(NOTIFY_NEW_TRANSACTIONS::request& arg, const boost::uuids::uuid& source, epee::net_utils::zone zone);
+ virtual bool relay_transactions(NOTIFY_NEW_TRANSACTIONS::request& arg, const boost::uuids::uuid& source, epee::net_utils::zone zone, relay_method tx_relay);
//----------------------------------------------------------------------------------
//bool get_payload_sync_data(HANDSHAKE_DATA::request& hshd, cryptonote_connection_context& context);
bool should_drop_connection(cryptonote_connection_context& context, uint32_t next_stripe);
@@ -170,6 +170,14 @@ namespace cryptonote
size_t m_block_download_max_size;
bool m_sync_pruned_blocks;
+ // Values for sync time estimates
+ boost::posix_time::ptime m_sync_start_time;
+ boost::posix_time::ptime m_period_start_time;
+ uint64_t m_sync_start_height;
+ uint64_t m_period_start_height;
+ uint64_t get_estimated_remaining_sync_seconds(uint64_t current_blockchain_height, uint64_t target_blockchain_height);
+ std::string get_periodic_sync_estimate(uint64_t current_blockchain_height, uint64_t target_blockchain_height);
+
boost::mutex m_buffer_mutex;
double get_avg_block_size();
boost::circular_buffer<size_t> m_avg_buffer = boost::circular_buffer<size_t>(10);
diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.inl b/src/cryptonote_protocol/cryptonote_protocol_handler.inl
index 3aacce421..f8e032fde 100644
--- a/src/cryptonote_protocol/cryptonote_protocol_handler.inl
+++ b/src/cryptonote_protocol/cryptonote_protocol_handler.inl
@@ -43,6 +43,7 @@
#include "profile_tools.h"
#include "net/network_throttle-detail.hpp"
#include "common/pruning.h"
+#include "common/util.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "net.cn"
@@ -362,7 +363,7 @@ namespace cryptonote
uint64_t last_block_v1 = m_core.get_nettype() == TESTNET ? 624633 : m_core.get_nettype() == MAINNET ? 1009826 : (uint64_t)-1;
uint64_t diff_v2 = max_block_height > last_block_v1 ? std::min(abs_diff, max_block_height - last_block_v1) : 0;
MCLOG(is_inital ? el::Level::Info : el::Level::Debug, "global", el::Color::Yellow, context << "Sync data returned a new top block candidate: " << m_core.get_current_blockchain_height() << " -> " << hshd.current_height
- << " [Your node is " << abs_diff << " blocks (" << ((abs_diff - diff_v2) / (24 * 60 * 60 / DIFFICULTY_TARGET_V1)) + (diff_v2 / (24 * 60 * 60 / DIFFICULTY_TARGET_V2)) << " days) "
+ << " [Your node is " << abs_diff << " blocks (" << tools::get_human_readable_timespan((abs_diff - diff_v2) * DIFFICULTY_TARGET_V1 + diff_v2 * DIFFICULTY_TARGET_V2) << ") "
<< (0 <= diff ? std::string("behind") : std::string("ahead"))
<< "] " << ENDL << "SYNCHRONIZATION started");
if (hshd.current_height >= m_core.get_current_blockchain_height() + 5) // don't switch to unsafe mode just for a few blocks
@@ -926,29 +927,60 @@ namespace cryptonote
return 1;
}
- std::vector<cryptonote::blobdata> newtxs;
- newtxs.reserve(arg.txs.size());
- for (size_t i = 0; i < arg.txs.size(); ++i)
+ relay_method tx_relay;
+ std::vector<blobdata> stem_txs{};
+ std::vector<blobdata> fluff_txs{};
+ if (arg.dandelionpp_fluff)
{
- cryptonote::tx_verification_context tvc{};
- m_core.handle_incoming_tx({arg.txs[i], crypto::null_hash}, tvc, relay_method::fluff, true);
- if(tvc.m_verifivation_failed)
+ tx_relay = relay_method::fluff;
+ fluff_txs.reserve(arg.txs.size());
+ }
+ else
+ {
+ tx_relay = relay_method::stem;
+ stem_txs.reserve(arg.txs.size());
+ }
+
+ for (auto& tx : arg.txs)
+ {
+ tx_verification_context tvc{};
+ if (!m_core.handle_incoming_tx({tx, crypto::null_hash}, tvc, tx_relay, true))
{
LOG_PRINT_CCONTEXT_L1("Tx verification failed, dropping connection");
drop_connection(context, false, false);
return 1;
}
- if(tvc.m_should_be_relayed)
- newtxs.push_back(std::move(arg.txs[i]));
+
+ switch (tvc.m_relay)
+ {
+ case relay_method::local:
+ case relay_method::stem:
+ stem_txs.push_back(std::move(tx));
+ break;
+ case relay_method::block:
+ case relay_method::fluff:
+ fluff_txs.push_back(std::move(tx));
+ break;
+ default:
+ case relay_method::none:
+ break;
+ }
}
- arg.txs = std::move(newtxs);
- if(arg.txs.size())
+ if (!stem_txs.empty())
{
//TODO: add announce usage here
- relay_transactions(arg, context.m_connection_id, context.m_remote_address.get_zone());
+ arg.dandelionpp_fluff = false;
+ arg.txs = std::move(stem_txs);
+ relay_transactions(arg, context.m_connection_id, context.m_remote_address.get_zone(), relay_method::stem);
+ }
+ if (!fluff_txs.empty())
+ {
+ //TODO: add announce usage here
+ arg.dandelionpp_fluff = true;
+ arg.txs = std::move(fluff_txs);
+ relay_transactions(arg, context.m_connection_id, context.m_remote_address.get_zone(), relay_method::fluff);
}
-
return 1;
}
//------------------------------------------------------------------------------------------------------------------------
@@ -1181,6 +1213,55 @@ namespace cryptonote
return 1;
}
+ // Get an estimate for the remaining sync time from given current to target blockchain height, in seconds
+ template<class t_core>
+ uint64_t t_cryptonote_protocol_handler<t_core>::get_estimated_remaining_sync_seconds(uint64_t current_blockchain_height, uint64_t target_blockchain_height)
+ {
+ // The average sync speed varies so much, even averaged over quite long time periods like 10 minutes,
+ // that using some sliding window would be difficult to implement without often leading to bad estimates.
+ // The simplest strategy - always average sync speed over the maximum available interval i.e. since sync
+ // started at all (from "m_sync_start_time" and "m_sync_start_height") - gives already useful results
+ // and seems to be quite robust. Some quite special cases like "Internet connection suddenly becoming
+ // much faster after syncing already a long time, and staying fast" are not well supported however.
+
+ if (target_blockchain_height <= current_blockchain_height)
+ {
+ // Syncing stuck, or other special circumstance: Avoid errors, simply give back 0
+ return 0;
+ }
+
+ const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
+ const boost::posix_time::time_duration sync_time = now - m_sync_start_time;
+ cryptonote::network_type nettype = m_core.get_nettype();
+
+ // Don't simply use remaining number of blocks for the estimate but "sync weight" as provided by
+ // "cumulative_block_sync_weight" which knows about strongly varying Monero mainnet block sizes
+ uint64_t synced_weight = tools::cumulative_block_sync_weight(nettype, m_sync_start_height, current_blockchain_height - m_sync_start_height);
+ float us_per_weight = (float)sync_time.total_microseconds() / (float)synced_weight;
+ uint64_t remaining_weight = tools::cumulative_block_sync_weight(nettype, current_blockchain_height, target_blockchain_height - current_blockchain_height);
+ float remaining_us = us_per_weight * (float)remaining_weight;
+ return (uint64_t)(remaining_us / 1e6);
+ }
+
+ // Return a textual remaining sync time estimate, or the empty string if waiting period not yet over
+ template<class t_core>
+ std::string t_cryptonote_protocol_handler<t_core>::get_periodic_sync_estimate(uint64_t current_blockchain_height, uint64_t target_blockchain_height)
+ {
+ std::string text = "";
+ const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
+ boost::posix_time::time_duration period_sync_time = now - m_period_start_time;
+ if (period_sync_time > boost::posix_time::minutes(2))
+ {
+ // Period is over, time to report another estimate
+ uint64_t remaining_seconds = get_estimated_remaining_sync_seconds(current_blockchain_height, target_blockchain_height);
+ text = tools::get_human_readable_timespan(remaining_seconds);
+
+ // Start the new period
+ m_period_start_time = now;
+ }
+ return text;
+ }
+
template<class t_core>
int t_cryptonote_protocol_handler<t_core>::try_add_next_blocks(cryptonote_connection_context& context)
{
@@ -1209,6 +1290,9 @@ namespace cryptonote
if (!starting)
m_last_add_end_time = tools::get_tick_count();
});
+ m_sync_start_time = boost::posix_time::microsec_clock::universal_time();
+ m_sync_start_height = m_core.get_current_blockchain_height();
+ m_period_start_time = m_sync_start_time;
while (1)
{
@@ -1459,7 +1543,16 @@ namespace cryptonote
if (completion_percent == 100) // never show 100% if not actually up to date
completion_percent = 99;
progress_message = " (" + std::to_string(completion_percent) + "%, "
- + std::to_string(target_blockchain_height - current_blockchain_height) + " left)";
+ + std::to_string(target_blockchain_height - current_blockchain_height) + " left";
+ std::string time_message = get_periodic_sync_estimate(current_blockchain_height, target_blockchain_height);
+ if (!time_message.empty())
+ {
+ uint64_t total_blocks_to_sync = target_blockchain_height - m_sync_start_height;
+ uint64_t total_blocks_synced = current_blockchain_height - m_sync_start_height;
+ progress_message += ", " + std::to_string(total_blocks_synced * 100 / total_blocks_to_sync) + "% of total synced";
+ progress_message += ", estimated " + time_message + " left";
+ }
+ progress_message += ")";
}
const uint32_t previous_stripe = tools::get_pruning_stripe(previous_height, target_blockchain_height, CRYPTONOTE_PRUNING_LOG_STRIPES);
const uint32_t current_stripe = tools::get_pruning_stripe(current_blockchain_height, target_blockchain_height, CRYPTONOTE_PRUNING_LOG_STRIPES);
@@ -2197,8 +2290,26 @@ skip:
bool t_cryptonote_protocol_handler<t_core>::on_connection_synchronized()
{
bool val_expected = false;
- if(!m_core.is_within_compiled_block_hash_area(m_core.get_current_blockchain_height()) && m_synchronized.compare_exchange_strong(val_expected, true))
+ uint64_t current_blockchain_height = m_core.get_current_blockchain_height();
+ if(!m_core.is_within_compiled_block_hash_area(current_blockchain_height) && m_synchronized.compare_exchange_strong(val_expected, true))
{
+ if ((current_blockchain_height > m_sync_start_height) && (m_sync_spans_downloaded > 0))
+ {
+ uint64_t synced_blocks = current_blockchain_height - m_sync_start_height;
+ // Report only after syncing an "interesting" number of blocks:
+ if (synced_blocks > 20)
+ {
+ const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
+ uint64_t synced_seconds = (now - m_sync_start_time).total_seconds();
+ if (synced_seconds == 0)
+ {
+ synced_seconds = 1;
+ }
+ float blocks_per_second = (1000 * synced_blocks / synced_seconds) / 1000.0f;
+ MGINFO_YELLOW("Synced " << synced_blocks << " blocks in "
+ << tools::get_human_readable_timespan(synced_seconds) << " (" << blocks_per_second << " blocks per second)");
+ }
+ }
MGINFO_YELLOW(ENDL << "**********************************************************************" << ENDL
<< "You are now synchronized with the network. You may now start monero-wallet-cli." << ENDL
<< ENDL
@@ -2387,14 +2498,14 @@ skip:
}
//------------------------------------------------------------------------------------------------------------------------
template<class t_core>
- bool t_cryptonote_protocol_handler<t_core>::relay_transactions(NOTIFY_NEW_TRANSACTIONS::request& arg, const boost::uuids::uuid& source, epee::net_utils::zone zone)
+ bool t_cryptonote_protocol_handler<t_core>::relay_transactions(NOTIFY_NEW_TRANSACTIONS::request& arg, const boost::uuids::uuid& source, epee::net_utils::zone zone, relay_method tx_relay)
{
/* Push all outgoing transactions to this function. The behavior needs to
identify how the transaction is going to be relayed, and then update the
local mempool before doing the relay. The code was already updating the
DB twice on received transactions - it is difficult to workaround this
due to the internal design. */
- return m_p2p->send_txs(std::move(arg.txs), zone, source, m_core) != epee::net_utils::zone::invalid;
+ return m_p2p->send_txs(std::move(arg.txs), zone, source, m_core, tx_relay) != epee::net_utils::zone::invalid;
}
//------------------------------------------------------------------------------------------------------------------------
template<class t_core>
diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler_common.h b/src/cryptonote_protocol/cryptonote_protocol_handler_common.h
index 978a9ebf3..11184299d 100644
--- a/src/cryptonote_protocol/cryptonote_protocol_handler_common.h
+++ b/src/cryptonote_protocol/cryptonote_protocol_handler_common.h
@@ -41,7 +41,7 @@ namespace cryptonote
struct i_cryptonote_protocol
{
virtual bool relay_block(NOTIFY_NEW_BLOCK::request& arg, cryptonote_connection_context& exclude_context)=0;
- virtual bool relay_transactions(NOTIFY_NEW_TRANSACTIONS::request& arg, const boost::uuids::uuid& source, epee::net_utils::zone zone)=0;
+ virtual bool relay_transactions(NOTIFY_NEW_TRANSACTIONS::request& arg, const boost::uuids::uuid& source, epee::net_utils::zone zone, relay_method tx_relay)=0;
//virtual bool request_objects(NOTIFY_REQUEST_GET_OBJECTS::request& arg, cryptonote_connection_context& context)=0;
};
@@ -54,7 +54,7 @@ namespace cryptonote
{
return false;
}
- virtual bool relay_transactions(NOTIFY_NEW_TRANSACTIONS::request& arg, const boost::uuids::uuid& source, epee::net_utils::zone zone)
+ virtual bool relay_transactions(NOTIFY_NEW_TRANSACTIONS::request& arg, const boost::uuids::uuid& source, epee::net_utils::zone zone, relay_method tx_relay)
{
return false;
}
diff --git a/src/cryptonote_protocol/enums.h b/src/cryptonote_protocol/enums.h
index 2ec622d94..a3a52b83f 100644
--- a/src/cryptonote_protocol/enums.h
+++ b/src/cryptonote_protocol/enums.h
@@ -37,7 +37,8 @@ namespace cryptonote
{
none = 0, //!< Received via RPC with `do_not_relay` set
local, //!< Received via RPC; trying to send over i2p/tor, etc.
- block, //!< Received in block, takes precedence over others
- fluff //!< Received/sent over public networks
+ stem, //!< Received/send over network using Dandelion++ stem
+ fluff, //!< Received/sent over network using Dandelion++ fluff
+ block //!< Received in block, takes precedence over others
};
}
diff --git a/src/cryptonote_protocol/levin_notify.cpp b/src/cryptonote_protocol/levin_notify.cpp
index 428b739bc..127801092 100644
--- a/src/cryptonote_protocol/levin_notify.cpp
+++ b/src/cryptonote_protocol/levin_notify.cpp
@@ -30,6 +30,7 @@
#include <boost/asio/steady_timer.hpp>
#include <boost/system/system_error.hpp>
+#include <boost/uuid/uuid_io.hpp>
#include <chrono>
#include <deque>
#include <stdexcept>
@@ -38,8 +39,10 @@
#include "common/expect.h"
#include "common/varint.h"
#include "cryptonote_config.h"
-#include "crypto/random.h"
+#include "crypto/crypto.h"
+#include "crypto/duration.h"
#include "cryptonote_basic/connection_context.h"
+#include "cryptonote_core/i_core_events.h"
#include "cryptonote_protocol/cryptonote_protocol_defs.h"
#include "net/dandelionpp.h"
#include "p2p/net_node.h"
@@ -61,11 +64,14 @@ namespace levin
{
namespace
{
- constexpr std::size_t connection_id_reserve_size = 100;
+ constexpr const std::size_t connection_id_reserve_size = 100;
constexpr const std::chrono::minutes noise_min_epoch{CRYPTONOTE_NOISE_MIN_EPOCH};
constexpr const std::chrono::seconds noise_epoch_range{CRYPTONOTE_NOISE_EPOCH_RANGE};
+ constexpr const std::chrono::minutes dandelionpp_min_epoch{CRYPTONOTE_DANDELIONPP_MIN_EPOCH};
+ constexpr const std::chrono::seconds dandelionpp_epoch_range{CRYPTONOTE_DANDELIONPP_EPOCH_RANGE};
+
constexpr const std::chrono::seconds noise_min_delay{CRYPTONOTE_NOISE_MIN_DELAY};
constexpr const std::chrono::seconds noise_delay_range{CRYPTONOTE_NOISE_DELAY_RANGE};
@@ -83,22 +89,8 @@ namespace levin
connections (Dandelion++ makes similar assumptions in its stem
algorithm). The randomization yields 95% values between 1s-4s in
1/4s increments. */
- constexpr const fluff_stepsize fluff_average_out{fluff_stepsize{fluff_average_in} / 2};
-
- class random_poisson
- {
- std::poisson_distribution<fluff_stepsize::rep> dist;
- public:
- explicit random_poisson(fluff_stepsize average)
- : dist(average.count() < 0 ? 0 : average.count())
- {}
-
- fluff_stepsize operator()()
- {
- crypto::random_device rand{};
- return fluff_stepsize{dist(rand)};
- }
- };
+ using fluff_duration = crypto::random_poisson_subseconds::result_type;
+ constexpr const fluff_duration fluff_average_out{fluff_duration{fluff_average_in} / 2};
/*! Select a randomized duration from 0 to `range`. The precision will be to
the systems `steady_clock`. As an example, supplying 3 seconds to this
@@ -132,10 +124,11 @@ namespace levin
return outs;
}
- std::string make_tx_payload(std::vector<blobdata>&& txs, const bool pad)
+ std::string make_tx_payload(std::vector<blobdata>&& txs, const bool pad, const bool fluff)
{
NOTIFY_NEW_TRANSACTIONS::request request{};
request.txs = std::move(txs);
+ request.dandelionpp_fluff = fluff;
if (pad)
{
@@ -172,9 +165,9 @@ namespace levin
return fullBlob;
}
- bool make_payload_send_txs(connections& p2p, std::vector<blobdata>&& txs, const boost::uuids::uuid& destination, const bool pad)
+ bool make_payload_send_txs(connections& p2p, std::vector<blobdata>&& txs, const boost::uuids::uuid& destination, const bool pad, const bool fluff)
{
- const cryptonote::blobdata blob = make_tx_payload(std::move(txs), pad);
+ const cryptonote::blobdata blob = make_tx_payload(std::move(txs), pad, fluff);
p2p.for_connection(destination, [&blob](detail::p2p_context& context) {
on_levin_traffic(context, true, true, false, blob.size(), get_command_from_message(blob));
return true;
@@ -251,7 +244,8 @@ namespace levin
flush_time(std::chrono::steady_clock::time_point::max()),
connection_count(0),
is_public(is_public),
- pad_txs(pad_txs)
+ pad_txs(pad_txs),
+ fluffing(false)
{
for (std::size_t count = 0; !noise.empty() && count < CRYPTONOTE_NOISE_CHANNELS; ++count)
channels.emplace_back(io_service);
@@ -268,6 +262,7 @@ namespace levin
std::atomic<std::size_t> connection_count; //!< Only update in strand, can be read at any time
const bool is_public; //!< Zone is public ipv4/ipv6 connections
const bool pad_txs; //!< Pad txs to the next boundary for privacy
+ bool fluffing; //!< Zone is in Dandelion++ fluff epoch
};
} // detail
@@ -362,10 +357,11 @@ namespace levin
return true;
});
+ // Always send txs in stem mode over i2p/tor, see comments in `send_txs` below.
for (auto& connection : connections)
{
std::sort(connection.first.begin(), connection.first.end()); // don't leak receive order
- make_payload_send_txs(*zone_->p2p, std::move(connection.first), connection.second, zone_->pad_txs);
+ make_payload_send_txs(*zone_->p2p, std::move(connection.first), connection.second, zone_->pad_txs, zone_->is_public);
}
if (next_flush != std::chrono::steady_clock::time_point::max())
@@ -387,29 +383,38 @@ namespace levin
void operator()()
{
- if (!zone_ || !zone_->p2p || txs_.empty())
+ run(std::move(zone_), epee::to_span(txs_), source_);
+ }
+
+ static void run(std::shared_ptr<detail::zone> zone, epee::span<const blobdata> txs, const boost::uuids::uuid& source)
+ {
+ if (!zone || !zone->p2p || txs.empty())
return;
- assert(zone_->strand.running_in_this_thread());
+ assert(zone->strand.running_in_this_thread());
const auto now = std::chrono::steady_clock::now();
auto next_flush = std::chrono::steady_clock::time_point::max();
- random_poisson in_duration(fluff_average_in);
- random_poisson out_duration(fluff_average_out);
+ crypto::random_poisson_subseconds in_duration(fluff_average_in);
+ crypto::random_poisson_subseconds out_duration(fluff_average_out);
+
+
+ MDEBUG("Queueing " << txs.size() << " transaction(s) for Dandelion++ fluffing");
bool available = false;
- zone_->p2p->foreach_connection([this, now, &in_duration, &out_duration, &next_flush, &available] (detail::p2p_context& context)
+ zone->p2p->foreach_connection([txs, now, &zone, &source, &in_duration, &out_duration, &next_flush, &available] (detail::p2p_context& context)
{
- if (this->source_ != context.m_connection_id && (this->zone_->is_public || !context.m_is_income))
+ // When i2p/tor, only fluff to outbound connections
+ if (source != context.m_connection_id && (zone->is_public || !context.m_is_income))
{
available = true;
if (context.fluff_txs.empty())
context.flush_time = now + (context.m_is_income ? in_duration() : out_duration());
next_flush = std::min(next_flush, context.flush_time);
- context.fluff_txs.reserve(context.fluff_txs.size() + this->txs_.size());
- for (const blobdata& tx : this->txs_)
+ context.fluff_txs.reserve(context.fluff_txs.size() + txs.size());
+ for (const blobdata& tx : txs)
context.fluff_txs.push_back(tx); // must copy instead of move (multiple conns)
}
return true;
@@ -418,8 +423,8 @@ namespace levin
if (!available)
MWARNING("Unable to send transaction(s), no available connections");
- if (next_flush < zone_->flush_time)
- fluff_flush::queue(std::move(zone_), next_flush);
+ if (next_flush < zone->flush_time)
+ fluff_flush::queue(std::move(zone), next_flush);
}
};
@@ -471,6 +476,11 @@ namespace levin
assert(zone->strand.running_in_this_thread());
zone->connection_count = zone->map.size();
+
+ // only noise uses the "noise channels", only update when enabled
+ if (zone->noise.empty())
+ return;
+
for (auto id = zone->map.begin(); id != zone->map.end(); ++id)
{
const std::size_t i = id - zone->map.begin();
@@ -479,26 +489,75 @@ namespace levin
}
//! \pre Called within `zone_->strand`.
+ static void run(std::shared_ptr<detail::zone> zone, std::vector<boost::uuids::uuid> out_connections)
+ {
+ if (!zone)
+ return;
+
+ assert(zone->strand.running_in_this_thread());
+ if (zone->map.update(std::move(out_connections)))
+ post(std::move(zone));
+ }
+
+ //! \pre Called within `zone_->strand`.
void operator()()
{
- if (!zone_)
+ run(std::move(zone_), std::move(out_connections_));
+ }
+ };
+
+ //! Checks fluff status for this node, and then does stem or fluff for txes
+ struct dandelionpp_notify
+ {
+ std::shared_ptr<detail::zone> zone_;
+ i_core_events* core_;
+ std::vector<blobdata> txs_;
+ boost::uuids::uuid source_;
+
+ //! \pre Called in `zone_->strand`
+ void operator()()
+ {
+ if (!zone_ || !core_ || txs_.empty())
return;
- assert(zone_->strand.running_in_this_thread());
- if (zone_->map.update(std::move(out_connections_)))
- post(std::move(zone_));
+ if (zone_->fluffing)
+ {
+ core_->on_transactions_relayed(epee::to_span(txs_), relay_method::fluff);
+ fluff_notify::run(std::move(zone_), epee::to_span(txs_), source_);
+ }
+ else // forward tx in stem
+ {
+ core_->on_transactions_relayed(epee::to_span(txs_), relay_method::stem);
+ for (int tries = 2; 0 < tries; tries--)
+ {
+ const boost::uuids::uuid destination = zone_->map.get_stem(source_);
+ if (!destination.is_nil() && make_payload_send_txs(*zone_->p2p, std::vector<blobdata>{txs_}, destination, zone_->pad_txs, false))
+ {
+ /* Source is intentionally omitted in debug log for privacy - a
+ nil uuid indicates source is that node. */
+ MDEBUG("Sent " << txs_.size() << " transaction(s) to " << destination << " using Dandelion++ stem");
+ return;
+ }
+
+ // connection list may be outdated, try again
+ update_channels::run(zone_, get_out_connections(*zone_->p2p));
+ }
+
+ MERROR("Unable to send transaction(s) via Dandelion++ stem");
+ }
}
};
- //! Swaps out noise channels entirely; new epoch start.
+ //! Swaps out noise/dandelionpp channels entirely; new epoch start.
class change_channels
{
std::shared_ptr<detail::zone> zone_;
net::dandelionpp::connection_map map_; // Requires manual copy constructor
+ bool fluffing_;
public:
- explicit change_channels(std::shared_ptr<detail::zone> zone, net::dandelionpp::connection_map map)
- : zone_(std::move(zone)), map_(std::move(map))
+ explicit change_channels(std::shared_ptr<detail::zone> zone, net::dandelionpp::connection_map map, const bool fluffing)
+ : zone_(std::move(zone)), map_(std::move(map)), fluffing_(fluffing)
{}
change_channels(change_channels&&) = default;
@@ -510,11 +569,15 @@ namespace levin
void operator()()
{
if (!zone_)
- return
+ return;
assert(zone_->strand.running_in_this_thread());
+ if (zone_->is_public)
+ MDEBUG("Starting new Dandelion++ epoch: " << (fluffing_ ? "fluff" : "stem"));
+
zone_->map = std::move(map_);
+ zone_->fluffing = fluffing_;
update_channels::post(std::move(zone_));
}
};
@@ -608,9 +671,10 @@ namespace levin
if (error && error != boost::system::errc::operation_canceled)
throw boost::system::system_error{error, "start_epoch timer failed"};
+ const bool fluffing = crypto::rand_idx(unsigned(100)) < CRYPTONOTE_DANDELIONPP_FLUFF_PROBABILITY;
const auto start = std::chrono::steady_clock::now();
zone_->strand.dispatch(
- change_channels{zone_, net::dandelionpp::connection_map{get_out_connections(*(zone_->p2p)), count_}}
+ change_channels{zone_, net::dandelionpp::connection_map{get_out_connections(*(zone_->p2p)), count_}, fluffing}
);
detail::zone& alias = *zone_;
@@ -626,10 +690,16 @@ namespace levin
if (!zone_->p2p)
throw std::logic_error{"cryptonote::levin::notify cannot have nullptr p2p argument"};
- if (!zone_->noise.empty())
+ const bool noise_enabled = !zone_->noise.empty();
+ if (noise_enabled || is_public)
{
const auto now = std::chrono::steady_clock::now();
- start_epoch{zone_, noise_min_epoch, noise_epoch_range, CRYPTONOTE_NOISE_CHANNELS}();
+ const auto min_epoch = noise_enabled ? noise_min_epoch : dandelionpp_min_epoch;
+ const auto epoch_range = noise_enabled ? noise_epoch_range : dandelionpp_epoch_range;
+ const std::size_t out_count = noise_enabled ? CRYPTONOTE_NOISE_CHANNELS : CRYPTONOTE_DANDELIONPP_STEMS;
+
+ start_epoch{zone_, min_epoch, epoch_range, out_count}();
+
for (std::size_t channel = 0; channel < zone_->channels.size(); ++channel)
send_noise::wait(now, zone_, channel);
}
@@ -679,7 +749,7 @@ namespace levin
zone_->flush_txs.cancel();
}
- bool notify::send_txs(std::vector<blobdata> txs, const boost::uuids::uuid& source)
+ bool notify::send_txs(std::vector<blobdata> txs, const boost::uuids::uuid& source, i_core_events& core, relay_method tx_relay)
{
if (txs.empty())
return true;
@@ -687,6 +757,17 @@ namespace levin
if (!zone_)
return false;
+ /* If noise is enabled in a zone, it always takes precedence. The technique
+ provides good protection against ISP adversaries, but not sybil
+ adversaries. Noise is currently only enabled over I2P/Tor - those
+ networks provide protection against sybil attacks (we only send to
+ outgoing connections).
+
+ If noise is disabled, Dandelion++ is used for public networks only.
+ Dandelion++ over I2P/Tor should be an interesting case to investigate,
+ but the mempool/stempool needs to know the zone a tx originated from to
+ work properly. */
+
if (!zone_->noise.empty() && !zone_->channels.empty())
{
// covert send in "noise" channel
@@ -694,8 +775,17 @@ namespace levin
CRYPTONOTE_MAX_FRAGMENTS * CRYPTONOTE_NOISE_BYTES <= LEVIN_DEFAULT_MAX_PACKET_SIZE, "most nodes will reject this fragment setting"
);
- // padding is not useful when using noise mode
- const std::string payload = make_tx_payload(std::move(txs), false);
+ if (tx_relay == relay_method::stem)
+ {
+ MWARNING("Dandelion++ stem not supported over noise networks");
+ tx_relay = relay_method::local; // do not put into stempool embargo (hopefully not there already!).
+ }
+
+ core.on_transactions_relayed(epee::to_span(txs), tx_relay);
+
+ // Padding is not useful when using noise mode. Send as stem so receiver
+ // forwards in Dandelion++ mode.
+ const std::string payload = make_tx_payload(std::move(txs), false, false);
epee::byte_slice message = epee::levin::make_fragmented_notify(
zone_->noise, NOTIFY_NEW_TRANSACTIONS::ID, epee::strspan<std::uint8_t>(payload)
);
@@ -714,9 +804,31 @@ namespace levin
}
else
{
- zone_->strand.dispatch(fluff_notify{zone_, std::move(txs), source});
+ switch (tx_relay)
+ {
+ default:
+ case relay_method::none:
+ case relay_method::block:
+ return false;
+ case relay_method::stem:
+ tx_relay = relay_method::fluff; // don't set stempool embargo when skipping to fluff
+ /* fallthrough */
+ case relay_method::local:
+ if (zone_->is_public)
+ {
+ // this will change a local tx to stem or fluff ...
+ zone_->strand.dispatch(
+ dandelionpp_notify{zone_, std::addressof(core), std::move(txs), source}
+ );
+ break;
+ }
+ /* fallthrough */
+ case relay_method::fluff:
+ core.on_transactions_relayed(epee::to_span(txs), tx_relay);
+ zone_->strand.dispatch(fluff_notify{zone_, std::move(txs), source});
+ break;
+ }
}
-
return true;
}
} // levin
diff --git a/src/cryptonote_protocol/levin_notify.h b/src/cryptonote_protocol/levin_notify.h
index ce652d933..641f1f956 100644
--- a/src/cryptonote_protocol/levin_notify.h
+++ b/src/cryptonote_protocol/levin_notify.h
@@ -35,6 +35,7 @@
#include "byte_slice.h"
#include "cryptonote_basic/blobdatatype.h"
+#include "cryptonote_protocol/enums.h"
#include "cryptonote_protocol/fwd.h"
#include "net/enums.h"
#include "span.h"
@@ -122,7 +123,7 @@ namespace levin
particular stem.
\return True iff the notification is queued for sending. */
- bool send_txs(std::vector<blobdata> txs, const boost::uuids::uuid& source);
+ bool send_txs(std::vector<blobdata> txs, const boost::uuids::uuid& source, i_core_events& core, relay_method tx_relay);
};
} // levin
} // net
diff --git a/src/daemon/rpc_command_executor.cpp b/src/daemon/rpc_command_executor.cpp
index 80b14d534..034d49918 100644
--- a/src/daemon/rpc_command_executor.cpp
+++ b/src/daemon/rpc_command_executor.cpp
@@ -695,11 +695,11 @@ bool t_rpc_command_executor::print_net_stats()
if (m_is_rpc)
{
- if (!m_rpc_client->json_rpc_request(net_stats_req, net_stats_res, "get_net_stats", fail_message.c_str()))
+ if (!m_rpc_client->rpc_request(net_stats_req, net_stats_res, "/get_net_stats", fail_message.c_str()))
{
return true;
}
- if (!m_rpc_client->json_rpc_request(limit_req, limit_res, "get_limit", fail_message.c_str()))
+ if (!m_rpc_client->rpc_request(limit_req, limit_res, "/get_limit", fail_message.c_str()))
{
return true;
}
diff --git a/src/p2p/net_node.h b/src/p2p/net_node.h
index 31d8aad3f..5337106dd 100644
--- a/src/p2p/net_node.h
+++ b/src/p2p/net_node.h
@@ -334,7 +334,7 @@ namespace nodetool
virtual void callback(p2p_connection_context& context);
//----------------- i_p2p_endpoint -------------------------------------------------------------
virtual bool relay_notify_to_list(int command, const epee::span<const uint8_t> data_buff, std::vector<std::pair<epee::net_utils::zone, boost::uuids::uuid>> connections);
- virtual epee::net_utils::zone send_txs(std::vector<cryptonote::blobdata> txs, const epee::net_utils::zone origin, const boost::uuids::uuid& source, cryptonote::i_core_events& core);
+ virtual epee::net_utils::zone send_txs(std::vector<cryptonote::blobdata> txs, const epee::net_utils::zone origin, const boost::uuids::uuid& source, cryptonote::i_core_events& core, cryptonote::relay_method tx_relay);
virtual bool invoke_command_to_peer(int command, const epee::span<const uint8_t> req_buff, std::string& resp_buff, const epee::net_utils::connection_context_base& context);
virtual bool invoke_notify_to_peer(int command, const epee::span<const uint8_t> req_buff, const epee::net_utils::connection_context_base& context);
virtual bool drop_connection(const epee::net_utils::connection_context_base& context);
diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl
index d9b46f459..4c253b673 100644
--- a/src/p2p/net_node.inl
+++ b/src/p2p/net_node.inl
@@ -628,6 +628,7 @@ namespace nodetool
full_addrs.insert("195.154.123.123:18080");
full_addrs.insert("212.83.172.165:18080");
full_addrs.insert("192.110.160.146:18080");
+ full_addrs.insert("88.198.163.90:18080");
}
return full_addrs;
}
@@ -1974,18 +1975,13 @@ namespace nodetool
}
//-----------------------------------------------------------------------------------
template<class t_payload_net_handler>
- epee::net_utils::zone node_server<t_payload_net_handler>::send_txs(std::vector<cryptonote::blobdata> txs, const epee::net_utils::zone origin, const boost::uuids::uuid& source, cryptonote::i_core_events& core)
+ epee::net_utils::zone node_server<t_payload_net_handler>::send_txs(std::vector<cryptonote::blobdata> txs, const epee::net_utils::zone origin, const boost::uuids::uuid& source, cryptonote::i_core_events& core, const cryptonote::relay_method tx_relay)
{
namespace enet = epee::net_utils;
- const auto send = [&txs, &source, &core] (std::pair<const enet::zone, network_zone>& network)
+ const auto send = [&txs, &source, &core, tx_relay] (std::pair<const enet::zone, network_zone>& network)
{
- const bool is_public = (network.first == enet::zone::public_);
- const cryptonote::relay_method tx_relay = is_public ?
- cryptonote::relay_method::fluff : cryptonote::relay_method::local;
-
- core.on_transactions_relayed(epee::to_span(txs), tx_relay);
- if (network.second.m_notifier.send_txs(std::move(txs), source))
+ if (network.second.m_notifier.send_txs(std::move(txs), source, core, tx_relay))
return network.first;
return enet::zone::invalid;
};
diff --git a/src/p2p/net_node_common.h b/src/p2p/net_node_common.h
index ed88aa28c..6a6100e0c 100644
--- a/src/p2p/net_node_common.h
+++ b/src/p2p/net_node_common.h
@@ -50,7 +50,7 @@ namespace nodetool
struct i_p2p_endpoint
{
virtual bool relay_notify_to_list(int command, const epee::span<const uint8_t> data_buff, std::vector<std::pair<epee::net_utils::zone, boost::uuids::uuid>> connections)=0;
- virtual epee::net_utils::zone send_txs(std::vector<cryptonote::blobdata> txs, const epee::net_utils::zone origin, const boost::uuids::uuid& source, cryptonote::i_core_events& core)=0;
+ virtual epee::net_utils::zone send_txs(std::vector<cryptonote::blobdata> txs, const epee::net_utils::zone origin, const boost::uuids::uuid& source, cryptonote::i_core_events& core, cryptonote::relay_method tx_relay)=0;
virtual bool invoke_command_to_peer(int command, const epee::span<const uint8_t> req_buff, std::string& resp_buff, const epee::net_utils::connection_context_base& context)=0;
virtual bool invoke_notify_to_peer(int command, const epee::span<const uint8_t> req_buff, const epee::net_utils::connection_context_base& context)=0;
virtual bool drop_connection(const epee::net_utils::connection_context_base& context)=0;
@@ -75,7 +75,7 @@ namespace nodetool
{
return false;
}
- virtual epee::net_utils::zone send_txs(std::vector<cryptonote::blobdata> txs, const epee::net_utils::zone origin, const boost::uuids::uuid& source, cryptonote::i_core_events& core)
+ virtual epee::net_utils::zone send_txs(std::vector<cryptonote::blobdata> txs, const epee::net_utils::zone origin, const boost::uuids::uuid& source, cryptonote::i_core_events& core, cryptonote::relay_method tx_relay)
{
return epee::net_utils::zone::invalid;
}
diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp
index f097c93fa..1fd0c037b 100644
--- a/src/rpc/core_rpc_server.cpp
+++ b/src/rpc/core_rpc_server.cpp
@@ -1152,7 +1152,7 @@ namespace cryptonote
return true;
}
- if(!tvc.m_should_be_relayed)
+ if(tvc.m_relay == relay_method::none)
{
LOG_PRINT_L0("[on_send_raw_tx]: tx accepted, but not relayed");
res.reason = "Not relayed";
@@ -1162,8 +1162,8 @@ namespace cryptonote
}
NOTIFY_NEW_TRANSACTIONS::request r;
- r.txs.push_back(tx_blob);
- m_core.get_protocol()->relay_transactions(r, boost::uuids::nil_uuid(), epee::net_utils::zone::invalid);
+ r.txs.push_back(std::move(tx_blob));
+ m_core.get_protocol()->relay_transactions(r, boost::uuids::nil_uuid(), epee::net_utils::zone::invalid, relay_method::local);
//TODO: make sure that tx has reached other nodes here, probably wait to receive reflections from other nodes
res.status = CORE_RPC_STATUS_OK;
return true;
@@ -2776,8 +2776,8 @@ namespace cryptonote
if (!m_core.get_pool_transaction(txid, txblob, relay_category::legacy))
{
NOTIFY_NEW_TRANSACTIONS::request r;
- r.txs.push_back(txblob);
- m_core.get_protocol()->relay_transactions(r, boost::uuids::nil_uuid(), epee::net_utils::zone::invalid);
+ r.txs.push_back(std::move(txblob));
+ m_core.get_protocol()->relay_transactions(r, boost::uuids::nil_uuid(), epee::net_utils::zone::invalid, relay_method::local);
//TODO: make sure that tx has reached other nodes here, probably wait to receive reflections from other nodes
}
else
diff --git a/src/rpc/daemon_handler.cpp b/src/rpc/daemon_handler.cpp
index 7292176b4..d05854e34 100644
--- a/src/rpc/daemon_handler.cpp
+++ b/src/rpc/daemon_handler.cpp
@@ -349,10 +349,10 @@ namespace rpc
res.error_details = "Invalid hex";
return;
}
- handleTxBlob(tx_blob, req.relay, res);
+ handleTxBlob(std::move(tx_blob), req.relay, res);
}
- void DaemonHandler::handleTxBlob(const std::string& tx_blob, bool relay, SendRawTx::Response& res)
+ void DaemonHandler::handleTxBlob(std::string&& tx_blob, bool relay, SendRawTx::Response& res)
{
if (!m_p2p.get_payload_object().is_synchronized())
{
@@ -423,7 +423,7 @@ namespace rpc
return;
}
- if(!tvc.m_should_be_relayed || !relay)
+ if(tvc.m_relay == relay_method::none || !relay)
{
MERROR("[SendRawTx]: tx accepted, but not relayed");
res.error_details = "Not relayed";
@@ -434,8 +434,8 @@ namespace rpc
}
NOTIFY_NEW_TRANSACTIONS::request r;
- r.txs.push_back(tx_blob);
- m_core.get_protocol()->relay_transactions(r, boost::uuids::nil_uuid(), epee::net_utils::zone::invalid);
+ r.txs.push_back(std::move(tx_blob));
+ m_core.get_protocol()->relay_transactions(r, boost::uuids::nil_uuid(), epee::net_utils::zone::invalid, relay_method::local);
//TODO: make sure that tx has reached other nodes here, probably wait to receive reflections from other nodes
res.status = Message::STATUS_OK;
diff --git a/src/rpc/daemon_handler.h b/src/rpc/daemon_handler.h
index c33f608ab..61eac17f0 100644
--- a/src/rpc/daemon_handler.h
+++ b/src/rpc/daemon_handler.h
@@ -138,7 +138,7 @@ class DaemonHandler : public RpcHandler
bool getBlockHeaderByHash(const crypto::hash& hash_in, cryptonote::rpc::BlockHeaderResponse& response);
- void handleTxBlob(const std::string& tx_blob, bool relay, SendRawTx::Response& res);
+ void handleTxBlob(std::string&& tx_blob, bool relay, SendRawTx::Response& res);
cryptonote::core& m_core;
t_p2p& m_p2p;
diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp
index 5efa54a19..d45ef3d7c 100644
--- a/src/simplewallet/simplewallet.cpp
+++ b/src/simplewallet/simplewallet.cpp
@@ -194,7 +194,7 @@ namespace
" account tag <tag_name> <account_index_1> [<account_index_2> ...]\n"
" account untag <account_index_1> [<account_index_2> ...]\n"
" account tag_description <tag_name> <description>");
- const char* USAGE_ADDRESS("address [ new <label text with white spaces allowed> | all | <index_min> [<index_max>] | label <index> <label text with white spaces allowed> | device [<index>]]");
+ const char* USAGE_ADDRESS("address [ new <label text with white spaces allowed> | all | <index_min> [<index_max>] | label <index> <label text with white spaces allowed> | device [<index>] | one-off <account> <subaddress>]");
const char* USAGE_INTEGRATED_ADDRESS("integrated_address [device] [<payment_id> | <address>]");
const char* USAGE_ADDRESS_BOOK("address_book [(add (<address>|<integrated address>) [<description possibly with whitespaces>])|(delete <index>)]");
const char* USAGE_SET_VARIABLE("set <option> [<value>]");
@@ -3181,7 +3181,7 @@ simple_wallet::simple_wallet()
m_cmd_binder.set_handler("address",
boost::bind(&simple_wallet::on_command, this, &simple_wallet::print_address, _1),
tr(USAGE_ADDRESS),
- tr("If no arguments are specified or <index> is specified, the wallet shows the default or specified address. If \"all\" is specified, the wallet shows all the existing addresses in the currently selected account. If \"new \" is specified, the wallet creates a new address with the provided label text (which can be empty). If \"label\" is specified, the wallet sets the label of the address specified by <index> to the provided label text."));
+ tr("If no arguments are specified or <index> is specified, the wallet shows the default or specified address. If \"all\" is specified, the wallet shows all the existing addresses in the currently selected account. If \"new \" is specified, the wallet creates a new address with the provided label text (which can be empty). If \"label\" is specified, the wallet sets the label of the address specified by <index> to the provided label text. If \"one-off\" is specified, the address for the specified index is generated and displayed, and remembered by the wallet"));
m_cmd_binder.set_handler("integrated_address",
boost::bind(&simple_wallet::on_command, this, &simple_wallet::print_integrated_address, _1),
tr(USAGE_INTEGRATED_ADDRESS),
@@ -5199,8 +5199,11 @@ void simple_wallet::check_background_mining(const epee::wipeable_string &passwor
if (is_background_mining_enabled)
{
// already active, nice
- m_wallet->setup_background_mining(tools::wallet2::BackgroundMiningYes);
- m_wallet->rewrite(m_wallet_file, password);
+ if (setup == tools::wallet2::BackgroundMiningMaybe)
+ {
+ m_wallet->setup_background_mining(tools::wallet2::BackgroundMiningYes);
+ m_wallet->rewrite(m_wallet_file, password);
+ }
start_background_mining();
return;
}
@@ -5223,6 +5226,11 @@ void simple_wallet::check_background_mining(const epee::wipeable_string &passwor
m_wallet->rewrite(m_wallet_file, password);
start_background_mining();
}
+ else
+ {
+ // the setting is already enabled, and the daemon is not mining yet, so start it
+ start_background_mining();
+ }
}
//----------------------------------------------------------------------------------------------------
bool simple_wallet::start_mining(const std::vector<std::string>& args)
@@ -9286,6 +9294,24 @@ bool simple_wallet::print_address(const std::vector<std::string> &args/* = std::
print_address_sub(m_wallet->get_num_subaddresses(m_current_subaddress_account) - 1);
m_wallet->device_show_address(m_current_subaddress_account, m_wallet->get_num_subaddresses(m_current_subaddress_account) - 1, boost::none);
}
+ else if (local_args[0] == "one-off")
+ {
+ local_args.erase(local_args.begin());
+ std::string label;
+ if (local_args.size() != 2)
+ {
+ fail_msg_writer() << tr("Expected exactly two arguments for index");
+ return true;
+ }
+ uint32_t major, minor;
+ if (!epee::string_tools::get_xtype_from_string(major, local_args[0]) || !epee::string_tools::get_xtype_from_string(minor, local_args[1]))
+ {
+ fail_msg_writer() << tr("failed to parse index: ") << local_args[0] << " " << local_args[1];
+ return true;
+ }
+ m_wallet->create_one_off_subaddress({major, minor});
+ success_msg_writer() << boost::format(tr("Address at %u %u: %s")) % major % minor % m_wallet->get_subaddress_as_str({major, minor});
+ }
else if (local_args.size() >= 2 && local_args[0] == "label")
{
if (!epee::string_tools::get_xtype_from_string(index, local_args[1]))
diff --git a/src/wallet/api/wallet2_api.h b/src/wallet/api/wallet2_api.h
index 09c64106e..6309724a4 100644
--- a/src/wallet/api/wallet2_api.h
+++ b/src/wallet/api/wallet2_api.h
@@ -1293,7 +1293,11 @@ struct WalletManager
virtual std::string resolveOpenAlias(const std::string &address, bool &dnssec_valid) const = 0;
//! checks for an update and returns version, hash and url
- static std::tuple<bool, std::string, std::string, std::string, std::string> checkUpdates(const std::string &software, std::string subdir);
+ static std::tuple<bool, std::string, std::string, std::string, std::string> checkUpdates(
+ const std::string &software,
+ std::string subdir,
+ const char *buildtag = nullptr,
+ const char *current_version = nullptr);
};
diff --git a/src/wallet/api/wallet_manager.cpp b/src/wallet/api/wallet_manager.cpp
index 44a184304..8d7541cea 100644
--- a/src/wallet/api/wallet_manager.cpp
+++ b/src/wallet/api/wallet_manager.cpp
@@ -342,22 +342,30 @@ std::string WalletManagerImpl::resolveOpenAlias(const std::string &address, bool
return addresses.front();
}
-std::tuple<bool, std::string, std::string, std::string, std::string> WalletManager::checkUpdates(const std::string &software, std::string subdir)
+std::tuple<bool, std::string, std::string, std::string, std::string> WalletManager::checkUpdates(
+ const std::string &software,
+ std::string subdir,
+ const char *buildtag/* = nullptr*/,
+ const char *current_version/* = nullptr*/)
{
+ if (buildtag == nullptr)
+ {
#ifdef BUILD_TAG
- static const char buildtag[] = BOOST_PP_STRINGIZE(BUILD_TAG);
+ static const char buildtag_default[] = BOOST_PP_STRINGIZE(BUILD_TAG);
#else
- static const char buildtag[] = "source";
- // Override the subdir string when built from source
- subdir = "source";
+ static const char buildtag_default[] = "source";
+ // Override the subdir string when built from source
+ subdir = "source";
#endif
+ buildtag = buildtag_default;
+ }
std::string version, hash;
MDEBUG("Checking for a new " << software << " version for " << buildtag);
if (!tools::check_updates(software, buildtag, version, hash))
return std::make_tuple(false, "", "", "", "");
- if (tools::vercmp(version.c_str(), MONERO_VERSION) > 0)
+ if (tools::vercmp(version.c_str(), current_version != nullptr ? current_version : MONERO_VERSION) > 0)
{
std::string user_url = tools::get_update_url(software, subdir, buildtag, version, true);
std::string auto_url = tools::get_update_url(software, subdir, buildtag, version, false);
diff --git a/src/wallet/message_store.cpp b/src/wallet/message_store.cpp
index 6e2cb933f..1bd462ef5 100644
--- a/src/wallet/message_store.cpp
+++ b/src/wallet/message_store.cpp
@@ -48,7 +48,7 @@
namespace mms
{
-message_store::message_store()
+message_store::message_store(std::unique_ptr<epee::net_utils::http::abstract_http_client> http_client) : m_transporter(std::move(http_client))
{
m_active = false;
m_auto_send = false;
diff --git a/src/wallet/message_store.h b/src/wallet/message_store.h
index 637bd29a1..d40daf186 100644
--- a/src/wallet/message_store.h
+++ b/src/wallet/message_store.h
@@ -43,6 +43,7 @@
#include "common/i18n.h"
#include "common/command_line.h"
#include "wipeable_string.h"
+#include "net/abstract_http_client.h"
#include "message_transporter.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
@@ -202,7 +203,8 @@ namespace mms
class message_store
{
public:
- message_store();
+ message_store(std::unique_ptr<epee::net_utils::http::abstract_http_client> http_client);
+
// Initialize and start to use the MMS, set the first signer, this wallet itself
// Filename, if not null and not empty, is used to create the ".mms" file
// reset it if already used, with deletion of all signers and messages
diff --git a/src/wallet/message_transporter.cpp b/src/wallet/message_transporter.cpp
index cf9b45b37..4dd4b8f01 100644
--- a/src/wallet/message_transporter.cpp
+++ b/src/wallet/message_transporter.cpp
@@ -80,7 +80,7 @@ namespace bitmessage_rpc
}
-message_transporter::message_transporter()
+message_transporter::message_transporter(std::unique_ptr<epee::net_utils::http::abstract_http_client> http_client) : m_http_client(std::move(http_client))
{
m_run = true;
}
@@ -96,7 +96,7 @@ void message_transporter::set_options(const std::string &bitmessage_address, con
}
m_bitmessage_login = bitmessage_login;
- m_http_client.set_server(address_parts.host, std::to_string(address_parts.port), boost::none);
+ m_http_client->set_server(address_parts.host, std::to_string(address_parts.port), boost::none);
}
bool message_transporter::receive_messages(const std::vector<std::string> &destination_transport_addresses,
@@ -256,7 +256,7 @@ bool message_transporter::post_request(const std::string &request, std::string &
additional_params.push_back(std::make_pair("Content-Type", "application/xml; charset=utf-8"));
const epee::net_utils::http::http_response_info* response = NULL;
std::chrono::milliseconds timeout = std::chrono::seconds(15);
- bool r = m_http_client.invoke("/", "POST", request, timeout, std::addressof(response), std::move(additional_params));
+ bool r = m_http_client->invoke("/", "POST", request, timeout, std::addressof(response), std::move(additional_params));
if (r)
{
answer = response->m_body;
@@ -266,7 +266,7 @@ bool message_transporter::post_request(const std::string &request, std::string &
LOG_ERROR("POST request to Bitmessage failed: " << request.substr(0, 300));
THROW_WALLET_EXCEPTION(tools::error::no_connection_to_bitmessage, m_bitmessage_url);
}
- m_http_client.disconnect(); // see comment above
+ m_http_client->disconnect(); // see comment above
std::string string_value = get_str_between_tags(answer, "<string>", "</string>");
if ((string_value.find("API Error") == 0) || (string_value.find("RPC ") == 0))
{
diff --git a/src/wallet/message_transporter.h b/src/wallet/message_transporter.h
index 28c099d87..84a2e9bae 100644
--- a/src/wallet/message_transporter.h
+++ b/src/wallet/message_transporter.h
@@ -34,9 +34,9 @@
#include "cryptonote_basic/cryptonote_basic.h"
#include "net/http_server_impl_base.h"
#include "net/http_client.h"
+#include "net/abstract_http_client.h"
#include "common/util.h"
#include "wipeable_string.h"
-#include "serialization/keyvalue_serialization.h"
#include <vector>
namespace mms
@@ -83,7 +83,7 @@ typedef epee::misc_utils::struct_init<transport_message_t> transport_message;
class message_transporter
{
public:
- message_transporter();
+ message_transporter(std::unique_ptr<epee::net_utils::http::abstract_http_client> http_client);
void set_options(const std::string &bitmessage_address, const epee::wipeable_string &bitmessage_login);
bool send_message(const transport_message &message);
bool receive_messages(const std::vector<std::string> &destination_transport_addresses,
@@ -94,7 +94,7 @@ public:
bool delete_transport_address(const std::string &transport_address);
private:
- epee::net_utils::http::http_simple_client m_http_client;
+ const std::unique_ptr<epee::net_utils::http::abstract_http_client> m_http_client;
std::string m_bitmessage_url;
epee::wipeable_string m_bitmessage_login;
std::atomic<bool> m_run;
diff --git a/src/wallet/node_rpc_proxy.cpp b/src/wallet/node_rpc_proxy.cpp
index f3698b599..873c2ee51 100644
--- a/src/wallet/node_rpc_proxy.cpp
+++ b/src/wallet/node_rpc_proxy.cpp
@@ -51,7 +51,7 @@ namespace tools
static const std::chrono::seconds rpc_timeout = std::chrono::minutes(3) + std::chrono::seconds(30);
-NodeRPCProxy::NodeRPCProxy(epee::net_utils::http::http_simple_client &http_client, rpc_payment_state_t &rpc_payment_state, boost::recursive_mutex &mutex)
+NodeRPCProxy::NodeRPCProxy(epee::net_utils::http::abstract_http_client &http_client, rpc_payment_state_t &rpc_payment_state, boost::recursive_mutex &mutex)
: m_http_client(http_client)
, m_rpc_payment_state(rpc_payment_state)
, m_daemon_rpc_mutex(mutex)
diff --git a/src/wallet/node_rpc_proxy.h b/src/wallet/node_rpc_proxy.h
index 65ca40640..b053659e9 100644
--- a/src/wallet/node_rpc_proxy.h
+++ b/src/wallet/node_rpc_proxy.h
@@ -31,7 +31,7 @@
#include <string>
#include <boost/thread/mutex.hpp>
#include "include_base_utils.h"
-#include "net/http_client.h"
+#include "net/abstract_http_client.h"
#include "rpc/core_rpc_server_commands_defs.h"
#include "wallet_rpc_helpers.h"
@@ -41,7 +41,7 @@ namespace tools
class NodeRPCProxy
{
public:
- NodeRPCProxy(epee::net_utils::http::http_simple_client &http_client, rpc_payment_state_t &rpc_payment_state, boost::recursive_mutex &mutex);
+ NodeRPCProxy(epee::net_utils::http::abstract_http_client &http_client, rpc_payment_state_t &rpc_payment_state, boost::recursive_mutex &mutex);
void set_client_secret_key(const crypto::secret_key &skey) { m_client_id_secret_key = skey; }
void invalidate();
@@ -72,7 +72,7 @@ private:
private:
boost::optional<std::string> get_info();
- epee::net_utils::http::http_simple_client &m_http_client;
+ epee::net_utils::http::abstract_http_client &m_http_client;
rpc_payment_state_t &m_rpc_payment_state;
boost::recursive_mutex &m_daemon_rpc_mutex;
crypto::secret_key m_client_id_secret_key;
diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp
index f05a67315..4220f18be 100644
--- a/src/wallet/wallet2.cpp
+++ b/src/wallet/wallet2.cpp
@@ -1122,7 +1122,8 @@ void wallet_device_callback::on_progress(const hw::device_progress& event)
wallet->on_device_progress(event);
}
-wallet2::wallet2(network_type nettype, uint64_t kdf_rounds, bool unattended):
+wallet2::wallet2(network_type nettype, uint64_t kdf_rounds, bool unattended, std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory):
+ m_http_client(std::move(http_client_factory->create())),
m_multisig_rescan_info(NULL),
m_multisig_rescan_k(NULL),
m_upper_transaction_weight_limit(0),
@@ -1167,7 +1168,7 @@ wallet2::wallet2(network_type nettype, uint64_t kdf_rounds, bool unattended):
m_watch_only(false),
m_multisig(false),
m_multisig_threshold(0),
- m_node_rpc_proxy(m_http_client, m_rpc_payment_state, m_daemon_rpc_mutex),
+ m_node_rpc_proxy(*m_http_client, m_rpc_payment_state, m_daemon_rpc_mutex),
m_account_public_address{crypto::null_pkey, crypto::null_pkey},
m_subaddress_lookahead_major(SUBADDRESS_LOOKAHEAD_MAJOR),
m_subaddress_lookahead_minor(SUBADDRESS_LOOKAHEAD_MINOR),
@@ -1178,7 +1179,7 @@ wallet2::wallet2(network_type nettype, uint64_t kdf_rounds, bool unattended):
m_light_wallet_balance(0),
m_light_wallet_unlocked_balance(0),
m_original_keys_available(false),
- m_message_store(),
+ m_message_store(http_client_factory->create()),
m_key_device_type(hw::device::device_type::SOFTWARE),
m_ring_history_saved(false),
m_ringdb(),
@@ -1298,8 +1299,8 @@ bool wallet2::set_daemon(std::string daemon_address, boost::optional<epee::net_u
{
boost::lock_guard<boost::recursive_mutex> lock(m_daemon_rpc_mutex);
- if(m_http_client.is_connected())
- m_http_client.disconnect();
+ if(m_http_client->is_connected())
+ m_http_client->disconnect();
const bool changed = m_daemon_address != daemon_address;
m_daemon_address = std::move(daemon_address);
m_daemon_login = std::move(daemon_login);
@@ -1313,7 +1314,7 @@ bool wallet2::set_daemon(std::string daemon_address, boost::optional<epee::net_u
const std::string address = get_daemon_address();
MINFO("setting daemon to " << address);
- bool ret = m_http_client.set_server(address, get_daemon_login(), std::move(ssl_options));
+ bool ret = m_http_client->set_server(address, get_daemon_login(), std::move(ssl_options));
if (ret)
{
CRITICAL_REGION_LOCAL(default_daemon_address_lock);
@@ -1328,7 +1329,12 @@ bool wallet2::init(std::string daemon_address, boost::optional<epee::net_utils::
m_is_initialized = true;
m_upper_transaction_weight_limit = upper_transaction_weight_limit;
if (proxy != boost::asio::ip::tcp::endpoint{})
- m_http_client.set_connector(net::socks::connector{std::move(proxy)});
+ {
+ epee::net_utils::http::abstract_http_client* abstract_http_client = m_http_client.get();
+ epee::net_utils::http::http_simple_client* http_simple_client = dynamic_cast<epee::net_utils::http::http_simple_client*>(abstract_http_client);
+ CHECK_AND_ASSERT_MES(http_simple_client != nullptr, false, "http_simple_client must be used to set proxy");
+ http_simple_client->set_connector(net::socks::connector{std::move(proxy)});
+ }
return set_daemon(daemon_address, daemon_login, trusted_daemon, std::move(ssl_options));
}
//----------------------------------------------------------------------------------------------------
@@ -1553,6 +1559,12 @@ void wallet2::expand_subaddresses(const cryptonote::subaddress_index& index)
}
}
//----------------------------------------------------------------------------------------------------
+void wallet2::create_one_off_subaddress(const cryptonote::subaddress_index& index)
+{
+ const crypto::public_key pkey = get_subaddress_spend_public_key(index);
+ m_subaddresses[pkey] = index;
+}
+//----------------------------------------------------------------------------------------------------
std::string wallet2::get_subaddress_label(const cryptonote::subaddress_index& index) const
{
if (index.major >= m_subaddress_labels.size() || index.minor >= m_subaddress_labels[index.major].size())
@@ -2094,7 +2106,8 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote
td.m_amount = amount;
td.m_pk_index = pk_index - 1;
td.m_subaddr_index = tx_scan_info[o].received->index;
- expand_subaddresses(tx_scan_info[o].received->index);
+ if (tx_scan_info[o].received->index.major < m_subaddress_labels.size() && tx_scan_info[o].received->index.minor < m_subaddress_labels[tx_scan_info[o].received->index.major].size())
+ expand_subaddresses(tx_scan_info[o].received->index);
if (tx.vout[o].amount == 0)
{
td.m_mask = tx_scan_info[o].mask;
@@ -2172,7 +2185,8 @@ void wallet2::process_new_transaction(const crypto::hash &txid, const cryptonote
td.m_amount = amount;
td.m_pk_index = pk_index - 1;
td.m_subaddr_index = tx_scan_info[o].received->index;
- expand_subaddresses(tx_scan_info[o].received->index);
+ if (tx_scan_info[o].received->index.major < m_subaddress_labels.size() && tx_scan_info[o].received->index.minor < m_subaddress_labels[tx_scan_info[o].received->index.major].size())
+ expand_subaddresses(tx_scan_info[o].received->index);
if (tx.vout[o].amount == 0)
{
td.m_mask = tx_scan_info[o].mask;
@@ -2585,7 +2599,7 @@ void wallet2::pull_blocks(uint64_t start_height, uint64_t &blocks_start_height,
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- bool r = net_utils::invoke_http_bin("/getblocks.bin", req, res, m_http_client, rpc_timeout);
+ bool r = net_utils::invoke_http_bin("/getblocks.bin", req, res, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR(r, {}, res, "getblocks.bin", error::get_blocks_error, get_rpc_status(res.status));
THROW_WALLET_EXCEPTION_IF(res.blocks.size() != res.output_indices.size(), error::wallet_internal_error,
"mismatched blocks (" + boost::lexical_cast<std::string>(res.blocks.size()) + ") and output_indices (" +
@@ -2614,7 +2628,7 @@ void wallet2::pull_hashes(uint64_t start_height, uint64_t &blocks_start_height,
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
req.client = get_client_signature();
uint64_t pre_call_credits = m_rpc_payment_state.credits;
- bool r = net_utils::invoke_http_bin("/gethashes.bin", req, res, m_http_client, rpc_timeout);
+ bool r = net_utils::invoke_http_bin("/gethashes.bin", req, res, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR(r, {}, res, "gethashes.bin", error::get_hashes_error, get_rpc_status(res.status));
check_rpc_cost("/gethashes.bin", res.credits, pre_call_credits, 1 + res.m_block_ids.size() * COST_PER_BLOCK_HASH);
}
@@ -2899,7 +2913,7 @@ void wallet2::update_pool_state(std::vector<std::tuple<cryptonote::transaction,
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- bool r = epee::net_utils::invoke_http_json("/get_transaction_pool_hashes.bin", req, res, m_http_client, rpc_timeout);
+ bool r = epee::net_utils::invoke_http_json("/get_transaction_pool_hashes.bin", req, res, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR(r, {}, res, "get_transaction_pool_hashes.bin", error::get_tx_pool_error);
check_rpc_cost("/get_transaction_pool_hashes.bin", res.credits, pre_call_credits, 1 + res.tx_hashes.size() * COST_PER_POOL_HASH);
}
@@ -3044,7 +3058,7 @@ void wallet2::update_pool_state(std::vector<std::tuple<cryptonote::transaction,
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- r = epee::net_utils::invoke_http_json("/gettransactions", req, res, m_http_client, rpc_timeout);
+ r = epee::net_utils::invoke_http_json("/gettransactions", req, res, *m_http_client, rpc_timeout);
if (r && res.status == CORE_RPC_STATUS_OK)
check_rpc_cost("/gettransactions", res.credits, pre_call_credits, res.txs.size() * COST_PER_TX);
}
@@ -3530,7 +3544,7 @@ bool wallet2::get_rct_distribution(uint64_t &start_height, std::vector<uint64_t>
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- r = net_utils::invoke_http_bin("/get_output_distribution.bin", req, res, m_http_client, rpc_timeout);
+ r = net_utils::invoke_http_bin("/get_output_distribution.bin", req, res, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR_GENERIC(r, {}, res, "/get_output_distribution.bin");
check_rpc_cost("/get_output_distribution.bin", res.credits, pre_call_credits, COST_PER_OUTPUT_DISTRIBUTION_0);
}
@@ -3689,6 +3703,30 @@ void wallet2::clear_soft(bool keep_key_images)
*/
bool wallet2::store_keys(const std::string& keys_file_name, const epee::wipeable_string& password, bool watch_only)
{
+ boost::optional<wallet2::keys_file_data> keys_file_data = get_keys_file_data(password, watch_only);
+ CHECK_AND_ASSERT_MES(keys_file_data != boost::none, false, "failed to generate wallet keys data");
+
+ std::string tmp_file_name = keys_file_name + ".new";
+ std::string buf;
+ bool r = ::serialization::dump_binary(keys_file_data.get(), buf);
+ r = r && save_to_file(tmp_file_name, buf);
+ CHECK_AND_ASSERT_MES(r, false, "failed to generate wallet keys file " << tmp_file_name);
+
+ unlock_keys_file();
+ std::error_code e = tools::replace_file(tmp_file_name, keys_file_name);
+ lock_keys_file();
+
+ if (e) {
+ boost::filesystem::remove(tmp_file_name);
+ LOG_ERROR("failed to update wallet keys file " << keys_file_name);
+ return false;
+ }
+
+ return true;
+}
+//----------------------------------------------------------------------------------------------------
+boost::optional<wallet2::keys_file_data> wallet2::get_keys_file_data(const epee::wipeable_string& password, bool watch_only)
+{
std::string account_data;
std::string multisig_signers;
std::string multisig_derivations;
@@ -3709,8 +3747,8 @@ bool wallet2::store_keys(const std::string& keys_file_name, const epee::wipeable
account.encrypt_keys(key);
bool r = epee::serialization::store_t_to_binary(account, account_data);
- CHECK_AND_ASSERT_MES(r, false, "failed to serialize wallet keys");
- wallet2::keys_file_data keys_file_data = {};
+ CHECK_AND_ASSERT_MES(r, boost::none, "failed to serialize wallet keys");
+ boost::optional<wallet2::keys_file_data> keys_file_data = (wallet2::keys_file_data) {};
// Create a JSON object with "key_data" and "seed_language" as keys.
rapidjson::Document json;
@@ -3741,12 +3779,12 @@ bool wallet2::store_keys(const std::string& keys_file_name, const epee::wipeable
if (m_multisig)
{
bool r = ::serialization::dump_binary(m_multisig_signers, multisig_signers);
- CHECK_AND_ASSERT_MES(r, false, "failed to serialize wallet multisig signers");
+ CHECK_AND_ASSERT_MES(r, boost::none, "failed to serialize wallet multisig signers");
value.SetString(multisig_signers.c_str(), multisig_signers.length());
json.AddMember("multisig_signers", value, json.GetAllocator());
r = ::serialization::dump_binary(m_multisig_derivations, multisig_derivations);
- CHECK_AND_ASSERT_MES(r, false, "failed to serialize wallet multisig derivations");
+ CHECK_AND_ASSERT_MES(r, boost::none, "failed to serialize wallet multisig derivations");
value.SetString(multisig_derivations.c_str(), multisig_derivations.length());
json.AddMember("multisig_derivations", value, json.GetAllocator());
@@ -3889,27 +3927,10 @@ bool wallet2::store_keys(const std::string& keys_file_name, const epee::wipeable
// Encrypt the entire JSON object.
std::string cipher;
cipher.resize(account_data.size());
- keys_file_data.iv = crypto::rand<crypto::chacha_iv>();
- crypto::chacha20(account_data.data(), account_data.size(), key, keys_file_data.iv, &cipher[0]);
- keys_file_data.account_data = cipher;
-
- std::string tmp_file_name = keys_file_name + ".new";
- std::string buf;
- r = ::serialization::dump_binary(keys_file_data, buf);
- r = r && save_to_file(tmp_file_name, buf);
- CHECK_AND_ASSERT_MES(r, false, "failed to generate wallet keys file " << tmp_file_name);
-
- unlock_keys_file();
- std::error_code e = tools::replace_file(tmp_file_name, keys_file_name);
- lock_keys_file();
-
- if (e) {
- boost::filesystem::remove(tmp_file_name);
- LOG_ERROR("failed to update wallet keys file " << keys_file_name);
- return false;
- }
-
- return true;
+ keys_file_data.get().iv = crypto::rand<crypto::chacha_iv>();
+ crypto::chacha20(account_data.data(), account_data.size(), key, keys_file_data.get().iv, &cipher[0]);
+ keys_file_data.get().account_data = cipher;
+ return keys_file_data;
}
//----------------------------------------------------------------------------------------------------
void wallet2::setup_keys(const epee::wipeable_string &password)
@@ -3949,16 +3970,51 @@ void wallet2::change_password(const std::string &filename, const epee::wipeable_
*/
bool wallet2::load_keys(const std::string& keys_file_name, const epee::wipeable_string& password)
{
- rapidjson::Document json;
- wallet2::keys_file_data keys_file_data;
- std::string buf;
- bool encrypted_secret_keys = false;
- bool r = load_from_file(keys_file_name, buf);
+ std::string keys_file_buf;
+ bool r = load_from_file(keys_file_name, keys_file_buf);
THROW_WALLET_EXCEPTION_IF(!r, error::file_read_error, keys_file_name);
+ // Load keys from buffer
+ boost::optional<crypto::chacha_key> keys_to_encrypt;
+ try {
+ r = wallet2::load_keys_buf(keys_file_buf, password, keys_to_encrypt);
+ } catch (const std::exception& e) {
+ std::size_t found = string(e.what()).find("failed to deserialize keys buffer");
+ THROW_WALLET_EXCEPTION_IF(found != std::string::npos, error::wallet_internal_error, "internal error: failed to deserialize \"" + keys_file_name + '\"');
+ throw e;
+ }
+
+ // Rewrite with encrypted keys if unencrypted, ignore errors
+ if (r && keys_to_encrypt != boost::none)
+ {
+ if (m_ask_password == AskPasswordToDecrypt && !m_unattended && !m_watch_only)
+ encrypt_keys(keys_to_encrypt.get());
+ bool saved_ret = store_keys(keys_file_name, password, m_watch_only);
+ if (!saved_ret)
+ {
+ // just moan a bit, but not fatal
+ MERROR("Error saving keys file with encrypted keys, not fatal");
+ }
+ if (m_ask_password == AskPasswordToDecrypt && !m_unattended && !m_watch_only)
+ decrypt_keys(keys_to_encrypt.get());
+ m_keys_file_locker.reset();
+ }
+ return r;
+}
+//----------------------------------------------------------------------------------------------------
+bool wallet2::load_keys_buf(const std::string& keys_buf, const epee::wipeable_string& password) {
+ boost::optional<crypto::chacha_key> keys_to_encrypt;
+ return wallet2::load_keys_buf(keys_buf, password, keys_to_encrypt);
+}
+//----------------------------------------------------------------------------------------------------
+bool wallet2::load_keys_buf(const std::string& keys_buf, const epee::wipeable_string& password, boost::optional<crypto::chacha_key>& keys_to_encrypt) {
+
// Decrypt the contents
- r = ::serialization::parse_binary(buf, keys_file_data);
- THROW_WALLET_EXCEPTION_IF(!r, error::wallet_internal_error, "internal error: failed to deserialize \"" + keys_file_name + '\"');
+ rapidjson::Document json;
+ wallet2::keys_file_data keys_file_data;
+ bool encrypted_secret_keys = false;
+ bool r = ::serialization::parse_binary(keys_buf, keys_file_data);
+ THROW_WALLET_EXCEPTION_IF(!r, error::wallet_internal_error, "internal error: failed to deserialize keys buffer");
crypto::chacha_key key;
crypto::generate_chacha_key(password.data(), password.size(), key, m_kdf_rounds);
std::string account_data;
@@ -4242,8 +4298,8 @@ bool wallet2::load_keys(const std::string& keys_file_name, const epee::wipeable_
}
else
{
- THROW_WALLET_EXCEPTION(error::wallet_internal_error, "invalid password");
- return false;
+ THROW_WALLET_EXCEPTION(error::wallet_internal_error, "invalid password");
+ return false;
}
r = epee::serialization::load_t_from_binary(m_account, account_data);
@@ -4277,24 +4333,13 @@ bool wallet2::load_keys(const std::string& keys_file_name, const epee::wipeable_
}
else
{
- // rewrite with encrypted keys, ignore errors
- if (m_ask_password == AskPasswordToDecrypt && !m_unattended && !m_watch_only)
- encrypt_keys(key);
- bool saved_ret = store_keys(keys_file_name, password, m_watch_only);
- if (!saved_ret)
- {
- // just moan a bit, but not fatal
- MERROR("Error saving keys file with encrypted keys, not fatal");
- }
- if (m_ask_password == AskPasswordToDecrypt && !m_unattended && !m_watch_only)
- decrypt_keys(key);
- m_keys_file_locker.reset();
+ keys_to_encrypt = key;
}
}
const cryptonote::account_keys& keys = m_account.get_keys();
hw::device &hwdev = m_account.get_device();
r = r && hwdev.verify_keys(keys.m_view_secret_key, keys.m_account_address.m_view_public_key);
- if(!m_watch_only && !m_multisig && hwdev.device_protocol() != hw::device::PROTOCOL_COLD)
+ if (!m_watch_only && !m_multisig && hwdev.device_protocol() != hw::device::PROTOCOL_COLD)
r = r && hwdev.verify_keys(keys.m_spend_secret_key, keys.m_account_address.m_spend_public_key);
THROW_WALLET_EXCEPTION_IF(!r, error::wallet_files_doesnt_correspond, m_keys_file, m_wallet_file);
@@ -4913,7 +4958,8 @@ std::string wallet2::make_multisig(const epee::wipeable_string &password,
// re-encrypt keys
keys_reencryptor = epee::misc_utils::auto_scope_leave_caller();
- create_keys_file(m_wallet_file, false, password, boost::filesystem::exists(m_wallet_file + ".address.txt"));
+ if (!m_wallet_file.empty())
+ create_keys_file(m_wallet_file, false, password, boost::filesystem::exists(m_wallet_file + ".address.txt"));
setup_new_blockchain();
@@ -5053,7 +5099,9 @@ std::string wallet2::exchange_multisig_keys(const epee::wipeable_string &passwor
++m_multisig_rounds_passed;
- create_keys_file(m_wallet_file, false, password, boost::filesystem::exists(m_wallet_file + ".address.txt"));
+ if (!m_wallet_file.empty())
+ create_keys_file(m_wallet_file, false, password, boost::filesystem::exists(m_wallet_file + ".address.txt"));
+
return extra_multisig_info;
}
@@ -5427,13 +5475,13 @@ bool wallet2::check_connection(uint32_t *version, bool *ssl, uint32_t timeout)
{
boost::lock_guard<boost::recursive_mutex> lock(m_daemon_rpc_mutex);
- if(!m_http_client.is_connected(ssl))
+ if(!m_http_client->is_connected(ssl))
{
m_rpc_version = 0;
m_node_rpc_proxy.invalidate();
- if (!m_http_client.connect(std::chrono::milliseconds(timeout)))
+ if (!m_http_client->connect(std::chrono::milliseconds(timeout)))
return false;
- if(!m_http_client.is_connected(ssl))
+ if(!m_http_client->is_connected(ssl))
return false;
}
}
@@ -5461,12 +5509,12 @@ void wallet2::set_offline(bool offline)
{
m_offline = offline;
m_node_rpc_proxy.set_offline(offline);
- m_http_client.set_auto_connect(!offline);
+ m_http_client->set_auto_connect(!offline);
if (offline)
{
boost::lock_guard<boost::recursive_mutex> lock(m_daemon_rpc_mutex);
- if(m_http_client.is_connected())
- m_http_client.disconnect();
+ if(m_http_client->is_connected())
+ m_http_client->disconnect();
}
}
//----------------------------------------------------------------------------------------------------
@@ -5481,48 +5529,63 @@ void wallet2::generate_chacha_key_from_password(const epee::wipeable_string &pas
crypto::generate_chacha_key(pass.data(), pass.size(), key, m_kdf_rounds);
}
//----------------------------------------------------------------------------------------------------
-void wallet2::load(const std::string& wallet_, const epee::wipeable_string& password)
+void wallet2::load(const std::string& wallet_, const epee::wipeable_string& password, const std::string& keys_buf, const std::string& cache_buf)
{
clear();
prepare_file_names(wallet_);
+ // determine if loading from file system or string buffer
+ bool use_fs = !wallet_.empty();
+ THROW_WALLET_EXCEPTION_IF((use_fs && !keys_buf.empty()) || (!use_fs && keys_buf.empty()), error::file_read_error, "must load keys either from file system or from buffer");\
+
boost::system::error_code e;
- bool exists = boost::filesystem::exists(m_keys_file, e);
- THROW_WALLET_EXCEPTION_IF(e || !exists, error::file_not_found, m_keys_file);
- lock_keys_file();
- THROW_WALLET_EXCEPTION_IF(!is_keys_file_locked(), error::wallet_internal_error, "internal error: \"" + m_keys_file + "\" is opened by another wallet program");
+ if (use_fs)
+ {
+ bool exists = boost::filesystem::exists(m_keys_file, e);
+ THROW_WALLET_EXCEPTION_IF(e || !exists, error::file_not_found, m_keys_file);
+ lock_keys_file();
+ THROW_WALLET_EXCEPTION_IF(!is_keys_file_locked(), error::wallet_internal_error, "internal error: \"" + m_keys_file + "\" is opened by another wallet program");
- // this temporary unlocking is necessary for Windows (otherwise the file couldn't be loaded).
- unlock_keys_file();
- if (!load_keys(m_keys_file, password))
+ // this temporary unlocking is necessary for Windows (otherwise the file couldn't be loaded).
+ unlock_keys_file();
+ if (!load_keys(m_keys_file, password))
+ {
+ THROW_WALLET_EXCEPTION_IF(true, error::file_read_error, m_keys_file);
+ }
+ LOG_PRINT_L0("Loaded wallet keys file, with public address: " << m_account.get_public_address_str(m_nettype));
+ lock_keys_file();
+ }
+ else if (!load_keys_buf(keys_buf, password))
{
- THROW_WALLET_EXCEPTION_IF(true, error::file_read_error, m_keys_file);
+ THROW_WALLET_EXCEPTION_IF(true, error::file_read_error, "failed to load keys from buffer");
}
- LOG_PRINT_L0("Loaded wallet keys file, with public address: " << m_account.get_public_address_str(m_nettype));
- lock_keys_file();
wallet_keys_unlocker unlocker(*this, m_ask_password == AskPasswordToDecrypt && !m_unattended && !m_watch_only, password);
//keys loaded ok!
//try to load wallet file. but even if we failed, it is not big problem
- if(!boost::filesystem::exists(m_wallet_file, e) || e)
+ if (use_fs && (!boost::filesystem::exists(m_wallet_file, e) || e))
{
LOG_PRINT_L0("file not found: " << m_wallet_file << ", starting with empty blockchain");
m_account_public_address = m_account.get_keys().m_account_address;
}
- else
+ else if (use_fs || !cache_buf.empty())
{
wallet2::cache_file_data cache_file_data;
- std::string buf;
- bool r = load_from_file(m_wallet_file, buf, std::numeric_limits<size_t>::max());
- THROW_WALLET_EXCEPTION_IF(!r, error::file_read_error, m_wallet_file);
+ std::string cache_file_buf;
+ bool r = true;
+ if (use_fs)
+ {
+ load_from_file(m_wallet_file, cache_file_buf, std::numeric_limits<size_t>::max());
+ THROW_WALLET_EXCEPTION_IF(!r, error::file_read_error, m_wallet_file);
+ }
// try to read it as an encrypted cache
try
{
LOG_PRINT_L1("Trying to decrypt cache data");
- r = ::serialization::parse_binary(buf, cache_file_data);
+ r = ::serialization::parse_binary(use_fs ? cache_file_buf : cache_buf, cache_file_data);
THROW_WALLET_EXCEPTION_IF(!r, error::wallet_internal_error, "internal error: failed to deserialize \"" + m_wallet_file + '\"');
std::string cache_data;
cache_data.resize(cache_file_data.cache_data.size());
@@ -5559,7 +5622,7 @@ void wallet2::load(const std::string& wallet_, const epee::wipeable_string& pass
catch (...)
{
LOG_PRINT_L0("Failed to open portable binary, trying unportable");
- boost::filesystem::copy_file(m_wallet_file, m_wallet_file + ".unportable", boost::filesystem::copy_option::overwrite_if_exists);
+ if (use_fs) boost::filesystem::copy_file(m_wallet_file, m_wallet_file + ".unportable", boost::filesystem::copy_option::overwrite_if_exists);
std::stringstream iss;
iss.str("");
iss << cache_data;
@@ -5574,17 +5637,17 @@ void wallet2::load(const std::string& wallet_, const epee::wipeable_string& pass
LOG_PRINT_L1("Failed to load encrypted cache, trying unencrypted");
try {
std::stringstream iss;
- iss << buf;
+ iss << cache_file_buf;
boost::archive::portable_binary_iarchive ar(iss);
ar >> *this;
}
catch (...)
{
LOG_PRINT_L0("Failed to open portable binary, trying unportable");
- boost::filesystem::copy_file(m_wallet_file, m_wallet_file + ".unportable", boost::filesystem::copy_option::overwrite_if_exists);
+ if (use_fs) boost::filesystem::copy_file(m_wallet_file, m_wallet_file + ".unportable", boost::filesystem::copy_option::overwrite_if_exists);
std::stringstream iss;
iss.str("");
- iss << buf;
+ iss << cache_file_buf;
boost::archive::binary_iarchive ar(iss);
ar >> *this;
}
@@ -5628,7 +5691,8 @@ void wallet2::load(const std::string& wallet_, const epee::wipeable_string& pass
try
{
- m_message_store.read_from_file(get_multisig_wallet_state(), m_mms_file);
+ if (use_fs)
+ m_message_store.read_from_file(get_multisig_wallet_state(), m_mms_file);
}
catch (const std::exception &e)
{
@@ -5656,7 +5720,7 @@ void wallet2::trim_hashchain()
req.height = m_blockchain.size() - 1;
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- r = net_utils::invoke_http_json_rpc("/json_rpc", "getblockheaderbyheight", req, res, m_http_client, rpc_timeout);
+ 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)
check_rpc_cost("getblockheaderbyheight", res.credits, pre_call_credits, COST_PER_BLOCK_HEADER);
}
@@ -5731,18 +5795,10 @@ void wallet2::store_to(const std::string &path, const epee::wipeable_string &pas
}
}
}
- // preparing wallet data
- std::stringstream oss;
- boost::archive::portable_binary_oarchive ar(oss);
- ar << *this;
- wallet2::cache_file_data cache_file_data = {};
- cache_file_data.cache_data = oss.str();
- std::string cipher;
- cipher.resize(cache_file_data.cache_data.size());
- cache_file_data.iv = crypto::rand<crypto::chacha_iv>();
- crypto::chacha20(cache_file_data.cache_data.data(), cache_file_data.cache_data.size(), m_cache_key, cache_file_data.iv, &cipher[0]);
- cache_file_data.cache_data = cipher;
+ // get wallet cache data
+ boost::optional<wallet2::cache_file_data> cache_file_data = get_cache_file_data(password);
+ THROW_WALLET_EXCEPTION_IF(cache_file_data == boost::none, error::wallet_internal_error, "failed to generate wallet cache data");
const std::string new_file = same_file ? m_wallet_file + ".new" : path;
const std::string old_file = m_wallet_file;
@@ -5793,7 +5849,7 @@ void wallet2::store_to(const std::string &path, const epee::wipeable_string &pas
// The price to pay is temporary higher memory consumption for string stream + binary archive
std::ostringstream oss;
binary_archive<true> oar(oss);
- bool success = ::serialization::serialize(oar, cache_file_data);
+ bool success = ::serialization::serialize(oar, cache_file_data.get());
if (success) {
success = save_to_file(new_file, oss.str());
}
@@ -5802,7 +5858,7 @@ void wallet2::store_to(const std::string &path, const epee::wipeable_string &pas
std::ofstream ostr;
ostr.open(new_file, std::ios_base::binary | std::ios_base::out | std::ios_base::trunc);
binary_archive<true> oar(ostr);
- bool success = ::serialization::serialize(oar, cache_file_data);
+ bool success = ::serialization::serialize(oar, cache_file_data.get());
ostr.close();
THROW_WALLET_EXCEPTION_IF(!success || !ostr.good(), error::file_save_error, new_file);
#endif
@@ -5818,7 +5874,30 @@ void wallet2::store_to(const std::string &path, const epee::wipeable_string &pas
// store should only exist if the MMS is really active
m_message_store.write_to_file(get_multisig_wallet_state(), m_mms_file);
}
-
+}
+//----------------------------------------------------------------------------------------------------
+boost::optional<wallet2::cache_file_data> wallet2::get_cache_file_data(const epee::wipeable_string &passwords)
+{
+ trim_hashchain();
+ try
+ {
+ std::stringstream oss;
+ boost::archive::portable_binary_oarchive ar(oss);
+ ar << *this;
+
+ boost::optional<wallet2::cache_file_data> cache_file_data = (wallet2::cache_file_data) {};
+ cache_file_data.get().cache_data = oss.str();
+ std::string cipher;
+ cipher.resize(cache_file_data.get().cache_data.size());
+ cache_file_data.get().iv = crypto::rand<crypto::chacha_iv>();
+ crypto::chacha20(cache_file_data.get().cache_data.data(), cache_file_data.get().cache_data.size(), m_cache_key, cache_file_data.get().iv, &cipher[0]);
+ cache_file_data.get().cache_data = cipher;
+ return cache_file_data;
+ }
+ catch(...)
+ {
+ return boost::none;
+ }
}
//----------------------------------------------------------------------------------------------------
uint64_t wallet2::balance(uint32_t index_major, bool strict) const
@@ -6022,7 +6101,7 @@ void wallet2::rescan_spent()
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- bool r = epee::net_utils::invoke_http_json("/is_key_image_spent", req, daemon_resp, m_http_client, rpc_timeout);
+ bool r = epee::net_utils::invoke_http_json("/is_key_image_spent", req, daemon_resp, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR(r, {}, daemon_resp, "is_key_image_spent", error::is_key_image_spent_error, get_rpc_status(daemon_resp.status));
THROW_WALLET_EXCEPTION_IF(daemon_resp.spent_status.size() != n_outputs, error::wallet_internal_error,
"daemon returned wrong response for is_key_image_spent, wrong amounts count = " +
@@ -6351,7 +6430,7 @@ void wallet2::commit_tx(pending_tx& ptx)
oreq.tx = epee::string_tools::buff_to_hex_nodelimer(tx_to_blob(ptx.tx));
{
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
- bool r = epee::net_utils::invoke_http_json("/submit_raw_tx", oreq, ores, m_http_client, rpc_timeout, "POST");
+ bool r = epee::net_utils::invoke_http_json("/submit_raw_tx", oreq, ores, *m_http_client, rpc_timeout, "POST");
THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "submit_raw_tx");
// MyMonero and OpenMonero use different status strings
THROW_WALLET_EXCEPTION_IF(ores.status != "OK" && ores.status != "success" , error::tx_rejected, ptx.tx, get_rpc_status(ores.status), ores.error);
@@ -6370,7 +6449,7 @@ void wallet2::commit_tx(pending_tx& ptx)
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- bool r = epee::net_utils::invoke_http_json("/sendrawtransaction", req, daemon_send_resp, m_http_client, rpc_timeout);
+ bool r = epee::net_utils::invoke_http_json("/sendrawtransaction", req, daemon_send_resp, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR(r, {}, daemon_send_resp, "sendrawtransaction", error::tx_rejected, ptx.tx, get_rpc_status(daemon_send_resp.status), get_text_reason(daemon_send_resp));
check_rpc_cost("/sendrawtransaction", daemon_send_resp.credits, pre_call_credits, COST_PER_TX_RELAY);
}
@@ -7342,7 +7421,7 @@ uint32_t wallet2::adjust_priority(uint32_t priority)
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
getbh_req.client = get_client_signature();
- bool r = net_utils::invoke_http_json_rpc("/json_rpc", "getblockheadersrange", getbh_req, getbh_res, m_http_client, rpc_timeout);
+ bool r = net_utils::invoke_http_json_rpc("/json_rpc", "getblockheadersrange", getbh_req, getbh_res, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR(r, {}, getbh_res, "getblockheadersrange", error::get_blocks_error, get_rpc_status(getbh_res.status));
check_rpc_cost("/sendrawtransaction", getbh_res.credits, pre_call_credits, N * COST_PER_BLOCK_HEADER);
}
@@ -7568,7 +7647,7 @@ bool wallet2::find_and_save_rings(bool force)
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- bool r = epee::net_utils::invoke_http_json("/gettransactions", req, res, m_http_client, rpc_timeout);
+ bool r = epee::net_utils::invoke_http_json("/gettransactions", req, res, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR_GENERIC(r, {}, res, "/gettransactions");
THROW_WALLET_EXCEPTION_IF(res.txs.size() != req.txs_hashes.size(), error::wallet_internal_error,
"daemon returned wrong response for gettransactions, wrong txs count = " +
@@ -7716,7 +7795,7 @@ void wallet2::light_wallet_get_outs(std::vector<std::vector<tools::wallet2::get_
{
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
- bool r = epee::net_utils::invoke_http_json("/get_random_outs", oreq, ores, m_http_client, rpc_timeout, "POST");
+ bool r = epee::net_utils::invoke_http_json("/get_random_outs", oreq, ores, *m_http_client, rpc_timeout, "POST");
m_daemon_rpc_mutex.unlock();
THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "get_random_outs");
THROW_WALLET_EXCEPTION_IF(ores.amount_outs.empty() , error::wallet_internal_error, "No outputs received from light wallet node. Error: " + ores.Error);
@@ -7903,7 +7982,7 @@ void wallet2::get_outs(std::vector<std::vector<tools::wallet2::get_outs_entry>>
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req_t.client = get_client_signature();
- bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_output_histogram", req_t, resp_t, m_http_client, rpc_timeout);
+ bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_output_histogram", req_t, resp_t, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR(r, {}, resp_t, "get_output_histogram", error::get_histogram_error, get_rpc_status(resp_t.status));
check_rpc_cost("get_output_histogram", resp_t.credits, pre_call_credits, COST_PER_OUTPUT_HISTOGRAM * req_t.amounts.size());
}
@@ -7929,7 +8008,7 @@ void wallet2::get_outs(std::vector<std::vector<tools::wallet2::get_outs_entry>>
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req_t.client = get_client_signature();
- bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_output_distribution", req_t, resp_t, m_http_client, rpc_timeout * 1000);
+ bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_output_distribution", req_t, resp_t, *m_http_client, rpc_timeout * 1000);
THROW_ON_RPC_RESPONSE_ERROR(r, {}, resp_t, "get_output_distribution", error::get_output_distribution, get_rpc_status(resp_t.status));
uint64_t expected_cost = 0;
for (uint64_t amount: req_t.amounts) expected_cost += (amount ? COST_PER_OUTPUT_DISTRIBUTION : COST_PER_OUTPUT_DISTRIBUTION_0);
@@ -8283,7 +8362,7 @@ void wallet2::get_outs(std::vector<std::vector<tools::wallet2::get_outs_entry>>
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- bool r = epee::net_utils::invoke_http_bin("/get_outs.bin", req, daemon_resp, m_http_client, rpc_timeout);
+ bool r = epee::net_utils::invoke_http_bin("/get_outs.bin", req, daemon_resp, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR(r, {}, daemon_resp, "get_outs.bin", error::get_outs_error, get_rpc_status(daemon_resp.status));
THROW_WALLET_EXCEPTION_IF(daemon_resp.outs.size() != req.outputs.size(), error::wallet_internal_error,
"daemon returned wrong response for get_outs.bin, wrong amounts count = " +
@@ -10492,7 +10571,7 @@ uint8_t wallet2::get_current_hard_fork()
m_daemon_rpc_mutex.lock();
req_t.version = 0;
- bool r = net_utils::invoke_http_json_rpc("/json_rpc", "hard_fork_info", req_t, resp_t, m_http_client, rpc_timeout);
+ bool r = net_utils::invoke_http_json_rpc("/json_rpc", "hard_fork_info", req_t, resp_t, *m_http_client, rpc_timeout);
m_daemon_rpc_mutex.unlock();
THROW_WALLET_EXCEPTION_IF(!r, tools::error::no_connection_to_daemon, "hard_fork_info");
THROW_WALLET_EXCEPTION_IF(resp_t.status == CORE_RPC_STATUS_BUSY, tools::error::daemon_busy, "hard_fork_info");
@@ -10587,7 +10666,7 @@ std::vector<size_t> wallet2::select_available_outputs_from_histogram(uint64_t co
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req_t.client = get_client_signature();
- bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_output_histogram", req_t, resp_t, m_http_client, rpc_timeout);
+ bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_output_histogram", req_t, resp_t, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR(r, {}, resp_t, "get_output_histogram", error::get_histogram_error, resp_t.status);
uint64_t cost = req_t.amounts.empty() ? COST_PER_FULL_OUTPUT_HISTOGRAM : (COST_PER_OUTPUT_HISTOGRAM * req_t.amounts.size());
check_rpc_cost("get_output_histogram", resp_t.credits, pre_call_credits, cost);
@@ -10629,7 +10708,7 @@ uint64_t wallet2::get_num_rct_outputs()
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req_t.client = get_client_signature();
- bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_output_histogram", req_t, resp_t, m_http_client, rpc_timeout);
+ bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_output_histogram", req_t, resp_t, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR(r, {}, resp_t, "get_output_histogram", error::get_histogram_error, resp_t.status);
THROW_WALLET_EXCEPTION_IF(resp_t.histogram.size() != 1, error::get_histogram_error, "Expected exactly one response");
THROW_WALLET_EXCEPTION_IF(resp_t.histogram[0].amount != 0, error::get_histogram_error, "Expected 0 amount");
@@ -10760,7 +10839,7 @@ bool wallet2::get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key, s
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
req.client = get_client_signature();
uint64_t pre_call_credits = m_rpc_payment_state.credits;
- bool ok = epee::net_utils::invoke_http_json("/gettransactions", req, res, m_http_client);
+ bool ok = epee::net_utils::invoke_http_json("/gettransactions", req, res, *m_http_client);
THROW_WALLET_EXCEPTION_IF(!ok || (res.txs.size() != 1 && res.txs_as_hex.size() != 1),
error::wallet_internal_error, "Failed to get transaction from daemon");
check_rpc_cost("/gettransactions", res.credits, pre_call_credits, res.txs.size() * COST_PER_TX);
@@ -10813,7 +10892,7 @@ void wallet2::set_tx_key(const crypto::hash &txid, const crypto::secret_key &tx_
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- r = epee::net_utils::invoke_http_json("/gettransactions", req, res, m_http_client, rpc_timeout);
+ r = epee::net_utils::invoke_http_json("/gettransactions", req, res, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR_GENERIC(r, {}, res, "/gettransactions");
THROW_WALLET_EXCEPTION_IF(res.txs.size() != 1, error::wallet_internal_error,
"daemon returned wrong response for gettransactions, wrong txs count = " +
@@ -10866,7 +10945,7 @@ std::string wallet2::get_spend_proof(const crypto::hash &txid, const std::string
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- r = epee::net_utils::invoke_http_json("/gettransactions", req, res, m_http_client, rpc_timeout);
+ r = epee::net_utils::invoke_http_json("/gettransactions", req, res, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR_GENERIC(r, {}, res, "gettransactions");
THROW_WALLET_EXCEPTION_IF(res.txs.size() != 1, error::wallet_internal_error,
"daemon returned wrong response for gettransactions, wrong txs count = " +
@@ -10930,7 +11009,7 @@ std::string wallet2::get_spend_proof(const crypto::hash &txid, const std::string
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- r = epee::net_utils::invoke_http_bin("/get_outs.bin", req, res, m_http_client, rpc_timeout);
+ r = epee::net_utils::invoke_http_bin("/get_outs.bin", req, res, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR(r, {}, res, "get_outs.bin", error::get_outs_error, res.status);
THROW_WALLET_EXCEPTION_IF(res.outs.size() != ring_size, error::wallet_internal_error,
"daemon returned wrong response for get_outs.bin, wrong amounts count = " +
@@ -10988,7 +11067,7 @@ bool wallet2::check_spend_proof(const crypto::hash &txid, const std::string &mes
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- r = epee::net_utils::invoke_http_json("/gettransactions", req, res, m_http_client, rpc_timeout);
+ r = epee::net_utils::invoke_http_json("/gettransactions", req, res, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR_GENERIC(r, {}, res, "gettransactions");
THROW_WALLET_EXCEPTION_IF(res.txs.size() != 1, error::wallet_internal_error,
"daemon returned wrong response for gettransactions, wrong txs count = " +
@@ -11063,7 +11142,7 @@ bool wallet2::check_spend_proof(const crypto::hash &txid, const std::string &mes
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- r = epee::net_utils::invoke_http_bin("/get_outs.bin", req, res, m_http_client, rpc_timeout);
+ r = epee::net_utils::invoke_http_bin("/get_outs.bin", req, res, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR(r, {}, res, "get_outs.bin", error::get_outs_error, res.status);
THROW_WALLET_EXCEPTION_IF(res.outs.size() != req.outputs.size(), error::wallet_internal_error,
"daemon returned wrong response for get_outs.bin, wrong amounts count = " +
@@ -11165,7 +11244,7 @@ void wallet2::check_tx_key_helper(const crypto::hash &txid, const crypto::key_de
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- ok = epee::net_utils::invoke_http_json("/gettransactions", req, res, m_http_client);
+ ok = epee::net_utils::invoke_http_json("/gettransactions", req, res, *m_http_client);
THROW_WALLET_EXCEPTION_IF(!ok || (res.txs.size() != 1 && res.txs_as_hex.size() != 1),
error::wallet_internal_error, "Failed to get transaction from daemon");
check_rpc_cost("/gettransactions", res.credits, pre_call_credits, COST_PER_TX);
@@ -11220,7 +11299,7 @@ std::string wallet2::get_tx_proof(const crypto::hash &txid, const cryptonote::ac
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- ok = net_utils::invoke_http_json("/gettransactions", req, res, m_http_client);
+ ok = net_utils::invoke_http_json("/gettransactions", req, res, *m_http_client);
THROW_WALLET_EXCEPTION_IF(!ok || (res.txs.size() != 1 && res.txs_as_hex.size() != 1),
error::wallet_internal_error, "Failed to get transaction from daemon");
check_rpc_cost("/gettransactions", res.credits, pre_call_credits, COST_PER_TX);
@@ -11381,7 +11460,7 @@ bool wallet2::check_tx_proof(const crypto::hash &txid, const cryptonote::account
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- ok = net_utils::invoke_http_json("/gettransactions", req, res, m_http_client);
+ ok = net_utils::invoke_http_json("/gettransactions", req, res, *m_http_client);
THROW_WALLET_EXCEPTION_IF(!ok || (res.txs.size() != 1 && res.txs_as_hex.size() != 1),
error::wallet_internal_error, "Failed to get transaction from daemon");
check_rpc_cost("/gettransactions", res.credits, pre_call_credits, COST_PER_TX);
@@ -11678,7 +11757,7 @@ bool wallet2::check_reserve_proof(const cryptonote::account_public_address &addr
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
gettx_req.client = get_client_signature();
- bool ok = net_utils::invoke_http_json("/gettransactions", gettx_req, gettx_res, m_http_client);
+ bool ok = net_utils::invoke_http_json("/gettransactions", gettx_req, gettx_res, *m_http_client);
THROW_WALLET_EXCEPTION_IF(!ok || gettx_res.txs.size() != proofs.size(),
error::wallet_internal_error, "Failed to get transaction from daemon");
check_rpc_cost("/gettransactions", gettx_res.credits, pre_call_credits, gettx_res.txs.size() * COST_PER_TX);
@@ -11695,7 +11774,7 @@ bool wallet2::check_reserve_proof(const cryptonote::account_public_address &addr
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
kispent_req.client = get_client_signature();
- ok = epee::net_utils::invoke_http_json("/is_key_image_spent", kispent_req, kispent_res, m_http_client, rpc_timeout);
+ ok = epee::net_utils::invoke_http_json("/is_key_image_spent", kispent_req, kispent_res, *m_http_client, rpc_timeout);
THROW_WALLET_EXCEPTION_IF(!ok || kispent_res.spent_status.size() != proofs.size(),
error::wallet_internal_error, "Failed to get key image spent status from daemon");
check_rpc_cost("/is_key_image_spent", kispent_res.credits, pre_call_credits, kispent_res.spent_status.size() * COST_PER_KEY_IMAGE);
@@ -12269,7 +12348,7 @@ uint64_t wallet2::import_key_images(const std::vector<std::pair<crypto::key_imag
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- bool r = epee::net_utils::invoke_http_json("/is_key_image_spent", req, daemon_resp, m_http_client, rpc_timeout);
+ bool r = epee::net_utils::invoke_http_json("/is_key_image_spent", req, daemon_resp, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR_GENERIC(r, {}, daemon_resp, "is_key_image_spent");
THROW_WALLET_EXCEPTION_IF(daemon_resp.spent_status.size() != signed_key_images.size(), error::wallet_internal_error,
"daemon returned wrong response for is_key_image_spent, wrong amounts count = " +
@@ -12358,7 +12437,7 @@ uint64_t wallet2::import_key_images(const std::vector<std::pair<crypto::key_imag
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
gettxs_req.client = get_client_signature();
uint64_t pre_call_credits = m_rpc_payment_state.credits;
- bool r = epee::net_utils::invoke_http_json("/gettransactions", gettxs_req, gettxs_res, m_http_client, rpc_timeout);
+ bool r = epee::net_utils::invoke_http_json("/gettransactions", gettxs_req, gettxs_res, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR_GENERIC(r, {}, gettxs_res, "gettransactions");
THROW_WALLET_EXCEPTION_IF(gettxs_res.txs.size() != spent_txids.size(), error::wallet_internal_error,
"daemon returned wrong response for gettransactions, wrong count = " + std::to_string(gettxs_res.txs.size()) + ", expected " + std::to_string(spent_txids.size()));
@@ -12676,7 +12755,8 @@ process:
const crypto::public_key& out_key = boost::get<cryptonote::txout_to_key>(td.m_tx.vout[td.m_internal_output_index].target).key;
bool r = cryptonote::generate_key_image_helper(m_account.get_keys(), m_subaddresses, out_key, tx_pub_key, additional_tx_pub_keys, td.m_internal_output_index, in_ephemeral, td.m_key_image, m_account.get_device());
THROW_WALLET_EXCEPTION_IF(!r, error::wallet_internal_error, "Failed to generate key image");
- expand_subaddresses(td.m_subaddr_index);
+ if (td.m_subaddr_index.major < m_subaddress_labels.size() && td.m_subaddr_index.minor < m_subaddress_labels[td.m_subaddr_index.major].size())
+ expand_subaddresses(td.m_subaddr_index);
td.m_key_image_known = true;
td.m_key_image_request = true;
td.m_key_image_partial = false;
@@ -13298,7 +13378,7 @@ uint64_t wallet2::get_blockchain_height_by_date(uint16_t year, uint8_t month, ui
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- r = net_utils::invoke_http_bin("/getblocks_by_height.bin", req, res, m_http_client, rpc_timeout);
+ r = net_utils::invoke_http_bin("/getblocks_by_height.bin", req, res, *m_http_client, rpc_timeout);
if (r && res.status == CORE_RPC_STATUS_OK)
check_rpc_cost("/getblocks_by_height.bin", res.credits, pre_call_credits, 3 * COST_PER_BLOCK);
}
@@ -13376,7 +13456,7 @@ std::vector<std::pair<uint64_t, uint64_t>> wallet2::estimate_backlog(const std::
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
- bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_txpool_backlog", req, res, m_http_client, rpc_timeout);
+ bool r = net_utils::invoke_http_json_rpc("/json_rpc", "get_txpool_backlog", req, res, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR(r, {}, res, "get_txpool_backlog", error::get_tx_pool_error);
check_rpc_cost("get_txpool_backlog", res.credits, pre_call_credits, COST_PER_TX_POOL_STATS * res.backlog.size());
}
@@ -13715,12 +13795,12 @@ void wallet2::finish_rescan_bc_keep_key_images(uint64_t transfer_height, const c
//----------------------------------------------------------------------------------------------------
uint64_t wallet2::get_bytes_sent() const
{
- return m_http_client.get_bytes_sent();
+ return m_http_client->get_bytes_sent();
}
//----------------------------------------------------------------------------------------------------
uint64_t wallet2::get_bytes_received() const
{
- return m_http_client.get_bytes_received();
+ return m_http_client->get_bytes_received();
}
//----------------------------------------------------------------------------------------------------
std::vector<cryptonote::public_node> wallet2::get_public_nodes(bool white_only)
@@ -13733,7 +13813,7 @@ std::vector<cryptonote::public_node> wallet2::get_public_nodes(bool white_only)
{
const boost::lock_guard<boost::recursive_mutex> lock{m_daemon_rpc_mutex};
- bool r = epee::net_utils::invoke_http_json("/get_public_nodes", req, res, m_http_client, rpc_timeout);
+ bool r = epee::net_utils::invoke_http_json("/get_public_nodes", req, res, *m_http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR_GENERIC(r, {}, res, "/get_public_nodes");
}
diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h
index 1dac8acac..1c3c00152 100644
--- a/src/wallet/wallet2.h
+++ b/src/wallet/wallet2.h
@@ -269,7 +269,7 @@ private:
static bool verify_password(const std::string& keys_file_name, const epee::wipeable_string& password, bool no_spend_key, hw::device &hwdev, uint64_t kdf_rounds);
static bool query_device(hw::device::device_type& device_type, const std::string& keys_file_name, const epee::wipeable_string& password, uint64_t kdf_rounds = 1);
- wallet2(cryptonote::network_type nettype = cryptonote::MAINNET, uint64_t kdf_rounds = 1, bool unattended = false);
+ wallet2(cryptonote::network_type nettype = cryptonote::MAINNET, uint64_t kdf_rounds = 1, bool unattended = false, std::unique_ptr<epee::net_utils::http::http_client_factory> http_client_factory = std::unique_ptr<epee::net_utils::http::http_simple_client_factory>(new epee::net_utils::http::http_simple_client_factory()));
~wallet2();
struct multisig_info
@@ -708,7 +708,7 @@ private:
*/
void rewrite(const std::string& wallet_name, const epee::wipeable_string& password);
void write_watch_only_wallet(const std::string& wallet_name, const epee::wipeable_string& password, std::string &new_keys_filename);
- void load(const std::string& wallet, const epee::wipeable_string& password);
+ void load(const std::string& wallet, const epee::wipeable_string& password, const std::string& keys_buf = "", const std::string& cache_buf = "");
void store();
/*!
* \brief store_to Stores wallet to another file(s), deleting old ones
@@ -716,6 +716,19 @@ private:
* \param password Password to protect new wallet (TODO: probably better save the password in the wallet object?)
*/
void store_to(const std::string &path, const epee::wipeable_string &password);
+ /*!
+ * \brief get_keys_file_data Get wallet keys data which can be stored to a wallet file.
+ * \param password Password of the encrypted wallet buffer (TODO: probably better save the password in the wallet object?)
+ * \param watch_only true to include only view key, false to include both spend and view keys
+ * \return Encrypted wallet keys data which can be stored to a wallet file
+ */
+ boost::optional<wallet2::keys_file_data> get_keys_file_data(const epee::wipeable_string& password, bool watch_only);
+ /*!
+ * \brief get_cache_file_data Get wallet cache data which can be stored to a wallet file.
+ * \param password Password to protect the wallet cache data (TODO: probably better save the password in the wallet object?)
+ * \return Encrypted wallet cache data which can be stored to a wallet file
+ */
+ boost::optional<wallet2::cache_file_data> get_cache_file_data(const epee::wipeable_string& password);
std::string path() const;
@@ -793,6 +806,7 @@ private:
size_t get_num_subaddresses(uint32_t index_major) const { return index_major < m_subaddress_labels.size() ? m_subaddress_labels[index_major].size() : 0; }
void add_subaddress(uint32_t index_major, const std::string& label); // throws when index is out of bound
void expand_subaddresses(const cryptonote::subaddress_index& index);
+ void create_one_off_subaddress(const cryptonote::subaddress_index& index);
std::string get_subaddress_label(const cryptonote::subaddress_index& index) const;
void set_subaddress_label(const cryptonote::subaddress_index &index, const std::string &label);
void set_subaddress_lookahead(size_t major, size_t minor);
@@ -1318,25 +1332,25 @@ private:
crypto::public_key get_multisig_signing_public_key(const crypto::secret_key &skey) const;
template<class t_request, class t_response>
- inline bool invoke_http_json(const boost::string_ref uri, const t_request& req, t_response& res, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "GET")
+ inline bool invoke_http_json(const boost::string_ref uri, const t_request& req, t_response& res, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "POST")
{
if (m_offline) return false;
boost::lock_guard<boost::recursive_mutex> lock(m_daemon_rpc_mutex);
- return epee::net_utils::invoke_http_json(uri, req, res, m_http_client, timeout, http_method);
+ return epee::net_utils::invoke_http_json(uri, req, res, *m_http_client, timeout, http_method);
}
template<class t_request, class t_response>
- inline bool invoke_http_bin(const boost::string_ref uri, const t_request& req, t_response& res, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "GET")
+ inline bool invoke_http_bin(const boost::string_ref uri, const t_request& req, t_response& res, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "POST")
{
if (m_offline) return false;
boost::lock_guard<boost::recursive_mutex> lock(m_daemon_rpc_mutex);
- return epee::net_utils::invoke_http_bin(uri, req, res, m_http_client, timeout, http_method);
+ return epee::net_utils::invoke_http_bin(uri, req, res, *m_http_client, timeout, http_method);
}
template<class t_request, class t_response>
- inline bool invoke_http_json_rpc(const boost::string_ref uri, const std::string& method_name, const t_request& req, t_response& res, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "GET", const std::string& req_id = "0")
+ inline bool invoke_http_json_rpc(const boost::string_ref uri, const std::string& method_name, const t_request& req, t_response& res, std::chrono::milliseconds timeout = std::chrono::seconds(15), const boost::string_ref http_method = "POST", const std::string& req_id = "0")
{
if (m_offline) return false;
boost::lock_guard<boost::recursive_mutex> lock(m_daemon_rpc_mutex);
- return epee::net_utils::invoke_http_json_rpc(uri, method_name, req, res, m_http_client, timeout, http_method, req_id);
+ return epee::net_utils::invoke_http_json_rpc(uri, method_name, req, res, *m_http_client, timeout, http_method, req_id);
}
bool set_ring_database(const std::string &filename);
@@ -1402,11 +1416,18 @@ private:
*/
bool store_keys(const std::string& keys_file_name, const epee::wipeable_string& password, bool watch_only = false);
/*!
- * \brief Load wallet information from wallet file.
+ * \brief Load wallet keys information from wallet file.
* \param keys_file_name Name of wallet file
* \param password Password of wallet file
*/
bool load_keys(const std::string& keys_file_name, const epee::wipeable_string& password);
+ /*!
+ * \brief Load wallet keys information from a string buffer.
+ * \param keys_buf Keys buffer to load
+ * \param password Password of keys buffer
+ */
+ 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);
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);
@@ -1501,7 +1522,7 @@ private:
std::string m_wallet_file;
std::string m_keys_file;
std::string m_mms_file;
- epee::net_utils::http::http_simple_client m_http_client;
+ const std::unique_ptr<epee::net_utils::http::abstract_http_client> m_http_client;
hashchain m_blockchain;
std::unordered_map<crypto::hash, unconfirmed_transfer_details> m_unconfirmed_txs;
std::unordered_map<crypto::hash, confirmed_transfer_details> m_confirmed_txs;
diff --git a/src/wallet/wallet_rpc_payments.cpp b/src/wallet/wallet_rpc_payments.cpp
index 41696d13b..4f5364269 100644
--- a/src/wallet/wallet_rpc_payments.cpp
+++ b/src/wallet/wallet_rpc_payments.cpp
@@ -85,7 +85,7 @@ bool wallet2::make_rpc_payment(uint32_t nonce, uint32_t cookie, uint64_t &credit
uint64_t pre_call_credits = m_rpc_payment_state.credits;
req.client = get_client_signature();
epee::json_rpc::error error;
- bool r = epee::net_utils::invoke_http_json_rpc("/json_rpc", "rpc_access_submit_nonce", req, res, error, m_http_client, rpc_timeout);
+ bool r = epee::net_utils::invoke_http_json_rpc("/json_rpc", "rpc_access_submit_nonce", req, res, error, *m_http_client, rpc_timeout);
m_daemon_rpc_mutex.unlock();
THROW_ON_RPC_RESPONSE_ERROR_GENERIC(r, error, res, "rpc_access_submit_nonce");
THROW_WALLET_EXCEPTION_IF(res.credits < pre_call_credits, error::wallet_internal_error, "RPC payment did not increase balance");
diff --git a/tests/core_tests/chaingen.h b/tests/core_tests/chaingen.h
index 80ce7404b..453ee923d 100644
--- a/tests/core_tests/chaingen.h
+++ b/tests/core_tests/chaingen.h
@@ -116,7 +116,8 @@ struct event_visitor_settings
{
set_txs_keeped_by_block = 1 << 0,
set_txs_do_not_relay = 1 << 1,
- set_local_relay = 1 << 2
+ set_local_relay = 1 << 2,
+ set_txs_stem = 1 << 3
};
event_visitor_settings(int a_mask = 0)
@@ -548,6 +549,10 @@ public:
{
m_tx_relay = cryptonote::relay_method::none;
}
+ else if (settings.mask & event_visitor_settings::set_txs_stem)
+ {
+ m_tx_relay = cryptonote::relay_method::stem;
+ }
else
{
m_tx_relay = cryptonote::relay_method::fluff;
diff --git a/tests/core_tests/chaingen_main.cpp b/tests/core_tests/chaingen_main.cpp
index 23f3170b8..8a313140c 100644
--- a/tests/core_tests/chaingen_main.cpp
+++ b/tests/core_tests/chaingen_main.cpp
@@ -161,6 +161,8 @@ int main(int argc, char* argv[])
GENERATE_AND_PLAY(txpool_spend_key_all);
GENERATE_AND_PLAY(txpool_double_spend_norelay);
GENERATE_AND_PLAY(txpool_double_spend_local);
+ GENERATE_AND_PLAY(txpool_double_spend_keyimage);
+ GENERATE_AND_PLAY(txpool_stem_loop);
// Double spend
GENERATE_AND_PLAY(gen_double_spend_in_tx<false>);
diff --git a/tests/core_tests/tx_pool.cpp b/tests/core_tests/tx_pool.cpp
index 537015dca..b60dd00fc 100644
--- a/tests/core_tests/tx_pool.cpp
+++ b/tests/core_tests/tx_pool.cpp
@@ -125,10 +125,12 @@ txpool_double_spend_base::txpool_double_spend_base()
, m_no_relay_hashes()
, m_all_hashes()
, m_no_new_index(0)
+ , m_failed_index(0)
, m_new_timestamp_index(0)
, m_last_tx(crypto::hash{})
{
REGISTER_CALLBACK_METHOD(txpool_double_spend_base, mark_no_new);
+ REGISTER_CALLBACK_METHOD(txpool_double_spend_base, mark_failed);
REGISTER_CALLBACK_METHOD(txpool_double_spend_base, mark_timestamp_change);
REGISTER_CALLBACK_METHOD(txpool_double_spend_base, timestamp_change_pause);
REGISTER_CALLBACK_METHOD(txpool_double_spend_base, check_unchanged);
@@ -143,6 +145,12 @@ bool txpool_double_spend_base::mark_no_new(cryptonote::core& /*c*/, size_t ev_in
return true;
}
+bool txpool_double_spend_base::mark_failed(cryptonote::core& /*c*/, size_t ev_index, const std::vector<test_event_entry>& /*events*/)
+{
+ m_failed_index = ev_index + 1;
+ return true;
+}
+
bool txpool_double_spend_base::mark_timestamp_change(cryptonote::core& /*c*/, size_t ev_index, const std::vector<test_event_entry>& /*events*/)
{
m_new_timestamp_index = ev_index + 1;
@@ -483,6 +491,8 @@ bool txpool_double_spend_base::check_tx_verification_context(const cryptonote::t
m_last_tx = cryptonote::get_transaction_hash(tx);
if (m_no_new_index == event_idx)
return !tvc.m_verifivation_failed && !tx_added;
+ else if (m_failed_index == event_idx)
+ return tvc.m_verifivation_failed;// && !tx_added;
else
return !tvc.m_verifivation_failed && tx_added;
}
@@ -542,7 +552,6 @@ bool txpool_double_spend_local::generate(std::vector<test_event_entry>& events)
DO_CALLBACK(events, "mark_no_new");
events.push_back(tx_0);
DO_CALLBACK(events, "check_txpool_spent_keys");
- DO_CALLBACK(events, "mark_timestamp_change");
DO_CALLBACK(events, "check_unchanged");
SET_EVENT_VISITOR_SETT(events, 0);
DO_CALLBACK(events, "timestamp_change_pause");
@@ -559,3 +568,73 @@ bool txpool_double_spend_local::generate(std::vector<test_event_entry>& events)
return true;
}
+bool txpool_double_spend_keyimage::generate(std::vector<test_event_entry>& events) const
+{
+ INIT_MEMPOOL_TEST();
+
+ DO_CALLBACK(events, "check_txpool_spent_keys");
+ SET_EVENT_VISITOR_SETT(events, event_visitor_settings::set_local_relay);
+ DO_CALLBACK(events, "mark_no_new");
+
+ const std::size_t tx_index1 = events.size();
+ MAKE_TX(events, tx_0, miner_account, bob_account, send_amount, blk_0);
+
+ SET_EVENT_VISITOR_SETT(events, event_visitor_settings::set_txs_stem);
+ DO_CALLBACK(events, "increase_all_tx_count");
+ DO_CALLBACK(events, "check_txpool_spent_keys");
+ DO_CALLBACK(events, "mark_timestamp_change");
+ DO_CALLBACK(events, "check_new_hidden");
+ DO_CALLBACK(events, "timestamp_change_pause");
+ DO_CALLBACK(events, "mark_no_new");
+ const std::size_t tx_index2 = events.size();
+ events.push_back(tx_0);
+ DO_CALLBACK(events, "check_txpool_spent_keys");
+ DO_CALLBACK(events, "mark_timestamp_change");
+ DO_CALLBACK(events, "check_unchanged");
+
+ // use same key image with different id
+ cryptonote::transaction tx_1;
+ {
+ auto events_copy = events;
+ events_copy.erase(events_copy.begin() + tx_index1);
+ events_copy.erase(events_copy.begin() + tx_index2 - 1);
+ MAKE_TX(events_copy, tx_temp, miner_account, bob_account, send_amount, blk_0);
+ tx_1 = tx_temp;
+ }
+
+ // same key image
+ DO_CALLBACK(events, "timestamp_change_pause");
+ DO_CALLBACK(events, "mark_failed");
+ events.push_back(tx_1);
+ DO_CALLBACK(events, "check_unchanged");
+
+ return true;
+}
+
+bool txpool_stem_loop::generate(std::vector<test_event_entry>& events) const
+{
+ INIT_MEMPOOL_TEST();
+
+ DO_CALLBACK(events, "check_txpool_spent_keys");
+ SET_EVENT_VISITOR_SETT(events, event_visitor_settings::set_txs_stem);
+ DO_CALLBACK(events, "mark_no_new");
+
+ MAKE_TX(events, tx_0, miner_account, bob_account, send_amount, blk_0);
+
+ DO_CALLBACK(events, "increase_all_tx_count");
+ DO_CALLBACK(events, "check_txpool_spent_keys");
+ DO_CALLBACK(events, "mark_timestamp_change");
+ DO_CALLBACK(events, "check_new_hidden");
+ DO_CALLBACK(events, "timestamp_change_pause");
+ events.push_back(tx_0);
+ DO_CALLBACK(events, "increase_broadcasted_tx_count");
+ DO_CALLBACK(events, "check_txpool_spent_keys");
+ DO_CALLBACK(events, "mark_timestamp_change");
+ DO_CALLBACK(events, "check_new_broadcasted");
+ DO_CALLBACK(events, "timestamp_change_pause");
+ DO_CALLBACK(events, "mark_no_new");
+ events.push_back(tx_0);
+ DO_CALLBACK(events, "check_unchanged");
+
+ return true;
+}
diff --git a/tests/core_tests/tx_pool.h b/tests/core_tests/tx_pool.h
index 996c76698..8a27e98f0 100644
--- a/tests/core_tests/tx_pool.h
+++ b/tests/core_tests/tx_pool.h
@@ -77,6 +77,7 @@ class txpool_double_spend_base : public txpool_base
std::unordered_set<crypto::hash> m_no_relay_hashes;
std::unordered_map<crypto::hash, uint64_t> m_all_hashes;
size_t m_no_new_index;
+ size_t m_failed_index;
size_t m_new_timestamp_index;
crypto::hash m_last_tx;
@@ -86,6 +87,7 @@ public:
txpool_double_spend_base();
bool mark_no_new(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
+ bool mark_failed(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
bool mark_timestamp_change(cryptonote::core& c, size_t ev_index, const std::vector<test_event_entry>& events);
//! Pause for 1 second, so that `receive_time` for tx meta changes (tx hidden from public rpc being updated)
@@ -116,3 +118,21 @@ struct txpool_double_spend_local : txpool_double_spend_base
bool generate(std::vector<test_event_entry>& events) const;
};
+
+struct txpool_double_spend_keyimage : txpool_double_spend_base
+{
+ txpool_double_spend_keyimage()
+ : txpool_double_spend_base()
+ {}
+
+ bool generate(std::vector<test_event_entry>& events) const;
+};
+
+struct txpool_stem_loop : txpool_double_spend_base
+{
+ txpool_stem_loop()
+ : txpool_double_spend_base()
+ {}
+
+ bool generate(std::vector<test_event_entry>& events) const;
+};
diff --git a/tests/fuzz/http-client.cpp b/tests/fuzz/http-client.cpp
index 1750838ae..ea6d5a2ad 100644
--- a/tests/fuzz/http-client.cpp
+++ b/tests/fuzz/http-client.cpp
@@ -29,6 +29,7 @@
#include "include_base_utils.h"
#include "file_io_utils.h"
#include "net/http_client.h"
+#include "net/net_ssl.h"
#include "fuzzer.h"
class dummy_client
@@ -46,6 +47,10 @@ public:
data.clear();
return true;
}
+ void set_ssl(epee::net_utils::ssl_options_t ssl_options) { }
+ bool is_connected(bool *ssl = NULL) { return true; }
+ uint64_t get_bytes_sent() const { return 1; }
+ uint64_t get_bytes_received() const { return 1; }
void set_test_data(const std::string &s) { data = s; }
diff --git a/tests/unit_tests/epee_utils.cpp b/tests/unit_tests/epee_utils.cpp
index 42bbb26bb..4f42140b3 100644
--- a/tests/unit_tests/epee_utils.cpp
+++ b/tests/unit_tests/epee_utils.cpp
@@ -841,6 +841,9 @@ TEST(HexLocale, String)
// decoding it this way also, ignoring spaces and colons between the numbers
hex.assign("00:ff 0f:f0");
EXPECT_EQ(source, epee::from_hex_locale::to_vector(hex));
+
+ hex.append("f0");
+ EXPECT_EQ(source, epee::from_hex_locale::to_vector(boost::string_ref{hex.data(), hex.size() - 2}));
}
TEST(ToHex, Array)
diff --git a/tests/unit_tests/levin.cpp b/tests/unit_tests/levin.cpp
index 720103e5a..62b101a42 100644
--- a/tests/unit_tests/levin.cpp
+++ b/tests/unit_tests/levin.cpp
@@ -34,11 +34,14 @@
#include <gtest/gtest.h>
#include <limits>
#include <set>
+#include <map>
#include "byte_slice.h"
#include "crypto/crypto.h"
#include "cryptonote_basic/connection_context.h"
+#include "cryptonote_config.h"
#include "cryptonote_core/cryptonote_core.h"
+#include "cryptonote_core/i_core_events.h"
#include "cryptonote_protocol/cryptonote_protocol_defs.h"
#include "cryptonote_protocol/levin_notify.h"
#include "int-util.h"
@@ -113,6 +116,44 @@ namespace
std::deque<epee::byte_slice> send_queue_;
};
+ class test_core_events final : public cryptonote::i_core_events
+ {
+ std::map<cryptonote::relay_method, std::vector<cryptonote::blobdata>> relayed_;
+
+ virtual void on_transactions_relayed(epee::span<const cryptonote::blobdata> txes, cryptonote::relay_method relay) override final
+ {
+ std::vector<cryptonote::blobdata>& cached = relayed_[relay];
+ for (const auto& tx : txes)
+ cached.push_back(tx);
+ }
+
+ public:
+ test_core_events()
+ : relayed_()
+ {}
+
+ std::size_t relayed_method_size() const noexcept
+ {
+ return relayed_.size();
+ }
+
+ bool has_stem_txes() const noexcept
+ {
+ return relayed_.count(cryptonote::relay_method::stem);
+ }
+
+ std::vector<cryptonote::blobdata> take_relayed(cryptonote::relay_method relay)
+ {
+ auto elems = relayed_.find(relay);
+ if (elems == relayed_.end())
+ throw std::logic_error{"on_transactions_relayed empty"};
+
+ std::vector<cryptonote::blobdata> out{std::move(elems->second)};
+ relayed_.erase(elems);
+ return out;
+ }
+ };
+
class test_connection
{
test_endpoint endpoint_;
@@ -146,6 +187,11 @@ namespace
{
return context_.m_connection_id;
}
+
+ bool is_incoming() const noexcept
+ {
+ return context_.m_is_income;
+ }
};
struct received_message
@@ -155,7 +201,7 @@ namespace
std::string payload;
};
- class test_receiver : public epee::levin::levin_commands_handler<cryptonote::levin::detail::p2p_context>
+ class test_receiver final : public epee::levin::levin_commands_handler<cryptonote::levin::detail::p2p_context>
{
std::deque<received_message> invoked_;
std::deque<received_message> notified_;
@@ -253,7 +299,8 @@ namespace
random_generator_(),
io_service_(),
receiver_(),
- contexts_()
+ contexts_(),
+ events_()
{
connections_->set_handler(std::addressof(receiver_), nullptr);
}
@@ -262,6 +309,7 @@ namespace
{
EXPECT_EQ(0u, receiver_.invoked_size());
EXPECT_EQ(0u, receiver_.notified_size());
+ EXPECT_EQ(0u, events_.relayed_method_size());
}
void add_connection(const bool is_incoming)
@@ -283,6 +331,7 @@ namespace
boost::asio::io_service io_service_;
test_receiver receiver_;
std::deque<test_connection> contexts_;
+ test_core_events events_;
};
}
@@ -434,11 +483,11 @@ TEST_F(levin_notify, defaulted)
EXPECT_FALSE(status.has_noise);
EXPECT_FALSE(status.connections_filled);
}
- EXPECT_TRUE(notifier.send_txs({}, random_generator_()));
+ EXPECT_TRUE(notifier.send_txs({}, random_generator_(), events_, cryptonote::relay_method::local));
std::vector<cryptonote::blobdata> txs(2);
txs[0].resize(100, 'e');
- EXPECT_FALSE(notifier.send_txs(std::move(txs), random_generator_()));
+ EXPECT_FALSE(notifier.send_txs(std::move(txs), random_generator_(), events_, cryptonote::relay_method::local));
}
TEST_F(levin_notify, fluff_without_padding)
@@ -455,11 +504,6 @@ TEST_F(levin_notify, fluff_without_padding)
}
notifier.new_out_connection();
io_service_.poll();
- {
- const auto status = notifier.get_status();
- EXPECT_FALSE(status.has_noise);
- EXPECT_FALSE(status.connections_filled); // not tracked
- }
std::vector<cryptonote::blobdata> txs(2);
txs[0].resize(100, 'f');
@@ -468,7 +512,7 @@ TEST_F(levin_notify, fluff_without_padding)
ASSERT_EQ(10u, contexts_.size());
{
auto context = contexts_.begin();
- EXPECT_TRUE(notifier.send_txs(txs, context->get_id()));
+ EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::fluff));
io_service_.reset();
ASSERT_LT(0u, io_service_.poll());
@@ -479,6 +523,7 @@ TEST_F(levin_notify, fluff_without_padding)
for (++context; context != contexts_.end(); ++context)
EXPECT_EQ(1u, context->process_send_queue());
+ EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::fluff));
std::sort(txs.begin(), txs.end());
ASSERT_EQ(9u, receiver_.notified_size());
for (unsigned count = 0; count < 9; ++count)
@@ -486,13 +531,14 @@ TEST_F(levin_notify, fluff_without_padding)
auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
EXPECT_EQ(txs, notification.txs);
EXPECT_TRUE(notification._.empty());
+ EXPECT_TRUE(notification.dandelionpp_fluff);
}
}
}
-TEST_F(levin_notify, fluff_with_padding)
+TEST_F(levin_notify, stem_without_padding)
{
- cryptonote::levin::notify notifier = make_notifier(0, true, true);
+ cryptonote::levin::notify notifier = make_notifier(0, true, false);
for (unsigned count = 0; count < 10; ++count)
add_connection(count % 2 == 0);
@@ -504,41 +550,428 @@ TEST_F(levin_notify, fluff_with_padding)
}
notifier.new_out_connection();
io_service_.poll();
+
+ std::vector<cryptonote::blobdata> txs(2);
+ txs[0].resize(100, 'f');
+ txs[1].resize(200, 'e');
+
+ std::vector<cryptonote::blobdata> sorted_txs = txs;
+ std::sort(sorted_txs.begin(), sorted_txs.end());
+
+ ASSERT_EQ(10u, contexts_.size());
+ bool has_stemmed = false;
+ bool has_fluffed = false;
+ while (!has_stemmed || !has_fluffed)
+ {
+ auto context = contexts_.begin();
+ EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::stem));
+
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+ const bool is_stem = events_.has_stem_txes();
+ EXPECT_EQ(txs, events_.take_relayed(is_stem ? cryptonote::relay_method::stem : cryptonote::relay_method::fluff));
+
+ if (!is_stem)
+ {
+ notifier.run_fluff();
+ ASSERT_LT(0u, io_service_.poll());
+ }
+
+ std::size_t send_count = 0;
+ EXPECT_EQ(0u, context->process_send_queue());
+ for (++context; context != contexts_.end(); ++context)
+ {
+ const std::size_t sent = context->process_send_queue();
+ if (sent && is_stem)
+ EXPECT_EQ(1u, (context - contexts_.begin()) % 2);
+ send_count += sent;
+ }
+
+ EXPECT_EQ(is_stem ? 1u : 9u, send_count);
+ ASSERT_EQ(is_stem ? 1u : 9u, receiver_.notified_size());
+ for (unsigned count = 0; count < (is_stem ? 1u : 9u); ++count)
+ {
+ auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
+ if (is_stem)
+ EXPECT_EQ(txs, notification.txs);
+ else
+ EXPECT_EQ(sorted_txs, notification.txs);
+ EXPECT_TRUE(notification._.empty());
+ EXPECT_EQ(!is_stem, notification.dandelionpp_fluff);
+ }
+
+ has_stemmed |= is_stem;
+ has_fluffed |= !is_stem;
+ notifier.run_epoch();
+ }
+}
+
+TEST_F(levin_notify, local_without_padding)
+{
+ cryptonote::levin::notify notifier = make_notifier(0, true, false);
+
+ for (unsigned count = 0; count < 10; ++count)
+ add_connection(count % 2 == 0);
+
{
const auto status = notifier.get_status();
EXPECT_FALSE(status.has_noise);
- EXPECT_FALSE(status.connections_filled); // not tracked
+ EXPECT_FALSE(status.connections_filled);
}
+ notifier.new_out_connection();
+ io_service_.poll();
std::vector<cryptonote::blobdata> txs(2);
txs[0].resize(100, 'f');
txs[1].resize(200, 'e');
+ std::vector<cryptonote::blobdata> sorted_txs = txs;
+ std::sort(sorted_txs.begin(), sorted_txs.end());
+
ASSERT_EQ(10u, contexts_.size());
+ bool has_stemmed = false;
+ bool has_fluffed = false;
+ while (!has_stemmed || !has_fluffed)
{
auto context = contexts_.begin();
- EXPECT_TRUE(notifier.send_txs(txs, context->get_id()));
+ EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::local));
+
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+ const bool is_stem = events_.has_stem_txes();
+ EXPECT_EQ(txs, events_.take_relayed(is_stem ? cryptonote::relay_method::stem : cryptonote::relay_method::fluff));
+
+ if (!is_stem)
+ {
+ notifier.run_fluff();
+ ASSERT_LT(0u, io_service_.poll());
+ }
+
+ std::size_t send_count = 0;
+ EXPECT_EQ(0u, context->process_send_queue());
+ for (++context; context != contexts_.end(); ++context)
+ {
+ const std::size_t sent = context->process_send_queue();
+ if (sent && is_stem)
+ EXPECT_EQ(1u, (context - contexts_.begin()) % 2);
+ send_count += sent;
+ }
+
+ EXPECT_EQ(is_stem ? 1u : 9u, send_count);
+ ASSERT_EQ(is_stem ? 1u : 9u, receiver_.notified_size());
+ for (unsigned count = 0; count < (is_stem ? 1u : 9u); ++count)
+ {
+ auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
+ if (is_stem)
+ EXPECT_EQ(txs, notification.txs);
+ else
+ EXPECT_EQ(sorted_txs, notification.txs);
+ EXPECT_TRUE(notification._.empty());
+ EXPECT_EQ(!is_stem, notification.dandelionpp_fluff);
+ }
+
+ has_stemmed |= is_stem;
+ has_fluffed |= !is_stem;
+ notifier.run_epoch();
+ }
+}
+
+TEST_F(levin_notify, block_without_padding)
+{
+ cryptonote::levin::notify notifier = make_notifier(0, true, false);
+
+ for (unsigned count = 0; count < 10; ++count)
+ add_connection(count % 2 == 0);
+
+ {
+ const auto status = notifier.get_status();
+ EXPECT_FALSE(status.has_noise);
+ EXPECT_FALSE(status.connections_filled);
+ }
+ notifier.new_out_connection();
+ io_service_.poll();
+
+ std::vector<cryptonote::blobdata> txs(2);
+ txs[0].resize(100, 'e');
+ txs[1].resize(200, 'f');
+
+ ASSERT_EQ(10u, contexts_.size());
+ {
+ auto context = contexts_.begin();
+ EXPECT_FALSE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::block));
+
+ io_service_.reset();
+ ASSERT_EQ(0u, io_service_.poll());
+ }
+}
+
+TEST_F(levin_notify, none_without_padding)
+{
+ cryptonote::levin::notify notifier = make_notifier(0, true, false);
+
+ for (unsigned count = 0; count < 10; ++count)
+ add_connection(count % 2 == 0);
+
+ {
+ const auto status = notifier.get_status();
+ EXPECT_FALSE(status.has_noise);
+ EXPECT_FALSE(status.connections_filled);
+ }
+ notifier.new_out_connection();
+ io_service_.poll();
+
+ std::vector<cryptonote::blobdata> txs(2);
+ txs[0].resize(100, 'e');
+ txs[1].resize(200, 'f');
+
+ ASSERT_EQ(10u, contexts_.size());
+ {
+ auto context = contexts_.begin();
+ EXPECT_FALSE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::none));
+
+ io_service_.reset();
+ ASSERT_EQ(0u, io_service_.poll());
+ }
+}
+
+TEST_F(levin_notify, fluff_with_padding)
+{
+ cryptonote::levin::notify notifier = make_notifier(0, true, true);
+
+ for (unsigned count = 0; count < 10; ++count)
+ add_connection(count % 2 == 0);
+
+ {
+ const auto status = notifier.get_status();
+ EXPECT_FALSE(status.has_noise);
+ EXPECT_FALSE(status.connections_filled);
+ }
+ notifier.new_out_connection();
+ io_service_.poll();
+
+ std::vector<cryptonote::blobdata> txs(2);
+ txs[0].resize(100, 'f');
+ txs[1].resize(200, 'e');
+
+ ASSERT_EQ(10u, contexts_.size());
+ {
+ auto context = contexts_.begin();
+ EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::fluff));
io_service_.reset();
ASSERT_LT(0u, io_service_.poll());
notifier.run_fluff();
ASSERT_LT(0u, io_service_.poll());
+ EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::fluff));
+ std::sort(txs.begin(), txs.end());
EXPECT_EQ(0u, context->process_send_queue());
for (++context; context != contexts_.end(); ++context)
EXPECT_EQ(1u, context->process_send_queue());
- std::sort(txs.begin(), txs.end());
ASSERT_EQ(9u, receiver_.notified_size());
for (unsigned count = 0; count < 9; ++count)
{
auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
EXPECT_EQ(txs, notification.txs);
EXPECT_FALSE(notification._.empty());
+ EXPECT_TRUE(notification.dandelionpp_fluff);
}
}
}
+TEST_F(levin_notify, stem_with_padding)
+{
+ cryptonote::levin::notify notifier = make_notifier(0, true, true);
+
+ for (unsigned count = 0; count < 10; ++count)
+ add_connection(count % 2 == 0);
+
+ {
+ const auto status = notifier.get_status();
+ EXPECT_FALSE(status.has_noise);
+ EXPECT_FALSE(status.connections_filled);
+ }
+ notifier.new_out_connection();
+ io_service_.poll();
+
+ std::vector<cryptonote::blobdata> txs(2);
+ txs[0].resize(100, 'e');
+ txs[1].resize(200, 'f');
+
+ ASSERT_EQ(10u, contexts_.size());
+ bool has_stemmed = false;
+ bool has_fluffed = false;
+ while (!has_stemmed || !has_fluffed)
+ {
+ auto context = contexts_.begin();
+ EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::stem));
+
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+ const bool is_stem = events_.has_stem_txes();
+ EXPECT_EQ(txs, events_.take_relayed(is_stem ? cryptonote::relay_method::stem : cryptonote::relay_method::fluff));
+
+ if (!is_stem)
+ {
+ notifier.run_fluff();
+ ASSERT_LT(0u, io_service_.poll());
+ }
+
+ std::size_t send_count = 0;
+ EXPECT_EQ(0u, context->process_send_queue());
+ for (++context; context != contexts_.end(); ++context)
+ {
+ const std::size_t sent = context->process_send_queue();
+ if (sent && is_stem)
+ {
+ EXPECT_EQ(1u, (context - contexts_.begin()) % 2);
+ EXPECT_FALSE(context->is_incoming());
+ }
+ send_count += sent;
+ }
+
+ EXPECT_EQ(is_stem ? 1u : 9u, send_count);
+ ASSERT_EQ(is_stem ? 1u : 9u, receiver_.notified_size());
+ for (unsigned count = 0; count < (is_stem ? 1u : 9u); ++count)
+ {
+ auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
+ EXPECT_EQ(txs, notification.txs);
+ EXPECT_FALSE(notification._.empty());
+ EXPECT_EQ(!is_stem, notification.dandelionpp_fluff);
+ }
+
+ has_stemmed |= is_stem;
+ has_fluffed |= !is_stem;
+ notifier.run_epoch();
+ }
+}
+
+TEST_F(levin_notify, local_with_padding)
+{
+ cryptonote::levin::notify notifier = make_notifier(0, true, true);
+
+ for (unsigned count = 0; count < 10; ++count)
+ add_connection(count % 2 == 0);
+
+ {
+ const auto status = notifier.get_status();
+ EXPECT_FALSE(status.has_noise);
+ EXPECT_FALSE(status.connections_filled);
+ }
+ notifier.new_out_connection();
+ io_service_.poll();
+
+ std::vector<cryptonote::blobdata> txs(2);
+ txs[0].resize(100, 'e');
+ txs[1].resize(200, 'f');
+
+ ASSERT_EQ(10u, contexts_.size());
+ bool has_stemmed = false;
+ bool has_fluffed = false;
+ while (!has_stemmed || !has_fluffed)
+ {
+ auto context = contexts_.begin();
+ EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::local));
+
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+ const bool is_stem = events_.has_stem_txes();
+ EXPECT_EQ(txs, events_.take_relayed(is_stem ? cryptonote::relay_method::stem : cryptonote::relay_method::fluff));
+
+ if (!is_stem)
+ {
+ notifier.run_fluff();
+ ASSERT_LT(0u, io_service_.poll());
+ }
+
+ std::size_t send_count = 0;
+ EXPECT_EQ(0u, context->process_send_queue());
+ for (++context; context != contexts_.end(); ++context)
+ {
+ const std::size_t sent = context->process_send_queue();
+ if (sent && is_stem)
+ {
+ EXPECT_EQ(1u, (context - contexts_.begin()) % 2);
+ EXPECT_FALSE(context->is_incoming());
+ }
+ send_count += sent;
+ }
+
+ EXPECT_EQ(is_stem ? 1u : 9u, send_count);
+ ASSERT_EQ(is_stem ? 1u : 9u, receiver_.notified_size());
+ for (unsigned count = 0; count < (is_stem ? 1u : 9u); ++count)
+ {
+ auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
+ EXPECT_EQ(txs, notification.txs);
+ EXPECT_FALSE(notification._.empty());
+ EXPECT_EQ(!is_stem, notification.dandelionpp_fluff);
+ }
+
+ has_stemmed |= is_stem;
+ has_fluffed |= !is_stem;
+ notifier.run_epoch();
+ }
+}
+
+TEST_F(levin_notify, block_with_padding)
+{
+ cryptonote::levin::notify notifier = make_notifier(0, true, true);
+
+ for (unsigned count = 0; count < 10; ++count)
+ add_connection(count % 2 == 0);
+
+ {
+ const auto status = notifier.get_status();
+ EXPECT_FALSE(status.has_noise);
+ EXPECT_FALSE(status.connections_filled);
+ }
+ notifier.new_out_connection();
+ io_service_.poll();
+
+ std::vector<cryptonote::blobdata> txs(2);
+ txs[0].resize(100, 'e');
+ txs[1].resize(200, 'f');
+
+ ASSERT_EQ(10u, contexts_.size());
+ {
+ auto context = contexts_.begin();
+ EXPECT_FALSE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::block));
+
+ io_service_.reset();
+ ASSERT_EQ(0u, io_service_.poll());
+ }
+}
+
+TEST_F(levin_notify, none_with_padding)
+{
+ cryptonote::levin::notify notifier = make_notifier(0, true, true);
+
+ for (unsigned count = 0; count < 10; ++count)
+ add_connection(count % 2 == 0);
+
+ {
+ const auto status = notifier.get_status();
+ EXPECT_FALSE(status.has_noise);
+ EXPECT_FALSE(status.connections_filled);
+ }
+ notifier.new_out_connection();
+ io_service_.poll();
+
+ std::vector<cryptonote::blobdata> txs(2);
+ txs[0].resize(100, 'e');
+ txs[1].resize(200, 'f');
+
+ ASSERT_EQ(10u, contexts_.size());
+ {
+ auto context = contexts_.begin();
+ EXPECT_FALSE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::none));
+
+ io_service_.reset();
+ ASSERT_EQ(0u, io_service_.poll());
+ }
+}
+
TEST_F(levin_notify, private_fluff_without_padding)
{
cryptonote::levin::notify notifier = make_notifier(0, false, false);
@@ -553,20 +986,66 @@ TEST_F(levin_notify, private_fluff_without_padding)
}
notifier.new_out_connection();
io_service_.poll();
+
+ std::vector<cryptonote::blobdata> txs(2);
+ txs[0].resize(100, 'e');
+ txs[1].resize(200, 'f');
+
+ ASSERT_EQ(10u, contexts_.size());
+ {
+ auto context = contexts_.begin();
+ EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::fluff));
+
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+ notifier.run_fluff();
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+
+ EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::fluff));
+
+ EXPECT_EQ(0u, context->process_send_queue());
+ for (++context; context != contexts_.end(); ++context)
+ {
+ const bool is_incoming = ((context - contexts_.begin()) % 2 == 0);
+ EXPECT_EQ(is_incoming ? 0u : 1u, context->process_send_queue());
+ }
+
+ ASSERT_EQ(5u, receiver_.notified_size());
+ for (unsigned count = 0; count < 5; ++count)
+ {
+ auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
+ EXPECT_EQ(txs, notification.txs);
+ EXPECT_TRUE(notification._.empty());
+ EXPECT_FALSE(notification.dandelionpp_fluff);
+ }
+ }
+}
+
+TEST_F(levin_notify, private_stem_without_padding)
+{
+ // private mode always uses fluff but marked as stem
+ cryptonote::levin::notify notifier = make_notifier(0, false, false);
+
+ for (unsigned count = 0; count < 10; ++count)
+ add_connection(count % 2 == 0);
+
{
const auto status = notifier.get_status();
EXPECT_FALSE(status.has_noise);
- EXPECT_FALSE(status.connections_filled); // not tracked
+ EXPECT_FALSE(status.connections_filled);
}
+ notifier.new_out_connection();
+ io_service_.poll();
std::vector<cryptonote::blobdata> txs(2);
- txs[0].resize(100, 'f');
- txs[1].resize(200, 'e');
+ txs[0].resize(100, 'e');
+ txs[1].resize(200, 'f');
ASSERT_EQ(10u, contexts_.size());
{
auto context = contexts_.begin();
- EXPECT_TRUE(notifier.send_txs(txs, context->get_id()));
+ EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::stem));
io_service_.reset();
ASSERT_LT(0u, io_service_.poll());
@@ -574,7 +1053,8 @@ TEST_F(levin_notify, private_fluff_without_padding)
io_service_.reset();
ASSERT_LT(0u, io_service_.poll());
- std::sort(txs.begin(), txs.end());
+ EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::fluff));
+
EXPECT_EQ(0u, context->process_send_queue());
for (++context; context != contexts_.end(); ++context)
{
@@ -588,10 +1068,122 @@ TEST_F(levin_notify, private_fluff_without_padding)
auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
EXPECT_EQ(txs, notification.txs);
EXPECT_TRUE(notification._.empty());
+ EXPECT_FALSE(notification.dandelionpp_fluff);
}
}
}
+TEST_F(levin_notify, private_local_without_padding)
+{
+ // private mode always uses fluff but marked as stem
+ cryptonote::levin::notify notifier = make_notifier(0, false, false);
+
+ for (unsigned count = 0; count < 10; ++count)
+ add_connection(count % 2 == 0);
+
+ {
+ const auto status = notifier.get_status();
+ EXPECT_FALSE(status.has_noise);
+ EXPECT_FALSE(status.connections_filled);
+ }
+ notifier.new_out_connection();
+ io_service_.poll();
+
+ std::vector<cryptonote::blobdata> txs(2);
+ txs[0].resize(100, 'e');
+ txs[1].resize(200, 'f');
+
+ ASSERT_EQ(10u, contexts_.size());
+ {
+ auto context = contexts_.begin();
+ EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::local));
+
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+ notifier.run_fluff();
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+
+ EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::local));
+
+ EXPECT_EQ(0u, context->process_send_queue());
+ for (++context; context != contexts_.end(); ++context)
+ {
+ const bool is_incoming = ((context - contexts_.begin()) % 2 == 0);
+ EXPECT_EQ(is_incoming ? 0u : 1u, context->process_send_queue());
+ }
+
+ ASSERT_EQ(5u, receiver_.notified_size());
+ for (unsigned count = 0; count < 5; ++count)
+ {
+ auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
+ EXPECT_EQ(txs, notification.txs);
+ EXPECT_TRUE(notification._.empty());
+ EXPECT_FALSE(notification.dandelionpp_fluff);
+ }
+ }
+}
+
+TEST_F(levin_notify, private_block_without_padding)
+{
+ // private mode always uses fluff but marked as stem
+ cryptonote::levin::notify notifier = make_notifier(0, false, false);
+
+ for (unsigned count = 0; count < 10; ++count)
+ add_connection(count % 2 == 0);
+
+ {
+ const auto status = notifier.get_status();
+ EXPECT_FALSE(status.has_noise);
+ EXPECT_FALSE(status.connections_filled);
+ }
+ notifier.new_out_connection();
+ io_service_.poll();
+
+ std::vector<cryptonote::blobdata> txs(2);
+ txs[0].resize(100, 'e');
+ txs[1].resize(200, 'f');
+
+ ASSERT_EQ(10u, contexts_.size());
+ {
+ auto context = contexts_.begin();
+ EXPECT_FALSE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::block));
+
+ io_service_.reset();
+ ASSERT_EQ(0u, io_service_.poll());
+ }
+}
+
+TEST_F(levin_notify, private_none_without_padding)
+{
+ // private mode always uses fluff but marked as stem
+ cryptonote::levin::notify notifier = make_notifier(0, false, false);
+
+ for (unsigned count = 0; count < 10; ++count)
+ add_connection(count % 2 == 0);
+
+ {
+ const auto status = notifier.get_status();
+ EXPECT_FALSE(status.has_noise);
+ EXPECT_FALSE(status.connections_filled);
+ }
+ notifier.new_out_connection();
+ io_service_.poll();
+
+ std::vector<cryptonote::blobdata> txs(2);
+ txs[0].resize(100, 'e');
+ txs[1].resize(200, 'f');
+
+ ASSERT_EQ(10u, contexts_.size());
+ {
+ auto context = contexts_.begin();
+ EXPECT_FALSE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::none));
+
+ io_service_.reset();
+ ASSERT_EQ(0u, io_service_.poll());
+ }
+}
+
TEST_F(levin_notify, private_fluff_with_padding)
{
cryptonote::levin::notify notifier = make_notifier(0, false, true);
@@ -606,20 +1198,65 @@ TEST_F(levin_notify, private_fluff_with_padding)
}
notifier.new_out_connection();
io_service_.poll();
+
+ std::vector<cryptonote::blobdata> txs(2);
+ txs[0].resize(100, 'e');
+ txs[1].resize(200, 'f');
+
+ ASSERT_EQ(10u, contexts_.size());
+ {
+ auto context = contexts_.begin();
+ EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::fluff));
+
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+ notifier.run_fluff();
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+
+ EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::fluff));
+
+ EXPECT_EQ(0u, context->process_send_queue());
+ for (++context; context != contexts_.end(); ++context)
+ {
+ const bool is_incoming = ((context - contexts_.begin()) % 2 == 0);
+ EXPECT_EQ(is_incoming ? 0u : 1u, context->process_send_queue());
+ }
+
+ ASSERT_EQ(5u, receiver_.notified_size());
+ for (unsigned count = 0; count < 5; ++count)
+ {
+ auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
+ EXPECT_EQ(txs, notification.txs);
+ EXPECT_FALSE(notification._.empty());
+ EXPECT_FALSE(notification.dandelionpp_fluff);
+ }
+ }
+}
+
+TEST_F(levin_notify, private_stem_with_padding)
+{
+ cryptonote::levin::notify notifier = make_notifier(0, false, true);
+
+ for (unsigned count = 0; count < 10; ++count)
+ add_connection(count % 2 == 0);
+
{
const auto status = notifier.get_status();
EXPECT_FALSE(status.has_noise);
- EXPECT_FALSE(status.connections_filled); // not tracked
+ EXPECT_FALSE(status.connections_filled);
}
+ notifier.new_out_connection();
+ io_service_.poll();
std::vector<cryptonote::blobdata> txs(2);
- txs[0].resize(100, 'f');
- txs[1].resize(200, 'e');
+ txs[0].resize(100, 'e');
+ txs[1].resize(200, 'f');
ASSERT_EQ(10u, contexts_.size());
{
auto context = contexts_.begin();
- EXPECT_TRUE(notifier.send_txs(txs, context->get_id()));
+ EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::stem));
io_service_.reset();
ASSERT_LT(0u, io_service_.poll());
@@ -627,7 +1264,58 @@ TEST_F(levin_notify, private_fluff_with_padding)
io_service_.reset();
ASSERT_LT(0u, io_service_.poll());
- std::sort(txs.begin(), txs.end());
+ EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::fluff));
+
+ EXPECT_EQ(0u, context->process_send_queue());
+ for (++context; context != contexts_.end(); ++context)
+ {
+ const bool is_incoming = ((context - contexts_.begin()) % 2 == 0);
+ EXPECT_EQ(is_incoming ? 0u : 1u, context->process_send_queue());
+ }
+
+ ASSERT_EQ(5u, receiver_.notified_size());
+ for (unsigned count = 0; count < 5; ++count)
+ {
+ auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
+ EXPECT_EQ(txs, notification.txs);
+ EXPECT_FALSE(notification._.empty());
+ EXPECT_FALSE(notification.dandelionpp_fluff);
+ }
+ }
+}
+
+TEST_F(levin_notify, private_local_with_padding)
+{
+ cryptonote::levin::notify notifier = make_notifier(0, false, true);
+
+ for (unsigned count = 0; count < 10; ++count)
+ add_connection(count % 2 == 0);
+
+ {
+ const auto status = notifier.get_status();
+ EXPECT_FALSE(status.has_noise);
+ EXPECT_FALSE(status.connections_filled);
+ }
+ notifier.new_out_connection();
+ io_service_.poll();
+
+ std::vector<cryptonote::blobdata> txs(2);
+ txs[0].resize(100, 'e');
+ txs[1].resize(200, 'f');
+
+ ASSERT_EQ(10u, contexts_.size());
+ {
+ auto context = contexts_.begin();
+ EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::local));
+
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+ notifier.run_fluff();
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+
+ EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::local));
+
EXPECT_EQ(0u, context->process_send_queue());
for (++context; context != contexts_.end(); ++context)
{
@@ -641,6 +1329,301 @@ TEST_F(levin_notify, private_fluff_with_padding)
auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
EXPECT_EQ(txs, notification.txs);
EXPECT_FALSE(notification._.empty());
+ EXPECT_FALSE(notification.dandelionpp_fluff);
+ }
+ }
+}
+
+TEST_F(levin_notify, private_block_with_padding)
+{
+ cryptonote::levin::notify notifier = make_notifier(0, false, true);
+
+ for (unsigned count = 0; count < 10; ++count)
+ add_connection(count % 2 == 0);
+
+ {
+ const auto status = notifier.get_status();
+ EXPECT_FALSE(status.has_noise);
+ EXPECT_FALSE(status.connections_filled);
+ }
+ notifier.new_out_connection();
+ io_service_.poll();
+
+ std::vector<cryptonote::blobdata> txs(2);
+ txs[0].resize(100, 'e');
+ txs[1].resize(200, 'f');
+
+ ASSERT_EQ(10u, contexts_.size());
+ {
+ auto context = contexts_.begin();
+ EXPECT_FALSE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::block));
+
+ io_service_.reset();
+ ASSERT_EQ(0u, io_service_.poll());
+ }
+}
+
+TEST_F(levin_notify, private_none_with_padding)
+{
+ cryptonote::levin::notify notifier = make_notifier(0, false, true);
+
+ for (unsigned count = 0; count < 10; ++count)
+ add_connection(count % 2 == 0);
+
+ {
+ const auto status = notifier.get_status();
+ EXPECT_FALSE(status.has_noise);
+ EXPECT_FALSE(status.connections_filled);
+ }
+ notifier.new_out_connection();
+ io_service_.poll();
+
+ std::vector<cryptonote::blobdata> txs(2);
+ txs[0].resize(100, 'e');
+ txs[1].resize(200, 'f');
+
+ ASSERT_EQ(10u, contexts_.size());
+ {
+ auto context = contexts_.begin();
+ EXPECT_FALSE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::none));
+
+ io_service_.reset();
+ ASSERT_EQ(0u, io_service_.poll());
+ }
+}
+
+TEST_F(levin_notify, stem_mappings)
+{
+ static constexpr const unsigned test_connections_count = (CRYPTONOTE_DANDELIONPP_STEMS + 1) * 2;
+
+ cryptonote::levin::notify notifier = make_notifier(0, true, false);
+
+ for (unsigned count = 0; count < test_connections_count; ++count)
+ add_connection(count % 2 == 0);
+
+ {
+ const auto status = notifier.get_status();
+ EXPECT_FALSE(status.has_noise);
+ EXPECT_FALSE(status.connections_filled);
+ }
+ notifier.new_out_connection();
+ io_service_.poll();
+
+ std::vector<cryptonote::blobdata> txs(2);
+ txs[0].resize(100, 'e');
+ txs[1].resize(200, 'f');
+
+ ASSERT_EQ(test_connections_count, contexts_.size());
+ for (;;)
+ {
+ auto context = contexts_.begin();
+ EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::stem));
+
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+ if (events_.has_stem_txes())
+ break;
+
+ EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::fluff));
+ notifier.run_fluff();
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+
+ EXPECT_EQ(0u, context->process_send_queue());
+ for (++context; context != contexts_.end(); ++context)
+ EXPECT_EQ(1u, context->process_send_queue());
+
+ ASSERT_EQ(test_connections_count - 1, receiver_.notified_size());
+ for (unsigned count = 0; count < test_connections_count - 1; ++count)
+ {
+ auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
+ EXPECT_EQ(txs, notification.txs);
+ EXPECT_TRUE(notification._.empty());
+ EXPECT_TRUE(notification.dandelionpp_fluff);
+ }
+
+ notifier.run_epoch();
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+ }
+ EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::stem));
+
+ std::set<boost::uuids::uuid> used;
+ std::map<boost::uuids::uuid, boost::uuids::uuid> mappings;
+ {
+ std::size_t send_count = 0;
+ for (auto context = contexts_.begin(); context != contexts_.end(); ++context)
+ {
+ const std::size_t sent = context->process_send_queue();
+ if (sent)
+ {
+ EXPECT_EQ(1u, (context - contexts_.begin()) % 2);
+ EXPECT_FALSE(context->is_incoming());
+ used.insert(context->get_id());
+ mappings[contexts_.front().get_id()] = context->get_id();
+ }
+ send_count += sent;
+ }
+
+ EXPECT_EQ(1u, send_count);
+ ASSERT_EQ(1u, receiver_.notified_size());
+ for (unsigned count = 0; count < 1u; ++count)
+ {
+ auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
+ EXPECT_EQ(txs, notification.txs);
+ EXPECT_TRUE(notification._.empty());
+ EXPECT_FALSE(notification.dandelionpp_fluff);
+ }
+ }
+
+ for (unsigned i = 0; i < contexts_.size() * 2; i += 2)
+ {
+ auto& incoming = contexts_[i % contexts_.size()];
+ EXPECT_TRUE(notifier.send_txs(txs, incoming.get_id(), events_, cryptonote::relay_method::stem));
+
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+ EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::stem));
+
+ std::size_t send_count = 0;
+ for (auto context = contexts_.begin(); context != contexts_.end(); ++context)
+ {
+ const std::size_t sent = context->process_send_queue();
+ if (sent)
+ {
+ EXPECT_EQ(1u, (context - contexts_.begin()) % 2);
+ EXPECT_FALSE(context->is_incoming());
+ used.insert(context->get_id());
+
+ auto inserted = mappings.emplace(incoming.get_id(), context->get_id()).first;
+ EXPECT_EQ(inserted->second, context->get_id()) << "incoming index " << i;
+ }
+ send_count += sent;
+ }
+
+ EXPECT_EQ(1u, send_count);
+ ASSERT_EQ(1u, receiver_.notified_size());
+ for (unsigned count = 0; count < 1u; ++count)
+ {
+ auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
+ EXPECT_EQ(txs, notification.txs);
+ EXPECT_TRUE(notification._.empty());
+ EXPECT_FALSE(notification.dandelionpp_fluff);
+ }
+ }
+
+ EXPECT_EQ(CRYPTONOTE_DANDELIONPP_STEMS, used.size());
+}
+
+TEST_F(levin_notify, fluff_multiple)
+{
+ static constexpr const unsigned test_connections_count = (CRYPTONOTE_DANDELIONPP_STEMS + 1) * 2;
+
+ cryptonote::levin::notify notifier = make_notifier(0, true, false);
+
+ for (unsigned count = 0; count < test_connections_count; ++count)
+ add_connection(count % 2 == 0);
+
+ {
+ const auto status = notifier.get_status();
+ EXPECT_FALSE(status.has_noise);
+ EXPECT_FALSE(status.connections_filled);
+ }
+ notifier.new_out_connection();
+ io_service_.poll();
+
+ std::vector<cryptonote::blobdata> txs(2);
+ txs[0].resize(100, 'e');
+ txs[1].resize(200, 'f');
+
+ ASSERT_EQ(test_connections_count, contexts_.size());
+ for (;;)
+ {
+ auto context = contexts_.begin();
+ EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), events_, cryptonote::relay_method::stem));
+
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+ if (!events_.has_stem_txes())
+ break;
+
+ EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::stem));
+
+ std::size_t send_count = 0;
+ EXPECT_EQ(0u, context->process_send_queue());
+ for (++context; context != contexts_.end(); ++context)
+ {
+ const std::size_t sent = context->process_send_queue();
+ if (sent)
+ {
+ EXPECT_EQ(1u, (context - contexts_.begin()) % 2);
+ EXPECT_FALSE(context->is_incoming());
+ }
+ send_count += sent;
+ }
+
+ EXPECT_EQ(1u, send_count);
+ ASSERT_EQ(1u, receiver_.notified_size());
+ for (unsigned count = 0; count < 1; ++count)
+ {
+ auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
+ EXPECT_EQ(txs, notification.txs);
+ EXPECT_TRUE(notification._.empty());
+ EXPECT_FALSE(notification.dandelionpp_fluff);
+ }
+
+ notifier.run_epoch();
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+ }
+ EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::fluff));
+ notifier.run_fluff();
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+ {
+ auto context = contexts_.begin();
+ EXPECT_EQ(0u, context->process_send_queue());
+ for (++context; context != contexts_.end(); ++context)
+ EXPECT_EQ(1u, context->process_send_queue());
+
+ ASSERT_EQ(contexts_.size() - 1, receiver_.notified_size());
+ for (unsigned count = 0; count < contexts_.size() - 1; ++count)
+ {
+ auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
+ EXPECT_EQ(txs, notification.txs);
+ EXPECT_TRUE(notification._.empty());
+ EXPECT_TRUE(notification.dandelionpp_fluff);
+ }
+ }
+
+ for (unsigned i = 0; i < contexts_.size() * 2; i += 2)
+ {
+ auto& incoming = contexts_[i % contexts_.size()];
+ EXPECT_TRUE(notifier.send_txs(txs, incoming.get_id(), events_, cryptonote::relay_method::stem));
+
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+ notifier.run_fluff();
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+
+ EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::fluff));
+
+ for (auto& context : contexts_)
+ {
+ if (std::addressof(incoming) == std::addressof(context))
+ EXPECT_EQ(0u, context.process_send_queue());
+ else
+ EXPECT_EQ(1u, context.process_send_queue());
+ }
+
+ ASSERT_EQ(contexts_.size() - 1, receiver_.notified_size());
+ for (unsigned count = 0; count < contexts_.size() - 1; ++count)
+ {
+ auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
+ EXPECT_EQ(txs, notification.txs);
+ EXPECT_TRUE(notification._.empty());
+ EXPECT_TRUE(notification.dandelionpp_fluff);
}
}
}
@@ -680,10 +1663,12 @@ TEST_F(levin_notify, noise)
EXPECT_EQ(0u, receiver_.notified_size());
}
- EXPECT_TRUE(notifier.send_txs(txs, incoming_id));
+ EXPECT_TRUE(notifier.send_txs(txs, incoming_id, events_, cryptonote::relay_method::local));
notifier.run_stems();
io_service_.reset();
ASSERT_LT(0u, io_service_.poll());
+
+ EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::local));
{
std::size_t sent = 0;
for (auto& context : contexts_)
@@ -695,11 +1680,68 @@ TEST_F(levin_notify, noise)
auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
EXPECT_EQ(txs, notification.txs);
EXPECT_TRUE(notification._.empty());
+ EXPECT_FALSE(notification.dandelionpp_fluff);
}
}
txs[0].resize(3000, 'r');
- EXPECT_TRUE(notifier.send_txs(txs, incoming_id));
+ EXPECT_TRUE(notifier.send_txs(txs, incoming_id, events_, cryptonote::relay_method::fluff));
+ notifier.run_stems();
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+
+ EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::fluff));
+ {
+ std::size_t sent = 0;
+ for (auto& context : contexts_)
+ sent += context.process_send_queue();
+
+ EXPECT_EQ(2u, sent);
+ EXPECT_EQ(0u, receiver_.notified_size());
+ }
+
+ notifier.run_stems();
+ io_service_.reset();
+ ASSERT_LT(0u, io_service_.poll());
+ {
+ std::size_t sent = 0;
+ for (auto& context : contexts_)
+ sent += context.process_send_queue();
+
+ ASSERT_EQ(2u, sent);
+ while (sent--)
+ {
+ auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
+ EXPECT_EQ(txs, notification.txs);
+ EXPECT_TRUE(notification._.empty());
+ EXPECT_FALSE(notification.dandelionpp_fluff);
+ }
+ }
+}
+
+TEST_F(levin_notify, noise_stem)
+{
+ for (unsigned count = 0; count < 10; ++count)
+ add_connection(count % 2 == 0);
+
+ std::vector<cryptonote::blobdata> txs(1);
+ txs[0].resize(1900, 'h');
+
+ const boost::uuids::uuid incoming_id = random_generator_();
+ cryptonote::levin::notify notifier = make_notifier(2048, false, true);
+
+ {
+ const auto status = notifier.get_status();
+ EXPECT_TRUE(status.has_noise);
+ EXPECT_FALSE(status.connections_filled);
+ }
+ ASSERT_LT(0u, io_service_.poll());
+ {
+ const auto status = notifier.get_status();
+ EXPECT_TRUE(status.has_noise);
+ EXPECT_TRUE(status.connections_filled);
+ }
+
notifier.run_stems();
io_service_.reset();
ASSERT_LT(0u, io_service_.poll());
@@ -712,9 +1754,13 @@ TEST_F(levin_notify, noise)
EXPECT_EQ(0u, receiver_.notified_size());
}
+ EXPECT_TRUE(notifier.send_txs(txs, incoming_id, events_, cryptonote::relay_method::stem));
notifier.run_stems();
io_service_.reset();
ASSERT_LT(0u, io_service_.poll());
+
+ // downgraded to local when being notified
+ EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::local));
{
std::size_t sent = 0;
for (auto& context : contexts_)
@@ -726,6 +1772,7 @@ TEST_F(levin_notify, noise)
auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
EXPECT_EQ(txs, notification.txs);
EXPECT_TRUE(notification._.empty());
+ EXPECT_FALSE(notification.dandelionpp_fluff);
}
}
}