Merge pull request #365

e63b854 blockchain_db: match tx addition semantics to original code (moneromooo-monero)
83bbea4 Add a is_key_image_spent daemon command and RPC call (moneromooo-monero)
35abef1 blockchain: remove dead code (moneromooo-monero)
This commit is contained in:
Riccardo Spagni 2015-08-11 14:19:41 +02:00
commit 01e81205e0
No known key found for this signature in database
GPG Key ID: 55432DF31CCD4FCD
13 changed files with 169 additions and 69 deletions

View File

@ -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<txin_to_key>(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<txin_to_key>(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<txin_to_key>(tx_input).k_image);
}
}
}
uint64_t BlockchainDB::add_block( const block& blk

View File

@ -81,6 +81,8 @@ m_is_blockchain_storing(false), m_enforce_dns_checkpoints(false), m_max_prepare_
template<class archive_t>
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<bool>
{
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__);

View File

@ -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<crypto::hash, std::unordered_map<crypto::key_image, std::vector<output_data_t>>> m_scan_table;
@ -233,8 +232,6 @@ namespace cryptonote
bool switch_to_alternative_blockchain(std::list<blocks_ext_by_hash::iterator>& 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);

View File

@ -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<crypto::key_image>& key_im, std::vector<bool> &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<crypto::key_image> ki;

View File

@ -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<crypto::key_image>& key_im, std::vector<bool> &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<crypto::signature>& sig);
bool is_tx_spendtime_unlocked(uint64_t unlock_time);
bool update_miner_block_template();

View File

@ -180,6 +180,26 @@ bool t_command_parser_executor::print_transaction(const std::vector<std::string>
return true;
}
bool t_command_parser_executor::is_key_image_spent(const std::vector<std::string>& args)
{
if (args.empty())
{
std::cout << "expected: is_key_image_spent <key_image>" << 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<std::string>& args)
{
if (!args.empty()) return false;

View File

@ -75,6 +75,8 @@ public:
bool print_transaction(const std::vector<std::string>& args);
bool is_key_image_spent(const std::vector<std::string>& args);
bool print_transaction_pool_long(const std::vector<std::string>& args);
bool print_transaction_pool_short(const std::vector<std::string>& args);

View File

@ -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 <transaction_hash>"
);
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 <key_image>"
);
m_command_lookup.set_handler(
"start_mining"
, std::bind(&t_command_parser_executor::start_mining, &m_parser, p::_1)

View File

@ -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;

View File

@ -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();

View File

@ -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<crypto::key_image> 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<const crypto::key_image*>(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();

View File

@ -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);

View File

@ -116,6 +116,31 @@ namespace cryptonote
};
};
//-----------------------------------------------
struct COMMAND_RPC_IS_KEY_IMAGE_SPENT
{
struct request
{
std::vector<std::string> key_images;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(key_images)
END_KV_SERIALIZE_MAP()
};
struct response
{
std::vector<bool> 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
{