2014-03-03 22:07:58 +00:00
|
|
|
// Copyright (c) 2012-2013 The Cryptonote developers
|
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#include "include_base_utils.h"
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <boost/serialization/version.hpp>
|
|
|
|
#include <boost/utility.hpp>
|
|
|
|
|
|
|
|
#include "string_tools.h"
|
|
|
|
#include "syncobj.h"
|
|
|
|
#include "cryptonote_basic_impl.h"
|
|
|
|
#include "verification_context.h"
|
|
|
|
#include "crypto/hash.h"
|
|
|
|
|
|
|
|
|
2014-06-25 17:21:42 +00:00
|
|
|
namespace cryptonote {
|
2014-03-03 22:07:58 +00:00
|
|
|
class blockchain_storage;
|
2014-06-25 17:21:42 +00:00
|
|
|
|
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:
|
|
|
|
tx_memory_pool(blockchain_storage& bchs);
|
2014-06-25 17:21:42 +00:00
|
|
|
|
|
|
|
// load/store operations
|
|
|
|
bool init(const std::string& config_folder);
|
|
|
|
bool deinit();
|
|
|
|
|
|
|
|
bool have_tx(const crypto::hash &id) const;
|
2014-03-03 22:07:58 +00:00
|
|
|
bool add_tx(const transaction &tx, const crypto::hash &id, size_t blob_size, tx_verification_context& tvc, bool keeped_by_block);
|
|
|
|
bool add_tx(const transaction &tx, tx_verification_context& tvc, bool keeped_by_block);
|
|
|
|
//gets tx and remove it from pool
|
|
|
|
bool take_tx(const crypto::hash &id, transaction &tx, size_t& blob_size, uint64_t& fee);
|
|
|
|
|
|
|
|
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-06-25 17:21:42 +00:00
|
|
|
void lock() const;
|
|
|
|
void unlock() const;
|
2014-03-03 22:07:58 +00:00
|
|
|
|
2014-04-02 16:00:17 +00:00
|
|
|
bool 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
|
|
|
|
2014-06-25 17:21:42 +00:00
|
|
|
void get_transactions(std::list<transaction>& txs) const;
|
|
|
|
size_t get_transactions_count() const;
|
|
|
|
std::string print_pool(bool short_format) const;
|
2014-03-03 22:07:58 +00:00
|
|
|
|
|
|
|
#define CURRENT_MEMPOOL_ARCHIVE_VER 7
|
|
|
|
|
|
|
|
template<class archive_t>
|
2014-06-25 17:21:42 +00:00
|
|
|
void serialize(archive_t & a, const unsigned int version) {
|
|
|
|
if (version < CURRENT_MEMPOOL_ARCHIVE_VER) {
|
2014-03-03 22:07:58 +00:00
|
|
|
return;
|
2014-06-25 17:21:42 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
|
|
|
a & m_transactions;
|
|
|
|
a & m_spent_key_images;
|
|
|
|
}
|
|
|
|
|
2014-06-25 17:21:42 +00:00
|
|
|
struct tx_details {
|
2014-03-03 22:07:58 +00:00
|
|
|
transaction tx;
|
|
|
|
size_t blob_size;
|
|
|
|
uint64_t fee;
|
|
|
|
crypto::hash max_used_block_id;
|
|
|
|
uint64_t max_used_block_height;
|
|
|
|
bool kept_by_block;
|
|
|
|
//
|
|
|
|
uint64_t last_failed_height;
|
|
|
|
crypto::hash last_failed_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2014-06-25 17:21:42 +00:00
|
|
|
bool have_tx_keyimg_as_spent(const crypto::key_image& key_im) const;
|
|
|
|
bool have_tx_keyimges_as_spent(const transaction& tx) const;
|
|
|
|
bool remove_transaction_keyimages(const transaction& tx);
|
|
|
|
static bool have_key_images(const std::unordered_set<crypto::key_image>& kic, const transaction& tx);
|
|
|
|
static bool append_key_images(std::unordered_set<crypto::key_image>& kic, const transaction& tx);
|
|
|
|
|
|
|
|
bool is_transaction_ready_to_go(tx_details& txd) const;
|
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
typedef std::unordered_map<crypto::hash, tx_details > transactions_container;
|
|
|
|
typedef std::unordered_map<crypto::key_image, std::unordered_set<crypto::hash> > key_images_container;
|
|
|
|
|
2014-06-25 17:21:42 +00:00
|
|
|
mutable epee::critical_section m_transactions_lock;
|
2014-03-03 22:07:58 +00:00
|
|
|
transactions_container m_transactions;
|
|
|
|
key_images_container m_spent_key_images;
|
|
|
|
|
|
|
|
std::string m_config_folder;
|
|
|
|
blockchain_storage& m_blockchain;
|
2014-06-25 17:21:42 +00:00
|
|
|
|
2014-03-03 22:07:58 +00:00
|
|
|
/************************************************************************/
|
|
|
|
/* */
|
|
|
|
/************************************************************************/
|
2014-06-25 17:21:42 +00:00
|
|
|
class amount_visitor: public boost::static_visitor<uint64_t> {
|
2014-03-03 22:07:58 +00:00
|
|
|
public:
|
2014-06-25 17:21:42 +00:00
|
|
|
uint64_t operator()(const txin_to_key& tx) const {
|
2014-03-03 22:07:58 +00:00
|
|
|
return tx.amount;
|
|
|
|
}
|
2014-06-25 17:21:42 +00:00
|
|
|
|
|
|
|
uint64_t operator()(const txin_gen& tx) const {
|
2014-03-03 22:07:58 +00:00
|
|
|
CHECK_AND_ASSERT_MES(false, false, "coinbase transaction in memory pool");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-25 17:21:42 +00:00
|
|
|
|
|
|
|
uint64_t operator()(const txin_to_script& tx) const { return 0; }
|
|
|
|
uint64_t operator()(const txin_to_scripthash& tx) const { return 0; }
|
2014-03-03 22:07:58 +00:00
|
|
|
};
|
|
|
|
|
2014-04-29 16:26:45 +00:00
|
|
|
#if defined(DEBUG_CREATE_BLOCK_TEMPLATE)
|
|
|
|
friend class blockchain_storage;
|
|
|
|
#endif
|
2014-03-03 22:07:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-06-25 17:21:42 +00:00
|
|
|
namespace boost {
|
|
|
|
namespace serialization {
|
2014-03-03 22:07:58 +00:00
|
|
|
template<class archive_t>
|
2014-06-25 17:21:42 +00:00
|
|
|
void serialize(archive_t & ar, cryptonote::tx_memory_pool::tx_details& td, const unsigned int version) {
|
2014-03-03 22:07:58 +00:00
|
|
|
ar & td.blob_size;
|
|
|
|
ar & td.fee;
|
|
|
|
ar & td.tx;
|
|
|
|
ar & td.max_used_block_height;
|
|
|
|
ar & td.max_used_block_id;
|
|
|
|
ar & td.last_failed_height;
|
|
|
|
ar & td.last_failed_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-25 17:21:42 +00:00
|
|
|
BOOST_CLASS_VERSION(cryptonote::tx_memory_pool, CURRENT_MEMPOOL_ARCHIVE_VER)
|