mempool.cpp raw

   1  // Copyright (c) 2022 The Limenka developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #include <consensus/amount.h>
   6  #include <consensus/consensus.h>
   7  #include <kernel/mempool_entry.h>
   8  #include <primitives/transaction.h>
   9  #include <test/fuzz/FuzzedDataProvider.h>
  10  #include <test/fuzz/util.h>
  11  #include <test/fuzz/util/mempool.h>
  12  
  13  #include <cassert>
  14  #include <cstdint>
  15  #include <limits>
  16  
  17  bool SanityCheckForConsumeTxMemPoolEntry(const CTransaction& tx) noexcept
  18  {
  19      try {
  20          (void)tx.GetValueOut();
  21          return true;
  22      } catch (const std::runtime_error&) {
  23          return false;
  24      }
  25  }
  26  
  27  // NOTE: Transaction must pass SanityCheckForConsumeTxMemPoolEntry first
  28  CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider& fuzzed_data_provider, const CTransaction& tx) noexcept
  29  {
  30      // Avoid:
  31      // policy/feerate.cpp:28:34: runtime error: signed integer overflow: 34873208148477500 * 1000 cannot be represented in type 'long'
  32      //
  33      // Reproduce using CFeeRate(348732081484775, 10).GetFeePerK()
  34      const CAmount fee{ConsumeMoney(fuzzed_data_provider, /*max=*/std::numeric_limits<CAmount>::max() / CAmount{100'000})};
  35      assert(MoneyRange(fee));
  36      const int64_t time = fuzzed_data_provider.ConsumeIntegral<int64_t>();
  37      const uint64_t entry_sequence{fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
  38      const double coin_age = fuzzed_data_provider.ConsumeFloatingPoint<double>();
  39      const unsigned int entry_height = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, std::numeric_limits<unsigned int>::max() - 1);
  40      const bool spends_coinbase = fuzzed_data_provider.ConsumeBool();
  41      const int32_t extra_weight = fuzzed_data_provider.ConsumeIntegralInRange<int32_t>(0, GetTransactionWeight(tx) * 3);
  42      const unsigned int sig_op_cost = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, MAX_BLOCK_SIGOPS_COST);
  43      return CTxMemPoolEntry{MakeTransactionRef(tx), fee, time, entry_height, entry_sequence, {
  44          .inputs_coin_age = static_cast<uint64_t>(coin_age),
  45          .in_chain_input_value = tx.GetValueOut(),
  46      },
  47          /*spends_coinbase=*/ spends_coinbase,
  48          /*extra_weight=*/ extra_weight,
  49          /*sigops_cost=*/ sig_op_cost,
  50          /*lp=*/ {}};
  51  }
  52