2019-03-05 16:05:34 -05:00
|
|
|
// Copyright (c) 2014-2019, The Monero Project
|
2015-12-13 23:54:39 -05:00
|
|
|
//
|
2014-07-23 09:03:52 -04:00
|
|
|
// All rights reserved.
|
2015-12-13 23:54:39 -05:00
|
|
|
//
|
2014-07-23 09:03:52 -04:00
|
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
|
|
|
// permitted provided that the following conditions are met:
|
2015-12-13 23:54:39 -05:00
|
|
|
//
|
2014-07-23 09:03:52 -04:00
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
|
|
// conditions and the following disclaimer.
|
2015-12-13 23:54:39 -05:00
|
|
|
//
|
2014-07-23 09:03:52 -04:00
|
|
|
// 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.
|
2015-12-13 23:54:39 -05:00
|
|
|
//
|
2014-07-23 09:03:52 -04:00
|
|
|
// 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.
|
2015-12-13 23:54:39 -05:00
|
|
|
//
|
2014-07-23 09:03:52 -04:00
|
|
|
// 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.
|
2015-12-13 23:54:39 -05:00
|
|
|
//
|
2014-07-23 09:03:52 -04:00
|
|
|
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
2014-03-03 17:07:58 -05:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#include "include_base_utils.h"
|
|
|
|
|
|
|
|
#include <set>
|
2019-11-02 16:36:03 -04:00
|
|
|
#include <tuple>
|
2014-03-03 17:07:58 -05:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
2015-04-30 01:02:12 -04:00
|
|
|
#include <queue>
|
2014-03-03 17:07:58 -05:00
|
|
|
#include <boost/serialization/version.hpp>
|
|
|
|
#include <boost/utility.hpp>
|
|
|
|
|
2019-11-02 16:36:03 -04:00
|
|
|
#include "span.h"
|
2014-03-03 17:07:58 -05:00
|
|
|
#include "string_tools.h"
|
|
|
|
#include "syncobj.h"
|
2014-06-15 03:48:13 -04:00
|
|
|
#include "math_helper.h"
|
2017-01-26 10:07:23 -05:00
|
|
|
#include "cryptonote_basic/cryptonote_basic_impl.h"
|
|
|
|
#include "cryptonote_basic/verification_context.h"
|
2019-11-02 16:36:03 -04:00
|
|
|
#include "cryptonote_protocol/enums.h"
|
2017-05-14 09:06:55 -04:00
|
|
|
#include "blockchain_db/blockchain_db.h"
|
2014-03-03 17:07:58 -05:00
|
|
|
#include "crypto/hash.h"
|
2015-04-23 08:13:07 -04:00
|
|
|
#include "rpc/core_rpc_server_commands_defs.h"
|
2017-09-05 12:20:27 -04:00
|
|
|
#include "rpc/message_data_structs.h"
|
2014-03-03 17:07:58 -05:00
|
|
|
|
|
|
|
namespace cryptonote
|
|
|
|
{
|
2014-10-27 23:44:45 -04:00
|
|
|
class Blockchain;
|
2014-03-03 17:07:58 -05:00
|
|
|
/************************************************************************/
|
|
|
|
/* */
|
|
|
|
/************************************************************************/
|
|
|
|
|
2016-03-24 20:03:02 -04:00
|
|
|
//! pair of <transaction fee, transaction hash> for organization
|
2017-01-13 10:08:37 -05:00
|
|
|
typedef std::pair<std::pair<double, std::time_t>, crypto::hash> tx_by_fee_and_receive_time_entry;
|
2016-03-24 20:03:02 -04:00
|
|
|
|
2015-05-13 20:27:06 -04:00
|
|
|
class txCompare
|
|
|
|
{
|
|
|
|
public:
|
2019-02-01 09:02:39 -05:00
|
|
|
bool operator()(const tx_by_fee_and_receive_time_entry& a, const tx_by_fee_and_receive_time_entry& b) const
|
2015-05-13 20:27:06 -04:00
|
|
|
{
|
|
|
|
// sort by greatest first, not least
|
2017-01-13 10:08:37 -05:00
|
|
|
if (a.first.first > b.first.first) return true;
|
|
|
|
else if (a.first.first < b.first.first) return false;
|
|
|
|
else if (a.first.second < b.first.second) return true;
|
|
|
|
else if (a.first.second > b.first.second) return false;
|
2015-05-13 20:27:06 -04:00
|
|
|
else if (a.second != b.second) return true;
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-24 20:03:02 -04:00
|
|
|
//! container for sorting transactions by fee per unit size
|
2017-01-13 10:08:37 -05:00
|
|
|
typedef std::set<tx_by_fee_and_receive_time_entry, txCompare> sorted_tx_container;
|
2015-05-13 20:27:06 -04:00
|
|
|
|
2016-03-24 20:03:02 -04:00
|
|
|
/**
|
|
|
|
* @brief Transaction pool, handles transactions which are not part of a block
|
|
|
|
*
|
|
|
|
* This class handles all transactions which have been received, but not as
|
|
|
|
* part of a block.
|
|
|
|
*
|
|
|
|
* This handling includes:
|
|
|
|
* storing the transactions
|
2018-07-18 17:24:53 -04:00
|
|
|
* organizing the transactions by fee per weight unit
|
2016-03-24 20:03:02 -04:00
|
|
|
* taking/giving transactions to and from various other components
|
|
|
|
* saving the transactions to disk on shutdown
|
|
|
|
* helping create a new block template by choosing transactions for it
|
|
|
|
*
|
|
|
|
*/
|
2014-03-03 17:07:58 -05:00
|
|
|
class tx_memory_pool: boost::noncopyable
|
|
|
|
{
|
|
|
|
public:
|
2016-03-24 20:03:02 -04:00
|
|
|
/**
|
|
|
|
* @brief Constructor
|
|
|
|
*
|
|
|
|
* @param bchs a Blockchain class instance, for getting chain info
|
|
|
|
*/
|
2014-10-27 23:44:45 -04:00
|
|
|
tx_memory_pool(Blockchain& bchs);
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-05-14 09:06:55 -04:00
|
|
|
* @copydoc add_tx(transaction&, tx_verification_context&, bool, bool, uint8_t)
|
2016-03-24 20:03:02 -04:00
|
|
|
*
|
|
|
|
* @param id the transaction's hash
|
2019-11-02 16:36:03 -04:00
|
|
|
* @tx_relay how the transaction was received
|
2018-07-18 17:24:53 -04:00
|
|
|
* @param tx_weight the transaction's weight
|
2016-03-24 20:03:02 -04:00
|
|
|
*/
|
2019-11-02 16:36:03 -04:00
|
|
|
bool add_tx(transaction &tx, const crypto::hash &id, const cryptonote::blobdata &blob, size_t tx_weight, tx_verification_context& tvc, relay_method tx_relay, bool relayed, uint8_t version);
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief add a transaction to the transaction pool
|
|
|
|
*
|
|
|
|
* Most likely the transaction will come from the network, but it is
|
|
|
|
* also possible for transactions to come from popped blocks during
|
|
|
|
* a reorg, or from local clients creating a transaction and
|
|
|
|
* submitting it to the network
|
|
|
|
*
|
|
|
|
* @param tx the transaction to be added
|
|
|
|
* @param tvc return-by-reference status about the transaction verification
|
2019-11-02 16:36:03 -04:00
|
|
|
* @tx_relay how the transaction was received
|
2016-03-24 20:03:02 -04:00
|
|
|
* @param relayed was this transaction from the network or a local client?
|
|
|
|
* @param version the version used to create the transaction
|
|
|
|
*
|
|
|
|
* @return true if the transaction passes validations, otherwise false
|
|
|
|
*/
|
2019-11-02 16:36:03 -04:00
|
|
|
bool add_tx(transaction &tx, tx_verification_context& tvc, relay_method tx_relay, bool relayed, uint8_t version);
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief takes a transaction with the given hash from the pool
|
|
|
|
*
|
|
|
|
* @param id the hash of the transaction
|
|
|
|
* @param tx return-by-reference the transaction taken
|
2018-11-11 09:51:03 -05:00
|
|
|
* @param txblob return-by-reference the transaction as a blob
|
2018-07-18 17:24:53 -04:00
|
|
|
* @param tx_weight return-by-reference the transaction's weight
|
2016-03-24 20:03:02 -04:00
|
|
|
* @param fee the transaction fee
|
|
|
|
* @param relayed return-by-reference was transaction relayed to us by the network?
|
2017-01-14 08:01:21 -05:00
|
|
|
* @param do_not_relay return-by-reference is transaction not to be relayed to the network?
|
2017-09-22 08:57:20 -04:00
|
|
|
* @param double_spend_seen return-by-reference was a double spend seen for that transaction?
|
2019-09-16 08:18:34 -04:00
|
|
|
* @param pruned return-by-reference is the tx pruned
|
2016-03-24 20:03:02 -04:00
|
|
|
*
|
|
|
|
* @return true unless the transaction cannot be found in the pool
|
|
|
|
*/
|
2019-09-16 08:18:34 -04:00
|
|
|
bool take_tx(const crypto::hash &id, transaction &tx, cryptonote::blobdata &txblob, size_t& tx_weight, uint64_t& fee, bool &relayed, bool &do_not_relay, bool &double_spend_seen, bool &pruned);
|
2014-03-03 17:07:58 -05:00
|
|
|
|
2016-03-24 20:03:02 -04:00
|
|
|
/**
|
|
|
|
* @brief checks if the pool has a transaction with the given hash
|
|
|
|
*
|
|
|
|
* @param id the hash to look for
|
2019-11-02 16:36:03 -04:00
|
|
|
* @param tx_category a filter for txes
|
2016-03-24 20:03:02 -04:00
|
|
|
*
|
2019-11-02 16:36:03 -04:00
|
|
|
* @return true if the transaction is in the pool and meets tx_category requirements
|
2016-03-24 20:03:02 -04:00
|
|
|
*/
|
2019-11-02 16:36:03 -04:00
|
|
|
bool have_tx(const crypto::hash &id, relay_category tx_category) const;
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief action to take when notified of a block added to the blockchain
|
|
|
|
*
|
|
|
|
* Currently does nothing
|
|
|
|
*
|
|
|
|
* @param new_block_height the height of the blockchain after the change
|
|
|
|
* @param top_block_id the hash of the new top block
|
|
|
|
*
|
|
|
|
* @return true
|
|
|
|
*/
|
2014-03-03 17:07:58 -05:00
|
|
|
bool on_blockchain_inc(uint64_t new_block_height, const crypto::hash& top_block_id);
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief action to take when notified of a block removed from the blockchain
|
|
|
|
*
|
|
|
|
* Currently does nothing
|
|
|
|
*
|
|
|
|
* @param new_block_height the height of the blockchain after the change
|
|
|
|
* @param top_block_id the hash of the new top block
|
|
|
|
*
|
|
|
|
* @return true
|
|
|
|
*/
|
2014-03-03 17:07:58 -05:00
|
|
|
bool on_blockchain_dec(uint64_t new_block_height, const crypto::hash& top_block_id);
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief action to take periodically
|
|
|
|
*
|
|
|
|
* Currently checks transaction pool for stale ("stuck") transactions
|
|
|
|
*/
|
2014-06-15 03:48:13 -04:00
|
|
|
void on_idle();
|
2014-03-03 17:07:58 -05:00
|
|
|
|
2016-03-24 20:03:02 -04:00
|
|
|
/**
|
|
|
|
* @brief locks the transaction pool
|
|
|
|
*/
|
2014-07-17 10:31:44 -04:00
|
|
|
void lock() const;
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief unlocks the transaction pool
|
|
|
|
*/
|
2014-07-17 10:31:44 -04:00
|
|
|
void unlock() const;
|
2014-03-03 17:07:58 -05:00
|
|
|
|
|
|
|
// load/store operations
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief loads pool state (if any) from disk, and initializes pool
|
|
|
|
*
|
2018-07-18 17:24:53 -04:00
|
|
|
* @param max_txpool_weight the max weight in bytes
|
2016-03-24 20:03:02 -04:00
|
|
|
*
|
|
|
|
* @return true
|
|
|
|
*/
|
2018-07-18 17:24:53 -04:00
|
|
|
bool init(size_t max_txpool_weight = 0);
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief attempts to save the transaction pool state to disk
|
|
|
|
*
|
|
|
|
* Currently fails (returns false) if the data directory from init()
|
|
|
|
* does not exist and cannot be created, but returns true even if
|
|
|
|
* saving to disk is unsuccessful.
|
|
|
|
*
|
|
|
|
* @return true in most cases (see above)
|
|
|
|
*/
|
2014-03-03 17:07:58 -05:00
|
|
|
bool deinit();
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Chooses transactions for a block to include
|
|
|
|
*
|
|
|
|
* @param bl return-by-reference the block to fill in with transactions
|
2018-07-18 17:24:53 -04:00
|
|
|
* @param median_weight the current median block weight
|
2016-03-24 20:03:02 -04:00
|
|
|
* @param already_generated_coins the current total number of coins "minted"
|
2018-07-18 17:24:53 -04:00
|
|
|
* @param total_weight return-by-reference the total weight of the new block
|
2016-03-24 20:03:02 -04:00
|
|
|
* @param fee return-by-reference the total of fees from the included transactions
|
2017-03-28 10:55:38 -04:00
|
|
|
* @param expected_reward return-by-reference the total reward awarded to the miner finding this block, including transaction fees
|
2017-01-06 15:34:37 -05:00
|
|
|
* @param version hard fork version to use for consensus rules
|
2016-03-24 20:03:02 -04:00
|
|
|
*
|
|
|
|
* @return true
|
|
|
|
*/
|
2018-07-18 17:24:53 -04:00
|
|
|
bool fill_block_template(block &bl, size_t median_weight, uint64_t already_generated_coins, size_t &total_weight, uint64_t &fee, uint64_t &expected_reward, uint8_t version);
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief get a list of all transactions in the pool
|
|
|
|
*
|
|
|
|
* @param txs return-by-reference the list of transactions
|
2019-11-02 16:36:03 -04:00
|
|
|
* @param include_sensitive return stempool, anonymity-pool, and unrelayed txes
|
2017-11-08 07:06:41 -05:00
|
|
|
*
|
2016-03-24 20:03:02 -04:00
|
|
|
*/
|
2019-11-02 16:36:03 -04:00
|
|
|
void get_transactions(std::vector<transaction>& txs, bool include_sensitive = false) const;
|
2016-03-24 20:03:02 -04:00
|
|
|
|
2017-03-22 14:03:23 -04:00
|
|
|
/**
|
|
|
|
* @brief get a list of all transaction hashes in the pool
|
|
|
|
*
|
|
|
|
* @param txs return-by-reference the list of transactions
|
2019-11-02 16:36:03 -04:00
|
|
|
* @param include_sensitive return stempool, anonymity-pool, and unrelayed txes
|
2017-11-08 07:06:41 -05:00
|
|
|
*
|
2017-03-22 14:03:23 -04:00
|
|
|
*/
|
2019-11-02 16:36:03 -04:00
|
|
|
void get_transaction_hashes(std::vector<crypto::hash>& txs, bool include_sensitive = false) const;
|
2017-03-22 14:03:23 -04:00
|
|
|
|
2017-08-26 11:23:31 -04:00
|
|
|
/**
|
2018-07-18 17:24:53 -04:00
|
|
|
* @brief get (weight, fee, receive time) for all transaction in the pool
|
2017-08-26 11:23:31 -04:00
|
|
|
*
|
|
|
|
* @param txs return-by-reference that data
|
2019-11-02 16:36:03 -04:00
|
|
|
* @param include_sensitive return stempool, anonymity-pool, and unrelayed txes
|
2017-11-08 07:06:41 -05:00
|
|
|
*
|
2017-08-26 11:23:31 -04:00
|
|
|
*/
|
2019-11-02 16:36:03 -04:00
|
|
|
void get_transaction_backlog(std::vector<tx_backlog_entry>& backlog, bool include_sensitive = false) const;
|
2017-08-26 11:23:31 -04:00
|
|
|
|
2017-05-31 14:11:56 -04:00
|
|
|
/**
|
|
|
|
* @brief get a summary statistics of all transaction hashes in the pool
|
|
|
|
*
|
|
|
|
* @param stats return-by-reference the pool statistics
|
2019-11-02 16:36:03 -04:00
|
|
|
* @param include_sensitive return stempool, anonymity-pool, and unrelayed txes
|
2017-11-08 07:06:41 -05:00
|
|
|
*
|
2017-05-31 14:11:56 -04:00
|
|
|
*/
|
2019-11-02 16:36:03 -04:00
|
|
|
void get_transaction_stats(struct txpool_stats& stats, bool include_sensitive = false) const;
|
2017-05-31 14:11:56 -04:00
|
|
|
|
2016-03-24 20:03:02 -04:00
|
|
|
/**
|
|
|
|
* @brief get information about all transactions and key images in the pool
|
|
|
|
*
|
|
|
|
* see documentation on tx_info and spent_key_image_info for more details
|
|
|
|
*
|
|
|
|
* @param tx_infos return-by-reference the transactions' information
|
|
|
|
* @param key_image_infos return-by-reference the spent key images' information
|
2019-11-02 16:36:03 -04:00
|
|
|
* @param include_sensitive_data return stempool, anonymity-pool, and unrelayed
|
|
|
|
* txes and fields that are sensitive to the node privacy
|
2016-03-24 20:03:02 -04:00
|
|
|
*
|
|
|
|
* @return true
|
|
|
|
*/
|
2019-11-02 16:36:03 -04:00
|
|
|
bool get_transactions_and_spent_keys_info(std::vector<tx_info>& tx_infos, std::vector<spent_key_image_info>& key_image_infos, bool include_sensitive_data = false) const;
|
2016-03-24 20:03:02 -04:00
|
|
|
|
2017-09-05 12:20:27 -04:00
|
|
|
/**
|
|
|
|
* @brief get information about all transactions and key images in the pool
|
|
|
|
*
|
|
|
|
* see documentation on tx_in_pool and key_images_with_tx_hashes for more details
|
|
|
|
*
|
|
|
|
* @param tx_infos [out] the transactions' information
|
|
|
|
* @param key_image_infos [out] the spent key images' information
|
|
|
|
*
|
|
|
|
* @return true
|
|
|
|
*/
|
|
|
|
bool get_pool_for_rpc(std::vector<cryptonote::rpc::tx_in_pool>& tx_infos, cryptonote::rpc::key_images_with_tx_hashes& key_image_infos) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief check for presence of key images in the pool
|
|
|
|
*
|
|
|
|
* @param key_images [in] vector of key images to check
|
|
|
|
* @param spent [out] vector of bool to return
|
|
|
|
*
|
|
|
|
* @return true
|
|
|
|
*/
|
2019-10-05 23:28:30 -04:00
|
|
|
bool check_for_key_images(const std::vector<crypto::key_image>& key_images, std::vector<bool>& spent) const;
|
2017-09-05 12:20:27 -04:00
|
|
|
|
2016-03-24 20:03:02 -04:00
|
|
|
/**
|
|
|
|
* @brief get a specific transaction from the pool
|
|
|
|
*
|
|
|
|
* @param h the hash of the transaction to get
|
2017-05-14 09:06:55 -04:00
|
|
|
* @param tx return-by-reference the transaction blob requested
|
2019-11-02 16:36:03 -04:00
|
|
|
* @param tx_relay last relay method us
|
2016-03-24 20:03:02 -04:00
|
|
|
*
|
|
|
|
* @return true if the transaction is found, otherwise false
|
|
|
|
*/
|
2019-11-02 16:36:03 -04:00
|
|
|
bool get_transaction(const crypto::hash& h, cryptonote::blobdata& txblob, relay_category tx_category) const;
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief get a list of all relayable transactions and their hashes
|
|
|
|
*
|
|
|
|
* "relayable" in this case means:
|
|
|
|
* nonzero fee
|
|
|
|
* hasn't been relayed too recently
|
|
|
|
* isn't old enough that relaying it is considered harmful
|
2017-11-08 07:06:41 -05:00
|
|
|
* Note a transaction can be "relayable" even if do_not_relay is true
|
2016-03-24 20:03:02 -04:00
|
|
|
*
|
|
|
|
* @param txs return-by-reference the transactions and their hashes
|
|
|
|
*
|
|
|
|
* @return true
|
|
|
|
*/
|
2019-11-02 16:36:03 -04:00
|
|
|
bool get_relayable_transactions(std::vector<std::tuple<crypto::hash, cryptonote::blobdata, relay_method>>& txs) const;
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief tell the pool that certain transactions were just relayed
|
|
|
|
*
|
2019-11-02 16:36:03 -04:00
|
|
|
* @param hashes list of tx hashes that are about to be relayed
|
|
|
|
* @param tx_relay update how the tx left this node
|
2016-03-24 20:03:02 -04:00
|
|
|
*/
|
2019-11-02 16:36:03 -04:00
|
|
|
void set_relayed(epee::span<const crypto::hash> hashes, relay_method tx_relay);
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief get the total number of transactions in the pool
|
|
|
|
*
|
|
|
|
* @return the number of transactions in the pool
|
|
|
|
*/
|
2019-11-02 16:36:03 -04:00
|
|
|
size_t get_transactions_count(bool include_sensitive = false) const;
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief get a string containing human-readable pool information
|
|
|
|
*
|
|
|
|
* @param short_format whether to use a shortened format for the info
|
|
|
|
*
|
|
|
|
* @return the string
|
|
|
|
*/
|
2014-07-17 10:31:44 -04:00
|
|
|
std::string print_pool(bool short_format) const;
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief remove transactions from the pool which are no longer valid
|
|
|
|
*
|
|
|
|
* With new versions of the currency, what conditions render a transaction
|
|
|
|
* invalid may change. This function clears those which were received
|
|
|
|
* before a version change and no longer conform to requirements.
|
|
|
|
*
|
|
|
|
* @param version the version the transactions must conform to
|
|
|
|
*
|
|
|
|
* @return the number of transactions removed
|
|
|
|
*/
|
2016-01-29 10:09:17 -05:00
|
|
|
size_t validate(uint8_t version);
|
2014-03-03 17:07:58 -05:00
|
|
|
|
2016-04-17 06:04:01 -04:00
|
|
|
/**
|
|
|
|
* @brief return the cookie
|
|
|
|
*
|
|
|
|
* @return the cookie
|
|
|
|
*/
|
|
|
|
uint64_t cookie() const { return m_cookie; }
|
|
|
|
|
2018-01-29 16:20:24 -05:00
|
|
|
/**
|
2018-07-18 17:24:53 -04:00
|
|
|
* @brief get the cumulative txpool weight in bytes
|
2018-01-29 16:20:24 -05:00
|
|
|
*
|
2018-07-18 17:24:53 -04:00
|
|
|
* @return the cumulative txpool weight in bytes
|
2018-01-29 16:20:24 -05:00
|
|
|
*/
|
2018-07-18 17:24:53 -04:00
|
|
|
size_t get_txpool_weight() const;
|
2018-01-29 16:20:24 -05:00
|
|
|
|
|
|
|
/**
|
2018-07-18 17:24:53 -04:00
|
|
|
* @brief set the max cumulative txpool weight in bytes
|
2018-01-29 16:20:24 -05:00
|
|
|
*
|
2018-07-18 17:24:53 -04:00
|
|
|
* @param bytes the max cumulative txpool weight in bytes
|
2018-01-29 16:20:24 -05:00
|
|
|
*/
|
2018-07-18 17:24:53 -04:00
|
|
|
void set_txpool_max_weight(size_t bytes);
|
2014-03-03 17:07:58 -05:00
|
|
|
|
2016-01-29 14:44:25 -05:00
|
|
|
#define CURRENT_MEMPOOL_ARCHIVE_VER 11
|
2018-07-18 17:24:53 -04:00
|
|
|
#define CURRENT_MEMPOOL_TX_DETAILS_ARCHIVE_VER 13
|
2014-03-03 17:07:58 -05:00
|
|
|
|
2016-03-24 20:03:02 -04:00
|
|
|
/**
|
|
|
|
* @brief information about a single transaction
|
|
|
|
*/
|
2014-03-03 17:07:58 -05:00
|
|
|
struct tx_details
|
|
|
|
{
|
2016-03-24 20:03:02 -04:00
|
|
|
transaction tx; //!< the transaction
|
|
|
|
size_t blob_size; //!< the transaction's size
|
2018-07-18 17:24:53 -04:00
|
|
|
size_t weight; //!< the transaction's weight
|
2016-03-24 20:03:02 -04:00
|
|
|
uint64_t fee; //!< the transaction's fee amount
|
|
|
|
crypto::hash max_used_block_id; //!< the hash of the highest block referenced by an input
|
|
|
|
uint64_t max_used_block_height; //!< the height of the highest block referenced by an input
|
|
|
|
|
|
|
|
//! whether or not the transaction has been in a block before
|
|
|
|
/*! if the transaction was returned to the pool from the blockchain
|
|
|
|
* due to a reorg, then this will be true
|
|
|
|
*/
|
|
|
|
bool kept_by_block;
|
|
|
|
|
|
|
|
//! the highest block the transaction referenced when last checking it failed
|
|
|
|
/*! if verifying a transaction's inputs fails, it's possible this is due
|
|
|
|
* to a reorg since it was created (if it used recently created outputs
|
|
|
|
* as inputs).
|
|
|
|
*/
|
|
|
|
uint64_t last_failed_height;
|
|
|
|
|
|
|
|
//! the hash of the highest block the transaction referenced when last checking it failed
|
|
|
|
/*! if verifying a transaction's inputs fails, it's possible this is due
|
|
|
|
* to a reorg since it was created (if it used recently created outputs
|
|
|
|
* as inputs).
|
|
|
|
*/
|
2014-03-03 17:07:58 -05:00
|
|
|
crypto::hash last_failed_id;
|
2015-11-20 19:26:48 -05:00
|
|
|
|
2016-03-24 20:03:02 -04:00
|
|
|
time_t receive_time; //!< the time when the transaction entered the pool
|
|
|
|
|
|
|
|
time_t last_relayed_time; //!< the last time the transaction was relayed to the network
|
|
|
|
bool relayed; //!< whether or not the transaction has been relayed to the network
|
2017-01-14 08:01:21 -05:00
|
|
|
bool do_not_relay; //!< to avoid relay this transaction to the network
|
2017-09-22 08:57:20 -04:00
|
|
|
|
|
|
|
bool double_spend_seen; //!< true iff another tx was seen double spending this one
|
2014-03-03 17:07:58 -05:00
|
|
|
};
|
|
|
|
|
2019-09-17 13:03:28 -04:00
|
|
|
/**
|
|
|
|
* @brief get infornation about a single transaction
|
|
|
|
*/
|
|
|
|
bool get_transaction_info(const crypto::hash &txid, tx_details &td) const;
|
|
|
|
|
2019-11-26 19:09:25 -05:00
|
|
|
/**
|
|
|
|
* @brief get transactions not in the passed set
|
|
|
|
*/
|
|
|
|
bool get_complement(const std::vector<crypto::hash> &hashes, std::vector<cryptonote::blobdata> &txes) const;
|
|
|
|
|
2014-03-03 17:07:58 -05:00
|
|
|
private:
|
2016-03-24 20:03:02 -04:00
|
|
|
|
2017-05-14 09:06:55 -04:00
|
|
|
/**
|
|
|
|
* @brief insert key images into m_spent_key_images
|
|
|
|
*
|
|
|
|
* @return true on success, false on error
|
|
|
|
*/
|
2019-11-02 16:36:03 -04:00
|
|
|
bool insert_key_images(const transaction_prefix &tx, const crypto::hash &txid, relay_method tx_relay);
|
2017-05-14 09:06:55 -04:00
|
|
|
|
2016-03-24 20:03:02 -04:00
|
|
|
/**
|
|
|
|
* @brief remove old transactions from the pool
|
|
|
|
*
|
|
|
|
* After a certain time, it is assumed that a transaction which has not
|
|
|
|
* yet been mined will likely not be mined. These transactions are removed
|
|
|
|
* from the pool to avoid buildup.
|
|
|
|
*
|
|
|
|
* @return true
|
|
|
|
*/
|
2015-12-13 23:54:39 -05:00
|
|
|
bool remove_stuck_transactions();
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief check if a transaction in the pool has a given spent key image
|
|
|
|
*
|
|
|
|
* @param key_im the spent key image to look for
|
2020-03-26 08:24:45 -04:00
|
|
|
* @param txid hash of the new transaction where `key_im` was seen.
|
2016-03-24 20:03:02 -04:00
|
|
|
*
|
|
|
|
* @return true if the spent key image is present, otherwise false
|
|
|
|
*/
|
2020-03-26 08:24:45 -04:00
|
|
|
bool have_tx_keyimg_as_spent(const crypto::key_image& key_im, const crypto::hash& txid) const;
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief check if any spent key image in a transaction is in the pool
|
|
|
|
*
|
|
|
|
* Checks if any of the spent key images in a given transaction are present
|
|
|
|
* in any of the transactions in the transaction pool.
|
|
|
|
*
|
|
|
|
* @note see tx_pool::have_tx_keyimg_as_spent
|
|
|
|
*
|
|
|
|
* @param tx the transaction to check spent key images of
|
2020-03-26 08:24:45 -04:00
|
|
|
* @param txid hash of `tx`.
|
2016-03-24 20:03:02 -04:00
|
|
|
*
|
|
|
|
* @return true if any spent key images are present in the pool, otherwise false
|
|
|
|
*/
|
2020-03-26 08:24:45 -04:00
|
|
|
bool have_tx_keyimges_as_spent(const transaction& tx, const crypto::hash& txid) const;
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief forget a transaction's spent key images
|
|
|
|
*
|
|
|
|
* Spent key images are stored separately from transactions for
|
|
|
|
* convenience/speed, so this is part of the process of removing
|
|
|
|
* a transaction from the pool.
|
|
|
|
*
|
|
|
|
* @param tx the transaction
|
2018-09-16 14:30:39 -04:00
|
|
|
* @param txid the transaction's hash
|
2016-03-24 20:03:02 -04:00
|
|
|
*
|
|
|
|
* @return false if any key images to be removed cannot be found, otherwise true
|
|
|
|
*/
|
2018-09-16 14:30:39 -04:00
|
|
|
bool remove_transaction_keyimages(const transaction_prefix& tx, const crypto::hash &txid);
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief check if any of a transaction's spent key images are present in a given set
|
|
|
|
*
|
|
|
|
* @param kic the set of key images to check against
|
|
|
|
* @param tx the transaction to check
|
|
|
|
*
|
|
|
|
* @return true if any key images present in the set, otherwise false
|
|
|
|
*/
|
2018-09-16 14:30:39 -04:00
|
|
|
static bool have_key_images(const std::unordered_set<crypto::key_image>& kic, const transaction_prefix& tx);
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief append the key images from a transaction to the given set
|
|
|
|
*
|
|
|
|
* @param kic the set of key images to append to
|
|
|
|
* @param tx the transaction
|
|
|
|
*
|
|
|
|
* @return false if any append fails, otherwise true
|
|
|
|
*/
|
2018-09-16 14:30:39 -04:00
|
|
|
static bool append_key_images(std::unordered_set<crypto::key_image>& kic, const transaction_prefix& tx);
|
2014-07-17 10:27:37 -04:00
|
|
|
|
2016-03-24 20:03:02 -04:00
|
|
|
/**
|
|
|
|
* @brief check if a transaction is a valid candidate for inclusion in a block
|
|
|
|
*
|
|
|
|
* @param txd the transaction to check (and info about it)
|
2018-06-24 05:46:57 -04:00
|
|
|
* @param txid the txid of the transaction to check
|
2018-06-01 16:50:46 -04:00
|
|
|
* @param txblob the transaction blob to check
|
2018-10-31 08:14:30 -04:00
|
|
|
* @param tx the parsed transaction, if successful
|
2016-03-24 20:03:02 -04:00
|
|
|
*
|
|
|
|
* @return true if the transaction is good to go, otherwise false
|
|
|
|
*/
|
2018-09-16 14:30:39 -04:00
|
|
|
bool is_transaction_ready_to_go(txpool_tx_meta_t& txd, const crypto::hash &txid, const cryptonote::blobdata &txblob, transaction&tx) const;
|
2016-03-24 20:03:02 -04:00
|
|
|
|
2017-09-22 08:57:20 -04:00
|
|
|
/**
|
|
|
|
* @brief mark all transactions double spending the one passed
|
|
|
|
*/
|
|
|
|
void mark_double_spend(const transaction &tx);
|
|
|
|
|
2018-01-29 16:20:24 -05:00
|
|
|
/**
|
|
|
|
* @brief prune lowest fee/byte txes till we're not above bytes
|
|
|
|
*
|
2018-07-18 17:24:53 -04:00
|
|
|
* if bytes is 0, use m_txpool_max_weight
|
2018-01-29 16:20:24 -05:00
|
|
|
*/
|
|
|
|
void prune(size_t bytes = 0);
|
|
|
|
|
2016-03-24 20:03:02 -04:00
|
|
|
//TODO: confirm the below comments and investigate whether or not this
|
|
|
|
// is the desired behavior
|
|
|
|
//! map key images to transactions which spent them
|
|
|
|
/*! this seems odd, but it seems that multiple transactions can exist
|
|
|
|
* in the pool which both have the same spent key. This would happen
|
|
|
|
* in the event of a reorg where someone creates a new/different
|
|
|
|
* transaction on the assumption that the original will not be in a
|
|
|
|
* block again.
|
|
|
|
*/
|
2019-11-02 16:36:03 -04:00
|
|
|
typedef std::unordered_map<crypto::key_image, std::unordered_set<crypto::hash>> key_images_container;
|
2014-03-03 17:07:58 -05:00
|
|
|
|
2016-09-15 16:36:16 -04:00
|
|
|
#if defined(DEBUG_CREATE_BLOCK_TEMPLATE)
|
|
|
|
public:
|
|
|
|
#endif
|
2016-03-24 20:03:02 -04:00
|
|
|
mutable epee::critical_section m_transactions_lock; //!< lock for the pool
|
2016-09-15 16:36:16 -04:00
|
|
|
#if defined(DEBUG_CREATE_BLOCK_TEMPLATE)
|
|
|
|
private:
|
|
|
|
#endif
|
2016-03-24 20:03:02 -04:00
|
|
|
|
|
|
|
//! container for spent key images from the transactions in the pool
|
|
|
|
key_images_container m_spent_key_images;
|
|
|
|
|
|
|
|
//TODO: this time should be a named constant somewhere, not hard-coded
|
|
|
|
//! interval on which to check for stale/"stuck" transactions
|
2014-06-15 03:48:13 -04:00
|
|
|
epee::math_helper::once_a_time_seconds<30> m_remove_stuck_tx_interval;
|
2014-03-03 17:07:58 -05:00
|
|
|
|
2016-03-24 20:03:02 -04:00
|
|
|
//TODO: look into doing this better
|
2017-01-13 10:08:37 -05:00
|
|
|
//!< container for transactions organized by fee per size and receive time
|
|
|
|
sorted_tx_container m_txs_by_fee_and_receive_time;
|
2015-05-13 20:27:06 -04:00
|
|
|
|
2016-04-17 06:04:01 -04:00
|
|
|
std::atomic<uint64_t> m_cookie; //!< incremented at each change
|
|
|
|
|
2016-03-24 20:03:02 -04:00
|
|
|
/**
|
|
|
|
* @brief get an iterator to a transaction in the sorted container
|
|
|
|
*
|
|
|
|
* @param id the hash of the transaction to look for
|
|
|
|
*
|
|
|
|
* @return an iterator, possibly to the end of the container if not found
|
|
|
|
*/
|
2015-05-13 20:27:06 -04:00
|
|
|
sorted_tx_container::iterator find_tx_in_sorted_container(const crypto::hash& id) const;
|
2015-04-30 01:02:12 -04:00
|
|
|
|
2018-06-24 05:46:57 -04:00
|
|
|
//! cache/call Blockchain::check_tx_inputs results
|
|
|
|
bool check_tx_inputs(const std::function<cryptonote::transaction&(void)> &get_tx, const crypto::hash &txid, uint64_t &max_used_block_height, crypto::hash &max_used_block_id, tx_verification_context &tvc, bool kept_by_block = false) const;
|
|
|
|
|
2016-03-24 20:03:02 -04:00
|
|
|
//! transactions which are unlikely to be included in blocks
|
|
|
|
/*! These transactions are kept in RAM in case they *are* included
|
|
|
|
* in a block eventually, but this container is not saved to disk.
|
|
|
|
*/
|
2016-01-29 12:15:09 -05:00
|
|
|
std::unordered_set<crypto::hash> m_timed_out_transactions;
|
|
|
|
|
2016-03-24 20:03:02 -04:00
|
|
|
Blockchain& m_blockchain; //!< reference to the Blockchain object
|
2018-01-29 16:20:24 -05:00
|
|
|
|
2018-07-18 17:24:53 -04:00
|
|
|
size_t m_txpool_max_weight;
|
|
|
|
size_t m_txpool_weight;
|
2018-06-24 05:46:57 -04:00
|
|
|
|
|
|
|
mutable std::unordered_map<crypto::hash, std::tuple<bool, tx_verification_context, uint64_t, crypto::hash>> m_input_cache;
|
2018-10-31 09:52:16 -04:00
|
|
|
|
|
|
|
std::unordered_map<crypto::hash, transaction> m_parsed_tx_cache;
|
2014-03-03 17:07:58 -05: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 03:48:13 -04:00
|
|
|
ar & td.receive_time;
|
2015-11-20 19:26:48 -05:00
|
|
|
ar & td.last_relayed_time;
|
|
|
|
ar & td.relayed;
|
2016-01-29 14:44:25 -05:00
|
|
|
if (version < 11)
|
|
|
|
return;
|
|
|
|
ar & td.kept_by_block;
|
2017-01-14 08:01:21 -05:00
|
|
|
if (version < 12)
|
|
|
|
return;
|
|
|
|
ar & td.do_not_relay;
|
2018-07-18 17:24:53 -04:00
|
|
|
if (version < 13)
|
|
|
|
return;
|
|
|
|
ar & td.weight;
|
2014-03-03 17:07:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BOOST_CLASS_VERSION(cryptonote::tx_memory_pool, CURRENT_MEMPOOL_ARCHIVE_VER)
|
2016-04-05 19:11:31 -04:00
|
|
|
BOOST_CLASS_VERSION(cryptonote::tx_memory_pool::tx_details, CURRENT_MEMPOOL_TX_DETAILS_ARCHIVE_VER)
|
2014-03-03 17:07:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
|