Skip to content

Commit

Permalink
more on remove tlc
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Nov 29, 2024
1 parent 695d013 commit 32c5656
Showing 1 changed file with 55 additions and 6 deletions.
61 changes: 55 additions & 6 deletions src/fiber/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,8 @@ where
FiberChannelMessage::AddTlc(add_tlc) => {
self.handle_add_tlc_peer_message(state, add_tlc)
}
FiberChannelMessage::RemoveTlc(_remove_tlc) => {
state.check_for_tlc_update(None, false)?;
// state
// .staging_tlc_operations
// .push(TlcOperation::RemoveTlc(remove_tlc.clone()));
Ok(())
FiberChannelMessage::RemoveTlc(remove_tlc) => {
self.handle_remove_tlc_peer_message(state, remove_tlc)
}
FiberChannelMessage::Shutdown(shutdown) => {
let flags = match state.state {
Expand Down Expand Up @@ -911,6 +907,30 @@ where
Ok(())
}

fn handle_remove_tlc_peer_message(
&self,
state: &mut ChannelActorState,
remove_tlc: RemoveTlc,
) -> Result<(), ProcessingChannelError> {
state.check_for_tlc_update(None, false)?;
// TODO: here if we received a invalid remove tlc, it's maybe a malioucious peer,
// maybe we need to go through shutdown process for this error
state
.check_remove_tlc_with_reason(TLCId::Offered(remove_tlc.tlc_id), &remove_tlc.reason)?;
state.tlc_state.add_remote_tlc(TlcInfo {
id: TLCId::Received(remove_tlc.tlc_id),
tlc_op: TlcOperation::RemoveTlc(remove_tlc.clone()),
created_at: state.get_current_commitment_numbers(),
previous_tlc: None,
removed_at: None,
removal_confirmed_at: None,
creation_confirmed_at: None,
payment_preimage: None,
relay_status: TlcRelayStatus::NoForward,
});
Ok(())
}

async fn apply_add_tlc_peer_message(
&self,
state: &mut ChannelActorState,
Expand Down Expand Up @@ -1179,6 +1199,7 @@ where
command: RemoveTlcCommand,
) -> ProcessingChannelResult {
state.check_for_tlc_update(None, false)?;
state.check_remove_tlc_with_reason(TLCId::Received(command.id), &command.reason)?;
let tlc_info = TlcInfo {
id: TLCId::Received(command.id),
tlc_op: TlcOperation::RemoveTlc(RemoveTlc {
Expand Down Expand Up @@ -4456,6 +4477,34 @@ impl ChannelActorState {
Ok(())
}

// Check whether the reason is valid for removing the tlc.
fn check_remove_tlc_with_reason(
&self,
tlc_id: TLCId,
reason: &RemoveTlcReason,
) -> ProcessingChannelResult {
if let Some(tlc) = self.tlc_state.get(&tlc_id) {
if tlc.removed_at.is_some() {
return Err(ProcessingChannelError::RepeatedProcessing(
"TLC is already removed".to_string(),
));
}
if let RemoveTlcReason::RemoveTlcFulfill(fulfill) = reason {
if let Some(preimage) = tlc.payment_preimage {
if preimage != fulfill.payment_preimage {
return Err(ProcessingChannelError::FinalIncorrectPreimage);
}
}
}
Ok(())
} else {
return Err(ProcessingChannelError::InvalidParameter(format!(
"Trying to remove non-existing tlc with id {:?}",
tlc_id
)));
}
}

fn check_for_tlc_update(
&self,
add_tlc_amount: Option<u128>,
Expand Down

0 comments on commit 32c5656

Please sign in to comment.