Skip to content

Commit 6b9a261

Browse files
-f DRY up htlc_msg macros
1 parent 1dec1f9 commit 6b9a261

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ use core::ops::Deref;
7171

7272
#[cfg(any(test, feature = "std"))]
7373
use std::time::Instant;
74+
use std::any::Any;
7475
use util::crypto::sign;
7576

7677
// We hold various information about HTLC relay in the HTLC objects in Channel itself:
@@ -3015,22 +3016,18 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
30153016
}}
30163017
}
30173018

3018-
macro_rules! add_update_add_htlc {
3019-
($add_htlc_msg: expr, $channel_id: expr, $counterparty_node_id: expr) => {{
3019+
macro_rules! add_update_htlc_msg {
3020+
($htlc_msg: expr, $channel_id: expr, $counterparty_node_id: expr) => {{
30203021
add_channel_key!($channel_id, $counterparty_node_id);
30213022
if let hash_map::Entry::Occupied(mut entry) = htlcs_msgs_by_id.entry($channel_id) {
30223023
let msgs_entry = entry.get_mut();
3023-
msgs_entry.0.push($add_htlc_msg);
3024-
}
3025-
}}
3026-
}
3027-
3028-
macro_rules! add_update_fail_htlc {
3029-
($fail_htlc_msg: expr, $channel_id: expr, $counterparty_node_id: expr) => {{
3030-
add_channel_key!($channel_id, $counterparty_node_id);
3031-
if let hash_map::Entry::Occupied(mut entry) = htlcs_msgs_by_id.entry($channel_id) {
3032-
let msgs_entry = entry.get_mut();
3033-
msgs_entry.1.push($fail_htlc_msg);
3024+
if let Some(msg) = (&$htlc_msg as &Any).downcast_ref::<msgs::UpdateAddHTLC>() {
3025+
msgs_entry.0.push(msg.clone());
3026+
} else if let Some(msg) = (&$htlc_msg as &Any).downcast_ref::<msgs::UpdateFailHTLC>() {
3027+
msgs_entry.1.push(msg.clone());
3028+
} else {
3029+
panic!("Only UpdateAddHTLC or UpdateFailHTLC msgs supported for add_update_htlc_msg");
3030+
}
30343031
}
30353032
}}
30363033
}
@@ -3154,7 +3151,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31543151
match update_add {
31553152
Some(msg) => {
31563153
log_info!(self.logger, "Will forward HTLC with payment_hash {}, over channel {}", log_bytes!(payment_hash.0), log_bytes!(chan_id));
3157-
add_update_add_htlc!(msg, chan_id, counterparty_node_id);
3154+
add_update_htlc_msg!(msg, chan_id, counterparty_node_id);
31583155
},
31593156
None => {
31603157
// Nothing to do here...we're waiting on a remote
@@ -3197,7 +3194,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31973194
// the chain and sending the HTLC-Timeout is their problem.
31983195
continue;
31993196
},
3200-
Ok(Some(msg)) => { add_update_fail_htlc!(msg, forward_chan_id, counterparty_node_id); },
3197+
Ok(Some(msg)) => { add_update_htlc_msg!(msg, forward_chan_id, counterparty_node_id); },
32013198
Ok(None) => {
32023199
// Nothing to do here...we're waiting on a remote
32033200
// revoke_and_ack before we can update the commitment

0 commit comments

Comments
 (0)