danicoin/src/cryptonote_core/tx_pool.cpp

392 lines
17 KiB
C++
Raw Normal View History

2014-08-13 10:38:35 +00:00
// Copyright (c) 2012-2014, The CryptoNote developers, The Bytecoin developers
//
// This file is part of Bytecoin.
//
// Bytecoin is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Bytecoin is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
2014-03-03 22:07:58 +00:00
2014-04-02 16:00:17 +00:00
#include <algorithm>
2014-03-03 22:07:58 +00:00
#include <boost/filesystem.hpp>
#include <unordered_set>
2014-04-02 16:00:17 +00:00
#include <vector>
2014-03-03 22:07:58 +00:00
#include "tx_pool.h"
#include "cryptonote_format_utils.h"
#include "cryptonote_boost_serialization.h"
#include "cryptonote_config.h"
#include "blockchain_storage.h"
#include "common/boost_serialization_helper.h"
2014-04-02 16:00:17 +00:00
#include "common/int-util.h"
2014-03-03 22:07:58 +00:00
#include "misc_language.h"
#include "warnings.h"
#include "crypto/hash.h"
DISABLE_VS_WARNINGS(4244 4345 4503) //'boost::foreach_detail_::or_' : decorated name length exceeded, name was truncated
2014-06-28 23:39:24 +00:00
#define TRANSACTION_SIZE_LIMIT (((CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE * 125) / 100) - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE)
2014-06-25 17:21:42 +00:00
namespace cryptonote {
2014-03-03 22:07:58 +00:00
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
tx_memory_pool::tx_memory_pool(blockchain_storage& bchs): m_blockchain(bchs) {
2014-03-03 22:07:58 +00:00
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
bool tx_memory_pool::add_tx(const transaction &tx, /*const crypto::hash& tx_prefix_hash,*/ const crypto::hash &id, size_t blob_size, tx_verification_context& tvc, bool kept_by_block) {
if (!check_inputs_types_supported(tx)) {
2014-03-03 22:07:58 +00:00
tvc.m_verifivation_failed = true;
return false;
}
uint64_t inputs_amount = 0;
2014-06-25 17:21:42 +00:00
if (!get_inputs_money_amount(tx, inputs_amount)) {
2014-03-03 22:07:58 +00:00
tvc.m_verifivation_failed = true;
return false;
}
uint64_t outputs_amount = get_outs_money_amount(tx);
2014-06-25 17:21:42 +00:00
if (outputs_amount >= inputs_amount) {
LOG_PRINT_L0("transaction use more money then it has: use " << print_money(outputs_amount) << ", have " << print_money(inputs_amount));
tvc.m_verifivation_failed = true;
return false;
}
uint64_t fee = inputs_amount - outputs_amount;
if (!kept_by_block && fee < MINIMUM_FEE) {
LOG_ERROR("transaction fee is not enought: " << print_money(fee) << ", minumim fee: " << print_money(MINIMUM_FEE));
2014-03-03 22:07:58 +00:00
tvc.m_verifivation_failed = true;
return false;
}
2014-06-28 23:39:24 +00:00
if (!kept_by_block && blob_size >= TRANSACTION_SIZE_LIMIT) {
LOG_ERROR("transaction is too big: " << blob_size << " bytes, maximum size: " << TRANSACTION_SIZE_LIMIT);
tvc.m_verifivation_failed = true;
return false;
}
2014-03-03 22:07:58 +00:00
//check key images for transaction if it is not kept by block
2014-06-25 17:21:42 +00:00
if (!kept_by_block) {
if (have_tx_keyimges_as_spent(tx)) {
2014-03-03 22:07:58 +00:00
LOG_ERROR("Transaction with id= "<< id << " used already spent key images");
tvc.m_verifivation_failed = true;
return false;
}
}
crypto::hash max_used_block_id = null_hash;
uint64_t max_used_block_height = 0;
bool ch_inp_res = m_blockchain.check_tx_inputs(tx, max_used_block_height, max_used_block_id);
CRITICAL_REGION_LOCAL(m_transactions_lock);
2014-06-25 17:21:42 +00:00
if (!ch_inp_res) {
if (kept_by_block) {
2014-03-03 22:07:58 +00:00
//anyway add this transaction to pool, because it related to block
auto txd_p = m_transactions.insert(transactions_container::value_type(id, tx_details()));
CHECK_AND_ASSERT_MES(txd_p.second, false, "transaction already exists at inserting in memory pool");
txd_p.first->second.blob_size = blob_size;
txd_p.first->second.tx = tx;
txd_p.first->second.fee = inputs_amount - outputs_amount;
txd_p.first->second.max_used_block_id = null_hash;
txd_p.first->second.max_used_block_height = 0;
txd_p.first->second.kept_by_block = kept_by_block;
tvc.m_verifivation_impossible = true;
tvc.m_added_to_pool = true;
2014-06-25 17:21:42 +00:00
} else {
2014-03-03 22:07:58 +00:00
LOG_PRINT_L0("tx used wrong inputs, rejected");
tvc.m_verifivation_failed = true;
return false;
}
2014-06-25 17:21:42 +00:00
} else {
2014-03-03 22:07:58 +00:00
//update transactions container
auto txd_p = m_transactions.insert(transactions_container::value_type(id, tx_details()));
CHECK_AND_ASSERT_MES(txd_p.second, false, "intrnal error: transaction already exists at inserting in memorypool");
txd_p.first->second.blob_size = blob_size;
txd_p.first->second.tx = tx;
txd_p.first->second.kept_by_block = kept_by_block;
txd_p.first->second.fee = inputs_amount - outputs_amount;
txd_p.first->second.max_used_block_id = max_used_block_id;
txd_p.first->second.max_used_block_height = max_used_block_height;
txd_p.first->second.last_failed_height = 0;
txd_p.first->second.last_failed_id = null_hash;
tvc.m_added_to_pool = true;
2014-06-25 17:21:42 +00:00
if (txd_p.first->second.fee > 0) {
2014-03-03 22:07:58 +00:00
tvc.m_should_be_relayed = true;
2014-06-25 17:21:42 +00:00
}
2014-03-03 22:07:58 +00:00
}
tvc.m_verifivation_failed = true;
//update image_keys container, here should everything goes ok.
2014-06-25 17:21:42 +00:00
for (const auto& in : tx.vin) {
2014-03-03 22:07:58 +00:00
CHECKED_GET_SPECIFIC_VARIANT(in, const txin_to_key, txin, false);
std::unordered_set<crypto::hash>& kei_image_set = m_spent_key_images[txin.k_image];
CHECK_AND_ASSERT_MES(kept_by_block || kei_image_set.size() == 0, false, "internal error: keeped_by_block=" << kept_by_block
<< ", kei_image_set.size()=" << kei_image_set.size() << ENDL << "txin.k_image=" << txin.k_image << ENDL
<< "tx_id=" << id );
auto ins_res = kei_image_set.insert(id);
CHECK_AND_ASSERT_MES(ins_res.second, false, "internal error: try to insert duplicate iterator in key_image set");
}
tvc.m_verifivation_failed = false;
//succeed
return true;
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
bool tx_memory_pool::add_tx(const transaction &tx, tx_verification_context& tvc, bool keeped_by_block) {
2014-03-03 22:07:58 +00:00
crypto::hash h = null_hash;
size_t blob_size = 0;
get_transaction_hash(tx, h, blob_size);
return add_tx(tx, h, blob_size, tvc, keeped_by_block);
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
bool tx_memory_pool::remove_transaction_keyimages(const transaction& tx) {
2014-03-03 22:07:58 +00:00
CRITICAL_REGION_LOCAL(m_transactions_lock);
2014-06-25 17:21:42 +00:00
crypto::hash tx_id = get_transaction_hash(tx);
for (const txin_v& vi : tx.vin) {
2014-03-03 22:07:58 +00:00
CHECKED_GET_SPECIFIC_VARIANT(vi, const txin_to_key, txin, false);
auto it = m_spent_key_images.find(txin.k_image);
2014-06-25 17:21:42 +00:00
CHECK_AND_ASSERT_MES(it != m_spent_key_images.end(), false, "failed to find transaction input in key images. img=" << txin.k_image << std::endl
<< "transaction id = " << tx_id);
std::unordered_set<crypto::hash>& key_image_set = it->second;
CHECK_AND_ASSERT_MES(!key_image_set.empty(), false, "empty key_image set, img=" << txin.k_image << std::endl
<< "transaction id = " << tx_id);
2014-03-03 22:07:58 +00:00
2014-06-25 17:21:42 +00:00
auto it_in_set = key_image_set.find(tx_id);
CHECK_AND_ASSERT_MES(it_in_set != key_image_set.end(), false, "transaction id not found in key_image set, img=" << txin.k_image << std::endl
<< "transaction id = " << tx_id);
2014-03-03 22:07:58 +00:00
key_image_set.erase(it_in_set);
2014-06-25 17:21:42 +00:00
if (key_image_set.empty()) {
2014-03-03 22:07:58 +00:00
//it is now empty hash container for this key_image
m_spent_key_images.erase(it);
}
}
return true;
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
bool tx_memory_pool::take_tx(const crypto::hash &id, transaction &tx, size_t& blob_size, uint64_t& fee) {
2014-03-03 22:07:58 +00:00
CRITICAL_REGION_LOCAL(m_transactions_lock);
auto it = m_transactions.find(id);
2014-06-25 17:21:42 +00:00
if (it == m_transactions.end()) {
2014-03-03 22:07:58 +00:00
return false;
2014-06-25 17:21:42 +00:00
}
2014-03-03 22:07:58 +00:00
tx = it->second.tx;
blob_size = it->second.blob_size;
fee = it->second.fee;
remove_transaction_keyimages(it->second.tx);
m_transactions.erase(it);
return true;
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
size_t tx_memory_pool::get_transactions_count() const {
2014-03-03 22:07:58 +00:00
CRITICAL_REGION_LOCAL(m_transactions_lock);
return m_transactions.size();
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
void tx_memory_pool::get_transactions(std::list<transaction>& txs) const {
2014-03-03 22:07:58 +00:00
CRITICAL_REGION_LOCAL(m_transactions_lock);
2014-06-25 17:21:42 +00:00
for (const auto& tx_vt : m_transactions) {
2014-03-03 22:07:58 +00:00
txs.push_back(tx_vt.second.tx);
2014-06-25 17:21:42 +00:00
}
2014-03-03 22:07:58 +00:00
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
bool tx_memory_pool::on_blockchain_inc(uint64_t new_block_height, const crypto::hash& top_block_id) {
2014-03-03 22:07:58 +00:00
return true;
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
bool tx_memory_pool::on_blockchain_dec(uint64_t new_block_height, const crypto::hash& top_block_id) {
2014-03-03 22:07:58 +00:00
return true;
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
bool tx_memory_pool::have_tx(const crypto::hash &id) const {
2014-03-03 22:07:58 +00:00
CRITICAL_REGION_LOCAL(m_transactions_lock);
2014-06-25 17:21:42 +00:00
if (m_transactions.count(id)) {
2014-03-03 22:07:58 +00:00
return true;
2014-06-25 17:21:42 +00:00
}
2014-03-03 22:07:58 +00:00
return false;
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
bool tx_memory_pool::have_tx_keyimges_as_spent(const transaction& tx) const {
2014-03-03 22:07:58 +00:00
CRITICAL_REGION_LOCAL(m_transactions_lock);
2014-06-25 17:21:42 +00:00
for (const auto& in : tx.vin) {
2014-03-03 22:07:58 +00:00
CHECKED_GET_SPECIFIC_VARIANT(in, const txin_to_key, tokey_in, true);//should never fail
2014-06-25 17:21:42 +00:00
if (have_tx_keyimg_as_spent(tokey_in.k_image)) {
return true;
}
2014-03-03 22:07:58 +00:00
}
return false;
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
bool tx_memory_pool::have_tx_keyimg_as_spent(const crypto::key_image& key_im) const {
2014-03-03 22:07:58 +00:00
CRITICAL_REGION_LOCAL(m_transactions_lock);
return m_spent_key_images.end() != m_spent_key_images.find(key_im);
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
void tx_memory_pool::lock() const {
2014-03-03 22:07:58 +00:00
m_transactions_lock.lock();
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
void tx_memory_pool::unlock() const {
2014-03-03 22:07:58 +00:00
m_transactions_lock.unlock();
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
bool tx_memory_pool::is_transaction_ready_to_go(tx_details& txd) const {
2014-03-03 22:07:58 +00:00
//not the best implementation at this time, sorry :(
//check is ring_signature already checked ?
2014-06-25 17:21:42 +00:00
if (txd.max_used_block_id == null_hash) {
//not checked, lets try to check
if (txd.last_failed_id != null_hash && m_blockchain.get_current_blockchain_height() > txd.last_failed_height && txd.last_failed_id == m_blockchain.get_block_id_by_height(txd.last_failed_height)) {
2014-03-03 22:07:58 +00:00
return false;//we already sure that this tx is broken for this height
2014-06-25 17:21:42 +00:00
}
2014-03-03 22:07:58 +00:00
2014-06-25 17:21:42 +00:00
if (!m_blockchain.check_tx_inputs(txd.tx, txd.max_used_block_height, txd.max_used_block_id)) {
2014-03-03 22:07:58 +00:00
txd.last_failed_height = m_blockchain.get_current_blockchain_height()-1;
txd.last_failed_id = m_blockchain.get_block_id_by_height(txd.last_failed_height);
return false;
}
2014-06-25 17:21:42 +00:00
} else {
if (txd.max_used_block_height >= m_blockchain.get_current_blockchain_height()) {
2014-03-03 22:07:58 +00:00
return false;
2014-06-25 17:21:42 +00:00
}
if (m_blockchain.get_block_id_by_height(txd.max_used_block_height) != txd.max_used_block_id) {
2014-03-03 22:07:58 +00:00
//if we already failed on this height and id, skip actual ring signature check
2014-06-25 17:21:42 +00:00
if (txd.last_failed_id == m_blockchain.get_block_id_by_height(txd.last_failed_height)) {
2014-03-03 22:07:58 +00:00
return false;
2014-06-25 17:21:42 +00:00
}
2014-03-03 22:07:58 +00:00
//check ring signature again, it is possible (with very small chance) that this transaction become again valid
2014-06-25 17:21:42 +00:00
if (!m_blockchain.check_tx_inputs(txd.tx, txd.max_used_block_height, txd.max_used_block_id)) {
2014-03-03 22:07:58 +00:00
txd.last_failed_height = m_blockchain.get_current_blockchain_height()-1;
txd.last_failed_id = m_blockchain.get_block_id_by_height(txd.last_failed_height);
return false;
}
}
}
2014-06-25 17:21:42 +00:00
2014-03-03 22:07:58 +00:00
//if we here, transaction seems valid, but, anyway, check for key_images collisions with blockchain, just to be sure
2014-06-25 17:21:42 +00:00
if (m_blockchain.have_tx_keyimges_as_spent(txd.tx)) {
2014-03-03 22:07:58 +00:00
return false;
2014-06-25 17:21:42 +00:00
}
2014-03-03 22:07:58 +00:00
//transaction is ok.
return true;
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
bool tx_memory_pool::have_key_images(const std::unordered_set<crypto::key_image>& k_images, const transaction& tx) {
for (size_t i = 0; i!= tx.vin.size(); i++) {
2014-03-03 22:07:58 +00:00
CHECKED_GET_SPECIFIC_VARIANT(tx.vin[i], const txin_to_key, itk, false);
2014-06-25 17:21:42 +00:00
if (k_images.count(itk.k_image)) {
2014-03-03 22:07:58 +00:00
return true;
2014-06-25 17:21:42 +00:00
}
2014-03-03 22:07:58 +00:00
}
return false;
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
bool tx_memory_pool::append_key_images(std::unordered_set<crypto::key_image>& k_images, const transaction& tx) {
for (size_t i = 0; i!= tx.vin.size(); i++) {
2014-03-03 22:07:58 +00:00
CHECKED_GET_SPECIFIC_VARIANT(tx.vin[i], const txin_to_key, itk, false);
auto i_res = k_images.insert(itk.k_image);
CHECK_AND_ASSERT_MES(i_res.second, false, "internal error: key images pool cache - inserted duplicate image in set: " << itk.k_image);
}
return true;
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
std::string tx_memory_pool::print_pool(bool short_format) const {
2014-03-03 22:07:58 +00:00
std::stringstream ss;
CRITICAL_REGION_LOCAL(m_transactions_lock);
2014-06-25 17:21:42 +00:00
for (const transactions_container::value_type& txe : m_transactions) {
const tx_details& txd = txe.second;
ss << "id: " << txe.first << std::endl;
if (!short_format) {
ss << obj_to_json_str(*const_cast<transaction*>(&txd.tx)) << std::endl;
2014-03-03 22:07:58 +00:00
}
2014-06-25 17:21:42 +00:00
ss << "blob_size: " << txd.blob_size << std::endl
<< "fee: " << print_money(txd.fee) << std::endl
<< "kept_by_block: " << (txd.kept_by_block ? 'T' : 'F') << std::endl
<< "max_used_block_height: " << txd.max_used_block_height << std::endl
<< "max_used_block_id: " << txd.max_used_block_id << std::endl
<< "last_failed_height: " << txd.last_failed_height << std::endl
<< "last_failed_id: " << txd.last_failed_id << std::endl;
2014-03-03 22:07:58 +00:00
}
2014-06-25 17:21:42 +00:00
2014-03-03 22:07:58 +00:00
return ss.str();
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
bool tx_memory_pool::fill_block_template(block& bl, size_t median_size, uint64_t already_generated_coins, size_t& total_size, uint64_t& fee) {
2014-03-03 22:07:58 +00:00
CRITICAL_REGION_LOCAL(m_transactions_lock);
2014-04-02 16:00:17 +00:00
total_size = 0;
2014-03-03 22:07:58 +00:00
fee = 0;
2014-06-25 17:21:42 +00:00
size_t max_total_size = (125 * median_size) / 100 - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE;
2014-04-02 16:00:17 +00:00
std::unordered_set<crypto::key_image> k_images;
2014-06-25 17:21:42 +00:00
for (transactions_container::value_type& tx : m_transactions) {
if (max_total_size < total_size + tx.second.blob_size) {
2014-03-03 22:07:58 +00:00
continue;
2014-06-25 17:21:42 +00:00
}
2014-03-03 22:07:58 +00:00
2014-06-25 17:21:42 +00:00
if (!is_transaction_ready_to_go(tx.second) || have_key_images(k_images, tx.second.tx)) {
2014-04-19 20:53:40 +00:00
continue;
2014-06-25 17:21:42 +00:00
}
2014-04-02 16:00:17 +00:00
2014-04-19 20:53:40 +00:00
bl.tx_hashes.push_back(tx.first);
total_size += tx.second.blob_size;
fee += tx.second.fee;
append_key_images(k_images, tx.second.tx);
2014-03-03 22:07:58 +00:00
}
return true;
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
bool tx_memory_pool::init(const std::string& config_folder) {
CRITICAL_REGION_LOCAL(m_transactions_lock);
2014-03-03 22:07:58 +00:00
m_config_folder = config_folder;
std::string state_file_path = config_folder + "/" + CRYPTONOTE_POOLDATA_FILENAME;
boost::system::error_code ec;
2014-06-25 17:21:42 +00:00
if (!boost::filesystem::exists(state_file_path, ec)) {
2014-03-03 22:07:58 +00:00
return true;
2014-06-25 17:21:42 +00:00
}
2014-03-03 22:07:58 +00:00
bool res = tools::unserialize_obj_from_file(*this, state_file_path);
2014-06-25 17:21:42 +00:00
if (!res) {
LOG_ERROR("Failed to load memory pool from file " << state_file_path);
m_transactions.clear();
m_spent_key_images.clear();
2014-03-03 22:07:58 +00:00
}
2014-06-28 23:39:24 +00:00
for (auto it = m_transactions.begin(); it != m_transactions.end(); ) {
auto it2 = it++;
if (it2->second.blob_size >= TRANSACTION_SIZE_LIMIT) {
LOG_PRINT_L0("Transaction " << get_transaction_hash(it2->second.tx) << " is too big (" << it2->second.blob_size << " bytes), removing it from pool");
remove_transaction_keyimages(it2->second.tx);
m_transactions.erase(it2);
}
}
2014-06-25 17:21:42 +00:00
// Ignore deserialization error
return true;
2014-03-03 22:07:58 +00:00
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
bool tx_memory_pool::deinit() {
if (!tools::create_directories_if_necessary(m_config_folder)) {
2014-03-03 22:07:58 +00:00
LOG_PRINT_L0("Failed to create data directory: " << m_config_folder);
return false;
}
std::string state_file_path = m_config_folder + "/" + CRYPTONOTE_POOLDATA_FILENAME;
bool res = tools::serialize_obj_to_file(*this, state_file_path);
2014-06-25 17:21:42 +00:00
if (!res) {
2014-03-03 22:07:58 +00:00
LOG_PRINT_L0("Failed to serialize memory pool to file " << state_file_path);
}
return true;
}
}