Skip to content

Commit 4d1b8ff

Browse files
committed
f: DRY up create_refund_builder
1 parent 57bb91d commit 4d1b8ff

File tree

1 file changed

+74
-64
lines changed

1 file changed

+74
-64
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 74 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8095,7 +8095,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
80958095

80968096
let per_peer_state = self.per_peer_state.read().unwrap();
80978097
let peer_state_mutex = per_peer_state.get(counterparty_node_id)
8098-
.ok_or_else(|| {
8098+
.ok_or_else(|| {
80998099
debug_assert!(false);
81008100
MsgHandleErrInternal::send_err_msg_no_close(
81018101
format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id),
@@ -10262,6 +10262,53 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
1026210262
} }
1026310263

1026410264
macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
10265+
fn create_refund_builder_intern<PF>(
10266+
&$self,
10267+
make_path: PF,
10268+
amount_msats: u64,
10269+
absolute_expiry: Duration,
10270+
payment_id: PaymentId,
10271+
retry_strategy: Retry,
10272+
route_params_config: RouteParametersConfig
10273+
) -> Result<$builder, Bolt12SemanticError>
10274+
where
10275+
PF: FnOnce(PublicKey, MessageContext, &secp256k1::Secp256k1<secp256k1::All>) -> Result<Option<BlindedMessagePath>, Bolt12SemanticError>,
10276+
{
10277+
let node_id = $self.get_our_node_id();
10278+
let expanded_key = &$self.inbound_payment_key;
10279+
let entropy = &*$self.entropy_source;
10280+
let secp_ctx = &$self.secp_ctx;
10281+
10282+
let nonce = Nonce::from_entropy_source(entropy);
10283+
let context = MessageContext::Offers(
10284+
OffersContext::OutboundPayment { payment_id, nonce, hmac: None }
10285+
);
10286+
10287+
// Create the base builder with common properties
10288+
let mut builder = RefundBuilder::deriving_signing_pubkey(
10289+
node_id, expanded_key, nonce, secp_ctx, amount_msats, payment_id
10290+
)?
10291+
.chain_hash($self.chain_hash)
10292+
.absolute_expiry(absolute_expiry);
10293+
10294+
// Add path if one is provided by the path creator
10295+
if let Some(path) = make_path(node_id, context, secp_ctx)? {
10296+
builder = builder.path(path);
10297+
}
10298+
10299+
// Handle persistence and payment tracking
10300+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop($self);
10301+
10302+
let expiration = StaleExpiration::AbsoluteTimeout(absolute_expiry);
10303+
$self.pending_outbound_payments
10304+
.add_new_awaiting_invoice(
10305+
payment_id, expiration, retry_strategy, route_params_config, None,
10306+
)
10307+
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
10308+
10309+
Ok(builder.into())
10310+
}
10311+
1026510312
/// Creates a [`RefundBuilder`] such that the [`Refund`] it builds is recognized by the
1026610313
/// [`ChannelManager`] when handling [`Bolt12Invoice`] messages for the refund.
1026710314
///
@@ -10310,34 +10357,19 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
1031010357
&$self, amount_msats: u64, absolute_expiry: Duration, payment_id: PaymentId,
1031110358
retry_strategy: Retry, route_params_config: RouteParametersConfig
1031210359
) -> Result<$builder, Bolt12SemanticError> {
10313-
let node_id = $self.get_our_node_id();
10314-
let expanded_key = &$self.inbound_payment_key;
10315-
let entropy = &*$self.entropy_source;
10316-
let secp_ctx = &$self.secp_ctx;
10317-
10318-
let nonce = Nonce::from_entropy_source(entropy);
10319-
let context = MessageContext::Offers(OffersContext::OutboundPayment { payment_id, nonce, hmac: None });
10320-
let path = $self.create_blinded_paths(context)
10321-
.and_then(|paths| paths.into_iter().next().ok_or(()))
10322-
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
10323-
10324-
let builder = RefundBuilder::deriving_signing_pubkey(
10325-
node_id, expanded_key, nonce, secp_ctx, amount_msats, payment_id
10326-
)?
10327-
.chain_hash($self.chain_hash)
10328-
.absolute_expiry(absolute_expiry)
10329-
.path(path);
10330-
10331-
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop($self);
10332-
10333-
let expiration = StaleExpiration::AbsoluteTimeout(absolute_expiry);
10334-
$self.pending_outbound_payments
10335-
.add_new_awaiting_invoice(
10336-
payment_id, expiration, retry_strategy, route_params_config, None,
10337-
)
10338-
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
10339-
10340-
Ok(builder.into())
10360+
$self.create_refund_builder_intern(
10361+
|_, context, _| {
10362+
$self.create_blinded_paths(context)
10363+
.and_then(|paths| paths.into_iter().next().ok_or(()))
10364+
.map(Some)
10365+
.map_err(|_| Bolt12SemanticError::MissingPaths)
10366+
},
10367+
amount_msats,
10368+
absolute_expiry,
10369+
payment_id,
10370+
retry_strategy,
10371+
route_params_config
10372+
)
1034110373
}
1034210374

1034310375
/// Creates a [`RefundBuilder`] such that the [`Refund`] it builds is recognized by the
@@ -10388,41 +10420,19 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
1038810420
&$self, router: ME, amount_msats: u64, absolute_expiry: Duration, payment_id: PaymentId,
1038910421
retry_strategy: Retry, route_params_config: RouteParametersConfig
1039010422
) -> Result<$builder, Bolt12SemanticError> {
10391-
let node_id = $self.get_our_node_id();
10392-
let expanded_key = &$self.inbound_payment_key;
10393-
let entropy = &*$self.entropy_source;
10394-
let secp_ctx = &$self.secp_ctx;
10395-
10396-
let nonce = Nonce::from_entropy_source(entropy);
10397-
let context = MessageContext::Offers(
10398-
OffersContext::OutboundPayment { payment_id, nonce, hmac: None }
10399-
);
10400-
10401-
let peers = $self.get_peers_for_blinded_path();
10402-
let path = router.create_blinded_paths(node_id, context, peers, secp_ctx)
10403-
.map_err(|_| Bolt12SemanticError::MissingPaths)?
10404-
.into_iter().next();
10405-
10406-
let mut builder = RefundBuilder::deriving_signing_pubkey(
10407-
node_id, expanded_key, nonce, secp_ctx, amount_msats, payment_id
10408-
)?
10409-
.chain_hash($self.chain_hash)
10410-
.absolute_expiry(absolute_expiry);
10411-
10412-
if let Some(path) = path {
10413-
builder = builder.path(path)
10414-
}
10415-
10416-
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop($self);
10417-
10418-
let expiration = StaleExpiration::AbsoluteTimeout(absolute_expiry);
10419-
$self.pending_outbound_payments
10420-
.add_new_awaiting_invoice(
10421-
payment_id, expiration, retry_strategy, route_params_config, None,
10422-
)
10423-
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
10424-
10425-
Ok(builder.into())
10423+
$self.create_refund_builder_intern(
10424+
|node_id, context, secp_ctx| {
10425+
let peers = $self.get_peers_for_blinded_path();
10426+
router.create_blinded_paths(node_id, context, peers, secp_ctx)
10427+
.map(|paths| paths.into_iter().next())
10428+
.map_err(|_| Bolt12SemanticError::MissingPaths)
10429+
},
10430+
amount_msats,
10431+
absolute_expiry,
10432+
payment_id,
10433+
retry_strategy,
10434+
route_params_config
10435+
)
1042610436
}
1042710437
} }
1042810438

0 commit comments

Comments
 (0)