Bitcoin ABC  0.22.13
P2P Digital Currency
transaction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 <node/transaction.h>
7 
8 #include <config.h>
9 #include <consensus/validation.h>
10 #include <net.h>
11 #include <net_processing.h>
12 #include <node/context.h>
13 #include <primitives/txid.h>
14 #include <txmempool.h>
15 #include <validation.h>
16 #include <validationinterface.h>
17 
18 #include <future>
19 
21  const CTransactionRef tx,
22  std::string &err_string,
23  const Amount max_tx_fee, bool relay,
24  bool wait_callback) {
25  // BroadcastTransaction can be called by either sendrawtransaction RPC or
26  // wallet RPCs. node.connman is assigned both before chain clients and
27  // before RPC server is accepting calls, and reset after chain clients and
28  // RPC sever are stopped. node.connman should never be null here.
29  assert(node.connman);
30  assert(node.mempool);
31  std::promise<void> promise;
32  TxId txid = tx->GetId();
33  bool callback_set = false;
34 
35  { // cs_main scope
36  LOCK(cs_main);
37  // If the transaction is already confirmed in the chain, don't do
38  // anything and return early.
40  for (size_t o = 0; o < tx->vout.size(); o++) {
41  const Coin &existingCoin = view.AccessCoin(COutPoint(txid, o));
42  // IsSpent doesn't mean the coin is spent, it means the output
43  // doesn't exist. So if the output does exist, then this transaction
44  // exists in the chain.
45  if (!existingCoin.IsSpent()) {
47  }
48  }
49 
50  if (!node.mempool->exists(txid)) {
51  // Transaction is not already in the mempool. Submit it.
52  TxValidationState state;
53  if (!AcceptToMemoryPool(config, *node.mempool, state, std::move(tx),
54  false /* bypass_limits */, max_tx_fee)) {
55  err_string = state.ToString();
56  if (state.IsInvalid()) {
57  if (state.GetResult() ==
60  }
62  }
64  }
65 
66  // Transaction was accepted to the mempool.
67 
68  if (wait_callback) {
69  // For transactions broadcast from outside the wallet, make sure
70  // that the wallet has been notified of the transaction before
71  // continuing.
72  //
73  // This prevents a race where a user might call
74  // sendrawtransaction with a transaction to/from their wallet,
75  // immediately call some wallet RPC, and get a stale result
76  // because callbacks have not yet been processed.
78  [&promise] { promise.set_value(); });
79  callback_set = true;
80  }
81  }
82  } // cs_main
83 
84  if (callback_set) {
85  // Wait until Validation Interface clients have been notified of the
86  // transaction entering the mempool.
87  promise.get_future().wait();
88  }
89 
90  if (relay) {
91  // the mempool tracks locally submitted transactions to make a
92  // best-effort of initial broadcast
93  node.mempool->AddUnbroadcastTx(txid);
94 
95  RelayTransaction(txid, *node.connman);
96  }
97 
98  return TransactionError::OK;
99 }
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:338
bool IsSpent() const
Definition: coins.h:46
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
Definition: coins.cpp:180
bool exists(const TxId &txid) const
Definition: txmempool.h:773
A UTXO entry.
Definition: coins.h:27
transaction was missing some of its inputs
Definition: amount.h:17
CChainState & ChainstateActive()
Definition: validation.cpp:72
Definition: config.h:19
NodeContext struct containing references to chain state and connection state.
Definition: context.h:35
std::unique_ptr< CConnman > connman
Definition: context.h:36
#define LOCK(cs)
Definition: sync.h:230
std::string ToString() const
Definition: validation.h:122
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:95
Result GetResult() const
Definition: validation.h:119
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:22
void AddUnbroadcastTx(const TxId &txid)
Adds a transaction to the unbroadcast set.
Definition: txmempool.h:787
A TxId is the identifier of a transaction.
Definition: txid.h:14
TransactionError
Definition: error.h:22
bool IsInvalid() const
Definition: validation.h:117
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: validation.h:775
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:231
TransactionError BroadcastTransaction(NodeContext &node, const Config &config, const CTransactionRef tx, std::string &err_string, const Amount 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:20
bool AcceptToMemoryPool(const Config &config, CTxMemPool &pool, TxValidationState &state, const CTransactionRef &tx, bool bypass_limits, const Amount nAbsurdFee, bool test_accept)
(try to) add transaction to memory pool
Definition: validation.cpp:781
void RelayTransaction(const TxId &txid, const CConnman &connman)
Relay transaction to every node.
CTxMemPool * mempool
Definition: context.h:38