]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add `MAX_FUNDING_SATOSHIS` constant.
authorJean Pierre Dudey <jeandudey@hotmail.com>
Sat, 30 Jun 2018 14:32:23 +0000 (10:32 -0400)
committerJean Pierre Dudey <jeandudey@hotmail.com>
Sat, 30 Jun 2018 15:13:18 +0000 (11:13 -0400)
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 <jeandudey@hotmail.com>
src/ln/channel.rs
src/ln/channelmanager.rs

index 059fc79114490f6a829d23f05bafbba863afe4bf..93e4a2cf3be5d12f0d903725b0ddcd32e6a2f1a7 100644 (file)
@@ -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<Channel, HandleError> {
                // 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:
index 98371fbd6a0e6e89f8bf46a663bccefef691efdb..4dfa20a34ef6499b07608627c9d6ec1dd7dcf024 100644 (file)
@@ -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<FeeEstimator>, monitor: Arc<ManyChannelMonitor>, chain_monitor: Arc<ChainWatchInterface>, tx_broadcaster: Arc<BroadcasterInterface>) -> Result<Arc<ChannelManager>, secp256k1::Error> {
                let secp_ctx = Secp256k1::new();