Skip to content

Commit 430ced3

Browse files
committed
f Check on-chain wallet before we try to open chan
1 parent d26b8fb commit 430ced3

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

src/error.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::fmt;
22

3-
#[derive(Debug)]
3+
#[derive(Debug, PartialEq, Eq)]
44
/// An error that possibly needs to be handled by the user.
55
pub enum Error {
66
/// Returned when trying to start [`crate::Node`] while it is already running.
@@ -17,6 +17,8 @@ pub enum Error {
1717
InvoiceInvalid,
1818
/// Invoice creation failed.
1919
InvoiceCreationFailed,
20+
/// There are insufficient funds to complete the given operation.
21+
FundsInsufficient,
2022
/// An attempted payment has failed.
2123
PaymentFailed,
2224
/// A given peer info could not be parsed.
@@ -47,6 +49,9 @@ impl fmt::Display for Error {
4749
Self::NonUniquePaymentHash => write!(f, "An invoice must not get payed twice."),
4850
Self::InvoiceInvalid => write!(f, "The given invoice is invalid."),
4951
Self::InvoiceCreationFailed => write!(f, "Failed to create invoice."),
52+
Self::FundsInsufficient => {
53+
write!(f, "There are insufficient funds to complete the given operation.")
54+
}
5055
Self::PaymentFailed => write!(f, "Failed to send the given payment."),
5156
Self::PeerInfoParseFailed => write!(f, "Failed to parse the given peer information."),
5257
Self::ChannelCreationFailed => write!(f, "Failed to create channel."),

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,12 @@ impl Node {
719719

720720
let runtime = runtime_lock.as_ref().unwrap();
721721

722+
let cur_balance = self.wallet.get_balance()?;
723+
if cur_balance.get_spendable() < channel_amount_sats {
724+
log_error!(self.logger, "Unable to create channel due to insufficient funds.");
725+
return Err(Error::FundsInsufficient);
726+
}
727+
722728
let peer_info = PeerInfo::try_from(node_pubkey_and_address.to_string())?;
723729

724730
let con_peer_info = peer_info.clone();

src/tests/functional_tests.rs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::tests::test_utils::expect_event;
2-
use crate::{Builder, Config, Event};
2+
use crate::{Builder, Config, Error, Event};
33

44
use bitcoin::{Address, Amount};
55
use bitcoind::bitcoincore_rpc::RpcApi;
@@ -88,14 +88,16 @@ where
8888
fn premine_and_distribute_funds(addrs: Vec<Address>, amount: Amount) {
8989
PREMINE.get_or_init(|| {
9090
generate_blocks_and_wait(101);
91-
for addr in addrs {
92-
get_bitcoind()
93-
.client
94-
.send_to_address(&addr, amount, None, None, None, None, None, None)
95-
.unwrap();
96-
}
97-
generate_blocks_and_wait(1);
9891
});
92+
93+
for addr in addrs {
94+
get_bitcoind()
95+
.client
96+
.send_to_address(&addr, amount, None, None, None, None, None, None)
97+
.unwrap();
98+
}
99+
100+
generate_blocks_and_wait(1);
99101
}
100102

101103
fn rand_config() -> Config {
@@ -199,3 +201,32 @@ fn channel_full_cycle() {
199201
node_b.stop().unwrap();
200202
println!("\nB stopped");
201203
}
204+
205+
#[test]
206+
fn channel_open_fails_when_funds_insufficient() {
207+
println!("== Node A ==");
208+
let config_a = rand_config();
209+
let node_a = Builder::from_config(config_a).build();
210+
node_a.start().unwrap();
211+
let addr_a = node_a.new_funding_address().unwrap();
212+
213+
println!("\n== Node B ==");
214+
let config_b = rand_config();
215+
let node_b = Builder::from_config(config_b).build();
216+
node_b.start().unwrap();
217+
let addr_b = node_b.new_funding_address().unwrap();
218+
219+
premine_and_distribute_funds(vec![addr_a, addr_b], Amount::from_sat(100000));
220+
node_a.sync_wallets().unwrap();
221+
node_b.sync_wallets().unwrap();
222+
assert_eq!(node_a.on_chain_balance().unwrap().get_spendable(), 100000);
223+
assert_eq!(node_b.on_chain_balance().unwrap().get_spendable(), 100000);
224+
225+
println!("\nA -- connect_open_channel -> B");
226+
let node_b_addr =
227+
format!("{}@{}", node_b.node_id().unwrap(), node_b.listening_address().unwrap());
228+
assert_eq!(
229+
Err(Error::FundsInsufficient),
230+
node_a.connect_open_channel(&node_b_addr, 120000, true)
231+
);
232+
}

0 commit comments

Comments
 (0)