block_assemble.cpp raw

   1  // Copyright (c) 2011-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 <bench/bench.h>
   6  #include <consensus/consensus.h>
   7  #include <node/miner.h>
   8  #include <primitives/transaction.h>
   9  #include <random.h>
  10  #include <script/script.h>
  11  #include <sync.h>
  12  #include <test/util/mining.h>
  13  #include <test/util/script.h>
  14  #include <test/util/setup_common.h>
  15  #include <validation.h>
  16  
  17  #include <array>
  18  #include <cassert>
  19  #include <cstddef>
  20  #include <memory>
  21  #include <vector>
  22  
  23  using node::BlockAssembler;
  24  
  25  static void AssembleBlock(benchmark::Bench& bench)
  26  {
  27      const auto test_setup = MakeNoLogFileContext<const TestingSetup>();
  28  
  29      CScriptWitness witness;
  30      witness.stack.push_back(WITNESS_STACK_ELEM_OP_TRUE);
  31      BlockAssembler::Options options;
  32      options.coinbase_output_script = P2WSH_OP_TRUE;
  33  
  34      // Collect some loose transactions that spend the coinbases of our mined blocks
  35      constexpr size_t NUM_BLOCKS{200};
  36      std::array<CTransactionRef, NUM_BLOCKS - COINBASE_MATURITY + 1> txs;
  37      for (size_t b{0}; b < NUM_BLOCKS; ++b) {
  38          CMutableTransaction tx;
  39          tx.vin.emplace_back(MineBlock(test_setup->m_node, options));
  40          tx.vin.back().scriptWitness = witness;
  41          tx.vout.emplace_back(1337, P2WSH_OP_TRUE);
  42          if (NUM_BLOCKS - b >= COINBASE_MATURITY)
  43              txs.at(b) = MakeTransactionRef(tx);
  44      }
  45      {
  46          LOCK(::cs_main);
  47  
  48          for (const auto& txr : txs) {
  49              const MempoolAcceptResult res = test_setup->m_node.chainman->ProcessTransaction(txr);
  50              assert(res.m_result_type == MempoolAcceptResult::ResultType::VALID);
  51          }
  52      }
  53  
  54      bench.run([&] {
  55          PrepareBlock(test_setup->m_node, options);
  56      });
  57  }
  58  static void BlockAssemblerAddPackageTxns(benchmark::Bench& bench)
  59  {
  60      FastRandomContext det_rand{true};
  61      auto testing_setup{MakeNoLogFileContext<TestChain100Setup>()};
  62      testing_setup->PopulateMempool(det_rand, /*num_transactions=*/1000, /*submit=*/true);
  63      BlockAssembler::Options assembler_options;
  64      assembler_options.test_block_validity = false;
  65      assembler_options.coinbase_output_script = P2WSH_OP_TRUE;
  66  
  67      bench.run([&] {
  68          PrepareBlock(testing_setup->m_node, assembler_options);
  69      });
  70  }
  71  
  72  BENCHMARK(AssembleBlock, benchmark::PriorityLevel::HIGH);
  73  BENCHMARK(BlockAssemblerAddPackageTxns, benchmark::PriorityLevel::LOW);
  74