Skip to content

Commit

Permalink
f: Handle Feedback
Browse files Browse the repository at this point in the history
1. Broke the long lines of code to ensure the lines are under 100 characters max.
2. Simplified the responder logic by using a match responder instead of a nested function.
  • Loading branch information
shaavan committed Mar 25, 2024
1 parent 8fefe50 commit 95d67f7
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9433,40 +9433,48 @@ where
let secp_ctx = &self.secp_ctx;
let expanded_key = &self.inbound_payment_key;

fn handle_error_response<T>(responder: Option<Responder<OffersMessage>>, error: T) -> ResponseInstruction<OffersMessage> where T: Into<OffersMessage> {
if let Some(responder) = responder {
responder.respond(error.into())
} else {
ResponseInstruction::NoResponse
}
}
let responder = match responder {
Some(responder) => responder,
None => return ResponseInstruction::NoResponse,
};

let response_option = match &message {
let response_opt = match &message {
OffersMessage::InvoiceRequest(invoice_request) => {
let amount_msats = match InvoiceBuilder::<DerivedSigningPubkey>::amount_msats(
&invoice_request
) {
Ok(amount_msats) => amount_msats,
Err(error) => return handle_error_response(responder, OffersMessage::InvoiceError(error.into())),
Err(error) => {
return responder.respond(OffersMessage::InvoiceError(error.into()));
}
};
let invoice_request = match invoice_request.clone().verify(expanded_key, secp_ctx) {
Ok(invoice_request) => invoice_request,
Err(()) => return handle_error_response(responder, OffersMessage::InvoiceError(Bolt12SemanticError::InvalidMetadata.into())),
Err(()) => {
let error = Bolt12SemanticError::InvalidMetadata;
return responder.respond(OffersMessage::InvoiceError(error.into()));
}
};

let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
let (payment_hash, payment_secret) = match self.create_inbound_payment(
Some(amount_msats), relative_expiry, None
) {
Ok((payment_hash, payment_secret)) => (payment_hash, payment_secret),
Err(()) => return handle_error_response(responder, OffersMessage::InvoiceError(Bolt12SemanticError::InvalidAmount.into())),
Err(()) => {
let error = Bolt12SemanticError::InvalidAmount;
return responder.respond(OffersMessage::InvoiceError(error.into()));
}
};

let payment_paths = match self.create_blinded_payment_paths(
amount_msats, payment_secret
) {
Ok(payment_paths) => payment_paths,
Err(()) => return handle_error_response(responder, OffersMessage::InvoiceError(Bolt12SemanticError::MissingPaths.into())),
Err(()) => {
let error = Bolt12SemanticError::MissingPaths;
return responder.respond(OffersMessage::InvoiceError(error.into()));
}
};

#[cfg(not(feature = "std"))]
Expand Down Expand Up @@ -9544,10 +9552,10 @@ where
None
},
};
if let (Some(response), Some(responder)) = (response_option, responder) {
responder.respond(response)
} else {
ResponseInstruction::NoResponse

match response_opt {
Some(response) => responder.respond(response),
None => ResponseInstruction::NoResponse
}
}

Expand Down

0 comments on commit 95d67f7

Please sign in to comment.