LCOV - code coverage report
Current view: top level - src/node - transaction.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 76 77 98.7 %
Date: 2026-06-25 07:23:43 Functions: 5 5 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2021 The Bitcoin Core developers
       3             : // Distributed under the MIT software license, see the accompanying
       4             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5             : 
       6             : #include <consensus/validation.h>
       7             : #include <index/txindex.h>
       8             : #include <net.h>
       9             : #include <net_processing.h>
      10             : #include <node/blockstorage.h>
      11             : #include <txmempool.h>
      12             : #include <validation.h>
      13             : #include <validationinterface.h>
      14             : #include <node/context.h>
      15             : #include <node/transaction.h>
      16             : #include <util/translation.h>
      17             : 
      18             : #include <future>
      19             : 
      20             : namespace node {
      21        1444 : static TransactionError HandleATMPError(const TxValidationState& state, std::string& err_string_out) {
      22        1444 :     err_string_out = state.ToString();
      23        1444 :     if (state.IsInvalid()) {
      24        1444 :         if (state.GetResult() == TxValidationResult::TX_MISSING_INPUTS) {
      25          46 :             return TransactionError::MISSING_INPUTS;
      26             :         }
      27        1398 :         return TransactionError::MEMPOOL_REJECTED;
      28             :     } else {
      29           0 :         return TransactionError::MEMPOOL_ERROR;
      30             :     }
      31        1444 : }
      32             : 
      33       18062 : TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef tx, bilingual_str& err_string, const CAmount& max_tx_fee, bool relay, bool wait_callback, bool bypass_limits)
      34             : {
      35             :     // BroadcastTransaction can be called by RPC or by the wallet.
      36             :     // chainman, mempool and peerman are initialized before the RPC server and wallet are started
      37             :     // and reset after the RPC sever and wallet are stopped.
      38       18062 :     assert(node.chainman);
      39       18062 :     assert(node.mempool);
      40       18062 :     assert(node.peerman);
      41             : 
      42       18062 :     std::promise<void> promise;
      43       18062 :     uint256 txid = tx->GetHash();
      44       18062 :     bool callback_set = false;
      45             : 
      46             :     {
      47       18062 :         LOCK(cs_main);
      48             : 
      49             :             // If the transaction is already confirmed in the chain, don't do anything
      50             :         // and return early.
      51       18062 :         CCoinsViewCache &view = node.chainman->ActiveChainstate().CoinsTip();
      52      116855 :         for (size_t o = 0; o < tx->vout.size(); o++) {
      53       98802 :             const Coin& existingCoin = view.AccessCoin(COutPoint(txid, o));
      54             :             // IsSpent does not mean the coin is spent, it means the output does not exist.
      55             :             // So if the output does exist, then this transaction exists in the chain.
      56       98802 :             if (!existingCoin.IsSpent()) return TransactionError::ALREADY_IN_CHAIN;
      57       98793 :         }
      58       36106 :         if (auto mempool_tx = node.mempool->get(txid); mempool_tx) {
      59             :             // There's already a transaction in the mempool with this txid. Don't
      60             :             // try to submit this transaction to the mempool (since it'll be
      61             :             // rejected as a TX_CONFLICT), but do attempt to reannounce the mempool
      62             :             // transaction if relay=true.
      63             :             //
      64         448 :         } else {
      65             :             // Transaction is not already in the mempool.
      66       17605 :             if (max_tx_fee > 0) {
      67             :                 // First, call ATMP with test_accept and check the fee. If ATMP
      68             :                 // fails here, return error immediately.
      69        7600 :                 const MempoolAcceptResult result = node.chainman->ProcessTransaction(tx, /*test_accept=*/true, bypass_limits);
      70        7600 :             if (result.m_result_type != MempoolAcceptResult::ResultType::VALID) {
      71         340 :                 return HandleATMPError(result.m_state, err_string.original);
      72        7260 :             } else if (result.m_base_fees.value() > max_tx_fee) {
      73           4 :                 return TransactionError::MAX_FEE_EXCEEDED;
      74             :             }
      75        7600 :         }
      76             :         // Try to submit the transaction to the mempool.
      77       17261 :         const MempoolAcceptResult result = node.chainman->ProcessTransaction(tx, /*test_accept=*/false, bypass_limits);
      78       17261 :             if (result.m_result_type != MempoolAcceptResult::ResultType::VALID) {
      79        1104 :                 return HandleATMPError(result.m_state, err_string.original);
      80             :             }
      81             : 
      82             :             // Transaction was accepted to the mempool.
      83             : 
      84       16157 :             if (relay) {
      85             :                 // the mempool tracks locally submitted transactions to make a
      86             :                 // best-effort of initial broadcast
      87       16135 :                 node.mempool->AddUnbroadcastTx(txid);
      88       16135 :             }
      89             : 
      90       16157 :             if (wait_callback) {
      91             :                 // For transactions broadcast from outside the wallet, make sure
      92             :                 // that the wallet has been notified of the transaction before
      93             :                 // continuing.
      94             :                 //
      95             :                 // This prevents a race where a user might call sendrawtransaction
      96             :                 // with a transaction to/from their wallet, immediately call some
      97             :                 // wallet RPC, and get a stale result because callbacks have not
      98             :                 // yet been processed.
      99       22046 :                 CallFunctionInValidationInterfaceQueue([&promise] {
     100       11023 :                     promise.set_value();
     101       11023 :                 });
     102       11023 :                 callback_set = true;
     103       11023 :             }
     104       17261 :         }
     105       18062 :     } // cs_main
     106             : 
     107       16605 :     if (callback_set) {
     108             :         // Wait until Validation Interface clients have been notified of the
     109             :         // transaction entering the mempool.
     110       11023 :         promise.get_future().wait();
     111       11023 :     }
     112             : 
     113       16605 :     if (relay) {
     114       16274 :         node.peerman->RelayTransaction(txid);
     115       16274 :     }
     116             : 
     117       16605 :     return TransactionError::OK;
     118       18062 : }
     119             : 
     120       31869 : CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock)
     121             : {
     122       31869 :     if (mempool && !block_index) {
     123       31606 :         CTransactionRef ptx = mempool->get(hash);
     124       31606 :         if (ptx) return ptx;
     125       55206 :     }
     126       23863 :     if (g_txindex) {
     127       23835 :         CTransactionRef tx;
     128       23835 :         uint256 block_hash;
     129       23835 :         if (g_txindex->FindTx(hash, block_hash, tx)) {
     130       23591 :             if (!block_index || block_index->GetBlockHash() == block_hash) {
     131             :                 // Don't return the transaction if the provided block hash doesn't match.
     132             :                 // The case where a transaction appears in multiple blocks (e.g. reorgs or
     133             :                 // BIP30) is handled by the block lookup below.
     134       23589 :                 hashBlock = block_hash;
     135       23589 :                 return tx;
     136             :             }
     137           2 :         }
     138       24081 :     }
     139         274 :     if (block_index) {
     140           8 :         CBlock block;
     141           8 :         if (ReadBlockFromDisk(block, block_index, consensusParams)) {
     142          16 :             for (const auto& tx : block.vtx) {
     143          12 :                 if (tx->GetHash() == hash) {
     144           4 :                     hashBlock = block_index->GetBlockHash();
     145           4 :                     return tx;
     146             :                 }
     147             :             }
     148           4 :         }
     149           8 :     }
     150         270 :     return nullptr;
     151       31869 : }
     152             : } // namespace node

Generated by: LCOV version 1.16