Skip to content

Commit fa59544

Browse files
channel: refactor max funding consts
MAX_FUNDING_SATOSHIS will no longer be accurately named once wumbo is merged. Also, we'll want to check that wumbo channels don't exceed the total bitcoin supply
1 parent 7bf7b3a commit fa59544

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

lightning/src/ln/channel.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -734,9 +734,13 @@ pub const COMMITMENT_TX_WEIGHT_PER_HTLC: u64 = 172;
734734

735735
pub const ANCHOR_OUTPUT_VALUE_SATOSHI: u64 = 330;
736736

737-
/// Maximum `funding_satoshis` value, according to the BOLT #2 specification
738-
/// it's 2^24.
739-
pub const MAX_FUNDING_SATOSHIS: u64 = 1 << 24;
737+
/// Maximum `funding_satoshis` value according to the BOLT #2 specification, if
738+
/// `option_support_large_channel` (aka wumbo channels) is not supported.
739+
/// It's 2^24.
740+
pub const MAX_FUNDING_SATOSHIS_NO_WUMBO: u64 = 1 << 24;
741+
742+
/// Total bitcoin supply in satoshis.
743+
pub const TOTAL_BITCOIN_SUPPLY_SATOSHIS: u64 = 21_000_000 * 1_0000_0000;
740744

741745
/// The maximum network dust limit for standard script formats. This currently represents the
742746
/// minimum output value for a P2SH output before Bitcoin Core 22 considers the entire
@@ -850,8 +854,8 @@ impl<Signer: Sign> Channel<Signer> {
850854
let holder_signer = keys_provider.get_channel_signer(false, channel_value_satoshis);
851855
let pubkeys = holder_signer.pubkeys().clone();
852856

853-
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
854-
return Err(APIError::APIMisuseError{err: format!("funding_value must be smaller than {}, it was {}", MAX_FUNDING_SATOSHIS, channel_value_satoshis)});
857+
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS_NO_WUMBO {
858+
return Err(APIError::APIMisuseError{err: format!("funding_value must be smaller than {}, it was {}", MAX_FUNDING_SATOSHIS_NO_WUMBO, channel_value_satoshis)});
855859
}
856860
let channel_value_msat = channel_value_satoshis * 1000;
857861
if push_msat > channel_value_msat {
@@ -1076,8 +1080,8 @@ impl<Signer: Sign> Channel<Signer> {
10761080
}
10771081

10781082
// Check sanity of message fields:
1079-
if msg.funding_satoshis >= MAX_FUNDING_SATOSHIS {
1080-
return Err(ChannelError::Close(format!("Funding must be smaller than {}. It was {}", MAX_FUNDING_SATOSHIS, msg.funding_satoshis)));
1083+
if msg.funding_satoshis >= MAX_FUNDING_SATOSHIS_NO_WUMBO {
1084+
return Err(ChannelError::Close(format!("Funding must be smaller than {}. It was {}", MAX_FUNDING_SATOSHIS_NO_WUMBO, msg.funding_satoshis)));
10811085
}
10821086
if msg.channel_reserve_satoshis > msg.funding_satoshis {
10831087
return Err(ChannelError::Close(format!("Bogus channel_reserve_satoshis ({}). Must be not greater than funding_satoshis: {}", msg.channel_reserve_satoshis, msg.funding_satoshis)));
@@ -4110,7 +4114,7 @@ impl<Signer: Sign> Channel<Signer> {
41104114
if !self.pending_inbound_htlcs.is_empty() || !self.pending_outbound_htlcs.is_empty() {
41114115
return Err(ChannelError::Close("Remote end sent us a closing_signed while there were still pending HTLCs".to_owned()));
41124116
}
4113-
if msg.fee_satoshis > 21_000_000 * 1_0000_0000 { //this is required to stop potential overflow in build_closing_transaction
4117+
if msg.fee_satoshis > TOTAL_BITCOIN_SUPPLY_SATOSHIS { // this is required to stop potential overflow in build_closing_transaction
41144118
return Err(ChannelError::Close("Remote tried to send us a closing tx with > 21 million BTC fee".to_owned()));
41154119
}
41164120

@@ -6298,7 +6302,7 @@ mod tests {
62986302
use ln::PaymentHash;
62996303
use ln::channelmanager::{HTLCSource, PaymentId};
63006304
use ln::channel::{Channel, InboundHTLCOutput, OutboundHTLCOutput, InboundHTLCState, OutboundHTLCState, HTLCCandidate, HTLCInitiator};
6301-
use ln::channel::MAX_FUNDING_SATOSHIS;
6305+
use ln::channel::{MAX_FUNDING_SATOSHIS_NO_WUMBO, TOTAL_BITCOIN_SUPPLY_SATOSHIS};
63026306
use ln::features::InitFeatures;
63036307
use ln::msgs::{ChannelUpdate, DataLossProtect, DecodeError, OptionalField, UnsignedChannelUpdate};
63046308
use ln::script::ShutdownScript;
@@ -6334,9 +6338,10 @@ mod tests {
63346338
}
63356339

63366340
#[test]
6337-
fn test_max_funding_satoshis() {
6338-
assert!(MAX_FUNDING_SATOSHIS <= 21_000_000 * 100_000_000,
6339-
"MAX_FUNDING_SATOSHIS is greater than all satoshis in existence");
6341+
fn test_max_funding_satoshis_no_wumbo() {
6342+
assert_eq!(TOTAL_BITCOIN_SUPPLY_SATOSHIS, 21_000_000 * 100_000_000);
6343+
assert!(MAX_FUNDING_SATOSHIS_NO_WUMBO <= TOTAL_BITCOIN_SUPPLY_SATOSHIS,
6344+
"MAX_FUNDING_SATOSHIS_NO_WUMBO is greater than all satoshis in existence");
63406345
}
63416346

63426347
#[test]

lightning/src/ln/functional_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ fn test_insane_channel_opens() {
9292
} else { assert!(false); }
9393
};
9494

95-
use ln::channel::MAX_FUNDING_SATOSHIS;
95+
use ln::channel::MAX_FUNDING_SATOSHIS_NO_WUMBO;
9696
use ln::channelmanager::MAX_LOCAL_BREAKDOWN_TIMEOUT;
9797

9898
// Test all mutations that would make the channel open message insane
99-
insane_open_helper(format!("Funding must be smaller than {}. It was {}", MAX_FUNDING_SATOSHIS, MAX_FUNDING_SATOSHIS).as_str(), |mut msg| { msg.funding_satoshis = MAX_FUNDING_SATOSHIS; msg });
99+
insane_open_helper(format!("Funding must be smaller than {}. It was {}", MAX_FUNDING_SATOSHIS_NO_WUMBO, MAX_FUNDING_SATOSHIS_NO_WUMBO).as_str(), |mut msg| { msg.funding_satoshis = MAX_FUNDING_SATOSHIS_NO_WUMBO; msg });
100100

101101
insane_open_helper("Bogus channel_reserve_satoshis", |mut msg| { msg.channel_reserve_satoshis = msg.funding_satoshis + 1; msg });
102102

0 commit comments

Comments
 (0)