Skip to content

Commit 628c757

Browse files
committed
f: use errors for onion payload receive types
1 parent f06c6cb commit 628c757

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

lightning/src/ln/onion_utils.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,15 @@ impl<'a> PathHop for &'a TrampolineHop {
167167

168168
trait OnionPayload<'a, 'b> {
169169
type PathHopForId: PathHop + 'b;
170+
type ReceiveType: OnionPayload<'a, 'b>;
170171
fn new_forward(
171172
hop_id: <<Self as OnionPayload<'a, 'b>>::PathHopForId as PathHop>::HopId,
172173
amt_to_forward: u64, outgoing_cltv_value: u32,
173174
) -> Self;
174175
fn new_receive(
175176
recipient_onion: &'a RecipientOnionFields, keysend_preimage: Option<PaymentPreimage>,
176177
sender_intended_htlc_amt_msat: u64, total_msat: u64, cltv_expiry_height: u32,
177-
) -> Self;
178+
) -> Result<Self::ReceiveType, APIError>;
178179
fn new_blinded_forward(
179180
encrypted_tlvs: &'a Vec<u8>, intro_node_blinding_point: Option<PublicKey>,
180181
) -> Self;
@@ -187,14 +188,15 @@ trait OnionPayload<'a, 'b> {
187188
}
188189
impl<'a, 'b> OnionPayload<'a, 'b> for msgs::OutboundOnionPayload<'a> {
189190
type PathHopForId = &'b RouteHop;
191+
type ReceiveType = OutboundOnionPayload<'a>;
190192
fn new_forward(short_channel_id: u64, amt_to_forward: u64, outgoing_cltv_value: u32) -> Self {
191193
Self::Forward { short_channel_id, amt_to_forward, outgoing_cltv_value }
192194
}
193195
fn new_receive(
194196
recipient_onion: &'a RecipientOnionFields, keysend_preimage: Option<PaymentPreimage>,
195197
sender_intended_htlc_amt_msat: u64, total_msat: u64, cltv_expiry_height: u32,
196-
) -> Self {
197-
Self::Receive {
198+
) -> Result<Self::ReceiveType, APIError> {
199+
Ok(Self::Receive {
198200
payment_data: recipient_onion
199201
.payment_secret
200202
.map(|payment_secret| msgs::FinalOnionHopData { payment_secret, total_msat }),
@@ -203,7 +205,7 @@ impl<'a, 'b> OnionPayload<'a, 'b> for msgs::OutboundOnionPayload<'a> {
203205
custom_tlvs: &recipient_onion.custom_tlvs,
204206
sender_intended_htlc_amt_msat,
205207
cltv_expiry_height,
206-
}
208+
})
207209
}
208210
fn new_blinded_forward(
209211
encrypted_tlvs: &'a Vec<u8>, intro_node_blinding_point: Option<PublicKey>,
@@ -230,6 +232,7 @@ impl<'a, 'b> OnionPayload<'a, 'b> for msgs::OutboundOnionPayload<'a> {
230232
}
231233
impl<'a, 'b> OnionPayload<'a, 'b> for msgs::OutboundTrampolinePayload<'a> {
232234
type PathHopForId = &'b TrampolineHop;
235+
type ReceiveType = msgs::OutboundTrampolinePayload<'a>;
233236
fn new_forward(
234237
outgoing_node_id: PublicKey, amt_to_forward: u64, outgoing_cltv_value: u32,
235238
) -> Self {
@@ -238,8 +241,10 @@ impl<'a, 'b> OnionPayload<'a, 'b> for msgs::OutboundTrampolinePayload<'a> {
238241
fn new_receive(
239242
_recipient_onion: &'a RecipientOnionFields, _keysend_preimage: Option<PaymentPreimage>,
240243
_sender_intended_htlc_amt_msat: u64, _total_msat: u64, _cltv_expiry_height: u32,
241-
) -> Self {
242-
unreachable!("Unblinded receiving is not supported for Trampoline!")
244+
) -> Result<Self::ReceiveType, APIError> {
245+
Err(APIError::InvalidRoute {
246+
err: "Unblinded receiving is not supported for Trampoline!".to_string(),
247+
})
243248
}
244249
fn new_blinded_forward(
245250
encrypted_tlvs: &'a Vec<u8>, intro_node_blinding_point: Option<PublicKey>,
@@ -456,7 +461,7 @@ where
456461
H: DoubleEndedIterator<Item = OP::PathHopForId>,
457462
B: ExactSizeIterator<Item = &'a BlindedHop>,
458463
F: FnMut(PayloadCallbackAction, OP),
459-
OP: OnionPayload<'a, 'b>,
464+
OP: OnionPayload<'a, 'b, ReceiveType = OP>,
460465
{
461466
let mut cur_value_msat = 0u64;
462467
let mut cur_cltv = starting_htlc_offset;
@@ -518,7 +523,7 @@ where
518523
value_msat,
519524
total_msat,
520525
cltv,
521-
),
526+
)?,
522527
);
523528
}
524529
} else {

0 commit comments

Comments
 (0)