Electroneum
tx_pool.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2021, The Electroneum Project
2 // Copyrights(c) 2014-2019, 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"
51 
52 namespace cryptonote
53 {
54  class Blockchain;
55  /************************************************************************/
56  /* */
57  /************************************************************************/
58 
60  typedef std::pair<std::pair<double, std::time_t>, crypto::hash> tx_by_fee_and_receive_time_entry;
61 
62  class txCompare
63  {
64  public:
66  {
67  // sort by greatest first, not least
68  if (a.first.first > b.first.first) return true;
69  else if (a.first.first < b.first.first) return false;
70  else if (a.first.second < b.first.second) return true;
71  else if (a.first.second > b.first.second) return false;
72  else if (a.second != b.second) return true;
73  else return false;
74  }
75  };
76 
78  typedef std::set<tx_by_fee_and_receive_time_entry, txCompare> sorted_tx_container;
79 
94  class tx_memory_pool: boost::noncopyable
95  {
96  public:
102  tx_memory_pool(Blockchain& bchs);
103 
104 
111  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);
112 
130  bool add_tx(transaction &tx, tx_verification_context& tvc, bool kept_by_block, bool relayed, bool do_not_relay, uint8_t version);
131 
147  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 &nonexistent_utxo_seen);
148 
156  bool have_tx(const crypto::hash &id) const;
157 
168  bool on_blockchain_inc(uint64_t new_block_height, const crypto::hash& top_block_id);
169 
180  bool on_blockchain_dec(uint64_t new_block_height, const crypto::hash& top_block_id);
181 
187  void on_idle();
188 
192  void lock() const;
193 
197  void unlock() const;
198 
199  // load/store operations
200 
208  bool init(size_t max_txpool_weight = 0);
209 
219  bool deinit();
220 
234  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);
235 
243  void get_transactions(std::vector<transaction>& txs, bool include_unrelayed_txes = true) const;
244 
252  void get_transaction_hashes(std::vector<crypto::hash>& txs, bool include_unrelayed_txes = true) const;
253 
261  void get_transaction_backlog(std::vector<tx_backlog_entry>& backlog, bool include_unrelayed_txes = true) const;
262 
270  void get_transaction_stats(struct txpool_stats& stats, bool include_unrelayed_txes = true) const;
271 
283  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;
284 
295  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;
296 
305  bool check_for_key_images(const std::vector<crypto::key_image>& key_images, std::vector<bool> spent) const;
306 
315  bool get_transaction(const crypto::hash& h, cryptonote::blobdata& txblob) const;
316 
330  bool get_relayable_transactions(std::vector<std::pair<crypto::hash, cryptonote::blobdata>>& txs) const;
331 
337  void set_relayed(const std::vector<std::pair<crypto::hash, cryptonote::blobdata>>& txs);
338 
344  size_t get_transactions_count(bool include_unrelayed_txes = true) const;
345 
353  std::string print_pool(bool short_format) const;
354 
366  size_t validate(uint8_t version);
367 
373  uint64_t cookie() const { return m_cookie; }
374 
380  size_t get_txpool_weight() const;
381 
387  void set_txpool_max_weight(size_t bytes);
388 
389 #define CURRENT_MEMPOOL_ARCHIVE_VER 11
390 #define CURRENT_MEMPOOL_TX_DETAILS_ARCHIVE_VER 13
391 
395  struct tx_details
396  {
398  size_t blob_size;
399  size_t weight;
403 
405 
409 
411 
416 
418 
423 
424  time_t receive_time;
425 
427  bool relayed;
429 
432  };
433 
434  private:
435 
441  bool insert_key_images(const transaction_prefix &tx, const crypto::hash &txid, bool kept_by_block);
442 
443  bool insert_utxos(const transaction_prefix &tx, const crypto::hash &id, bool kept_by_block);
444 
454  bool remove_stuck_transactions();
455 
463  bool have_tx_keyimg_as_spent(const crypto::key_image& key_im) const;
464 
465  bool have_tx_utxo_as_spent(const txin_to_key_public& in) const;
466 
480  bool key_images_already_spent(const transaction &tx) const;
481 
490  bool utxo_nonexistent(const transaction &tx) const;
491 
492 
507  bool remove_transaction_keyimages(const transaction_prefix& tx, const crypto::hash &txid);
508 
517  static bool have_key_images(const std::unordered_set<crypto::key_image>& kic, const transaction_prefix& tx);
518 
519  static bool have_utxos(const std::unordered_set<std::string>& utxos, const transaction_prefix& tx);
520 
529  static bool append_key_images(std::unordered_set<crypto::key_image>& kic, const transaction_prefix& tx);
530 
531  static bool append_utxos(std::unordered_set<std::string>& utxos, const transaction_prefix& tx);
532 
543  bool is_transaction_ready_to_go(txpool_tx_meta_t& txd, const crypto::hash &txid, const cryptonote::blobdata &txblob, transaction&tx) const;
544 
548  void mark_double_spend_or_nonexistent_utxo(const transaction &tx);
549 
555  void prune(size_t bytes = 0);
556 
557  //TODO: confirm the below comments and investigate whether or not this
558  // is the desired behavior
560 
566  typedef std::unordered_map<crypto::key_image, std::unordered_set<crypto::hash> > key_images_container;
567 
568  typedef std::unordered_map<std::string, std::unordered_set<crypto::hash> > utxos_container;
569 
570 #if defined(DEBUG_CREATE_BLOCK_TEMPLATE)
571 public:
572 #endif
573  mutable epee::critical_section m_transactions_lock;
574 #if defined(DEBUG_CREATE_BLOCK_TEMPLATE)
575 private:
576 #endif
577 
579  key_images_container m_spent_key_images;
580 
582  utxos_container m_spent_utxos;
583 
584  //TODO: this time should be a named constant somewhere, not hard-coded
586  epee::math_helper::once_a_time_seconds<30> m_remove_stuck_tx_interval;
587 
588  //TODO: look into doing this better
590  sorted_tx_container m_txs_by_fee_and_receive_time;
591 
592  std::atomic<uint64_t> m_cookie;
593 
601  sorted_tx_container::iterator find_tx_in_sorted_container(const crypto::hash& id) const;
602 
604  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;
605 
607 
610  std::unordered_set<crypto::hash> m_timed_out_transactions;
611 
612  Blockchain& m_blockchain;
613 
614  size_t m_txpool_max_weight;
615  size_t m_txpool_weight;
616 
617  mutable std::unordered_map<crypto::hash, std::tuple<bool, tx_verification_context, uint64_t, crypto::hash>> m_input_cache;
618 
619  std::unordered_map<crypto::hash, transaction> m_parsed_tx_cache;
620  };
621 }
622 
623 namespace boost
624 {
625  namespace serialization
626  {
627  template<class archive_t>
628  void serialize(archive_t & ar, cryptonote::tx_memory_pool::tx_details& td, const unsigned int version)
629  {
630  ar & td.blob_size;
631  ar & td.fee;
632  ar & td.tx;
633  ar & td.max_used_block_height;
634  ar & td.max_used_block_id;
635  ar & td.last_failed_height;
636  ar & td.last_failed_id;
637  ar & td.receive_time;
638  ar & td.last_relayed_time;
639  ar & td.relayed;
640  if (version < 11)
641  return;
642  ar & td.kept_by_block;
643  if (version < 12)
644  return;
645  ar & td.do_not_relay;
646  if (version < 13)
647  return;
648  ar & td.weight;
649  }
650  }
651 }
654 
655 
656 
Transaction pool, handles transactions which are not part of a block.
Definition: tx_pool.h:95
void unlock() const
unlocks the transaction pool
Definition: tx_pool.cpp:1083
void lock() const
locks the transaction pool
Definition: tx_pool.cpp:1078
void on_idle()
action to take periodically
Definition: tx_pool.cpp:587
void set_relayed(const std::vector< std::pair< crypto::hash, cryptonote::blobdata >> &txs)
tell the pool that certain transactions were just relayed
Definition: tx_pool.cpp:699
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
Definition: tx_pool.cpp:931
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
Definition: tx_pool.cpp:1017
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.
Definition: tx_pool.cpp:1358
bool have_tx(const crypto::hash &id) const
checks if the pool has a transaction with the given hash
Definition: tx_pool.cpp:1025
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
Definition: tx_pool.cpp:1009
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
Definition: tx_pool.cpp:752
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)
Definition: tx_pool.cpp:120
uint64_t cookie() const
return the cookie
Definition: tx_pool.h:373
void set_txpool_max_weight(size_t bytes)
set the max cumulative txpool weight in bytes
Definition: tx_pool.cpp:370
void get_transactions(std::vector< transaction > &txs, bool include_unrelayed_txes=true) const
get a list of all transactions in the pool
Definition: tx_pool.cpp:733
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 &nonexistent_utxo_seen)
takes a transaction with the given hash from the pool
Definition: tx_pool.cpp:531
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
Definition: tx_pool.cpp:855
bool init(size_t max_txpool_weight=0)
loads pool state (if any) from disk, and initializes pool
Definition: tx_pool.cpp:1554
std::string print_pool(bool short_format) const
get a string containing human-readable pool information
Definition: tx_pool.cpp:1325
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
Definition: tx_pool.cpp:763
tx_memory_pool(Blockchain &bchs)
Constructor.
Definition: tx_pool.cpp:115
size_t validate(uint8_t version)
remove transactions from the pool which are no longer valid
Definition: tx_pool.cpp:1490
size_t get_txpool_weight() const
get the cumulative txpool weight in bytes
Definition: tx_pool.cpp:364
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
Definition: tx_pool.cpp:775
size_t get_transactions_count(bool include_unrelayed_txes=true) const
get the total number of transactions in the pool
Definition: tx_pool.cpp:726
bool get_relayable_transactions(std::vector< std::pair< crypto::hash, cryptonote::blobdata >> &txs) const
get a list of all relayable transactions and their hashes
Definition: tx_pool.cpp:665
bool get_transaction(const crypto::hash &h, cryptonote::blobdata &txblob) const
get a specific transaction from the pool
Definition: tx_pool.cpp:995
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
Definition: tx_pool.cpp:980
bool deinit()
attempts to save the transaction pool state to disk
Definition: tx_pool.cpp:1623
bool operator()(const tx_by_fee_and_receive_time_entry &a, const tx_by_fee_and_receive_time_entry &b) const
Definition: tx_pool.h:65
BOOST_CLASS_VERSION(nodetool::node_server< cryptonote::t_cryptonote_protocol_handler< tests::proxy_core > >, 1)
void serialize(Archive &a, std::unordered_map< h_key, hval > &x, const boost::serialization::version_type ver)
POD_CLASS key_image
Definition: crypto.h:102
POD_CLASS hash
Definition: hash.h:50
std::unordered_map< crypto::key_image, std::vector< crypto::hash > > key_images_with_tx_hashes
Holds cryptonote related classes and helpers.
Definition: ban.cpp:40
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:78
std::string blobdata
Definition: blobdatatype.h:39
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:54
version
Supported socks variants.
Definition: socks.h:58
::std::string string
Definition: gtest-port.h:1097
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
unsigned char uint8_t
Definition: stdint.h:124
unsigned __int64 uint64_t
Definition: stdint.h:136
information about a single transaction
Definition: tx_pool.h:396
transaction tx
the transaction
Definition: tx_pool.h:397
bool do_not_relay
to avoid relay this transaction to the network
Definition: tx_pool.h:428
uint64_t max_used_block_height
the height of the highest block referenced by an input
Definition: tx_pool.h:402
crypto::hash last_failed_id
the hash of the highest block the transaction referenced when last checking it failed
Definition: tx_pool.h:422
uint64_t fee
the transaction's fee amount
Definition: tx_pool.h:400
bool kept_by_block
whether or not the transaction has been in a block before
Definition: tx_pool.h:408
size_t weight
the transaction's weight
Definition: tx_pool.h:399
time_t last_relayed_time
the last time the transaction was relayed to the network
Definition: tx_pool.h:426
bool relayed
whether or not the transaction has been relayed to the network
Definition: tx_pool.h:427
crypto::hash max_used_block_id
the hash of the highest block referenced by an input
Definition: tx_pool.h:401
size_t blob_size
the transaction's size
Definition: tx_pool.h:398
uint64_t last_failed_height
the highest block the transaction referenced when last checking it failed
Definition: tx_pool.h:415
bool double_spend_seen
true iff another tx was seen double spending this one
Definition: tx_pool.h:430
time_t receive_time
the time when the transaction entered the pool
Definition: tx_pool.h:424
a struct containing txpool per transaction metadata
#define CURRENT_MEMPOOL_ARCHIVE_VER
Definition: tx_pool.h:389
#define CURRENT_MEMPOOL_TX_DETAILS_ARCHIVE_VER
Definition: tx_pool.h:390