Merge pull request #998 from TheBlueMatt/2021-07-fix-chan-reserve-msat-sat
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Mon, 26 Jul 2021 16:03:22 +0000 (16:03 +0000)
committerGitHub <noreply@github.com>
Mon, 26 Jul 2021 16:03:22 +0000 (16:03 +0000)
Fix channel reserve calculation on the sending side

20 files changed:
.github/workflows/build.yml
lightning/Cargo.toml
lightning/src/chain/chainmonitor.rs
lightning/src/chain/channelmonitor.rs
lightning/src/lib.rs
lightning/src/ln/chanmon_update_fail_tests.rs
lightning/src/ln/channel.rs
lightning/src/ln/channelmanager.rs
lightning/src/ln/features.rs
lightning/src/ln/functional_test_utils.rs
lightning/src/ln/functional_tests.rs
lightning/src/ln/peer_handler.rs
lightning/src/routing/network_graph.rs
lightning/src/routing/router.rs
lightning/src/sync.rs [new file with mode: 0644]
lightning/src/util/enforcing_trait_impls.rs
lightning/src/util/logger.rs
lightning/src/util/message_signing.rs
lightning/src/util/ser.rs
lightning/src/util/test_utils.rs

index 85aaac6a3a0d378e5cef84a1f7ef307d121a8658..ebca7680f4414bc0cdf7565ff8c8bdffa8d59e7a 100644 (file)
@@ -90,13 +90,15 @@ jobs:
         if: "matrix.build-no-std && !matrix.coverage"
         run: |
           cd lightning
-          cargo test --verbose --color always --features hashbrown
+          cargo test --verbose --color always --no-default-features --features no_std
+          # check if there is a conflict between no_std and the default std feature
+          cargo test --verbose --color always --features no_std
           cd ..
       - name: Test on no_std bullds Rust ${{ matrix.toolchain }} and full code-linking for coverage generation
         if: "matrix.build-no-std && matrix.coverage"
         run: |
           cd lightning
-          RUSTFLAGS="-C link-dead-code" cargo test --verbose --color always --features hashbrown
+          RUSTFLAGS="-C link-dead-code" cargo test --verbose --color always --no-default-features --features no_std
           cd ..
       - name: Test on Rust ${{ matrix.toolchain }}
         if: "! matrix.build-net-tokio"
index 7172ee07cb8e99204b83005310b6540ef649e442..32c0d0af63403aaf34676465790c6697cd8f9fd5 100644 (file)
@@ -25,7 +25,11 @@ max_level_debug = []
 # This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
 unsafe_revoked_tx_signing = []
 unstable = []
+
 no_std = ["hashbrown"]
+std = []
+
+default = ["std"]
 
 [dependencies]
 bitcoin = "0.26"
index 23fc42f54a4fc16c43d149f4e117ff42ec46d623..12bfebbc4aeee8941b6ad4734cd1f539f8816360 100644 (file)
@@ -38,7 +38,7 @@ use util::events;
 use util::events::EventHandler;
 
 use prelude::*;
-use std::sync::RwLock;
+use sync::RwLock;
 use core::ops::Deref;
 
 /// An implementation of [`chain::Watch`] for monitoring channels.
index e78faf7764025b92c2ae773cbd8d5e32e4e62395..cf368f15259cad2e9f03e3cc3728eb61b80a701b 100644 (file)
@@ -55,7 +55,7 @@ use prelude::*;
 use core::{cmp, mem};
 use std::io::Error;
 use core::ops::Deref;
-use std::sync::Mutex;
+use sync::Mutex;
 
 /// An update generated by the underlying Channel itself which contains some new information the
 /// ChannelMonitor should be made aware of.
@@ -2843,7 +2843,7 @@ mod tests {
        use util::test_utils::{TestLogger, TestBroadcaster, TestFeeEstimator};
        use bitcoin::secp256k1::key::{SecretKey,PublicKey};
        use bitcoin::secp256k1::Secp256k1;
-       use std::sync::{Arc, Mutex};
+       use sync::{Arc, Mutex};
        use chain::keysinterface::InMemorySigner;
        use prelude::*;
 
index c84e3d2d8da24aaeb826cc2e258410d4c7c79c67..14445710655b277f32eaeb1033a475b3b3c53c9e 100644 (file)
@@ -53,3 +53,11 @@ mod prelude {
        #[cfg(feature = "hashbrown")]
        pub use self::hashbrown::{HashMap, HashSet, hash_map};
 }
+
+#[cfg(feature = "std")]
+mod sync {
+       pub use ::std::sync::{Arc, Mutex, Condvar, MutexGuard, RwLock, RwLockReadGuard};
+}
+
+#[cfg(not(feature = "std"))]
+mod sync;
index 5f90b030dfb12ac78a0a089b9acf0a40e43f6166..1e76117fdb2e661d8fd8335c0f7251b2a1d1182d 100644 (file)
@@ -41,7 +41,7 @@ use ln::functional_test_utils::*;
 use util::test_utils;
 
 use prelude::*;
-use std::sync::{Arc, Mutex};
+use sync::{Arc, Mutex};
 
 // If persister_fail is true, we have the persister return a PermanentFailure
 // instead of the higher-level ChainMonitor.
index 45f7aa73b9b6a4c2d1167d79d42a681dbd2591e3..dc2dd758a9ea97bae465a69836f79200190c97d7 100644 (file)
@@ -44,8 +44,8 @@ use util::scid_utils::scid_from_parts;
 use prelude::*;
 use core::{cmp,mem,fmt};
 use core::ops::Deref;
-#[cfg(any(test, feature = "fuzztarget"))]
-use std::sync::Mutex;
+#[cfg(any(test, feature = "fuzztarget", debug_assertions))]
+use sync::Mutex;
 use bitcoin::hashes::hex::ToHex;
 use bitcoin::blockdata::opcodes::all::OP_PUSHBYTES_0;
 
@@ -374,10 +374,10 @@ pub(super) struct Channel<Signer: Sign> {
 
        #[cfg(debug_assertions)]
        /// Max to_local and to_remote outputs in a locally-generated commitment transaction
-       holder_max_commitment_tx_output: ::std::sync::Mutex<(u64, u64)>,
+       holder_max_commitment_tx_output: Mutex<(u64, u64)>,
        #[cfg(debug_assertions)]
        /// Max to_local and to_remote outputs in a remote-generated commitment transaction
-       counterparty_max_commitment_tx_output: ::std::sync::Mutex<(u64, u64)>,
+       counterparty_max_commitment_tx_output: Mutex<(u64, u64)>,
 
        last_sent_closing_fee: Option<(u32, u64, Signature)>, // (feerate, fee, holder_sig)
 
@@ -595,9 +595,9 @@ impl<Signer: Sign> Channel<Signer> {
                        monitor_pending_failures: Vec::new(),
 
                        #[cfg(debug_assertions)]
-                       holder_max_commitment_tx_output: ::std::sync::Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
+                       holder_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
                        #[cfg(debug_assertions)]
-                       counterparty_max_commitment_tx_output: ::std::sync::Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
+                       counterparty_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
 
                        last_sent_closing_fee: None,
 
@@ -836,9 +836,9 @@ impl<Signer: Sign> Channel<Signer> {
                        monitor_pending_failures: Vec::new(),
 
                        #[cfg(debug_assertions)]
-                       holder_max_commitment_tx_output: ::std::sync::Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
+                       holder_max_commitment_tx_output: Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
                        #[cfg(debug_assertions)]
-                       counterparty_max_commitment_tx_output: ::std::sync::Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
+                       counterparty_max_commitment_tx_output: Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
 
                        last_sent_closing_fee: None,
 
@@ -4943,9 +4943,9 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
                        feerate_per_kw,
 
                        #[cfg(debug_assertions)]
-                       holder_max_commitment_tx_output: ::std::sync::Mutex::new((0, 0)),
+                       holder_max_commitment_tx_output: Mutex::new((0, 0)),
                        #[cfg(debug_assertions)]
-                       counterparty_max_commitment_tx_output: ::std::sync::Mutex::new((0, 0)),
+                       counterparty_max_commitment_tx_output: Mutex::new((0, 0)),
 
                        last_sent_closing_fee,
 
@@ -5023,7 +5023,7 @@ mod tests {
        use bitcoin::hashes::sha256::Hash as Sha256;
        use bitcoin::hashes::Hash;
        use bitcoin::hash_types::{Txid, WPubkeyHash};
-       use std::sync::Arc;
+       use sync::Arc;
        use prelude::*;
 
        struct TestFeeEstimator {
index 49af440851bb5e2f8f71b6d9dae30c5a33481e87..22a7982b1de4a26a8136557a01f35685be7eba9e 100644 (file)
@@ -64,7 +64,7 @@ use prelude::*;
 use core::{cmp, mem};
 use core::cell::RefCell;
 use std::io::{Cursor, Read};
-use std::sync::{Arc, Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard};
+use sync::{Arc, Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard};
 use core::sync::atomic::{AtomicUsize, Ordering};
 use core::time::Duration;
 #[cfg(any(test, feature = "allow_wallclock_use"))]
@@ -4951,7 +4951,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
 #[cfg(test)]
 mod tests {
        use ln::channelmanager::PersistenceNotifier;
-       use std::sync::Arc;
+       use sync::Arc;
        use core::sync::atomic::{AtomicBool, Ordering};
        use std::thread;
        use core::time::Duration;
@@ -4959,6 +4959,7 @@ mod tests {
        use ln::features::InitFeatures;
        use ln::msgs::ChannelMessageHandler;
 
+       #[cfg(feature = "std")]
        #[test]
        fn test_wait_timeout() {
                let persistence_notifier = Arc::new(PersistenceNotifier::new());
@@ -5094,7 +5095,7 @@ pub mod bench {
        use bitcoin::hashes::sha256::Hash as Sha256;
        use bitcoin::{Block, BlockHeader, Transaction, TxOut};
 
-       use std::sync::{Arc, Mutex};
+       use sync::{Arc, Mutex};
 
        use test::Bencher;
 
index b459baf06580bff8414715b82c1845e7334c23ee..5d908ee01a4957bd7c85d305043af8e5204523a2 100644 (file)
@@ -301,27 +301,7 @@ mod sealed {
                set_shutdown_any_segwit_required);
 
        #[cfg(test)]
-       define_context!(TestingContext {
-               required_features: [
-                       // Byte 0
-                       ,
-                       // Byte 1
-                       ,
-                       // Byte 2
-                       UnknownFeature,
-               ],
-               optional_features: [
-                       // Byte 0
-                       ,
-                       // Byte 1
-                       ,
-                       // Byte 2
-                       ,
-               ],
-       });
-
-       #[cfg(test)]
-       define_feature!(23, UnknownFeature, [TestingContext],
+       define_feature!(123456789, UnknownFeature, [NodeContext, ChannelContext, InvoiceContext],
                "Feature flags for an unknown feature used in testing.", set_unknown_feature_optional,
                set_unknown_feature_required);
 }
@@ -490,13 +470,14 @@ impl<T: sealed::Context> Features<T> {
        /// Converts `Features<T>` to `Features<C>`. Only known `T` features relevant to context `C` are
        /// included in the result.
        fn to_context_internal<C: sealed::Context>(&self) -> Features<C> {
-               let byte_count = C::KNOWN_FEATURE_MASK.len();
+               let from_byte_count = T::KNOWN_FEATURE_MASK.len();
+               let to_byte_count = C::KNOWN_FEATURE_MASK.len();
                let mut flags = Vec::new();
                for (i, byte) in self.flags.iter().enumerate() {
-                       if i < byte_count {
-                               let known_source_features = T::KNOWN_FEATURE_MASK[i];
-                               let known_target_features = C::KNOWN_FEATURE_MASK[i];
-                               flags.push(byte & known_source_features & known_target_features);
+                       if i < from_byte_count && i < to_byte_count {
+                               let from_known_features = T::KNOWN_FEATURE_MASK[i];
+                               let to_known_features = C::KNOWN_FEATURE_MASK[i];
+                               flags.push(byte & from_known_features & to_known_features);
                        }
                }
                Features::<C> { flags, mark: PhantomData, }
@@ -552,21 +533,6 @@ impl<T: sealed::Context> Features<T> {
        pub(crate) fn byte_count(&self) -> usize {
                self.flags.len()
        }
-
-       #[cfg(test)]
-       pub(crate) fn set_required_unknown_bits(&mut self) {
-               <sealed::TestingContext as sealed::UnknownFeature>::set_required_bit(&mut self.flags);
-       }
-
-       #[cfg(test)]
-       pub(crate) fn set_optional_unknown_bits(&mut self) {
-               <sealed::TestingContext as sealed::UnknownFeature>::set_optional_bit(&mut self.flags);
-       }
-
-       #[cfg(test)]
-       pub(crate) fn clear_unknown_bits(&mut self) {
-               <sealed::TestingContext as sealed::UnknownFeature>::clear_bits(&mut self.flags);
-       }
 }
 
 impl<T: sealed::DataLossProtect> Features<T> {
@@ -764,19 +730,15 @@ mod tests {
 
        #[test]
        fn sanity_test_unknown_bits() {
-               let mut features = ChannelFeatures::empty();
+               let features = ChannelFeatures::empty();
                assert!(!features.requires_unknown_bits());
                assert!(!features.supports_unknown_bits());
 
-               features.set_required_unknown_bits();
+               let features = ChannelFeatures::empty().set_unknown_feature_required();
                assert!(features.requires_unknown_bits());
                assert!(features.supports_unknown_bits());
 
-               features.clear_unknown_bits();
-               assert!(!features.requires_unknown_bits());
-               assert!(!features.supports_unknown_bits());
-
-               features.set_optional_unknown_bits();
+               let features = ChannelFeatures::empty().set_unknown_feature_optional();
                assert!(!features.requires_unknown_bits());
                assert!(features.supports_unknown_bits());
        }
@@ -812,6 +774,16 @@ mod tests {
                assert!(!init_features.supports_gossip_queries());
        }
 
+       #[test]
+       fn convert_to_context_with_unknown_flags() {
+               // Ensure the `from` context has fewer known feature bytes than the `to` context.
+               assert!(InvoiceFeatures::known().byte_count() < NodeFeatures::known().byte_count());
+               let invoice_features = InvoiceFeatures::known().set_unknown_feature_optional();
+               assert!(invoice_features.supports_unknown_bits());
+               let node_features: NodeFeatures = invoice_features.to_context();
+               assert!(!node_features.supports_unknown_bits());
+       }
+
        #[test]
        fn set_feature_bits() {
                let features = InvoiceFeatures::empty()
index 8bb49ef97d70cdb59a2c21a76bcb7aaf89313cc4..0adea15c16722eceffaf167a3cecb97ab0eab323 100644 (file)
@@ -42,7 +42,7 @@ use bitcoin::secp256k1::key::PublicKey;
 use prelude::*;
 use core::cell::RefCell;
 use std::rc::Rc;
-use std::sync::{Arc, Mutex};
+use sync::{Arc, Mutex};
 use core::mem;
 
 pub const CHAN_CONFIRM_DEPTH: u32 = 10;
index d0b2bda047b9cb3b835052423b95f41a6fb03d06..a149ed23f8a2af0c016c2f9dae42ad858137dc1c 100644 (file)
@@ -54,7 +54,7 @@ use regex;
 use prelude::*;
 use alloc::collections::BTreeSet;
 use core::default::Default;
-use std::sync::{Arc, Mutex};
+use sync::{Arc, Mutex};
 
 use ln::functional_test_utils::*;
 use ln::chan_utils::CommitmentTransaction;
index 96ec31c98ee1b4aa289f2ba78ee32c76a4e5f22d..0ec24fbf84c3ad31bfdf32bff421ffa310b9873d 100644 (file)
@@ -33,7 +33,7 @@ use routing::network_graph::NetGraphMsgHandler;
 use prelude::*;
 use alloc::collections::LinkedList;
 use alloc::fmt::Debug;
-use std::sync::{Arc, Mutex};
+use sync::{Arc, Mutex};
 use core::sync::atomic::{AtomicUsize, Ordering};
 use core::{cmp, hash, fmt, mem};
 use core::ops::Deref;
@@ -1447,7 +1447,7 @@ mod tests {
        use bitcoin::secp256k1::key::{SecretKey, PublicKey};
 
        use prelude::*;
-       use std::sync::{Arc, Mutex};
+       use sync::{Arc, Mutex};
        use core::sync::atomic::Ordering;
 
        #[derive(Clone)]
index 22cd2a92f6f6fba841de9f1cc01d86bf401552fb..9e4813e9155926fac85b20d307af1bb9ac8a1dc8 100644 (file)
@@ -35,9 +35,9 @@ use util::scid_utils::{block_from_scid, scid_from_parts, MAX_SCID_BLOCK};
 use prelude::*;
 use alloc::collections::{BTreeMap, btree_map::Entry as BtreeEntry};
 use core::{cmp, fmt};
-use std::sync::{RwLock, RwLockReadGuard};
+use sync::{RwLock, RwLockReadGuard};
 use core::sync::atomic::{AtomicUsize, Ordering};
-use std::sync::Mutex;
+use sync::Mutex;
 use core::ops::Deref;
 use bitcoin::hashes::hex::ToHex;
 
@@ -1088,7 +1088,7 @@ mod tests {
        use bitcoin::secp256k1::{All, Secp256k1};
 
        use prelude::*;
-       use std::sync::Arc;
+       use sync::Arc;
 
        fn create_net_graph_msg_handler() -> (Secp256k1<All>, NetGraphMsgHandler<Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>) {
                let secp_ctx = Secp256k1::new();
index 6c14ebebd89b70d55b1e4f59af827a9f0a6b1cde..dd5cadecc8322e88b56d369004cbde5a85e47e66 100644 (file)
@@ -882,7 +882,11 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
                                        htlc_maximum_msat: hop.htlc_maximum_msat,
                                        fees: hop.fees,
                                };
-                               if add_entry!(hop.short_channel_id, hop.src_node_id, payee, directional_info, None::<u64>, &empty_channel_features, 0, path_value_msat, 0) {
+                               // We assume that the recipient only included route hints for routes which had
+                               // sufficient value to route `final_value_msat`. Note that in the case of "0-value"
+                               // invoices where the invoice does not specify value this may not be the case, but
+                               // better to include the hints than not.
+                               if add_entry!(hop.short_channel_id, hop.src_node_id, payee, directional_info, Some((final_value_msat + 999) / 1000), &empty_channel_features, 0, path_value_msat, 0) {
                                        // If this hop connects to a node with which we have a direct channel,
                                        // ignore the network graph and, if the last hop was added, add our
                                        // direct channel to the candidate set.
@@ -1198,7 +1202,7 @@ mod tests {
        use bitcoin::secp256k1::{Secp256k1, All};
 
        use prelude::*;
-       use std::sync::Arc;
+       use sync::{self, Arc};
 
        fn get_channel_details(short_channel_id: Option<u64>, node_id: PublicKey,
                        features: InitFeatures, outbound_capacity_msat: u64) -> channelmanager::ChannelDetails {
@@ -1321,7 +1325,7 @@ mod tests {
                }
        }
 
-       fn build_graph() -> (Secp256k1<All>, NetGraphMsgHandler<std::sync::Arc<test_utils::TestChainSource>, std::sync::Arc<crate::util::test_utils::TestLogger>>, std::sync::Arc<test_utils::TestChainSource>, std::sync::Arc<test_utils::TestLogger>) {
+       fn build_graph() -> (Secp256k1<All>, NetGraphMsgHandler<sync::Arc<test_utils::TestChainSource>, sync::Arc<crate::util::test_utils::TestLogger>>, sync::Arc<test_utils::TestChainSource>, sync::Arc<test_utils::TestLogger>) {
                let secp_ctx = Secp256k1::new();
                let logger = Arc::new(test_utils::TestLogger::new());
                let chain_monitor = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
@@ -1992,8 +1996,7 @@ mod tests {
                let (_, our_id, privkeys, nodes) = get_nodes(&secp_ctx);
 
                // Disable nodes 1, 2, and 8 by requiring unknown feature bits
-               let mut unknown_features = NodeFeatures::known();
-               unknown_features.set_required_unknown_bits();
+               let unknown_features = NodeFeatures::known().set_unknown_feature_required();
                add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[0], unknown_features.clone(), 1);
                add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[1], unknown_features.clone(), 1);
                add_or_update_node(&net_graph_msg_handler, &secp_ctx, &privkeys[7], unknown_features.clone(), 1);
diff --git a/lightning/src/sync.rs b/lightning/src/sync.rs
new file mode 100644 (file)
index 0000000..bde5470
--- /dev/null
@@ -0,0 +1,115 @@
+pub use ::alloc::sync::Arc;
+use core::ops::{Deref, DerefMut};
+use core::time::Duration;
+use core::cell::{RefCell, Ref, RefMut};
+
+pub type LockResult<Guard> = Result<Guard, ()>;
+
+pub struct Condvar {}
+
+impl Condvar {
+       pub fn new() -> Condvar {
+               Condvar { }
+       }
+
+       pub fn wait<'a, T>(&'a self, guard: MutexGuard<'a, T>) -> LockResult<MutexGuard<'a, T>> {
+               Ok(guard)
+       }
+
+       #[allow(unused)]
+       pub fn wait_timeout<'a, T>(&'a self, guard: MutexGuard<'a, T>, _dur: Duration) -> LockResult<(MutexGuard<'a, T>, ())> {
+               Ok((guard, ()))
+       }
+
+       pub fn notify_all(&self) {}
+}
+
+pub struct Mutex<T: ?Sized> {
+       inner: RefCell<T>
+}
+
+#[must_use = "if unused the Mutex will immediately unlock"]
+pub struct MutexGuard<'a, T: ?Sized + 'a> {
+       lock: RefMut<'a, T>,
+}
+
+impl<T: ?Sized> Deref for MutexGuard<'_, T> {
+       type Target = T;
+
+       fn deref(&self) -> &T {
+               &self.lock.deref()
+       }
+}
+
+impl<T: ?Sized> DerefMut for MutexGuard<'_, T> {
+       fn deref_mut(&mut self) -> &mut T {
+               self.lock.deref_mut()
+       }
+}
+
+impl<T> Mutex<T> {
+       pub fn new(inner: T) -> Mutex<T> {
+               Mutex { inner: RefCell::new(inner) }
+       }
+
+       pub fn lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
+               Ok(MutexGuard { lock: self.inner.borrow_mut() })
+       }
+
+       pub fn try_lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
+               Ok(MutexGuard { lock: self.inner.borrow_mut() })
+       }
+}
+
+pub struct RwLock<T: ?Sized> {
+       inner: RefCell<T>
+}
+
+pub struct RwLockReadGuard<'a, T: ?Sized + 'a> {
+       lock: Ref<'a, T>,
+}
+
+pub struct RwLockWriteGuard<'a, T: ?Sized + 'a> {
+       lock: RefMut<'a, T>,
+}
+
+impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
+       type Target = T;
+
+       fn deref(&self) -> &T {
+               &self.lock.deref()
+       }
+}
+
+impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T> {
+       type Target = T;
+
+       fn deref(&self) -> &T {
+               &self.lock.deref()
+       }
+}
+
+impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
+       fn deref_mut(&mut self) -> &mut T {
+               self.lock.deref_mut()
+       }
+}
+
+impl<T> RwLock<T> {
+       pub fn new(inner: T) -> RwLock<T> {
+               RwLock { inner: RefCell::new(inner) }
+       }
+
+       pub fn read<'a>(&'a self) -> LockResult<RwLockReadGuard<'a, T>> {
+               Ok(RwLockReadGuard { lock: self.inner.borrow() })
+       }
+
+       pub fn write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
+               Ok(RwLockWriteGuard { lock: self.inner.borrow_mut() })
+       }
+
+       pub fn try_write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
+               // There is no try, grasshopper - only used for tests and expected to fail
+               Err(())
+       }
+}
index 26f9d87bba9bc40c03d6f5439c4828d500be0be3..d7ddc38b3ddfee2641ba414108ca5b541878f503 100644 (file)
@@ -13,7 +13,7 @@ use chain::keysinterface::{Sign, InMemorySigner, BaseSign};
 
 use prelude::*;
 use core::cmp;
-use std::sync::{Mutex, Arc};
+use sync::{Mutex, Arc};
 
 use bitcoin::blockdata::transaction::{Transaction, SigHashType};
 use bitcoin::util::bip143;
index 98037aac298280a5b418b9c84bb94b948ae3bc30..b34d1f0b782d8618756edc28e3d4a4791be0779f 100644 (file)
@@ -124,7 +124,7 @@ pub trait Logger {
 mod tests {
        use util::logger::{Logger, Level};
        use util::test_utils::TestLogger;
-       use std::sync::Arc;
+       use sync::Arc;
 
        #[test]
        fn test_level_show() {
index 2055b4087749ada5b3baef731770608baceb629e..8beff835a4bffe1f0b3d59e672380323b6164f98 100644 (file)
@@ -36,6 +36,11 @@ fn sigrec_encode(sig_rec: RecoverableSignature) -> Vec<u8> {
 }
 
 fn sigrec_decode(sig_rec: Vec<u8>) -> Result<RecoverableSignature, Error> {
+    // Signature must be 64 + 1 bytes long (compact signature + recovery id)
+    if sig_rec.len() != 65 {
+        return Err(Error::InvalidSignature);
+    }
+
     let rsig = &sig_rec[1..];
     let rid = sig_rec[0] as i32 - 31;
 
index b02fef275d2c08d644f1f706c8440f3d688d682c..fe331f6bf4d748cb7f74a22607b87f0622a03589 100644 (file)
@@ -13,7 +13,7 @@
 use prelude::*;
 use std::io::{Read, Write};
 use core::hash::Hash;
-use std::sync::Mutex;
+use sync::Mutex;
 use core::cmp;
 
 use bitcoin::secp256k1::Signature;
index 2775697b1dbb1d672e396341848b08b6b5c059bf..fbd5a26d9f327de34bf60ce0ed8d3cc35ff56406 100644 (file)
@@ -39,7 +39,7 @@ use regex;
 
 use prelude::*;
 use core::time::Duration;
-use std::sync::{Mutex, Arc};
+use sync::{Mutex, Arc};
 use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
 use core::{cmp, mem};
 use chain::keysinterface::InMemorySigner;