Bitcoin Core  0.20.99
P2P Digital Currency
transaction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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 <net.h>
8 #include <net_processing.h>
9 #include <node/context.h>
10 #include <validation.h>
11 #include <validationinterface.h>
12 #include <node/transaction.h>
13 
14 #include <future>
15 
16 TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef tx, std::string& err_string, const CAmount& max_tx_fee, bool relay, bool wait_callback)
17 {
18  // BroadcastTransaction can be called by either sendrawtransaction RPC or wallet RPCs.
19  // node.connman is assigned both before chain clients and before RPC server is accepting calls,
20  // and reset after chain clients and RPC sever are stopped. node.connman should never be null here.
21  assert(node.connman);
22  assert(node.mempool);
23  std::promise<void> promise;
24  uint256 hashTx = tx->GetHash();
25  bool callback_set = false;
26 
27  { // cs_main scope
28  LOCK(cs_main);
29  // If the transaction is already confirmed in the chain, don't do anything
30  // and return early.
32  for (size_t o = 0; o < tx->vout.size(); o++) {
33  const Coin& existingCoin = view.AccessCoin(COutPoint(hashTx, o));
34  // IsSpent doesn't mean the coin is spent, it means the output doesn't exist.
35  // So if the output does exist, then this transaction exists in the chain.
36  if (!existingCoin.IsSpent()) return TransactionError::ALREADY_IN_CHAIN;
37  }
38  if (!node.mempool->exists(hashTx)) {
39  // Transaction is not already in the mempool. Submit it.
40  TxValidationState state;
41  if (!AcceptToMemoryPool(*node.mempool, state, std::move(tx),
42  nullptr /* plTxnReplaced */, false /* bypass_limits */, max_tx_fee)) {
43  err_string = state.ToString();
44  if (state.IsInvalid()) {
47  }
49  } else {
51  }
52  }
53 
54  // Transaction was accepted to the mempool.
55 
56  if (wait_callback) {
57  // For transactions broadcast from outside the wallet, make sure
58  // that the wallet has been notified of the transaction before
59  // continuing.
60  //
61  // This prevents a race where a user might call sendrawtransaction
62  // with a transaction to/from their wallet, immediately call some
63  // wallet RPC, and get a stale result because callbacks have not
64  // yet been processed.
66  promise.set_value();
67  });
68  callback_set = true;
69  }
70  }
71 
72  } // cs_main
73 
74  if (callback_set) {
75  // Wait until Validation Interface clients have been notified of the
76  // transaction entering the mempool.
77  promise.get_future().wait();
78  }
79 
80  if (relay) {
81  // the mempool tracks locally submitted transactions to make a
82  // best-effort of initial broadcast
83  node.mempool->AddUnbroadcastTx(hashTx);
84 
85  RelayTransaction(hashTx, *node.connman);
86  }
87 
88  return TransactionError::OK;
89 }
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:387
bool IsSpent() const
Definition: coins.h:76
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
Definition: coins.cpp:131
A UTXO entry.
Definition: coins.h:30
transaction was missing some of its inputs
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
void AddUnbroadcastTx(const uint256 &txid)
Adds a transaction to the unbroadcast set.
Definition: txmempool.h:705
NodeContext struct containing references to chain state and connection state.
Definition: context.h:34
std::unique_ptr< CConnman > connman
Definition: context.h:35
bool exists(const uint256 &hash) const
Definition: txmempool.h:692
bool AcceptToMemoryPool(CTxMemPool &pool, TxValidationState &state, const CTransactionRef &tx, std::list< CTransactionRef > *plTxnReplaced, bool bypass_limits, const CAmount nAbsurdFee, bool test_accept)
(try to) add transaction to memory pool plTxnReplaced will be appended to with all transactions repla...
#define LOCK(cs)
Definition: sync.h:226
std::string ToString() const
Definition: validation.h:112
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:128
Result GetResult() const
Definition: validation.h:109
void CallFunctionInValidationInterfaceQueue(std::function< void()> func)
Pushes a function to callback onto the notification queue, guaranteeing any callbacks generated prior...
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:18
void RelayTransaction(const uint256 &txid, const CConnman &connman)
Relay transaction to every node.
256-bit opaque blob.
Definition: uint256.h:120
TransactionError BroadcastTransaction(NodeContext &node, const CTransactionRef tx, std::string &err_string, const CAmount &max_tx_fee, bool relay, bool wait_callback)
Submit a transaction to the mempool and (optionally) relay it to all P2P peers.
Definition: transaction.cpp:16
CChainState & ChainstateActive()
Please prefer the identical ChainstateManager::ActiveChainstate.
Definition: validation.cpp:105
TransactionError
Definition: error.h:22
bool IsInvalid() const
Definition: validation.h:107
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: validation.h:552
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:236
CTxMemPool * mempool
Definition: context.h:36