Skip to content

Commit 41874b1

Browse files
committed
review: ActBuilder::fill() returns the number of bytes consumed
Instead of returning a subslice of input for the caller to process, just return the number of bytes consume and let them do the accounting.
1 parent ebe3559 commit 41874b1

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

lightning/src/ln/peers/handshake/acts.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ impl ActBuilder {
8282
}
8383
}
8484

85-
/// Fills the Act with bytes from input and returns the unprocessed bytes
86-
pub(super) fn fill<'a>(&mut self, input: &'a [u8]) -> &'a [u8] {
85+
/// Fills the Act with bytes from input and returns the number of bytes consumed from input.
86+
pub(super) fn fill(&mut self, input: &[u8]) -> usize {
8787
// Simple fill implementation for both almost-identical structs to deduplicate code
8888
// $act: Act[One|Two|Three], $input: &[u8]; returns &[u8] of remaining input that was not processed
8989
macro_rules! fill_act_content {
@@ -93,7 +93,7 @@ impl ActBuilder {
9393
$act[$write_pos..$write_pos + fill_amount].copy_from_slice(&$input[..fill_amount]);
9494

9595
$write_pos += fill_amount;
96-
&$input[fill_amount..]
96+
fill_amount
9797
}}
9898
}
9999

@@ -125,40 +125,41 @@ mod tests {
125125
fn partial_fill() {
126126
let mut builder = ActBuilder::new(Act::One(EMPTY_ACT_ONE));
127127

128-
let remaining = builder.fill(&[1, 2, 3]);
128+
let input = [1, 2, 3];
129+
let bytes_read = builder.fill(&input);
129130
assert_eq!(builder.partial_act.len(), ACT_ONE_LENGTH);
130131
assert_eq!(builder.write_pos, 3);
131132
assert!(!builder.is_finished());
132-
assert_eq!(remaining, &[]);
133+
assert_eq!(bytes_read, input.len());
133134
}
134135

135136
// Test bookkeeping of exact fill
136137
#[test]
137138
fn exact_fill() {
138139
let mut builder = ActBuilder::new(Act::One(EMPTY_ACT_ONE));
139140

140-
let input = [0; 50];
141-
let remaining = builder.fill(&input);
141+
let input = [0; ACT_ONE_LENGTH];
142+
let bytes_read = builder.fill(&input);
142143
assert_eq!(builder.partial_act.len(), ACT_ONE_LENGTH);
143144
assert_eq!(builder.write_pos, ACT_ONE_LENGTH);
144145
assert!(builder.is_finished());
145146
assert_eq!(Act::from(builder).as_ref(), &input[..]);
146-
assert_eq!(remaining, &[]);
147+
assert_eq!(bytes_read, input.len());
147148
}
148149

149150
// Test bookkeeping of overfill
150151
#[test]
151152
fn over_fill() {
152153
let mut builder = ActBuilder::new(Act::One(EMPTY_ACT_ONE));
153154

154-
let input = [0; 51];
155-
let remaining = builder.fill(&input);
155+
let input = [0; ACT_ONE_LENGTH + 1];
156+
let bytes_read = builder.fill(&input);
156157

157158
assert_eq!(builder.partial_act.len(), ACT_ONE_LENGTH);
158159
assert_eq!(builder.write_pos, ACT_ONE_LENGTH);
159160
assert!(builder.is_finished());
160-
assert_eq!(Act::from(builder).as_ref(), &input[..50]);
161-
assert_eq!(remaining, &[0]);
161+
assert_eq!(Act::from(builder).as_ref(), &input[..ACT_ONE_LENGTH]);
162+
assert_eq!(bytes_read, ACT_ONE_LENGTH);
162163
}
163164

164165
// Converting an unfinished ActBuilder panics

lightning/src/ln/peers/handshake/states.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,17 @@ impl IHandshakeState for ResponderAwaitingActOneState {
181181
// https://github.com/lightningnetwork/lightning-rfc/blob/master/08-transport.md#act-two (sender)
182182
fn next(self, input: &[u8]) -> Result<(Option<Act>, HandshakeState), String> {
183183
let mut act_one_builder = self.act_one_builder;
184-
let remaining = act_one_builder.fill(input);
184+
let bytes_read = act_one_builder.fill(input);
185185

186-
// Any payload larger than ACT_ONE_LENGTH indicates a bad peer since initiator data
187-
// is required to generate act3 (so it can't come before we transition)
188-
if remaining.len() != 0 {
186+
// Any payload that is not fully consumed by the builder indicates a bad peer since responder
187+
// data is required to generate act3 (so it can't come before we transition)
188+
if bytes_read < input.len() {
189189
return Err("Act One too large".to_string());
190190
}
191191

192192
// In the event of a partial fill, stay in the same state and wait for more data
193193
if !act_one_builder.is_finished() {
194-
assert_eq!(remaining.len(), 0);
194+
assert_eq!(bytes_read, input.len());
195195
return Ok((
196196
None,
197197
HandshakeState::ResponderAwaitingActOne(Self {
@@ -247,17 +247,17 @@ impl IHandshakeState for InitiatorAwaitingActTwoState {
247247
// https://github.com/lightningnetwork/lightning-rfc/blob/master/08-transport.md#act-three (sender)
248248
fn next(self, input: &[u8]) -> Result<(Option<Act>, HandshakeState), String> {
249249
let mut act_two_builder = self.act_two_builder;
250-
let remaining = act_two_builder.fill(input);
250+
let bytes_read = act_two_builder.fill(input);
251251

252-
// Any payload larger than ACT_TWO_LENGTH indicates a bad peer since responder data
252+
// Any payload that is not fully consumed by the builder indicates a bad peer since responder data
253253
// is required to generate post-authentication messages (so it can't come before we transition)
254-
if remaining.len() != 0 {
254+
if bytes_read < input.len() {
255255
return Err("Act Two too large".to_string());
256256
}
257257

258258
// In the event of a partial fill, stay in the same state and wait for more data
259259
if !act_two_builder.is_finished() {
260-
assert_eq!(remaining.len(), 0);
260+
assert_eq!(bytes_read, input.len());
261261
return Ok((
262262
None,
263263
HandshakeState::InitiatorAwaitingActTwo(Self {
@@ -325,11 +325,11 @@ impl IHandshakeState for ResponderAwaitingActThreeState {
325325
// https://github.com/lightningnetwork/lightning-rfc/blob/master/08-transport.md#act-three (receiver)
326326
fn next(self, input: &[u8]) -> Result<(Option<Act>, HandshakeState), String> {
327327
let mut act_three_builder = self.act_three_builder;
328-
let remaining = act_three_builder.fill(input);
328+
let bytes_read = act_three_builder.fill(input);
329329

330330
// In the event of a partial fill, stay in the same state and wait for more data
331331
if !act_three_builder.is_finished() {
332-
assert_eq!(remaining.len(), 0);
332+
assert_eq!(bytes_read, input.len());
333333
return Ok((
334334
None,
335335
HandshakeState::ResponderAwaitingActThree(Self {
@@ -392,7 +392,7 @@ impl IHandshakeState for ResponderAwaitingActThreeState {
392392

393393
// Any remaining data in the read buffer would be encrypted, so transfer ownership
394394
// to the Conduit for future use.
395-
conduit.read(remaining);
395+
conduit.read(&input[bytes_read..]);
396396

397397
Ok((
398398
None,

0 commit comments

Comments
 (0)