Skip to content

Commit 8e79c05

Browse files
committed
Add support for initiating channel closure to Channel{,Manager}
1 parent c528ff3 commit 8e79c05

File tree

3 files changed

+149
-10
lines changed

3 files changed

+149
-10
lines changed

src/ln/channel.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,6 +1955,42 @@ impl Channel {
19551955
None => Ok(None)
19561956
}
19571957
}
1958+
1959+
/// Begins the shutdown process, getting a message for the remote peer and returning all
1960+
/// holding cell HTLCs for payment failure.
1961+
pub fn get_shutdown(&mut self) -> Result<(msgs::Shutdown, Vec<[u8; 32]>), HandleError> {
1962+
for htlc in self.pending_htlcs.iter() {
1963+
if htlc.state == HTLCState::LocalAnnounced {
1964+
return Err(HandleError{err: "Cannot begin shutdown with pending HTLCs, call send_commitment first", msg: None});
1965+
}
1966+
}
1967+
if self.channel_state & BOTH_SIDES_SHUTDOWN_MASK != 0 {
1968+
return Err(HandleError{err: "Shutdown already in progress", msg: None});
1969+
}
1970+
assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
1971+
1972+
let our_closing_script = self.get_closing_scriptpubkey();
1973+
1974+
// From here on out, we may not fail!
1975+
if self.channel_state < ChannelState::FundingSent as u32 {
1976+
self.channel_state = ChannelState::ShutdownComplete as u32;
1977+
} else {
1978+
self.channel_state |= ChannelState::LocalShutdownSent as u32;
1979+
}
1980+
1981+
// We can't send our shutdown until we've committed all of our pending HTLCs, but the
1982+
// remote side is unlikely to accept any new HTLCs, so we go ahead and "free" any holding
1983+
// cell HTLCs and return them to fail the payment.
1984+
let mut dropped_outbound_htlcs = Vec::with_capacity(self.holding_cell_htlcs.len());
1985+
for htlc in self.holding_cell_htlcs.drain(..) {
1986+
dropped_outbound_htlcs.push(htlc.payment_hash);
1987+
}
1988+
1989+
Ok((msgs::Shutdown {
1990+
channel_id: self.channel_id,
1991+
scriptpubkey: our_closing_script,
1992+
}, dropped_outbound_htlcs))
1993+
}
19581994
}
19591995

19601996
#[cfg(test)]

src/ln/channelmanager.rs

Lines changed: 108 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@ struct OnionKeys {
160160
mu: [u8; 32],
161161
}
162162

163+
pub struct ChannelDetails {
164+
/// The channel's ID (prior to funding transaction generation, this is a random 32 bytes,
165+
/// thereafter this is the txid of the funding transaction xor the funding transaction output).
166+
/// Note that this means this value is *not* persistent - it can change once during the
167+
/// lifetime of the channel.
168+
pub channel_id: Uint256,
169+
/// The position of the funding transaction in the chain. None if the funding transaction has
170+
/// not yet been confirmed and the channel fully opened.
171+
pub short_channel_id: Option<u64>,
172+
pub remote_network_id: PublicKey,
173+
pub channel_value_satoshis: u64,
174+
/// The user_id passed in to create_channel, or 0 if the channel was inbound.
175+
pub user_id: u64,
176+
}
177+
163178
impl ChannelManager {
164179
/// Constructs a new ChannelManager to hold several channels and route between them. This is
165180
/// the main "logic hub" for all channel-related actions, and implements ChannelMessageHandler.
@@ -206,6 +221,47 @@ impl ChannelManager {
206221
}
207222
}
208223

224+
/// Gets the list of open channels, in random order. See ChannelDetail field documentation for
225+
/// more information.
226+
pub fn list_channels(&self) -> Vec<ChannelDetails> {
227+
let channel_state = self.channel_state.lock().unwrap();
228+
let mut res = Vec::with_capacity(channel_state.by_id.len());
229+
for (channel_id, channel) in channel_state.by_id.iter() {
230+
res.push(ChannelDetails {
231+
channel_id: (*channel_id).clone(),
232+
short_channel_id: channel.get_short_channel_id(),
233+
remote_network_id: channel.get_their_node_id(),
234+
channel_value_satoshis: channel.get_value_satoshis(),
235+
user_id: channel.get_user_id(),
236+
});
237+
}
238+
res
239+
}
240+
241+
/// Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs
242+
/// will be accepted on the given channel, and after additional timeout/the closing of all
243+
/// pending HTLCs, the channel will be closed on chain.
244+
pub fn close_channel(&self, channel_id: &Uint256) -> Result<msgs::Shutdown, HandleError> {
245+
let res = {
246+
let mut channel_state = self.channel_state.lock().unwrap();
247+
match channel_state.by_id.entry(channel_id.clone()) {
248+
hash_map::Entry::Occupied(mut chan_entry) => {
249+
let res = chan_entry.get_mut().get_shutdown()?;
250+
if chan_entry.get().is_shutdown() {
251+
chan_entry.remove_entry();
252+
}
253+
res
254+
},
255+
hash_map::Entry::Vacant(_) => return Err(HandleError{err: "No such channel", msg: None})
256+
}
257+
};
258+
for payment_hash in res.1 {
259+
// unknown_next_peer...I dunno who that is anymore....
260+
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), &payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 10, data: &[0; 0] });
261+
}
262+
Ok(res.0)
263+
}
264+
209265
#[inline]
210266
fn gen_rho_mu_from_shared_secret(shared_secret: &SharedSecret) -> ([u8; 32], [u8; 32]) {
211267
({
@@ -1438,6 +1494,7 @@ mod tests {
14381494

14391495
use bitcoin::util::misc::hex_bytes;
14401496
use bitcoin::util::hash::Sha256dHash;
1497+
use bitcoin::util::uint::Uint256;
14411498
use bitcoin::blockdata::block::BlockHeader;
14421499
use bitcoin::blockdata::transaction::Transaction;
14431500
use bitcoin::network::constants::Network;
@@ -1452,7 +1509,7 @@ mod tests {
14521509

14531510
use rand::{thread_rng,Rng};
14541511

1455-
use std::sync::Arc;
1512+
use std::sync::{Arc, Mutex};
14561513
use std::default::Default;
14571514
use std::time::Instant;
14581515

@@ -1617,7 +1674,7 @@ mod tests {
16171674
}
16181675
}
16191676

1620-
fn create_chan_between_nodes(node_a: &ChannelManager, chain_a: &chaininterface::ChainWatchInterfaceUtil, node_b: &ChannelManager, chain_b: &chaininterface::ChainWatchInterfaceUtil) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate) {
1677+
fn create_chan_between_nodes(node_a: &ChannelManager, chain_a: &chaininterface::ChainWatchInterfaceUtil, node_b: &ChannelManager, chain_b: &chaininterface::ChainWatchInterfaceUtil) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, Uint256) {
16211678
let open_chan = node_a.create_channel(node_b.get_our_node_id(), 100000, 42).unwrap();
16221679
let accept_chan = node_b.handle_open_channel(&node_a.get_our_node_id(), &open_chan).unwrap();
16231680
node_a.handle_accept_channel(&node_b.get_our_node_id(), &accept_chan).unwrap();
@@ -1674,12 +1731,15 @@ mod tests {
16741731
_ => panic!("Unexpected event"),
16751732
};
16761733

1734+
let channel_id;
1735+
16771736
confirm_transaction(&chain_b, &tx);
16781737
let events_5 = node_b.get_and_clear_pending_events();
16791738
assert_eq!(events_5.len(), 1);
16801739
let as_announcement_sigs = match events_5[0] {
16811740
Event::SendFundingLocked { ref node_id, ref msg, ref announcement_sigs } => {
16821741
assert_eq!(*node_id, node_a.get_our_node_id());
1742+
channel_id = msg.channel_id.clone();
16831743
let as_announcement_sigs = node_a.handle_funding_locked(&node_b.get_our_node_id(), msg).unwrap().unwrap();
16841744
node_a.handle_announcement_signatures(&node_b.get_our_node_id(), &(*announcement_sigs).clone().unwrap()).unwrap();
16851745
as_announcement_sigs
@@ -1711,7 +1771,42 @@ mod tests {
17111771
CHAN_COUNT += 1;
17121772
}
17131773

1714-
((*announcement).clone(), (*as_update).clone(), (*bs_update).clone())
1774+
((*announcement).clone(), (*as_update).clone(), (*bs_update).clone(), channel_id)
1775+
}
1776+
1777+
fn close_channel(outbound_node: &ChannelManager, outbound_broadcaster: &test_utils::TestBroadcaster, inbound_node: &ChannelManager, inbound_broadcaster: &test_utils::TestBroadcaster, channel_id: &Uint256, close_inbound_first: bool) {
1778+
let (node_a, broadcaster_a) = if close_inbound_first { (inbound_node, inbound_broadcaster) } else { (outbound_node, outbound_broadcaster) };
1779+
let (node_b, broadcaster_b) = if close_inbound_first { (outbound_node, outbound_broadcaster) } else { (inbound_node, inbound_broadcaster) };
1780+
let (tx_a, tx_b);
1781+
1782+
let shutdown_a = node_a.close_channel(channel_id).unwrap();
1783+
let (shutdown_b, mut closing_signed_b) = node_b.handle_shutdown(&node_a.get_our_node_id(), &shutdown_a).unwrap();
1784+
if !close_inbound_first {
1785+
assert!(closing_signed_b.is_none());
1786+
}
1787+
let (empty_a, mut closing_signed_a) = node_a.handle_shutdown(&node_b.get_our_node_id(), &shutdown_b.unwrap()).unwrap();
1788+
assert!(empty_a.is_none());
1789+
if close_inbound_first {
1790+
assert!(closing_signed_a.is_none());
1791+
closing_signed_a = node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap()).unwrap();
1792+
assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
1793+
tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
1794+
1795+
let empty_b = node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a.unwrap()).unwrap();
1796+
assert!(empty_b.is_none());
1797+
assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
1798+
tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
1799+
} else {
1800+
closing_signed_b = node_b.handle_closing_signed(&node_a.get_our_node_id(), &closing_signed_a.unwrap()).unwrap();
1801+
assert_eq!(broadcaster_b.txn_broadcasted.lock().unwrap().len(), 1);
1802+
tx_b = broadcaster_b.txn_broadcasted.lock().unwrap().remove(0);
1803+
1804+
let empty_a2 = node_a.handle_closing_signed(&node_b.get_our_node_id(), &closing_signed_b.unwrap()).unwrap();
1805+
assert!(empty_a2.is_none());
1806+
assert_eq!(broadcaster_a.txn_broadcasted.lock().unwrap().len(), 1);
1807+
tx_a = broadcaster_a.txn_broadcasted.lock().unwrap().remove(0);
1808+
}
1809+
assert_eq!(tx_a, tx_b);
17151810
}
17161811

17171812
struct SendEvent {
@@ -1925,7 +2020,7 @@ mod tests {
19252020
let feeest_1 = Arc::new(test_utils::TestFeeEstimator { sat_per_vbyte: 1 });
19262021
let chain_monitor_1 = Arc::new(chaininterface::ChainWatchInterfaceUtil::new());
19272022
let chan_monitor_1 = Arc::new(test_utils::TestChannelMonitor{});
1928-
let tx_broadcaster_1 = Arc::new(test_utils::TestBroadcaster{});
2023+
let tx_broadcaster_1 = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
19292024
let node_id_1 = {
19302025
let mut key_slice = [0; 32];
19312026
rng.fill_bytes(&mut key_slice);
@@ -1937,7 +2032,7 @@ mod tests {
19372032
let feeest_2 = Arc::new(test_utils::TestFeeEstimator { sat_per_vbyte: 1 });
19382033
let chain_monitor_2 = Arc::new(chaininterface::ChainWatchInterfaceUtil::new());
19392034
let chan_monitor_2 = Arc::new(test_utils::TestChannelMonitor{});
1940-
let tx_broadcaster_2 = Arc::new(test_utils::TestBroadcaster{});
2035+
let tx_broadcaster_2 = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
19412036
let node_id_2 = {
19422037
let mut key_slice = [0; 32];
19432038
rng.fill_bytes(&mut key_slice);
@@ -1949,7 +2044,7 @@ mod tests {
19492044
let feeest_3 = Arc::new(test_utils::TestFeeEstimator { sat_per_vbyte: 1 });
19502045
let chain_monitor_3 = Arc::new(chaininterface::ChainWatchInterfaceUtil::new());
19512046
let chan_monitor_3 = Arc::new(test_utils::TestChannelMonitor{});
1952-
let tx_broadcaster_3 = Arc::new(test_utils::TestBroadcaster{});
2047+
let tx_broadcaster_3 = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
19532048
let node_id_3 = {
19542049
let mut key_slice = [0; 32];
19552050
rng.fill_bytes(&mut key_slice);
@@ -1961,7 +2056,7 @@ mod tests {
19612056
let feeest_4 = Arc::new(test_utils::TestFeeEstimator { sat_per_vbyte: 1 });
19622057
let chain_monitor_4 = Arc::new(chaininterface::ChainWatchInterfaceUtil::new());
19632058
let chan_monitor_4 = Arc::new(test_utils::TestChannelMonitor{});
1964-
let tx_broadcaster_4 = Arc::new(test_utils::TestBroadcaster{});
2059+
let tx_broadcaster_4 = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
19652060
let node_id_4 = {
19662061
let mut key_slice = [0; 32];
19672062
rng.fill_bytes(&mut key_slice);
@@ -2093,6 +2188,12 @@ mod tests {
20932188
claim_payment(&node_1, &vec!(&*node_2, &*node_4)[..], payment_preimage_4);
20942189
claim_payment(&node_1, &vec!(&*node_2, &*node_4)[..], payment_preimage_5);
20952190

2191+
// Close down the channels...
2192+
close_channel(&node_1, &tx_broadcaster_1, &node_2, &tx_broadcaster_2, &chan_announcement_1.3, true);
2193+
close_channel(&node_2, &tx_broadcaster_2, &node_3, &tx_broadcaster_3, &chan_announcement_2.3, false);
2194+
close_channel(&node_3, &tx_broadcaster_3, &node_4, &tx_broadcaster_4, &chan_announcement_3.3, true);
2195+
close_channel(&node_2, &tx_broadcaster_2, &node_4, &tx_broadcaster_4, &chan_announcement_4.3, false);
2196+
20962197
// Check that we processed all pending events
20972198
for node in vec!(&node_1, &node_2, &node_3, &node_4) {
20982199
assert_eq!(node.get_and_clear_pending_events().len(), 0);

src/util/test_utils.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use ln::msgs::HandleError;
66
use bitcoin::blockdata::transaction::Transaction;
77
use bitcoin::util::hash::Sha256dHash;
88

9+
use std::sync::Mutex;
10+
911
pub struct TestFeeEstimator {
1012
pub sat_per_vbyte: u64,
1113
}
@@ -26,10 +28,10 @@ impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
2628
}
2729

2830
pub struct TestBroadcaster {
29-
31+
pub txn_broadcasted: Mutex<Vec<Transaction>>,
3032
}
3133
impl chaininterface::BroadcasterInterface for TestBroadcaster {
32-
fn broadcast_transaction(&self, _tx: &Transaction) {
33-
//TODO
34+
fn broadcast_transaction(&self, tx: &Transaction) {
35+
self.txn_broadcasted.lock().unwrap().push(tx.clone());
3436
}
3537
}

0 commit comments

Comments
 (0)