For better readability, I suggest deciding upfront if mempool-adding and relaying is required respectively, and working accordingly. This avoids repeating case statements.
In fact this logic (for a certain enum option mempool-adding/relaying is needed) could go as a method into the enum.
diff --git a/src/node/transaction.cpp b/src/node/transaction.cpp
index 5da4e878ab..e2d0ba76e2 100644
--- a/src/node/transaction.cpp
+++ b/src/node/transaction.cpp
@@ -49,6 +49,18 @@ TransactionError BroadcastTransaction(NodeContext& node,
Txid txid = tx->GetHash();
Wtxid wtxid = tx->GetWitnessHash();
bool callback_set = false;
+ bool need_add_to_mempool{false};
+ bool need_broadcast{false};
+ switch (broadcast_method) {
+ case ADD_TO_MEMPOOL_NO_BROADCAST:
+ need_add_to_mempool = true;
+ need_broadcast = false;
+ break;
+ case ADD_TO_MEMPOOL_AND_BROADCAST_TO_ALL:
+ need_add_to_mempool = true;
+ need_broadcast = true;
+ break;
+ }
{
LOCK(cs_main);
@@ -85,9 +97,7 @@ TransactionError BroadcastTransaction(NodeContext& node,
}
}
- switch (broadcast_method) {
- case ADD_TO_MEMPOOL_NO_BROADCAST:
- case ADD_TO_MEMPOOL_AND_BROADCAST_TO_ALL:
+ if (need_add_to_mempool) {
// Try to submit the transaction to the mempool.
{
const MempoolAcceptResult result =
@@ -98,12 +108,11 @@ TransactionError BroadcastTransaction(NodeContext& node,
}
// Transaction was accepted to the mempool.
- if (broadcast_method == ADD_TO_MEMPOOL_AND_BROADCAST_TO_ALL) {
+ if (need_broadcast) {
// the mempool tracks locally submitted transactions to make a
// best-effort of initial broadcast
node.mempool->AddUnbroadcastTx(txid);
}
- break;
}
if (wait_callback && node.validation_signals) {
@@ -129,12 +138,8 @@ TransactionError BroadcastTransaction(NodeContext& node,
promise.get_future().wait();
}
- switch (broadcast_method) {
- case ADD_TO_MEMPOOL_NO_BROADCAST:
- break;
- case ADD_TO_MEMPOOL_AND_BROADCAST_TO_ALL:
+ if (need_broadcast) {
node.peerman->RelayTransaction(txid, wtxid);
- break;
}
return TransactionError::OK;