From 35abef1b92bd475f1b91bb503b9831deddbe54f1 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Tue, 11 Aug 2015 10:48:51 +0100 Subject: [PATCH 1/3] blockchain: remove dead code --- src/cryptonote_core/blockchain.cpp | 62 +++--------------------------- src/cryptonote_core/blockchain.h | 3 -- 2 files changed, 6 insertions(+), 59 deletions(-) diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 648b77b2f..efac2d3d9 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -81,6 +81,8 @@ m_is_blockchain_storing(false), m_enforce_dns_checkpoints(false), m_max_prepare_ template void Blockchain::serialize(archive_t & ar, const unsigned int version) { + key_images_container dummy_key_images_container; + LOG_PRINT_L3("Blockchain::" << __func__); if(version < 11) return; @@ -88,7 +90,7 @@ void Blockchain::serialize(archive_t & ar, const unsigned int version) ar & m_blocks; ar & m_blocks_index; ar & m_transactions; - ar & m_spent_keys; + ar & dummy_key_images_container; ar & m_alternative_chains; ar & m_outputs; ar & m_invalid_blocks; @@ -96,7 +98,7 @@ void Blockchain::serialize(archive_t & ar, const unsigned int version) /*serialization bug workaround*/ if(version > 11) { - uint64_t total_check_count = m_db->height() + m_blocks_index.size() + m_transactions.size() + m_spent_keys.size() + m_alternative_chains.size() + m_outputs.size() + m_invalid_blocks.size() + m_current_block_cumul_sz_limit; + uint64_t total_check_count = m_db->height() + m_blocks_index.size() + m_transactions.size() + dummy_key_images_container.size() + m_alternative_chains.size() + m_outputs.size() + m_invalid_blocks.size() + m_current_block_cumul_sz_limit; if(archive_t::is_saving::value) { ar & total_check_count; @@ -109,14 +111,14 @@ void Blockchain::serialize(archive_t & ar, const unsigned int version) { LOG_ERROR("Blockchain storage data corruption detected. total_count loaded from file = " << total_check_count_loaded << ", expected = " << total_check_count); - LOG_PRINT_L0("Blockchain storage:" << std::endl << "m_blocks: " << m_db->height() << std::endl << "m_blocks_index: " << m_blocks_index.size() << std::endl << "m_transactions: " << m_transactions.size() << std::endl << "m_spent_keys: " << m_spent_keys.size() << std::endl << "m_alternative_chains: " << m_alternative_chains.size() << std::endl << "m_outputs: " << m_outputs.size() << std::endl << "m_invalid_blocks: " << m_invalid_blocks.size() << std::endl << "m_current_block_cumul_sz_limit: " << m_current_block_cumul_sz_limit); + LOG_PRINT_L0("Blockchain storage:" << std::endl << "m_blocks: " << m_db->height() << std::endl << "m_blocks_index: " << m_blocks_index.size() << std::endl << "m_transactions: " << m_transactions.size() << std::endl << "dummy_key_images_container: " << dummy_key_images_container.size() << std::endl << "m_alternative_chains: " << m_alternative_chains.size() << std::endl << "m_outputs: " << m_outputs.size() << std::endl << "m_invalid_blocks: " << m_invalid_blocks.size() << std::endl << "m_current_block_cumul_sz_limit: " << m_current_block_cumul_sz_limit); throw std::runtime_error("Blockchain data corruption"); } } } - LOG_PRINT_L3("Blockchain storage:" << std::endl << "m_blocks: " << m_db->height() << std::endl << "m_blocks_index: " << m_blocks_index.size() << std::endl << "m_transactions: " << m_transactions.size() << std::endl << "m_spent_keys: " << m_spent_keys.size() << std::endl << "m_alternative_chains: " << m_alternative_chains.size() << std::endl << "m_outputs: " << m_outputs.size() << std::endl << "m_invalid_blocks: " << m_invalid_blocks.size() << std::endl << "m_current_block_cumul_sz_limit: " << m_current_block_cumul_sz_limit); + LOG_PRINT_L3("Blockchain storage:" << std::endl << "m_blocks: " << m_db->height() << std::endl << "m_blocks_index: " << m_blocks_index.size() << std::endl << "m_transactions: " << m_transactions.size() << std::endl << "dummy_key_images_container: " << dummy_key_images_container.size() << std::endl << "m_alternative_chains: " << m_alternative_chains.size() << std::endl << "m_outputs: " << m_outputs.size() << std::endl << "m_invalid_blocks: " << m_invalid_blocks.size() << std::endl << "m_current_block_cumul_sz_limit: " << m_current_block_cumul_sz_limit); } //------------------------------------------------------------------ bool Blockchain::have_tx(const crypto::hash &id) const @@ -470,7 +472,6 @@ bool Blockchain::reset_and_set_genesis_block(const block& b) LOG_PRINT_L3("Blockchain::" << __func__); CRITICAL_REGION_LOCAL(m_blockchain_lock); m_transactions.clear(); - m_spent_keys.clear(); m_blocks.clear(); m_blocks_index.clear(); m_alternative_chains.clear(); @@ -482,57 +483,6 @@ bool Blockchain::reset_and_set_genesis_block(const block& b) return bvc.m_added_to_main_chain && !bvc.m_verifivation_failed; } //------------------------------------------------------------------ -//TODO: move to BlockchainDB subclass -bool Blockchain::purge_transaction_keyimages_from_blockchain(const transaction& tx, bool strict_check) -{ - LOG_PRINT_L3("Blockchain::" << __func__); - CRITICAL_REGION_LOCAL(m_blockchain_lock); - struct purge_transaction_visitor: public boost::static_visitor - { - key_images_container& m_spent_keys; - bool m_strict_check; - purge_transaction_visitor(key_images_container& spent_keys, bool strict_check) : - m_spent_keys(spent_keys), m_strict_check(strict_check) - { - } - - bool operator()(const txin_to_key& inp) const - { - //const crypto::key_image& ki = inp.k_image; - auto r = m_spent_keys.find(inp.k_image); - if(r != m_spent_keys.end()) - { - m_spent_keys.erase(r); - } - else - { - CHECK_AND_ASSERT_MES(!m_strict_check, false, "purge_block_data_from_blockchain: key image in transaction not found"); - } - return true; - } - bool operator()(const txin_gen& inp) const - { - return true; - } - bool operator()(const txin_to_script& tx) const - { - return false; - } - - bool operator()(const txin_to_scripthash& tx) const - { - return false; - } - }; - - BOOST_FOREACH(const txin_v& in, tx.vin) - { - bool r = boost::apply_visitor(purge_transaction_visitor(m_spent_keys, strict_check), in); - CHECK_AND_ASSERT_MES(!strict_check || r, false, "failed to process purge_transaction_visitor"); - } - return true; -} -//------------------------------------------------------------------ crypto::hash Blockchain::get_tail_id(uint64_t& height) const { LOG_PRINT_L3("Blockchain::" << __func__); diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h index 1c6f58dbf..57763f6ca 100644 --- a/src/cryptonote_core/blockchain.h +++ b/src/cryptonote_core/blockchain.h @@ -185,7 +185,6 @@ namespace cryptonote blocks_container m_blocks; // height -> block_extended_info blocks_by_id_index m_blocks_index; // crypto::hash -> height transactions_container m_transactions; - key_images_container m_spent_keys; size_t m_current_block_cumul_sz_limit; std::unordered_map>> m_scan_table; @@ -233,8 +232,6 @@ namespace cryptonote bool switch_to_alternative_blockchain(std::list& alt_chain, bool discard_disconnected_chain); block pop_block_from_blockchain(); - bool purge_transaction_from_blockchain(const crypto::hash& tx_id); - bool purge_transaction_keyimages_from_blockchain(const transaction& tx, bool strict_check); bool handle_block_to_main_chain(const block& bl, block_verification_context& bvc); bool handle_block_to_main_chain(const block& bl, const crypto::hash& id, block_verification_context& bvc); From 83bbea4c7f47d05933ec1b5baca2e9724561e49c Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Tue, 11 Aug 2015 10:49:15 +0100 Subject: [PATCH 2/3] Add a is_key_image_spent daemon command and RPC call --- src/cryptonote_core/cryptonote_core.cpp | 15 +++++++++++ src/cryptonote_core/cryptonote_core.h | 5 ++-- src/daemon/command_parser_executor.cpp | 20 ++++++++++++++ src/daemon/command_parser_executor.h | 2 ++ src/daemon/command_server.cpp | 5 ++++ src/daemon/rpc_command_executor.cpp | 36 +++++++++++++++++++++++++ src/daemon/rpc_command_executor.h | 2 ++ src/rpc/core_rpc_server.cpp | 29 ++++++++++++++++++++ src/rpc/core_rpc_server.h | 2 ++ src/rpc/core_rpc_server_commands_defs.h | 25 +++++++++++++++++ 10 files changed, 139 insertions(+), 2 deletions(-) diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp index 31e46b534..4d90eec1e 100644 --- a/src/cryptonote_core/cryptonote_core.cpp +++ b/src/cryptonote_core/cryptonote_core.cpp @@ -515,6 +515,21 @@ namespace cryptonote return true; } //----------------------------------------------------------------------------------------------- + bool core::is_key_image_spent(const crypto::key_image &key_image) + { + return m_blockchain_storage.have_tx_keyimg_as_spent(key_image); + } + //----------------------------------------------------------------------------------------------- + bool core::are_key_images_spent(const std::vector& key_im, std::vector &spent) + { + spent.clear(); + BOOST_FOREACH(auto& ki, key_im) + { + spent.push_back(m_blockchain_storage.have_tx_keyimg_as_spent(ki)); + } + return true; + } + //----------------------------------------------------------------------------------------------- bool core::check_tx_inputs_keyimages_diff(const transaction& tx) { std::unordered_set ki; diff --git a/src/cryptonote_core/cryptonote_core.h b/src/cryptonote_core/cryptonote_core.h index de953da07..d700b3b47 100644 --- a/src/cryptonote_core/cryptonote_core.h +++ b/src/cryptonote_core/cryptonote_core.h @@ -145,6 +145,9 @@ namespace cryptonote void stop(); + bool is_key_image_spent(const crypto::key_image& key_im); + bool are_key_images_spent(const std::vector& key_im, std::vector &spent); + private: bool add_new_tx(const transaction& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prefix_hash, size_t blob_size, tx_verification_context& tvc, bool keeped_by_block); bool add_new_tx(const transaction& tx, tx_verification_context& tvc, bool keeped_by_block); @@ -157,8 +160,6 @@ namespace cryptonote bool check_tx_semantic(const transaction& tx, bool keeped_by_block); //check if tx already in memory pool or in main blockchain - bool is_key_image_spent(const crypto::key_image& key_im); - bool check_tx_ring_signature(const txin_to_key& tx, const crypto::hash& tx_prefix_hash, const std::vector& sig); bool is_tx_spendtime_unlocked(uint64_t unlock_time); bool update_miner_block_template(); diff --git a/src/daemon/command_parser_executor.cpp b/src/daemon/command_parser_executor.cpp index 58cb475ab..e6666c443 100644 --- a/src/daemon/command_parser_executor.cpp +++ b/src/daemon/command_parser_executor.cpp @@ -180,6 +180,26 @@ bool t_command_parser_executor::print_transaction(const std::vector return true; } +bool t_command_parser_executor::is_key_image_spent(const std::vector& args) +{ + if (args.empty()) + { + std::cout << "expected: is_key_image_spent " << std::endl; + return true; + } + + const std::string& str = args.front(); + crypto::key_image ki; + crypto::hash hash; + if (parse_hash256(str, hash)) + { + memcpy(&ki, &hash, sizeof(ki)); + m_executor.is_key_image_spent(ki); + } + + return true; +} + bool t_command_parser_executor::print_transaction_pool_long(const std::vector& args) { if (!args.empty()) return false; diff --git a/src/daemon/command_parser_executor.h b/src/daemon/command_parser_executor.h index d1e299877..ddc207cfe 100644 --- a/src/daemon/command_parser_executor.h +++ b/src/daemon/command_parser_executor.h @@ -75,6 +75,8 @@ public: bool print_transaction(const std::vector& args); + bool is_key_image_spent(const std::vector& args); + bool print_transaction_pool_long(const std::vector& args); bool print_transaction_pool_short(const std::vector& args); diff --git a/src/daemon/command_server.cpp b/src/daemon/command_server.cpp index 65bceff75..047b52c3e 100644 --- a/src/daemon/command_server.cpp +++ b/src/daemon/command_server.cpp @@ -84,6 +84,11 @@ t_command_server::t_command_server( , std::bind(&t_command_parser_executor::print_transaction, &m_parser, p::_1) , "Print transaction, print_tx " ); + m_command_lookup.set_handler( + "is_key_image_spent" + , std::bind(&t_command_parser_executor::is_key_image_spent, &m_parser, p::_1) + , "Prints whether a given key image is in the spent key images set, is_key_image_spent " + ); m_command_lookup.set_handler( "start_mining" , std::bind(&t_command_parser_executor::start_mining, &m_parser, p::_1) diff --git a/src/daemon/rpc_command_executor.cpp b/src/daemon/rpc_command_executor.cpp index 2f3a6b4d5..62f254c76 100644 --- a/src/daemon/rpc_command_executor.cpp +++ b/src/daemon/rpc_command_executor.cpp @@ -520,6 +520,42 @@ bool t_rpc_command_executor::print_transaction(crypto::hash transaction_hash) { return true; } +bool t_rpc_command_executor::is_key_image_spent(const crypto::key_image &ki) { + cryptonote::COMMAND_RPC_IS_KEY_IMAGE_SPENT::request req; + cryptonote::COMMAND_RPC_IS_KEY_IMAGE_SPENT::response res; + + std::string fail_message = "Problem checkking key image"; + + req.key_images.push_back(epee::string_tools::pod_to_hex(ki)); + if (m_is_rpc) + { + if (!m_rpc_client->rpc_request(req, res, "/is_key_image_spent", fail_message.c_str())) + { + return true; + } + } + else + { + if (!m_rpc_server->on_is_key_image_spent(req, res)) + { + tools::fail_msg_writer() << fail_message.c_str(); + return true; + } + } + + if (1 == res.spent_status.size()) + { + // first as hex + tools::success_msg_writer() << ki << ": " << (res.spent_status.front() ? "spent" : "unspent"); + } + else + { + tools::fail_msg_writer() << "key image status could not be determined" << std::endl; + } + + return true; +} + bool t_rpc_command_executor::print_transaction_pool_long() { cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::request req; cryptonote::COMMAND_RPC_GET_TRANSACTION_POOL::response res; diff --git a/src/daemon/rpc_command_executor.h b/src/daemon/rpc_command_executor.h index 5e332f3fc..b40a67bf8 100644 --- a/src/daemon/rpc_command_executor.h +++ b/src/daemon/rpc_command_executor.h @@ -87,6 +87,8 @@ public: bool print_transaction(crypto::hash transaction_hash); + bool is_key_image_spent(const crypto::key_image &ki); + bool print_transaction_pool_long(); bool print_transaction_pool_short(); diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp index 24c7d242f..0c8384306 100644 --- a/src/rpc/core_rpc_server.cpp +++ b/src/rpc/core_rpc_server.cpp @@ -241,6 +241,35 @@ namespace cryptonote return true; } //------------------------------------------------------------------------------------------------------------------------------ + bool core_rpc_server::on_is_key_image_spent(const COMMAND_RPC_IS_KEY_IMAGE_SPENT::request& req, COMMAND_RPC_IS_KEY_IMAGE_SPENT::response& res) + { + CHECK_CORE_BUSY(); + std::vector key_images; + BOOST_FOREACH(const auto& ki_hex_str, req.key_images) + { + blobdata b; + if(!string_tools::parse_hexstr_to_binbuff(ki_hex_str, b)) + { + res.status = "Failed to parse hex representation of key image"; + return true; + } + if(b.size() != sizeof(crypto::key_image)) + { + res.status = "Failed, size of data mismatch"; + } + key_images.push_back(*reinterpret_cast(b.data())); + } + bool r = m_core.are_key_images_spent(key_images, res.spent_status); + if(!r) + { + res.status = "Failed"; + return true; + } + + res.status = CORE_RPC_STATUS_OK; + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ bool core_rpc_server::on_send_raw_tx(const COMMAND_RPC_SEND_RAW_TX::request& req, COMMAND_RPC_SEND_RAW_TX::response& res) { CHECK_CORE_READY(); diff --git a/src/rpc/core_rpc_server.h b/src/rpc/core_rpc_server.h index 6152dea03..3213e6b1c 100644 --- a/src/rpc/core_rpc_server.h +++ b/src/rpc/core_rpc_server.h @@ -77,6 +77,7 @@ namespace cryptonote MAP_URI_AUTO_BIN2("/get_o_indexes.bin", on_get_indexes, COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES) MAP_URI_AUTO_BIN2("/getrandom_outs.bin", on_get_random_outs, COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS) MAP_URI_AUTO_JON2("/gettransactions", on_get_transactions, COMMAND_RPC_GET_TRANSACTIONS) + 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("/start_mining", on_start_mining, COMMAND_RPC_START_MINING) MAP_URI_AUTO_JON2("/stop_mining", on_stop_mining, COMMAND_RPC_STOP_MINING) @@ -108,6 +109,7 @@ namespace cryptonote 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_transactions(const COMMAND_RPC_GET_TRANSACTIONS::request& req, COMMAND_RPC_GET_TRANSACTIONS::response& res); + bool on_is_key_image_spent(const COMMAND_RPC_IS_KEY_IMAGE_SPENT::request& req, COMMAND_RPC_IS_KEY_IMAGE_SPENT::response& res); bool on_get_indexes(const COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::request& req, COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES::response& res); bool on_send_raw_tx(const COMMAND_RPC_SEND_RAW_TX::request& req, COMMAND_RPC_SEND_RAW_TX::response& res); bool on_start_mining(const COMMAND_RPC_START_MINING::request& req, COMMAND_RPC_START_MINING::response& res); diff --git a/src/rpc/core_rpc_server_commands_defs.h b/src/rpc/core_rpc_server_commands_defs.h index b6a2edd0b..beb7b81e6 100644 --- a/src/rpc/core_rpc_server_commands_defs.h +++ b/src/rpc/core_rpc_server_commands_defs.h @@ -116,6 +116,31 @@ namespace cryptonote }; }; + //----------------------------------------------- + struct COMMAND_RPC_IS_KEY_IMAGE_SPENT + { + struct request + { + std::vector key_images; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(key_images) + END_KV_SERIALIZE_MAP() + }; + + + struct response + { + std::vector spent_status; + std::string status; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(spent_status) + KV_SERIALIZE(status) + END_KV_SERIALIZE_MAP() + }; + }; + //----------------------------------------------- struct COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES { From e63b85496790b458ac8b49d9d26c9de75b84859c Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Tue, 11 Aug 2015 11:09:09 +0100 Subject: [PATCH 3/3] blockchain_db: match tx addition semantics to original code The original code removed key images from a tx from the blockchain when an non to-key nor gen input was found in that tx. Additionally, the remainder of the tx data was added to the blockchain only after the double spend check passed. --- src/blockchain_db/blockchain_db.cpp | 32 +++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/blockchain_db/blockchain_db.cpp b/src/blockchain_db/blockchain_db.cpp index 41fee5dc7..9d865d4de 100644 --- a/src/blockchain_db/blockchain_db.cpp +++ b/src/blockchain_db/blockchain_db.cpp @@ -56,6 +56,30 @@ void BlockchainDB::add_transaction(const crypto::hash& blk_hash, const transacti tx_hash = *tx_hash_ptr; } + for (const txin_v& tx_input : tx.vin) + { + if (tx_input.type() == typeid(txin_to_key)) + { + add_spent_key(boost::get(tx_input).k_image); + } + else if (tx_input.type() == typeid(txin_gen)) + { + /* nothing to do here */ + } + else + { + LOG_PRINT_L1("Unsupported input type, removing key images and aborting transaction addition"); + for (const txin_v& tx_input : tx.vin) + { + if (tx_input.type() == typeid(txin_to_key)) + { + remove_spent_key(boost::get(tx_input).k_image); + } + } + return; + } + } + add_transaction_data(blk_hash, tx, tx_hash); // iterate tx.vout using indices instead of C++11 foreach syntax because @@ -64,14 +88,6 @@ void BlockchainDB::add_transaction(const crypto::hash& blk_hash, const transacti { add_output(tx_hash, tx.vout[i], i, tx.unlock_time); } - - for (const txin_v& tx_input : tx.vin) - { - if (tx_input.type() == typeid(txin_to_key)) - { - add_spent_key(boost::get(tx_input).k_image); - } - } } uint64_t BlockchainDB::add_block( const block& blk