Bitcoin ABC  0.24.10
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  std::string &err_string_out) {
22  err_string_out = state.ToString();
23  if (state.IsInvalid()) {
26  }
28  } else {
30  }
31 }
32 
34  const CTransactionRef tx,
35  std::string &err_string,
36  const Amount max_tx_fee, bool relay,
37  bool wait_callback) {
38  // BroadcastTransaction can be called by either sendrawtransaction RPC or
39  // wallet RPCs. node.connman is assigned both before chain clients and
40  // before RPC server is accepting calls, and reset after chain clients and
41  // RPC sever are stopped. node.connman should never be null here.
42  assert(node.connman);
43  assert(node.mempool);
44  std::promise<void> promise;
45  TxId txid = tx->GetId();
46  bool callback_set = false;
47 
48  { // cs_main scope
49  LOCK(cs_main);
50  // If the transaction is already confirmed in the chain, don't do
51  // anything and return early.
53  for (size_t o = 0; o < tx->vout.size(); o++) {
54  const Coin &existingCoin = view.AccessCoin(COutPoint(txid, o));
55  // IsSpent doesn't mean the coin is spent, it means the output
56  // doesn't exist. So if the output does exist, then this transaction
57  // exists in the chain.
58  if (!existingCoin.IsSpent()) {
60  }
61  }
62 
63  if (!node.mempool->exists(txid)) {
64  // Transaction is not already in the mempool.
65  TxValidationState state;
66  if (max_tx_fee > Amount::zero()) {
67  // First, call ATMP with test_accept and check the fee. If ATMP
68  // fails here, return error immediately.
69  Amount fee = Amount::zero();
70  if (!AcceptToMemoryPool(config, *node.mempool, state, tx,
71  false /* bypass_limits */,
72  /* test_accept */ true, &fee)) {
73  return HandleATMPError(state, err_string);
74  } else if (fee > max_tx_fee) {
76  }
77  }
78  // Try to submit the transaction to the mempool.
79  if (!AcceptToMemoryPool(config, *node.mempool, state, tx,
80  false /* bypass_limits */)) {
81  return HandleATMPError(state, err_string);
82  }
83 
84  // Transaction was accepted to the mempool.
85 
86  if (wait_callback) {
87  // For transactions broadcast from outside the wallet, make sure
88  // that the wallet has been notified of the transaction before
89  // continuing.
90  //
91  // This prevents a race where a user might call
92  // sendrawtransaction with a transaction to/from their wallet,
93  // immediately call some wallet RPC, and get a stale result
94  // because callbacks have not yet been processed.
96  [&promise] { promise.set_value(); });
97  callback_set = true;
98  }
99  }
100  } // cs_main
101 
102  if (callback_set) {
103  // Wait until Validation Interface clients have been notified of the
104  // transaction entering the mempool.
105  promise.get_future().wait();
106  }
107 
108  if (relay) {
109  // the mempool tracks locally submitted transactions to make a
110  // best-effort of initial broadcast
111  node.mempool->AddUnbroadcastTx(txid);
112 
113  RelayTransaction(txid, *node.connman);
114  }
115 
116  return TransactionError::OK;
117 }
TransactionError::MEMPOOL_REJECTED
@ MEMPOOL_REJECTED
NodeContext::mempool
std::unique_ptr< CTxMemPool > mempool
Definition: context.h:38
validationinterface.h
validation.h
TransactionError::MEMPOOL_ERROR
@ MEMPOOL_ERROR
ChainstateActive
CChainState & ChainstateActive()
Please prefer the identical ChainstateManager::ActiveChainstate.
Definition: validation.cpp:80
CCoinsViewCache::AccessCoin
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
Definition: coins.cpp:180
context.h
ValidationState::ToString
std::string ToString() const
Definition: validation.h:124
Amount::zero
static constexpr Amount zero()
Definition: amount.h:42
txmempool.h
transaction.h
TxValidationState
Definition: validation.h:137
Config
Definition: config.h:17
TransactionError
TransactionError
Definition: error.h:22
cs_main
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:103
Coin
A UTXO entry.
Definition: coins.h:27
HandleATMPError
static TransactionError HandleATMPError(const TxValidationState &state, std::string &err_string_out)
Definition: transaction.cpp:20
TxId
A TxId is the identifier of a transaction.
Definition: txid.h:14
Amount
Definition: amount.h:19
net_processing.h
ValidationState::GetResult
Result GetResult() const
Definition: validation.h:121
RelayTransaction
void RelayTransaction(const TxId &txid, const CConnman &connman)
Relay transaction to every node.
Definition: net_processing.cpp:1826
CCoinsViewCache
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:231
TxValidationResult::TX_MISSING_INPUTS
@ TX_MISSING_INPUTS
transaction was missing some of its inputs
LOCK
#define LOCK(cs)
Definition: sync.h:241
TransactionError::OK
@ OK
No error.
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:319
config.h
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
txid.h
BroadcastTransaction
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:33
Coin::IsSpent
bool IsSpent() const
Definition: coins.h:46
NodeContext::connman
std::unique_ptr< CConnman > connman
Definition: context.h:37
AcceptToMemoryPool
bool AcceptToMemoryPool(const Config &config, CTxMemPool &pool, TxValidationState &state, const CTransactionRef &tx, bool bypass_limits, bool test_accept, Amount *fee_out)
(try to) add transaction to memory pool
Definition: validation.cpp:779
NodeContext
NodeContext struct containing references to chain state and connection state.
Definition: context.h:36
net.h
CallFunctionInValidationInterfaceQueue
void CallFunctionInValidationInterfaceQueue(std::function< void()> func)
Pushes a function to callback onto the notification queue, guaranteeing any callbacks generated prior...
Definition: validationinterface.cpp:158
TransactionError::MISSING_INPUTS
@ MISSING_INPUTS
TransactionError::MAX_FEE_EXCEEDED
@ MAX_FEE_EXCEEDED
CChainState::CoinsTip
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: validation.h:837
ValidationState::IsInvalid
bool IsInvalid() const
Definition: validation.h:119
TransactionError::ALREADY_IN_CHAIN
@ ALREADY_IN_CHAIN