Bitcoin Core  0.19.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 <util/validation.h>
11 #include <validation.h>
12 #include <validationinterface.h>
13 #include <node/transaction.h>
14 
15 #include <future>
16 
17 TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef tx, std::string& err_string, const CAmount& max_tx_fee, bool relay, bool wait_callback)
18 {
19  // BroadcastTransaction can be called by either sendrawtransaction RPC or wallet RPCs.
20  // node.connman is assigned both before chain clients and before RPC server is accepting calls,
21  // and reset after chain clients and RPC sever are stopped. node.connman should never be null here.
22  assert(node.connman);
23  assert(node.mempool);
24  std::promise<void> promise;
25  uint256 hashTx = tx->GetHash();
26  bool callback_set = false;
27 
28  { // cs_main scope
29  LOCK(cs_main);
30  // If the transaction is already confirmed in the chain, don't do anything
31  // and return early.
33  for (size_t o = 0; o < tx->vout.size(); o++) {
34  const Coin& existingCoin = view.AccessCoin(COutPoint(hashTx, o));
35  // IsSpent doesn't mean the coin is spent, it means the output doesn't exist.
36  // So if the output does exist, then this transaction exists in the chain.
37  if (!existingCoin.IsSpent()) return TransactionError::ALREADY_IN_CHAIN;
38  }
39  if (!node.mempool->exists(hashTx)) {
40  // Transaction is not already in the mempool. Submit it.
41  TxValidationState state;
42  if (!AcceptToMemoryPool(*node.mempool, state, std::move(tx),
43  nullptr /* plTxnReplaced */, false /* bypass_limits */, max_tx_fee)) {
44  err_string = FormatStateMessage(state);
45  if (state.IsInvalid()) {
48  }
50  } else {
52  }
53  }
54 
55  // Transaction was accepted to the mempool.
56 
57  if (wait_callback) {
58  // For transactions broadcast from outside the wallet, make sure
59  // that the wallet has been notified of the transaction before
60  // continuing.
61  //
62  // This prevents a race where a user might call sendrawtransaction
63  // with a transaction to/from their wallet, immediately call some
64  // wallet RPC, and get a stale result because callbacks have not
65  // yet been processed.
67  promise.set_value();
68  });
69  callback_set = true;
70  }
71  }
72 
73  } // cs_main
74 
75  if (callback_set) {
76  // Wait until Validation Interface clients have been notified of the
77  // transaction entering the mempool.
78  promise.get_future().wait();
79  }
80 
81  if (relay) {
82  RelayTransaction(hashTx, *node.connman);
83  }
84 
85  return TransactionError::OK;
86 }
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:408
bool IsSpent() const
Definition: coins.h:76
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or a pruned one if not found.
Definition: coins.cpp:118
A UTXO entry.
Definition: coins.h:30
bool IsInvalid() const
Definition: validation.h:107
transaction was missing some of its inputs
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
std::string FormatStateMessage(const ValidationState &state)
Convert ValidationState to a human-readable message for logging.
Definition: validation.cpp:11
NodeContext struct containing references to chain state and connection state.
Definition: context.h:31
std::unique_ptr< CConnman > connman
Definition: context.h:32
bool exists(const uint256 &hash) const
Definition: txmempool.h:690
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:179
TxValidationResult GetResult() const
Definition: validation.h:127
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: validation.cpp:106
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:17
CChainState & ChainstateActive()
Definition: validation.cpp:86
TransactionError
Definition: error.h:22
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: validation.h:631
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:210
CTxMemPool * mempool
Definition: context.h:33