Skip to content

Commit bec2595

Browse files
authored
Merge pull request #135 from yuntai/201808-channelreestablish
Make my_current_per_commitment_point in ChannelReestablish optional
2 parents 7659877 + 9a28496 commit bec2595

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

src/ln/msgs.rs

Lines changed: 21 additions & 25 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: PublicKey,
288+
pub data_loss_protect: Option<DataLossProtect>,
285289
}
286290

287291
#[derive(Clone)]
@@ -1118,48 +1122,42 @@ impl MsgEncodable for UpdateFee {
11181122

11191123
impl MsgDecodable for ChannelReestablish {
11201124
fn decode(v: &[u8]) -> Result<Self, DecodeError> {
1121-
if v.len() < 32+2*8+33 {
1125+
if v.len() < 32+2*8 {
11221126
return Err(DecodeError::ShortRead);
11231127
}
11241128

1125-
let your_last_per_commitment_secret = if v.len() > 32+2*8+33 {
1126-
if v.len() < 32+2*8+33 + 32 {
1129+
let data_loss_protect = if v.len() > 32+2*8 {
1130+
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)
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]),
1138+
})
11321139
} else { None };
11331140

1134-
let option_size = match &your_last_per_commitment_secret {
1135-
&Some(ref _ary) => 32,
1136-
&None => 0,
1137-
};
11381141
Ok(Self {
11391142
channel_id: deserialize(&v[0..32]).unwrap(),
11401143
next_local_commitment_number: byte_utils::slice_to_be64(&v[32..40]),
11411144
next_remote_commitment_number: byte_utils::slice_to_be64(&v[40..48]),
1142-
your_last_per_commitment_secret: your_last_per_commitment_secret,
1143-
my_current_per_commitment_point: {
1144-
let ctx = Secp256k1::without_caps();
1145-
secp_pubkey!(&ctx, &v[48+option_size..48+option_size+33])
1146-
}
1145+
data_loss_protect: data_loss_protect,
11471146
})
11481147
}
11491148
}
11501149
impl MsgEncodable for ChannelReestablish {
11511150
fn encode(&self) -> Vec<u8> {
1152-
let mut res = Vec::with_capacity(if self.your_last_per_commitment_secret.is_some() { 32+2*3+33 + 32 } else { 32+2*8+33 });
1151+
let mut res = Vec::with_capacity(if self.data_loss_protect.is_some() { 32+2*8+33+32 } else { 32+2*8 });
11531152

11541153
res.extend_from_slice(&serialize(&self.channel_id).unwrap()[..]);
11551154
res.extend_from_slice(&byte_utils::be64_to_array(self.next_local_commitment_number));
11561155
res.extend_from_slice(&byte_utils::be64_to_array(self.next_remote_commitment_number));
11571156

1158-
if let &Some(ref ary) = &self.your_last_per_commitment_secret {
1159-
res.extend_from_slice(&ary[..]);
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());
11601160
}
1161-
1162-
res.extend_from_slice(&self.my_current_per_commitment_point.serialize());
11631161
res
11641162
}
11651163
}
@@ -1668,14 +1666,13 @@ mod tests {
16681666
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],
16691667
next_local_commitment_number: 3,
16701668
next_remote_commitment_number: 4,
1671-
your_last_per_commitment_secret: None,
1672-
my_current_per_commitment_point: public_key,
1669+
data_loss_protect: None,
16731670
};
16741671

16751672
let encoded_value = cr.encode();
16761673
assert_eq!(
16771674
encoded_value,
1678-
vec![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, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 3, 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186, 5, 101, 215, 30, 24, 52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143]
1675+
vec![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, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4]
16791676
);
16801677
}
16811678

@@ -1690,8 +1687,7 @@ mod tests {
16901687
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],
16911688
next_local_commitment_number: 3,
16921689
next_remote_commitment_number: 4,
1693-
your_last_per_commitment_secret: Some([9; 32]),
1694-
my_current_per_commitment_point: public_key,
1690+
data_loss_protect: Some(msgs::DataLossProtect { your_last_per_commitment_secret: [9;32], my_current_per_commitment_point: public_key}),
16951691
};
16961692

16971693
let encoded_value = cr.encode();

0 commit comments

Comments
 (0)