2015-01-02 16:52:46 +00:00
|
|
|
// Copyright (c) 2014-2015, The Monero Project
|
2014-07-23 13:03:52 +00:00
|
|
|
//
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
|
|
|
// permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
|
|
// conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
|
|
// of conditions and the following disclaimer in the documentation and/or other
|
|
|
|
// materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
|
|
|
// used to endorse or promote products derived from this software without specific
|
|
|
|
// prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
|
|
|
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
|
|
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
|
|
|
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
2014-03-03 22:07:58 +00:00
|
|
|
|
|
|
|
#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"
|
2014-06-15 07:48:13 +00:00
|
|
|
#include "math_helper.h"
|
2014-03-03 22:07:58 +00:00
|
|
|
#include "cryptonote_basic_impl.h"
|
|
|
|
#include "verification_context.h"
|
|
|
|
#include "crypto/hash.h"
|
|
|
|
|
|
|
|
namespace cryptonote
|
|
|
|
{
|
2015-01-26 05:36:09 +00:00
|
|
|
#if BLOCKCHAIN_DB == DB_LMDB
|
2014-10-28 03:44:45 +00:00
|
|
|
class Blockchain;
|
2015-01-26 05:36:09 +00:00
|
|
|
#else
|
|
|
|
class blockchain_storage;
|
|
|
|
#endif
|
2014-03-03 22:07:58 +00:00
|
|
|
/************************************************************************/
|
|
|
|
/* */
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
class tx_memory_pool: boost::noncopyable
|
|
|
|
{
|
|
|
|
public:
|
2015-01-26 05:36:09 +00:00
|
|
|
#if BLOCKCHAIN_DB == DB_LMDB
|
2014-10-28 03:44:45 +00:00
|
|
|
tx_memory_pool(Blockchain& bchs);
|
2015-01-26 05:36:09 +00:00
|
|
|
#else
|
|
|
|
tx_memory_pool(blockchain_storage& bchs);
|
|
|
|
#endif
|
2014-09-25 06:24:42 +00:00
|
|
|
bool add_tx(const transaction &tx, const crypto::hash &id, size_t blob_size, tx_verification_context& tvc, bool keeped_by_block);
|
2014-03-03 22:07:58 +00:00
|
|
|
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);
|
|
|
|
|
2014-07-17 14:31:44 +00:00
|
|
|
bool have_tx(const crypto::hash &id) const;
|
2014-03-03 22:07:58 +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-06-15 07:48:13 +00:00
|
|
|
void on_idle();
|
2014-03-03 22:07:58 +00:00
|
|
|
|
2014-07-17 14:31:44 +00:00
|
|
|
void lock() const;
|
|
|
|
void unlock() const;
|
2014-03-03 22:07:58 +00:00
|
|
|
|
|
|
|
// load/store operations
|
|
|
|
bool init(const std::string& config_folder);
|
|
|
|
bool deinit();
|
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-07-17 14:31:44 +00:00
|
|
|
void get_transactions(std::list<transaction>& txs) const;
|
|
|
|
bool get_transaction(const crypto::hash& h, transaction& tx) const;
|
|
|
|
size_t get_transactions_count() const;
|
|
|
|
std::string print_pool(bool short_format) const;
|
2014-03-03 22:07:58 +00:00
|
|
|
|
|
|
|
/*bool flush_pool(const std::strig& folder);
|
|
|
|
bool inflate_pool(const std::strig& folder);*/
|
|
|
|
|
2014-09-25 06:24:42 +00:00
|
|
|
#define CURRENT_MEMPOOL_ARCHIVE_VER 8
|
2014-03-03 22:07:58 +00:00
|
|
|
|
|
|
|
template<class archive_t>
|
|
|
|
void serialize(archive_t & a, const unsigned int version)
|
|
|
|
{
|
|
|
|
if(version < CURRENT_MEMPOOL_ARCHIVE_VER )
|
|
|
|
return;
|
|
|
|
CRITICAL_REGION_LOCAL(m_transactions_lock);
|
|
|
|
a & m_transactions;
|
|
|
|
a & m_spent_key_images;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tx_details
|
|
|
|
{
|
|
|
|
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;
|
2014-06-15 07:48:13 +00:00
|
|
|
time_t receive_time;
|
2014-03-03 22:07:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2014-06-15 07:48:13 +00:00
|
|
|
bool remove_stuck_transactions();
|
2014-07-17 14:31:44 +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;
|
2014-07-17 14:27:37 +00:00
|
|
|
bool remove_transaction_keyimages(const transaction& tx);
|
2014-07-17 14:47:17 +00:00
|
|
|
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);
|
2014-07-17 14:27:37 +00:00
|
|
|
|
2014-07-17 14:31:44 +00:00
|
|
|
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-07-17 14:31:44 +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;
|
2014-06-15 07:48:13 +00:00
|
|
|
epee::math_helper::once_a_time_seconds<30> m_remove_stuck_tx_interval;
|
2014-03-03 22:07:58 +00:00
|
|
|
|
|
|
|
//transactions_container m_alternative_transactions;
|
|
|
|
|
|
|
|
std::string m_config_folder;
|
2015-01-26 05:36:09 +00:00
|
|
|
#if BLOCKCHAIN_DB == DB_LMDB
|
2014-10-28 03:44:45 +00:00
|
|
|
Blockchain& m_blockchain;
|
2015-01-26 05:36:09 +00:00
|
|
|
#else
|
|
|
|
blockchain_storage& m_blockchain;
|
|
|
|
#endif
|
2014-03-03 22:07:58 +00:00
|
|
|
/************************************************************************/
|
|
|
|
/* */
|
|
|
|
/************************************************************************/
|
|
|
|
/*class inputs_visitor: public boost::static_visitor<bool>
|
|
|
|
{
|
|
|
|
key_images_container& m_spent_keys;
|
|
|
|
public:
|
|
|
|
inputs_visitor(key_images_container& spent_keys): m_spent_keys(spent_keys)
|
|
|
|
{}
|
|
|
|
bool operator()(const txin_to_key& tx) const
|
|
|
|
{
|
|
|
|
auto pr = m_spent_keys.insert(tx.k_image);
|
|
|
|
CHECK_AND_ASSERT_MES(pr.second, false, "Tried to insert transaction with input seems already spent, input: " << epee::string_tools::pod_to_hex(tx.k_image));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool operator()(const txin_gen& tx) const
|
|
|
|
{
|
|
|
|
CHECK_AND_ASSERT_MES(false, false, "coinbase transaction in memory pool");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool operator()(const txin_to_script& tx) const {return false;}
|
|
|
|
bool operator()(const txin_to_scripthash& tx) const {return false;}
|
|
|
|
}; */
|
|
|
|
/************************************************************************/
|
|
|
|
/* */
|
|
|
|
/************************************************************************/
|
|
|
|
class amount_visitor: public boost::static_visitor<uint64_t>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
uint64_t operator()(const txin_to_key& tx) const
|
|
|
|
{
|
|
|
|
return tx.amount;
|
|
|
|
}
|
|
|
|
uint64_t operator()(const txin_gen& tx) const
|
|
|
|
{
|
|
|
|
CHECK_AND_ASSERT_MES(false, false, "coinbase transaction in memory pool");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint64_t operator()(const txin_to_script& tx) const {return 0;}
|
|
|
|
uint64_t operator()(const txin_to_scripthash& tx) const {return 0;}
|
|
|
|
};
|
|
|
|
|
2015-01-26 05:36:09 +00:00
|
|
|
#if BLOCKCHAIN_DB == DB_LMDB
|
|
|
|
#else
|
|
|
|
#if defined(DEBUG_CREATE_BLOCK_TEMPLATE)
|
|
|
|
friend class blockchain_storage;
|
|
|
|
#endif
|
|
|
|
#endif
|
2014-03-03 22:07:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace boost
|
|
|
|
{
|
|
|
|
namespace serialization
|
|
|
|
{
|
|
|
|
template<class archive_t>
|
|
|
|
void serialize(archive_t & ar, cryptonote::tx_memory_pool::tx_details& td, const unsigned int version)
|
|
|
|
{
|
|
|
|
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-15 07:48:13 +00:00
|
|
|
ar & td.receive_time;
|
2014-03-03 22:07:58 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BOOST_CLASS_VERSION(cryptonote::tx_memory_pool, CURRENT_MEMPOOL_ARCHIVE_VER)
|
|
|
|
|
|
|
|
|
|
|
|
|