Skip to content

Commit 8892efe

Browse files
committed
Add defaulting policy field to ScidWithPeer
Persist a FeePolicy per intercept SCID record so a later milestone can look up a peer's policy at the skim site. ScidWithPeer::new still hands every record Flat(Standard), so nothing changes yet. The field uses a length-delimited TLV with a default_value of Flat(Standard) at type 4. Records written before this field existed carry only types 0 and 2, so they read back as Standard with no migration. A test encodes a copy of the old two-field layout and decodes it through the new struct to pin that default.
1 parent c0c0845 commit 8892efe

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

lightning-liquidity/src/lsps4/scid_store.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::ops::Deref;
1919
use crate::sync::RwLock;
2020

2121

22+
use crate::lsps4::fee_policy::{FeePolicy, FeeTier};
2223
use crate::lsps4::utils;
2324

2425
/// The Intercepted HTLC store information will be persisted under this key.
@@ -31,6 +32,7 @@ pub(crate) const INTERCEPT_SCID_STORE_PERSISTENCE_SECONDARY_NAMESPACE: &str = ""
3132
pub struct ScidWithPeer {
3233
scid: u64,
3334
peer_id: PublicKey,
35+
policy: FeePolicy,
3436
}
3537

3638
impl ScidWithPeer {
@@ -40,6 +42,7 @@ impl ScidWithPeer {
4042
Self {
4143
scid,
4244
peer_id,
45+
policy: FeePolicy::Flat(FeeTier::Standard),
4346
}
4447
}
4548

@@ -54,11 +57,16 @@ impl ScidWithPeer {
5457
pub fn peer_id(&self) -> PublicKey {
5558
self.peer_id
5659
}
60+
61+
pub fn policy(&self) -> &FeePolicy {
62+
&self.policy
63+
}
5764
}
5865

5966
impl_writeable_tlv_based!(ScidWithPeer, {
6067
(0, scid, required),
6168
(2, peer_id, required),
69+
(4, policy, (default_value, FeePolicy::Flat(FeeTier::Standard))),
6270
});
6371

6472
pub struct ScidStore<L: Deref, KV: Deref + Clone>
@@ -204,4 +212,51 @@ where L::Target: Logger, KV::Target: KVStoreSync {
204212
);
205213
result
206214
}
215+
}
216+
217+
#[cfg(test)]
218+
mod tests {
219+
use super::*;
220+
use lightning::impl_writeable_tlv_based;
221+
222+
/// A copy of the pre-policy `ScidWithPeer` layout (tlv 0/2 only) used to prove that records
223+
/// persisted before the `policy` field existed still decode, defaulting to `Flat(Standard)`.
224+
struct LegacyScidWithPeer {
225+
scid: u64,
226+
peer_id: PublicKey,
227+
}
228+
229+
impl_writeable_tlv_based!(LegacyScidWithPeer, {
230+
(0, scid, required),
231+
(2, peer_id, required),
232+
});
233+
234+
fn test_peer() -> PublicKey {
235+
// The secp256k1 generator point: a valid compressed public key.
236+
PublicKey::from_slice(&[
237+
0x02, 0x79, 0xBE, 0x66, 0x7E, 0xF9, 0xDC, 0xBB, 0xAC, 0x55, 0xA0, 0x62, 0x95, 0xCE,
238+
0x87, 0x0B, 0x07, 0x02, 0x9B, 0xFC, 0xDB, 0x2D, 0xCE, 0x28, 0xD9, 0x59, 0xF2, 0x81,
239+
0x5B, 0x16, 0xF8, 0x17, 0x98,
240+
])
241+
.unwrap()
242+
}
243+
244+
#[test]
245+
fn round_trips_with_policy() {
246+
let record = ScidWithPeer::new(42, test_peer());
247+
let bytes = record.encode();
248+
let decoded = ScidWithPeer::read(&mut &bytes[..]).unwrap();
249+
assert_eq!(record, decoded);
250+
assert_eq!(decoded.policy(), &FeePolicy::Flat(FeeTier::Standard));
251+
}
252+
253+
#[test]
254+
fn legacy_record_defaults_to_standard_policy() {
255+
let legacy = LegacyScidWithPeer { scid: 42, peer_id: test_peer() };
256+
let bytes = legacy.encode();
257+
let decoded = ScidWithPeer::read(&mut &bytes[..]).unwrap();
258+
assert_eq!(decoded.scid(), 42);
259+
assert_eq!(decoded.peer_id(), test_peer());
260+
assert_eq!(decoded.policy(), &FeePolicy::Flat(FeeTier::Standard));
261+
}
207262
}

0 commit comments

Comments
 (0)