Skip to content

Commit

Permalink
Add option to keep closed channels
Browse files Browse the repository at this point in the history
  • Loading branch information
contrun committed Jun 18, 2024
1 parent 0fc63a5 commit db66051
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/ckb/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,21 @@ pub struct ChannelActor<S> {
peer_id: PeerId,
network: ActorRef<NetworkActorMessage>,
store: S,
keep_on_closed: bool,
}

impl<S> ChannelActor<S> {
pub fn new(peer_id: PeerId, network: ActorRef<NetworkActorMessage>, store: S) -> Self {
pub fn new(
peer_id: PeerId,
network: ActorRef<NetworkActorMessage>,
store: S,
keep_on_closed: bool,
) -> Self {
Self {
peer_id,
network,
store,
keep_on_closed,
}
}

Expand Down Expand Up @@ -1082,7 +1089,9 @@ where
match state.state {
ChannelState::Closed => {
myself.stop(Some("ChannelClosed".to_string()));
self.store.delete_channel_actor_state(&state.get_id());
if self.keep_on_closed {
self.store.delete_channel_actor_state(&state.get_id());
}
}
_ => {
self.store.insert_channel_actor_state(state.clone());
Expand Down
12 changes: 12 additions & 0 deletions src/ckb/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ pub struct CkbConfig {
help = "whether to accept open channel requests with ckb funding amount automatically, unit: shannons [default: 6100000000 shannons], if this is set to zero, it means to disable auto accept"
)]
pub auto_accept_channel_ckb_funding_amount: Option<u128>,
/// whether to keep closed channels from store [default: false]
#[arg(
name = "CKB_KEEP_CLOSED_CHANNELS",
long = "ckb-keep-closed-channels",
env,
help = "whether to keep closed channels from store [default: false]"
)]
pub keep_closed_channels: Option<bool>,
}

impl CkbConfig {
Expand All @@ -75,6 +83,10 @@ impl CkbConfig {
self.auto_accept_channel_ckb_funding_amount
.unwrap_or(6100000000)
}

pub fn keep_closed_channels(&self) -> bool {
self.keep_closed_channels.unwrap_or(false)
}
}

// Basically ckb_sdk::types::NetworkType. But we added a `Mocknet` variant.
Expand Down
23 changes: 20 additions & 3 deletions src/ckb/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ pub struct NetworkActorState {
chain_actor: ActorRef<CkbChainMessage>,
open_channel_min_ckb_funding_amount: u128,
auto_accept_channel_ckb_funding_amount: u128,
keep_closed_channels: bool,
}

impl NetworkActorState {
Expand Down Expand Up @@ -774,7 +775,12 @@ impl NetworkActorState {
let (tx, rx) = oneshot::channel::<Hash256>();
let channel = Actor::spawn_linked(
None,
ChannelActor::new(peer_id.clone(), network.clone(), store),
ChannelActor::new(
peer_id.clone(),
network.clone(),
store,
self.keep_closed_channels,
),
ChannelInitializationParameter::OpenChannel(
funding_amount,
seed,
Expand Down Expand Up @@ -816,7 +822,12 @@ impl NetworkActorState {
let (tx, rx) = oneshot::channel::<Hash256>();
let channel = Actor::spawn_linked(
None,
ChannelActor::new(peer_id.clone(), network.clone(), store),
ChannelActor::new(
peer_id.clone(),
network.clone(),
store,
self.keep_closed_channels,
),
ChannelInitializationParameter::AcceptChannel(
funding_amount,
seed,
Expand Down Expand Up @@ -884,7 +895,12 @@ impl NetworkActorState {
debug!("Reestablishing channel {:?}", &channel_id);
if let Ok((channel, _)) = Actor::spawn_linked(
None,
ChannelActor::new(peer_id.clone(), self.network.clone(), store.clone()),
ChannelActor::new(
peer_id.clone(),
self.network.clone(),
store.clone(),
self.keep_closed_channels,
),
ChannelInitializationParameter::ReestablishChannel(channel_id),
self.network.get_cell(),
)
Expand Down Expand Up @@ -1168,6 +1184,7 @@ where
chain_actor: self.chain_actor.clone(),
open_channel_min_ckb_funding_amount: config.open_channel_min_ckb_funding_amount(),
auto_accept_channel_ckb_funding_amount: config.auto_accept_channel_ckb_funding_amount(),
keep_closed_channels: config.keep_closed_channels(),
})
}

Expand Down

0 comments on commit db66051

Please sign in to comment.