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.
0diff --git a/src/node/transaction.cpp b/src/node/transaction.cpp
1index 5da4e878ab..e2d0ba76e2 100644
2--- a/src/node/transaction.cpp
3+++ b/src/node/transaction.cpp
4@@ -49,6 +49,18 @@ TransactionError BroadcastTransaction(NodeContext& node,
5 Txid txid = tx->GetHash();
6 Wtxid wtxid = tx->GetWitnessHash();
7 bool callback_set = false;
8+ bool need_add_to_mempool{false};
9+ bool need_broadcast{false};
10+ switch (broadcast_method) {
11+ case ADD_TO_MEMPOOL_NO_BROADCAST:
12+ need_add_to_mempool = true;
13+ need_broadcast = false;
14+ break;
15+ case ADD_TO_MEMPOOL_AND_BROADCAST_TO_ALL:
16+ need_add_to_mempool = true;
17+ need_broadcast = true;
18+ break;
19+ }
20
21 {
22 LOCK(cs_main);
23@@ -85,9 +97,7 @@ TransactionError BroadcastTransaction(NodeContext& node,
24 }
25 }
26
27- switch (broadcast_method) {
28- case ADD_TO_MEMPOOL_NO_BROADCAST:
29- case ADD_TO_MEMPOOL_AND_BROADCAST_TO_ALL:
30+ if (need_add_to_mempool) {
31 // Try to submit the transaction to the mempool.
32 {
33 const MempoolAcceptResult result =
34@@ -98,12 +108,11 @@ TransactionError BroadcastTransaction(NodeContext& node,
35 }
36 // Transaction was accepted to the mempool.
37
38- if (broadcast_method == ADD_TO_MEMPOOL_AND_BROADCAST_TO_ALL) {
39+ if (need_broadcast) {
40 // the mempool tracks locally submitted transactions to make a
41 // best-effort of initial broadcast
42 node.mempool->AddUnbroadcastTx(txid);
43 }
44- break;
45 }
46
47 if (wait_callback && node.validation_signals) {
48@@ -129,12 +138,8 @@ TransactionError BroadcastTransaction(NodeContext& node,
49 promise.get_future().wait();
50 }
51
52- switch (broadcast_method) {
53- case ADD_TO_MEMPOOL_NO_BROADCAST:
54- break;
55- case ADD_TO_MEMPOOL_AND_BROADCAST_TO_ALL:
56+ if (need_broadcast) {
57 node.peerman->RelayTransaction(txid, wtxid);
58- break;
59 }
60
61 return TransactionError::OK;