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 5884740
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
24 changes: 23 additions & 1 deletion src/rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ You may refer to the e2e test cases in the `tests/bruno/e2e` directory for examp
* [Type `ChannelInfo`](#type-channelinfo)
* [Type `NodeInfo`](#type-nodeinfo)
* [Type `RemoveTlcReason`](#type-removetlcreason)
* [Type `RpcChannelState`](#type-rpcchannelstate)
* [Type `UdtArgInfo`](#type-udtarginfo)
* [Type `UdtCellDep`](#type-udtcelldep)
* [Type `UdtCfgInfos`](#type-udtcfginfos)
Expand Down Expand Up @@ -519,6 +520,27 @@ Disconnect from a peer.
## RPC Types


<a id="#type-rpcchannelstate"></a>
### Type `RpcChannelState`

The state of a channel


#### Enum with values of

* `NegotiatingFunding` - NegotiatingFundingFlags, We are negotiating the parameters required for the channel prior to funding it.
* `CollaboratingFundingTx` - CollaboratingFundingTxFlags, We're collaborating with the other party on the funding transaction.
* `SigningCommitment` - SigningCommitmentFlags, We have collaborated over the funding and are now waiting for CommitmentSigned messages.
* `AwaitingTxSignatures` - AwaitingTxSignaturesFlags, We've received and sent `commitment_signed` and are now waiting for both
party to collaborate on creating a valid funding transaction.
* `AwaitingChannelReady` - AwaitingChannelReadyFlags, We've received/sent `funding_created` and `funding_signed` and are thus now waiting on the
funding transaction to confirm.
* `ChannelReady` - , Both we and our counterparty consider the funding transaction confirmed and the channel is
now operational.
* `ShuttingDown` - ShuttingDownFlags, We've successfully negotiated a `closing_signed` dance. At this point, the `ChannelManager`
is about to drop us, but we store this anyway.
* `Closed` - CloseFlags, This channel is closed.

<a id="#type-channel"></a>
### Type `Channel`

Expand All @@ -532,7 +554,7 @@ The channel data structure
* `channel_outpoint` - `Option<OutPoint>`, The outpoint of the channel
* `peer_id` - PeerId, The peer ID of the channel
* `funding_udt_type_script` - `Option<Script>`, The UDT type script of the channel
* `state` - ChannelState, The state of the channel
* `state` - RpcChannelState, The state of the channel
* `local_balance` - u128, The local balance of the channel
* `offered_tlc_balance` - u128, The offered balance of the channel
* `remote_balance` - u128, The remote balance of the channel
Expand Down
63 changes: 59 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,59 @@ pub(crate) struct ListChannelsResult {
channels: Vec<Channel>,
}

/// The state of a 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 +201,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 +535,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 5884740

Please sign in to comment.