Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Dec 15, 2024
1 parent 389579d commit d1e14b9
Showing 1 changed file with 13 additions and 47 deletions.
60 changes: 13 additions & 47 deletions src/fiber/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,9 @@ where
));
}
CkbInvoiceStatus::Paid => {
unreachable!("Paid invoice should not be paid again");
// we have already checked invoice status in apply_add_tlc_operation_with_peeled_onion_packet
// this maybe happened when process is killed and restart
error!("invoice already paid, ignore");
}
_ => {
self.store
Expand Down Expand Up @@ -796,7 +798,7 @@ where
// - Extract public key from onion_packet[1..34]
// - Obtain share secret using DH Key Exchange from the public key and the network private key stored in the network actor state.
if let Some(peeled_onion_packet) = self
.apply_add_tlc_operation_without_peeled_onion_packet(state, add_tlc)
.try_add_tlc_peel_onion_packet(state, add_tlc)
.await
.map_err(ProcessingChannelError::without_shared_secret)?
{
Expand Down Expand Up @@ -830,7 +832,7 @@ where
Ok(())
}

async fn apply_add_tlc_operation_without_peeled_onion_packet(
async fn try_add_tlc_peel_onion_packet(
&self,
state: &mut ChannelActorState,
add_tlc: &AddTlcInfo,
Expand Down Expand Up @@ -1189,16 +1191,16 @@ where
state.check_for_tlc_update(Some(command.amount), true, true)?;
state.check_tlc_expiry(command.expiry)?;
let tlc = state.create_outbounding_tlc(command.clone());
state.check_insert_tlc(tlc.as_add_tlc())?;
state.tlc_state.add_local_tlc(tlc.clone());
state.check_insert_tlc(&tlc)?;
state.tlc_state.add_local_tlc(TlcKind::AddTlc(tlc.clone()));
state.increment_next_offered_tlc_id();

debug!("Inserted tlc into channel state: {:?}", &tlc);
let add_tlc = AddTlc {
channel_id: state.get_id(),
tlc_id: tlc.tlc_id().into(),
tlc_id: tlc.tlc_id.into(),
amount: command.amount,
payment_hash: tlc.payment_hash(),
payment_hash: command.payment_hash,
expiry: command.expiry,
hash_algorithm: command.hash_algorithm,
onion_packet: command.onion_packet,
Expand All @@ -1217,7 +1219,7 @@ where

self.handle_commitment_signed_command(state)?;
state.tlc_state.set_waiting_ack(true);
Ok(tlc.tlc_id().into())
Ok(tlc.tlc_id.into())
}

pub fn handle_remove_tlc_command(
Expand Down Expand Up @@ -2266,42 +2268,6 @@ impl TlcKind {
pub fn is_received(&self) -> bool {
!self.is_offered()
}

pub fn flip_mut(&mut self) {
match self {
TlcKind::AddTlc(info) => info.tlc_id.flip_mut(),
TlcKind::RemoveTlc(_) => {
unreachable!("RemoveTlc should not flip")
}
}
}

pub fn amount(&self) -> u128 {
match self {
TlcKind::AddTlc(add_tlc) => add_tlc.amount,
TlcKind::RemoveTlc(..) => {
unreachable!("RemoveTlc should not have amount")
}
}
}

pub fn payment_hash(&self) -> Hash256 {
match self {
TlcKind::AddTlc(add_tlc) => add_tlc.payment_hash,
TlcKind::RemoveTlc(..) => {
unreachable!("RemoveTlc should not have payment hash")
}
}
}

pub fn as_add_tlc(&self) -> &AddTlcInfo {
match self {
TlcKind::AddTlc(add_tlc) => &add_tlc,
TlcKind::RemoveTlc(..) => {
unreachable!("RemoveTlc should not be AddTlc")
}
}
}
}

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -4797,7 +4763,7 @@ impl ChannelActorState {
Ok(())
}

pub fn create_outbounding_tlc(&self, command: AddTlcCommand) -> TlcKind {
pub fn create_outbounding_tlc(&self, command: AddTlcCommand) -> AddTlcInfo {
// TODO: we are filling the user command with a new id here.
// The advantage of this is that we don't need to burden the users to
// provide a next id for each tlc. The disadvantage is that users may
Expand All @@ -4810,7 +4776,7 @@ impl ChannelActorState {
"Must not have the same id in pending offered tlcs"
);

TlcKind::AddTlc(AddTlcInfo {
AddTlcInfo {
channel_id: self.get_id(),
tlc_id: TLCId::Offered(id),
amount: command.amount,
Expand All @@ -4825,7 +4791,7 @@ impl ChannelActorState {
previous_tlc: command
.previous_tlc
.map(|(channel_id, tlc_id)| (channel_id, TLCId::Received(tlc_id))),
})
}
}

pub fn create_inbounding_tlc(
Expand Down

0 comments on commit d1e14b9

Please sign in to comment.