From: Jean Pierre Dudey Date: Sat, 30 Jun 2018 14:32:23 +0000 (-0400) Subject: Add `MAX_FUNDING_SATOSHIS` constant. X-Git-Tag: v0.0.12~396^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=6707db6eae050d86bc3d1db9e85a737cd7efc3c6;hp=5fa80d022ab5249bb7f20f62aa227676247ca821;p=rust-lightning 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 #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 --- diff --git a/src/ln/channel.rs b/src/ln/channel.rs index 059fc7911..93e4a2cf3 100644 --- a/src/ln/channel.rs +++ b/src/ln/channel.rs @@ -316,6 +316,9 @@ const COMMITMENT_TX_BASE_WEIGHT: u64 = 724; const COMMITMENT_TX_WEIGHT_PER_HTLC: u64 = 172; 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 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?) +/// Maximmum `funding_satoshis` value, according to the BOLT #2 specification +/// it's 2^24. +pub const MAX_FUNDING_SATOSHIS: u64 = (1 << 24); macro_rules! secp_call { ( $res: expr, $err: expr ) => { @@ -353,9 +356,9 @@ impl Channel { // Constructors: - /// panics if channel_value_satoshis is >= (1 << 24) + /// panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS` 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 { - if channel_value_satoshis >= (1 << 24) { + if channel_value_satoshis >= MAX_FUNDING_SATOSHIS { panic!("funding value > 2^24"); } @@ -441,12 +444,9 @@ impl Channel { /// that we're rejecting the new channel. 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 { // Check sanity of message fields: - if msg.funding_satoshis >= (1 << 24) { + if msg.funding_satoshis >= MAX_FUNDING_SATOSHIS { return Err(HandleError{err: "funding value > 2^24", msg: Some(msgs::ErrorAction::DisconnectPeer{})}); } - if msg.funding_satoshis > 21000000 * 100000000 { - return Err(HandleError{err: "More funding_satoshis than there are satoshis!", msg: Some(msgs::ErrorAction::DisconnectPeer{})}); - } if msg.channel_reserve_satoshis > msg.funding_satoshis { return Err(HandleError{err: "Bogus channel_reserve_satoshis", msg: Some(msgs::ErrorAction::DisconnectPeer{})}); } @@ -2328,6 +2328,7 @@ mod tests { use bitcoin::network::serialize::serialize; use bitcoin::blockdata::transaction::Transaction; use ln::channel::{Channel,ChannelKeys,HTLCOutput,HTLCState,HTLCOutputInCommitment,TxCreationKeys}; + use ln::channel::MAX_FUNDING_SATOSHIS; use ln::chan_utils; use chain::chaininterface::{FeeEstimator,ConfirmationTarget}; use chain::transaction::OutPoint; @@ -2345,6 +2346,12 @@ mod tests { } } + #[test] + fn test_max_funding_satoshis() { + assert!(MAX_FUNDING_SATOSHIS <= 21_000_000 * 100_000_000, + "MAX_FUNDING_SATOSHIS is greater than all satoshis on existence"); + } + #[test] fn outbound_commitment_test() { // Test vectors from BOLT 3 Appendix C: diff --git a/src/ln/channelmanager.rs b/src/ln/channelmanager.rs index 98371fbd6..4dfa20a34 100644 --- a/src/ln/channelmanager.rs +++ b/src/ln/channelmanager.rs @@ -202,7 +202,7 @@ impl ChannelManager { /// the main "logic hub" for all channel-related actions, and implements ChannelMessageHandler. /// fee_proportional_millionths is an optional fee to charge any payments routed through us. /// Non-proportional fees are fixed according to our risk using the provided fee estimator. - /// panics if channel_value_satoshis is >= (1 << 24)! + /// panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS`! pub fn new(our_network_key: SecretKey, fee_proportional_millionths: u32, announce_channels_publicly: bool, network: Network, feeest: Arc, monitor: Arc, chain_monitor: Arc, tx_broadcaster: Arc) -> Result, secp256k1::Error> { let secp_ctx = Secp256k1::new();