danicoin/src/CryptoNoteCore/TransactionPool.h

209 lines
7.3 KiB
C
Raw Normal View History

// Copyright (c) 2011-2016 The Cryptonote developers
2014-03-03 22:07:58 +00:00
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#pragma once
#include <set>
#include <unordered_map>
#include <unordered_set>
2014-08-13 10:51:37 +00:00
2014-03-03 22:07:58 +00:00
#include <boost/utility.hpp>
2014-08-13 10:51:37 +00:00
// multi index
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
2015-07-30 15:22:07 +00:00
#include "Common/Util.h"
2015-05-27 12:08:46 +00:00
#include "Common/int-util.h"
#include "Common/ObserverManager.h"
2014-03-03 22:07:58 +00:00
#include "crypto/hash.h"
2015-07-30 15:22:07 +00:00
#include "CryptoNoteCore/CryptoNoteBasic.h"
#include "CryptoNoteCore/CryptoNoteBasicImpl.h"
#include "CryptoNoteCore/Currency.h"
#include "CryptoNoteCore/ITimeProvider.h"
#include "CryptoNoteCore/ITransactionValidator.h"
#include "CryptoNoteCore/ITxPoolObserver.h"
#include "CryptoNoteCore/VerificationContext.h"
#include "CryptoNoteCore/BlockchainIndices.h"
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
#include <Logging/LoggerRef.h>
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
namespace CryptoNote {
2014-08-13 10:51:37 +00:00
2015-07-30 15:22:07 +00:00
class ISerializer;
2014-08-13 10:51:37 +00:00
class OnceInTimeInterval {
public:
OnceInTimeInterval(unsigned interval, CryptoNote::ITimeProvider& timeProvider)
: m_interval(interval), m_timeProvider(timeProvider) {
m_lastWorkedTime = 0;
}
template<class functor_t>
bool call(functor_t functr) {
time_t now = m_timeProvider.now();
if (now - m_lastWorkedTime > m_interval) {
bool res = functr();
m_lastWorkedTime = m_timeProvider.now();
return res;
}
return true;
}
private:
time_t m_lastWorkedTime;
unsigned m_interval;
CryptoNote::ITimeProvider& m_timeProvider;
};
2014-03-03 22:07:58 +00:00
2014-08-13 10:51:37 +00:00
using CryptoNote::BlockInfo;
using namespace boost::multi_index;
2014-03-03 22:07:58 +00:00
/************************************************************************/
/* */
/************************************************************************/
2014-06-25 17:21:42 +00:00
class tx_memory_pool: boost::noncopyable {
2014-03-03 22:07:58 +00:00
public:
2015-05-27 12:08:46 +00:00
tx_memory_pool(
const CryptoNote::Currency& currency,
CryptoNote::ITransactionValidator& validator,
CryptoNote::ITimeProvider& timeProvider,
Logging::ILogger& log);
2014-03-03 22:07:58 +00:00
bool addObserver(ITxPoolObserver* observer);
bool removeObserver(ITxPoolObserver* observer);
2014-06-25 17:21:42 +00:00
// load/store operations
bool init(const std::string& config_folder);
bool deinit();
2015-07-30 15:22:07 +00:00
bool have_tx(const Crypto::Hash &id) const;
bool add_tx(const Transaction &tx, const Crypto::Hash &id, size_t blobSize, tx_verification_context& tvc, bool keeped_by_block);
2014-08-13 10:51:37 +00:00
bool add_tx(const Transaction &tx, tx_verification_context& tvc, bool keeped_by_block);
2014-03-03 22:07:58 +00:00
//gets tx and remove it from pool
2015-07-30 15:22:07 +00:00
bool take_tx(const Crypto::Hash &id, Transaction &tx, size_t& blobSize, uint64_t& fee);
2014-03-03 22:07:58 +00:00
2015-07-30 15:22:07 +00:00
bool on_blockchain_inc(uint64_t new_block_height, const Crypto::Hash& top_block_id);
bool on_blockchain_dec(uint64_t new_block_height, const Crypto::Hash& top_block_id);
2014-03-03 22:07:58 +00:00
2014-06-25 17:21:42 +00:00
void lock() const;
void unlock() const;
2015-08-27 18:55:14 +00:00
std::unique_lock<std::recursive_mutex> obtainGuard() const;
2014-03-03 22:07:58 +00:00
2014-08-13 10:51:37 +00:00
bool fill_block_template(Block &bl, size_t median_size, size_t maxCumulativeSize, uint64_t already_generated_coins, size_t &total_size, uint64_t &fee);
2014-03-03 22:07:58 +00:00
2014-08-13 10:51:37 +00:00
void get_transactions(std::list<Transaction>& txs) const;
2015-07-30 15:22:07 +00:00
void 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;
2014-06-25 17:21:42 +00:00
size_t get_transactions_count() const;
std::string print_pool(bool short_format) const;
2014-08-13 10:51:37 +00:00
void on_idle();
2014-03-03 22:07:58 +00:00
2015-07-30 15:22:07 +00:00
bool getTransactionIdsByPaymentId(const Crypto::Hash& paymentId, std::vector<Crypto::Hash>& transactionIds);
bool getTransactionIdsByTimestamp(uint64_t timestampBegin, uint64_t timestampEnd, uint32_t transactionsNumberLimit, std::vector<Crypto::Hash>& hashes, uint64_t& transactionsNumberWithinTimestamps);
2014-08-13 10:51:37 +00:00
template<class t_ids_container, class t_tx_container, class t_missed_container>
void getTransactions(const t_ids_container& txsIds, t_tx_container& txs, t_missed_container& missedTxs) {
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-08-13 10:51:37 +00:00
for (const auto& id : txsIds) {
auto it = m_transactions.find(id);
if (it == m_transactions.end()) {
missedTxs.push_back(id);
} else {
txs.push_back(it->tx);
}
}
}
2015-07-30 15:22:07 +00:00
void serialize(ISerializer& s);
2014-03-03 22:07:58 +00:00
2014-08-13 10:51:37 +00:00
struct TransactionCheckInfo {
BlockInfo maxUsedBlock;
BlockInfo lastFailedBlock;
};
struct TransactionDetails : public TransactionCheckInfo {
2015-07-30 15:22:07 +00:00
Crypto::Hash id;
2014-08-13 10:51:37 +00:00
Transaction tx;
size_t blobSize;
2014-03-03 22:07:58 +00:00
uint64_t fee;
2014-08-13 10:51:37 +00:00
bool keptByBlock;
time_t receiveTime;
2014-03-03 22:07:58 +00:00
};
private:
2014-06-25 17:21:42 +00:00
2014-08-13 10:51:37 +00:00
struct TransactionPriorityComparator {
// lhs > hrs
bool operator()(const TransactionDetails& lhs, const TransactionDetails& rhs) const {
// price(lhs) = lhs.fee / lhs.blobSize
// price(lhs) > price(rhs) -->
// lhs.fee / lhs.blobSize > rhs.fee / rhs.blobSize -->
// lhs.fee * rhs.blobSize > rhs.fee * lhs.blobSize
uint64_t lhs_hi, lhs_lo = mul128(lhs.fee, rhs.blobSize, &lhs_hi);
uint64_t rhs_hi, rhs_lo = mul128(rhs.fee, lhs.blobSize, &rhs_hi);
return
// prefer more profitable transactions
(lhs_hi > rhs_hi) ||
(lhs_hi == rhs_hi && lhs_lo > rhs_lo) ||
// prefer smaller
(lhs_hi == rhs_hi && lhs_lo == rhs_lo && lhs.blobSize < rhs.blobSize) ||
// prefer older
(lhs_hi == rhs_hi && lhs_lo == rhs_lo && lhs.blobSize == rhs.blobSize && lhs.receiveTime < rhs.receiveTime);
}
};
2015-07-30 15:22:07 +00:00
typedef hashed_unique<BOOST_MULTI_INDEX_MEMBER(TransactionDetails, Crypto::Hash, id)> main_index_t;
2014-08-13 10:51:37 +00:00
typedef ordered_non_unique<identity<TransactionDetails>, TransactionPriorityComparator> fee_index_t;
2014-06-25 17:21:42 +00:00
2014-08-13 10:51:37 +00:00
typedef multi_index_container<TransactionDetails,
indexed_by<main_index_t, fee_index_t>
> tx_container_t;
typedef std::pair<uint64_t, uint64_t> GlobalOutput;
typedef std::set<GlobalOutput> GlobalOutputsContainer;
2015-07-30 15:22:07 +00:00
typedef std::unordered_map<Crypto::KeyImage, std::unordered_set<Crypto::Hash> > key_images_container;
2014-03-03 22:07:58 +00:00
2014-08-13 10:51:37 +00:00
// double spending checking
2015-07-30 15:22:07 +00:00
bool addTransactionInputs(const Crypto::Hash& id, const Transaction& tx, bool keptByBlock);
2014-08-13 10:51:37 +00:00
bool haveSpentInputs(const Transaction& tx) const;
2015-07-30 15:22:07 +00:00
bool removeTransactionInputs(const Crypto::Hash& id, const Transaction& tx, bool keptByBlock);
2014-08-13 10:51:37 +00:00
tx_container_t::iterator removeTransaction(tx_container_t::iterator i);
bool removeExpiredTransactions();
bool is_transaction_ready_to_go(const Transaction& tx, TransactionCheckInfo& txd) const;
2015-07-30 15:22:07 +00:00
void buildIndices();
2015-07-30 15:22:07 +00:00
Tools::ObserverManager<ITxPoolObserver> m_observerManager;
2015-05-27 12:08:46 +00:00
const CryptoNote::Currency& m_currency;
2014-08-13 10:51:37 +00:00
OnceInTimeInterval m_txCheckInterval;
2015-05-27 12:08:46 +00:00
mutable std::recursive_mutex m_transactions_lock;
2014-03-03 22:07:58 +00:00
key_images_container m_spent_key_images;
2014-08-13 10:51:37 +00:00
GlobalOutputsContainer m_spentOutputs;
2014-03-03 22:07:58 +00:00
std::string m_config_folder;
2014-08-13 10:51:37 +00:00
CryptoNote::ITransactionValidator& m_validator;
CryptoNote::ITimeProvider& m_timeProvider;
2014-06-25 17:21:42 +00:00
2014-08-13 10:51:37 +00:00
tx_container_t m_transactions;
tx_container_t::nth_index<1>::type& m_fee_index;
2015-07-30 15:22:07 +00:00
std::unordered_map<Crypto::Hash, uint64_t> m_recentlyDeletedTransactions;
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
Logging::LoggerRef logger;
2014-03-03 22:07:58 +00:00
2015-07-30 15:22:07 +00:00
PaymentIdIndex m_paymentIdIndex;
TimestampTransactionsIndex m_timestampIndex;
2014-03-03 22:07:58 +00:00
};
}