/tmp/bitcoin/src/node/transaction.cpp
Line | Count | Source |
1 | | // Copyright (c) 2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-present 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 <consensus/validation.h> |
9 | | #include <index/txindex.h> |
10 | | #include <net.h> |
11 | | #include <net_processing.h> |
12 | | #include <node/blockstorage.h> |
13 | | #include <node/context.h> |
14 | | #include <node/types.h> |
15 | | #include <policy/feerate.h> |
16 | | #include <tinyformat.h> |
17 | | #include <txmempool.h> |
18 | | #include <validation.h> |
19 | | #include <validationinterface.h> |
20 | | |
21 | | #include <stdexcept> |
22 | | |
23 | | namespace node { |
24 | | static TransactionError HandleATMPError(const TxValidationState& state, std::string& err_string_out) |
25 | 4.48k | { |
26 | 4.48k | err_string_out = state.ToString(); |
27 | 4.48k | if (state.IsInvalid()) { |
28 | 4.48k | if (state.GetResult() == TxValidationResult::TX_MISSING_INPUTS) { |
29 | 9 | return TransactionError::MISSING_INPUTS; |
30 | 9 | } |
31 | 4.48k | return TransactionError::MEMPOOL_REJECTED; |
32 | 4.48k | } else { |
33 | 0 | return TransactionError::MEMPOOL_ERROR; |
34 | 0 | } |
35 | 4.48k | } |
36 | | |
37 | | TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef tx, std::string& err_string, const CAmount& max_tx_fee, const CFeeRate& max_tx_fee_rate, TxBroadcast broadcast_method, bool wait_callback) |
38 | 37.7k | { |
39 | | // BroadcastTransaction can be called by RPC or by the wallet. |
40 | | // chainman, mempool and peerman are initialized before the RPC server and wallet are started |
41 | | // and reset after the RPC sever and wallet are stopped. |
42 | 37.7k | assert(node.chainman); |
43 | 37.7k | assert(node.mempool); |
44 | 37.7k | assert(node.peerman); |
45 | | |
46 | 37.7k | Txid txid = tx->GetHash(); |
47 | 37.7k | Wtxid wtxid = tx->GetWitnessHash(); |
48 | 37.7k | bool callback_set = false; |
49 | | |
50 | 37.7k | { |
51 | 37.7k | LOCK(cs_main); |
52 | | |
53 | | // If the transaction is already confirmed in the chain, don't do anything |
54 | | // and return early. |
55 | 37.7k | CCoinsViewCache &view = node.chainman->ActiveChainstate().CoinsTip(); |
56 | 118k | for (size_t o = 0; o < tx->vout.size(); o++) { |
57 | 80.7k | const Coin& existingCoin = view.AccessCoin(COutPoint(txid, o)); |
58 | | // IsSpent doesn't mean the coin is spent, it means the output doesn't exist. |
59 | | // So if the output does exist, then this transaction exists in the chain. |
60 | 80.7k | if (!existingCoin.IsSpent()) return TransactionError::ALREADY_IN_UTXO_SET; |
61 | 80.7k | } |
62 | | |
63 | 37.7k | if (auto mempool_tx = node.mempool->get(txid); mempool_tx) { |
64 | | // There's already a transaction in the mempool with this txid. Don't |
65 | | // try to submit this transaction to the mempool (since it'll be |
66 | | // rejected as a TX_CONFLICT), but do attempt to reannounce the mempool |
67 | | // transaction if broadcast_method is not TxBroadcast::MEMPOOL_NO_BROADCAST. |
68 | | // |
69 | | // The mempool transaction may have the same or different witness (and |
70 | | // wtxid) as this transaction. Use the mempool's wtxid for reannouncement. |
71 | 10.5k | wtxid = mempool_tx->GetWitnessHash(); |
72 | 27.2k | } else { |
73 | | // Transaction is not already in the mempool. |
74 | 27.2k | const bool check_max_fee{max_tx_fee > 0}; |
75 | 27.2k | const bool check_max_feerate{max_tx_fee_rate > CFeeRate(0)}; |
76 | 27.2k | if (check_max_fee || check_max_feerate || broadcast_method == TxBroadcast::NO_MEMPOOL_PRIVATE_BROADCAST) { |
77 | | // First, call ATMP with test_accept and check the fee. If ATMP |
78 | | // fails here, return error immediately. |
79 | 15.4k | const MempoolAcceptResult result = node.chainman->ProcessTransaction(tx, /*test_accept=*/ true); |
80 | 15.4k | if (result.m_result_type != MempoolAcceptResult::ResultType::VALID) { |
81 | 145 | return HandleATMPError(result.m_state, err_string); |
82 | 15.3k | } else if (check_max_fee && result.m_base_fees.value() > max_tx_fee) { |
83 | 0 | return TransactionError::MAX_FEE_EXCEEDED; |
84 | 15.3k | } else if (check_max_feerate && result.m_base_fees.value() > max_tx_fee_rate.GetFee(result.m_vsize.value())) { |
85 | 4 | return TransactionError::MAX_FEE_RATE_EXCEEDED; |
86 | 4 | } |
87 | 15.4k | } |
88 | | |
89 | 27.0k | switch (broadcast_method) { |
90 | 20 | case TxBroadcast::MEMPOOL_NO_BROADCAST: |
91 | 17.0k | case TxBroadcast::MEMPOOL_AND_BROADCAST_TO_ALL: |
92 | | // Try to submit the transaction to the mempool. |
93 | 17.0k | { |
94 | 17.0k | const MempoolAcceptResult result = |
95 | 17.0k | node.chainman->ProcessTransaction(tx, /*test_accept=*/false); |
96 | 17.0k | if (result.m_result_type != MempoolAcceptResult::ResultType::VALID) { |
97 | 4.34k | return HandleATMPError(result.m_state, err_string); |
98 | 4.34k | } |
99 | 17.0k | } |
100 | | // Transaction was accepted to the mempool. |
101 | | |
102 | 12.6k | if (broadcast_method == TxBroadcast::MEMPOOL_AND_BROADCAST_TO_ALL) { |
103 | | // the mempool tracks locally submitted transactions to make a |
104 | | // best-effort of initial broadcast |
105 | 12.6k | node.mempool->AddUnbroadcastTx(txid); |
106 | 12.6k | } |
107 | 12.6k | break; |
108 | 10.0k | case TxBroadcast::NO_MEMPOOL_PRIVATE_BROADCAST: |
109 | 10.0k | break; |
110 | 27.0k | } |
111 | | |
112 | 22.7k | if (wait_callback && node.validation_signals) { |
113 | | // For transactions broadcast from outside the wallet, make sure |
114 | | // that the wallet has been notified of the transaction before |
115 | | // continuing. |
116 | | // |
117 | | // This prevents a race where a user might call sendrawtransaction |
118 | | // with a transaction to/from their wallet, immediately call some |
119 | | // wallet RPC, and get a stale result because callbacks have not |
120 | | // yet been processed. |
121 | 21.0k | callback_set = true; |
122 | 21.0k | } |
123 | 22.7k | } |
124 | 37.7k | } // cs_main |
125 | | |
126 | 33.2k | if (callback_set) { |
127 | | // Wait until Validation Interface clients have been notified of the |
128 | | // transaction entering the mempool. |
129 | 21.0k | node.validation_signals->SyncWithValidationInterfaceQueue(); |
130 | 21.0k | } |
131 | | |
132 | 33.2k | switch (broadcast_method) { |
133 | 131 | case TxBroadcast::MEMPOOL_NO_BROADCAST: |
134 | 131 | break; |
135 | 23.1k | case TxBroadcast::MEMPOOL_AND_BROADCAST_TO_ALL: |
136 | 23.1k | node.peerman->InitiateTxBroadcastToAll(wtxid); |
137 | 23.1k | break; |
138 | 10.0k | case TxBroadcast::NO_MEMPOOL_PRIVATE_BROADCAST: |
139 | 10.0k | return node.peerman->InitiateTxBroadcastPrivate(tx); |
140 | 33.2k | } |
141 | | |
142 | 23.2k | return TransactionError::OK; |
143 | 33.2k | } |
144 | | |
145 | | CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const Txid& hash, const BlockManager& blockman, uint256& hashBlock) |
146 | 3.60k | { |
147 | 3.60k | if (mempool && !block_index) { |
148 | 3.56k | CTransactionRef ptx = mempool->get(hash); |
149 | 3.56k | if (ptx) return ptx; |
150 | 3.56k | } |
151 | 101 | if (g_txindex) { |
152 | 48 | if (auto result{g_txindex->FindTx(hash)}) { |
153 | 40 | if (!block_index || block_index->GetBlockHash() == result->block_hash) { |
154 | | // Don't return the transaction if the provided block hash doesn't match. |
155 | | // The case where a transaction appears in multiple blocks (e.g. reorgs or |
156 | | // BIP30) is handled by the block lookup below. |
157 | 39 | hashBlock = result->block_hash; |
158 | 39 | return result->tx; |
159 | 39 | } |
160 | 40 | } |
161 | 48 | } |
162 | 62 | if (block_index) { |
163 | 32 | CBlock block; |
164 | 32 | if (!blockman.ReadBlock(block, *block_index)) { |
165 | 2 | throw std::runtime_error{strprintf("I/O error reading block data for block %s", block_index->GetBlockHash().ToString())}; |
166 | 2 | } |
167 | 58 | for (const auto& tx : block.vtx) { |
168 | 58 | if (tx->GetHash() == hash) { |
169 | 28 | hashBlock = block_index->GetBlockHash(); |
170 | 28 | return tx; |
171 | 28 | } |
172 | 58 | } |
173 | 30 | } |
174 | 32 | return nullptr; |
175 | 62 | } |
176 | | } // namespace node |