Skip to content

Commit 6707db6

Browse files
committed
Add MAX_FUNDING_SATOSHIS constant.
This constants defines the maximum value of `open_channel.funding_satoshis`, currently it's defined to be 2^24 according to the BOLT lightningdevkit#2 specification. Also a test was added to check that the constant is never over 2,1x10^15 (maximum satoshis in bitcoin) if modified. Signed-off-by: Jean Pierre Dudey <[email protected]>
1 parent 5fa80d0 commit 6707db6

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/ln/channel.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ const COMMITMENT_TX_BASE_WEIGHT: u64 = 724;
316316
const COMMITMENT_TX_WEIGHT_PER_HTLC: u64 = 172;
317317
const SPENDING_INPUT_FOR_A_OUTPUT_WEIGHT: u64 = 79; // prevout: 36, nSequence: 4, script len: 1, witness lengths: (3+1)/4, sig: 73/4, if-selector: 1, redeemScript: (6 ops + 2*33 pubkeys + 1*2 delay)/4
318318
const B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT: u64 = 104; // prevout: 40, nSequence: 4, script len: 1, witness lengths: 3/4, sig: 73/4, pubkey: 33/4, output: 31 (TODO: Wrong? Useless?)
319+
/// Maximmum `funding_satoshis` value, according to the BOLT #2 specification
320+
/// it's 2^24.
321+
pub const MAX_FUNDING_SATOSHIS: u64 = (1 << 24);
319322

320323
macro_rules! secp_call {
321324
( $res: expr, $err: expr ) => {
@@ -353,9 +356,9 @@ impl Channel {
353356

354357
// Constructors:
355358

356-
/// panics if channel_value_satoshis is >= (1 << 24)
359+
/// panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS`
357360
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, announce_publicly: bool, user_id: u64) -> Channel {
358-
if channel_value_satoshis >= (1 << 24) {
361+
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
359362
panic!("funding value > 2^24");
360363
}
361364

@@ -441,12 +444,9 @@ impl Channel {
441444
/// that we're rejecting the new channel.
442445
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, announce_publicly: bool) -> Result<Channel, HandleError> {
443446
// Check sanity of message fields:
444-
if msg.funding_satoshis >= (1 << 24) {
447+
if msg.funding_satoshis >= MAX_FUNDING_SATOSHIS {
445448
return Err(HandleError{err: "funding value > 2^24", msg: Some(msgs::ErrorAction::DisconnectPeer{})});
446449
}
447-
if msg.funding_satoshis > 21000000 * 100000000 {
448-
return Err(HandleError{err: "More funding_satoshis than there are satoshis!", msg: Some(msgs::ErrorAction::DisconnectPeer{})});
449-
}
450450
if msg.channel_reserve_satoshis > msg.funding_satoshis {
451451
return Err(HandleError{err: "Bogus channel_reserve_satoshis", msg: Some(msgs::ErrorAction::DisconnectPeer{})});
452452
}
@@ -2328,6 +2328,7 @@ mod tests {
23282328
use bitcoin::network::serialize::serialize;
23292329
use bitcoin::blockdata::transaction::Transaction;
23302330
use ln::channel::{Channel,ChannelKeys,HTLCOutput,HTLCState,HTLCOutputInCommitment,TxCreationKeys};
2331+
use ln::channel::MAX_FUNDING_SATOSHIS;
23312332
use ln::chan_utils;
23322333
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
23332334
use chain::transaction::OutPoint;
@@ -2345,6 +2346,12 @@ mod tests {
23452346
}
23462347
}
23472348

2349+
#[test]
2350+
fn test_max_funding_satoshis() {
2351+
assert!(MAX_FUNDING_SATOSHIS <= 21_000_000 * 100_000_000,
2352+
"MAX_FUNDING_SATOSHIS is greater than all satoshis on existence");
2353+
}
2354+
23482355
#[test]
23492356
fn outbound_commitment_test() {
23502357
// Test vectors from BOLT 3 Appendix C:

src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl ChannelManager {
202202
/// the main "logic hub" for all channel-related actions, and implements ChannelMessageHandler.
203203
/// fee_proportional_millionths is an optional fee to charge any payments routed through us.
204204
/// Non-proportional fees are fixed according to our risk using the provided fee estimator.
205-
/// panics if channel_value_satoshis is >= (1 << 24)!
205+
/// panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS`!
206206
pub fn new(our_network_key: SecretKey, fee_proportional_millionths: u32, announce_channels_publicly: bool, network: Network, feeest: Arc<FeeEstimator>, monitor: Arc<ManyChannelMonitor>, chain_monitor: Arc<ChainWatchInterface>, tx_broadcaster: Arc<BroadcasterInterface>) -> Result<Arc<ChannelManager>, secp256k1::Error> {
207207
let secp_ctx = Secp256k1::new();
208208

0 commit comments

Comments
 (0)