From: benthecarman Date: Tue, 9 May 2023 00:02:05 +0000 (-0500) Subject: Impl PartialOrd and Ord for Invoice X-Git-Tag: v0.0.116-alpha1~41^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=2ddce64ba6b5e5ee7db206cc9f7321855473ec88;p=rust-lightning Impl PartialOrd and Ord for Invoice --- diff --git a/lightning-invoice/src/lib.rs b/lightning-invoice/src/lib.rs index f53c8953..8ccd9d97 100644 --- a/lightning-invoice/src/lib.rs +++ b/lightning-invoice/src/lib.rs @@ -61,6 +61,7 @@ use secp256k1::PublicKey; use secp256k1::{Message, Secp256k1}; use secp256k1::ecdsa::RecoverableSignature; +use core::cmp::Ordering; use core::fmt::{Display, Formatter, self}; use core::iter::FilterMap; use core::num::ParseIntError; @@ -248,7 +249,7 @@ pub struct InvoiceBuilder(&str)` (see [`Invoice::from_str`]) /// /// [`Invoice::from_str`]: crate::Invoice#impl-FromStr -#[derive(Eq, PartialEq, Debug, Clone, Hash)] +#[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)] pub struct Invoice { signed_invoice: SignedRawInvoice, } @@ -258,7 +259,7 @@ pub struct Invoice { /// /// This is not exported to bindings users as we don't have a good way to map the reference lifetimes making this /// practically impossible to use safely in languages like C. -#[derive(Eq, PartialEq, Debug, Clone)] +#[derive(Eq, PartialEq, Debug, Clone, Ord, PartialOrd)] pub enum InvoiceDescription<'f> { /// Reference to the directly supplied description in the invoice Direct(&'f Description), @@ -272,7 +273,7 @@ pub enum InvoiceDescription<'f> { /// /// # Invariants /// The hash has to be either from the deserialized invoice or from the serialized [`RawInvoice`]. -#[derive(Eq, PartialEq, Debug, Clone, Hash)] +#[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)] pub struct SignedRawInvoice { /// The rawInvoice that the signature belongs to raw_invoice: RawInvoice, @@ -295,7 +296,7 @@ pub struct SignedRawInvoice { /// Decoding and encoding should not lead to information loss but may lead to different hashes. /// /// For methods without docs see the corresponding methods in [`Invoice`]. -#[derive(Eq, PartialEq, Debug, Clone, Hash)] +#[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)] pub struct RawInvoice { /// human readable part pub hrp: RawHrp, @@ -307,7 +308,7 @@ pub struct RawInvoice { /// Data of the [`RawInvoice`] that is encoded in the human readable part. /// /// This is not exported to bindings users as we don't yet support `Option` -#[derive(Eq, PartialEq, Debug, Clone, Hash)] +#[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)] pub struct RawHrp { /// The currency deferred from the 3rd and 4th character of the bech32 transaction pub currency: Currency, @@ -320,7 +321,7 @@ pub struct RawHrp { } /// Data of the [`RawInvoice`] that is encoded in the data part -#[derive(Eq, PartialEq, Debug, Clone, Hash)] +#[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)] pub struct RawDataPart { /// generation time of the invoice pub timestamp: PositiveTimestamp, @@ -335,11 +336,11 @@ pub struct RawDataPart { /// /// The Unix timestamp representing the stored time has to be positive and no greater than /// [`MAX_TIMESTAMP`]. -#[derive(Eq, PartialEq, Debug, Clone, Hash)] +#[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)] pub struct PositiveTimestamp(Duration); /// SI prefixes for the human readable part -#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)] +#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash, Ord, PartialOrd)] pub enum SiPrefix { /// 10^-3 Milli, @@ -376,7 +377,7 @@ impl SiPrefix { } /// Enum representing the crypto currencies (or networks) supported by this library -#[derive(Clone, Debug, Hash, Eq, PartialEq)] +#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)] pub enum Currency { /// Bitcoin mainnet Bitcoin, @@ -420,7 +421,7 @@ impl From for Network { /// Tagged field which may have an unknown tag /// /// This is not exported to bindings users as we don't currently support TaggedField -#[derive(Clone, Debug, Hash, Eq, PartialEq)] +#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)] pub enum RawTaggedField { /// Parsed tagged field with known tag KnownSemantics(TaggedField), @@ -435,7 +436,7 @@ pub enum RawTaggedField { /// This is not exported to bindings users as we don't yet support enum variants with the same name the struct contained /// in the variant. #[allow(missing_docs)] -#[derive(Clone, Debug, Hash, Eq, PartialEq)] +#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)] pub enum TaggedField { PaymentHash(Sha256), Description(Description), @@ -451,7 +452,7 @@ pub enum TaggedField { } /// SHA-256 hash -#[derive(Clone, Debug, Hash, Eq, PartialEq)] +#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)] pub struct Sha256(/// This is not exported to bindings users as the native hash types are not currently mapped pub sha256::Hash); @@ -468,25 +469,25 @@ impl Sha256 { /// /// # Invariants /// The description can be at most 639 __bytes__ long -#[derive(Clone, Debug, Hash, Eq, PartialEq)] +#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)] pub struct Description(String); /// Payee public key -#[derive(Clone, Debug, Hash, Eq, PartialEq)] +#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)] pub struct PayeePubKey(pub PublicKey); /// Positive duration that defines when (relatively to the timestamp) in the future the invoice /// expires -#[derive(Clone, Debug, Hash, Eq, PartialEq)] +#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)] pub struct ExpiryTime(Duration); /// `min_final_cltv_expiry_delta` to use for the last HTLC in the route -#[derive(Clone, Debug, Hash, Eq, PartialEq)] +#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)] pub struct MinFinalCltvExpiryDelta(pub u64); /// Fallback address in case no LN payment is possible #[allow(missing_docs)] -#[derive(Clone, Debug, Hash, Eq, PartialEq)] +#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)] pub enum Fallback { SegWitProgram { version: WitnessVersion, @@ -500,12 +501,24 @@ pub enum Fallback { #[derive(Clone, Debug, Hash, Eq, PartialEq)] pub struct InvoiceSignature(pub RecoverableSignature); +impl PartialOrd for InvoiceSignature { + fn partial_cmp(&self, other: &Self) -> Option { + self.0.serialize_compact().1.partial_cmp(&other.0.serialize_compact().1) + } +} + +impl Ord for InvoiceSignature { + fn cmp(&self, other: &Self) -> Ordering { + self.0.serialize_compact().1.cmp(&other.0.serialize_compact().1) + } +} + /// Private routing information /// /// # Invariants /// The encoded route has to be <1024 5bit characters long (<=639 bytes or <=12 hops) /// -#[derive(Clone, Debug, Hash, Eq, PartialEq)] +#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)] pub struct PrivateRoute(RouteHint); /// Tag constants as specified in BOLT11 diff --git a/lightning/src/ln/features.rs b/lightning/src/ln/features.rs index 8ccbf416..9997a1ec 100644 --- a/lightning/src/ln/features.rs +++ b/lightning/src/ln/features.rs @@ -451,6 +451,16 @@ impl PartialEq for Features { self.flags.eq(&o.flags) } } +impl PartialOrd for Features { + fn partial_cmp(&self, other: &Self) -> Option { + self.flags.partial_cmp(&other.flags) + } +} +impl Ord for Features { + fn cmp(&self, other: &Self) -> cmp::Ordering { + self.flags.cmp(&other.flags) + } +} impl fmt::Debug for Features { fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { self.flags.fmt(fmt) diff --git a/lightning/src/ln/mod.rs b/lightning/src/ln/mod.rs index 3a0efd38..92edb061 100644 --- a/lightning/src/ln/mod.rs +++ b/lightning/src/ln/mod.rs @@ -72,19 +72,17 @@ pub use self::peer_channel_encryptor::LN_MAX_MSG_LEN; /// payment_hash type, use to cross-lock hop /// /// This is not exported to bindings users as we just use [u8; 32] directly -#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)] -#[cfg_attr(test, derive(PartialOrd, Ord))] +#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug, Ord, PartialOrd)] pub struct PaymentHash(pub [u8; 32]); /// payment_preimage type, use to route payment between hop /// /// This is not exported to bindings users as we just use [u8; 32] directly -#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)] -#[cfg_attr(test, derive(PartialOrd, Ord))] +#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug, Ord, PartialOrd)] pub struct PaymentPreimage(pub [u8; 32]); /// payment_secret type, use to authenticate sender to the receiver and tie MPP HTLCs together /// /// This is not exported to bindings users as we just use [u8; 32] directly -#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)] +#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug, Ord, PartialOrd)] pub struct PaymentSecret(pub [u8; 32]); use crate::prelude::*; diff --git a/lightning/src/routing/gossip.rs b/lightning/src/routing/gossip.rs index 90ead987..c5c08cf4 100644 --- a/lightning/src/routing/gossip.rs +++ b/lightning/src/routing/gossip.rs @@ -1056,7 +1056,7 @@ impl EffectiveCapacity { } /// Fees for routing via a given channel or a node -#[derive(Eq, PartialEq, Copy, Clone, Debug, Hash)] +#[derive(Eq, PartialEq, Copy, Clone, Debug, Hash, Ord, PartialOrd)] pub struct RoutingFees { /// Flat routing fee in millisatoshis. pub base_msat: u32, diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index d1d0296b..1031bc93 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -684,7 +684,7 @@ pub enum Hints { } /// A list of hops along a payment path terminating with a channel to the recipient. -#[derive(Clone, Debug, Hash, Eq, PartialEq)] +#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)] pub struct RouteHint(pub Vec); impl Writeable for RouteHint { @@ -709,7 +709,7 @@ impl Readable for RouteHint { } /// A channel descriptor for a hop along a payment path. -#[derive(Clone, Debug, Hash, Eq, PartialEq)] +#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)] pub struct RouteHintHop { /// The node_id of the non-target end of the route pub src_node_id: PublicKey,