Bitcoin ABC 0.33.11
P2P Digital Currency
preconsensus.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 The Bitcoin developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
6
8#include <blockindex.h>
10#include <util/hasher.h>
11
12#include <unordered_map>
13
15 if (!m_mempool || !m_blockIndex.pprev) {
16 return true;
17 }
19 return true;
20 }
21
23
24 // Finalized txs mined into a non-finalized block are removed from
25 // mapNextTx, so GetConflictTx alone cannot see them during a reorg.
26 // Build an outpoint index from disconnectpool entries that are still
27 // preconsensus-finalized.
28 std::unordered_map<COutPoint, TxId, SaltedOutpointHasher>
29 finalizedDisconnectedSpends;
30 if (m_disconnectpool) {
31 for (const CTransactionRef &tx : m_disconnectpool->GetQueuedTx()) {
32 const TxId txid = tx->GetId();
33 // A coinbase tx can't be finalized, but this saves a lookup
34 if (tx->IsCoinBase() ||
36 continue;
37 }
38 for (const CTxIn &txin : tx->vin) {
39 finalizedDisconnectedSpends.emplace(txin.prevout, txid);
40 }
41 }
42 }
43
44 auto rejectConflict = [&](const TxId &txid, const TxId &conflictingId) {
46 "finalized-tx-conflict",
47 strprintf("Block %s contains tx %s that conflicts "
48 "with finalized tx %s",
50 txid.ToString(),
51 conflictingId.ToString()));
52 };
53
54 // TODO Use a CoinViewCache
55 for (const auto &tx : m_block.vtx) {
56 const TxId txid = tx->GetId();
57 for (const auto &txin : tx->vin) {
58 const CTransactionRef ptxConflicting =
59 m_mempool->GetConflictTx(txin.prevout);
60
61 // Only allow for the exact txid for each coin spent
62 if (ptxConflicting && ptxConflicting->GetId() != txid &&
64 ptxConflicting->GetId())) {
65 return rejectConflict(txid, ptxConflicting->GetId());
66 }
67
68 auto it = finalizedDisconnectedSpends.find(txin.prevout);
69 if (it != finalizedDisconnectedSpends.end() && it->second != txid) {
70 return rejectConflict(txid, it->second);
71 }
72 }
73 }
74
75 return true;
76}
BlockHash GetHash() const
Definition: block.cpp:11
std::vector< CTransactionRef > vtx
Definition: block.h:63
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:32
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:316
bool setAvalancheFinalized(const CTxMemPoolEntryRef &tx, const Consensus::Params &params, const CBlockIndex &active_chain_tip, std::vector< TxId > &finalizedTxIds) EXCLUSIVE_LOCKS_REQUIRED(bool isAvalancheFinalizedPreConsensus(const TxId &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:539
CTransactionRef GetConflictTx(const COutPoint &prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Get the transaction in the pool that spends the same prevout.
Definition: txmempool.cpp:739
const indexed_disconnected_transactions & GetQueuedTx() const
const CBlockIndex & m_blockIndex
Definition: preconsensus.h:26
const CTxMemPool * m_mempool
Definition: preconsensus.h:27
const CBlock & m_block
Definition: preconsensus.h:25
const DisconnectedBlockTransactions * m_disconnectpool
Definition: preconsensus.h:28
bool operator()(BlockPolicyValidationState &state) override EXCLUSIVE_LOCKS_REQUIRED(m_mempool -> cs)
const avalanche::Processor & m_avalanche
Definition: preconsensus.h:24
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Definition: validation.h:101
bool isPreconsensusActivated(const CBlockIndex *pprev) const
Definition: processor.cpp:1557
std::string ToString() const
Definition: uint256.h:80
@ POLICY_VIOLATION
A block policy rule was violated. This block should be parked.
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
A TxId is the identifier of a transaction.
Definition: txid.h:14
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
AssertLockHeld(pool.cs)