diff options
Diffstat (limited to 'src')
29 files changed, 573 insertions, 101 deletions
diff --git a/src/blockchain_db/lmdb/db_lmdb.cpp b/src/blockchain_db/lmdb/db_lmdb.cpp index b5459b56b..1ad9876ac 100644 --- a/src/blockchain_db/lmdb/db_lmdb.cpp +++ b/src/blockchain_db/lmdb/db_lmdb.cpp @@ -51,12 +51,15 @@ using epee::string_tools::pod_to_hex; namespace { +#pragma pack(push, 1) +// This MUST be identical to output_data_t, without the extra rct data at the end struct pre_rct_output_data_t { crypto::public_key pubkey; //!< the output's public key (for spend verification) uint64_t unlock_time; //!< the output's unlock time (or height) uint64_t height; //!< the height of the block which created the output }; +#pragma pack(pop) template <typename T> inline void throw0(const T &e) @@ -877,12 +880,11 @@ void BlockchainLMDB::remove_tx_outputs(const uint64_t tx_id, const transaction& throw0(DB_ERROR("tx has outputs, but no output indices found")); } - bool is_miner_tx = tx.vin.size() == 1 && tx.vin[0].type() == typeid(txin_gen); - for (uint64_t i = tx.vout.size(); i > 0; --i) + bool is_pseudo_rct = tx.version >= 2 && tx.vin.size() == 1 && tx.vin[0].type() == typeid(txin_gen); + for (size_t i = tx.vout.size(); i-- > 0;) { - const tx_out tx_output = tx.vout[i-1]; - uint64_t amount = is_miner_tx && tx.version >= 2 ? 0 : tx_output.amount; - remove_output(amount, amount_output_indices[i-1]); + uint64_t amount = is_pseudo_rct ? 0 : tx.vout[i].amount; + remove_output(amount, amount_output_indices[i]); } } @@ -903,12 +905,12 @@ void BlockchainLMDB::remove_output(const uint64_t amount, const uint64_t& out_in else if (result) throw0(DB_ERROR(lmdb_error("DB error attempting to get an output", result).c_str())); - outkey *ok = (outkey *)v.mv_data; + const pre_rct_outkey *ok = (const pre_rct_outkey *)v.mv_data; MDB_val_set(otxk, ok->output_id); result = mdb_cursor_get(m_cur_output_txs, (MDB_val *)&zerokval, &otxk, MDB_GET_BOTH); if (result == MDB_NOTFOUND) { - LOG_PRINT_L0("Unexpected: global output index not found in m_output_txs"); + throw0(DB_ERROR("Unexpected: global output index not found in m_output_txs")); } else if (result) { @@ -2041,9 +2043,10 @@ std::vector<uint64_t> BlockchainLMDB::get_tx_amount_output_indices(const uint64_ else if (result) throw0(DB_ERROR(lmdb_error("DB error attempting to get data for tx_outputs[tx_index]", result).c_str())); - uint64_t* indices = (uint64_t*)v.mv_data; + const uint64_t* indices = (const uint64_t*)v.mv_data; int num_outputs = v.mv_size / sizeof(uint64_t); + amount_output_indices.reserve(num_outputs); for (int i = 0; i < num_outputs; ++i) { // LOG_PRINT_L0("amount output index[" << 2*i << "]" << ": " << paired_indices[2*i] << " global output index: " << paired_indices[2*i+1]); @@ -2597,7 +2600,7 @@ void BlockchainLMDB::get_output_key(const uint64_t &amount, const std::vector<ui auto get_result = mdb_cursor_get(m_cur_output_amounts, &k, &v, MDB_GET_BOTH); if (get_result == MDB_NOTFOUND) - throw1(OUTPUT_DNE("Attempting to get output pubkey by global index, but key does not exist")); + throw1(OUTPUT_DNE((std::string("Attempting to get output pubkey by global index (amount ") + boost::lexical_cast<std::string>(amount) + ", index " + boost::lexical_cast<std::string>(index) + ", count " + boost::lexical_cast<std::string>(get_num_outputs(amount)) + "), but key does not exist").c_str())); else if (get_result) throw0(DB_ERROR(lmdb_error("Error attempting to retrieve an output pubkey from the db", get_result).c_str())); @@ -2644,7 +2647,7 @@ void BlockchainLMDB::get_output_tx_and_index(const uint64_t& amount, const std:: else if (get_result) throw0(DB_ERROR(lmdb_error("Error attempting to retrieve an output from the db", get_result).c_str())); - outkey *okp = (outkey *)v.mv_data; + const outkey *okp = (const outkey *)v.mv_data; tx_indices.push_back(okp->output_id); } diff --git a/src/common/command_line.cpp b/src/common/command_line.cpp index b3f488447..28879e098 100644 --- a/src/common/command_line.cpp +++ b/src/common/command_line.cpp @@ -29,11 +29,22 @@ // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers #include "command_line.h" -#include "string_tools.h" +#include <boost/algorithm/string/compare.hpp> +#include <boost/algorithm/string/predicate.hpp> +#include "common/i18n.h" #include "cryptonote_config.h" +#include "string_tools.h" namespace command_line { + namespace + { + const char* tr(const char* str) + { + return i18n_translate(str, "command_line"); + } + } + std::string input_line(const std::string& prompt) { std::cout << prompt; @@ -45,6 +56,20 @@ namespace command_line } + bool is_yes(const std::string& str) + { + if (str == "y" || str == "Y") + return true; + + boost::algorithm::is_iequal ignore_case{}; + if (boost::algorithm::equals("yes", str, ignore_case)) + return true; + if (boost::algorithm::equals(command_line::tr("yes"), str, ignore_case)) + return true; + + return false; + } + const arg_descriptor<bool> arg_help = {"help", "Produce help message"}; const arg_descriptor<bool> arg_version = {"version", "Output version information"}; const arg_descriptor<std::string> arg_data_dir = {"data-dir", "Specify data directory"}; diff --git a/src/common/command_line.h b/src/common/command_line.h index 0ea749168..98c115bb7 100644 --- a/src/common/command_line.h +++ b/src/common/command_line.h @@ -43,6 +43,9 @@ namespace command_line std::string input_line(const std::string& prompt); + //! \return True if `str` is `is_iequal("y" || "yes" || `tr("yes"))`. + bool is_yes(const std::string& str); + template<typename T, bool required = false> struct arg_descriptor; diff --git a/src/common/stack_trace.cpp b/src/common/stack_trace.cpp index 4387d4ffd..5bbd3e252 100644 --- a/src/common/stack_trace.cpp +++ b/src/common/stack_trace.cpp @@ -104,7 +104,7 @@ void log_stack_trace(const char *msg) if (msg) LOG_PRINT2(log, msg, LOG_LEVEL_0); - LOG_PRINT2(log, "Unwinded call stack:", LOG_LEVEL_0); + LOG_PRINT2(log, "Unwound call stack:", LOG_LEVEL_0); if (unw_getcontext(&ctx) < 0) { LOG_PRINT2(log, "Failed to create unwind context", LOG_LEVEL_0); return; diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index c2ccf3db0..df9c0f3f3 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -73,6 +73,9 @@ extern "C" void slow_hash_free_state(); DISABLE_VS_WARNINGS(4267) +// used to overestimate the block reward when estimating a per kB to use +#define BLOCK_REWARD_OVERESTIMATE (10 * 1000000000000) + static const struct { uint8_t version; uint64_t height; @@ -118,7 +121,7 @@ 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_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) { LOG_PRINT_L3("Blockchain::" << __func__); } @@ -1146,7 +1149,7 @@ bool Blockchain::create_block_template(block& b, const account_public_address& m uint8_t hf_version = m_hardfork->get_current_version(); size_t max_outs = hf_version >= 4 ? 1 : 11; bool r = construct_miner_tx(height, median_size, already_generated_coins, txs_size, fee, miner_address, b.miner_tx, ex_nonce, max_outs, hf_version); - CHECK_AND_ASSERT_MES(r, false, "Failed to construc miner tx, first chance"); + CHECK_AND_ASSERT_MES(r, false, "Failed to construct miner tx, first chance"); size_t cumulative_size = txs_size + get_object_blobsize(b.miner_tx); #if defined(DEBUG_CREATE_BLOCK_TEMPLATE) LOG_PRINT_L1("Creating block template: miner tx size " << get_object_blobsize(b.miner_tx) << @@ -1156,7 +1159,7 @@ bool Blockchain::create_block_template(block& b, const account_public_address& m { r = construct_miner_tx(height, median_size, already_generated_coins, cumulative_size, fee, miner_address, b.miner_tx, ex_nonce, max_outs, hf_version); - CHECK_AND_ASSERT_MES(r, false, "Failed to construc miner tx, second chance"); + CHECK_AND_ASSERT_MES(r, false, "Failed to construct miner tx, second chance"); size_t coinbase_blob_size = get_object_blobsize(b.miner_tx); if (coinbase_blob_size > cumulative_size - txs_size) { @@ -1799,7 +1802,7 @@ bool Blockchain::find_blockchain_supplement(const std::list<crypto::hash>& qbloc auto gen_hash = m_db->get_block_hash_from_height(0); if(qblock_ids.back() != gen_hash) { - LOG_PRINT_L1("Client sent wrong NOTIFY_REQUEST_CHAIN: genesis block missmatch: " << std::endl << "id: " << qblock_ids.back() << ", " << std::endl << "expected: " << gen_hash << "," << std::endl << " dropping connection"); + LOG_PRINT_L1("Client sent wrong NOTIFY_REQUEST_CHAIN: genesis block mismatch: " << std::endl << "id: " << qblock_ids.back() << ", " << std::endl << "expected: " << gen_hash << "," << std::endl << " dropping connection"); m_db->block_txn_abort(); return false; } @@ -2231,6 +2234,19 @@ bool Blockchain::check_tx_outputs(const transaction& tx, tx_verification_context } } + // from v4, forbid invalid pubkeys + if (m_hardfork->get_current_version() >= 4) { + for (const auto &o: tx.vout) { + if (o.target.type() == typeid(txout_to_key)) { + const txout_to_key& out_to_key = boost::get<txout_to_key>(o.target); + if (!crypto::check_key(out_to_key.key)) { + tvc.m_invalid_output = true; + return false; + } + } + } + } + return true; } //------------------------------------------------------------------ @@ -2783,7 +2799,10 @@ uint64_t Blockchain::get_dynamic_per_kb_fee_estimate(uint64_t grace_blocks) cons uint64_t already_generated_coins = m_db->height() ? m_db->get_block_already_generated_coins(m_db->height() - 1) : 0; uint64_t base_reward; if (!get_block_reward(median, 1, already_generated_coins, base_reward, version)) - return false; + { + LOG_PRINT_L1("Failed to determine block reward, using placeholder " << print_money(BLOCK_REWARD_OVERESTIMATE) << " as a high bound"); + base_reward = BLOCK_REWARD_OVERESTIMATE; + } uint64_t fee = get_dynamic_per_kb_fee(base_reward, median); LOG_PRINT_L2("Estimating " << grace_blocks << "-block fee at " << print_money(fee) << "/kB"); @@ -3436,6 +3455,8 @@ void Blockchain::block_longhash_worker(const uint64_t height, const std::vector< // the height of the block passed to it for (const auto & block : blocks) { + if (m_cancel) + return; crypto::hash id = get_block_hash(block); crypto::hash pow = get_block_longhash(block, height); map.emplace(id, pow); @@ -3611,6 +3632,9 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::list<block_complete_e thread_list.clear(); + if (m_cancel) + return false; + for (const auto & map : maps) { m_blocks_longhash_table.insert(map.begin(), map.end()); @@ -3618,6 +3642,9 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::list<block_complete_e } } + if (m_cancel) + return false; + if (blocks_exist) { LOG_PRINT_L0("Skipping prepare blocks. Blocks exist."); @@ -3655,6 +3682,9 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::list<block_complete_e // generate sorted tables for all amounts and absolute offsets for (const auto &entry : blocks_entry) { + if (m_cancel) + return false; + for (const auto &tx_blob : entry.txs) { crypto::hash tx_hash = null_hash; @@ -3763,6 +3793,9 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::list<block_complete_e // now generate a table for each tx_prefix and k_image hashes for (const auto &entry : blocks_entry) { + if (m_cancel) + return false; + for (const auto &tx_blob : entry.txs) { crypto::hash tx_hash = null_hash; @@ -3844,6 +3877,11 @@ std::map<uint64_t, std::tuple<uint64_t, uint64_t, uint64_t>> Blockchain:: get_ou return m_db->get_output_histogram(amounts, unlocked, recent_cutoff); } +void Blockchain::cancel() +{ + m_cancel = true; +} + #if defined(PER_BLOCK_CHECKPOINT) void Blockchain::load_compiled_in_block_hashes() { diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h index f9ae9d8aa..9afc22657 100644 --- a/src/cryptonote_core/blockchain.h +++ b/src/cryptonote_core/blockchain.h @@ -843,6 +843,9 @@ namespace cryptonote */ void block_longhash_worker(const uint64_t height, const std::vector<block> &blocks, std::unordered_map<crypto::hash, crypto::hash> &map) const; + + void cancel(); + private: // TODO: evaluate whether or not each of these typedefs are left over from blockchain_storage @@ -912,6 +915,8 @@ namespace cryptonote bool m_testnet; + std::atomic<bool> m_cancel; + /** * @brief collects the keys for all outputs being "spent" as an input * diff --git a/src/cryptonote_core/cryptonote_basic_impl.cpp b/src/cryptonote_core/cryptonote_basic_impl.cpp index 74f44e2af..4f35b8298 100644 --- a/src/cryptonote_core/cryptonote_basic_impl.cpp +++ b/src/cryptonote_core/cryptonote_basic_impl.cpp @@ -199,7 +199,7 @@ namespace cryptonote { uint64_t prefix; if (!tools::base58::decode_addr(str, prefix, data)) { - LOG_PRINT_L1("Invalid address format"); + LOG_PRINT_L2("Invalid address format"); return false; } diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp index 84a41cfbf..3ddda9efb 100644 --- a/src/cryptonote_core/cryptonote_core.cpp +++ b/src/cryptonote_core/cryptonote_core.cpp @@ -123,7 +123,7 @@ namespace cryptonote //----------------------------------------------------------------------------------- void core::stop() { - graceful_exit(); + m_blockchain_storage.cancel(); } //----------------------------------------------------------------------------------- void core::init_options(boost::program_options::options_description& desc) @@ -315,7 +315,7 @@ namespace cryptonote } else { - LOG_ERROR("Attempted to use non-existant database type"); + LOG_ERROR("Attempted to use non-existent database type"); return false; } @@ -716,7 +716,7 @@ namespace cryptonote crypto::hash tx_hash, tx_prefix_hash; if (!parse_and_validate_tx_from_blob(tx_blob, tx, tx_hash, tx_prefix_hash)) { - LOG_ERROR("Failed to parse relayed tranasction"); + LOG_ERROR("Failed to parse relayed transaction"); return; } txs.push_back(std::make_pair(tx_hash, std::move(tx))); diff --git a/src/cryptonote_core/cryptonote_format_utils.cpp b/src/cryptonote_core/cryptonote_format_utils.cpp index d88f66e3b..394a43831 100644 --- a/src/cryptonote_core/cryptonote_format_utils.cpp +++ b/src/cryptonote_core/cryptonote_format_utils.cpp @@ -553,7 +553,7 @@ namespace cryptonote //check that derivated key is equal with real output key if( !(in_ephemeral.pub == src_entr.outputs[src_entr.real_output].second.dest) ) { - LOG_ERROR("derived public key missmatch with output public key! "<< ENDL << "derived_key:" + LOG_ERROR("derived public key mismatch with output public key! "<< ENDL << "derived_key:" << string_tools::pod_to_hex(in_ephemeral.pub) << ENDL << "real output_public_key:" << string_tools::pod_to_hex(src_entr.outputs[src_entr.real_output].second) ); return false; diff --git a/src/cryptonote_core/tx_pool.cpp b/src/cryptonote_core/tx_pool.cpp index e72a592ca..4cfd61f9f 100644 --- a/src/cryptonote_core/tx_pool.cpp +++ b/src/cryptonote_core/tx_pool.cpp @@ -205,7 +205,7 @@ namespace cryptonote { //update transactions container auto txd_p = m_transactions.insert(transactions_container::value_type(id, txd)); - CHECK_AND_ASSERT_MES(txd_p.second, false, "intrnal error: transaction already exists at inserting in memorypool"); + CHECK_AND_ASSERT_MES(txd_p.second, false, "internal error: transaction already exists at inserting in memorypool"); txd_p.first->second.blob_size = blob_size; txd_p.first->second.kept_by_block = kept_by_block; txd_p.first->second.fee = fee; diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.h b/src/cryptonote_protocol/cryptonote_protocol_handler.h index ab5d8230d..08dde4904 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.h +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.h @@ -109,6 +109,7 @@ namespace cryptonote bool is_synchronized(){return m_synchronized;} void log_connections(); std::list<connection_info> get_connections(); + void stop(); private: //----------------- commands handlers ---------------------------------------------- int handle_notify_new_block(int command, NOTIFY_NEW_BLOCK::request& arg, cryptonote_connection_context& context); @@ -135,29 +136,30 @@ namespace cryptonote std::atomic<uint32_t> m_syncronized_connections_count; std::atomic<bool> m_synchronized; bool m_one_request = true; + std::atomic<bool> m_stopping; // static std::ofstream m_logreq; boost::mutex m_buffer_mutex; double get_avg_block_size(); boost::circular_buffer<size_t> m_avg_buffer = boost::circular_buffer<size_t>(10); - template<class t_parametr> - bool post_notify(typename t_parametr::request& arg, cryptonote_connection_context& context) + template<class t_parameter> + bool post_notify(typename t_parameter::request& arg, cryptonote_connection_context& context) { - LOG_PRINT_L2("[" << epee::net_utils::print_connection_context_short(context) << "] post " << typeid(t_parametr).name() << " -->"); + LOG_PRINT_L2("[" << epee::net_utils::print_connection_context_short(context) << "] post " << typeid(t_parameter).name() << " -->"); std::string blob; epee::serialization::store_t_to_binary(arg, blob); //handler_response_blocks_now(blob.size()); // XXX - return m_p2p->invoke_notify_to_peer(t_parametr::ID, blob, context); + return m_p2p->invoke_notify_to_peer(t_parameter::ID, blob, context); } - template<class t_parametr> - bool relay_post_notify(typename t_parametr::request& arg, cryptonote_connection_context& exlude_context) + template<class t_parameter> + bool relay_post_notify(typename t_parameter::request& arg, cryptonote_connection_context& exclude_context) { - LOG_PRINT_L2("[" << epee::net_utils::print_connection_context_short(exlude_context) << "] post relay " << typeid(t_parametr).name() << " -->"); + LOG_PRINT_L2("[" << epee::net_utils::print_connection_context_short(exclude_context) << "] post relay " << typeid(t_parameter).name() << " -->"); std::string arg_buff; epee::serialization::store_t_to_binary(arg, arg_buff); - return m_p2p->relay_notify_to_all(t_parametr::ID, arg_buff, exlude_context); + return m_p2p->relay_notify_to_all(t_parameter::ID, arg_buff, exclude_context); } virtual std::ofstream& get_logreq() const ; diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.inl b/src/cryptonote_protocol/cryptonote_protocol_handler.inl index 65377f990..51ade0221 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.inl +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.inl @@ -60,7 +60,8 @@ namespace cryptonote t_cryptonote_protocol_handler<t_core>::t_cryptonote_protocol_handler(t_core& rcore, nodetool::i_p2p_endpoint<connection_context>* p_net_layout):m_core(rcore), m_p2p(p_net_layout), m_syncronized_connections_count(0), - m_synchronized(false) + m_synchronized(false), + m_stopping(false) { if(!m_p2p) @@ -793,6 +794,11 @@ namespace cryptonote size_t count = 0; BOOST_FOREACH(const block_complete_entry& block_entry, arg.blocks) { + if (m_stopping) + { + return 1; + } + ++count; block b; if(!parse_and_validate_block_from_blob(block_entry.block, b)) @@ -857,6 +863,12 @@ namespace cryptonote m_core.prepare_handle_incoming_blocks(arg.blocks); BOOST_FOREACH(const block_complete_entry& block_entry, arg.blocks) { + if (m_stopping) + { + m_core.cleanup_handle_incoming_blocks(); + return 1; + } + // process transactions TIME_MEASURE_START(transactions_process_time); BOOST_FOREACH(auto& tx_blob, block_entry.txs) @@ -1092,21 +1104,36 @@ namespace cryptonote std::list<blobdata> fluffy_txs; fluffy_arg.b = arg.b; fluffy_arg.b.txs = fluffy_txs; - - m_p2p->for_each_connection([this, &arg, &fluffy_arg](connection_context& cntxt, nodetool::peerid_type peer_id, uint32_t support_flags) + + // pre-serialize them + std::string fullBlob, fluffyBlob; + epee::serialization::store_t_to_binary(arg, fullBlob); + epee::serialization::store_t_to_binary(fluffy_arg, fluffyBlob); + + // sort peers between fluffy ones and others + std::list<boost::uuids::uuid> fullConnections, fluffyConnections; + m_p2p->for_each_connection([this, &arg, &fluffy_arg, &exclude_context, &fullConnections, &fluffyConnections](connection_context& context, nodetool::peerid_type peer_id, uint32_t support_flags) { - if(m_core.get_testnet() && support_flags & P2P_SUPPORT_FLAG_FLUFFY_BLOCKS) - { - LOG_PRINT_YELLOW("PEER SUPPORTS FLUFFY BLOCKS - RELAYING THIN/COMPACT WHATEVER BLOCK", LOG_LEVEL_1); - return post_notify<NOTIFY_NEW_FLUFFY_BLOCK>(fluffy_arg, cntxt); - } - else + if (peer_id && exclude_context.m_connection_id != context.m_connection_id) { - LOG_PRINT_YELLOW("PEER DOESN'T SUPPORT FLUFFY BLOCKS - RELAYING FULL BLOCK", LOG_LEVEL_1); - return post_notify<NOTIFY_NEW_BLOCK>(arg, cntxt); + if(m_core.get_testnet() && (support_flags & P2P_SUPPORT_FLAG_FLUFFY_BLOCKS)) + { + LOG_PRINT_CCONTEXT_YELLOW("PEER SUPPORTS FLUFFY BLOCKS - RELAYING THIN/COMPACT WHATEVER BLOCK", LOG_LEVEL_1); + fluffyConnections.push_back(context.m_connection_id); + } + else + { + LOG_PRINT_CCONTEXT_YELLOW("PEER DOESN'T SUPPORT FLUFFY BLOCKS - RELAYING FULL BLOCK", LOG_LEVEL_1); + fullConnections.push_back(context.m_connection_id); + } } + return true; }); - + + // send fluffy ones first, we want to encourage people to run that + m_p2p->relay_notify_to_list(NOTIFY_NEW_FLUFFY_BLOCK::ID, fluffyBlob, fluffyConnections); + m_p2p->relay_notify_to_list(NOTIFY_NEW_BLOCK::ID, fullBlob, fullConnections); + return 1; } //------------------------------------------------------------------------------------------------------------------------ @@ -1131,5 +1158,11 @@ namespace cryptonote (*logreq) << "log used" << std::endl; return *logreq; } - + //------------------------------------------------------------------------------------------------------------------------ + template<class t_core> + void t_cryptonote_protocol_handler<t_core>::stop() + { + m_stopping = true; + m_core.stop(); + } } // namespace diff --git a/src/p2p/net_node.h b/src/p2p/net_node.h index 619bad40f..cc6a486d3 100644 --- a/src/p2p/net_node.h +++ b/src/p2p/net_node.h @@ -172,6 +172,7 @@ namespace nodetool virtual void on_connection_close(p2p_connection_context& context); virtual void callback(p2p_connection_context& context); //----------------- i_p2p_endpoint ------------------------------------------------------------- + virtual bool relay_notify_to_list(int command, const std::string& data_buff, const std::list<boost::uuids::uuid> &connections); virtual bool relay_notify_to_all(int command, const std::string& data_buff, const epee::net_utils::connection_context_base& context); virtual bool invoke_command_to_peer(int command, const std::string& req_buff, std::string& resp_buff, const epee::net_utils::connection_context_base& context); virtual bool invoke_notify_to_peer(int command, const std::string& req_buff, const epee::net_utils::connection_context_base& context); diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index b5bfc2979..442c42517 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -203,15 +203,17 @@ namespace nodetool m_blocked_ips[addr] = time(nullptr) + seconds; // drop any connection to that IP - while (!m_net_server.get_config_object().foreach_connection([&](const p2p_connection_context& cntxt) + std::list<boost::uuids::uuid> conns; + m_net_server.get_config_object().foreach_connection([&](const p2p_connection_context& cntxt) { if (cntxt.m_remote_ip == addr) { - drop_connection(cntxt); - return false; + conns.push_back(cntxt.m_connection_id); } return true; - })); + }); + for (const auto &c: conns) + m_net_server.get_config_object().close(c); LOG_PRINT_CYAN("IP " << epee::string_tools::get_ip_string_from_int32(addr) << " blocked.", LOG_LEVEL_0); return true; @@ -541,7 +543,7 @@ namespace nodetool } else if (result == 2) { LOG_PRINT_L0("IGD was found but reported as not connected."); } else if (result == 3) { - LOG_PRINT_L0("UPnP device was found but not recoginzed as IGD."); + LOG_PRINT_L0("UPnP device was found but not recognized as IGD."); } else { LOG_ERROR("UPNP_GetValidIGD returned an unknown result code."); } @@ -654,6 +656,7 @@ namespace nodetool template<class t_payload_net_handler> bool node_server<t_payload_net_handler>::send_stop_signal() { + m_payload_handler.stop(); m_net_server.send_stop_signal(); LOG_PRINT_L0("[node] Stop signal sent"); return true; @@ -1242,6 +1245,16 @@ namespace nodetool } //----------------------------------------------------------------------------------- template<class t_payload_net_handler> + bool node_server<t_payload_net_handler>::relay_notify_to_list(int command, const std::string& data_buff, const std::list<boost::uuids::uuid> &connections) + { + BOOST_FOREACH(const auto& c_id, connections) + { + m_net_server.get_config_object().notify(command, data_buff, c_id); + } + return true; + } + //----------------------------------------------------------------------------------- + template<class t_payload_net_handler> bool node_server<t_payload_net_handler>::relay_notify_to_all(int command, const std::string& data_buff, const epee::net_utils::connection_context_base& context) { std::list<boost::uuids::uuid> connections; @@ -1251,12 +1264,7 @@ namespace nodetool connections.push_back(cntxt.m_connection_id); return true; }); - - BOOST_FOREACH(const auto& c_id, connections) - { - m_net_server.get_config_object().notify(command, data_buff, c_id); - } - return true; + return relay_notify_to_list(command, data_buff, connections); } //----------------------------------------------------------------------------------- template<class t_payload_net_handler> @@ -1477,10 +1485,10 @@ namespace nodetool template<class t_payload_net_handler> bool node_server<t_payload_net_handler>::log_peerlist() { - std::list<peerlist_entry> pl_wite; + std::list<peerlist_entry> pl_white; std::list<peerlist_entry> pl_gray; - m_peerlist.get_peerlist_full(pl_gray, pl_wite); - LOG_PRINT_L0(ENDL << "Peerlist white:" << ENDL << print_peerlist_to_string(pl_wite) << ENDL << "Peerlist gray:" << ENDL << print_peerlist_to_string(pl_gray) ); + m_peerlist.get_peerlist_full(pl_gray, pl_white); + LOG_PRINT_L0(ENDL << "Peerlist white:" << ENDL << print_peerlist_to_string(pl_white) << ENDL << "Peerlist gray:" << ENDL << print_peerlist_to_string(pl_gray) ); return true; } //----------------------------------------------------------------------------------- diff --git a/src/p2p/net_node_common.h b/src/p2p/net_node_common.h index 846c07779..69bee890c 100644 --- a/src/p2p/net_node_common.h +++ b/src/p2p/net_node_common.h @@ -43,6 +43,7 @@ namespace nodetool template<class t_connection_context> struct i_p2p_endpoint { + virtual bool relay_notify_to_list(int command, const std::string& data_buff, const std::list<boost::uuids::uuid>& connections)=0; virtual bool relay_notify_to_all(int command, const std::string& data_buff, const epee::net_utils::connection_context_base& context)=0; virtual bool invoke_command_to_peer(int command, const std::string& req_buff, std::string& resp_buff, const epee::net_utils::connection_context_base& context)=0; virtual bool invoke_notify_to_peer(int command, const std::string& req_buff, const epee::net_utils::connection_context_base& context)=0; @@ -59,6 +60,10 @@ namespace nodetool template<class t_connection_context> struct p2p_endpoint_stub: public i_p2p_endpoint<t_connection_context> { + virtual bool relay_notify_to_list(int command, const std::string& data_buff, const std::list<boost::uuids::uuid>& connections) + { + return false; + } virtual bool relay_notify_to_all(int command, const std::string& data_buff, const epee::net_utils::connection_context_base& context) { return false; diff --git a/src/ringct/rctTypes.h b/src/ringct/rctTypes.h index 25f6f9bc9..b1921b71a 100644 --- a/src/ringct/rctTypes.h +++ b/src/ringct/rctTypes.h @@ -281,6 +281,7 @@ namespace rct { // we save the MGs contents directly, because we want it to save its // arrays and matrices without the size prefixes, and the load can't // know what size to expect if it's not in the data + ar.begin_object(); ar.tag("ss"); ar.begin_array(); PREPARE_CUSTOM_VECTOR_SERIALIZATION(mixin + 1, MGs[i].ss); @@ -296,7 +297,7 @@ namespace rct { for (size_t k = 0; k < mg_ss2_elements; ++k) { FIELDS(MGs[i].ss[j][k]) - if (mg_ss2_elements - j > 1) + if (mg_ss2_elements - k > 1) ar.delimit_array(); } ar.end_array(); @@ -306,10 +307,13 @@ namespace rct { } ar.end_array(); + ar.tag("cc"); FIELDS(MGs[i].cc) // MGs[i].II not saved, it can be reconstructed if (mg_elements - i > 1) ar.delimit_array(); + + ar.end_object(); } ar.end_array(); return true; diff --git a/src/rpc/core_rpc_server_commands_defs.h b/src/rpc/core_rpc_server_commands_defs.h index e19238c44..23fcb0a92 100644 --- a/src/rpc/core_rpc_server_commands_defs.h +++ b/src/rpc/core_rpc_server_commands_defs.h @@ -41,7 +41,16 @@ namespace cryptonote #define CORE_RPC_STATUS_BUSY "BUSY" #define CORE_RPC_STATUS_NOT_MINING "NOT MINING" -#define CORE_RPC_VERSION 5 +// When making *any* change here, bump minor +// If the change is incompatible, then bump major and set minor to 0 +// This ensures CORE_RPC_VERSION always increases, that every change +// has its own version, and that clients can just test major to see +// whether they can talk to a given daemon without having to know in +// 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 0 +#define CORE_RPC_VERSION (((CORE_RPC_VERSION_MAJOR)<<16)|(CORE_RPC_VERSION_MINOR)) struct COMMAND_RPC_GET_HEIGHT { diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 462cdf58f..faeed31ce 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -212,17 +212,17 @@ namespace return message_writer(epee::log_space::console_color_red, true, sw::tr("Error: "), LOG_LEVEL_0); } - bool is_it_true(std::string s) + bool is_it_true(const std::string& s) { - std::transform(s.begin(), s.end(), s.begin(), ::tolower); - if (s == "true") + if (s == "1" || command_line::is_yes(s)) return true; - if (s == "1") - return true; - if (s == "y" || s == "yes") + + boost::algorithm::is_iequal ignore_case{}; + if (boost::algorithm::equals("true", s, ignore_case)) return true; - if (s == sw::tr("yes")) + if (boost::algorithm::equals(simple_wallet::tr("true"), s, ignore_case)) return true; + return false; } @@ -263,6 +263,10 @@ namespace return "invalid"; } + std::string get_version_string(uint32_t version) + { + return boost::lexical_cast<std::string>(version >> 16) + "." + boost::lexical_cast<std::string>(version & 0xffff); + } } @@ -916,7 +920,7 @@ bool simple_wallet::ask_wallet_create_if_needed() LOG_ERROR("Unexpected std::cin.eof() - Exited simple_wallet::ask_wallet_create_if_needed()"); return false; } - if(is_it_true(confirm_creation)) + if(command_line::is_yes(confirm_creation)) { success_msg_writer() << tr("Generating new wallet..."); m_generate_new = wallet_path; @@ -1189,8 +1193,8 @@ bool simple_wallet::handle_command_line(const boost::program_options::variables_ //---------------------------------------------------------------------------------------------------- bool simple_wallet::try_connect_to_daemon(bool silent) { - bool same_version = false; - if (!m_wallet->check_connection(&same_version)) + uint32_t version = 0; + if (!m_wallet->check_connection(&version)) { if (!silent) fail_msg_writer() << tr("wallet failed to connect to daemon: ") << m_wallet->get_daemon_address() << ". " << @@ -1198,11 +1202,10 @@ bool simple_wallet::try_connect_to_daemon(bool silent) "Please make sure daemon is running or restart the wallet with the correct daemon address."); return false; } - if (!m_allow_mismatched_daemon_version && !same_version) + if (!m_allow_mismatched_daemon_version && ((version >> 16) != CORE_RPC_VERSION_MAJOR)) { if (!silent) - fail_msg_writer() << tr("Daemon uses a different RPC version that the wallet: ") << m_wallet->get_daemon_address() << ". " << - tr("Either update one of them, or use --allow-mismatched-daemon-version."); + fail_msg_writer() << boost::format(tr("Daemon uses a different RPC major version (%u) than the wallet (%u): %s. Either update one of them, or use --allow-mismatched-daemon-version.")) % (version>>16) % CORE_RPC_VERSION_MAJOR % m_wallet->get_daemon_address(); return false; } return true; @@ -1977,8 +1980,7 @@ bool simple_wallet::get_address_from_str(const std::string &str, cryptonote::acc { return false; } - if (confirm_dns_ok != "Y" && confirm_dns_ok != "y" && confirm_dns_ok != "Yes" && confirm_dns_ok != "yes" - && confirm_dns_ok != tr("yes") && confirm_dns_ok != tr("no")) + if (!command_line::is_yes(confirm_dns_ok)) { fail_msg_writer() << tr("you have cancelled the transfer request"); return false; @@ -2136,7 +2138,7 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri std::string accepted = command_line::input_line(tr("No payment id is included with this transaction. Is this okay? (Y/Yes/N/No)")); if (std::cin.eof()) return true; - if (accepted != "Y" && accepted != "y" && accepted != "Yes" && accepted != "yes") + if (!command_line::is_yes(accepted)) { fail_msg_writer() << tr("transaction cancelled."); @@ -2220,7 +2222,7 @@ bool simple_wallet::transfer_main(int transfer_type, const std::vector<std::stri std::string accepted = command_line::input_line(prompt.str()); if (std::cin.eof()) return true; - if (accepted != "Y" && accepted != "y" && accepted != "Yes" && accepted != "yes") + if (!command_line::is_yes(accepted)) { fail_msg_writer() << tr("transaction cancelled."); @@ -2399,7 +2401,7 @@ bool simple_wallet::sweep_unmixable(const std::vector<std::string> &args_) std::string accepted = command_line::input_line(prompt_str); if (std::cin.eof()) return true; - if (accepted != "Y" && accepted != "y" && accepted != "Yes" && accepted != "yes") + if (!command_line::is_yes(accepted)) { fail_msg_writer() << tr("transaction cancelled."); @@ -2613,7 +2615,7 @@ bool simple_wallet::sweep_all(const std::vector<std::string> &args_) std::string accepted = command_line::input_line(tr("No payment id is included with this transaction. Is this okay? (Y/Yes/N/No)")); if (std::cin.eof()) return true; - if (accepted != "Y" && accepted != "y" && accepted != "Yes" && accepted != "yes") + if (!command_line::is_yes(accepted)) { fail_msg_writer() << tr("transaction cancelled."); @@ -2658,7 +2660,7 @@ bool simple_wallet::sweep_all(const std::vector<std::string> &args_) std::string accepted = command_line::input_line(prompt_str); if (std::cin.eof()) return true; - if (accepted != "Y" && accepted != "y" && accepted != "Yes" && accepted != "yes") + if (!command_line::is_yes(accepted)) { fail_msg_writer() << tr("transaction cancelled."); @@ -2853,8 +2855,7 @@ bool simple_wallet::accept_loaded_tx(const std::function<size_t()> get_num_txes, 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 accepted = command_line::input_line(prompt_str); - return is_it_true(accepted); + return command_line::is_yes(command_line::input_line(prompt_str)); } //---------------------------------------------------------------------------------------------------- bool simple_wallet::accept_loaded_tx(const tools::wallet2::unsigned_tx_set &txs) @@ -3564,7 +3565,8 @@ bool simple_wallet::get_tx_note(const std::vector<std::string> &args) bool simple_wallet::status(const std::vector<std::string> &args) { uint64_t local_height = m_wallet->get_blockchain_current_height(); - if (!try_connect_to_daemon()) + uint32_t version = 0; + if (!m_wallet->check_connection(&version)) { success_msg_writer() << "Refreshed " << local_height << "/?, no daemon connected"; return true; @@ -3575,7 +3577,8 @@ bool simple_wallet::status(const std::vector<std::string> &args) if (err.empty()) { bool synced = local_height == bc_height; - success_msg_writer() << "Refreshed " << local_height << "/" << bc_height << ", " << (synced ? "synced" : "syncing"); + success_msg_writer() << "Refreshed " << local_height << "/" << bc_height << ", " << (synced ? "synced" : "syncing") + << ", daemon RPC v" << get_version_string(version); } else { diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index 215b61aef..a772498cd 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -399,6 +399,11 @@ std::string WalletImpl::integratedAddress(const std::string &payment_id) const return m_wallet->get_account().get_public_integrated_address_str(pid, m_wallet->testnet()); } +std::string WalletImpl::path() const +{ + return m_wallet->path(); +} + bool WalletImpl::store(const std::string &path) { clearStatus(); @@ -521,7 +526,7 @@ bool WalletImpl::refresh() void WalletImpl::refreshAsync() { - LOG_PRINT_L3(__FUNCTION__ << ": Refreshing asyncronously.."); + LOG_PRINT_L3(__FUNCTION__ << ": Refreshing asynchronously.."); clearStatus(); m_refreshCV.notify_one(); } @@ -906,11 +911,11 @@ bool WalletImpl::connectToDaemon() Wallet::ConnectionStatus WalletImpl::connected() const { - bool same_version = false; - bool is_connected = m_wallet->check_connection(&same_version); + uint32_t version = 0; + bool is_connected = m_wallet->check_connection(&version); if (!is_connected) return Wallet::ConnectionStatus_Disconnected; - if (!same_version) + if ((version >> 16) != CORE_RPC_VERSION_MAJOR) return Wallet::ConnectionStatus_WrongVersion; return Wallet::ConnectionStatus_Connected; } diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h index 02a46da8c..5e4a64ff8 100644 --- a/src/wallet/api/wallet.h +++ b/src/wallet/api/wallet.h @@ -64,6 +64,7 @@ public: bool setPassword(const std::string &password); std::string address() const; std::string integratedAddress(const std::string &payment_id) const; + std::string path() const; bool store(const std::string &path); std::string filename() const; std::string keysFilename() const; diff --git a/src/wallet/api/wallet_manager.cpp b/src/wallet/api/wallet_manager.cpp index 2d1c44d0e..25b081921 100644 --- a/src/wallet/api/wallet_manager.cpp +++ b/src/wallet/api/wallet_manager.cpp @@ -130,9 +130,26 @@ std::string WalletManagerImpl::errorString() const return m_errorString; } -void WalletManagerImpl::setDaemonHost(const std::string &hostname) +void WalletManagerImpl::setDaemonAddress(const std::string &address) { + m_daemonAddress = address; +} +bool WalletManagerImpl::connected(uint32_t *version = NULL) const +{ + epee::json_rpc::request<cryptonote::COMMAND_RPC_GET_VERSION::request> req_t = AUTO_VAL_INIT(req_t); + epee::json_rpc::response<cryptonote::COMMAND_RPC_GET_VERSION::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_version"; + epee::net_utils::http::http_simple_client http_client; + bool r = epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/json_rpc", req_t, resp_t, http_client); + if (!r) + return false; + + if (version) + *version = resp_t.result.version; + return true; } bool WalletManagerImpl::checkPayment(const std::string &address_text, const std::string &txid_text, const std::string &txkey_text, const std::string &daemon_address, uint64_t &received, uint64_t &height, std::string &error) const @@ -287,6 +304,52 @@ bool WalletManagerImpl::checkPayment(const std::string &address_text, const std: return true; } +uint64_t WalletManagerImpl::blockchainHeight() const +{ + cryptonote::COMMAND_RPC_GET_INFO::request ireq; + cryptonote::COMMAND_RPC_GET_INFO::response ires; + + epee::net_utils::http::http_simple_client http_client; + if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/getinfo", ireq, ires, http_client)) + return 0; + return ires.height; +} + +uint64_t WalletManagerImpl::blockchainTargetHeight() const +{ + cryptonote::COMMAND_RPC_GET_INFO::request ireq; + cryptonote::COMMAND_RPC_GET_INFO::response ires; + + epee::net_utils::http::http_simple_client http_client; + if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/getinfo", ireq, ires, http_client)) + return 0; + return ires.target_height >= ires.height ? ires.target_height : ires.height; +} + +uint64_t WalletManagerImpl::networkDifficulty() const +{ + cryptonote::COMMAND_RPC_GET_INFO::request ireq; + cryptonote::COMMAND_RPC_GET_INFO::response ires; + + epee::net_utils::http::http_simple_client http_client; + if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/getinfo", ireq, ires, http_client)) + return 0; + return ires.difficulty; +} + +double WalletManagerImpl::miningHashRate() const +{ + cryptonote::COMMAND_RPC_MINING_STATUS::request mreq; + cryptonote::COMMAND_RPC_MINING_STATUS::response mres; + + epee::net_utils::http::http_simple_client http_client; + if (!epee::net_utils::invoke_http_json_remote_command2(m_daemonAddress + "/getinfo", mreq, mres, http_client)) + return 0.0; + if (!mres.active) + return 0.0; + return mres.speed; +} + ///////////////////// WalletManagerFactory implementation ////////////////////// WalletManager *WalletManagerFactory::getWalletManager() diff --git a/src/wallet/api/wallet_manager.h b/src/wallet/api/wallet_manager.h index 489abe764..d454548f8 100644 --- a/src/wallet/api/wallet_manager.h +++ b/src/wallet/api/wallet_manager.h @@ -45,12 +45,18 @@ public: bool walletExists(const std::string &path); std::vector<std::string> findWallets(const std::string &path); std::string errorString() const; - void setDaemonHost(const std::string &hostname); + void setDaemonAddress(const std::string &address); + bool connected(uint32_t *version) const; bool checkPayment(const std::string &address, const std::string &txid, const std::string &txkey, const std::string &daemon_address, uint64_t &received, uint64_t &height, std::string &error) const; + uint64_t blockchainHeight() const; + uint64_t blockchainTargetHeight() const; + uint64_t networkDifficulty() const; + double miningHashRate() const; private: WalletManagerImpl() {} friend struct WalletManagerFactory; + std::string m_daemonAddress; std::string m_errorString; }; diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index f637c8b03..2d293d45c 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -2220,7 +2220,7 @@ bool wallet2::prepare_file_names(const std::string& file_path) return true; } //---------------------------------------------------------------------------------------------------- -bool wallet2::check_connection(bool *same_version) +bool wallet2::check_connection(uint32_t *version) { boost::lock_guard<boost::mutex> lock(m_daemon_rpc_mutex); @@ -2238,7 +2238,7 @@ bool wallet2::check_connection(bool *same_version) return false; } - if (same_version) + if (version) { epee::json_rpc::request<cryptonote::COMMAND_RPC_GET_VERSION::request> req_t = AUTO_VAL_INIT(req_t); epee::json_rpc::response<cryptonote::COMMAND_RPC_GET_VERSION::response, std::string> resp_t = AUTO_VAL_INIT(resp_t); @@ -2247,9 +2247,9 @@ bool wallet2::check_connection(bool *same_version) req_t.method = "get_version"; bool r = net_utils::invoke_http_json_remote_command2(m_daemon_address + "/json_rpc", req_t, resp_t, m_http_client); if (!r || resp_t.result.status != CORE_RPC_STATUS_OK) - *same_version = false; + *version = 0; else - *same_version = resp_t.result.version == CORE_RPC_VERSION; + *version = resp_t.result.version; } return true; @@ -2347,11 +2347,16 @@ void wallet2::load(const std::string& wallet_, const std::string& password) } //---------------------------------------------------------------------------------------------------- void wallet2::check_genesis(const crypto::hash& genesis_hash) const { - std::string what("Genesis block missmatch. You probably use wallet without testnet flag with blockchain from test network or vice versa"); + std::string what("Genesis block mismatch. You probably use wallet without testnet flag with blockchain from test network or vice versa"); THROW_WALLET_EXCEPTION_IF(genesis_hash != m_blockchain[0], error::wallet_internal_error, what); } //---------------------------------------------------------------------------------------------------- +std::string wallet2::path() const +{ + return m_wallet_file; +} +//---------------------------------------------------------------------------------------------------- void wallet2::store() { store_to("", ""); @@ -3850,12 +3855,12 @@ static size_t estimate_rct_tx_size(int n_inputs, int mixin, int n_outputs) return size; } -std::vector<size_t> wallet2::pick_prefered_rct_inputs(uint64_t needed_money) const +std::vector<size_t> wallet2::pick_preferred_rct_inputs(uint64_t needed_money) const { std::vector<size_t> picks; float current_output_relatdness = 1.0f; - LOG_PRINT_L2("pick_prefered_rct_inputs: needed_money " << print_money(needed_money)); + LOG_PRINT_L2("pick_preferred_rct_inputs: needed_money " << print_money(needed_money)); // try to find a rct input of enough size for (size_t i = 0; i < m_transfers.size(); ++i) @@ -4018,7 +4023,7 @@ 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_prefered_rct_inputs(needed_money + estimated_fee); + prefered_inputs = pick_preferred_rct_inputs(needed_money + estimated_fee); if (!prefered_inputs.empty()) { string s; @@ -4988,6 +4993,148 @@ std::string wallet2::decrypt_with_view_secret_key(const std::string &ciphertext, return decrypt(ciphertext, get_account().get_keys().m_view_secret_key, authenticated); } //---------------------------------------------------------------------------------------------------- +std::string wallet2::make_uri(const std::string &address, const std::string &payment_id, uint64_t amount, const std::string &tx_description, const std::string &recipient_name, std::string &error) +{ + cryptonote::account_public_address tmp_address; + bool has_payment_id; + crypto::hash8 new_payment_id; + if(!get_account_integrated_address_from_str(tmp_address, has_payment_id, new_payment_id, testnet(), address)) + { + error = std::string("wrong address: ") + address; + return std::string(); + } + + // we want only one payment id + if (has_payment_id && !payment_id.empty()) + { + error = "A single payment id is allowed"; + return std::string(); + } + + if (!payment_id.empty()) + { + crypto::hash pid32; + crypto::hash8 pid8; + if (!wallet2::parse_long_payment_id(payment_id, pid32) && !wallet2::parse_short_payment_id(payment_id, pid8)) + { + error = "Invalid payment id"; + return std::string(); + } + } + + std::string uri = "monero:" + address; + bool n_fields = 0; + + if (!payment_id.empty()) + { + uri += (n_fields++ ? "&" : "?") + std::string("tx_payment_id=") + payment_id; + } + + if (amount > 0) + { + // URI encoded amount is in decimal units, not atomic units + uri += (n_fields++ ? "&" : "?") + std::string("tx_amount=") + cryptonote::print_money(amount); + } + + if (!recipient_name.empty()) + { + uri += (n_fields++ ? "&" : "?") + std::string("recipient_name=") + epee::net_utils::conver_to_url_format(recipient_name); + } + + if (!tx_description.empty()) + { + uri += (n_fields++ ? "&" : "?") + std::string("tx_description=") + epee::net_utils::conver_to_url_format(tx_description); + } + + return uri; +} +//---------------------------------------------------------------------------------------------------- +bool wallet2::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) +{ + if (uri.substr(0, 7) != "monero:") + { + error = std::string("URI has wrong scheme (expected \"monero:\"): ") + uri; + return false; + } + + std::string remainder = uri.substr(7); + const char *ptr = strchr(remainder.c_str(), '?'); + address = ptr ? remainder.substr(0, ptr-remainder.c_str()) : remainder; + + cryptonote::account_public_address addr; + bool has_payment_id; + crypto::hash8 new_payment_id; + if(!get_account_integrated_address_from_str(addr, has_payment_id, new_payment_id, testnet(), address)) + { + error = std::string("URI has wrong address: ") + address; + return false; + } + if (!strchr(remainder.c_str(), '?')) + return true; + + std::vector<std::string> arguments; + std::string body = remainder.substr(address.size() + 1); + if (body.empty()) + return true; + boost::split(arguments, body, boost::is_any_of("&")); + std::set<std::string> have_arg; + for (const auto &arg: arguments) + { + std::vector<std::string> kv; + boost::split(kv, arg, boost::is_any_of("=")); + if (kv.size() != 2) + { + error = std::string("URI has wrong parameter: ") + arg; + return false; + } + if (have_arg.find(kv[0]) != have_arg.end()) + { + error = std::string("URI has more than one instance of " + kv[0]); + return false; + } + have_arg.insert(kv[0]); + + if (kv[0] == "tx_amount") + { + amount = 0; + if (!cryptonote::parse_amount(amount, kv[1])) + { + error = std::string("URI has invalid amount: ") + kv[1]; + return false; + } + } + else if (kv[0] == "tx_payment_id") + { + if (has_payment_id) + { + error = "Separate payment id given with an integrated address"; + return false; + } + crypto::hash hash; + crypto::hash8 hash8; + if (!wallet2::parse_long_payment_id(kv[1], hash) && !wallet2::parse_short_payment_id(kv[1], hash8)) + { + error = "Invalid payment id: " + kv[1]; + return false; + } + payment_id = kv[1]; + } + else if (kv[0] == "recipient_name") + { + recipient_name = epee::net_utils::convert_from_url_format(kv[1]); + } + else if (kv[0] == "tx_description") + { + tx_description = epee::net_utils::convert_from_url_format(kv[1]); + } + else + { + unknown_parameters.push_back(arg); + } + } + return true; +} +//---------------------------------------------------------------------------------------------------- void wallet2::generate_genesis(cryptonote::block& b) { if (m_testnet) { diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index b6d3250b2..dcb6367d1 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -332,6 +332,8 @@ namespace tools */ void store_to(const std::string &path, const std::string &password); + std::string path() const; + /*! * \brief verifies given password is correct for default wallet keys file */ @@ -408,7 +410,7 @@ namespace tools std::vector<wallet2::pending_tx> create_transactions_all(const cryptonote::account_public_address &address, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t> extra, bool trusted_daemon); std::vector<wallet2::pending_tx> create_transactions_from(const cryptonote::account_public_address &address, std::vector<size_t> unused_transfers_indices, std::vector<size_t> unused_dust_indices, const size_t fake_outs_count, const uint64_t unlock_time, uint32_t priority, const std::vector<uint8_t> extra, bool trusted_daemon); std::vector<pending_tx> create_unmixable_sweep_transactions(bool trusted_daemon); - bool check_connection(bool *same_version = NULL); + bool check_connection(uint32_t *version = NULL); void get_transfers(wallet2::transfer_container& incoming_transfers) const; void get_payments(const crypto::hash& payment_id, std::list<wallet2::payment_details>& payments, uint64_t min_height = 0) const; void get_payments(std::list<std::pair<crypto::hash,wallet2::payment_details>>& payments, uint64_t min_height, uint64_t max_height = (uint64_t)-1) const; @@ -547,6 +549,9 @@ namespace tools std::string decrypt(const std::string &ciphertext, const crypto::secret_key &skey, bool authenticated = true) const; std::string decrypt_with_view_secret_key(const std::string &ciphertext, bool authenticated = true) const; + std::string make_uri(const std::string &address, const std::string &payment_id, uint64_t amount, const std::string &tx_description, const std::string &recipient_name, std::string &error); + 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); + private: /*! * \brief Stores wallet information to wallet file. @@ -590,7 +595,7 @@ namespace tools 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_prefered_rct_inputs(uint64_t needed_money) const; + std::vector<size_t> pick_preferred_rct_inputs(uint64_t needed_money) const; void set_spent(size_t idx, uint64_t height); void set_unspent(size_t idx); template<typename entry> diff --git a/src/wallet/wallet2_api.h b/src/wallet/wallet2_api.h index e624ffa69..60907b436 100644 --- a/src/wallet/wallet2_api.h +++ b/src/wallet/wallet2_api.h @@ -195,6 +195,7 @@ struct Wallet virtual std::string errorString() const = 0; virtual bool setPassword(const std::string &password) = 0; virtual std::string address() const = 0; + virtual std::string path() const = 0; /*! * \brief integratedAddress - returns integrated address for current wallet address and given payment_id. @@ -486,6 +487,23 @@ struct WalletManager //! returns verbose error string regarding last error; virtual std::string errorString() const = 0; + //! set the daemon address (hostname and port) + virtual void setDaemonAddress(const std::string &address) = 0; + + //! returns whether the daemon can be reached, and its version number + virtual bool connected(uint32_t *version = NULL) const = 0; + + //! returns current blockchain height + virtual uint64_t blockchainHeight() const = 0; + + //! returns current blockchain target height + virtual uint64_t blockchainTargetHeight() const = 0; + + //! returns current network difficulty + virtual uint64_t networkDifficulty() const = 0; + + //! returns current mining hash rate (0 if not mining) + virtual double miningHashRate() const = 0; }; diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp index f1c3faa3e..5352b0b73 100644 --- a/src/wallet/wallet_rpc_server.cpp +++ b/src/wallet/wallet_rpc_server.cpp @@ -558,7 +558,7 @@ namespace tools if(!epee::string_tools::parse_hexstr_to_binbuff(req.payment_id, payment_id_blob)) { er.code = WALLET_RPC_ERROR_CODE_WRONG_PAYMENT_ID; - er.message = "Payment ID has invald format"; + er.message = "Payment ID has invalid format"; return false; } @@ -1075,6 +1075,33 @@ namespace tools return true; } //------------------------------------------------------------------------------------------------------------------------------ + bool wallet_rpc_server::on_make_uri(const wallet_rpc::COMMAND_RPC_MAKE_URI::request& req, wallet_rpc::COMMAND_RPC_MAKE_URI::response& res, epee::json_rpc::error& er) + { + std::string error; + std::string uri = m_wallet.make_uri(req.address, req.payment_id, req.amount, req.tx_description, req.recipient_name, error); + if (uri.empty()) + { + er.code = WALLET_RPC_ERROR_CODE_WRONG_URI; + er.message = std::string("Cannot make URI from supplied parameters: ") + error; + return false; + } + + res.uri = uri; + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ + bool wallet_rpc_server::on_parse_uri(const wallet_rpc::COMMAND_RPC_PARSE_URI::request& req, wallet_rpc::COMMAND_RPC_PARSE_URI::response& res, epee::json_rpc::error& er) + { + std::string error; + if (!m_wallet.parse_uri(req.uri, res.uri.address, res.uri.payment_id, res.uri.amount, res.uri.tx_description, res.uri.recipient_name, res.unknown_parameters, error)) + { + er.code = WALLET_RPC_ERROR_CODE_WRONG_URI; + er.message = "Error parsing URI: " + error; + return false; + } + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ } int main(int argc, char** argv) { diff --git a/src/wallet/wallet_rpc_server.h b/src/wallet/wallet_rpc_server.h index 4eceb1d55..7d6f94e56 100644 --- a/src/wallet/wallet_rpc_server.h +++ b/src/wallet/wallet_rpc_server.h @@ -80,6 +80,8 @@ namespace tools MAP_JON_RPC_WE("verify", on_verify, wallet_rpc::COMMAND_RPC_VERIFY) MAP_JON_RPC_WE("export_key_images", on_export_key_images, wallet_rpc::COMMAND_RPC_EXPORT_KEY_IMAGES) MAP_JON_RPC_WE("import_key_images", on_import_key_images, wallet_rpc::COMMAND_RPC_IMPORT_KEY_IMAGES) + MAP_JON_RPC_WE("make_uri", on_make_uri, wallet_rpc::COMMAND_RPC_MAKE_URI) + MAP_JON_RPC_WE("parse_uri", on_parse_uri, wallet_rpc::COMMAND_RPC_PARSE_URI) END_JSON_RPC_MAP() END_URI_MAP2() @@ -107,6 +109,8 @@ namespace tools bool on_verify(const wallet_rpc::COMMAND_RPC_VERIFY::request& req, wallet_rpc::COMMAND_RPC_VERIFY::response& res, epee::json_rpc::error& er); bool on_export_key_images(const wallet_rpc::COMMAND_RPC_EXPORT_KEY_IMAGES::request& req, wallet_rpc::COMMAND_RPC_EXPORT_KEY_IMAGES::response& res, epee::json_rpc::error& er); bool on_import_key_images(const wallet_rpc::COMMAND_RPC_IMPORT_KEY_IMAGES::request& req, wallet_rpc::COMMAND_RPC_IMPORT_KEY_IMAGES::response& res, epee::json_rpc::error& er); + bool on_make_uri(const wallet_rpc::COMMAND_RPC_MAKE_URI::request& req, wallet_rpc::COMMAND_RPC_MAKE_URI::response& res, epee::json_rpc::error& er); + bool on_parse_uri(const wallet_rpc::COMMAND_RPC_PARSE_URI::request& req, wallet_rpc::COMMAND_RPC_PARSE_URI::response& res, epee::json_rpc::error& er); bool handle_command_line(const boost::program_options::variables_map& vm); diff --git a/src/wallet/wallet_rpc_server_commands_defs.h b/src/wallet/wallet_rpc_server_commands_defs.h index 76de7bc9d..50b1613f9 100644 --- a/src/wallet/wallet_rpc_server_commands_defs.h +++ b/src/wallet/wallet_rpc_server_commands_defs.h @@ -703,5 +703,61 @@ namespace wallet_rpc }; }; + struct uri_spec + { + std::string address; + std::string payment_id; + uint64_t amount; + std::string tx_description; + std::string recipient_name; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(address); + KV_SERIALIZE(payment_id); + KV_SERIALIZE(amount); + KV_SERIALIZE(tx_description); + KV_SERIALIZE(recipient_name); + END_KV_SERIALIZE_MAP() + }; + + struct COMMAND_RPC_MAKE_URI + { + struct request: public uri_spec + { + }; + + struct response + { + std::string uri; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(uri) + END_KV_SERIALIZE_MAP() + }; + }; + + struct COMMAND_RPC_PARSE_URI + { + struct request + { + std::string uri; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(uri) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + uri_spec uri; + std::vector<std::string> unknown_parameters; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(uri); + KV_SERIALIZE(unknown_parameters); + END_KV_SERIALIZE_MAP() + }; + }; + } } diff --git a/src/wallet/wallet_rpc_server_error_codes.h b/src/wallet/wallet_rpc_server_error_codes.h index 4617a1449..38fbffcc2 100644 --- a/src/wallet/wallet_rpc_server_error_codes.h +++ b/src/wallet/wallet_rpc_server_error_codes.h @@ -41,3 +41,4 @@ #define WALLET_RPC_ERROR_CODE_WRONG_TXID -8 #define WALLET_RPC_ERROR_CODE_WRONG_SIGNATURE -9 #define WALLET_RPC_ERROR_CODE_WRONG_KEY_IMAGE -10 +#define WALLET_RPC_ERROR_CODE_WRONG_URI -11 |