aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt5
-rw-r--r--src/blockchain_db/blockchain_db.cpp73
-rw-r--r--src/blockchain_db/blockchain_db.h25
-rw-r--r--src/blockchain_db/db_types.h7
-rw-r--r--src/blockchain_db/lmdb/db_lmdb.cpp51
-rw-r--r--src/blockchain_db/lmdb/db_lmdb.h5
-rw-r--r--src/blockchain_utilities/CMakeLists.txt37
-rw-r--r--src/blockchain_utilities/blockchain_export.cpp46
-rw-r--r--src/blockchain_utilities/blockchain_import.cpp454
-rw-r--r--src/blockchain_utilities/blockchain_utilities.h1
-rw-r--r--src/blockchain_utilities/blocksdat_file.h1
-rw-r--r--src/blockchain_utilities/bootstrap_file.cpp4
-rw-r--r--src/blockchain_utilities/fake_core.h133
-rw-r--r--src/blocks/checkpoints.datbin38400036 -> 44480036 bytes
-rw-r--r--src/common/command_line.cpp21
-rw-r--r--src/common/command_line.h3
-rw-r--r--src/common/dns_utils.cpp54
-rw-r--r--src/common/dns_utils.h8
-rw-r--r--src/common/perf_timer.h15
-rw-r--r--src/common/rpc_client.h6
-rw-r--r--src/common/scoped_message_writer.h15
-rw-r--r--src/common/sfinae_helpers.h147
-rw-r--r--src/common/stack_trace.cpp5
-rw-r--r--src/common/stack_trace.h1
-rw-r--r--src/common/thread_group.cpp5
-rw-r--r--src/common/updates.cpp1
-rw-r--r--src/common/util.h9
-rw-r--r--src/crypto/crypto.cpp19
-rw-r--r--src/cryptonote_basic/checkpoints.cpp2
-rw-r--r--src/cryptonote_basic/connection_context.h13
-rw-r--r--src/cryptonote_basic/cryptonote_basic_impl.cpp8
-rw-r--r--src/cryptonote_basic/cryptonote_basic_impl.h13
-rw-r--r--src/cryptonote_basic/cryptonote_format_utils.h1
-rw-r--r--src/cryptonote_basic/difficulty.h4
-rw-r--r--src/cryptonote_basic/miner.cpp120
-rw-r--r--src/cryptonote_config.h5
-rw-r--r--src/cryptonote_core/blockchain.cpp336
-rw-r--r--src/cryptonote_core/blockchain.h98
-rw-r--r--src/cryptonote_core/cryptonote_core.cpp264
-rw-r--r--src/cryptonote_core/cryptonote_core.h103
-rw-r--r--src/cryptonote_core/cryptonote_tx_utils.cpp4
-rw-r--r--src/cryptonote_core/cryptonote_tx_utils.h1
-rw-r--r--src/cryptonote_core/tx_pool.cpp103
-rw-r--r--src/cryptonote_core/tx_pool.h31
-rw-r--r--src/cryptonote_protocol/block_queue.cpp417
-rw-r--r--src/cryptonote_protocol/block_queue.h99
-rw-r--r--src/cryptonote_protocol/cryptonote_protocol_defs.h12
-rw-r--r--src/cryptonote_protocol/cryptonote_protocol_handler-base.cpp14
-rw-r--r--src/cryptonote_protocol/cryptonote_protocol_handler.h16
-rw-r--r--src/cryptonote_protocol/cryptonote_protocol_handler.inl853
-rw-r--r--src/daemon/CMakeLists.txt3
-rw-r--r--src/daemon/command_line_args.h19
-rw-r--r--src/daemon/command_parser_executor.cpp10
-rw-r--r--src/daemon/command_parser_executor.h2
-rw-r--r--src/daemon/command_server.cpp7
-rw-r--r--src/daemon/daemon.cpp43
-rw-r--r--src/daemon/daemon.h2
-rw-r--r--src/daemon/main.cpp14
-rw-r--r--src/daemon/rpc_command_executor.cpp117
-rw-r--r--src/daemon/rpc_command_executor.h7
-rw-r--r--src/daemonizer/posix_daemonizer.inl12
-rw-r--r--src/daemonizer/posix_fork.cpp56
-rw-r--r--src/daemonizer/posix_fork.h15
-rw-r--r--src/debug_utilities/CMakeLists.txt73
-rw-r--r--src/debug_utilities/cn_deserialize.cpp (renamed from src/blockchain_utilities/cn_deserialize.cpp)3
-rw-r--r--src/debug_utilities/object_sizes.cpp116
-rw-r--r--src/mnemonics/CMakeLists.txt3
-rw-r--r--src/mnemonics/electrum-words.cpp9
-rw-r--r--src/mnemonics/esperanto.h1695
-rw-r--r--src/p2p/connection_basic.cpp17
-rw-r--r--src/p2p/connection_basic.hpp4
-rw-r--r--src/p2p/net_node.h5
-rw-r--r--src/p2p/net_node.inl122
-rw-r--r--src/p2p/net_node_common.h5
-rw-r--r--src/p2p/net_peerlist_boost_serialization.h2
-rw-r--r--src/p2p/network_throttle-detail.cpp8
-rw-r--r--src/p2p/network_throttle-detail.hpp2
-rw-r--r--src/p2p/network_throttle.cpp20
-rw-r--r--src/p2p/network_throttle.hpp11
-rw-r--r--src/p2p/p2p_protocol_defs.h17
-rw-r--r--src/rpc/CMakeLists.txt70
-rw-r--r--src/rpc/core_rpc_server.cpp92
-rw-r--r--src/rpc/core_rpc_server.h6
-rw-r--r--src/rpc/core_rpc_server_commands_defs.h106
-rw-r--r--src/rpc/daemon_handler.cpp887
-rw-r--r--src/rpc/daemon_handler.h145
-rw-r--r--src/rpc/daemon_messages.cpp900
-rw-r--r--src/rpc/daemon_messages.h421
-rw-r--r--src/rpc/daemon_rpc_version.h44
-rw-r--r--src/rpc/message.cpp286
-rw-r--r--src/rpc/message.h131
-rw-r--r--src/rpc/message_data_structs.h189
-rw-r--r--src/rpc/rpc_handler.h54
-rw-r--r--src/rpc/zmq_server.cpp141
-rw-r--r--src/rpc/zmq_server.h83
-rw-r--r--src/serialization/CMakeLists.txt54
-rw-r--r--src/serialization/json_object.cpp1165
-rw-r--r--src/serialization/json_object.h371
-rw-r--r--src/simplewallet/simplewallet.cpp646
-rw-r--r--src/simplewallet/simplewallet.h7
-rw-r--r--src/version.h.in4
-rw-r--r--src/wallet/api/pending_transaction.cpp2
-rw-r--r--src/wallet/api/transaction_history.cpp1
-rw-r--r--src/wallet/api/transaction_info.cpp6
-rw-r--r--src/wallet/api/transaction_info.h2
-rw-r--r--src/wallet/api/unsigned_transaction.cpp10
-rw-r--r--src/wallet/api/wallet.cpp54
-rw-r--r--src/wallet/api/wallet.h2
-rw-r--r--src/wallet/api/wallet_manager.cpp4
-rw-r--r--src/wallet/api/wallet_manager.h1
-rw-r--r--src/wallet/node_rpc_proxy.cpp37
-rw-r--r--src/wallet/node_rpc_proxy.h25
-rw-r--r--src/wallet/wallet2.cpp310
-rw-r--r--src/wallet/wallet2.h43
-rw-r--r--src/wallet/wallet2_api.h21
-rw-r--r--src/wallet/wallet_args.cpp6
-rw-r--r--src/wallet/wallet_errors.h4
-rw-r--r--src/wallet/wallet_rpc_server.cpp139
-rw-r--r--src/wallet/wallet_rpc_server.h1
-rw-r--r--src/wallet/wallet_rpc_server_commands_defs.h26
120 files changed, 11140 insertions, 1443 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index d83242a3c..e3cb7cfd6 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -107,6 +107,7 @@ endif()
add_subdirectory(mnemonics)
if(NOT IOS)
add_subdirectory(rpc)
+ add_subdirectory(serialization)
endif()
add_subdirectory(wallet)
if(NOT IOS)
@@ -120,6 +121,10 @@ if(NOT IOS)
add_subdirectory(blockchain_utilities)
endif()
+if(CMAKE_BUILD_TYPE STREQUAL Debug)
+ add_subdirectory(debug_utilities)
+endif()
+
if(PER_BLOCK_CHECKPOINT)
add_subdirectory(blocks)
endif()
diff --git a/src/blockchain_db/blockchain_db.cpp b/src/blockchain_db/blockchain_db.cpp
index 136d4fa80..d62a250ff 100644
--- a/src/blockchain_db/blockchain_db.cpp
+++ b/src/blockchain_db/blockchain_db.cpp
@@ -31,6 +31,20 @@
#include "blockchain_db.h"
#include "cryptonote_basic/cryptonote_format_utils.h"
#include "profile_tools.h"
+#include "ringct/rctOps.h"
+
+#include "lmdb/db_lmdb.h"
+#ifdef BERKELEY_DB
+#include "berkeleydb/db_bdb.h"
+#endif
+
+static const char *db_types[] = {
+ "lmdb",
+#ifdef BERKELEY_DB
+ "berkeley",
+#endif
+ NULL
+};
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "blockchain.db"
@@ -40,6 +54,65 @@ using epee::string_tools::pod_to_hex;
namespace cryptonote
{
+bool blockchain_valid_db_type(const std::string& db_type)
+{
+ int i;
+ for (i=0; db_types[i]; i++)
+ {
+ if (db_types[i] == db_type)
+ return true;
+ }
+ return false;
+}
+
+std::string blockchain_db_types(const std::string& sep)
+{
+ int i;
+ std::string ret = "";
+ for (i=0; db_types[i]; i++)
+ {
+ if (i)
+ ret += sep;
+ ret += db_types[i];
+ }
+ return ret;
+}
+
+std::string arg_db_type_description = "Specify database type, available: " + cryptonote::blockchain_db_types(", ");
+const command_line::arg_descriptor<std::string> arg_db_type = {
+ "db-type"
+, arg_db_type_description.c_str()
+, DEFAULT_DB_TYPE
+};
+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]."
+, "fast:async:1000"
+};
+const command_line::arg_descriptor<bool> arg_db_salvage = {
+ "db-salvage"
+, "Try to salvage a blockchain database if it seems corrupted"
+, false
+};
+
+BlockchainDB *new_db(const std::string& db_type)
+{
+ if (db_type == "lmdb")
+ return new BlockchainLMDB();
+#if defined(BERKELEY_DB)
+ if (db_type == "berkeley")
+ return new BlockchainBDB();
+#endif
+ return NULL;
+}
+
+void BlockchainDB::init_options(boost::program_options::options_description& desc)
+{
+ command_line::add_arg(desc, arg_db_type);
+ command_line::add_arg(desc, arg_db_sync_mode);
+ command_line::add_arg(desc, arg_db_salvage);
+}
+
void BlockchainDB::pop_block()
{
block blk;
diff --git a/src/blockchain_db/blockchain_db.h b/src/blockchain_db/blockchain_db.h
index 27e63801d..85a494ce7 100644
--- a/src/blockchain_db/blockchain_db.h
+++ b/src/blockchain_db/blockchain_db.h
@@ -33,6 +33,8 @@
#include <list>
#include <string>
#include <exception>
+#include <boost/program_options.hpp>
+#include "common/command_line.h"
#include "crypto/hash.h"
#include "cryptonote_protocol/blobdatatype.h"
#include "cryptonote_basic/cryptonote_basic.h"
@@ -101,6 +103,10 @@ namespace cryptonote
/** a pair of <transaction hash, output index>, typedef for convenience */
typedef std::pair<crypto::hash, uint64_t> tx_out_index;
+extern const command_line::arg_descriptor<std::string> arg_db_type;
+extern const command_line::arg_descriptor<std::string> arg_db_sync_mode;
+extern const command_line::arg_descriptor<bool, false> arg_db_salvage;
+
#pragma pack(push, 1)
/**
@@ -145,6 +151,12 @@ struct txpool_tx_meta_t
uint8_t padding[77]; // till 192 bytes
};
+#define DBF_SAFE 1
+#define DBF_FAST 2
+#define DBF_FASTEST 4
+#define DBF_RDONLY 8
+#define DBF_SALVAGE 0x10
+
/***********************************
* Exception Definitions
***********************************/
@@ -530,6 +542,11 @@ public:
virtual ~BlockchainDB() { };
/**
+ * @brief init command line options
+ */
+ static void init_options(boost::program_options::options_description& desc);
+
+ /**
* @brief reset profiling stats
*/
void reset_stats();
@@ -600,6 +617,13 @@ public:
virtual void sync() = 0;
/**
+ * @brief toggle safe syncs for the DB
+ *
+ * Used to switch DBF_SAFE on or off after starting up with DBF_FAST.
+ */
+ virtual void safesyncmode(const bool onoff) = 0;
+
+ /**
* @brief Remove everything from the BlockchainDB
*
* This function should completely remove all data from a BlockchainDB.
@@ -1491,6 +1515,7 @@ public:
}; // class BlockchainDB
+BlockchainDB *new_db(const std::string& db_type);
} // namespace cryptonote
diff --git a/src/blockchain_db/db_types.h b/src/blockchain_db/db_types.h
index 6c21b029e..8e2f58a61 100644
--- a/src/blockchain_db/db_types.h
+++ b/src/blockchain_db/db_types.h
@@ -31,9 +31,6 @@
namespace cryptonote
{
-
- const std::unordered_set<std::string> blockchain_db_types =
- { "lmdb"
- };
-
+ bool blockchain_valid_db_type(const std::string& db_type);
+ std::string blockchain_db_types(const std::string& sep);
} // namespace cryptonote
diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp
index 6bb96d1db..b6978bdc4 100644
--- a/src/blockchain_db/lmdb/db_lmdb.cpp
+++ b/src/blockchain_db/lmdb/db_lmdb.cpp
@@ -37,6 +37,7 @@
#include "cryptonote_basic/cryptonote_format_utils.h"
#include "crypto/crypto.h"
#include "profile_tools.h"
+#include "ringct/rctOps.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "blockchain.db.lmdb"
@@ -212,7 +213,7 @@ const std::string lmdb_error(const std::string& error_string, int mdb_res)
inline void lmdb_db_open(MDB_txn* txn, const char* name, int flags, MDB_dbi& dbi, const std::string& error_string)
{
if (auto res = mdb_dbi_open(txn, name, flags, &dbi))
- throw0(cryptonote::DB_OPEN_FAILURE(lmdb_error(error_string + " : ", res).c_str()));
+ throw0(cryptonote::DB_OPEN_FAILURE((lmdb_error(error_string + " : ", res) + std::string(" - you may want to start with --db-salvage")).c_str()));
}
@@ -1082,9 +1083,10 @@ BlockchainLMDB::BlockchainLMDB(bool batch_transactions)
m_hardfork = nullptr;
}
-void BlockchainLMDB::open(const std::string& filename, const int mdb_flags)
+void BlockchainLMDB::open(const std::string& filename, const int db_flags)
{
int result;
+ int mdb_flags = MDB_NORDAHEAD;
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
@@ -1123,6 +1125,15 @@ void BlockchainLMDB::open(const std::string& filename, const int mdb_flags)
size_t mapsize = DEFAULT_MAPSIZE;
+ if (db_flags & DBF_FAST)
+ mdb_flags |= MDB_NOSYNC;
+ if (db_flags & DBF_FASTEST)
+ mdb_flags |= MDB_NOSYNC | MDB_WRITEMAP | MDB_MAPASYNC;
+ if (db_flags & DBF_RDONLY)
+ mdb_flags = MDB_RDONLY;
+ if (db_flags & DBF_SALVAGE)
+ mdb_flags |= MDB_PREVSNAPSHOT;
+
if (auto result = mdb_env_open(m_env, filename.c_str(), mdb_flags, 0644))
throw0(DB_ERROR(lmdb_error("Failed to open lmdb environment: ", result).c_str()));
@@ -1307,6 +1318,11 @@ void BlockchainLMDB::sync()
}
}
+void BlockchainLMDB::safesyncmode(const bool onoff)
+{
+ mdb_env_set_flags(m_env, MDB_NOSYNC|MDB_MAPASYNC, !onoff);
+}
+
void BlockchainLMDB::reset()
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
@@ -2588,6 +2604,16 @@ void BlockchainLMDB::batch_commit()
memset(&m_wcursors, 0, sizeof(m_wcursors));
}
+void BlockchainLMDB::cleanup_batch()
+{
+ // for destruction of batch transaction
+ m_write_txn = nullptr;
+ delete m_write_batch_txn;
+ m_write_batch_txn = nullptr;
+ m_batch_active = false;
+ memset(&m_wcursors, 0, sizeof(m_wcursors));
+}
+
void BlockchainLMDB::batch_stop()
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
@@ -2602,15 +2628,18 @@ void BlockchainLMDB::batch_stop()
check_open();
LOG_PRINT_L3("batch transaction: committing...");
TIME_MEASURE_START(time1);
- m_write_txn->commit();
- TIME_MEASURE_FINISH(time1);
- time_commit1 += time1;
- // for destruction of batch transaction
- m_write_txn = nullptr;
- delete m_write_batch_txn;
- m_write_batch_txn = nullptr;
- m_batch_active = false;
- memset(&m_wcursors, 0, sizeof(m_wcursors));
+ try
+ {
+ m_write_txn->commit();
+ TIME_MEASURE_FINISH(time1);
+ time_commit1 += time1;
+ cleanup_batch();
+ }
+ catch (const std::exception &e)
+ {
+ cleanup_batch();
+ throw;
+ }
LOG_PRINT_L3("batch transaction: end");
}
diff --git a/src/blockchain_db/lmdb/db_lmdb.h b/src/blockchain_db/lmdb/db_lmdb.h
index 14e5d34e2..90274b904 100644
--- a/src/blockchain_db/lmdb/db_lmdb.h
+++ b/src/blockchain_db/lmdb/db_lmdb.h
@@ -165,6 +165,8 @@ public:
virtual void sync();
+ virtual void safesyncmode(const bool onoff);
+
virtual void reset();
virtual std::vector<std::string> get_filenames() const;
@@ -366,6 +368,9 @@ private:
// migrate from DB version 0 to 1
void migrate_0_1();
+ void cleanup_batch();
+
+private:
MDB_env* m_env;
MDB_dbi m_blocks;
diff --git a/src/blockchain_utilities/CMakeLists.txt b/src/blockchain_utilities/CMakeLists.txt
index bb23cdc8b..ffdaad4af 100644
--- a/src/blockchain_utilities/CMakeLists.txt
+++ b/src/blockchain_utilities/CMakeLists.txt
@@ -26,6 +26,16 @@
# 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.
+set(blocksdat "")
+if(PER_BLOCK_CHECKPOINT)
+ if(APPLE)
+ add_custom_command(OUTPUT blocksdat.o MAIN_DEPENDENCY ../blocks/checkpoints.dat COMMAND cd ${CMAKE_CURRENT_SOURCE_DIR} && touch stub.c && ${CMAKE_C_COMPILER} -o stub.o -c stub.c COMMAND cd ${CMAKE_CURRENT_SOURCE_DIR} && ${CMAKE_LINKER} -r -sectcreate __DATA __blocks_dat ../blocks/checkpoints.dat -o ${CMAKE_CURRENT_BINARY_DIR}/blocksdat.o stub.o && rm -f stub.*)
+ else()
+ add_custom_command(OUTPUT blocksdat.o MAIN_DEPENDENCY ../blocks/checkpoints.dat COMMAND cd ${CMAKE_CURRENT_SOURCE_DIR} && cp ../blocks/checkpoints.dat blocks.dat && ${CMAKE_LINKER} -r -b binary -o ${CMAKE_CURRENT_BINARY_DIR}/blocksdat.o blocks.dat && rm -f blocks.dat)
+ endif()
+ set(blocksdat "blocksdat.o")
+endif()
+
set(blockchain_import_sources
blockchain_import.cpp
bootstrap_file.cpp
@@ -33,7 +43,6 @@ set(blockchain_import_sources
)
set(blockchain_import_private_headers
- fake_core.h
bootstrap_file.h
blocksdat_file.h
bootstrap_serialization.h
@@ -58,14 +67,10 @@ monero_private_headers(blockchain_export
${blockchain_export_private_headers})
-set(cn_deserialize_sources
- cn_deserialize.cpp
- )
-
-
monero_add_executable(blockchain_import
${blockchain_import_sources}
- ${blockchain_import_private_headers})
+ ${blockchain_import_private_headers}
+ ${blocksdat})
target_link_libraries(blockchain_import
PRIVATE
@@ -112,21 +117,3 @@ set_property(TARGET blockchain_export
PROPERTY
OUTPUT_NAME "monero-blockchain-export")
-monero_add_executable(cn_deserialize
- ${cn_deserialize_sources}
- ${cn_deserialize_private_headers})
-
-target_link_libraries(cn_deserialize
- LINK_PRIVATE
- cryptonote_core
- blockchain_db
- p2p
- epee
- ${CMAKE_THREAD_LIBS_INIT})
-
-add_dependencies(cn_deserialize
- version)
-set_property(TARGET cn_deserialize
- PROPERTY
- OUTPUT_NAME "monero-utils-deserialize")
-
diff --git a/src/blockchain_utilities/blockchain_export.cpp b/src/blockchain_utilities/blockchain_export.cpp
index f145bc107..20eca09f2 100644
--- a/src/blockchain_utilities/blockchain_export.cpp
+++ b/src/blockchain_utilities/blockchain_export.cpp
@@ -31,10 +31,6 @@
#include "common/command_line.h"
#include "cryptonote_core/tx_pool.h"
#include "blockchain_db/blockchain_db.h"
-#include "blockchain_db/lmdb/db_lmdb.h"
-#if defined(BERKELEY_DB)
-#include "blockchain_db/berkeleydb/db_bdb.h"
-#endif
#include "blockchain_db/db_types.h"
#include "version.h"
@@ -44,17 +40,6 @@
namespace po = boost::program_options;
using namespace epee;
-std::string join_set_strings(const std::unordered_set<std::string>& db_types_all, const char* delim)
-{
- std::string result;
- std::ostringstream s;
- std::copy(db_types_all.begin(), db_types_all.end(), std::ostream_iterator<std::string>(s, delim));
- result = s.str();
- if (result.length() > 0)
- result.erase(result.end()-strlen(delim), result.end());
- return result;
-}
-
int main(int argc, char* argv[])
{
TRY_ENTRY();
@@ -63,10 +48,7 @@ int main(int argc, char* argv[])
std::string default_db_type = "lmdb";
- std::unordered_set<std::string> db_types_all = cryptonote::blockchain_db_types;
- db_types_all.insert("memory");
-
- std::string available_dbs = join_set_strings(db_types_all, ", ");
+ std::string available_dbs = cryptonote::blockchain_db_types(", ");
available_dbs = "available: " + available_dbs;
uint32_t log_level = 0;
@@ -144,18 +126,11 @@ int main(int argc, char* argv[])
m_config_folder = command_line::get_arg(vm, data_dir_arg);
std::string db_type = command_line::get_arg(vm, arg_database);
- if (db_types_all.count(db_type) == 0)
+ if (!cryptonote::blockchain_valid_db_type(db_type))
{
std::cerr << "Invalid database type: " << db_type << std::endl;
return 1;
}
-#if !defined(BERKELEY_DB)
- if (db_type == "berkeley")
- {
- LOG_ERROR("BerkeleyDB support disabled.");
- return false;
- }
-#endif
if (command_line::has_arg(vm, arg_output_file))
output_file_path = boost::filesystem::path(command_line::get_arg(vm, arg_output_file));
@@ -179,19 +154,8 @@ int main(int argc, char* argv[])
tx_memory_pool m_mempool(*core_storage);
core_storage = new Blockchain(m_mempool);
- int db_flags = 0;
-
- BlockchainDB* db = nullptr;
- if (db_type == "lmdb")
- {
- db_flags |= MDB_RDONLY;
- db = new BlockchainLMDB();
- }
-#if defined(BERKELEY_DB)
- else if (db_type == "berkeley")
- db = new BlockchainBDB();
-#endif
- else
+ BlockchainDB* db = new_db(db_type);
+ if (db == NULL)
{
LOG_ERROR("Attempted to use non-existent database type: " << db_type);
throw std::runtime_error("Attempting to use non-existent database type");
@@ -205,7 +169,7 @@ int main(int argc, char* argv[])
LOG_PRINT_L0("Loading blockchain from folder " << filename << " ...");
try
{
- db->open(filename, db_flags);
+ db->open(filename, DBF_RDONLY);
}
catch (const std::exception& e)
{
diff --git a/src/blockchain_utilities/blockchain_import.cpp b/src/blockchain_utilities/blockchain_import.cpp
index a43e2b4ad..635a70b10 100644
--- a/src/blockchain_utilities/blockchain_import.cpp
+++ b/src/blockchain_utilities/blockchain_import.cpp
@@ -32,6 +32,7 @@
#include <fstream>
#include <boost/filesystem.hpp>
+#include "misc_log_ex.h"
#include "bootstrap_file.h"
#include "bootstrap_serialization.h"
#include "cryptonote_basic/cryptonote_format_utils.h"
@@ -39,10 +40,7 @@
#include "serialization/json_utils.h" // dump_json()
#include "include_base_utils.h"
#include "blockchain_db/db_types.h"
-
-#include <lmdb.h> // for db flag arguments
-
-#include "fake_core.h"
+#include "cryptonote_core/cryptonote_core.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "bcutil"
@@ -78,40 +76,16 @@ namespace po = boost::program_options;
using namespace cryptonote;
using namespace epee;
-
-std::string join_set_strings(const std::unordered_set<std::string>& db_types_all, const char* delim)
-{
- std::string result;
- std::ostringstream s;
- std::copy(db_types_all.begin(), db_types_all.end(), std::ostream_iterator<std::string>(s, delim));
- result = s.str();
- if (result.length() > 0)
- result.erase(result.end()-strlen(delim), result.end());
- return result;
-}
-
-// db_type: lmdb, berkeley
// db_mode: safe, fast, fastest
-int get_db_flags_from_mode(const std::string& db_type, const std::string& db_mode)
+int get_db_flags_from_mode(const std::string& db_mode)
{
- uint64_t BDB_FAST_MODE = 0;
- uint64_t BDB_FASTEST_MODE = 0;
- uint64_t BDB_SAFE_MODE = 0;
-
-#if defined(BERKELEY_DB)
- BDB_FAST_MODE = DB_TXN_WRITE_NOSYNC;
- BDB_FASTEST_MODE = DB_TXN_NOSYNC;
- BDB_SAFE_MODE = DB_TXN_SYNC;
-#endif
-
int db_flags = 0;
- bool islmdb = db_type == "lmdb";
if (db_mode == "safe")
- db_flags = islmdb ? MDB_NORDAHEAD : BDB_SAFE_MODE;
+ db_flags = DBF_SAFE;
else if (db_mode == "fast")
- db_flags = islmdb ? MDB_NOMETASYNC | MDB_NOSYNC | MDB_NORDAHEAD : BDB_FAST_MODE;
+ db_flags = DBF_FAST;
else if (db_mode == "fastest")
- db_flags = islmdb ? MDB_WRITEMAP | MDB_MAPASYNC | MDB_NORDAHEAD | MDB_NOMETASYNC | MDB_NOSYNC : BDB_FASTEST_MODE;
+ db_flags = DBF_FASTEST;
return db_flags;
}
@@ -132,14 +106,6 @@ int parse_db_arguments(const std::string& db_arg_str, std::string& db_type, int&
return 1;
}
-#if !defined(BERKELEY_DB)
- if (db_type == "berkeley")
- {
- MFATAL("BerkeleyDB support disabled.");
- return false;
- }
-#endif
-
std::string db_arg_str2 = db_args[1];
boost::split(db_args, db_arg_str2, boost::is_any_of(","));
@@ -155,70 +121,18 @@ int parse_db_arguments(const std::string& db_arg_str, std::string& db_type, int&
}
if (! db_mode.empty())
{
- db_flags = get_db_flags_from_mode(db_type, db_mode);
- }
- else
- {
- for (auto& it : db_args)
- {
- boost::algorithm::trim(it);
- if (it.empty())
- continue;
- if (db_type == "lmdb")
- {
- MINFO("LMDB flag: " << it);
- if (it == "nosync")
- db_flags |= MDB_NOSYNC;
- else if (it == "nometasync")
- db_flags |= MDB_NOMETASYNC;
- else if (it == "writemap")
- db_flags |= MDB_WRITEMAP;
- else if (it == "mapasync")
- db_flags |= MDB_MAPASYNC;
- else if (it == "nordahead")
- db_flags |= MDB_NORDAHEAD;
- else
- {
- std::cerr << "unrecognized database flag: " << it << ENDL;
- return 1;
- }
- }
-#if defined(BERKELEY_DB)
- else if (db_type == "berkeley")
- {
- if (it == "txn_write_nosync")
- db_flags = DB_TXN_WRITE_NOSYNC;
- else if (it == "txn_nosync")
- db_flags = DB_TXN_NOSYNC;
- else if (it == "txn_sync")
- db_flags = DB_TXN_SYNC;
- else
- {
- std::cerr << "unrecognized database flag: " << it << ENDL;
- return 1;
- }
- }
-#endif
- }
+ db_flags = get_db_flags_from_mode(db_mode);
}
return 0;
}
-template <typename FakeCore>
-int pop_blocks(FakeCore& simple_core, int num_blocks)
+int pop_blocks(cryptonote::core& core, int num_blocks)
{
- bool use_batch = false;
- if (opt_batch)
- {
- if (simple_core.support_batch)
- use_batch = true;
- else
- MWARNING("WARNING: batch transactions enabled but unsupported or unnecessary for this database type - ignoring");
- }
+ bool use_batch = opt_batch;
if (use_batch)
- simple_core.batch_start();
+ core.get_blockchain_storage().get_db().batch_start();
int quit = 0;
block popped_block;
@@ -226,12 +140,11 @@ int pop_blocks(FakeCore& simple_core, int num_blocks)
for (int i=0; i < num_blocks; ++i)
{
// simple_core.m_storage.pop_block_from_blockchain() is private, so call directly through db
- simple_core.m_storage.get_db().pop_block(popped_block, popped_txs);
+ core.get_blockchain_storage().get_db().pop_block(popped_block, popped_txs);
quit = 1;
}
-
if (use_batch)
{
if (quit > 1)
@@ -241,24 +154,74 @@ int pop_blocks(FakeCore& simple_core, int num_blocks)
}
else
{
- simple_core.batch_stop();
+ core.get_blockchain_storage().get_db().batch_stop();
}
- simple_core.m_storage.get_db().show_stats();
+ core.get_blockchain_storage().get_db().show_stats();
}
return num_blocks;
}
-template <typename FakeCore>
-int import_from_file(FakeCore& simple_core, const std::string& import_file_path, uint64_t block_stop=0)
+int check_flush(cryptonote::core &core, std::list<block_complete_entry> &blocks, bool force)
{
- if (std::is_same<fake_core_db, FakeCore>::value)
+ if (blocks.empty())
+ return 0;
+ if (!force && blocks.size() < db_batch_size)
+ return 0;
+
+ core.prepare_handle_incoming_blocks(blocks);
+
+ for(const block_complete_entry& block_entry: blocks)
{
- // Reset stats, in case we're using newly created db, accumulating stats
- // from addition of genesis block.
- // This aligns internal db counts with importer counts.
- simple_core.m_storage.get_db().reset_stats();
- }
+ // process transactions
+ for(auto& tx_blob: block_entry.txs)
+ {
+ tx_verification_context tvc = AUTO_VAL_INIT(tvc);
+ core.handle_incoming_tx(tx_blob, tvc, true, true, false);
+ if(tvc.m_verifivation_failed)
+ {
+ MERROR("transaction verification failed, tx_id = "
+ << epee::string_tools::pod_to_hex(get_blob_hash(tx_blob)));
+ core.cleanup_handle_incoming_blocks();
+ return 1;
+ }
+ }
+
+ // process block
+
+ block_verification_context bvc = boost::value_initialized<block_verification_context>();
+
+ core.handle_incoming_block(block_entry.block, bvc, false); // <--- process block
+
+ if(bvc.m_verifivation_failed)
+ {
+ MERROR("Block verification failed, id = "
+ << epee::string_tools::pod_to_hex(get_blob_hash(block_entry.block)));
+ core.cleanup_handle_incoming_blocks();
+ return 1;
+ }
+ if(bvc.m_marked_as_orphaned)
+ {
+ MERROR("Block received at sync phase was marked as orphaned");
+ core.cleanup_handle_incoming_blocks();
+ return 1;
+ }
+
+ } // each download block
+ if (!core.cleanup_handle_incoming_blocks())
+ return 1;
+
+ blocks.clear();
+ return 0;
+}
+
+int import_from_file(cryptonote::core& core, const std::string& import_file_path, uint64_t block_stop=0)
+{
+ // Reset stats, in case we're using newly created db, accumulating stats
+ // from addition of genesis block.
+ // This aligns internal db counts with importer counts.
+ core.get_blockchain_storage().get_db().reset_stats();
+
boost::filesystem::path fs_import_file_path(import_file_path);
boost::system::error_code ec;
if (!boost::filesystem::exists(fs_import_file_path, ec))
@@ -300,7 +263,7 @@ int import_from_file(FakeCore& simple_core, const std::string& import_file_path,
uint64_t start_height = 1;
if (opt_resume)
- start_height = simple_core.m_storage.get_current_blockchain_height();
+ start_height = core.get_blockchain_storage().get_current_blockchain_height();
// Note that a new blockchain will start with block number 0 (total blocks: 1)
// due to genesis block being added at initialization.
@@ -315,21 +278,16 @@ int import_from_file(FakeCore& simple_core, const std::string& import_file_path,
MINFO("start block: " << start_height << " stop block: " <<
block_stop);
- bool use_batch = false;
- if (opt_batch)
- {
- if (simple_core.support_batch)
- use_batch = true;
- else
- MWARNING("WARNING: batch transactions enabled but unsupported or unnecessary for this database type - ignoring");
- }
+ bool use_batch = opt_batch && !opt_verify;
if (use_batch)
- simple_core.batch_start(db_batch_size);
+ core.get_blockchain_storage().get_db().batch_start(db_batch_size);
MINFO("Reading blockchain from bootstrap file...");
std::cout << ENDL;
+ std::list<block_complete_entry> blocks;
+
// Within the loop, we skip to start_height before we start adding.
// TODO: Not a bottleneck, but we can use what's done in count_blocks() and
// only do the chunk size reads, skipping the chunk content reads until we're
@@ -359,9 +317,9 @@ int import_from_file(FakeCore& simple_core, const std::string& import_file_path,
MWARNING("WARNING: chunk_size " << chunk_size << " > BUFFER_SIZE " << BUFFER_SIZE);
throw std::runtime_error("Aborting: chunk size exceeds buffer size");
}
- if (chunk_size > 100000)
+ if (chunk_size > CHUNK_SIZE_WARNING_THRESHOLD)
{
- MINFO("NOTE: chunk_size " << chunk_size << " > 100000");
+ MINFO("NOTE: chunk_size " << chunk_size << " > " << CHUNK_SIZE_WARNING_THRESHOLD);
}
else if (chunk_size == 0) {
MFATAL("ERROR: chunk_size == 0");
@@ -369,9 +327,19 @@ int import_from_file(FakeCore& simple_core, const std::string& import_file_path,
}
import_file.read(buffer_block, chunk_size);
if (! import_file) {
- MFATAL("ERROR: unexpected end of file: bytes read before error: "
- << import_file.gcount() << " of chunk_size " << chunk_size);
- return 2;
+ if (import_file.eof())
+ {
+ std::cout << refresh_string;
+ MINFO("End of file reached - file was truncated");
+ quit = 1;
+ break;
+ }
+ else
+ {
+ MFATAL("ERROR: unexpected end of file: bytes read before error: "
+ << import_file.gcount() << " of chunk_size " << chunk_size);
+ return 2;
+ }
}
bytes_read += chunk_size;
MDEBUG("Total bytes read: " << bytes_read);
@@ -424,67 +392,37 @@ int import_from_file(FakeCore& simple_core, const std::string& import_file_path,
<< std::flush;
}
- std::vector<transaction> txs;
- std::vector<transaction> archived_txs;
-
- archived_txs = bp.txs;
-
- // std::cout << refresh_string;
- // MDEBUG("txs: " << archived_txs.size());
-
- // if archived_txs is invalid
- // {
- // std::cout << refresh_string;
- // MFATAL("exception while de-archiving txs, height=" << h);
- // quit = 1;
- // break;
- // }
-
- // tx number 1: coinbase tx
- // tx number 2 onwards: archived_txs
- unsigned int tx_num = 1;
- for (transaction tx : archived_txs)
+ if (opt_verify)
{
- ++tx_num;
- // if tx is invalid
- // {
- // MFATAL("exception while indexing tx from txs, height=" << h <<", tx_num=" << tx_num);
- // quit = 1;
- // break;
- // }
-
- // std::cout << refresh_string;
- // MDEBUG("tx hash: " << get_transaction_hash(tx));
-
- // crypto::hash hsh = null_hash;
- // size_t blob_size = 0;
- // NOTE: all tx hashes except for coinbase tx are available in the block data
- // get_transaction_hash(tx, hsh, blob_size);
- // MDEBUG("tx " << tx_num << " " << hsh << " : " << ENDL);
- // MDEBUG(obj_to_json_str(tx) << ENDL);
-
- // add blocks with verification.
- // for Blockchain and blockchain_storage add_new_block().
- if (opt_verify)
+ cryptonote::blobdata block;
+ cryptonote::block_to_blob(bp.block, block);
+ std::list<cryptonote::blobdata> txs;
+ for (const auto &tx: bp.txs)
+ {
+ txs.push_back(cryptonote::blobdata());
+ cryptonote::tx_to_blob(tx, txs.back());
+ }
+ blocks.push_back({block, txs});
+ int ret = check_flush(core, blocks, false);
+ if (ret)
{
- // crypto::hash hsh = null_hash;
- // size_t blob_size = 0;
- // get_transaction_hash(tx, hsh, blob_size);
+ quit = 2; // make sure we don't commit partial block data
+ break;
+ }
+ }
+ else
+ {
+ std::vector<transaction> txs;
+ std::vector<transaction> archived_txs;
+ archived_txs = bp.txs;
- uint8_t version = simple_core.m_storage.get_current_hard_fork_version();
- tx_verification_context tvc = AUTO_VAL_INIT(tvc);
- bool r = true;
- r = simple_core.m_pool.add_tx(tx, tvc, true, true, false, version);
- if (!r)
- {
- MFATAL("failed to add transaction to transaction pool, height=" << h <<", tx_num=" << tx_num);
- quit = 1;
- break;
- }
- }
- else
+ // tx number 1: coinbase tx
+ // tx number 2 onwards: archived_txs
+ for (transaction tx : archived_txs)
{
+ // add blocks with verification.
+ // for Blockchain and blockchain_storage add_new_block().
// for add_block() method, without (much) processing.
// don't add coinbase transaction to txs.
//
@@ -493,34 +431,7 @@ int import_from_file(FakeCore& simple_core, const std::string& import_file_path,
// then a for loop for the transactions in txs.
txs.push_back(tx);
}
- }
-
- if (opt_verify)
- {
- block_verification_context bvc = boost::value_initialized<block_verification_context>();
- simple_core.m_storage.add_new_block(b, bvc);
- if (bvc.m_verifivation_failed)
- {
- MFATAL("Failed to add block to blockchain, verification failed, height = " << h);
- MFATAL("skipping rest of file");
- // ok to commit previously batched data because it failed only in
- // verification of potential new block with nothing added to batch
- // yet
- quit = 1;
- break;
- }
- if (! bvc.m_added_to_main_chain)
- {
- MFATAL("Failed to add block to blockchain, height = " << h);
- MFATAL("skipping rest of file");
- // make sure we don't commit partial block data
- quit = 2;
- break;
- }
- }
- else
- {
size_t block_size;
difficulty_type cumulative_difficulty;
uint64_t coins_generated;
@@ -529,14 +440,9 @@ int import_from_file(FakeCore& simple_core, const std::string& import_file_path,
cumulative_difficulty = bp.cumulative_difficulty;
coins_generated = bp.coins_generated;
- // std::cout << refresh_string;
- // MDEBUG("block_size: " << block_size);
- // MDEBUG("cumulative_difficulty: " << cumulative_difficulty);
- // MDEBUG("coins_generated: " << coins_generated);
-
try
{
- simple_core.add_block(b, block_size, cumulative_difficulty, coins_generated, txs);
+ core.get_blockchain_storage().get_db().add_block(b, block_size, cumulative_difficulty, coins_generated, txs);
}
catch (const std::exception& e)
{
@@ -545,22 +451,22 @@ int import_from_file(FakeCore& simple_core, const std::string& import_file_path,
quit = 2; // make sure we don't commit partial block data
break;
}
- }
- ++num_imported;
- if (use_batch)
- {
- if ((h-1) % db_batch_size == 0)
+ if (use_batch)
{
- std::cout << refresh_string;
- // zero-based height
- std::cout << ENDL << "[- batch commit at height " << h-1 << " -]" << ENDL;
- simple_core.batch_stop();
- simple_core.batch_start(db_batch_size);
- std::cout << ENDL;
- simple_core.m_storage.get_db().show_stats();
+ if ((h-1) % db_batch_size == 0)
+ {
+ std::cout << refresh_string;
+ // zero-based height
+ std::cout << ENDL << "[- batch commit at height " << h-1 << " -]" << ENDL;
+ core.get_blockchain_storage().get_db().batch_stop();
+ core.get_blockchain_storage().get_db().batch_start(db_batch_size);
+ std::cout << ENDL;
+ core.get_blockchain_storage().get_db().show_stats();
+ }
}
}
+ ++num_imported;
}
}
catch (const std::exception& e)
@@ -573,6 +479,13 @@ int import_from_file(FakeCore& simple_core, const std::string& import_file_path,
import_file.close();
+ if (opt_verify)
+ {
+ int ret = check_flush(core, blocks, true);
+ if (ret)
+ return ret;
+ }
+
if (use_batch)
{
if (quit > 1)
@@ -582,14 +495,16 @@ int import_from_file(FakeCore& simple_core, const std::string& import_file_path,
}
else
{
- simple_core.batch_stop();
+ core.get_blockchain_storage().get_db().batch_stop();
}
- simple_core.m_storage.get_db().show_stats();
- MINFO("Number of blocks imported: " << num_imported);
- if (h > 0)
- // TODO: if there was an error, the last added block is probably at zero-based height h-2
- MINFO("Finished at block: " << h-1 << " total blocks: " << h);
}
+
+ core.get_blockchain_storage().get_db().show_stats();
+ MINFO("Number of blocks imported: " << num_imported);
+ if (h > 0)
+ // TODO: if there was an error, the last added block is probably at zero-based height h-2
+ MINFO("Finished at block: " << h-1 << " total blocks: " << h);
+
std::cout << ENDL;
return 0;
}
@@ -601,12 +516,8 @@ int main(int argc, char* argv[])
epee::string_tools::set_module_name_and_folder(argv[0]);
std::string default_db_type = "lmdb";
- std::string default_db_engine_compiled = "blockchain_db";
- std::unordered_set<std::string> db_types_all = cryptonote::blockchain_db_types;
- db_types_all.insert("memory");
-
- std::string available_dbs = join_set_strings(db_types_all, ", ");
+ std::string available_dbs = cryptonote::blockchain_db_types(", ");
available_dbs = "available: " + available_dbs;
uint32_t log_level = 0;
@@ -649,10 +560,10 @@ int main(int argc, char* argv[])
const command_line::arg_descriptor<bool> arg_resume = {"resume",
"Resume from current height if output database already exists", true};
- command_line::add_arg(desc_cmd_sett, command_line::arg_data_dir, default_data_path.string());
- command_line::add_arg(desc_cmd_sett, command_line::arg_testnet_data_dir, default_testnet_data_path.string());
+ //command_line::add_arg(desc_cmd_sett, command_line::arg_data_dir, default_data_path.string());
+ //command_line::add_arg(desc_cmd_sett, command_line::arg_testnet_data_dir, default_testnet_data_path.string());
command_line::add_arg(desc_cmd_sett, arg_input_file);
- command_line::add_arg(desc_cmd_sett, arg_testnet_on);
+ //command_line::add_arg(desc_cmd_sett, arg_testnet_on);
command_line::add_arg(desc_cmd_sett, arg_log_level);
command_line::add_arg(desc_cmd_sett, arg_database);
command_line::add_arg(desc_cmd_sett, arg_batch_size);
@@ -673,6 +584,7 @@ int main(int argc, char* argv[])
po::options_description desc_options("Allowed options");
desc_options.add(desc_cmd_only).add(desc_cmd_sett);
+ cryptonote::core::init_options(desc_options);
po::variables_map vm;
bool r = command_line::handle_error_helper(desc_options, [&]()
@@ -751,7 +663,6 @@ int main(int argc, char* argv[])
std::string db_type;
- std::string db_engine_compiled;
int db_flags = 0;
int res = 0;
res = parse_db_arguments(db_arg_str, db_type, db_flags);
@@ -761,25 +672,12 @@ int main(int argc, char* argv[])
return 1;
}
- if (db_types_all.count(db_type) == 0)
+ if (!cryptonote::blockchain_valid_db_type(db_type))
{
std::cerr << "Invalid database type: " << db_type << std::endl;
return 1;
}
- if ((db_type == "lmdb")
-#if defined(BERKELEY_DB)
- || (db_type == "berkeley")
-#endif
- )
- {
- db_engine_compiled = "blockchain_db";
- }
- else
- {
- db_engine_compiled = "memory";
- }
-
MINFO("database: " << db_type);
MINFO("database flags: " << db_flags);
MINFO("verify: " << std::boolalpha << opt_verify << std::noboolalpha);
@@ -798,64 +696,52 @@ int main(int argc, char* argv[])
MINFO("bootstrap file path: " << import_file_path);
MINFO("database path: " << m_config_folder);
+ cryptonote::cryptonote_protocol_stub pr; //TODO: stub only for this kind of test, make real validation of relayed objects
+ cryptonote::core core(&pr);
+
try
{
- // fake_core needed for verification to work when enabled.
- //
- // NOTE: don't need fake_core method of doing things when we're going to call
- // BlockchainDB add_block() directly and have available the 3 block
- // properties to do so. Both ways work, but fake core isn't necessary in that
- // circumstance.
-
- if (db_type != "lmdb"
-#if defined(BERKELEY_DB)
- && db_type != "berkeley"
-#endif
- )
+ core.disable_dns_checkpoints(true);
+ if (!core.init(vm, NULL))
{
- std::cerr << "database type unrecognized" << ENDL;
+ std::cerr << "Failed to initialize core" << ENDL;
return 1;
}
-
- // for multi_db_compile:
- fake_core_db simple_core(m_config_folder, opt_testnet, opt_batch, db_type, db_flags);
+ core.get_blockchain_storage().get_db().set_batch_transactions(true);
if (! vm["pop-blocks"].defaulted())
{
num_blocks = command_line::get_arg(vm, arg_pop_blocks);
- MINFO("height: " << simple_core.m_storage.get_current_blockchain_height());
- pop_blocks(simple_core, num_blocks);
- MINFO("height: " << simple_core.m_storage.get_current_blockchain_height());
+ MINFO("height: " << core.get_blockchain_storage().get_current_blockchain_height());
+ pop_blocks(core, num_blocks);
+ MINFO("height: " << core.get_blockchain_storage().get_current_blockchain_height());
return 0;
}
if (! vm["drop-hard-fork"].defaulted())
{
MINFO("Dropping hard fork tables...");
- simple_core.m_storage.get_db().drop_hard_fork_info();
+ core.get_blockchain_storage().get_db().drop_hard_fork_info();
+ core.deinit();
return 0;
}
- import_from_file(simple_core, import_file_path, block_stop);
+ import_from_file(core, import_file_path, block_stop);
+ // ensure db closed
+ // - transactions properly checked and handled
+ // - disk sync if needed
+ //
+ core.deinit();
}
catch (const DB_ERROR& e)
{
std::cout << std::string("Error loading blockchain db: ") + e.what() + " -- shutting down now" << ENDL;
+ core.deinit();
return 1;
}
- // destructors called at exit:
- //
- // ensure db closed
- // - transactions properly checked and handled
- // - disk sync if needed
- //
- // fake_core object's destructor is called when it goes out of scope. For an
- // LMDB fake_core, it calls Blockchain::deinit() on its object, which in turn
- // calls delete on its BlockchainDB derived class' object, which closes its
- // files.
return 0;
CATCH_ENTRY("Import error", 1);
diff --git a/src/blockchain_utilities/blockchain_utilities.h b/src/blockchain_utilities/blockchain_utilities.h
index af934bf29..6fb5e1131 100644
--- a/src/blockchain_utilities/blockchain_utilities.h
+++ b/src/blockchain_utilities/blockchain_utilities.h
@@ -34,6 +34,7 @@
// bounds checking is done before writing to buffer, but buffer size
// should be a sensible maximum
#define BUFFER_SIZE 1000000
+#define CHUNK_SIZE_WARNING_THRESHOLD 500000
#define NUM_BLOCKS_PER_CHUNK 1
#define BLOCKCHAIN_RAW "blockchain.raw"
diff --git a/src/blockchain_utilities/blocksdat_file.h b/src/blockchain_utilities/blocksdat_file.h
index 4e9d8173b..0a5913058 100644
--- a/src/blockchain_utilities/blocksdat_file.h
+++ b/src/blockchain_utilities/blocksdat_file.h
@@ -38,7 +38,6 @@
#include "cryptonote_basic/cryptonote_boost_serialization.h"
#include "cryptonote_core/blockchain.h"
#include "blockchain_db/blockchain_db.h"
-#include "blockchain_db/lmdb/db_lmdb.h"
#include <algorithm>
#include <cstdio>
diff --git a/src/blockchain_utilities/bootstrap_file.cpp b/src/blockchain_utilities/bootstrap_file.cpp
index d5bb37d93..2b1a5d6c7 100644
--- a/src/blockchain_utilities/bootstrap_file.cpp
+++ b/src/blockchain_utilities/bootstrap_file.cpp
@@ -436,10 +436,10 @@ uint64_t BootstrapFile::count_blocks(const std::string& import_file_path)
<< " height: " << h-1);
throw std::runtime_error("Aborting: chunk size exceeds buffer size");
}
- if (chunk_size > 100000)
+ if (chunk_size > CHUNK_SIZE_WARNING_THRESHOLD)
{
std::cout << refresh_string;
- MDEBUG("NOTE: chunk_size " << chunk_size << " > 100000" << " height: "
+ MDEBUG("NOTE: chunk_size " << chunk_size << " > " << CHUNK_SIZE_WARNING_THRESHOLD << " << height: "
<< h-1);
}
else if (chunk_size <= 0) {
diff --git a/src/blockchain_utilities/fake_core.h b/src/blockchain_utilities/fake_core.h
deleted file mode 100644
index 809c6fe43..000000000
--- a/src/blockchain_utilities/fake_core.h
+++ /dev/null
@@ -1,133 +0,0 @@
-// Copyright (c) 2014-2017, 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 <boost/filesystem.hpp>
-#include "cryptonote_core/blockchain.h" // BlockchainDB
-#include "cryptonote_core/tx_pool.h"
-#include "blockchain_db/blockchain_db.h"
-#include "blockchain_db/lmdb/db_lmdb.h"
-#if defined(BERKELEY_DB)
-#include "blockchain_db/berkeleydb/db_bdb.h"
-#endif
-
-using namespace cryptonote;
-
-namespace
-{
- // NOTE: These values should match blockchain.cpp
- // TODO: Refactor
- const uint64_t mainnet_hard_fork_version_1_till = 1009826;
- const uint64_t testnet_hard_fork_version_1_till = 624633;
-}
-
-
-struct fake_core_db
-{
- Blockchain m_storage;
- HardFork* m_hardfork = nullptr;
- tx_memory_pool m_pool;
- bool support_batch;
- bool support_add_block;
-
- // for multi_db_runtime:
- fake_core_db(const boost::filesystem::path &path, const bool use_testnet=false, const bool do_batch=true, const std::string& db_type="lmdb", const int db_flags=0) : m_pool(m_storage), m_storage(m_pool)
- {
- BlockchainDB* db = nullptr;
- if (db_type == "lmdb")
- db = new BlockchainLMDB();
-#if defined(BERKELEY_DB)
- else if (db_type == "berkeley")
- db = new BlockchainBDB();
-#endif
- else
- {
- LOG_ERROR("Attempted to use non-existent database type: " << db_type);
- throw std::runtime_error("Attempting to use non-existent database type");
- }
-
- boost::filesystem::path folder(path);
-
- folder /= db->get_db_name();
-
- LOG_PRINT_L0("Loading blockchain from folder " << folder.string() << " ...");
-
- const std::string filename = folder.string();
- try
- {
- db->open(filename, db_flags);
- }
- catch (const std::exception& e)
- {
- LOG_PRINT_L0("Error opening database: " << e.what());
- throw;
- }
-
- db->check_hard_fork_info();
-
- uint64_t hard_fork_version_1_till = use_testnet ? testnet_hard_fork_version_1_till : mainnet_hard_fork_version_1_till;
- m_hardfork = new HardFork(*db, 1, hard_fork_version_1_till);
-
- m_storage.init(db, m_hardfork, use_testnet);
-
- m_pool.init();
-
- if (do_batch)
- m_storage.get_db().set_batch_transactions(do_batch);
- support_batch = true;
- support_add_block = true;
- }
- ~fake_core_db()
- {
- m_storage.get_db().check_hard_fork_info();
- m_storage.deinit();
- }
-
- uint64_t add_block(const block& blk
- , const size_t& block_size
- , const difficulty_type& cumulative_difficulty
- , const uint64_t& coins_generated
- , const std::vector<transaction>& txs
- )
- {
- return m_storage.get_db().add_block(blk, block_size, cumulative_difficulty, coins_generated, txs);
- }
-
- bool batch_start(uint64_t batch_num_blocks = 0)
- {
- return m_storage.get_db().batch_start(batch_num_blocks);
- }
-
- void batch_stop()
- {
- m_storage.get_db().batch_stop();
- }
-
-};
-
diff --git a/src/blocks/checkpoints.dat b/src/blocks/checkpoints.dat
index a15f53e67..15fa042cf 100644
--- a/src/blocks/checkpoints.dat
+++ b/src/blocks/checkpoints.dat
Binary files differ
diff --git a/src/common/command_line.cpp b/src/common/command_line.cpp
index 8c2796bbe..666b3267f 100644
--- a/src/common/command_line.cpp
+++ b/src/common/command_line.cpp
@@ -32,7 +32,6 @@
#include <boost/algorithm/string/compare.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <unordered_set>
-#include "blockchain_db/db_types.h"
#include "common/i18n.h"
#include "cryptonote_config.h"
#include "string_tools.h"
@@ -96,17 +95,6 @@ namespace command_line
, "checkpoints from DNS server will be enforced"
, false
};
- std::string arg_db_type_description = "Specify database type, available: " + boost::algorithm::join(cryptonote::blockchain_db_types, ", ");
- const command_line::arg_descriptor<std::string> arg_db_type = {
- "db-type"
- , arg_db_type_description.c_str()
- , DEFAULT_DB_TYPE
- };
- 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]."
- , "fast:async:1000"
- };
const command_line::arg_descriptor<uint64_t> arg_fast_block_sync = {
"fast-block-sync"
, "Sync up most of the way by using embedded, known block hashes."
@@ -124,12 +112,17 @@ namespace command_line
};
const command_line::arg_descriptor<size_t> arg_block_sync_size = {
"block-sync-size"
- , "How many blocks to sync at once during chain synchronization."
- , BLOCKS_SYNCHRONIZING_DEFAULT_COUNT
+ , "How many blocks to sync at once during chain synchronization (0 = adaptive)."
+ , 0
};
const command_line::arg_descriptor<std::string> arg_check_updates = {
"check-updates"
, "Check for new versions of monero: [disabled|notify|download|update]"
, "notify"
};
+ const arg_descriptor<bool> arg_fluffy_blocks = {
+ "fluffy-blocks"
+ , "Relay blocks as fluffy blocks where possible (automatic on testnet)"
+ , false
+ };
}
diff --git a/src/common/command_line.h b/src/common/command_line.h
index 2110b8849..d4231acd0 100644
--- a/src/common/command_line.h
+++ b/src/common/command_line.h
@@ -212,11 +212,10 @@ namespace command_line
extern const arg_descriptor<int> arg_test_dbg_lock_sleep;
extern const arg_descriptor<bool, false> arg_testnet_on;
extern const arg_descriptor<bool> arg_dns_checkpoints;
- extern const arg_descriptor<std::string> arg_db_type;
- extern const arg_descriptor<std::string> arg_db_sync_mode;
extern const arg_descriptor<uint64_t> arg_fast_block_sync;
extern const arg_descriptor<uint64_t> arg_prep_blocks_threads;
extern const arg_descriptor<uint64_t> arg_show_time_stats;
extern const arg_descriptor<size_t> arg_block_sync_size;
extern const arg_descriptor<std::string> arg_check_updates;
+ extern const arg_descriptor<bool> arg_fluffy_blocks;
}
diff --git a/src/common/dns_utils.cpp b/src/common/dns_utils.cpp
index ab38cbbae..e7ff11c5c 100644
--- a/src/common/dns_utils.cpp
+++ b/src/common/dns_utils.cpp
@@ -26,12 +26,9 @@
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#include "common/command_line.h"
-#include "common/i18n.h"
#include "common/dns_utils.h"
+#include "common/i18n.h"
#include "cryptonote_basic/cryptonote_basic_impl.h"
-#include <cstring>
-#include <sstream>
// check local first (in the event of static or in-source compilation of libunbound)
#include "unbound.h"
@@ -307,12 +304,8 @@ DNSResolver& DNSResolver::instance()
{
boost::lock_guard<boost::mutex> lock(instance_lock);
- static DNSResolver* staticInstance = NULL;
- if (staticInstance == NULL)
- {
- staticInstance = new DNSResolver();
- }
- return *staticInstance;
+ static DNSResolver staticInstance;
+ return staticInstance;
}
DNSResolver DNSResolver::create()
@@ -405,7 +398,7 @@ std::vector<std::string> addresses_from_url(const std::string& url, bool& dnssec
return addresses;
}
-std::string get_account_address_as_str_from_url(const std::string& url, bool& dnssec_valid, bool cli_confirm)
+std::string get_account_address_as_str_from_url(const std::string& url, bool& dnssec_valid, std::function<std::string(const std::string&, const std::vector<std::string>&, bool)> dns_confirm)
{
// attempt to get address from dns query
auto addresses = addresses_from_url(url, dnssec_valid);
@@ -414,44 +407,7 @@ std::string get_account_address_as_str_from_url(const std::string& url, bool& dn
LOG_ERROR("wrong address: " << url);
return {};
}
- // for now, move on only if one address found
- if (addresses.size() > 1)
- {
- LOG_ERROR("not yet supported: Multiple Monero addresses found for given URL: " << url);
- return {};
- }
- if (!cli_confirm)
- return addresses[0];
- // prompt user for confirmation.
- // inform user of DNSSEC validation status as well.
- std::string dnssec_str;
- if (dnssec_valid)
- {
- dnssec_str = tr("DNSSEC validation passed");
- }
- else
- {
- dnssec_str = tr("WARNING: DNSSEC validation was unsuccessful, this address may not be correct!");
- }
- std::stringstream prompt;
- prompt << tr("For URL: ") << url
- << ", " << dnssec_str << std::endl
- << tr(" Monero Address = ") << addresses[0]
- << std::endl
- << tr("Is this OK? (Y/n) ")
- ;
- // prompt the user for confirmation given the dns query and dnssec status
- std::string confirm_dns_ok = command_line::input_line(prompt.str());
- if (std::cin.eof())
- {
- return {};
- }
- if (!command_line::is_yes(confirm_dns_ok))
- {
- std::cout << tr("you have cancelled the transfer request") << std::endl;
- return {};
- }
- return addresses[0];
+ return dns_confirm(url, addresses, dnssec_valid);
}
namespace
diff --git a/src/common/dns_utils.h b/src/common/dns_utils.h
index 53c0c1c7b..f19584516 100644
--- a/src/common/dns_utils.h
+++ b/src/common/dns_utils.h
@@ -29,7 +29,7 @@
#include <vector>
#include <string>
-#include "cryptonote_basic/cryptonote_basic.h"
+#include <functional>
namespace tools
{
@@ -101,7 +101,7 @@ public:
*
* @return A vector of strings containing a TXT record; or an empty vector
*/
- // TODO: modify this to accomodate DNSSEC
+ // TODO: modify this to accommodate DNSSEC
std::vector<std::string> get_txt_record(const std::string& url, bool& dnssec_available, bool& dnssec_valid);
/**
@@ -142,7 +142,7 @@ private:
*
* @return A vector of strings containing the requested record; or an empty vector
*/
- // TODO: modify this to accomodate DNSSEC
+ // TODO: modify this to accommodate DNSSEC
std::vector<std::string> get_record(const std::string& url, int record_type, std::string (*reader)(const char *,size_t), bool& dnssec_available, bool& dnssec_valid);
/**
@@ -163,7 +163,7 @@ namespace dns_utils
std::string address_from_txt_record(const std::string& s);
std::vector<std::string> addresses_from_url(const std::string& url, bool& dnssec_valid);
-std::string get_account_address_as_str_from_url(const std::string& url, bool& dnssec_valid, bool cli_confirm = true);
+std::string get_account_address_as_str_from_url(const std::string& url, bool& dnssec_valid, std::function<std::string(const std::string&, const std::vector<std::string>&, bool)> confirm_dns);
bool load_txt_records_from_dns(std::vector<std::string> &records, const std::vector<std::string> &dns_urls);
diff --git a/src/common/perf_timer.h b/src/common/perf_timer.h
index 56662ff24..bc8e05800 100644
--- a/src/common/perf_timer.h
+++ b/src/common/perf_timer.h
@@ -46,9 +46,9 @@ extern __thread std::vector<PerformanceTimer*> *performance_timers;
class PerformanceTimer
{
public:
- PerformanceTimer(const std::string &s, el::Level l = el::Level::Debug): name(s), level(l), started(false)
+ PerformanceTimer(const std::string &s, uint64_t unit, el::Level l = el::Level::Debug): name(s), unit(unit), level(l), started(false)
{
- ticks = epee::misc_utils::get_tick_count();
+ ticks = epee::misc_utils::get_ns_count();
if (!performance_timers)
{
MLOG(level, "PERF ----------");
@@ -69,9 +69,9 @@ public:
~PerformanceTimer()
{
performance_timers->pop_back();
- ticks = epee::misc_utils::get_tick_count() - ticks;
+ ticks = epee::misc_utils::get_ns_count() - ticks;
char s[12];
- snprintf(s, sizeof(s), "%8llu ", (unsigned long long)ticks);
+ snprintf(s, sizeof(s), "%8llu ", (unsigned long long)ticks / (1000000000 / unit));
MLOG(level, "PERF " << s << std::string(performance_timers->size() * 2, ' ') << " " << name);
if (performance_timers->empty())
{
@@ -82,6 +82,7 @@ public:
private:
std::string name;
+ uint64_t unit;
el::Level level;
uint64_t ticks;
bool started;
@@ -89,7 +90,9 @@ private:
void set_performance_timer_log_level(el::Level level);
-#define PERF_TIMER(name) tools::PerformanceTimer pt_##name(#name, tools::performance_timer_log_level)
-#define PERF_TIMER_L(name, l) tools::PerformanceTimer pt_##name(#name, l)
+#define PERF_TIMER_UNIT(name, unit) tools::PerformanceTimer pt_##name(#name, unit, tools::performance_timer_log_level)
+#define PERF_TIMER_UNIT_L(name, unit, l) tools::PerformanceTimer pt_##name(#name, unit, l)
+#define PERF_TIMER(name) PERF_TIMER_UNIT(name, 1000)
+#define PERF_TIMER_L(name, l) PERF_TIMER_UNIT_L(name, 1000, l)
}
diff --git a/src/common/rpc_client.h b/src/common/rpc_client.h
index 8494b4a60..297020ef2 100644
--- a/src/common/rpc_client.h
+++ b/src/common/rpc_client.h
@@ -69,7 +69,7 @@ namespace tools
bool ok = connection.is_open();
if (!ok)
{
- fail_msg_writer() << "Couldn't connect to daemon";
+ fail_msg_writer() << "Couldn't connect to daemon: " << m_http_client.get_host() << ":" << m_http_client.get_port();
return false;
}
ok = ok && epee::net_utils::invoke_http_json_rpc("/json_rpc", method_name, req, res, m_http_client, t_http_connection::TIMEOUT());
@@ -98,7 +98,7 @@ namespace tools
ok = ok && epee::net_utils::invoke_http_json_rpc("/json_rpc", method_name, req, res, m_http_client, t_http_connection::TIMEOUT());
if (!ok)
{
- fail_msg_writer() << "Couldn't connect to daemon";
+ fail_msg_writer() << "Couldn't connect to daemon: " << m_http_client.get_host() << ":" << m_http_client.get_port();
return false;
}
else if (res.status != CORE_RPC_STATUS_OK) // TODO - handle CORE_RPC_STATUS_BUSY ?
@@ -126,7 +126,7 @@ namespace tools
ok = ok && epee::net_utils::invoke_http_json(relative_url, req, res, m_http_client, t_http_connection::TIMEOUT());
if (!ok)
{
- fail_msg_writer() << "Couldn't connect to daemon";
+ fail_msg_writer() << "Couldn't connect to daemon: " << m_http_client.get_host() << ":" << m_http_client.get_port();
return false;
}
else if (res.status != CORE_RPC_STATUS_OK) // TODO - handle CORE_RPC_STATUS_BUSY ?
diff --git a/src/common/scoped_message_writer.h b/src/common/scoped_message_writer.h
index 7ee4f1379..8fc98d2b0 100644
--- a/src/common/scoped_message_writer.h
+++ b/src/common/scoped_message_writer.h
@@ -31,6 +31,14 @@
#include "misc_log_ex.h"
#include <iostream>
+#ifdef HAVE_READLINE
+ #include "readline_buffer.h"
+ #define PAUSE_READLINE() \
+ rdln::suspend_readline pause_readline;
+#else
+ #define PAUSE_READLINE()
+#endif
+
namespace tools
{
@@ -91,7 +99,7 @@ public:
{
m_flush = false;
- MCLOG(m_log_level, "msgwriter", m_oss.str());
+ MCLOG_FILE(m_log_level, "msgwriter", m_oss.str());
if (epee::console_color_default == m_color)
{
@@ -99,6 +107,7 @@ public:
}
else
{
+ PAUSE_READLINE();
set_console_color(m_color, m_bright);
std::cout << m_oss.str();
epee::reset_console_color();
@@ -108,9 +117,9 @@ public:
}
};
-inline scoped_message_writer success_msg_writer()
+inline scoped_message_writer success_msg_writer(bool color = true)
{
- return scoped_message_writer(epee::console_color_green, false, std::string(), el::Level::Info);
+ return scoped_message_writer(color ? epee::console_color_green : epee::console_color_default, false, std::string(), el::Level::Info);
}
inline scoped_message_writer msg_writer(epee::console_colors color = epee::console_color_default)
diff --git a/src/common/sfinae_helpers.h b/src/common/sfinae_helpers.h
new file mode 100644
index 000000000..ddd456dd2
--- /dev/null
+++ b/src/common/sfinae_helpers.h
@@ -0,0 +1,147 @@
+// Copyright (c) 2016-2017, 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
+
+// the loose definitions of types in this file are, well, loose.
+//
+// these helpers aren't here for absolute type certainty at compile-time,
+// but rather to help with templated functions telling types apart.
+
+namespace sfinae
+{
+
+ typedef char true_type;
+
+ struct false_type { true_type a[2]; };
+
+ template <typename T>
+ struct is_not_container
+ {
+ private:
+
+ // does not have const iterator
+ template <typename C> static false_type c_iter(typename C::const_iterator*);
+ template <typename C> static true_type c_iter(...);
+
+ // does not have value_type
+ template <typename C> static false_type v_type(typename C::value_type*);
+ template <typename C> static true_type v_type(...);
+
+ // does not have key_type
+ template <typename C> static false_type k_type(typename C::key_type*);
+ template <typename C> static true_type k_type(...);
+
+ // does not have mapped_type
+ template <typename C> static false_type m_type(typename C::mapped_type*);
+ template <typename C> static true_type m_type(...);
+
+ public:
+
+ static const bool value = (
+ (
+ sizeof(c_iter<T>(0)) == sizeof(true_type) &&
+ sizeof(v_type<T>(0)) == sizeof(true_type) &&
+ sizeof(k_type<T>(0)) == sizeof(true_type) &&
+ sizeof(m_type<T>(0)) == sizeof(true_type)
+ )
+ || std::is_same<T, std::string>::value
+ );
+
+ typedef T type;
+ };
+
+ template <typename T>
+ struct is_vector_like
+ {
+ private:
+
+ // has const iterator
+ template <typename C> static true_type c_iter(typename C::const_iterator*);
+ template <typename C> static false_type c_iter(...);
+
+ // has value_type
+ template <typename C> static true_type v_type(typename C::value_type*);
+ template <typename C> static false_type v_type(...);
+
+ // does not have key_type
+ template <typename C> static false_type k_type(typename C::key_type*);
+ template <typename C> static true_type k_type(...);
+
+ // does not have mapped_type
+ template <typename C> static false_type m_type(typename C::mapped_type*);
+ template <typename C> static true_type m_type(...);
+
+ public:
+
+ static const bool value = (
+ sizeof(c_iter<T>(0)) == sizeof(true_type) &&
+ sizeof(v_type<T>(0)) == sizeof(true_type) &&
+ sizeof(k_type<T>(0)) == sizeof(true_type) &&
+ sizeof(m_type<T>(0)) == sizeof(true_type) &&
+ !std::is_same<T, std::string>::value
+ );
+
+ typedef T type;
+ };
+
+ template <typename T>
+ struct is_map_like
+ {
+ private:
+
+ // has const iterator
+ template <typename C> static true_type c_iter(typename C::const_iterator*);
+ template <typename C> static false_type c_iter(...);
+
+ // has value_type
+ template <typename C> static true_type v_type(typename C::value_type*);
+ template <typename C> static false_type v_type(...);
+
+ // has key_type
+ template <typename C> static true_type k_type(typename C::key_type*);
+ template <typename C> static false_type k_type(...);
+
+ // has mapped_type
+ template <typename C> static true_type m_type(typename C::mapped_type*);
+ template <typename C> static false_type m_type(...);
+
+ public:
+
+ static const bool value = (
+ sizeof(c_iter<T>(0)) == sizeof(true_type) &&
+ sizeof(v_type<T>(0)) == sizeof(true_type) &&
+ sizeof(k_type<T>(0)) == sizeof(true_type) &&
+ sizeof(m_type<T>(0)) == sizeof(true_type) &&
+ !std::is_same<T, std::string>::value
+ );
+
+ typedef T type;
+ };
+
+} // namespace sfinae
diff --git a/src/common/stack_trace.cpp b/src/common/stack_trace.cpp
index ef64c20c5..6fdf4dd47 100644
--- a/src/common/stack_trace.cpp
+++ b/src/common/stack_trace.cpp
@@ -30,8 +30,7 @@
#define USE_UNWIND
#endif
-#include "common/stack_trace.h"
-#include "misc_log_ex.h"
+#include <stdexcept>
#ifdef USE_UNWIND
#define UNW_LOCAL_ONLY
#include <libunwind.h>
@@ -40,6 +39,8 @@
#ifndef STATICLIB
#include <dlfcn.h>
#endif
+#include "common/stack_trace.h"
+#include "misc_log_ex.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "stacktrace"
diff --git a/src/common/stack_trace.h b/src/common/stack_trace.h
index 25eec9fb3..0f6bdc08b 100644
--- a/src/common/stack_trace.h
+++ b/src/common/stack_trace.h
@@ -29,7 +29,6 @@
#ifndef MONERO_EXCEPTION_H
#define MONERO_EXCEPTION_H
-#include <stdexcept>
#include <string>
namespace tools
diff --git a/src/common/thread_group.cpp b/src/common/thread_group.cpp
index 860d0b732..691a27a25 100644
--- a/src/common/thread_group.cpp
+++ b/src/common/thread_group.cpp
@@ -32,6 +32,7 @@
#include <limits>
#include <stdexcept>
+#include "cryptonote_config.h"
#include "common/util.h"
namespace tools
@@ -63,8 +64,10 @@ thread_group::data::data(std::size_t count)
, has_work()
, stop(false) {
threads.reserve(count);
+ boost::thread::attributes attrs;
+ attrs.set_stack_size(THREAD_STACK_SIZE);
while (count--) {
- threads.push_back(boost::thread(&thread_group::data::run, this));
+ threads.push_back(boost::thread(attrs, boost::bind(&thread_group::data::run, this)));
}
}
diff --git a/src/common/updates.cpp b/src/common/updates.cpp
index 5b1acf5fa..8a057b1cf 100644
--- a/src/common/updates.cpp
+++ b/src/common/updates.cpp
@@ -26,6 +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.
+#include "misc_log_ex.h"
#include "util.h"
#include "dns_utils.h"
#include "updates.h"
diff --git a/src/common/util.h b/src/common/util.h
index 4291d7e18..2452bc9d5 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -40,7 +40,6 @@
#include <string>
#include "crypto/hash.h"
-#include "p2p/p2p_protocol_defs.h"
/*! \brief Various Tools
*
@@ -108,14 +107,6 @@ namespace tools
bool sanitize_locale();
- inline crypto::hash get_proof_of_trust_hash(const nodetool::proof_of_trust& pot)
- {
- std::string s;
- s.append(reinterpret_cast<const char*>(&pot.peer_id), sizeof(pot.peer_id));
- s.append(reinterpret_cast<const char*>(&pot.time), sizeof(pot.time));
- return crypto::cn_fast_hash(s.data(), s.size());
- }
-
/*! \brief Defines a signal handler for win32 and *nix
*/
class signal_handler
diff --git a/src/crypto/crypto.cpp b/src/crypto/crypto.cpp
index 1c7adff3b..5fb670f87 100644
--- a/src/crypto/crypto.cpp
+++ b/src/crypto/crypto.cpp
@@ -36,18 +36,13 @@
#include <memory>
#include <boost/thread/mutex.hpp>
#include <boost/thread/lock_guard.hpp>
+#include <boost/shared_ptr.hpp>
#include "common/varint.h"
#include "warnings.h"
#include "crypto.h"
#include "hash.h"
-#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__DragonFly__)
- #include <alloca.h>
-#else
- #include <stdlib.h>
-#endif
-
namespace crypto {
using std::abort;
@@ -411,7 +406,9 @@ POP_WARNINGS
ge_p3 image_unp;
ge_dsmp image_pre;
ec_scalar sum, k, h;
- rs_comm *const buf = reinterpret_cast<rs_comm *>(alloca(rs_comm_size(pubs_count)));
+ boost::shared_ptr<rs_comm> buf(reinterpret_cast<rs_comm *>(malloc(rs_comm_size(pubs_count))), free);
+ if (!buf)
+ abort();
assert(sec_index < pubs_count);
#if !defined(NDEBUG)
{
@@ -459,7 +456,7 @@ POP_WARNINGS
sc_add(&sum, &sum, &sig[i].c);
}
}
- hash_to_scalar(buf, rs_comm_size(pubs_count), h);
+ hash_to_scalar(buf.get(), rs_comm_size(pubs_count), h);
sc_sub(&sig[sec_index].c, &h, &sum);
sc_mulsub(&sig[sec_index].r, &sig[sec_index].c, &sec, &k);
}
@@ -471,7 +468,9 @@ POP_WARNINGS
ge_p3 image_unp;
ge_dsmp image_pre;
ec_scalar sum, h;
- rs_comm *const buf = reinterpret_cast<rs_comm *>(alloca(rs_comm_size(pubs_count)));
+ boost::shared_ptr<rs_comm> buf(reinterpret_cast<rs_comm *>(malloc(rs_comm_size(pubs_count))), free);
+ if (!buf)
+ return false;
#if !defined(NDEBUG)
for (i = 0; i < pubs_count; i++) {
assert(check_key(*pubs[i]));
@@ -499,7 +498,7 @@ POP_WARNINGS
ge_tobytes(&buf->ab[i].b, &tmp2);
sc_add(&sum, &sum, &sig[i].c);
}
- hash_to_scalar(buf, rs_comm_size(pubs_count), h);
+ hash_to_scalar(buf.get(), rs_comm_size(pubs_count), h);
sc_sub(&h, &h, &sum);
return sc_isnonzero(&h) == 0;
}
diff --git a/src/cryptonote_basic/checkpoints.cpp b/src/cryptonote_basic/checkpoints.cpp
index 103a4a33e..98e509561 100644
--- a/src/cryptonote_basic/checkpoints.cpp
+++ b/src/cryptonote_basic/checkpoints.cpp
@@ -167,6 +167,8 @@ namespace cryptonote
ADD_CHECKPOINT(1100000, "3fd720c5c8b3072fc1ccda922dec1ef25f9ed88a1e6ad4103d0fe00b180a5903");
ADD_CHECKPOINT(1150000, "1dd16f626d18e1e988490dfd06de5920e22629c972c58b4d8daddea0038627b2");
ADD_CHECKPOINT(1200000, "fa7d13a90850882060479d100141ff84286599ae39c3277c8ea784393f882d1f");
+ ADD_CHECKPOINT(1300000, "31b34272343a44a9f4ac7de7a8fcf3b7d8a3124d7d6870affd510d2f37e74cd0");
+ ADD_CHECKPOINT(1390000, "a8f5649dd4ded60eedab475f2bec8c934681c07e3cf640e9be0617554f13ff6c");
return true;
diff --git a/src/cryptonote_basic/connection_context.h b/src/cryptonote_basic/connection_context.h
index 8d739900e..3283543e2 100644
--- a/src/cryptonote_basic/connection_context.h
+++ b/src/cryptonote_basic/connection_context.h
@@ -39,11 +39,14 @@ namespace cryptonote
struct cryptonote_connection_context: public epee::net_utils::connection_context_base
{
+ cryptonote_connection_context(): m_state(state_before_handshake), m_remote_blockchain_height(0), m_last_response_height(0),
+ m_last_known_hash(cryptonote::null_hash) {}
enum state
{
- state_befor_handshake = 0, //default state
+ state_before_handshake = 0, //default state
state_synchronizing,
+ state_standby,
state_idle,
state_normal
};
@@ -53,7 +56,9 @@ namespace cryptonote
std::unordered_set<crypto::hash> m_requested_objects;
uint64_t m_remote_blockchain_height;
uint64_t m_last_response_height;
+ boost::posix_time::ptime m_last_request_time;
epee::copyable_atomic m_callback_request_count; //in debug purpose: problem with double callback rise
+ crypto::hash m_last_known_hash;
//size_t m_score; TODO: add score calculations
};
@@ -61,10 +66,12 @@ namespace cryptonote
{
switch (s)
{
- case cryptonote_connection_context::state_befor_handshake:
- return "state_befor_handshake";
+ case cryptonote_connection_context::state_before_handshake:
+ return "state_before_handshake";
case cryptonote_connection_context::state_synchronizing:
return "state_synchronizing";
+ case cryptonote_connection_context::state_standby:
+ return "state_standby";
case cryptonote_connection_context::state_idle:
return "state_idle";
case cryptonote_connection_context::state_normal:
diff --git a/src/cryptonote_basic/cryptonote_basic_impl.cpp b/src/cryptonote_basic/cryptonote_basic_impl.cpp
index edd67793f..a59f96956 100644
--- a/src/cryptonote_basic/cryptonote_basic_impl.cpp
+++ b/src/cryptonote_basic/cryptonote_basic_impl.cpp
@@ -308,13 +308,13 @@ namespace cryptonote {
, crypto::hash8& payment_id
, bool testnet
, const std::string& str_or_url
- , bool cli_confirm
+ , std::function<std::string(const std::string&, const std::vector<std::string>&, bool)> dns_confirm
)
{
if (get_account_integrated_address_from_str(address, has_payment_id, payment_id, testnet, str_or_url))
return true;
bool dnssec_valid;
- std::string address_str = tools::dns_utils::get_account_address_as_str_from_url(str_or_url, dnssec_valid, cli_confirm);
+ std::string address_str = tools::dns_utils::get_account_address_as_str_from_url(str_or_url, dnssec_valid, dns_confirm);
return !address_str.empty() &&
get_account_integrated_address_from_str(address, has_payment_id, payment_id, testnet, address_str);
}
@@ -323,12 +323,12 @@ namespace cryptonote {
cryptonote::account_public_address& address
, bool testnet
, const std::string& str_or_url
- , bool cli_confirm
+ , std::function<std::string(const std::string&, const std::vector<std::string>&, bool)> dns_confirm
)
{
bool has_payment_id;
crypto::hash8 payment_id;
- return get_account_address_from_str_or_url(address, has_payment_id, payment_id, testnet, str_or_url, cli_confirm);
+ return get_account_address_from_str_or_url(address, has_payment_id, payment_id, testnet, str_or_url, dns_confirm);
}
//--------------------------------------------------------------------------------
bool operator ==(const cryptonote::transaction& a, const cryptonote::transaction& b) {
diff --git a/src/cryptonote_basic/cryptonote_basic_impl.h b/src/cryptonote_basic/cryptonote_basic_impl.h
index 14c03ac4c..7a2259b32 100644
--- a/src/cryptonote_basic/cryptonote_basic_impl.h
+++ b/src/cryptonote_basic/cryptonote_basic_impl.h
@@ -67,6 +67,15 @@ namespace cryptonote {
};
#pragma pack (pop)
+ namespace
+ {
+ inline std::string return_first_address(const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid)
+ {
+ if (addresses.empty())
+ return {};
+ return addresses[0];
+ }
+ }
/************************************************************************/
/* Cryptonote helper functions */
@@ -109,14 +118,14 @@ namespace cryptonote {
, crypto::hash8& payment_id
, bool testnet
, const std::string& str_or_url
- , bool cli_confirm = true
+ , std::function<std::string(const std::string&, const std::vector<std::string>&, bool)> dns_confirm = return_first_address
);
bool get_account_address_from_str_or_url(
cryptonote::account_public_address& address
, bool testnet
, const std::string& str_or_url
- , bool cli_confirm = true
+ , std::function<std::string(const std::string&, const std::vector<std::string>&, bool)> dns_confirm = return_first_address
);
bool is_coinbase(const transaction& tx);
diff --git a/src/cryptonote_basic/cryptonote_format_utils.h b/src/cryptonote_basic/cryptonote_format_utils.h
index 5c10907fd..d8ccf8eec 100644
--- a/src/cryptonote_basic/cryptonote_format_utils.h
+++ b/src/cryptonote_basic/cryptonote_format_utils.h
@@ -35,7 +35,6 @@
#include "include_base_utils.h"
#include "crypto/crypto.h"
#include "crypto/hash.h"
-#include "ringct/rctOps.h"
namespace cryptonote
{
diff --git a/src/cryptonote_basic/difficulty.h b/src/cryptonote_basic/difficulty.h
index aed6cb289..aeb1c030d 100644
--- a/src/cryptonote_basic/difficulty.h
+++ b/src/cryptonote_basic/difficulty.h
@@ -42,9 +42,9 @@ namespace cryptonote
/**
* @brief checks if a hash fits the given difficulty
*
- * The hash passes if (hash * difficulty) < 2^192.
+ * The hash passes if (hash * difficulty) < 2^256.
* Phrased differently, if (hash * difficulty) fits without overflow into
- * the least significant 192 bits of the 256 bit multiplication result.
+ * the least significant 256 bits of the 320 bit multiplication result.
*
* @param hash the hash to check
* @param difficulty the difficulty to check against
diff --git a/src/cryptonote_basic/miner.cpp b/src/cryptonote_basic/miner.cpp
index 6928a0ded..3c5811d61 100644
--- a/src/cryptonote_basic/miner.cpp
+++ b/src/cryptonote_basic/miner.cpp
@@ -74,8 +74,8 @@ namespace cryptonote
const command_line::arg_descriptor<bool> arg_bg_mining_enable = {"bg-mining-enable", "enable/disable background mining", true, true};
const command_line::arg_descriptor<bool> arg_bg_mining_ignore_battery = {"bg-mining-ignore-battery", "if true, assumes plugged in when unable to query system power status", false, true};
const command_line::arg_descriptor<uint64_t> arg_bg_mining_min_idle_interval_seconds = {"bg-mining-min-idle-interval", "Specify min lookback interval in seconds for determining idle state", miner::BACKGROUND_MINING_DEFAULT_MIN_IDLE_INTERVAL_IN_SECONDS, true};
- const command_line::arg_descriptor<uint8_t> arg_bg_mining_idle_threshold_percentage = {"bg-mining-idle-threshold", "Specify minimum avg idle percentage over lookback interval", miner::BACKGROUND_MINING_DEFAULT_IDLE_THRESHOLD_PERCENTAGE, true};
- const command_line::arg_descriptor<uint8_t> arg_bg_mining_miner_target_percentage = {"bg-mining-miner-target", "Specificy maximum percentage cpu use by miner(s)", miner::BACKGROUND_MINING_DEFAULT_MINING_TARGET_PERCENTAGE, true};
+ const command_line::arg_descriptor<uint16_t> arg_bg_mining_idle_threshold_percentage = {"bg-mining-idle-threshold", "Specify minimum avg idle percentage over lookback interval", miner::BACKGROUND_MINING_DEFAULT_IDLE_THRESHOLD_PERCENTAGE, true};
+ const command_line::arg_descriptor<uint16_t> arg_bg_mining_miner_target_percentage = {"bg-mining-miner-target", "Specificy maximum percentage cpu use by miner(s)", miner::BACKGROUND_MINING_DEFAULT_MINING_TARGET_PERCENTAGE, true};
}
@@ -382,7 +382,7 @@ namespace cryptonote
boost::thread::attributes attrs;
attrs.set_stack_size(THREAD_STACK_SIZE);
- start(m_mine_address, m_threads_total, attrs, get_is_background_mining_enabled());
+ start(m_mine_address, m_threads_total, attrs, get_is_background_mining_enabled(), get_ignore_battery());
}
}
//-----------------------------------------------------------------------------------------------------
@@ -412,8 +412,8 @@ namespace cryptonote
bool miner::worker_thread()
{
uint32_t th_local_index = boost::interprocess::ipcdetail::atomic_inc32(&m_thread_index);
- MGINFO("Miner thread was started ["<< th_local_index << "]");
MLOG_SET_THREAD_NAME(std::string("[miner ") + std::to_string(th_local_index) + "]");
+ MGINFO("Miner thread was started ["<< th_local_index << "]");
uint32_t nonce = m_starter_nonce + th_local_index;
uint64_t height = 0;
difficulty_type local_diff = 0;
@@ -846,40 +846,100 @@ namespace cryptonote
#elif defined(__linux__)
- // i've only tested on UBUNTU, these paths might be different on other systems
- // need to figure out a way to make this more flexible
- std::string power_supply_path = "";
- const std::string POWER_SUPPLY_STATUS_PATHS[] =
- {
- "/sys/class/power_supply/ACAD/online",
- "/sys/class/power_supply/AC/online",
- "/sys/class/power_supply/AC0/online",
- "/sys/class/power_supply/ADP0/online"
- };
+ // Use the power_supply class http://lxr.linux.no/#linux+v4.10.1/Documentation/power/power_supply_class.txt
+ std::string power_supply_class_path = "/sys/class/power_supply";
- for(const std::string& path : POWER_SUPPLY_STATUS_PATHS)
+ boost::tribool on_battery = boost::logic::tribool(boost::logic::indeterminate);
+ if (boost::filesystem::is_directory(power_supply_class_path))
{
- if( epee::file_io_utils::is_file_exist(path) )
+ const boost::filesystem::directory_iterator end_itr;
+ for (boost::filesystem::directory_iterator iter(power_supply_class_path); iter != end_itr; ++iter)
{
- power_supply_path = path;
- break;
+ const boost::filesystem::path& power_supply_path = iter->path();
+ if (boost::filesystem::is_directory(power_supply_path))
+ {
+ std::ifstream power_supply_present_stream((power_supply_path / "present").string());
+ if (power_supply_present_stream.fail())
+ {
+ LOG_PRINT_L0("Unable to read from " << power_supply_path << " to check if power supply present");
+ continue;
+ }
+
+ if (power_supply_present_stream.get() != '1')
+ {
+ LOG_PRINT_L4("Power supply not present at " << power_supply_path);
+ continue;
+ }
+
+ boost::filesystem::path power_supply_type_path = power_supply_path / "type";
+ if (boost::filesystem::is_regular_file(power_supply_type_path))
+ {
+ std::ifstream power_supply_type_stream(power_supply_type_path.string());
+ if (power_supply_type_stream.fail())
+ {
+ LOG_PRINT_L0("Unable to read from " << power_supply_type_path << " to check power supply type");
+ continue;
+ }
+
+ std::string power_supply_type;
+ std::getline(power_supply_type_stream, power_supply_type);
+
+ // If there is an AC adapter that's present and online we can break early
+ if (boost::starts_with(power_supply_type, "Mains"))
+ {
+ boost::filesystem::path power_supply_online_path = power_supply_path / "online";
+ if (boost::filesystem::is_regular_file(power_supply_online_path))
+ {
+ std::ifstream power_supply_online_stream(power_supply_online_path.string());
+ if (power_supply_online_stream.fail())
+ {
+ LOG_PRINT_L0("Unable to read from " << power_supply_online_path << " to check ac power supply status");
+ continue;
+ }
+
+ if (power_supply_online_stream.get() == '1')
+ {
+ return boost::logic::tribool(false);
+ }
+ }
+ }
+ else if (boost::starts_with(power_supply_type, "Battery") && boost::logic::indeterminate(on_battery))
+ {
+ boost::filesystem::path power_supply_status_path = power_supply_path / "status";
+ if (boost::filesystem::is_regular_file(power_supply_status_path))
+ {
+ std::ifstream power_supply_status_stream(power_supply_status_path.string());
+ if (power_supply_status_stream.fail())
+ {
+ LOG_PRINT_L0("Unable to read from " << power_supply_status_path << " to check battery power supply status");
+ continue;
+ }
+
+ // Possible status are Charging, Full, Discharging, Not Charging, and Unknown
+ // We are only need to handle negative states right now
+ std::string power_supply_status;
+ std::getline(power_supply_status_stream, power_supply_status);
+ if (boost::starts_with(power_supply_status, "Charging") || boost::starts_with(power_supply_status, "Full"))
+ {
+ on_battery = boost::logic::tribool(false);
+ }
+
+ if (boost::starts_with(power_supply_status, "Discharging"))
+ {
+ on_battery = boost::logic::tribool(true);
+ }
+ }
+ }
+ }
+ }
}
}
- if( power_supply_path.empty() )
+ if (boost::logic::indeterminate(on_battery))
{
- LOG_ERROR("Couldn't find battery/power status file, can't determine if plugged in!");
- return boost::logic::tribool(boost::logic::indeterminate);;
+ LOG_ERROR("couldn't query power status from " << power_supply_class_path);
}
-
- std::ifstream power_stream(power_supply_path);
- if( power_stream.fail() )
- {
- LOG_ERROR("failed to open '" << power_supply_path << "'");
- return boost::logic::tribool(boost::logic::indeterminate);;
- }
-
- return boost::logic::tribool( (power_stream.get() != '1') );
+ return on_battery;
#endif
diff --git a/src/cryptonote_config.h b/src/cryptonote_config.h
index 8dbf1b53b..cdb46dda9 100644
--- a/src/cryptonote_config.h
+++ b/src/cryptonote_config.h
@@ -89,7 +89,8 @@
#define BLOCKS_IDS_SYNCHRONIZING_DEFAULT_COUNT 10000 //by default, blocks ids count in synchronizing
-#define BLOCKS_SYNCHRONIZING_DEFAULT_COUNT 200 //by default, blocks count in blocks downloading
+#define BLOCKS_SYNCHRONIZING_DEFAULT_COUNT_PRE_V4 100 //by default, blocks count in blocks downloading
+#define BLOCKS_SYNCHRONIZING_DEFAULT_COUNT 20 //by default, blocks count in blocks downloading
#define CRYPTONOTE_PROTOCOL_HOP_RELAX_COUNT 3 //value of hop, after which we use only announce of new block
#define CRYPTONOTE_MEMPOOL_TX_LIVETIME 86400 //seconds, one day
@@ -149,6 +150,7 @@ namespace config
uint64_t const CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX = 19;
uint16_t const P2P_DEFAULT_PORT = 18080;
uint16_t const RPC_DEFAULT_PORT = 18081;
+ uint16_t const ZMQ_RPC_DEFAULT_PORT = 18082;
boost::uuids::uuid const NETWORK_ID = { {
0x12 ,0x30, 0xF1, 0x71 , 0x61, 0x04 , 0x41, 0x61, 0x17, 0x31, 0x00, 0x82, 0x16, 0xA1, 0xA1, 0x10
} }; // Bender's nightmare
@@ -161,6 +163,7 @@ namespace config
uint64_t const CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX = 54;
uint16_t const P2P_DEFAULT_PORT = 28080;
uint16_t const RPC_DEFAULT_PORT = 28081;
+ uint16_t const ZMQ_RPC_DEFAULT_PORT = 28082;
boost::uuids::uuid const NETWORK_ID = { {
0x12 ,0x30, 0xF1, 0x71 , 0x61, 0x04 , 0x41, 0x61, 0x17, 0x31, 0x00, 0x82, 0x16, 0xA1, 0xA1, 0x11
} }; // Bender's daydream
diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp
index 745608b9f..2330b6c42 100644
--- a/src/cryptonote_core/blockchain.cpp
+++ b/src/cryptonote_core/blockchain.cpp
@@ -59,6 +59,8 @@
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "blockchain"
+#define FIND_BLOCKCHAIN_SUPPLEMENT_MAX_SIZE (100*1024*1024) // 100 MB
+
//#include "serialization/json_archive.h"
/* TODO:
@@ -99,6 +101,9 @@ static const struct {
// version 5 starts from block 1288616, which is on or around the 15th of April, 2017. Fork time finalised on 2017-03-14.
{ 5, 1288616, 0, 1489520158 },
+
+ // version 6 starts from block 1400000, which is on or around the 16th of September, 2017. Fork time finalised on 2017-08-18.
+ { 6, 1400000, 0, 1503046577 },
};
static const uint64_t mainnet_hard_fork_version_1_till = 1009826;
@@ -118,13 +123,15 @@ static const struct {
{ 3, 800500, 0, 1472415034 },
{ 4, 801219, 0, 1472415035 },
{ 5, 802660, 0, 1472415036 + 86400*180 }, // add 5 months on testnet to shut the update warning up since there's a large gap to v6
+
+ { 6, 971400, 0, 1501709789 },
};
static const uint64_t testnet_hard_fork_version_1_till = 624633;
//------------------------------------------------------------------
Blockchain::Blockchain(tx_memory_pool& tx_pool) :
- m_db(), m_tx_pool(tx_pool), m_hardfork(NULL), m_timestamps_and_difficulties_height(0), m_current_block_cumul_sz_limit(0), m_is_in_checkpoint_zone(false),
- m_is_blockchain_storing(false), m_enforce_dns_checkpoints(false), m_max_prepare_blocks_threads(4), m_db_blocks_per_sync(1), m_db_sync_mode(db_async), m_fast_sync(true), m_show_time_stats(false), m_sync_counter(0), m_cancel(false)
+ m_db(), m_tx_pool(tx_pool), m_hardfork(NULL), m_timestamps_and_difficulties_height(0), m_current_block_cumul_sz_limit(0),
+ m_enforce_dns_checkpoints(false), m_max_prepare_blocks_threads(4), m_db_blocks_per_sync(1), m_db_sync_mode(db_async), m_db_default_sync(false), m_fast_sync(true), m_show_time_stats(false), m_sync_counter(0), m_cancel(false)
{
LOG_PRINT_L3("Blockchain::" << __func__);
}
@@ -279,7 +286,8 @@ uint64_t Blockchain::get_current_blockchain_height() const
bool Blockchain::init(BlockchainDB* db, const bool testnet, const cryptonote::test_options *test_options)
{
LOG_PRINT_L3("Blockchain::" << __func__);
- CRITICAL_REGION_LOCAL(m_blockchain_lock);
+ CRITICAL_REGION_LOCAL(m_tx_pool);
+ CRITICAL_REGION_LOCAL1(m_blockchain_lock);
bool fakechain = test_options != NULL;
@@ -743,7 +751,7 @@ difficulty_type Blockchain::get_difficulty_for_next_block()
m_timestamps = timestamps;
m_difficulties = difficulties;
}
- size_t target = get_current_hard_fork_version() < 2 ? DIFFICULTY_TARGET_V1 : DIFFICULTY_TARGET_V2;
+ size_t target = get_difficulty_target();
return next_difficulty(timestamps, difficulties, target);
}
//------------------------------------------------------------------
@@ -1350,7 +1358,6 @@ bool Blockchain::handle_alternative_block(const block& b, const crypto::hash& id
}
// Check the block's hash against the difficulty target for its alt chain
- m_is_in_checkpoint_zone = false;
difficulty_type current_diff = get_next_difficulty_for_alternative_chain(alt_chain, bei);
CHECK_AND_ASSERT_MES(current_diff, false, "!!!!!!! DIFFICULTY OVERHEAD !!!!!!!");
crypto::hash proof_of_work = null_hash;
@@ -1425,7 +1432,9 @@ bool Blockchain::handle_alternative_block(const block& b, const crypto::hash& id
{
//block orphaned
bvc.m_marked_as_orphaned = true;
- MERROR_VER("Block recognized as orphaned and rejected, id = " << id);
+ MERROR_VER("Block recognized as orphaned and rejected, id = " << id << ", height " << block_height
+ << ", parent in alt " << (it_prev != m_alternative_chains.end()) << ", parent in main " << parent_in_main
+ << " (parent " << b.prev_id << ", current top " << get_tail_id() << ", chain height " << get_current_blockchain_height() << ")");
}
return true;
@@ -1562,6 +1571,98 @@ void Blockchain::add_out_to_get_random_outs(COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_A
output_data_t data = m_db->get_output_key(amount, i);
oen.out_key = data.pubkey;
}
+
+uint64_t Blockchain::get_num_mature_outputs(uint64_t amount) const
+{
+ uint64_t num_outs = m_db->get_num_outputs(amount);
+ // ensure we don't include outputs that aren't yet eligible to be used
+ // outpouts are sorted by height
+ while (num_outs > 0)
+ {
+ const tx_out_index toi = m_db->get_output_tx_and_index(amount, num_outs - 1);
+ const uint64_t height = m_db->get_tx_block_height(toi.first);
+ if (height + CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE <= m_db->height())
+ break;
+ --num_outs;
+ }
+
+ return num_outs;
+}
+
+std::vector<uint64_t> Blockchain::get_random_outputs(uint64_t amount, uint64_t count) const
+{
+ uint64_t num_outs = get_num_mature_outputs(amount);
+
+ std::vector<uint64_t> indices;
+
+ std::unordered_set<uint64_t> seen_indices;
+
+ // if there aren't enough outputs to mix with (or just enough),
+ // use all of them. Eventually this should become impossible.
+ if (num_outs <= count)
+ {
+ for (uint64_t i = 0; i < num_outs; i++)
+ {
+ // get tx_hash, tx_out_index from DB
+ tx_out_index toi = m_db->get_output_tx_and_index(amount, i);
+
+ // if tx is unlocked, add output to indices
+ if (is_tx_spendtime_unlocked(m_db->get_tx_unlock_time(toi.first)))
+ {
+ indices.push_back(i);
+ }
+ }
+ }
+ else
+ {
+ // while we still need more mixins
+ while (indices.size() < count)
+ {
+ // if we've gone through every possible output, we've gotten all we can
+ if (seen_indices.size() == num_outs)
+ {
+ break;
+ }
+
+ // get a random output index from the DB. If we've already seen it,
+ // return to the top of the loop and try again, otherwise add it to the
+ // list of output indices we've seen.
+
+ // triangular distribution over [a,b) with a=0, mode c=b=up_index_limit
+ uint64_t r = crypto::rand<uint64_t>() % ((uint64_t)1 << 53);
+ double frac = std::sqrt((double)r / ((uint64_t)1 << 53));
+ uint64_t i = (uint64_t)(frac*num_outs);
+ // just in case rounding up to 1 occurs after sqrt
+ if (i == num_outs)
+ --i;
+
+ if (seen_indices.count(i))
+ {
+ continue;
+ }
+ seen_indices.emplace(i);
+
+ // get tx_hash, tx_out_index from DB
+ tx_out_index toi = m_db->get_output_tx_and_index(amount, i);
+
+ // if the output's transaction is unlocked, add the output's index to
+ // our list.
+ if (is_tx_spendtime_unlocked(m_db->get_tx_unlock_time(toi.first)))
+ {
+ indices.push_back(i);
+ }
+ }
+ }
+
+ return indices;
+}
+
+crypto::public_key Blockchain::get_output_key(uint64_t amount, uint64_t global_index) const
+{
+ output_data_t data = m_db->get_output_key(amount, global_index);
+ return data.pubkey;
+}
+
//------------------------------------------------------------------
// This function takes an RPC request for mixins and creates an RPC response
// with the requested mixins.
@@ -1576,80 +1677,18 @@ bool Blockchain::get_random_outs_for_amounts(const COMMAND_RPC_GET_RANDOM_OUTPUT
// from BlockchainDB where <n> is req.outs_count (number of mixins).
for (uint64_t amount : req.amounts)
{
- auto num_outs = m_db->get_num_outputs(amount);
- // ensure we don't include outputs that aren't yet eligible to be used
- // outpouts are sorted by height
- while (num_outs > 0)
- {
- const tx_out_index toi = m_db->get_output_tx_and_index(amount, num_outs - 1);
- const uint64_t height = m_db->get_tx_block_height(toi.first);
- if (height + CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE <= m_db->height())
- break;
- --num_outs;
- }
-
// create outs_for_amount struct and populate amount field
COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount& result_outs = *res.outs.insert(res.outs.end(), COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount());
result_outs.amount = amount;
- std::unordered_set<uint64_t> seen_indices;
-
- // if there aren't enough outputs to mix with (or just enough),
- // use all of them. Eventually this should become impossible.
- if (num_outs <= req.outs_count)
- {
- for (uint64_t i = 0; i < num_outs; i++)
- {
- // get tx_hash, tx_out_index from DB
- tx_out_index toi = m_db->get_output_tx_and_index(amount, i);
-
- // if tx is unlocked, add output to result_outs
- if (is_tx_spendtime_unlocked(m_db->get_tx_unlock_time(toi.first)))
- {
- add_out_to_get_random_outs(result_outs, amount, i);
- }
+ std::vector<uint64_t> indices = get_random_outputs(amount, req.outs_count);
- }
- }
- else
+ for (auto i : indices)
{
- // while we still need more mixins
- while (result_outs.outs.size() < req.outs_count)
- {
- // if we've gone through every possible output, we've gotten all we can
- if (seen_indices.size() == num_outs)
- {
- break;
- }
-
- // get a random output index from the DB. If we've already seen it,
- // return to the top of the loop and try again, otherwise add it to the
- // list of output indices we've seen.
-
- // triangular distribution over [a,b) with a=0, mode c=b=up_index_limit
- uint64_t r = crypto::rand<uint64_t>() % ((uint64_t)1 << 53);
- double frac = std::sqrt((double)r / ((uint64_t)1 << 53));
- uint64_t i = (uint64_t)(frac*num_outs);
- // just in case rounding up to 1 occurs after sqrt
- if (i == num_outs)
- --i;
-
- if (seen_indices.count(i))
- {
- continue;
- }
- seen_indices.emplace(i);
-
- // get tx_hash, tx_out_index from DB
- tx_out_index toi = m_db->get_output_tx_and_index(amount, i);
+ COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::out_entry& oe = *result_outs.outs.insert(result_outs.outs.end(), COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::out_entry());
- // if the output's transaction is unlocked, add the output's index to
- // our list.
- if (is_tx_spendtime_unlocked(m_db->get_tx_unlock_time(toi.first)))
- {
- add_out_to_get_random_outs(result_outs, amount, i);
- }
- }
+ oe.global_amount_index = i;
+ oe.out_key = get_output_key(amount, i);
}
}
return true;
@@ -1807,6 +1846,15 @@ bool Blockchain::get_outs(const COMMAND_RPC_GET_OUTPUTS_BIN::request& req, COMMA
return true;
}
//------------------------------------------------------------------
+void Blockchain::get_output_key_mask_unlocked(const uint64_t& amount, const uint64_t& index, crypto::public_key& key, rct::key& mask, bool& unlocked) const
+{
+ const auto o_data = m_db->get_output_key(amount, index);
+ key = o_data.pubkey;
+ mask = o_data.commitment;
+ tx_out_index toi = m_db->get_output_tx_and_index(amount, index);
+ unlocked = is_tx_spendtime_unlocked(m_db->get_tx_unlock_time(toi.first));
+}
+//------------------------------------------------------------------
// This function takes a list of block hashes from another node
// on the network to find where the split point is between us and them.
// This is used to see what to send another node that needs to sync.
@@ -2016,27 +2064,39 @@ void Blockchain::print_blockchain_outs(const std::string& file) const
// Find the split point between us and foreign blockchain and return
// (by reference) the most recent common block hash along with up to
// BLOCKS_IDS_SYNCHRONIZING_DEFAULT_COUNT additional (more recent) hashes.
-bool Blockchain::find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const
+bool Blockchain::find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, std::list<crypto::hash>& hashes, uint64_t& start_height, uint64_t& current_height) const
{
LOG_PRINT_L3("Blockchain::" << __func__);
CRITICAL_REGION_LOCAL(m_blockchain_lock);
// if we can't find the split point, return false
- if(!find_blockchain_supplement(qblock_ids, resp.start_height))
+ if(!find_blockchain_supplement(qblock_ids, start_height))
{
return false;
}
m_db->block_txn_start(true);
- resp.total_height = get_current_blockchain_height();
+ current_height = get_current_blockchain_height();
size_t count = 0;
- for(size_t i = resp.start_height; i < resp.total_height && count < BLOCKS_IDS_SYNCHRONIZING_DEFAULT_COUNT; i++, count++)
+ for(size_t i = start_height; i < current_height && count < BLOCKS_IDS_SYNCHRONIZING_DEFAULT_COUNT; i++, count++)
{
- resp.m_block_ids.push_back(m_db->get_block_hash_from_height(i));
+ hashes.push_back(m_db->get_block_hash_from_height(i));
}
+
m_db->block_txn_stop();
return true;
}
+
+bool Blockchain::find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const
+{
+ LOG_PRINT_L3("Blockchain::" << __func__);
+ CRITICAL_REGION_LOCAL(m_blockchain_lock);
+
+ bool result = find_blockchain_supplement(qblock_ids, resp.m_block_ids, resp.start_height, resp.total_height);
+ resp.cumulative_difficulty = m_db->get_block_cumulative_difficulty(m_db->height() - 1);
+
+ return result;
+}
//------------------------------------------------------------------
//FIXME: change argument to std::vector, low priority
// find split point between ours and foreign blockchain (or start at
@@ -2067,8 +2127,8 @@ bool Blockchain::find_blockchain_supplement(const uint64_t req_start_block, cons
m_db->block_txn_start(true);
total_height = get_current_blockchain_height();
- size_t count = 0;
- for(size_t i = start_height; i < total_height && count < max_count; i++, count++)
+ size_t count = 0, size = 0;
+ for(size_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 = m_db->get_block_blob_from_height(i);
@@ -2077,6 +2137,9 @@ bool Blockchain::find_blockchain_supplement(const uint64_t req_start_block, cons
std::list<crypto::hash> mis;
get_transactions_blobs(b.tx_hashes, blocks.back().second, mis);
CHECK_AND_ASSERT_MES(!mis.size(), false, "internal error, transaction from block not found");
+ size += blocks.back().first.size();
+ for (const auto &t: blocks.back().second)
+ size += t.size();
}
m_db->block_txn_stop();
return true;
@@ -2254,8 +2317,8 @@ bool Blockchain::check_tx_inputs(transaction& tx, uint64_t& max_used_block_heigh
TIME_MEASURE_FINISH(a);
if(m_show_time_stats)
{
- size_t mixin = tx.vin[0].type() == typeid(txin_to_key) ? boost::get<txin_to_key>(tx.vin[0]).key_offsets.size() - 1 : 0;
- MINFO("HASH: " << "-" << " I/M/O: " << tx.vin.size() << "/" << mixin << "/" << tx.vout.size() << " H: " << 0 << " chcktx: " << a);
+ size_t ring_size = tx.vin[0].type() == typeid(txin_to_key) ? boost::get<txin_to_key>(tx.vin[0]).key_offsets.size() : 0;
+ MINFO("HASH: " << "-" << " I/M/O: " << tx.vin.size() << "/" << ring_size << "/" << tx.vout.size() << " H: " << 0 << " chcktx: " << a);
}
return true;
}
@@ -2266,8 +2329,8 @@ bool Blockchain::check_tx_inputs(transaction& tx, uint64_t& max_used_block_heigh
TIME_MEASURE_FINISH(a);
if(m_show_time_stats)
{
- size_t mixin = tx.vin[0].type() == typeid(txin_to_key) ? boost::get<txin_to_key>(tx.vin[0]).key_offsets.size() - 1 : 0;
- MINFO("HASH: " << get_transaction_hash(tx) << " I/M/O: " << tx.vin.size() << "/" << mixin << "/" << tx.vout.size() << " H: " << max_used_block_height << " ms: " << a + m_fake_scan_time << " B: " << get_object_blobsize(tx));
+ size_t ring_size = tx.vin[0].type() == typeid(txin_to_key) ? boost::get<txin_to_key>(tx.vin[0]).key_offsets.size() : 0;
+ MINFO("HASH: " << get_transaction_hash(tx) << " I/M/O: " << tx.vin.size() << "/" << ring_size << "/" << tx.vout.size() << " H: " << max_used_block_height << " ms: " << a + m_fake_scan_time << " B: " << get_object_blobsize(tx));
}
if (!res)
return false;
@@ -2459,13 +2522,13 @@ bool Blockchain::check_tx_inputs(transaction& tx, tx_verification_context &tvc,
{
if (n_unmixable == 0)
{
- MERROR_VER("Tx " << get_transaction_hash(tx) << " has too low mixin (" << mixin << "), and no unmixable inputs");
+ MERROR_VER("Tx " << get_transaction_hash(tx) << " has too low ring size (" << (mixin + 1) << "), and no unmixable inputs");
tvc.m_low_mixin = true;
return false;
}
if (n_mixable > 1)
{
- MERROR_VER("Tx " << get_transaction_hash(tx) << " has too low mixin (" << mixin << "), and more than one mixable input with unmixable inputs");
+ MERROR_VER("Tx " << get_transaction_hash(tx) << " has too low ring size (" << (mixin + 1) << "), and more than one mixable input with unmixable inputs");
tvc.m_low_mixin = true;
return false;
}
@@ -2496,7 +2559,6 @@ bool Blockchain::check_tx_inputs(transaction& tx, tx_verification_context &tvc,
assert(it != m_check_txin_table.end());
}
- uint64_t t_t1 = 0;
std::vector<std::vector<rct::ctkey>> pubkeys(tx.vin.size());
std::vector < uint64_t > results;
results.resize(tx.vin.size(), 0);
@@ -2630,7 +2692,7 @@ bool Blockchain::check_tx_inputs(transaction& tx, tx_verification_context &tvc,
if (failed)
{
- MERROR_VER("Failed to check ring signatures!, t_loop: " << t_t1);
+ MERROR_VER("Failed to check ring signatures!");
return false;
}
}
@@ -2779,12 +2841,6 @@ bool Blockchain::check_tx_inputs(transaction& tx, tx_verification_context &tvc,
//------------------------------------------------------------------
void Blockchain::check_ring_signature(const crypto::hash &tx_prefix_hash, const crypto::key_image &key_image, const std::vector<rct::ctkey> &pubkeys, const std::vector<crypto::signature>& sig, uint64_t &result)
{
- if (m_is_in_checkpoint_zone)
- {
- result = true;
- return;
- }
-
std::vector<const crypto::public_key *> p_output_keys;
for (auto &key : pubkeys)
{
@@ -3097,6 +3153,8 @@ bool Blockchain::handle_block_to_main_chain(const block& bl, const crypto::hash&
CRITICAL_REGION_LOCAL(m_blockchain_lock);
TIME_MEASURE_START(t1);
+ static bool seen_future_version = false;
+
m_db->block_txn_start(true);
if(bl.prev_id != get_tail_id())
{
@@ -3106,6 +3164,18 @@ leave:
return false;
}
+ // warn users if they're running an old version
+ if (!seen_future_version && bl.major_version > m_hardfork->get_ideal_version())
+ {
+ seen_future_version = true;
+ const el::Level level = el::Level::Warning;
+ MCLOG_RED(level, "global", "**********************************************************************");
+ MCLOG_RED(level, "global", "A block was seen on the network with a version higher than the last");
+ MCLOG_RED(level, "global", "known one. This may be an old version of the daemon, and a software");
+ MCLOG_RED(level, "global", "update may be required to sync further. Try running: update check");
+ MCLOG_RED(level, "global", "**********************************************************************");
+ }
+
// this is a cheap test
if (!m_hardfork->check(bl))
{
@@ -3541,19 +3611,17 @@ void Blockchain::set_enforce_dns_checkpoints(bool enforce_checkpoints)
}
//------------------------------------------------------------------
-void Blockchain::block_longhash_worker(const uint64_t height, const std::vector<block> &blocks, std::unordered_map<crypto::hash, crypto::hash> &map) const
+void Blockchain::block_longhash_worker(uint64_t height, const std::vector<block> &blocks, std::unordered_map<crypto::hash, crypto::hash> &map) const
{
TIME_MEASURE_START(t);
slow_hash_allocate_state();
- //FIXME: height should be changing here, as get_block_longhash expects
- // the height of the block passed to it
for (const auto & block : blocks)
{
if (m_cancel)
- return;
+ break;
crypto::hash id = get_block_hash(block);
- crypto::hash pow = get_block_longhash(block, height);
+ crypto::hash pow = get_block_longhash(block, height++);
map.emplace(id, pow);
}
@@ -3564,12 +3632,23 @@ void Blockchain::block_longhash_worker(const uint64_t height, const std::vector<
//------------------------------------------------------------------
bool Blockchain::cleanup_handle_incoming_blocks(bool force_sync)
{
+ bool success = false;
+
MTRACE("Blockchain::" << __func__);
CRITICAL_REGION_BEGIN(m_blockchain_lock);
TIME_MEASURE_START(t1);
- m_db->batch_stop();
- if (m_sync_counter > 0)
+ try
+ {
+ m_db->batch_stop();
+ success = true;
+ }
+ catch (const std::exception &e)
+ {
+ MERROR("Exception in cleanup_handle_incoming_blocks: " << e.what());
+ }
+
+ if (success && m_sync_counter > 0)
{
if (force_sync)
{
@@ -3604,7 +3683,7 @@ bool Blockchain::cleanup_handle_incoming_blocks(bool force_sync)
CRITICAL_REGION_END();
m_tx_pool.unlock();
- return true;
+ return success;
}
//------------------------------------------------------------------
@@ -3745,9 +3824,11 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::list<block_complete_e
if (!blocks_exist)
{
m_blocks_longhash_table.clear();
+ uint64_t thread_height = height;
for (uint64_t i = 0; i < threads; i++)
{
- thread_list.push_back(new boost::thread(attrs, boost::bind(&Blockchain::block_longhash_worker, this, height + (i * batches), std::cref(blocks[i]), std::ref(maps[i]))));
+ thread_list.push_back(new boost::thread(attrs, boost::bind(&Blockchain::block_longhash_worker, this, thread_height, std::cref(blocks[i]), std::ref(maps[i]))));
+ thread_height += blocks[i].size();
}
for (size_t j = 0; j < thread_list.size(); j++)
@@ -3866,17 +3947,17 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::list<block_complete_e
offset_map[in_to_key.amount].push_back(offset);
}
-
- // sort and remove duplicate absolute_offsets in offset_map
- for (auto &offsets : offset_map)
- {
- std::sort(offsets.second.begin(), offsets.second.end());
- auto last = std::unique(offsets.second.begin(), offsets.second.end());
- offsets.second.erase(last, offsets.second.end());
- }
}
}
+ // sort and remove duplicate absolute_offsets in offset_map
+ for (auto &offsets : offset_map)
+ {
+ std::sort(offsets.second.begin(), offsets.second.end());
+ auto last = std::unique(offsets.second.begin(), offsets.second.end());
+ offsets.second.erase(last, offsets.second.end());
+ }
+
// [output] stores all transactions for each tx_out_index::hash found
std::vector<std::unordered_map<crypto::hash, cryptonote::transaction>> transactions(amounts.size());
@@ -4022,12 +4103,29 @@ bool Blockchain::for_all_txpool_txes(std::function<bool(const crypto::hash&, con
void Blockchain::set_user_options(uint64_t maxthreads, uint64_t blocks_per_sync, blockchain_db_sync_mode sync_mode, bool fast_sync)
{
+ if (sync_mode == db_defaultsync)
+ {
+ m_db_default_sync = true;
+ sync_mode = db_async;
+ }
m_db_sync_mode = sync_mode;
m_fast_sync = fast_sync;
m_db_blocks_per_sync = blocks_per_sync;
m_max_prepare_blocks_threads = maxthreads;
}
+void Blockchain::safesyncmode(const bool onoff)
+{
+ /* all of this is no-op'd if the user set a specific
+ * --db-sync-mode at startup.
+ */
+ if (m_db_default_sync)
+ {
+ m_db->safesyncmode(onoff);
+ m_db_sync_mode = onoff ? db_nosync : db_async;
+ }
+}
+
HardFork::State Blockchain::get_hard_fork_state() const
{
return m_hardfork->get_state();
@@ -4038,6 +4136,11 @@ bool Blockchain::get_hard_fork_voting_info(uint8_t version, uint32_t &window, ui
return m_hardfork->get_voting_info(version, window, votes, threshold, earliest_height, voting);
}
+uint64_t Blockchain::get_difficulty_target() const
+{
+ return get_current_hard_fork_version() < 2 ? DIFFICULTY_TARGET_V1 : DIFFICULTY_TARGET_V2;
+}
+
std::map<uint64_t, std::tuple<uint64_t, uint64_t, uint64_t>> Blockchain:: get_output_histogram(const std::vector<uint64_t> &amounts, bool unlocked, uint64_t recent_cutoff) const
{
return m_db->get_output_histogram(amounts, unlocked, recent_cutoff);
@@ -4081,7 +4184,7 @@ void Blockchain::cancel()
}
#if defined(PER_BLOCK_CHECKPOINT)
-static const char expected_block_hashes_hash[] = "23d8a8c73de7b2383c72a016d9a6034e69d62dd48077d1c414e064ceab6daa94";
+static const char expected_block_hashes_hash[] = "d3ca80d50661684cde0e715d46d7c19704d2e216b21ed088af9fd4ef37ed4d65";
void Blockchain::load_compiled_in_block_hashes()
{
if (m_fast_sync && get_blocks_dat_start(m_testnet) != nullptr && get_blocks_dat_size(m_testnet) > 0)
@@ -4154,6 +4257,15 @@ void Blockchain::load_compiled_in_block_hashes()
}
#endif
+bool Blockchain::is_within_compiled_block_hash_area(uint64_t height) const
+{
+#if defined(PER_BLOCK_CHECKPOINT)
+ return height < m_blocks_hash_check.size();
+#else
+ return false;
+#endif
+}
+
void Blockchain::lock()
{
m_blockchain_lock.lock();
diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h
index 4f2e4f0d3..e2da535cd 100644
--- a/src/cryptonote_core/blockchain.h
+++ b/src/cryptonote_core/blockchain.h
@@ -65,6 +65,7 @@ namespace cryptonote
*/
enum blockchain_db_sync_mode
{
+ db_defaultsync, //!< user didn't specify, use db_async
db_sync, //!< handle syncing calls instead of the backing db, synchronously
db_async, //!< handle syncing calls instead of the backing db, asynchronously
db_nosync //!< Leave syncing up to the backing db (safest, but slowest because of disk I/O)
@@ -373,6 +374,22 @@ namespace cryptonote
* BLOCKS_IDS_SYNCHRONIZING_DEFAULT_COUNT additional (more recent) hashes.
*
* @param qblock_ids the foreign chain's "short history" (see get_short_chain_history)
+ * @param hashes the hashes to be returned, return-by-reference
+ * @param start_height the start height, return-by-reference
+ * @param current_height the current blockchain height, return-by-reference
+ *
+ * @return true if a block found in common, else false
+ */
+ bool find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, std::list<crypto::hash>& hashes, uint64_t& start_height, uint64_t& current_height) const;
+
+ /**
+ * @brief get recent block hashes for a foreign chain
+ *
+ * Find the split point between us and foreign blockchain and return
+ * (by reference) the most recent common block hash along with up to
+ * BLOCKS_IDS_SYNCHRONIZING_DEFAULT_COUNT additional (more recent) hashes.
+ *
+ * @param qblock_ids the foreign chain's "short history" (see get_short_chain_history)
* @param resp return-by-reference the split height and subsequent blocks' hashes
*
* @return true if a block found in common, else false
@@ -426,6 +443,35 @@ namespace cryptonote
bool handle_get_objects(NOTIFY_REQUEST_GET_OBJECTS::request& arg, NOTIFY_RESPONSE_GET_OBJECTS::request& rsp);
/**
+ * @brief get number of outputs of an amount past the minimum spendable age
+ *
+ * @param amount the output amount
+ *
+ * @return the number of mature outputs
+ */
+ uint64_t get_num_mature_outputs(uint64_t amount) const;
+
+ /**
+ * @brief get random outputs (indices) for an amount
+ *
+ * @param amount the amount
+ * @param count the number of random outputs to choose
+ *
+ * @return the outputs' amount-global indices
+ */
+ std::vector<uint64_t> get_random_outputs(uint64_t amount, uint64_t count) const;
+
+ /**
+ * @brief get the public key for an output
+ *
+ * @param amount the output amount
+ * @param global_index the output amount-global index
+ *
+ * @return the public key
+ */
+ crypto::public_key get_output_key(uint64_t amount, uint64_t global_index) const;
+
+ /**
* @brief gets random outputs to mix with
*
* This function takes an RPC request for outputs to mix with
@@ -457,6 +503,17 @@ namespace cryptonote
bool get_outs(const COMMAND_RPC_GET_OUTPUTS_BIN::request& req, COMMAND_RPC_GET_OUTPUTS_BIN::response& res) const;
/**
+ * @brief gets an output's key and unlocked state
+ *
+ * @param amount in - the output amount
+ * @param index in - the output global amount index
+ * @param mask out - the output's RingCT mask
+ * @param key out - the output's key
+ * @param unlocked out - the output's unlocked state
+ */
+ void get_output_key_mask_unlocked(const uint64_t& amount, const uint64_t& index, crypto::public_key& key, rct::key& mask, bool& unlocked) const;
+
+ /**
* @brief gets random ringct outputs to mix with
*
* This function takes an RPC request for outputs to mix with
@@ -585,7 +642,7 @@ namespace cryptonote
*
* @return true if Blockchain is having the chain stored currently, else false
*/
- bool is_storing_blockchain()const{return m_is_blockchain_storing;}
+ bool is_storing_blockchain()const{return false;}
/**
* @brief gets the difficulty of the block with a given height
@@ -701,6 +758,11 @@ namespace cryptonote
blockchain_db_sync_mode sync_mode, bool fast_sync);
/**
+ * @brief Put DB in safe sync mode
+ */
+ void safesyncmode(const bool onoff);
+
+ /**
* @brief set whether or not to show/print time statistics
*
* @param stats the new time stats setting
@@ -746,6 +808,15 @@ namespace cryptonote
uint8_t get_ideal_hard_fork_version(uint64_t height) const { return m_hardfork->get_ideal_version(height); }
/**
+ * @brief returns the actual hardfork version for a given block height
+ *
+ * @param height the height for which to check version info
+ *
+ * @return the version
+ */
+ uint8_t get_hard_fork_version(uint64_t height) const { return m_hardfork->get(height); }
+
+ /**
* @brief get information about hardfork voting for a version
*
* @param version the version in question
@@ -760,6 +831,13 @@ namespace cryptonote
bool get_hard_fork_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint64_t &earliest_height, uint8_t &voting) const;
/**
+ * @brief get difficulty target based on chain and hardfork version
+ *
+ * @return difficulty target
+ */
+ uint64_t get_difficulty_target() const;
+
+ /**
* @brief remove transactions from the transaction pool (if present)
*
* @param txids a list of hashes of transactions to be removed
@@ -822,6 +900,16 @@ namespace cryptonote
*
* @return a reference to the BlockchainDB instance
*/
+ const BlockchainDB& get_db() const
+ {
+ return *m_db;
+ }
+
+ /**
+ * @brief get a reference to the BlockchainDB in use by Blockchain
+ *
+ * @return a reference to the BlockchainDB instance
+ */
BlockchainDB& get_db()
{
return *m_db;
@@ -846,7 +934,7 @@ namespace cryptonote
* @param blocks the blocks to be hashed
* @param map return-by-reference the hashes for each block
*/
- void block_longhash_worker(const uint64_t height, const std::vector<block> &blocks,
+ void block_longhash_worker(uint64_t height, const std::vector<block> &blocks,
std::unordered_map<crypto::hash, crypto::hash> &map) const;
/**
@@ -865,6 +953,9 @@ namespace cryptonote
cryptonote::blobdata get_txpool_tx_blob(const crypto::hash& txid) const;
bool for_all_txpool_txes(std::function<bool(const crypto::hash&, const txpool_tx_meta_t&, const cryptonote::blobdata*)>, bool include_blob = false) const;
+ bool is_within_compiled_block_hash_area(uint64_t height) const;
+ bool is_within_compiled_block_hash_area() const { return is_within_compiled_block_hash_area(m_db->height()); }
+
void lock();
void unlock();
@@ -910,6 +1001,7 @@ namespace cryptonote
blockchain_db_sync_mode m_db_sync_mode;
bool m_fast_sync;
bool m_show_time_stats;
+ bool m_db_default_sync;
uint64_t m_db_blocks_per_sync;
uint64_t m_max_prepare_blocks_threads;
uint64_t m_fake_pow_calc_time;
@@ -931,8 +1023,6 @@ namespace cryptonote
checkpoints m_checkpoints;
- std::atomic<bool> m_is_in_checkpoint_zone;
- std::atomic<bool> m_is_blockchain_storing;
bool m_enforce_dns_checkpoints;
HardFork *m_hardfork;
diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp
index c601c066c..01ee64b78 100644
--- a/src/cryptonote_core/cryptonote_core.cpp
+++ b/src/cryptonote_core/cryptonote_core.cpp
@@ -37,6 +37,7 @@ using namespace epee;
#include "common/util.h"
#include "common/updates.h"
#include "common/download.h"
+#include "common/task_region.h"
#include "warnings.h"
#include "crypto/crypto.h"
#include "cryptonote_config.h"
@@ -47,10 +48,6 @@ using namespace epee;
#include "cryptonote_basic/checkpoints.h"
#include "ringct/rctTypes.h"
#include "blockchain_db/blockchain_db.h"
-#include "blockchain_db/lmdb/db_lmdb.h"
-#if defined(BERKELEY_DB)
-#include "blockchain_db/berkeleydb/db_bdb.h"
-#endif
#include "ringct/rctSigs.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
@@ -76,8 +73,11 @@ namespace cryptonote
m_checkpoints_path(""),
m_last_dns_checkpoints_update(0),
m_last_json_checkpoints_update(0),
+ m_disable_dns_checkpoints(false),
+ m_threadpool(tools::thread_group::optimal()),
m_update_download(0)
{
+ m_checkpoints_updating.clear();
set_cryptonote_protocol(pprotocol);
}
void core::set_cryptonote_protocol(i_cryptonote_protocol* pprotocol)
@@ -105,7 +105,7 @@ namespace cryptonote
//-----------------------------------------------------------------------------------------------
bool core::update_checkpoints()
{
- if (m_testnet || m_fakechain) return true;
+ if (m_testnet || m_fakechain || m_disable_dns_checkpoints) return true;
if (m_checkpoints_updating.test_and_set()) return true;
@@ -156,19 +156,19 @@ namespace cryptonote
command_line::add_arg(desc, command_line::arg_testnet_on);
command_line::add_arg(desc, command_line::arg_dns_checkpoints);
- command_line::add_arg(desc, command_line::arg_db_type);
command_line::add_arg(desc, command_line::arg_prep_blocks_threads);
command_line::add_arg(desc, command_line::arg_fast_block_sync);
- command_line::add_arg(desc, command_line::arg_db_sync_mode);
command_line::add_arg(desc, command_line::arg_show_time_stats);
command_line::add_arg(desc, command_line::arg_block_sync_size);
command_line::add_arg(desc, command_line::arg_check_updates);
+ command_line::add_arg(desc, command_line::arg_fluffy_blocks);
// we now also need some of net_node's options (p2p bind arg, for separate data dir)
command_line::add_arg(desc, nodetool::arg_testnet_p2p_bind_port, false);
command_line::add_arg(desc, nodetool::arg_p2p_bind_port, false);
miner::init_options(desc);
+ BlockchainDB::init_options(desc);
}
//-----------------------------------------------------------------------------------------------
bool core::handle_command_line(const boost::program_options::variables_map& vm)
@@ -198,6 +198,7 @@ namespace cryptonote
set_enforce_dns_checkpoints(command_line::get_arg(vm, command_line::arg_dns_checkpoints));
test_drop_download_height(command_line::get_arg(vm, command_line::arg_test_drop_download_height));
+ m_fluffy_blocks_enabled = m_testnet || get_arg(vm, command_line::arg_fluffy_blocks);
if (command_line::get_arg(vm, command_line::arg_test_drop_download) == true)
test_drop_download();
@@ -241,6 +242,12 @@ namespace cryptonote
return m_blockchain_storage.get_transactions_blobs(txs_ids, txs, missed_txs);
}
//-----------------------------------------------------------------------------------------------
+ bool core::get_txpool_backlog(std::vector<tx_backlog_entry>& backlog) const
+ {
+ m_mempool.get_transaction_backlog(backlog);
+ return true;
+ }
+ //-----------------------------------------------------------------------------------------------
bool core::get_transactions(const std::vector<crypto::hash>& txs_ids, std::list<transaction>& txs, std::list<crypto::hash>& missed_txs) const
{
return m_blockchain_storage.get_transactions(txs_ids, txs, missed_txs);
@@ -272,8 +279,9 @@ namespace cryptonote
m_config_folder_mempool = m_config_folder_mempool + "/" + m_port;
}
- std::string db_type = command_line::get_arg(vm, command_line::arg_db_type);
- std::string db_sync_mode = command_line::get_arg(vm, command_line::arg_db_sync_mode);
+ std::string db_type = command_line::get_arg(vm, cryptonote::arg_db_type);
+ std::string db_sync_mode = command_line::get_arg(vm, cryptonote::arg_db_sync_mode);
+ bool db_salvage = command_line::get_arg(vm, cryptonote::arg_db_salvage) != 0;
bool fast_sync = command_line::get_arg(vm, command_line::arg_fast_block_sync) != 0;
uint64_t blocks_threads = command_line::get_arg(vm, command_line::arg_prep_blocks_threads);
std::string check_updates_string = command_line::get_arg(vm, command_line::arg_check_updates);
@@ -302,18 +310,8 @@ namespace cryptonote
// folder might not be a directory, etc, etc
catch (...) { }
- BlockchainDB* db = nullptr;
- uint64_t DBS_FAST_MODE = 0;
- uint64_t DBS_FASTEST_MODE = 0;
- uint64_t DBS_SAFE_MODE = 0;
- if (db_type == "lmdb")
- {
- db = new BlockchainLMDB();
- DBS_SAFE_MODE = MDB_NORDAHEAD;
- DBS_FAST_MODE = MDB_NORDAHEAD | MDB_NOSYNC;
- DBS_FASTEST_MODE = MDB_NORDAHEAD | MDB_NOSYNC | MDB_WRITEMAP | MDB_MAPASYNC;
- }
- else
+ BlockchainDB* db = new_db(db_type);
+ if (db == NULL)
{
LOG_ERROR("Attempted to use non-existent database type");
return false;
@@ -324,7 +322,7 @@ namespace cryptonote
const std::string filename = folder.string();
// default to fast:async:1
- blockchain_db_sync_mode sync_mode = db_async;
+ blockchain_db_sync_mode sync_mode = db_defaultsync;
uint64_t blocks_per_sync = 1;
try
@@ -339,7 +337,7 @@ namespace cryptonote
MDEBUG("option: " << option);
// default to fast:async:1
- uint64_t DEFAULT_FLAGS = DBS_FAST_MODE;
+ uint64_t DEFAULT_FLAGS = DBF_FAST;
if(options.size() == 0)
{
@@ -353,15 +351,19 @@ namespace cryptonote
if(options[0] == "safe")
{
safemode = true;
- db_flags = DBS_SAFE_MODE;
+ db_flags = DBF_SAFE;
sync_mode = db_nosync;
}
else if(options[0] == "fast")
- db_flags = DBS_FAST_MODE;
+ {
+ db_flags = DBF_FAST;
+ sync_mode = db_async;
+ }
else if(options[0] == "fastest")
{
- db_flags = DBS_FASTEST_MODE;
+ db_flags = DBF_FASTEST;
blocks_per_sync = 1000; // default to fastest:async:1000
+ sync_mode = db_async;
}
else
db_flags = DEFAULT_FLAGS;
@@ -383,6 +385,9 @@ namespace cryptonote
blocks_per_sync = bps;
}
+ if (db_salvage)
+ db_flags |= DBF_SALVAGE;
+
db->open(filename, db_flags);
if(!db->m_open)
return false;
@@ -410,8 +415,8 @@ namespace cryptonote
CHECK_AND_ASSERT_MES(r, false, "Failed to initialize blockchain storage");
block_sync_size = command_line::get_arg(vm, command_line::arg_block_sync_size);
- if (block_sync_size == 0)
- block_sync_size = BLOCKS_SYNCHRONIZING_DEFAULT_COUNT;
+
+ MGINFO("Loading checkpoints");
// load json & DNS checkpoints, and verify them
// with respect to what blocks we already have
@@ -482,11 +487,9 @@ namespace cryptonote
return false;
}
//-----------------------------------------------------------------------------------------------
- bool core::handle_incoming_tx(const blobdata& tx_blob, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay)
+ bool core::handle_incoming_tx_pre(const blobdata& tx_blob, tx_verification_context& tvc, cryptonote::transaction &tx, crypto::hash &tx_hash, crypto::hash &tx_prefixt_hash, bool keeped_by_block, bool relayed, bool do_not_relay)
{
tvc = boost::value_initialized<tx_verification_context>();
- //want to process all transactions sequentially
- CRITICAL_REGION_LOCAL(m_incoming_tx_lock);
if(tx_blob.size() > get_max_tx_size())
{
@@ -496,9 +499,8 @@ namespace cryptonote
return false;
}
- crypto::hash tx_hash = null_hash;
- crypto::hash tx_prefixt_hash = null_hash;
- transaction tx;
+ tx_hash = null_hash;
+ tx_prefixt_hash = null_hash;
if(!parse_tx_from_blob(tx, tx_hash, tx_prefixt_hash, tx_blob))
{
@@ -508,15 +510,18 @@ namespace cryptonote
}
//std::cout << "!"<< tx.vin.size() << std::endl;
+ bad_semantics_txes_lock.lock();
for (int idx = 0; idx < 2; ++idx)
{
if (bad_semantics_txes[idx].find(tx_hash) != bad_semantics_txes[idx].end())
{
+ bad_semantics_txes_lock.unlock();
LOG_PRINT_L1("Transaction already seen with bad semantics, rejected");
tvc.m_verifivation_failed = true;
return false;
}
}
+ bad_semantics_txes_lock.unlock();
uint8_t version = m_blockchain_storage.get_current_hard_fork_version();
const size_t max_tx_version = version == 1 ? 1 : 2;
@@ -527,18 +532,11 @@ namespace cryptonote
return false;
}
- if(m_mempool.have_tx(tx_hash))
- {
- LOG_PRINT_L2("tx " << tx_hash << "already have transaction in tx_pool");
- return true;
- }
-
- if(m_blockchain_storage.have_tx(tx_hash))
- {
- LOG_PRINT_L2("tx " << tx_hash << " already have transaction in blockchain");
- return true;
- }
-
+ return true;
+ }
+ //-----------------------------------------------------------------------------------------------
+ bool core::handle_incoming_tx_post(const blobdata& tx_blob, tx_verification_context& tvc, cryptonote::transaction &tx, crypto::hash &tx_hash, crypto::hash &tx_prefixt_hash, bool keeped_by_block, bool relayed, bool do_not_relay)
+ {
if(!check_tx_syntax(tx))
{
LOG_PRINT_L1("WRONG TRANSACTION BLOB, Failed to check tx " << tx_hash << " syntax, rejected");
@@ -563,27 +561,112 @@ namespace cryptonote
rv.outPk[n].dest = rct::pk2rct(boost::get<txout_to_key>(tx.vout[n].target).key);
}
- if(!check_tx_semantic(tx, keeped_by_block))
+ if (keeped_by_block && get_blockchain_storage().is_within_compiled_block_hash_area())
+ {
+ MTRACE("Skipping semantics check for tx kept by block in embedded hash area");
+ }
+ else if(!check_tx_semantic(tx, keeped_by_block))
{
LOG_PRINT_L1("WRONG TRANSACTION BLOB, Failed to check tx " << tx_hash << " semantic, rejected");
tvc.m_verifivation_failed = true;
+ bad_semantics_txes_lock.lock();
bad_semantics_txes[0].insert(tx_hash);
if (bad_semantics_txes[0].size() >= BAD_SEMANTICS_TXES_MAX_SIZE)
{
std::swap(bad_semantics_txes[0], bad_semantics_txes[1]);
bad_semantics_txes[0].clear();
}
+ bad_semantics_txes_lock.unlock();
return false;
}
- bool r = add_new_tx(tx, tx_hash, tx_prefixt_hash, tx_blob.size(), tvc, keeped_by_block, relayed, do_not_relay);
- if(tvc.m_verifivation_failed)
- {MERROR_VER("Transaction verification failed: " << tx_hash);}
- else if(tvc.m_verifivation_impossible)
- {MERROR_VER("Transaction verification impossible: " << tx_hash);}
+ return true;
+ }
+ //-----------------------------------------------------------------------------------------------
+ bool core::handle_incoming_txs(const std::list<blobdata>& tx_blobs, std::vector<tx_verification_context>& tvc, bool keeped_by_block, bool relayed, bool do_not_relay)
+ {
+ TRY_ENTRY();
+
+ struct result { bool res; cryptonote::transaction tx; crypto::hash hash; crypto::hash prefix_hash; bool in_txpool; bool in_blockchain; };
+ std::vector<result> results(tx_blobs.size());
+
+ tvc.resize(tx_blobs.size());
+ tools::task_region(m_threadpool, [&] (tools::task_region_handle& region) {
+ std::list<blobdata>::const_iterator it = tx_blobs.begin();
+ for (size_t i = 0; i < tx_blobs.size(); i++, ++it) {
+ region.run([&, i, it] {
+ try
+ {
+ results[i].res = handle_incoming_tx_pre(*it, tvc[i], results[i].tx, results[i].hash, results[i].prefix_hash, keeped_by_block, relayed, do_not_relay);
+ }
+ catch (const std::exception &e)
+ {
+ MERROR_VER("Exception in handle_incoming_tx_pre: " << e.what());
+ results[i].res = false;
+ }
+ });
+ }
+ });
+ tools::task_region(m_threadpool, [&] (tools::task_region_handle& region) {
+ std::list<blobdata>::const_iterator it = tx_blobs.begin();
+ for (size_t i = 0; i < tx_blobs.size(); i++, ++it) {
+ if (!results[i].res)
+ continue;
+ if(m_mempool.have_tx(results[i].hash))
+ {
+ LOG_PRINT_L2("tx " << results[i].hash << "already have transaction in tx_pool");
+ }
+ else if(m_blockchain_storage.have_tx(results[i].hash))
+ {
+ LOG_PRINT_L2("tx " << results[i].hash << " already have transaction in blockchain");
+ }
+ else
+ {
+ region.run([&, i, it] {
+ try
+ {
+ results[i].res = handle_incoming_tx_post(*it, tvc[i], results[i].tx, results[i].hash, results[i].prefix_hash, keeped_by_block, relayed, do_not_relay);
+ }
+ catch (const std::exception &e)
+ {
+ MERROR_VER("Exception in handle_incoming_tx_post: " << e.what());
+ results[i].res = false;
+ }
+ });
+ }
+ }
+ });
+
+ bool ok = true;
+ std::list<blobdata>::const_iterator it = tx_blobs.begin();
+ for (size_t i = 0; i < tx_blobs.size(); i++, ++it) {
+ if (!results[i].res)
+ {
+ ok = false;
+ continue;
+ }
+
+ ok &= add_new_tx(results[i].tx, results[i].hash, results[i].prefix_hash, it->size(), tvc[i], keeped_by_block, relayed, do_not_relay);
+ if(tvc[i].m_verifivation_failed)
+ {MERROR_VER("Transaction verification failed: " << results[i].hash);}
+ else if(tvc[i].m_verifivation_impossible)
+ {MERROR_VER("Transaction verification impossible: " << results[i].hash);}
- if(tvc.m_added_to_pool)
- MDEBUG("tx added: " << tx_hash);
+ if(tvc[i].m_added_to_pool)
+ MDEBUG("tx added: " << results[i].hash);
+ }
+ return ok;
+
+ CATCH_ENTRY_L0("core::handle_incoming_txs()", false);
+ }
+ //-----------------------------------------------------------------------------------------------
+ bool core::handle_incoming_tx(const blobdata& tx_blob, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay)
+ {
+ std::list<cryptonote::blobdata> tx_blobs;
+ tx_blobs.push_back(tx_blob);
+ std::vector<tx_verification_context> tvcv(1);
+ bool r = handle_incoming_txs(tx_blobs, tvcv, keeped_by_block, relayed, do_not_relay);
+ tvc = tvcv[0];
return r;
}
//-----------------------------------------------------------------------------------------------
@@ -659,6 +742,12 @@ namespace cryptonote
return false;
}
+ if (!check_tx_inputs_ring_members_diff(tx))
+ {
+ MERROR_VER("tx uses duplicate ring members");
+ return false;
+ }
+
if (!check_tx_inputs_keyimages_domain(tx))
{
MERROR_VER("tx uses key image not in the valid domain");
@@ -711,6 +800,23 @@ namespace cryptonote
return true;
}
//-----------------------------------------------------------------------------------------------
+ size_t core::get_block_sync_size(uint64_t height) const
+ {
+ static const uint64_t quick_height = m_testnet ? 801219 : 1220516;
+ if (block_sync_size > 0)
+ return block_sync_size;
+ if (height >= quick_height)
+ return BLOCKS_SYNCHRONIZING_DEFAULT_COUNT;
+ return BLOCKS_SYNCHRONIZING_DEFAULT_COUNT_PRE_V4;
+ }
+ //-----------------------------------------------------------------------------------------------
+ bool core::are_key_images_spent_in_pool(const std::vector<crypto::key_image>& key_im, std::vector<bool> &spent) const
+ {
+ spent.clear();
+
+ return m_mempool.check_for_key_images(key_im, spent);
+ }
+ //-----------------------------------------------------------------------------------------------
std::pair<uint64_t, uint64_t> core::get_coinbase_tx_sum(const uint64_t start_offset, const size_t count)
{
uint64_t emission_amount = 0;
@@ -751,6 +857,22 @@ namespace cryptonote
return true;
}
//-----------------------------------------------------------------------------------------------
+ bool core::check_tx_inputs_ring_members_diff(const transaction& tx) const
+ {
+ const uint8_t version = m_blockchain_storage.get_current_hard_fork_version();
+ if (version >= 6)
+ {
+ for(const auto& in: tx.vin)
+ {
+ CHECKED_GET_SPECIFIC_VARIANT(in, const txin_to_key, tokey_in, false);
+ for (size_t n = 1; n < tokey_in.key_offsets.size(); ++n)
+ if (tokey_in.key_offsets[n] == 0)
+ return false;
+ }
+ }
+ return true;
+ }
+ //-----------------------------------------------------------------------------------------------
bool core::check_tx_inputs_keyimages_domain(const transaction& tx) const
{
std::unordered_set<crypto::key_image> ki;
@@ -948,6 +1070,11 @@ namespace cryptonote
m_miner.on_synchronized();
}
//-----------------------------------------------------------------------------------------------
+ void core::safesyncmode(const bool onoff)
+ {
+ m_blockchain_storage.safesyncmode(onoff);
+ }
+ //-----------------------------------------------------------------------------------------------
bool core::add_new_block(const block& b, block_verification_context& bvc)
{
return m_blockchain_storage.add_new_block(b, bvc);
@@ -964,17 +1091,20 @@ namespace cryptonote
//-----------------------------------------------------------------------------------------------
bool core::cleanup_handle_incoming_blocks(bool force_sync)
{
+ bool success = false;
try {
- m_blockchain_storage.cleanup_handle_incoming_blocks(force_sync);
+ success = m_blockchain_storage.cleanup_handle_incoming_blocks(force_sync);
}
catch (...) {}
m_incoming_tx_lock.unlock();
- return true;
+ return success;
}
//-----------------------------------------------------------------------------------------------
bool core::handle_incoming_block(const blobdata& block_blob, block_verification_context& bvc, bool update_miner_blocktemplate)
{
+ TRY_ENTRY();
+
// load json & DNS checkpoints every 10min/hour respectively,
// and verify them with respect to what blocks we already have
CHECK_AND_ASSERT_MES(update_checkpoints(), false, "One or more checkpoints loaded from json or dns conflicted with existing checkpoints.");
@@ -998,6 +1128,8 @@ namespace cryptonote
if(update_miner_blocktemplate && bvc.m_added_to_main_chain)
update_miner_block_template();
return true;
+
+ CATCH_ENTRY_L0("core::handle_incoming_block()", false);
}
//-----------------------------------------------------------------------------------------------
// Used by the RPC server to check the size of an incoming
@@ -1017,6 +1149,11 @@ namespace cryptonote
return m_blockchain_storage.get_tail_id();
}
//-----------------------------------------------------------------------------------------------
+ difficulty_type core::get_block_cumulative_difficulty(uint64_t height) const
+ {
+ return m_blockchain_storage.get_db().get_block_cumulative_difficulty(height);
+ }
+ //-----------------------------------------------------------------------------------------------
size_t core::get_pool_transactions_count() const
{
return m_mempool.get_transactions_count();
@@ -1070,6 +1207,11 @@ namespace cryptonote
return m_mempool.get_transactions_and_spent_keys_info(tx_infos, key_image_infos);
}
//-----------------------------------------------------------------------------------------------
+ bool core::get_pool_for_rpc(std::vector<cryptonote::rpc::tx_in_pool>& tx_infos, cryptonote::rpc::key_images_with_tx_hashes& key_image_infos) const
+ {
+ return m_mempool.get_pool_for_rpc(tx_infos, key_image_infos);
+ }
+ //-----------------------------------------------------------------------------------------------
bool core::get_short_chain_history(std::list<crypto::hash>& ids) const
{
return m_blockchain_storage.get_short_chain_history(ids);
@@ -1146,6 +1288,16 @@ namespace cryptonote
return true;
}
//-----------------------------------------------------------------------------------------------
+ uint8_t core::get_ideal_hard_fork_version(uint64_t height) const
+ {
+ return get_blockchain_storage().get_ideal_hard_fork_version(height);
+ }
+ //-----------------------------------------------------------------------------------------------
+ uint8_t core::get_hard_fork_version(uint64_t height) const
+ {
+ return get_blockchain_storage().get_hard_fork_version(height);
+ }
+ //-----------------------------------------------------------------------------------------------
bool core::check_updates()
{
static const char software[] = "monero";
diff --git a/src/cryptonote_core/cryptonote_core.h b/src/cryptonote_core/cryptonote_core.h
index e5fbf7f91..a9ee9e9d0 100644
--- a/src/cryptonote_core/cryptonote_core.h
+++ b/src/cryptonote_core/cryptonote_core.h
@@ -40,6 +40,7 @@
#include "cryptonote_protocol/cryptonote_protocol_handler_common.h"
#include "storages/portable_storage_template_helper.h"
#include "common/download.h"
+#include "common/thread_group.h"
#include "tx_pool.h"
#include "blockchain.h"
#include "cryptonote_basic/miner.h"
@@ -115,6 +116,22 @@ namespace cryptonote
bool handle_incoming_tx(const blobdata& tx_blob, tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
/**
+ * @brief handles a list of incoming transactions
+ *
+ * Parses incoming transactions and, if nothing is obviously wrong,
+ * passes them along to the transaction pool
+ *
+ * @param tx_blobs the txs to handle
+ * @param tvc metadata about the transactions' validity
+ * @param keeped_by_block if the transactions have been in a block
+ * @param relayed whether or not the transactions were relayed to us
+ * @param do_not_relay whether to prevent the transactions from being relayed
+ *
+ * @return true if the transactions made it to the transaction pool, otherwise false
+ */
+ bool handle_incoming_txs(const std::list<blobdata>& tx_blobs, std::vector<tx_verification_context>& tvc, bool keeped_by_block, bool relayed, bool do_not_relay);
+
+ /**
* @brief handles an incoming block
*
* periodic update to checkpoints is triggered here
@@ -390,6 +407,13 @@ namespace cryptonote
void set_enforce_dns_checkpoints(bool enforce_dns);
/**
+ * @brief set whether or not to enable or disable DNS checkpoints
+ *
+ * @param disble whether to disable DNS checkpoints
+ */
+ void disable_dns_checkpoints(bool disable = true) { m_disable_dns_checkpoints = disable; }
+
+ /**
* @copydoc tx_memory_pool::have_tx
*
* @note see tx_memory_pool::have_tx
@@ -404,6 +428,13 @@ namespace cryptonote
bool get_pool_transactions(std::list<transaction>& txs) const;
/**
+ * @copydoc tx_memory_pool::get_txpool_backlog
+ *
+ * @note see tx_memory_pool::get_txpool_backlog
+ */
+ bool get_txpool_backlog(std::vector<tx_backlog_entry>& backlog) const;
+
+ /**
* @copydoc tx_memory_pool::get_transactions
*
* @note see tx_memory_pool::get_transactions
@@ -432,6 +463,13 @@ namespace cryptonote
bool get_pool_transactions_and_spent_keys_info(std::vector<tx_info>& tx_infos, std::vector<spent_key_image_info>& key_image_infos) const;
/**
+ * @copydoc tx_memory_pool::get_pool_for_rpc
+ *
+ * @note see tx_memory_pool::get_pool_for_rpc
+ */
+ bool get_pool_for_rpc(std::vector<cryptonote::rpc::tx_in_pool>& tx_infos, cryptonote::rpc::key_images_with_tx_hashes& key_image_infos) const;
+
+ /**
* @copydoc tx_memory_pool::get_transactions_count
*
* @note see tx_memory_pool::get_transactions_count
@@ -497,6 +535,13 @@ namespace cryptonote
crypto::hash get_tail_id() const;
/**
+ * @copydoc Blockchain::get_block_cumulative_difficulty
+ *
+ * @note see Blockchain::get_block_cumulative_difficulty
+ */
+ difficulty_type get_block_cumulative_difficulty(uint64_t height) const;
+
+ /**
* @copydoc Blockchain::get_random_outs_for_amounts
*
* @note see Blockchain::get_random_outs_for_amounts
@@ -583,6 +628,13 @@ namespace cryptonote
void on_synchronized();
/**
+ * @copydoc Blockchain::safesyncmode
+ *
+ * 2note see Blockchain::safesyncmode
+ */
+ void safesyncmode(const bool onoff);
+
+ /**
* @brief sets the target blockchain height
*
* @param target_blockchain_height the height to set
@@ -597,6 +649,20 @@ namespace cryptonote
uint64_t get_target_blockchain_height() const;
/**
+ * @brief return the ideal hard fork version for a given block height
+ *
+ * @return what it says above
+ */
+ uint8_t get_ideal_hard_fork_version(uint64_t height) const;
+
+ /**
+ * @brief return the hard fork version for a given block height
+ *
+ * @return what it says above
+ */
+ uint8_t get_hard_fork_version(uint64_t height) const;
+
+ /**
* @brief gets start_time
*
*/
@@ -649,11 +715,21 @@ namespace cryptonote
bool are_key_images_spent(const std::vector<crypto::key_image>& key_im, std::vector<bool> &spent) const;
/**
+ * @brief check if multiple key images are spent in the transaction pool
+ *
+ * @param key_im list of key images to check
+ * @param spent return-by-reference result for each image checked
+ *
+ * @return true
+ */
+ bool are_key_images_spent_in_pool(const std::vector<crypto::key_image>& key_im, std::vector<bool> &spent) const;
+
+ /**
* @brief get the number of blocks to sync in one go
*
* @return the number of blocks to sync in one go
*/
- size_t get_block_sync_size() const { return block_sync_size; }
+ size_t get_block_sync_size(uint64_t height) const;
/**
* @brief get the sum of coinbase tx amounts between blocks
@@ -669,6 +745,13 @@ namespace cryptonote
*/
bool get_testnet() const { return m_testnet; };
+ /**
+ * @brief get whether fluffy blocks are enabled
+ *
+ * @return whether fluffy blocks are enabled
+ */
+ bool fluffy_blocks_enabled() const { return m_fluffy_blocks_enabled; }
+
private:
/**
@@ -753,6 +836,9 @@ namespace cryptonote
*/
bool check_tx_semantic(const transaction& tx, bool keeped_by_block) const;
+ bool handle_incoming_tx_pre(const blobdata& tx_blob, tx_verification_context& tvc, cryptonote::transaction &tx, crypto::hash &tx_hash, crypto::hash &tx_prefixt_hash, bool keeped_by_block, bool relayed, bool do_not_relay);
+ bool handle_incoming_tx_post(const blobdata& tx_blob, tx_verification_context& tvc, cryptonote::transaction &tx, crypto::hash &tx_hash, crypto::hash &tx_prefixt_hash, bool keeped_by_block, bool relayed, bool do_not_relay);
+
/**
* @copydoc miner::on_block_chain_update
*
@@ -781,6 +867,15 @@ namespace cryptonote
bool check_tx_inputs_keyimages_diff(const transaction& tx) const;
/**
+ * @brief verify that each ring uses distinct members
+ *
+ * @param tx the transaction to check
+ *
+ * @return false if any ring uses duplicate members, true otherwise
+ */
+ bool check_tx_inputs_ring_members_diff(const transaction& tx) const;
+
+ /**
* @brief verify that each input key image in a transaction is in
* the valid domain
*
@@ -853,12 +948,16 @@ namespace cryptonote
time_t m_last_json_checkpoints_update; //!< time when json checkpoints were last updated
std::atomic_flag m_checkpoints_updating; //!< set if checkpoints are currently updating to avoid multiple threads attempting to update at once
+ bool m_disable_dns_checkpoints;
size_t block_sync_size;
time_t start_time;
std::unordered_set<crypto::hash> bad_semantics_txes[2];
+ boost::mutex bad_semantics_txes_lock;
+
+ tools::thread_group m_threadpool;
enum {
UPDATES_DISABLED,
@@ -870,6 +969,8 @@ namespace cryptonote
tools::download_async_handle m_update_download;
size_t m_last_update_length;
boost::mutex m_update_mutex;
+
+ bool m_fluffy_blocks_enabled;
};
}
diff --git a/src/cryptonote_core/cryptonote_tx_utils.cpp b/src/cryptonote_core/cryptonote_tx_utils.cpp
index 26d5fb767..94f069827 100644
--- a/src/cryptonote_core/cryptonote_tx_utils.cpp
+++ b/src/cryptonote_core/cryptonote_tx_utils.cpp
@@ -265,7 +265,7 @@ namespace cryptonote
// "Shuffle" outs
std::vector<tx_destination_entry> shuffled_dsts(destinations);
- std::sort(shuffled_dsts.begin(), shuffled_dsts.end(), [](const tx_destination_entry& de1, const tx_destination_entry& de2) { return de1.amount < de2.amount; } );
+ std::random_shuffle(shuffled_dsts.begin(), shuffled_dsts.end(), [](unsigned int i) { return crypto::rand<unsigned int>() % i; });
uint64_t summary_outs_money = 0;
//fill outputs
@@ -371,7 +371,7 @@ namespace cryptonote
// enforce same mixin for all outputs
for (size_t i = 1; i < sources.size(); ++i) {
if (n_total_outs != sources[i].outputs.size()) {
- LOG_ERROR("Non-simple ringct transaction has varying mixin");
+ LOG_ERROR("Non-simple ringct transaction has varying ring size");
return false;
}
}
diff --git a/src/cryptonote_core/cryptonote_tx_utils.h b/src/cryptonote_core/cryptonote_tx_utils.h
index 933070e1e..7aa7c280d 100644
--- a/src/cryptonote_core/cryptonote_tx_utils.h
+++ b/src/cryptonote_core/cryptonote_tx_utils.h
@@ -32,6 +32,7 @@
#include "cryptonote_basic/cryptonote_format_utils.h"
#include <boost/serialization/vector.hpp>
#include <boost/serialization/utility.hpp>
+#include "ringct/rctOps.h"
namespace cryptonote
{
diff --git a/src/cryptonote_core/tx_pool.cpp b/src/cryptonote_core/tx_pool.cpp
index ffb5b478b..7de392036 100644
--- a/src/cryptonote_core/tx_pool.cpp
+++ b/src/cryptonote_core/tx_pool.cpp
@@ -80,7 +80,7 @@ namespace cryptonote
uint64_t get_transaction_size_limit(uint8_t version)
{
- return get_min_block_size(version) * 125 / 100 - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE;
+ return get_min_block_size(version) - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE;
}
// This class is meant to create a batch when none currently exists.
@@ -92,7 +92,7 @@ namespace cryptonote
LockedTXN(Blockchain &b): m_blockchain(b), m_batch(false) {
m_batch = m_blockchain.get_db().batch_start();
}
- ~LockedTXN() { if (m_batch) { m_blockchain.get_db().batch_stop(); } }
+ ~LockedTXN() { try { if (m_batch) { m_blockchain.get_db().batch_stop(); } } catch (const std::exception &e) { MWARNING("LockedTXN dtor filtering exception: " << e.what()); } }
private:
Blockchain &m_blockchain;
bool m_batch;
@@ -225,6 +225,7 @@ namespace cryptonote
meta.last_relayed_time = time(NULL);
meta.relayed = relayed;
meta.do_not_relay = do_not_relay;
+ memset(meta.padding, 0, sizeof(meta.padding));
try
{
CRITICAL_REGION_LOCAL1(m_blockchain);
@@ -261,6 +262,7 @@ namespace cryptonote
meta.last_relayed_time = time(NULL);
meta.relayed = relayed;
meta.do_not_relay = do_not_relay;
+ memset(meta.padding, 0, sizeof(meta.padding));
try
{
@@ -551,6 +553,17 @@ namespace cryptonote
});
}
//------------------------------------------------------------------
+ void tx_memory_pool::get_transaction_backlog(std::vector<tx_backlog_entry>& backlog) const
+ {
+ CRITICAL_REGION_LOCAL(m_transactions_lock);
+ CRITICAL_REGION_LOCAL1(m_blockchain);
+ const uint64_t now = time(NULL);
+ m_blockchain.for_all_txpool_txes([&backlog, now](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *bd){
+ backlog.push_back({meta.blob_size, meta.fee, meta.receive_time - now});
+ return true;
+ });
+ }
+ //------------------------------------------------------------------
void tx_memory_pool::get_transaction_stats(struct txpool_stats& stats) const
{
CRITICAL_REGION_LOCAL(m_transactions_lock);
@@ -558,7 +571,10 @@ namespace cryptonote
const uint64_t now = time(NULL);
std::map<uint64_t, txpool_histo> agebytes;
stats.txs_total = m_blockchain.get_txpool_tx_count();
- m_blockchain.for_all_txpool_txes([&stats, now, &agebytes](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *bd){
+ std::vector<uint32_t> sizes;
+ sizes.reserve(stats.txs_total);
+ m_blockchain.for_all_txpool_txes([&stats, &sizes, now, &agebytes](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *bd){
+ sizes.push_back(meta.blob_size);
stats.bytes_total += meta.blob_size;
if (!stats.bytes_min || meta.blob_size < stats.bytes_min)
stats.bytes_min = meta.blob_size;
@@ -573,11 +589,12 @@ namespace cryptonote
stats.num_10m++;
if (meta.last_failed_height)
stats.num_failing++;
- uint64_t age = now - meta.receive_time;
+ uint64_t age = now - meta.receive_time + (now == meta.receive_time);
agebytes[age].txs++;
agebytes[age].bytes += meta.blob_size;
return true;
});
+ stats.bytes_med = epee::misc_utils::median(sizes);
if (stats.txs_total > 1)
{
/* looking for 98th percentile */
@@ -667,6 +684,65 @@ namespace cryptonote
return true;
}
//---------------------------------------------------------------------------------
+ bool tx_memory_pool::get_pool_for_rpc(std::vector<cryptonote::rpc::tx_in_pool>& tx_infos, cryptonote::rpc::key_images_with_tx_hashes& key_image_infos) const
+ {
+ CRITICAL_REGION_LOCAL(m_transactions_lock);
+ CRITICAL_REGION_LOCAL1(m_blockchain);
+ m_blockchain.for_all_txpool_txes([&tx_infos, key_image_infos](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata *bd){
+ cryptonote::rpc::tx_in_pool txi;
+ txi.tx_hash = txid;
+ transaction tx;
+ if (!parse_and_validate_tx_from_blob(*bd, tx))
+ {
+ MERROR("Failed to parse tx from txpool");
+ // continue
+ return true;
+ }
+ txi.tx = tx;
+ txi.blob_size = meta.blob_size;
+ txi.fee = meta.fee;
+ txi.kept_by_block = meta.kept_by_block;
+ txi.max_used_block_height = meta.max_used_block_height;
+ txi.max_used_block_hash = meta.max_used_block_id;
+ txi.last_failed_block_height = meta.last_failed_height;
+ 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.do_not_relay = meta.do_not_relay;
+ tx_infos.push_back(txi);
+ return true;
+ }, true);
+
+ for (const key_images_container::value_type& kee : m_spent_key_images) {
+ std::vector<crypto::hash> tx_hashes;
+ const std::unordered_set<crypto::hash>& kei_image_set = kee.second;
+ for (const crypto::hash& tx_id_hash : kei_image_set)
+ {
+ tx_hashes.push_back(tx_id_hash);
+ }
+
+ const crypto::key_image& k_image = kee.first;
+ key_image_infos[k_image] = tx_hashes;
+ }
+ return true;
+ }
+ //---------------------------------------------------------------------------------
+ bool tx_memory_pool::check_for_key_images(const std::vector<crypto::key_image>& key_images, std::vector<bool> spent) const
+ {
+ CRITICAL_REGION_LOCAL(m_transactions_lock);
+ CRITICAL_REGION_LOCAL1(m_blockchain);
+
+ spent.clear();
+
+ for (const auto& image : key_images)
+ {
+ spent.push_back(m_spent_key_images.find(image) == m_spent_key_images.end() ? false : true);
+ }
+
+ return true;
+ }
+ //---------------------------------------------------------------------------------
bool tx_memory_pool::get_transaction(const crypto::hash& id, cryptonote::blobdata& txblob) const
{
CRITICAL_REGION_LOCAL(m_transactions_lock);
@@ -846,6 +922,9 @@ namespace cryptonote
std::unordered_set<crypto::key_image> k_images;
LOG_PRINT_L2("Filling block template, median size " << median_size << ", " << m_txs_by_fee_and_receive_time.size() << " txes in the pool");
+
+ LockedTXN lock(m_blockchain);
+
auto sorted_it = m_txs_by_fee_and_receive_time.begin();
while (sorted_it != m_txs_by_fee_and_receive_time.end())
{
@@ -903,7 +982,21 @@ namespace cryptonote
// Skip transactions that are not ready to be
// included into the blockchain or that are
// missing key images
- if (!is_transaction_ready_to_go(meta, tx))
+ const cryptonote::txpool_tx_meta_t original_meta = meta;
+ bool ready = is_transaction_ready_to_go(meta, tx);
+ if (memcmp(&original_meta, &meta, sizeof(meta)))
+ {
+ try
+ {
+ m_blockchain.update_txpool_tx(sorted_it->second, meta);
+ }
+ catch (const std::exception &e)
+ {
+ MERROR("Failed to update tx meta: " << e.what());
+ // continue, not fatal
+ }
+ }
+ if (!ready)
{
LOG_PRINT_L2(" not ready to go");
sorted_it++;
diff --git a/src/cryptonote_core/tx_pool.h b/src/cryptonote_core/tx_pool.h
index 1858ccdd8..3e4ccb338 100644
--- a/src/cryptonote_core/tx_pool.h
+++ b/src/cryptonote_core/tx_pool.h
@@ -46,6 +46,7 @@
#include "blockchain_db/blockchain_db.h"
#include "crypto/hash.h"
#include "rpc/core_rpc_server_commands_defs.h"
+#include "rpc/message_data_structs.h"
namespace cryptonote
{
@@ -243,6 +244,13 @@ namespace cryptonote
void get_transaction_hashes(std::vector<crypto::hash>& txs) const;
/**
+ * @brief get (size, fee, receive time) for all transaction in the pool
+ *
+ * @param txs return-by-reference that data
+ */
+ void get_transaction_backlog(std::vector<tx_backlog_entry>& backlog) const;
+
+ /**
* @brief get a summary statistics of all transaction hashes in the pool
*
* @param stats return-by-reference the pool statistics
@@ -262,6 +270,28 @@ namespace cryptonote
bool get_transactions_and_spent_keys_info(std::vector<tx_info>& tx_infos, std::vector<spent_key_image_info>& key_image_infos) const;
/**
+ * @brief get information about all transactions and key images in the pool
+ *
+ * see documentation on tx_in_pool and key_images_with_tx_hashes for more details
+ *
+ * @param tx_infos [out] the transactions' information
+ * @param key_image_infos [out] the spent key images' information
+ *
+ * @return true
+ */
+ bool get_pool_for_rpc(std::vector<cryptonote::rpc::tx_in_pool>& tx_infos, cryptonote::rpc::key_images_with_tx_hashes& key_image_infos) const;
+
+ /**
+ * @brief check for presence of key images in the pool
+ *
+ * @param key_images [in] vector of key images to check
+ * @param spent [out] vector of bool to return
+ *
+ * @return true
+ */
+ bool check_for_key_images(const std::vector<crypto::key_image>& key_images, std::vector<bool> spent) const;
+
+ /**
* @brief get a specific transaction from the pool
*
* @param h the hash of the transaction to get
@@ -493,7 +523,6 @@ private:
*/
std::unordered_set<crypto::hash> m_timed_out_transactions;
- std::string m_config_folder; //!< the folder to save state to
Blockchain& m_blockchain; //!< reference to the Blockchain object
};
}
diff --git a/src/cryptonote_protocol/block_queue.cpp b/src/cryptonote_protocol/block_queue.cpp
new file mode 100644
index 000000000..02a8e3ec2
--- /dev/null
+++ b/src/cryptonote_protocol/block_queue.cpp
@@ -0,0 +1,417 @@
+// Copyright (c) 2017, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
+
+#include <vector>
+#include <unordered_map>
+#include <boost/uuid/nil_generator.hpp>
+#include "cryptonote_protocol_defs.h"
+#include "block_queue.h"
+
+#undef MONERO_DEFAULT_LOG_CATEGORY
+#define MONERO_DEFAULT_LOG_CATEGORY "cn.block_queue"
+
+namespace std {
+ static_assert(sizeof(size_t) <= sizeof(boost::uuids::uuid), "boost::uuids::uuid too small");
+ template<> struct hash<boost::uuids::uuid> {
+ std::size_t operator()(const boost::uuids::uuid &_v) const {
+ return reinterpret_cast<const std::size_t &>(_v);
+ }
+ };
+}
+
+namespace cryptonote
+{
+
+void block_queue::add_blocks(uint64_t height, std::list<cryptonote::block_complete_entry> bcel, const boost::uuids::uuid &connection_id, float rate, size_t size)
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ std::list<crypto::hash> hashes;
+ bool has_hashes = remove_span(height, &hashes);
+ blocks.insert(span(height, std::move(bcel), connection_id, rate, size));
+ if (has_hashes)
+ set_span_hashes(height, connection_id, hashes);
+}
+
+void block_queue::add_blocks(uint64_t height, uint64_t nblocks, const boost::uuids::uuid &connection_id, boost::posix_time::ptime time)
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ blocks.insert(span(height, nblocks, connection_id, time));
+}
+
+void block_queue::flush_spans(const boost::uuids::uuid &connection_id, bool all)
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ block_map::iterator i = blocks.begin();
+ while (i != blocks.end())
+ {
+ block_map::iterator j = i++;
+ if (j->connection_id == connection_id && (all || j->blocks.size() == 0))
+ {
+ blocks.erase(j);
+ }
+ }
+}
+
+void block_queue::flush_stale_spans(const std::set<boost::uuids::uuid> &live_connections)
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ block_map::iterator i = blocks.begin();
+ if (i != blocks.end() && is_blockchain_placeholder(*i))
+ ++i;
+ while (i != blocks.end())
+ {
+ block_map::iterator j = i++;
+ if (live_connections.find(j->connection_id) == live_connections.end() && j->blocks.size() == 0)
+ {
+ blocks.erase(j);
+ }
+ }
+}
+
+bool block_queue::remove_span(uint64_t start_block_height, std::list<crypto::hash> *hashes)
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ for (block_map::iterator i = blocks.begin(); i != blocks.end(); ++i)
+ {
+ if (i->start_block_height == start_block_height)
+ {
+ if (hashes)
+ *hashes = std::move(i->hashes);
+ blocks.erase(i);
+ return true;
+ }
+ }
+ return false;
+}
+
+void block_queue::remove_spans(const boost::uuids::uuid &connection_id, uint64_t start_block_height)
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ for (block_map::iterator i = blocks.begin(); i != blocks.end(); )
+ {
+ block_map::iterator j = i++;
+ if (j->connection_id == connection_id && j->start_block_height <= start_block_height)
+ {
+ blocks.erase(j);
+ }
+ }
+}
+
+uint64_t block_queue::get_max_block_height() const
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ uint64_t height = 0;
+ for (const auto &span: blocks)
+ {
+ const uint64_t h = span.start_block_height + span.nblocks - 1;
+ if (h > height)
+ height = h;
+ }
+ return height;
+}
+
+void block_queue::print() const
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ MDEBUG("Block queue has " << blocks.size() << " spans");
+ for (const auto &span: blocks)
+ MDEBUG(" " << span.start_block_height << " - " << (span.start_block_height+span.nblocks-1) << " (" << span.nblocks << ") - " << (is_blockchain_placeholder(span) ? "blockchain" : span.blocks.empty() ? "scheduled" : "filled ") << " " << span.connection_id << " (" << ((unsigned)(span.rate*10/1024.f))/10.f << " kB/s)");
+}
+
+std::string block_queue::get_overview() const
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ if (blocks.empty())
+ return "[]";
+ block_map::const_iterator i = blocks.begin();
+ std::string s = std::string("[") + std::to_string(i->start_block_height + i->nblocks - 1) + ":";
+ while (++i != blocks.end())
+ s += i->blocks.empty() ? "." : "o";
+ s += "]";
+ return s;
+}
+
+bool block_queue::requested(const crypto::hash &hash) const
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ for (const auto &span: blocks)
+ {
+ for (const auto &h: span.hashes)
+ if (h == hash)
+ return true;
+ }
+ return false;
+}
+
+std::pair<uint64_t, uint64_t> block_queue::reserve_span(uint64_t first_block_height, uint64_t last_block_height, uint64_t max_blocks, const boost::uuids::uuid &connection_id, const std::list<crypto::hash> &block_hashes, boost::posix_time::ptime time)
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+
+ if (last_block_height < first_block_height || max_blocks == 0)
+ {
+ MDEBUG("reserve_span: early out: first_block_height " << first_block_height << ", last_block_height " << last_block_height << ", max_blocks " << max_blocks);
+ return std::make_pair(0, 0);
+ }
+
+ uint64_t span_start_height = last_block_height - block_hashes.size() + 1;
+ std::list<crypto::hash>::const_iterator i = block_hashes.begin();
+ while (i != block_hashes.end() && requested(*i))
+ {
+ ++i;
+ ++span_start_height;
+ }
+ uint64_t span_length = 0;
+ std::list<crypto::hash> hashes;
+ while (i != block_hashes.end() && span_length < max_blocks)
+ {
+ hashes.push_back(*i);
+ ++i;
+ ++span_length;
+ }
+ if (span_length == 0)
+ return std::make_pair(0, 0);
+ MDEBUG("Reserving span " << span_start_height << " - " << (span_start_height + span_length - 1) << " for " << connection_id);
+ add_blocks(span_start_height, span_length, connection_id, time);
+ set_span_hashes(span_start_height, connection_id, hashes);
+ return std::make_pair(span_start_height, span_length);
+}
+
+bool block_queue::is_blockchain_placeholder(const span &span) const
+{
+ static const boost::uuids::uuid uuid0 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+ return span.connection_id == uuid0;
+}
+
+std::pair<uint64_t, uint64_t> block_queue::get_start_gap_span() const
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ if (blocks.empty())
+ return std::make_pair(0, 0);
+ block_map::const_iterator i = blocks.begin();
+ if (!is_blockchain_placeholder(*i))
+ return std::make_pair(0, 0);
+ uint64_t current_height = i->start_block_height + i->nblocks - 1;
+ ++i;
+ if (i == blocks.end())
+ return std::make_pair(0, 0);
+ uint64_t first_span_height = i->start_block_height;
+ if (first_span_height <= current_height + 1)
+ return std::make_pair(0, 0);
+ MDEBUG("Found gap at start of spans: last blockchain block height " << current_height << ", first span's block height " << first_span_height);
+ print();
+ return std::make_pair(current_height + 1, first_span_height - current_height - 1);
+}
+
+std::pair<uint64_t, uint64_t> block_queue::get_next_span_if_scheduled(std::list<crypto::hash> &hashes, boost::uuids::uuid &connection_id, boost::posix_time::ptime &time) const
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ if (blocks.empty())
+ return std::make_pair(0, 0);
+ block_map::const_iterator i = blocks.begin();
+ if (is_blockchain_placeholder(*i))
+ ++i;
+ if (i == blocks.end())
+ return std::make_pair(0, 0);
+ if (!i->blocks.empty())
+ return std::make_pair(0, 0);
+ hashes = i->hashes;
+ connection_id = i->connection_id;
+ time = i->time;
+ return std::make_pair(i->start_block_height, i->nblocks);
+}
+
+void block_queue::set_span_hashes(uint64_t start_height, const boost::uuids::uuid &connection_id, std::list<crypto::hash> hashes)
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ for (block_map::iterator i = blocks.begin(); i != blocks.end(); ++i)
+ {
+ if (i->start_block_height == start_height && i->connection_id == connection_id)
+ {
+ span s = *i;
+ blocks.erase(i);
+ s.hashes = std::move(hashes);
+ blocks.insert(s);
+ return;
+ }
+ }
+}
+
+bool block_queue::get_next_span(uint64_t &height, std::list<cryptonote::block_complete_entry> &bcel, boost::uuids::uuid &connection_id, bool filled) const
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ if (blocks.empty())
+ return false;
+ block_map::const_iterator i = blocks.begin();
+ if (is_blockchain_placeholder(*i))
+ ++i;
+ for (; i != blocks.end(); ++i)
+ {
+ if (!filled || !i->blocks.empty())
+ {
+ height = i->start_block_height;
+ bcel = i->blocks;
+ connection_id = i->connection_id;
+ return true;
+ }
+ }
+ return false;
+}
+
+bool block_queue::has_next_span(const boost::uuids::uuid &connection_id, bool &filled) const
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ if (blocks.empty())
+ return false;
+ block_map::const_iterator i = blocks.begin();
+ if (is_blockchain_placeholder(*i))
+ ++i;
+ if (i == blocks.end())
+ return false;
+ if (i->connection_id != connection_id)
+ return false;
+ filled = !i->blocks.empty();
+ return true;
+}
+
+size_t block_queue::get_data_size() const
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ size_t size = 0;
+ for (const auto &span: blocks)
+ size += span.size;
+ return size;
+}
+
+size_t block_queue::get_num_filled_spans_prefix() const
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+
+ if (blocks.empty())
+ return 0;
+ block_map::const_iterator i = blocks.begin();
+ if (is_blockchain_placeholder(*i))
+ ++i;
+ size_t size = 0;
+ while (i != blocks.end() && !i->blocks.empty())
+ {
+ ++i;
+ ++size;
+ }
+ return size;
+}
+
+size_t block_queue::get_num_filled_spans() const
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ size_t size = 0;
+ for (const auto &span: blocks)
+ if (!span.blocks.empty())
+ ++size;
+ return size;
+}
+
+crypto::hash block_queue::get_last_known_hash(const boost::uuids::uuid &connection_id) const
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ crypto::hash hash = cryptonote::null_hash;
+ uint64_t highest_height = 0;
+ for (const auto &span: blocks)
+ {
+ if (span.connection_id != connection_id)
+ continue;
+ uint64_t h = span.start_block_height + span.nblocks - 1;
+ if (h > highest_height && span.hashes.size() == span.nblocks)
+ {
+ hash = span.hashes.back();
+ highest_height = h;
+ }
+ }
+ return hash;
+}
+
+bool block_queue::has_spans(const boost::uuids::uuid &connection_id) const
+{
+ for (const auto &span: blocks)
+ {
+ if (span.connection_id == connection_id)
+ return true;
+ }
+ return false;
+}
+
+float block_queue::get_speed(const boost::uuids::uuid &connection_id) const
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ std::unordered_map<boost::uuids::uuid, float> speeds;
+ for (const auto &span: blocks)
+ {
+ if (span.blocks.empty())
+ continue;
+ // note that the average below does not average over the whole set, but over the
+ // previous pseudo average and the latest rate: this gives much more importance
+ // to the latest measurements, which is fine here
+ std::unordered_map<boost::uuids::uuid, float>::iterator i = speeds.find(span.connection_id);
+ if (i == speeds.end())
+ speeds.insert(std::make_pair(span.connection_id, span.rate));
+ else
+ i->second = (i->second + span.rate) / 2;
+ }
+ float conn_rate = -1, best_rate = 0;
+ for (auto i: speeds)
+ {
+ if (i.first == connection_id)
+ conn_rate = i.second;
+ if (i.second > best_rate)
+ best_rate = i.second;
+ }
+
+ if (conn_rate <= 0)
+ return 1.0f; // not found, assume good speed
+ if (best_rate == 0)
+ return 1.0f; // everything dead ? Can't happen, but let's trap anyway
+
+ const float speed = conn_rate / best_rate;
+ MTRACE(" Relative speed for " << connection_id << ": " << speed << " (" << conn_rate << "/" << best_rate);
+ return speed;
+}
+
+bool block_queue::foreach(std::function<bool(const span&)> f, bool include_blockchain_placeholder) const
+{
+ boost::unique_lock<boost::recursive_mutex> lock(mutex);
+ block_map::const_iterator i = blocks.begin();
+ if (!include_blockchain_placeholder && i != blocks.end() && is_blockchain_placeholder(*i))
+ ++i;
+ while (i != blocks.end())
+ if (!f(*i++))
+ return false;
+ return true;
+}
+
+}
diff --git a/src/cryptonote_protocol/block_queue.h b/src/cryptonote_protocol/block_queue.h
new file mode 100644
index 000000000..13d4619bf
--- /dev/null
+++ b/src/cryptonote_protocol/block_queue.h
@@ -0,0 +1,99 @@
+// Copyright (c) 2017, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
+
+#pragma once
+
+#include <string>
+#include <list>
+#include <set>
+#include <boost/thread/recursive_mutex.hpp>
+#include <boost/uuid/uuid.hpp>
+
+#undef MONERO_DEFAULT_LOG_CATEGORY
+#define MONERO_DEFAULT_LOG_CATEGORY "cn.block_queue"
+
+namespace cryptonote
+{
+ struct block_complete_entry;
+
+ class block_queue
+ {
+ public:
+ struct span
+ {
+ uint64_t start_block_height;
+ std::list<crypto::hash> hashes;
+ std::list<cryptonote::block_complete_entry> blocks;
+ boost::uuids::uuid connection_id;
+ uint64_t nblocks;
+ float rate;
+ size_t size;
+ boost::posix_time::ptime time;
+
+ span(uint64_t start_block_height, std::list<cryptonote::block_complete_entry> blocks, const boost::uuids::uuid &connection_id, float rate, size_t size):
+ start_block_height(start_block_height), blocks(std::move(blocks)), connection_id(connection_id), nblocks(this->blocks.size()), rate(rate), size(size), time() {}
+ span(uint64_t start_block_height, uint64_t nblocks, const boost::uuids::uuid &connection_id, boost::posix_time::ptime time):
+ start_block_height(start_block_height), connection_id(connection_id), nblocks(nblocks), rate(0.0f), size(0), time(time) {}
+
+ bool operator<(const span &s) const { return start_block_height < s.start_block_height; }
+ };
+ typedef std::set<span> block_map;
+
+ public:
+ void add_blocks(uint64_t height, std::list<cryptonote::block_complete_entry> bcel, const boost::uuids::uuid &connection_id, float rate, size_t size);
+ void add_blocks(uint64_t height, uint64_t nblocks, const boost::uuids::uuid &connection_id, boost::posix_time::ptime time = boost::date_time::min_date_time);
+ void flush_spans(const boost::uuids::uuid &connection_id, bool all = false);
+ void flush_stale_spans(const std::set<boost::uuids::uuid> &live_connections);
+ bool remove_span(uint64_t start_block_height, std::list<crypto::hash> *hashes = NULL);
+ void remove_spans(const boost::uuids::uuid &connection_id, uint64_t start_block_height);
+ uint64_t get_max_block_height() const;
+ void print() const;
+ std::string get_overview() const;
+ std::pair<uint64_t, uint64_t> reserve_span(uint64_t first_block_height, uint64_t last_block_height, uint64_t max_blocks, const boost::uuids::uuid &connection_id, const std::list<crypto::hash> &block_hashes, boost::posix_time::ptime time = boost::posix_time::microsec_clock::universal_time());
+ bool is_blockchain_placeholder(const span &span) const;
+ std::pair<uint64_t, uint64_t> get_start_gap_span() const;
+ std::pair<uint64_t, uint64_t> get_next_span_if_scheduled(std::list<crypto::hash> &hashes, boost::uuids::uuid &connection_id, boost::posix_time::ptime &time) const;
+ void set_span_hashes(uint64_t start_height, const boost::uuids::uuid &connection_id, std::list<crypto::hash> hashes);
+ bool get_next_span(uint64_t &height, std::list<cryptonote::block_complete_entry> &bcel, boost::uuids::uuid &connection_id, bool filled = true) const;
+ bool has_next_span(const boost::uuids::uuid &connection_id, bool &filled) const;
+ size_t get_data_size() const;
+ size_t get_num_filled_spans_prefix() const;
+ size_t get_num_filled_spans() const;
+ crypto::hash get_last_known_hash(const boost::uuids::uuid &connection_id) const;
+ bool has_spans(const boost::uuids::uuid &connection_id) const;
+ float get_speed(const boost::uuids::uuid &connection_id) const;
+ bool foreach(std::function<bool(const span&)> f, bool include_blockchain_placeholder = false) const;
+ bool requested(const crypto::hash &hash) const;
+
+ private:
+ block_map blocks;
+ mutable boost::recursive_mutex mutex;
+ };
+}
diff --git a/src/cryptonote_protocol/cryptonote_protocol_defs.h b/src/cryptonote_protocol/cryptonote_protocol_defs.h
index 6f6c1a803..71e205c23 100644
--- a/src/cryptonote_protocol/cryptonote_protocol_defs.h
+++ b/src/cryptonote_protocol/cryptonote_protocol_defs.h
@@ -74,6 +74,10 @@ namespace cryptonote
uint32_t support_flags;
+ boost::uuids::uuid connection_id;
+
+ uint64_t height;
+
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(incoming)
KV_SERIALIZE(localhost)
@@ -94,6 +98,8 @@ namespace cryptonote
KV_SERIALIZE(avg_upload)
KV_SERIALIZE(current_upload)
KV_SERIALIZE(support_flags)
+ KV_SERIALIZE_VAL_POD_AS_BLOB(connection_id)
+ KV_SERIALIZE(height)
END_KV_SERIALIZE_MAP()
};
@@ -191,11 +197,15 @@ namespace cryptonote
struct CORE_SYNC_DATA
{
uint64_t current_height;
+ uint64_t cumulative_difficulty;
crypto::hash top_id;
+ uint8_t top_version;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(current_height)
+ KV_SERIALIZE(cumulative_difficulty)
KV_SERIALIZE_VAL_POD_AS_BLOB(top_id)
+ KV_SERIALIZE_OPT(top_version, (uint8_t)0)
END_KV_SERIALIZE_MAP()
};
@@ -221,11 +231,13 @@ namespace cryptonote
{
uint64_t start_height;
uint64_t total_height;
+ uint64_t cumulative_difficulty;
std::list<crypto::hash> m_block_ids;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(start_height)
KV_SERIALIZE(total_height)
+ KV_SERIALIZE(cumulative_difficulty)
KV_SERIALIZE_CONTAINER_POD_AS_BLOB(m_block_ids)
END_KV_SERIALIZE_MAP()
};
diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler-base.cpp b/src/cryptonote_protocol/cryptonote_protocol_handler-base.cpp
index e31276031..3bda50c22 100644
--- a/src/cryptonote_protocol/cryptonote_protocol_handler-base.cpp
+++ b/src/cryptonote_protocol/cryptonote_protocol_handler-base.cpp
@@ -49,8 +49,8 @@
#include "syncobj.h"
-#include "../../contrib/epee/include/net/net_utils_base.h"
-#include "../../contrib/epee/include/misc_log_ex.h"
+#include "net/net_utils_base.h"
+#include "misc_log_ex.h"
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/uuid/random_generator.hpp>
@@ -69,10 +69,10 @@
#include <boost/asio/basic_socket.hpp>
#include <boost/asio/ip/unicast.hpp>
-#include "../../src/cryptonote_protocol/cryptonote_protocol_handler.h"
-#include "../../src/p2p/network_throttle.hpp"
+#include "cryptonote_protocol_handler.h"
+#include "p2p/network_throttle.hpp"
-#include "../../../src/cryptonote_core/cryptonote_core.h" // e.g. for the send_stop_signal()
+#include "cryptonote_core/cryptonote_core.h" // e.g. for the send_stop_signal()
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "net.cn"
@@ -120,10 +120,6 @@ cryptonote_protocol_handler_base::~cryptonote_protocol_handler_base() {
}
void cryptonote_protocol_handler_base::handler_request_blocks_history(std::list<crypto::hash>& ids) {
- using namespace epee::net_utils;
- MDEBUG("### ~~~RRRR~~~~ ### sending request (type 2), limit = " << ids.size());
- MDEBUG("RATE LIMIT NOT IMPLEMENTED HERE YET (download at unlimited speed?)");
- // TODO
}
void cryptonote_protocol_handler_base::handler_response_blocks_now(size_t packet_size) {
diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.h b/src/cryptonote_protocol/cryptonote_protocol_handler.h
index 9d8bc43c2..d54687e6a 100644
--- a/src/cryptonote_protocol/cryptonote_protocol_handler.h
+++ b/src/cryptonote_protocol/cryptonote_protocol_handler.h
@@ -38,10 +38,12 @@
#include <string>
#include <ctime>
+#include "math_helper.h"
#include "storages/levin_abstract_invoke2.h"
#include "warnings.h"
#include "cryptonote_protocol_defs.h"
#include "cryptonote_protocol_handler_common.h"
+#include "block_queue.h"
#include "cryptonote_basic/connection_context.h"
#include "cryptonote_basic/cryptonote_stat_info.h"
#include "cryptonote_basic/verification_context.h"
@@ -107,7 +109,9 @@ namespace cryptonote
bool is_synchronized(){return m_synchronized;}
void log_connections();
std::list<connection_info> get_connections();
+ const block_queue &get_block_queue() const { return m_block_queue; }
void stop();
+ void on_connection_close(cryptonote_connection_context &context);
private:
//----------------- commands handlers ----------------------------------------------
int handle_notify_new_block(int command, NOTIFY_NEW_BLOCK::request& arg, cryptonote_connection_context& context);
@@ -124,18 +128,24 @@ namespace cryptonote
virtual bool relay_transactions(NOTIFY_NEW_TRANSACTIONS::request& arg, cryptonote_connection_context& exclude_context);
//----------------------------------------------------------------------------------
//bool get_payload_sync_data(HANDSHAKE_DATA::request& hshd, cryptonote_connection_context& context);
- bool request_missing_objects(cryptonote_connection_context& context, bool check_having_blocks);
+ bool request_missing_objects(cryptonote_connection_context& context, bool check_having_blocks, bool force_next_span = false);
size_t get_synchronizing_connections_count();
bool on_connection_synchronized();
+ bool should_download_next_span(cryptonote_connection_context& context) const;
+ void drop_connection(cryptonote_connection_context &context, bool add_fail, bool flush_all_spans);
+ bool kick_idle_peers();
+ int try_add_next_blocks(cryptonote_connection_context &context);
+
t_core& m_core;
nodetool::p2p_endpoint_stub<connection_context> m_p2p_stub;
nodetool::i_p2p_endpoint<connection_context>* m_p2p;
std::atomic<uint32_t> m_syncronized_connections_count;
std::atomic<bool> m_synchronized;
- bool m_one_request = true;
std::atomic<bool> m_stopping;
- epee::critical_section m_sync_lock;
+ boost::mutex m_sync_lock;
+ block_queue m_block_queue;
+ epee::math_helper::once_a_time_seconds<30> m_idle_peer_kicker;
boost::mutex m_buffer_mutex;
double get_avg_block_size();
diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.inl b/src/cryptonote_protocol/cryptonote_protocol_handler.inl
index c5bc834ad..803d948cc 100644
--- a/src/cryptonote_protocol/cryptonote_protocol_handler.inl
+++ b/src/cryptonote_protocol/cryptonote_protocol_handler.inl
@@ -41,13 +41,18 @@
#include "cryptonote_basic/cryptonote_format_utils.h"
#include "profile_tools.h"
-#include "../../src/p2p/network_throttle-detail.hpp"
+#include "p2p/network_throttle-detail.hpp"
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "net.cn"
#define MLOG_P2P_MESSAGE(x) MCINFO("net.p2p.msg", context << x)
+#define BLOCK_QUEUE_NBLOCKS_THRESHOLD 10 // chunks of N blocks
+#define BLOCK_QUEUE_SIZE_THRESHOLD (100*1024*1024) // MB
+#define REQUEST_NEXT_SCHEDULED_SPAN_THRESHOLD (5 * 1000000) // microseconds
+#define IDLE_PEER_KICK_TIME (45 * 1000000) // microseconds
+
namespace cryptonote
{
@@ -101,6 +106,11 @@ namespace cryptonote
LOG_PRINT_CCONTEXT_L2("-->>NOTIFY_REQUEST_CHAIN: m_block_ids.size()=" << r.block_ids.size() );
post_notify<NOTIFY_REQUEST_CHAIN>(r, context);
}
+ else if(context.m_state == cryptonote_connection_context::state_standby)
+ {
+ context.m_state = cryptonote_connection_context::state_synchronizing;
+ try_add_next_blocks(context);
+ }
return true;
}
@@ -199,16 +209,16 @@ namespace cryptonote
}
std::stringstream peer_id_str;
- peer_id_str << std::hex << peer_id;
+ peer_id_str << std::hex << std::setw(16) << peer_id;
peer_id_str >> cnx.peer_id;
cnx.support_flags = support_flags;
cnx.recv_count = cntxt.m_recv_cnt;
- cnx.recv_idle_time = timestamp - cntxt.m_last_recv;
+ cnx.recv_idle_time = timestamp - std::max(cntxt.m_started, cntxt.m_last_recv);
cnx.send_count = cntxt.m_send_cnt;
- cnx.send_idle_time = timestamp;
+ cnx.send_idle_time = timestamp - std::max(cntxt.m_started, cntxt.m_last_send);
cnx.state = get_protocol_state_string(cntxt.m_state);
@@ -233,6 +243,10 @@ namespace cryptonote
cnx.current_download = cntxt.m_current_speed_down / 1024;
cnx.current_upload = cntxt.m_current_speed_up / 1024;
+ cnx.connection_id = cntxt.m_connection_id;
+
+ cnx.height = cntxt.m_remote_blockchain_height;
+
connections.push_back(cnx);
return true;
@@ -244,12 +258,24 @@ namespace cryptonote
template<class t_core>
bool t_cryptonote_protocol_handler<t_core>::process_payload_sync_data(const CORE_SYNC_DATA& hshd, cryptonote_connection_context& context, bool is_inital)
{
- if(context.m_state == cryptonote_connection_context::state_befor_handshake && !is_inital)
+ if(context.m_state == cryptonote_connection_context::state_before_handshake && !is_inital)
return true;
if(context.m_state == cryptonote_connection_context::state_synchronizing)
return true;
+ // from v6, if the peer advertises a top block version, reject if it's not what it should be (will only work if no voting)
+ const uint8_t version = m_core.get_ideal_hard_fork_version(hshd.current_height - 1);
+ if (version >= 6 && version != hshd.top_version)
+ {
+ if (version < hshd.top_version)
+ MCLOG_RED(el::Level::Warning, "global", context << " peer claims higher version that we think - we may be forked from the network and a software upgrade may be needed");
+ LOG_DEBUG_CC(context, "Ignoring due to wrong top version for block " << (hshd.current_height - 1) << ": " << (unsigned)hshd.top_version << ", expected " << (unsigned)version);
+ return false;
+ }
+
+ context.m_remote_blockchain_height = hshd.current_height;
+
uint64_t target = m_core.get_target_blockchain_height();
if (target == 0)
target = m_core.get_current_blockchain_height();
@@ -267,19 +293,20 @@ namespace cryptonote
/* As I don't know if accessing hshd from core could be a good practice,
I prefer pushing target height to the core at the same time it is pushed to the user.
Nz. */
- m_core.set_target_blockchain_height(static_cast<int64_t>(hshd.current_height));
+ m_core.set_target_blockchain_height((hshd.current_height));
int64_t diff = static_cast<int64_t>(hshd.current_height) - static_cast<int64_t>(m_core.get_current_blockchain_height());
- int64_t max_block_height = max(static_cast<int64_t>(hshd.current_height),static_cast<int64_t>(m_core.get_current_blockchain_height()));
- int64_t last_block_v1 = 1009826;
- int64_t diff_v2 = max_block_height > last_block_v1 ? min(abs(diff), max_block_height - last_block_v1) : 0;
+ uint64_t abs_diff = std::abs(diff);
+ uint64_t max_block_height = max(hshd.current_height,m_core.get_current_blockchain_height());
+ uint64_t last_block_v1 = m_core.get_testnet() ? 624633 : 1009826;
+ uint64_t diff_v2 = max_block_height > last_block_v1 ? min(abs_diff, max_block_height - last_block_v1) : 0;
MCLOG(is_inital ? el::Level::Info : el::Level::Debug, "global", context << "Sync data returned a new top block candidate: " << m_core.get_current_blockchain_height() << " -> " << hshd.current_height
- << " [Your node is " << std::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 (" << ((abs_diff - diff_v2) / (24 * 60 * 60 / DIFFICULTY_TARGET_V1)) + (diff_v2 / (24 * 60 * 60 / DIFFICULTY_TARGET_V2)) << " days) "
<< (0 <= diff ? std::string("behind") : std::string("ahead"))
<< "] " << ENDL << "SYNCHRONIZATION started");
+ m_core.safesyncmode(false);
}
LOG_PRINT_L1("Remote blockchain height: " << hshd.current_height << ", id: " << hshd.top_id);
context.m_state = cryptonote_connection_context::state_synchronizing;
- context.m_remote_blockchain_height = hshd.current_height;
//let the socket to send response to handshake, but request callback, to let send request data after response
LOG_PRINT_CCONTEXT_L2("requesting callback");
++context.m_callback_request_count;
@@ -291,6 +318,8 @@ namespace cryptonote
bool t_cryptonote_protocol_handler<t_core>::get_payload_sync_data(CORE_SYNC_DATA& hshd)
{
m_core.get_blockchain_top(hshd.current_height, hshd.top_id);
+ hshd.top_version = m_core.get_ideal_hard_fork_version(hshd.current_height);
+ hshd.cumulative_difficulty = m_core.get_block_cumulative_difficulty(hshd.current_height);
hshd.current_height +=1;
return true;
}
@@ -310,6 +339,11 @@ namespace cryptonote
MLOG_P2P_MESSAGE("Received NOTIFY_NEW_BLOCK (hop " << arg.hop << ", " << arg.b.txs.size() << " txes)");
if(context.m_state != cryptonote_connection_context::state_normal)
return 1;
+ if(!is_synchronized()) // can happen if a peer connection goes to normal but another thread still hasn't finished adding queued blocks
+ {
+ LOG_DEBUG_CC(context, "Received new block while syncing, ignored");
+ return 1;
+ }
m_core.pause_mine();
std::list<block_complete_entry> blocks;
blocks.push_back(arg.b);
@@ -321,7 +355,7 @@ namespace cryptonote
if(tvc.m_verifivation_failed)
{
LOG_PRINT_CCONTEXT_L1("Block verification failed: transaction verification failed, dropping connection");
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
m_core.cleanup_handle_incoming_blocks();
m_core.resume_mine();
return 1;
@@ -330,12 +364,17 @@ namespace cryptonote
block_verification_context bvc = boost::value_initialized<block_verification_context>();
m_core.handle_incoming_block(arg.b.block, bvc); // got block from handle_notify_new_block
- m_core.cleanup_handle_incoming_blocks(true);
+ if (!m_core.cleanup_handle_incoming_blocks(true))
+ {
+ LOG_PRINT_CCONTEXT_L0("Failure in cleanup_handle_incoming_blocks");
+ m_core.resume_mine();
+ return 1;
+ }
m_core.resume_mine();
if(bvc.m_verifivation_failed)
{
LOG_PRINT_CCONTEXT_L0("Block verification failed, dropping connection");
- m_p2p->drop_connection(context);
+ drop_connection(context, true, false);
return 1;
}
if(bvc.m_added_to_main_chain)
@@ -361,6 +400,11 @@ namespace cryptonote
MLOG_P2P_MESSAGE("Received NOTIFY_NEW_FLUFFY_BLOCK (height " << arg.current_blockchain_height << ", hop " << arg.hop << ", " << arg.b.txs.size() << " txes)");
if(context.m_state != cryptonote_connection_context::state_normal)
return 1;
+ if(!is_synchronized()) // can happen if a peer connection goes to normal but another thread still hasn't finished adding queued blocks
+ {
+ LOG_DEBUG_CC(context, "Received new block while syncing, ignored");
+ return 1;
+ }
m_core.pause_mine();
@@ -383,7 +427,7 @@ namespace cryptonote
<< ", dropping connection"
);
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
m_core.resume_mine();
return 1;
}
@@ -416,7 +460,7 @@ namespace cryptonote
<< ", dropping connection"
);
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
m_core.resume_mine();
return 1;
}
@@ -430,7 +474,7 @@ namespace cryptonote
<< ", dropping connection"
);
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
m_core.resume_mine();
return 1;
}
@@ -454,7 +498,7 @@ namespace cryptonote
<< "dropping connection"
);
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
m_core.resume_mine();
return 1;
}
@@ -471,7 +515,7 @@ namespace cryptonote
if(!m_core.handle_incoming_tx(tx_blob, tvc, true, true, false) || tvc.m_verifivation_failed)
{
LOG_PRINT_CCONTEXT_L1("Block verification failed: transaction verification failed, dropping connection");
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
m_core.resume_mine();
return 1;
}
@@ -493,7 +537,7 @@ namespace cryptonote
<< ", dropping connection"
);
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
m_core.resume_mine();
return 1;
}
@@ -513,7 +557,7 @@ namespace cryptonote
<< ", dropping connection"
);
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
m_core.resume_mine();
return 1;
}
@@ -584,13 +628,18 @@ namespace cryptonote
block_verification_context bvc = boost::value_initialized<block_verification_context>();
m_core.handle_incoming_block(arg.b.block, bvc); // got block from handle_notify_new_block
- m_core.cleanup_handle_incoming_blocks(true);
+ if (!m_core.cleanup_handle_incoming_blocks(true))
+ {
+ LOG_PRINT_CCONTEXT_L0("Failure in cleanup_handle_incoming_blocks");
+ m_core.resume_mine();
+ return 1;
+ }
m_core.resume_mine();
if( bvc.m_verifivation_failed )
{
LOG_PRINT_CCONTEXT_L0("Block verification failed, dropping connection");
- m_p2p->drop_connection(context);
+ drop_connection(context, true, false);
return 1;
}
if( bvc.m_added_to_main_chain )
@@ -623,7 +672,7 @@ namespace cryptonote
);
m_core.resume_mine();
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
return 1;
}
@@ -643,7 +692,7 @@ namespace cryptonote
if (!m_core.get_block_by_hash(arg.block_hash, b))
{
LOG_ERROR_CCONTEXT("failed to find block: " << arg.block_hash << ", dropping connection");
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
return 1;
}
@@ -670,7 +719,7 @@ namespace cryptonote
<< ", dropping connection"
);
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
return 1;
}
}
@@ -681,14 +730,14 @@ namespace cryptonote
{
LOG_ERROR_CCONTEXT("Failed to handle request NOTIFY_REQUEST_FLUFFY_MISSING_TX, "
<< "failed to get requested transactions");
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
return 1;
}
if (!missed.empty() || txs.size() != txids.size())
{
LOG_ERROR_CCONTEXT("Failed to handle request NOTIFY_REQUEST_FLUFFY_MISSING_TX, "
<< missed.size() << " requested transactions not found" << ", dropping connection");
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
return 1;
}
@@ -731,7 +780,7 @@ namespace cryptonote
if(tvc.m_verifivation_failed)
{
LOG_PRINT_CCONTEXT_L1("Tx verification failed, dropping connection");
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
return 1;
}
if(tvc.m_should_be_relayed)
@@ -757,7 +806,8 @@ namespace cryptonote
if(!m_core.handle_get_objects(arg, rsp, context))
{
LOG_ERROR_CCONTEXT("failed to handle request NOTIFY_REQUEST_GET_OBJECTS, dropping connection");
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
+ return 1;
}
LOG_PRINT_CCONTEXT_L2("-->>NOTIFY_RESPONSE_GET_OBJECTS: blocks.size()=" << rsp.blocks.size() << ", txs.size()=" << rsp.txs.size()
<< ", rsp.m_current_blockchain_height=" << rsp.current_blockchain_height << ", missed_ids.size()=" << rsp.missed_ids.size());
@@ -770,21 +820,15 @@ namespace cryptonote
template<class t_core>
- double t_cryptonote_protocol_handler<t_core>::get_avg_block_size() {
- // return m_core.get_blockchain_storage().get_avg_block_size(count); // this does not count too well the actuall network-size of data we need to download
-
+ double t_cryptonote_protocol_handler<t_core>::get_avg_block_size()
+ {
CRITICAL_REGION_LOCAL(m_buffer_mutex);
- double avg = 0;
- if (m_avg_buffer.size() == 0) {
- _warn("m_avg_buffer.size() == 0");
+ if (m_avg_buffer.empty()) {
+ MWARNING("m_avg_buffer.size() == 0");
return 500;
}
-
- const bool dbg_poke_lock = 0; // debug: try to trigger an error by poking around with locks. TODO: configure option
- long int dbg_repeat=0;
- do {
- for (auto element : m_avg_buffer) avg += element;
- } while(dbg_poke_lock && (dbg_repeat++)<100000); // in debug/poke mode, repeat this calculation to trigger hidden locking error if there is one
+ double avg = 0;
+ for (const auto &element : m_avg_buffer) avg += element;
return avg / m_avg_buffer.size();
}
@@ -794,31 +838,28 @@ namespace cryptonote
{
MLOG_P2P_MESSAGE("Received NOTIFY_RESPONSE_GET_OBJECTS (" << arg.blocks.size() << " blocks, " << arg.txs.size() << " txes)");
- // calculate size of request - mainly for logging/debug
+ // calculate size of request
size_t size = 0;
- for (auto element : arg.txs) size += element.size();
+ for (const auto &element : arg.txs) size += element.size();
- for (auto element : arg.blocks) {
- size += element.block.size();
- for (auto tx : element.txs)
- size += tx.size();
+ size_t blocks_size = 0;
+ for (const auto &element : arg.blocks) {
+ blocks_size += element.block.size();
+ for (const auto &tx : element.txs)
+ blocks_size += tx.size();
}
+ size += blocks_size;
- for (auto element : arg.missed_ids)
+ for (const auto &element : arg.missed_ids)
size += sizeof(element.data);
size += sizeof(arg.current_blockchain_height);
{
CRITICAL_REGION_LOCAL(m_buffer_mutex);
m_avg_buffer.push_back(size);
-
- const bool dbg_poke_lock = 0; // debug: try to trigger an error by poking around with locks. TODO: configure option
- long int dbg_repeat=0;
- do {
- m_avg_buffer.push_back(666); // a test value
- m_avg_buffer.erase_end(1);
- } while(dbg_poke_lock && (dbg_repeat++)<100000); // in debug/poke mode, repeat this calculation to trigger hidden locking error if there is one
}
+ MDEBUG(context << " downloaded " << size << " bytes worth of blocks");
+
/*using namespace boost::chrono;
auto point = steady_clock::now();
auto time_from_epoh = point.time_since_epoch();
@@ -830,15 +871,17 @@ namespace cryptonote
{
LOG_ERROR_CCONTEXT("sent wrong NOTIFY_HAVE_OBJECTS: arg.m_current_blockchain_height=" << arg.current_blockchain_height
<< " < m_last_response_height=" << context.m_last_response_height << ", dropping connection");
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
return 1;
}
context.m_remote_blockchain_height = arg.current_blockchain_height;
- size_t count = 0;
std::vector<crypto::hash> block_hashes;
block_hashes.reserve(arg.blocks.size());
+ const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
+ uint64_t start_height = std::numeric_limits<uint64_t>::max();
+ cryptonote::block b;
for(const block_complete_entry& block_entry: arg.blocks)
{
if (m_stopping)
@@ -846,42 +889,37 @@ namespace cryptonote
return 1;
}
- ++count;
- block b;
if(!parse_and_validate_block_from_blob(block_entry.block, b))
{
LOG_ERROR_CCONTEXT("sent wrong block: failed to parse and validate block: "
<< epee::string_tools::buff_to_hex_nodelimer(block_entry.block) << ", dropping connection");
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
return 1;
}
- //to avoid concurrency in core between connections, suspend connections which delivered block later then first one
- const crypto::hash block_hash = get_block_hash(b);
- if(count == 2)
+ if (b.miner_tx.vin.size() != 1 || b.miner_tx.vin.front().type() != typeid(txin_gen))
{
- if(m_core.have_block(block_hash))
- {
- context.m_state = cryptonote_connection_context::state_idle;
- context.m_needed_objects.clear();
- context.m_requested_objects.clear();
- LOG_PRINT_CCONTEXT_L1("Connection set to idle state.");
- return 1;
- }
+ LOG_ERROR_CCONTEXT("sent wrong block: block: miner tx does not have exactly one txin_gen input"
+ << epee::string_tools::buff_to_hex_nodelimer(block_entry.block) << ", dropping connection");
+ drop_connection(context, false, false);
+ return 1;
}
+ if (start_height == std::numeric_limits<uint64_t>::max())
+ start_height = boost::get<txin_gen>(b.miner_tx.vin[0]).height;
+ const crypto::hash block_hash = get_block_hash(b);
auto req_it = context.m_requested_objects.find(block_hash);
if(req_it == context.m_requested_objects.end())
{
LOG_ERROR_CCONTEXT("sent wrong NOTIFY_RESPONSE_GET_OBJECTS: block with id=" << epee::string_tools::pod_to_hex(get_blob_hash(block_entry.block))
<< " wasn't requested, dropping connection");
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
return 1;
}
if(b.tx_hashes.size() != block_entry.txs.size())
{
LOG_ERROR_CCONTEXT("sent wrong NOTIFY_RESPONSE_GET_OBJECTS: block with id=" << epee::string_tools::pod_to_hex(get_blob_hash(block_entry.block))
<< ", tx_hashes.size()=" << b.tx_hashes.size() << " mismatch with block_complete_entry.m_txs.size()=" << block_entry.txs.size() << ", dropping connection");
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
return 1;
}
@@ -893,125 +931,279 @@ namespace cryptonote
{
MERROR("returned not all requested objects (context.m_requested_objects.size()="
<< context.m_requested_objects.size() << "), dropping connection");
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
return 1;
}
+ // get the last parsed block, which should be the highest one
+ if(m_core.have_block(cryptonote::get_block_hash(b)))
+ {
+ const uint64_t subchain_height = start_height + arg.blocks.size();
+ LOG_DEBUG_CC(context, "These are old blocks, ignoring: blocks " << start_height << " - " << (subchain_height-1) << ", blockchain height " << m_core.get_current_blockchain_height());
+ m_block_queue.remove_spans(context.m_connection_id, start_height);
+ goto skip;
+ }
{
- MLOG_YELLOW(el::Level::Debug, "Got NEW BLOCKS inside of " << __FUNCTION__ << ": size: " << arg.blocks.size());
+ MLOG_YELLOW(el::Level::Debug, context << " Got NEW BLOCKS inside of " << __FUNCTION__ << ": size: " << arg.blocks.size()
+ << ", blocks: " << start_height << " - " << (start_height + arg.blocks.size() - 1));
- if (m_core.get_test_drop_download() && m_core.get_test_drop_download_height()) { // DISCARD BLOCKS for testing
+ // add that new span to the block queue
+ const boost::posix_time::time_duration dt = now - context.m_last_request_time;
+ const float rate = size * 1e6 / (dt.total_microseconds() + 1);
+ MDEBUG(context << " adding span: " << arg.blocks.size() << " at height " << start_height << ", " << dt.total_microseconds()/1e6 << " seconds, " << (rate/1e3) << " kB/s, size now " << (m_block_queue.get_data_size() + blocks_size) / 1048576.f << " MB");
+ m_block_queue.add_blocks(start_height, arg.blocks, context.m_connection_id, rate, blocks_size);
- // we lock all the rest to avoid having multiple connections redo a lot
- // of the same work, and one of them doing it for nothing: subsequent
- // connections will wait until the current one's added its blocks, then
- // will add any extra it has, if any
- CRITICAL_REGION_LOCAL(m_sync_lock);
+ context.m_last_known_hash = cryptonote::get_blob_hash(arg.blocks.back().block);
+ if (!m_core.get_test_drop_download() || !m_core.get_test_drop_download_height()) { // DISCARD BLOCKS for testing
+ return 1;
+ }
+ }
+
+skip:
+ try_add_next_blocks(context);
+ return 1;
+ }
+
+ template<class t_core>
+ int t_cryptonote_protocol_handler<t_core>::try_add_next_blocks(cryptonote_connection_context& context)
+ {
+ bool force_next_span = false;
+
+ {
+ // We try to lock the sync lock. If we can, it means no other thread is
+ // currently adding blocks, so we do that for as long as we can from the
+ // block queue. Then, we go back to download.
+ const boost::unique_lock<boost::mutex> sync{m_sync_lock, boost::try_to_lock};
+ if (!sync.owns_lock())
+ {
+ MINFO("Failed to lock m_sync_lock, going back to download");
+ goto skip;
+ }
+ MDEBUG(context << " lock m_sync_lock, adding blocks to chain...");
+
+ {
m_core.pause_mine();
epee::misc_utils::auto_scope_leave_caller scope_exit_handler = epee::misc_utils::create_scope_leave_handler(
boost::bind(&t_core::resume_mine, &m_core));
- const uint64_t previous_height = m_core.get_current_blockchain_height();
-
- // dismiss what another connection might already have done (likely everything)
- uint64_t top_height;
- crypto::hash top_hash;
- if (m_core.get_blockchain_top(top_height, top_hash)) {
- uint64_t dismiss = 1;
- for (const auto &h: block_hashes) {
- if (top_hash == h) {
- LOG_DEBUG_CC(context, "Found current top block in synced blocks, dismissing "
- << dismiss << "/" << arg.blocks.size() << " blocks");
- while (dismiss--)
- arg.blocks.pop_front();
- break;
- }
- ++dismiss;
+ while (1)
+ {
+ const uint64_t previous_height = m_core.get_current_blockchain_height();
+ uint64_t start_height;
+ std::list<cryptonote::block_complete_entry> blocks;
+ boost::uuids::uuid span_connection_id;
+ if (!m_block_queue.get_next_span(start_height, blocks, span_connection_id))
+ {
+ MDEBUG(context << " no next span found, going back to download");
+ break;
}
- }
-
- if (arg.blocks.empty())
- goto skip;
+ MDEBUG(context << " next span in the queue has blocks " << start_height << "-" << (start_height + blocks.size() - 1)
+ << ", we need " << previous_height);
- m_core.prepare_handle_incoming_blocks(arg.blocks);
- for(const block_complete_entry& block_entry: arg.blocks)
- {
- if (m_stopping)
+ block new_block;
+ if (!parse_and_validate_block_from_blob(blocks.front().block, new_block))
{
- m_core.cleanup_handle_incoming_blocks();
- return 1;
+ MERROR("Failed to parse block, but it should already have been parsed");
+ break;
+ }
+ bool parent_known = m_core.have_block(new_block.prev_id);
+ if (!parent_known)
+ {
+ // it could be:
+ // - later in the current chain
+ // - later in an alt chain
+ // - orphan
+ // if it was requested, then it'll be resolved later, otherwise it's an orphan
+ bool parent_requested = m_block_queue.requested(new_block.prev_id);
+ if (!parent_requested)
+ {
+ // this can happen if a connection was sicced onto a late span, if it did not have those blocks,
+ // since we don't know that at the sic time
+ LOG_ERROR_CCONTEXT("Got block with unknown parent which was not requested - querying block hashes");
+ context.m_needed_objects.clear();
+ context.m_last_response_height = 0;
+ goto skip;
+ }
+
+ // parent was requested, so we wait for it to be retrieved
+ MINFO(context << " parent was requested, we'll get back to it");
+ break;
}
- // process transactions
- TIME_MEASURE_START(transactions_process_time);
- for(auto& tx_blob: block_entry.txs)
+ const boost::posix_time::ptime start = boost::posix_time::microsec_clock::universal_time();
+ context.m_last_request_time = start;
+
+ m_core.prepare_handle_incoming_blocks(blocks);
+
+ uint64_t block_process_time_full = 0, transactions_process_time_full = 0;
+ size_t num_txs = 0;
+ for(const block_complete_entry& block_entry: blocks)
{
- tx_verification_context tvc = AUTO_VAL_INIT(tvc);
- m_core.handle_incoming_tx(tx_blob, tvc, true, true, false);
- if(tvc.m_verifivation_failed)
+ if (m_stopping)
+ {
+ m_core.cleanup_handle_incoming_blocks();
+ return 1;
+ }
+
+ // process transactions
+ TIME_MEASURE_START(transactions_process_time);
+ num_txs += block_entry.txs.size();
+ std::vector<tx_verification_context> tvc;
+ m_core.handle_incoming_txs(block_entry.txs, tvc, true, true, false);
+ std::list<blobdata>::const_iterator it = block_entry.txs.begin();
+ for (size_t i = 0; i < tvc.size(); ++i, ++it)
+ {
+ if(tvc[i].m_verifivation_failed)
+ {
+ if (!m_p2p->for_connection(span_connection_id, [&](cryptonote_connection_context& context, nodetool::peerid_type peer_id, uint32_t f)->bool{
+ LOG_ERROR_CCONTEXT("transaction verification failed on NOTIFY_RESPONSE_GET_OBJECTS, tx_id = "
+ << epee::string_tools::pod_to_hex(get_blob_hash(*it)) << ", dropping connection");
+ drop_connection(context, false, true);
+ return true;
+ }))
+ LOG_ERROR_CCONTEXT("span connection id not found");
+
+ if (!m_core.cleanup_handle_incoming_blocks())
+ {
+ LOG_PRINT_CCONTEXT_L0("Failure in cleanup_handle_incoming_blocks");
+ return 1;
+ }
+ // in case the peer had dropped beforehand, remove the span anyway so other threads can wake up and get it
+ m_block_queue.remove_spans(span_connection_id, start_height);
+ return 1;
+ }
+ }
+ TIME_MEASURE_FINISH(transactions_process_time);
+ transactions_process_time_full += transactions_process_time;
+
+ // process block
+
+ TIME_MEASURE_START(block_process_time);
+ block_verification_context bvc = boost::value_initialized<block_verification_context>();
+
+ m_core.handle_incoming_block(block_entry.block, bvc, false); // <--- process block
+
+ if(bvc.m_verifivation_failed)
{
- LOG_ERROR_CCONTEXT("transaction verification failed on NOTIFY_RESPONSE_GET_OBJECTS, tx_id = "
- << epee::string_tools::pod_to_hex(get_blob_hash(tx_blob)) << ", dropping connection");
- m_p2p->drop_connection(context);
- m_core.cleanup_handle_incoming_blocks();
+ if (!m_p2p->for_connection(span_connection_id, [&](cryptonote_connection_context& context, nodetool::peerid_type peer_id, uint32_t f)->bool{
+ LOG_PRINT_CCONTEXT_L1("Block verification failed, dropping connection");
+ drop_connection(context, true, true);
+ return true;
+ }))
+ LOG_ERROR_CCONTEXT("span connection id not found");
+
+ if (!m_core.cleanup_handle_incoming_blocks())
+ {
+ LOG_PRINT_CCONTEXT_L0("Failure in cleanup_handle_incoming_blocks");
+ return 1;
+ }
+
+ // in case the peer had dropped beforehand, remove the span anyway so other threads can wake up and get it
+ m_block_queue.remove_spans(span_connection_id, start_height);
+ return 1;
+ }
+ if(bvc.m_marked_as_orphaned)
+ {
+ if (!m_p2p->for_connection(span_connection_id, [&](cryptonote_connection_context& context, nodetool::peerid_type peer_id, uint32_t f)->bool{
+ LOG_PRINT_CCONTEXT_L1("Block received at sync phase was marked as orphaned, dropping connection");
+ drop_connection(context, true, true);
+ return true;
+ }))
+ LOG_ERROR_CCONTEXT("span connection id not found");
+
+ if (!m_core.cleanup_handle_incoming_blocks())
+ {
+ LOG_PRINT_CCONTEXT_L0("Failure in cleanup_handle_incoming_blocks");
+ return 1;
+ }
+
+ // in case the peer had dropped beforehand, remove the span anyway so other threads can wake up and get it
+ m_block_queue.remove_spans(span_connection_id, start_height);
return 1;
}
- }
- TIME_MEASURE_FINISH(transactions_process_time);
- // process block
+ TIME_MEASURE_FINISH(block_process_time);
+ block_process_time_full += block_process_time;
- TIME_MEASURE_START(block_process_time);
- block_verification_context bvc = boost::value_initialized<block_verification_context>();
+ } // each download block
- m_core.handle_incoming_block(block_entry.block, bvc, false); // <--- process block
+ MCINFO("sync-info", "Block process time (" << blocks.size() << " blocks, " << num_txs << " txs): " << block_process_time_full + transactions_process_time_full << " (" << transactions_process_time_full << "/" << block_process_time_full << ") ms");
- if(bvc.m_verifivation_failed)
- {
- LOG_PRINT_CCONTEXT_L1("Block verification failed, dropping connection");
- m_p2p->drop_connection(context);
- m_p2p->add_host_fail(context.m_remote_address);
- m_core.cleanup_handle_incoming_blocks();
- return 1;
- }
- if(bvc.m_marked_as_orphaned)
+ if (!m_core.cleanup_handle_incoming_blocks())
{
- LOG_PRINT_CCONTEXT_L1("Block received at sync phase was marked as orphaned, dropping connection");
- m_p2p->drop_connection(context);
- m_p2p->add_host_fail(context.m_remote_address);
- m_core.cleanup_handle_incoming_blocks();
+ LOG_PRINT_CCONTEXT_L0("Failure in cleanup_handle_incoming_blocks");
return 1;
}
- TIME_MEASURE_FINISH(block_process_time);
- LOG_PRINT_CCONTEXT_L2("Block process time: " << block_process_time + transactions_process_time << "(" << transactions_process_time << "/" << block_process_time << ")ms");
-
- } // each download block
- m_core.cleanup_handle_incoming_blocks();
+ m_block_queue.remove_spans(span_connection_id, start_height);
- if (m_core.get_current_blockchain_height() > previous_height)
- {
- MGINFO_YELLOW(context << " Synced " << m_core.get_current_blockchain_height() << "/" << m_core.get_target_blockchain_height());
+ if (m_core.get_current_blockchain_height() > previous_height)
+ {
+ const boost::posix_time::time_duration dt = boost::posix_time::microsec_clock::universal_time() - start;
+ std::string timing_message = "";
+ if (ELPP->vRegistry()->allowed(el::Level::Info, "sync-info"))
+ timing_message = std::string(" (") + std::to_string(dt.total_microseconds()/1e6) + " sec, "
+ + std::to_string((m_core.get_current_blockchain_height() - previous_height) * 1e6 / dt.total_microseconds())
+ + " blocks/sec), " + std::to_string(m_block_queue.get_data_size() / 1048576.f) + " MB queued";
+ if (ELPP->vRegistry()->allowed(el::Level::Debug, "sync-info"))
+ timing_message += std::string(": ") + m_block_queue.get_overview();
+ MGINFO_YELLOW(context << " Synced " << m_core.get_current_blockchain_height() << "/" << m_core.get_target_blockchain_height()
+ << timing_message);
+ }
}
- } // if not DISCARD BLOCK
-
+ }
+ if (should_download_next_span(context))
+ {
+ context.m_needed_objects.clear();
+ context.m_last_response_height = 0;
+ force_next_span = true;
+ }
}
+
skip:
- request_missing_objects(context, true);
+ if (!request_missing_objects(context, true, force_next_span))
+ {
+ LOG_ERROR_CCONTEXT("Failed to request missing objects, dropping connection");
+ drop_connection(context, false, false);
+ return 1;
+ }
return 1;
}
//------------------------------------------------------------------------------------------------------------------------
template<class t_core>
bool t_cryptonote_protocol_handler<t_core>::on_idle()
{
+ m_idle_peer_kicker.do_call(boost::bind(&t_cryptonote_protocol_handler<t_core>::kick_idle_peers, this));
return m_core.on_idle();
}
//------------------------------------------------------------------------------------------------------------------------
template<class t_core>
+ bool t_cryptonote_protocol_handler<t_core>::kick_idle_peers()
+ {
+ MTRACE("Checking for idle peers...");
+ m_p2p->for_each_connection([&](cryptonote_connection_context& context, nodetool::peerid_type peer_id, uint32_t support_flags)->bool
+ {
+ if (context.m_state == cryptonote_connection_context::state_synchronizing)
+ {
+ const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
+ const boost::posix_time::time_duration dt = now - context.m_last_request_time;
+ if (dt.total_microseconds() > IDLE_PEER_KICK_TIME)
+ {
+ MINFO(context << " kicking idle peer");
+ ++context.m_callback_request_count;
+ m_p2p->request_callback(context);
+ }
+ }
+ return true;
+ });
+ return true;
+ }
+ //------------------------------------------------------------------------------------------------------------------------
+ template<class t_core>
int t_cryptonote_protocol_handler<t_core>::handle_request_chain(int command, NOTIFY_REQUEST_CHAIN::request& arg, cryptonote_connection_context& context)
{
MLOG_P2P_MESSAGE("Received NOTIFY_REQUEST_CHAIN (" << arg.block_ids.size() << " blocks");
@@ -1019,7 +1211,7 @@ skip:
if(!m_core.find_blockchain_supplement(arg.block_ids, r))
{
LOG_ERROR_CCONTEXT("Failed to handle NOTIFY_REQUEST_CHAIN.");
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
return 1;
}
LOG_PRINT_CCONTEXT_L2("-->>NOTIFY_RESPONSE_CHAIN_ENTRY: m_start_height=" << r.start_height << ", m_total_height=" << r.total_height << ", m_block_ids.size()=" << r.m_block_ids.size());
@@ -1028,54 +1220,287 @@ skip:
}
//------------------------------------------------------------------------------------------------------------------------
template<class t_core>
- bool t_cryptonote_protocol_handler<t_core>::request_missing_objects(cryptonote_connection_context& context, bool check_having_blocks)
+ bool t_cryptonote_protocol_handler<t_core>::should_download_next_span(cryptonote_connection_context& context) const
{
- //if (!m_one_request == false)
- //return true;
- m_one_request = false;
- // save request size to log (dr monero)
- /*using namespace boost::chrono;
- auto point = steady_clock::now();
- auto time_from_epoh = point.time_since_epoch();
- auto sec = duration_cast< seconds >( time_from_epoh ).count();*/
+ std::list<crypto::hash> hashes;
+ boost::uuids::uuid span_connection_id;
+ boost::posix_time::ptime request_time;
+ std::pair<uint64_t, uint64_t> span;
+
+ span = m_block_queue.get_start_gap_span();
+ if (span.second > 0)
+ {
+ MDEBUG(context << " we should download it as there is a gap");
+ return true;
+ }
- if(context.m_needed_objects.size())
+ // if the next span is not scheduled (or there is none)
+ span = m_block_queue.get_next_span_if_scheduled(hashes, span_connection_id, request_time);
+ if (span.second == 0)
+ {
+ // we might be in a weird case where there is a filled next span,
+ // but it starts higher than the current height
+ uint64_t height;
+ std::list<cryptonote::block_complete_entry> bcel;
+ if (!m_block_queue.get_next_span(height, bcel, span_connection_id, true))
+ return false;
+ if (height > m_core.get_current_blockchain_height())
+ {
+ MDEBUG(context << " we should download it as the next block isn't scheduled");
+ return true;
+ }
+ return false;
+ }
+ // if it was scheduled by this particular peer
+ if (span_connection_id == context.m_connection_id)
+ return false;
+
+ float span_speed = m_block_queue.get_speed(span_connection_id);
+ float speed = m_block_queue.get_speed(context.m_connection_id);
+ MDEBUG(context << " next span is scheduled for " << span_connection_id << ", speed " << span_speed << ", ours " << speed);
+
+ // we try for that span too if:
+ // - we're substantially faster, or:
+ // - we're the fastest and the other one isn't (avoids a peer being waaaay slow but yet unmeasured)
+ // - the other one asked at least 5 seconds ago
+ if (span_speed < .25 && speed > .75f)
+ {
+ MDEBUG(context << " we should download it as we're substantially faster");
+ return true;
+ }
+ if (speed == 1.0f && span_speed != 1.0f)
+ {
+ MDEBUG(context << " we should download it as we're the fastest peer");
+ return true;
+ }
+ const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
+ if ((now - request_time).total_microseconds() > REQUEST_NEXT_SCHEDULED_SPAN_THRESHOLD)
+ {
+ MDEBUG(context << " we should download it as this span was requested long ago");
+ return true;
+ }
+ return false;
+ }
+ //------------------------------------------------------------------------------------------------------------------------
+ template<class t_core>
+ bool t_cryptonote_protocol_handler<t_core>::request_missing_objects(cryptonote_connection_context& context, bool check_having_blocks, bool force_next_span)
+ {
+ // flush stale spans
+ std::set<boost::uuids::uuid> live_connections;
+ m_p2p->for_each_connection([&](cryptonote_connection_context& context, nodetool::peerid_type peer_id, uint32_t support_flags)->bool{
+ live_connections.insert(context.m_connection_id);
+ return true;
+ });
+ m_block_queue.flush_stale_spans(live_connections);
+
+ // if we don't need to get next span, and the block queue is full enough, wait a bit
+ bool start_from_current_chain = false;
+ if (!force_next_span)
+ {
+ bool first = true;
+ while (1)
+ {
+ size_t nblocks = m_block_queue.get_num_filled_spans();
+ size_t size = m_block_queue.get_data_size();
+ if (nblocks < BLOCK_QUEUE_NBLOCKS_THRESHOLD || size < BLOCK_QUEUE_SIZE_THRESHOLD)
+ {
+ if (!first)
+ {
+ LOG_DEBUG_CC(context, "Block queue is " << nblocks << " and " << size << ", resuming");
+ }
+ break;
+ }
+
+ if (should_download_next_span(context))
+ {
+ MDEBUG(context << " we should try for that next span too, we think we could get it faster, resuming");
+ force_next_span = true;
+ break;
+ }
+
+ if (first)
+ {
+ LOG_DEBUG_CC(context, "Block queue is " << nblocks << " and " << size << ", pausing");
+ first = false;
+ context.m_state = cryptonote_connection_context::state_standby;
+ }
+
+ // this needs doing after we went to standby, so the callback knows what to do
+ bool filled;
+ if (m_block_queue.has_next_span(context.m_connection_id, filled) && !filled)
+ {
+ MDEBUG(context << " we have the next span, and it is scheduled, resuming");
+ ++context.m_callback_request_count;
+ m_p2p->request_callback(context);
+ return 1;
+ }
+
+ for (size_t n = 0; n < 50; ++n)
+ {
+ if (m_stopping)
+ return 1;
+ boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
+ }
+ }
+ context.m_state = cryptonote_connection_context::state_synchronizing;
+ }
+
+ MDEBUG(context << " request_missing_objects: check " << check_having_blocks << ", force_next_span " << force_next_span << ", m_needed_objects " << context.m_needed_objects.size() << " lrh " << context.m_last_response_height << ", chain " << m_core.get_current_blockchain_height());
+ if(context.m_needed_objects.size() || force_next_span)
{
//we know objects that we need, request this objects
NOTIFY_REQUEST_GET_OBJECTS::request req;
+ bool is_next = false;
size_t count = 0;
- auto it = context.m_needed_objects.begin();
-
- const size_t count_limit = m_core.get_block_sync_size();
- MDEBUG("Setting count_limit: " << count_limit);
- while(it != context.m_needed_objects.end() && count < count_limit)
+ const size_t count_limit = m_core.get_block_sync_size(m_core.get_current_blockchain_height());
+ std::pair<uint64_t, uint64_t> span = std::make_pair(0, 0);
{
- if( !(check_having_blocks && m_core.have_block(*it)))
+ MDEBUG(context << " checking for gap");
+ span = m_block_queue.get_start_gap_span();
+ if (span.second > 0)
{
- req.blocks.push_back(*it);
- ++count;
- context.m_requested_objects.insert(*it);
+ const uint64_t first_block_height_known = context.m_last_response_height - context.m_needed_objects.size() + 1;
+ const uint64_t last_block_height_known = context.m_last_response_height;
+ const uint64_t first_block_height_needed = span.first;
+ const uint64_t last_block_height_needed = span.first + std::min(span.second, (uint64_t)count_limit) - 1;
+ MDEBUG(context << " gap found, span: " << span.first << " - " << span.first + span.second - 1 << " (" << last_block_height_needed << ")");
+ MDEBUG(context << " current known hashes from from " << first_block_height_known << " to " << last_block_height_known);
+ if (first_block_height_needed < first_block_height_known || last_block_height_needed > last_block_height_known)
+ {
+ MDEBUG(context << " we are missing some of the necessary hashes for this gap, requesting chain again");
+ context.m_needed_objects.clear();
+ context.m_last_response_height = 0;
+ start_from_current_chain = true;
+ goto skip;
+ }
+ MDEBUG(context << " we have the hashes for this gap");
}
- context.m_needed_objects.erase(it++);
}
- LOG_PRINT_CCONTEXT_L1("-->>NOTIFY_REQUEST_GET_OBJECTS: blocks.size()=" << req.blocks.size() << ", txs.size()=" << req.txs.size()
- << "requested blocks count=" << count << " / " << count_limit);
- //epee::net_utils::network_throttle_manager::get_global_throttle_inreq().logger_handle_net("log/dr-monero/net/req-all.data", sec, get_avg_block_size());
+ if (force_next_span)
+ {
+ if (span.second == 0)
+ {
+ std::list<crypto::hash> hashes;
+ boost::uuids::uuid span_connection_id;
+ boost::posix_time::ptime time;
+ span = m_block_queue.get_next_span_if_scheduled(hashes, span_connection_id, time);
+ if (span.second > 0)
+ {
+ is_next = true;
+ for (const auto &hash: hashes)
+ {
+ req.blocks.push_back(hash);
+ context.m_requested_objects.insert(hash);
+ }
+ }
+ }
+ }
+ if (span.second == 0)
+ {
+ MDEBUG(context << " span size is 0");
+ if (context.m_last_response_height + 1 < context.m_needed_objects.size())
+ {
+ MERROR(context << " ERROR: inconsistent context: lrh " << context.m_last_response_height << ", nos " << context.m_needed_objects.size());
+ context.m_needed_objects.clear();
+ context.m_last_response_height = 0;
+ goto skip;
+ }
+ // take out blocks we already have
+ while (!context.m_needed_objects.empty() && m_core.have_block(context.m_needed_objects.front()))
+ {
+ context.m_needed_objects.pop_front();
+ }
+ const uint64_t first_block_height = context.m_last_response_height - context.m_needed_objects.size() + 1;
+ span = m_block_queue.reserve_span(first_block_height, context.m_last_response_height, count_limit, context.m_connection_id, context.m_needed_objects);
+ MDEBUG(context << " span from " << first_block_height << ": " << span.first << "/" << span.second);
+ }
+ if (span.second == 0 && !force_next_span)
+ {
+ MDEBUG(context << " still no span reserved, we may be in the corner case of next span scheduled and everything else scheduled/filled");
+ std::list<crypto::hash> hashes;
+ boost::uuids::uuid span_connection_id;
+ boost::posix_time::ptime time;
+ span = m_block_queue.get_next_span_if_scheduled(hashes, span_connection_id, time);
+ if (span.second > 0)
+ {
+ is_next = true;
+ for (const auto &hash: hashes)
+ {
+ req.blocks.push_back(hash);
+ ++count;
+ context.m_requested_objects.insert(hash);
+ // that's atrocious O(n) wise, but this is rare
+ auto i = std::find(context.m_needed_objects.begin(), context.m_needed_objects.end(), hash);
+ if (i != context.m_needed_objects.end())
+ context.m_needed_objects.erase(i);
+ }
+ }
+ }
+ MDEBUG(context << " span: " << span.first << "/" << span.second << " (" << span.first << " - " << (span.first + span.second - 1) << ")");
+ if (span.second > 0)
+ {
+ if (!is_next)
+ {
+ const uint64_t first_context_block_height = context.m_last_response_height - context.m_needed_objects.size() + 1;
+ uint64_t skip = span.first - first_context_block_height;
+ if (skip > context.m_needed_objects.size())
+ {
+ MERROR("ERROR: skip " << skip << ", m_needed_objects " << context.m_needed_objects.size() << ", first_context_block_height" << first_context_block_height);
+ return false;
+ }
+ while (skip--)
+ context.m_needed_objects.pop_front();
+ if (context.m_needed_objects.size() < span.second)
+ {
+ MERROR("ERROR: span " << span.first << "/" << span.second << ", m_needed_objects " << context.m_needed_objects.size());
+ return false;
+ }
- post_notify<NOTIFY_REQUEST_GET_OBJECTS>(req, context);
- }else if(context.m_last_response_height < context.m_remote_blockchain_height-1)
+ auto it = context.m_needed_objects.begin();
+ for (size_t n = 0; n < span.second; ++n)
+ {
+ req.blocks.push_back(*it);
+ ++count;
+ context.m_requested_objects.insert(*it);
+ auto j = it++;
+ context.m_needed_objects.erase(j);
+ }
+ }
+
+ context.m_last_request_time = boost::posix_time::microsec_clock::universal_time();
+ LOG_PRINT_CCONTEXT_L1("-->>NOTIFY_REQUEST_GET_OBJECTS: blocks.size()=" << req.blocks.size() << ", txs.size()=" << req.txs.size()
+ << "requested blocks count=" << count << " / " << count_limit << " from " << span.first << ", first hash " << req.blocks.front());
+ //epee::net_utils::network_throttle_manager::get_global_throttle_inreq().logger_handle_net("log/dr-monero/net/req-all.data", sec, get_avg_block_size());
+
+ post_notify<NOTIFY_REQUEST_GET_OBJECTS>(req, context);
+ return true;
+ }
+ }
+
+skip:
+ context.m_needed_objects.clear();
+ if(context.m_last_response_height < context.m_remote_blockchain_height-1)
{//we have to fetch more objects ids, request blockchain entry
NOTIFY_REQUEST_CHAIN::request r = boost::value_initialized<NOTIFY_REQUEST_CHAIN::request>();
m_core.get_short_chain_history(r.block_ids);
+
+ if (!start_from_current_chain)
+ {
+ // we'll want to start off from where we are on that peer, which may not be added yet
+ if (context.m_last_known_hash != cryptonote::null_hash && r.block_ids.front() != context.m_last_known_hash)
+ r.block_ids.push_front(context.m_last_known_hash);
+ }
+
handler_request_blocks_history( r.block_ids ); // change the limit(?), sleep(?)
//std::string blob; // for calculate size of request
//epee::serialization::store_t_to_binary(r, blob);
//epee::net_utils::network_throttle_manager::get_global_throttle_inreq().logger_handle_net("log/dr-monero/net/req-all.data", sec, get_avg_block_size());
- LOG_PRINT_CCONTEXT_L1("r = " << 200);
+ //LOG_PRINT_CCONTEXT_L1("r = " << 200);
- LOG_PRINT_CCONTEXT_L1("-->>NOTIFY_REQUEST_CHAIN: m_block_ids.size()=" << r.block_ids.size() );
+ context.m_last_request_time = boost::posix_time::microsec_clock::universal_time();
+ LOG_PRINT_CCONTEXT_L1("-->>NOTIFY_REQUEST_CHAIN: m_block_ids.size()=" << r.block_ids.size() << ", start_from_current_chain " << start_from_current_chain);
post_notify<NOTIFY_REQUEST_CHAIN>(r, context);
}else
{
@@ -1089,8 +1514,18 @@ skip:
<< "\r\non connection [" << epee::net_utils::print_connection_context_short(context)<< "]");
context.m_state = cryptonote_connection_context::state_normal;
- MGINFO_GREEN("SYNCHRONIZED OK");
- on_connection_synchronized();
+ if (context.m_remote_blockchain_height >= m_core.get_target_blockchain_height())
+ {
+ if (m_core.get_current_blockchain_height() >= m_core.get_target_blockchain_height())
+ {
+ MGINFO_GREEN("SYNCHRONIZED OK");
+ on_connection_synchronized();
+ }
+ }
+ else
+ {
+ MINFO(context << " we've reached this peer's blockchain height");
+ }
}
return true;
}
@@ -1108,6 +1543,7 @@ skip:
<< "**********************************************************************");
m_core.on_synchronized();
}
+ m_core.safesyncmode(true);
return true;
}
//------------------------------------------------------------------------------------------------------------------------
@@ -1132,17 +1568,7 @@ skip:
if(!arg.m_block_ids.size())
{
LOG_ERROR_CCONTEXT("sent empty m_block_ids, dropping connection");
- m_p2p->drop_connection(context);
- m_p2p->add_host_fail(context.m_remote_address);
- return 1;
- }
-
- if(!m_core.have_block(arg.m_block_ids.front()))
- {
- LOG_ERROR_CCONTEXT("sent m_block_ids starting from unknown id: "
- << epee::string_tools::pod_to_hex(arg.m_block_ids.front()) << " , dropping connection");
- m_p2p->drop_connection(context);
- m_p2p->add_host_fail(context.m_remote_address);
+ drop_connection(context, true, false);
return 1;
}
@@ -1153,16 +1579,25 @@ skip:
LOG_ERROR_CCONTEXT("sent wrong NOTIFY_RESPONSE_CHAIN_ENTRY, with m_total_height=" << arg.total_height
<< ", m_start_height=" << arg.start_height
<< ", m_block_ids.size()=" << arg.m_block_ids.size());
- m_p2p->drop_connection(context);
+ drop_connection(context, false, false);
+ return 1;
}
for(auto& bl_id: arg.m_block_ids)
{
- if(!m_core.have_block(bl_id))
- context.m_needed_objects.push_back(bl_id);
+ context.m_needed_objects.push_back(bl_id);
+ }
+
+ if (!request_missing_objects(context, false))
+ {
+ LOG_ERROR_CCONTEXT("Failed to request missing objects, dropping connection");
+ drop_connection(context, false, false);
+ return 1;
}
- request_missing_objects(context, false);
+ if (arg.total_height > m_core.get_target_blockchain_height())
+ m_core.set_target_blockchain_height(arg.total_height);
+
return 1;
}
//------------------------------------------------------------------------------------------------------------------------
@@ -1187,7 +1622,7 @@ skip:
{
if (peer_id && exclude_context.m_connection_id != context.m_connection_id)
{
- if(m_core.get_testnet() && (support_flags & P2P_SUPPORT_FLAG_FLUFFY_BLOCKS))
+ if(m_core.fluffy_blocks_enabled() && (support_flags & P2P_SUPPORT_FLAG_FLUFFY_BLOCKS))
{
LOG_DEBUG_CC(context, "PEER SUPPORTS FLUFFY BLOCKS - RELAYING THIN/COMPACT WHATEVER BLOCK");
fluffyConnections.push_back(context.m_connection_id);
@@ -1216,6 +1651,36 @@ skip:
m_core.on_transaction_relayed(*tx_blob_it);
return relay_post_notify<NOTIFY_NEW_TRANSACTIONS>(arg, exclude_context);
}
+ //------------------------------------------------------------------------------------------------------------------------
+ template<class t_core>
+ void t_cryptonote_protocol_handler<t_core>::drop_connection(cryptonote_connection_context &context, bool add_fail, bool flush_all_spans)
+ {
+ if (add_fail)
+ m_p2p->add_host_fail(context.m_remote_address);
+
+ m_p2p->drop_connection(context);
+
+ m_block_queue.flush_spans(context.m_connection_id, flush_all_spans);
+ }
+ //------------------------------------------------------------------------------------------------------------------------
+ template<class t_core>
+ void t_cryptonote_protocol_handler<t_core>::on_connection_close(cryptonote_connection_context &context)
+ {
+ uint64_t target = 0;
+ m_p2p->for_each_connection([&](const connection_context& cntxt, nodetool::peerid_type peer_id, uint32_t support_flags) {
+ if (cntxt.m_state >= cryptonote_connection_context::state_synchronizing && cntxt.m_connection_id != context.m_connection_id)
+ target = std::max(target, cntxt.m_remote_blockchain_height);
+ return true;
+ });
+ const uint64_t previous_target = m_core.get_target_blockchain_height();
+ if (target < previous_target)
+ {
+ MINFO("Target height decreasing from " << previous_target << " to " << target);
+ m_core.set_target_blockchain_height(target);
+ }
+
+ m_block_queue.flush_spans(context.m_connection_id, false);
+ }
//------------------------------------------------------------------------------------------------------------------------
template<class t_core>
diff --git a/src/daemon/CMakeLists.txt b/src/daemon/CMakeLists.txt
index 795442a2d..782667867 100644
--- a/src/daemon/CMakeLists.txt
+++ b/src/daemon/CMakeLists.txt
@@ -92,12 +92,15 @@ target_link_libraries(daemon
p2p
cryptonote_protocol
daemonizer
+ serialization
+ daemon_rpc_server
${Boost_CHRONO_LIBRARY}
${Boost_FILESYSTEM_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
${Boost_REGEX_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${CMAKE_THREAD_LIBS_INIT}
+ ${ZMQ_LIB}
${EXTRA_LIBRARIES})
add_dependencies(daemon version)
set_property(TARGET daemon
diff --git a/src/daemon/command_line_args.h b/src/daemon/command_line_args.h
index 8eb3db195..f19d5cc63 100644
--- a/src/daemon/command_line_args.h
+++ b/src/daemon/command_line_args.h
@@ -64,6 +64,25 @@ namespace daemon_args
, "Max number of threads to use for a parallel job"
, 0
};
+
+ const command_line::arg_descriptor<std::string> arg_zmq_rpc_bind_ip = {
+ "zmq-rpc-bind-ip"
+ , "IP for ZMQ RPC server to listen on"
+ , "127.0.0.1"
+ };
+
+ const command_line::arg_descriptor<std::string> arg_zmq_rpc_bind_port = {
+ "zmq-rpc-bind-port"
+ , "Port for ZMQ RPC server to listen on"
+ , std::to_string(config::ZMQ_RPC_DEFAULT_PORT)
+ };
+
+ const command_line::arg_descriptor<std::string> arg_zmq_testnet_rpc_bind_port = {
+ "zmq-testnet-rpc-bind-port"
+ , "Port for testnet ZMQ RPC server to listen on"
+ , std::to_string(config::testnet::ZMQ_RPC_DEFAULT_PORT)
+ };
+
} // namespace daemon_args
#endif // DAEMON_COMMAND_LINE_ARGS_H
diff --git a/src/daemon/command_parser_executor.cpp b/src/daemon/command_parser_executor.cpp
index a7caeeffc..d949a57b1 100644
--- a/src/daemon/command_parser_executor.cpp
+++ b/src/daemon/command_parser_executor.cpp
@@ -256,7 +256,8 @@ bool t_command_parser_executor::start_mining(const std::vector<std::string>& arg
if(!cryptonote::get_account_integrated_address_from_str(adr, has_payment_id, payment_id, true, args.front()))
{
bool dnssec_valid;
- std::string address_str = tools::dns_utils::get_account_address_as_str_from_url(args.front(), dnssec_valid);
+ std::string address_str = tools::dns_utils::get_account_address_as_str_from_url(args.front(), dnssec_valid,
+ [](const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid){return addresses[0];});
if(!cryptonote::get_account_integrated_address_from_str(adr, has_payment_id, payment_id, false, address_str))
{
if(!cryptonote::get_account_integrated_address_from_str(adr, has_payment_id, payment_id, true, address_str))
@@ -578,4 +579,11 @@ bool t_command_parser_executor::relay_tx(const std::vector<std::string>& args)
return m_executor.relay_tx(txid);
}
+bool t_command_parser_executor::sync_info(const std::vector<std::string>& args)
+{
+ if (args.size() != 0) return false;
+
+ return m_executor.sync_info();
+}
+
} // namespace daemonize
diff --git a/src/daemon/command_parser_executor.h b/src/daemon/command_parser_executor.h
index a453553f1..f301ef14a 100644
--- a/src/daemon/command_parser_executor.h
+++ b/src/daemon/command_parser_executor.h
@@ -134,6 +134,8 @@ public:
bool update(const std::vector<std::string>& args);
bool relay_tx(const std::vector<std::string>& args);
+
+ bool sync_info(const std::vector<std::string>& args);
};
} // namespace daemonize
diff --git a/src/daemon/command_server.cpp b/src/daemon/command_server.cpp
index f47891fdd..9df698547 100644
--- a/src/daemon/command_server.cpp
+++ b/src/daemon/command_server.cpp
@@ -231,7 +231,7 @@ t_command_server::t_command_server(
m_command_lookup.set_handler(
"print_coinbase_tx_sum"
, std::bind(&t_command_parser_executor::print_coinbase_tx_sum, &m_parser, p::_1)
- , "Print sum of coinbase transactions (start height, block count)"
+ , "Print sum of coinbase transactions <start height> [block count]"
);
m_command_lookup.set_handler(
"alt_chain_info"
@@ -253,6 +253,11 @@ t_command_server::t_command_server(
, std::bind(&t_command_parser_executor::relay_tx, &m_parser, p::_1)
, "Relay a given transaction by its txid"
);
+ m_command_lookup.set_handler(
+ "sync_info"
+ , std::bind(&t_command_parser_executor::sync_info, &m_parser, p::_1)
+ , "Print information about blockchain sync state"
+ );
}
bool t_command_server::process_command_str(const std::string& cmd)
diff --git a/src/daemon/daemon.cpp b/src/daemon/daemon.cpp
index 241cb3883..faa620c54 100644
--- a/src/daemon/daemon.cpp
+++ b/src/daemon/daemon.cpp
@@ -32,6 +32,8 @@
#include <stdexcept>
#include "misc_log_ex.h"
#include "daemon/daemon.h"
+#include "rpc/daemon_handler.h"
+#include "rpc/zmq_server.h"
#include "common/password.h"
#include "common/util.h"
@@ -41,7 +43,7 @@
#include "daemon/rpc.h"
#include "daemon/command_server.h"
#include "version.h"
-#include "../../contrib/epee/include/syncobj.h"
+#include "syncobj.h"
using namespace epee;
@@ -85,7 +87,18 @@ t_daemon::t_daemon(
boost::program_options::variables_map const & vm
)
: mp_internals{new t_internals{vm}}
-{}
+{
+ bool testnet = command_line::get_arg(vm, command_line::arg_testnet_on);
+ if (testnet)
+ {
+ zmq_rpc_bind_port = command_line::get_arg(vm, daemon_args::arg_zmq_testnet_rpc_bind_port);
+ }
+ else
+ {
+ zmq_rpc_bind_port = command_line::get_arg(vm, daemon_args::arg_zmq_rpc_bind_port);
+ }
+ zmq_rpc_bind_address = command_line::get_arg(vm, daemon_args::arg_zmq_rpc_bind_ip);
+}
t_daemon::~t_daemon() = default;
@@ -133,6 +146,30 @@ bool t_daemon::run(bool interactive)
rpc_commands->start_handling(std::bind(&daemonize::t_daemon::stop_p2p, this));
}
+ cryptonote::rpc::DaemonHandler rpc_daemon_handler(mp_internals->core.get(), mp_internals->p2p.get());
+ cryptonote::rpc::ZmqServer zmq_server(rpc_daemon_handler);
+
+ if (!zmq_server.addTCPSocket(zmq_rpc_bind_address, zmq_rpc_bind_port))
+ {
+ LOG_ERROR(std::string("Failed to add TCP Socket (") + zmq_rpc_bind_address
+ + ":" + zmq_rpc_bind_port + ") to ZMQ RPC Server");
+
+ if (interactive)
+ {
+ rpc_commands->stop_handling();
+ }
+
+ mp_internals->rpc.stop();
+
+ return false;
+ }
+
+ MINFO("Starting ZMQ server...");
+ zmq_server.run();
+
+ MINFO(std::string("ZMQ server started at ") + zmq_rpc_bind_address
+ + ":" + zmq_rpc_bind_port + ".");
+
mp_internals->p2p.run(); // blocks until p2p goes down
if (rpc_commands)
@@ -140,6 +177,8 @@ bool t_daemon::run(bool interactive)
rpc_commands->stop_handling();
}
+ zmq_server.stop();
+
mp_internals->rpc.stop();
mp_internals->core.get().get_miner().stop();
MGINFO("Node stopped.");
diff --git a/src/daemon/daemon.h b/src/daemon/daemon.h
index 2b9f18669..8c7547f62 100644
--- a/src/daemon/daemon.h
+++ b/src/daemon/daemon.h
@@ -43,6 +43,8 @@ private:
void stop_p2p();
private:
std::unique_ptr<t_internals> mp_internals;
+ std::string zmq_rpc_bind_address;
+ std::string zmq_rpc_bind_port;
public:
t_daemon(
boost::program_options::variables_map const & vm
diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp
index 19dd02171..8f6d542b6 100644
--- a/src/daemon/main.cpp
+++ b/src/daemon/main.cpp
@@ -89,6 +89,9 @@ int main(int argc, char const * argv[])
command_line::add_arg(core_settings, daemon_args::arg_log_file, default_log.string());
command_line::add_arg(core_settings, daemon_args::arg_log_level);
command_line::add_arg(core_settings, daemon_args::arg_max_concurrency);
+ command_line::add_arg(core_settings, daemon_args::arg_zmq_rpc_bind_ip);
+ command_line::add_arg(core_settings, daemon_args::arg_zmq_rpc_bind_port);
+ command_line::add_arg(core_settings, daemon_args::arg_zmq_testnet_rpc_bind_port);
daemonizer::init_options(hidden_options, visible_options);
daemonize::t_executor::init_options(core_settings);
@@ -142,16 +145,13 @@ int main(int argc, char const * argv[])
epee::debug::g_test_dbg_lock_sleep() = command_line::get_arg(vm, command_line::arg_test_dbg_lock_sleep);
- std::string db_type = command_line::get_arg(vm, command_line::arg_db_type);
+ std::string db_type = command_line::get_arg(vm, cryptonote::arg_db_type);
// verify that blockchaindb type is valid
- if(cryptonote::blockchain_db_types.count(db_type) == 0)
+ if(!cryptonote::blockchain_valid_db_type(db_type))
{
- std::cout << "Invalid database type (" << db_type << "), available types are:" << std::endl;
- for (const auto& type : cryptonote::blockchain_db_types)
- {
- std::cout << "\t" << type << std::endl;
- }
+ std::cout << "Invalid database type (" << db_type << "), available types are: " <<
+ cryptonote::blockchain_db_types(", ") << std::endl;
return 0;
}
diff --git a/src/daemon/rpc_command_executor.cpp b/src/daemon/rpc_command_executor.cpp
index 1c9cd714d..cda6f3f95 100644
--- a/src/daemon/rpc_command_executor.cpp
+++ b/src/daemon/rpc_command_executor.cpp
@@ -55,7 +55,9 @@ namespace {
std::string port_str;
std::string elapsed = epee::misc_utils::get_time_interval_string(now - last_seen);
std::string ip_str = epee::string_tools::get_ip_string_from_int32(peer.ip);
- epee::string_tools::xtype_to_string(peer.id, id_str);
+ std::stringstream peer_id_str;
+ peer_id_str << std::hex << std::setw(16) << peer.id;
+ peer_id_str >> id_str;
epee::string_tools::xtype_to_string(peer.port, port_str);
std::string addr_str = ip_str + ":" + port_str;
tools::msg_writer() << boost::format("%-10s %-25s %-25s %s") % prefix % id_str % addr_str % elapsed;
@@ -369,6 +371,7 @@ bool t_rpc_command_executor::show_status() {
cryptonote::COMMAND_RPC_MINING_STATUS::request mreq;
cryptonote::COMMAND_RPC_MINING_STATUS::response mres;
epee::json_rpc::error error_resp;
+ bool has_mining_info = true;
std::string fail_message = "Problem fetching info";
@@ -384,10 +387,8 @@ bool t_rpc_command_executor::show_status() {
{
return true;
}
- if (!m_rpc_client->rpc_request(mreq, mres, "/mining_status", fail_message.c_str()))
- {
- return true;
- }
+ // mining info is only available non unrestricted RPC mode
+ has_mining_info = m_rpc_client->rpc_request(mreq, mres, "/mining_status", fail_message.c_str());
}
else
{
@@ -419,16 +420,17 @@ bool t_rpc_command_executor::show_status() {
}
std::time_t uptime = std::time(nullptr) - ires.start_time;
+ uint64_t net_height = ires.target_height > ires.height ? ires.target_height : ires.height;
tools::success_msg_writer() << boost::format("Height: %llu/%llu (%.1f%%) on %s, %s, net hash %s, v%u%s, %s, %u(out)+%u(in) connections, uptime %ud %uh %um %us")
% (unsigned long long)ires.height
- % (unsigned long long)(ires.target_height >= ires.height ? ires.target_height : ires.height)
+ % (unsigned long long)net_height
% get_sync_percentage(ires)
% (ires.testnet ? "testnet" : "mainnet")
- % (mining_busy ? "syncing" : mres.active ? ( ( mres.is_background_mining_enabled ? "smart " : "" ) + std::string("mining at ") + get_mining_speed(mres.speed) ) : "not mining")
+ % (!has_mining_info ? "mining info unavailable" : mining_busy ? "syncing" : mres.active ? ( ( mres.is_background_mining_enabled ? "smart " : "" ) + std::string("mining at ") + get_mining_speed(mres.speed) ) : "not mining")
% get_mining_speed(ires.difficulty / ires.target)
% (unsigned)hfres.version
- % get_fork_extra_info(hfres.earliest_height, ires.height, ires.target)
+ % get_fork_extra_info(hfres.earliest_height, net_height, ires.target)
% (hfres.state == cryptonote::HardFork::Ready ? "up to date" : hfres.state == cryptonote::HardFork::UpdateNeeded ? "update needed" : "out of date, likely forked")
% (unsigned)ires.outgoing_connections_count
% (unsigned)ires.incoming_connections_count
@@ -484,7 +486,7 @@ bool t_rpc_command_executor::print_connections() {
tools::msg_writer()
//<< std::setw(30) << std::left << in_out
<< std::setw(30) << std::left << address
- << std::setw(20) << info.peer_id
+ << std::setw(20) << epee::string_tools::pad_string(info.peer_id, 16, '0', true)
<< std::setw(20) << info.support_flags
<< std::setw(30) << std::to_string(info.recv_count) + "(" + std::to_string(info.recv_idle_time) + ")/" + std::to_string(info.send_count) + "(" + std::to_string(info.send_idle_time) + ")"
<< std::setw(25) << info.state
@@ -926,6 +928,8 @@ bool t_rpc_command_executor::print_transaction_pool_short() {
bool t_rpc_command_executor::print_transaction_pool_stats() {
cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL_STATS::request req;
cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL_STATS::response res;
+ cryptonote::COMMAND_RPC_GET_INFO::request ireq;
+ cryptonote::COMMAND_RPC_GET_INFO::response ires;
std::string fail_message = "Problem fetching transaction pool stats";
@@ -935,6 +939,10 @@ bool t_rpc_command_executor::print_transaction_pool_stats() {
{
return true;
}
+ if (!m_rpc_client->rpc_request(ireq, ires, "/getinfo", fail_message.c_str()))
+ {
+ return true;
+ }
}
else
{
@@ -944,15 +952,32 @@ bool t_rpc_command_executor::print_transaction_pool_stats() {
tools::fail_msg_writer() << make_error(fail_message, res.status);
return true;
}
+ if (!m_rpc_server->on_get_info(ireq, ires) || ires.status != CORE_RPC_STATUS_OK)
+ {
+ tools::fail_msg_writer() << make_error(fail_message, ires.status);
+ return true;
+ }
}
size_t n_transactions = res.pool_stats.txs_total;
const uint64_t now = time(NULL);
size_t avg_bytes = n_transactions ? res.pool_stats.bytes_total / n_transactions : 0;
- tools::msg_writer() << n_transactions << " tx(es), " << res.pool_stats.bytes_total << " bytes total (min " << res.pool_stats.bytes_min << ", max " << res.pool_stats.bytes_max << ", avg " << avg_bytes << ")" << std::endl
- << "fees " << cryptonote::print_money(res.pool_stats.fee_total) << " (avg " << cryptonote::print_money(n_transactions ? res.pool_stats.fee_total / n_transactions : 0) << " per tx" << ", " << cryptonote::print_money(res.pool_stats.bytes_total ? res.pool_stats.fee_total / res.pool_stats.bytes_total : 0) << " per byte )" << std::endl
- << res.pool_stats.num_not_relayed << " not relayed, " << res.pool_stats.num_failing << " failing, " << res.pool_stats.num_10m << " older than 10 minutes (oldest " << (res.pool_stats.oldest == 0 ? "-" : get_human_time_ago(res.pool_stats.oldest, now)) << ")";
+ std::string backlog_message;
+ const uint64_t full_reward_zone = ires.block_size_limit / 2;
+ if (res.pool_stats.bytes_total <= full_reward_zone)
+ {
+ backlog_message = "no backlog";
+ }
+ else
+ {
+ uint64_t backlog = (res.pool_stats.bytes_total + full_reward_zone - 1) / full_reward_zone;
+ backlog_message = (boost::format("estimated %u block (%u minutes) backlog") % backlog % (backlog * DIFFICULTY_TARGET_V2 / 60)).str();
+ }
+
+ tools::msg_writer() << n_transactions << " tx(es), " << res.pool_stats.bytes_total << " bytes total (min " << res.pool_stats.bytes_min << ", max " << res.pool_stats.bytes_max << ", avg " << avg_bytes << ", median " << res.pool_stats.bytes_med << ")" << std::endl
+ << "fees " << cryptonote::print_money(res.pool_stats.fee_total) << " (avg " << cryptonote::print_money(n_transactions ? res.pool_stats.fee_total / n_transactions : 0) << " per tx" << ", " << cryptonote::print_money(res.pool_stats.bytes_total ? res.pool_stats.fee_total / res.pool_stats.bytes_total : 0) << " per byte)" << std::endl
+ << res.pool_stats.num_not_relayed << " not relayed, " << res.pool_stats.num_failing << " failing, " << res.pool_stats.num_10m << " older than 10 minutes (oldest " << (res.pool_stats.oldest == 0 ? "-" : get_human_time_ago(res.pool_stats.oldest, now)) << "), " << backlog_message;
if (n_transactions > 1 && res.pool_stats.histo.size())
{
@@ -966,7 +991,7 @@ bool t_rpc_command_executor::print_transaction_pool_stats() {
denom = n-1;
for (i=0; i<denom; i++)
times[i] = i * numer / denom;
- times[i] = res.pool_stats.oldest;
+ times[i] = now - res.pool_stats.oldest;
} else
{
numer = now - res.pool_stats.oldest;
@@ -1584,6 +1609,7 @@ bool t_rpc_command_executor::print_blockchain_dynamic_stats(uint64_t nblocks)
double avgreward = 0;
std::vector<uint64_t> sizes;
sizes.reserve(nblocks);
+ uint64_t earliest = std::numeric_limits<uint64_t>::max(), latest = 0;
std::vector<unsigned> major_versions(256, 0), minor_versions(256, 0);
for (const auto &bhr: bhres.headers)
{
@@ -1595,12 +1621,14 @@ bool t_rpc_command_executor::print_blockchain_dynamic_stats(uint64_t nblocks)
static_assert(sizeof(bhr.minor_version) == 1, "major_version expected to be uint8_t");
major_versions[(unsigned)bhr.major_version]++;
minor_versions[(unsigned)bhr.minor_version]++;
+ earliest = std::min(earliest, bhr.timestamp);
+ latest = std::max(latest, bhr.timestamp);
}
avgdiff /= nblocks;
avgnumtxes /= nblocks;
avgreward /= nblocks;
uint64_t median_block_size = epee::misc_utils::median(sizes);
- tools::msg_writer() << "Last " << nblocks << ": avg. diff " << (uint64_t)avgdiff << ", avg num txes " << avgnumtxes
+ tools::msg_writer() << "Last " << nblocks << ": avg. diff " << (uint64_t)avgdiff << ", " << (latest - earliest) / nblocks << " avg sec/block, avg num txes " << avgnumtxes
<< ", avg. reward " << cryptonote::print_money(avgreward) << ", median block size " << median_block_size;
unsigned int max_major = 256, max_minor = 256;
@@ -1695,4 +1723,65 @@ bool t_rpc_command_executor::relay_tx(const std::string &txid)
return true;
}
+bool t_rpc_command_executor::sync_info()
+{
+ cryptonote::COMMAND_RPC_SYNC_INFO::request req;
+ cryptonote::COMMAND_RPC_SYNC_INFO::response res;
+ std::string fail_message = "Unsuccessful";
+ epee::json_rpc::error error_resp;
+
+ if (m_is_rpc)
+ {
+ if (!m_rpc_client->json_rpc_request(req, res, "sync_info", fail_message.c_str()))
+ {
+ return true;
+ }
+ }
+ else
+ {
+ if (!m_rpc_server->on_sync_info(req, res, error_resp) || res.status != CORE_RPC_STATUS_OK)
+ {
+ tools::fail_msg_writer() << make_error(fail_message, res.status);
+ return true;
+ }
+ }
+
+ uint64_t target = res.target_height < res.height ? res.height : res.target_height;
+ tools::success_msg_writer() << "Height: " << res.height << ", target: " << target << " (" << (100.0 * res.height / target) << "%)";
+ uint64_t current_download = 0;
+ for (const auto &p: res.peers)
+ current_download += p.info.current_download;
+ tools::success_msg_writer() << "Downloading at " << current_download << " kB/s";
+
+ tools::success_msg_writer() << std::to_string(res.peers.size()) << " peers";
+ for (const auto &p: res.peers)
+ {
+ std::string address = epee::string_tools::pad_string(p.info.address, 24);
+ uint64_t nblocks = 0, size = 0;
+ for (const auto &s: res.spans)
+ if (s.rate > 0.0f && s.connection_id == p.info.connection_id)
+ nblocks += s.nblocks, size += s.size;
+ tools::success_msg_writer() << address << " " << epee::string_tools::pad_string(p.info.peer_id, 16, '0', true) << " " << p.info.height << " " << p.info.current_download << " kB/s, " << nblocks << " blocks / " << size/1e6 << " MB queued";
+ }
+
+ uint64_t total_size = 0;
+ for (const auto &s: res.spans)
+ total_size += s.size;
+ tools::success_msg_writer() << std::to_string(res.spans.size()) << " spans, " << total_size/1e6 << " MB";
+ for (const auto &s: res.spans)
+ {
+ std::string address = epee::string_tools::pad_string(s.remote_address, 24);
+ if (s.size == 0)
+ {
+ tools::success_msg_writer() << address << " " << s.nblocks << " (" << s.start_block_height << " - " << (s.start_block_height + s.nblocks - 1) << ") -";
+ }
+ else
+ {
+ tools::success_msg_writer() << address << " " << s.nblocks << " (" << s.start_block_height << " - " << (s.start_block_height + s.nblocks - 1) << ", " << (uint64_t)(s.size/1e3) << " kB) " << (unsigned)(s.rate/1e3) << " kB/s (" << s.speed/100.0f << ")";
+ }
+ }
+
+ return true;
+}
+
}// namespace daemonize
diff --git a/src/daemon/rpc_command_executor.h b/src/daemon/rpc_command_executor.h
index 3f551bd14..fc0b39654 100644
--- a/src/daemon/rpc_command_executor.h
+++ b/src/daemon/rpc_command_executor.h
@@ -42,10 +42,7 @@
#include "common/common_fwd.h"
#include "common/rpc_client.h"
-#include "misc_log_ex.h"
-#include "cryptonote_core/cryptonote_core.h"
-#include "cryptonote_protocol/cryptonote_protocol_handler.h"
-#include "p2p/net_node.h"
+#include "cryptonote_basic/cryptonote_basic.h"
#include "rpc/core_rpc_server.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
@@ -155,6 +152,8 @@ public:
bool update(const std::string &command);
bool relay_tx(const std::string &txid);
+
+ bool sync_info();
};
} // namespace daemonize
diff --git a/src/daemonizer/posix_daemonizer.inl b/src/daemonizer/posix_daemonizer.inl
index f8be15dda..506c7766f 100644
--- a/src/daemonizer/posix_daemonizer.inl
+++ b/src/daemonizer/posix_daemonizer.inl
@@ -43,6 +43,10 @@ namespace daemonizer
"detach"
, "Run as daemon"
};
+ const command_line::arg_descriptor<std::string> arg_pidfile = {
+ "pidfile"
+ , "File path to write the daemon's PID to (optional, requires --detach)"
+ };
const command_line::arg_descriptor<bool> arg_non_interactive = {
"non-interactive"
, "Run non-interactive"
@@ -55,6 +59,7 @@ namespace daemonizer
)
{
command_line::add_arg(normal_options, arg_detach);
+ command_line::add_arg(normal_options, arg_pidfile);
command_line::add_arg(normal_options, arg_non_interactive);
}
@@ -80,7 +85,12 @@ namespace daemonizer
if (command_line::has_arg(vm, arg_detach))
{
tools::success_msg_writer() << "Forking to background...";
- posix::fork();
+ std::string pidfile;
+ if (command_line::has_arg(vm, arg_pidfile))
+ {
+ pidfile = command_line::get_arg(vm, arg_pidfile);
+ }
+ posix::fork(pidfile);
auto daemon = executor.create_daemon(vm);
return daemon.run();
}
diff --git a/src/daemonizer/posix_fork.cpp b/src/daemonizer/posix_fork.cpp
index 949c0f593..4dff04f3f 100644
--- a/src/daemonizer/posix_fork.cpp
+++ b/src/daemonizer/posix_fork.cpp
@@ -14,18 +14,50 @@
#include <string>
#include <sys/stat.h>
+#ifndef TMPDIR
+#define TMPDIR "/tmp"
+#endif
+
namespace posix {
namespace {
- void quit(std::string const & message)
+ void quit(const std::string & message)
{
LOG_ERROR(message);
throw std::runtime_error(message);
}
}
-void fork()
+void fork(const std::string & pidfile)
{
+ // If a PID file is specified, we open the file here, because
+ // we can't report errors after the fork operation.
+ // When we fork, we close thise file in each of the parent
+ // processes.
+ // Only in the final child process do we write the PID to the
+ // file (and close it).
+ std::ofstream pidofs;
+ if (! pidfile.empty ())
+ {
+ int oldpid;
+ std::ifstream pidrifs;
+ pidrifs.open(pidfile, std::fstream::in);
+ if (! pidrifs.fail())
+ {
+ // Read the PID and send signal 0 to see if the process exists.
+ if (pidrifs >> oldpid && oldpid > 1 && kill(oldpid, 0) == 0)
+ {
+ quit("PID file " + pidfile + " already exists and the PID therein is valid");
+ }
+ pidrifs.close();
+ }
+
+ pidofs.open(pidfile, std::fstream::out | std::fstream::trunc);
+ if (pidofs.fail())
+ {
+ quit("Failed to open specified PID file for writing");
+ }
+ }
// Fork the process and have the parent exit. If the process was started
// from a shell, this returns control to the user. Forking a new process is
// also a prerequisite for the subsequent call to setsid().
@@ -34,7 +66,7 @@ void fork()
if (pid > 0)
{
// We're in the parent process and need to exit.
- //
+ pidofs.close();
// When the exit() function is used, the program terminates without
// invoking local variables' destructors. Only global variables are
// destroyed.
@@ -55,6 +87,7 @@ void fork()
{
if (pid > 0)
{
+ pidofs.close();
exit(0);
}
else
@@ -63,6 +96,13 @@ void fork()
}
}
+ if (! pidofs.fail())
+ {
+ int pid = ::getpid();
+ pidofs << pid << std::endl;
+ pidofs.close();
+ }
+
// Close the standard streams. This decouples the daemon from the terminal
// that started it.
close(0);
@@ -76,12 +116,16 @@ void fork()
}
// Send standard output to a log file.
- const char* output = "/tmp/bitmonero.daemon.stdout.stderr";
+ const char *tmpdir = getenv("TMPDIR");
+ if (!tmpdir)
+ tmpdir = TMPDIR;
+ std::string output = tmpdir;
+ output += "/bitmonero.daemon.stdout.stderr";
const int flags = O_WRONLY | O_CREAT | O_APPEND;
const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
- if (open(output, flags, mode) < 0)
+ if (open(output.c_str(), flags, mode) < 0)
{
- quit("Unable to open output file: " + std::string(output));
+ quit("Unable to open output file: " + output);
}
// Also send standard error to the same log file.
diff --git a/src/daemonizer/posix_fork.h b/src/daemonizer/posix_fork.h
index 459417d25..77ef4cb19 100644
--- a/src/daemonizer/posix_fork.h
+++ b/src/daemonizer/posix_fork.h
@@ -1,21 +1,21 @@
// Copyright (c) 2014-2017, 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
@@ -27,12 +27,13 @@
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once
+#include <string>
#ifndef WIN32
namespace posix {
-void fork();
+void fork(const std::string & pidfile);
}
diff --git a/src/debug_utilities/CMakeLists.txt b/src/debug_utilities/CMakeLists.txt
new file mode 100644
index 000000000..99198dc57
--- /dev/null
+++ b/src/debug_utilities/CMakeLists.txt
@@ -0,0 +1,73 @@
+# Copyright (c) 2014-2017, 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.
+
+set(cn_deserialize_sources
+ cn_deserialize.cpp
+ )
+
+monero_add_executable(cn_deserialize
+ ${cn_deserialize_sources}
+ ${cn_deserialize_private_headers})
+
+target_link_libraries(cn_deserialize
+ LINK_PRIVATE
+ cryptonote_core
+ blockchain_db
+ p2p
+ epee
+ ${CMAKE_THREAD_LIBS_INIT})
+
+add_dependencies(cn_deserialize
+ version)
+set_property(TARGET cn_deserialize
+ PROPERTY
+ OUTPUT_NAME "monero-utils-deserialize")
+
+
+set(object_sizes_sources
+ object_sizes.cpp
+ )
+
+monero_add_executable(object_sizes
+ ${object_sizes_sources}
+ ${object_sizes_private_headers})
+
+target_link_libraries(object_sizes
+ LINK_PRIVATE
+ cryptonote_core
+ blockchain_db
+ p2p
+ epee
+ ${CMAKE_THREAD_LIBS_INIT})
+
+add_dependencies(object_sizes
+ version)
+set_property(TARGET object_sizes
+ PROPERTY
+ OUTPUT_NAME "monero-utils-object-sizes")
+
diff --git a/src/blockchain_utilities/cn_deserialize.cpp b/src/debug_utilities/cn_deserialize.cpp
index b178e4e03..a1b569554 100644
--- a/src/blockchain_utilities/cn_deserialize.cpp
+++ b/src/debug_utilities/cn_deserialize.cpp
@@ -29,12 +29,11 @@
#include "cryptonote_basic/cryptonote_basic.h"
#include "cryptonote_basic/tx_extra.h"
#include "cryptonote_core/blockchain.h"
-#include "blockchain_utilities.h"
#include "common/command_line.h"
#include "version.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
-#define MONERO_DEFAULT_LOG_CATEGORY "bcutil"
+#define MONERO_DEFAULT_LOG_CATEGORY "debugtools.deserialize"
namespace po = boost::program_options;
using namespace epee;
diff --git a/src/debug_utilities/object_sizes.cpp b/src/debug_utilities/object_sizes.cpp
new file mode 100644
index 000000000..47ba5cf6c
--- /dev/null
+++ b/src/debug_utilities/object_sizes.cpp
@@ -0,0 +1,116 @@
+// Copyright (c) 2017, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include <map>
+#include "cryptonote_basic/cryptonote_basic.h"
+#include "cryptonote_basic/tx_extra.h"
+#include "cryptonote_core/blockchain.h"
+#include "p2p/p2p_protocol_defs.h"
+#include "p2p/connection_basic.hpp"
+#include "p2p/net_peerlist.h"
+#include "p2p/net_node.h"
+#include "cryptonote_protocol/cryptonote_protocol_handler.h"
+#include "blockchain_db/lmdb/db_lmdb.h"
+#include "wallet/wallet2.h"
+#include "wallet/api/wallet.h"
+#include "wallet/api/transaction_info.h"
+#include "wallet/api/transaction_history.h"
+#include "wallet/api/unsigned_transaction.h"
+#include "wallet/api/pending_transaction.h"
+
+#undef MONERO_DEFAULT_LOG_CATEGORY
+#define MONERO_DEFAULT_LOG_CATEGORY "debugtools.objectsizes"
+
+class size_logger
+{
+public:
+ ~size_logger()
+ {
+ for (const auto i: types)
+ std::cout << std::to_string(i.first) << "\t" << i.second << std::endl;
+ }
+ void add(const char *type, size_t size) { types.insert(std::make_pair(size, type)); }
+private:
+ std::multimap<size_t, const std::string> types;
+};
+#define SL(type) sl.add(#type, sizeof(type))
+
+int main(int argc, char* argv[])
+{
+ size_logger sl;
+
+ tools::sanitize_locale();
+
+ mlog_configure("", true);
+
+ SL(boost::thread);
+ SL(boost::asio::io_service);
+ SL(boost::asio::io_service::work);
+ SL(boost::asio::deadline_timer);
+
+ SL(cryptonote::DB_ERROR);
+ SL(cryptonote::mdb_txn_safe);
+ SL(cryptonote::mdb_threadinfo);
+
+ SL(cryptonote::block_header);
+ SL(cryptonote::block);
+ SL(cryptonote::transaction_prefix);
+ SL(cryptonote::transaction);
+
+ SL(cryptonote::txpool_tx_meta_t);
+
+ SL(epee::net_utils::network_address_base);
+ SL(epee::net_utils::ipv4_network_address);
+ SL(epee::net_utils::network_address);
+ SL(epee::net_utils::connection_context_base);
+ SL(epee::net_utils::connection_basic);
+
+ SL(nodetool::peerlist_entry);
+ SL(nodetool::anchor_peerlist_entry);
+ SL(nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<cryptonote::core>>);
+ SL(nodetool::p2p_connection_context_t<cryptonote::t_cryptonote_protocol_handler<cryptonote::core>::connection_context>);
+ SL(nodetool::network_address_old);
+
+ SL(tools::wallet2::transfer_details);
+ SL(tools::wallet2::payment_details);
+ SL(tools::wallet2::unconfirmed_transfer_details);
+ SL(tools::wallet2::confirmed_transfer_details);
+ SL(tools::wallet2::tx_construction_data);
+ SL(tools::wallet2::pending_tx);
+ SL(tools::wallet2::unsigned_tx_set);
+ SL(tools::wallet2::signed_tx_set);
+
+ SL(Monero::WalletImpl);
+ SL(Monero::AddressBookRow);
+ SL(Monero::TransactionInfoImpl);
+ SL(Monero::TransactionHistoryImpl);
+ SL(Monero::PendingTransactionImpl);
+ SL(Monero::UnsignedTransactionImpl);
+
+ return 0;
+}
diff --git a/src/mnemonics/CMakeLists.txt b/src/mnemonics/CMakeLists.txt
index 269cfe846..942b6eca3 100644
--- a/src/mnemonics/CMakeLists.txt
+++ b/src/mnemonics/CMakeLists.txt
@@ -45,7 +45,8 @@ set(mnemonics_private_headers
portuguese.h
russian.h
singleton.h
- spanish.h)
+ spanish.h
+ esperanto.h)
monero_private_headers(mnemonics
${mnemonics_private_headers})
diff --git a/src/mnemonics/electrum-words.cpp b/src/mnemonics/electrum-words.cpp
index 3b1dc53d7..2fe5d9985 100644
--- a/src/mnemonics/electrum-words.cpp
+++ b/src/mnemonics/electrum-words.cpp
@@ -61,6 +61,7 @@
#include "portuguese.h"
#include "japanese.h"
#include "russian.h"
+#include "esperanto.h"
#include "english_old.h"
#include "language_base.h"
#include "singleton.h"
@@ -95,6 +96,7 @@ namespace
Language::Singleton<Language::Portuguese>::instance(),
Language::Singleton<Language::Japanese>::instance(),
Language::Singleton<Language::Russian>::instance(),
+ Language::Singleton<Language::Esperanto>::instance(),
Language::Singleton<Language::EnglishOld>::instance()
});
Language::Base *fallback = NULL;
@@ -354,6 +356,10 @@ namespace crypto
{
language = Language::Singleton<Language::Chinese_Simplified>::instance();
}
+ else if (language_name == "Esperanto")
+ {
+ language = Language::Singleton<Language::Esperanto>::instance();
+ }
else
{
return false;
@@ -408,7 +414,8 @@ namespace crypto
Language::Singleton<Language::Portuguese>::instance(),
Language::Singleton<Language::Russian>::instance(),
Language::Singleton<Language::Japanese>::instance(),
- Language::Singleton<Language::Chinese_Simplified>::instance()
+ Language::Singleton<Language::Chinese_Simplified>::instance(),
+ Language::Singleton<Language::Esperanto>::instance()
});
for (std::vector<Language::Base*>::iterator it = language_instances.begin();
it != language_instances.end(); it++)
diff --git a/src/mnemonics/esperanto.h b/src/mnemonics/esperanto.h
new file mode 100644
index 000000000..8589f871e
--- /dev/null
+++ b/src/mnemonics/esperanto.h
@@ -0,0 +1,1695 @@
+// Copyright (c) 2014-2017, 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.
+
+/*!
+ * \file esperanto.h
+ *
+ * \brief New Esperanto word list and map.
+ */
+
+/*
+ * Word list authored by: dnaleor, Engelberg, ProkhorZ
+ * Sources:
+ * Baza Radikaro Oficiala
+ * Reta Vortaro (http://www.reta-vortaro.de/revo/)
+ * Esperanto Panorama - Esperanto-English Dictionary (http://www.esperanto-panorama.net/vortaro/eoen.htm)
+ * ESPDIC - Paul Denisowski (http://www.denisowski.org/Esperanto/ESPDIC/espdic.txt)
+ */
+
+#ifndef ESPERANTO_H
+#define ESPERANTO_H
+
+#include <vector>
+#include <unordered_map>
+#include "language_base.h"
+#include <string>
+
+/*!
+ * \namespace Language
+ * \brief Mnemonic language related namespace.
+ */
+namespace Language
+{
+ class Esperanto: public Base
+ {
+ public:
+ Esperanto(): Base("Esperanto", std::vector<std::string>({
+ "abako",
+ "abdiki",
+ "abelo",
+ "abituriento",
+ "ablativo",
+ "abnorma",
+ "abonantoj",
+ "abrikoto",
+ "absoluta",
+ "abunda",
+ "acetono",
+ "acida",
+ "adapti",
+ "adekvata",
+ "adheri",
+ "adicii",
+ "adjektivo",
+ "administri",
+ "adolesko",
+ "adreso",
+ "adstringa",
+ "adulto",
+ "advokato",
+ "adzo",
+ "aeroplano",
+ "aferulo",
+ "afgana",
+ "afiksi",
+ "aflaba",
+ "aforismo",
+ "afranki",
+ "aftozo",
+ "afusto",
+ "agavo",
+ "agento",
+ "agiti",
+ "aglo",
+ "agmaniero",
+ "agnoski",
+ "agordo",
+ "agrabla",
+ "agtipo",
+ "agutio",
+ "aikido",
+ "ailanto",
+ "aina",
+ "ajatolo",
+ "ajgenvaloro",
+ "ajlobulbo",
+ "ajnlitera",
+ "ajuto",
+ "ajzi",
+ "akademio",
+ "akcepti",
+ "akeo",
+ "akiri",
+ "aklamado",
+ "akmeo",
+ "akno",
+ "akompani",
+ "akrobato",
+ "akselo",
+ "aktiva",
+ "akurata",
+ "akvofalo",
+ "alarmo",
+ "albumo",
+ "alcedo",
+ "aldoni",
+ "aleo",
+ "alfabeto",
+ "algo",
+ "alhasti",
+ "aligatoro",
+ "alkoholo",
+ "almozo",
+ "alnomo",
+ "alojo",
+ "alpinisto",
+ "alrigardi",
+ "alskribi",
+ "alta",
+ "alumeto",
+ "alveni",
+ "alzaca",
+ "amaso",
+ "ambasado",
+ "amdeklaro",
+ "amebo",
+ "amfibio",
+ "amhara",
+ "amiko",
+ "amkanto",
+ "amletero",
+ "amnestio",
+ "amoranto",
+ "amplekso",
+ "amrakonto",
+ "amsterdama",
+ "amuzi",
+ "ananaso",
+ "androido",
+ "anekdoto",
+ "anfrakto",
+ "angulo",
+ "anheli",
+ "animo",
+ "anjono",
+ "ankro",
+ "anonci",
+ "anpriskribo",
+ "ansero",
+ "antikva",
+ "anuitato",
+ "aorto",
+ "aparta",
+ "aperti",
+ "apika",
+ "aplikado",
+ "apneo",
+ "apogi",
+ "aprobi",
+ "apsido",
+ "apterigo",
+ "apudesto",
+ "araneo",
+ "arbo",
+ "ardeco",
+ "aresti",
+ "argilo",
+ "aristokrato",
+ "arko",
+ "arlekeno",
+ "armi",
+ "arniko",
+ "aromo",
+ "arpio",
+ "arsenalo",
+ "artisto",
+ "aruba",
+ "arvorto",
+ "asaio",
+ "asbesto",
+ "ascendi",
+ "asekuri",
+ "asfalto",
+ "asisti",
+ "askalono",
+ "asocio",
+ "aspekti",
+ "astro",
+ "asulo",
+ "atakonto",
+ "atendi",
+ "atingi",
+ "atleto",
+ "atmosfero",
+ "atomo",
+ "atropino",
+ "atuto",
+ "avataro",
+ "aventuro",
+ "aviadilo",
+ "avokado",
+ "azaleo",
+ "azbuko",
+ "azenino",
+ "azilpetanto",
+ "azoto",
+ "azteka",
+ "babili",
+ "bacilo",
+ "badmintono",
+ "bagatelo",
+ "bahama",
+ "bajoneto",
+ "baki",
+ "balai",
+ "bambuo",
+ "bani",
+ "baobabo",
+ "bapti",
+ "baro",
+ "bastono",
+ "batilo",
+ "bavara",
+ "bazalto",
+ "beata",
+ "bebofono",
+ "bedo",
+ "begonio",
+ "behaviorismo",
+ "bejlo",
+ "bekero",
+ "belarto",
+ "bemolo",
+ "benko",
+ "bereto",
+ "besto",
+ "betulo",
+ "bevelo",
+ "bezoni",
+ "biaso",
+ "biblioteko",
+ "biciklo",
+ "bidaro",
+ "bieno",
+ "bifsteko",
+ "bigamiulo",
+ "bijekcio",
+ "bikino",
+ "bildo",
+ "bimetalismo",
+ "bindi",
+ "biografio",
+ "birdo",
+ "biskvito",
+ "bitlibro",
+ "bivako",
+ "bizara",
+ "bjalistoka",
+ "blanka",
+ "bleki",
+ "blinda",
+ "blovi",
+ "blua",
+ "boato",
+ "bobsledo",
+ "bocvanano",
+ "bodisatvo",
+ "bofratino",
+ "bogefratoj",
+ "bohema",
+ "boji",
+ "bokalo",
+ "boli",
+ "bombono",
+ "bona",
+ "bopatrino",
+ "bordo",
+ "bosko",
+ "botelo",
+ "bovido",
+ "brakpleno",
+ "bretaro",
+ "brikmuro",
+ "broso",
+ "brulema",
+ "bubalo",
+ "buctrapi",
+ "budo",
+ "bufedo",
+ "bugio",
+ "bujabeso",
+ "buklo",
+ "buldozo",
+ "bumerango",
+ "bunta",
+ "burokrataro",
+ "busbileto",
+ "butero",
+ "buzuko",
+ "caro",
+ "cebo",
+ "ceceo",
+ "cedro",
+ "cefalo",
+ "cejana",
+ "cekumo",
+ "celebri",
+ "cemento",
+ "cent",
+ "cepo",
+ "certa",
+ "cetera",
+ "cezio",
+ "ciano",
+ "cibeto",
+ "cico",
+ "cidro",
+ "cifero",
+ "cigaredo",
+ "ciklo",
+ "cilindro",
+ "cimbalo",
+ "cinamo",
+ "cipreso",
+ "cirkonstanco",
+ "cisterno",
+ "citrono",
+ "ciumi",
+ "civilizado",
+ "colo",
+ "congo",
+ "cunamo",
+ "cvana",
+ "dabi",
+ "daco",
+ "dadaismo",
+ "dafodilo",
+ "dago",
+ "daimio",
+ "dajmono",
+ "daktilo",
+ "dalio",
+ "damo",
+ "danki",
+ "darmo",
+ "datumoj",
+ "dazipo",
+ "deadmoni",
+ "debeto",
+ "decidi",
+ "dedukti",
+ "deerigi",
+ "defendi",
+ "degeli",
+ "dehaki",
+ "deirpunkto",
+ "deklaracio",
+ "delikata",
+ "demandi",
+ "dento",
+ "dependi",
+ "derivi",
+ "desegni",
+ "detrui",
+ "devi",
+ "deziri",
+ "dialogo",
+ "dicentro",
+ "didaktika",
+ "dieto",
+ "diferenci",
+ "digesti",
+ "diino",
+ "dikfingro",
+ "diligenta",
+ "dimensio",
+ "dinamo",
+ "diodo",
+ "diplomo",
+ "direkte",
+ "diskuti",
+ "diurno",
+ "diversa",
+ "dizajno",
+ "dobrogitaro",
+ "docento",
+ "dogano",
+ "dojeno",
+ "doktoro",
+ "dolori",
+ "domego",
+ "donaci",
+ "dopado",
+ "dormi",
+ "dosierujo",
+ "dotita",
+ "dozeno",
+ "drato",
+ "dresi",
+ "drinki",
+ "droni",
+ "druido",
+ "duaranga",
+ "dubi",
+ "ducent",
+ "dudek",
+ "duelo",
+ "dufoje",
+ "dugongo",
+ "duhufa",
+ "duilo",
+ "dujare",
+ "dukato",
+ "duloka",
+ "dumtempe",
+ "dungi",
+ "duobla",
+ "dupiedulo",
+ "dura",
+ "dusenca",
+ "dutaga",
+ "duuma",
+ "duvalvuloj",
+ "duzo",
+ "ebena",
+ "eblecoj",
+ "ebono",
+ "ebria",
+ "eburo",
+ "ecaro",
+ "ecigi",
+ "ecoj",
+ "edelvejso",
+ "editoro",
+ "edro",
+ "eduki",
+ "edzino",
+ "efektiva",
+ "efiki",
+ "efloreski",
+ "egala",
+ "egeco",
+ "egiptologo",
+ "eglefino",
+ "egoista",
+ "egreto",
+ "ejakuli",
+ "ejlo",
+ "ekarto",
+ "ekbruligi",
+ "ekceli",
+ "ekde",
+ "ekesti",
+ "ekfirmao",
+ "ekgliti",
+ "ekhavi",
+ "ekipi",
+ "ekkapti",
+ "eklezio",
+ "ekmalsati",
+ "ekonomio",
+ "ekpluvi",
+ "ekrano",
+ "ekster",
+ "ektiri",
+ "ekumeno",
+ "ekvilibro",
+ "ekzemplo",
+ "elasta",
+ "elbalai",
+ "elcento",
+ "eldoni",
+ "elektro",
+ "elfari",
+ "elgliti",
+ "elhaki",
+ "elipso",
+ "elkovi",
+ "ellasi",
+ "elmeti",
+ "elnutri",
+ "elokventa",
+ "elparoli",
+ "elrevigi",
+ "elstari",
+ "elteni",
+ "eluzita",
+ "elvoki",
+ "elzasa",
+ "emajlo",
+ "embaraso",
+ "emerito",
+ "emfazo",
+ "eminenta",
+ "emocio",
+ "empiria",
+ "emulsio",
+ "enarkivigi",
+ "enboteligi",
+ "enciklopedio",
+ "endorfino",
+ "energio",
+ "enfermi",
+ "engluti",
+ "enhavo",
+ "enigmo",
+ "enjekcio",
+ "enketi",
+ "enlanda",
+ "enmeti",
+ "enorma",
+ "enplanti",
+ "enradiki",
+ "enspezo",
+ "entrepreni",
+ "enui",
+ "envolvi",
+ "enzimo",
+ "eono",
+ "eosto",
+ "epitafo",
+ "epoko",
+ "epriskribebla",
+ "epsilono",
+ "erari",
+ "erbio",
+ "erco",
+ "erekti",
+ "ergonomia",
+ "erikejo",
+ "ermito",
+ "erotika",
+ "erpilo",
+ "erupcio",
+ "esameno",
+ "escepti",
+ "esenco",
+ "eskapi",
+ "esotera",
+ "esperi",
+ "estonto",
+ "etapo",
+ "etendi",
+ "etfingro",
+ "etikedo",
+ "etlitero",
+ "etmakleristo",
+ "etnika",
+ "etoso",
+ "etradio",
+ "etskala",
+ "etullernejo",
+ "evakui",
+ "evento",
+ "eviti",
+ "evolui",
+ "ezoko",
+ "fabriko",
+ "facila",
+ "fadeno",
+ "fagoto",
+ "fajro",
+ "fakto",
+ "fali",
+ "familio",
+ "fanatiko",
+ "farbo",
+ "fasko",
+ "fatala",
+ "favora",
+ "fazeolo",
+ "febro",
+ "federacio",
+ "feino",
+ "fekunda",
+ "felo",
+ "femuro",
+ "fenestro",
+ "fermi",
+ "festi",
+ "fetora",
+ "fezo",
+ "fiasko",
+ "fibro",
+ "fidela",
+ "fiera",
+ "fifama",
+ "figuro",
+ "fiherbo",
+ "fiinsekto",
+ "fiksa",
+ "filmo",
+ "fimensa",
+ "finalo",
+ "fiolo",
+ "fiparoli",
+ "firmao",
+ "fisko",
+ "fitingo",
+ "fiuzanto",
+ "fivorto",
+ "fiziko",
+ "fjordo",
+ "flago",
+ "flegi",
+ "flirti",
+ "floro",
+ "flugi",
+ "fobio",
+ "foceno",
+ "foirejo",
+ "fojfoje",
+ "fokuso",
+ "folio",
+ "fomenti",
+ "fonto",
+ "formulo",
+ "fosforo",
+ "fotografi",
+ "fratino",
+ "fremda",
+ "friti",
+ "frosto",
+ "frua",
+ "ftizo",
+ "fuelo",
+ "fugo",
+ "fuksia",
+ "fulmilo",
+ "fumanto",
+ "fundamento",
+ "fuorto",
+ "furioza",
+ "fusilo",
+ "futbalo",
+ "fuzio",
+ "gabardino",
+ "gado",
+ "gaela",
+ "gafo",
+ "gagato",
+ "gaja",
+ "gaki",
+ "galanta",
+ "gamao",
+ "ganto",
+ "gapulo",
+ "gardi",
+ "gasto",
+ "gavio",
+ "gazeto",
+ "geamantoj",
+ "gebani",
+ "geedzeco",
+ "gefratoj",
+ "geheno",
+ "gejsero",
+ "geko",
+ "gelateno",
+ "gemisto",
+ "geniulo",
+ "geografio",
+ "gepardo",
+ "geranio",
+ "gestolingvo",
+ "geto",
+ "geumo",
+ "gibono",
+ "giganta",
+ "gildo",
+ "gimnastiko",
+ "ginekologo",
+ "gipsi",
+ "girlando",
+ "gistfungo",
+ "gitaro",
+ "glazuro",
+ "glebo",
+ "gliti",
+ "globo",
+ "gluti",
+ "gnafalio",
+ "gnejso",
+ "gnomo",
+ "gnuo",
+ "gobio",
+ "godetio",
+ "goeleto",
+ "gojo",
+ "golfludejo",
+ "gombo",
+ "gondolo",
+ "gorilo",
+ "gospelo",
+ "gotika",
+ "granda",
+ "greno",
+ "griza",
+ "groto",
+ "grupo",
+ "guano",
+ "gubernatoro",
+ "gudrotuko",
+ "gufo",
+ "gujavo",
+ "guldeno",
+ "gumi",
+ "gupio",
+ "guruo",
+ "gusto",
+ "guto",
+ "guvernistino",
+ "gvardio",
+ "gverilo",
+ "gvidanto",
+ "habitato",
+ "hadito",
+ "hafnio",
+ "hagiografio",
+ "haitiano",
+ "hajlo",
+ "hakbloko",
+ "halti",
+ "hamstro",
+ "hangaro",
+ "hapalo",
+ "haro",
+ "hasta",
+ "hati",
+ "havebla",
+ "hazardo",
+ "hebrea",
+ "hedero",
+ "hegemonio",
+ "hejmo",
+ "hektaro",
+ "helpi",
+ "hemisfero",
+ "heni",
+ "hepato",
+ "herbo",
+ "hesa",
+ "heterogena",
+ "heziti",
+ "hiacinto",
+ "hibrida",
+ "hidrogeno",
+ "hieroglifo",
+ "higieno",
+ "hihii",
+ "hilumo",
+ "himno",
+ "hindino",
+ "hiperteksto",
+ "hirundo",
+ "historio",
+ "hobio",
+ "hojli",
+ "hokeo",
+ "hologramo",
+ "homido",
+ "honesta",
+ "hopi",
+ "horizonto",
+ "hospitalo",
+ "hotelo",
+ "huadi",
+ "hubo",
+ "hufumo",
+ "hugenoto",
+ "hukero",
+ "huligano",
+ "humana",
+ "hundo",
+ "huoj",
+ "hupilo",
+ "hurai",
+ "husaro",
+ "hutuo",
+ "huzo",
+ "iafoje",
+ "iagrade",
+ "iamaniere",
+ "iarelate",
+ "iaspeca",
+ "ibekso",
+ "ibiso",
+ "idaro",
+ "ideala",
+ "idiomo",
+ "idolo",
+ "iele",
+ "igluo",
+ "ignori",
+ "iguamo",
+ "igvano",
+ "ikono",
+ "iksodo",
+ "ikto",
+ "iliaflanke",
+ "ilkomputilo",
+ "ilobreto",
+ "ilremedo",
+ "ilumini",
+ "imagi",
+ "imitado",
+ "imperio",
+ "imuna",
+ "incidento",
+ "industrio",
+ "inerta",
+ "infano",
+ "ingenra",
+ "inhali",
+ "iniciati",
+ "injekti",
+ "inklino",
+ "inokuli",
+ "insekto",
+ "inteligenta",
+ "inundi",
+ "inviti",
+ "ioma",
+ "ionosfero",
+ "iperito",
+ "ipomeo",
+ "irana",
+ "irejo",
+ "irigacio",
+ "ironio",
+ "isato",
+ "islamo",
+ "istempo",
+ "itinero",
+ "itrio",
+ "iuloke",
+ "iumaniere",
+ "iutempe",
+ "izolita",
+ "jado",
+ "jaguaro",
+ "jakto",
+ "jama",
+ "januaro",
+ "japano",
+ "jarringo",
+ "jazo",
+ "jenoj",
+ "jesulo",
+ "jetavio",
+ "jezuito",
+ "jodli",
+ "joviala",
+ "juano",
+ "jubileo",
+ "judismo",
+ "jufto",
+ "juki",
+ "julio",
+ "juneca",
+ "jupo",
+ "juristo",
+ "juste",
+ "juvelo",
+ "kabineto",
+ "kadrato",
+ "kafo",
+ "kahelo",
+ "kajako",
+ "kakao",
+ "kalkuli",
+ "kampo",
+ "kanti",
+ "kapitalo",
+ "karaktero",
+ "kaserolo",
+ "katapulto",
+ "kaverna",
+ "kazino",
+ "kebabo",
+ "kefiro",
+ "keglo",
+ "kejlo",
+ "kekso",
+ "kelka",
+ "kemio",
+ "kerno",
+ "kesto",
+ "kiamaniere",
+ "kibuco",
+ "kidnapi",
+ "kielo",
+ "kikero",
+ "kilogramo",
+ "kimono",
+ "kinejo",
+ "kiosko",
+ "kirurgo",
+ "kisi",
+ "kitelo",
+ "kivio",
+ "klavaro",
+ "klerulo",
+ "klini",
+ "klopodi",
+ "klubo",
+ "knabo",
+ "knedi",
+ "koalo",
+ "kobalto",
+ "kodigi",
+ "kofro",
+ "kohera",
+ "koincidi",
+ "kojoto",
+ "kokoso",
+ "koloro",
+ "komenci",
+ "kontrakto",
+ "kopio",
+ "korekte",
+ "kosti",
+ "kotono",
+ "kovri",
+ "krajono",
+ "kredi",
+ "krii",
+ "krom",
+ "kruco",
+ "ksantino",
+ "ksenono",
+ "ksilofono",
+ "ksosa",
+ "kubuto",
+ "kudri",
+ "kuglo",
+ "kuiri",
+ "kuko",
+ "kulero",
+ "kumuluso",
+ "kuneco",
+ "kupro",
+ "kuri",
+ "kuseno",
+ "kutimo",
+ "kuvo",
+ "kuzino",
+ "kvalito",
+ "kverko",
+ "kvin",
+ "kvoto",
+ "labori",
+ "laculo",
+ "ladbotelo",
+ "lafo",
+ "laguno",
+ "laikino",
+ "laktobovino",
+ "lampolumo",
+ "landkarto",
+ "laosa",
+ "lapono",
+ "larmoguto",
+ "lastjare",
+ "latitudo",
+ "lavejo",
+ "lazanjo",
+ "leciono",
+ "ledosako",
+ "leganto",
+ "lekcio",
+ "lemura",
+ "lentuga",
+ "leopardo",
+ "leporo",
+ "lerni",
+ "lesivo",
+ "letero",
+ "levilo",
+ "lezi",
+ "liano",
+ "libera",
+ "liceo",
+ "lieno",
+ "lifto",
+ "ligilo",
+ "likvoro",
+ "lila",
+ "limono",
+ "lingvo",
+ "lipo",
+ "lirika",
+ "listo",
+ "literatura",
+ "liveri",
+ "lobio",
+ "logika",
+ "lojala",
+ "lokalo",
+ "longa",
+ "lordo",
+ "lotado",
+ "loza",
+ "luanto",
+ "lubriki",
+ "lucida",
+ "ludema",
+ "luigi",
+ "lukso",
+ "luli",
+ "lumbilda",
+ "lunde",
+ "lupago",
+ "lustro",
+ "lutilo",
+ "luzerno",
+ "maato",
+ "maceri",
+ "madono",
+ "mafiano",
+ "magazeno",
+ "mahometano",
+ "maizo",
+ "majstro",
+ "maketo",
+ "malgranda",
+ "mamo",
+ "mandareno",
+ "maorio",
+ "mapigi",
+ "marini",
+ "masko",
+ "mateno",
+ "mazuto",
+ "meandro",
+ "meblo",
+ "mecenato",
+ "medialo",
+ "mefito",
+ "megafono",
+ "mejlo",
+ "mekanika",
+ "melodia",
+ "membro",
+ "mendi",
+ "mergi",
+ "mespilo",
+ "metoda",
+ "mevo",
+ "mezuri",
+ "miaflanke",
+ "micelio",
+ "mielo",
+ "migdalo",
+ "mikrofilmo",
+ "militi",
+ "mimiko",
+ "mineralo",
+ "miopa",
+ "miri",
+ "mistera",
+ "mitralo",
+ "mizeri",
+ "mjelo",
+ "mnemoniko",
+ "mobilizi",
+ "mocio",
+ "moderna",
+ "mohajro",
+ "mokadi",
+ "molaro",
+ "momento",
+ "monero",
+ "mopso",
+ "mordi",
+ "moskito",
+ "motoro",
+ "movimento",
+ "mozaiko",
+ "mueli",
+ "mukozo",
+ "muldi",
+ "mumio",
+ "munti",
+ "muro",
+ "muskolo",
+ "mutacio",
+ "muzikisto",
+ "nabo",
+ "nacio",
+ "nadlo",
+ "nafto",
+ "naiva",
+ "najbaro",
+ "nanometro",
+ "napo",
+ "narciso",
+ "naski",
+ "naturo",
+ "navigi",
+ "naztruo",
+ "neatendite",
+ "nebulo",
+ "necesa",
+ "nedankinde",
+ "neebla",
+ "nefari",
+ "negoco",
+ "nehavi",
+ "neimagebla",
+ "nektaro",
+ "nelonga",
+ "nematura",
+ "nenia",
+ "neordinara",
+ "nepra",
+ "nervuro",
+ "nesto",
+ "nete",
+ "neulo",
+ "nevino",
+ "nifo",
+ "nigra",
+ "nihilisto",
+ "nikotino",
+ "nilono",
+ "nimfeo",
+ "nitrogeno",
+ "nivelo",
+ "nobla",
+ "nocio",
+ "nodozo",
+ "nokto",
+ "nomkarto",
+ "norda",
+ "nostalgio",
+ "notbloko",
+ "novico",
+ "nuanco",
+ "nuboza",
+ "nuda",
+ "nugato",
+ "nuklea",
+ "nuligi",
+ "numero",
+ "nuntempe",
+ "nupto",
+ "nura",
+ "nutri",
+ "oazo",
+ "obei",
+ "objekto",
+ "oblikva",
+ "obolo",
+ "observi",
+ "obtuza",
+ "obuso",
+ "oceano",
+ "odekolono",
+ "odori",
+ "oferti",
+ "oficiala",
+ "ofsajdo",
+ "ofte",
+ "ogivo",
+ "ogro",
+ "ojstredoj",
+ "okaze",
+ "okcidenta",
+ "okro",
+ "oksido",
+ "oktobro",
+ "okulo",
+ "oldulo",
+ "oleo",
+ "olivo",
+ "omaro",
+ "ombro",
+ "omego",
+ "omikrono",
+ "omleto",
+ "omnibuso",
+ "onagro",
+ "ondo",
+ "oneco",
+ "onidire",
+ "onklino",
+ "onlajna",
+ "onomatopeo",
+ "ontologio",
+ "opaka",
+ "operacii",
+ "opinii",
+ "oportuna",
+ "opresi",
+ "optimisto",
+ "oratoro",
+ "orbito",
+ "ordinara",
+ "orelo",
+ "orfino",
+ "organizi",
+ "orienta",
+ "orkestro",
+ "orlo",
+ "orminejo",
+ "ornami",
+ "ortangulo",
+ "orumi",
+ "oscedi",
+ "osmozo",
+ "ostocerbo",
+ "ovalo",
+ "ovingo",
+ "ovoblanko",
+ "ovri",
+ "ovulado",
+ "ozono",
+ "pacama",
+ "padeli",
+ "pafilo",
+ "pagigi",
+ "pajlo",
+ "paketo",
+ "palaco",
+ "pampelmo",
+ "pantalono",
+ "papero",
+ "paroli",
+ "pasejo",
+ "patro",
+ "pavimo",
+ "peco",
+ "pedalo",
+ "peklita",
+ "pelikano",
+ "pensiono",
+ "peplomo",
+ "pesilo",
+ "petanto",
+ "pezoforto",
+ "piano",
+ "picejo",
+ "piede",
+ "pigmento",
+ "pikema",
+ "pilkoludo",
+ "pimento",
+ "pinglo",
+ "pioniro",
+ "pipromento",
+ "pirato",
+ "pistolo",
+ "pitoreska",
+ "piulo",
+ "pivoti",
+ "pizango",
+ "planko",
+ "plektita",
+ "plibonigi",
+ "ploradi",
+ "plurlingva",
+ "pobo",
+ "podio",
+ "poeto",
+ "pogranda",
+ "pohora",
+ "pokalo",
+ "politekniko",
+ "pomarbo",
+ "ponevosto",
+ "populara",
+ "porcelana",
+ "postkompreno",
+ "poteto",
+ "poviga",
+ "pozitiva",
+ "prapatroj",
+ "precize",
+ "pridemandi",
+ "probable",
+ "pruntanto",
+ "psalmo",
+ "psikologio",
+ "psoriazo",
+ "pterido",
+ "publiko",
+ "pudro",
+ "pufo",
+ "pugnobato",
+ "pulovero",
+ "pumpi",
+ "punkto",
+ "pupo",
+ "pureo",
+ "puso",
+ "putrema",
+ "puzlo",
+ "rabate",
+ "racionala",
+ "radiko",
+ "rafinado",
+ "raguo",
+ "rajto",
+ "rakonti",
+ "ralio",
+ "rampi",
+ "rando",
+ "rapida",
+ "rastruma",
+ "ratifiki",
+ "raviolo",
+ "razeno",
+ "reakcio",
+ "rebildo",
+ "recepto",
+ "redakti",
+ "reenigi",
+ "reformi",
+ "regiono",
+ "rehavi",
+ "reinspekti",
+ "rejesi",
+ "reklamo",
+ "relativa",
+ "rememori",
+ "renkonti",
+ "reorganizado",
+ "reprezenti",
+ "respondi",
+ "retumilo",
+ "reuzebla",
+ "revidi",
+ "rezulti",
+ "rialo",
+ "ribeli",
+ "ricevi",
+ "ridiga",
+ "rifuginto",
+ "rigardi",
+ "rikolti",
+ "rilati",
+ "rimarki",
+ "rinocero",
+ "ripozi",
+ "riski",
+ "ritmo",
+ "rivero",
+ "rizokampo",
+ "roboto",
+ "rododendro",
+ "rojo",
+ "rokmuziko",
+ "rolvorto",
+ "romantika",
+ "ronroni",
+ "rosino",
+ "rotondo",
+ "rovero",
+ "rozeto",
+ "rubando",
+ "rudimenta",
+ "rufa",
+ "rugbeo",
+ "ruino",
+ "ruleto",
+ "rumoro",
+ "runo",
+ "rupio",
+ "rura",
+ "rustimuna",
+ "ruzulo",
+ "sabato",
+ "sadismo",
+ "safario",
+ "sagaca",
+ "sakfluto",
+ "salti",
+ "samtage",
+ "sandalo",
+ "sapejo",
+ "sarongo",
+ "satelito",
+ "savano",
+ "sbiro",
+ "sciado",
+ "seanco",
+ "sebo",
+ "sedativo",
+ "segligno",
+ "sekretario",
+ "selektiva",
+ "semajno",
+ "senpeza",
+ "separeo",
+ "servilo",
+ "sesangulo",
+ "setli",
+ "seurigi",
+ "severa",
+ "sezono",
+ "sfagno",
+ "sfero",
+ "sfinkso",
+ "siatempe",
+ "siblado",
+ "sidejo",
+ "siesto",
+ "sifono",
+ "signalo",
+ "siklo",
+ "silenti",
+ "simpla",
+ "sinjoro",
+ "siropo",
+ "sistemo",
+ "situacio",
+ "siverto",
+ "sizifa",
+ "skatolo",
+ "skemo",
+ "skianto",
+ "sklavo",
+ "skorpio",
+ "skribisto",
+ "skulpti",
+ "skvamo",
+ "slango",
+ "sledeto",
+ "sliparo",
+ "smeraldo",
+ "smirgi",
+ "smokingo",
+ "smuto",
+ "snoba",
+ "snufegi",
+ "sobra",
+ "sociano",
+ "sodakvo",
+ "sofo",
+ "soifi",
+ "sojlo",
+ "soklo",
+ "soldato",
+ "somero",
+ "sonilo",
+ "sopiri",
+ "sorto",
+ "soulo",
+ "soveto",
+ "sparkado",
+ "speciala",
+ "spiri",
+ "splito",
+ "sporto",
+ "sprita",
+ "spuro",
+ "stabila",
+ "stelfiguro",
+ "stimulo",
+ "stomako",
+ "strato",
+ "studanto",
+ "subgrupo",
+ "suden",
+ "suferanta",
+ "sugesti",
+ "suito",
+ "sukero",
+ "sulko",
+ "sume",
+ "sunlumo",
+ "super",
+ "surskribeto",
+ "suspekti",
+ "suturo",
+ "svati",
+ "svenfali",
+ "svingi",
+ "svopo",
+ "tabako",
+ "taglumo",
+ "tajloro",
+ "taksimetro",
+ "talento",
+ "tamen",
+ "tanko",
+ "taoismo",
+ "tapioko",
+ "tarifo",
+ "tasko",
+ "tatui",
+ "taverno",
+ "teatro",
+ "tedlaboro",
+ "tegmento",
+ "tehoro",
+ "teknika",
+ "telefono",
+ "tempo",
+ "tenisejo",
+ "teorie",
+ "teraso",
+ "testudo",
+ "tetablo",
+ "teujo",
+ "tezo",
+ "tialo",
+ "tibio",
+ "tielnomata",
+ "tifono",
+ "tigro",
+ "tikli",
+ "timida",
+ "tinkturo",
+ "tiom",
+ "tiparo",
+ "tirkesto",
+ "titolo",
+ "tiutempe",
+ "tizano",
+ "tobogano",
+ "tofeo",
+ "togo",
+ "toksa",
+ "tolerema",
+ "tombolo",
+ "tondri",
+ "topografio",
+ "tordeti",
+ "tosti",
+ "totalo",
+ "traduko",
+ "tredi",
+ "triangulo",
+ "tropika",
+ "trumpeto",
+ "tualeto",
+ "tubisto",
+ "tufgrebo",
+ "tuja",
+ "tukano",
+ "tulipo",
+ "tumulto",
+ "tunelo",
+ "turisto",
+ "tusi",
+ "tutmonda",
+ "tvisto",
+ "udono",
+ "uesto",
+ "ukazo",
+ "ukelelo",
+ "ulcero",
+ "ulmo",
+ "ultimato",
+ "ululi",
+ "umbiliko",
+ "unco",
+ "ungego",
+ "uniformo",
+ "unkti",
+ "unukolora",
+ "uragano",
+ "urbano",
+ "uretro",
+ "urino",
+ "ursido",
+ "uskleco",
+ "usonigi",
+ "utero",
+ "utila",
+ "utopia",
+ "uverturo",
+ "uzadi",
+ "uzeblo",
+ "uzino",
+ "uzkutimo",
+ "uzofini",
+ "uzurpi",
+ "uzvaloro",
+ "vadejo",
+ "vafleto",
+ "vagono",
+ "vahabismo",
+ "vajco",
+ "vakcino",
+ "valoro",
+ "vampiro",
+ "vangharoj",
+ "vaporo",
+ "varma",
+ "vasta",
+ "vato",
+ "vazaro",
+ "veaspekta",
+ "vedismo",
+ "vegetalo",
+ "vehiklo",
+ "vejno",
+ "vekita",
+ "velstango",
+ "vemieno",
+ "vendi",
+ "vepro",
+ "verando",
+ "vespero",
+ "veturi",
+ "veziko",
+ "viando",
+ "vibri",
+ "vico",
+ "videbla",
+ "vifio",
+ "vigla",
+ "viktimo",
+ "vila",
+ "vimeno",
+ "vintro",
+ "violo",
+ "vippuno",
+ "virtuala",
+ "viskoza",
+ "vitro",
+ "viveca",
+ "viziti",
+ "vobli",
+ "vodko",
+ "vojeto",
+ "vokegi",
+ "volbo",
+ "vomema",
+ "vono",
+ "vortaro",
+ "vosto",
+ "voti",
+ "vrako",
+ "vringi",
+ "vualo",
+ "vulkano",
+ "vundo",
+ "vuvuzelo",
+ "zamenhofa",
+ "zapi",
+ "zebro",
+ "zefiro",
+ "zeloto",
+ "zenismo",
+ "zeolito",
+ "zepelino",
+ "zeto",
+ "zigzagi",
+ "zinko",
+ "zipo",
+ "zirkonio",
+ "zodiako",
+ "zoeto",
+ "zombio",
+ "zono",
+ "zoologio",
+ "zorgi",
+ "zukino",
+ "zumilo",
+ }), 4)
+ {
+ populate_maps();
+ }
+ };
+}
+
+#endif
diff --git a/src/p2p/connection_basic.cpp b/src/p2p/connection_basic.cpp
index df34379e2..b95f36b99 100644
--- a/src/p2p/connection_basic.cpp
+++ b/src/p2p/connection_basic.cpp
@@ -53,8 +53,8 @@
#include "syncobj.h"
-#include "../../contrib/epee/include/net/net_utils_base.h"
-#include "../../contrib/epee/include/misc_log_ex.h"
+#include "net/net_utils_base.h"
+#include "misc_log_ex.h"
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/uuid/random_generator.hpp>
@@ -74,11 +74,11 @@
#include <boost/asio/basic_socket.hpp>
#include <boost/asio/ip/unicast.hpp>
-#include "../../contrib/epee/include/net/abstract_tcp_server2.h"
+#include "net/abstract_tcp_server2.h"
// TODO:
-#include "../../src/p2p/network_throttle-detail.hpp"
-#include "../../src/cryptonote_core/cryptonote_core.h"
+#include "network_throttle-detail.hpp"
+#include "cryptonote_core/cryptonote_core.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "net.p2p"
@@ -244,8 +244,7 @@ void connection_basic::sleep_before_packet(size_t packet_size, int phase, int q
delay *= 0.50;
if (delay > 0) {
long int ms = (long int)(delay * 1000);
- MDEBUG("Sleeping in " << __FUNCTION__ << " for " << ms << " ms before packet_size="<<packet_size); // debug sleep
- _dbg1("sleep in sleep_before_packet");
+ MTRACE("Sleeping in " << __FUNCTION__ << " for " << ms << " ms before packet_size="<<packet_size); // debug sleep
boost::this_thread::sleep(boost::posix_time::milliseconds( ms ) );
}
} while(delay > 0);
@@ -264,13 +263,13 @@ void connection_basic::set_start_time() {
void connection_basic::do_send_handler_write(const void* ptr , size_t cb ) {
sleep_before_packet(cb,1,-1);
- MDEBUG("handler_write (direct) - before ASIO write, for packet="<<cb<<" B (after sleep)");
+ MTRACE("handler_write (direct) - before ASIO write, for packet="<<cb<<" B (after sleep)");
set_start_time();
}
void connection_basic::do_send_handler_write_from_queue( const boost::system::error_code& e, size_t cb, int q_len ) {
sleep_before_packet(cb,2,q_len);
- MDEBUG("handler_write (after write, from queue="<<q_len<<") - before ASIO write, for packet="<<cb<<" B (after sleep)");
+ MTRACE("handler_write (after write, from queue="<<q_len<<") - before ASIO write, for packet="<<cb<<" B (after sleep)");
set_start_time();
}
diff --git a/src/p2p/connection_basic.hpp b/src/p2p/connection_basic.hpp
index bea2df1cd..16de469a7 100644
--- a/src/p2p/connection_basic.hpp
+++ b/src/p2p/connection_basic.hpp
@@ -59,8 +59,8 @@
#include <memory>
-#include "../../contrib/epee/include/net/net_utils_base.h"
-#include "../../contrib/epee/include/syncobj.h"
+#include "net/net_utils_base.h"
+#include "syncobj.h"
namespace epee
{
diff --git a/src/p2p/net_node.h b/src/p2p/net_node.h
index 8798a52e0..22ae5ec26 100644
--- a/src/p2p/net_node.h
+++ b/src/p2p/net_node.h
@@ -61,8 +61,11 @@ namespace nodetool
template<class base_type>
struct p2p_connection_context_t: base_type //t_payload_net_handler::connection_context //public net_utils::connection_context_base
{
+ p2p_connection_context_t(): peer_id(0), support_flags(0), m_in_timedsync(false) {}
+
peerid_type peer_id;
uint32_t support_flags;
+ bool m_in_timedsync;
};
template<class t_payload_net_handler>
@@ -186,6 +189,7 @@ namespace nodetool
virtual bool drop_connection(const epee::net_utils::connection_context_base& context);
virtual void request_callback(const epee::net_utils::connection_context_base& context);
virtual void for_each_connection(std::function<bool(typename t_payload_net_handler::connection_context&, peerid_type, uint32_t)> f);
+ virtual bool for_connection(const boost::uuids::uuid&, std::function<bool(typename t_payload_net_handler::connection_context&, peerid_type, uint32_t)> f);
virtual bool add_host_fail(const epee::net_utils::network_address &address);
//----------------- i_connection_filter --------------------------------------------------------
virtual bool is_remote_host_allowed(const epee::net_utils::network_address &address);
@@ -222,6 +226,7 @@ namespace nodetool
bool is_addr_recently_failed(const epee::net_utils::network_address& addr);
bool is_priority_node(const epee::net_utils::network_address& na);
std::set<std::string> get_seed_nodes(bool testnet) const;
+ bool connect_to_seed();
template <class Container>
bool connect_to_peerlist(const Container& peers);
diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl
index b23090c7d..889cfaf9d 100644
--- a/src/p2p/net_node.inl
+++ b/src/p2p/net_node.inl
@@ -159,7 +159,7 @@ namespace nodetool
}
catch (const std::exception &e)
{
- LOG_ERROR("Failed to load p2p config file, falling back to default config");
+ MWARNING("Failed to load p2p config file, falling back to default config");
m_peerlist = peerlist_manager(); // it was probably half clobbered by the failed load
make_default_config();
}
@@ -200,6 +200,14 @@ namespace nodetool
}
//-----------------------------------------------------------------------------------
template<class t_payload_net_handler>
+ bool node_server<t_payload_net_handler>::for_connection(const boost::uuids::uuid &connection_id, std::function<bool(typename t_payload_net_handler::connection_context&, peerid_type, uint32_t)> f)
+ {
+ return m_net_server.get_config_object().for_connection(connection_id, [&](p2p_connection_context& cntx){
+ return f(cntx, cntx.peer_id, cntx.support_flags);
+ });
+ }
+ //-----------------------------------------------------------------------------------
+ template<class t_payload_net_handler>
bool node_server<t_payload_net_handler>::is_remote_host_allowed(const epee::net_utils::network_address &address)
{
CRITICAL_REGION_LOCAL(m_blocked_hosts_lock);
@@ -388,7 +396,7 @@ namespace nodetool
}
else
{
- MERROR("IPv6 unsupported, skip '" << host << "' -> " << endpoint.address().to_v6().to_string(ec));
+ MWARNING("IPv6 unsupported, skip '" << host << "' -> " << endpoint.address().to_v6().to_string(ec));
throw std::runtime_error("IPv6 unsupported");
}
}
@@ -444,11 +452,11 @@ namespace nodetool
std::vector<std::vector<std::string>> dns_results;
dns_results.resize(m_seed_nodes_list.size());
- std::list<boost::thread*> dns_threads;
+ std::list<boost::thread> dns_threads;
uint64_t result_index = 0;
for (const std::string& addr_str : m_seed_nodes_list)
{
- boost::thread* th = new boost::thread([=, &dns_results, &addr_str]
+ boost::thread th = boost::thread([=, &dns_results, &addr_str]
{
MDEBUG("dns_threads[" << result_index << "] created for: " << addr_str);
// TODO: care about dnssec avail/valid
@@ -474,19 +482,19 @@ namespace nodetool
dns_results[result_index] = addr_list;
});
- dns_threads.push_back(th);
+ dns_threads.push_back(std::move(th));
++result_index;
}
MDEBUG("dns_threads created, now waiting for completion or timeout of " << CRYPTONOTE_DNS_TIMEOUT_MS << "ms");
boost::chrono::system_clock::time_point deadline = boost::chrono::system_clock::now() + boost::chrono::milliseconds(CRYPTONOTE_DNS_TIMEOUT_MS);
uint64_t i = 0;
- for (boost::thread* th : dns_threads)
+ for (boost::thread& th : dns_threads)
{
- if (! th->try_join_until(deadline))
+ if (! th.try_join_until(deadline))
{
MWARNING("dns_threads[" << i << "] timed out, sending interrupt");
- th->interrupt();
+ th.interrupt();
}
++i;
}
@@ -714,6 +722,14 @@ namespace nodetool
template<class t_payload_net_handler>
bool node_server<t_payload_net_handler>::send_stop_signal()
{
+ std::list<boost::uuids::uuid> connection_ids;
+ m_net_server.get_config_object().foreach_connection([&](const p2p_connection_context& cntxt) {
+ connection_ids.push_back(cntxt.m_connection_id);
+ return true;
+ });
+ for (const auto &connection_id: connection_ids)
+ m_net_server.get_config_object().close(connection_id);
+
m_payload_handler.stop();
m_net_server.send_stop_signal();
MDEBUG("[node] Stop signal sent");
@@ -740,19 +756,19 @@ namespace nodetool
if(code < 0)
{
- LOG_ERROR_CC(context, "COMMAND_HANDSHAKE invoke failed. (" << code << ", " << epee::levin::get_err_descr(code) << ")");
+ LOG_WARNING_CC(context, "COMMAND_HANDSHAKE invoke failed. (" << code << ", " << epee::levin::get_err_descr(code) << ")");
return;
}
if(rsp.node_data.network_id != m_network_id)
{
- LOG_ERROR_CC(context, "COMMAND_HANDSHAKE Failed, wrong network! (" << epee::string_tools::get_str_from_guid_a(rsp.node_data.network_id) << "), closing connection.");
+ LOG_WARNING_CC(context, "COMMAND_HANDSHAKE Failed, wrong network! (" << epee::string_tools::get_str_from_guid_a(rsp.node_data.network_id) << "), closing connection.");
return;
}
if(!handle_remote_peerlist(rsp.local_peerlist_new, rsp.node_data.local_time, context))
{
- LOG_ERROR_CC(context, "COMMAND_HANDSHAKE: failed to handle_remote_peerlist(...), closing connection.");
+ LOG_WARNING_CC(context, "COMMAND_HANDSHAKE: failed to handle_remote_peerlist(...), closing connection.");
add_host_fail(context.m_remote_address);
return;
}
@@ -761,7 +777,7 @@ namespace nodetool
{
if(!m_payload_handler.process_payload_sync_data(rsp.payload_data, context, true))
{
- LOG_ERROR_CC(context, "COMMAND_HANDSHAKE invoked, but process_payload_sync_data returned false, dropping connection.");
+ LOG_WARNING_CC(context, "COMMAND_HANDSHAKE invoked, but process_payload_sync_data returned false, dropping connection.");
hsh_result = false;
return;
}
@@ -789,7 +805,7 @@ namespace nodetool
if(!hsh_result)
{
- LOG_ERROR_CC(context_, "COMMAND_HANDSHAKE Failed");
+ LOG_WARNING_CC(context_, "COMMAND_HANDSHAKE Failed");
m_net_server.get_config_object().close(context_.m_connection_id);
}
else
@@ -815,7 +831,7 @@ namespace nodetool
context.m_in_timedsync = false;
if(code < 0)
{
- LOG_ERROR_CC(context, "COMMAND_TIMED_SYNC invoke failed. (" << code << ", " << epee::levin::get_err_descr(code) << ")");
+ LOG_WARNING_CC(context, "COMMAND_TIMED_SYNC invoke failed. (" << code << ", " << epee::levin::get_err_descr(code) << ")");
return;
}
@@ -832,7 +848,7 @@ namespace nodetool
if(!r)
{
- LOG_ERROR_CC(context_, "COMMAND_TIMED_SYNC Failed");
+ LOG_WARNING_CC(context_, "COMMAND_TIMED_SYNC Failed");
return false;
}
return true;
@@ -1061,7 +1077,7 @@ namespace nodetool
bool node_server<t_payload_net_handler>::make_new_connection_from_anchor_peerlist(const std::vector<anchor_peerlist_entry>& anchor_peerlist)
{
for (const auto& pe: anchor_peerlist) {
- _note("Considering connecting (out) to peer: " << pe.id << " " << pe.adr.str());
+ _note("Considering connecting (out) to peer: " << peerid_type(pe.id) << " " << pe.adr.str());
if(is_peer_used(pe)) {
_note("Peer is used");
@@ -1076,7 +1092,7 @@ namespace nodetool
continue;
}
- MDEBUG("Selected peer: " << pe.id << " " << pe.adr.str()
+ MDEBUG("Selected peer: " << peerid_to_string(pe.id) << " " << pe.adr.str()
<< "[peer_type=" << anchor
<< "] first_seen: " << epee::misc_utils::get_time_interval_string(time(NULL) - pe.first_seen));
@@ -1110,6 +1126,8 @@ namespace nodetool
size_t random_index;
if (use_white_list) {
+ local_peers_count = m_peerlist.get_white_peers_count();
+ max_random_index = std::min<uint64_t>(local_peers_count -1, 20);
random_index = get_random_index_with_fixed_probability(max_random_index);
} else {
random_index = crypto::rand<size_t>() % m_peerlist.get_gray_peers_count();
@@ -1127,7 +1145,7 @@ namespace nodetool
++try_count;
- _note("Considering connecting (out) to peer: " << pe.id << " " << pe.adr.str());
+ _note("Considering connecting (out) to peer: " << peerid_to_string(pe.id) << " " << pe.adr.str());
if(is_peer_used(pe)) {
_note("Peer is used");
@@ -1140,7 +1158,7 @@ namespace nodetool
if(is_addr_recently_failed(pe.adr))
continue;
- MDEBUG("Selected peer: " << pe.id << " " << pe.adr.str()
+ MDEBUG("Selected peer: " << peerid_to_string(pe.id) << " " << pe.adr.str()
<< "[peer_list=" << (use_white_list ? white : gray)
<< "] last_seen: " << (pe.last_seen ? epee::misc_utils::get_time_interval_string(time(NULL) - pe.last_seen) : "never"));
@@ -1155,14 +1173,11 @@ namespace nodetool
}
//-----------------------------------------------------------------------------------
template<class t_payload_net_handler>
- bool node_server<t_payload_net_handler>::connections_maker()
+ bool node_server<t_payload_net_handler>::connect_to_seed()
{
- if (!connect_to_peerlist(m_exclusive_peers)) return false;
-
- if (!m_exclusive_peers.empty()) return true;
+ if (m_seed_nodes.empty())
+ return true;
- if(!m_peerlist.get_white_peers_count() && m_seed_nodes.size())
- {
size_t try_count = 0;
size_t current_index = crypto::rand<size_t>()%m_seed_nodes.size();
bool fallback_nodes_added = false;
@@ -1195,6 +1210,21 @@ namespace nodetool
if(++current_index >= m_seed_nodes.size())
current_index = 0;
}
+ return true;
+ }
+ //-----------------------------------------------------------------------------------
+ template<class t_payload_net_handler>
+ bool node_server<t_payload_net_handler>::connections_maker()
+ {
+ if (!connect_to_peerlist(m_exclusive_peers)) return false;
+
+ if (!m_exclusive_peers.empty()) return true;
+
+ size_t start_conn_count = get_outgoing_connections_count();
+ if(!m_peerlist.get_white_peers_count() && m_seed_nodes.size())
+ {
+ if (!connect_to_seed())
+ return false;
}
if (!connect_to_peerlist(m_priority_peers)) return false;
@@ -1226,6 +1256,13 @@ namespace nodetool
}
}
+ if (start_conn_count == get_outgoing_connections_count() && start_conn_count < m_config.m_net_config.connections_count)
+ {
+ MINFO("Failed to connect to any, trying seeds");
+ if (!connect_to_seed())
+ return false;
+ }
+
return true;
}
//-----------------------------------------------------------------------------------
@@ -1367,25 +1404,25 @@ namespace nodetool
uint64_t time_delata = local_time > tr.time ? local_time - tr.time: tr.time - local_time;
if(time_delata > 24*60*60 )
{
- LOG_ERROR("check_trust failed to check time conditions, local_time=" << local_time << ", proof_time=" << tr.time);
+ MWARNING("check_trust failed to check time conditions, local_time=" << local_time << ", proof_time=" << tr.time);
return false;
}
if(m_last_stat_request_time >= tr.time )
{
- LOG_ERROR("check_trust failed to check time conditions, last_stat_request_time=" << m_last_stat_request_time << ", proof_time=" << tr.time);
+ MWARNING("check_trust failed to check time conditions, last_stat_request_time=" << m_last_stat_request_time << ", proof_time=" << tr.time);
return false;
}
if(m_config.m_peer_id != tr.peer_id)
{
- LOG_ERROR("check_trust failed: peer_id mismatch (passed " << tr.peer_id << ", expected " << m_config.m_peer_id<< ")");
+ MWARNING("check_trust failed: peer_id mismatch (passed " << tr.peer_id << ", expected " << m_config.m_peer_id<< ")");
return false;
}
crypto::public_key pk = AUTO_VAL_INIT(pk);
epee::string_tools::hex_to_pod(::config::P2P_REMOTE_DEBUG_TRUSTED_PUB_KEY, pk);
- crypto::hash h = tools::get_proof_of_trust_hash(tr);
+ crypto::hash h = get_proof_of_trust_hash(tr);
if(!crypto::check_signature(h, pk, tr.sign))
{
- LOG_ERROR("check_trust failed: sign check failed");
+ MWARNING("check_trust failed: sign check failed");
return false;
}
//update last request time
@@ -1546,13 +1583,13 @@ namespace nodetool
{
if(code <= 0)
{
- LOG_ERROR_CC(ping_context, "Failed to invoke COMMAND_PING to " << address.str() << "(" << code << ", " << epee::levin::get_err_descr(code) << ")");
+ LOG_WARNING_CC(ping_context, "Failed to invoke COMMAND_PING to " << address.str() << "(" << code << ", " << epee::levin::get_err_descr(code) << ")");
return;
}
if(rsp.status != PING_OK_RESPONSE_STATUS_TEXT || pr != rsp.peer_id)
{
- LOG_ERROR_CC(ping_context, "back ping invoke wrong response \"" << rsp.status << "\" from" << address.str() << ", hsh_peer_id=" << pr_ << ", rsp.peer_id=" << rsp.peer_id);
+ LOG_WARNING_CC(ping_context, "back ping invoke wrong response \"" << rsp.status << "\" from" << address.str() << ", hsh_peer_id=" << pr_ << ", rsp.peer_id=" << rsp.peer_id);
m_net_server.get_config_object().close(ping_context.m_connection_id);
return;
}
@@ -1562,7 +1599,7 @@ namespace nodetool
if(!inv_call_res)
{
- LOG_ERROR_CC(ping_context, "back ping invoke failed to " << address.str());
+ LOG_WARNING_CC(ping_context, "back ping invoke failed to " << address.str());
m_net_server.get_config_object().close(ping_context.m_connection_id);
return false;
}
@@ -1570,7 +1607,7 @@ namespace nodetool
});
if(!r)
{
- LOG_ERROR_CC(context, "Failed to call connect_async, network error.");
+ LOG_WARNING_CC(context, "Failed to call connect_async, network error.");
}
return r;
}
@@ -1589,7 +1626,7 @@ namespace nodetool
{
if(code < 0)
{
- LOG_ERROR_CC(context_, "COMMAND_REQUEST_SUPPORT_FLAGS invoke failed. (" << code << ", " << epee::levin::get_err_descr(code) << ")");
+ LOG_WARNING_CC(context_, "COMMAND_REQUEST_SUPPORT_FLAGS invoke failed. (" << code << ", " << epee::levin::get_err_descr(code) << ")");
return;
}
@@ -1606,7 +1643,7 @@ namespace nodetool
{
if(!m_payload_handler.process_payload_sync_data(arg.payload_data, context, false))
{
- LOG_ERROR_CC(context, "Failed to process_payload_sync_data(), dropping connection");
+ LOG_WARNING_CC(context, "Failed to process_payload_sync_data(), dropping connection");
drop_connection(context);
return 1;
}
@@ -1633,7 +1670,7 @@ namespace nodetool
if(!context.m_is_income)
{
- LOG_ERROR_CC(context, "COMMAND_HANDSHAKE came not from incoming connection");
+ LOG_WARNING_CC(context, "COMMAND_HANDSHAKE came not from incoming connection");
drop_connection(context);
add_host_fail(context.m_remote_address);
return 1;
@@ -1641,14 +1678,14 @@ namespace nodetool
if(context.peer_id)
{
- LOG_ERROR_CC(context, "COMMAND_HANDSHAKE came, but seems that connection already have associated peer_id (double COMMAND_HANDSHAKE?)");
+ LOG_WARNING_CC(context, "COMMAND_HANDSHAKE came, but seems that connection already have associated peer_id (double COMMAND_HANDSHAKE?)");
drop_connection(context);
return 1;
}
if(!m_payload_handler.process_payload_sync_data(arg.payload_data, context, true))
{
- LOG_ERROR_CC(context, "COMMAND_HANDSHAKE came, but process_payload_sync_data returned false, dropping connection.");
+ LOG_WARNING_CC(context, "COMMAND_HANDSHAKE came, but process_payload_sync_data returned false, dropping connection.");
drop_connection(context);
return 1;
}
@@ -1662,6 +1699,7 @@ namespace nodetool
//associate peer_id with this connection
context.peer_id = arg.node_data.peer_id;
+ context.m_in_timedsync = false;
if(arg.node_data.peer_id != m_config.m_peer_id && arg.node_data.my_port)
{
@@ -1757,6 +1795,8 @@ namespace nodetool
m_peerlist.remove_from_peer_anchor(na);
}
+ m_payload_handler.on_connection_close(context);
+
MINFO("["<< epee::net_utils::print_connection_context(context) << "] CLOSE CONNECTION");
}
@@ -1922,14 +1962,14 @@ namespace nodetool
if (!success) {
m_peerlist.remove_from_peer_gray(pe);
- LOG_PRINT_L2("PEER EVICTED FROM GRAY PEER LIST IP address: " << pe.adr.host_str() << " Peer ID: " << std::hex << pe.id);
+ LOG_PRINT_L2("PEER EVICTED FROM GRAY PEER LIST IP address: " << pe.adr.host_str() << " Peer ID: " << peerid_type(pe.id));
return true;
}
m_peerlist.set_peer_just_seen(pe.id, pe.adr);
- LOG_PRINT_L2("PEER PROMOTED TO WHITE PEER LIST IP address: " << pe.adr.host_str() << " Peer ID: " << std::hex << pe.id);
+ LOG_PRINT_L2("PEER PROMOTED TO WHITE PEER LIST IP address: " << pe.adr.host_str() << " Peer ID: " << peerid_type(pe.id));
return true;
}
diff --git a/src/p2p/net_node_common.h b/src/p2p/net_node_common.h
index 42de2655d..26bad7c72 100644
--- a/src/p2p/net_node_common.h
+++ b/src/p2p/net_node_common.h
@@ -51,6 +51,7 @@ namespace nodetool
virtual void request_callback(const epee::net_utils::connection_context_base& context)=0;
virtual uint64_t get_connections_count()=0;
virtual void for_each_connection(std::function<bool(t_connection_context&, peerid_type, uint32_t)> f)=0;
+ virtual bool for_connection(const boost::uuids::uuid&, std::function<bool(t_connection_context&, peerid_type, uint32_t)> f)=0;
virtual bool block_host(const epee::net_utils::network_address &address, time_t seconds = 0)=0;
virtual bool unblock_host(const epee::net_utils::network_address &address)=0;
virtual std::map<std::string, time_t> get_blocked_hosts()=0;
@@ -88,6 +89,10 @@ namespace nodetool
{
}
+ virtual bool for_connection(const boost::uuids::uuid&, std::function<bool(t_connection_context&,peerid_type,uint32_t)> f)
+ {
+ return false;
+ }
virtual uint64_t get_connections_count()
{
diff --git a/src/p2p/net_peerlist_boost_serialization.h b/src/p2p/net_peerlist_boost_serialization.h
index 43c5ea5f0..6ea2d48fd 100644
--- a/src/p2p/net_peerlist_boost_serialization.h
+++ b/src/p2p/net_peerlist_boost_serialization.h
@@ -59,6 +59,8 @@ namespace boost
{
a & na.m_ip;
a & na.m_port;
+ if (!typename Archive::is_saving())
+ na.init_ids();
}
diff --git a/src/p2p/network_throttle-detail.cpp b/src/p2p/network_throttle-detail.cpp
index 0747b6f36..1df48ee26 100644
--- a/src/p2p/network_throttle-detail.cpp
+++ b/src/p2p/network_throttle-detail.cpp
@@ -51,8 +51,8 @@
#include "syncobj.h"
-#include "../../contrib/epee/include/net/net_utils_base.h"
-#include "../../contrib/epee/include/misc_log_ex.h"
+#include "net/net_utils_base.h"
+#include "misc_log_ex.h"
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/uuid/random_generator.hpp>
@@ -71,10 +71,10 @@
#include <boost/asio/basic_socket.hpp>
#include <boost/asio/ip/unicast.hpp>
-#include "../../contrib/epee/include/net/abstract_tcp_server2.h"
+#include "net/abstract_tcp_server2.h"
// TODO:
-#include "../../src/p2p/network_throttle-detail.hpp"
+#include "network_throttle-detail.hpp"
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "net.throttle"
diff --git a/src/p2p/network_throttle-detail.hpp b/src/p2p/network_throttle-detail.hpp
index c514db450..27caa85d3 100644
--- a/src/p2p/network_throttle-detail.hpp
+++ b/src/p2p/network_throttle-detail.hpp
@@ -36,7 +36,7 @@
#ifndef INCLUDED_src_p2p_throttle_detail_hpp
#define INCLUDED_src_p2p_throttle_detail_hpp
-#include "../../src/p2p/network_throttle.hpp"
+#include "network_throttle.hpp"
namespace epee
{
diff --git a/src/p2p/network_throttle.cpp b/src/p2p/network_throttle.cpp
index 6d68f3286..74b20376d 100644
--- a/src/p2p/network_throttle.cpp
+++ b/src/p2p/network_throttle.cpp
@@ -54,7 +54,7 @@ Throttling work by:
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#include "../../src/p2p/network_throttle-detail.hpp"
+#include "network_throttle-detail.hpp"
namespace epee
{
@@ -77,28 +77,22 @@ int network_throttle_manager::xxx;
// ================================================================================================
// methods:
i_network_throttle & network_throttle_manager::get_global_throttle_in() {
- boost::call_once(m_once_get_global_throttle_in, [] { m_obj_get_global_throttle_in.reset(new network_throttle("in/all","<<< global-IN",10)); } );
- return * m_obj_get_global_throttle_in;
+ static network_throttle obj_get_global_throttle_in("in/all","<<< global-IN",10);
+ return obj_get_global_throttle_in;
}
-boost::once_flag network_throttle_manager::m_once_get_global_throttle_in;
-std::unique_ptr<i_network_throttle> network_throttle_manager::m_obj_get_global_throttle_in;
i_network_throttle & network_throttle_manager::get_global_throttle_inreq() {
- boost::call_once(m_once_get_global_throttle_inreq, [] { m_obj_get_global_throttle_inreq.reset(new network_throttle("inreq/all", "<== global-IN-REQ",10)); } );
- return * m_obj_get_global_throttle_inreq;
+ static network_throttle obj_get_global_throttle_inreq("inreq/all", "<== global-IN-REQ",10);
+ return obj_get_global_throttle_inreq;
}
-boost::once_flag network_throttle_manager::m_once_get_global_throttle_inreq;
-std::unique_ptr<i_network_throttle> network_throttle_manager::m_obj_get_global_throttle_inreq;
i_network_throttle & network_throttle_manager::get_global_throttle_out() {
- boost::call_once(m_once_get_global_throttle_out, [] { m_obj_get_global_throttle_out.reset(new network_throttle("out/all", ">>> global-OUT",10)); } );
- return * m_obj_get_global_throttle_out;
+ static network_throttle obj_get_global_throttle_out("out/all", ">>> global-OUT",10);
+ return obj_get_global_throttle_out;
}
-boost::once_flag network_throttle_manager::m_once_get_global_throttle_out;
-std::unique_ptr<i_network_throttle> network_throttle_manager::m_obj_get_global_throttle_out;
diff --git a/src/p2p/network_throttle.hpp b/src/p2p/network_throttle.hpp
index a747cdd71..9853df5e1 100644
--- a/src/p2p/network_throttle.hpp
+++ b/src/p2p/network_throttle.hpp
@@ -54,8 +54,8 @@
#include "syncobj.h"
-#include "../../contrib/epee/include/net/net_utils_base.h"
-#include "../../contrib/epee/include/misc_log_ex.h"
+#include "net/net_utils_base.h"
+#include "misc_log_ex.h"
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/uuid/random_generator.hpp>
@@ -111,13 +111,6 @@ class network_throttle_manager {
//protected:
public: // XXX
- // [[note1]]
- static boost::once_flag m_once_get_global_throttle_in;
- static boost::once_flag m_once_get_global_throttle_inreq; // [[note2]]
- static boost::once_flag m_once_get_global_throttle_out;
- static std::unique_ptr<i_network_throttle> m_obj_get_global_throttle_in;
- static std::unique_ptr<i_network_throttle> m_obj_get_global_throttle_inreq;
- static std::unique_ptr<i_network_throttle> m_obj_get_global_throttle_out;
static boost::mutex m_lock_get_global_throttle_in;
static boost::mutex m_lock_get_global_throttle_inreq;
diff --git a/src/p2p/p2p_protocol_defs.h b/src/p2p/p2p_protocol_defs.h
index a471211a6..f2b2cd1da 100644
--- a/src/p2p/p2p_protocol_defs.h
+++ b/src/p2p/p2p_protocol_defs.h
@@ -35,13 +35,22 @@
#include "net/net_utils_base.h"
#include "misc_language.h"
#include "cryptonote_config.h"
+#ifdef ALLOW_DEBUG_COMMANDS
#include "crypto/crypto.h"
+#endif
namespace nodetool
{
typedef boost::uuids::uuid uuid;
typedef uint64_t peerid_type;
+ static inline std::string peerid_to_string(peerid_type peer_id)
+ {
+ std::ostringstream s;
+ s << std::hex << peer_id;
+ return epee::string_tools::pad_string(s.str(), 16, '0', true);
+ }
+
#pragma pack (push, 1)
struct network_address_old
@@ -440,6 +449,14 @@ namespace nodetool
#endif
+ inline crypto::hash get_proof_of_trust_hash(const nodetool::proof_of_trust& pot)
+ {
+ std::string s;
+ s.append(reinterpret_cast<const char*>(&pot.peer_id), sizeof(pot.peer_id));
+ s.append(reinterpret_cast<const char*>(&pot.time), sizeof(pot.time));
+ return crypto::cn_fast_hash(s.data(), s.size());
+ }
+
}
diff --git a/src/rpc/CMakeLists.txt b/src/rpc/CMakeLists.txt
index f6037d7e3..b5c38b1a8 100644
--- a/src/rpc/CMakeLists.txt
+++ b/src/rpc/CMakeLists.txt
@@ -30,20 +30,61 @@ set(rpc_sources
core_rpc_server.cpp
rpc_args.cpp)
+set(daemon_messages_sources
+ message.cpp
+ daemon_messages.cpp)
+
+set(daemon_rpc_server_sources
+ daemon_handler.cpp
+ zmq_server.cpp)
+
+
set(rpc_headers
rpc_args.h)
-set(rpc_private_headers
+set(daemon_rpc_server_headers)
+
+
+set(rpc_daemon_private_headers
core_rpc_server.h
core_rpc_server_commands_defs.h
core_rpc_server_error_codes.h)
+set(daemon_messages_private_headers
+ message.h
+ daemon_messages.h)
+
+set(daemon_rpc_server_private_headers
+ message.h
+ daemon_messages.h
+ daemon_handler.h
+ rpc_handler.h
+ zmq_server.h)
+
+
monero_private_headers(rpc
${rpc_private_headers})
+
+monero_private_headers(daemon_rpc_server
+ ${daemon_rpc_server_private_headers})
+
+
monero_add_library(rpc
${rpc_sources}
${rpc_headers}
${rpc_private_headers})
+
+monero_add_library(daemon_messages
+ ${daemon_messages_sources}
+ ${daemon_messages_headers}
+ ${daemon_messages_private_headers})
+
+monero_add_library(daemon_rpc_server
+ ${daemon_rpc_server_sources}
+ ${daemon_rpc_server_headers}
+ ${daemon_rpc_server_private_headers})
+
+
target_link_libraries(rpc
PUBLIC
common
@@ -54,5 +95,32 @@ target_link_libraries(rpc
${Boost_THREAD_LIBRARY}
PRIVATE
${EXTRA_LIBRARIES})
+
+target_link_libraries(daemon_messages
+ LINK_PRIVATE
+ cryptonote_core
+ cryptonote_protocol
+ serialization
+ ${EXTRA_LIBRARIES})
+
+target_link_libraries(daemon_rpc_server
+ LINK_PRIVATE
+ cryptonote_core
+ cryptonote_protocol
+ daemon_messages
+ serialization
+ ${Boost_CHRONO_LIBRARY}
+ ${Boost_REGEX_LIBRARY}
+ ${Boost_SYSTEM_LIBRARY}
+ ${Boost_THREAD_LIBRARY}
+ ${ZMQ_LIB}
+ ${EXTRA_LIBRARIES})
+target_include_directories(daemon_rpc_server PUBLIC ${ZMQ_INCLUDE_PATH})
+target_include_directories(obj_daemon_rpc_server PUBLIC ${ZMQ_INCLUDE_PATH})
+
+
add_dependencies(rpc
version)
+
+add_dependencies(daemon_rpc_server
+ version)
diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp
index 97fe18696..1f7f4a1ff 100644
--- a/src/rpc/core_rpc_server.cpp
+++ b/src/rpc/core_rpc_server.cpp
@@ -137,7 +137,7 @@ namespace cryptonote
res.top_block_hash = string_tools::pod_to_hex(top_hash);
res.target_height = m_core.get_target_blockchain_height();
res.difficulty = m_core.get_blockchain_storage().get_difficulty_for_next_block();
- res.target = m_core.get_blockchain_storage().get_current_hard_fork_version() < 2 ? DIFFICULTY_TARGET_V1 : DIFFICULTY_TARGET_V2;
+ res.target = m_core.get_blockchain_storage().get_difficulty_target();
res.tx_count = m_core.get_blockchain_storage().get_total_transactions() - res.height; //without coinbase
res.tx_pool_size = m_core.get_pool_transactions_count();
res.alt_blocks_count = m_core.get_blockchain_storage().get_alternative_blocks_count();
@@ -205,14 +205,17 @@ namespace cryptonote
}
size_t txidx = 0;
ntxes += bd.second.size();
- for(const auto& t: bd.second)
+ for (std::list<cryptonote::blobdata>::iterator i = bd.second.begin(); i != bd.second.end(); ++i)
{
+ unpruned_size += i->size();
if (req.prune)
- res.blocks.back().txs.push_back(get_pruned_tx_blob(t));
+ res.blocks.back().txs.push_back(get_pruned_tx_blob(std::move(*i)));
else
- res.blocks.back().txs.push_back(t);
+ res.blocks.back().txs.push_back(std::move(*i));
+ i->clear();
+ i->shrink_to_fit();
pruned_size += res.blocks.back().txs.back().size();
- unpruned_size += t.size();
+
res.output_indices.back().indices.push_back(COMMAND_RPC_GET_BLOCKS_FAST::tx_output_indices());
bool r = m_core.get_tx_outputs_gindexs(b.tx_hashes[txidx++], res.output_indices.back().indices.back().indices);
if (!r)
@@ -227,6 +230,28 @@ namespace cryptonote
res.status = CORE_RPC_STATUS_OK;
return true;
}
+ bool core_rpc_server::on_get_alt_blocks_hashes(const COMMAND_RPC_GET_ALT_BLOCKS_HASHES::request& req, COMMAND_RPC_GET_ALT_BLOCKS_HASHES::response& res)
+ {
+ CHECK_CORE_BUSY();
+ std::list<block> blks;
+
+ if(!m_core.get_alternative_blocks(blks))
+ {
+ res.status = "Failed";
+ return false;
+ }
+
+ res.blks_hashes.reserve(blks.size());
+
+ for (auto const& blk: blks)
+ {
+ res.blks_hashes.push_back(epee::string_tools::pod_to_hex(get_block_hash(blk)));
+ }
+
+ MDEBUG("on_get_alt_blocks_hashes: " << blks.size() << " blocks " );
+ res.status = CORE_RPC_STATUS_OK;
+ return true;
+ }
//------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server::on_get_blocks_by_height(const COMMAND_RPC_GET_BLOCKS_BY_HEIGHT::request& req, COMMAND_RPC_GET_BLOCKS_BY_HEIGHT::response& res)
{
@@ -608,7 +633,7 @@ namespace cryptonote
}
res.status = "Failed";
if ((res.low_mixin = tvc.m_low_mixin))
- res.reason = "mixin too low";
+ res.reason = "ring size too small";
if ((res.double_spend = tvc.m_double_spend))
res.reason = "double spend";
if ((res.invalid_input = tvc.m_invalid_input))
@@ -1668,6 +1693,61 @@ namespace cryptonote
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
+ bool core_rpc_server::on_sync_info(const COMMAND_RPC_SYNC_INFO::request& req, COMMAND_RPC_SYNC_INFO::response& res, epee::json_rpc::error& error_resp)
+ {
+ if(!check_core_busy())
+ {
+ error_resp.code = CORE_RPC_ERROR_CODE_CORE_BUSY;
+ error_resp.message = "Core is busy.";
+ return false;
+ }
+
+ crypto::hash top_hash;
+ if (!m_core.get_blockchain_top(res.height, top_hash))
+ {
+ res.status = "Failed";
+ return false;
+ }
+ ++res.height; // turn top block height into blockchain height
+ res.target_height = m_core.get_target_blockchain_height();
+
+ for (const auto &c: m_p2p.get_payload_object().get_connections())
+ res.peers.push_back({c});
+ const cryptonote::block_queue &block_queue = m_p2p.get_payload_object().get_block_queue();
+ block_queue.foreach([&](const cryptonote::block_queue::span &span) {
+ uint32_t speed = (uint32_t)(100.0f * block_queue.get_speed(span.connection_id) + 0.5f);
+ std::string address = "";
+ for (const auto &c: m_p2p.get_payload_object().get_connections())
+ if (c.connection_id == span.connection_id)
+ address = c.address;
+ res.spans.push_back({span.start_block_height, span.nblocks, span.connection_id, (uint32_t)(span.rate + 0.5f), speed, span.size, address});
+ return true;
+ });
+
+ res.status = CORE_RPC_STATUS_OK;
+ return true;
+ }
+ //------------------------------------------------------------------------------------------------------------------------------
+ bool core_rpc_server::on_get_txpool_backlog(const COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG::request& req, COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG::response& res, epee::json_rpc::error& error_resp)
+ {
+ if(!check_core_busy())
+ {
+ error_resp.code = CORE_RPC_ERROR_CODE_CORE_BUSY;
+ error_resp.message = "Core is busy.";
+ return false;
+ }
+
+ if (!m_core.get_txpool_backlog(res.backlog))
+ {
+ error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR;
+ error_resp.message = "Failed to get txpool backlog";
+ return false;
+ }
+
+ res.status = CORE_RPC_STATUS_OK;
+ return true;
+ }
+ //------------------------------------------------------------------------------------------------------------------------------
const command_line::arg_descriptor<std::string> core_rpc_server::arg_rpc_bind_port = {
"rpc-bind-port"
diff --git a/src/rpc/core_rpc_server.h b/src/rpc/core_rpc_server.h
index 44ac6f07a..dbbe07972 100644
--- a/src/rpc/core_rpc_server.h
+++ b/src/rpc/core_rpc_server.h
@@ -81,6 +81,7 @@ namespace cryptonote
MAP_URI_AUTO_BIN2("/get_outs.bin", on_get_outs_bin, COMMAND_RPC_GET_OUTPUTS_BIN)
MAP_URI_AUTO_BIN2("/getrandom_rctouts.bin", on_get_random_rct_outs, COMMAND_RPC_GET_RANDOM_RCT_OUTPUTS)
MAP_URI_AUTO_JON2("/gettransactions", on_get_transactions, COMMAND_RPC_GET_TRANSACTIONS)
+ MAP_URI_AUTO_JON2("/get_alt_blocks_hashes", on_get_alt_blocks_hashes, COMMAND_RPC_GET_ALT_BLOCKS_HASHES)
MAP_URI_AUTO_JON2("/is_key_image_spent", on_is_key_image_spent, COMMAND_RPC_IS_KEY_IMAGE_SPENT)
MAP_URI_AUTO_JON2("/sendrawtransaction", on_send_raw_tx, COMMAND_RPC_SEND_RAW_TX)
MAP_URI_AUTO_JON2_IF("/start_mining", on_start_mining, COMMAND_RPC_START_MINING, !m_restricted)
@@ -123,11 +124,14 @@ namespace cryptonote
MAP_JON_RPC_WE("get_fee_estimate", on_get_per_kb_fee_estimate, COMMAND_RPC_GET_PER_KB_FEE_ESTIMATE)
MAP_JON_RPC_WE_IF("get_alternate_chains",on_get_alternate_chains, COMMAND_RPC_GET_ALTERNATE_CHAINS, !m_restricted)
MAP_JON_RPC_WE_IF("relay_tx", on_relay_tx, COMMAND_RPC_RELAY_TX, !m_restricted)
+ MAP_JON_RPC_WE_IF("sync_info", on_sync_info, COMMAND_RPC_SYNC_INFO, !m_restricted)
+ MAP_JON_RPC_WE("get_txpool_backlog", on_get_txpool_backlog, COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG)
END_JSON_RPC_MAP()
END_URI_MAP2()
bool on_get_height(const COMMAND_RPC_GET_HEIGHT::request& req, COMMAND_RPC_GET_HEIGHT::response& res);
bool on_get_blocks(const COMMAND_RPC_GET_BLOCKS_FAST::request& req, COMMAND_RPC_GET_BLOCKS_FAST::response& res);
+ bool on_get_alt_blocks_hashes(const COMMAND_RPC_GET_ALT_BLOCKS_HASHES::request& req, COMMAND_RPC_GET_ALT_BLOCKS_HASHES::response& res);
bool on_get_blocks_by_height(const COMMAND_RPC_GET_BLOCKS_BY_HEIGHT::request& req, COMMAND_RPC_GET_BLOCKS_BY_HEIGHT::response& res);
bool on_get_hashes(const COMMAND_RPC_GET_HASHES_FAST::request& req, COMMAND_RPC_GET_HASHES_FAST::response& res);
bool on_get_transactions(const COMMAND_RPC_GET_TRANSACTIONS::request& req, COMMAND_RPC_GET_TRANSACTIONS::response& res);
@@ -178,6 +182,8 @@ namespace cryptonote
bool on_get_per_kb_fee_estimate(const COMMAND_RPC_GET_PER_KB_FEE_ESTIMATE::request& req, COMMAND_RPC_GET_PER_KB_FEE_ESTIMATE::response& res, epee::json_rpc::error& error_resp);
bool on_get_alternate_chains(const COMMAND_RPC_GET_ALTERNATE_CHAINS::request& req, COMMAND_RPC_GET_ALTERNATE_CHAINS::response& res, epee::json_rpc::error& error_resp);
bool on_relay_tx(const COMMAND_RPC_RELAY_TX::request& req, COMMAND_RPC_RELAY_TX::response& res, epee::json_rpc::error& error_resp);
+ bool on_sync_info(const COMMAND_RPC_SYNC_INFO::request& req, COMMAND_RPC_SYNC_INFO::response& res, epee::json_rpc::error& error_resp);
+ bool on_get_txpool_backlog(const COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG::request& req, COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG::response& res, epee::json_rpc::error& error_resp);
//-----------------------
private:
diff --git a/src/rpc/core_rpc_server_commands_defs.h b/src/rpc/core_rpc_server_commands_defs.h
index 7a1f5a963..88327dd75 100644
--- a/src/rpc/core_rpc_server_commands_defs.h
+++ b/src/rpc/core_rpc_server_commands_defs.h
@@ -49,7 +49,7 @@ namespace cryptonote
// advance which version they will stop working with
// Don't go over 32767 for any of these
#define CORE_RPC_VERSION_MAJOR 1
-#define CORE_RPC_VERSION_MINOR 12
+#define CORE_RPC_VERSION_MINOR 13
#define MAKE_CORE_RPC_VERSION(major,minor) (((major)<<16)|(minor))
#define CORE_RPC_VERSION MAKE_CORE_RPC_VERSION(CORE_RPC_VERSION_MAJOR, CORE_RPC_VERSION_MINOR)
@@ -146,6 +146,25 @@ namespace cryptonote
};
};
+ struct COMMAND_RPC_GET_ALT_BLOCKS_HASHES
+ {
+ struct request
+ {
+ BEGIN_KV_SERIALIZE_MAP()
+ END_KV_SERIALIZE_MAP()
+ };
+
+ struct response
+ {
+ std::vector<std::string> blks_hashes;
+ std::string status;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(blks_hashes)
+ KV_SERIALIZE(status)
+ END_KV_SERIALIZE_MAP()
+ };
+ };
struct COMMAND_RPC_GET_HASHES_FAST
{
@@ -1052,6 +1071,33 @@ namespace cryptonote
};
};
+ struct tx_backlog_entry
+ {
+ uint64_t blob_size;
+ uint64_t fee;
+ uint64_t time_in_pool;
+ };
+
+ struct COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG
+ {
+ struct request
+ {
+ BEGIN_KV_SERIALIZE_MAP()
+ END_KV_SERIALIZE_MAP()
+ };
+
+ struct response
+ {
+ std::string status;
+ std::vector<tx_backlog_entry> backlog;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(status)
+ KV_SERIALIZE_CONTAINER_POD_AS_BLOB(backlog)
+ END_KV_SERIALIZE_MAP()
+ };
+ };
+
struct txpool_histo
{
uint32_t txs;
@@ -1068,6 +1114,7 @@ namespace cryptonote
uint64_t bytes_total;
uint32_t bytes_min;
uint32_t bytes_max;
+ uint32_t bytes_med;
uint64_t fee_total;
uint64_t oldest;
uint32_t txs_total;
@@ -1081,6 +1128,7 @@ namespace cryptonote
KV_SERIALIZE(bytes_total)
KV_SERIALIZE(bytes_min)
KV_SERIALIZE(bytes_max)
+ KV_SERIALIZE(bytes_med)
KV_SERIALIZE(fee_total)
KV_SERIALIZE(oldest)
KV_SERIALIZE(txs_total)
@@ -1587,4 +1635,60 @@ namespace cryptonote
END_KV_SERIALIZE_MAP()
};
};
+
+ struct COMMAND_RPC_SYNC_INFO
+ {
+ struct request
+ {
+ BEGIN_KV_SERIALIZE_MAP()
+ END_KV_SERIALIZE_MAP()
+ };
+
+ struct peer
+ {
+ connection_info info;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(info)
+ END_KV_SERIALIZE_MAP()
+ };
+
+ struct span
+ {
+ uint64_t start_block_height;
+ uint64_t nblocks;
+ boost::uuids::uuid connection_id;
+ uint32_t rate;
+ uint32_t speed;
+ uint64_t size;
+ std::string remote_address;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(start_block_height)
+ KV_SERIALIZE(nblocks)
+ KV_SERIALIZE_VAL_POD_AS_BLOB(connection_id)
+ KV_SERIALIZE(rate)
+ KV_SERIALIZE(speed)
+ KV_SERIALIZE(size)
+ KV_SERIALIZE(remote_address)
+ END_KV_SERIALIZE_MAP()
+ };
+
+ struct response
+ {
+ std::string status;
+ uint64_t height;
+ uint64_t target_height;
+ std::list<peer> peers;
+ std::list<span> spans;
+
+ BEGIN_KV_SERIALIZE_MAP()
+ KV_SERIALIZE(status)
+ KV_SERIALIZE(height)
+ KV_SERIALIZE(target_height)
+ KV_SERIALIZE(peers)
+ KV_SERIALIZE(spans)
+ END_KV_SERIALIZE_MAP()
+ };
+ };
}
diff --git a/src/rpc/daemon_handler.cpp b/src/rpc/daemon_handler.cpp
new file mode 100644
index 000000000..53eeb5e76
--- /dev/null
+++ b/src/rpc/daemon_handler.cpp
@@ -0,0 +1,887 @@
+// Copyright (c) 2017, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "daemon_handler.h"
+
+// likely included by daemon_handler.h's includes,
+// but including here for clarity
+#include "cryptonote_core/cryptonote_core.h"
+#include "cryptonote_basic/cryptonote_format_utils.h"
+#include "cryptonote_protocol/blobdatatype.h"
+#include "ringct/rctSigs.h"
+
+namespace cryptonote
+{
+
+namespace rpc
+{
+
+ void DaemonHandler::handle(const GetHeight::Request& req, GetHeight::Response& res)
+ {
+ res.height = m_core.get_current_blockchain_height();
+
+ res.status = Message::STATUS_OK;
+ }
+
+ void DaemonHandler::handle(const GetBlocksFast::Request& req, GetBlocksFast::Response& res)
+ {
+ std::list<std::pair<blobdata, std::list<blobdata> > > blocks;
+
+ if(!m_core.find_blockchain_supplement(req.start_height, req.block_ids, blocks, res.current_height, res.start_height, COMMAND_RPC_GET_BLOCKS_FAST_MAX_COUNT))
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "core::find_blockchain_supplement() returned false";
+ return;
+ }
+
+ res.blocks.resize(blocks.size());
+ res.output_indices.resize(blocks.size());
+
+ //TODO: really need to switch uses of std::list to std::vector unless
+ // it's a huge performance concern
+
+ auto it = blocks.begin();
+
+ uint64_t block_count = 0;
+ while (it != blocks.end())
+ {
+ cryptonote::rpc::block_with_transactions& bwt = res.blocks[block_count];
+
+ if (!parse_and_validate_block_from_blob(it->first, bwt.block))
+ {
+ res.blocks.clear();
+ res.output_indices.clear();
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "failed retrieving a requested block";
+ return;
+ }
+
+ if (it->second.size() != bwt.block.tx_hashes.size())
+ {
+ res.blocks.clear();
+ res.output_indices.clear();
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "incorrect number of transactions retrieved for block";
+ return;
+ }
+ std::list<transaction> txs;
+ for (const auto& blob : it->second)
+ {
+ txs.resize(txs.size() + 1);
+ if (!parse_and_validate_tx_from_blob(blob, txs.back()))
+ {
+ res.blocks.clear();
+ res.output_indices.clear();
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "failed retrieving a requested transaction";
+ return;
+ }
+ }
+
+ cryptonote::rpc::block_output_indices& indices = res.output_indices[block_count];
+
+ // miner tx output indices
+ {
+ cryptonote::rpc::tx_output_indices tx_indices;
+ bool r = m_core.get_tx_outputs_gindexs(get_transaction_hash(bwt.block.miner_tx), tx_indices);
+ if (!r)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "core::get_tx_outputs_gindexs() returned false";
+ return;
+ }
+ indices.push_back(tx_indices);
+ }
+
+ // assume each block returned is returned with all its transactions
+ // in the correct order.
+ auto tx_it = txs.begin();
+ for (const crypto::hash& h : bwt.block.tx_hashes)
+ {
+ bwt.transactions.emplace(h, *tx_it);
+ tx_it++;
+
+ cryptonote::rpc::tx_output_indices tx_indices;
+ bool r = m_core.get_tx_outputs_gindexs(h, tx_indices);
+ if (!r)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "core::get_tx_outputs_gindexs() returned false";
+ return;
+ }
+
+ indices.push_back(tx_indices);
+ }
+
+ it++;
+ block_count++;
+ }
+
+ res.status = Message::STATUS_OK;
+ }
+
+ void DaemonHandler::handle(const GetHashesFast::Request& req, GetHashesFast::Response& res)
+ {
+ res.start_height = req.start_height;
+
+ auto& chain = m_core.get_blockchain_storage();
+
+ if (!chain.find_blockchain_supplement(req.known_hashes, res.hashes, res.start_height, res.current_height))
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "Blockchain::find_blockchain_supplement() returned false";
+ return;
+ }
+
+ res.status = Message::STATUS_OK;
+ }
+
+ void DaemonHandler::handle(const GetTransactions::Request& req, GetTransactions::Response& res)
+ {
+ std::list<cryptonote::transaction> found_txs;
+ std::list<crypto::hash> missed_hashes;
+
+ bool r = m_core.get_transactions(req.tx_hashes, found_txs, missed_hashes);
+
+ // TODO: consider fixing core::get_transactions to not hide exceptions
+ if (!r)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "core::get_transactions() returned false (exception caught there)";
+ return;
+ }
+
+ size_t num_found = found_txs.size();
+
+ // std::list is annoying
+ std::vector<cryptonote::transaction> found_txs_vec
+ {
+ std::make_move_iterator(std::begin(found_txs)),
+ std::make_move_iterator(std::end(found_txs))
+ };
+
+ std::vector<crypto::hash> missed_vec
+ {
+ std::make_move_iterator(std::begin(missed_hashes)),
+ std::make_move_iterator(std::end(missed_hashes))
+ };
+
+ std::vector<uint64_t> heights(num_found);
+ std::vector<bool> in_pool(num_found, false);
+ std::vector<crypto::hash> found_hashes(num_found);
+
+ for (size_t i=0; i < num_found; i++)
+ {
+ found_hashes[i] = get_transaction_hash(found_txs_vec[i]);
+ heights[i] = m_core.get_blockchain_storage().get_db().get_tx_block_height(found_hashes[i]);
+ }
+
+ // if any missing from blockchain, check in tx pool
+ if (!missed_vec.empty())
+ {
+ std::list<cryptonote::transaction> pool_txs;
+
+ m_core.get_pool_transactions(pool_txs);
+
+ for (const auto& tx : pool_txs)
+ {
+ crypto::hash h = get_transaction_hash(tx);
+
+ auto itr = std::find(missed_vec.begin(), missed_vec.end(), h);
+
+ if (itr != missed_vec.end())
+ {
+ found_hashes.push_back(h);
+ found_txs_vec.push_back(tx);
+ heights.push_back(std::numeric_limits<uint64_t>::max());
+ in_pool.push_back(true);
+ missed_vec.erase(itr);
+ }
+ }
+ }
+
+ for (size_t i=0; i < found_hashes.size(); i++)
+ {
+ cryptonote::rpc::transaction_info info;
+ info.height = heights[i];
+ info.in_pool = in_pool[i];
+ info.transaction = std::move(found_txs_vec[i]);
+
+ res.txs.emplace(found_hashes[i], std::move(info));
+ }
+
+ res.missed_hashes = std::move(missed_vec);
+ res.status = Message::STATUS_OK;
+ }
+
+ void DaemonHandler::handle(const KeyImagesSpent::Request& req, KeyImagesSpent::Response& res)
+ {
+ res.spent_status.resize(req.key_images.size(), KeyImagesSpent::STATUS::UNSPENT);
+
+ std::vector<bool> chain_spent_status;
+ std::vector<bool> pool_spent_status;
+
+ m_core.are_key_images_spent(req.key_images, chain_spent_status);
+ m_core.are_key_images_spent_in_pool(req.key_images, pool_spent_status);
+
+ if ((chain_spent_status.size() != req.key_images.size()) || (pool_spent_status.size() != req.key_images.size()))
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "tx_pool::have_key_images_as_spent() gave vectors of wrong size(s).";
+ return;
+ }
+
+ for(size_t i=0; i < req.key_images.size(); i++)
+ {
+ if ( chain_spent_status[i] )
+ {
+ res.spent_status[i] = KeyImagesSpent::STATUS::SPENT_IN_BLOCKCHAIN;
+ }
+ else if ( pool_spent_status[i] )
+ {
+ res.spent_status[i] = KeyImagesSpent::STATUS::SPENT_IN_POOL;
+ }
+ }
+
+ res.status = Message::STATUS_OK;
+ }
+
+ void DaemonHandler::handle(const GetTxGlobalOutputIndices::Request& req, GetTxGlobalOutputIndices::Response& res)
+ {
+ if (!m_core.get_tx_outputs_gindexs(req.tx_hash, res.output_indices))
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "core::get_tx_outputs_gindexs() returned false";
+ return;
+ }
+
+ res.status = Message::STATUS_OK;
+
+ }
+
+ //TODO: handle "restricted" RPC
+ void DaemonHandler::handle(const GetRandomOutputsForAmounts::Request& req, GetRandomOutputsForAmounts::Response& res)
+ {
+ auto& chain = m_core.get_blockchain_storage();
+
+ try
+ {
+ for (const uint64_t& amount : req.amounts)
+ {
+ std::vector<uint64_t> indices = chain.get_random_outputs(amount, req.count);
+
+ outputs_for_amount ofa;
+
+ ofa.resize(indices.size());
+
+ for (size_t i = 0; i < indices.size(); i++)
+ {
+ crypto::public_key key = chain.get_output_key(amount, indices[i]);
+ ofa[i].amount_index = indices[i];
+ ofa[i].key = key;
+ }
+
+ amount_with_random_outputs amt;
+ amt.amount = amount;
+ amt.outputs = ofa;
+
+ res.amounts_with_outputs.push_back(amt);
+ }
+
+ res.status = Message::STATUS_OK;
+ }
+ catch (const std::exception& e)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = e.what();
+ }
+ }
+
+ void DaemonHandler::handle(const SendRawTx::Request& req, SendRawTx::Response& res)
+ {
+ auto tx_blob = cryptonote::tx_to_blob(req.tx);
+
+ cryptonote_connection_context fake_context = AUTO_VAL_INIT(fake_context);
+ tx_verification_context tvc = AUTO_VAL_INIT(tvc);
+
+ if(!m_core.handle_incoming_tx(tx_blob, tvc, false, false, !req.relay) || tvc.m_verifivation_failed)
+ {
+ if (tvc.m_verifivation_failed)
+ {
+ LOG_PRINT_L0("[on_send_raw_tx]: tx verification failed");
+ }
+ else
+ {
+ LOG_PRINT_L0("[on_send_raw_tx]: Failed to process tx");
+ }
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "";
+
+ if (tvc.m_low_mixin)
+ {
+ res.error_details = "mixin too low";
+ }
+ if (tvc.m_double_spend)
+ {
+ if (!res.error_details.empty()) res.error_details += " and ";
+ res.error_details = "double spend";
+ }
+ if (tvc.m_invalid_input)
+ {
+ if (!res.error_details.empty()) res.error_details += " and ";
+ res.error_details = "invalid input";
+ }
+ if (tvc.m_invalid_output)
+ {
+ if (!res.error_details.empty()) res.error_details += " and ";
+ res.error_details = "invalid output";
+ }
+ if (tvc.m_too_big)
+ {
+ if (!res.error_details.empty()) res.error_details += " and ";
+ res.error_details = "too big";
+ }
+ if (tvc.m_overspend)
+ {
+ if (!res.error_details.empty()) res.error_details += " and ";
+ res.error_details = "overspend";
+ }
+ if (tvc.m_fee_too_low)
+ {
+ if (!res.error_details.empty()) res.error_details += " and ";
+ res.error_details = "fee too low";
+ }
+ if (tvc.m_not_rct)
+ {
+ if (!res.error_details.empty()) res.error_details += " and ";
+ res.error_details = "tx is not ringct";
+ }
+ if (res.error_details.empty())
+ {
+ res.error_details = "an unknown issue was found with the transaction";
+ }
+
+ return;
+ }
+
+ if(!tvc.m_should_be_relayed || !req.relay)
+ {
+ LOG_PRINT_L0("[on_send_raw_tx]: tx accepted, but not relayed");
+ res.error_details = "Not relayed";
+ res.relayed = false;
+ res.status = Message::STATUS_OK;
+
+ return;
+ }
+
+ NOTIFY_NEW_TRANSACTIONS::request r;
+ r.txs.push_back(tx_blob);
+ m_core.get_protocol()->relay_transactions(r, fake_context);
+
+ //TODO: make sure that tx has reached other nodes here, probably wait to receive reflections from other nodes
+ res.status = Message::STATUS_OK;
+ res.relayed = true;
+
+ return;
+ }
+
+ void DaemonHandler::handle(const StartMining::Request& req, StartMining::Response& res)
+ {
+ account_public_address adr;
+ if(!get_account_address_from_str(adr, m_core.get_testnet(), req.miner_address))
+ {
+ res.error_details = "Failed, wrong address";
+ LOG_PRINT_L0(res.error_details);
+ res.status = Message::STATUS_FAILED;
+ return;
+ }
+
+ unsigned int concurrency_count = boost::thread::hardware_concurrency() * 4;
+
+ // if we couldn't detect threads, set it to a ridiculously high number
+ if(concurrency_count == 0)
+ {
+ concurrency_count = 257;
+ }
+
+ // if there are more threads requested than the hardware supports
+ // then we fail and log that.
+ if(req.threads_count > concurrency_count)
+ {
+ res.error_details = "Failed, too many threads relative to CPU cores.";
+ LOG_PRINT_L0(res.error_details);
+ res.status = Message::STATUS_FAILED;
+ return;
+ }
+
+ boost::thread::attributes attrs;
+ attrs.set_stack_size(THREAD_STACK_SIZE);
+
+ if(!m_core.get_miner().start(adr, static_cast<size_t>(req.threads_count), attrs, req.do_background_mining, req.ignore_battery))
+ {
+ res.error_details = "Failed, mining not started";
+ LOG_PRINT_L0(res.error_details);
+ res.status = Message::STATUS_FAILED;
+ return;
+ }
+ res.status = Message::STATUS_OK;
+ res.error_details = "";
+
+ }
+
+ void DaemonHandler::handle(const GetInfo::Request& req, GetInfo::Response& res)
+ {
+ res.info.height = m_core.get_current_blockchain_height();
+
+ res.info.target_height = m_core.get_target_blockchain_height();
+
+ if (res.info.height > res.info.target_height)
+ {
+ res.info.target_height = res.info.height;
+ }
+
+ auto& chain = m_core.get_blockchain_storage();
+
+ res.info.difficulty = chain.get_difficulty_for_next_block();
+
+ res.info.target = chain.get_difficulty_target();
+
+ res.info.tx_count = chain.get_total_transactions() - res.info.height; //without coinbase
+
+ res.info.tx_pool_size = m_core.get_pool_transactions_count();
+
+ res.info.alt_blocks_count = chain.get_alternative_blocks_count();
+
+ uint64_t total_conn = m_p2p.get_connections_count();
+ res.info.outgoing_connections_count = m_p2p.get_outgoing_connections_count();
+ res.info.incoming_connections_count = total_conn - res.info.outgoing_connections_count;
+
+ res.info.white_peerlist_size = m_p2p.get_peerlist_manager().get_white_peers_count();
+
+ res.info.grey_peerlist_size = m_p2p.get_peerlist_manager().get_gray_peers_count();
+
+ res.info.testnet = m_core.get_testnet();
+ res.info.cumulative_difficulty = m_core.get_blockchain_storage().get_db().get_block_cumulative_difficulty(res.info.height - 1);
+ res.info.block_size_limit = m_core.get_blockchain_storage().get_current_cumulative_blocksize_limit();
+ res.info.start_time = (uint64_t)m_core.get_start_time();
+
+ res.status = Message::STATUS_OK;
+ res.error_details = "";
+ }
+
+ void DaemonHandler::handle(const StopMining::Request& req, StopMining::Response& res)
+ {
+ if(!m_core.get_miner().stop())
+ {
+ res.error_details = "Failed, mining not stopped";
+ LOG_PRINT_L0(res.error_details);
+ res.status = Message::STATUS_FAILED;
+ return;
+ }
+
+ res.status = Message::STATUS_OK;
+ res.error_details = "";
+ }
+
+ void DaemonHandler::handle(const MiningStatus::Request& req, MiningStatus::Response& res)
+ {
+ const cryptonote::miner& lMiner = m_core.get_miner();
+ res.active = lMiner.is_mining();
+ res.is_background_mining_enabled = lMiner.get_is_background_mining_enabled();
+
+ if ( lMiner.is_mining() ) {
+ res.speed = lMiner.get_speed();
+ res.threads_count = lMiner.get_threads_count();
+ const account_public_address& lMiningAdr = lMiner.get_mining_address();
+ res.address = get_account_address_as_str(m_core.get_testnet(), lMiningAdr);
+ }
+
+ res.status = Message::STATUS_OK;
+ res.error_details = "";
+ }
+
+ void DaemonHandler::handle(const SaveBC::Request& req, SaveBC::Response& res)
+ {
+ if (!m_core.get_blockchain_storage().store_blockchain())
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "Error storing the blockchain";
+ }
+ else
+ {
+ res.status = Message::STATUS_OK;
+ }
+ }
+
+ void DaemonHandler::handle(const GetBlockHash::Request& req, GetBlockHash::Response& res)
+ {
+ if (m_core.get_current_blockchain_height() <= req.height)
+ {
+ res.hash = cryptonote::null_hash;
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "height given is higher than current chain height";
+ return;
+ }
+
+ res.hash = m_core.get_block_id_by_height(req.height);
+
+ res.status = Message::STATUS_OK;
+ }
+
+ void DaemonHandler::handle(const GetBlockTemplate::Request& req, GetBlockTemplate::Response& res)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "RPC method not yet implemented.";
+ }
+
+ void DaemonHandler::handle(const SubmitBlock::Request& req, SubmitBlock::Response& res)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "RPC method not yet implemented.";
+ }
+
+ void DaemonHandler::handle(const GetLastBlockHeader::Request& req, GetLastBlockHeader::Response& res)
+ {
+ const crypto::hash block_hash = m_core.get_tail_id();
+
+ if (!getBlockHeaderByHash(block_hash, res.header))
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "Requested block does not exist";
+ return;
+ }
+
+ res.status = Message::STATUS_OK;
+ }
+
+ void DaemonHandler::handle(const GetBlockHeaderByHash::Request& req, GetBlockHeaderByHash::Response& res)
+ {
+ if (!getBlockHeaderByHash(req.hash, res.header))
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "Requested block does not exist";
+ return;
+ }
+
+ res.status = Message::STATUS_OK;
+ }
+
+ void DaemonHandler::handle(const GetBlockHeaderByHeight::Request& req, GetBlockHeaderByHeight::Response& res)
+ {
+ const crypto::hash block_hash = m_core.get_block_id_by_height(req.height);
+
+ if (!getBlockHeaderByHash(block_hash, res.header))
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "Requested block does not exist";
+ return;
+ }
+
+ res.status = Message::STATUS_OK;
+ }
+
+ void DaemonHandler::handle(const GetBlockHeadersByHeight::Request& req, GetBlockHeadersByHeight::Response& res)
+ {
+ res.headers.resize(req.heights.size());
+
+ for (size_t i=0; i < req.heights.size(); i++)
+ {
+ const crypto::hash block_hash = m_core.get_block_id_by_height(req.heights[i]);
+
+ if (!getBlockHeaderByHash(block_hash, res.headers[i]))
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "A requested block does not exist";
+ return;
+ }
+ }
+
+ res.status = Message::STATUS_OK;
+ }
+
+ void DaemonHandler::handle(const GetBlock::Request& req, GetBlock::Response& res)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "RPC method not yet implemented.";
+ }
+
+ void DaemonHandler::handle(const GetPeerList::Request& req, GetPeerList::Response& res)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "RPC method not yet implemented.";
+ }
+
+ void DaemonHandler::handle(const SetLogHashRate::Request& req, SetLogHashRate::Response& res)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "RPC method not yet implemented.";
+ }
+
+ void DaemonHandler::handle(const SetLogLevel::Request& req, SetLogLevel::Response& res)
+ {
+ if (req.level < 0 || req.level > 4)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "Error: log level not valid";
+ }
+ else
+ {
+ res.status = Message::STATUS_OK;
+ mlog_set_log_level(req.level);
+ }
+ }
+
+ void DaemonHandler::handle(const GetTransactionPool::Request& req, GetTransactionPool::Response& res)
+ {
+ bool r = m_core.get_pool_for_rpc(res.transactions, res.key_images);
+
+ if (!r) res.status = Message::STATUS_FAILED;
+ else res.status = Message::STATUS_OK;
+ }
+
+ void DaemonHandler::handle(const GetConnections::Request& req, GetConnections::Response& res)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "RPC method not yet implemented.";
+ }
+
+ void DaemonHandler::handle(const GetBlockHeadersRange::Request& req, GetBlockHeadersRange::Response& res)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "RPC method not yet implemented.";
+ }
+
+ void DaemonHandler::handle(const StopDaemon::Request& req, StopDaemon::Response& res)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "RPC method not yet implemented.";
+ }
+
+ void DaemonHandler::handle(const StartSaveGraph::Request& req, StartSaveGraph::Response& res)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "RPC method not yet implemented.";
+ }
+
+ void DaemonHandler::handle(const StopSaveGraph::Request& req, StopSaveGraph::Response& res)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "RPC method not yet implemented.";
+ }
+
+ void DaemonHandler::handle(const HardForkInfo::Request& req, HardForkInfo::Response& res)
+ {
+ const Blockchain &blockchain = m_core.get_blockchain_storage();
+ uint8_t version = req.version > 0 ? req.version : blockchain.get_ideal_hard_fork_version();
+ res.info.version = blockchain.get_current_hard_fork_version();
+ res.info.enabled = blockchain.get_hard_fork_voting_info(version, res.info.window, res.info.votes, res.info.threshold, res.info.earliest_height, res.info.voting);
+ res.info.state = blockchain.get_hard_fork_state();
+ res.status = Message::STATUS_OK;
+ }
+
+ void DaemonHandler::handle(const GetBans::Request& req, GetBans::Response& res)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "RPC method not yet implemented.";
+ }
+
+ void DaemonHandler::handle(const SetBans::Request& req, SetBans::Response& res)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "RPC method not yet implemented.";
+ }
+
+ void DaemonHandler::handle(const FlushTransactionPool::Request& req, FlushTransactionPool::Response& res)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = "RPC method not yet implemented.";
+ }
+
+ void DaemonHandler::handle(const GetOutputHistogram::Request& req, GetOutputHistogram::Response& res)
+ {
+ std::map<uint64_t, std::tuple<uint64_t, uint64_t, uint64_t> > histogram;
+ try
+ {
+ histogram = m_core.get_blockchain_storage().get_output_histogram(req.amounts, req.unlocked, req.recent_cutoff);
+ }
+ catch (const std::exception &e)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = e.what();
+ return;
+ }
+
+ res.histogram.clear();
+ res.histogram.reserve(histogram.size());
+ for (const auto &i: histogram)
+ {
+ if (std::get<0>(i.second) >= req.min_count && (std::get<0>(i.second) <= req.max_count || req.max_count == 0))
+ res.histogram.emplace_back(output_amount_count{i.first, std::get<0>(i.second), std::get<1>(i.second), std::get<2>(i.second)});
+ }
+
+ res.status = Message::STATUS_OK;
+ }
+
+ void DaemonHandler::handle(const GetOutputKeys::Request& req, GetOutputKeys::Response& res)
+ {
+ try
+ {
+ for (const auto& i : req.outputs)
+ {
+ crypto::public_key key;
+ rct::key mask;
+ bool unlocked;
+ m_core.get_blockchain_storage().get_output_key_mask_unlocked(i.amount, i.index, key, mask, unlocked);
+ res.keys.emplace_back(output_key_mask_unlocked{key, mask, unlocked});
+ }
+ }
+ catch (const std::exception& e)
+ {
+ res.status = Message::STATUS_FAILED;
+ res.error_details = e.what();
+ return;
+ }
+
+ res.status = Message::STATUS_OK;
+ }
+
+ void DaemonHandler::handle(const GetRPCVersion::Request& req, GetRPCVersion::Response& res)
+ {
+ res.version = DAEMON_RPC_VERSION_ZMQ;
+ res.status = Message::STATUS_OK;
+ }
+
+ void DaemonHandler::handle(const GetPerKBFeeEstimate::Request& req, GetPerKBFeeEstimate::Response& res)
+ {
+ res.estimated_fee_per_kb = m_core.get_blockchain_storage().get_dynamic_per_kb_fee_estimate(req.num_grace_blocks);
+ res.status = Message::STATUS_OK;
+ }
+
+ bool DaemonHandler::getBlockHeaderByHash(const crypto::hash& hash_in, cryptonote::rpc::BlockHeaderResponse& header)
+ {
+ block b;
+
+ if (!m_core.get_block_by_hash(hash_in, b))
+ {
+ return false;
+ }
+
+ header.hash = hash_in;
+ header.height = boost::get<txin_gen>(b.miner_tx.vin.front()).height;
+
+ header.major_version = b.major_version;
+ header.minor_version = b.minor_version;
+ header.timestamp = b.timestamp;
+ header.nonce = b.nonce;
+ header.prev_id = b.prev_id;
+
+ header.depth = m_core.get_current_blockchain_height() - header.height - 1;
+
+ header.reward = 0;
+ for (const auto& out : b.miner_tx.vout)
+ {
+ header.reward += out.amount;
+ }
+
+ header.difficulty = m_core.get_blockchain_storage().block_difficulty(header.height);
+
+ return true;
+ }
+
+ std::string DaemonHandler::handle(const std::string& request)
+ {
+ MDEBUG("Handling RPC request: " << request);
+
+ Message* resp_message = NULL;
+
+ try
+ {
+ FullMessage req_full(request, true);
+
+ rapidjson::Value& req_json = req_full.getMessage();
+
+ const std::string request_type = req_full.getRequestType();
+
+ // create correct Message subclass and call handle() on it
+ REQ_RESP_TYPES_MACRO(request_type, GetHeight, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, GetBlocksFast, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, GetHashesFast, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, GetTransactions, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, KeyImagesSpent, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, GetTxGlobalOutputIndices, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, GetRandomOutputsForAmounts, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, SendRawTx, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, GetInfo, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, StartMining, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, StopMining, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, MiningStatus, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, SaveBC, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, GetBlockHash, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, GetLastBlockHeader, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, GetBlockHeaderByHash, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, GetBlockHeaderByHeight, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, GetBlockHeadersByHeight, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, GetPeerList, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, SetLogLevel, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, GetTransactionPool, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, HardForkInfo, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, GetOutputHistogram, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, GetOutputKeys, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, GetRPCVersion, req_json, resp_message, handle);
+ REQ_RESP_TYPES_MACRO(request_type, GetPerKBFeeEstimate, req_json, resp_message, handle);
+
+ // if none of the request types matches
+ if (resp_message == NULL)
+ {
+ return BAD_REQUEST(request_type, req_full.getID());
+ }
+
+ FullMessage resp_full = FullMessage::responseMessage(resp_message, req_full.getID());
+
+ const std::string response = resp_full.getJson();
+ delete resp_message;
+ resp_message = NULL;
+
+ MDEBUG("Returning RPC response: " << response);
+
+ return response;
+ }
+ catch (const std::exception& e)
+ {
+ if (resp_message)
+ {
+ delete resp_message;
+ }
+
+ return BAD_JSON(e.what());
+ }
+ }
+
+} // namespace rpc
+
+} // namespace cryptonote
diff --git a/src/rpc/daemon_handler.h b/src/rpc/daemon_handler.h
new file mode 100644
index 000000000..0d356bad2
--- /dev/null
+++ b/src/rpc/daemon_handler.h
@@ -0,0 +1,145 @@
+// Copyright (c) 2017, 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 "daemon_messages.h"
+#include "daemon_rpc_version.h"
+#include "rpc_handler.h"
+#include "cryptonote_core/cryptonote_core.h"
+#include "cryptonote_protocol/cryptonote_protocol_handler.h"
+#include "p2p/net_node.h"
+
+namespace
+{
+ typedef nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<cryptonote::core> > t_p2p;
+} // anonymous namespace
+
+namespace cryptonote
+{
+
+namespace rpc
+{
+
+class DaemonHandler : public RpcHandler
+{
+ public:
+
+ DaemonHandler(cryptonote::core& c, t_p2p& p2p) : m_core(c), m_p2p(p2p) { }
+
+ ~DaemonHandler() { }
+
+ void handle(const GetHeight::Request& req, GetHeight::Response& res);
+
+ void handle(const GetBlocksFast::Request& req, GetBlocksFast::Response& res);
+
+ void handle(const GetHashesFast::Request& req, GetHashesFast::Response& res);
+
+ void handle(const GetTransactions::Request& req, GetTransactions::Response& res);
+
+ void handle(const KeyImagesSpent::Request& req, KeyImagesSpent::Response& res);
+
+ void handle(const GetTxGlobalOutputIndices::Request& req, GetTxGlobalOutputIndices::Response& res);
+
+ void handle(const GetRandomOutputsForAmounts::Request& req, GetRandomOutputsForAmounts::Response& res);
+
+ void handle(const SendRawTx::Request& req, SendRawTx::Response& res);
+
+ void handle(const StartMining::Request& req, StartMining::Response& res);
+
+ void handle(const GetInfo::Request& req, GetInfo::Response& res);
+
+ void handle(const StopMining::Request& req, StopMining::Response& res);
+
+ void handle(const MiningStatus::Request& req, MiningStatus::Response& res);
+
+ void handle(const SaveBC::Request& req, SaveBC::Response& res);
+
+ void handle(const GetBlockHash::Request& req, GetBlockHash::Response& res);
+
+ void handle(const GetBlockTemplate::Request& req, GetBlockTemplate::Response& res);
+
+ void handle(const SubmitBlock::Request& req, SubmitBlock::Response& res);
+
+ void handle(const GetLastBlockHeader::Request& req, GetLastBlockHeader::Response& res);
+
+ void handle(const GetBlockHeaderByHash::Request& req, GetBlockHeaderByHash::Response& res);
+
+ void handle(const GetBlockHeaderByHeight::Request& req, GetBlockHeaderByHeight::Response& res);
+
+ void handle(const GetBlockHeadersByHeight::Request& req, GetBlockHeadersByHeight::Response& res);
+
+ void handle(const GetBlock::Request& req, GetBlock::Response& res);
+
+ void handle(const GetPeerList::Request& req, GetPeerList::Response& res);
+
+ void handle(const SetLogHashRate::Request& req, SetLogHashRate::Response& res);
+
+ void handle(const SetLogLevel::Request& req, SetLogLevel::Response& res);
+
+ void handle(const GetTransactionPool::Request& req, GetTransactionPool::Response& res);
+
+ void handle(const GetConnections::Request& req, GetConnections::Response& res);
+
+ void handle(const GetBlockHeadersRange::Request& req, GetBlockHeadersRange::Response& res);
+
+ void handle(const StopDaemon::Request& req, StopDaemon::Response& res);
+
+ void handle(const StartSaveGraph::Request& req, StartSaveGraph::Response& res);
+
+ void handle(const StopSaveGraph::Request& req, StopSaveGraph::Response& res);
+
+ void handle(const HardForkInfo::Request& req, HardForkInfo::Response& res);
+
+ void handle(const GetBans::Request& req, GetBans::Response& res);
+
+ void handle(const SetBans::Request& req, SetBans::Response& res);
+
+ void handle(const FlushTransactionPool::Request& req, FlushTransactionPool::Response& res);
+
+ void handle(const GetOutputHistogram::Request& req, GetOutputHistogram::Response& res);
+
+ void handle(const GetOutputKeys::Request& req, GetOutputKeys::Response& res);
+
+ void handle(const GetRPCVersion::Request& req, GetRPCVersion::Response& res);
+
+ void handle(const GetPerKBFeeEstimate::Request& req, GetPerKBFeeEstimate::Response& res);
+
+ std::string handle(const std::string& request);
+
+ private:
+
+ bool getBlockHeaderByHash(const crypto::hash& hash_in, cryptonote::rpc::BlockHeaderResponse& response);
+
+ cryptonote::core& m_core;
+ t_p2p& m_p2p;
+};
+
+} // namespace rpc
+
+} // namespace cryptonote
diff --git a/src/rpc/daemon_messages.cpp b/src/rpc/daemon_messages.cpp
new file mode 100644
index 000000000..640058df9
--- /dev/null
+++ b/src/rpc/daemon_messages.cpp
@@ -0,0 +1,900 @@
+// Copyright (c) 2016-2017, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "daemon_messages.h"
+#include "serialization/json_object.h"
+
+namespace cryptonote
+{
+
+namespace rpc
+{
+
+const char* const GetHeight::name = "get_height";
+const char* const GetBlocksFast::name = "get_blocks_fast";
+const char* const GetHashesFast::name = "get_hashes_fast";
+const char* const GetTransactions::name = "get_transactions";
+const char* const KeyImagesSpent::name = "key_images_spent";
+const char* const GetTxGlobalOutputIndices::name = "get_tx_global_output_indices";
+const char* const GetRandomOutputsForAmounts::name = "get_random_outputs_for_amounts";
+const char* const SendRawTx::name = "send_raw_tx";
+const char* const StartMining::name = "start_mining";
+const char* const StopMining::name = "stop_mining";
+const char* const MiningStatus::name = "mining_status";
+const char* const GetInfo::name = "get_info";
+const char* const SaveBC::name = "save_bc";
+const char* const GetBlockHash::name = "get_block_hash";
+const char* const GetLastBlockHeader::name = "get_last_block_header";
+const char* const GetBlockHeaderByHash::name = "get_block_header_by_hash";
+const char* const GetBlockHeaderByHeight::name = "get_block_header_by_height";
+const char* const GetBlockHeadersByHeight::name = "get_block_headers_by_height";
+const char* const GetPeerList::name = "get_peer_list";
+const char* const SetLogLevel::name = "set_log_level";
+const char* const GetTransactionPool::name = "get_transaction_pool";
+const char* const HardForkInfo::name = "hard_fork_info";
+const char* const GetOutputHistogram::name = "get_output_histogram";
+const char* const GetOutputKeys::name = "get_output_keys";
+const char* const GetRPCVersion::name = "get_rpc_version";
+const char* const GetPerKBFeeEstimate::name = "get_dynamic_per_kb_fee_estimate";
+
+
+
+
+rapidjson::Value GetHeight::Request::toJson(rapidjson::Document& doc) const
+{
+ return Message::toJson(doc);
+}
+
+void GetHeight::Request::fromJson(rapidjson::Value& val)
+{
+}
+
+rapidjson::Value GetHeight::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ val.AddMember("height", height, al);
+
+ return val;
+}
+
+void GetHeight::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, height, height);
+}
+
+
+rapidjson::Value GetBlocksFast::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, block_ids, block_ids);
+ val.AddMember("start_height", start_height, al);
+ val.AddMember("prune", prune, al);
+
+ return val;
+}
+
+void GetBlocksFast::Request::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, block_ids, block_ids);
+ GET_FROM_JSON_OBJECT(val, start_height, start_height);
+ GET_FROM_JSON_OBJECT(val, prune, prune);
+}
+
+rapidjson::Value GetBlocksFast::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, blocks, blocks);
+ val.AddMember("start_height", start_height, al);
+ val.AddMember("current_height", current_height, al);
+ INSERT_INTO_JSON_OBJECT(val, doc, output_indices, output_indices);
+
+ return val;
+}
+
+void GetBlocksFast::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, blocks, blocks);
+ GET_FROM_JSON_OBJECT(val, start_height, start_height);
+ GET_FROM_JSON_OBJECT(val, current_height, current_height);
+ GET_FROM_JSON_OBJECT(val, output_indices, output_indices);
+}
+
+
+rapidjson::Value GetHashesFast::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, known_hashes, known_hashes);
+ val.AddMember("start_height", start_height, al);
+
+ return val;
+}
+
+void GetHashesFast::Request::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, known_hashes, known_hashes);
+ GET_FROM_JSON_OBJECT(val, start_height, start_height);
+}
+
+rapidjson::Value GetHashesFast::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, hashes, hashes);
+ val.AddMember("start_height", start_height, al);
+ val.AddMember("current_height", current_height, al);
+
+ return val;
+}
+
+void GetHashesFast::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, hashes, hashes);
+ GET_FROM_JSON_OBJECT(val, start_height, start_height);
+ GET_FROM_JSON_OBJECT(val, current_height, current_height);
+}
+
+
+rapidjson::Value GetTransactions::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, tx_hashes, tx_hashes);
+
+ return val;
+}
+
+void GetTransactions::Request::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, tx_hashes, tx_hashes);
+}
+
+rapidjson::Value GetTransactions::Response::toJson(rapidjson::Document& doc) const
+{
+ rapidjson::Value val(rapidjson::kObjectType);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, txs, txs);
+ INSERT_INTO_JSON_OBJECT(val, doc, missed_hashes, missed_hashes);
+
+ return val;
+}
+
+void GetTransactions::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, txs, txs);
+ GET_FROM_JSON_OBJECT(val, missed_hashes, missed_hashes);
+}
+
+
+rapidjson::Value KeyImagesSpent::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, key_images, key_images);
+
+ return val;
+}
+
+void KeyImagesSpent::Request::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, key_images, key_images);
+}
+
+rapidjson::Value KeyImagesSpent::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, spent_status, spent_status);
+
+ return val;
+}
+
+void KeyImagesSpent::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, spent_status, spent_status);
+}
+
+
+rapidjson::Value GetTxGlobalOutputIndices::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, tx_hash, tx_hash);
+
+ return val;
+}
+
+void GetTxGlobalOutputIndices::Request::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, tx_hash, tx_hash);
+}
+
+rapidjson::Value GetTxGlobalOutputIndices::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, output_indices, output_indices);
+
+ return val;
+}
+
+void GetTxGlobalOutputIndices::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, output_indices, output_indices);
+}
+
+
+rapidjson::Value GetRandomOutputsForAmounts::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, amounts, amounts);
+ INSERT_INTO_JSON_OBJECT(val, doc, count, count);
+
+ return val;
+}
+
+void GetRandomOutputsForAmounts::Request::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, amounts, amounts);
+ GET_FROM_JSON_OBJECT(val, count, count);
+}
+
+rapidjson::Value GetRandomOutputsForAmounts::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, amounts_with_outputs, amounts_with_outputs);
+
+ return val;
+}
+
+void GetRandomOutputsForAmounts::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, amounts_with_outputs, amounts_with_outputs);
+}
+
+
+rapidjson::Value SendRawTx::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, tx, tx);
+ INSERT_INTO_JSON_OBJECT(val, doc, relay, relay);
+
+ return val;
+}
+
+void SendRawTx::Request::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, tx, tx);
+ GET_FROM_JSON_OBJECT(val, relay, relay);
+}
+
+rapidjson::Value SendRawTx::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, relayed, relayed);
+
+ return val;
+}
+
+
+void SendRawTx::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, relayed, relayed);
+}
+
+rapidjson::Value StartMining::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, miner_address, miner_address);
+ INSERT_INTO_JSON_OBJECT(val, doc, threads_count, threads_count);
+ INSERT_INTO_JSON_OBJECT(val, doc, do_background_mining, do_background_mining);
+ INSERT_INTO_JSON_OBJECT(val, doc, ignore_battery, ignore_battery);
+
+ return val;
+}
+
+void StartMining::Request::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, miner_address, miner_address);
+ GET_FROM_JSON_OBJECT(val, threads_count, threads_count);
+ GET_FROM_JSON_OBJECT(val, do_background_mining, do_background_mining);
+ GET_FROM_JSON_OBJECT(val, ignore_battery, ignore_battery);
+}
+
+rapidjson::Value StartMining::Response::toJson(rapidjson::Document& doc) const
+{
+ return Message::toJson(doc);
+}
+
+void StartMining::Response::fromJson(rapidjson::Value& val)
+{
+}
+
+
+rapidjson::Value StopMining::Request::toJson(rapidjson::Document& doc) const
+{
+ return Message::toJson(doc);
+}
+
+void StopMining::Request::fromJson(rapidjson::Value& val)
+{
+}
+
+rapidjson::Value StopMining::Response::toJson(rapidjson::Document& doc) const
+{
+ return Message::toJson(doc);
+}
+
+void StopMining::Response::fromJson(rapidjson::Value& val)
+{
+}
+
+
+rapidjson::Value MiningStatus::Request::toJson(rapidjson::Document& doc) const
+{
+ return Message::toJson(doc);
+}
+
+void MiningStatus::Request::fromJson(rapidjson::Value& val)
+{
+}
+
+rapidjson::Value MiningStatus::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, active, active);
+ INSERT_INTO_JSON_OBJECT(val, doc, speed, speed);
+ INSERT_INTO_JSON_OBJECT(val, doc, threads_count, threads_count);
+ INSERT_INTO_JSON_OBJECT(val, doc, address, address);
+ INSERT_INTO_JSON_OBJECT(val, doc, is_background_mining_enabled, is_background_mining_enabled);
+
+ return val;
+}
+
+void MiningStatus::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, active, active);
+ GET_FROM_JSON_OBJECT(val, speed, speed);
+ GET_FROM_JSON_OBJECT(val, threads_count, threads_count);
+ GET_FROM_JSON_OBJECT(val, address, address);
+ GET_FROM_JSON_OBJECT(val, is_background_mining_enabled, is_background_mining_enabled);
+}
+
+
+rapidjson::Value GetInfo::Request::toJson(rapidjson::Document& doc) const
+{
+ return Message::toJson(doc);
+}
+
+void GetInfo::Request::fromJson(rapidjson::Value& val)
+{
+}
+
+rapidjson::Value GetInfo::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, info, info);
+
+ return val;
+}
+
+void GetInfo::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, info, info);
+}
+
+
+rapidjson::Value SaveBC::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ return val;
+}
+
+void SaveBC::Request::fromJson(rapidjson::Value& val)
+{
+}
+
+rapidjson::Value SaveBC::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ return val;
+}
+
+void SaveBC::Response::fromJson(rapidjson::Value& val)
+{
+}
+
+
+rapidjson::Value GetBlockHash::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, height, height);
+
+ return val;
+}
+
+void GetBlockHash::Request::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, height, height);
+}
+
+rapidjson::Value GetBlockHash::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, hash, hash);
+
+ return val;
+}
+
+void GetBlockHash::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, hash, hash);
+}
+
+
+rapidjson::Value GetLastBlockHeader::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ return val;
+}
+
+void GetLastBlockHeader::Request::fromJson(rapidjson::Value& val)
+{
+}
+
+rapidjson::Value GetLastBlockHeader::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, header, header);
+
+ return val;
+}
+
+void GetLastBlockHeader::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, header, header);
+}
+
+
+rapidjson::Value GetBlockHeaderByHash::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, hash, hash);
+
+ return val;
+}
+
+void GetBlockHeaderByHash::Request::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, hash, hash);
+}
+
+rapidjson::Value GetBlockHeaderByHash::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, header, header);
+
+ return val;
+}
+
+void GetBlockHeaderByHash::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, header, header);
+}
+
+
+rapidjson::Value GetBlockHeaderByHeight::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, height, height);
+
+ return val;
+}
+
+void GetBlockHeaderByHeight::Request::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, height, height);
+}
+
+rapidjson::Value GetBlockHeaderByHeight::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, header, header);
+
+ return val;
+}
+
+void GetBlockHeaderByHeight::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, header, header);
+}
+
+
+rapidjson::Value GetBlockHeadersByHeight::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, heights, heights);
+
+ return val;
+}
+
+void GetBlockHeadersByHeight::Request::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, heights, heights);
+}
+
+rapidjson::Value GetBlockHeadersByHeight::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, headers, headers);
+
+ return val;
+}
+
+void GetBlockHeadersByHeight::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, headers, headers);
+}
+
+
+rapidjson::Value GetPeerList::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ return val;
+}
+
+void GetPeerList::Request::fromJson(rapidjson::Value& val)
+{
+}
+
+rapidjson::Value GetPeerList::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, white_list, white_list);
+ INSERT_INTO_JSON_OBJECT(val, doc, gray_list, gray_list);
+
+ return val;
+}
+
+void GetPeerList::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, white_list, white_list);
+ GET_FROM_JSON_OBJECT(val, gray_list, gray_list);
+}
+
+
+rapidjson::Value SetLogLevel::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ val.AddMember("level", level, al);
+
+ return val;
+}
+
+void SetLogLevel::Request::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, level, level);
+}
+
+rapidjson::Value SetLogLevel::Response::toJson(rapidjson::Document& doc) const
+{
+ return Message::toJson(doc);
+}
+
+void SetLogLevel::Response::fromJson(rapidjson::Value& val)
+{
+}
+
+
+rapidjson::Value GetTransactionPool::Request::toJson(rapidjson::Document& doc) const
+{
+ return Message::toJson(doc);
+}
+
+void GetTransactionPool::Request::fromJson(rapidjson::Value& val)
+{
+}
+
+rapidjson::Value GetTransactionPool::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, transactions, transactions);
+ INSERT_INTO_JSON_OBJECT(val, doc, key_images, key_images);
+
+ return val;
+}
+
+void GetTransactionPool::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, transactions, transactions);
+ GET_FROM_JSON_OBJECT(val, key_images, key_images);
+}
+
+
+rapidjson::Value HardForkInfo::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, version, version);
+
+ return val;
+}
+
+void HardForkInfo::Request::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, version, version);
+}
+
+rapidjson::Value HardForkInfo::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, info, info);
+
+ return val;
+}
+
+void HardForkInfo::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, info, info);
+}
+
+
+rapidjson::Value GetOutputHistogram::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, amounts, amounts);
+ INSERT_INTO_JSON_OBJECT(val, doc, min_count, min_count);
+ INSERT_INTO_JSON_OBJECT(val, doc, max_count, max_count);
+ INSERT_INTO_JSON_OBJECT(val, doc, unlocked, unlocked);
+ INSERT_INTO_JSON_OBJECT(val, doc, recent_cutoff, recent_cutoff);
+
+ return val;
+}
+
+void GetOutputHistogram::Request::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, amounts, amounts);
+ GET_FROM_JSON_OBJECT(val, min_count, min_count);
+ GET_FROM_JSON_OBJECT(val, max_count, max_count);
+ GET_FROM_JSON_OBJECT(val, unlocked, unlocked);
+ GET_FROM_JSON_OBJECT(val, recent_cutoff, recent_cutoff);
+}
+
+rapidjson::Value GetOutputHistogram::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, histogram, histogram);
+
+ return val;
+}
+
+void GetOutputHistogram::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, histogram, histogram);
+}
+
+
+rapidjson::Value GetOutputKeys::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, outputs, outputs);
+
+ return val;
+}
+
+void GetOutputKeys::Request::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, outputs, outputs);
+}
+
+rapidjson::Value GetOutputKeys::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, keys, keys);
+
+ return val;
+}
+
+void GetOutputKeys::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, keys, keys);
+}
+
+
+rapidjson::Value GetRPCVersion::Request::toJson(rapidjson::Document& doc) const
+{
+ return Message::toJson(doc);
+}
+
+void GetRPCVersion::Request::fromJson(rapidjson::Value& val)
+{
+}
+
+rapidjson::Value GetRPCVersion::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, version, version);
+
+ return val;
+}
+
+void GetRPCVersion::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, version, version);
+}
+
+rapidjson::Value GetPerKBFeeEstimate::Request::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, num_grace_blocks, num_grace_blocks);
+
+ return val;
+}
+
+void GetPerKBFeeEstimate::Request::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, num_grace_blocks, num_grace_blocks);
+}
+
+rapidjson::Value GetPerKBFeeEstimate::Response::toJson(rapidjson::Document& doc) const
+{
+ auto val = Message::toJson(doc);
+
+ auto& al = doc.GetAllocator();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, estimated_fee_per_kb, estimated_fee_per_kb);
+
+ return val;
+}
+
+void GetPerKBFeeEstimate::Response::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, estimated_fee_per_kb, estimated_fee_per_kb);
+}
+
+
+} // namespace rpc
+
+} // namespace cryptonote
diff --git a/src/rpc/daemon_messages.h b/src/rpc/daemon_messages.h
new file mode 100644
index 000000000..685f66843
--- /dev/null
+++ b/src/rpc/daemon_messages.h
@@ -0,0 +1,421 @@
+// Copyright (c) 2016-2017, 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 "message.h"
+#include "cryptonote_protocol/cryptonote_protocol_defs.h"
+#include "rpc/message_data_structs.h"
+#include "rpc/daemon_rpc_version.h"
+#include "cryptonote_basic/cryptonote_basic.h"
+
+#define BEGIN_RPC_MESSAGE_CLASS(classname) \
+class classname \
+{ \
+ public: \
+ static const char* const name;
+
+#define BEGIN_RPC_MESSAGE_REQUEST \
+ class Request : public Message \
+ { \
+ public: \
+ Request() { } \
+ ~Request() { } \
+ rapidjson::Value toJson(rapidjson::Document& doc) const; \
+ void fromJson(rapidjson::Value& val);
+
+#define BEGIN_RPC_MESSAGE_RESPONSE \
+ class Response : public Message \
+ { \
+ public: \
+ Response() { } \
+ ~Response() { } \
+ rapidjson::Value toJson(rapidjson::Document& doc) const; \
+ void fromJson(rapidjson::Value& val);
+
+#define END_RPC_MESSAGE_REQUEST };
+#define END_RPC_MESSAGE_RESPONSE };
+#define END_RPC_MESSAGE_CLASS };
+
+#define COMMA() ,
+
+// NOTE: when using a type with multiple template parameters,
+// replace any comma in the template specifier with the macro
+// above, or the preprocessor will eat the comma in a bad way.
+#define RPC_MESSAGE_MEMBER(type, name) type name;
+
+
+namespace cryptonote
+{
+
+namespace rpc
+{
+
+BEGIN_RPC_MESSAGE_CLASS(GetHeight);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(uint64_t, height);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+
+BEGIN_RPC_MESSAGE_CLASS(GetBlocksFast);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ RPC_MESSAGE_MEMBER(std::list<crypto::hash>, block_ids);
+ RPC_MESSAGE_MEMBER(uint64_t, start_height);
+ RPC_MESSAGE_MEMBER(bool, prune);
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(std::vector<cryptonote::rpc::block_with_transactions>, blocks);
+ RPC_MESSAGE_MEMBER(uint64_t, start_height);
+ RPC_MESSAGE_MEMBER(uint64_t, current_height);
+ RPC_MESSAGE_MEMBER(std::vector<cryptonote::rpc::block_output_indices>, output_indices);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+
+BEGIN_RPC_MESSAGE_CLASS(GetHashesFast);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ RPC_MESSAGE_MEMBER(std::list<crypto::hash>, known_hashes);
+ RPC_MESSAGE_MEMBER(uint64_t, start_height);
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(std::list<crypto::hash>, hashes);
+ RPC_MESSAGE_MEMBER(uint64_t, start_height);
+ RPC_MESSAGE_MEMBER(uint64_t, current_height);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+
+BEGIN_RPC_MESSAGE_CLASS(GetTransactions);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ RPC_MESSAGE_MEMBER(std::vector<crypto::hash>, tx_hashes);
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(std::unordered_map<crypto::hash COMMA() cryptonote::rpc::transaction_info>, txs);
+ RPC_MESSAGE_MEMBER(std::vector<crypto::hash>, missed_hashes);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+
+BEGIN_RPC_MESSAGE_CLASS(KeyImagesSpent);
+ enum STATUS {
+ UNSPENT = 0,
+ SPENT_IN_BLOCKCHAIN = 1,
+ SPENT_IN_POOL = 2,
+ };
+ BEGIN_RPC_MESSAGE_REQUEST;
+ RPC_MESSAGE_MEMBER(std::vector<crypto::key_image>, key_images);
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(std::vector<uint64_t>, spent_status);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+
+BEGIN_RPC_MESSAGE_CLASS(GetTxGlobalOutputIndices);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ RPC_MESSAGE_MEMBER(crypto::hash, tx_hash);
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(std::vector<uint64_t>, output_indices);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+
+BEGIN_RPC_MESSAGE_CLASS(GetRandomOutputsForAmounts);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ RPC_MESSAGE_MEMBER(std::vector<uint64_t>, amounts);
+ RPC_MESSAGE_MEMBER(uint64_t, count);
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(std::vector<amount_with_random_outputs>, amounts_with_outputs);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(SendRawTx);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ RPC_MESSAGE_MEMBER(cryptonote::transaction, tx);
+ RPC_MESSAGE_MEMBER(bool, relay);
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(bool, relayed);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(StartMining);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ RPC_MESSAGE_MEMBER(std::string, miner_address);
+ RPC_MESSAGE_MEMBER(uint64_t, threads_count);
+ RPC_MESSAGE_MEMBER(bool, do_background_mining);
+ RPC_MESSAGE_MEMBER(bool, ignore_battery);
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(GetInfo);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(DaemonInfo, info);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(StopMining);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(MiningStatus);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(bool, active);
+ RPC_MESSAGE_MEMBER(uint64_t, speed);
+ RPC_MESSAGE_MEMBER(uint64_t, threads_count);
+ RPC_MESSAGE_MEMBER(std::string, address);
+ RPC_MESSAGE_MEMBER(bool, is_background_mining_enabled);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(SaveBC);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(GetBlockHash);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ RPC_MESSAGE_MEMBER(uint64_t, height);
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(crypto::hash, hash);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(GetBlockTemplate);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(SubmitBlock);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(GetLastBlockHeader);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(cryptonote::rpc::BlockHeaderResponse, header);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(GetBlockHeaderByHash);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ RPC_MESSAGE_MEMBER(crypto::hash, hash);
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(cryptonote::rpc::BlockHeaderResponse, header);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(GetBlockHeaderByHeight);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ RPC_MESSAGE_MEMBER(uint64_t, height);
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(cryptonote::rpc::BlockHeaderResponse, header);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(GetBlockHeadersByHeight);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ RPC_MESSAGE_MEMBER(std::vector<uint64_t>, heights);
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(std::vector<cryptonote::rpc::BlockHeaderResponse>, headers);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(GetBlock);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(GetPeerList);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(std::vector<peer>, white_list);
+ RPC_MESSAGE_MEMBER(std::vector<peer>, gray_list);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(SetLogHashRate);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(SetLogLevel);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ RPC_MESSAGE_MEMBER(int8_t, level);
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(GetTransactionPool);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(std::vector<cryptonote::rpc::tx_in_pool>, transactions);
+ RPC_MESSAGE_MEMBER(key_images_with_tx_hashes, key_images);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(GetConnections);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(GetBlockHeadersRange);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(StopDaemon);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(StartSaveGraph);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(StopSaveGraph);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(HardForkInfo);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ RPC_MESSAGE_MEMBER(uint8_t, version);
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(hard_fork_info, info);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(GetBans);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(SetBans);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(FlushTransactionPool);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(GetOutputHistogram);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ RPC_MESSAGE_MEMBER(std::vector<uint64_t>, amounts);
+ RPC_MESSAGE_MEMBER(uint64_t, min_count);
+ RPC_MESSAGE_MEMBER(uint64_t, max_count);
+ RPC_MESSAGE_MEMBER(bool, unlocked);
+ RPC_MESSAGE_MEMBER(uint64_t, recent_cutoff);
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(std::vector<output_amount_count>, histogram);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(GetOutputKeys);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ RPC_MESSAGE_MEMBER(std::vector<output_amount_and_index>, outputs);
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(std::vector<output_key_mask_unlocked>, keys);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(GetRPCVersion);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(uint32_t, version);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+BEGIN_RPC_MESSAGE_CLASS(GetPerKBFeeEstimate);
+ BEGIN_RPC_MESSAGE_REQUEST;
+ RPC_MESSAGE_MEMBER(uint64_t, num_grace_blocks);
+ END_RPC_MESSAGE_REQUEST;
+ BEGIN_RPC_MESSAGE_RESPONSE;
+ RPC_MESSAGE_MEMBER(uint64_t, estimated_fee_per_kb);
+ END_RPC_MESSAGE_RESPONSE;
+END_RPC_MESSAGE_CLASS;
+
+} // namespace rpc
+
+} // namespace cryptonote
diff --git a/src/rpc/daemon_rpc_version.h b/src/rpc/daemon_rpc_version.h
new file mode 100644
index 000000000..bb735fe7c
--- /dev/null
+++ b/src/rpc/daemon_rpc_version.h
@@ -0,0 +1,44 @@
+// Copyright (c) 2016-2017, 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
+
+namespace cryptonote
+{
+
+namespace rpc
+{
+
+static const uint32_t DAEMON_RPC_VERSION_ZMQ_MINOR = 0;
+static const uint32_t DAEMON_RPC_VERSION_ZMQ_MAJOR = 1;
+
+static const uint32_t DAEMON_RPC_VERSION_ZMQ = DAEMON_RPC_VERSION_ZMQ_MINOR + (DAEMON_RPC_VERSION_ZMQ_MAJOR << 16);
+
+} // namespace rpc
+
+} // namespace cryptonote
diff --git a/src/rpc/message.cpp b/src/rpc/message.cpp
new file mode 100644
index 000000000..086820180
--- /dev/null
+++ b/src/rpc/message.cpp
@@ -0,0 +1,286 @@
+// Copyright (c) 2016-2017, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "message.h"
+#include "daemon_rpc_version.h"
+#include "serialization/json_object.h"
+
+#include "rapidjson/writer.h"
+#include "rapidjson/stringbuffer.h"
+
+namespace cryptonote
+{
+
+namespace rpc
+{
+
+const char* Message::STATUS_OK = "OK";
+const char* Message::STATUS_RETRY = "Retry";
+const char* Message::STATUS_FAILED = "Failed";
+const char* Message::STATUS_BAD_REQUEST = "Invalid request type";
+const char* Message::STATUS_BAD_JSON = "Malformed json";
+
+rapidjson::Value Message::toJson(rapidjson::Document& doc) const
+{
+ rapidjson::Value val(rapidjson::kObjectType);
+
+ auto& al = doc.GetAllocator();
+
+ val.AddMember("status", rapidjson::StringRef(status.c_str()), al);
+ val.AddMember("error_details", rapidjson::StringRef(error_details.c_str()), al);
+ INSERT_INTO_JSON_OBJECT(val, doc, rpc_version, DAEMON_RPC_VERSION_ZMQ);
+
+ return val;
+}
+
+void Message::fromJson(rapidjson::Value& val)
+{
+ GET_FROM_JSON_OBJECT(val, status, status);
+ GET_FROM_JSON_OBJECT(val, error_details, error_details);
+ GET_FROM_JSON_OBJECT(val, rpc_version, rpc_version);
+}
+
+
+FullMessage::FullMessage(const std::string& request, Message* message)
+{
+ doc.SetObject();
+
+ doc.AddMember("method", rapidjson::StringRef(request.c_str()), doc.GetAllocator());
+ doc.AddMember("params", message->toJson(doc), doc.GetAllocator());
+
+ // required by JSON-RPC 2.0 spec
+ doc.AddMember("jsonrpc", rapidjson::Value("2.0"), doc.GetAllocator());
+}
+
+FullMessage::FullMessage(Message* message)
+{
+ doc.SetObject();
+
+ // required by JSON-RPC 2.0 spec
+ doc.AddMember("jsonrpc", "2.0", doc.GetAllocator());
+
+ if (message->status == Message::STATUS_OK)
+ {
+ doc.AddMember("response", message->toJson(doc), doc.GetAllocator());
+ }
+ else
+ {
+ cryptonote::rpc::error err;
+
+ err.error_str = message->status;
+ err.message = message->error_details;
+
+ INSERT_INTO_JSON_OBJECT(doc, doc, error, err);
+ }
+}
+
+FullMessage::FullMessage(const std::string& json_string, bool request)
+{
+ doc.Parse(json_string.c_str());
+ if (doc.HasParseError())
+ {
+ throw cryptonote::json::PARSE_FAIL();
+ }
+
+ OBJECT_HAS_MEMBER_OR_THROW(doc, "jsonrpc")
+
+ if (request)
+ {
+ OBJECT_HAS_MEMBER_OR_THROW(doc, "method")
+ OBJECT_HAS_MEMBER_OR_THROW(doc, "params")
+ }
+ else
+ {
+ if (!doc.HasMember("response") && !doc.HasMember("error"))
+ {
+ throw cryptonote::json::MISSING_KEY("error/response");
+ }
+ }
+}
+
+std::string FullMessage::getJson()
+{
+
+ if (!doc.HasMember("id"))
+ {
+ doc.AddMember("id", rapidjson::Value("unused"), doc.GetAllocator());
+ }
+
+ rapidjson::StringBuffer buf;
+
+ rapidjson::Writer<rapidjson::StringBuffer> writer(buf);
+
+ doc.Accept(writer);
+
+ return std::string(buf.GetString(), buf.GetSize());
+}
+
+std::string FullMessage::getRequestType() const
+{
+ OBJECT_HAS_MEMBER_OR_THROW(doc, "method")
+ return doc["method"].GetString();
+}
+
+rapidjson::Value& FullMessage::getMessage()
+{
+ if (doc.HasMember("params"))
+ {
+ return doc["params"];
+ }
+ else if (doc.HasMember("response"))
+ {
+ return doc["response"];
+ }
+
+ //else
+ OBJECT_HAS_MEMBER_OR_THROW(doc, "error")
+ return doc["error"];
+
+}
+
+rapidjson::Value FullMessage::getMessageCopy()
+{
+ rapidjson::Value& val = getMessage();
+
+ return rapidjson::Value(val, doc.GetAllocator());
+}
+
+rapidjson::Value& FullMessage::getID()
+{
+ OBJECT_HAS_MEMBER_OR_THROW(doc, "id")
+ return doc["id"];
+}
+
+void FullMessage::setID(rapidjson::Value& id)
+{
+ auto itr = doc.FindMember("id");
+ if (itr != doc.MemberEnd())
+ {
+ itr->value = id;
+ }
+ else
+ {
+ doc.AddMember("id", id, doc.GetAllocator());
+ }
+}
+
+cryptonote::rpc::error FullMessage::getError()
+{
+ cryptonote::rpc::error err;
+ err.use = false;
+ if (doc.HasMember("error"))
+ {
+ GET_FROM_JSON_OBJECT(doc, err, error);
+ err.use = true;
+ }
+
+ return err;
+}
+
+FullMessage FullMessage::requestMessage(const std::string& request, Message* message)
+{
+ return FullMessage(request, message);
+}
+
+FullMessage FullMessage::requestMessage(const std::string& request, Message* message, rapidjson::Value& id)
+{
+ auto mes = requestMessage(request, message);
+ mes.setID(id);
+ return mes;
+}
+
+FullMessage FullMessage::responseMessage(Message* message)
+{
+ return FullMessage(message);
+}
+
+FullMessage FullMessage::responseMessage(Message* message, rapidjson::Value& id)
+{
+ auto mes = responseMessage(message);
+ mes.setID(id);
+ return mes;
+}
+
+FullMessage* FullMessage::timeoutMessage()
+{
+ auto *full_message = new FullMessage();
+
+ auto& doc = full_message->doc;
+ auto& al = full_message->doc.GetAllocator();
+
+ doc.SetObject();
+
+ // required by JSON-RPC 2.0 spec
+ doc.AddMember("jsonrpc", "2.0", al);
+
+ cryptonote::rpc::error err;
+
+ err.error_str = "RPC request timed out.";
+ INSERT_INTO_JSON_OBJECT(doc, doc, err, err);
+
+ return full_message;
+}
+
+// convenience functions for bad input
+std::string BAD_REQUEST(const std::string& request)
+{
+ Message fail;
+ fail.status = Message::STATUS_BAD_REQUEST;
+ fail.error_details = std::string("\"") + request + "\" is not a valid request.";
+
+ FullMessage fail_response = FullMessage::responseMessage(&fail);
+
+ return fail_response.getJson();
+}
+
+std::string BAD_REQUEST(const std::string& request, rapidjson::Value& id)
+{
+ Message fail;
+ fail.status = Message::STATUS_BAD_REQUEST;
+ fail.error_details = std::string("\"") + request + "\" is not a valid request.";
+
+ FullMessage fail_response = FullMessage::responseMessage(&fail, id);
+
+ return fail_response.getJson();
+}
+
+std::string BAD_JSON(const std::string& error_details)
+{
+ Message fail;
+ fail.status = Message::STATUS_BAD_JSON;
+ fail.error_details = error_details;
+
+ FullMessage fail_response = FullMessage::responseMessage(&fail);
+
+ return fail_response.getJson();
+}
+
+
+} // namespace rpc
+
+} // namespace cryptonote
diff --git a/src/rpc/message.h b/src/rpc/message.h
new file mode 100644
index 000000000..d1abe3fbe
--- /dev/null
+++ b/src/rpc/message.h
@@ -0,0 +1,131 @@
+// Copyright (c) 2016-2017, 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 "rapidjson/document.h"
+#include "rpc/message_data_structs.h"
+#include <string>
+
+/* I normally hate using macros, but in this case it would be untenably
+ * verbose to not use a macro. This macro saves the trouble of explicitly
+ * writing the below if block for every single RPC call.
+ */
+#define REQ_RESP_TYPES_MACRO( runtime_str, type, reqjson, resp_message_ptr, handler) \
+ \
+ if (runtime_str == type::name) \
+ { \
+ type::Request reqvar; \
+ type::Response *respvar = new type::Response(); \
+ \
+ reqvar.fromJson(reqjson); \
+ \
+ handler(reqvar, *respvar); \
+ \
+ resp_message_ptr = respvar; \
+ }
+
+namespace cryptonote
+{
+
+namespace rpc
+{
+
+ class Message
+ {
+ public:
+ static const char* STATUS_OK;
+ static const char* STATUS_RETRY;
+ static const char* STATUS_FAILED;
+ static const char* STATUS_BAD_REQUEST;
+ static const char* STATUS_BAD_JSON;
+
+ Message() : status(STATUS_OK) { }
+
+ virtual ~Message() { }
+
+ virtual rapidjson::Value toJson(rapidjson::Document& doc) const;
+
+ virtual void fromJson(rapidjson::Value& val);
+
+ std::string status;
+ std::string error_details;
+ uint32_t rpc_version;
+ };
+
+ class FullMessage
+ {
+ public:
+ ~FullMessage() { }
+
+ FullMessage(FullMessage&& rhs) noexcept : doc(std::move(rhs.doc)) { }
+
+ FullMessage(const std::string& json_string, bool request=false);
+
+ std::string getJson();
+
+ std::string getRequestType() const;
+
+ rapidjson::Value& getMessage();
+
+ rapidjson::Value getMessageCopy();
+
+ rapidjson::Value& getID();
+
+ void setID(rapidjson::Value& id);
+
+ cryptonote::rpc::error getError();
+
+ static FullMessage requestMessage(const std::string& request, Message* message);
+ static FullMessage requestMessage(const std::string& request, Message* message, rapidjson::Value& id);
+
+ static FullMessage responseMessage(Message* message);
+ static FullMessage responseMessage(Message* message, rapidjson::Value& id);
+
+ static FullMessage* timeoutMessage();
+ private:
+
+ FullMessage() = default;
+
+ FullMessage(const std::string& request, Message* message);
+ FullMessage(Message* message);
+
+ rapidjson::Document doc;
+ };
+
+
+ // convenience functions for bad input
+ std::string BAD_REQUEST(const std::string& request);
+ std::string BAD_REQUEST(const std::string& request, rapidjson::Value& id);
+
+ std::string BAD_JSON(const std::string& error_details);
+
+
+} // namespace rpc
+
+} // namespace cryptonote
diff --git a/src/rpc/message_data_structs.h b/src/rpc/message_data_structs.h
new file mode 100644
index 000000000..00f1e0caa
--- /dev/null
+++ b/src/rpc/message_data_structs.h
@@ -0,0 +1,189 @@
+// Copyright (c) 2016-2017, 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 "crypto/hash.h"
+#include "cryptonote_basic/cryptonote_basic.h"
+#include "ringct/rctSigs.h"
+
+#include <unordered_map>
+#include <vector>
+
+namespace cryptonote
+{
+
+namespace rpc
+{
+
+ struct block_with_transactions
+ {
+ cryptonote::block block;
+ std::unordered_map<crypto::hash, cryptonote::transaction> transactions;
+ };
+
+ typedef std::vector<uint64_t> tx_output_indices;
+
+ typedef std::vector<tx_output_indices> block_output_indices;
+
+ struct transaction_info
+ {
+ cryptonote::transaction transaction;
+ bool in_pool;
+ uint64_t height;
+ };
+
+ struct output_key_and_amount_index
+ {
+ uint64_t amount_index;
+ crypto::public_key key;
+ };
+
+ typedef std::vector<output_key_and_amount_index> outputs_for_amount;
+
+ struct amount_with_random_outputs
+ {
+ uint64_t amount;
+ outputs_for_amount outputs;
+ };
+
+ struct peer
+ {
+ uint64_t id;
+ uint32_t ip;
+ uint16_t port;
+ uint64_t last_seen;
+ };
+
+ struct tx_in_pool
+ {
+ cryptonote::transaction tx;
+ crypto::hash tx_hash;
+ uint64_t blob_size;
+ uint64_t fee;
+ crypto::hash max_used_block_hash;
+ uint64_t max_used_block_height;
+ bool kept_by_block;
+ crypto::hash last_failed_block_hash;
+ uint64_t last_failed_block_height;
+ uint64_t receive_time;
+ uint64_t last_relayed_time;
+ bool relayed;
+ bool do_not_relay;
+ };
+
+ typedef std::unordered_map<crypto::key_image, std::vector<crypto::hash> > key_images_with_tx_hashes;
+
+ struct output_amount_count
+ {
+ uint64_t amount;
+ uint64_t total_count;
+ uint64_t unlocked_count;
+ uint64_t recent_count;
+ };
+
+ struct output_amount_and_index
+ {
+ uint64_t amount;
+ uint64_t index;
+ };
+
+ struct output_key_mask_unlocked
+ {
+ crypto::public_key key;
+ rct::key mask;
+ bool unlocked;
+ };
+
+ struct hard_fork_info
+ {
+ uint8_t version;
+ bool enabled;
+ uint32_t window;
+ uint32_t votes;
+ uint32_t threshold;
+ uint8_t voting;
+ uint32_t state;
+ uint64_t earliest_height;
+ };
+
+ //required by JSON-RPC 2.0 spec
+ struct error
+ {
+ // not really using code, maybe later.
+ error() : use(false), code(1) { }
+
+ bool use; // do not serialize
+
+ int32_t code;
+
+ // not required by spec, but int error codes aren't perfect
+ std::string error_str;
+
+ std::string message;
+
+ //TODO: data member? not required, may want later.
+ };
+
+ struct BlockHeaderResponse
+ {
+ uint64_t major_version;
+ uint64_t minor_version;
+ uint64_t timestamp;
+ crypto::hash prev_id;
+ uint32_t nonce;
+ uint64_t height;
+ uint64_t depth;
+ crypto::hash hash;
+ uint64_t difficulty;
+ uint64_t reward;
+ };
+
+ struct DaemonInfo
+ {
+ uint64_t height;
+ uint64_t target_height;
+ uint64_t difficulty;
+ uint64_t target;
+ uint64_t tx_count;
+ uint64_t tx_pool_size;
+ uint64_t alt_blocks_count;
+ uint64_t outgoing_connections_count;
+ uint64_t incoming_connections_count;
+ uint64_t white_peerlist_size;
+ uint64_t grey_peerlist_size;
+ bool testnet;
+ crypto::hash top_block_hash;
+ uint64_t cumulative_difficulty;
+ uint64_t block_size_limit;
+ uint64_t start_time;
+ };
+
+} // namespace rpc
+
+} // namespace cryptonote
diff --git a/src/rpc/rpc_handler.h b/src/rpc/rpc_handler.h
new file mode 100644
index 000000000..d75180199
--- /dev/null
+++ b/src/rpc/rpc_handler.h
@@ -0,0 +1,54 @@
+// Copyright (c) 2016, 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 <string>
+
+namespace cryptonote
+{
+
+namespace rpc
+{
+
+
+class RpcHandler
+{
+ public:
+
+ virtual std::string handle(const std::string& request) = 0;
+
+ RpcHandler() { }
+
+ virtual ~RpcHandler() { }
+};
+
+
+} // rpc
+
+} // cryptonote
diff --git a/src/rpc/zmq_server.cpp b/src/rpc/zmq_server.cpp
new file mode 100644
index 000000000..afdff2328
--- /dev/null
+++ b/src/rpc/zmq_server.cpp
@@ -0,0 +1,141 @@
+// Copyright (c) 2016, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "zmq_server.h"
+#include <boost/chrono/chrono.hpp>
+
+namespace cryptonote
+{
+
+namespace rpc
+{
+
+ZmqServer::ZmqServer(RpcHandler& h) :
+ handler(h),
+ stop_signal(false),
+ running(false),
+ context(DEFAULT_NUM_ZMQ_THREADS) // TODO: make this configurable
+{
+}
+
+ZmqServer::~ZmqServer()
+{
+}
+
+void ZmqServer::serve()
+{
+
+ while (1)
+ {
+ try
+ {
+ zmq::message_t message;
+
+ if (!rep_socket)
+ {
+ throw std::runtime_error("ZMQ RPC server reply socket is null");
+ }
+ while (rep_socket->recv(&message))
+ {
+ std::string message_string(reinterpret_cast<const char *>(message.data()), message.size());
+
+ MDEBUG(std::string("Received RPC request: \"") + message_string + "\"");
+
+ std::string response = handler.handle(message_string);
+
+ zmq::message_t reply(response.size());
+ memcpy((void *) reply.data(), response.c_str(), response.size());
+
+ rep_socket->send(reply);
+ MDEBUG(std::string("Sent RPC reply: \"") + response + "\"");
+
+ }
+ }
+ catch (const boost::thread_interrupted& e)
+ {
+ MDEBUG("ZMQ Server thread interrupted.");
+ }
+ catch (const zmq::error_t& e)
+ {
+ MERROR(std::string("ZMQ error: ") + e.what());
+ }
+ boost::this_thread::interruption_point();
+ }
+}
+
+bool ZmqServer::addIPCSocket(std::string address, std::string port)
+{
+ MERROR("ZmqServer::addIPCSocket not yet implemented!");
+ return false;
+}
+
+bool ZmqServer::addTCPSocket(std::string address, std::string port)
+{
+ try
+ {
+ std::string addr_prefix("tcp://");
+
+ rep_socket.reset(new zmq::socket_t(context, ZMQ_REP));
+
+ rep_socket->setsockopt(ZMQ_RCVTIMEO, DEFAULT_RPC_RECV_TIMEOUT_MS);
+
+ std::string bind_address = addr_prefix + address + std::string(":") + port;
+ rep_socket->bind(bind_address.c_str());
+ }
+ catch (const std::exception& e)
+ {
+ MERROR(std::string("Error creating ZMQ Socket: ") + e.what());
+ return false;
+ }
+ return true;
+}
+
+void ZmqServer::run()
+{
+ running = true;
+ run_thread = boost::thread(boost::bind(&ZmqServer::serve, this));
+}
+
+void ZmqServer::stop()
+{
+ if (!running) return;
+
+ stop_signal = true;
+
+ run_thread.interrupt();
+ run_thread.join();
+
+ running = false;
+
+ return;
+}
+
+
+} // namespace cryptonote
+
+} // namespace rpc
diff --git a/src/rpc/zmq_server.h b/src/rpc/zmq_server.h
new file mode 100644
index 000000000..c278ff759
--- /dev/null
+++ b/src/rpc/zmq_server.h
@@ -0,0 +1,83 @@
+// Copyright (c) 2016, 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 <boost/thread/thread.hpp>
+#include <zmq.hpp>
+#include <string>
+#include <memory>
+
+#include "common/command_line.h"
+
+#include "rpc_handler.h"
+
+namespace cryptonote
+{
+
+namespace rpc
+{
+
+static constexpr int DEFAULT_NUM_ZMQ_THREADS = 1;
+static constexpr int DEFAULT_RPC_RECV_TIMEOUT_MS = 1000;
+
+class ZmqServer
+{
+ public:
+
+ ZmqServer(RpcHandler& h);
+
+ ~ZmqServer();
+
+ static void init_options(boost::program_options::options_description& desc);
+
+ void serve();
+
+ bool addIPCSocket(std::string address, std::string port);
+ bool addTCPSocket(std::string address, std::string port);
+
+ void run();
+ void stop();
+
+ private:
+ RpcHandler& handler;
+
+ volatile bool stop_signal;
+ volatile bool running;
+
+ zmq::context_t context;
+
+ boost::thread run_thread;
+
+ std::unique_ptr<zmq::socket_t> rep_socket;
+};
+
+
+} // namespace cryptonote
+
+} // namespace rpc
diff --git a/src/serialization/CMakeLists.txt b/src/serialization/CMakeLists.txt
new file mode 100644
index 000000000..e4aa0b6a2
--- /dev/null
+++ b/src/serialization/CMakeLists.txt
@@ -0,0 +1,54 @@
+# Copyright (c) 2016, 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.
+
+set(serialization_sources
+ json_object.cpp)
+
+set(serialization_headers)
+
+set(serialization_private_headers
+ json_object.h)
+
+monero_private_headers(serialization
+ ${serialization_private_headers})
+monero_add_library(serialization
+ ${serialization_sources}
+ ${serialization_headers}
+ ${serialization_private_headers})
+target_link_libraries(serialization
+ LINK_PRIVATE
+ cryptonote_core
+ cryptonote_protocol
+ ${Boost_CHRONO_LIBRARY}
+ ${Boost_REGEX_LIBRARY}
+ ${Boost_SYSTEM_LIBRARY}
+ ${Boost_THREAD_LIBRARY}
+ ${EXTRA_LIBRARIES})
+add_dependencies(serialization
+ version)
+
diff --git a/src/serialization/json_object.cpp b/src/serialization/json_object.cpp
new file mode 100644
index 000000000..ead3fdd58
--- /dev/null
+++ b/src/serialization/json_object.cpp
@@ -0,0 +1,1165 @@
+// Copyright (c) 2016-2017, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "json_object.h"
+
+#include <limits>
+#include "string_tools.h"
+
+namespace cryptonote
+{
+
+namespace json
+{
+
+namespace
+{
+ template<typename Source, typename Destination>
+ constexpr bool precision_loss()
+ {
+ return
+ std::numeric_limits<Destination>::is_signed != std::numeric_limits<Source>::is_signed ||
+ std::numeric_limits<Destination>::min() > std::numeric_limits<Source>::min() ||
+ std::numeric_limits<Destination>::max() < std::numeric_limits<Source>::max();
+ }
+
+ template<typename Source, typename Type>
+ void convert_numeric(Source source, Type& i)
+ {
+ static_assert(
+ std::numeric_limits<Source>::is_signed == std::numeric_limits<Type>::is_signed,
+ "source and destination signs do not match"
+ );
+ if (source < std::numeric_limits<Type>::min())
+ {
+ throw WRONG_TYPE{"numeric underflow"};
+ }
+ if (std::numeric_limits<Type>::max() < source)
+ {
+ throw WRONG_TYPE{"numeric overflow"};
+ }
+ i = Type(source);
+ }
+
+ template<typename Type>
+ void to_int(const rapidjson::Value& val, Type& i)
+ {
+ if (!val.IsInt())
+ {
+ throw WRONG_TYPE{"integer"};
+ }
+ convert_numeric(val.GetInt(), i);
+ }
+ template<typename Type>
+ void to_int64(const rapidjson::Value& val, Type& i)
+ {
+ if (!val.IsInt64())
+ {
+ throw WRONG_TYPE{"integer"};
+ }
+ convert_numeric(val.GetInt64(), i);
+ }
+
+ template<typename Type>
+ void to_uint(const rapidjson::Value& val, Type& i)
+ {
+ if (!val.IsUint())
+ {
+ throw WRONG_TYPE{"unsigned integer"};
+ }
+ convert_numeric(val.GetUint(), i);
+ }
+ template<typename Type>
+ void to_uint64(const rapidjson::Value& val, Type& i)
+ {
+ if (!val.IsUint64())
+ {
+ throw WRONG_TYPE{"unsigned integer"};
+ }
+ convert_numeric(val.GetUint64(), i);
+ }
+}
+
+void toJsonValue(rapidjson::Document& doc, const std::string& i, rapidjson::Value& val)
+{
+ val = rapidjson::Value(i.c_str(), doc.GetAllocator());
+}
+
+void fromJsonValue(const rapidjson::Value& val, std::string& str)
+{
+ if (!val.IsString())
+ {
+ throw WRONG_TYPE("string");
+ }
+
+ str = val.GetString();
+}
+
+void toJsonValue(rapidjson::Document& doc, bool i, rapidjson::Value& val)
+{
+ val.SetBool(i);
+}
+
+void fromJsonValue(const rapidjson::Value& val, bool& b)
+{
+ if (!val.IsBool())
+ {
+ throw WRONG_TYPE("boolean");
+ }
+ b = val.GetBool();
+}
+
+void fromJsonValue(const rapidjson::Value& val, unsigned char& i)
+{
+ to_uint(val, i);
+}
+
+void fromJsonValue(const rapidjson::Value& val, char& i)
+{
+ to_int(val, i);
+}
+
+void fromJsonValue(const rapidjson::Value& val, signed char& i)
+{
+ to_int(val, i);
+}
+
+void fromJsonValue(const rapidjson::Value& val, unsigned short& i)
+{
+ to_uint(val, i);
+}
+
+void fromJsonValue(const rapidjson::Value& val, short& i)
+{
+ to_int(val, i);
+}
+
+void toJsonValue(rapidjson::Document& doc, const unsigned int i, rapidjson::Value& val)
+{
+ val = rapidjson::Value(i);
+}
+
+void fromJsonValue(const rapidjson::Value& val, unsigned int& i)
+{
+ to_uint(val, i);
+}
+
+void toJsonValue(rapidjson::Document& doc, const int i, rapidjson::Value& val)
+{
+ val = rapidjson::Value(i);
+}
+
+void fromJsonValue(const rapidjson::Value& val, int& i)
+{
+ to_int(val, i);
+}
+
+void toJsonValue(rapidjson::Document& doc, const unsigned long long i, rapidjson::Value& val)
+{
+ static_assert(!precision_loss<unsigned long long, std::uint64_t>(), "precision loss");
+ val = rapidjson::Value(std::uint64_t(i));
+}
+
+void fromJsonValue(const rapidjson::Value& val, unsigned long long& i)
+{
+ to_uint64(val, i);
+}
+
+void toJsonValue(rapidjson::Document& doc, const long long i, rapidjson::Value& val)
+{
+ static_assert(!precision_loss<long long, std::int64_t>(), "precision loss");
+ val = rapidjson::Value(std::int64_t(i));
+}
+
+void fromJsonValue(const rapidjson::Value& val, long long& i)
+{
+ to_int64(val, i);
+}
+
+void fromJsonValue(const rapidjson::Value& val, unsigned long& i)
+{
+ to_uint64(val, i);
+}
+
+void fromJsonValue(const rapidjson::Value& val, long& i)
+{
+ to_int64(val, i);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::transaction& tx, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, version, tx.version);
+ INSERT_INTO_JSON_OBJECT(val, doc, unlock_time, tx.unlock_time);
+ INSERT_INTO_JSON_OBJECT(val, doc, vin, tx.vin);
+ INSERT_INTO_JSON_OBJECT(val, doc, vout, tx.vout);
+ INSERT_INTO_JSON_OBJECT(val, doc, extra, tx.extra);
+ INSERT_INTO_JSON_OBJECT(val, doc, signatures, tx.signatures);
+ INSERT_INTO_JSON_OBJECT(val, doc, rct_signatures, tx.rct_signatures);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::transaction& tx)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, tx.version, version);
+ GET_FROM_JSON_OBJECT(val, tx.unlock_time, unlock_time);
+ GET_FROM_JSON_OBJECT(val, tx.vin, vin);
+ GET_FROM_JSON_OBJECT(val, tx.vout, vout);
+ GET_FROM_JSON_OBJECT(val, tx.extra, extra);
+ GET_FROM_JSON_OBJECT(val, tx.signatures, signatures);
+ GET_FROM_JSON_OBJECT(val, tx.rct_signatures, rct_signatures);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::block& b, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, major_version, b.major_version);
+ INSERT_INTO_JSON_OBJECT(val, doc, minor_version, b.minor_version);
+ INSERT_INTO_JSON_OBJECT(val, doc, timestamp, b.timestamp);
+ INSERT_INTO_JSON_OBJECT(val, doc, prev_id, b.prev_id);
+ INSERT_INTO_JSON_OBJECT(val, doc, nonce, b.nonce);
+ INSERT_INTO_JSON_OBJECT(val, doc, miner_tx, b.miner_tx);
+ INSERT_INTO_JSON_OBJECT(val, doc, tx_hashes, b.tx_hashes);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::block& b)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, b.major_version, major_version);
+ GET_FROM_JSON_OBJECT(val, b.minor_version, minor_version);
+ GET_FROM_JSON_OBJECT(val, b.timestamp, timestamp);
+ GET_FROM_JSON_OBJECT(val, b.prev_id, prev_id);
+ GET_FROM_JSON_OBJECT(val, b.nonce, nonce);
+ GET_FROM_JSON_OBJECT(val, b.miner_tx, miner_tx);
+ GET_FROM_JSON_OBJECT(val, b.tx_hashes, tx_hashes);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_v& txin, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ if (txin.type() == typeid(cryptonote::txin_gen))
+ {
+ val.AddMember("type", "txin_gen", doc.GetAllocator());
+ INSERT_INTO_JSON_OBJECT(val, doc, value, boost::get<cryptonote::txin_gen>(txin));
+ }
+ else if (txin.type() == typeid(cryptonote::txin_to_script))
+ {
+ val.AddMember("type", "txin_to_script", doc.GetAllocator());
+ INSERT_INTO_JSON_OBJECT(val, doc, value, boost::get<cryptonote::txin_to_script>(txin));
+ }
+ else if (txin.type() == typeid(cryptonote::txin_to_scripthash))
+ {
+ val.AddMember("type", "txin_to_scripthash", doc.GetAllocator());
+ INSERT_INTO_JSON_OBJECT(val, doc, value, boost::get<cryptonote::txin_to_scripthash>(txin));
+ }
+ else if (txin.type() == typeid(cryptonote::txin_to_key))
+ {
+ val.AddMember("type", "txin_to_key", doc.GetAllocator());
+ INSERT_INTO_JSON_OBJECT(val, doc, value, boost::get<cryptonote::txin_to_key>(txin));
+ }
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_v& txin)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ OBJECT_HAS_MEMBER_OR_THROW(val, "type")
+ OBJECT_HAS_MEMBER_OR_THROW(val, "value")
+ if (val["type"]== "txin_gen")
+ {
+ cryptonote::txin_gen tmpVal;
+ fromJsonValue(val["value"], tmpVal);
+ txin = tmpVal;
+ }
+ else if (val["type"]== "txin_to_script")
+ {
+ cryptonote::txin_to_script tmpVal;
+ fromJsonValue(val["value"], tmpVal);
+ txin = tmpVal;
+ }
+ else if (val["type"] == "txin_to_scripthash")
+ {
+ cryptonote::txin_to_scripthash tmpVal;
+ fromJsonValue(val["value"], tmpVal);
+ txin = tmpVal;
+ }
+ else if (val["type"] == "txin_to_key")
+ {
+ cryptonote::txin_to_key tmpVal;
+ fromJsonValue(val["value"], tmpVal);
+ txin = tmpVal;
+ }
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_gen& txin, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, height, txin.height);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_gen& txin)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, txin.height, height);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_to_script& txin, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, prev, txin.prev);
+ INSERT_INTO_JSON_OBJECT(val, doc, prevout, txin.prevout);
+ INSERT_INTO_JSON_OBJECT(val, doc, sigset, txin.sigset);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_to_script& txin)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, txin.prev, prev);
+ GET_FROM_JSON_OBJECT(val, txin.prevout, prevout);
+ GET_FROM_JSON_OBJECT(val, txin.sigset, sigset);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_to_scripthash& txin, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, prev, txin.prev);
+ INSERT_INTO_JSON_OBJECT(val, doc, prevout, txin.prevout);
+ INSERT_INTO_JSON_OBJECT(val, doc, script, txin.script);
+ INSERT_INTO_JSON_OBJECT(val, doc, sigset, txin.sigset);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_to_scripthash& txin)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, txin.prev, prev);
+ GET_FROM_JSON_OBJECT(val, txin.prevout, prevout);
+ GET_FROM_JSON_OBJECT(val, txin.script, script);
+ GET_FROM_JSON_OBJECT(val, txin.sigset, sigset);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_to_key& txin, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, amount, txin.amount);
+ INSERT_INTO_JSON_OBJECT(val, doc, key_offsets, txin.key_offsets);
+ INSERT_INTO_JSON_OBJECT(val, doc, k_image, txin.k_image);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_to_key& txin)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, txin.amount, amount);
+ GET_FROM_JSON_OBJECT(val, txin.key_offsets, key_offsets);
+ GET_FROM_JSON_OBJECT(val, txin.k_image, k_image);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::txout_target_v& txout, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ if (txout.type() == typeid(cryptonote::txout_to_script))
+ {
+ val.AddMember("type", "txout_to_script", doc.GetAllocator());
+ INSERT_INTO_JSON_OBJECT(val, doc, value, boost::get<cryptonote::txout_to_script>(txout));
+ }
+ else if (txout.type() == typeid(cryptonote::txout_to_scripthash))
+ {
+ val.AddMember("type", "txout_to_scripthash", doc.GetAllocator());
+ INSERT_INTO_JSON_OBJECT(val, doc, value, boost::get<cryptonote::txout_to_scripthash>(txout));
+ }
+ else if (txout.type() == typeid(cryptonote::txout_to_key))
+ {
+ val.AddMember("type", "txout_to_key", doc.GetAllocator());
+ INSERT_INTO_JSON_OBJECT(val, doc, value, boost::get<cryptonote::txout_to_key>(txout));
+ }
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_target_v& txout)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ OBJECT_HAS_MEMBER_OR_THROW(val, "type")
+ OBJECT_HAS_MEMBER_OR_THROW(val, "value")
+ if (val["type"]== "txout_to_script")
+ {
+ cryptonote::txout_to_script tmpVal;
+ fromJsonValue(val["value"], tmpVal);
+ txout = tmpVal;
+ }
+ else if (val["type"] == "txout_to_scripthash")
+ {
+ cryptonote::txout_to_scripthash tmpVal;
+ fromJsonValue(val["value"], tmpVal);
+ txout = tmpVal;
+ }
+ else if (val["type"] == "txout_to_key")
+ {
+ cryptonote::txout_to_key tmpVal;
+ fromJsonValue(val["value"], tmpVal);
+ txout = tmpVal;
+ }
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::txout_to_script& txout, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, keys, txout.keys);
+ INSERT_INTO_JSON_OBJECT(val, doc, script, txout.script);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_script& txout)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, txout.keys, keys);
+ GET_FROM_JSON_OBJECT(val, txout.script, script);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::txout_to_scripthash& txout, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, hash, txout.hash);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_scripthash& txout)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, txout.hash, hash);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::txout_to_key& txout, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, key, txout.key);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_key& txout)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, txout.key, key);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::tx_out& txout, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, amount, txout.amount);
+ INSERT_INTO_JSON_OBJECT(val, doc, target, txout.target);
+}
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::tx_out& txout)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, txout.amount, amount);
+ GET_FROM_JSON_OBJECT(val, txout.target, target);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::connection_info& info, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ auto& al = doc.GetAllocator();
+ INSERT_INTO_JSON_OBJECT(val, doc, incoming, info.incoming);
+ INSERT_INTO_JSON_OBJECT(val, doc, localhost, info.localhost);
+ INSERT_INTO_JSON_OBJECT(val, doc, local_ip, info.local_ip);
+
+ INSERT_INTO_JSON_OBJECT(val, doc, ip, info.ip);
+ INSERT_INTO_JSON_OBJECT(val, doc, port, info.port);
+
+ INSERT_INTO_JSON_OBJECT(val, doc, peer_id, info.peer_id);
+
+ INSERT_INTO_JSON_OBJECT(val, doc, recv_count, info.recv_count);
+ INSERT_INTO_JSON_OBJECT(val, doc, recv_idle_time, info.recv_idle_time);
+
+ INSERT_INTO_JSON_OBJECT(val, doc, send_count, info.send_count);
+ INSERT_INTO_JSON_OBJECT(val, doc, send_idle_time, info.send_idle_time);
+
+ INSERT_INTO_JSON_OBJECT(val, doc, state, info.state);
+
+ INSERT_INTO_JSON_OBJECT(val, doc, live_time, info.live_time);
+
+ INSERT_INTO_JSON_OBJECT(val, doc, avg_download, info.avg_download);
+ INSERT_INTO_JSON_OBJECT(val, doc, current_download, info.current_download);
+
+ INSERT_INTO_JSON_OBJECT(val, doc, avg_upload, info.avg_upload);
+ INSERT_INTO_JSON_OBJECT(val, doc, current_upload, info.current_upload);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::connection_info& info)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, info.incoming, incoming);
+ GET_FROM_JSON_OBJECT(val, info.localhost, localhost);
+ GET_FROM_JSON_OBJECT(val, info.local_ip, local_ip);
+
+ GET_FROM_JSON_OBJECT(val, info.ip, ip);
+ GET_FROM_JSON_OBJECT(val, info.port, port);
+
+ GET_FROM_JSON_OBJECT(val, info.peer_id, peer_id);
+
+ GET_FROM_JSON_OBJECT(val, info.recv_count, recv_count);
+ GET_FROM_JSON_OBJECT(val, info.recv_idle_time, recv_idle_time);
+
+ GET_FROM_JSON_OBJECT(val, info.send_count, send_count);
+ GET_FROM_JSON_OBJECT(val, info.send_idle_time, send_idle_time);
+
+ GET_FROM_JSON_OBJECT(val, info.state, state);
+
+ GET_FROM_JSON_OBJECT(val, info.live_time, live_time);
+
+ GET_FROM_JSON_OBJECT(val, info.avg_download, avg_download);
+ GET_FROM_JSON_OBJECT(val, info.current_download, current_download);
+
+ GET_FROM_JSON_OBJECT(val, info.avg_upload, avg_upload);
+ GET_FROM_JSON_OBJECT(val, info.current_upload, current_upload);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::block_complete_entry& blk, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, block, blk.block);
+ INSERT_INTO_JSON_OBJECT(val, doc, txs, blk.txs);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::block_complete_entry& blk)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, blk.block, block);
+ GET_FROM_JSON_OBJECT(val, blk.txs, txs);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::block_with_transactions& blk, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, block, blk.block);
+ INSERT_INTO_JSON_OBJECT(val, doc, transactions, blk.transactions);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::block_with_transactions& blk)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, blk.block, block);
+ GET_FROM_JSON_OBJECT(val, blk.transactions, transactions);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::transaction_info& tx_info, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, height, tx_info.height);
+ INSERT_INTO_JSON_OBJECT(val, doc, in_pool, tx_info.in_pool);
+ INSERT_INTO_JSON_OBJECT(val, doc, transaction, tx_info.transaction);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::transaction_info& tx_info)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, tx_info.height, height);
+ GET_FROM_JSON_OBJECT(val, tx_info.in_pool, in_pool);
+ GET_FROM_JSON_OBJECT(val, tx_info.transaction, transaction);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::output_key_and_amount_index& out, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, amount_index, out.amount_index);
+ INSERT_INTO_JSON_OBJECT(val, doc, key, out.key);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_key_and_amount_index& out)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, out.amount_index, amount_index);
+ GET_FROM_JSON_OBJECT(val, out.key, key);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::amount_with_random_outputs& out, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, amount, out.amount);
+ INSERT_INTO_JSON_OBJECT(val, doc, outputs, out.outputs);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::amount_with_random_outputs& out)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, out.amount, amount);
+ GET_FROM_JSON_OBJECT(val, out.outputs, outputs);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::peer& peer, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, id, peer.id);
+ INSERT_INTO_JSON_OBJECT(val, doc, ip, peer.ip);
+ INSERT_INTO_JSON_OBJECT(val, doc, port, peer.port);
+ INSERT_INTO_JSON_OBJECT(val, doc, last_seen, peer.last_seen);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::peer& peer)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, peer.id, id);
+ GET_FROM_JSON_OBJECT(val, peer.ip, ip);
+ GET_FROM_JSON_OBJECT(val, peer.port, port);
+ GET_FROM_JSON_OBJECT(val, peer.last_seen, last_seen);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::tx_in_pool& tx, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, tx, tx.tx);
+ INSERT_INTO_JSON_OBJECT(val, doc, tx_hash, tx.tx_hash);
+ INSERT_INTO_JSON_OBJECT(val, doc, blob_size, tx.blob_size);
+ INSERT_INTO_JSON_OBJECT(val, doc, fee, tx.fee);
+ INSERT_INTO_JSON_OBJECT(val, doc, max_used_block_hash, tx.max_used_block_hash);
+ INSERT_INTO_JSON_OBJECT(val, doc, max_used_block_height, tx.max_used_block_height);
+ INSERT_INTO_JSON_OBJECT(val, doc, kept_by_block, tx.kept_by_block);
+ INSERT_INTO_JSON_OBJECT(val, doc, last_failed_block_hash, tx.last_failed_block_hash);
+ INSERT_INTO_JSON_OBJECT(val, doc, last_failed_block_height, tx.last_failed_block_height);
+ INSERT_INTO_JSON_OBJECT(val, doc, receive_time, tx.receive_time);
+ INSERT_INTO_JSON_OBJECT(val, doc, last_relayed_time, tx.last_relayed_time);
+ INSERT_INTO_JSON_OBJECT(val, doc, relayed, tx.relayed);
+ INSERT_INTO_JSON_OBJECT(val, doc, do_not_relay, tx.do_not_relay);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::tx_in_pool& tx)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, tx.tx, tx);
+ GET_FROM_JSON_OBJECT(val, tx.blob_size, blob_size);
+ GET_FROM_JSON_OBJECT(val, tx.fee, fee);
+ GET_FROM_JSON_OBJECT(val, tx.max_used_block_hash, max_used_block_hash);
+ GET_FROM_JSON_OBJECT(val, tx.max_used_block_height, max_used_block_height);
+ GET_FROM_JSON_OBJECT(val, tx.kept_by_block, kept_by_block);
+ GET_FROM_JSON_OBJECT(val, tx.last_failed_block_hash, last_failed_block_hash);
+ GET_FROM_JSON_OBJECT(val, tx.last_failed_block_height, last_failed_block_height);
+ GET_FROM_JSON_OBJECT(val, tx.receive_time, receive_time);
+ GET_FROM_JSON_OBJECT(val, tx.last_relayed_time, last_relayed_time);
+ GET_FROM_JSON_OBJECT(val, tx.relayed, relayed);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::hard_fork_info& info, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, version, info.version);
+ INSERT_INTO_JSON_OBJECT(val, doc, enabled, info.enabled);
+ INSERT_INTO_JSON_OBJECT(val, doc, window, info.window);
+ INSERT_INTO_JSON_OBJECT(val, doc, votes, info.votes);
+ INSERT_INTO_JSON_OBJECT(val, doc, threshold, info.threshold);
+ INSERT_INTO_JSON_OBJECT(val, doc, voting, info.voting);
+ INSERT_INTO_JSON_OBJECT(val, doc, state, info.state);
+ INSERT_INTO_JSON_OBJECT(val, doc, earliest_height, info.earliest_height);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::hard_fork_info& info)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, info.version, version);
+ GET_FROM_JSON_OBJECT(val, info.enabled, enabled);
+ GET_FROM_JSON_OBJECT(val, info.window, window);
+ GET_FROM_JSON_OBJECT(val, info.votes, votes);
+ GET_FROM_JSON_OBJECT(val, info.threshold, threshold);
+ GET_FROM_JSON_OBJECT(val, info.voting, voting);
+ GET_FROM_JSON_OBJECT(val, info.state, state);
+ GET_FROM_JSON_OBJECT(val, info.earliest_height, earliest_height);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::output_amount_count& out, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, amount, out.amount);
+ INSERT_INTO_JSON_OBJECT(val, doc, total_count, out.total_count);
+ INSERT_INTO_JSON_OBJECT(val, doc, unlocked_count, out.unlocked_count);
+ INSERT_INTO_JSON_OBJECT(val, doc, recent_count, out.recent_count);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_amount_count& out)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, out.amount, amount);
+ GET_FROM_JSON_OBJECT(val, out.total_count, total_count);
+ GET_FROM_JSON_OBJECT(val, out.unlocked_count, unlocked_count);
+ GET_FROM_JSON_OBJECT(val, out.recent_count, recent_count);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::output_amount_and_index& out, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, amount, out.amount);
+ INSERT_INTO_JSON_OBJECT(val, doc, index, out.index);
+}
+
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_amount_and_index& out)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, out.amount, amount);
+ GET_FROM_JSON_OBJECT(val, out.index, index);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::output_key_mask_unlocked& out, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, key, out.key);
+ INSERT_INTO_JSON_OBJECT(val, doc, mask, out.mask);
+ INSERT_INTO_JSON_OBJECT(val, doc, unlocked, out.unlocked);
+}
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_key_mask_unlocked& out)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, out.key, key);
+ GET_FROM_JSON_OBJECT(val, out.mask, mask);
+ GET_FROM_JSON_OBJECT(val, out.unlocked, unlocked);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::error& err, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, code, err.code);
+ INSERT_INTO_JSON_OBJECT(val, doc, error_str, err.error_str);
+ INSERT_INTO_JSON_OBJECT(val, doc, message, err.message);
+}
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::error& error)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, error.code, code);
+ GET_FROM_JSON_OBJECT(val, error.error_str, error_str);
+ GET_FROM_JSON_OBJECT(val, error.message, message);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::BlockHeaderResponse& response, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, major_version, response.major_version);
+ INSERT_INTO_JSON_OBJECT(val, doc, minor_version, response.minor_version);
+ INSERT_INTO_JSON_OBJECT(val, doc, timestamp, response.timestamp);
+ INSERT_INTO_JSON_OBJECT(val, doc, prev_id, response.prev_id);
+ INSERT_INTO_JSON_OBJECT(val, doc, nonce, response.nonce);
+ INSERT_INTO_JSON_OBJECT(val, doc, height, response.height);
+ INSERT_INTO_JSON_OBJECT(val, doc, depth, response.depth);
+ INSERT_INTO_JSON_OBJECT(val, doc, hash, response.hash);
+ INSERT_INTO_JSON_OBJECT(val, doc, difficulty, response.difficulty);
+ INSERT_INTO_JSON_OBJECT(val, doc, reward, response.reward);
+}
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::BlockHeaderResponse& response)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, response.major_version, major_version);
+ GET_FROM_JSON_OBJECT(val, response.minor_version, minor_version);
+ GET_FROM_JSON_OBJECT(val, response.timestamp, timestamp);
+ GET_FROM_JSON_OBJECT(val, response.prev_id, prev_id);
+ GET_FROM_JSON_OBJECT(val, response.nonce, nonce);
+ GET_FROM_JSON_OBJECT(val, response.height, height);
+ GET_FROM_JSON_OBJECT(val, response.depth, depth);
+ GET_FROM_JSON_OBJECT(val, response.hash, hash);
+ GET_FROM_JSON_OBJECT(val, response.difficulty, difficulty);
+ GET_FROM_JSON_OBJECT(val, response.reward, reward);
+}
+
+void toJsonValue(rapidjson::Document& doc, const rct::rctSig& sig, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, type, sig.type);
+ INSERT_INTO_JSON_OBJECT(val, doc, message, sig.message);
+ INSERT_INTO_JSON_OBJECT(val, doc, mixRing, sig.mixRing);
+ INSERT_INTO_JSON_OBJECT(val, doc, pseudoOuts, sig.pseudoOuts);
+ INSERT_INTO_JSON_OBJECT(val, doc, ecdhInfo, sig.ecdhInfo);
+ INSERT_INTO_JSON_OBJECT(val, doc, outPk, sig.outPk);
+ INSERT_INTO_JSON_OBJECT(val, doc, txnFee, sig.txnFee);
+ INSERT_INTO_JSON_OBJECT(val, doc, p, sig.p);
+}
+
+void fromJsonValue(const rapidjson::Value& val, rct::rctSig& sig)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, sig.type, type);
+ GET_FROM_JSON_OBJECT(val, sig.message, message);
+ GET_FROM_JSON_OBJECT(val, sig.mixRing, mixRing);
+ GET_FROM_JSON_OBJECT(val, sig.pseudoOuts, pseudoOuts);
+ GET_FROM_JSON_OBJECT(val, sig.ecdhInfo, ecdhInfo);
+ GET_FROM_JSON_OBJECT(val, sig.outPk, outPk);
+ GET_FROM_JSON_OBJECT(val, sig.txnFee, txnFee);
+ GET_FROM_JSON_OBJECT(val, sig.p, p);
+}
+
+void toJsonValue(rapidjson::Document& doc, const rct::ctkey& key, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, dest, key.dest);
+ INSERT_INTO_JSON_OBJECT(val, doc, mask, key.mask);
+}
+
+void fromJsonValue(const rapidjson::Value& val, rct::ctkey& key)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+ GET_FROM_JSON_OBJECT(val, key.dest, dest);
+ GET_FROM_JSON_OBJECT(val, key.mask, mask);
+}
+
+void toJsonValue(rapidjson::Document& doc, const rct::ecdhTuple& tuple, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, mask, tuple.mask);
+ INSERT_INTO_JSON_OBJECT(val, doc, amount, tuple.amount);
+}
+
+void fromJsonValue(const rapidjson::Value& val, rct::ecdhTuple& tuple)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, tuple.mask, mask);
+ GET_FROM_JSON_OBJECT(val, tuple.amount, amount);
+}
+
+void toJsonValue(rapidjson::Document& doc, const rct::rctSigPrunable& sig, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, rangeSigs, sig.rangeSigs);
+ INSERT_INTO_JSON_OBJECT(val, doc, MGs, sig.MGs);
+}
+
+void fromJsonValue(const rapidjson::Value& val, rct::rctSigPrunable& sig)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, sig.rangeSigs, rangeSigs);
+ GET_FROM_JSON_OBJECT(val, sig.MGs, MGs);
+}
+
+void toJsonValue(rapidjson::Document& doc, const rct::rangeSig& sig, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, asig, sig.asig);
+
+ std::vector<rct::key> keyVector(sig.Ci, std::end(sig.Ci));
+ INSERT_INTO_JSON_OBJECT(val, doc, Ci, keyVector);
+}
+
+void fromJsonValue(const rapidjson::Value& val, rct::rangeSig& sig)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, sig.asig, asig);
+
+ std::vector<rct::key> keyVector;
+ cryptonote::json::fromJsonValue(val["Ci"], keyVector);
+ if (!(keyVector.size() == 64))
+ {
+ throw WRONG_TYPE("key64 (rct::key[64])");
+ }
+ for (size_t i=0; i < 64; i++)
+ {
+ sig.Ci[i] = keyVector[i];
+ }
+}
+
+void toJsonValue(rapidjson::Document& doc, const rct::boroSig& sig, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ std::vector<rct::key> keyVector(sig.s0, std::end(sig.s0));
+ INSERT_INTO_JSON_OBJECT(val, doc, s0, sig.s0);
+
+ keyVector.assign(sig.s1, std::end(sig.s1));
+ INSERT_INTO_JSON_OBJECT(val, doc, s1, sig.s1);
+
+ INSERT_INTO_JSON_OBJECT(val, doc, ee, sig.ee);
+}
+
+void fromJsonValue(const rapidjson::Value& val, rct::boroSig& sig)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ OBJECT_HAS_MEMBER_OR_THROW(val, "s0")
+ std::vector<rct::key> keyVector;
+ cryptonote::json::fromJsonValue(val["s0"], keyVector);
+ if (!(keyVector.size() == 64))
+ {
+ throw WRONG_TYPE("key64 (rct::key[64])");
+ }
+ for (size_t i=0; i < 64; i++)
+ {
+ sig.s0[i] = keyVector[i];
+ }
+
+ OBJECT_HAS_MEMBER_OR_THROW(val, "s1")
+ keyVector.clear();
+ cryptonote::json::fromJsonValue(val["s1"], keyVector);
+ if (!(keyVector.size() == 64))
+ {
+ throw WRONG_TYPE("key64 (rct::key[64])");
+ }
+ for (size_t i=0; i < 64; i++)
+ {
+ sig.s1[i] = keyVector[i];
+ }
+
+ GET_FROM_JSON_OBJECT(val, sig.ee, ee);
+}
+
+void toJsonValue(rapidjson::Document& doc, const rct::mgSig& sig, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, ss, sig.ss);
+ INSERT_INTO_JSON_OBJECT(val, doc, cc, sig.cc);
+}
+
+void fromJsonValue(const rapidjson::Value& val, rct::mgSig& sig)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("key64 (rct::key[64])");
+ }
+
+ GET_FROM_JSON_OBJECT(val, sig.ss, ss);
+ GET_FROM_JSON_OBJECT(val, sig.cc, cc);
+}
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::DaemonInfo& info, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ INSERT_INTO_JSON_OBJECT(val, doc, height, info.height);
+ INSERT_INTO_JSON_OBJECT(val, doc, target_height, info.target_height);
+ INSERT_INTO_JSON_OBJECT(val, doc, difficulty, info.difficulty);
+ INSERT_INTO_JSON_OBJECT(val, doc, target, info.target);
+ INSERT_INTO_JSON_OBJECT(val, doc, tx_count, info.tx_count);
+ INSERT_INTO_JSON_OBJECT(val, doc, tx_pool_size, info.tx_pool_size);
+ INSERT_INTO_JSON_OBJECT(val, doc, alt_blocks_count, info.alt_blocks_count);
+ INSERT_INTO_JSON_OBJECT(val, doc, outgoing_connections_count, info.outgoing_connections_count);
+ INSERT_INTO_JSON_OBJECT(val, doc, incoming_connections_count, info.incoming_connections_count);
+ INSERT_INTO_JSON_OBJECT(val, doc, white_peerlist_size, info.white_peerlist_size);
+ INSERT_INTO_JSON_OBJECT(val, doc, grey_peerlist_size, info.grey_peerlist_size);
+ INSERT_INTO_JSON_OBJECT(val, doc, testnet, info.testnet);
+ INSERT_INTO_JSON_OBJECT(val, doc, top_block_hash, info.top_block_hash);
+ INSERT_INTO_JSON_OBJECT(val, doc, cumulative_difficulty, info.cumulative_difficulty);
+ INSERT_INTO_JSON_OBJECT(val, doc, block_size_limit, info.block_size_limit);
+ INSERT_INTO_JSON_OBJECT(val, doc, start_time, info.start_time);
+}
+
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::DaemonInfo& info)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ GET_FROM_JSON_OBJECT(val, info.height, height);
+ GET_FROM_JSON_OBJECT(val, info.target_height, target_height);
+ GET_FROM_JSON_OBJECT(val, info.difficulty, difficulty);
+ GET_FROM_JSON_OBJECT(val, info.target, target);
+ GET_FROM_JSON_OBJECT(val, info.tx_count, tx_count);
+ GET_FROM_JSON_OBJECT(val, info.tx_pool_size, tx_pool_size);
+ GET_FROM_JSON_OBJECT(val, info.alt_blocks_count, alt_blocks_count);
+ GET_FROM_JSON_OBJECT(val, info.outgoing_connections_count, outgoing_connections_count);
+ GET_FROM_JSON_OBJECT(val, info.incoming_connections_count, incoming_connections_count);
+ GET_FROM_JSON_OBJECT(val, info.white_peerlist_size, white_peerlist_size);
+ GET_FROM_JSON_OBJECT(val, info.grey_peerlist_size, grey_peerlist_size);
+ GET_FROM_JSON_OBJECT(val, info.testnet, testnet);
+ GET_FROM_JSON_OBJECT(val, info.top_block_hash, top_block_hash);
+ GET_FROM_JSON_OBJECT(val, info.cumulative_difficulty, cumulative_difficulty);
+ GET_FROM_JSON_OBJECT(val, info.block_size_limit, block_size_limit);
+ GET_FROM_JSON_OBJECT(val, info.start_time, start_time);
+}
+
+} // namespace json
+
+} // namespace cryptonote
diff --git a/src/serialization/json_object.h b/src/serialization/json_object.h
new file mode 100644
index 000000000..7b9519c48
--- /dev/null
+++ b/src/serialization/json_object.h
@@ -0,0 +1,371 @@
+// Copyright (c) 2016-2017, 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 "rapidjson/document.h"
+#include "cryptonote_basic/cryptonote_basic.h"
+#include "rpc/message_data_structs.h"
+#include "cryptonote_protocol/cryptonote_protocol_defs.h"
+#include "common/sfinae_helpers.h"
+
+#define OBJECT_HAS_MEMBER_OR_THROW(val, key) \
+ do \
+ { \
+ if (!val.HasMember(key)) \
+ { \
+ throw cryptonote::json::MISSING_KEY(key); \
+ } \
+ } while (0);
+
+#define INSERT_INTO_JSON_OBJECT(jsonVal, doc, key, source) \
+ rapidjson::Value key##Val; \
+ cryptonote::json::toJsonValue(doc, source, key##Val); \
+ jsonVal.AddMember(#key, key##Val, doc.GetAllocator());
+
+#define GET_FROM_JSON_OBJECT(source, dst, key) \
+ OBJECT_HAS_MEMBER_OR_THROW(source, #key) \
+ decltype(dst) dstVal##key; \
+ cryptonote::json::fromJsonValue(source[#key], dstVal##key); \
+ dst = dstVal##key;
+
+namespace cryptonote
+{
+
+namespace json
+{
+
+struct JSON_ERROR : public std::exception
+{
+ protected:
+ JSON_ERROR() { }
+ std::string m;
+
+ public:
+ virtual ~JSON_ERROR() { }
+
+ const char* what() const throw()
+ {
+ return m.c_str();
+ }
+};
+
+struct MISSING_KEY : public JSON_ERROR
+{
+ MISSING_KEY(const char* key)
+ {
+ m = std::string("Key \"") + key + "\" missing from object.";
+ }
+};
+
+struct WRONG_TYPE : public JSON_ERROR
+{
+ WRONG_TYPE(const char* type)
+ {
+ m = std::string("Json value has incorrect type, expected: ") + type;
+ }
+};
+
+struct BAD_INPUT : public JSON_ERROR
+{
+ BAD_INPUT()
+ {
+ m = "An item failed to convert from json object to native object";
+ }
+};
+
+struct PARSE_FAIL : public JSON_ERROR
+{
+ PARSE_FAIL()
+ {
+ m = "Failed to parse the json request";
+ }
+};
+
+template<typename Type>
+inline constexpr bool is_to_hex()
+{
+ return std::is_pod<Type>() && !std::is_integral<Type>();
+}
+
+
+// POD to json value
+template <class Type>
+typename std::enable_if<is_to_hex<Type>()>::type toJsonValue(rapidjson::Document& doc, const Type& pod, rapidjson::Value& value)
+{
+ value = rapidjson::Value(epee::string_tools::pod_to_hex(pod).c_str(), doc.GetAllocator());
+}
+
+template <class Type>
+typename std::enable_if<is_to_hex<Type>()>::type fromJsonValue(const rapidjson::Value& val, Type& t)
+{
+ if (!val.IsString())
+ {
+ throw WRONG_TYPE("string");
+ }
+
+ //TODO: handle failure to convert hex string to POD type
+ bool success = epee::string_tools::hex_to_pod(val.GetString(), t);
+
+ if (!success)
+ {
+ throw BAD_INPUT();
+ }
+}
+
+void toJsonValue(rapidjson::Document& doc, const std::string& i, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, std::string& str);
+
+void toJsonValue(rapidjson::Document& doc, bool i, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, bool& b);
+
+// integers overloads for toJsonValue are not needed for standard promotions
+
+void fromJsonValue(const rapidjson::Value& val, unsigned char& i);
+
+void fromJsonValue(const rapidjson::Value& val, signed char& i);
+
+void fromJsonValue(const rapidjson::Value& val, char& i);
+
+void fromJsonValue(const rapidjson::Value& val, unsigned short& i);
+
+void fromJsonValue(const rapidjson::Value& val, short& i);
+
+void toJsonValue(rapidjson::Document& doc, const unsigned i, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, unsigned& i);
+
+void toJsonValue(rapidjson::Document& doc, const int, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, int& i);
+
+
+void toJsonValue(rapidjson::Document& doc, const unsigned long long i, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, unsigned long long& i);
+
+void toJsonValue(rapidjson::Document& doc, const long long i, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, long long& i);
+
+inline void toJsonValue(rapidjson::Document& doc, const unsigned long i, rapidjson::Value& val) {
+ toJsonValue(doc, static_cast<unsigned long long>(i), val);
+}
+void fromJsonValue(const rapidjson::Value& val, unsigned long& i);
+
+inline void toJsonValue(rapidjson::Document& doc, const long i, rapidjson::Value& val) {
+ toJsonValue(doc, static_cast<long long>(i), val);
+}
+void fromJsonValue(const rapidjson::Value& val, long& i);
+
+// end integers
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::transaction& tx, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::transaction& tx);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::block& b, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::block& b);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_v& txin, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_v& txin);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_gen& txin, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_gen& txin);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_to_script& txin, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_to_script& txin);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_to_scripthash& txin, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_to_scripthash& txin);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::txin_to_key& txin, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_to_key& txin);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::txout_target_v& txout, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_target_v& txout);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::txout_to_script& txout, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_script& txout);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::txout_to_scripthash& txout, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_scripthash& txout);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::txout_to_key& txout, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_key& txout);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::tx_out& txout, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::tx_out& txout);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::connection_info& info, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::connection_info& info);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::block_complete_entry& blk, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::block_complete_entry& blk);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::block_with_transactions& blk, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::block_with_transactions& blk);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::transaction_info& tx_info, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::transaction_info& tx_info);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::output_key_and_amount_index& out, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_key_and_amount_index& out);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::amount_with_random_outputs& out, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::amount_with_random_outputs& out);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::peer& peer, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::peer& peer);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::tx_in_pool& tx, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::tx_in_pool& tx);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::hard_fork_info& info, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::hard_fork_info& info);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::output_amount_count& out, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_amount_count& out);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::output_amount_and_index& out, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_amount_and_index& out);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::output_key_mask_unlocked& out, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_key_mask_unlocked& out);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::error& err, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::error& error);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::BlockHeaderResponse& response, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::BlockHeaderResponse& response);
+
+void toJsonValue(rapidjson::Document& doc, const rct::rctSig& i, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& i, rct::rctSig& sig);
+
+void toJsonValue(rapidjson::Document& doc, const rct::ctkey& key, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, rct::ctkey& key);
+
+void toJsonValue(rapidjson::Document& doc, const rct::ecdhTuple& tuple, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, rct::ecdhTuple& tuple);
+
+void toJsonValue(rapidjson::Document& doc, const rct::rctSigPrunable& sig, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, rct::rctSigPrunable& sig);
+
+void toJsonValue(rapidjson::Document& doc, const rct::rangeSig& sig, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, rct::rangeSig& sig);
+
+void toJsonValue(rapidjson::Document& doc, const rct::boroSig& sig, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, rct::boroSig& sig);
+
+void toJsonValue(rapidjson::Document& doc, const rct::mgSig& sig, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, rct::mgSig& sig);
+
+void toJsonValue(rapidjson::Document& doc, const cryptonote::rpc::DaemonInfo& info, rapidjson::Value& val);
+void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::DaemonInfo& info);
+
+template <typename Map>
+typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type toJsonValue(rapidjson::Document& doc, const Map& map, rapidjson::Value& val);
+
+template <typename Map>
+typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type fromJsonValue(const rapidjson::Value& val, Map& map);
+
+template <typename Vec>
+typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type toJsonValue(rapidjson::Document& doc, const Vec &vec, rapidjson::Value& val);
+
+template <typename Vec>
+typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type fromJsonValue(const rapidjson::Value& val, Vec& vec);
+
+
+// ideally would like to have the below functions in the .cpp file, but
+// unfortunately because of how templates work they have to be here.
+
+template <typename Map>
+typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type toJsonValue(rapidjson::Document& doc, const Map& map, rapidjson::Value& val)
+{
+ val.SetObject();
+
+ auto& al = doc.GetAllocator();
+
+ for (const auto& i : map)
+ {
+ rapidjson::Value k;
+ rapidjson::Value m;
+ toJsonValue(doc, i.first, k);
+ toJsonValue(doc, i.second, m);
+ val.AddMember(k, m, al);
+ }
+}
+
+template <typename Map>
+typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type fromJsonValue(const rapidjson::Value& val, Map& map)
+{
+ if (!val.IsObject())
+ {
+ throw WRONG_TYPE("json object");
+ }
+
+ auto itr = val.MemberBegin();
+
+ while (itr != val.MemberEnd())
+ {
+ typename Map::key_type k;
+ typename Map::mapped_type m;
+ fromJsonValue(itr->name, k);
+ fromJsonValue(itr->value, m);
+ map.emplace(k, m);
+ ++itr;
+ }
+}
+
+template <typename Vec>
+typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type toJsonValue(rapidjson::Document& doc, const Vec &vec, rapidjson::Value& val)
+{
+ val.SetArray();
+
+ for (const auto& t : vec)
+ {
+ rapidjson::Value v;
+ toJsonValue(doc, t, v);
+ val.PushBack(v, doc.GetAllocator());
+ }
+}
+
+template <typename Vec>
+typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type fromJsonValue(const rapidjson::Value& val, Vec& vec)
+{
+ if (!val.IsArray())
+ {
+ throw WRONG_TYPE("json array");
+ }
+
+ for (rapidjson::SizeType i=0; i < val.Size(); i++)
+ {
+ typename Vec::value_type v;
+ fromJsonValue(val[i], v);
+ vec.push_back(v);
+ }
+}
+
+} // namespace json
+
+} // namespace cryptonote
diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp
index 6d5ebc25a..857e2af6e 100644
--- a/src/simplewallet/simplewallet.cpp
+++ b/src/simplewallet/simplewallet.cpp
@@ -48,6 +48,7 @@
#include "common/util.h"
#include "common/dns_utils.h"
#include "common/base58.h"
+#include "common/scoped_message_writer.h"
#include "p2p/net_node.h"
#include "cryptonote_protocol/cryptonote_protocol_handler.h"
#include "simplewallet.h"
@@ -63,12 +64,7 @@
#include <stdexcept>
#ifdef HAVE_READLINE
- #include "readline_buffer.h"
- #define PAUSE_READLINE() \
- rdln::suspend_readline pause_readline; \
- std::cout << std::endl
-#else
- #define PAUSE_READLINE()
+#include "readline_buffer.h"
#endif
using namespace std;
@@ -116,7 +112,9 @@ namespace
const command_line::arg_descriptor<std::string> arg_generate_new_wallet = {"generate-new-wallet", sw::tr("Generate new wallet and save it to <arg>"), ""};
const command_line::arg_descriptor<std::string> arg_generate_from_view_key = {"generate-from-view-key", sw::tr("Generate incoming-only wallet from view key"), ""};
const command_line::arg_descriptor<std::string> arg_generate_from_keys = {"generate-from-keys", sw::tr("Generate wallet from private keys"), ""};
+ const command_line::arg_descriptor<std::string> arg_generate_from_multisig_keys = {"generate-from-multisig-keys", sw::tr("Generate a master wallet from multisig wallet keys"), ""};
const auto arg_generate_from_json = wallet_args::arg_generate_from_json();
+ const command_line::arg_descriptor<std::string> arg_mnemonic_language = {"mnemonic-language", sw::tr("Language for mnemonic"), ""};
const command_line::arg_descriptor<std::string> arg_electrum_seed = {"electrum-seed", sw::tr("Specify Electrum seed for wallet recovery/creation"), ""};
const command_line::arg_descriptor<bool> arg_restore_deterministic_wallet = {"restore-deterministic-wallet", sw::tr("Recover wallet using Electrum-style mnemonic seed"), false};
const command_line::arg_descriptor<bool> arg_non_deterministic = {"non-deterministic", sw::tr("Create non-deterministic view and spend keys"), false};
@@ -147,83 +145,19 @@ namespace
return err;
}
- class message_writer
+ tools::scoped_message_writer success_msg_writer(bool color = false)
{
- public:
- message_writer(console_colors color = console_color_default, bool bright = false,
- std::string&& prefix = std::string(), el::Level log_level = el::Level::Info)
- : m_flush(true)
- , m_color(color)
- , m_bright(bright)
- , m_log_level(log_level)
- {
- m_oss << prefix;
- }
-
- message_writer(message_writer&& rhs)
- : m_flush(std::move(rhs.m_flush))
-#if defined(_MSC_VER)
- , m_oss(std::move(rhs.m_oss))
-#else
- // GCC bug: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316
- , m_oss(rhs.m_oss.str(), ios_base::out | ios_base::ate)
-#endif
- , m_color(std::move(rhs.m_color))
- , m_log_level(std::move(rhs.m_log_level))
- {
- rhs.m_flush = false;
- }
-
- template<typename T>
- std::ostream& operator<<(const T& val)
- {
- m_oss << val;
- return m_oss;
- }
-
- ~message_writer()
- {
- if (m_flush)
- {
- m_flush = false;
-
- MCLOG(m_log_level, "global", m_oss.str());
-
- if (console_color_default == m_color)
- {
- std::cout << m_oss.str();
- }
- else
- {
- set_console_color(m_color, m_bright);
- std::cout << m_oss.str();
- reset_console_color();
- }
- std::cout << std::endl;
- }
- }
-
- private:
- message_writer(message_writer& rhs);
- message_writer& operator=(message_writer& rhs);
- message_writer& operator=(message_writer&& rhs);
-
- private:
- bool m_flush;
- std::stringstream m_oss;
- console_colors m_color;
- bool m_bright;
- el::Level m_log_level;
- };
+ return tools::scoped_message_writer(color ? console_color_green : console_color_default, false, std::string(), el::Level::Info);
+ }
- message_writer success_msg_writer(bool color = false)
+ tools::scoped_message_writer message_writer(epee::console_colors color = epee::console_color_default, bool bright = false)
{
- return message_writer(color ? console_color_green : console_color_default, false, std::string(), el::Level::Info);
+ return tools::scoped_message_writer(color, bright);
}
- message_writer fail_msg_writer()
+ tools::scoped_message_writer fail_msg_writer()
{
- return message_writer(console_color_red, true, sw::tr("Error: "), el::Level::Error);
+ return tools::scoped_message_writer(console_color_red, true, sw::tr("Error: "), el::Level::Error);
}
bool is_it_true(const std::string& s)
@@ -281,6 +215,42 @@ namespace
{
return boost::lexical_cast<std::string>(version >> 16) + "." + boost::lexical_cast<std::string>(version & 0xffff);
}
+
+ std::string oa_prompter(const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid)
+ {
+ if (addresses.empty())
+ return {};
+ // prompt user for confirmation.
+ // inform user of DNSSEC validation status as well.
+ std::string dnssec_str;
+ if (dnssec_valid)
+ {
+ dnssec_str = tr("DNSSEC validation passed");
+ }
+ else
+ {
+ dnssec_str = tr("WARNING: DNSSEC validation was unsuccessful, this address may not be correct!");
+ }
+ std::stringstream prompt;
+ prompt << tr("For URL: ") << url
+ << ", " << dnssec_str << std::endl
+ << tr(" Monero Address = ") << addresses[0]
+ << std::endl
+ << tr("Is this OK? (Y/n) ")
+ ;
+ // prompt the user for confirmation given the dns query and dnssec status
+ std::string confirm_dns_ok = command_line::input_line(prompt.str());
+ if (std::cin.eof())
+ {
+ return {};
+ }
+ if (!command_line::is_yes(confirm_dns_ok))
+ {
+ std::cout << tr("you have cancelled the transfer request") << std::endl;
+ return {};
+ }
+ return addresses[0];
+ }
}
@@ -421,6 +391,61 @@ bool simple_wallet::payment_id(const std::vector<std::string> &args/* = std::vec
return true;
}
+bool simple_wallet::print_fee_info(const std::vector<std::string> &args/* = std::vector<std::string>()*/)
+{
+ if (!try_connect_to_daemon())
+ {
+ fail_msg_writer() << tr("Cannot connect to daemon");
+ return true;
+ }
+ const uint64_t per_kb_fee = m_wallet->get_per_kb_fee();
+ const uint64_t typical_size_kb = 13;
+ message_writer() << (boost::format(tr("Current fee is %s monero per kB")) % print_money(per_kb_fee)).str();
+
+ std::vector<uint64_t> fees;
+ for (uint32_t priority = 1; priority <= 4; ++priority)
+ {
+ uint64_t mult = m_wallet->get_fee_multiplier(priority);
+ fees.push_back(per_kb_fee * typical_size_kb * mult);
+ }
+ std::vector<std::pair<uint64_t, uint64_t>> blocks;
+ try
+ {
+ uint64_t base_size = typical_size_kb * 1024;
+ blocks = m_wallet->estimate_backlog(base_size, base_size + 1023, fees);
+ }
+ catch (const std::exception &e)
+ {
+ fail_msg_writer() << tr("Error: failed to estimate backlog array size: ") << e.what();
+ return true;
+ }
+ if (blocks.size() != 4)
+ {
+ fail_msg_writer() << tr("Error: bad estimated backlog array size");
+ return true;
+ }
+
+ for (uint32_t priority = 1; priority <= 4; ++priority)
+ {
+ uint64_t nblocks_low = blocks[priority - 1].first;
+ uint64_t nblocks_high = blocks[priority - 1].second;
+ if (nblocks_low > 0)
+ {
+ std::string msg;
+ if (priority == m_wallet->get_default_priority() || (m_wallet->get_default_priority() == 0 && priority == 2))
+ msg = tr(" (current)");
+ uint64_t minutes_low = nblocks_low * DIFFICULTY_TARGET_V2 / 60, minutes_high = nblocks_high * DIFFICULTY_TARGET_V2 / 60;
+ if (nblocks_high == nblocks_low)
+ message_writer() << (boost::format(tr("%u block (%u minutes) backlog at priority %u%s")) % nblocks_low % minutes_low % priority % msg).str();
+ else
+ message_writer() << (boost::format(tr("%u to %u block (%u to %u minutes) backlog at priority %u")) % nblocks_low % nblocks_high % minutes_low % minutes_high % priority).str();
+ }
+ else
+ message_writer() << tr("No backlog at priority ") << priority;
+ }
+ return true;
+}
+
bool simple_wallet::set_always_confirm_transfers(const std::vector<std::string> &args/* = std::vector<std::string>()*/)
{
const auto pwd_container = get_and_verify_password();
@@ -460,7 +485,7 @@ bool simple_wallet::set_store_tx_info(const std::vector<std::string> &args/* = s
return true;
}
-bool simple_wallet::set_default_mixin(const std::vector<std::string> &args/* = std::vector<std::string>()*/)
+bool simple_wallet::set_default_ring_size(const std::vector<std::string> &args/* = std::vector<std::string>()*/)
{
if (m_wallet->watch_only())
{
@@ -471,34 +496,34 @@ bool simple_wallet::set_default_mixin(const std::vector<std::string> &args/* = s
{
if (strchr(args[1].c_str(), '-'))
{
- fail_msg_writer() << tr("mixin must be an integer >= 2");
+ fail_msg_writer() << tr("ring size must be an integer >= 3");
return true;
}
- uint32_t mixin = boost::lexical_cast<uint32_t>(args[1]);
- if (mixin < 2 && mixin != 0)
+ uint32_t ring_size = boost::lexical_cast<uint32_t>(args[1]);
+ if (ring_size < 3 && ring_size != 0)
{
- fail_msg_writer() << tr("mixin must be an integer >= 2");
+ fail_msg_writer() << tr("ring size must be an integer >= 3");
return true;
}
- if (mixin == 0)
- mixin = DEFAULT_MIX;
+ if (ring_size == 0)
+ ring_size = DEFAULT_MIX + 1;
const auto pwd_container = get_and_verify_password();
if (pwd_container)
{
- m_wallet->default_mixin(mixin);
+ m_wallet->default_mixin(ring_size - 1);
m_wallet->rewrite(m_wallet_file, pwd_container->password());
}
return true;
}
catch(const boost::bad_lexical_cast &)
{
- fail_msg_writer() << tr("mixin must be an integer >= 2");
+ fail_msg_writer() << tr("ring size must be an integer >= 3");
return true;
}
catch(...)
{
- fail_msg_writer() << tr("could not change default mixin");
+ fail_msg_writer() << tr("could not change default ring size");
return true;
}
}
@@ -680,6 +705,17 @@ bool simple_wallet::set_merge_destinations(const std::vector<std::string> &args/
return true;
}
+bool simple_wallet::set_confirm_backlog(const std::vector<std::string> &args/* = std::vector<std::string>()*/)
+{
+ const auto pwd_container = get_and_verify_password();
+ if (pwd_container)
+ {
+ m_wallet->confirm_backlog(is_it_true(args[1]));
+ m_wallet->rewrite(m_wallet_file, pwd_container->password());
+ }
+ return true;
+}
+
bool simple_wallet::help(const std::vector<std::string> &args/* = std::vector<std::string>()*/)
{
success_msg_writer() << get_commands_str();
@@ -703,12 +739,12 @@ simple_wallet::simple_wallet()
m_cmd_binder.set_handler("payments", boost::bind(&simple_wallet::show_payments, this, _1), tr("payments <PID_1> [<PID_2> ... <PID_N>] - Show payments for given payment ID[s]"));
m_cmd_binder.set_handler("bc_height", boost::bind(&simple_wallet::show_blockchain_height, this, _1), tr("Show blockchain height"));
m_cmd_binder.set_handler("transfer_original", boost::bind(&simple_wallet::transfer, this, _1), tr("Same as transfer, but using an older transaction building algorithm"));
- m_cmd_binder.set_handler("transfer", boost::bind(&simple_wallet::transfer_new, this, _1), tr("transfer [<priority>] [<mixin_count>] <address> <amount> [<payment_id>] - Transfer <amount> to <address>. <priority> is the priority of the transaction. The higher the priority, the higher the fee of the transaction. Valid values in priority order (from lowest to highest) are: unimportant, normal, elevated, priority. If omitted, the default value (see the command \"set priority\") is used. <mixin_count> is the number of extra inputs to include for untraceability. Multiple payments can be made at once by adding <address_2> <amount_2> etcetera (before the payment ID, if it's included)"));
- m_cmd_binder.set_handler("locked_transfer", boost::bind(&simple_wallet::locked_transfer, this, _1), tr("locked_transfer [<mixin_count>] <addr> <amount> <lockblocks>(Number of blocks to lock the transaction for, max 1000000) [<payment_id>]"));
- m_cmd_binder.set_handler("sweep_unmixable", boost::bind(&simple_wallet::sweep_unmixable, this, _1), tr("Send all unmixable outputs to yourself with mixin 0"));
- m_cmd_binder.set_handler("sweep_all", boost::bind(&simple_wallet::sweep_all, this, _1), tr("sweep_all [mixin] address [payment_id] - Send all unlocked balance to an address"));
- m_cmd_binder.set_handler("sweep_below", boost::bind(&simple_wallet::sweep_below, this, _1), tr("sweep_below <amount_threshold> [mixin] address [payment_id] - Send all unlocked outputs below the threshold to an address"));
- m_cmd_binder.set_handler("donate", boost::bind(&simple_wallet::donate, this, _1), tr("donate [<mixin_count>] <amount> [payment_id] - Donate <amount> to the development team (donate.getmonero.org)"));
+ m_cmd_binder.set_handler("transfer", boost::bind(&simple_wallet::transfer_new, this, _1), tr("transfer [<priority>] [<ring_size>] <address> <amount> [<payment_id>] - Transfer <amount> to <address>. <priority> is the priority of the transaction. The higher the priority, the higher the fee of the transaction. Valid values in priority order (from lowest to highest) are: unimportant, normal, elevated, priority. If omitted, the default value (see the command \"set priority\") is used. <ring_size> is the number of inputs to include for untraceability. Multiple payments can be made at once by adding <address_2> <amount_2> etcetera (before the payment ID, if it's included)"));
+ m_cmd_binder.set_handler("locked_transfer", boost::bind(&simple_wallet::locked_transfer, this, _1), tr("locked_transfer [<ring_size>] <addr> <amount> <lockblocks>(Number of blocks to lock the transaction for, max 1000000) [<payment_id>]"));
+ m_cmd_binder.set_handler("sweep_unmixable", boost::bind(&simple_wallet::sweep_unmixable, this, _1), tr("Send all unmixable outputs to yourself with ring_size 1"));
+ m_cmd_binder.set_handler("sweep_all", boost::bind(&simple_wallet::sweep_all, this, _1), tr("sweep_all [ring_size] address [payment_id] - Send all unlocked balance to an address"));
+ m_cmd_binder.set_handler("sweep_below", boost::bind(&simple_wallet::sweep_below, this, _1), tr("sweep_below <amount_threshold> [ring_size] address [payment_id] - Send all unlocked outputs below the threshold to an address"));
+ m_cmd_binder.set_handler("donate", boost::bind(&simple_wallet::donate, this, _1), tr("donate [<ring_size>] <amount> [payment_id] - Donate <amount> to the development team (donate.getmonero.org)"));
m_cmd_binder.set_handler("sign_transfer", boost::bind(&simple_wallet::sign_transfer, this, _1), tr("Sign a transaction from a file"));
m_cmd_binder.set_handler("submit_transfer", boost::bind(&simple_wallet::submit_transfer, this, _1), tr("Submit a signed transaction from a file"));
m_cmd_binder.set_handler("set_log", boost::bind(&simple_wallet::set_log, this, _1), tr("set_log <level>|<categories> - Change current log detail (level must be <0-4>)"));
@@ -720,7 +756,7 @@ simple_wallet::simple_wallet()
m_cmd_binder.set_handler("viewkey", boost::bind(&simple_wallet::viewkey, this, _1), tr("Display private view key"));
m_cmd_binder.set_handler("spendkey", boost::bind(&simple_wallet::spendkey, this, _1), tr("Display private spend key"));
m_cmd_binder.set_handler("seed", boost::bind(&simple_wallet::seed, this, _1), tr("Display Electrum-style mnemonic seed"));
- m_cmd_binder.set_handler("set", boost::bind(&simple_wallet::set_variable, this, _1), tr("Available options: seed language - set wallet seed language; always-confirm-transfers <1|0> - whether to confirm unsplit txes; print-ring-members <1|0> - whether to print detailed information about ring members during confirmation; store-tx-info <1|0> - whether to store outgoing tx info (destination address, payment ID, tx secret key) for future reference; default-mixin <n> - set default mixin (default is 4); auto-refresh <1|0> - whether to automatically sync new blocks from the daemon; refresh-type <full|optimize-coinbase|no-coinbase|default> - set wallet refresh behaviour; priority [0|1|2|3|4] - default/unimportant/normal/elevated/priority fee; confirm-missing-payment-id <1|0>; ask-password <1|0>; unit <monero|millinero|micronero|nanonero|piconero> - set default monero (sub-)unit; min-outputs-count [n] - try to keep at least that many outputs of value at least min-outputs-value; min-outputs-value [n] - try to keep at least min-outputs-count outputs of at least that value; merge-destinations <1|0> - whether to merge multiple payments to the same destination address"));
+ m_cmd_binder.set_handler("set", boost::bind(&simple_wallet::set_variable, this, _1), tr("Available options: seed language - set wallet seed language; always-confirm-transfers <1|0> - whether to confirm unsplit txes; print-ring-members <1|0> - whether to print detailed information about ring members during confirmation; store-tx-info <1|0> - whether to store outgoing tx info (destination address, payment ID, tx secret key) for future reference; default-ring-size <n> - set default ring size (default is 5); auto-refresh <1|0> - whether to automatically sync new blocks from the daemon; refresh-type <full|optimize-coinbase|no-coinbase|default> - set wallet refresh behaviour; priority [0|1|2|3|4] - default/unimportant/normal/elevated/priority fee; confirm-missing-payment-id <1|0>; ask-password <1|0>; unit <monero|millinero|micronero|nanonero|piconero> - set default monero (sub-)unit; min-outputs-count [n] - try to keep at least that many outputs of value at least min-outputs-value; min-outputs-value [n] - try to keep at least min-outputs-count outputs of at least that value; merge-destinations <1|0> - whether to merge multiple payments to the same destination address; confirm-backlog <1|0> - whether to warn if there is transaction backlog"));
m_cmd_binder.set_handler("rescan_spent", boost::bind(&simple_wallet::rescan_spent, this, _1), tr("Rescan blockchain for spent outputs"));
m_cmd_binder.set_handler("get_tx_key", boost::bind(&simple_wallet::get_tx_key, this, _1), tr("Get transaction key (r) for a given <txid>"));
m_cmd_binder.set_handler("check_tx_key", boost::bind(&simple_wallet::check_tx_key, this, _1), tr("Check amount going to <address> in <txid>"));
@@ -741,6 +777,7 @@ simple_wallet::simple_wallet()
m_cmd_binder.set_handler("show_transfer", boost::bind(&simple_wallet::show_transfer, this, _1), tr("Show information about a transfer to/from this address"));
m_cmd_binder.set_handler("password", boost::bind(&simple_wallet::change_password, this, _1), tr("Change wallet password"));
m_cmd_binder.set_handler("payment_id", boost::bind(&simple_wallet::payment_id, this, _1), tr("Generate a new random full size payment id - these will be unencrypted on the blockchain, see integrated_address for encrypted short payment ids"));
+ m_cmd_binder.set_handler("fee", boost::bind(&simple_wallet::print_fee_info, this, _1), tr("Print information about fee and current transaction backlog"));
m_cmd_binder.set_handler("help", boost::bind(&simple_wallet::help, this, _1), tr("Show this help"));
}
//----------------------------------------------------------------------------------------------------
@@ -752,7 +789,7 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
success_msg_writer() << "always-confirm-transfers = " << m_wallet->always_confirm_transfers();
success_msg_writer() << "print-ring-members = " << m_wallet->print_ring_members();
success_msg_writer() << "store-tx-info = " << m_wallet->store_tx_info();
- success_msg_writer() << "default-mixin = " << m_wallet->default_mixin();
+ success_msg_writer() << "default-ring-size = " << (m_wallet->default_mixin() ? m_wallet->default_mixin() + 1 : 0);
success_msg_writer() << "auto-refresh = " << m_wallet->auto_refresh();
success_msg_writer() << "refresh-type = " << get_refresh_type_name(m_wallet->get_refresh_type());
success_msg_writer() << "priority = " << m_wallet->get_default_priority();
@@ -762,6 +799,7 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
success_msg_writer() << "min-outputs-count = " << m_wallet->get_min_output_count();
success_msg_writer() << "min-outputs-value = " << cryptonote::print_money(m_wallet->get_min_output_value());
success_msg_writer() << "merge-destinations = " << m_wallet->merge_destinations();
+ success_msg_writer() << "confirm-backlog = " << m_wallet->confirm_backlog();
return true;
}
else
@@ -797,7 +835,7 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
CHECK_SIMPLE_VARIABLE("always-confirm-transfers", set_always_confirm_transfers, tr("0 or 1"));
CHECK_SIMPLE_VARIABLE("print-ring-members", set_print_ring_members, tr("0 or 1"));
CHECK_SIMPLE_VARIABLE("store-tx-info", set_store_tx_info, tr("0 or 1"));
- CHECK_SIMPLE_VARIABLE("default-mixin", set_default_mixin, tr("integer >= 2"));
+ CHECK_SIMPLE_VARIABLE("default-ring-size", set_default_ring_size, tr("integer >= 3"));
CHECK_SIMPLE_VARIABLE("auto-refresh", set_auto_refresh, tr("0 or 1"));
CHECK_SIMPLE_VARIABLE("refresh-type", set_refresh_type, tr("full (slowest, no assumptions); optimize-coinbase (fast, assumes the whole coinbase is paid to a single address); no-coinbase (fastest, assumes we receive no coinbase transaction), default (same as optimize-coinbase)"));
CHECK_SIMPLE_VARIABLE("priority", set_default_priority, tr("0, 1, 2, 3, or 4"));
@@ -807,6 +845,7 @@ bool simple_wallet::set_variable(const std::vector<std::string> &args)
CHECK_SIMPLE_VARIABLE("min-outputs-count", set_min_output_count, tr("unsigned integer"));
CHECK_SIMPLE_VARIABLE("min-outputs-value", set_min_output_value, tr("amount"));
CHECK_SIMPLE_VARIABLE("merge-destinations", set_merge_destinations, tr("0 or 1"));
+ CHECK_SIMPLE_VARIABLE("confirm-backlog", set_confirm_backlog, tr("0 or 1"));
}
fail_msg_writer() << tr("set: unrecognized argument(s)");
return true;
@@ -882,14 +921,19 @@ bool simple_wallet::ask_wallet_create_if_needed()
}
else if(!wallet_file_exists && !keys_file_exists) //No wallet, no keys
{
- message_writer() << tr(m_restoring ? "Confirm wallet name: " : "No wallet found with that name. Confirm creation of new wallet named: ") << wallet_path;
- confirm_creation = command_line::input_line(tr("(Y/Yes/N/No): "));
- if(std::cin.eof())
+ bool ok = true;
+ if (!m_restoring)
{
- LOG_ERROR("Unexpected std::cin.eof() - Exited simple_wallet::ask_wallet_create_if_needed()");
- return false;
+ message_writer() << tr("No wallet found with that name. Confirm creation of new wallet named: ") << wallet_path;
+ confirm_creation = command_line::input_line(tr("(Y/Yes/N/No): "));
+ if(std::cin.eof())
+ {
+ LOG_ERROR("Unexpected std::cin.eof() - Exited simple_wallet::ask_wallet_create_if_needed()");
+ return false;
+ }
+ ok = command_line::is_yes(confirm_creation);
}
- if(command_line::is_yes(confirm_creation))
+ if (ok)
{
success_msg_writer() << tr("Generating new wallet...");
m_generate_new = wallet_path;
@@ -932,12 +976,12 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
if (!handle_command_line(vm))
return false;
- if((!m_generate_new.empty()) + (!m_wallet_file.empty()) + (!m_generate_from_view_key.empty()) + (!m_generate_from_keys.empty()) + (!m_generate_from_json.empty()) > 1)
+ if((!m_generate_new.empty()) + (!m_wallet_file.empty()) + (!m_generate_from_view_key.empty()) + (!m_generate_from_keys.empty()) + (!m_generate_from_multisig_keys.empty()) + (!m_generate_from_json.empty()) > 1)
{
- fail_msg_writer() << tr("can't specify more than one of --generate-new-wallet=\"wallet_name\", --wallet-file=\"wallet_name\", --generate-from-view-key=\"wallet_name\", --generate-from-json=\"jsonfilename\" and --generate-from-keys=\"wallet_name\"");
+ fail_msg_writer() << tr("can't specify more than one of --generate-new-wallet=\"wallet_name\", --wallet-file=\"wallet_name\", --generate-from-view-key=\"wallet_name\", --generate-from-keys=\"wallet_name\", --generate-from-multisig-keys=\"wallet_name\" and --generate-from-json=\"jsonfilename\"");
return false;
}
- else if (m_generate_new.empty() && m_wallet_file.empty() && m_generate_from_view_key.empty() && m_generate_from_keys.empty() && m_generate_from_json.empty())
+ else if (m_generate_new.empty() && m_wallet_file.empty() && m_generate_from_view_key.empty() && m_generate_from_keys.empty() && m_generate_from_multisig_keys.empty() && m_generate_from_json.empty())
{
if(!ask_wallet_create_if_needed()) return false;
}
@@ -1056,7 +1100,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
}
// parse spend secret key
- std::string spendkey_string = command_line::input_line("Spend key: ");
+ std::string spendkey_string = command_line::input_line("Secret spend key: ");
if (std::cin.eof())
return false;
if (spendkey_string.empty()) {
@@ -1072,7 +1116,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
crypto::secret_key spendkey = *reinterpret_cast<const crypto::secret_key*>(spendkey_data.data());
// parse view secret key
- std::string viewkey_string = command_line::input_line("View key: ");
+ std::string viewkey_string = command_line::input_line("Secret view key: ");
if (std::cin.eof())
return false;
if (viewkey_string.empty()) {
@@ -1110,6 +1154,143 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
bool r = new_wallet(vm, address, spendkey, viewkey);
CHECK_AND_ASSERT_MES(r, false, tr("account creation failed"));
}
+
+ // Asks user for all the data required to merge secret keys from multisig wallets into one master wallet, which then gets full control of the multisig wallet. The resulting wallet will be the same as any other regular wallet.
+ else if (!m_generate_from_multisig_keys.empty())
+ {
+ m_wallet_file = m_generate_from_multisig_keys;
+ unsigned int multisig_m;
+ unsigned int multisig_n;
+
+ // parse multisig type
+ std::string multisig_type_string = command_line::input_line("Multisig type (input as M/N with M <= N and M > 1): ");
+ if (std::cin.eof())
+ return false;
+ if (multisig_type_string.empty())
+ {
+ fail_msg_writer() << tr("No data supplied, cancelled");
+ return false;
+ }
+ if (sscanf(multisig_type_string.c_str(), "%u/%u", &multisig_m, &multisig_n) != 2)
+ {
+ fail_msg_writer() << tr("Error: expected M/N, but got: ") << multisig_type_string;
+ return false;
+ }
+ if (multisig_m <= 1 || multisig_m > multisig_n)
+ {
+ fail_msg_writer() << tr("Error: expected N > 1 and N <= M, but got: ") << multisig_type_string;
+ return false;
+ }
+ if (multisig_m != multisig_n)
+ {
+ fail_msg_writer() << tr("Error: M/N is currently unsupported. ");
+ return false;
+ }
+ message_writer() << boost::format(tr("Generating master wallet from %u of %u multisig wallet keys")) % multisig_m % multisig_n;
+
+ // parse multisig address
+ std::string address_string = command_line::input_line("Multisig wallet address: ");
+ if (std::cin.eof())
+ return false;
+ if (address_string.empty()) {
+ fail_msg_writer() << tr("No data supplied, cancelled");
+ return false;
+ }
+ cryptonote::account_public_address address;
+ bool has_payment_id;
+ crypto::hash8 new_payment_id;
+ if(!get_account_integrated_address_from_str(address, has_payment_id, new_payment_id, tools::wallet2::has_testnet_option(vm), address_string))
+ {
+ fail_msg_writer() << tr("failed to parse address");
+ return false;
+ }
+
+ // parse secret view key
+ std::string viewkey_string = command_line::input_line("Secret view key: ");
+ if (std::cin.eof())
+ return false;
+ if (viewkey_string.empty())
+ {
+ fail_msg_writer() << tr("No data supplied, cancelled");
+ return false;
+ }
+ cryptonote::blobdata viewkey_data;
+ if(!epee::string_tools::parse_hexstr_to_binbuff(viewkey_string, viewkey_data) || viewkey_data.size() != sizeof(crypto::secret_key))
+ {
+ fail_msg_writer() << tr("failed to parse secret view key");
+ return false;
+ }
+ crypto::secret_key viewkey = *reinterpret_cast<const crypto::secret_key*>(viewkey_data.data());
+
+ // check that the view key matches the given address
+ crypto::public_key pkey;
+ if (!crypto::secret_key_to_public_key(viewkey, pkey))
+ {
+ fail_msg_writer() << tr("failed to verify secret view key");
+ return false;
+ }
+ if (address.m_view_public_key != pkey)
+ {
+ fail_msg_writer() << tr("view key does not match standard address");
+ return false;
+ }
+
+ // parse multisig spend keys
+ crypto::secret_key spendkey;
+ // parsing N/N
+ if(multisig_m == multisig_n)
+ {
+ std::vector<crypto::secret_key> multisig_secret_spendkeys(multisig_n);
+ std::string spendkey_string;
+ cryptonote::blobdata spendkey_data;
+ // get N secret spend keys from user
+ for(unsigned int i=0; i<multisig_n; ++i)
+ {
+ spendkey_string = command_line::input_line(tr((boost::format(tr("Secret spend key (%u of %u):")) % (i+i) % multisig_m).str().c_str()));
+ if (std::cin.eof())
+ return false;
+ if (spendkey_string.empty())
+ {
+ fail_msg_writer() << tr("No data supplied, cancelled");
+ return false;
+ }
+ if(!epee::string_tools::parse_hexstr_to_binbuff(spendkey_string, spendkey_data) || spendkey_data.size() != sizeof(crypto::secret_key))
+ {
+ fail_msg_writer() << tr("failed to parse spend key secret key");
+ return false;
+ }
+ multisig_secret_spendkeys[i] = *reinterpret_cast<const crypto::secret_key*>(spendkey_data.data());
+ }
+
+ // sum the spend keys together to get the master spend key
+ spendkey = multisig_secret_spendkeys[0];
+ for(unsigned int i=1; i<multisig_n; ++i)
+ sc_add(reinterpret_cast<unsigned char*>(&spendkey), reinterpret_cast<unsigned char*>(&spendkey), reinterpret_cast<unsigned char*>(&multisig_secret_spendkeys[i]));
+ }
+ // parsing M/N
+ else
+ {
+ fail_msg_writer() << tr("Error: M/N is currently unsupported");
+ return false;
+ }
+
+ // check that the spend key matches the given address
+ if (!crypto::secret_key_to_public_key(spendkey, pkey))
+ {
+ fail_msg_writer() << tr("failed to verify spend key secret key");
+ return false;
+ }
+ if (address.m_spend_public_key != pkey)
+ {
+ fail_msg_writer() << tr("spend key does not match standard address");
+ return false;
+ }
+
+ // create wallet
+ bool r = new_wallet(vm, address, spendkey, viewkey);
+ CHECK_AND_ASSERT_MES(r, false, tr("account creation failed"));
+ }
+
else if (!m_generate_from_json.empty())
{
m_wallet_file = m_generate_from_json;
@@ -1235,7 +1416,9 @@ bool simple_wallet::handle_command_line(const boost::program_options::variables_
m_generate_new = command_line::get_arg(vm, arg_generate_new_wallet);
m_generate_from_view_key = command_line::get_arg(vm, arg_generate_from_view_key);
m_generate_from_keys = command_line::get_arg(vm, arg_generate_from_keys);
+ m_generate_from_multisig_keys = command_line::get_arg(vm, arg_generate_from_multisig_keys);
m_generate_from_json = command_line::get_arg(vm, arg_generate_from_json);
+ m_mnemonic_language = command_line::get_arg(vm, arg_mnemonic_language);
m_electrum_seed = command_line::get_arg(vm, arg_electrum_seed);
m_restore_deterministic_wallet = command_line::get_arg(vm, arg_restore_deterministic_wallet);
m_non_deterministic = command_line::get_arg(vm, arg_non_deterministic);
@@ -1244,6 +1427,7 @@ bool simple_wallet::handle_command_line(const boost::program_options::variables_
m_restore_height = command_line::get_arg(vm, arg_restore_height);
m_restoring = !m_generate_from_view_key.empty() ||
!m_generate_from_keys.empty() ||
+ !m_generate_from_multisig_keys.empty() ||
!m_generate_from_json.empty() ||
m_restore_deterministic_wallet;
@@ -1342,11 +1526,20 @@ bool simple_wallet::new_wallet(const boost::program_options::variables_map& vm,
crypto::ElectrumWords::get_is_old_style_seed(m_electrum_seed));
std::string mnemonic_language = old_language;
+
+ std::vector<std::string> language_list;
+ crypto::ElectrumWords::get_language_list(language_list);
+ if (mnemonic_language.empty() && std::find(language_list.begin(), language_list.end(), m_mnemonic_language) != language_list.end())
+ {
+ mnemonic_language = m_mnemonic_language;
+ }
+
// Ask for seed language if:
// it's a deterministic wallet AND
+ // a seed language is not already specified AND
// (it is not a wallet restore OR if it was a deprecated wallet
// that was earlier used before this restore)
- if ((!two_random) && (!m_restore_deterministic_wallet || was_deprecated_wallet))
+ if ((!two_random) && (mnemonic_language.empty()) && (!m_restore_deterministic_wallet || was_deprecated_wallet))
{
if (was_deprecated_wallet)
{
@@ -1715,6 +1908,10 @@ bool simple_wallet::refresh_main(uint64_t start_height, bool reset)
if (reset)
m_wallet->rescan_blockchain(false);
+#ifdef HAVE_READLINE
+ rdln::suspend_readline pause_readline;
+#endif
+
message_writer() << tr("Starting refresh...");
uint64_t fetched_blocks = 0;
@@ -2134,7 +2331,8 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri
size_t fake_outs_count;
if(local_args.size() > 0) {
- if(!epee::string_tools::get_xtype_from_string(fake_outs_count, local_args[0]))
+ size_t ring_size;
+ if(!epee::string_tools::get_xtype_from_string(ring_size, local_args[0]))
{
fake_outs_count = m_wallet->default_mixin();
if (fake_outs_count == 0)
@@ -2142,6 +2340,7 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri
}
else
{
+ fake_outs_count = ring_size - 1;
local_args.erase(local_args.begin());
}
}
@@ -2215,7 +2414,7 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri
cryptonote::tx_destination_entry de;
bool has_payment_id;
crypto::hash8 new_payment_id;
- if (!cryptonote::get_account_address_from_str_or_url(de.addr, has_payment_id, new_payment_id, m_wallet->testnet(), local_args[i]))
+ if (!cryptonote::get_account_address_from_str_or_url(de.addr, has_payment_id, new_payment_id, m_wallet->testnet(), local_args[i], oa_prompter))
{
fail_msg_writer() << tr("failed to parse address");
return true;
@@ -2288,6 +2487,7 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri
break;
default:
LOG_ERROR("Unknown transfer method, using original");
+ /* FALLTHRU */
case TransferOriginal:
ptx_vector = m_wallet->create_transactions(dsts, fake_outs_count, 0 /* unlock_time */, priority, extra, m_trusted_daemon);
break;
@@ -2299,6 +2499,56 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri
return true;
}
+ // if we need to check for backlog, check the worst case tx
+ if (m_wallet->confirm_backlog())
+ {
+ std::stringstream prompt;
+ double worst_fee_per_byte = std::numeric_limits<double>::max();
+ uint64_t size = 0, fee = 0;
+ for (size_t n = 0; n < ptx_vector.size(); ++n)
+ {
+ const uint64_t blob_size = cryptonote::tx_to_blob(ptx_vector[n].tx).size();
+ const double fee_per_byte = ptx_vector[n].fee / (double)blob_size;
+ if (fee_per_byte < worst_fee_per_byte)
+ {
+ worst_fee_per_byte = fee_per_byte;
+ fee = ptx_vector[n].fee;
+ }
+ size += blob_size;
+ }
+ try
+ {
+ std::vector<std::pair<uint64_t, uint64_t>> nblocks = m_wallet->estimate_backlog(size, size, {fee});
+ if (nblocks.size() != 1)
+ {
+ prompt << "Internal error checking for backlog. " << tr("Is this okay anyway? (Y/Yes/N/No): ");
+ }
+ else
+ {
+ if (nblocks[0].first > 0)
+ prompt << (boost::format(tr("There is currently a %u block backlog at that fee level. Is this okay? (Y/Yes/N/No)")) % nblocks[0].first).str();
+ }
+ }
+ catch (const std::exception &e)
+ {
+ prompt << tr("Failed to check for backlog: ") << e.what() << ENDL << tr("Is this okay anyway? (Y/Yes/N/No): ");
+ }
+
+ std::string prompt_str = prompt.str();
+ if (!prompt_str.empty())
+ {
+ std::string accepted = command_line::input_line(prompt_str);
+ if (std::cin.eof())
+ return true;
+ if (!command_line::is_yes(accepted))
+ {
+ fail_msg_writer() << tr("transaction cancelled.");
+
+ return true;
+ }
+ }
+ }
+
// if more than one tx necessary, prompt user to confirm
if (m_wallet->always_confirm_transfers() || ptx_vector.size() > 1)
{
@@ -2375,7 +2625,8 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri
{
auto & ptx = ptx_vector.back();
m_wallet->commit_tx(ptx);
- success_msg_writer(true) << tr("Money successfully sent, transaction ") << get_transaction_hash(ptx.tx);
+ success_msg_writer(true) << tr("Transaction successfully submitted, transaction ") << get_transaction_hash(ptx.tx) << ENDL
+ << tr("You can check its status by using the `show_transfers` command.");
// if no exception, remove element from vector
ptx_vector.pop_back();
@@ -2417,10 +2668,10 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri
catch (const tools::error::not_enough_outs_to_mix& e)
{
auto writer = fail_msg_writer();
- writer << tr("not enough outputs for specified mixin_count") << " = " << e.mixin_count() << ":";
+ writer << tr("not enough outputs for specified ring size") << " = " << (e.mixin_count() + 1) << ":";
for (std::pair<uint64_t, uint64_t> outs_for_amount : e.scanty_outs())
{
- writer << "\n" << tr("output amount") << " = " << print_money(outs_for_amount.first) << ", " << tr("found outputs to mix") << " = " << outs_for_amount.second;
+ writer << "\n" << tr("output amount") << " = " << print_money(outs_for_amount.first) << ", " << tr("found outputs to use") << " = " << outs_for_amount.second;
}
}
catch (const tools::error::tx_not_constructed&)
@@ -2594,10 +2845,10 @@ bool simple_wallet::sweep_unmixable(const std::vector<std::string> &args_)
catch (const tools::error::not_enough_outs_to_mix& e)
{
auto writer = fail_msg_writer();
- writer << tr("not enough outputs for specified mixin_count") << " = " << e.mixin_count() << ":";
+ writer << tr("not enough outputs for specified ring size") << " = " << (e.mixin_count() + 1) << ":";
for (std::pair<uint64_t, uint64_t> outs_for_amount : e.scanty_outs())
{
- writer << "\n" << tr("output amount") << " = " << print_money(outs_for_amount.first) << ", " << tr("found outputs to mix") << " = " << outs_for_amount.second;
+ writer << "\n" << tr("output amount") << " = " << print_money(outs_for_amount.first) << ", " << tr("found outputs to use") << " = " << outs_for_amount.second;
}
}
catch (const tools::error::tx_not_constructed&)
@@ -2657,7 +2908,8 @@ bool simple_wallet::sweep_main(uint64_t below, const std::vector<std::string> &a
size_t fake_outs_count;
if(local_args.size() > 0) {
- if(!epee::string_tools::get_xtype_from_string(fake_outs_count, local_args[0]))
+ size_t ring_size;
+ if(!epee::string_tools::get_xtype_from_string(ring_size, local_args[0]))
{
fake_outs_count = m_wallet->default_mixin();
if (fake_outs_count == 0)
@@ -2665,6 +2917,7 @@ bool simple_wallet::sweep_main(uint64_t below, const std::vector<std::string> &a
}
else
{
+ fake_outs_count = ring_size - 1;
local_args.erase(local_args.begin());
}
}
@@ -2713,7 +2966,7 @@ bool simple_wallet::sweep_main(uint64_t below, const std::vector<std::string> &a
bool has_payment_id;
crypto::hash8 new_payment_id;
cryptonote::account_public_address address;
- if (!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, new_payment_id, m_wallet->testnet(), local_args[0]))
+ if (!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, new_payment_id, m_wallet->testnet(), local_args[0], oa_prompter))
{
fail_msg_writer() << tr("failed to parse address");
return true;
@@ -2752,6 +3005,8 @@ bool simple_wallet::sweep_main(uint64_t below, const std::vector<std::string> &a
}
}
+ LOCK_IDLE_SCOPE();
+
try
{
// figure out what tx will be necessary
@@ -2855,10 +3110,10 @@ bool simple_wallet::sweep_main(uint64_t below, const std::vector<std::string> &a
catch (const tools::error::not_enough_outs_to_mix& e)
{
auto writer = fail_msg_writer();
- writer << tr("not enough outputs for specified mixin_count") << " = " << e.mixin_count() << ":";
+ writer << tr("not enough outputs for specified ring size") << " = " << (e.mixin_count() + 1) << ":";
for (std::pair<uint64_t, uint64_t> outs_for_amount : e.scanty_outs())
{
- writer << "\n" << tr("output amount") << " = " << print_money(outs_for_amount.first) << ", " << tr("found outputs to mix") << " = " << outs_for_amount.second;
+ writer << "\n" << tr("output amount") << " = " << print_money(outs_for_amount.first) << ", " << tr("found outputs to use") << " = " << outs_for_amount.second;
}
}
catch (const tools::error::tx_not_constructed&)
@@ -2937,7 +3192,7 @@ bool simple_wallet::donate(const std::vector<std::string> &args_)
fail_msg_writer() << tr("wrong number of arguments");
return true;
}
- std::string mixin_str;
+ std::string ring_size_str;
// Hardcode Monero's donation address (see #1447)
const std::string address_str = "44AFFq5kSiGBoZ4NMDwYtN18obc8AemS33DBLWs3H7otXft3XjrpDtQGv7SqSsaBYBb98uNbr2VBBEt7f2wfn3RVGQBEP3A";
std::string amount_str;
@@ -2951,17 +3206,17 @@ bool simple_wallet::donate(const std::vector<std::string> &args_)
payment_id_str = local_args.back();
local_args.pop_back();
}
- // check mixin
+ // check ring size
if (local_args.size() > 1)
{
- mixin_str = local_args[0];
+ ring_size_str = local_args[0];
local_args.erase(local_args.begin());
}
amount_str = local_args[0];
// refill args as necessary
local_args.clear();
- if (!mixin_str.empty())
- local_args.push_back(mixin_str);
+ if (!ring_size_str.empty())
+ local_args.push_back(ring_size_str);
local_args.push_back(address_str);
local_args.push_back(amount_str);
if (!payment_id_str.empty())
@@ -2975,40 +3230,74 @@ bool simple_wallet::accept_loaded_tx(const std::function<size_t()> get_num_txes,
{
// gather info to ask the user
uint64_t amount = 0, amount_to_dests = 0, change = 0;
- size_t min_mixin = ~0;
- std::unordered_map<std::string, uint64_t> dests;
+ size_t min_ring_size = ~0;
+ std::unordered_map<std::string, std::pair<std::string, uint64_t>> dests;
const std::string wallet_address = m_wallet->get_account().get_public_address_str(m_wallet->testnet());
int first_known_non_zero_change_index = -1;
+ std::string payment_id_string = "";
for (size_t n = 0; n < get_num_txes(); ++n)
{
const tools::wallet2::tx_construction_data &cd = get_tx(n);
+
+ std::vector<tx_extra_field> tx_extra_fields;
+ bool has_encrypted_payment_id = false;
+ crypto::hash8 payment_id8 = cryptonote::null_hash8;
+ if (cryptonote::parse_tx_extra(cd.extra, tx_extra_fields))
+ {
+ tx_extra_nonce extra_nonce;
+ if (find_tx_extra_field_by_type(tx_extra_fields, extra_nonce))
+ {
+ crypto::hash payment_id;
+ if(get_encrypted_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id8))
+ {
+ if (!payment_id_string.empty())
+ payment_id_string += ", ";
+ payment_id_string = std::string("encrypted payment ID ") + epee::string_tools::pod_to_hex(payment_id8);
+ has_encrypted_payment_id = true;
+ }
+ else if (get_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id))
+ {
+ if (!payment_id_string.empty())
+ payment_id_string += ", ";
+ payment_id_string = std::string("unencrypted payment ID ") + epee::string_tools::pod_to_hex(payment_id);
+ }
+ }
+ }
+
for (size_t s = 0; s < cd.sources.size(); ++s)
{
amount += cd.sources[s].amount;
- size_t mixin = cd.sources[s].outputs.size() - 1;
- if (mixin < min_mixin)
- min_mixin = mixin;
+ size_t ring_size = cd.sources[s].outputs.size();
+ if (ring_size < min_ring_size)
+ min_ring_size = ring_size;
}
for (size_t d = 0; d < cd.splitted_dsts.size(); ++d)
{
const tx_destination_entry &entry = cd.splitted_dsts[d];
- std::string address = get_account_address_as_str(m_wallet->testnet(), entry.addr);
- std::unordered_map<std::string,uint64_t>::iterator i = dests.find(address);
+ std::string address, standard_address = get_account_address_as_str(m_wallet->testnet(), entry.addr);
+ if (has_encrypted_payment_id)
+ {
+ address = get_account_integrated_address_as_str(m_wallet->testnet(), entry.addr, payment_id8);
+ address += std::string(" (" + standard_address + " with encrypted payment id " + epee::string_tools::pod_to_hex(payment_id8) + ")");
+ }
+ else
+ address = standard_address;
+ std::unordered_map<std::string,std::pair<std::string,uint64_t>>::iterator i = dests.find(standard_address);
if (i == dests.end())
- dests.insert(std::make_pair(address, entry.amount));
+ dests.insert(std::make_pair(standard_address, std::make_pair(address, entry.amount)));
else
- i->second += entry.amount;
+ i->second.second += entry.amount;
amount_to_dests += entry.amount;
}
if (cd.change_dts.amount > 0)
{
- std::unordered_map<std::string, uint64_t>::iterator it = dests.find(get_account_address_as_str(m_wallet->testnet(), cd.change_dts.addr));
+ std::unordered_map<std::string, std::pair<std::string, uint64_t>>::iterator it = dests.find(get_account_address_as_str(m_wallet->testnet(), cd.change_dts.addr));
if (it == dests.end())
{
fail_msg_writer() << tr("Claimed change does not go to a paid address");
return false;
}
- if (it->second < cd.change_dts.amount)
+ if (it->second.second < cd.change_dts.amount)
{
fail_msg_writer() << tr("Claimed change is larger than payment to the change address");
return false;
@@ -3024,15 +3313,19 @@ bool simple_wallet::accept_loaded_tx(const std::function<size_t()> get_num_txes,
}
}
change += cd.change_dts.amount;
- it->second -= cd.change_dts.amount;
- if (it->second == 0)
+ it->second.second -= cd.change_dts.amount;
+ if (it->second.second == 0)
dests.erase(get_account_address_as_str(m_wallet->testnet(), cd.change_dts.addr));
}
}
+
+ if (payment_id_string.empty())
+ payment_id_string = "no payment ID";
+
std::string dest_string;
- for (std::unordered_map<std::string, uint64_t>::const_iterator i = dests.begin(); i != dests.end(); )
+ for (std::unordered_map<std::string, std::pair<std::string, uint64_t>>::const_iterator i = dests.begin(); i != dests.end(); )
{
- dest_string += (boost::format(tr("sending %s to %s")) % print_money(i->second) % i->first).str();
+ dest_string += (boost::format(tr("sending %s to %s")) % print_money(i->second.second) % i->second.first).str();
++i;
if (i != dests.end())
dest_string += ", ";
@@ -3050,7 +3343,7 @@ bool simple_wallet::accept_loaded_tx(const std::function<size_t()> get_num_txes,
change_string += tr("no change");
uint64_t fee = amount - amount_to_dests;
- std::string prompt_str = (boost::format(tr("Loaded %lu transactions, for %s, fee %s, %s, %s, with min mixin %lu. %sIs this okay? (Y/Yes/N/No): ")) % (unsigned long)get_num_txes() % print_money(amount) % print_money(fee) % dest_string % change_string % (unsigned long)min_mixin % extra_message).str();
+ std::string prompt_str = (boost::format(tr("Loaded %lu transactions, for %s, fee %s, %s, %s, with min ring size %lu, %s. %sIs this okay? (Y/Yes/N/No): ")) % (unsigned long)get_num_txes() % print_money(amount) % print_money(fee) % dest_string % change_string % (unsigned long)min_ring_size % payment_id_string % extra_message).str();
return command_line::is_yes(command_line::input_line(prompt_str));
}
//----------------------------------------------------------------------------------------------------
@@ -3168,10 +3461,10 @@ bool simple_wallet::submit_transfer(const std::vector<std::string> &args_)
catch (const tools::error::not_enough_outs_to_mix& e)
{
auto writer = fail_msg_writer();
- writer << tr("not enough outputs for specified mixin_count") << " = " << e.mixin_count() << ":";
+ writer << tr("not enough outputs for specified ring size") << " = " << (e.mixin_count() + 1) << ":";
for (std::pair<uint64_t, uint64_t> outs_for_amount : e.scanty_outs())
{
- writer << "\n" << tr("output amount") << " = " << print_money(outs_for_amount.first) << ", " << tr("found outputs to mix") << " = " << outs_for_amount.second;
+ writer << "\n" << tr("output amount") << " = " << print_money(outs_for_amount.first) << ", " << tr("found outputs to use") << " = " << outs_for_amount.second;
}
}
catch (const tools::error::tx_not_constructed&)
@@ -3271,7 +3564,7 @@ bool simple_wallet::get_tx_proof(const std::vector<std::string> &args)
cryptonote::account_public_address address;
bool has_payment_id;
crypto::hash8 payment_id;
- if(!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), args[1]))
+ if(!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), args[1], oa_prompter))
{
fail_msg_writer() << tr("failed to parse address");
return true;
@@ -3373,7 +3666,7 @@ bool simple_wallet::check_tx_key(const std::vector<std::string> &args_)
cryptonote::account_public_address address;
bool has_payment_id;
crypto::hash8 payment_id;
- if(!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), local_args[2]))
+ if(!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), local_args[2], oa_prompter))
{
fail_msg_writer() << tr("failed to parse address");
return true;
@@ -3525,7 +3818,7 @@ bool simple_wallet::check_tx_proof(const std::vector<std::string> &args)
cryptonote::account_public_address address;
bool has_payment_id;
crypto::hash8 payment_id;
- if(!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), args[1]))
+ if(!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), args[1], oa_prompter))
{
fail_msg_writer() << tr("failed to parse address");
return true;
@@ -3646,6 +3939,22 @@ static std::string get_human_readable_timestamp(uint64_t ts)
return std::string(buffer);
}
//----------------------------------------------------------------------------------------------------
+static std::string get_human_readable_timespan(std::chrono::seconds seconds)
+{
+ uint64_t ts = seconds.count();
+ if (ts < 60)
+ return std::to_string(ts) + tr(" seconds");
+ if (ts < 3600)
+ return std::to_string((uint64_t)(ts / 60)) + tr(" minutes");
+ if (ts < 3600 * 24)
+ return std::to_string((uint64_t)(ts / 3600)) + tr(" hours");
+ if (ts < 3600 * 24 * 30.5)
+ return std::to_string((uint64_t)(ts / (3600 * 24))) + tr(" days");
+ if (ts < 3600 * 24 * 365.25)
+ return std::to_string((uint64_t)(ts / (3600 * 24 * 365.25))) + tr(" months");
+ return tr("a long time");
+}
+//----------------------------------------------------------------------------------------------------
bool simple_wallet::show_transfers(const std::vector<std::string> &args_)
{
std::vector<std::string> local_args = args_;
@@ -3954,6 +4263,19 @@ void simple_wallet::wallet_idle_thread()
}
}
//----------------------------------------------------------------------------------------------------
+std::string simple_wallet::get_prompt() const
+{
+ std::string addr_start = m_wallet->get_account().get_public_address_str(m_wallet->testnet()).substr(0, 6);
+ std::string prompt = std::string("[") + tr("wallet") + " " + addr_start;
+ uint32_t version;
+ if (!m_wallet->check_connection(&version))
+ prompt += tr(" (no daemon)");
+ else if (!m_wallet->is_synced())
+ prompt += tr(" (out of sync)");
+ prompt += "]: ";
+ return prompt;
+}
+//----------------------------------------------------------------------------------------------------
bool simple_wallet::run()
{
// check and display warning, but go on anyway
@@ -3964,9 +4286,8 @@ bool simple_wallet::run()
m_auto_refresh_enabled = m_wallet->auto_refresh();
m_idle_thread = boost::thread([&]{wallet_idle_thread();});
- std::string addr_start = m_wallet->get_account().get_public_address_str(m_wallet->testnet()).substr(0, 6);
message_writer(console_color_green, false) << "Background refresh thread started";
- return m_cmd_binder.run_handling(std::string("[") + tr("wallet") + " " + addr_start + "]: ", "");
+ return m_cmd_binder.run_handling([this](){return get_prompt();}, "");
}
//----------------------------------------------------------------------------------------------------
void simple_wallet::stop()
@@ -4043,7 +4364,7 @@ bool simple_wallet::address_book(const std::vector<std::string> &args/* = std::v
cryptonote::account_public_address address;
bool has_payment_id;
crypto::hash8 payment_id8;
- if(!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, payment_id8, m_wallet->testnet(), args[1]))
+ if(!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, payment_id8, m_wallet->testnet(), args[1], oa_prompter))
{
fail_msg_writer() << tr("failed to parse address");
return true;
@@ -4234,7 +4555,7 @@ bool simple_wallet::verify(const std::vector<std::string> &args)
cryptonote::account_public_address address;
bool has_payment_id;
crypto::hash8 payment_id;
- if(!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), address_string))
+ if(!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), address_string, oa_prompter))
{
fail_msg_writer() << tr("failed to parse address");
return true;
@@ -4288,6 +4609,12 @@ bool simple_wallet::export_key_images(const std::vector<std::string> &args)
//----------------------------------------------------------------------------------------------------
bool simple_wallet::import_key_images(const std::vector<std::string> &args)
{
+ if (!m_trusted_daemon)
+ {
+ fail_msg_writer() << tr("this command requires a trusted daemon. Enable with --trusted-daemon");
+ return true;
+ }
+
if (args.size() != 1)
{
fail_msg_writer() << tr("usage: import_key_images <filename>");
@@ -4452,6 +4779,8 @@ bool simple_wallet::show_transfer(const std::vector<std::string> &args)
}
crypto::hash txid = *reinterpret_cast<const crypto::hash*>(txid_data.data());
+ const uint64_t last_block_height = m_wallet->get_blockchain_current_height();
+
std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> payments;
m_wallet->get_payments(payments, 0);
for (std::list<std::pair<crypto::hash, tools::wallet2::payment_details>>::const_iterator i = payments.begin(); i != payments.end(); ++i) {
@@ -4466,6 +4795,23 @@ bool simple_wallet::show_transfer(const std::vector<std::string> &args)
success_msg_writer() << "Timestamp: " << get_human_readable_timestamp(pd.m_timestamp);
success_msg_writer() << "Amount: " << print_money(pd.m_amount);
success_msg_writer() << "Payment ID: " << payment_id;
+ if (pd.m_unlock_time < CRYPTONOTE_MAX_BLOCK_NUMBER)
+ {
+ uint64_t bh = std::max(pd.m_unlock_time, pd.m_block_height + CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE);
+ if (bh >= last_block_height)
+ success_msg_writer() << "Locked: " << (bh - last_block_height) << " blocks to unlock";
+ else
+ success_msg_writer() << std::to_string(last_block_height - bh) << " confirmations";
+ }
+ else
+ {
+ uint64_t current_time = static_cast<uint64_t>(time(NULL));
+ uint64_t threshold = current_time + (m_wallet->use_fork_rules(2, 0) ? CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_SECONDS_V2 : CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_SECONDS_V1);
+ if (threshold >= pd.m_unlock_time)
+ success_msg_writer() << "unlocked for " << get_human_readable_timespan(std::chrono::seconds(threshold - pd.m_unlock_time));
+ else
+ success_msg_writer() << "locked for " << get_human_readable_timespan(std::chrono::seconds(pd.m_unlock_time - threshold));
+ }
success_msg_writer() << "Note: " << m_wallet->get_tx_note(txid);
return true;
}
@@ -4583,7 +4929,9 @@ int main(int argc, char* argv[])
command_line::add_arg(desc_params, arg_generate_new_wallet);
command_line::add_arg(desc_params, arg_generate_from_view_key);
command_line::add_arg(desc_params, arg_generate_from_keys);
+ command_line::add_arg(desc_params, arg_generate_from_multisig_keys);
command_line::add_arg(desc_params, arg_generate_from_json);
+ command_line::add_arg(desc_params, arg_mnemonic_language);
command_line::add_arg(desc_params, arg_command);
command_line::add_arg(desc_params, arg_restore_deterministic_wallet );
diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h
index e04352295..079fae9f5 100644
--- a/src/simplewallet/simplewallet.h
+++ b/src/simplewallet/simplewallet.h
@@ -111,7 +111,7 @@ namespace cryptonote
bool set_always_confirm_transfers(const std::vector<std::string> &args = std::vector<std::string>());
bool set_print_ring_members(const std::vector<std::string> &args = std::vector<std::string>());
bool set_store_tx_info(const std::vector<std::string> &args = std::vector<std::string>());
- bool set_default_mixin(const std::vector<std::string> &args = std::vector<std::string>());
+ bool set_default_ring_size(const std::vector<std::string> &args = std::vector<std::string>());
bool set_auto_refresh(const std::vector<std::string> &args = std::vector<std::string>());
bool set_refresh_type(const std::vector<std::string> &args = std::vector<std::string>());
bool set_confirm_missing_payment_id(const std::vector<std::string> &args = std::vector<std::string>());
@@ -120,6 +120,7 @@ namespace cryptonote
bool set_min_output_count(const std::vector<std::string> &args = std::vector<std::string>());
bool set_min_output_value(const std::vector<std::string> &args = std::vector<std::string>());
bool set_merge_destinations(const std::vector<std::string> &args = std::vector<std::string>());
+ bool set_confirm_backlog(const std::vector<std::string> &args = std::vector<std::string>());
bool help(const std::vector<std::string> &args = std::vector<std::string>());
bool start_mining(const std::vector<std::string> &args);
bool stop_mining(const std::vector<std::string> &args);
@@ -174,6 +175,7 @@ namespace cryptonote
bool show_transfer(const std::vector<std::string> &args);
bool change_password(const std::vector<std::string>& args);
bool payment_id(const std::vector<std::string> &args);
+ bool print_fee_info(const std::vector<std::string> &args);
uint64_t get_daemon_blockchain_height(std::string& err);
bool try_connect_to_daemon(bool silent = false, uint32_t* version = nullptr);
@@ -182,6 +184,7 @@ namespace cryptonote
bool accept_loaded_tx(const tools::wallet2::unsigned_tx_set &txs);
bool accept_loaded_tx(const tools::wallet2::signed_tx_set &txs);
bool print_ring_members(const std::vector<tools::wallet2::pending_tx>& ptx_vector, std::ostream& ostr);
+ std::string get_prompt() const;
/*!
* \brief Prints the seed with a nice message
@@ -264,7 +267,9 @@ namespace cryptonote
std::string m_generate_new;
std::string m_generate_from_view_key;
std::string m_generate_from_keys;
+ std::string m_generate_from_multisig_keys;
std::string m_generate_from_json;
+ std::string m_mnemonic_language;
std::string m_import_path;
std::string m_electrum_seed; // electrum-style seed parameter
diff --git a/src/version.h.in b/src/version.h.in
index 852c8023b..281b52db4 100644
--- a/src/version.h.in
+++ b/src/version.h.in
@@ -1,4 +1,4 @@
#define MONERO_VERSION_TAG "@VERSIONTAG@"
-#define MONERO_VERSION "0.10.3.1"
-#define MONERO_RELEASE_NAME "Wolfram Warptangent"
+#define MONERO_VERSION "0.11.0.0"
+#define MONERO_RELEASE_NAME "Helium Hydra"
#define MONERO_VERSION_FULL MONERO_VERSION "-" MONERO_VERSION_TAG
diff --git a/src/wallet/api/pending_transaction.cpp b/src/wallet/api/pending_transaction.cpp
index 9798d66c6..c98a599e7 100644
--- a/src/wallet/api/pending_transaction.cpp
+++ b/src/wallet/api/pending_transaction.cpp
@@ -102,6 +102,7 @@ bool PendingTransactionImpl::commit(const std::string &filename, bool overwrite)
}
// Commit tx
else {
+ m_wallet.pauseRefresh();
while (!m_pending_tx.empty()) {
auto & ptx = m_pending_tx.back();
m_wallet.m_wallet->commit_tx(ptx);
@@ -133,6 +134,7 @@ bool PendingTransactionImpl::commit(const std::string &filename, bool overwrite)
m_status = Status_Error;
}
+ m_wallet.startRefresh();
return m_status == Status_Ok;
}
diff --git a/src/wallet/api/transaction_history.cpp b/src/wallet/api/transaction_history.cpp
index 85f2b05ce..23d3905b2 100644
--- a/src/wallet/api/transaction_history.cpp
+++ b/src/wallet/api/transaction_history.cpp
@@ -132,6 +132,7 @@ void TransactionHistoryImpl::refresh()
ti->m_blockheight = pd.m_block_height;
ti->m_timestamp = pd.m_timestamp;
ti->m_confirmations = wallet_height - pd.m_block_height;
+ ti->m_unlock_time = pd.m_unlock_time;
m_history.push_back(ti);
/* output.insert(std::make_pair(pd.m_block_height, std::make_pair(true, (boost::format("%20.20s %s %s %s")
diff --git a/src/wallet/api/transaction_info.cpp b/src/wallet/api/transaction_info.cpp
index 79a8fe9b5..171272265 100644
--- a/src/wallet/api/transaction_info.cpp
+++ b/src/wallet/api/transaction_info.cpp
@@ -50,6 +50,7 @@ TransactionInfoImpl::TransactionInfoImpl()
, m_blockheight(0)
, m_timestamp(0)
, m_confirmations(0)
+ , m_unlock_time(0)
{
}
@@ -115,6 +116,11 @@ uint64_t TransactionInfoImpl::confirmations() const
return m_confirmations;
}
+uint64_t TransactionInfoImpl::unlockTime() const
+{
+ return m_unlock_time;
+}
+
} // namespace
namespace Bitmonero = Monero;
diff --git a/src/wallet/api/transaction_info.h b/src/wallet/api/transaction_info.h
index 16fa5da7a..ee56b859f 100644
--- a/src/wallet/api/transaction_info.h
+++ b/src/wallet/api/transaction_info.h
@@ -56,6 +56,7 @@ public:
virtual std::string paymentId() const;
virtual const std::vector<Transfer> &transfers() const;
virtual uint64_t confirmations() const;
+ virtual uint64_t unlockTime() const;
private:
int m_direction;
@@ -69,6 +70,7 @@ private:
std::string m_paymentid;
std::vector<Transfer> m_transfers;
uint64_t m_confirmations;
+ uint64_t m_unlock_time;
friend class TransactionHistoryImpl;
diff --git a/src/wallet/api/unsigned_transaction.cpp b/src/wallet/api/unsigned_transaction.cpp
index 1d9ef5d7c..961bd772a 100644
--- a/src/wallet/api/unsigned_transaction.cpp
+++ b/src/wallet/api/unsigned_transaction.cpp
@@ -101,7 +101,7 @@ bool UnsignedTransactionImpl::checkLoadedTx(const std::function<size_t()> get_nu
{
// gather info to ask the user
uint64_t amount = 0, amount_to_dests = 0, change = 0;
- size_t min_mixin = ~0;
+ size_t min_ring_size = ~0;
std::unordered_map<std::string, uint64_t> dests;
const std::string wallet_address = m_wallet.m_wallet->get_account().get_public_address_str(m_wallet.m_wallet->testnet());
int first_known_non_zero_change_index = -1;
@@ -111,9 +111,9 @@ bool UnsignedTransactionImpl::checkLoadedTx(const std::function<size_t()> get_nu
for (size_t s = 0; s < cd.sources.size(); ++s)
{
amount += cd.sources[s].amount;
- size_t mixin = cd.sources[s].outputs.size() - 1;
- if (mixin < min_mixin)
- min_mixin = mixin;
+ size_t ring_size = cd.sources[s].outputs.size();
+ if (ring_size < min_ring_size)
+ min_ring_size = ring_size;
}
for (size_t d = 0; d < cd.splitted_dsts.size(); ++d)
{
@@ -178,7 +178,7 @@ bool UnsignedTransactionImpl::checkLoadedTx(const std::function<size_t()> get_nu
else
change_string += tr("no change");
uint64_t fee = amount - amount_to_dests;
- m_confirmationMessage = (boost::format(tr("Loaded %lu transactions, for %s, fee %s, %s, %s, with min mixin %lu. %s")) % (unsigned long)get_num_txes() % cryptonote::print_money(amount) % cryptonote::print_money(fee) % dest_string % change_string % (unsigned long)min_mixin % extra_message).str();
+ m_confirmationMessage = (boost::format(tr("Loaded %lu transactions, for %s, fee %s, %s, %s, with min ring size %lu. %s")) % (unsigned long)get_num_txes() % cryptonote::print_money(amount) % cryptonote::print_money(fee) % dest_string % change_string % (unsigned long)min_ring_size % extra_message).str();
return true;
}
diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp
index 6a0e1727c..7afc1f449 100644
--- a/src/wallet/api/wallet.cpp
+++ b/src/wallet/api/wallet.cpp
@@ -35,6 +35,7 @@
#include "transaction_history.h"
#include "address_book.h"
#include "common_defines.h"
+#include "common/util.h"
#include "mnemonics/electrum-words.h"
#include <boost/format.hpp>
@@ -99,7 +100,7 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback
}
}
- virtual void on_money_received(uint64_t height, const crypto::hash &txid, uint64_t amount)
+ virtual void on_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount)
{
std::string tx_hash = epee::string_tools::pod_to_hex(txid);
@@ -114,7 +115,7 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback
}
}
- virtual void on_unconfirmed_money_received(uint64_t height, const crypto::hash &txid, uint64_t amount)
+ virtual void on_unconfirmed_money_received(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& tx, uint64_t amount)
{
std::string tx_hash = epee::string_tools::pod_to_hex(txid);
@@ -129,8 +130,8 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback
}
}
- virtual void on_money_spent(uint64_t height, const crypto::hash &txid, uint64_t amount,
- const cryptonote::transaction& spend_tx)
+ virtual void on_money_spent(uint64_t height, const crypto::hash &txid, const cryptonote::transaction& in_tx,
+ uint64_t amount, const cryptonote::transaction& spend_tx)
{
// TODO;
std::string tx_hash = epee::string_tools::pod_to_hex(txid);
@@ -378,7 +379,32 @@ bool WalletImpl::createWatchOnly(const std::string &path, const std::string &pas
const cryptonote::account_public_address address = m_wallet->get_account().get_keys().m_account_address;
try {
+ // Generate view only wallet
view_wallet->generate(path, password, address, viewkey);
+
+ // Export/Import outputs
+ auto outputs = m_wallet->export_outputs();
+ view_wallet->import_outputs(outputs);
+
+ // Copy scanned blockchain
+ auto bc = m_wallet->export_blockchain();
+ view_wallet->import_blockchain(bc);
+
+ // copy payments
+ auto payments = m_wallet->export_payments();
+ view_wallet->import_payments(payments);
+
+ // copy confirmed outgoing payments
+ std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>> out_payments;
+ m_wallet->get_payments_out(out_payments, 0);
+ view_wallet->import_payments_out(out_payments);
+
+ // Export/Import key images
+ // We already know the spent status from the outputs we exported, thus no need to check them again
+ auto key_images = m_wallet->export_key_images();
+ uint64_t spent = 0;
+ uint64_t unspent = 0;
+ view_wallet->import_key_images(key_images,spent,unspent,false);
m_status = Status_Ok;
} catch (const std::exception &e) {
LOG_ERROR("Error creating view only wallet: " << e.what());
@@ -386,6 +412,8 @@ bool WalletImpl::createWatchOnly(const std::string &path, const std::string &pas
m_errorString = e.what();
return false;
}
+ // Store wallet
+ view_wallet->store();
return true;
}
@@ -861,6 +889,11 @@ bool WalletImpl::exportKeyImages(const string &filename)
bool WalletImpl::importKeyImages(const string &filename)
{
+ if (!trustedDaemon()) {
+ m_status = Status_Error;
+ m_errorString = tr("Key images can only be imported with a trusted daemon");
+ return false;
+ }
try
{
uint64_t spent = 0, unspent = 0;
@@ -1009,9 +1042,9 @@ PendingTransaction *WalletImpl::createTransaction(const string &dst_addr, const
} catch (const tools::error::not_enough_outs_to_mix& e) {
std::ostringstream writer;
- writer << tr("not enough outputs for specified mixin_count") << " = " << e.mixin_count() << ":";
+ writer << tr("not enough outputs for specified ring size") << " = " << (e.mixin_count() + 1) << ":";
for (const std::pair<uint64_t, uint64_t> outs_for_amount : e.scanty_outs()) {
- writer << "\n" << tr("output amount") << " = " << print_money(outs_for_amount.first) << ", " << tr("found outputs to mix") << " = " << outs_for_amount.second;
+ writer << "\n" << tr("output amount") << " = " << print_money(outs_for_amount.first) << ", " << tr("found outputs to use") << " = " << outs_for_amount.second;
}
m_errorString = writer.str();
m_status = Status_Error;
@@ -1103,9 +1136,9 @@ PendingTransaction *WalletImpl::createSweepUnmixableTransaction()
} catch (const tools::error::not_enough_outs_to_mix& e) {
std::ostringstream writer;
- writer << tr("not enough outputs for specified mixin_count") << " = " << e.mixin_count() << ":";
+ writer << tr("not enough outputs for specified ring size") << " = " << (e.mixin_count() + 1) << ":";
for (const std::pair<uint64_t, uint64_t> outs_for_amount : e.scanty_outs()) {
- writer << "\n" << tr("output amount") << " = " << print_money(outs_for_amount.first) << ", " << tr("found outputs to mix") << " = " << outs_for_amount.second;
+ writer << "\n" << tr("output amount") << " = " << print_money(outs_for_amount.first) << ", " << tr("found outputs to use") << " = " << outs_for_amount.second;
}
m_errorString = writer.str();
m_status = Status_Error;
@@ -1411,6 +1444,11 @@ bool WalletImpl::parse_uri(const std::string &uri, std::string &address, std::st
return m_wallet->parse_uri(uri, address, payment_id, amount, tx_description, recipient_name, unknown_parameters, error);
}
+std::string WalletImpl::getDefaultDataDir() const
+{
+ return tools::get_default_data_dir();
+}
+
bool WalletImpl::rescanSpent()
{
clearStatus();
diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h
index e9e2cc580..8190c7873 100644
--- a/src/wallet/api/wallet.h
+++ b/src/wallet/api/wallet.h
@@ -98,6 +98,7 @@ public:
void setAutoRefreshInterval(int millis);
int autoRefreshInterval() const;
void setRefreshFromBlockHeight(uint64_t refresh_from_block_height);
+ uint64_t getRefreshFromBlockHeight() const { return m_wallet->get_refresh_from_block_height(); };
void setRecoveringFromSeed(bool recoveringFromSeed);
bool watchOnly() const;
bool rescanSpent();
@@ -128,6 +129,7 @@ public:
virtual void startRefresh();
virtual void pauseRefresh();
virtual bool parse_uri(const std::string &uri, std::string &address, std::string &payment_id, uint64_t &amount, std::string &tx_description, std::string &recipient_name, std::vector<std::string> &unknown_parameters, std::string &error);
+ virtual std::string getDefaultDataDir() const;
private:
void clearStatus() const;
diff --git a/src/wallet/api/wallet_manager.cpp b/src/wallet/api/wallet_manager.cpp
index b2f947972..a23533530 100644
--- a/src/wallet/api/wallet_manager.cpp
+++ b/src/wallet/api/wallet_manager.cpp
@@ -125,6 +125,10 @@ bool WalletManagerImpl::walletExists(const std::string &path)
return false;
}
+bool WalletManagerImpl::verifyWalletPassword(const std::string &keys_file_name, const std::string &password, bool watch_only) const
+{
+ return tools::wallet2::verify_password(keys_file_name, password, watch_only);
+}
std::vector<std::string> WalletManagerImpl::findWallets(const std::string &path)
{
diff --git a/src/wallet/api/wallet_manager.h b/src/wallet/api/wallet_manager.h
index 033e8108f..aa6ea439e 100644
--- a/src/wallet/api/wallet_manager.h
+++ b/src/wallet/api/wallet_manager.h
@@ -50,6 +50,7 @@ public:
const std::string &spendKeyString = "");
virtual bool closeWallet(Wallet *wallet);
bool walletExists(const std::string &path);
+ bool verifyWalletPassword(const std::string &keys_file_name, const std::string &password, bool watch_only) const;
std::vector<std::string> findWallets(const std::string &path);
std::string errorString() const;
void setDaemonAddress(const std::string &address);
diff --git a/src/wallet/node_rpc_proxy.cpp b/src/wallet/node_rpc_proxy.cpp
index 1143a89d7..9f30e4ac5 100644
--- a/src/wallet/node_rpc_proxy.cpp
+++ b/src/wallet/node_rpc_proxy.cpp
@@ -48,6 +48,8 @@ NodeRPCProxy::NodeRPCProxy(epee::net_utils::http::http_simple_client &http_clien
, m_dynamic_per_kb_fee_estimate_cached_height(0)
, m_dynamic_per_kb_fee_estimate_grace_blocks(0)
, m_rpc_version(0)
+ , m_target_height(0)
+ , m_target_height_time(0)
{}
void NodeRPCProxy::invalidate()
@@ -60,9 +62,11 @@ void NodeRPCProxy::invalidate()
m_dynamic_per_kb_fee_estimate_cached_height = 0;
m_dynamic_per_kb_fee_estimate_grace_blocks = 0;
m_rpc_version = 0;
+ m_target_height = 0;
+ m_target_height_time = 0;
}
-boost::optional<std::string> NodeRPCProxy::get_rpc_version(uint32_t &rpc_version)
+boost::optional<std::string> NodeRPCProxy::get_rpc_version(uint32_t &rpc_version) const
{
const time_t now = time(NULL);
if (m_rpc_version == 0)
@@ -84,7 +88,7 @@ boost::optional<std::string> NodeRPCProxy::get_rpc_version(uint32_t &rpc_version
return boost::optional<std::string>();
}
-boost::optional<std::string> NodeRPCProxy::get_height(uint64_t &height)
+boost::optional<std::string> NodeRPCProxy::get_height(uint64_t &height) const
{
const time_t now = time(NULL);
if (m_height == 0 || now >= m_height_time + 30) // re-cache every 30 seconds
@@ -110,7 +114,32 @@ void NodeRPCProxy::set_height(uint64_t h)
m_height = h;
}
-boost::optional<std::string> NodeRPCProxy::get_earliest_height(uint8_t version, uint64_t &earliest_height)
+boost::optional<std::string> NodeRPCProxy::get_target_height(uint64_t &height) const
+{
+ const time_t now = time(NULL);
+ if (m_height == 0 || now >= m_height_time + 30) // re-cache every 30 seconds
+ {
+ epee::json_rpc::request<cryptonote::COMMAND_RPC_GET_INFO::request> req_t = AUTO_VAL_INIT(req_t);
+ epee::json_rpc::response<cryptonote::COMMAND_RPC_GET_INFO::response, std::string> resp_t = AUTO_VAL_INIT(resp_t);
+
+ req_t.jsonrpc = "2.0";
+ req_t.id = epee::serialization::storage_entry(0);
+ req_t.method = "get_info";
+ m_daemon_rpc_mutex.lock();
+ bool r = net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client, rpc_timeout);
+ m_daemon_rpc_mutex.unlock();
+
+ CHECK_AND_ASSERT_MES(r, std::string(), "Failed to connect to daemon");
+ CHECK_AND_ASSERT_MES(resp_t.result.status != CORE_RPC_STATUS_BUSY, resp_t.result.status, "Failed to connect to daemon");
+ CHECK_AND_ASSERT_MES(resp_t.result.status == CORE_RPC_STATUS_OK, resp_t.result.status, "Failed to get target blockchain height");
+ m_target_height = resp_t.result.target_height;
+ m_target_height_time = now;
+ }
+ height = m_target_height;
+ return boost::optional<std::string>();
+}
+
+boost::optional<std::string> NodeRPCProxy::get_earliest_height(uint8_t version, uint64_t &earliest_height) const
{
if (m_earliest_height[version] == 0)
{
@@ -134,7 +163,7 @@ boost::optional<std::string> NodeRPCProxy::get_earliest_height(uint8_t version,
return boost::optional<std::string>();
}
-boost::optional<std::string> NodeRPCProxy::get_dynamic_per_kb_fee_estimate(uint64_t grace_blocks, uint64_t &fee)
+boost::optional<std::string> NodeRPCProxy::get_dynamic_per_kb_fee_estimate(uint64_t grace_blocks, uint64_t &fee) const
{
uint64_t height;
diff --git a/src/wallet/node_rpc_proxy.h b/src/wallet/node_rpc_proxy.h
index 02d1d8d93..fb288189a 100644
--- a/src/wallet/node_rpc_proxy.h
+++ b/src/wallet/node_rpc_proxy.h
@@ -43,23 +43,26 @@ public:
void invalidate();
- boost::optional<std::string> get_rpc_version(uint32_t &version);
- boost::optional<std::string> get_height(uint64_t &height);
+ boost::optional<std::string> get_rpc_version(uint32_t &version) const;
+ boost::optional<std::string> get_height(uint64_t &height) const;
void set_height(uint64_t h);
- boost::optional<std::string> get_earliest_height(uint8_t version, uint64_t &earliest_height);
- boost::optional<std::string> get_dynamic_per_kb_fee_estimate(uint64_t grace_blocks, uint64_t &fee);
+ boost::optional<std::string> get_target_height(uint64_t &height) const;
+ boost::optional<std::string> get_earliest_height(uint8_t version, uint64_t &earliest_height) const;
+ boost::optional<std::string> get_dynamic_per_kb_fee_estimate(uint64_t grace_blocks, uint64_t &fee) const;
private:
epee::net_utils::http::http_simple_client &m_http_client;
boost::mutex &m_daemon_rpc_mutex;
- uint64_t m_height;
- time_t m_height_time;
- uint64_t m_earliest_height[256];
- uint64_t m_dynamic_per_kb_fee_estimate;
- uint64_t m_dynamic_per_kb_fee_estimate_cached_height;
- uint64_t m_dynamic_per_kb_fee_estimate_grace_blocks;
- uint32_t m_rpc_version;
+ mutable uint64_t m_height;
+ mutable time_t m_height_time;
+ mutable uint64_t m_earliest_height[256];
+ mutable uint64_t m_dynamic_per_kb_fee_estimate;
+ mutable uint64_t m_dynamic_per_kb_fee_estimate_cached_height;
+ mutable uint64_t m_dynamic_per_kb_fee_estimate_grace_blocks;
+ mutable uint32_t m_rpc_version;
+ mutable uint64_t m_target_height;
+ mutable time_t m_target_height_time;
};
}
diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp
index 6b1026a55..323a3a7fe 100644
--- a/src/wallet/wallet2.cpp
+++ b/src/wallet/wallet2.cpp
@@ -1097,6 +1097,7 @@ void wallet2::process_outgoing(const crypto::hash &txid, const cryptonote::trans
}
entry.first->second.m_block_height = height;
entry.first->second.m_timestamp = ts;
+ entry.first->second.m_unlock_time = tx.unlock_time;
}
//----------------------------------------------------------------------------------------------------
void wallet2::process_new_blockchain_entry(const cryptonote::block& b, const cryptonote::block_complete_entry& bche, const crypto::hash& bl_id, uint64_t height, const cryptonote::COMMAND_RPC_GET_BLOCKS_FAST::block_output_indices &o_indices)
@@ -1388,7 +1389,7 @@ void wallet2::pull_next_blocks(uint64_t start_height, uint64_t &blocks_start_hei
}
}
//----------------------------------------------------------------------------------------------------
-void wallet2::update_pool_state()
+void wallet2::update_pool_state(bool refreshed)
{
MDEBUG("update_pool_state start");
@@ -1424,13 +1425,14 @@ void wallet2::update_pool_state()
// a tx is removed from the pool due to being found in a new block, but
// just before the block is visible by refresh. So we keep a boolean, so
// that the first time we don't see the tx, we set that boolean, and only
- // delete it the second time it is checked
+ // delete it the second time it is checked (but only when refreshed, so
+ // we're sure we've seen the blockchain state first)
if (pit->second.m_state == wallet2::unconfirmed_transfer_details::pending)
{
LOG_PRINT_L1("Pending txid " << txid << " not in pool, marking as not in pool");
pit->second.m_state = wallet2::unconfirmed_transfer_details::pending_not_in_pool;
}
- else if (pit->second.m_state == wallet2::unconfirmed_transfer_details::pending_not_in_pool)
+ else if (pit->second.m_state == wallet2::unconfirmed_transfer_details::pending_not_in_pool && refreshed)
{
LOG_PRINT_L1("Pending txid " << txid << " not in pool, marking as failed");
pit->second.m_state = wallet2::unconfirmed_transfer_details::failed;
@@ -1459,24 +1461,30 @@ void wallet2::update_pool_state()
MDEBUG("update_pool_state done first loop");
// remove pool txes to us that aren't in the pool anymore
- std::unordered_map<crypto::hash, wallet2::payment_details>::iterator uit = m_unconfirmed_payments.begin();
- while (uit != m_unconfirmed_payments.end())
+ // but only if we just refreshed, so that the tx can go in
+ // the in transfers list instead (or nowhere if it just
+ // disappeared without being mined)
+ if (refreshed)
{
- const crypto::hash &txid = uit->second.m_tx_hash;
- bool found = false;
- for (const auto &it2: res.tx_hashes)
+ std::unordered_map<crypto::hash, wallet2::payment_details>::iterator uit = m_unconfirmed_payments.begin();
+ while (uit != m_unconfirmed_payments.end())
{
- if (it2 == txid)
+ const crypto::hash &txid = uit->second.m_tx_hash;
+ bool found = false;
+ for (const auto &it2: res.tx_hashes)
{
- found = true;
- break;
+ if (it2 == txid)
+ {
+ found = true;
+ break;
+ }
+ }
+ auto pit = uit++;
+ if (!found)
+ {
+ MDEBUG("Removing " << txid << " from unconfirmed payments, not found in pool");
+ m_unconfirmed_payments.erase(pit);
}
- }
- auto pit = uit++;
- if (!found)
- {
- MDEBUG("Removing " << txid << " from unconfirmed payments, not found in pool");
- m_unconfirmed_payments.erase(pit);
}
}
MDEBUG("update_pool_state done second loop");
@@ -1490,7 +1498,16 @@ void wallet2::update_pool_state()
LOG_PRINT_L2("Already seen " << txid << ", skipped");
continue;
}
- if (m_unconfirmed_payments.find(txid) == m_unconfirmed_payments.end())
+ bool txid_found_in_up = false;
+ for (const auto &up: m_unconfirmed_payments)
+ {
+ if (up.second.m_tx_hash == txid)
+ {
+ txid_found_in_up = true;
+ break;
+ }
+ }
+ if (!txid_found_in_up)
{
LOG_PRINT_L1("Found new pool tx: " << txid);
bool found = false;
@@ -1685,6 +1702,7 @@ void wallet2::refresh(uint64_t start_height, uint64_t & blocks_fetched, bool& re
uint64_t blocks_start_height;
std::list<cryptonote::block_complete_entry> blocks;
std::vector<COMMAND_RPC_GET_BLOCKS_FAST::block_output_indices> o_indices;
+ bool refreshed = false;
// pull the first set of blocks
get_short_chain_history(short_chain_history);
@@ -1726,6 +1744,7 @@ void wallet2::refresh(uint64_t start_height, uint64_t & blocks_fetched, bool& re
if(blocks_start_height == next_blocks_start_height)
{
m_node_rpc_proxy.set_height(m_blockchain.size());
+ refreshed = true;
break;
}
@@ -1764,7 +1783,7 @@ void wallet2::refresh(uint64_t start_height, uint64_t & blocks_fetched, bool& re
{
// If stop() is called we don't need to check pending transactions
if(m_run.load(std::memory_order_relaxed))
- update_pool_state();
+ update_pool_state(refreshed);
}
catch (...)
{
@@ -1860,6 +1879,10 @@ bool wallet2::clear()
m_payments.clear();
m_tx_keys.clear();
m_confirmed_txs.clear();
+ m_unconfirmed_payments.clear();
+ m_scanned_pool_txs[0].clear();
+ m_scanned_pool_txs[1].clear();
+ m_address_book.clear();
m_local_bc_height = 1;
return true;
}
@@ -1940,6 +1963,12 @@ bool wallet2::store_keys(const std::string& keys_file_name, const std::string& p
value2.SetInt(m_merge_destinations ? 1 :0);
json.AddMember("merge_destinations", value2, json.GetAllocator());
+ value2.SetInt(m_confirm_backlog ? 1 :0);
+ json.AddMember("confirm_backlog", value2, json.GetAllocator());
+
+ value2.SetInt(m_testnet ? 1 :0);
+ json.AddMember("testnet", value2, json.GetAllocator());
+
// Serialize the JSON object
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
@@ -2011,6 +2040,7 @@ bool wallet2::load_keys(const std::string& keys_file_name, const std::string& pa
m_min_output_count = 0;
m_min_output_value = 0;
m_merge_destinations = false;
+ m_confirm_backlog = true;
}
else
{
@@ -2081,6 +2111,13 @@ bool wallet2::load_keys(const std::string& keys_file_name, const std::string& pa
m_min_output_value = field_min_output_value;
GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, merge_destinations, int, Int, false, false);
m_merge_destinations = field_merge_destinations;
+ GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, confirm_backlog, int, Int, false, true);
+ m_confirm_backlog = field_confirm_backlog;
+ GET_FIELD_FROM_JSON_RETURN_ON_ERROR(json, testnet, int, Int, false, m_testnet);
+ // Wallet is being opened with testnet flag, but is saved as a mainnet wallet
+ THROW_WALLET_EXCEPTION_IF(m_testnet && !field_testnet, error::wallet_internal_error, "Mainnet wallet can not be opened as testnet wallet");
+ // Wallet is being opened without testnet flag but is saved as a testnet wallet.
+ THROW_WALLET_EXCEPTION_IF(!m_testnet && field_testnet, error::wallet_internal_error, "Testnet wallet can not be opened as mainnet wallet");
}
const cryptonote::account_keys& keys = m_account.get_keys();
@@ -2095,6 +2132,7 @@ bool wallet2::load_keys(const std::string& keys_file_name, const std::string& pa
/*!
* \brief verify password for default wallet keys file.
* \param password Password to verify
+ * \return true if password is correct
*
* for verification only
* should not mutate state, unlike load_keys()
@@ -2103,7 +2141,23 @@ bool wallet2::load_keys(const std::string& keys_file_name, const std::string& pa
*/
bool wallet2::verify_password(const std::string& password) const
{
- const std::string keys_file_name = m_keys_file;
+ return verify_password(m_keys_file, password, m_watch_only);
+}
+
+/*!
+ * \brief verify password for specified wallet keys file.
+ * \param keys_file_name Keys file to verify password for
+ * \param password Password to verify
+ * \param watch_only If set = only verify view keys, otherwise also spend keys
+ * \return true if password is correct
+ *
+ * for verification only
+ * should not mutate state, unlike load_keys()
+ * can be used prior to rewriting wallet keys file, to ensure user has entered the correct password
+ *
+ */
+bool wallet2::verify_password(const std::string& keys_file_name, const std::string& password, bool watch_only)
+{
wallet2::keys_file_data keys_file_data;
std::string buf;
bool r = epee::file_io_utils::load_file_to_string(keys_file_name, buf);
@@ -2136,7 +2190,7 @@ bool wallet2::verify_password(const std::string& password) const
const cryptonote::account_keys& keys = account_data_check.get_keys();
r = r && verify_keys(keys.m_view_secret_key, keys.m_account_address.m_view_public_key);
- if(!m_watch_only)
+ if(!watch_only)
r = r && verify_keys(keys.m_spend_secret_key, keys.m_account_address.m_spend_public_key);
return r;
}
@@ -2174,9 +2228,26 @@ crypto::secret_key wallet2::generate(const std::string& wallet_, const std::stri
// try asking the daemon first
if(m_refresh_from_block_height == 0 && !recover){
std::string err;
- uint64_t height = get_daemon_blockchain_height(err);
- if (err.empty())
- m_refresh_from_block_height = height - blocks_per_month;
+ uint64_t height = 0;
+
+ // we get the max of approximated height and known height
+ // approximated height is the least of daemon target height
+ // (the max of what the other daemons are claiming is their
+ // height) and the theoretical height based on the local
+ // clock. This will be wrong only if both the local clock
+ // is bad *and* a peer daemon claims a highest height than
+ // the real chain.
+ // known height is the height the local daemon is currently
+ // synced to, it will be lower than the real chain height if
+ // the daemon is currently syncing.
+ height = get_approximate_blockchain_height();
+ uint64_t target_height = get_daemon_blockchain_target_height(err);
+ if (err.empty() && target_height < height)
+ height = target_height;
+ uint64_t local_height = get_daemon_blockchain_height(err);
+ if (err.empty() && local_height > height)
+ height = local_height;
+ m_refresh_from_block_height = height >= blocks_per_month ? height - blocks_per_month : 0;
}
if(m_refresh_from_block_height == 0 && !recover){
@@ -3252,7 +3323,7 @@ bool wallet2::sign_tx(unsigned_tx_set &exported_txs, const std::string &signed_f
for (size_t n = 0; n < exported_txs.txes.size(); ++n)
{
const tools::wallet2::tx_construction_data &sd = exported_txs.txes[n];
- LOG_PRINT_L1(" " << (n+1) << ": " << sd.sources.size() << " inputs, mixin " << (sd.sources[0].outputs.size()-1));
+ LOG_PRINT_L1(" " << (n+1) << ": " << sd.sources.size() << " inputs, ring size " << sd.sources[0].outputs.size());
signed_txes.ptx.push_back(pending_tx());
tools::wallet2::pending_tx &ptx = signed_txes.ptx.back();
crypto::secret_key tx_key;
@@ -3386,12 +3457,15 @@ bool wallet2::load_tx(const std::string &signed_filename, std::vector<tools::wal
return true;
}
//----------------------------------------------------------------------------------------------------
-uint64_t wallet2::get_fee_multiplier(uint32_t priority, int fee_algorithm) const
+uint64_t wallet2::get_fee_multiplier(uint32_t priority, int fee_algorithm)
{
static const uint64_t old_multipliers[3] = {1, 2, 3};
static const uint64_t new_multipliers[3] = {1, 20, 166};
static const uint64_t newer_multipliers[4] = {1, 4, 20, 166};
+ if (fee_algorithm == -1)
+ fee_algorithm = get_fee_algorithm();
+
// 0 -> default (here, x1 till fee algorithm 2, x4 from it)
if (priority == 0)
priority = m_default_priority;
@@ -4119,7 +4193,7 @@ static size_t estimate_rct_tx_size(int n_inputs, int mixin, int n_outputs)
// txnFee
size += 4;
- LOG_PRINT_L2("estimated rct tx size for " << n_inputs << " at mixin " << mixin << " and " << n_outputs << ": " << size << " (" << ((32 * n_inputs/*+1*/) + 2 * 32 * (mixin+1) * n_inputs + 32 * n_outputs) << " saved)");
+ LOG_PRINT_L2("estimated rct tx size for " << n_inputs << " with ring size " << (mixin+1) << " and " << n_outputs << ": " << size << " (" << ((32 * n_inputs/*+1*/) + 2 * 32 * (mixin+1) * n_inputs + 32 * n_outputs) << " saved)");
return size;
}
@@ -4359,7 +4433,7 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp
// try to pick outputs not from the same block. We will get two outputs, one for
// the destination, and one for change.
LOG_PRINT_L2("checking preferred");
- std::vector<size_t> prefered_inputs;
+ std::vector<size_t> preferred_inputs;
uint64_t rct_outs_needed = 2 * (fake_outs_count + 1);
rct_outs_needed += 100; // some fudge factor since we don't know how many are locked
if (use_rct && get_num_rct_outputs() >= rct_outs_needed)
@@ -4367,12 +4441,12 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp
// this is used to build a tx that's 1 or 2 inputs, and 2 outputs, which
// will get us a known fee.
uint64_t estimated_fee = calculate_fee(fee_per_kb, estimate_rct_tx_size(2, fake_outs_count + 1, 2), fee_multiplier);
- prefered_inputs = pick_preferred_rct_inputs(needed_money + estimated_fee);
- if (!prefered_inputs.empty())
+ preferred_inputs = pick_preferred_rct_inputs(needed_money + estimated_fee);
+ if (!preferred_inputs.empty())
{
string s;
- for (auto i: prefered_inputs) s += boost::lexical_cast<std::string>(i) + "(" + print_money(m_transfers[i].amount()) + ") ";
- LOG_PRINT_L1("Found prefered rct inputs for rct tx: " << s);
+ for (auto i: preferred_inputs) s += boost::lexical_cast<std::string>(i) + "(" + print_money(m_transfers[i].amount()) + ") ";
+ LOG_PRINT_L1("Found preferred rct inputs for rct tx: " << s);
}
}
LOG_PRINT_L2("done checking preferred");
@@ -4427,8 +4501,8 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp
}
pop_if_present(unused_transfers_indices, idx);
pop_if_present(unused_dust_indices, idx);
- } else if (!prefered_inputs.empty()) {
- idx = pop_back(prefered_inputs);
+ } else if (!preferred_inputs.empty()) {
+ idx = pop_back(preferred_inputs);
pop_if_present(unused_transfers_indices, idx);
pop_if_present(unused_dust_indices, idx);
} else
@@ -4521,7 +4595,7 @@ std::vector<wallet2::pending_tx> wallet2::create_transactions_2(std::vector<cryp
{
uint64_t new_paid_amount = i->amount /*+ test_ptx.fee*/ - needed_fee;
LOG_PRINT_L2("Adjusting amount paid to " << get_account_address_as_str(m_testnet, i->addr) << " from " <<
- print_money(i->amount) << " to " << print_money(new_paid_amount) << " to accomodate " <<
+ print_money(i->amount) << " to " << print_money(new_paid_amount) << " to accommodate " <<
print_money(needed_fee) << " fee");
dsts[0].amount += i->amount - new_paid_amount;
i->amount = new_paid_amount;
@@ -5027,6 +5101,9 @@ uint64_t wallet2::get_approximate_blockchain_height() const
const int seconds_per_block = DIFFICULTY_TARGET_V2;
// Calculated blockchain height
uint64_t approx_blockchain_height = fork_block + (time(NULL) - fork_time)/seconds_per_block;
+ // testnet got some huge rollbacks, so the estimation is way off
+ if (m_testnet && approx_blockchain_height > 105000)
+ approx_blockchain_height -= 105000;
LOG_PRINT_L2("Calculated blockchain height: " << approx_blockchain_height);
return approx_blockchain_height;
}
@@ -5259,7 +5336,7 @@ uint64_t wallet2::import_key_images(const std::string &filename, uint64_t &spent
}
//----------------------------------------------------------------------------------------------------
-uint64_t wallet2::import_key_images(const std::vector<std::pair<crypto::key_image, crypto::signature>> &signed_key_images, uint64_t &spent, uint64_t &unspent)
+uint64_t wallet2::import_key_images(const std::vector<std::pair<crypto::key_image, crypto::signature>> &signed_key_images, uint64_t &spent, uint64_t &unspent, bool check_spent)
{
COMMAND_RPC_IS_KEY_IMAGE_SPENT::request req = AUTO_VAL_INIT(req);
COMMAND_RPC_IS_KEY_IMAGE_SPENT::response daemon_resp = AUTO_VAL_INIT(daemon_resp);
@@ -5308,34 +5385,88 @@ uint64_t wallet2::import_key_images(const std::vector<std::pair<crypto::key_imag
m_transfers[n].m_key_image_known = true;
}
- m_daemon_rpc_mutex.lock();
- bool r = epee::net_utils::invoke_http_json("/is_key_image_spent", req, daemon_resp, m_http_client, rpc_timeout);
- m_daemon_rpc_mutex.unlock();
- THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "is_key_image_spent");
- THROW_WALLET_EXCEPTION_IF(daemon_resp.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "is_key_image_spent");
- THROW_WALLET_EXCEPTION_IF(daemon_resp.status != CORE_RPC_STATUS_OK, error::is_key_image_spent_error, daemon_resp.status);
- 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 = " +
- std::to_string(daemon_resp.spent_status.size()) + ", expected " + std::to_string(signed_key_images.size()));
-
+ if(check_spent)
+ {
+ m_daemon_rpc_mutex.lock();
+ bool r = epee::net_utils::invoke_http_json("/is_key_image_spent", req, daemon_resp, m_http_client, rpc_timeout);
+ m_daemon_rpc_mutex.unlock();
+ THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "is_key_image_spent");
+ THROW_WALLET_EXCEPTION_IF(daemon_resp.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "is_key_image_spent");
+ THROW_WALLET_EXCEPTION_IF(daemon_resp.status != CORE_RPC_STATUS_OK, error::is_key_image_spent_error, daemon_resp.status);
+ 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 = " +
+ std::to_string(daemon_resp.spent_status.size()) + ", expected " + std::to_string(signed_key_images.size()));
+ for (size_t n = 0; n < daemon_resp.spent_status.size(); ++n)
+ {
+ transfer_details &td = m_transfers[n];
+ td.m_spent = daemon_resp.spent_status[n] != COMMAND_RPC_IS_KEY_IMAGE_SPENT::UNSPENT;
+ }
+ }
spent = 0;
unspent = 0;
- for (size_t n = 0; n < daemon_resp.spent_status.size(); ++n)
+ for(size_t i = 0; i < m_transfers.size(); ++i)
{
- transfer_details &td = m_transfers[n];
+ transfer_details &td = m_transfers[i];
uint64_t amount = td.amount();
- td.m_spent = daemon_resp.spent_status[n] != COMMAND_RPC_IS_KEY_IMAGE_SPENT::UNSPENT;
if (td.m_spent)
spent += amount;
else
unspent += amount;
- LOG_PRINT_L2("Transfer " << n << ": " << print_money(amount) << " (" << td.m_global_output_index << "): "
- << (td.m_spent ? "spent" : "unspent") << " (key image " << req.key_images[n] << ")");
+ LOG_PRINT_L2("Transfer " << i << ": " << print_money(amount) << " (" << td.m_global_output_index << "): "
+ << (td.m_spent ? "spent" : "unspent") << " (key image " << req.key_images[i] << ")");
}
- LOG_PRINT_L1("Total: " << print_money(spent) << " spent, " << print_money(unspent) << " unspent");
-
+ MDEBUG("Total: " << print_money(spent) << " spent, " << print_money(unspent) << " unspent");
return m_transfers[signed_key_images.size() - 1].m_block_height;
}
+wallet2::payment_container wallet2::export_payments() const
+{
+ payment_container payments;
+ for (auto const &p : m_payments)
+ {
+ payments.emplace(p);
+ }
+ return payments;
+}
+void wallet2::import_payments(const payment_container &payments)
+{
+ m_payments.clear();
+ for (auto const &p : payments)
+ {
+ m_payments.emplace(p);
+ }
+}
+void wallet2::import_payments_out(const std::list<std::pair<crypto::hash,wallet2::confirmed_transfer_details>> &confirmed_payments)
+{
+ m_confirmed_txs.clear();
+ for (auto const &p : confirmed_payments)
+ {
+ m_confirmed_txs.emplace(p);
+ }
+}
+
+std::vector<crypto::hash> wallet2::export_blockchain() const
+{
+ std::vector<crypto::hash> bc;
+ for (auto const &b : m_blockchain)
+ {
+ bc.push_back(b);
+ }
+ return bc;
+}
+
+void wallet2::import_blockchain(const std::vector<crypto::hash> &bc)
+{
+ m_blockchain.clear();
+ for (auto const &b : bc)
+ {
+ m_blockchain.push_back(b);
+ }
+ cryptonote::block genesis;
+ generate_genesis(genesis);
+ crypto::hash genesis_hash = get_block_hash(genesis);
+ check_genesis(genesis_hash);
+ m_local_bc_height = m_blockchain.size();
+}
//----------------------------------------------------------------------------------------------------
std::vector<tools::wallet2::transfer_details> wallet2::export_outputs() const
{
@@ -5667,6 +5798,79 @@ uint64_t wallet2::get_blockchain_height_by_date(uint16_t year, uint8_t month, ui
}
}
//----------------------------------------------------------------------------------------------------
+bool wallet2::is_synced() const
+{
+ uint64_t height;
+ boost::optional<std::string> result = m_node_rpc_proxy.get_target_height(height);
+ if (result && *result != CORE_RPC_STATUS_OK)
+ return false;
+ return get_blockchain_current_height() >= height;
+}
+//----------------------------------------------------------------------------------------------------
+std::vector<std::pair<uint64_t, uint64_t>> wallet2::estimate_backlog(uint64_t min_blob_size, uint64_t max_blob_size, const std::vector<uint64_t> &fees)
+{
+ THROW_WALLET_EXCEPTION_IF(min_blob_size == 0, error::wallet_internal_error, "Invalid 0 fee");
+ THROW_WALLET_EXCEPTION_IF(max_blob_size == 0, error::wallet_internal_error, "Invalid 0 fee");
+ for (uint64_t fee: fees)
+ {
+ THROW_WALLET_EXCEPTION_IF(fee == 0, error::wallet_internal_error, "Invalid 0 fee");
+ }
+
+ // get txpool backlog
+ epee::json_rpc::request<cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG::request> req = AUTO_VAL_INIT(req);
+ epee::json_rpc::response<cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG::response, std::string> res = AUTO_VAL_INIT(res);
+ m_daemon_rpc_mutex.lock();
+ req.jsonrpc = "2.0";
+ req.id = epee::serialization::storage_entry(0);
+ req.method = "get_txpool_backlog";
+ bool r = net_utils::invoke_http_json("/json_rpc", req, res, m_http_client, rpc_timeout);
+ m_daemon_rpc_mutex.unlock();
+ THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "Failed to connect to daemon");
+ THROW_WALLET_EXCEPTION_IF(res.result.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "get_txpool_backlog");
+ THROW_WALLET_EXCEPTION_IF(res.result.status != CORE_RPC_STATUS_OK, error::get_tx_pool_error);
+
+ epee::json_rpc::request<cryptonote::COMMAND_RPC_GET_INFO::request> req_t = AUTO_VAL_INIT(req_t);
+ epee::json_rpc::response<cryptonote::COMMAND_RPC_GET_INFO::response, std::string> resp_t = AUTO_VAL_INIT(resp_t);
+ m_daemon_rpc_mutex.lock();
+ req_t.jsonrpc = "2.0";
+ req_t.id = epee::serialization::storage_entry(0);
+ req_t.method = "get_info";
+ r = net_utils::invoke_http_json("/json_rpc", req_t, resp_t, m_http_client);
+ m_daemon_rpc_mutex.unlock();
+ THROW_WALLET_EXCEPTION_IF(!r, error::no_connection_to_daemon, "get_info");
+ THROW_WALLET_EXCEPTION_IF(resp_t.result.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "get_info");
+ THROW_WALLET_EXCEPTION_IF(resp_t.result.status != CORE_RPC_STATUS_OK, error::get_tx_pool_error);
+ uint64_t full_reward_zone = resp_t.result.block_size_limit / 2;
+
+ std::vector<std::pair<uint64_t, uint64_t>> blocks;
+ for (uint64_t fee: fees)
+ {
+ double our_fee_byte_min = fee / (double)min_blob_size, our_fee_byte_max = fee / (double)max_blob_size;
+ uint64_t priority_size_min = 0, priority_size_max = 0;
+ for (const auto &i: res.result.backlog)
+ {
+ if (i.blob_size == 0)
+ {
+ MWARNING("Got 0 sized blob from txpool, ignored");
+ continue;
+ }
+ double this_fee_byte = i.fee / (double)i.blob_size;
+ if (this_fee_byte >= our_fee_byte_min)
+ priority_size_min += i.blob_size;
+ if (this_fee_byte >= our_fee_byte_max)
+ priority_size_max += i.blob_size;
+ }
+
+ uint64_t nblocks_min = (priority_size_min + full_reward_zone - 1) / full_reward_zone;
+ uint64_t nblocks_max = (priority_size_max + full_reward_zone - 1) / full_reward_zone;
+ MDEBUG("estimate_backlog: priority_size " << priority_size_min << " - " << priority_size_max << " for " << fee
+ << " (" << our_fee_byte_min << " - " << our_fee_byte_max << " piconero byte fee), "
+ << nblocks_min << " - " << nblocks_max << " blocks at block size " << full_reward_zone);
+ blocks.push_back(std::make_pair(nblocks_min, nblocks_max));
+ }
+ return blocks;
+}
+//----------------------------------------------------------------------------------------------------
void wallet2::generate_genesis(cryptonote::block& b) {
if (m_testnet)
{
diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h
index e7692badb..adf03abcc 100644
--- a/src/wallet/wallet2.h
+++ b/src/wallet/wallet2.h
@@ -105,7 +105,7 @@ namespace tools
};
private:
- wallet2(const wallet2&) : m_run(true), m_callback(0), m_testnet(false), m_always_confirm_transfers(true), m_print_ring_members(false), m_store_tx_info(true), m_default_mixin(0), m_default_priority(0), m_refresh_type(RefreshOptimizeCoinbase), m_auto_refresh(true), m_refresh_from_block_height(0), m_confirm_missing_payment_id(true), m_ask_password(true), m_min_output_count(0), m_min_output_value(0), m_merge_destinations(false), m_is_initialized(false),m_node_rpc_proxy(m_http_client, m_daemon_rpc_mutex) {}
+ wallet2(const wallet2&) : m_run(true), m_callback(0), m_testnet(false), m_always_confirm_transfers(true), m_print_ring_members(false), m_store_tx_info(true), m_default_mixin(0), m_default_priority(0), m_refresh_type(RefreshOptimizeCoinbase), m_auto_refresh(true), m_refresh_from_block_height(0), m_confirm_missing_payment_id(true), m_ask_password(true), m_min_output_count(0), m_min_output_value(0), m_merge_destinations(false), m_confirm_backlog(true), m_is_initialized(false),m_node_rpc_proxy(m_http_client, m_daemon_rpc_mutex) {}
public:
static const char* tr(const char* str);
@@ -129,7 +129,9 @@ namespace tools
//! Just parses variables.
static std::unique_ptr<wallet2> make_dummy(const boost::program_options::variables_map& vm);
- wallet2(bool testnet = false, bool restricted = false) : m_run(true), m_callback(0), m_testnet(testnet), m_always_confirm_transfers(true), m_print_ring_members(false), m_store_tx_info(true), m_default_mixin(0), m_default_priority(0), m_refresh_type(RefreshOptimizeCoinbase), m_auto_refresh(true), m_refresh_from_block_height(0), m_confirm_missing_payment_id(true), m_ask_password(true), m_min_output_count(0), m_min_output_value(0), m_merge_destinations(false), m_is_initialized(false), m_restricted(restricted), is_old_file_format(false), m_node_rpc_proxy(m_http_client, m_daemon_rpc_mutex) {}
+ static bool verify_password(const std::string& keys_file_name, const std::string& password, bool watch_only);
+
+ wallet2(bool testnet = false, bool restricted = false) : m_run(true), m_callback(0), m_testnet(testnet), m_always_confirm_transfers(true), m_print_ring_members(false), m_store_tx_info(true), m_default_mixin(0), m_default_priority(0), m_refresh_type(RefreshOptimizeCoinbase), m_auto_refresh(true), m_refresh_from_block_height(0), m_confirm_missing_payment_id(true), m_ask_password(true), m_min_output_count(0), m_min_output_value(0), m_merge_destinations(false), m_confirm_backlog(true), m_is_initialized(false), m_restricted(restricted), is_old_file_format(false), m_node_rpc_proxy(m_http_client, m_daemon_rpc_mutex) {}
struct transfer_details
{
@@ -199,10 +201,11 @@ namespace tools
std::vector<cryptonote::tx_destination_entry> m_dests;
crypto::hash m_payment_id;
uint64_t m_timestamp;
+ uint64_t m_unlock_time;
- confirmed_transfer_details(): m_amount_in(0), m_amount_out(0), m_change((uint64_t)-1), m_block_height(0), m_payment_id(cryptonote::null_hash) {}
+ confirmed_transfer_details(): m_amount_in(0), m_amount_out(0), m_change((uint64_t)-1), m_block_height(0), m_payment_id(cryptonote::null_hash), m_timestamp(0), m_unlock_time(0) {}
confirmed_transfer_details(const unconfirmed_transfer_details &utd, uint64_t height):
- m_amount_in(utd.m_amount_in), m_amount_out(utd.m_amount_out), m_change(utd.m_change), m_block_height(height), m_dests(utd.m_dests), m_payment_id(utd.m_payment_id), m_timestamp(utd.m_timestamp) {}
+ m_amount_in(utd.m_amount_in), m_amount_out(utd.m_amount_out), m_change(utd.m_change), m_block_height(height), m_dests(utd.m_dests), m_payment_id(utd.m_payment_id), m_timestamp(utd.m_timestamp), m_unlock_time(utd.m_tx.unlock_time) {}
};
struct tx_construction_data
@@ -536,6 +539,8 @@ namespace tools
uint64_t get_min_output_value() const { return m_min_output_value; }
void merge_destinations(bool merge) { m_merge_destinations = merge; }
bool merge_destinations() const { return m_merge_destinations; }
+ bool confirm_backlog() const { return m_confirm_backlog; }
+ void confirm_backlog(bool always) { m_confirm_backlog = always; }
bool get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key) const;
@@ -577,15 +582,20 @@ namespace tools
std::string sign(const std::string &data) const;
bool verify(const std::string &data, const cryptonote::account_public_address &address, const std::string &signature) const;
+ // Import/Export wallet data
std::vector<tools::wallet2::transfer_details> export_outputs() const;
size_t import_outputs(const std::vector<tools::wallet2::transfer_details> &outputs);
-
+ payment_container export_payments() const;
+ void import_payments(const payment_container &payments);
+ void import_payments_out(const std::list<std::pair<crypto::hash,wallet2::confirmed_transfer_details>> &confirmed_payments);
+ std::vector<crypto::hash> export_blockchain() const;
+ void import_blockchain(const std::vector<crypto::hash> &bc);
bool export_key_images(const std::string filename);
std::vector<std::pair<crypto::key_image, crypto::signature>> export_key_images() const;
- uint64_t import_key_images(const std::vector<std::pair<crypto::key_image, crypto::signature>> &signed_key_images, uint64_t &spent, uint64_t &unspent);
+ uint64_t import_key_images(const std::vector<std::pair<crypto::key_image, crypto::signature>> &signed_key_images, uint64_t &spent, uint64_t &unspent, bool check_spent = true);
uint64_t import_key_images(const std::string &filename, uint64_t &spent, uint64_t &unspent);
- void update_pool_state();
+ void update_pool_state(bool refreshed = false);
std::string encrypt(const std::string &plaintext, const crypto::secret_key &skey, bool authenticated = true) const;
std::string encrypt_with_view_secret_key(const std::string &plaintext, bool authenticated = true) const;
@@ -597,6 +607,13 @@ namespace tools
uint64_t get_blockchain_height_by_date(uint16_t year, uint8_t month, uint8_t day); // 1<=month<=12, 1<=day<=31
+ bool is_synced() const;
+
+ std::vector<std::pair<uint64_t, uint64_t>> estimate_backlog(uint64_t min_blob_size, uint64_t max_blob_size, const std::vector<uint64_t> &fees);
+
+ uint64_t get_fee_multiplier(uint32_t priority, int fee_algorithm = -1);
+ uint64_t get_per_kb_fee();
+
private:
/*!
* \brief Stores wallet information to wallet file.
@@ -637,9 +654,7 @@ namespace tools
void parse_block_round(const cryptonote::blobdata &blob, cryptonote::block &bl, crypto::hash &bl_id, bool &error) const;
uint64_t get_upper_transaction_size_limit();
std::vector<uint64_t> get_unspent_amounts_vector();
- uint64_t get_fee_multiplier(uint32_t priority, int fee_algorithm) const;
uint64_t get_dynamic_per_kb_fee_estimate();
- uint64_t get_per_kb_fee();
float get_output_relatedness(const transfer_details &td0, const transfer_details &td1) const;
std::vector<size_t> pick_preferred_rct_inputs(uint64_t needed_money) const;
void set_spent(size_t idx, uint64_t height);
@@ -695,6 +710,7 @@ namespace tools
uint32_t m_min_output_count;
uint64_t m_min_output_value;
bool m_merge_destinations;
+ bool m_confirm_backlog;
bool m_is_initialized;
NodeRPCProxy m_node_rpc_proxy;
std::unordered_set<crypto::hash> m_scanned_pool_txs[2];
@@ -704,7 +720,7 @@ BOOST_CLASS_VERSION(tools::wallet2, 18)
BOOST_CLASS_VERSION(tools::wallet2::transfer_details, 7)
BOOST_CLASS_VERSION(tools::wallet2::payment_details, 1)
BOOST_CLASS_VERSION(tools::wallet2::unconfirmed_transfer_details, 6)
-BOOST_CLASS_VERSION(tools::wallet2::confirmed_transfer_details, 3)
+BOOST_CLASS_VERSION(tools::wallet2::confirmed_transfer_details, 4)
BOOST_CLASS_VERSION(tools::wallet2::address_book_row, 16)
BOOST_CLASS_VERSION(tools::wallet2::unsigned_tx_set, 0)
BOOST_CLASS_VERSION(tools::wallet2::signed_tx_set, 0)
@@ -879,6 +895,13 @@ namespace boost
x.m_amount_out += x.m_change;
}
}
+ if (ver < 4)
+ {
+ if (!typename Archive::is_saving())
+ x.m_unlock_time = 0;
+ return;
+ }
+ a & x.m_unlock_time;
}
template <class Archive>
diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h
index a136954f0..8da8c62eb 100644
--- a/src/wallet/wallet2_api.h
+++ b/src/wallet/wallet2_api.h
@@ -156,6 +156,7 @@ struct TransactionInfo
virtual uint64_t fee() const = 0;
virtual uint64_t blockHeight() const = 0;
virtual uint64_t confirmations() const = 0;
+ virtual uint64_t unlockTime() const = 0;
//! transaction_id
virtual std::string hash() const = 0;
virtual std::time_t timestamp() const = 0;
@@ -380,6 +381,12 @@ struct Wallet
virtual void setRefreshFromBlockHeight(uint64_t refresh_from_block_height) = 0;
/*!
+ * \brief getRestoreHeight - get wallet creation height
+ *
+ */
+ virtual uint64_t getRefreshFromBlockHeight() const = 0;
+
+ /*!
* \brief setRecoveringFromSeed - set state recover form seed
*
* \param recoveringFromSeed - true/false
@@ -589,6 +596,9 @@ struct Wallet
virtual bool verifySignedMessage(const std::string &message, const std::string &addres, const std::string &signature) const = 0;
virtual bool parse_uri(const std::string &uri, std::string &address, std::string &payment_id, uint64_t &amount, std::string &tx_description, std::string &recipient_name, std::vector<std::string> &unknown_parameters, std::string &error) = 0;
+
+ virtual std::string getDefaultDataDir() const = 0;
+
/*
* \brief rescanSpent - Rescan spent outputs - Can only be used with trusted daemon
* \return true on success
@@ -662,11 +672,20 @@ struct WalletManager
/*!
* @brief TODO: delme walletExists - check if the given filename is the wallet
* @param path - filename
- * @return
+ * @return - true if wallet exists
*/
virtual bool walletExists(const std::string &path) = 0;
/*!
+ * @brief verifyWalletPassword - check if the given filename is the wallet
+ * @param keys_file_name - location of keys file
+ * @param password - password to verify
+ * @param watch_only - verify only view keys?
+ * @return - true if password is correct
+ */
+ virtual bool verifyWalletPassword(const std::string &keys_file_name, const std::string &password, bool watch_only) const = 0;
+
+ /*!
* \brief findWallets - searches for the wallet files by given path name recursively
* \param path - starting point to search
* \return - list of strings with found wallets (absolute paths);
diff --git a/src/wallet/wallet_args.cpp b/src/wallet/wallet_args.cpp
index 1508f3791..34c5a2a5d 100644
--- a/src/wallet/wallet_args.cpp
+++ b/src/wallet/wallet_args.cpp
@@ -149,8 +149,10 @@ namespace wallet_args
if (command_line::get_arg(vm, command_line::arg_help))
{
- tools::msg_writer() << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")";
- tools::msg_writer() << wallet_args::tr("Usage:") << ' ' << usage;
+ tools::msg_writer() << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")" << ENDL;
+ tools::msg_writer() << wallet_args::tr("This is the command line monero wallet. It needs to connect to a monero\n"
+ "daemon to work correctly.") << ENDL;
+ tools::msg_writer() << wallet_args::tr("Usage:") << ENDL << " " << usage;
tools::msg_writer() << desc_all;
return boost::none;
}
diff --git a/src/wallet/wallet_errors.h b/src/wallet/wallet_errors.h
index 16807e045..2e5acc8cb 100644
--- a/src/wallet/wallet_errors.h
+++ b/src/wallet/wallet_errors.h
@@ -417,7 +417,7 @@ namespace tools
typedef std::unordered_map<uint64_t, uint64_t> scanty_outs_t;
explicit not_enough_outs_to_mix(std::string&& loc, const scanty_outs_t& scanty_outs, size_t mixin_count)
- : transfer_error(std::move(loc), "not enough outputs to mix")
+ : transfer_error(std::move(loc), "not enough outputs to use")
, m_scanty_outs(scanty_outs)
, m_mixin_count(mixin_count)
{
@@ -429,7 +429,7 @@ namespace tools
std::string to_string() const
{
std::ostringstream ss;
- ss << transfer_error::to_string() << ", mixin_count = " << m_mixin_count << ", scanty_outs:";
+ ss << transfer_error::to_string() << ", ring size = " << (m_mixin_count + 1) << ", scanty_outs:";
for (const auto& out: m_scanty_outs)
{
ss << '\n' << cryptonote::print_money(out.first) << " - " << out.second;
diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp
index e7b9b5a71..773d12775 100644
--- a/src/wallet/wallet_rpc_server.cpp
+++ b/src/wallet/wallet_rpc_server.cpp
@@ -227,6 +227,19 @@ namespace tools
return false;
}
//------------------------------------------------------------------------------------------------------------------------------
+ uint64_t wallet_rpc_server::adjust_mixin(uint64_t mixin)
+ {
+ if (mixin < 4 && m_wallet->use_fork_rules(6, 10)) {
+ MWARNING("Requested ring size " << (mixin + 1) << " too low for hard fork 6, using 5");
+ mixin = 4;
+ }
+ else if (mixin < 2 && m_wallet->use_fork_rules(2, 10)) {
+ MWARNING("Requested ring size " << (mixin + 1) << " too low for hard fork 2, using 3");
+ mixin = 2;
+ }
+ return mixin;
+ }
+ //------------------------------------------------------------------------------------------------------------------------------
void wallet_rpc_server::fill_transfer_entry(tools::wallet_rpc::transfer_entry &entry, const crypto::hash &txid, const crypto::hash &payment_id, const tools::wallet2::payment_details &pd)
{
entry.txid = string_tools::pod_to_hex(pd.m_tx_hash);
@@ -236,6 +249,7 @@ namespace tools
entry.height = pd.m_block_height;
entry.timestamp = pd.m_timestamp;
entry.amount = pd.m_amount;
+ entry.unlock_time = pd.m_unlock_time;
entry.fee = 0; // TODO
entry.note = m_wallet->get_tx_note(pd.m_tx_hash);
entry.type = "in";
@@ -249,6 +263,7 @@ namespace tools
entry.payment_id = entry.payment_id.substr(0,16);
entry.height = pd.m_block_height;
entry.timestamp = pd.m_timestamp;
+ entry.unlock_time = pd.m_unlock_time;
entry.fee = pd.m_amount_in - pd.m_amount_out;
uint64_t change = pd.m_change == (uint64_t)-1 ? 0 : pd.m_change; // change may not be known
entry.amount = pd.m_amount_in - change - entry.fee;
@@ -276,6 +291,7 @@ namespace tools
entry.timestamp = pd.m_timestamp;
entry.fee = pd.m_amount_in - pd.m_amount_out;
entry.amount = pd.m_amount_in - pd.m_change - entry.fee;
+ entry.unlock_time = pd.m_tx.unlock_time;
entry.note = m_wallet->get_tx_note(txid);
entry.type = is_failed ? "failed" : "pending";
}
@@ -289,6 +305,7 @@ namespace tools
entry.height = 0;
entry.timestamp = pd.m_timestamp;
entry.amount = pd.m_amount;
+ entry.unlock_time = pd.m_unlock_time;
entry.fee = 0; // TODO
entry.note = m_wallet->get_tx_note(pd.m_tx_hash);
entry.type = "pool";
@@ -352,10 +369,25 @@ namespace tools
cryptonote::tx_destination_entry de;
bool has_payment_id;
crypto::hash8 new_payment_id;
- if(!get_account_address_from_str_or_url(de.addr, has_payment_id, new_payment_id, m_wallet->testnet(), it->address, false))
+ er.message = "";
+ if(!get_account_address_from_str_or_url(de.addr, has_payment_id, new_payment_id, m_wallet->testnet(), it->address,
+ [&er](const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid)->std::string {
+ if (!dnssec_valid)
+ {
+ er.message = std::string("Invalid DNSSEC for ") + url;
+ return {};
+ }
+ if (addresses.empty())
+ {
+ er.message = std::string("No Monero address found at ") + url;
+ return {};
+ }
+ return addresses[0];
+ }))
{
er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS;
- er.message = std::string("WALLET_RPC_ERROR_CODE_WRONG_ADDRESS: ") + it->address;
+ if (er.message.empty())
+ er.message = std::string("WALLET_RPC_ERROR_CODE_WRONG_ADDRESS: ") + it->address;
return false;
}
de.amount = it->amount;
@@ -439,11 +471,7 @@ namespace tools
try
{
- uint64_t mixin = req.mixin;
- if (mixin < 2 && m_wallet->use_fork_rules(2, 10)) {
- LOG_PRINT_L1("Requested mixin " << req.mixin << " too low for hard fork 2, using 2");
- mixin = 2;
- }
+ uint64_t mixin = adjust_mixin(req.mixin);
std::vector<wallet2::pending_tx> ptx_vector = m_wallet->create_transactions_2(dsts, mixin, req.unlock_time, req.priority, extra, m_trusted_daemon);
// reject proposed transactions if there are more than one. see on_transfer_split below.
@@ -454,7 +482,8 @@ namespace tools
return false;
}
- m_wallet->commit_tx(ptx_vector);
+ if (!req.do_not_relay)
+ m_wallet->commit_tx(ptx_vector);
// populate response with tx hash
res.tx_hash = epee::string_tools::pod_to_hex(cryptonote::get_transaction_hash(ptx_vector.back().tx));
@@ -463,6 +492,13 @@ namespace tools
res.tx_key = epee::string_tools::pod_to_hex(ptx_vector.back().tx_key);
}
res.fee = ptx_vector.back().fee;
+
+ if (req.get_tx_hex)
+ {
+ cryptonote::blobdata blob;
+ tx_to_blob(ptx_vector.back().tx, blob);
+ res.tx_blob = epee::string_tools::buff_to_hex_nodelimer(blob);
+ }
return true;
}
catch (const tools::error::daemon_busy& e)
@@ -508,20 +544,19 @@ namespace tools
try
{
- uint64_t mixin = req.mixin;
+ uint64_t mixin = adjust_mixin(req.mixin);
uint64_t ptx_amount;
- if (mixin < 2 && m_wallet->use_fork_rules(2, 10)) {
- LOG_PRINT_L1("Requested mixin " << req.mixin << " too low for hard fork 2, using 2");
- mixin = 2;
- }
std::vector<wallet2::pending_tx> ptx_vector;
LOG_PRINT_L2("on_transfer_split calling create_transactions_2");
ptx_vector = m_wallet->create_transactions_2(dsts, mixin, req.unlock_time, req.priority, extra, m_trusted_daemon);
LOG_PRINT_L2("on_transfer_split called create_transactions_2");
- LOG_PRINT_L2("on_transfer_split calling commit_tx");
- m_wallet->commit_tx(ptx_vector);
- LOG_PRINT_L2("on_transfer_split called commit_tx");
+ if (!req.do_not_relay)
+ {
+ LOG_PRINT_L2("on_transfer_split calling commit_tx");
+ m_wallet->commit_tx(ptx_vector);
+ LOG_PRINT_L2("on_transfer_split called commit_tx");
+ }
// populate response with tx hashes
for (auto & ptx : ptx_vector)
@@ -538,6 +573,13 @@ namespace tools
res.amount_list.push_back(ptx_amount);
res.fee_list.push_back(ptx.fee);
+
+ if (req.get_tx_hex)
+ {
+ cryptonote::blobdata blob;
+ tx_to_blob(ptx.tx, blob);
+ res.tx_blob_list.push_back(epee::string_tools::buff_to_hex_nodelimer(blob));
+ }
}
return true;
@@ -577,7 +619,8 @@ namespace tools
{
std::vector<wallet2::pending_tx> ptx_vector = m_wallet->create_unmixable_sweep_transactions(m_trusted_daemon);
- m_wallet->commit_tx(ptx_vector);
+ if (!req.do_not_relay)
+ m_wallet->commit_tx(ptx_vector);
// populate response with tx hashes
for (auto & ptx : ptx_vector)
@@ -588,6 +631,12 @@ namespace tools
res.tx_key_list.push_back(epee::string_tools::pod_to_hex(ptx.tx_key));
}
res.fee_list.push_back(ptx.fee);
+ if (req.get_tx_hex)
+ {
+ cryptonote::blobdata blob;
+ tx_to_blob(ptx.tx, blob);
+ res.tx_blob_list.push_back(epee::string_tools::buff_to_hex_nodelimer(blob));
+ }
}
return true;
@@ -638,9 +687,11 @@ namespace tools
try
{
- std::vector<wallet2::pending_tx> ptx_vector = m_wallet->create_transactions_all(req.below_amount, dsts[0].addr, req.mixin, req.unlock_time, req.priority, extra, m_trusted_daemon);
+ uint64_t mixin = adjust_mixin(req.mixin);
+ std::vector<wallet2::pending_tx> ptx_vector = m_wallet->create_transactions_all(req.below_amount, dsts[0].addr, mixin, req.unlock_time, req.priority, extra, m_trusted_daemon);
- m_wallet->commit_tx(ptx_vector);
+ if (!req.do_not_relay)
+ m_wallet->commit_tx(ptx_vector);
// populate response with tx hashes
for (auto & ptx : ptx_vector)
@@ -650,7 +701,12 @@ namespace tools
{
res.tx_key_list.push_back(epee::string_tools::pod_to_hex(ptx.tx_key));
}
- res.fee_list.push_back(ptx.fee);
+ if (req.get_tx_hex)
+ {
+ cryptonote::blobdata blob;
+ tx_to_blob(ptx.tx, blob);
+ res.tx_blob_list.push_back(epee::string_tools::buff_to_hex_nodelimer(blob));
+ }
}
return true;
@@ -1018,10 +1074,23 @@ namespace tools
cryptonote::account_public_address address;
bool has_payment_id;
crypto::hash8 payment_id;
- if(!get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), req.address, false))
+ er.message = "";
+ if(!get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), req.address,
+ [&er](const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid)->std::string {
+ if (!dnssec_valid)
+ {
+ er.message = std::string("Invalid DNSSEC for ") + url;
+ return {};
+ }
+ if (addresses.empty())
+ {
+ er.message = std::string("No Monero address found at ") + url;
+ return {};
+ }
+ return addresses[0];
+ }))
{
er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS;
- er.message = "";
return false;
}
@@ -1301,7 +1370,12 @@ namespace tools
er.message = "Command unavailable in restricted mode.";
return false;
}
-
+ if (!m_trusted_daemon)
+ {
+ er.code = WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR;
+ er.message = "This command requires a trusted daemon.";
+ return false;
+ }
try
{
std::vector<std::pair<crypto::key_image, crypto::signature>> ski;
@@ -1412,10 +1486,25 @@ namespace tools
bool has_payment_id;
crypto::hash8 payment_id8;
crypto::hash payment_id = cryptonote::null_hash;
- if(!get_account_address_from_str_or_url(address, has_payment_id, payment_id8, m_wallet->testnet(), req.address, false))
+ er.message = "";
+ if(!get_account_address_from_str_or_url(address, has_payment_id, payment_id8, m_wallet->testnet(), req.address,
+ [&er](const std::string &url, const std::vector<std::string> &addresses, bool dnssec_valid)->std::string {
+ if (!dnssec_valid)
+ {
+ er.message = std::string("Invalid DNSSEC for ") + url;
+ return {};
+ }
+ if (addresses.empty())
+ {
+ er.message = std::string("No Monero address found at ") + url;
+ return {};
+ }
+ return addresses[0];
+ }))
{
er.code = WALLET_RPC_ERROR_CODE_WRONG_ADDRESS;
- er.message = std::string("WALLET_RPC_ERROR_CODE_WRONG_ADDRESS: ") + req.address;
+ if (er.message.empty())
+ er.message = std::string("WALLET_RPC_ERROR_CODE_WRONG_ADDRESS: ") + req.address;
return false;
}
if (has_payment_id)
diff --git a/src/wallet/wallet_rpc_server.h b/src/wallet/wallet_rpc_server.h
index 68e4c049a..dd54222b0 100644
--- a/src/wallet/wallet_rpc_server.h
+++ b/src/wallet/wallet_rpc_server.h
@@ -150,6 +150,7 @@ namespace tools
void fill_transfer_entry(tools::wallet_rpc::transfer_entry &entry, const crypto::hash &txid, const tools::wallet2::unconfirmed_transfer_details &pd);
void fill_transfer_entry(tools::wallet_rpc::transfer_entry &entry, const crypto::hash &payment_id, const tools::wallet2::payment_details &pd);
bool not_open(epee::json_rpc::error& er);
+ uint64_t adjust_mixin(uint64_t mixin);
wallet2 *m_wallet;
std::string m_wallet_dir;
diff --git a/src/wallet/wallet_rpc_server_commands_defs.h b/src/wallet/wallet_rpc_server_commands_defs.h
index 12ac281e4..fa5c154de 100644
--- a/src/wallet/wallet_rpc_server_commands_defs.h
+++ b/src/wallet/wallet_rpc_server_commands_defs.h
@@ -119,6 +119,8 @@ namespace wallet_rpc
uint64_t unlock_time;
std::string payment_id;
bool get_tx_key;
+ bool do_not_relay;
+ bool get_tx_hex;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(destinations)
@@ -127,6 +129,8 @@ namespace wallet_rpc
KV_SERIALIZE(unlock_time)
KV_SERIALIZE(payment_id)
KV_SERIALIZE(get_tx_key)
+ KV_SERIALIZE_OPT(do_not_relay, false)
+ KV_SERIALIZE_OPT(get_tx_hex, false)
END_KV_SERIALIZE_MAP()
};
@@ -136,12 +140,14 @@ namespace wallet_rpc
std::string tx_key;
std::list<std::string> amount_keys;
uint64_t fee;
+ std::string tx_blob;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(tx_hash)
KV_SERIALIZE(tx_key)
KV_SERIALIZE(amount_keys)
KV_SERIALIZE(fee)
+ KV_SERIALIZE(tx_blob)
END_KV_SERIALIZE_MAP()
};
};
@@ -156,6 +162,8 @@ namespace wallet_rpc
uint64_t unlock_time;
std::string payment_id;
bool get_tx_keys;
+ bool do_not_relay;
+ bool get_tx_hex;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(destinations)
@@ -164,6 +172,8 @@ namespace wallet_rpc
KV_SERIALIZE(unlock_time)
KV_SERIALIZE(payment_id)
KV_SERIALIZE(get_tx_keys)
+ KV_SERIALIZE_OPT(do_not_relay, false)
+ KV_SERIALIZE_OPT(get_tx_hex, false)
END_KV_SERIALIZE_MAP()
};
@@ -182,12 +192,14 @@ namespace wallet_rpc
std::list<std::string> tx_key_list;
std::list<uint64_t> amount_list;
std::list<uint64_t> fee_list;
+ std::list<std::string> tx_blob_list;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(tx_hash_list)
KV_SERIALIZE(tx_key_list)
KV_SERIALIZE(amount_list)
KV_SERIALIZE(fee_list)
+ KV_SERIALIZE(tx_blob_list)
END_KV_SERIALIZE_MAP()
};
};
@@ -197,9 +209,13 @@ namespace wallet_rpc
struct request
{
bool get_tx_keys;
+ bool do_not_relay;
+ bool get_tx_hex;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(get_tx_keys)
+ KV_SERIALIZE_OPT(do_not_relay, false)
+ KV_SERIALIZE_OPT(get_tx_hex, false)
END_KV_SERIALIZE_MAP()
};
@@ -217,11 +233,13 @@ namespace wallet_rpc
std::list<std::string> tx_hash_list;
std::list<std::string> tx_key_list;
std::list<uint64_t> fee_list;
+ std::list<std::string> tx_blob_list;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(tx_hash_list)
KV_SERIALIZE(tx_key_list)
KV_SERIALIZE(fee_list)
+ KV_SERIALIZE(tx_blob_list)
END_KV_SERIALIZE_MAP()
};
};
@@ -237,6 +255,8 @@ namespace wallet_rpc
std::string payment_id;
bool get_tx_keys;
uint64_t below_amount;
+ bool do_not_relay;
+ bool get_tx_hex;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(address)
@@ -246,6 +266,8 @@ namespace wallet_rpc
KV_SERIALIZE(payment_id)
KV_SERIALIZE(get_tx_keys)
KV_SERIALIZE(below_amount)
+ KV_SERIALIZE_OPT(do_not_relay, false)
+ KV_SERIALIZE_OPT(get_tx_hex, false)
END_KV_SERIALIZE_MAP()
};
@@ -263,11 +285,13 @@ namespace wallet_rpc
std::list<std::string> tx_hash_list;
std::list<std::string> tx_key_list;
std::list<uint64_t> fee_list;
+ std::list<std::string> tx_blob_list;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(tx_hash_list)
KV_SERIALIZE(tx_key_list)
KV_SERIALIZE(fee_list)
+ KV_SERIALIZE(tx_blob_list)
END_KV_SERIALIZE_MAP()
};
};
@@ -536,6 +560,7 @@ namespace wallet_rpc
std::string note;
std::list<transfer_destination> destinations;
std::string type;
+ uint64_t unlock_time;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(txid);
@@ -547,6 +572,7 @@ namespace wallet_rpc
KV_SERIALIZE(note);
KV_SERIALIZE(destinations);
KV_SERIALIZE(type);
+ KV_SERIALIZE(unlock_time)
END_KV_SERIALIZE_MAP()
};