Skip to content

Commit 0a8a0ff

Browse files
committed
Allow for trusted inbound 0conf channels
1 parent ab6c009 commit 0a8a0ff

File tree

5 files changed

+112
-9
lines changed

5 files changed

+112
-9
lines changed

bindings/ldk_node.udl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ dictionary Config {
66
Network network;
77
NetAddress? listening_address;
88
u32 default_cltv_expiry_delta;
9+
sequence<PublicKey> peers_trusted_0conf;
910
};
1011

1112
interface Builder {

src/event.rs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ where
231231
payment_store: Arc<PaymentStore<K, L>>,
232232
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
233233
logger: L,
234-
_config: Arc<Config>,
234+
config: Arc<Config>,
235235
}
236236

237237
impl<K: KVStore + Sync + Send + 'static, L: Deref> EventHandler<K, L>
@@ -242,7 +242,7 @@ where
242242
wallet: Arc<Wallet<bdk::database::SqliteDatabase>>, event_queue: Arc<EventQueue<K, L>>,
243243
channel_manager: Arc<ChannelManager<K>>, network_graph: Arc<NetworkGraph>,
244244
keys_manager: Arc<KeysManager>, payment_store: Arc<PaymentStore<K, L>>,
245-
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>, logger: L, _config: Arc<Config>,
245+
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>, logger: L, config: Arc<Config>,
246246
) -> Self {
247247
Self {
248248
event_queue,
@@ -253,7 +253,7 @@ where
253253
payment_store,
254254
logger,
255255
runtime,
256-
_config,
256+
config,
257257
}
258258
}
259259

@@ -544,7 +544,52 @@ where
544544
}
545545
}
546546
}
547-
LdkEvent::OpenChannelRequest { .. } => {}
547+
LdkEvent::OpenChannelRequest {
548+
temporary_channel_id,
549+
counterparty_node_id,
550+
funding_satoshis,
551+
channel_type: _,
552+
push_msat: _,
553+
} => {
554+
let user_channel_id: u128 = rand::thread_rng().gen::<u128>();
555+
let allow_0conf = self.config.peers_trusted_0conf.contains(&counterparty_node_id);
556+
let res = if allow_0conf {
557+
self.channel_manager.accept_inbound_channel_from_trusted_peer_0conf(
558+
&temporary_channel_id,
559+
&counterparty_node_id,
560+
user_channel_id,
561+
)
562+
} else {
563+
self.channel_manager.accept_inbound_channel(
564+
&temporary_channel_id,
565+
&counterparty_node_id,
566+
user_channel_id,
567+
)
568+
};
569+
570+
match res {
571+
Ok(()) => {
572+
log_info!(
573+
self.logger,
574+
"Accepting inbound{} channel of {}sats from{} peer {}",
575+
if allow_0conf { " 0conf" } else { "" },
576+
funding_satoshis,
577+
if allow_0conf { " trusted" } else { "" },
578+
counterparty_node_id,
579+
);
580+
}
581+
Err(e) => {
582+
log_error!(
583+
self.logger,
584+
"Error while accepting inbound{} channel from{} peer {}: {:?}",
585+
if allow_0conf { " 0conf" } else { "" },
586+
counterparty_node_id,
587+
if allow_0conf { " trusted" } else { "" },
588+
e,
589+
);
590+
}
591+
}
592+
}
548593
LdkEvent::PaymentForwarded {
549594
prev_channel_id,
550595
next_channel_id,

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ const WALLET_KEYS_SEED_LEN: usize = 64;
207207
/// | `network` | Network::Bitcoin |
208208
/// | `listening_address` | 0.0.0.0:9735 |
209209
/// | `default_cltv_expiry_delta` | 144 |
210+
/// | `peers_trusted_0conf` | [] |
210211
///
211212
pub struct Config {
212213
/// The path where the underlying LDK and BDK persist their data.
@@ -217,6 +218,12 @@ pub struct Config {
217218
pub listening_address: Option<NetAddress>,
218219
/// The default CLTV expiry delta to be used for payments.
219220
pub default_cltv_expiry_delta: u32,
221+
/// A list of peers which we allow to establish zero confirmation channels to us.
222+
///
223+
/// **Note:** Allowing payments via zero-confirmation channels is potentially insecure if the
224+
/// funding transaction ends up never being confirmed on-chain. Zero-confirmation channels
225+
/// should therefore only be accepted from trusted peers.
226+
pub peers_trusted_0conf: Vec<PublicKey>,
220227
}
221228

222229
impl Default for Config {
@@ -226,6 +233,7 @@ impl Default for Config {
226233
network: DEFAULT_NETWORK,
227234
listening_address: Some(DEFAULT_LISTENING_ADDR.parse().unwrap()),
228235
default_cltv_expiry_delta: DEFAULT_CLTV_EXPIRY_DELTA,
236+
peers_trusted_0conf: Vec::new(),
229237
}
230238
}
231239
}
@@ -519,6 +527,11 @@ impl Builder {
519527
// Initialize the ChannelManager
520528
let mut user_config = UserConfig::default();
521529
user_config.channel_handshake_limits.force_announced_channel_preference = false;
530+
if !config.peers_trusted_0conf.is_empty() {
531+
// Manually accept inbound channels if we expect 0conf channel requests, avoid
532+
// generating the events otherwise.
533+
user_config.manually_accept_inbound_channels = true;
534+
}
522535
let channel_manager = {
523536
if let Ok(mut reader) = kv_store
524537
.read(CHANNEL_MANAGER_PERSISTENCE_NAMESPACE, CHANNEL_MANAGER_PERSISTENCE_KEY)

src/peer_store.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ where
3232
pub(crate) fn add_peer(&self, peer_info: PeerInfo) -> Result<(), Error> {
3333
let mut locked_peers = self.peers.write().unwrap();
3434

35+
if locked_peers.contains_key(&peer_info.node_id) {
36+
return Ok(());
37+
}
38+
3539
locked_peers.insert(peer_info.node_id, peer_info);
3640
self.persist_peers(&*locked_peers)
3741
}

src/test/functional_tests.rs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
use crate::io::KVStore;
12
use crate::test::utils::*;
23
use crate::test::utils::{expect_event, random_config};
3-
use crate::{Builder, Error, Event, PaymentDirection, PaymentStatus};
4+
use crate::{Builder, Error, Event, Node, PaymentDirection, PaymentStatus};
45

56
use bitcoin::Amount;
7+
use electrsd::bitcoind::BitcoinD;
8+
use electrsd::ElectrsD;
9+
10+
use std::sync::Arc;
611

712
#[test]
813
fn channel_full_cycle() {
@@ -14,14 +19,46 @@ fn channel_full_cycle() {
1419
builder_a.set_esplora_server(esplora_url.clone());
1520
let node_a = builder_a.build();
1621
node_a.start().unwrap();
17-
let addr_a = node_a.new_funding_address().unwrap();
1822

1923
println!("\n== Node B ==");
2024
let config_b = random_config();
2125
let builder_b = Builder::from_config(config_b);
2226
builder_b.set_esplora_server(esplora_url);
2327
let node_b = builder_b.build();
2428
node_b.start().unwrap();
29+
30+
do_channel_full_cycle(node_a, node_b, &bitcoind, &electrsd, false);
31+
}
32+
33+
#[test]
34+
fn channel_full_cycle_0conf() {
35+
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
36+
println!("== Node A ==");
37+
let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap());
38+
let config_a = random_config();
39+
let builder_a = Builder::from_config(config_a);
40+
builder_a.set_esplora_server(esplora_url.clone());
41+
let node_a = builder_a.build();
42+
node_a.start().unwrap();
43+
44+
println!("\n== Node B ==");
45+
let mut config_b = random_config();
46+
config_b.peers_trusted_0conf.push(node_a.node_id());
47+
48+
let builder_b = Builder::from_config(config_b);
49+
builder_b.set_esplora_server(esplora_url.clone());
50+
let node_b = builder_b.build();
51+
52+
node_b.start().unwrap();
53+
54+
do_channel_full_cycle(node_a, node_b, &bitcoind, &electrsd, true)
55+
}
56+
57+
fn do_channel_full_cycle<K: KVStore + Sync + Send>(
58+
node_a: Arc<Node<K>>, node_b: Arc<Node<K>>, bitcoind: &BitcoinD, electrsd: &ElectrsD,
59+
allow_0conf: bool,
60+
) {
61+
let addr_a = node_a.new_funding_address().unwrap();
2562
let addr_b = node_b.new_funding_address().unwrap();
2663

2764
let premine_amount_sat = 100_000;
@@ -71,8 +108,11 @@ fn channel_full_cycle() {
71108

72109
wait_for_tx(&electrsd, funding_txo.txid);
73110

74-
println!("\n .. generating blocks, syncing wallets .. ");
75-
generate_blocks_and_wait(&bitcoind, &electrsd, 6);
111+
if !allow_0conf {
112+
println!("\n .. generating blocks ..");
113+
generate_blocks_and_wait(&bitcoind, &electrsd, 6);
114+
}
115+
76116
node_a.sync_wallets().unwrap();
77117
node_b.sync_wallets().unwrap();
78118

@@ -262,7 +302,7 @@ fn channel_open_fails_when_funds_insufficient() {
262302
node_b.listening_address().unwrap().into(),
263303
120000,
264304
None,
265-
true
305+
true,
266306
)
267307
);
268308
}

0 commit comments

Comments
 (0)