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 28, 2024
1 parent 279f395 commit af90cf5
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
22 changes: 22 additions & 0 deletions src/rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ You may refer to the e2e test cases in the `tests/bruno/e2e` directory for examp

* [Type `Channel`](#type-channel)
* [Type `ChannelInfo`](#type-channelinfo)
* [Type `ChannelState`](#type-channelstate)
* [Type `NodeInfo`](#type-nodeinfo)
* [Type `RemoveTlcReason`](#type-removetlcreason)
* [Type `UdtArgInfo`](#type-udtarginfo)
Expand Down Expand Up @@ -519,6 +520,27 @@ Disconnect from a peer.
## RPC Types


<a id="#type-channelstate"></a>
### Type `ChannelState`

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 Down
62 changes: 59 additions & 3 deletions src/rpc/channel.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::fiber::{
channel::{
AddTlcCommand, ChannelActorStateStore, ChannelCommand, ChannelCommandWithId, ChannelState,
RemoveTlcCommand, ShutdownCommand, UpdateCommand,
AddTlcCommand, AwaitingChannelReadyFlags, AwaitingTxSignaturesFlags,
ChannelActorStateStore, ChannelCommand, ChannelCommandWithId,
ChannelState as RawChannelState, CloseFlags, CollaboratingFundingTxFlags,
NegotiatingFundingFlags, RemoveTlcCommand, ShutdownCommand, ShuttingDownFlags,
SigningCommitmentFlags, UpdateCommand,
},
graph::PaymentSessionStatus,
hash_algorithm::HashAlgorithm,
Expand Down Expand Up @@ -129,6 +132,59 @@ pub(crate) struct ListChannelsResult {
channels: Vec<Channel>,
}

/// The state of a channel
// `ChannelState` 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 ChannelState {
/// 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<RawChannelState> for ChannelState {
fn from(state: RawChannelState) -> Self {
match state {
RawChannelState::NegotiatingFunding(flags) => ChannelState::NegotiatingFunding(flags),
RawChannelState::CollaboratingFundingTx(flags) => {
ChannelState::CollaboratingFundingTx(flags)
}
RawChannelState::SigningCommitment(flags) => ChannelState::SigningCommitment(flags),
RawChannelState::AwaitingTxSignatures(flags) => {
ChannelState::AwaitingTxSignatures(flags)
}
RawChannelState::AwaitingChannelReady(flags) => {
ChannelState::AwaitingChannelReady(flags)
}
RawChannelState::ChannelReady() => ChannelState::ChannelReady(),
RawChannelState::ShuttingDown(flags) => ChannelState::ShuttingDown(flags),
RawChannelState::Closed(flags) => ChannelState::Closed(flags),
}
}
}

/// The channel data structure
#[serde_as]
#[derive(Clone, Serialize)]
Expand Down Expand Up @@ -480,7 +536,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 af90cf5

Please sign in to comment.