Skip to content

Commit 3c24a13

Browse files
committed
Don't consume readers with FixedLengthReader
We can't always assume that we're done reading after using a FixedLengthReader. In some cases, we may need to read a set of length-prefixed objects one at a time, and we'd like to do so without incurring an additional allocation by reading a number of bytes first to then deserialize them.
1 parent 6d11111 commit 3c24a13

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

lightning/src/crypto/streams.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ mod tests {
151151
let writeable_len = $obj.serialized_length() as u64 + 16;
152152
let write_adapter = ChaChaPolyWriteAdapter::new(rho, &$obj);
153153
let encrypted_writeable_bytes = write_adapter.encode();
154-
let encrypted_writeable = &encrypted_writeable_bytes[..];
154+
let encrypted_writeable = &mut &encrypted_writeable_bytes[..];
155155

156156
// Now deserialize the object back and make sure it matches the original.
157157
let mut rd = FixedLengthReader::new(encrypted_writeable, writeable_len);

lightning/src/events/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,7 @@ impl MaybeReadable for Event {
12941294
// Note that we do not write a length-prefixed TLV for FundingGenerationReady events.
12951295
0u8 => Ok(None),
12961296
1u8 => {
1297-
let f = || {
1297+
let mut f = || {
12981298
let mut payment_hash = PaymentHash([0; 32]);
12991299
let mut payment_preimage = None;
13001300
let mut payment_secret = None;
@@ -1342,7 +1342,7 @@ impl MaybeReadable for Event {
13421342
f()
13431343
},
13441344
2u8 => {
1345-
let f = || {
1345+
let mut f = || {
13461346
let mut payment_preimage = PaymentPreimage([0; 32]);
13471347
let mut payment_hash = None;
13481348
let mut payment_id = None;
@@ -1366,7 +1366,7 @@ impl MaybeReadable for Event {
13661366
f()
13671367
},
13681368
3u8 => {
1369-
let f = || {
1369+
let mut f = || {
13701370
#[cfg(test)]
13711371
let error_code = Readable::read(reader)?;
13721372
#[cfg(test)]
@@ -1409,7 +1409,7 @@ impl MaybeReadable for Event {
14091409
},
14101410
4u8 => Ok(None),
14111411
5u8 => {
1412-
let f = || {
1412+
let mut f = || {
14131413
let mut outputs = WithoutLength(Vec::new());
14141414
let mut channel_id: Option<ChannelId> = None;
14151415
read_tlv_fields!(reader, {
@@ -1445,7 +1445,7 @@ impl MaybeReadable for Event {
14451445
}))
14461446
},
14471447
7u8 => {
1448-
let f = || {
1448+
let mut f = || {
14491449
let mut prev_channel_id = None;
14501450
let mut next_channel_id = None;
14511451
let mut prev_user_channel_id = None;
@@ -1473,7 +1473,7 @@ impl MaybeReadable for Event {
14731473
f()
14741474
},
14751475
9u8 => {
1476-
let f = || {
1476+
let mut f = || {
14771477
let mut channel_id = ChannelId::new_zero();
14781478
let mut reason = UpgradableRequired(None);
14791479
let mut user_channel_id_low_opt: Option<u64> = None;
@@ -1503,7 +1503,7 @@ impl MaybeReadable for Event {
15031503
f()
15041504
},
15051505
11u8 => {
1506-
let f = || {
1506+
let mut f = || {
15071507
let mut channel_id = ChannelId::new_zero();
15081508
let mut transaction = Transaction{ version: 2, lock_time: LockTime::ZERO, input: Vec::new(), output: Vec::new() };
15091509
read_tlv_fields!(reader, {
@@ -1515,7 +1515,7 @@ impl MaybeReadable for Event {
15151515
f()
15161516
},
15171517
13u8 => {
1518-
let f = || {
1518+
let mut f = || {
15191519
_init_and_read_len_prefixed_tlv_fields!(reader, {
15201520
(0, payment_id, required),
15211521
(2, payment_hash, option),
@@ -1531,7 +1531,7 @@ impl MaybeReadable for Event {
15311531
f()
15321532
},
15331533
15u8 => {
1534-
let f = || {
1534+
let mut f = || {
15351535
let mut payment_hash = PaymentHash([0; 32]);
15361536
let mut payment_id = PaymentId([0; 32]);
15371537
let mut reason = None;
@@ -1553,7 +1553,7 @@ impl MaybeReadable for Event {
15531553
Ok(None)
15541554
},
15551555
19u8 => {
1556-
let f = || {
1556+
let mut f = || {
15571557
let mut payment_hash = PaymentHash([0; 32]);
15581558
let mut purpose = UpgradableRequired(None);
15591559
let mut amount_msat = 0;
@@ -1580,7 +1580,7 @@ impl MaybeReadable for Event {
15801580
f()
15811581
},
15821582
21u8 => {
1583-
let f = || {
1583+
let mut f = || {
15841584
_init_and_read_len_prefixed_tlv_fields!(reader, {
15851585
(0, payment_id, required),
15861586
(2, payment_hash, required),
@@ -1596,7 +1596,7 @@ impl MaybeReadable for Event {
15961596
f()
15971597
},
15981598
23u8 => {
1599-
let f = || {
1599+
let mut f = || {
16001600
_init_and_read_len_prefixed_tlv_fields!(reader, {
16011601
(0, payment_id, required),
16021602
(2, payment_hash, required),
@@ -1614,7 +1614,7 @@ impl MaybeReadable for Event {
16141614
f()
16151615
},
16161616
25u8 => {
1617-
let f = || {
1617+
let mut f = || {
16181618
let mut prev_channel_id = ChannelId::new_zero();
16191619
let mut failed_next_destination_opt = UpgradableRequired(None);
16201620
read_tlv_fields!(reader, {
@@ -1630,7 +1630,7 @@ impl MaybeReadable for Event {
16301630
},
16311631
27u8 => Ok(None),
16321632
29u8 => {
1633-
let f = || {
1633+
let mut f = || {
16341634
let mut channel_id = ChannelId::new_zero();
16351635
let mut user_channel_id: u128 = 0;
16361636
let mut counterparty_node_id = RequiredWrapper(None);
@@ -1652,7 +1652,7 @@ impl MaybeReadable for Event {
16521652
f()
16531653
},
16541654
31u8 => {
1655-
let f = || {
1655+
let mut f = || {
16561656
let mut channel_id = ChannelId::new_zero();
16571657
let mut user_channel_id: u128 = 0;
16581658
let mut former_temporary_channel_id = None;
@@ -1680,7 +1680,7 @@ impl MaybeReadable for Event {
16801680
f()
16811681
},
16821682
33u8 => {
1683-
let f = || {
1683+
let mut f = || {
16841684
_init_and_read_len_prefixed_tlv_fields!(reader, {
16851685
(0, payment_id, required),
16861686
});

lightning/src/util/ser.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ impl Writer for LengthCalculatingWriter {
108108
/// forward to ensure we always consume exactly the fixed length specified.
109109
///
110110
/// This is not exported to bindings users as manual TLV building is not currently supported in bindings
111-
pub struct FixedLengthReader<R: Read> {
112-
read: R,
111+
pub struct FixedLengthReader<'a, R: Read> {
112+
read: &'a mut R,
113113
bytes_read: u64,
114114
total_bytes: u64,
115115
}
116-
impl<R: Read> FixedLengthReader<R> {
116+
impl<'a, R: Read> FixedLengthReader<'a, R> {
117117
/// Returns a new [`FixedLengthReader`].
118-
pub fn new(read: R, total_bytes: u64) -> Self {
118+
pub fn new(read: &'a mut R, total_bytes: u64) -> Self {
119119
Self { read, bytes_read: 0, total_bytes }
120120
}
121121

@@ -136,7 +136,7 @@ impl<R: Read> FixedLengthReader<R> {
136136
}
137137
}
138138
}
139-
impl<R: Read> Read for FixedLengthReader<R> {
139+
impl<'a, R: Read> Read for FixedLengthReader<'a, R> {
140140
#[inline]
141141
fn read(&mut self, dest: &mut [u8]) -> Result<usize, io::Error> {
142142
if self.total_bytes == self.bytes_read {
@@ -154,7 +154,7 @@ impl<R: Read> Read for FixedLengthReader<R> {
154154
}
155155
}
156156

157-
impl<R: Read> LengthRead for FixedLengthReader<R> {
157+
impl<'a, R: Read> LengthRead for FixedLengthReader<'a, R> {
158158
#[inline]
159159
fn total_bytes(&self) -> u64 {
160160
self.total_bytes

lightning/src/util/ser_macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ macro_rules! impl_writeable_tlv_based_enum {
10331033
$($variant_id => {
10341034
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
10351035
// in the same function body. Instead, we define a closure and call it.
1036-
let f = || {
1036+
let mut f = || {
10371037
$crate::_init_and_read_len_prefixed_tlv_fields!(reader, {
10381038
$(($type, $field, $fieldty)),*
10391039
});
@@ -1087,7 +1087,7 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable {
10871087
$($variant_id => {
10881088
// Because read_tlv_fields creates a labeled loop, we cannot call it twice
10891089
// in the same function body. Instead, we define a closure and call it.
1090-
let f = || {
1090+
let mut f = || {
10911091
$crate::_init_and_read_len_prefixed_tlv_fields!(reader, {
10921092
$(($type, $field, $fieldty)),*
10931093
});

0 commit comments

Comments
 (0)