Merge commit 'refs/pull/229/head' of github.com:bitcoindevkit/bdk

This commit is contained in:
Alekos Filini 2020-12-16 10:48:10 +01:00
commit 931a110e4e
No known key found for this signature in database
GPG Key ID: 5E8AFC3034FDFA4F

View File

@ -91,8 +91,9 @@
//! ``` //! ```
use std::cmp::Ordering; use std::cmp::Ordering;
use std::collections::HashMap; use std::collections::BTreeMap;
use std::fmt; use std::fmt;
use std::ops::Bound::Included;
use std::sync::Arc; use std::sync::Arc;
use bitcoin::blockdata::opcodes; use bitcoin::blockdata::opcodes;
@ -111,7 +112,7 @@ use crate::descriptor::XKeyUtils;
/// Identifier of a signer in the `SignersContainers`. Used as a key to find the right signer among /// Identifier of a signer in the `SignersContainers`. Used as a key to find the right signer among
/// multiple of them /// multiple of them
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Ord, PartialOrd, PartialEq, Eq, Hash)]
pub enum SignerId { pub enum SignerId {
PkHash(hash160::Hash), PkHash(hash160::Hash),
Fingerprint(Fingerprint), Fingerprint(Fingerprint),
@ -295,7 +296,7 @@ impl Signer for PrivateKey {
/// The default value is `100`. Signers with an ordering above that will be called later, /// The default value is `100`. Signers with an ordering above that will be called later,
/// and they will thus see the partial signatures added to the transaction once they get to sign /// and they will thus see the partial signatures added to the transaction once they get to sign
/// themselves. /// themselves.
#[derive(Debug, Clone, Hash, PartialOrd, PartialEq, Ord, Eq)] #[derive(Debug, Clone, PartialOrd, PartialEq, Ord, Eq)]
pub struct SignerOrdering(pub usize); pub struct SignerOrdering(pub usize);
impl std::default::Default for SignerOrdering { impl std::default::Default for SignerOrdering {
@ -304,7 +305,7 @@ impl std::default::Default for SignerOrdering {
} }
} }
#[derive(Debug, Clone, Hash, Eq, PartialEq)] #[derive(Debug, Clone)]
struct SignersContainerKey { struct SignersContainerKey {
id: SignerId, id: SignerId,
ordering: SignerOrdering, ordering: SignerOrdering,
@ -321,7 +322,7 @@ impl From<(SignerId, SignerOrdering)> for SignersContainerKey {
/// Container for multiple signers /// Container for multiple signers
#[derive(Debug, Default, Clone)] #[derive(Debug, Default, Clone)]
pub struct SignersContainer(HashMap<SignersContainerKey, Arc<dyn Signer>>); pub struct SignersContainer(BTreeMap<SignersContainerKey, Arc<dyn Signer>>);
impl SignersContainer { impl SignersContainer {
pub fn as_key_map(&self, secp: &SecpCtx) -> KeyMap { pub fn as_key_map(&self, secp: &SecpCtx) -> KeyMap {
@ -394,18 +395,19 @@ impl SignersContainer {
/// Returns the list of signers in the container, sorted by lowest to highest `ordering` /// Returns the list of signers in the container, sorted by lowest to highest `ordering`
pub fn signers(&self) -> Vec<&Arc<dyn Signer>> { pub fn signers(&self) -> Vec<&Arc<dyn Signer>> {
let mut items = self.0.iter().collect::<Vec<_>>(); self.0.values().collect()
items.sort_unstable_by_key(|(key, _)| *key);
items.into_iter().map(|(_, v)| v).collect()
} }
/// Finds the signer with lowest ordering for a given id in the container. /// Finds the signer with lowest ordering for a given id in the container.
pub fn find(&self, id: SignerId) -> Option<&Arc<dyn Signer>> { pub fn find(&self, id: SignerId) -> Option<&Arc<dyn Signer>> {
self.0 self.0
.iter() .range((
.filter(|(key, _)| key.id == id) Included(&(id.clone(), SignerOrdering(0)).into()),
.min_by(|(k_a, _), (k_b, _)| k_a.cmp(k_b)) Included(&(id.clone(), SignerOrdering(usize::MAX)).into()),
))
.filter(|(k, _)| k.id == id)
.map(|(_, v)| v) .map(|(_, v)| v)
.next()
} }
} }
@ -521,10 +523,20 @@ impl PartialOrd for SignersContainerKey {
impl Ord for SignersContainerKey { impl Ord for SignersContainerKey {
fn cmp(&self, other: &Self) -> Ordering { fn cmp(&self, other: &Self) -> Ordering {
self.ordering.cmp(&other.ordering) self.ordering
.cmp(&other.ordering)
.then(self.id.cmp(&other.id))
} }
} }
impl PartialEq for SignersContainerKey {
fn eq(&self, other: &Self) -> bool {
self.id == other.id && self.ordering == other.ordering
}
}
impl Eq for SignersContainerKey {}
#[cfg(test)] #[cfg(test)]
mod signers_container_tests { mod signers_container_tests {
use super::*; use super::*;