Skip to content

Commit 93900f9

Browse files
committed
Add TlvStream::intersection
When we drop non-matching custom TLVs on multi-part receives, since we can assume the TLV streams are sorted, we can implement faster algorithm to get the intersection of the two streams.
1 parent 25455ef commit 93900f9

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

lightning/src/offers/merkle.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,23 @@ impl<'a> TlvStream<'a> {
185185
fn skip_signatures(self) -> core::iter::Filter<TlvStream<'a>, fn(&TlvRecord) -> bool> {
186186
self.filter(|record| !SIGNATURE_TYPES.contains(&record.r#type))
187187
}
188+
189+
pub(crate) fn intersection(self, rhs: Self) -> Vec<u8> {
190+
let mut rhs_iter = rhs.into_iter();
191+
let mut rhs_tlv = rhs_iter.next();
192+
let mut intersection = Vec::new();
193+
for tlv in self.into_iter() {
194+
while let Some(ref rhs_tlv_inner) = rhs_tlv {
195+
if rhs_tlv_inner == &tlv {
196+
tlv.write(&mut intersection).expect("Should be able to write TLV");
197+
} else if rhs_tlv_inner.r#type > tlv.r#type {
198+
break;
199+
}
200+
rhs_tlv = rhs_iter.next();
201+
}
202+
}
203+
intersection
204+
}
188205
}
189206

190207
/// A slice into a [`TlvStream`] for a record.

0 commit comments

Comments
 (0)