Merge pull request #122 from TheBlueMatt/2018-08-doc-usability-fixes
[rust-lightning] / src / ln / channel.rs
index 93e164809b08f63b942d4c9cd45ed034cb6fbef3..c7c3208e78e3007293e7171074f52d555aa1ba60 100644 (file)
@@ -23,7 +23,7 @@ use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
 use chain::transaction::OutPoint;
 use util::{transaction_utils,rng};
 use util::sha2::Sha256;
-use util::logger::{Logger, Record};
+use util::logger::Logger;
 use util::errors::APIError;
 
 use std;
@@ -360,7 +360,8 @@ impl Channel {
 
        /// Guaranteed to return a value no larger than channel_value_satoshis
        fn get_our_channel_reserve_satoshis(channel_value_satoshis: u64) -> u64 {
-               cmp::min(channel_value_satoshis, 1000) //TODO
+               let (q, _) = channel_value_satoshis.overflowing_div(100);
+               cmp::min(channel_value_satoshis, cmp::max(q, 1000)) //TODO
        }
 
        fn derive_our_dust_limit_satoshis(at_open_background_feerate: u64) -> u64 {
@@ -391,8 +392,13 @@ impl Channel {
                        return Err(APIError::APIMisuseError{err: "push value > channel value"});
                }
 
-               let feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
+
                let background_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
+               if Channel::get_our_channel_reserve_satoshis(channel_value_satoshis) < Channel::derive_our_dust_limit_satoshis(background_feerate) {
+                       return Err(APIError::FeeRateTooHigh{err: format!("Not enough reserve above dust limit can be found at current fee rate({})", background_feerate), feerate: background_feerate});
+               }
+
+               let feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
 
                let secp_ctx = Secp256k1::new();
                let our_channel_monitor_claim_key_hash = Hash160::from_data(&PublicKey::from_secret_key(&secp_ctx, &chan_keys.channel_monitor_claim_key).unwrap().serialize());
@@ -512,9 +518,6 @@ impl Channel {
                if msg.max_accepted_htlcs > 483 {
                        return_error_message!("max_accpted_htlcs > 483");
                }
-               if (msg.channel_flags & 254) != 0 {
-                       return Err(HandleError{err: "Unknown channel flags", action: Some(msgs::ErrorAction::IgnoreError) });
-               }
 
                // Convert things into internal flags and prep our state:
 
@@ -1210,6 +1213,15 @@ impl Channel {
                        return_error_message!("max_accpted_htlcs > 483");
                }
 
+               // TODO: Optional additional constraints mentioned in the spec
+               // MAY fail the channel if
+               // funding_satoshi is too small
+               // htlc_minimum_msat too large
+               // max_htlc_value_in_flight_msat too small
+               // channel_reserve_satoshis too large
+               // max_accepted_htlcs too small
+               // dust_limit_satoshis too small
+
                self.channel_monitor.set_their_htlc_base_key(&msg.htlc_basepoint);
 
                self.their_dust_limit_satoshis = msg.dust_limit_satoshis;