Skip to content

Commit 9bc8b28

Browse files
author
Antoine Riard
committed
refactor : use RelayTransaction in BroadcastTransaction utility
To do so, we also refactor RelayTransaction to take a txid instead of passing a tx
1 parent 0626b8c commit 9bc8b28

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

src/interfaces/chain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <interfaces/handler.h>
1010
#include <interfaces/wallet.h>
1111
#include <net.h>
12+
#include <net_processing.h>
1213
#include <node/coin.h>
1314
#include <policy/fees.h>
1415
#include <policy/policy.h>
@@ -292,8 +293,7 @@ class ChainImpl : public Chain
292293
}
293294
void relayTransaction(const uint256& txid) override
294295
{
295-
CInv inv(MSG_TX, txid);
296-
g_connman->ForEachNode([&inv](CNode* node) { node->PushInventory(inv); });
296+
RelayTransaction(txid, *g_connman);
297297
}
298298
void getTransactionAncestry(const uint256& txid, size_t& ancestors, size_t& descendants) override
299299
{

src/net_processing.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,10 +1305,10 @@ bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
13051305
return true;
13061306
}
13071307

1308-
static void RelayTransaction(const CTransaction& tx, CConnman* connman)
1308+
void RelayTransaction(const uint256& txid, const CConnman& connman)
13091309
{
1310-
CInv inv(MSG_TX, tx.GetHash());
1311-
connman->ForEachNode([&inv](CNode* pnode)
1310+
CInv inv(MSG_TX, txid);
1311+
connman.ForEachNode([&inv](CNode* pnode)
13121312
{
13131313
pnode->PushInventory(inv);
13141314
});
@@ -1811,7 +1811,7 @@ void static ProcessOrphanTx(CConnman* connman, std::set<uint256>& orphan_work_se
18111811
if (setMisbehaving.count(fromPeer)) continue;
18121812
if (AcceptToMemoryPool(mempool, orphan_state, porphanTx, &fMissingInputs2, &removed_txn, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
18131813
LogPrint(BCLog::MEMPOOL, " accepted orphan tx %s\n", orphanHash.ToString());
1814-
RelayTransaction(orphanTx, connman);
1814+
RelayTransaction(orphanHash, *connman);
18151815
for (unsigned int i = 0; i < orphanTx.vout.size(); i++) {
18161816
auto it_by_prev = mapOrphanTransactionsByPrev.find(COutPoint(orphanHash, i));
18171817
if (it_by_prev != mapOrphanTransactionsByPrev.end()) {
@@ -2498,7 +2498,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
24982498
if (!AlreadyHave(inv) &&
24992499
AcceptToMemoryPool(mempool, state, ptx, &fMissingInputs, &lRemovedTxn, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
25002500
mempool.check(pcoinsTip.get());
2501-
RelayTransaction(tx, connman);
2501+
RelayTransaction(tx.GetHash(), *connman);
25022502
for (unsigned int i = 0; i < tx.vout.size(); i++) {
25032503
auto it_by_prev = mapOrphanTransactionsByPrev.find(COutPoint(inv.hash, i));
25042504
if (it_by_prev != mapOrphanTransactionsByPrev.end()) {
@@ -2577,7 +2577,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
25772577
LogPrintf("Not relaying invalid transaction %s from whitelisted peer=%d (%s)\n", tx.GetHash().ToString(), pfrom->GetId(), FormatStateMessage(state));
25782578
} else {
25792579
LogPrintf("Force relaying tx %s from whitelisted peer=%d\n", tx.GetHash().ToString(), pfrom->GetId());
2580-
RelayTransaction(tx, connman);
2580+
RelayTransaction(tx.GetHash(), *connman);
25812581
}
25822582
}
25832583
}

src/net_processing.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,7 @@ struct CNodeStateStats {
9090
/** Get statistics from node state */
9191
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats);
9292

93+
/** Relay transaction to every node */
94+
void RelayTransaction(const uint256&, const CConnman& connman);
95+
9396
#endif // BITCOIN_NET_PROCESSING_H

src/node/transaction.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <consensus/validation.h>
77
#include <net.h>
8+
#include <net_processing.h>
89
#include <txmempool.h>
910
#include <util/validation.h>
1011
#include <validation.h>
@@ -69,10 +70,7 @@ TransactionError BroadcastTransaction(const CTransactionRef tx, uint256& hashTx,
6970
return TransactionError::P2P_DISABLED;
7071
}
7172

72-
CInv inv(MSG_TX, hashTx);
73-
g_connman->ForEachNode([&inv](CNode* pnode) {
74-
pnode->PushInventory(inv);
75-
});
73+
RelayTransaction(hashTx, *g_connman);
7674

7775
return TransactionError::OK;
7876
}

0 commit comments

Comments
 (0)