Skip to content

Commit 9a28496

Browse files
committed
Use Option<DataLossProtect> for ChannelReestablish
and fix test
1 parent 5d923e2 commit 9a28496

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/ln/msgs.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,16 @@ pub struct UpdateFee {
276276
pub feerate_per_kw: u32,
277277
}
278278

279+
pub struct DataLossProtect {
280+
pub your_last_per_commitment_secret: [u8; 32],
281+
pub my_current_per_commitment_point: PublicKey,
282+
}
283+
279284
pub struct ChannelReestablish {
280285
pub channel_id: [u8; 32],
281286
pub next_local_commitment_number: u64,
282287
pub next_remote_commitment_number: u64,
283-
pub your_last_per_commitment_secret: Option<[u8; 32]>,
284-
pub my_current_per_commitment_point: Option<PublicKey>,
288+
pub data_loss_protect: Option<DataLossProtect>,
285289
}
286290

287291
#[derive(Clone)]
@@ -1122,38 +1126,37 @@ impl MsgDecodable for ChannelReestablish {
11221126
return Err(DecodeError::ShortRead);
11231127
}
11241128

1125-
let (your_last_per_commitment_secret, my_current_per_commitment_point) = if v.len() > 32+2*8 {
1129+
let data_loss_protect = if v.len() > 32+2*8 {
11261130
if v.len() < 32+2*8 + 33+32 {
11271131
return Err(DecodeError::ShortRead);
11281132
}
11291133
let mut inner_array = [0; 32];
11301134
inner_array.copy_from_slice(&v[48..48+32]);
1131-
(Some(inner_array), {
1132-
let ctx = Secp256k1::without_caps();
1133-
Some(secp_pubkey!(&ctx, &v[48+32..48+32+33]))
1135+
Some(DataLossProtect {
1136+
your_last_per_commitment_secret: inner_array,
1137+
my_current_per_commitment_point: secp_pubkey!(&Secp256k1::without_caps(), &v[48+32..48+32+33]),
11341138
})
1135-
} else { (None, None) };
1139+
} else { None };
11361140

11371141
Ok(Self {
11381142
channel_id: deserialize(&v[0..32]).unwrap(),
11391143
next_local_commitment_number: byte_utils::slice_to_be64(&v[32..40]),
11401144
next_remote_commitment_number: byte_utils::slice_to_be64(&v[40..48]),
1141-
your_last_per_commitment_secret: your_last_per_commitment_secret,
1142-
my_current_per_commitment_point: my_current_per_commitment_point,
1145+
data_loss_protect: data_loss_protect,
11431146
})
11441147
}
11451148
}
11461149
impl MsgEncodable for ChannelReestablish {
11471150
fn encode(&self) -> Vec<u8> {
1148-
let mut res = Vec::with_capacity(if self.your_last_per_commitment_secret.is_some() { 32+2*8+33+32 } else { 32+2*8 });
1151+
let mut res = Vec::with_capacity(if self.data_loss_protect.is_some() { 32+2*8+33+32 } else { 32+2*8 });
11491152

11501153
res.extend_from_slice(&serialize(&self.channel_id).unwrap()[..]);
11511154
res.extend_from_slice(&byte_utils::be64_to_array(self.next_local_commitment_number));
11521155
res.extend_from_slice(&byte_utils::be64_to_array(self.next_remote_commitment_number));
11531156

1154-
if let &Some(ref ary) = &self.your_last_per_commitment_secret {
1155-
res.extend_from_slice(&ary[..]);
1156-
res.extend_from_slice(&self.my_current_per_commitment_point.expect("my_current_per_commitment_point should have been filled").serialize());
1157+
if let &Some(ref data_loss_protect) = &self.data_loss_protect {
1158+
res.extend_from_slice(&data_loss_protect.your_last_per_commitment_secret[..]);
1159+
res.extend_from_slice(&data_loss_protect.my_current_per_commitment_point.serialize());
11571160
}
11581161
res
11591162
}
@@ -1663,8 +1666,7 @@ mod tests {
16631666
channel_id: [4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0],
16641667
next_local_commitment_number: 3,
16651668
next_remote_commitment_number: 4,
1666-
your_last_per_commitment_secret: None,
1667-
my_current_per_commitment_point: None,
1669+
data_loss_protect: None,
16681670
};
16691671

16701672
let encoded_value = cr.encode();
@@ -1685,8 +1687,7 @@ mod tests {
16851687
channel_id: [4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0],
16861688
next_local_commitment_number: 3,
16871689
next_remote_commitment_number: 4,
1688-
your_last_per_commitment_secret: Some([9; 32]),
1689-
my_current_per_commitment_point: Some(public_key),
1690+
data_loss_protect: Some(msgs::DataLossProtect { your_last_per_commitment_secret: [9;32], my_current_per_commitment_point: public_key}),
16901691
};
16911692

16921693
let encoded_value = cr.encode();

0 commit comments

Comments
 (0)