Skip to content

Commit fcad1cf

Browse files
Compute aggregated BlindedPayInfo in path construction
1 parent 63b60e2 commit fcad1cf

File tree

2 files changed

+121
-5
lines changed

2 files changed

+121
-5
lines changed

lightning/src/blinded_path/mod.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ pub(crate) mod utils;
1616
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
1717

1818
use crate::blinded_path::payment::{ForwardTlvs, ReceiveTlvs};
19-
use crate::sign::EntropySource;
2019
use crate::ln::msgs::DecodeError;
20+
use crate::offers::invoice::BlindedPayInfo;
21+
use crate::sign::EntropySource;
2122
use crate::util::ser::{Readable, Writeable, Writer};
2223

2324
use crate::io;
@@ -79,22 +80,27 @@ impl BlindedPath {
7980
/// Create a blinded path for a payment, to be forwarded along `path`. The last node
8081
/// in `path` will be the destination node.
8182
///
82-
/// Errors if `path` is empty or a node id in `path` is invalid.
83+
/// Errors if:
84+
/// * `path` is empty
85+
/// * a node id in `path` is invalid
86+
/// * [`BlindedPayInfo`] calculation results in an integer overflow
87+
/// * any unknown features are required in the provided [`BlindedPaymentTlvs`]
8388
// TODO: make all payloads the same size with padding + add dummy hops
8489
pub fn new_for_payment<ES: EntropySource, T: secp256k1::Signing + secp256k1::Verification>(
8590
intermediate_nodes: &[(PublicKey, ForwardTlvs)], payee_node_id: PublicKey,
8691
payee_tlvs: ReceiveTlvs, entropy_source: &ES, secp_ctx: &Secp256k1<T>
87-
) -> Result<Self, ()> {
92+
) -> Result<(BlindedPayInfo, Self), ()> {
8893
let blinding_secret_bytes = entropy_source.get_secure_random_bytes();
8994
let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
9095

91-
Ok(BlindedPath {
96+
let blinded_payinfo = payment::compute_payinfo(intermediate_nodes, &payee_tlvs)?;
97+
Ok((blinded_payinfo, BlindedPath {
9298
introduction_node_id: intermediate_nodes.first().map_or(payee_node_id, |n| n.0),
9399
blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret),
94100
blinded_hops: payment::blinded_hops(
95101
secp_ctx, intermediate_nodes, payee_node_id, payee_tlvs, &blinding_secret
96102
).map_err(|_| ())?,
97-
})
103+
}))
98104
}
99105
}
100106

lightning/src/blinded_path/payment.rs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ use crate::io;
1010
use crate::ln::PaymentSecret;
1111
use crate::ln::features::BlindedHopFeatures;
1212
use crate::ln::msgs::DecodeError;
13+
use crate::offers::invoice::BlindedPayInfo;
1314
use crate::prelude::*;
1415
use crate::util::ser::{Readable, Writeable, Writer};
1516

17+
use core::convert::TryFrom;
18+
1619
/// Data to construct a [`BlindedHop`] for forwarding a payment.
1720
pub struct ForwardTlvs {
1821
/// The short channel id this payment should be forwarded out over.
@@ -148,6 +151,47 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
148151
utils::construct_blinded_hops(secp_ctx, pks, tlvs, session_priv)
149152
}
150153

154+
pub(super) fn compute_payinfo(
155+
intermediate_nodes: &[(PublicKey, ForwardTlvs)], payee_tlvs: &ReceiveTlvs
156+
) -> Result<BlindedPayInfo, ()> {
157+
let mut curr_base_fee: u128 = 0;
158+
let mut curr_prop_mil: u128 = 0;
159+
let mut cltv_expiry_delta: u16 = 0;
160+
for (_, tlvs) in intermediate_nodes.iter().rev() {
161+
// In the future, we'll want to take the intersection of all supported features for the
162+
// `BlindedPayInfo`, but there are no features in that context right now.
163+
if tlvs.features.requires_unknown_bits() { return Err(()) }
164+
165+
let next_base_fee = tlvs.payment_relay.fee_base_msat as u128;
166+
let next_prop_mil = tlvs.payment_relay.fee_proportional_millionths as u128;
167+
// Use integer arithmetic to compute `ceil(a/b)` as `(a+b-1)/b`
168+
// ((next_base_fee * 1_000_000 + (curr_base_fee * (1_000_000 + next_prop_mil))) + 1_000_000 - 1) / 1_000_000
169+
curr_base_fee = next_prop_mil.checked_add(1_000_000)
170+
.and_then(|f| f.checked_mul(curr_base_fee))
171+
.and_then(|f| next_base_fee.checked_mul(1_000_000).and_then(|base| base.checked_add(f)))
172+
.and_then(|f| f.checked_add(1_000_000 - 1))
173+
.map(|f| f / 1_000_000)
174+
.ok_or(())?;
175+
// (((curr_prop_mil + next_prop_mil) * 1_000_000 + curr_prop_mil * next_prop_mil) + 1_000_000 - 1) / 1_000_000
176+
curr_prop_mil = curr_prop_mil.checked_add(next_prop_mil)
177+
.and_then(|f| f.checked_mul(1_000_000))
178+
.and_then(|f| curr_prop_mil.checked_mul(next_prop_mil).and_then(|prop_mil| prop_mil.checked_add(f)))
179+
.and_then(|f| f.checked_add(1_000_000 - 1))
180+
.map(|f| f / 1_000_000)
181+
.ok_or(())?;
182+
183+
cltv_expiry_delta = cltv_expiry_delta.checked_add(tlvs.payment_relay.cltv_expiry_delta).ok_or(())?;
184+
}
185+
Ok(BlindedPayInfo {
186+
fee_base_msat: u32::try_from(curr_base_fee).map_err(|_| ())?,
187+
fee_proportional_millionths: u32::try_from(curr_prop_mil).map_err(|_| ())?,
188+
cltv_expiry_delta,
189+
htlc_minimum_msat: 1, // TODO
190+
htlc_maximum_msat: 21_000_000 * 100_000_000 * 1_000, // TODO
191+
features: BlindedHopFeatures::empty(),
192+
})
193+
}
194+
151195
impl_writeable_msg!(PaymentRelay, {
152196
cltv_expiry_delta,
153197
fee_proportional_millionths,
@@ -158,3 +202,69 @@ impl_writeable_msg!(PaymentConstraints, {
158202
max_cltv_expiry,
159203
htlc_minimum_msat
160204
}, {});
205+
206+
#[cfg(test)]
207+
mod tests {
208+
use bitcoin::secp256k1::PublicKey;
209+
use crate::blinded_path::payment::{ForwardTlvs, ReceiveTlvs, PaymentConstraints, PaymentRelay};
210+
use crate::ln::PaymentSecret;
211+
use crate::ln::features::BlindedHopFeatures;
212+
213+
#[test]
214+
fn compute_payinfo() {
215+
// Taken from the spec example for aggregating blinded payment info. See
216+
// https://github.com/lightning/bolts/blob/master/proposals/route-blinding.md#blinded-payments
217+
let dummy_pk = PublicKey::from_slice(&[2; 33]).unwrap();
218+
let intermediate_nodes = vec![(dummy_pk, ForwardTlvs {
219+
short_channel_id: 0,
220+
payment_relay: PaymentRelay {
221+
cltv_expiry_delta: 144,
222+
fee_proportional_millionths: 500,
223+
fee_base_msat: 100,
224+
},
225+
payment_constraints: PaymentConstraints {
226+
max_cltv_expiry: 0,
227+
htlc_minimum_msat: 100,
228+
},
229+
features: BlindedHopFeatures::empty(),
230+
}), (dummy_pk, ForwardTlvs {
231+
short_channel_id: 0,
232+
payment_relay: PaymentRelay {
233+
cltv_expiry_delta: 144,
234+
fee_proportional_millionths: 500,
235+
fee_base_msat: 100,
236+
},
237+
payment_constraints: PaymentConstraints {
238+
max_cltv_expiry: 0,
239+
htlc_minimum_msat: 1_000,
240+
},
241+
features: BlindedHopFeatures::empty(),
242+
})];
243+
let recv_tlvs = ReceiveTlvs {
244+
payment_secret: PaymentSecret([0; 32]),
245+
payment_constraints: PaymentConstraints {
246+
max_cltv_expiry: 0,
247+
htlc_minimum_msat: 1,
248+
},
249+
};
250+
let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs).unwrap();
251+
assert_eq!(blinded_payinfo.fee_base_msat, 201);
252+
assert_eq!(blinded_payinfo.fee_proportional_millionths, 1001);
253+
assert_eq!(blinded_payinfo.cltv_expiry_delta, 288);
254+
}
255+
256+
#[test]
257+
fn compute_payinfo_1_hop() {
258+
let recv_tlvs = ReceiveTlvs {
259+
payment_secret: PaymentSecret([0; 32]),
260+
payment_constraints: PaymentConstraints {
261+
max_cltv_expiry: 0,
262+
htlc_minimum_msat: 1,
263+
},
264+
};
265+
let blinded_payinfo = super::compute_payinfo(&[], &recv_tlvs).unwrap();
266+
assert_eq!(blinded_payinfo.fee_base_msat, 0);
267+
assert_eq!(blinded_payinfo.fee_proportional_millionths, 0);
268+
assert_eq!(blinded_payinfo.cltv_expiry_delta, 0);
269+
}
270+
}

0 commit comments

Comments
 (0)