Electroneum
tx_pool.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2019, The Electroneum Project
2 // Copyrights(c) 2014-2017, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31 
32 #pragma once
33 #include "include_base_utils.h"
34 
35 #include <set>
36 #include <unordered_map>
37 #include <unordered_set>
38 #include <queue>
39 #include <boost/serialization/version.hpp>
40 #include <boost/utility.hpp>
41 
42 #include "string_tools.h"
43 #include "syncobj.h"
44 #include "math_helper.h"
48 #include "crypto/hash.h"
50 
51 namespace cryptonote
52 {
53  class Blockchain;
54  /************************************************************************/
55  /* */
56  /************************************************************************/
57 
59  typedef std::pair<std::pair<double, std::time_t>, crypto::hash> tx_by_fee_and_receive_time_entry;
60 
61  class txCompare
62  {
63  public:
65  {
66  // sort by greatest first, not least
67  if (a.first.first > b.first.first) return true;
68  else if (a.first.first < b.first.first) return false;
69  else if (a.first.second < b.first.second) return true;
70  else if (a.first.second > b.first.second) return false;
71  else if (a.second != b.second) return true;
72  else return false;
73  }
74  };
75 
77  typedef std::set<tx_by_fee_and_receive_time_entry, txCompare> sorted_tx_container;
78 
93  class tx_memory_pool: boost::noncopyable
94  {
95  public:
101  tx_memory_pool(Blockchain& bchs);
102 
103 
110  bool add_tx(transaction &tx, const crypto::hash &id, size_t blob_size, tx_verification_context& tvc, bool kept_by_block, bool relayed, bool do_not_relay, uint8_t version);
111 
129  bool add_tx(transaction &tx, tx_verification_context& tvc, bool kept_by_block, bool relayed, bool do_not_relay, uint8_t version);
130 
143  bool take_tx(const crypto::hash &id, transaction &tx, size_t& blob_size, uint64_t& fee, bool &relayed, bool &do_not_relay);
144 
152  bool have_tx(const crypto::hash &id) const;
153 
164  bool on_blockchain_inc(uint64_t new_block_height, const crypto::hash& top_block_id);
165 
176  bool on_blockchain_dec(uint64_t new_block_height, const crypto::hash& top_block_id);
177 
183  void on_idle();
184 
188  void lock() const;
189 
193  void unlock() const;
194 
195  // load/store operations
196 
204  bool init();
205 
215  bool deinit();
216 
230  bool fill_block_template(block &bl, size_t median_size, uint64_t already_generated_coins, size_t &total_size, uint64_t &fee, uint64_t &expected_reward, uint8_t version);
231 
237  void get_transactions(std::list<transaction>& txs) const;
238 
244  void get_transaction_hashes(std::vector<crypto::hash>& txs) const;
245 
251  void get_transaction_backlog(std::vector<tx_backlog_entry>& backlog) const;
252 
258  void get_transaction_stats(struct txpool_stats& stats) const;
259 
270  bool get_transactions_and_spent_keys_info(std::vector<tx_info>& tx_infos, std::vector<spent_key_image_info>& key_image_infos) const;
271 
280  bool get_transaction(const crypto::hash& h, cryptonote::blobdata& txblob) const;
281 
294  bool get_relayable_transactions(std::list<std::pair<crypto::hash, cryptonote::blobdata>>& txs) const;
295 
301  void set_relayed(const std::list<std::pair<crypto::hash, cryptonote::blobdata>>& txs);
302 
308  size_t get_transactions_count() const;
309 
317  std::string print_pool(bool short_format) const;
318 
330  size_t validate(uint8_t version);
331 
337  uint64_t cookie() const { return m_cookie; }
338 
339 #define CURRENT_MEMPOOL_ARCHIVE_VER 11
340 #define CURRENT_MEMPOOL_TX_DETAILS_ARCHIVE_VER 12
341 
345  struct tx_details
346  {
348  size_t blob_size;
349  uint64_t fee;
352 
354 
358 
360 
365 
367 
372 
373  time_t receive_time;
374 
376  bool relayed;
378  };
379 
380  private:
381 
387  bool insert_key_images(const transaction &tx, bool kept_by_block);
388 
399 
407  bool have_tx_keyimg_as_spent(const crypto::key_image& key_im) const;
408 
421  bool have_tx_keyimges_as_spent(const transaction& tx) const;
422 
435 
444  static bool have_key_images(const std::unordered_set<crypto::key_image>& kic, const transaction& tx);
445 
454  static bool append_key_images(std::unordered_set<crypto::key_image>& kic, const transaction& tx);
455 
464 
465  //TODO: confirm the below comments and investigate whether or not this
466  // is the desired behavior
468 
474  typedef std::unordered_map<crypto::key_image, std::unordered_set<crypto::hash> > key_images_container;
475 
476 #if defined(DEBUG_CREATE_BLOCK_TEMPLATE)
477 public:
478 #endif
479  mutable epee::critical_section m_transactions_lock;
480 #if defined(DEBUG_CREATE_BLOCK_TEMPLATE)
481 private:
482 #endif
483 
486 
487  //TODO: this time should be a named constant somewhere, not hard-coded
489  epee::math_helper::once_a_time_seconds<30> m_remove_stuck_tx_interval;
490 
491  //TODO: look into doing this better
494 
495  std::atomic<uint64_t> m_cookie;
496 
504  sorted_tx_container::iterator find_tx_in_sorted_container(const crypto::hash& id) const;
505 
507 
510  std::unordered_set<crypto::hash> m_timed_out_transactions;
511 
513  };
514 }
515 
516 namespace boost
517 {
518  namespace serialization
519  {
520  template<class archive_t>
521  void serialize(archive_t & ar, cryptonote::tx_memory_pool::tx_details& td, const unsigned int version)
522  {
523  ar & td.blob_size;
524  ar & td.fee;
525  ar & td.tx;
526  ar & td.max_used_block_height;
527  ar & td.max_used_block_id;
528  ar & td.last_failed_height;
529  ar & td.last_failed_id;
530  ar & td.receive_time;
531  ar & td.last_relayed_time;
532  ar & td.relayed;
533  if (version < 11)
534  return;
535  ar & td.kept_by_block;
536  if (version < 12)
537  return;
538  ar & td.do_not_relay;
539  }
540  }
541 }
544 
545 
546 
void on_idle()
action to take periodically
Definition: binary_utils.h:37
#define CURRENT_MEMPOOL_ARCHIVE_VER
Definition: tx_pool.h:339
bool relayed
whether or not the transaction has been relayed to the network
Definition: tx_pool.h:376
void get_transaction_stats(struct txpool_stats &stats) const
get a summary statistics of all transaction hashes in the pool
uint64_t cookie() const
return the cookie
Definition: tx_pool.h:337
crypto::hash max_used_block_id
the hash of the highest block referenced by an input
Definition: tx_pool.h:350
crypto::hash last_failed_id
the hash of the highest block the transaction referenced when last checking it failed ...
Definition: tx_pool.h:371
uint64_t max_used_block_height
the height of the highest block referenced by an input
Definition: tx_pool.h:351
void get_transaction_hashes(std::vector< crypto::hash > &txs) const
get a list of all transaction hashes in the pool
Definition: tx_pool.h:61
bool operator()(const tx_by_fee_and_receive_time_entry &a, const tx_by_fee_and_receive_time_entry &b)
Definition: tx_pool.h:64
Definition: unordered_containers_boost_serialization.h:38
bool is_transaction_ready_to_go(txpool_tx_meta_t &txd, transaction &tx) const
check if a transaction is a valid candidate for inclusion in a block
Definition: cryptonote_basic.h:382
void serialize(Archive &a, std::unordered_map< h_key, hval > &x, const boost::serialization::version_type ver)
Definition: unordered_containers_boost_serialization.h:127
key_images_container m_spent_key_images
container for spent key images from the transactions in the pool
Definition: tx_pool.h:485
bool remove_stuck_transactions()
remove old transactions from the pool
epee::critical_section m_transactions_lock
lock for the pool
Definition: tx_pool.h:479
std::unordered_set< crypto::hash > m_timed_out_transactions
transactions which are unlikely to be included in blocks
Definition: tx_pool.h:510
transaction tx
the transaction
Definition: tx_pool.h:347
bool fill_block_template(block &bl, size_t median_size, uint64_t already_generated_coins, size_t &total_size, uint64_t &fee, uint64_t &expected_reward, uint8_t version)
Chooses transactions for a block to include.
sorted_tx_container::iterator find_tx_in_sorted_container(const crypto::hash &id) const
get an iterator to a transaction in the sorted container
bool do_not_relay
to avoid relay this transaction to the network
Definition: tx_pool.h:377
time_t receive_time
the time when the transaction entered the pool
Definition: tx_pool.h:373
void get_transaction_backlog(std::vector< tx_backlog_entry > &backlog) const
get (size, fee, receive time) for all transaction in the pool
bool get_transactions_and_spent_keys_info(std::vector< tx_info > &tx_infos, std::vector< spent_key_image_info > &key_image_infos) const
get information about all transactions and key images in the pool
#define CURRENT_MEMPOOL_TX_DETAILS_ARCHIVE_VER
Definition: tx_pool.h:340
BOOST_CLASS_VERSION(nodetool::node_server< cryptonote::t_cryptonote_protocol_handler< cryptonote::core > >, 1)
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:225
Definition: verification_context.h:38
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
size_t validate(uint8_t version)
remove transactions from the pool which are no longer valid
bool insert_key_images(const transaction &tx, bool kept_by_block)
insert key images into m_spent_key_images
void get_transactions(std::list< transaction > &txs) const
get a list of all transactions in the pool
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
size_t get_transactions_count() const
get the total number of transactions in the pool
std::pair< std::pair< double, std::time_t >, crypto::hash > tx_by_fee_and_receive_time_entry
pair of <transaction fee, transaction hash> for organization
Definition: tx_pool.h:53
size_t blob_size
the transaction&#39;s size
Definition: tx_pool.h:348
int b
Definition: base.py:1
uint64_t last_failed_height
the highest block the transaction referenced when last checking it failed
Definition: tx_pool.h:364
uint64_t fee
the transaction&#39;s fee amount
Definition: tx_pool.h:349
tx_memory_pool(Blockchain &bchs)
Constructor.
uint8_t version
Definition: blockchain.cpp:86
bool remove_transaction_keyimages(const transaction &tx)
forget a transaction&#39;s spent key images
Transaction pool, handles transactions which are not part of a block.
Definition: tx_pool.h:93
std::string blobdata
Definition: blobdatatype.h:36
bool init()
loads pool state (if any) from disk, and initializes pool
Definition: core_rpc_server_commands_defs.h:1123
bool add_tx(transaction &tx, const crypto::hash &id, size_t blob_size, tx_verification_context &tvc, bool kept_by_block, bool relayed, bool do_not_relay, uint8_t version)
void unlock() const
unlocks the transaction pool
bool deinit()
attempts to save the transaction pool state to disk
void set_relayed(const std::list< std::pair< crypto::hash, cryptonote::blobdata >> &txs)
tell the pool that certain transactions were just relayed
bool get_relayable_transactions(std::list< std::pair< crypto::hash, cryptonote::blobdata >> &txs) const
get a list of all relayable transactions and their hashes
bool have_tx_keyimges_as_spent(const transaction &tx) const
check if any spent key image in a transaction is in the pool
bool get_transaction(const crypto::hash &h, cryptonote::blobdata &txblob) const
get a specific transaction from the pool
information about a single transaction
Definition: tx_pool.h:345
POD_CLASS key_image
Definition: crypto.h:93
std::set< tx_by_fee_and_receive_time_entry, txCompare > sorted_tx_container
container for sorting transactions by fee per unit size
Definition: tx_pool.h:77
a struct containing txpool per transaction metadata
Definition: blockchain_db.h:137
bool kept_by_block
whether or not the transaction has been in a block before
Definition: tx_pool.h:357
string a
Definition: MakeCryptoOps.py:15
bool have_tx(const crypto::hash &id) const
checks if the pool has a transaction with the given hash
epee::math_helper::once_a_time_seconds< 30 > m_remove_stuck_tx_interval
interval on which to check for stale/"stuck" transactions
Definition: tx_pool.h:489
POD_CLASS hash
Definition: hash.h:46
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
time_t last_relayed_time
the last time the transaction was relayed to the network
Definition: tx_pool.h:375
bool take_tx(const crypto::hash &id, transaction &tx, size_t &blob_size, uint64_t &fee, bool &relayed, bool &do_not_relay)
takes a transaction with the given hash from the pool
static bool append_key_images(std::unordered_set< crypto::key_image > &kic, const transaction &tx)
append the key images from a transaction to the given set
sorted_tx_container m_txs_by_fee_and_receive_time
< container for transactions organized by fee per size and receive time
Definition: tx_pool.h:493
void lock() const
locks the transaction pool
Blockchain & m_blockchain
reference to the Blockchain object
Definition: tx_pool.h:512
Definition: cryptonote_basic.h:198
std::atomic< uint64_t > m_cookie
incremented at each change
Definition: tx_pool.h:495
static bool have_key_images(const std::unordered_set< crypto::key_image > &kic, const transaction &tx)
check if any of a transaction&#39;s spent key images are present in a given set
std::string print_pool(bool short_format) const
get a string containing human-readable pool information
Definition: blockchain.h:80
std::unordered_map< crypto::key_image, std::unordered_set< crypto::hash > > key_images_container
map key images to transactions which spent them
Definition: tx_pool.h:474