mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-05 14:06:27 -05:00
Merge #19879: [p2p] miscellaneous wtxid followups
a8a64acaf3
[BroadcastTransaction] Remove unsafe move operator (Amiti Uttarwar)125c038126
[p2p] Remove dead code (Amiti Uttarwar)fc66d0a65c
[p2p] Check for nullptr before dereferencing pointer (Adam Jonas)cb79b9dbf4
[mempool] Revert unbroadcast set to tracking just txid (Amiti Uttarwar) Pull request description: Addresses some outstanding review comments from #18044 - reverts unbroadcast txids to a set instead of a map (simpler, communicates intent better, takes less space, no efficiency advantages of map) - adds safety around two touchpoints (check for nullptr before dereferencing pointer, remove an inaccurate std::move operator) - removes some dead code Links to comments on wtxid PR: [1](https://github.com/bitcoin/bitcoin/pull/18044#discussion_r460495254) [2](https://github.com/bitcoin/bitcoin/pull/18044#discussion_r460496023) [3](https://github.com/bitcoin/bitcoin/pull/18044#discussion_r463532611) thanks to jnewbery & adamjonas for flagging these ! ! ACKs for top commit: sdaftuar: utACKa8a64acaf3
naumenkogs: utACKa8a64acaf3
jnewbery: utACKa8a64acaf3
Tree-SHA512: 7be669cb30cc17fb9e06b50e636ef7887c6a27354697987e4e4d38dba4b8f50e175647587430cd9bc3295bec01ce8b1e6639a50a4249d8fff9b1ca1b9ead3277
This commit is contained in:
commit
1c4f59728c
4 changed files with 29 additions and 32 deletions
|
@ -889,15 +889,16 @@ void PeerManager::InitializeNode(CNode *pnode) {
|
||||||
|
|
||||||
void PeerManager::ReattemptInitialBroadcast(CScheduler& scheduler) const
|
void PeerManager::ReattemptInitialBroadcast(CScheduler& scheduler) const
|
||||||
{
|
{
|
||||||
std::map<uint256, uint256> unbroadcast_txids = m_mempool.GetUnbroadcastTxs();
|
std::set<uint256> unbroadcast_txids = m_mempool.GetUnbroadcastTxs();
|
||||||
|
|
||||||
for (const auto& elem : unbroadcast_txids) {
|
for (const auto& txid : unbroadcast_txids) {
|
||||||
// Sanity check: all unbroadcast txns should exist in the mempool
|
CTransactionRef tx = m_mempool.get(txid);
|
||||||
if (m_mempool.exists(elem.first)) {
|
|
||||||
|
if (tx != nullptr) {
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
RelayTransaction(elem.first, elem.second, m_connman);
|
RelayTransaction(txid, tx->GetWitnessHash(), m_connman);
|
||||||
} else {
|
} else {
|
||||||
m_mempool.RemoveUnbroadcastTx(elem.first, true);
|
m_mempool.RemoveUnbroadcastTx(txid, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1492,8 +1493,9 @@ void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman&
|
||||||
{
|
{
|
||||||
LockAssertion lock(::cs_main);
|
LockAssertion lock(::cs_main);
|
||||||
|
|
||||||
CNodeState &state = *State(pnode->GetId());
|
CNodeState* state = State(pnode->GetId());
|
||||||
if (state.m_wtxid_relay) {
|
if (state == nullptr) return;
|
||||||
|
if (state->m_wtxid_relay) {
|
||||||
pnode->PushTxInventory(wtxid);
|
pnode->PushTxInventory(wtxid);
|
||||||
} else {
|
} else {
|
||||||
pnode->PushTxInventory(txid);
|
pnode->PushTxInventory(txid);
|
||||||
|
@ -3105,8 +3107,6 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
|
||||||
if (RecursiveDynamicUsage(*ptx) < 100000) {
|
if (RecursiveDynamicUsage(*ptx) < 100000) {
|
||||||
AddToCompactExtraTransactions(ptx);
|
AddToCompactExtraTransactions(ptx);
|
||||||
}
|
}
|
||||||
} else if (tx.HasWitness() && RecursiveDynamicUsage(*ptx) < 100000) {
|
|
||||||
AddToCompactExtraTransactions(ptx);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pfrom.HasPermission(PF_FORCERELAY)) {
|
if (pfrom.HasPermission(PF_FORCERELAY)) {
|
||||||
|
|
|
@ -38,7 +38,7 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
|
||||||
if (!node.mempool->exists(hashTx)) {
|
if (!node.mempool->exists(hashTx)) {
|
||||||
// Transaction is not already in the mempool. Submit it.
|
// Transaction is not already in the mempool. Submit it.
|
||||||
TxValidationState state;
|
TxValidationState state;
|
||||||
if (!AcceptToMemoryPool(*node.mempool, state, std::move(tx),
|
if (!AcceptToMemoryPool(*node.mempool, state, tx,
|
||||||
nullptr /* plTxnReplaced */, false /* bypass_limits */, max_tx_fee)) {
|
nullptr /* plTxnReplaced */, false /* bypass_limits */, max_tx_fee)) {
|
||||||
err_string = state.ToString();
|
err_string = state.ToString();
|
||||||
if (state.IsInvalid()) {
|
if (state.IsInvalid()) {
|
||||||
|
@ -80,7 +80,7 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
|
||||||
if (relay) {
|
if (relay) {
|
||||||
// the mempool tracks locally submitted transactions to make a
|
// the mempool tracks locally submitted transactions to make a
|
||||||
// best-effort of initial broadcast
|
// best-effort of initial broadcast
|
||||||
node.mempool->AddUnbroadcastTx(hashTx, tx->GetWitnessHash());
|
node.mempool->AddUnbroadcastTx(hashTx);
|
||||||
|
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
RelayTransaction(hashTx, tx->GetWitnessHash(), *node.connman);
|
RelayTransaction(hashTx, tx->GetWitnessHash(), *node.connman);
|
||||||
|
|
|
@ -587,10 +587,9 @@ private:
|
||||||
std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);
|
std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* track locally submitted transactions to periodically retry initial broadcast
|
* Track locally submitted transactions to periodically retry initial broadcast.
|
||||||
* map of txid -> wtxid
|
|
||||||
*/
|
*/
|
||||||
std::map<uint256, uint256> m_unbroadcast_txids GUARDED_BY(cs);
|
std::set<uint256> m_unbroadcast_txids GUARDED_BY(cs);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
indirectmap<COutPoint, const CTransaction*> mapNextTx GUARDED_BY(cs);
|
indirectmap<COutPoint, const CTransaction*> mapNextTx GUARDED_BY(cs);
|
||||||
|
@ -752,19 +751,20 @@ public:
|
||||||
size_t DynamicMemoryUsage() const;
|
size_t DynamicMemoryUsage() const;
|
||||||
|
|
||||||
/** Adds a transaction to the unbroadcast set */
|
/** Adds a transaction to the unbroadcast set */
|
||||||
void AddUnbroadcastTx(const uint256& txid, const uint256& wtxid) {
|
void AddUnbroadcastTx(const uint256& txid)
|
||||||
|
{
|
||||||
LOCK(cs);
|
LOCK(cs);
|
||||||
// Sanity Check: the transaction should also be in the mempool
|
// Sanity check the transaction is in the mempool & insert into
|
||||||
if (exists(txid)) {
|
// unbroadcast set.
|
||||||
m_unbroadcast_txids[txid] = wtxid;
|
if (exists(txid)) m_unbroadcast_txids.insert(txid);
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
/** Removes a transaction from the unbroadcast set */
|
/** Removes a transaction from the unbroadcast set */
|
||||||
void RemoveUnbroadcastTx(const uint256& txid, const bool unchecked = false);
|
void RemoveUnbroadcastTx(const uint256& txid, const bool unchecked = false);
|
||||||
|
|
||||||
/** Returns transactions in unbroadcast set */
|
/** Returns transactions in unbroadcast set */
|
||||||
std::map<uint256, uint256> GetUnbroadcastTxs() const {
|
std::set<uint256> GetUnbroadcastTxs() const
|
||||||
|
{
|
||||||
LOCK(cs);
|
LOCK(cs);
|
||||||
return m_unbroadcast_txids;
|
return m_unbroadcast_txids;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5123,7 +5123,7 @@ bool LoadMempool(CTxMemPool& pool)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: remove this try except in v0.22
|
// TODO: remove this try except in v0.22
|
||||||
std::map<uint256, uint256> unbroadcast_txids;
|
std::set<uint256> unbroadcast_txids;
|
||||||
try {
|
try {
|
||||||
file >> unbroadcast_txids;
|
file >> unbroadcast_txids;
|
||||||
unbroadcast = unbroadcast_txids.size();
|
unbroadcast = unbroadcast_txids.size();
|
||||||
|
@ -5131,13 +5131,10 @@ bool LoadMempool(CTxMemPool& pool)
|
||||||
// mempool.dat files created prior to v0.21 will not have an
|
// mempool.dat files created prior to v0.21 will not have an
|
||||||
// unbroadcast set. No need to log a failure if parsing fails here.
|
// unbroadcast set. No need to log a failure if parsing fails here.
|
||||||
}
|
}
|
||||||
for (const auto& elem : unbroadcast_txids) {
|
for (const auto& txid : unbroadcast_txids) {
|
||||||
// Don't add unbroadcast transactions that didn't get back into the
|
// Ensure transactions were accepted to mempool then add to
|
||||||
// mempool.
|
// unbroadcast set.
|
||||||
const CTransactionRef& added_tx = pool.get(elem.first);
|
if (pool.get(txid) != nullptr) pool.AddUnbroadcastTx(txid);
|
||||||
if (added_tx != nullptr) {
|
|
||||||
pool.AddUnbroadcastTx(elem.first, added_tx->GetWitnessHash());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
LogPrintf("Failed to deserialize mempool data on disk: %s. Continuing anyway.\n", e.what());
|
LogPrintf("Failed to deserialize mempool data on disk: %s. Continuing anyway.\n", e.what());
|
||||||
|
@ -5154,7 +5151,7 @@ bool DumpMempool(const CTxMemPool& pool)
|
||||||
|
|
||||||
std::map<uint256, CAmount> mapDeltas;
|
std::map<uint256, CAmount> mapDeltas;
|
||||||
std::vector<TxMempoolInfo> vinfo;
|
std::vector<TxMempoolInfo> vinfo;
|
||||||
std::map<uint256, uint256> unbroadcast_txids;
|
std::set<uint256> unbroadcast_txids;
|
||||||
|
|
||||||
static Mutex dump_mutex;
|
static Mutex dump_mutex;
|
||||||
LOCK(dump_mutex);
|
LOCK(dump_mutex);
|
||||||
|
|
Loading…
Add table
Reference in a new issue