danicoin/src/CryptoNoteCore/TransactionPool.cpp

634 lines
22 KiB
C++
Raw Normal View History

2015-05-27 12:08:46 +00:00
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
2014-08-13 10:38:35 +00:00
//
// 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
2015-07-30 15:22:07 +00:00
#include "TransactionPool.h"
2014-08-13 10:51:37 +00:00
2014-04-02 16:00:17 +00:00
#include <algorithm>
2014-08-13 10:51:37 +00:00
#include <ctime>
2014-04-02 16:00:17 +00:00
#include <vector>
2014-08-13 10:51:37 +00:00
#include <unordered_set>
2014-03-03 22:07:58 +00:00
2014-08-13 10:51:37 +00:00
#include <boost/filesystem.hpp>
2015-05-27 12:08:46 +00:00
#include "Common/int-util.h"
2015-07-30 15:22:07 +00:00
#include "Common/Util.h"
2014-03-03 22:07:58 +00:00
#include "crypto/hash.h"
2015-07-30 15:22:07 +00:00
#include "Serialization/SerializationTools.h"
#include "Serialization/BinarySerializationTools.h"
#include "CryptoNoteFormatUtils.h"
#include "CryptoNoteTools.h"
#include "CryptoNoteConfig.h"
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
using namespace Logging;
2014-03-03 22:07:58 +00:00
2015-07-15 12:23:00 +00:00
#undef ERROR
2015-05-27 12:08:46 +00:00
namespace CryptoNote {
2014-08-13 10:51:37 +00:00
2014-03-03 22:07:58 +00:00
//---------------------------------------------------------------------------------
2014-08-13 10:51:37 +00:00
// BlockTemplate
//---------------------------------------------------------------------------------
class BlockTemplate {
public:
2015-07-30 15:22:07 +00:00
bool addTransaction(const Crypto::Hash& txid, const Transaction& tx) {
2014-08-13 10:51:37 +00:00
if (!canAdd(tx))
return false;
2015-07-30 15:22:07 +00:00
for (const auto& in : tx.inputs) {
if (in.type() == typeid(KeyInput)) {
auto r = m_keyImages.insert(boost::get<KeyInput>(in).keyImage);
2014-08-13 10:51:37 +00:00
(void)r; //just to make compiler to shut up
assert(r.second);
2015-07-30 15:22:07 +00:00
} else if (in.type() == typeid(MultisignatureInput)) {
const auto& msig = boost::get<MultisignatureInput>(in);
2014-08-13 10:51:37 +00:00
auto r = m_usedOutputs.insert(std::make_pair(msig.amount, msig.outputIndex));
(void)r; //just to make compiler to shut up
assert(r.second);
}
}
m_txHashes.push_back(txid);
return true;
}
2015-07-30 15:22:07 +00:00
const std::vector<Crypto::Hash>& getTransactions() const {
2014-08-13 10:51:37 +00:00
return m_txHashes;
}
private:
bool canAdd(const Transaction& tx) {
2015-07-30 15:22:07 +00:00
for (const auto& in : tx.inputs) {
if (in.type() == typeid(KeyInput)) {
if (m_keyImages.count(boost::get<KeyInput>(in).keyImage)) {
2014-08-13 10:51:37 +00:00
return false;
}
2015-07-30 15:22:07 +00:00
} else if (in.type() == typeid(MultisignatureInput)) {
const auto& msig = boost::get<MultisignatureInput>(in);
2014-08-13 10:51:37 +00:00
if (m_usedOutputs.count(std::make_pair(msig.amount, msig.outputIndex))) {
return false;
}
}
}
return true;
}
2015-07-30 15:22:07 +00:00
std::unordered_set<Crypto::KeyImage> m_keyImages;
2014-08-13 10:51:37 +00:00
std::set<std::pair<uint64_t, uint64_t>> m_usedOutputs;
2015-07-30 15:22:07 +00:00
std::vector<Crypto::Hash> m_txHashes;
2014-08-13 10:51:37 +00:00
};
using CryptoNote::BlockInfo;
//---------------------------------------------------------------------------------
2015-05-27 12:08:46 +00:00
tx_memory_pool::tx_memory_pool(
const CryptoNote::Currency& currency,
CryptoNote::ITransactionValidator& validator,
CryptoNote::ITimeProvider& timeProvider,
Logging::ILogger& log) :
2014-08-13 10:51:37 +00:00
m_currency(currency),
m_validator(validator),
m_timeProvider(timeProvider),
m_txCheckInterval(60, timeProvider),
2015-05-27 12:08:46 +00:00
m_fee_index(boost::get<1>(m_transactions)),
logger(log, "txpool") {
2014-03-03 22:07:58 +00:00
}
2014-08-13 10:51:37 +00:00
2014-03-03 22:07:58 +00:00
//---------------------------------------------------------------------------------
2015-07-30 15:22:07 +00:00
bool tx_memory_pool::add_tx(const Transaction &tx, /*const Crypto::Hash& tx_prefix_hash,*/ const Crypto::Hash &id, size_t blobSize, tx_verification_context& tvc, bool keptByBlock) {
2014-06-25 17:21:42 +00:00
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) {
2015-05-27 12:08:46 +00:00
logger(INFO) << "transaction use more money then it has: use " << m_currency.formatAmount(outputs_amount) <<
", have " << m_currency.formatAmount(inputs_amount);
2014-06-25 17:21:42 +00:00
tvc.m_verifivation_failed = true;
return false;
}
2014-08-13 10:51:37 +00:00
const uint64_t fee = inputs_amount - outputs_amount;
if (!keptByBlock && fee < m_currency.minimumFee()) {
2015-07-15 12:23:00 +00:00
logger(INFO) << "transaction fee is not enough: " << m_currency.formatAmount(fee) <<
", minimum fee: " << m_currency.formatAmount(m_currency.minimumFee());
2014-06-28 23:39:24 +00:00
tvc.m_verifivation_failed = true;
2014-08-13 10:51:37 +00:00
tvc.m_tx_fee_too_small = true;
2014-06-28 23:39:24 +00:00
return false;
}
2014-03-03 22:07:58 +00:00
//check key images for transaction if it is not kept by block
2014-08-13 10:51:37 +00:00
if (!keptByBlock) {
2015-05-27 12:08:46 +00:00
std::lock_guard<std::recursive_mutex> lock(m_transactions_lock);
2014-08-13 10:51:37 +00:00
if (haveSpentInputs(tx)) {
2015-05-27 12:08:46 +00:00
logger(INFO) << "Transaction with id= " << id << " used already spent inputs";
2014-03-03 22:07:58 +00:00
tvc.m_verifivation_failed = true;
return false;
}
}
2014-08-13 10:51:37 +00:00
BlockInfo maxUsedBlock;
// check inputs
bool inputsValid = m_validator.checkTransactionInputs(tx, maxUsedBlock);
if (!inputsValid) {
if (!keptByBlock) {
2015-05-27 12:08:46 +00:00
logger(INFO) << "tx used wrong inputs, rejected";
2014-03-03 22:07:58 +00:00
tvc.m_verifivation_failed = true;
return false;
}
2014-08-13 10:51:37 +00:00
maxUsedBlock.clear();
tvc.m_verifivation_impossible = true;
2014-03-03 22:07:58 +00:00
}
2015-07-30 15:22:07 +00:00
if (!keptByBlock) {
bool sizeValid = m_validator.checkTransactionSize(blobSize);
if (!sizeValid) {
logger(INFO) << "tx too big, rejected";
tvc.m_verifivation_failed = true;
return false;
}
}
2015-05-27 12:08:46 +00:00
std::lock_guard<std::recursive_mutex> lock(m_transactions_lock);
2014-08-13 10:51:37 +00:00
2015-07-15 12:23:00 +00:00
if (!keptByBlock && m_recentlyDeletedTransactions.find(id) != m_recentlyDeletedTransactions.end()) {
logger(INFO) << "Trying to add recently deleted transaction. Ignore: " << id;
tvc.m_verifivation_failed = false;
tvc.m_should_be_relayed = false;
tvc.m_added_to_pool = false;
return true;
}
2014-08-13 10:51:37 +00:00
// add to pool
{
TransactionDetails txd;
txd.id = id;
txd.blobSize = blobSize;
txd.tx = tx;
txd.fee = fee;
txd.keptByBlock = keptByBlock;
txd.receiveTime = m_timeProvider.now();
txd.maxUsedBlock = maxUsedBlock;
txd.lastFailedBlock.clear();
auto txd_p = m_transactions.insert(std::move(txd));
2015-07-15 12:23:00 +00:00
if (!(txd_p.second)) {
logger(ERROR, BRIGHT_RED) << "transaction already exists at inserting in memory pool";
return false;
}
2015-07-30 15:22:07 +00:00
m_paymentIdIndex.add(txd.tx);
m_timestampIndex.add(txd.receiveTime, txd.id);
2014-03-03 22:07:58 +00:00
}
2014-08-13 10:51:37 +00:00
tvc.m_added_to_pool = true;
if (inputsValid && fee > 0)
tvc.m_should_be_relayed = true;
tvc.m_verifivation_failed = true;
if (!addTransactionInputs(id, tx, keptByBlock))
return false;
2014-03-03 22:07:58 +00:00
tvc.m_verifivation_failed = false;
//succeed
return true;
}
//---------------------------------------------------------------------------------
2014-08-13 10:51:37 +00:00
bool tx_memory_pool::add_tx(const Transaction &tx, tx_verification_context& tvc, bool keeped_by_block) {
2015-07-30 15:22:07 +00:00
Crypto::Hash h = NULL_HASH;
2014-08-13 10:51:37 +00:00
size_t blobSize = 0;
2015-07-30 15:22:07 +00:00
getObjectHash(tx, h, blobSize);
2014-08-13 10:51:37 +00:00
return add_tx(tx, h, blobSize, tvc, keeped_by_block);
2014-03-03 22:07:58 +00:00
}
//---------------------------------------------------------------------------------
2015-07-30 15:22:07 +00:00
bool tx_memory_pool::take_tx(const Crypto::Hash &id, Transaction &tx, size_t& blobSize, uint64_t& fee) {
2015-05-27 12:08:46 +00:00
std::lock_guard<std::recursive_mutex> lock(m_transactions_lock);
2014-03-03 22:07:58 +00:00
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
2014-08-13 10:51:37 +00:00
auto& txd = *it;
tx = txd.tx;
blobSize = txd.blobSize;
fee = txd.fee;
removeTransaction(it);
2014-03-03 22:07:58 +00:00
return true;
}
//---------------------------------------------------------------------------------
2014-06-25 17:21:42 +00:00
size_t tx_memory_pool::get_transactions_count() const {
2015-05-27 12:08:46 +00:00
std::lock_guard<std::recursive_mutex> lock(m_transactions_lock);
2014-03-03 22:07:58 +00:00
return m_transactions.size();
}
//---------------------------------------------------------------------------------
2014-08-13 10:51:37 +00:00
void tx_memory_pool::get_transactions(std::list<Transaction>& txs) const {
2015-05-27 12:08:46 +00:00
std::lock_guard<std::recursive_mutex> lock(m_transactions_lock);
2014-06-25 17:21:42 +00:00
for (const auto& tx_vt : m_transactions) {
2014-08-13 10:51:37 +00:00
txs.push_back(tx_vt.tx);
2014-06-25 17:21:42 +00:00
}
2014-03-03 22:07:58 +00:00
}
//---------------------------------------------------------------------------------
2015-07-30 15:22:07 +00:00
void tx_memory_pool::get_difference(const std::vector<Crypto::Hash>& known_tx_ids, std::vector<Crypto::Hash>& new_tx_ids, std::vector<Crypto::Hash>& deleted_tx_ids) const {
2015-05-27 12:08:46 +00:00
std::lock_guard<std::recursive_mutex> lock(m_transactions_lock);
2015-07-30 15:22:07 +00:00
std::unordered_set<Crypto::Hash> ready_tx_ids;
for (const auto& tx : m_transactions) {
TransactionCheckInfo checkInfo(tx);
if (is_transaction_ready_to_go(tx.tx, checkInfo)) {
ready_tx_ids.insert(tx.id);
}
}
2015-07-30 15:22:07 +00:00
std::unordered_set<Crypto::Hash> known_set(known_tx_ids.begin(), known_tx_ids.end());
for (auto it = ready_tx_ids.begin(), e = ready_tx_ids.end(); it != e;) {
auto known_it = known_set.find(*it);
if (known_it != known_set.end()) {
known_set.erase(known_it);
it = ready_tx_ids.erase(it);
}
else {
++it;
}
}
new_tx_ids.assign(ready_tx_ids.begin(), ready_tx_ids.end());
deleted_tx_ids.assign(known_set.begin(), known_set.end());
}
//---------------------------------------------------------------------------------
2015-07-30 15:22:07 +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;
}
//---------------------------------------------------------------------------------
2015-07-30 15:22:07 +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;
}
//---------------------------------------------------------------------------------
2015-07-30 15:22:07 +00:00
bool tx_memory_pool::have_tx(const Crypto::Hash &id) const {
2015-05-27 12:08:46 +00:00
std::lock_guard<std::recursive_mutex> lock(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
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-08-13 10:51:37 +00:00
bool tx_memory_pool::is_transaction_ready_to_go(const Transaction& tx, TransactionCheckInfo& txd) const {
2014-03-03 22:07:58 +00:00
2014-08-13 10:51:37 +00:00
if (!m_validator.checkTransactionInputs(tx, txd.maxUsedBlock, txd.lastFailedBlock))
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-08-13 10:51:37 +00:00
if (m_validator.haveSpentKeyImages(tx))
2014-03-03 22:07:58 +00:00
return false;
//transaction is ok.
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;
2015-05-27 12:08:46 +00:00
std::lock_guard<std::recursive_mutex> lock(m_transactions_lock);
2014-08-13 10:51:37 +00:00
for (const auto& txd : m_fee_index) {
ss << "id: " << txd.id << std::endl;
2015-07-30 15:22:07 +00:00
2014-06-25 17:21:42 +00:00
if (!short_format) {
2015-07-30 15:22:07 +00:00
ss << storeToJson(txd.tx) << std::endl;
2014-03-03 22:07:58 +00:00
}
2015-07-30 15:22:07 +00:00
2014-08-13 10:51:37 +00:00
ss << "blobSize: " << txd.blobSize << std::endl
<< "fee: " << m_currency.formatAmount(txd.fee) << std::endl
<< "keptByBlock: " << (txd.keptByBlock ? 'T' : 'F') << std::endl
<< "max_used_block_height: " << txd.maxUsedBlock.height << std::endl
<< "max_used_block_id: " << txd.maxUsedBlock.id << std::endl
<< "last_failed_height: " << txd.lastFailedBlock.height << std::endl
<< "last_failed_id: " << txd.lastFailedBlock.id << std::endl
<< "received: " << std::ctime(&txd.receiveTime) << 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-08-13 10:51:37 +00:00
bool tx_memory_pool::fill_block_template(Block& bl, size_t median_size, size_t maxCumulativeSize,
uint64_t already_generated_coins, size_t& total_size, uint64_t& fee) {
2015-05-27 12:08:46 +00:00
std::lock_guard<std::recursive_mutex> lock(m_transactions_lock);
2014-03-03 22:07:58 +00:00
2014-04-02 16:00:17 +00:00
total_size = 0;
2014-03-03 22:07:58 +00:00
fee = 0;
2014-08-13 10:51:37 +00:00
size_t max_total_size = (125 * median_size) / 100 - m_currency.minerTxBlobReservedSize();
max_total_size = std::min(max_total_size, maxCumulativeSize);
BlockTemplate blockTemplate;
for (auto i = m_fee_index.begin(); i != m_fee_index.end(); ++i) {
const auto& txd = *i;
2014-03-03 22:07:58 +00:00
2014-08-13 10:51:37 +00:00
if (max_total_size < total_size + txd.blobSize) {
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-08-13 10:51:37 +00:00
TransactionCheckInfo checkInfo(txd);
bool ready = is_transaction_ready_to_go(txd.tx, checkInfo);
// update item state
m_fee_index.modify(i, [&checkInfo](TransactionCheckInfo& item) {
item = checkInfo;
});
if (ready && blockTemplate.addTransaction(txd.id, txd.tx)) {
total_size += txd.blobSize;
fee += txd.fee;
}
2014-03-03 22:07:58 +00:00
}
2015-07-30 15:22:07 +00:00
bl.transactionHashes = blockTemplate.getTransactions();
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) {
2015-05-27 12:08:46 +00:00
std::lock_guard<std::recursive_mutex> lock(m_transactions_lock);
2014-06-25 17:21:42 +00:00
2014-03-03 22:07:58 +00:00
m_config_folder = config_folder;
2014-08-13 10:51:37 +00:00
std::string state_file_path = config_folder + "/" + m_currency.txPoolFileName();
2014-03-03 22:07:58 +00:00
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
}
2015-07-30 15:22:07 +00:00
if (!loadFromBinaryFile(*this, state_file_path)) {
2015-05-27 12:08:46 +00:00
logger(ERROR) << "Failed to load memory pool from file " << state_file_path;
2014-06-25 17:21:42 +00:00
m_transactions.clear();
m_spent_key_images.clear();
2014-08-13 10:51:37 +00:00
m_spentOutputs.clear();
2015-07-30 15:22:07 +00:00
m_paymentIdIndex.clear();
m_timestampIndex.clear();
} else {
buildIndices();
2014-03-03 22:07:58 +00:00
}
2015-07-15 12:23:00 +00:00
removeExpiredTransactions();
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() {
2015-07-30 15:22:07 +00:00
if (!Tools::create_directories_if_necessary(m_config_folder)) {
2015-05-27 12:08:46 +00:00
logger(INFO) << "Failed to create data directory: " << m_config_folder;
2014-03-03 22:07:58 +00:00
return false;
}
2014-08-13 10:51:37 +00:00
std::string state_file_path = m_config_folder + "/" + m_currency.txPoolFileName();
2015-07-30 15:22:07 +00:00
if (!storeToBinaryFile(*this, state_file_path)) {
2015-05-27 12:08:46 +00:00
logger(INFO) << "Failed to serialize memory pool to file " << state_file_path;
2014-03-03 22:07:58 +00:00
}
2015-07-30 15:22:07 +00:00
m_paymentIdIndex.clear();
m_timestampIndex.clear();
2014-03-03 22:07:58 +00:00
return true;
}
2014-08-13 10:51:37 +00:00
2015-07-30 15:22:07 +00:00
#define CURRENT_MEMPOOL_ARCHIVE_VER 1
void serialize(CryptoNote::tx_memory_pool::TransactionDetails& td, ISerializer& s) {
s(td.id, "id");
s(td.blobSize, "blobSize");
s(td.fee, "fee");
s(td.tx, "tx");
s(td.maxUsedBlock.height, "maxUsedBlock.height");
s(td.maxUsedBlock.id, "maxUsedBlock.id");
s(td.lastFailedBlock.height, "lastFailedBlock.height");
s(td.lastFailedBlock.id, "lastFailedBlock.id");
s(td.keptByBlock, "keptByBlock");
s(reinterpret_cast<uint64_t&>(td.receiveTime), "receiveTime");
}
//---------------------------------------------------------------------------------
void tx_memory_pool::serialize(ISerializer& s) {
uint8_t version = CURRENT_MEMPOOL_ARCHIVE_VER;
s(version, "version");
if (version != CURRENT_MEMPOOL_ARCHIVE_VER) {
return;
}
std::lock_guard<std::recursive_mutex> lock(m_transactions_lock);
if (s.type() == ISerializer::INPUT) {
m_transactions.clear();
readSequence<TransactionDetails>(std::inserter(m_transactions, m_transactions.end()), "transactions", s);
} else {
writeSequence<TransactionDetails>(m_transactions.begin(), m_transactions.end(), "transactions", s);
}
KV_MEMBER(m_spent_key_images);
KV_MEMBER(m_spentOutputs);
KV_MEMBER(m_recentlyDeletedTransactions);
}
2014-08-13 10:51:37 +00:00
//---------------------------------------------------------------------------------
void tx_memory_pool::on_idle() {
m_txCheckInterval.call([this](){ return removeExpiredTransactions(); });
}
//---------------------------------------------------------------------------------
bool tx_memory_pool::removeExpiredTransactions() {
bool somethingRemoved = false;
{
2015-07-15 12:23:00 +00:00
std::lock_guard<std::recursive_mutex> lock(m_transactions_lock);
2014-08-13 10:51:37 +00:00
2015-07-15 12:23:00 +00:00
uint64_t now = m_timeProvider.now();
for (auto it = m_recentlyDeletedTransactions.begin(); it != m_recentlyDeletedTransactions.end();) {
uint64_t elapsedTimeSinceDeletion = now - it->second;
if (elapsedTimeSinceDeletion > m_currency.numberOfPeriodsToForgetTxDeletedFromPool() * m_currency.mempoolTxLiveTime()) {
it = m_recentlyDeletedTransactions.erase(it);
} else {
++it;
}
}
2014-08-13 10:51:37 +00:00
2015-07-15 12:23:00 +00:00
for (auto it = m_transactions.begin(); it != m_transactions.end();) {
uint64_t txAge = now - it->receiveTime;
bool remove = txAge > (it->keptByBlock ? m_currency.mempoolTxFromAltBlockLiveTime() : m_currency.mempoolTxLiveTime());
2015-07-15 12:23:00 +00:00
if (remove) {
logger(TRACE) << "Tx " << it->id << " removed from tx pool due to outdated, age: " << txAge;
m_recentlyDeletedTransactions.emplace(it->id, now);
it = removeTransaction(it);
somethingRemoved = true;
2015-07-15 12:23:00 +00:00
} else {
++it;
}
2014-08-13 10:51:37 +00:00
}
}
if (somethingRemoved) {
m_observerManager.notify(&ITxPoolObserver::txDeletedFromPool);
}
2014-08-13 10:51:37 +00:00
return true;
}
tx_memory_pool::tx_container_t::iterator tx_memory_pool::removeTransaction(tx_memory_pool::tx_container_t::iterator i) {
removeTransactionInputs(i->id, i->tx, i->keptByBlock);
2015-07-30 15:22:07 +00:00
m_paymentIdIndex.remove(i->tx);
m_timestampIndex.remove(i->receiveTime, i->id);
2014-08-13 10:51:37 +00:00
return m_transactions.erase(i);
}
2015-07-30 15:22:07 +00:00
bool tx_memory_pool::removeTransactionInputs(const Crypto::Hash& tx_id, const Transaction& tx, bool keptByBlock) {
for (const auto& in : tx.inputs) {
if (in.type() == typeid(KeyInput)) {
const auto& txin = boost::get<KeyInput>(in);
2014-08-13 10:51:37 +00:00
auto it = m_spent_key_images.find(txin.keyImage);
2015-05-27 12:08:46 +00:00
if (!(it != m_spent_key_images.end())) { logger(ERROR, BRIGHT_RED) << "failed to find transaction input in key images. img=" << txin.keyImage << std::endl
<< "transaction id = " << tx_id; return false; }
2015-07-30 15:22:07 +00:00
std::unordered_set<Crypto::Hash>& key_image_set = it->second;
2015-05-27 12:08:46 +00:00
if (!(!key_image_set.empty())) { logger(ERROR, BRIGHT_RED) << "empty key_image set, img=" << txin.keyImage << std::endl
<< "transaction id = " << tx_id; return false; }
2014-08-13 10:51:37 +00:00
auto it_in_set = key_image_set.find(tx_id);
2015-05-27 12:08:46 +00:00
if (!(it_in_set != key_image_set.end())) { logger(ERROR, BRIGHT_RED) << "transaction id not found in key_image set, img=" << txin.keyImage << std::endl
<< "transaction id = " << tx_id; return false; }
2014-08-13 10:51:37 +00:00
key_image_set.erase(it_in_set);
if (key_image_set.empty()) {
//it is now empty hash container for this key_image
m_spent_key_images.erase(it);
}
2015-07-30 15:22:07 +00:00
} else if (in.type() == typeid(MultisignatureInput)) {
2014-08-13 10:51:37 +00:00
if (!keptByBlock) {
2015-07-30 15:22:07 +00:00
const auto& msig = boost::get<MultisignatureInput>(in);
2014-08-13 10:51:37 +00:00
auto output = GlobalOutput(msig.amount, msig.outputIndex);
assert(m_spentOutputs.count(output));
m_spentOutputs.erase(output);
}
}
}
return true;
}
//---------------------------------------------------------------------------------
2015-07-30 15:22:07 +00:00
bool tx_memory_pool::addTransactionInputs(const Crypto::Hash& id, const Transaction& tx, bool keptByBlock) {
2014-08-13 10:51:37 +00:00
// should not fail
2015-07-30 15:22:07 +00:00
for (const auto& in : tx.inputs) {
if (in.type() == typeid(KeyInput)) {
const auto& txin = boost::get<KeyInput>(in);
std::unordered_set<Crypto::Hash>& kei_image_set = m_spent_key_images[txin.keyImage];
2015-05-27 12:08:46 +00:00
if (!(keptByBlock || kei_image_set.size() == 0)) {
logger(ERROR, BRIGHT_RED)
<< "internal error: keptByBlock=" << keptByBlock
<< ", kei_image_set.size()=" << kei_image_set.size() << ENDL
<< "txin.keyImage=" << txin.keyImage << ENDL << "tx_id=" << id;
return false;
}
2014-08-13 10:51:37 +00:00
auto ins_res = kei_image_set.insert(id);
2015-05-27 12:08:46 +00:00
if (!(ins_res.second)) {
logger(ERROR, BRIGHT_RED) << "internal error: try to insert duplicate iterator in key_image set";
return false;
}
2015-07-30 15:22:07 +00:00
} else if (in.type() == typeid(MultisignatureInput)) {
2014-08-13 10:51:37 +00:00
if (!keptByBlock) {
2015-07-30 15:22:07 +00:00
const auto& msig = boost::get<MultisignatureInput>(in);
2014-08-13 10:51:37 +00:00
auto r = m_spentOutputs.insert(GlobalOutput(msig.amount, msig.outputIndex));
(void)r;
assert(r.second);
}
}
}
return true;
}
//---------------------------------------------------------------------------------
bool tx_memory_pool::haveSpentInputs(const Transaction& tx) const {
2015-07-30 15:22:07 +00:00
for (const auto& in : tx.inputs) {
if (in.type() == typeid(KeyInput)) {
const auto& tokey_in = boost::get<KeyInput>(in);
2014-08-13 10:51:37 +00:00
if (m_spent_key_images.count(tokey_in.keyImage)) {
return true;
}
2015-07-30 15:22:07 +00:00
} else if (in.type() == typeid(MultisignatureInput)) {
const auto& msig = boost::get<MultisignatureInput>(in);
2014-08-13 10:51:37 +00:00
if (m_spentOutputs.count(GlobalOutput(msig.amount, msig.outputIndex))) {
return true;
}
}
}
return false;
}
bool tx_memory_pool::addObserver(ITxPoolObserver* observer) {
return m_observerManager.add(observer);
}
bool tx_memory_pool::removeObserver(ITxPoolObserver* observer) {
return m_observerManager.remove(observer);
}
2015-07-30 15:22:07 +00:00
void tx_memory_pool::buildIndices() {
std::lock_guard<std::recursive_mutex> lock(m_transactions_lock);
for (auto it = m_transactions.begin(); it != m_transactions.end(); it++) {
m_paymentIdIndex.add(it->tx);
m_timestampIndex.add(it->receiveTime, it->id);
}
}
bool tx_memory_pool::getTransactionIdsByPaymentId(const Crypto::Hash& paymentId, std::vector<Crypto::Hash>& transactionIds) {
std::lock_guard<std::recursive_mutex> lock(m_transactions_lock);
return m_paymentIdIndex.find(paymentId, transactionIds);
}
bool tx_memory_pool::getTransactionIdsByTimestamp(uint64_t timestampBegin, uint64_t timestampEnd, uint32_t transactionsNumberLimit, std::vector<Crypto::Hash>& hashes, uint64_t& transactionsNumberWithinTimestamps) {
std::lock_guard<std::recursive_mutex> lock(m_transactions_lock);
return m_timestampIndex.find(timestampBegin, timestampEnd, transactionsNumberLimit, hashes, transactionsNumberWithinTimestamps);
}
2014-03-03 22:07:58 +00:00
}