From d1e14b90b849ca73c2593fa4799ee45fa01f809d Mon Sep 17 00:00:00 2001 From: yukang Date: Sun, 15 Dec 2024 11:25:28 +0800 Subject: [PATCH] code cleanup --- src/fiber/channel.rs | 60 ++++++++++---------------------------------- 1 file changed, 13 insertions(+), 47 deletions(-) diff --git a/src/fiber/channel.rs b/src/fiber/channel.rs index df7f2abb..71110c34 100644 --- a/src/fiber/channel.rs +++ b/src/fiber/channel.rs @@ -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 @@ -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)? { @@ -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, @@ -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, @@ -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( @@ -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)] @@ -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 @@ -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, @@ -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(