Skip to content

Commit

Permalink
fix remove tlc e2e failure
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Oct 13, 2024
1 parent 0240cea commit 5d4e998
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 24 deletions.
8 changes: 6 additions & 2 deletions src/fiber/tests/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::test_utils::generate_seckey;
use crate::fiber::{
gen::fiber as molecule_fiber,
hash_algorithm::HashAlgorithm,
Expand All @@ -10,8 +11,7 @@ use crate::fiber::{
use ckb_types::packed::OutPointBuilder;
use ckb_types::prelude::Builder;
use secp256k1::{Secp256k1, SecretKey};

use super::test_utils::generate_seckey;
use std::str::FromStr;

#[test]
fn test_serde_public_key() {
Expand Down Expand Up @@ -115,4 +115,8 @@ fn test_tlc_fail_error() {
let tlc_fail = TlcErrPacket::new(node_fail.clone());
let convert_back = tlc_fail.decode().expect("decoded fail");
assert_eq!(node_fail, convert_back);

let error_code = TlcErrorCode::PermanentNodeFailure;
let convert = TlcErrorCode::from_str("PermanentNodeFailure").expect("convert error");
assert_eq!(error_code, convert);
}
4 changes: 2 additions & 2 deletions src/fiber/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use std::marker::PhantomData;
use std::str::FromStr;
use strum::{AsRefStr, IntoStaticStr};
use strum::{AsRefStr, EnumString};
use tentacle::multiaddr::MultiAddr;
use tentacle::secio::PeerId;
use thiserror::Error;
Expand Down Expand Up @@ -1399,7 +1399,7 @@ const NODE: u16 = 0x2000;
const UPDATE: u16 = 0x1000;

#[repr(u16)]
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq, AsRefStr, IntoStaticStr)]
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq, AsRefStr, EnumString)]
pub enum TlcErrorCode {
TemporaryNodeFailure = NODE | 2,
PermanentNodeFailure = PERM | NODE | 2,
Expand Down
41 changes: 26 additions & 15 deletions src/rpc/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::fiber::{
graph::PaymentSessionStatus,
hash_algorithm::HashAlgorithm,
network::{AcceptChannelCommand, OpenChannelCommand, SendPaymentCommand},
serde_utils::{U128Hex, U16Hex, U64Hex},
types::{Hash256, LockTime, Pubkey, RemoveTlcFulfill},
serde_utils::{U128Hex, U64Hex},
types::{Hash256, LockTime, Pubkey, RemoveTlcFulfill, TlcErr, TlcErrPacket, TlcErrorCode},
NetworkActorCommand, NetworkActorMessage,
};
use crate::{handle_actor_call, handle_actor_cast, log_and_error};
Expand All @@ -22,6 +22,7 @@ use ractor::{call, ActorRef};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
use std::cmp::Reverse;
use std::str::FromStr;
use tentacle::secio::PeerId;

#[serde_as]
Expand Down Expand Up @@ -128,7 +129,7 @@ pub(crate) struct AddTlcResult {
}

#[serde_as]
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub(crate) struct RemoveTlcParams {
channel_id: Hash256,
#[serde_as(as = "U64Hex")]
Expand All @@ -137,16 +138,11 @@ pub(crate) struct RemoveTlcParams {
}

#[serde_as]
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
enum RemoveTlcReason {
RemoveTlcFulfill {
payment_preimage: Hash256,
},
RemoveTlcFail {
#[serde_as(as = "U16Hex")]
error_code: u16,
},
RemoveTlcFulfill { payment_preimage: Hash256 },
RemoveTlcFail { error_code: String },
}

#[serde_as]
Expand Down Expand Up @@ -423,22 +419,37 @@ where
}

async fn remove_tlc(&self, params: RemoveTlcParams) -> Result<(), ErrorObjectOwned> {
let err_code = match &params.reason {
RemoveTlcReason::RemoveTlcFail { error_code } => {
let Ok(err) = TlcErrorCode::from_str(&error_code) else {
return log_and_error!(params, format!("invalid error code: {}", error_code));
};
Some(err)
}
_ => None,
};
let message = |rpc_reply| -> NetworkActorMessage {
NetworkActorMessage::Command(NetworkActorCommand::ControlFiberChannel(
ChannelCommandWithId {
channel_id: params.channel_id,
command: ChannelCommand::RemoveTlc(
RemoveTlcCommand {
id: params.tlc_id,
reason: match params.reason {
reason: match &params.reason {
RemoveTlcReason::RemoveTlcFulfill { payment_preimage } => {
crate::fiber::types::RemoveTlcReason::RemoveTlcFulfill(
RemoveTlcFulfill { payment_preimage },
RemoveTlcFulfill {
payment_preimage: *payment_preimage,
},
)
}
RemoveTlcReason::RemoveTlcFail { error_code: _ } => {
RemoveTlcReason::RemoveTlcFail { .. } => {
// TODO: maybe we should remove this PRC or move add_tlc and remove_tlc to `test` module?
unimplemented!("RemoveTlcFail is only for internal use");
crate::fiber::types::RemoveTlcReason::RemoveTlcFail(
TlcErrPacket::new(TlcErr::new(
err_code.expect("expect error code"),
)),
)
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ body:json {
"channel_id": "{{CHANNEL_ID}}",
"tlc_id": "{{TLC_ID2}}",
"reason": {
"error_code": "0x2a"
"error_code": "TemporaryNodeFailure"
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ body:json {
"channel_id": "{{CHANNEL_ID}}",
"tlc_id": "{{TLC_ID3}}",
"reason": {
"error_code": "0x2a"
"error_code": "TemporaryNodeFailure"
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ body:json {
"channel_id": "{{CHANNEL_ID}}",
"tlc_id": "{{TLC_ID3}}",
"reason": {
"error_code": "0x2a"
"error_code": "TemporaryNodeFailure"
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ body:json {
"channel_id": "{{CHANNEL_ID}}",
"tlc_id": "{{TLC_ID6}}",
"reason": {
"error_code": "0x2a"
"error_code": "TemporaryNodeFailure"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion tests/bruno/e2e/reestablish/12-remove-tlc-from-NODE1.bru
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ body:json {
"channel_id": "{{CHANNEL_ID}}",
"tlc_id": "{{TLC_ID1}}",
"reason": {
"error_code": "0x2a"
"error_code": "TemporaryNodeFailure"
}
}
]
Expand Down

0 comments on commit 5d4e998

Please sign in to comment.