Skip to content

Commit 695eec2

Browse files
committed
Pull out regenerating RAA/CS in channel_reestablish handling
1 parent bd2c839 commit 695eec2

File tree

1 file changed

+67
-59
lines changed

1 file changed

+67
-59
lines changed

src/ln/channel.rs

Lines changed: 67 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,6 +2082,71 @@ impl Channel {
20822082
Ok(())
20832083
}
20842084

2085+
fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
2086+
let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(self.cur_local_commitment_transaction_number));
2087+
let per_commitment_secret = chan_utils::build_commitment_secret(self.local_keys.commitment_seed, self.cur_local_commitment_transaction_number + 2);
2088+
msgs::RevokeAndACK {
2089+
channel_id: self.channel_id,
2090+
per_commitment_secret,
2091+
next_per_commitment_point,
2092+
}
2093+
}
2094+
2095+
fn get_last_commitment_update(&self) -> msgs::CommitmentUpdate {
2096+
let mut update_add_htlcs = Vec::new();
2097+
let mut update_fulfill_htlcs = Vec::new();
2098+
let mut update_fail_htlcs = Vec::new();
2099+
let mut update_fail_malformed_htlcs = Vec::new();
2100+
2101+
for htlc in self.pending_outbound_htlcs.iter() {
2102+
if let &OutboundHTLCState::LocalAnnounced(ref onion_packet) = &htlc.state {
2103+
update_add_htlcs.push(msgs::UpdateAddHTLC {
2104+
channel_id: self.channel_id(),
2105+
htlc_id: htlc.htlc_id,
2106+
amount_msat: htlc.amount_msat,
2107+
payment_hash: htlc.payment_hash,
2108+
cltv_expiry: htlc.cltv_expiry,
2109+
onion_routing_packet: (**onion_packet).clone(),
2110+
});
2111+
}
2112+
}
2113+
2114+
for htlc in self.pending_inbound_htlcs.iter() {
2115+
if let &InboundHTLCState::LocalRemoved(ref reason) = &htlc.state {
2116+
match reason {
2117+
&InboundHTLCRemovalReason::FailRelay(ref err_packet) => {
2118+
update_fail_htlcs.push(msgs::UpdateFailHTLC {
2119+
channel_id: self.channel_id(),
2120+
htlc_id: htlc.htlc_id,
2121+
reason: err_packet.clone()
2122+
});
2123+
},
2124+
&InboundHTLCRemovalReason::FailMalformed((ref sha256_of_onion, ref failure_code)) => {
2125+
update_fail_malformed_htlcs.push(msgs::UpdateFailMalformedHTLC {
2126+
channel_id: self.channel_id(),
2127+
htlc_id: htlc.htlc_id,
2128+
sha256_of_onion: sha256_of_onion.clone(),
2129+
failure_code: failure_code.clone(),
2130+
});
2131+
},
2132+
&InboundHTLCRemovalReason::Fulfill(ref payment_preimage) => {
2133+
update_fulfill_htlcs.push(msgs::UpdateFulfillHTLC {
2134+
channel_id: self.channel_id(),
2135+
htlc_id: htlc.htlc_id,
2136+
payment_preimage: payment_preimage.clone(),
2137+
});
2138+
},
2139+
}
2140+
}
2141+
}
2142+
2143+
msgs::CommitmentUpdate {
2144+
update_add_htlcs, update_fulfill_htlcs, update_fail_htlcs, update_fail_malformed_htlcs,
2145+
update_fee: None, //TODO: We need to support re-generating any update_fees in the last commitment_signed!
2146+
commitment_signed: self.send_commitment_no_state_update().expect("It looks like we failed to re-generate a commitment_signed we had previously sent?").0,
2147+
}
2148+
}
2149+
20852150
/// May panic if some calls other than message-handling calls (which will all Err immediately)
20862151
/// have been called between remove_uncommitted_htlcs_and_mark_paused and this call.
20872152
pub fn channel_reestablish(&mut self, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, Option<ChannelMonitor>, RAACommitmentOrder), ChannelError> {
@@ -2106,13 +2171,7 @@ impl Channel {
21062171
// Note that if we need to repeat our FundingLocked we'll do that in the next if block.
21072172
None
21082173
} else if msg.next_remote_commitment_number == (INITIAL_COMMITMENT_NUMBER - 1) - self.cur_local_commitment_transaction_number {
2109-
let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(self.cur_local_commitment_transaction_number));
2110-
let per_commitment_secret = chan_utils::build_commitment_secret(self.local_keys.commitment_seed, self.cur_local_commitment_transaction_number + 2);
2111-
Some(msgs::RevokeAndACK {
2112-
channel_id: self.channel_id,
2113-
per_commitment_secret,
2114-
next_per_commitment_point,
2115-
})
2174+
Some(self.get_last_revoke_and_ack())
21162175
} else {
21172176
return Err(ChannelError::Close("Peer attempted to reestablish channel with a very old local commitment transaction"));
21182177
};
@@ -2172,59 +2231,8 @@ impl Channel {
21722231
} else {
21732232
log_debug!(self, "Reconnected channel {} with only lost remote commitment tx", log_bytes!(self.channel_id()));
21742233
}
2175-
let mut update_add_htlcs = Vec::new();
2176-
let mut update_fulfill_htlcs = Vec::new();
2177-
let mut update_fail_htlcs = Vec::new();
2178-
let mut update_fail_malformed_htlcs = Vec::new();
2179-
2180-
for htlc in self.pending_outbound_htlcs.iter() {
2181-
if let &OutboundHTLCState::LocalAnnounced(ref onion_packet) = &htlc.state {
2182-
update_add_htlcs.push(msgs::UpdateAddHTLC {
2183-
channel_id: self.channel_id(),
2184-
htlc_id: htlc.htlc_id,
2185-
amount_msat: htlc.amount_msat,
2186-
payment_hash: htlc.payment_hash,
2187-
cltv_expiry: htlc.cltv_expiry,
2188-
onion_routing_packet: (**onion_packet).clone(),
2189-
});
2190-
}
2191-
}
2192-
2193-
for htlc in self.pending_inbound_htlcs.iter() {
2194-
if let &InboundHTLCState::LocalRemoved(ref reason) = &htlc.state {
2195-
match reason {
2196-
&InboundHTLCRemovalReason::FailRelay(ref err_packet) => {
2197-
update_fail_htlcs.push(msgs::UpdateFailHTLC {
2198-
channel_id: self.channel_id(),
2199-
htlc_id: htlc.htlc_id,
2200-
reason: err_packet.clone()
2201-
});
2202-
},
2203-
&InboundHTLCRemovalReason::FailMalformed((ref sha256_of_onion, ref failure_code)) => {
2204-
update_fail_malformed_htlcs.push(msgs::UpdateFailMalformedHTLC {
2205-
channel_id: self.channel_id(),
2206-
htlc_id: htlc.htlc_id,
2207-
sha256_of_onion: sha256_of_onion.clone(),
2208-
failure_code: failure_code.clone(),
2209-
});
2210-
},
2211-
&InboundHTLCRemovalReason::Fulfill(ref payment_preimage) => {
2212-
update_fulfill_htlcs.push(msgs::UpdateFulfillHTLC {
2213-
channel_id: self.channel_id(),
2214-
htlc_id: htlc.htlc_id,
2215-
payment_preimage: payment_preimage.clone(),
2216-
});
2217-
},
2218-
}
2219-
}
2220-
}
22212234

2222-
return Ok((resend_funding_locked, required_revoke,
2223-
Some(msgs::CommitmentUpdate {
2224-
update_add_htlcs, update_fulfill_htlcs, update_fail_htlcs, update_fail_malformed_htlcs,
2225-
update_fee: None, //TODO: We need to support re-generating any update_fees in the last commitment_signed!
2226-
commitment_signed: self.send_commitment_no_state_update().expect("It looks like we failed to re-generate a commitment_signed we had previously sent?").0,
2227-
}), None, order));
2235+
return Ok((resend_funding_locked, required_revoke, Some(self.get_last_commitment_update()), None, order));
22282236
} else {
22292237
return Err(ChannelError::Close("Peer attempted to reestablish channel with a very old remote commitment transaction"));
22302238
}

0 commit comments

Comments
 (0)