| 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 | #include <algorithm> |
| 32 | #include <boost/filesystem.hpp> |
| 33 | #include <unordered_set> |
| 34 | #include <vector> |
| 35 | |
| 36 | #include "tx_pool.h" |
| 37 | #include "cryptonote_tx_utils.h" |
| 38 | #include "cryptonote_basic/cryptonote_boost_serialization.h" |
| 39 | #include "cryptonote_basic/events.h" |
| 40 | #include "cryptonote_config.h" |
| 41 | #include "blockchain.h" |
| 42 | #include "blockchain_db/locked_txn.h" |
| 43 | #include "blockchain_db/blockchain_db.h" |
| 44 | #include "common/boost_serialization_helper.h" |
| 45 | #include "int-util.h" |
| 46 | #include "misc_language.h" |
| 47 | #include "misc_log_ex.h" |
| 48 | #include "tx_verification_utils.h" |
| 49 | #include "warnings.h" |
| 50 | #include "common/perf_timer.h" |
| 51 | #include "crypto/hash.h" |
| 52 | #include "crypto/duration.h" |
| 53 | |
| 54 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 55 | #define MONERO_DEFAULT_LOG_CATEGORY "txpool" |
| 56 | |
| 57 | DISABLE_VS_WARNINGS(4244 4345 4503) //'boost::foreach_detail_::or_' : decorated name length exceeded, name was truncated |
| 58 | |
| 59 | using namespace crypto; |
| 60 | |
| 61 | namespace cryptonote |
| 62 | { |
| 63 | namespace |
| 64 | { |
| 65 | /*! The Dandelion++ has formula for calculating the average embargo timeout: |
| 66 | (-k*(k-1)*hop)/(2*log(1-ep)) |
| 67 | where k is the number of hops before this node and ep is the probability |
| 68 | that one of the k hops hits their embargo timer, and hop is the average |
| 69 | time taken between hops. So decreasing ep will make it more probable |
| 70 | that "this" node is the first to expire the embargo timer. Increasing k |
| 71 | will increase the number of nodes that will be "hidden" as a prior |
| 72 | recipient of the tx. |
| 73 | |
| 74 | As example, k=5 and ep=0.1 means "this" embargo timer has a 90% |
| 75 | probability of being the first to expire amongst 5 nodes that saw the |
| 76 | tx before "this" one. These values are independent to the fluff |
| 77 | probability, but setting a low k with a low p (fluff probability) is |
| 78 | not ideal since a blackhole is more likely to reveal earlier nodes in |
| 79 | the chain. |
| 80 | |
| 81 | This value was calculated with k=5, ep=0.10, and hop = 175 ms. A |
| 82 | testrun from a recent Intel laptop took ~80ms to |
| 83 | receive+parse+proces+send transaction. At least 50ms will be added to |
| 84 | the latency if crossing an ocean. So 175ms is the fudge factor for |
| 85 | a single hop with 39s being the embargo timer. */ |
| 86 | constexpr const std::chrono::seconds dandelionpp_embargo_average{CRYPTONOTE_DANDELIONPP_EMBARGO_AVERAGE}; |
| 87 | |
| 88 | //TODO: constants such as these should at least be in the header, |
| 89 | // but probably somewhere more accessible to the rest of the |
| 90 | // codebase. As it stands, it is at best nontrivial to test |
| 91 | // whether or not changing these parameters (or adding new) |
| 92 | // will work correctly. |
| 93 | time_t const MIN_RELAY_TIME = (60 * 5); // only start re-relaying transactions after that many seconds |
| 94 | time_t const MAX_RELAY_TIME = (60 * 60 * 4); // at most that many seconds between resends |
| 95 | float const ACCEPT_THRESHOLD = 1.0f; |
| 96 | |
| 97 | //! Max DB check interval for relayable txes |
| 98 | constexpr const std::chrono::minutes max_relayable_check{2}; |
| 99 | |
| 100 | constexpr const std::chrono::seconds forward_delay_average{CRYPTONOTE_FORWARD_DELAY_AVERAGE}; |
| 101 | |
| 102 | // a kind of increasing backoff within min/max bounds |
| 103 | uint64_t get_relay_delay(time_t last_relay, time_t received) |
| 104 | { |
| 105 | time_t d = (last_relay - received + MIN_RELAY_TIME) / MIN_RELAY_TIME * MIN_RELAY_TIME; |
| 106 | if (d > MAX_RELAY_TIME) |
| 107 | d = MAX_RELAY_TIME; |
| 108 | return d; |
| 109 | } |
| 110 | |
| 111 | uint64_t template_accept_threshold(uint64_t amount) |
| 112 | { |
| 113 | return amount * ACCEPT_THRESHOLD; |
| 114 | } |
| 115 | |
| 116 | // external lock must be held for the comparison+set to work properly |
| 117 | void set_if_less(std::atomic<time_t>& next_check, const time_t candidate) noexcept |
| 118 | { |
| 119 | if (candidate < next_check.load(std::memory_order_relaxed)) |
| 120 | next_check = candidate; |
| 121 | } |
| 122 | } |
| 123 | //--------------------------------------------------------------------------------- |
| 124 | //--------------------------------------------------------------------------------- |
| 125 | tx_memory_pool::tx_memory_pool(Blockchain& bchs): m_blockchain(bchs), m_cookie(0), m_txpool_max_weight(DEFAULT_TXPOOL_MAX_WEIGHT), m_txpool_weight(0), m_mine_stem_txes(false), m_next_check(std::time(nullptr)) |
| 126 | { |
| 127 | // class code expects unsigned values throughout |
| 128 | if (m_next_check < time_t(0)) |
| 129 | throw std::runtime_error{"Unexpected time_t (system clock) value"}; |
| 130 | |
| 131 | m_added_txs_start_time = (time_t)0; |
| 132 | m_removed_txs_start_time = (time_t)0; |
| 133 | // We don't set these to "now" already here as we don't know how long it takes from construction |
| 134 | // of the pool until it "goes to work". It's safer to set when the first actual txs enter the |
| 135 | // corresponding lists. |
| 136 | } |
| 137 | //--------------------------------------------------------------------------------- |
| 138 | bool tx_memory_pool::add_tx(transaction &tx, /*const crypto::hash& tx_prefix_hash,*/ |
| 139 | const crypto::hash &id, const cryptonote::blobdata &blob, size_t tx_weight, |
| 140 | tx_verification_context& tvc, relay_method tx_relay, bool relayed, |
| 141 | uint8_t version, uint8_t nic_verified_hf_version) |
| 142 | { |
| 143 | const bool kept_by_block = (tx_relay == relay_method::block); |
| 144 | |
| 145 | // this should already be called with that lock, but let's make it explicit for clarity |
| 146 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 147 | |
| 148 | PERF_TIMER(add_tx); |
| 149 | |
| 150 | // we do not accept transactions that timed out before, unless they're |
| 151 | // kept_by_block |
| 152 | if (!kept_by_block && m_timed_out_transactions.find(id) != m_timed_out_transactions.end()) |
| 153 | { |
| 154 | // not clear if we should set that, since verifivation (sic) did not fail before, since |
| 155 | // the tx was accepted before timing out. |
| 156 | tvc.m_verifivation_failed = true; |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | if (version != nic_verified_hf_version && !cryptonote::ver_non_input_consensus(tx, tvc, version)) |
| 161 | { |
| 162 | LOG_PRINT_L1("transaction " << id << " failed non-input consensus rule checks"); |
| 163 | tvc.m_verifivation_failed = true; // should already be set, but just in case |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | uint64_t fee; |
| 168 | bool fee_good = false; |
| 169 | try |
| 170 | { |
| 171 | // get_tx_fee() can throw. It shouldn't throw because we check preconditions in |
| 172 | // ver_non_input_consensus(), but let's put it in a try block just in case. |
| 173 | fee = get_tx_fee(tx); |
| 174 | fee_good = kept_by_block || m_blockchain.check_fee(tx_weight, fee); |
| 175 | } |
| 176 | catch(...) {} |
| 177 | if (!fee_good) // if fee calculation failed or fee in relayed tx is too low... |
| 178 | { |
| 179 | tvc.m_verifivation_failed = true; |
| 180 | tvc.m_fee_too_low = true; |
| 181 | tvc.m_no_drop_offense = true; |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | size_t tx_extra_size = tx.extra.size(); |
| 186 | if (!kept_by_block && tx_extra_size > MAX_TX_EXTRA_SIZE) |
| 187 | { |
| 188 | LOG_PRINT_L1("transaction tx-extra is too big: " << tx_extra_size << " bytes, the limit is: " << MAX_TX_EXTRA_SIZE); |
| 189 | tvc.m_verifivation_failed = true; |
| 190 | tvc.m_tx_extra_too_big = true; |
| 191 | tvc.m_no_drop_offense = true; |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | if (!kept_by_block && tx.unlock_time) |
| 196 | { |
| 197 | LOG_PRINT_L1("transaction unlock time is not zero: " << tx.unlock_time); |
| 198 | tvc.m_verifivation_failed = true; |
| 199 | tvc.m_nonzero_unlock_time = true; |
| 200 | tvc.m_no_drop_offense = true; |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | // if the transaction came from a block popped from the chain, |
| 205 | // don't check if we have its key images as spent. |
| 206 | // TODO: Investigate why not? |
| 207 | if(!kept_by_block) |
| 208 | { |
| 209 | if(have_tx_keyimges_as_spent(tx, id)) |
| 210 | { |
| 211 | mark_double_spend(tx); |
| 212 | LOG_PRINT_L1("Transaction with id= "<< id << " used already spent key images"); |
| 213 | tvc.m_verifivation_failed = true; |
| 214 | tvc.m_double_spend = true; |
| 215 | tvc.m_no_drop_offense = true; |
| 216 | return false; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // assume failure during verification steps until success is certain |
| 221 | tvc.m_verifivation_failed = true; |
| 222 | |
| 223 | time_t receive_time = time(nullptr); |
| 224 | |
| 225 | crypto::hash max_used_block_id = null_hash; |
| 226 | uint64_t max_used_block_height = 0; |
| 227 | cryptonote::txpool_tx_meta_t meta{}; |
| 228 | bool ch_inp_res = check_tx_inputs([&tx]()->cryptonote::transaction&{ return tx; }, id, max_used_block_height, max_used_block_id, tvc, kept_by_block); |
| 229 | if(!ch_inp_res) |
| 230 | { |
| 231 | // if the transaction was valid before (kept_by_block), then it |
| 232 | // may become valid again, so ignore the failed inputs check. |
| 233 | if(kept_by_block) |
| 234 | { |
| 235 | meta.weight = tx_weight; |
| 236 | meta.fee = fee; |
| 237 | meta.max_used_block_id = null_hash; |
| 238 | meta.max_used_block_height = 0; |
| 239 | meta.last_failed_height = 0; |
| 240 | meta.last_failed_id = null_hash; |
| 241 | meta.receive_time = receive_time; |
| 242 | meta.last_relayed_time = time(NULL); |
| 243 | meta.relayed = relayed; |
| 244 | meta.set_relay_method(tx_relay); |
| 245 | meta.double_spend_seen = have_tx_keyimges_as_spent(tx, id); |
| 246 | meta.pruned = tx.pruned; |
| 247 | meta.bf_padding = 0; |
| 248 | memset(meta.padding, 0, sizeof(meta.padding)); |
| 249 | try |
| 250 | { |
| 251 | if (kept_by_block) |
| 252 | m_parsed_tx_cache.insert(std::make_pair(id, tx)); |
| 253 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 254 | LockedTXN lock(m_blockchain.get_db()); |
| 255 | if (!insert_key_images(tx, id, tx_relay)) |
| 256 | return false; |
| 257 | |
| 258 | m_blockchain.add_txpool_tx(id, blob, meta); |
| 259 | add_tx_to_transient_lists(id, fee / (double)(tx_weight ? tx_weight : 1), receive_time); |
| 260 | lock.commit(); |
| 261 | } |
| 262 | catch (const std::exception &e) |
| 263 | { |
| 264 | MERROR("Error adding transaction to txpool: " << e.what()); |
| 265 | return false; |
| 266 | } |
| 267 | tvc.m_verifivation_impossible = true; |
| 268 | tvc.m_added_to_pool = true; |
| 269 | }else |
| 270 | { |
| 271 | LOG_PRINT_L1("tx used wrong inputs, rejected"); |
| 272 | tvc.m_verifivation_failed = true; |
| 273 | tvc.m_invalid_input = true; |
| 274 | return false; |
| 275 | } |
| 276 | }else |
| 277 | { |
| 278 | try |
| 279 | { |
| 280 | if (kept_by_block) |
| 281 | m_parsed_tx_cache.insert(std::make_pair(id, tx)); |
| 282 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 283 | LockedTXN lock(m_blockchain.get_db()); |
| 284 | |
| 285 | const bool existing_tx = m_blockchain.get_txpool_tx_meta(id, meta); |
| 286 | if (existing_tx) |
| 287 | { |
| 288 | /* If Dandelion++ loop. Do not use txes in the `local` state in the |
| 289 | loop detection - txes in that state should be outgoing over i2p/tor |
| 290 | then routed back via public dandelion++ stem. Pretend to be |
| 291 | another stem node in that situation, a loop over the public |
| 292 | network hasn't been hit yet. */ |
| 293 | if (tx_relay == relay_method::stem && meta.dandelionpp_stem) |
| 294 | tx_relay = relay_method::fluff; |
| 295 | } |
| 296 | else |
| 297 | meta.set_relay_method(relay_method::none); |
| 298 | |
| 299 | if (meta.upgrade_relay_method(tx_relay) || !existing_tx) // synchronize with embargo timer or stem/fluff out-of-order messages |
| 300 | { |
| 301 | using clock = std::chrono::system_clock; |
| 302 | auto last_relayed_time = std::numeric_limits<decltype(meta.last_relayed_time)>::max(); |
| 303 | if (tx_relay == relay_method::forward) |
| 304 | { |
| 305 | last_relayed_time = clock::to_time_t(clock::now() + crypto::random_poisson_seconds{forward_delay_average}()); |
| 306 | set_if_less(m_next_check, time_t(last_relayed_time)); |
| 307 | } |
| 308 | // else the `set_relayed` function will adjust the time accordingly later |
| 309 | |
| 310 | //update transactions container |
| 311 | meta.last_relayed_time = last_relayed_time; |
| 312 | meta.receive_time = receive_time; |
| 313 | meta.weight = tx_weight; |
| 314 | meta.fee = fee; |
| 315 | meta.max_used_block_id = max_used_block_id; |
| 316 | meta.max_used_block_height = max_used_block_height; |
| 317 | meta.last_failed_height = 0; |
| 318 | meta.last_failed_id = null_hash; |
| 319 | meta.relayed = relayed; |
| 320 | meta.double_spend_seen = false; |
| 321 | meta.pruned = tx.pruned; |
| 322 | meta.bf_padding = 0; |
| 323 | memset(meta.padding, 0, sizeof(meta.padding)); |
| 324 | |
| 325 | if (!insert_key_images(tx, id, tx_relay)) |
| 326 | return false; |
| 327 | |
| 328 | m_blockchain.remove_txpool_tx(id); |
| 329 | m_blockchain.add_txpool_tx(id, blob, meta); |
| 330 | add_tx_to_transient_lists(id, meta.fee / (double)(tx_weight ? tx_weight : 1), receive_time); |
| 331 | } |
| 332 | lock.commit(); |
| 333 | tvc.m_added_to_pool = !existing_tx; |
| 334 | } |
| 335 | catch (const std::exception &e) |
| 336 | { |
| 337 | MERROR("internal error: error adding transaction to txpool: " << e.what()); |
| 338 | return false; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | tvc.m_verifivation_failed = false; |
| 343 | if (tvc.m_added_to_pool) |
| 344 | m_txpool_weight += tx_weight; |
| 345 | |
| 346 | static_assert(unsigned(relay_method::none) == 0, "expected relay_method::none value to be zero"); |
| 347 | if (meta.fee > 0 && tx_relay != relay_method::forward) |
| 348 | tvc.m_relay = tx_relay; |
| 349 | |
| 350 | ++m_cookie; |
| 351 | |
| 352 | MINFO("Transaction added to pool: txid " << id << " weight: " << tx_weight << " fee/byte: " << (fee / (double)(tx_weight ? tx_weight : 1)) << ", count: " << m_added_txs_by_id.size()); |
| 353 | |
| 354 | prune(m_txpool_max_weight); |
| 355 | |
| 356 | return true; |
| 357 | } |
| 358 | //--------------------------------------------------------------------------------- |
| 359 | bool tx_memory_pool::add_tx(transaction &tx, tx_verification_context& tvc, relay_method tx_relay, |
| 360 | bool relayed, uint8_t version, uint8_t nic_verified_hf_version) |
| 361 | { |
| 362 | crypto::hash h = null_hash; |
| 363 | cryptonote::blobdata bl; |
| 364 | t_serializable_object_to_blob(tx, bl); |
| 365 | if (bl.size() == 0 || !get_transaction_hash(tx, h)) |
| 366 | return false; |
| 367 | return add_tx(tx, h, bl, get_transaction_weight(tx, bl.size()), tvc, tx_relay, relayed, version, |
| 368 | nic_verified_hf_version); |
| 369 | } |
| 370 | //--------------------------------------------------------------------------------- |
| 371 | size_t tx_memory_pool::get_txpool_weight() const |
| 372 | { |
| 373 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 374 | return m_txpool_weight; |
| 375 | } |
| 376 | //--------------------------------------------------------------------------------- |
| 377 | void tx_memory_pool::set_txpool_max_weight(size_t bytes) |
| 378 | { |
| 379 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 380 | m_txpool_max_weight = bytes; |
| 381 | } |
| 382 | //--------------------------------------------------------------------------------- |
| 383 | void tx_memory_pool::reduce_txpool_weight(size_t weight) |
| 384 | { |
| 385 | if (weight > m_txpool_weight) |
| 386 | { |
| 387 | MERROR("Underflow in txpool weight"); |
| 388 | m_txpool_weight = 0; |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | m_txpool_weight -= weight; |
| 393 | } |
| 394 | } |
| 395 | //--------------------------------------------------------------------------------- |
| 396 | void tx_memory_pool::prune(size_t bytes) |
| 397 | { |
| 398 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 399 | |
| 400 | // Nothing to do if already empty |
| 401 | if (m_txs_by_fee_and_receive_time.empty()) |
| 402 | return; |
| 403 | |
| 404 | if (bytes == 0) |
| 405 | bytes = m_txpool_max_weight; |
| 406 | |
| 407 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 408 | LockedTXN lock(m_blockchain.get_db()); |
| 409 | bool changed = false; |
| 410 | |
| 411 | // this will never remove the first one, but we don't care |
| 412 | auto it = --m_txs_by_fee_and_receive_time.end(); |
| 413 | while (it != m_txs_by_fee_and_receive_time.begin()) |
| 414 | { |
| 415 | if (m_txpool_weight <= bytes) |
| 416 | break; |
| 417 | try |
| 418 | { |
| 419 | const crypto::hash &txid = it->second; |
| 420 | txpool_tx_meta_t meta; |
| 421 | if (!m_blockchain.get_txpool_tx_meta(txid, meta)) |
| 422 | { |
| 423 | static bool warned = false; |
| 424 | if (!warned) |
| 425 | { |
| 426 | MERROR("Failed to find tx_meta in txpool (will only print once)"); |
| 427 | warned = true; |
| 428 | } |
| 429 | --it; |
| 430 | continue; |
| 431 | } |
| 432 | // don't prune the kept_by_block ones, they're likely added because we're adding a block with those |
| 433 | if (meta.kept_by_block) |
| 434 | { |
| 435 | --it; |
| 436 | continue; |
| 437 | } |
| 438 | cryptonote::blobdata txblob = m_blockchain.get_txpool_tx_blob(txid, relay_category::all); |
| 439 | cryptonote::transaction_prefix tx; |
| 440 | if (!parse_and_validate_tx_prefix_from_blob(txblob, tx)) |
| 441 | { |
| 442 | MERROR("Failed to parse tx from txpool"); |
| 443 | return; |
| 444 | } |
| 445 | // remove first, in case this throws, so key images aren't removed |
| 446 | MINFO("Pruning tx " << txid << " from txpool: weight: " << meta.weight << ", fee/byte: " << it->first.first); |
| 447 | m_blockchain.remove_txpool_tx(txid); |
| 448 | reduce_txpool_weight(meta.weight); |
| 449 | remove_transaction_keyimages(tx, txid); |
| 450 | MINFO("Pruned tx " << txid << " from txpool: weight: " << meta.weight << ", fee/byte: " << it->first.first); |
| 451 | |
| 452 | auto it_prev = it; |
| 453 | --it_prev; |
| 454 | |
| 455 | remove_tx_from_transient_lists(it, txid, !meta.matches(relay_category::broadcasted)); |
| 456 | it = it_prev; |
| 457 | |
| 458 | changed = true; |
| 459 | } |
| 460 | catch (const std::exception &e) |
| 461 | { |
| 462 | MERROR("Error while pruning txpool: " << e.what()); |
| 463 | return; |
| 464 | } |
| 465 | } |
| 466 | lock.commit(); |
| 467 | if (changed) |
| 468 | ++m_cookie; |
| 469 | if (m_txpool_weight > bytes) |
| 470 | MINFO("Pool weight after pruning is larger than limit: " << m_txpool_weight << "/" << bytes); |
| 471 | } |
| 472 | //--------------------------------------------------------------------------------- |
| 473 | bool tx_memory_pool::insert_key_images(const transaction_prefix &tx, const crypto::hash &id, relay_method tx_relay) |
| 474 | { |
| 475 | for(const auto& in: tx.vin) |
| 476 | { |
| 477 | CHECKED_GET_SPECIFIC_VARIANT(in, const txin_to_key, txin, false); |
| 478 | std::unordered_set<crypto::hash>& kei_image_set = m_spent_key_images[txin.k_image]; |
| 479 | |
| 480 | // Only allow multiple txes per key-image if kept-by-block. Only allow |
| 481 | // the same txid if going from local/stem->fluff. |
| 482 | |
| 483 | if (tx_relay != relay_method::block) |
| 484 | { |
| 485 | const bool one_txid = |
| 486 | (kei_image_set.empty() || (kei_image_set.size() == 1 && *(kei_image_set.cbegin()) == id)); |
| 487 | CHECK_AND_ASSERT_MES(one_txid, false, "internal error: tx_relay=" << unsigned(tx_relay) |
| 488 | << ", kei_image_set.size()=" << kei_image_set.size() << ENDL << "txin.k_image=" << txin.k_image << ENDL |
| 489 | << "tx_id=" << id); |
| 490 | } |
| 491 | |
| 492 | const bool new_or_previously_private = |
| 493 | kei_image_set.insert(id).second || |
| 494 | !m_blockchain.txpool_tx_matches_category(id, relay_category::legacy); |
| 495 | CHECK_AND_ASSERT_MES(new_or_previously_private, false, "internal error: try to insert duplicate iterator in key_image set"); |
| 496 | } |
| 497 | ++m_cookie; |
| 498 | return true; |
| 499 | } |
| 500 | //--------------------------------------------------------------------------------- |
| 501 | //FIXME: Can return early before removal of all of the key images. |
| 502 | // At the least, need to make sure that a false return here |
| 503 | // is treated properly. Should probably not return early, however. |
| 504 | bool tx_memory_pool::remove_transaction_keyimages(const transaction_prefix& tx, const crypto::hash &actual_hash) |
| 505 | { |
| 506 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 507 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 508 | // ND: Speedup |
| 509 | for(const txin_v& vi: tx.vin) |
| 510 | { |
| 511 | CHECKED_GET_SPECIFIC_VARIANT(vi, const txin_to_key, txin, false); |
| 512 | auto it = m_spent_key_images.find(txin.k_image); |
| 513 | CHECK_AND_ASSERT_MES(it != m_spent_key_images.end(), false, "failed to find transaction input in key images. img=" << txin.k_image << ENDL |
| 514 | << "transaction id = " << actual_hash); |
| 515 | std::unordered_set<crypto::hash>& key_image_set = it->second; |
| 516 | CHECK_AND_ASSERT_MES(key_image_set.size(), false, "empty key_image set, img=" << txin.k_image << ENDL |
| 517 | << "transaction id = " << actual_hash); |
| 518 | |
| 519 | auto it_in_set = key_image_set.find(actual_hash); |
| 520 | CHECK_AND_ASSERT_MES(it_in_set != key_image_set.end(), false, "transaction id not found in key_image set, img=" << txin.k_image << ENDL |
| 521 | << "transaction id = " << actual_hash); |
| 522 | key_image_set.erase(it_in_set); |
| 523 | if(!key_image_set.size()) |
| 524 | { |
| 525 | //it is now empty hash container for this key_image |
| 526 | m_spent_key_images.erase(it); |
| 527 | } |
| 528 | |
| 529 | } |
| 530 | ++m_cookie; |
| 531 | return true; |
| 532 | } |
| 533 | //--------------------------------------------------------------------------------- |
| 534 | bool 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, bool &pruned, const bool suppress_missing_msgs) |
| 535 | { |
| 536 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 537 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 538 | |
| 539 | bool sensitive = false; |
| 540 | try |
| 541 | { |
| 542 | LockedTXN lock(m_blockchain.get_db()); |
| 543 | txpool_tx_meta_t meta; |
| 544 | if (!m_blockchain.get_txpool_tx_meta(id, meta)) |
| 545 | { |
| 546 | if (!suppress_missing_msgs) |
| 547 | { |
| 548 | MERROR("Failed to find tx_meta in txpool"); |
| 549 | } |
| 550 | return false; |
| 551 | } |
| 552 | txblob = m_blockchain.get_txpool_tx_blob(id, relay_category::all); |
| 553 | auto ci = m_parsed_tx_cache.find(id); |
| 554 | if (ci != m_parsed_tx_cache.end()) |
| 555 | { |
| 556 | tx = ci->second; |
| 557 | } |
| 558 | else if (!(meta.pruned ? parse_and_validate_tx_base_from_blob(txblob, tx) : parse_and_validate_tx_from_blob(txblob, tx))) |
| 559 | { |
| 560 | MERROR("Failed to parse tx from txpool"); |
| 561 | return false; |
| 562 | } |
| 563 | else |
| 564 | { |
| 565 | tx.set_hash(id); |
| 566 | } |
| 567 | tx_weight = meta.weight; |
| 568 | fee = meta.fee; |
| 569 | relayed = meta.relayed; |
| 570 | do_not_relay = meta.do_not_relay; |
| 571 | double_spend_seen = meta.double_spend_seen; |
| 572 | pruned = meta.pruned; |
| 573 | sensitive = !meta.matches(relay_category::broadcasted); |
| 574 | |
| 575 | // remove first, in case this throws, so key images aren't removed |
| 576 | m_blockchain.remove_txpool_tx(id); |
| 577 | reduce_txpool_weight(tx_weight); |
| 578 | remove_transaction_keyimages(tx, id); |
| 579 | lock.commit(); |
| 580 | } |
| 581 | catch (const std::exception &e) |
| 582 | { |
| 583 | MERROR("Failed to remove tx from txpool: " << e.what()); |
| 584 | return false; |
| 585 | } |
| 586 | |
| 587 | remove_tx_from_transient_lists(find_tx_in_sorted_container(id), id, sensitive); |
| 588 | ++m_cookie; |
| 589 | return true; |
| 590 | } |
| 591 | //--------------------------------------------------------------------------------- |
| 592 | bool tx_memory_pool::get_transaction_info(const crypto::hash &txid, tx_details &td, bool include_sensitive_data, bool include_blob) const |
| 593 | { |
| 594 | PERF_TIMER(get_transaction_info); |
| 595 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 596 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 597 | |
| 598 | try |
| 599 | { |
| 600 | LockedTXN lock(m_blockchain.get_db()); |
| 601 | txpool_tx_meta_t meta; |
| 602 | if (!m_blockchain.get_txpool_tx_meta(txid, meta)) |
| 603 | { |
| 604 | LOG_PRINT_L2("Failed to find tx in txpool: " << txid); |
| 605 | return false; |
| 606 | } |
| 607 | if (!include_sensitive_data && !meta.matches(relay_category::broadcasted)) |
| 608 | { |
| 609 | // We don't want sensitive data && the tx is sensitive, so no need to return it |
| 610 | return false; |
| 611 | } |
| 612 | cryptonote::blobdata txblob = m_blockchain.get_txpool_tx_blob(txid, relay_category::all); |
| 613 | auto ci = m_parsed_tx_cache.find(txid); |
| 614 | if (ci != m_parsed_tx_cache.end()) |
| 615 | { |
| 616 | td.tx = ci->second; |
| 617 | } |
| 618 | else if (!(meta.pruned ? parse_and_validate_tx_base_from_blob(txblob, td.tx) : parse_and_validate_tx_from_blob(txblob, td.tx))) |
| 619 | { |
| 620 | MERROR("Failed to parse tx from txpool"); |
| 621 | return false; |
| 622 | } |
| 623 | else |
| 624 | { |
| 625 | td.tx.set_hash(txid); |
| 626 | } |
| 627 | td.blob_size = txblob.size(); |
| 628 | td.weight = meta.weight; |
| 629 | td.fee = meta.fee; |
| 630 | td.max_used_block_id = meta.max_used_block_id; |
| 631 | td.max_used_block_height = meta.max_used_block_height; |
| 632 | td.kept_by_block = meta.kept_by_block; |
| 633 | td.last_failed_height = meta.last_failed_height; |
| 634 | td.last_failed_id = meta.last_failed_id; |
| 635 | td.receive_time = include_sensitive_data ? meta.receive_time : 0; |
| 636 | td.last_relayed_time = (include_sensitive_data && !meta.dandelionpp_stem) ? meta.last_relayed_time : 0; |
| 637 | td.relayed = meta.relayed; |
| 638 | td.do_not_relay = meta.do_not_relay; |
| 639 | td.double_spend_seen = meta.double_spend_seen; |
| 640 | if (include_blob) |
| 641 | td.tx_blob = std::move(txblob); |
| 642 | } |
| 643 | catch (const std::exception &e) |
| 644 | { |
| 645 | MERROR("Failed to get tx from txpool: " << e.what()); |
| 646 | return false; |
| 647 | } |
| 648 | |
| 649 | return true; |
| 650 | } |
| 651 | //------------------------------------------------------------------ |
| 652 | bool tx_memory_pool::get_transactions_info(const std::vector<crypto::hash>& txids, std::vector<std::pair<crypto::hash, tx_details>>& txs, bool include_sensitive) const |
| 653 | { |
| 654 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 655 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 656 | |
| 657 | txs.clear(); |
| 658 | |
| 659 | for (const auto &it: txids) |
| 660 | { |
| 661 | tx_details details; |
| 662 | bool success = get_transaction_info(it, details, include_sensitive, true/*include_blob*/); |
| 663 | if (success) |
| 664 | { |
| 665 | txs.push_back(std::make_pair(it, std::move(details))); |
| 666 | } |
| 667 | } |
| 668 | return true; |
| 669 | } |
| 670 | //--------------------------------------------------------------------------------- |
| 671 | bool tx_memory_pool::get_complement(std::vector<crypto::hash> hashes, std::vector<cryptonote::blobdata> &txes) const |
| 672 | { |
| 673 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 674 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 675 | |
| 676 | // Sort so we can do binary search later |
| 677 | std::sort(hashes.begin(), hashes.end()); |
| 678 | |
| 679 | m_blockchain.for_all_txpool_txes([this, &hashes, &txes](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata_ref*) { |
| 680 | const auto tx_relay_method = meta.get_relay_method(); |
| 681 | if (tx_relay_method != relay_method::block && tx_relay_method != relay_method::fluff) |
| 682 | return true; |
| 683 | |
| 684 | // Do binary search for our pool TXID in given list, skip to next if already present |
| 685 | const auto hash_it = std::lower_bound(hashes.cbegin(), hashes.cend(), txid); |
| 686 | if (hash_it != hashes.cend() && *hash_it == txid) |
| 687 | return true; |
| 688 | |
| 689 | { |
| 690 | cryptonote::blobdata bd; |
| 691 | try |
| 692 | { |
| 693 | if (!m_blockchain.get_txpool_tx_blob(txid, bd, cryptonote::relay_category::broadcasted)) |
| 694 | { |
| 695 | MERROR("Failed to get blob for txpool transaction " << txid); |
| 696 | return true; |
| 697 | } |
| 698 | txes.emplace_back(std::move(bd)); |
| 699 | } |
| 700 | catch (const std::exception &e) |
| 701 | { |
| 702 | MERROR("Failed to get blob for txpool transaction " << txid << ": " << e.what()); |
| 703 | return true; |
| 704 | } |
| 705 | } |
| 706 | return true; |
| 707 | }, false); |
| 708 | return true; |
| 709 | } |
| 710 | //--------------------------------------------------------------------------------- |
| 711 | void tx_memory_pool::on_idle() |
| 712 | { |
| 713 | m_remove_stuck_tx_interval.do_call([this](){return remove_stuck_transactions();}); |
| 714 | } |
| 715 | //--------------------------------------------------------------------------------- |
| 716 | sorted_tx_container::iterator tx_memory_pool::find_tx_in_sorted_container(const crypto::hash& id) const |
| 717 | { |
| 718 | return std::find_if( m_txs_by_fee_and_receive_time.begin(), m_txs_by_fee_and_receive_time.end() |
| 719 | , [&](const sorted_tx_container::value_type& a){ |
| 720 | return a.second == id; |
| 721 | } |
| 722 | ); |
| 723 | } |
| 724 | //--------------------------------------------------------------------------------- |
| 725 | //TODO: investigate whether boolean return is appropriate |
| 726 | bool tx_memory_pool::remove_stuck_transactions() |
| 727 | { |
| 728 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 729 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 730 | std::list<std::pair<crypto::hash, uint64_t>> remove; |
| 731 | m_blockchain.for_all_txpool_txes([this, &remove](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata_ref*) { |
| 732 | uint64_t tx_age = time(nullptr) - meta.receive_time; |
| 733 | |
| 734 | if((tx_age > CRYPTONOTE_MEMPOOL_TX_LIVETIME && !meta.kept_by_block) || |
| 735 | (tx_age > CRYPTONOTE_MEMPOOL_TX_FROM_ALT_BLOCK_LIVETIME && meta.kept_by_block) ) |
| 736 | { |
| 737 | LOG_PRINT_L1("Tx " << txid << " removed from tx pool due to outdated, age: " << tx_age ); |
| 738 | remove_tx_from_transient_lists(find_tx_in_sorted_container(txid), txid, !meta.matches(relay_category::broadcasted)); |
| 739 | m_timed_out_transactions.insert(txid); |
| 740 | remove.push_back(std::make_pair(txid, meta.weight)); |
| 741 | } |
| 742 | return true; |
| 743 | }, false, relay_category::all); |
| 744 | |
| 745 | if (!remove.empty()) |
| 746 | { |
| 747 | LockedTXN lock(m_blockchain.get_db()); |
| 748 | for (const std::pair<crypto::hash, uint64_t> &entry: remove) |
| 749 | { |
| 750 | const crypto::hash &txid = entry.first; |
| 751 | try |
| 752 | { |
| 753 | cryptonote::blobdata bd = m_blockchain.get_txpool_tx_blob(txid, relay_category::all); |
| 754 | cryptonote::transaction_prefix tx; |
| 755 | if (!parse_and_validate_tx_prefix_from_blob(bd, tx)) |
| 756 | { |
| 757 | MERROR("Failed to parse tx from txpool"); |
| 758 | // continue |
| 759 | } |
| 760 | else |
| 761 | { |
| 762 | // remove first, so we only remove key images if the tx removal succeeds |
| 763 | m_blockchain.remove_txpool_tx(txid); |
| 764 | reduce_txpool_weight(entry.second); |
| 765 | remove_transaction_keyimages(tx, txid); |
| 766 | } |
| 767 | } |
| 768 | catch (const std::exception &e) |
| 769 | { |
| 770 | MWARNING("Failed to remove stuck transaction: " << txid); |
| 771 | // ignore error |
| 772 | } |
| 773 | } |
| 774 | lock.commit(); |
| 775 | ++m_cookie; |
| 776 | } |
| 777 | return true; |
| 778 | } |
| 779 | //--------------------------------------------------------------------------------- |
| 780 | //TODO: investigate whether boolean return is appropriate |
| 781 | bool tx_memory_pool::get_relayable_transactions(std::vector<std::tuple<crypto::hash, cryptonote::blobdata, relay_method>> &txs) |
| 782 | { |
| 783 | using clock = std::chrono::system_clock; |
| 784 | |
| 785 | const uint64_t now = time(NULL); |
| 786 | if (uint64_t{std::numeric_limits<time_t>::max()} < now || time_t(now) < m_next_check) |
| 787 | return false; |
| 788 | |
| 789 | uint64_t next_check = clock::to_time_t(clock::from_time_t(time_t(now)) + max_relayable_check); |
| 790 | std::vector<std::pair<crypto::hash, txpool_tx_meta_t>> change_timestamps; |
| 791 | |
| 792 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 793 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 794 | LockedTXN lock(m_blockchain.get_db()); |
| 795 | txs.reserve(m_blockchain.get_txpool_tx_count()); |
| 796 | m_blockchain.for_all_txpool_txes([this, now, &txs, &change_timestamps, &next_check](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata_ref *){ |
| 797 | // 0 fee transactions are never relayed |
| 798 | if(!meta.pruned && meta.fee > 0 && !meta.do_not_relay) |
| 799 | { |
| 800 | const relay_method tx_relay = meta.get_relay_method(); |
| 801 | switch (tx_relay) |
| 802 | { |
| 803 | case relay_method::stem: |
| 804 | case relay_method::forward: |
| 805 | if (meta.last_relayed_time > now) |
| 806 | { |
| 807 | next_check = std::min(next_check, meta.last_relayed_time); |
| 808 | return true; // continue to next tx |
| 809 | } |
| 810 | change_timestamps.emplace_back(txid, meta); |
| 811 | break; |
| 812 | default: |
| 813 | case relay_method::none: |
| 814 | return true; |
| 815 | case relay_method::local: |
| 816 | case relay_method::fluff: |
| 817 | case relay_method::block: |
| 818 | if (now - meta.last_relayed_time <= get_relay_delay(meta.last_relayed_time, meta.receive_time)) |
| 819 | return true; // continue to next tx |
| 820 | break; |
| 821 | } |
| 822 | |
| 823 | // if the tx is older than half the max lifetime, we don't re-relay it, to avoid a problem |
| 824 | // mentioned by smooth where nodes would flush txes at slightly different times, causing |
| 825 | // flushed txes to be re-added when received from a node which was just about to flush it |
| 826 | uint64_t max_age = (tx_relay == relay_method::block) ? CRYPTONOTE_MEMPOOL_TX_FROM_ALT_BLOCK_LIVETIME : CRYPTONOTE_MEMPOOL_TX_LIVETIME; |
| 827 | if (now - meta.receive_time <= max_age / 2) |
| 828 | { |
| 829 | try |
| 830 | { |
| 831 | txs.emplace_back(txid, m_blockchain.get_txpool_tx_blob(txid, relay_category::all), tx_relay); |
| 832 | } |
| 833 | catch (const std::exception &e) |
| 834 | { |
| 835 | MERROR("Failed to get transaction blob from db"); |
| 836 | // ignore error |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | return true; |
| 841 | }, false, relay_category::relayable); |
| 842 | |
| 843 | for (auto& elem : change_timestamps) |
| 844 | { |
| 845 | /* These transactions are still in forward or stem state, so the field |
| 846 | represents the next time a relay should be attempted. Will be |
| 847 | overwritten when the state is upgraded to stem, fluff or block. This |
| 848 | function is only called every ~2 minutes, so this resetting should be |
| 849 | unnecessary, but is primarily a precaution against potential changes |
| 850 | to the callback routines. */ |
| 851 | elem.second.last_relayed_time = now + get_relay_delay(elem.second.last_relayed_time, elem.second.receive_time); |
| 852 | m_blockchain.update_txpool_tx(elem.first, elem.second); |
| 853 | } |
| 854 | |
| 855 | m_next_check = time_t(next_check); |
| 856 | return true; |
| 857 | } |
| 858 | //--------------------------------------------------------------------------------- |
| 859 | void tx_memory_pool::set_relayed(const epee::span<const crypto::hash> hashes, const relay_method method, std::vector<bool> &just_broadcasted) |
| 860 | { |
| 861 | just_broadcasted.clear(); |
| 862 | |
| 863 | crypto::random_poisson_seconds embargo_duration{dandelionpp_embargo_average}; |
| 864 | const auto now = std::chrono::system_clock::now(); |
| 865 | uint64_t next_relay = uint64_t{std::numeric_limits<time_t>::max()}; |
| 866 | |
| 867 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 868 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 869 | LockedTXN lock(m_blockchain.get_db()); |
| 870 | for (const auto& hash : hashes) |
| 871 | { |
| 872 | bool was_just_broadcasted = false; |
| 873 | try |
| 874 | { |
| 875 | txpool_tx_meta_t meta; |
| 876 | if (m_blockchain.get_txpool_tx_meta(hash, meta)) |
| 877 | { |
| 878 | // txes can be received as "stem" or "fluff" in either order |
| 879 | const bool already_broadcasted = meta.matches(relay_category::broadcasted); |
| 880 | meta.upgrade_relay_method(method); |
| 881 | meta.relayed = true; |
| 882 | |
| 883 | if (meta.dandelionpp_stem) |
| 884 | { |
| 885 | meta.last_relayed_time = std::chrono::system_clock::to_time_t(now + embargo_duration()); |
| 886 | next_relay = std::min(next_relay, meta.last_relayed_time); |
| 887 | } |
| 888 | else |
| 889 | meta.last_relayed_time = std::chrono::system_clock::to_time_t(now); |
| 890 | |
| 891 | m_blockchain.update_txpool_tx(hash, meta); |
| 892 | // wait until db update succeeds to ensure tx is visible in the pool |
| 893 | was_just_broadcasted = !already_broadcasted && meta.matches(relay_category::broadcasted); |
| 894 | |
| 895 | if (was_just_broadcasted) |
| 896 | // Make sure the tx gets re-added with an updated time |
| 897 | add_tx_to_transient_lists(hash, meta.fee / (double)meta.weight, std::chrono::system_clock::to_time_t(now)); |
| 898 | } |
| 899 | } |
| 900 | catch (const std::exception &e) |
| 901 | { |
| 902 | MERROR("Failed to update txpool transaction metadata: " << e.what()); |
| 903 | // continue |
| 904 | } |
| 905 | just_broadcasted.emplace_back(was_just_broadcasted); |
| 906 | } |
| 907 | lock.commit(); |
| 908 | set_if_less(m_next_check, time_t(next_relay)); |
| 909 | } |
| 910 | //--------------------------------------------------------------------------------- |
| 911 | size_t tx_memory_pool::get_transactions_count(bool include_sensitive) const |
| 912 | { |
| 913 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 914 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 915 | return m_blockchain.get_txpool_tx_count(include_sensitive); |
| 916 | } |
| 917 | //--------------------------------------------------------------------------------- |
| 918 | void tx_memory_pool::get_transactions(std::vector<transaction>& txs, bool include_sensitive) const |
| 919 | { |
| 920 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 921 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 922 | const relay_category category = include_sensitive ? relay_category::all : relay_category::broadcasted; |
| 923 | txs.reserve(m_blockchain.get_txpool_tx_count(include_sensitive)); |
| 924 | m_blockchain.for_all_txpool_txes([&txs](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata_ref *bd){ |
| 925 | transaction tx; |
| 926 | if (!(meta.pruned ? parse_and_validate_tx_base_from_blob(*bd, tx) : parse_and_validate_tx_from_blob(*bd, tx))) |
| 927 | { |
| 928 | MERROR("Failed to parse tx from txpool"); |
| 929 | // continue |
| 930 | return true; |
| 931 | } |
| 932 | tx.set_hash(txid); |
| 933 | txs.push_back(std::move(tx)); |
| 934 | return true; |
| 935 | }, true, category); |
| 936 | } |
| 937 | //------------------------------------------------------------------ |
| 938 | void tx_memory_pool::get_transaction_hashes(std::vector<crypto::hash>& txs, bool include_sensitive) const |
| 939 | { |
| 940 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 941 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 942 | const relay_category category = include_sensitive ? relay_category::all : relay_category::broadcasted; |
| 943 | txs.reserve(m_blockchain.get_txpool_tx_count(include_sensitive)); |
| 944 | m_blockchain.for_all_txpool_txes([&txs](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata_ref *bd){ |
| 945 | txs.push_back(txid); |
| 946 | return true; |
| 947 | }, false, category); |
| 948 | } |
| 949 | //------------------------------------------------------------------ |
| 950 | bool tx_memory_pool::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 |
| 951 | { |
| 952 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 953 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 954 | |
| 955 | incremental = true; |
| 956 | if (start_time == (time_t)0) |
| 957 | { |
| 958 | // Giving no start time means give back whole pool |
| 959 | incremental = false; |
| 960 | } |
| 961 | else if ((m_added_txs_start_time != (time_t)0) && (m_removed_txs_start_time != (time_t)0)) |
| 962 | { |
| 963 | if ((start_time <= m_added_txs_start_time) || (start_time <= m_removed_txs_start_time)) |
| 964 | { |
| 965 | // If either of the two lists do not go back far enough it's not possible to |
| 966 | // deliver incremental pool info |
| 967 | incremental = false; |
| 968 | } |
| 969 | // The check uses "<=": We cannot be sure to have ALL txs exactly at start_time, only AFTER that time |
| 970 | } |
| 971 | else |
| 972 | { |
| 973 | // Some incremental info still missing completely |
| 974 | incremental = false; |
| 975 | } |
| 976 | |
| 977 | added_txs.clear(); |
| 978 | remaining_added_txids.clear(); |
| 979 | removed_txs.clear(); |
| 980 | |
| 981 | std::vector<crypto::hash> txids; |
| 982 | if (!incremental) |
| 983 | { |
| 984 | LOG_PRINT_L2("Giving back the whole pool"); |
| 985 | // Give back the whole pool in 'added_txs'; because calling 'get_transaction_info' right inside the |
| 986 | // anonymous method somehow results in an LMDB error with transactions we have to build a list of |
| 987 | // ids first and get the full info afterwards |
| 988 | get_transaction_hashes(txids, include_sensitive); |
| 989 | if (txids.size() > max_tx_count) |
| 990 | { |
| 991 | remaining_added_txids = std::vector<crypto::hash>(txids.begin() + max_tx_count, txids.end()); |
| 992 | txids.erase(txids.begin() + max_tx_count, txids.end()); |
| 993 | } |
| 994 | get_transactions_info(txids, added_txs, include_sensitive); |
| 995 | return true; |
| 996 | } |
| 997 | |
| 998 | // Give back incrementally, based on time of entry into the map |
| 999 | for (const auto &pit : m_added_txs_by_id) |
| 1000 | { |
| 1001 | if (pit.second >= start_time) |
| 1002 | txids.push_back(pit.first); |
| 1003 | } |
| 1004 | get_transactions_info(txids, added_txs, include_sensitive); |
| 1005 | if (added_txs.size() > max_tx_count) |
| 1006 | { |
| 1007 | remaining_added_txids.reserve(added_txs.size() - max_tx_count); |
| 1008 | for (size_t i = max_tx_count; i < added_txs.size(); ++i) |
| 1009 | remaining_added_txids.push_back(added_txs[i].first); |
| 1010 | added_txs.erase(added_txs.begin() + max_tx_count, added_txs.end()); |
| 1011 | } |
| 1012 | |
| 1013 | std::multimap<time_t, removed_tx_info>::const_iterator rit = m_removed_txs_by_time.lower_bound(start_time); |
| 1014 | while (rit != m_removed_txs_by_time.end()) |
| 1015 | { |
| 1016 | if (include_sensitive || !rit->second.sensitive) |
| 1017 | { |
| 1018 | removed_txs.push_back(rit->second.txid); |
| 1019 | } |
| 1020 | ++rit; |
| 1021 | } |
| 1022 | return true; |
| 1023 | } |
| 1024 | //------------------------------------------------------------------ |
| 1025 | void tx_memory_pool::get_transaction_backlog(std::vector<tx_backlog_entry>& backlog, bool include_sensitive) const |
| 1026 | { |
| 1027 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 1028 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 1029 | const uint64_t now = time(NULL); |
| 1030 | const relay_category category = include_sensitive ? relay_category::all : relay_category::broadcasted; |
| 1031 | backlog.reserve(m_blockchain.get_txpool_tx_count(include_sensitive)); |
| 1032 | m_blockchain.for_all_txpool_txes([&backlog, now](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata_ref *bd){ |
| 1033 | backlog.push_back({meta.weight, meta.fee, meta.receive_time - now}); |
| 1034 | return true; |
| 1035 | }, false, category); |
| 1036 | } |
| 1037 | //------------------------------------------------------------------ |
| 1038 | void tx_memory_pool::get_block_template_backlog(std::vector<tx_block_template_backlog_entry>& backlog, bool include_sensitive) const |
| 1039 | { |
| 1040 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 1041 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 1042 | |
| 1043 | std::vector<tx_block_template_backlog_entry> tmp; |
| 1044 | uint64_t total_weight = 0; |
| 1045 | |
| 1046 | // First get everything from the mempool, filter it later |
| 1047 | m_blockchain.for_all_txpool_txes([&tmp, &total_weight](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata_ref*){ |
| 1048 | tmp.emplace_back(tx_block_template_backlog_entry{txid, meta.weight, meta.fee}); |
| 1049 | total_weight += meta.weight; |
| 1050 | return true; |
| 1051 | }, false, include_sensitive ? relay_category::all : relay_category::broadcasted); |
| 1052 | |
| 1053 | // Limit backlog to 112.5% of current median weight. This is enough to mine a full block with the optimal block reward |
| 1054 | const uint64_t median_weight = m_blockchain.get_current_cumulative_block_weight_median(); |
| 1055 | const uint64_t max_backlog_weight = median_weight + (median_weight / 8); |
| 1056 | |
| 1057 | // If the total weight is too high, choose the best paying transactions |
| 1058 | if (total_weight > max_backlog_weight) |
| 1059 | std::stable_sort(tmp.begin(), tmp.end(), [](const auto& a, const auto& b){ return a.fee * b.weight > b.fee * a.weight; }); |
| 1060 | |
| 1061 | backlog.clear(); |
| 1062 | uint64_t w = 0; |
| 1063 | |
| 1064 | std::unordered_set<crypto::key_image> k_images; |
| 1065 | |
| 1066 | for (const tx_block_template_backlog_entry& e : tmp) |
| 1067 | { |
| 1068 | try |
| 1069 | { |
| 1070 | txpool_tx_meta_t meta; |
| 1071 | if (!m_blockchain.get_txpool_tx_meta(e.id, meta)) |
| 1072 | continue; |
| 1073 | |
| 1074 | cryptonote::blobdata txblob; |
| 1075 | if (!m_blockchain.get_txpool_tx_blob(e.id, txblob, relay_category::all)) |
| 1076 | continue; |
| 1077 | |
| 1078 | cryptonote::transaction tx; |
| 1079 | if (is_transaction_ready_to_go(meta, e.id, txblob, tx)) |
| 1080 | { |
| 1081 | if (have_key_images(k_images, tx)) |
| 1082 | continue; |
| 1083 | append_key_images(k_images, tx); |
| 1084 | |
| 1085 | backlog.push_back(e); |
| 1086 | w += e.weight; |
| 1087 | if (w > max_backlog_weight) |
| 1088 | break; |
| 1089 | } |
| 1090 | } |
| 1091 | catch (const std::exception &e) |
| 1092 | { |
| 1093 | MERROR("Failed to check transaction readiness: " << e.what()); |
| 1094 | // continue, not fatal |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | //------------------------------------------------------------------ |
| 1099 | void tx_memory_pool::get_transaction_stats(struct txpool_stats& stats, bool include_sensitive) const |
| 1100 | { |
| 1101 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 1102 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 1103 | const uint64_t now = time(NULL); |
| 1104 | const relay_category category = include_sensitive ? relay_category::all : relay_category::broadcasted; |
| 1105 | std::map<uint64_t, txpool_histo> agebytes; |
| 1106 | stats.txs_total = m_blockchain.get_txpool_tx_count(include_sensitive); |
| 1107 | std::vector<uint32_t> weights; |
| 1108 | weights.reserve(stats.txs_total); |
| 1109 | m_blockchain.for_all_txpool_txes([&stats, &weights, now, &agebytes](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata_ref *bd){ |
| 1110 | weights.push_back(meta.weight); |
| 1111 | stats.bytes_total += meta.weight; |
| 1112 | if (!stats.bytes_min || meta.weight < stats.bytes_min) |
| 1113 | stats.bytes_min = meta.weight; |
| 1114 | if (meta.weight > stats.bytes_max) |
| 1115 | stats.bytes_max = meta.weight; |
| 1116 | if (!meta.relayed) |
| 1117 | stats.num_not_relayed++; |
| 1118 | stats.fee_total += meta.fee; |
| 1119 | if (!stats.oldest || meta.receive_time < stats.oldest) |
| 1120 | stats.oldest = meta.receive_time; |
| 1121 | if (meta.receive_time < now - 600) |
| 1122 | stats.num_10m++; |
| 1123 | if (meta.last_failed_height) |
| 1124 | stats.num_failing++; |
| 1125 | uint64_t age = now - meta.receive_time + (now == meta.receive_time); |
| 1126 | agebytes[age].txs++; |
| 1127 | agebytes[age].bytes += meta.weight; |
| 1128 | if (meta.double_spend_seen) |
| 1129 | ++stats.num_double_spends; |
| 1130 | return true; |
| 1131 | }, false, category); |
| 1132 | |
| 1133 | stats.bytes_med = epee::misc_utils::median(weights); |
| 1134 | if (stats.txs_total > 1) |
| 1135 | { |
| 1136 | /* looking for 98th percentile */ |
| 1137 | size_t end = stats.txs_total * 0.02; |
| 1138 | uint64_t delta, factor; |
| 1139 | std::map<uint64_t, txpool_histo>::iterator it, i2; |
| 1140 | if (end) |
| 1141 | { |
| 1142 | /* If enough txs, spread the first 98% of results across |
| 1143 | * the first 9 bins, drop final 2% in last bin. |
| 1144 | */ |
| 1145 | it = agebytes.end(); |
| 1146 | size_t cumulative_num = 0; |
| 1147 | /* Since agebytes is not empty and end is nonzero, the |
| 1148 | * below loop can always run at least once. |
| 1149 | */ |
| 1150 | do { |
| 1151 | --it; |
| 1152 | cumulative_num += it->second.txs; |
| 1153 | } while (it != agebytes.begin() && cumulative_num < end); |
| 1154 | stats.histo_98pc = it->first; |
| 1155 | factor = 9; |
| 1156 | delta = it->first; |
| 1157 | stats.histo.resize(10); |
| 1158 | } else |
| 1159 | { |
| 1160 | /* If not enough txs, don't reserve the last slot; |
| 1161 | * spread evenly across all 10 bins. |
| 1162 | */ |
| 1163 | stats.histo_98pc = 0; |
| 1164 | it = agebytes.end(); |
| 1165 | factor = stats.txs_total > 9 ? 10 : stats.txs_total; |
| 1166 | delta = now - stats.oldest; |
| 1167 | stats.histo.resize(factor); |
| 1168 | } |
| 1169 | if (!delta) |
| 1170 | delta = 1; |
| 1171 | for (i2 = agebytes.begin(); i2 != it; i2++) |
| 1172 | { |
| 1173 | size_t i = (i2->first * factor - 1) / delta; |
| 1174 | stats.histo[i].txs += i2->second.txs; |
| 1175 | stats.histo[i].bytes += i2->second.bytes; |
| 1176 | } |
| 1177 | for (; i2 != agebytes.end(); i2++) |
| 1178 | { |
| 1179 | stats.histo[factor].txs += i2->second.txs; |
| 1180 | stats.histo[factor].bytes += i2->second.bytes; |
| 1181 | } |
| 1182 | } |
| 1183 | } |
| 1184 | //------------------------------------------------------------------ |
| 1185 | //TODO: investigate whether boolean return is appropriate |
| 1186 | bool 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) const |
| 1187 | { |
| 1188 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 1189 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 1190 | const relay_category category = include_sensitive_data ? relay_category::all : relay_category::broadcasted; |
| 1191 | const size_t count = m_blockchain.get_txpool_tx_count(include_sensitive_data); |
| 1192 | tx_infos.reserve(count); |
| 1193 | key_image_infos.reserve(count); |
| 1194 | m_blockchain.for_all_txpool_txes([&tx_infos, key_image_infos, include_sensitive_data](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata_ref *bd){ |
| 1195 | tx_info txi; |
| 1196 | txi.id_hash = epee::string_tools::pod_to_hex(txid); |
| 1197 | txi.tx_blob = blobdata(bd->data(), bd->size()); |
| 1198 | transaction tx; |
| 1199 | if (!(meta.pruned ? parse_and_validate_tx_base_from_blob(*bd, tx) : parse_and_validate_tx_from_blob(*bd, tx))) |
| 1200 | { |
| 1201 | MERROR("Failed to parse tx from txpool"); |
| 1202 | // continue |
| 1203 | return true; |
| 1204 | } |
| 1205 | tx.set_hash(txid); |
| 1206 | txi.tx_json = obj_to_json_str(tx); |
| 1207 | txi.blob_size = bd->size(); |
| 1208 | txi.weight = meta.weight; |
| 1209 | txi.fee = meta.fee; |
| 1210 | txi.kept_by_block = meta.kept_by_block; |
| 1211 | txi.max_used_block_height = meta.max_used_block_height; |
| 1212 | txi.max_used_block_id_hash = epee::string_tools::pod_to_hex(meta.max_used_block_id); |
| 1213 | txi.last_failed_height = meta.last_failed_height; |
| 1214 | txi.last_failed_id_hash = epee::string_tools::pod_to_hex(meta.last_failed_id); |
| 1215 | // In restricted mode we do not include this data: |
| 1216 | txi.receive_time = include_sensitive_data ? meta.receive_time : 0; |
| 1217 | txi.relayed = meta.relayed; |
| 1218 | // In restricted mode we do not include this data: |
| 1219 | txi.last_relayed_time = (include_sensitive_data && !meta.dandelionpp_stem) ? meta.last_relayed_time : 0; |
| 1220 | txi.do_not_relay = meta.do_not_relay; |
| 1221 | txi.double_spend_seen = meta.double_spend_seen; |
| 1222 | tx_infos.push_back(std::move(txi)); |
| 1223 | return true; |
| 1224 | }, true, category); |
| 1225 | |
| 1226 | for (const key_images_container::value_type& kee : m_spent_key_images) { |
| 1227 | const crypto::key_image& k_image = kee.first; |
| 1228 | const std::unordered_set<crypto::hash>& kei_image_set = kee.second; |
| 1229 | spent_key_image_info ki; |
| 1230 | ki.id_hash = epee::string_tools::pod_to_hex(k_image); |
| 1231 | for (const crypto::hash& tx_id_hash : kei_image_set) |
| 1232 | { |
| 1233 | if (m_blockchain.txpool_tx_matches_category(tx_id_hash, category)) |
| 1234 | ki.txs_hashes.push_back(epee::string_tools::pod_to_hex(tx_id_hash)); |
| 1235 | } |
| 1236 | |
| 1237 | // Only return key images for which we have at least one tx that we can show for them |
| 1238 | if (!ki.txs_hashes.empty()) |
| 1239 | key_image_infos.push_back(std::move(ki)); |
| 1240 | } |
| 1241 | return true; |
| 1242 | } |
| 1243 | //--------------------------------------------------------------------------------- |
| 1244 | bool 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 |
| 1245 | { |
| 1246 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 1247 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 1248 | tx_infos.reserve(m_blockchain.get_txpool_tx_count()); |
| 1249 | key_image_infos.reserve(m_blockchain.get_txpool_tx_count()); |
| 1250 | m_blockchain.for_all_txpool_txes([&tx_infos, key_image_infos](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata_ref *bd){ |
| 1251 | cryptonote::rpc::tx_in_pool txi; |
| 1252 | txi.tx_hash = txid; |
| 1253 | if (!(meta.pruned ? parse_and_validate_tx_base_from_blob(*bd, txi.tx) : parse_and_validate_tx_from_blob(*bd, txi.tx))) |
| 1254 | { |
| 1255 | MERROR("Failed to parse tx from txpool"); |
| 1256 | // continue |
| 1257 | return true; |
| 1258 | } |
| 1259 | txi.tx.set_hash(txid); |
| 1260 | txi.blob_size = bd->size(); |
| 1261 | txi.weight = meta.weight; |
| 1262 | txi.fee = meta.fee; |
| 1263 | txi.kept_by_block = meta.kept_by_block; |
| 1264 | txi.max_used_block_height = meta.max_used_block_height; |
| 1265 | txi.max_used_block_hash = meta.max_used_block_id; |
| 1266 | txi.last_failed_block_height = meta.last_failed_height; |
| 1267 | txi.last_failed_block_hash = meta.last_failed_id; |
| 1268 | txi.receive_time = meta.receive_time; |
| 1269 | txi.relayed = meta.relayed; |
| 1270 | txi.last_relayed_time = meta.dandelionpp_stem ? 0 : meta.last_relayed_time; |
| 1271 | txi.do_not_relay = meta.do_not_relay; |
| 1272 | txi.double_spend_seen = meta.double_spend_seen; |
| 1273 | tx_infos.push_back(txi); |
| 1274 | return true; |
| 1275 | }, true, relay_category::broadcasted); |
| 1276 | |
| 1277 | for (const key_images_container::value_type& kee : m_spent_key_images) { |
| 1278 | std::vector<crypto::hash> tx_hashes; |
| 1279 | const std::unordered_set<crypto::hash>& kei_image_set = kee.second; |
| 1280 | for (const crypto::hash& tx_id_hash : kei_image_set) |
| 1281 | { |
| 1282 | if (m_blockchain.txpool_tx_matches_category(tx_id_hash, relay_category::broadcasted)) |
| 1283 | tx_hashes.push_back(tx_id_hash); |
| 1284 | } |
| 1285 | |
| 1286 | if (!tx_hashes.empty()) |
| 1287 | key_image_infos[kee.first] = std::move(tx_hashes); |
| 1288 | } |
| 1289 | return true; |
| 1290 | } |
| 1291 | //--------------------------------------------------------------------------------- |
| 1292 | bool tx_memory_pool::check_for_key_images(const std::vector<crypto::key_image>& key_images, std::vector<bool>& spent) const |
| 1293 | { |
| 1294 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 1295 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 1296 | |
| 1297 | spent.clear(); |
| 1298 | |
| 1299 | for (const auto& image : key_images) |
| 1300 | { |
| 1301 | bool is_spent = false; |
| 1302 | const auto found = m_spent_key_images.find(image); |
| 1303 | if (found != m_spent_key_images.end()) |
| 1304 | { |
| 1305 | for (const crypto::hash& tx_hash : found->second) |
| 1306 | is_spent |= m_blockchain.txpool_tx_matches_category(tx_hash, relay_category::broadcasted); |
| 1307 | } |
| 1308 | spent.push_back(is_spent); |
| 1309 | } |
| 1310 | |
| 1311 | return true; |
| 1312 | } |
| 1313 | //--------------------------------------------------------------------------------- |
| 1314 | bool tx_memory_pool::get_transaction(const crypto::hash& id, cryptonote::blobdata& txblob, relay_category tx_category) const |
| 1315 | { |
| 1316 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 1317 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 1318 | try |
| 1319 | { |
| 1320 | return m_blockchain.get_txpool_tx_blob(id, txblob, tx_category); |
| 1321 | } |
| 1322 | catch (const std::exception &e) |
| 1323 | { |
| 1324 | return false; |
| 1325 | } |
| 1326 | } |
| 1327 | //--------------------------------------------------------------------------------- |
| 1328 | bool tx_memory_pool::on_blockchain_inc(uint64_t new_block_height, const crypto::hash& top_block_id) |
| 1329 | { |
| 1330 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 1331 | m_input_cache.clear(); |
| 1332 | m_parsed_tx_cache.clear(); |
| 1333 | return true; |
| 1334 | } |
| 1335 | //--------------------------------------------------------------------------------- |
| 1336 | bool tx_memory_pool::on_blockchain_dec(uint64_t new_block_height, const crypto::hash& top_block_id) |
| 1337 | { |
| 1338 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 1339 | m_input_cache.clear(); |
| 1340 | m_parsed_tx_cache.clear(); |
| 1341 | return true; |
| 1342 | } |
| 1343 | //--------------------------------------------------------------------------------- |
| 1344 | bool tx_memory_pool::have_tx(const crypto::hash &id, relay_category tx_category) const |
| 1345 | { |
| 1346 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 1347 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 1348 | return m_blockchain.get_db().txpool_has_tx(id, tx_category); |
| 1349 | } |
| 1350 | //--------------------------------------------------------------------------------- |
| 1351 | bool tx_memory_pool::have_tx_keyimges_as_spent(const transaction& tx, const crypto::hash& txid) const |
| 1352 | { |
| 1353 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 1354 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 1355 | for(const auto& in: tx.vin) |
| 1356 | { |
| 1357 | CHECKED_GET_SPECIFIC_VARIANT(in, const txin_to_key, tokey_in, true);//should never fail |
| 1358 | if(have_tx_keyimg_as_spent(tokey_in.k_image, txid)) |
| 1359 | return true; |
| 1360 | } |
| 1361 | return false; |
| 1362 | } |
| 1363 | //--------------------------------------------------------------------------------- |
| 1364 | bool tx_memory_pool::have_tx_keyimg_as_spent(const crypto::key_image& key_im, const crypto::hash& txid) const |
| 1365 | { |
| 1366 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 1367 | const auto found = m_spent_key_images.find(key_im); |
| 1368 | if (found != m_spent_key_images.end() && !found->second.empty()) |
| 1369 | { |
| 1370 | // If another tx is using the key image, always return as spent. |
| 1371 | // See `insert_key_images`. |
| 1372 | if (1 < found->second.size() || *(found->second.cbegin()) != txid) |
| 1373 | return true; |
| 1374 | return m_blockchain.txpool_tx_matches_category(txid, relay_category::legacy); |
| 1375 | } |
| 1376 | return false; |
| 1377 | } |
| 1378 | //--------------------------------------------------------------------------------- |
| 1379 | void tx_memory_pool::lock() const |
| 1380 | { |
| 1381 | m_transactions_lock.lock(); |
| 1382 | } |
| 1383 | //--------------------------------------------------------------------------------- |
| 1384 | void tx_memory_pool::unlock() const |
| 1385 | { |
| 1386 | m_transactions_lock.unlock(); |
| 1387 | } |
| 1388 | //--------------------------------------------------------------------------------- |
| 1389 | bool 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) const |
| 1390 | { |
| 1391 | if (!kept_by_block) |
| 1392 | { |
| 1393 | const std::unordered_map<crypto::hash, std::tuple<bool, tx_verification_context, uint64_t, crypto::hash>>::const_iterator i = m_input_cache.find(txid); |
| 1394 | if (i != m_input_cache.end()) |
| 1395 | { |
| 1396 | max_used_block_height = std::get<2>(i->second); |
| 1397 | max_used_block_id = std::get<3>(i->second); |
| 1398 | tvc = std::get<1>(i->second); |
| 1399 | return std::get<0>(i->second); |
| 1400 | } |
| 1401 | } |
| 1402 | bool ret = m_blockchain.check_tx_inputs(get_tx(), max_used_block_height, max_used_block_id, tvc, kept_by_block); |
| 1403 | if (!kept_by_block) |
| 1404 | m_input_cache.insert(std::make_pair(txid, std::make_tuple(ret, tvc, max_used_block_height, max_used_block_id))); |
| 1405 | return ret; |
| 1406 | } |
| 1407 | //--------------------------------------------------------------------------------- |
| 1408 | bool tx_memory_pool::is_transaction_ready_to_go(txpool_tx_meta_t& txd, const crypto::hash &txid, const cryptonote::blobdata_ref& txblob, transaction &tx) const |
| 1409 | { |
| 1410 | struct transaction_parser |
| 1411 | { |
| 1412 | transaction_parser(const cryptonote::blobdata_ref &txblob, const crypto::hash &txid, transaction &tx): txblob(txblob), txid(txid), tx(tx), parsed(false) {} |
| 1413 | cryptonote::transaction &operator()() |
| 1414 | { |
| 1415 | if (!parsed) |
| 1416 | { |
| 1417 | if (!parse_and_validate_tx_from_blob(txblob, tx)) |
| 1418 | throw std::runtime_error("failed to parse transaction blob"); |
| 1419 | tx.set_hash(txid); |
| 1420 | parsed = true; |
| 1421 | } |
| 1422 | return tx; |
| 1423 | } |
| 1424 | const cryptonote::blobdata_ref &txblob; |
| 1425 | const crypto::hash &txid; |
| 1426 | transaction &tx; |
| 1427 | bool parsed; |
| 1428 | } lazy_tx(txblob, txid, tx); |
| 1429 | |
| 1430 | const std::uint64_t top_block_height{m_blockchain.get_current_blockchain_height() - 1}; |
| 1431 | const crypto::hash top_block_hash{m_blockchain.get_block_id_by_height(top_block_height)}; |
| 1432 | |
| 1433 | if (txd.last_failed_id == top_block_hash) |
| 1434 | return false; // we are already sure that this tx isn't passing for this exact chain |
| 1435 | |
| 1436 | tx_verification_context tvc{}; |
| 1437 | if (!check_tx_inputs([&lazy_tx]()->cryptonote::transaction&{ return lazy_tx(); }, |
| 1438 | txid, |
| 1439 | txd.max_used_block_height, |
| 1440 | txd.max_used_block_id, |
| 1441 | tvc)) |
| 1442 | { |
| 1443 | txd.last_failed_height = top_block_height; |
| 1444 | txd.last_failed_id = top_block_hash; |
| 1445 | return false; |
| 1446 | } |
| 1447 | |
| 1448 | //transaction is ok. |
| 1449 | return true; |
| 1450 | } |
| 1451 | //--------------------------------------------------------------------------------- |
| 1452 | bool tx_memory_pool::is_transaction_ready_to_go(txpool_tx_meta_t& txd, const crypto::hash &txid, const cryptonote::blobdata& txblob, transaction &tx) const |
| 1453 | { |
| 1454 | return is_transaction_ready_to_go(txd, txid, cryptonote::blobdata_ref{txblob.data(), txblob.size()}, tx); |
| 1455 | } |
| 1456 | //--------------------------------------------------------------------------------- |
| 1457 | bool tx_memory_pool::have_key_images(const std::unordered_set<crypto::key_image>& k_images, const transaction_prefix& tx) |
| 1458 | { |
| 1459 | for(size_t i = 0; i!= tx.vin.size(); i++) |
| 1460 | { |
| 1461 | CHECKED_GET_SPECIFIC_VARIANT(tx.vin[i], const txin_to_key, itk, false); |
| 1462 | if(k_images.count(itk.k_image)) |
| 1463 | return true; |
| 1464 | } |
| 1465 | return false; |
| 1466 | } |
| 1467 | //--------------------------------------------------------------------------------- |
| 1468 | bool tx_memory_pool::append_key_images(std::unordered_set<crypto::key_image>& k_images, const transaction_prefix& tx) |
| 1469 | { |
| 1470 | for(size_t i = 0; i!= tx.vin.size(); i++) |
| 1471 | { |
| 1472 | CHECKED_GET_SPECIFIC_VARIANT(tx.vin[i], const txin_to_key, itk, false); |
| 1473 | auto i_res = k_images.insert(itk.k_image); |
| 1474 | CHECK_AND_ASSERT_MES(i_res.second, false, "internal error: key images pool cache - inserted duplicate image in set: " << itk.k_image); |
| 1475 | } |
| 1476 | return true; |
| 1477 | } |
| 1478 | //--------------------------------------------------------------------------------- |
| 1479 | void tx_memory_pool::mark_double_spend(const transaction &tx) |
| 1480 | { |
| 1481 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 1482 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 1483 | bool changed = false; |
| 1484 | LockedTXN lock(m_blockchain.get_db()); |
| 1485 | for(size_t i = 0; i!= tx.vin.size(); i++) |
| 1486 | { |
| 1487 | CHECKED_GET_SPECIFIC_VARIANT(tx.vin[i], const txin_to_key, itk, void()); |
| 1488 | const key_images_container::const_iterator it = m_spent_key_images.find(itk.k_image); |
| 1489 | if (it != m_spent_key_images.end()) |
| 1490 | { |
| 1491 | for (const crypto::hash &txid: it->second) |
| 1492 | { |
| 1493 | txpool_tx_meta_t meta; |
| 1494 | if (!m_blockchain.get_txpool_tx_meta(txid, meta)) |
| 1495 | { |
| 1496 | MERROR("Failed to find tx meta in txpool"); |
| 1497 | // continue, not fatal |
| 1498 | continue; |
| 1499 | } |
| 1500 | if (!meta.double_spend_seen) |
| 1501 | { |
| 1502 | MDEBUG("Marking " << txid << " as double spending " << itk.k_image); |
| 1503 | meta.double_spend_seen = true; |
| 1504 | changed = true; |
| 1505 | try |
| 1506 | { |
| 1507 | m_blockchain.update_txpool_tx(txid, meta); |
| 1508 | } |
| 1509 | catch (const std::exception &e) |
| 1510 | { |
| 1511 | MERROR("Failed to update tx meta: " << e.what()); |
| 1512 | // continue, not fatal |
| 1513 | } |
| 1514 | } |
| 1515 | } |
| 1516 | } |
| 1517 | } |
| 1518 | lock.commit(); |
| 1519 | if (changed) |
| 1520 | ++m_cookie; |
| 1521 | } |
| 1522 | //--------------------------------------------------------------------------------- |
| 1523 | std::string tx_memory_pool::print_pool(bool short_format) const |
| 1524 | { |
| 1525 | std::stringstream ss; |
| 1526 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 1527 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 1528 | m_blockchain.for_all_txpool_txes([&ss, short_format](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata_ref *txblob) { |
| 1529 | ss << "id: " << txid << std::endl; |
| 1530 | if (!short_format) { |
| 1531 | cryptonote::transaction tx; |
| 1532 | if (!(meta.pruned ? parse_and_validate_tx_base_from_blob(*txblob, tx) : parse_and_validate_tx_from_blob(*txblob, tx))) |
| 1533 | { |
| 1534 | MERROR("Failed to parse tx from txpool"); |
| 1535 | return true; // continue |
| 1536 | } |
| 1537 | ss << obj_to_json_str(tx) << std::endl; |
| 1538 | } |
| 1539 | ss << "blob_size: " << (short_format ? "-" : std::to_string(txblob->size())) << std::endl |
| 1540 | << "weight: " << meta.weight << std::endl |
| 1541 | << "fee: " << print_money(meta.fee) << std::endl |
| 1542 | << "kept_by_block: " << (meta.kept_by_block ? 'T' : 'F') << std::endl |
| 1543 | << "is_local" << (meta.is_local ? 'T' : 'F') << std::endl |
| 1544 | << "double_spend_seen: " << (meta.double_spend_seen ? 'T' : 'F') << std::endl |
| 1545 | << "max_used_block_height: " << meta.max_used_block_height << std::endl |
| 1546 | << "max_used_block_id: " << meta.max_used_block_id << std::endl |
| 1547 | << "last_failed_height: " << meta.last_failed_height << std::endl |
| 1548 | << "last_failed_id: " << meta.last_failed_id << std::endl; |
| 1549 | return true; |
| 1550 | }, !short_format, relay_category::all); |
| 1551 | |
| 1552 | return ss.str(); |
| 1553 | } |
| 1554 | //--------------------------------------------------------------------------------- |
| 1555 | //TODO: investigate whether boolean return is appropriate |
| 1556 | bool 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) |
| 1557 | { |
| 1558 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 1559 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 1560 | |
| 1561 | uint64_t best_coinbase = 0, coinbase = 0; |
| 1562 | total_weight = 0; |
| 1563 | fee = 0; |
| 1564 | |
| 1565 | //baseline empty block |
| 1566 | if (!get_block_reward(median_weight, total_weight, already_generated_coins, best_coinbase, version)) |
| 1567 | { |
| 1568 | MERROR("Failed to get block reward for empty block"); |
| 1569 | return false; |
| 1570 | } |
| 1571 | |
| 1572 | |
| 1573 | size_t max_total_weight_pre_v5 = (130 * median_weight) / 100 - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE; |
| 1574 | size_t max_total_weight_v5 = 2 * median_weight - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE; |
| 1575 | size_t max_total_weight = version >= 5 ? max_total_weight_v5 : max_total_weight_pre_v5; |
| 1576 | std::unordered_set<crypto::key_image> k_images; |
| 1577 | |
| 1578 | LOG_PRINT_L2("Filling block template, median weight " << median_weight << ", " << m_txs_by_fee_and_receive_time.size() << " txes in the pool"); |
| 1579 | |
| 1580 | LockedTXN lock(m_blockchain.get_db()); |
| 1581 | |
| 1582 | auto sorted_it = m_txs_by_fee_and_receive_time.begin(); |
| 1583 | for (; sorted_it != m_txs_by_fee_and_receive_time.end(); ++sorted_it) |
| 1584 | { |
| 1585 | txpool_tx_meta_t meta; |
| 1586 | if (!m_blockchain.get_txpool_tx_meta(sorted_it->second, meta)) |
| 1587 | { |
| 1588 | static bool warned = false; |
| 1589 | if (!warned) |
| 1590 | MERROR(" failed to find tx meta: " << sorted_it->second << " (will only print once)"); |
| 1591 | warned = true; |
| 1592 | continue; |
| 1593 | } |
| 1594 | LOG_PRINT_L2("Considering " << sorted_it->second << ", weight " << meta.weight << ", current block weight " << total_weight << "/" << max_total_weight << ", current coinbase " << print_money(best_coinbase) << ", relay method " << (unsigned)meta.get_relay_method()); |
| 1595 | |
| 1596 | if (!meta.matches(relay_category::legacy) && !(m_mine_stem_txes && meta.get_relay_method() == relay_method::stem)) |
| 1597 | { |
| 1598 | LOG_PRINT_L2(" tx relay method is " << (unsigned)meta.get_relay_method()); |
| 1599 | continue; |
| 1600 | } |
| 1601 | if (meta.pruned) |
| 1602 | { |
| 1603 | LOG_PRINT_L2(" tx is pruned"); |
| 1604 | continue; |
| 1605 | } |
| 1606 | |
| 1607 | // Can not exceed maximum block weight |
| 1608 | if (max_total_weight < total_weight + meta.weight) |
| 1609 | { |
| 1610 | LOG_PRINT_L2(" would exceed maximum block weight"); |
| 1611 | continue; |
| 1612 | } |
| 1613 | |
| 1614 | // start using the optimal filling algorithm from v5 |
| 1615 | if (version >= 5) |
| 1616 | { |
| 1617 | // If we're getting lower coinbase tx, |
| 1618 | // stop including more tx |
| 1619 | uint64_t block_reward; |
| 1620 | if(!get_block_reward(median_weight, total_weight + meta.weight, already_generated_coins, block_reward, version)) |
| 1621 | { |
| 1622 | LOG_PRINT_L2(" would exceed maximum block weight"); |
| 1623 | continue; |
| 1624 | } |
| 1625 | coinbase = block_reward + fee + meta.fee; |
| 1626 | if (coinbase < template_accept_threshold(best_coinbase)) |
| 1627 | { |
| 1628 | LOG_PRINT_L2(" would decrease coinbase to " << print_money(coinbase)); |
| 1629 | continue; |
| 1630 | } |
| 1631 | } |
| 1632 | else |
| 1633 | { |
| 1634 | // If we've exceeded the penalty free weight, |
| 1635 | // stop including more tx |
| 1636 | if (total_weight > median_weight) |
| 1637 | { |
| 1638 | LOG_PRINT_L2(" would exceed median block weight"); |
| 1639 | break; |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | // "local" and "stem" txes are filtered above |
| 1644 | cryptonote::blobdata txblob = m_blockchain.get_txpool_tx_blob(sorted_it->second, relay_category::all); |
| 1645 | |
| 1646 | cryptonote::transaction tx; |
| 1647 | |
| 1648 | // Skip transactions that are not ready to be |
| 1649 | // included into the blockchain or that are |
| 1650 | // missing key images |
| 1651 | const cryptonote::txpool_tx_meta_t original_meta = meta; |
| 1652 | bool ready = false; |
| 1653 | try |
| 1654 | { |
| 1655 | ready = is_transaction_ready_to_go(meta, sorted_it->second, txblob, tx); |
| 1656 | } |
| 1657 | catch (const std::exception &e) |
| 1658 | { |
| 1659 | MERROR("Failed to check transaction readiness: " << e.what()); |
| 1660 | // continue, not fatal |
| 1661 | } |
| 1662 | if (memcmp(&original_meta, &meta, sizeof(meta))) |
| 1663 | { |
| 1664 | try |
| 1665 | { |
| 1666 | m_blockchain.update_txpool_tx(sorted_it->second, meta); |
| 1667 | } |
| 1668 | catch (const std::exception &e) |
| 1669 | { |
| 1670 | MERROR("Failed to update tx meta: " << e.what()); |
| 1671 | // continue, not fatal |
| 1672 | } |
| 1673 | } |
| 1674 | if (!ready) |
| 1675 | { |
| 1676 | LOG_PRINT_L2(" not ready to go"); |
| 1677 | continue; |
| 1678 | } |
| 1679 | if (have_key_images(k_images, tx)) |
| 1680 | { |
| 1681 | LOG_PRINT_L2(" key images already seen"); |
| 1682 | continue; |
| 1683 | } |
| 1684 | |
| 1685 | bl.tx_hashes.push_back(sorted_it->second); |
| 1686 | total_weight += meta.weight; |
| 1687 | fee += meta.fee; |
| 1688 | best_coinbase = coinbase; |
| 1689 | append_key_images(k_images, tx); |
| 1690 | LOG_PRINT_L2(" added, new block weight " << total_weight << "/" << max_total_weight << ", coinbase " << print_money(best_coinbase)); |
| 1691 | } |
| 1692 | lock.commit(); |
| 1693 | |
| 1694 | expected_reward = best_coinbase; |
| 1695 | LOG_PRINT_L2("Block template filled with " << bl.tx_hashes.size() << " txes, weight " |
| 1696 | << total_weight << "/" << max_total_weight << ", coinbase " << print_money(best_coinbase) |
| 1697 | << " (including " << print_money(fee) << " in fees)"); |
| 1698 | return true; |
| 1699 | } |
| 1700 | //--------------------------------------------------------------------------------- |
| 1701 | size_t tx_memory_pool::validate(uint8_t version) |
| 1702 | { |
| 1703 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 1704 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 1705 | |
| 1706 | // Simply throw away incremental info, too difficult to update |
| 1707 | m_added_txs_by_id.clear(); |
| 1708 | m_added_txs_start_time = (time_t)0; |
| 1709 | m_removed_txs_by_time.clear(); |
| 1710 | m_removed_txs_start_time = (time_t)0; |
| 1711 | |
| 1712 | MINFO("Validating txpool contents for v" << (unsigned)version); |
| 1713 | |
| 1714 | LockedTXN lock(m_blockchain.get_db()); |
| 1715 | |
| 1716 | struct tx_entry_t |
| 1717 | { |
| 1718 | crypto::hash txid; |
| 1719 | txpool_tx_meta_t meta; |
| 1720 | }; |
| 1721 | |
| 1722 | // get all txids |
| 1723 | std::vector<tx_entry_t> txes; |
| 1724 | m_blockchain.for_all_txpool_txes([&txes](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata_ref*) { |
| 1725 | if (!meta.pruned) // skip pruned txes |
| 1726 | txes.push_back({txid, meta}); |
| 1727 | return true; |
| 1728 | }, false, relay_category::all); |
| 1729 | |
| 1730 | // take them all out and add them back in, some might fail |
| 1731 | size_t added = 0; |
| 1732 | for (auto &e: txes) |
| 1733 | { |
| 1734 | try |
| 1735 | { |
| 1736 | size_t weight; |
| 1737 | uint64_t fee; |
| 1738 | cryptonote::transaction tx; |
| 1739 | cryptonote::blobdata blob; |
| 1740 | bool relayed, do_not_relay, double_spend_seen, pruned; |
| 1741 | if (!take_tx(e.txid, tx, blob, weight, fee, relayed, do_not_relay, double_spend_seen, pruned)) |
| 1742 | MERROR("Failed to get tx " << e.txid << " from txpool for re-validation"); |
| 1743 | |
| 1744 | cryptonote::tx_verification_context tvc{}; |
| 1745 | relay_method tx_relay = e.meta.get_relay_method(); |
| 1746 | if (!add_tx(tx, e.txid, blob, e.meta.weight, tvc, tx_relay, relayed, version)) |
| 1747 | { |
| 1748 | MINFO("Failed to re-validate tx " << e.txid << " for v" << (unsigned)version << ", dropped"); |
| 1749 | continue; |
| 1750 | } |
| 1751 | m_blockchain.update_txpool_tx(e.txid, e.meta); |
| 1752 | ++added; |
| 1753 | } |
| 1754 | catch (const std::exception &e) |
| 1755 | { |
| 1756 | MERROR("Failed to re-validate tx from pool"); |
| 1757 | continue; |
| 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | lock.commit(); |
| 1762 | |
| 1763 | const size_t n_removed = txes.size() - added; |
| 1764 | if (n_removed > 0) |
| 1765 | ++m_cookie; |
| 1766 | return n_removed; |
| 1767 | } |
| 1768 | //--------------------------------------------------------------------------------- |
| 1769 | void tx_memory_pool::add_tx_to_transient_lists(const crypto::hash& txid, double fee, time_t receive_time) |
| 1770 | { |
| 1771 | |
| 1772 | time_t now = time(NULL); |
| 1773 | const std::unordered_map<crypto::hash, time_t>::iterator it = m_added_txs_by_id.find(txid); |
| 1774 | if (it == m_added_txs_by_id.end()) |
| 1775 | { |
| 1776 | m_added_txs_by_id.insert(std::make_pair(txid, now)); |
| 1777 | } |
| 1778 | else |
| 1779 | { |
| 1780 | // This tx was already added to the map earlier, probably because then it was in the "stem" |
| 1781 | // phase of Dandelion++ and now is in the "fluff" phase i.e. got broadcasted: We have to set |
| 1782 | // a new time for clients that are not allowed to see sensitive txs to make sure they will |
| 1783 | // see it now if they query incrementally |
| 1784 | it->second = now; |
| 1785 | |
| 1786 | auto sorted_it = find_tx_in_sorted_container(txid); |
| 1787 | if (sorted_it == m_txs_by_fee_and_receive_time.end()) |
| 1788 | { |
| 1789 | MDEBUG("Re-adding tx " << txid << " to tx pool, but it was not found in the sorted txs container"); |
| 1790 | } |
| 1791 | else |
| 1792 | { |
| 1793 | m_txs_by_fee_and_receive_time.erase(sorted_it); |
| 1794 | } |
| 1795 | } |
| 1796 | m_txs_by_fee_and_receive_time.emplace(std::pair<double, time_t>(fee, receive_time), txid); |
| 1797 | |
| 1798 | // Don't check for "resurrected" txs in case of reorgs i.e. don't check in 'm_removed_txs_by_time' |
| 1799 | // whether we have that txid there and if yes remove it; this results in possible duplicates |
| 1800 | // where we return certain txids as deleted AND in the pool at the same time which requires |
| 1801 | // clients to process deleted ones BEFORE processing pool txs |
| 1802 | if (m_added_txs_start_time == (time_t)0) |
| 1803 | { |
| 1804 | m_added_txs_start_time = now; |
| 1805 | } |
| 1806 | } |
| 1807 | //--------------------------------------------------------------------------------- |
| 1808 | void tx_memory_pool::remove_tx_from_transient_lists(const cryptonote::sorted_tx_container::iterator& sorted_it, const crypto::hash& txid, bool sensitive) |
| 1809 | { |
| 1810 | if (sorted_it == m_txs_by_fee_and_receive_time.end()) |
| 1811 | { |
| 1812 | LOG_PRINT_L1("Removing tx " << txid << " from tx pool, but it was not found in the sorted txs container!"); |
| 1813 | } |
| 1814 | else |
| 1815 | { |
| 1816 | m_txs_by_fee_and_receive_time.erase(sorted_it); |
| 1817 | } |
| 1818 | |
| 1819 | const std::unordered_map<crypto::hash, time_t>::iterator it = m_added_txs_by_id.find(txid); |
| 1820 | if (it != m_added_txs_by_id.end()) |
| 1821 | { |
| 1822 | m_added_txs_by_id.erase(it); |
| 1823 | } |
| 1824 | else |
| 1825 | { |
| 1826 | MDEBUG("Removing tx " << txid << " from tx pool, but it was not found in the map of added txs"); |
| 1827 | } |
| 1828 | track_removed_tx(txid, sensitive); |
| 1829 | } |
| 1830 | //--------------------------------------------------------------------------------- |
| 1831 | void tx_memory_pool::track_removed_tx(const crypto::hash& txid, bool sensitive) |
| 1832 | { |
| 1833 | time_t now = time(NULL); |
| 1834 | m_removed_txs_by_time.insert(std::make_pair(now, removed_tx_info{txid, sensitive})); |
| 1835 | MDEBUG("Transaction removed from pool: txid " << txid << ", total entries in removed list now " << m_removed_txs_by_time.size()); |
| 1836 | if (m_removed_txs_start_time == (time_t)0) |
| 1837 | { |
| 1838 | m_removed_txs_start_time = now; |
| 1839 | } |
| 1840 | |
| 1841 | // Simple system to make sure the list of removed ids does not swell to an unmanageable size: Set |
| 1842 | // an absolute size limit plus delete entries that are x minutes old (which is ok because clients |
| 1843 | // will sync with sensible time intervalls and should not ask for incremental info e.g. 1 hour back) |
| 1844 | const int MAX_REMOVED = 20000; |
| 1845 | if (m_removed_txs_by_time.size() > MAX_REMOVED) |
| 1846 | { |
| 1847 | auto erase_it = m_removed_txs_by_time.begin(); |
| 1848 | std::advance(erase_it, MAX_REMOVED / 4 + 1); |
| 1849 | m_removed_txs_by_time.erase(m_removed_txs_by_time.begin(), erase_it); |
| 1850 | m_removed_txs_start_time = m_removed_txs_by_time.begin()->first; |
| 1851 | MDEBUG("Erased old transactions from big removed list, leaving " << m_removed_txs_by_time.size()); |
| 1852 | } |
| 1853 | else |
| 1854 | { |
| 1855 | time_t earliest = now - (30 * 60); // 30 minutes |
| 1856 | std::map<time_t, removed_tx_info>::iterator from, to; |
| 1857 | from = m_removed_txs_by_time.begin(); |
| 1858 | to = m_removed_txs_by_time.lower_bound(earliest); |
| 1859 | int distance = std::distance(from, to); |
| 1860 | if (distance > 0) |
| 1861 | { |
| 1862 | m_removed_txs_by_time.erase(from, to); |
| 1863 | m_removed_txs_start_time = earliest; |
| 1864 | MDEBUG("Erased " << distance << " old transactions from removed list, leaving " << m_removed_txs_by_time.size()); |
| 1865 | } |
| 1866 | } |
| 1867 | } |
| 1868 | //--------------------------------------------------------------------------------- |
| 1869 | bool tx_memory_pool::init(size_t max_txpool_weight, bool mine_stem_txes) |
| 1870 | { |
| 1871 | CRITICAL_REGION_LOCAL(m_transactions_lock); |
| 1872 | CRITICAL_REGION_LOCAL1(m_blockchain); |
| 1873 | |
| 1874 | m_txpool_max_weight = max_txpool_weight ? max_txpool_weight : DEFAULT_TXPOOL_MAX_WEIGHT; |
| 1875 | m_txs_by_fee_and_receive_time.clear(); |
| 1876 | m_added_txs_by_id.clear(); |
| 1877 | m_added_txs_start_time = (time_t)0; |
| 1878 | m_removed_txs_by_time.clear(); |
| 1879 | m_removed_txs_start_time = (time_t)0; |
| 1880 | m_spent_key_images.clear(); |
| 1881 | m_txpool_weight = 0; |
| 1882 | std::vector<crypto::hash> remove; |
| 1883 | |
| 1884 | // first add the not kept by block, then the kept by block, |
| 1885 | // to avoid rejection due to key image collision |
| 1886 | for (int pass = 0; pass < 2; ++pass) |
| 1887 | { |
| 1888 | const bool kept = pass == 1; |
| 1889 | bool r = m_blockchain.for_all_txpool_txes([this, &remove, kept](const crypto::hash &txid, const txpool_tx_meta_t &meta, const cryptonote::blobdata_ref *bd) { |
| 1890 | if (!!kept != !!meta.kept_by_block) |
| 1891 | return true; |
| 1892 | cryptonote::transaction_prefix tx; |
| 1893 | if (!parse_and_validate_tx_prefix_from_blob(*bd, tx)) |
| 1894 | { |
| 1895 | MWARNING("Failed to parse tx from txpool, removing"); |
| 1896 | remove.push_back(txid); |
| 1897 | return true; |
| 1898 | } |
| 1899 | if (!insert_key_images(tx, txid, meta.get_relay_method())) |
| 1900 | { |
| 1901 | MFATAL("Failed to insert key images from txpool tx"); |
| 1902 | return false; |
| 1903 | } |
| 1904 | add_tx_to_transient_lists(txid, meta.fee / (double)meta.weight, meta.receive_time); |
| 1905 | m_txpool_weight += meta.weight; |
| 1906 | return true; |
| 1907 | }, true, relay_category::all); |
| 1908 | if (!r) |
| 1909 | return false; |
| 1910 | } |
| 1911 | if (!remove.empty()) |
| 1912 | { |
| 1913 | LockedTXN lock(m_blockchain.get_db()); |
| 1914 | for (const auto &txid: remove) |
| 1915 | { |
| 1916 | try |
| 1917 | { |
| 1918 | m_blockchain.remove_txpool_tx(txid); |
| 1919 | } |
| 1920 | catch (const std::exception &e) |
| 1921 | { |
| 1922 | MWARNING("Failed to remove corrupt transaction: " << txid); |
| 1923 | // ignore error |
| 1924 | } |
| 1925 | } |
| 1926 | lock.commit(); |
| 1927 | } |
| 1928 | |
| 1929 | m_mine_stem_txes = mine_stem_txes; |
| 1930 | m_cookie = 0; |
| 1931 | |
| 1932 | // Ignore deserialization error |
| 1933 | return true; |
| 1934 | } |
| 1935 | |
| 1936 | //--------------------------------------------------------------------------------- |
| 1937 | bool tx_memory_pool::deinit() |
| 1938 | { |
| 1939 | return true; |
| 1940 | } |
| 1941 | } |