Skip to content

Commit

Permalink
Fix channel state issue in RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Nov 27, 2024
1 parent 279f395 commit ac2cd64
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
62 changes: 58 additions & 4 deletions src/rpc/channel.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::fiber::{
channel::{
AddTlcCommand, ChannelActorStateStore, ChannelCommand, ChannelCommandWithId, ChannelState,
RemoveTlcCommand, ShutdownCommand, UpdateCommand,
AddTlcCommand, AwaitingChannelReadyFlags, AwaitingTxSignaturesFlags,
ChannelActorStateStore, ChannelCommand, ChannelCommandWithId, ChannelState, CloseFlags,
CollaboratingFundingTxFlags, NegotiatingFundingFlags, RemoveTlcCommand, ShutdownCommand,
ShuttingDownFlags, SigningCommitmentFlags, UpdateCommand,
},
graph::PaymentSessionStatus,
hash_algorithm::HashAlgorithm,
Expand Down Expand Up @@ -129,6 +131,58 @@ pub(crate) struct ListChannelsResult {
channels: Vec<Channel>,
}

// `RpcChannelState` is a copy of `ChannelState` with `#[serde(...)]` attributes for compatibility
// `bincode` does not support deserialize_identifier
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(
rename_all = "SCREAMING_SNAKE_CASE",
tag = "state_name",
content = "state_flags"
)]
pub enum RpcChannelState {
/// We are negotiating the parameters required for the channel prior to funding it.
NegotiatingFunding(NegotiatingFundingFlags),
/// We're collaborating with the other party on the funding transaction.
CollaboratingFundingTx(CollaboratingFundingTxFlags),
/// We have collaborated over the funding and are now waiting for CommitmentSigned messages.
SigningCommitment(SigningCommitmentFlags),
/// We've received and sent `commitment_signed` and are now waiting for both
/// party to collaborate on creating a valid funding transaction.
AwaitingTxSignatures(AwaitingTxSignaturesFlags),
/// We've received/sent `funding_created` and `funding_signed` and are thus now waiting on the
/// funding transaction to confirm.
AwaitingChannelReady(AwaitingChannelReadyFlags),
/// Both we and our counterparty consider the funding transaction confirmed and the channel is
/// now operational.
ChannelReady(),
/// We've successfully negotiated a `closing_signed` dance. At this point, the `ChannelManager`
/// is about to drop us, but we store this anyway.
ShuttingDown(ShuttingDownFlags),
/// This channel is closed.
Closed(CloseFlags),
}

impl From<ChannelState> for RpcChannelState {
fn from(state: ChannelState) -> Self {
match state {
ChannelState::NegotiatingFunding(flags) => RpcChannelState::NegotiatingFunding(flags),
ChannelState::CollaboratingFundingTx(flags) => {
RpcChannelState::CollaboratingFundingTx(flags)
}
ChannelState::SigningCommitment(flags) => RpcChannelState::SigningCommitment(flags),
ChannelState::AwaitingTxSignatures(flags) => {
RpcChannelState::AwaitingTxSignatures(flags)
}
ChannelState::AwaitingChannelReady(flags) => {
RpcChannelState::AwaitingChannelReady(flags)
}
ChannelState::ChannelReady() => RpcChannelState::ChannelReady(),
ChannelState::ShuttingDown(flags) => RpcChannelState::ShuttingDown(flags),
ChannelState::Closed(flags) => RpcChannelState::Closed(flags),
}
}
}

/// The channel data structure
#[serde_as]
#[derive(Clone, Serialize)]
Expand All @@ -146,7 +200,7 @@ pub(crate) struct Channel {
/// The UDT type script of the channel
funding_udt_type_script: Option<Script>,
/// The state of the channel
state: ChannelState,
state: RpcChannelState,
/// The local balance of the channel
#[serde_as(as = "U128Hex")]
local_balance: u128,
Expand Down Expand Up @@ -480,7 +534,7 @@ where
.funding_udt_type_script
.clone()
.map(Into::into),
state: state.state,
state: state.state.into(),
local_balance: state.get_local_balance(),
remote_balance: state.get_remote_balance(),
offered_tlc_balance: state.get_offered_tlc_balance(),
Expand Down
3 changes: 3 additions & 0 deletions tests/bruno/e2e/3-nodes-transfer/20-node3-list-channels.bru
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ script:post-response {
if (res.body.result.channels[0].local_balance != "0xe7b68ae00" || res.body.result.channels[0].remote_balance != "0x376abe5fa400") {
throw new Error("Assertion failed: channel amount is not right");
}
if (res.body.result.channels[0].state.state_name != "CHANNEL_READY") {
throw new Error("Assertion failed: channel status is not right");
}
}

0 comments on commit ac2cd64

Please sign in to comment.