| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers |
| 30 | |
| 31 | #pragma once |
| 32 | #include "include_base_utils.h" |
| 33 | |
| 34 | #include <atomic> |
| 35 | #include <set> |
| 36 | #include <tuple> |
| 37 | #include <unordered_map> |
| 38 | #include <unordered_set> |
| 39 | #include <queue> |
| 40 | #include <boost/serialization/version.hpp> |
| 41 | #include <boost/utility.hpp> |
| 42 | |
| 43 | #include "span.h" |
| 44 | #include "string_tools.h" |
| 45 | #include "syncobj.h" |
| 46 | #include "math_helper.h" |
| 47 | #include "cryptonote_basic/cryptonote_basic_impl.h" |
| 48 | #include "cryptonote_basic/verification_context.h" |
| 49 | #include "cryptonote_protocol/enums.h" |
| 50 | #include "blockchain_db/blockchain_db.h" |
| 51 | #include "crypto/hash.h" |
| 52 | #include "rpc/core_rpc_server_commands_defs.h" |
| 53 | #include "rpc/message_data_structs.h" |
| 54 | |
| 55 | namespace cryptonote |
| 56 | { |
| 57 | class Blockchain; |
| 58 | /************************************************************************/ |
| 59 | /* */ |
| 60 | /************************************************************************/ |
| 61 | |
| 62 | //! pair of <transaction fee, transaction hash> for organization |
| 63 | typedef std::pair<std::pair<double, std::time_t>, crypto::hash> tx_by_fee_and_receive_time_entry; |
| 64 | |
| 65 | class txCompare |
| 66 | { |
| 67 | public: |
| 68 | bool operator()(const tx_by_fee_and_receive_time_entry& a, const tx_by_fee_and_receive_time_entry& b) const |
| 69 | { |
| 70 | // sort by greatest first, not least |
| 71 | if (a.first.first > b.first.first) return true; |
| 72 | if (a.first.first < b.first.first) return false; |
| 73 | |
| 74 | if (a.first.second < b.first.second) return true; |
| 75 | if (a.first.second > b.first.second) return false; |
| 76 | |
| 77 | return memcmp(a.second.data, b.second.data, sizeof(crypto::hash)) < 0; |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | //! container for sorting transactions by fee per unit size |
| 82 | typedef std::set<tx_by_fee_and_receive_time_entry, txCompare> sorted_tx_container; |
| 83 | |
| 84 | /** |
| 85 | * @brief Transaction pool, handles transactions which are not part of a block |
| 86 | * |
| 87 | * This class handles all transactions which have been received, but not as |
| 88 | * part of a block. |
| 89 | * |
| 90 | * This handling includes: |
| 91 | * storing the transactions |
| 92 | * organizing the transactions by fee per weight unit |
| 93 | * taking/giving transactions to and from various other components |
| 94 | * saving the transactions to disk on shutdown |
| 95 | * helping create a new block template by choosing transactions for it |
| 96 | * |
| 97 | */ |
| 98 | class tx_memory_pool: boost::noncopyable |
| 99 | { |
| 100 | public: |
| 101 | /** |
| 102 | * @brief Constructor |
| 103 | * |
| 104 | * @param bchs a Blockchain class instance, for getting chain info |
| 105 | */ |
| 106 | tx_memory_pool(Blockchain& bchs); |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * @copydoc add_tx(transaction&, tx_verification_context&, bool, bool, uint8_t) |
| 111 | * |
| 112 | * @param id the transaction's hash |
| 113 | * @tx_relay how the transaction was received |
| 114 | * @param tx_weight the transaction's weight |
| 115 | * @return True if tx passes verification checks AND is first observation |
| 116 | * of tx in `broadcasted` relay method. |
| 117 | */ |
| 118 | bool add_tx(transaction &tx, const crypto::hash &id, const cryptonote::blobdata &blob, |
| 119 | size_t tx_weight, tx_verification_context& tvc, relay_method tx_relay, bool relayed, |
| 120 | uint8_t version, uint8_t nic_verified_hf_version = 0); |
| 121 | |
| 122 | /** |
| 123 | * @brief add a transaction to the transaction pool |
| 124 | * |
| 125 | * Most likely the transaction will come from the network, but it is |
| 126 | * also possible for transactions to come from popped blocks during |
| 127 | * a reorg, or from local clients creating a transaction and |
| 128 | * submitting it to the network |
| 129 | * |
| 130 | * @param tx the transaction to be added |
| 131 | * @param tvc return-by-reference status about the transaction verification |
| 132 | * @tx_relay how the transaction was received |
| 133 | * @param relayed was this transaction from the network or a local client? |
| 134 | * @param version the version used to create the transaction |
| 135 | * @param nic_verified_hf_version hard fork which "tx" is known to pass non-input consensus test |
| 136 | * |
| 137 | * If "nic_verified_hf_version" parameter is equal to "version" parameter, then we skip the |
| 138 | * asserting `ver_non_input_consensus(tx)`, which greatly speeds up block popping and returning |
| 139 | * txs to mempool for txs which we know will pass the test. If nothing is known about how "tx" |
| 140 | * passes the non-input consensus tests (e.g. for newly received relayed txs), then leave |
| 141 | * "nic_verified_hf_version" as its default value of 0 (there is no v0 fork). |
| 142 | * |
| 143 | * @return True if tx passes verification checks AND is first observation |
| 144 | * of tx in `broadcasted` relay method. |
| 145 | */ |
| 146 | bool add_tx(transaction &tx, tx_verification_context& tvc, relay_method tx_relay, bool relayed, |
| 147 | uint8_t version, uint8_t nic_verified_hf_version = 0); |
| 148 | |
| 149 | /** |
| 150 | * @brief takes a transaction with the given hash from the pool |
| 151 | * |
| 152 | * @param id the hash of the transaction |
| 153 | * @param tx return-by-reference the transaction taken |
| 154 | * @param txblob return-by-reference the transaction as a blob |
| 155 | * @param tx_weight return-by-reference the transaction's weight |
| 156 | * @param fee the transaction fee |
| 157 | * @param relayed return-by-reference was transaction relayed to us by the network? |
| 158 | * @param do_not_relay return-by-reference is transaction not to be relayed to the network? |
| 159 | * @param double_spend_seen return-by-reference was a double spend seen for that transaction? |
| 160 | * @param pruned return-by-reference is the tx pruned |
| 161 | * @param suppress_missing_msgs suppress warning msgs when txid is missing (optional, defaults to `false`) |
| 162 | * |
| 163 | * @return true unless the transaction cannot be found in the pool |
| 164 | */ |
| 165 | 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, bool suppress_missing_msgs = false); |
| 166 | |
| 167 | /** |
| 168 | * @brief checks if the pool has a transaction with the given hash |
| 169 | * |
| 170 | * @param id the hash to look for |
| 171 | * @param tx_category a filter for txes |
| 172 | * |
| 173 | * @return true if the transaction is in the pool and meets tx_category requirements |
| 174 | */ |
| 175 | bool have_tx(const crypto::hash &id, relay_category tx_category) const; |
| 176 | |
| 177 | /** |
| 178 | * @brief action to take when notified of a block added to the blockchain |
| 179 | * |
| 180 | * Currently does nothing |
| 181 | * |
| 182 | * @param new_block_height the height of the blockchain after the change |
| 183 | * @param top_block_id the hash of the new top block |
| 184 | * |
| 185 | * @return true |
| 186 | */ |
| 187 | bool on_blockchain_inc(uint64_t new_block_height, const crypto::hash& top_block_id); |
| 188 | |
| 189 | /** |
| 190 | * @brief action to take when notified of a block removed from the blockchain |
| 191 | * |
| 192 | * Currently does nothing |
| 193 | * |
| 194 | * @param new_block_height the height of the blockchain after the change |
| 195 | * @param top_block_id the hash of the new top block |
| 196 | * |
| 197 | * @return true |
| 198 | */ |
| 199 | bool on_blockchain_dec(uint64_t new_block_height, const crypto::hash& top_block_id); |
| 200 | |
| 201 | /** |
| 202 | * @brief action to take periodically |
| 203 | * |
| 204 | * Currently checks transaction pool for stale ("stuck") transactions |
| 205 | */ |
| 206 | void on_idle(); |
| 207 | |
| 208 | /** |
| 209 | * @brief locks the transaction pool |
| 210 | */ |
| 211 | void lock() const; |
| 212 | |
| 213 | /** |
| 214 | * @brief unlocks the transaction pool |
| 215 | */ |
| 216 | void unlock() const; |
| 217 | |
| 218 | // load/store operations |
| 219 | |
| 220 | /** |
| 221 | * @brief loads pool state (if any) from disk, and initializes pool |
| 222 | * |
| 223 | * @param max_txpool_weight the max weight in bytes |
| 224 | * @param mine_stem_txes whether to mine txes in stem relay mode |
| 225 | * |
| 226 | * @return true |
| 227 | */ |
| 228 | bool init(size_t max_txpool_weight = 0, bool mine_stem_txes = false); |
| 229 | |
| 230 | /** |
| 231 | * @brief attempts to save the transaction pool state to disk |
| 232 | * |
| 233 | * Currently fails (returns false) if the data directory from init() |
| 234 | * does not exist and cannot be created, but returns true even if |
| 235 | * saving to disk is unsuccessful. |
| 236 | * |
| 237 | * @return true in most cases (see above) |
| 238 | */ |
| 239 | bool deinit(); |
| 240 | |
| 241 | /** |
| 242 | * @brief Chooses transactions for a block to include |
| 243 | * |
| 244 | * @param bl return-by-reference the block to fill in with transactions |
| 245 | * @param median_weight the current median block weight |
| 246 | * @param already_generated_coins the current total number of coins "minted" |
| 247 | * @param total_weight return-by-reference the total weight of the new block |
| 248 | * @param fee return-by-reference the total of fees from the included transactions |
| 249 | * @param expected_reward return-by-reference the total reward awarded to the miner finding this block, including transaction fees |
| 250 | * @param version hard fork version to use for consensus rules |
| 251 | * |
| 252 | * @return true |
| 253 | */ |
| 254 | 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); |
| 255 | |
| 256 | /** |
| 257 | * @brief get a list of all transactions in the pool |
| 258 | * |
| 259 | * @param txs return-by-reference the list of transactions |
| 260 | * @param include_sensitive return stempool, anonymity-pool, and unrelayed txes |
| 261 | * |
| 262 | */ |
| 263 | void get_transactions(std::vector<transaction>& txs, bool include_sensitive = false) const; |
| 264 | |
| 265 | /** |
| 266 | * @brief get a list of all transaction hashes in the pool |
| 267 | * |
| 268 | * @param txs return-by-reference the list of transactions |
| 269 | * @param include_sensitive return stempool, anonymity-pool, and unrelayed txes |
| 270 | * |
| 271 | */ |
| 272 | void get_transaction_hashes(std::vector<crypto::hash>& txs, bool include_sensitive = false) const; |
| 273 | |
| 274 | /** |
| 275 | * @brief get (weight, fee, receive time) for all transaction in the pool |
| 276 | * |
| 277 | * @param txs return-by-reference that data |
| 278 | * @param include_sensitive return stempool, anonymity-pool, and unrelayed txes |
| 279 | * |
| 280 | */ |
| 281 | void get_transaction_backlog(std::vector<tx_backlog_entry>& backlog, bool include_sensitive = false) const; |
| 282 | |
| 283 | /** |
| 284 | * @brief get (hash, weight, fee) for transactions in the pool - the minimum required information to create a block template |
| 285 | * |
| 286 | * Not all transactions in the pool will be returned for performance reasons |
| 287 | * If there are too many transactions in the pool, only the highest-paying transactions |
| 288 | * will be returned - but enough for the miner to create a full block |
| 289 | * |
| 290 | * @param backlog return-by-reference that data |
| 291 | * @param include_sensitive return stempool, anonymity-pool, and unrelayed txes |
| 292 | * |
| 293 | */ |
| 294 | void get_block_template_backlog(std::vector<tx_block_template_backlog_entry>& backlog, bool include_sensitive = false) const; |
| 295 | |
| 296 | /** |
| 297 | * @brief get a summary statistics of all transaction hashes in the pool |
| 298 | * |
| 299 | * @param stats return-by-reference the pool statistics |
| 300 | * @param include_sensitive return stempool, anonymity-pool, and unrelayed txes |
| 301 | * |
| 302 | */ |
| 303 | void get_transaction_stats(struct txpool_stats& stats, bool include_sensitive = false) const; |
| 304 | |
| 305 | /** |
| 306 | * @brief get information about all transactions and key images in the pool |
| 307 | * |
| 308 | * see documentation on tx_info and spent_key_image_info for more details |
| 309 | * |
| 310 | * @param tx_infos return-by-reference the transactions' information |
| 311 | * @param key_image_infos return-by-reference the spent key images' information |
| 312 | * @param include_sensitive_data return stempool, anonymity-pool, and unrelayed |
| 313 | * txes and fields that are sensitive to the node privacy |
| 314 | * |
| 315 | * @return true |
| 316 | */ |
| 317 | 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; |
| 318 | |
| 319 | /** |
| 320 | * @brief get information about all transactions and key images in the pool |
| 321 | * |
| 322 | * see documentation on tx_in_pool and key_images_with_tx_hashes for more details |
| 323 | * |
| 324 | * @param tx_infos [out] the transactions' information |
| 325 | * @param key_image_infos [out] the spent key images' information |
| 326 | * |
| 327 | * @return true |
| 328 | */ |
| 329 | 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; |
| 330 | |
| 331 | /** |
| 332 | * @brief check for presence of key images in the pool |
| 333 | * |
| 334 | * @param key_images [in] vector of key images to check |
| 335 | * @param spent [out] vector of bool to return |
| 336 | * |
| 337 | * @return true |
| 338 | */ |
| 339 | bool check_for_key_images(const std::vector<crypto::key_image>& key_images, std::vector<bool>& spent) const; |
| 340 | |
| 341 | /** |
| 342 | * @brief get a specific transaction from the pool |
| 343 | * |
| 344 | * @param h the hash of the transaction to get |
| 345 | * @param tx return-by-reference the transaction blob requested |
| 346 | * @param tx_relay last relay method us |
| 347 | * |
| 348 | * @return true if the transaction is found, otherwise false |
| 349 | */ |
| 350 | bool get_transaction(const crypto::hash& h, cryptonote::blobdata& txblob, relay_category tx_category) const; |
| 351 | |
| 352 | /** |
| 353 | * @brief get a list of all relayable transactions and their hashes |
| 354 | * |
| 355 | * "relayable" in this case means: |
| 356 | * nonzero fee |
| 357 | * hasn't been relayed too recently |
| 358 | * isn't old enough that relaying it is considered harmful |
| 359 | * Note a transaction can be "relayable" even if do_not_relay is true |
| 360 | * |
| 361 | * This function will skip all DB checks if an insufficient amount of |
| 362 | * time since the last call. |
| 363 | * |
| 364 | * @param txs return-by-reference the transactions and their hashes |
| 365 | * |
| 366 | * @return True if DB was checked, false if DB checks skipped. |
| 367 | */ |
| 368 | bool get_relayable_transactions(std::vector<std::tuple<crypto::hash, cryptonote::blobdata, relay_method>>& txs); |
| 369 | |
| 370 | /** |
| 371 | * @brief tell the pool that certain transactions were just relayed |
| 372 | * |
| 373 | * @param hashes list of tx hashes that are about to be relayed |
| 374 | * @param tx_relay update how the tx left this node |
| 375 | * @param just_broadcasted true if a tx was just broadcasted |
| 376 | * |
| 377 | */ |
| 378 | void set_relayed(epee::span<const crypto::hash> hashes, relay_method tx_relay, std::vector<bool> &just_broadcasted); |
| 379 | |
| 380 | /** |
| 381 | * @brief get the total number of transactions in the pool |
| 382 | * |
| 383 | * @return the number of transactions in the pool |
| 384 | */ |
| 385 | size_t get_transactions_count(bool include_sensitive = false) const; |
| 386 | |
| 387 | /** |
| 388 | * @brief get a string containing human-readable pool information |
| 389 | * |
| 390 | * @param short_format whether to use a shortened format for the info |
| 391 | * |
| 392 | * @return the string |
| 393 | */ |
| 394 | std::string print_pool(bool short_format) const; |
| 395 | |
| 396 | /** |
| 397 | * @brief remove transactions from the pool which are no longer valid |
| 398 | * |
| 399 | * With new versions of the currency, what conditions render a transaction |
| 400 | * invalid may change. This function clears those which were received |
| 401 | * before a version change and no longer conform to requirements. |
| 402 | * |
| 403 | * @param version the version the transactions must conform to |
| 404 | * |
| 405 | * @return the number of transactions removed |
| 406 | */ |
| 407 | size_t validate(uint8_t version); |
| 408 | |
| 409 | /** |
| 410 | * @brief return the cookie |
| 411 | * |
| 412 | * @return the cookie |
| 413 | */ |
| 414 | uint64_t cookie() const { return m_cookie; } |
| 415 | |
| 416 | /** |
| 417 | * @brief get the cumulative txpool weight in bytes |
| 418 | * |
| 419 | * @return the cumulative txpool weight in bytes |
| 420 | */ |
| 421 | size_t get_txpool_weight() const; |
| 422 | |
| 423 | /** |
| 424 | * @brief set the max cumulative txpool weight in bytes |
| 425 | * |
| 426 | * @param bytes the max cumulative txpool weight in bytes |
| 427 | */ |
| 428 | void set_txpool_max_weight(size_t bytes); |
| 429 | |
| 430 | /** |
| 431 | * @brief reduce the cumulative txpool weight by the weight provided |
| 432 | * |
| 433 | * @param weight the weight to reduce the total txpool weight by |
| 434 | */ |
| 435 | void reduce_txpool_weight(size_t weight); |
| 436 | |
| 437 | #define CURRENT_MEMPOOL_ARCHIVE_VER 11 |
| 438 | #define CURRENT_MEMPOOL_TX_DETAILS_ARCHIVE_VER 13 |
| 439 | |
| 440 | /** |
| 441 | * @brief information about a single transaction |
| 442 | */ |
| 443 | struct tx_details |
| 444 | { |
| 445 | transaction tx; //!< the transaction |
| 446 | cryptonote::blobdata tx_blob; //!< the transaction's binary blob |
| 447 | size_t blob_size; //!< the transaction's size |
| 448 | size_t weight; //!< the transaction's weight |
| 449 | uint64_t fee; //!< the transaction's fee amount |
| 450 | crypto::hash max_used_block_id; //!< the hash of the highest block referenced by an input |
| 451 | uint64_t max_used_block_height; //!< the height of the highest block referenced by an input |
| 452 | |
| 453 | //! whether or not the transaction has been in a block before |
| 454 | /*! if the transaction was returned to the pool from the blockchain |
| 455 | * due to a reorg, then this will be true |
| 456 | */ |
| 457 | bool kept_by_block; |
| 458 | |
| 459 | //! the highest block the transaction referenced when last checking it failed |
| 460 | /*! if verifying a transaction's inputs fails, it's possible this is due |
| 461 | * to a reorg since it was created (if it used recently created outputs |
| 462 | * as inputs). |
| 463 | */ |
| 464 | uint64_t last_failed_height; |
| 465 | |
| 466 | //! the hash of the highest block the transaction referenced when last checking it failed |
| 467 | /*! if verifying a transaction's inputs fails, it's possible this is due |
| 468 | * to a reorg since it was created (if it used recently created outputs |
| 469 | * as inputs). |
| 470 | */ |
| 471 | crypto::hash last_failed_id; |
| 472 | |
| 473 | time_t receive_time; //!< the time when the transaction entered the pool |
| 474 | |
| 475 | time_t last_relayed_time; //!< the last time the transaction was relayed to the network |
| 476 | bool relayed; //!< whether or not the transaction has been relayed to the network |
| 477 | bool do_not_relay; //!< to avoid relay this transaction to the network |
| 478 | |
| 479 | bool double_spend_seen; //!< true iff another tx was seen double spending this one |
| 480 | }; |
| 481 | |
| 482 | /** |
| 483 | * @brief get infornation about a single transaction |
| 484 | */ |
| 485 | bool get_transaction_info(const crypto::hash &txid, tx_details &td, bool include_sensitive_data, bool include_blob = false) const; |
| 486 | |
| 487 | /** |
| 488 | * @brief get information about multiple transactions |
| 489 | */ |
| 490 | bool get_transactions_info(const std::vector<crypto::hash>& txids, std::vector<std::pair<crypto::hash, tx_details>>& txs, bool include_sensitive_data = false) const; |
| 491 | |
| 492 | /** |
| 493 | * @brief get transactions not in the passed set |
| 494 | */ |
| 495 | bool get_complement(std::vector<crypto::hash> hashes, std::vector<cryptonote::blobdata> &txes) const; |
| 496 | |
| 497 | /** |
| 498 | * @brief get info necessary for update of pool-related info in a wallet, preferably incremental |
| 499 | * |
| 500 | * @return true on success, false on error |
| 501 | */ |
| 502 | bool get_pool_info(time_t start_time, bool include_sensitive, size_t max_tx_count, std::vector<std::pair<crypto::hash, tx_details>>& added_txs, std::vector<crypto::hash>& remaining_added_txids, std::vector<crypto::hash>& removed_txs, bool& incremental) const; |
| 503 | |
| 504 | private: |
| 505 | |
| 506 | /** |
| 507 | * @brief insert key images into m_spent_key_images |
| 508 | * |
| 509 | * @return true on success, false on error |
| 510 | */ |
| 511 | bool insert_key_images(const transaction_prefix &tx, const crypto::hash &txid, relay_method tx_relay); |
| 512 | |
| 513 | /** |
| 514 | * @brief remove old transactions from the pool |
| 515 | * |
| 516 | * After a certain time, it is assumed that a transaction which has not |
| 517 | * yet been mined will likely not be mined. These transactions are removed |
| 518 | * from the pool to avoid buildup. |
| 519 | * |
| 520 | * @return true |
| 521 | */ |
| 522 | bool remove_stuck_transactions(); |
| 523 | |
| 524 | /** |
| 525 | * @brief check if a transaction in the pool has a given spent key image |
| 526 | * |
| 527 | * @param key_im the spent key image to look for |
| 528 | * @param txid hash of the new transaction where `key_im` was seen. |
| 529 | * |
| 530 | * @return true if the spent key image is present, otherwise false |
| 531 | */ |
| 532 | bool have_tx_keyimg_as_spent(const crypto::key_image& key_im, const crypto::hash& txid) const; |
| 533 | |
| 534 | /** |
| 535 | * @brief check if any spent key image in a transaction is in the pool |
| 536 | * |
| 537 | * Checks if any of the spent key images in a given transaction are present |
| 538 | * in any of the transactions in the transaction pool. |
| 539 | * |
| 540 | * @note see tx_pool::have_tx_keyimg_as_spent |
| 541 | * |
| 542 | * @param tx the transaction to check spent key images of |
| 543 | * @param txid hash of `tx`. |
| 544 | * |
| 545 | * @return true if any spent key images are present in the pool, otherwise false |
| 546 | */ |
| 547 | bool have_tx_keyimges_as_spent(const transaction& tx, const crypto::hash& txid) const; |
| 548 | |
| 549 | /** |
| 550 | * @brief forget a transaction's spent key images |
| 551 | * |
| 552 | * Spent key images are stored separately from transactions for |
| 553 | * convenience/speed, so this is part of the process of removing |
| 554 | * a transaction from the pool. |
| 555 | * |
| 556 | * @param tx the transaction |
| 557 | * @param txid the transaction's hash |
| 558 | * |
| 559 | * @return false if any key images to be removed cannot be found, otherwise true |
| 560 | */ |
| 561 | bool remove_transaction_keyimages(const transaction_prefix& tx, const crypto::hash &txid); |
| 562 | |
| 563 | /** |
| 564 | * @brief check if any of a transaction's spent key images are present in a given set |
| 565 | * |
| 566 | * @param kic the set of key images to check against |
| 567 | * @param tx the transaction to check |
| 568 | * |
| 569 | * @return true if any key images present in the set, otherwise false |
| 570 | */ |
| 571 | static bool have_key_images(const std::unordered_set<crypto::key_image>& kic, const transaction_prefix& tx); |
| 572 | |
| 573 | /** |
| 574 | * @brief append the key images from a transaction to the given set |
| 575 | * |
| 576 | * @param kic the set of key images to append to |
| 577 | * @param tx the transaction |
| 578 | * |
| 579 | * @return false if any append fails, otherwise true |
| 580 | */ |
| 581 | static bool append_key_images(std::unordered_set<crypto::key_image>& kic, const transaction_prefix& tx); |
| 582 | |
| 583 | /** |
| 584 | * @brief check if a transaction is a valid candidate for inclusion in a block |
| 585 | * |
| 586 | * @param txd the transaction to check (and info about it) |
| 587 | * @param txid the txid of the transaction to check |
| 588 | * @param txblob the transaction blob to check |
| 589 | * @param tx the parsed transaction, if successful |
| 590 | * |
| 591 | * @return true if the transaction is good to go, otherwise false |
| 592 | */ |
| 593 | bool is_transaction_ready_to_go(txpool_tx_meta_t& txd, const crypto::hash &txid, const cryptonote::blobdata_ref &txblob, transaction&tx) const; |
| 594 | bool is_transaction_ready_to_go(txpool_tx_meta_t& txd, const crypto::hash &txid, const cryptonote::blobdata &txblob, transaction&tx) const; |
| 595 | |
| 596 | /** |
| 597 | * @brief mark all transactions double spending the one passed |
| 598 | */ |
| 599 | void mark_double_spend(const transaction &tx); |
| 600 | |
| 601 | /** |
| 602 | * @brief prune lowest fee/byte txes till we're not above bytes |
| 603 | * |
| 604 | * if bytes is 0, use m_txpool_max_weight |
| 605 | */ |
| 606 | void prune(size_t bytes = 0); |
| 607 | |
| 608 | void add_tx_to_transient_lists(const crypto::hash& txid, double fee, time_t receive_time); |
| 609 | void remove_tx_from_transient_lists(const cryptonote::sorted_tx_container::iterator& sorted_it, const crypto::hash& txid, bool sensitive); |
| 610 | void track_removed_tx(const crypto::hash& txid, bool sensitive); |
| 611 | |
| 612 | //TODO: confirm the below comments and investigate whether or not this |
| 613 | // is the desired behavior |
| 614 | //! map key images to transactions which spent them |
| 615 | /*! this seems odd, but it seems that multiple transactions can exist |
| 616 | * in the pool which both have the same spent key. This would happen |
| 617 | * in the event of a reorg where someone creates a new/different |
| 618 | * transaction on the assumption that the original will not be in a |
| 619 | * block again. |
| 620 | */ |
| 621 | typedef std::unordered_map<crypto::key_image, std::unordered_set<crypto::hash>> key_images_container; |
| 622 | |
| 623 | #if defined(DEBUG_CREATE_BLOCK_TEMPLATE) |
| 624 | public: |
| 625 | #endif |
| 626 | mutable epee::critical_section m_transactions_lock; //!< lock for the pool |
| 627 | #if defined(DEBUG_CREATE_BLOCK_TEMPLATE) |
| 628 | private: |
| 629 | #endif |
| 630 | |
| 631 | //! container for spent key images from the transactions in the pool |
| 632 | key_images_container m_spent_key_images; |
| 633 | |
| 634 | //TODO: this time should be a named constant somewhere, not hard-coded |
| 635 | //! interval on which to check for stale/"stuck" transactions |
| 636 | epee::math_helper::once_a_time_seconds<30> m_remove_stuck_tx_interval; |
| 637 | |
| 638 | //TODO: look into doing this better |
| 639 | //!< container for transactions organized by fee per size and receive time |
| 640 | sorted_tx_container m_txs_by_fee_and_receive_time; |
| 641 | |
| 642 | std::atomic<uint64_t> m_cookie; //!< incremented at each change |
| 643 | |
| 644 | // Info when transactions entered the pool, accessible by txid |
| 645 | std::unordered_map<crypto::hash, time_t> m_added_txs_by_id; |
| 646 | |
| 647 | // Info at what time the pool started to track the adding of transactions |
| 648 | time_t m_added_txs_start_time; |
| 649 | |
| 650 | struct removed_tx_info |
| 651 | { |
| 652 | crypto::hash txid; |
| 653 | bool sensitive; |
| 654 | }; |
| 655 | |
| 656 | // Info about transactions that were removed from the pool, ordered by the time |
| 657 | // of deletion |
| 658 | std::multimap<time_t, removed_tx_info> m_removed_txs_by_time; |
| 659 | |
| 660 | // Info how far back in time the list of removed tx ids currently reaches |
| 661 | // (it gets shorted periodically to prevent overflow) |
| 662 | time_t m_removed_txs_start_time; |
| 663 | |
| 664 | /** |
| 665 | * @brief get an iterator to a transaction in the sorted container |
| 666 | * |
| 667 | * @param id the hash of the transaction to look for |
| 668 | * |
| 669 | * @return an iterator, possibly to the end of the container if not found |
| 670 | */ |
| 671 | sorted_tx_container::iterator find_tx_in_sorted_container(const crypto::hash& id) const; |
| 672 | |
| 673 | //! cache/call Blockchain::check_tx_inputs results |
| 674 | 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; |
| 675 | |
| 676 | //! transactions which are unlikely to be included in blocks |
| 677 | /*! These transactions are kept in RAM in case they *are* included |
| 678 | * in a block eventually, but this container is not saved to disk. |
| 679 | */ |
| 680 | std::unordered_set<crypto::hash> m_timed_out_transactions; |
| 681 | |
| 682 | Blockchain& m_blockchain; //!< reference to the Blockchain object |
| 683 | |
| 684 | size_t m_txpool_max_weight; |
| 685 | size_t m_txpool_weight; |
| 686 | bool m_mine_stem_txes; |
| 687 | |
| 688 | mutable std::unordered_map<crypto::hash, std::tuple<bool, tx_verification_context, uint64_t, crypto::hash>> m_input_cache; |
| 689 | |
| 690 | std::unordered_map<crypto::hash, transaction> m_parsed_tx_cache; |
| 691 | |
| 692 | //! Next timestamp that a DB check for relayable txes is allowed |
| 693 | std::atomic<time_t> m_next_check; |
| 694 | }; |
| 695 | } |
| 696 | |
| 697 | namespace boost |
| 698 | { |
| 699 | namespace serialization |
| 700 | { |
| 701 | template<class archive_t> |
| 702 | void serialize(archive_t & ar, cryptonote::tx_memory_pool::tx_details& td, const unsigned int version) |
| 703 | { |
| 704 | ar & td.blob_size; |
| 705 | ar & td.fee; |
| 706 | ar & td.tx; |
| 707 | ar & td.max_used_block_height; |
| 708 | ar & td.max_used_block_id; |
| 709 | ar & td.last_failed_height; |
| 710 | ar & td.last_failed_id; |
| 711 | ar & td.receive_time; |
| 712 | ar & td.last_relayed_time; |
| 713 | ar & td.relayed; |
| 714 | if (version < 11) |
| 715 | return; |
| 716 | ar & td.kept_by_block; |
| 717 | if (version < 12) |
| 718 | return; |
| 719 | ar & td.do_not_relay; |
| 720 | if (version < 13) |
| 721 | return; |
| 722 | ar & td.weight; |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | BOOST_CLASS_VERSION(cryptonote::tx_memory_pool, CURRENT_MEMPOOL_ARCHIVE_VER) |
| 727 | BOOST_CLASS_VERSION(cryptonote::tx_memory_pool::tx_details, CURRENT_MEMPOOL_TX_DETAILS_ARCHIVE_VER) |
| 728 | |
| 729 | |
| 730 |