Skip to content

Commit c97c388

Browse files
committed
f: DRY up create_refund_builder
1 parent f159cd6 commit c97c388

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),
@@ -10263,6 +10263,53 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
1026310263
} }
1026410264

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

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

0 commit comments

Comments
 (0)