Electroneum
Classes | Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
cryptonote::tx_memory_pool Class Reference

Transaction pool, handles transactions which are not part of a block. More...

#include <tx_pool.h>

Inheritance diagram for cryptonote::tx_memory_pool:
Inheritance graph
[legend]
Collaboration diagram for cryptonote::tx_memory_pool:
Collaboration graph
[legend]

Classes

struct  tx_details
 information about a single transaction More...
 

Public Member Functions

 tx_memory_pool (Blockchain &bchs)
 Constructor. More...
 
bool add_tx (transaction &tx, const crypto::hash &id, const cryptonote::blobdata &blob, size_t tx_weight, tx_verification_context &tvc, bool kept_by_block, bool relayed, bool do_not_relay, uint8_t version)
 
bool add_tx (transaction &tx, tx_verification_context &tvc, bool kept_by_block, bool relayed, bool do_not_relay, uint8_t version)
 add a transaction to the transaction pool More...
 
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)
 takes a transaction with the given hash from the pool More...
 
bool have_tx (const crypto::hash &id) const
 checks if the pool has a transaction with the given hash More...
 
bool on_blockchain_inc (uint64_t new_block_height, const crypto::hash &top_block_id)
 action to take when notified of a block added to the blockchain More...
 
bool on_blockchain_dec (uint64_t new_block_height, const crypto::hash &top_block_id)
 action to take when notified of a block removed from the blockchain More...
 
void on_idle ()
 action to take periodically More...
 
void lock () const
 locks the transaction pool More...
 
void unlock () const
 unlocks the transaction pool More...
 
bool init (size_t max_txpool_weight=0)
 loads pool state (if any) from disk, and initializes pool More...
 
bool deinit ()
 attempts to save the transaction pool state to disk More...
 
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)
 Chooses transactions for a block to include. More...
 
void get_transactions (std::vector< transaction > &txs, bool include_unrelayed_txes=true) const
 get a list of all transactions in the pool More...
 
void get_transaction_hashes (std::vector< crypto::hash > &txs, bool include_unrelayed_txes=true) const
 get a list of all transaction hashes in the pool More...
 
void get_transaction_backlog (std::vector< tx_backlog_entry > &backlog, bool include_unrelayed_txes=true) const
 get (weight, fee, receive time) for all transaction in the pool More...
 
void get_transaction_stats (struct txpool_stats &stats, bool include_unrelayed_txes=true) const
 get a summary statistics of all transaction hashes in the pool More...
 
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=true) const
 get information about all transactions and key images in the pool More...
 
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
 get information about all transactions and key images in the pool More...
 
bool check_for_key_images (const std::vector< crypto::key_image > &key_images, std::vector< bool > spent) const
 check for presence of key images in the pool More...
 
bool get_transaction (const crypto::hash &h, cryptonote::blobdata &txblob) const
 get a specific transaction from the pool More...
 
bool get_relayable_transactions (std::vector< std::pair< crypto::hash, cryptonote::blobdata >> &txs) const
 get a list of all relayable transactions and their hashes More...
 
void set_relayed (const std::vector< std::pair< crypto::hash, cryptonote::blobdata >> &txs)
 tell the pool that certain transactions were just relayed More...
 
size_t get_transactions_count (bool include_unrelayed_txes=true) const
 get the total number of transactions in the pool More...
 
std::string print_pool (bool short_format) const
 get a string containing human-readable pool information More...
 
size_t validate (uint8_t version)
 remove transactions from the pool which are no longer valid More...
 
uint64_t cookie () const
 return the cookie More...
 
size_t get_txpool_weight () const
 get the cumulative txpool weight in bytes More...
 
void set_txpool_max_weight (size_t bytes)
 set the max cumulative txpool weight in bytes More...
 

Private Types

typedef std::unordered_map< crypto::key_image, std::unordered_set< crypto::hash > > key_images_container
 map key images to transactions which spent them More...
 

Private Member Functions

bool insert_key_images (const transaction_prefix &tx, const crypto::hash &txid, bool kept_by_block)
 insert key images into m_spent_key_images More...
 
bool remove_stuck_transactions ()
 remove old transactions from the pool More...
 
bool have_tx_keyimg_as_spent (const crypto::key_image &key_im) const
 check if a transaction in the pool has a given spent key image More...
 
bool have_tx_keyimges_as_spent (const transaction &tx) const
 check if any spent key image in a transaction is in the pool More...
 
bool remove_transaction_keyimages (const transaction_prefix &tx, const crypto::hash &txid)
 forget a transaction's spent key images More...
 
bool is_transaction_ready_to_go (txpool_tx_meta_t &txd, const crypto::hash &txid, const cryptonote::blobdata &txblob, transaction &tx) const
 check if a transaction is a valid candidate for inclusion in a block More...
 
void mark_double_spend (const transaction &tx)
 mark all transactions double spending the one passed More...
 
void prune (size_t bytes=0)
 prune lowest fee/byte txes till we're not above bytes More...
 
sorted_tx_container::iterator find_tx_in_sorted_container (const crypto::hash &id) const
 get an iterator to a transaction in the sorted container More...
 
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
 cache/call Blockchain::check_tx_inputs results More...
 

Static Private Member Functions

static bool have_key_images (const std::unordered_set< crypto::key_image > &kic, const transaction_prefix &tx)
 check if any of a transaction's spent key images are present in a given set More...
 
static bool append_key_images (std::unordered_set< crypto::key_image > &kic, const transaction_prefix &tx)
 append the key images from a transaction to the given set More...
 

Private Attributes

epee::critical_section m_transactions_lock
 lock for the pool More...
 
key_images_container m_spent_key_images
 container for spent key images from the transactions in the pool More...
 
epee::math_helper::once_a_time_seconds< 30 > m_remove_stuck_tx_interval
 interval on which to check for stale/"stuck" transactions More...
 
sorted_tx_container m_txs_by_fee_and_receive_time
 < container for transactions organized by fee per size and receive time More...
 
std::atomic< uint64_t > m_cookie
 incremented at each change More...
 
std::unordered_set< crypto::hashm_timed_out_transactions
 transactions which are unlikely to be included in blocks More...
 
Blockchainm_blockchain
 reference to the Blockchain object More...
 
size_t m_txpool_max_weight
 
size_t m_txpool_weight
 
std::unordered_map< crypto::hash, std::tuple< bool, tx_verification_context, uint64_t, crypto::hash > > m_input_cache
 
std::unordered_map< crypto::hash, transactionm_parsed_tx_cache
 

Detailed Description

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 organizing the transactions by fee per weight unit 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

Member Typedef Documentation

◆ key_images_container

typedef std::unordered_map<crypto::key_image, std::unordered_set<crypto::hash> > cryptonote::tx_memory_pool::key_images_container
private

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.

Constructor & Destructor Documentation

◆ tx_memory_pool()

cryptonote::tx_memory_pool::tx_memory_pool ( Blockchain bchs)

Constructor.

Parameters
bchsa Blockchain class instance, for getting chain info

Member Function Documentation

◆ add_tx() [1/2]

bool cryptonote::tx_memory_pool::add_tx ( transaction tx,
const crypto::hash id,
const cryptonote::blobdata blob,
size_t  tx_weight,
tx_verification_context tvc,
bool  kept_by_block,
bool  relayed,
bool  do_not_relay,
uint8_t  version 
)

Parameters
idthe transaction's hash
tx_weightthe transaction's weight

◆ add_tx() [2/2]

bool cryptonote::tx_memory_pool::add_tx ( transaction tx,
tx_verification_context tvc,
bool  kept_by_block,
bool  relayed,
bool  do_not_relay,
uint8_t  version 
)

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

Parameters
txthe transaction to be added
tvcreturn-by-reference status about the transaction verification
kept_by_blockhas this transaction been in a block?
relayedwas this transaction from the network or a local client?
do_not_relayto avoid relaying the transaction to the network
versionthe version used to create the transaction
Returns
true if the transaction passes validations, otherwise false

◆ append_key_images()

bool cryptonote::tx_memory_pool::append_key_images ( std::unordered_set< crypto::key_image > &  kic,
const transaction_prefix tx 
)
staticprivate

append the key images from a transaction to the given set

Parameters
kicthe set of key images to append to
txthe transaction
Returns
false if any append fails, otherwise true

◆ check_for_key_images()

bool cryptonote::tx_memory_pool::check_for_key_images ( const std::vector< crypto::key_image > &  key_images,
std::vector< bool spent 
) const

check for presence of key images in the pool

Parameters
key_images[in] vector of key images to check
spent[out] vector of bool to return
Returns
true

◆ check_tx_inputs()

bool cryptonote::tx_memory_pool::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
private

cache/call Blockchain::check_tx_inputs results

◆ cookie()

uint64_t cryptonote::tx_memory_pool::cookie ( ) const
inline

return the cookie

Returns
the cookie

◆ deinit()

bool cryptonote::tx_memory_pool::deinit ( )

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.

Returns
true in most cases (see above)

◆ fill_block_template()

bool cryptonote::tx_memory_pool::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 
)

Chooses transactions for a block to include.

Parameters
blreturn-by-reference the block to fill in with transactions
median_weightthe current median block weight
already_generated_coinsthe current total number of coins "minted"
total_weightreturn-by-reference the total weight of the new block
feereturn-by-reference the total of fees from the included transactions
expected_rewardreturn-by-reference the total reward awarded to the miner finding this block, including transaction fees
versionhard fork version to use for consensus rules
Returns
true

◆ find_tx_in_sorted_container()

sorted_tx_container::iterator cryptonote::tx_memory_pool::find_tx_in_sorted_container ( const crypto::hash id) const
private

get an iterator to a transaction in the sorted container

Parameters
idthe hash of the transaction to look for
Returns
an iterator, possibly to the end of the container if not found

◆ get_pool_for_rpc()

bool cryptonote::tx_memory_pool::get_pool_for_rpc ( std::vector< cryptonote::rpc::tx_in_pool > &  tx_infos,
cryptonote::rpc::key_images_with_tx_hashes key_image_infos 
) const

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

Parameters
tx_infos[out] the transactions' information
key_image_infos[out] the spent key images' information
Returns
true

◆ get_relayable_transactions()

bool cryptonote::tx_memory_pool::get_relayable_transactions ( std::vector< std::pair< crypto::hash, cryptonote::blobdata >> &  txs) const

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 Note a transaction can be "relayable" even if do_not_relay is true

Parameters
txsreturn-by-reference the transactions and their hashes
Returns
true

◆ get_transaction()

bool cryptonote::tx_memory_pool::get_transaction ( const crypto::hash h,
cryptonote::blobdata txblob 
) const

get a specific transaction from the pool

Parameters
hthe hash of the transaction to get
txreturn-by-reference the transaction blob requested
Returns
true if the transaction is found, otherwise false

◆ get_transaction_backlog()

void cryptonote::tx_memory_pool::get_transaction_backlog ( std::vector< tx_backlog_entry > &  backlog,
bool  include_unrelayed_txes = true 
) const

get (weight, fee, receive time) for all transaction in the pool

Parameters
txsreturn-by-reference that data
include_unrelayed_txesinclude unrelayed txes in the result

◆ get_transaction_hashes()

void cryptonote::tx_memory_pool::get_transaction_hashes ( std::vector< crypto::hash > &  txs,
bool  include_unrelayed_txes = true 
) const

get a list of all transaction hashes in the pool

Parameters
txsreturn-by-reference the list of transactions
include_unrelayed_txesinclude unrelayed txes in the result

◆ get_transaction_stats()

void cryptonote::tx_memory_pool::get_transaction_stats ( struct txpool_stats stats,
bool  include_unrelayed_txes = true 
) const

get a summary statistics of all transaction hashes in the pool

Parameters
statsreturn-by-reference the pool statistics
include_unrelayed_txesinclude unrelayed txes in the result

◆ get_transactions()

void cryptonote::tx_memory_pool::get_transactions ( std::vector< transaction > &  txs,
bool  include_unrelayed_txes = true 
) const

get a list of all transactions in the pool

Parameters
txsreturn-by-reference the list of transactions
include_unrelayed_txesinclude unrelayed txes in the result

◆ get_transactions_and_spent_keys_info()

bool cryptonote::tx_memory_pool::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 = true 
) const

get information about all transactions and key images in the pool

see documentation on tx_info and spent_key_image_info for more details

Parameters
tx_infosreturn-by-reference the transactions' information
key_image_infosreturn-by-reference the spent key images' information
include_sensitive_datainclude unrelayed txes and fields that are sensitive to the node privacy
Returns
true

◆ get_transactions_count()

size_t cryptonote::tx_memory_pool::get_transactions_count ( bool  include_unrelayed_txes = true) const

get the total number of transactions in the pool

Returns
the number of transactions in the pool

◆ get_txpool_weight()

size_t cryptonote::tx_memory_pool::get_txpool_weight ( ) const

get the cumulative txpool weight in bytes

Returns
the cumulative txpool weight in bytes

◆ have_key_images()

bool cryptonote::tx_memory_pool::have_key_images ( const std::unordered_set< crypto::key_image > &  kic,
const transaction_prefix tx 
)
staticprivate

check if any of a transaction's spent key images are present in a given set

Parameters
kicthe set of key images to check against
txthe transaction to check
Returns
true if any key images present in the set, otherwise false

◆ have_tx()

bool cryptonote::tx_memory_pool::have_tx ( const crypto::hash id) const

checks if the pool has a transaction with the given hash

Parameters
idthe hash to look for
Returns
true if the transaction is in the pool, otherwise false

◆ have_tx_keyimg_as_spent()

bool cryptonote::tx_memory_pool::have_tx_keyimg_as_spent ( const crypto::key_image key_im) const
private

check if a transaction in the pool has a given spent key image

Parameters
key_imthe spent key image to look for
Returns
true if the spent key image is present, otherwise false

◆ have_tx_keyimges_as_spent()

bool cryptonote::tx_memory_pool::have_tx_keyimges_as_spent ( const transaction tx) const
private

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
Parameters
txthe transaction to check spent key images of
Returns
true if any spent key images are present in the pool, otherwise false

◆ init()

bool cryptonote::tx_memory_pool::init ( size_t  max_txpool_weight = 0)

loads pool state (if any) from disk, and initializes pool

Parameters
max_txpool_weightthe max weight in bytes
Returns
true

◆ insert_key_images()

bool cryptonote::tx_memory_pool::insert_key_images ( const transaction_prefix tx,
const crypto::hash txid,
bool  kept_by_block 
)
private

insert key images into m_spent_key_images

Returns
true on success, false on error

◆ is_transaction_ready_to_go()

bool cryptonote::tx_memory_pool::is_transaction_ready_to_go ( txpool_tx_meta_t txd,
const crypto::hash txid,
const cryptonote::blobdata txblob,
transaction tx 
) const
private

check if a transaction is a valid candidate for inclusion in a block

Parameters
txdthe transaction to check (and info about it)
txidthe txid of the transaction to check
txblobthe transaction blob to check
txthe parsed transaction, if successful
Returns
true if the transaction is good to go, otherwise false

◆ lock()

void cryptonote::tx_memory_pool::lock ( ) const

locks the transaction pool

◆ mark_double_spend()

void cryptonote::tx_memory_pool::mark_double_spend ( const transaction tx)
private

mark all transactions double spending the one passed

◆ on_blockchain_dec()

bool cryptonote::tx_memory_pool::on_blockchain_dec ( uint64_t  new_block_height,
const crypto::hash top_block_id 
)

action to take when notified of a block removed from the blockchain

Currently does nothing

Parameters
new_block_heightthe height of the blockchain after the change
top_block_idthe hash of the new top block
Returns
true

◆ on_blockchain_inc()

bool cryptonote::tx_memory_pool::on_blockchain_inc ( uint64_t  new_block_height,
const crypto::hash top_block_id 
)

action to take when notified of a block added to the blockchain

Currently does nothing

Parameters
new_block_heightthe height of the blockchain after the change
top_block_idthe hash of the new top block
Returns
true

◆ on_idle()

void cryptonote::tx_memory_pool::on_idle ( )

action to take periodically

Currently checks transaction pool for stale ("stuck") transactions

◆ print_pool()

std::string cryptonote::tx_memory_pool::print_pool ( bool  short_format) const

get a string containing human-readable pool information

Parameters
short_formatwhether to use a shortened format for the info
Returns
the string

◆ prune()

void cryptonote::tx_memory_pool::prune ( size_t  bytes = 0)
private

prune lowest fee/byte txes till we're not above bytes

if bytes is 0, use m_txpool_max_weight

◆ remove_stuck_transactions()

bool cryptonote::tx_memory_pool::remove_stuck_transactions ( )
private

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.

Returns
true

◆ remove_transaction_keyimages()

bool cryptonote::tx_memory_pool::remove_transaction_keyimages ( const transaction_prefix tx,
const crypto::hash txid 
)
private

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.

Parameters
txthe transaction
txidthe transaction's hash
Returns
false if any key images to be removed cannot be found, otherwise true

◆ set_relayed()

void cryptonote::tx_memory_pool::set_relayed ( const std::vector< std::pair< crypto::hash, cryptonote::blobdata >> &  txs)

tell the pool that certain transactions were just relayed

Parameters
txsthe list of transactions (and their hashes)

◆ set_txpool_max_weight()

void cryptonote::tx_memory_pool::set_txpool_max_weight ( size_t  bytes)

set the max cumulative txpool weight in bytes

Parameters
bytesthe max cumulative txpool weight in bytes

◆ take_tx()

bool cryptonote::tx_memory_pool::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 
)

takes a transaction with the given hash from the pool

Parameters
idthe hash of the transaction
txreturn-by-reference the transaction taken
txblobreturn-by-reference the transaction as a blob
tx_weightreturn-by-reference the transaction's weight
feethe transaction fee
relayedreturn-by-reference was transaction relayed to us by the network?
do_not_relayreturn-by-reference is transaction not to be relayed to the network?
double_spend_seenreturn-by-reference was a double spend seen for that transaction?
Returns
true unless the transaction cannot be found in the pool

◆ unlock()

void cryptonote::tx_memory_pool::unlock ( ) const

unlocks the transaction pool

◆ validate()

size_t cryptonote::tx_memory_pool::validate ( uint8_t  version)

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.

Parameters
versionthe version the transactions must conform to
Returns
the number of transactions removed

Member Data Documentation

◆ m_blockchain

Blockchain& cryptonote::tx_memory_pool::m_blockchain
private

reference to the Blockchain object

◆ m_cookie

std::atomic<uint64_t> cryptonote::tx_memory_pool::m_cookie
private

incremented at each change

◆ m_input_cache

std::unordered_map<crypto::hash, std::tuple<bool, tx_verification_context, uint64_t, crypto::hash> > cryptonote::tx_memory_pool::m_input_cache
mutableprivate

◆ m_parsed_tx_cache

std::unordered_map<crypto::hash, transaction> cryptonote::tx_memory_pool::m_parsed_tx_cache
private

◆ m_remove_stuck_tx_interval

epee::math_helper::once_a_time_seconds<30> cryptonote::tx_memory_pool::m_remove_stuck_tx_interval
private

interval on which to check for stale/"stuck" transactions

◆ m_spent_key_images

key_images_container cryptonote::tx_memory_pool::m_spent_key_images
private

container for spent key images from the transactions in the pool

◆ m_timed_out_transactions

std::unordered_set<crypto::hash> cryptonote::tx_memory_pool::m_timed_out_transactions
private

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.

◆ m_transactions_lock

epee::critical_section cryptonote::tx_memory_pool::m_transactions_lock
mutableprivate

lock for the pool

◆ m_txpool_max_weight

size_t cryptonote::tx_memory_pool::m_txpool_max_weight
private

◆ m_txpool_weight

size_t cryptonote::tx_memory_pool::m_txpool_weight
private

◆ m_txs_by_fee_and_receive_time

sorted_tx_container cryptonote::tx_memory_pool::m_txs_by_fee_and_receive_time
private

< container for transactions organized by fee per size and receive time


The documentation for this class was generated from the following files: