Skip to content

Commit 7b64527

Browse files
authored
Merge pull request #2279 from benthecarman/ord-invoice
Impl PartialOrd and Ord for Invoice
2 parents 9e542ec + 2ddce64 commit 7b64527

File tree

5 files changed

+47
-26
lines changed

5 files changed

+47
-26
lines changed

lightning-invoice/src/lib.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ use secp256k1::PublicKey;
6161
use secp256k1::{Message, Secp256k1};
6262
use secp256k1::ecdsa::RecoverableSignature;
6363

64+
use core::cmp::Ordering;
6465
use core::fmt::{Display, Formatter, self};
6566
use core::iter::FilterMap;
6667
use core::num::ParseIntError;
@@ -248,7 +249,7 @@ pub struct InvoiceBuilder<D: tb::Bool, H: tb::Bool, T: tb::Bool, C: tb::Bool, S:
248249
/// 3. using `str::parse::<Invoice>(&str)` (see [`Invoice::from_str`])
249250
///
250251
/// [`Invoice::from_str`]: crate::Invoice#impl-FromStr
251-
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
252+
#[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)]
252253
pub struct Invoice {
253254
signed_invoice: SignedRawInvoice,
254255
}
@@ -258,7 +259,7 @@ pub struct Invoice {
258259
///
259260
/// This is not exported to bindings users as we don't have a good way to map the reference lifetimes making this
260261
/// practically impossible to use safely in languages like C.
261-
#[derive(Eq, PartialEq, Debug, Clone)]
262+
#[derive(Eq, PartialEq, Debug, Clone, Ord, PartialOrd)]
262263
pub enum InvoiceDescription<'f> {
263264
/// Reference to the directly supplied description in the invoice
264265
Direct(&'f Description),
@@ -272,7 +273,7 @@ pub enum InvoiceDescription<'f> {
272273
///
273274
/// # Invariants
274275
/// The hash has to be either from the deserialized invoice or from the serialized [`RawInvoice`].
275-
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
276+
#[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)]
276277
pub struct SignedRawInvoice {
277278
/// The rawInvoice that the signature belongs to
278279
raw_invoice: RawInvoice,
@@ -295,7 +296,7 @@ pub struct SignedRawInvoice {
295296
/// Decoding and encoding should not lead to information loss but may lead to different hashes.
296297
///
297298
/// For methods without docs see the corresponding methods in [`Invoice`].
298-
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
299+
#[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)]
299300
pub struct RawInvoice {
300301
/// human readable part
301302
pub hrp: RawHrp,
@@ -307,7 +308,7 @@ pub struct RawInvoice {
307308
/// Data of the [`RawInvoice`] that is encoded in the human readable part.
308309
///
309310
/// This is not exported to bindings users as we don't yet support `Option<Enum>`
310-
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
311+
#[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)]
311312
pub struct RawHrp {
312313
/// The currency deferred from the 3rd and 4th character of the bech32 transaction
313314
pub currency: Currency,
@@ -320,7 +321,7 @@ pub struct RawHrp {
320321
}
321322

322323
/// Data of the [`RawInvoice`] that is encoded in the data part
323-
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
324+
#[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)]
324325
pub struct RawDataPart {
325326
/// generation time of the invoice
326327
pub timestamp: PositiveTimestamp,
@@ -335,11 +336,11 @@ pub struct RawDataPart {
335336
///
336337
/// The Unix timestamp representing the stored time has to be positive and no greater than
337338
/// [`MAX_TIMESTAMP`].
338-
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
339+
#[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)]
339340
pub struct PositiveTimestamp(Duration);
340341

341342
/// SI prefixes for the human readable part
342-
#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)]
343+
#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash, Ord, PartialOrd)]
343344
pub enum SiPrefix {
344345
/// 10^-3
345346
Milli,
@@ -376,7 +377,7 @@ impl SiPrefix {
376377
}
377378

378379
/// Enum representing the crypto currencies (or networks) supported by this library
379-
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
380+
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
380381
pub enum Currency {
381382
/// Bitcoin mainnet
382383
Bitcoin,
@@ -420,7 +421,7 @@ impl From<Currency> for Network {
420421
/// Tagged field which may have an unknown tag
421422
///
422423
/// This is not exported to bindings users as we don't currently support TaggedField
423-
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
424+
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
424425
pub enum RawTaggedField {
425426
/// Parsed tagged field with known tag
426427
KnownSemantics(TaggedField),
@@ -435,7 +436,7 @@ pub enum RawTaggedField {
435436
/// This is not exported to bindings users as we don't yet support enum variants with the same name the struct contained
436437
/// in the variant.
437438
#[allow(missing_docs)]
438-
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
439+
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
439440
pub enum TaggedField {
440441
PaymentHash(Sha256),
441442
Description(Description),
@@ -451,7 +452,7 @@ pub enum TaggedField {
451452
}
452453

453454
/// SHA-256 hash
454-
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
455+
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
455456
pub struct Sha256(/// This is not exported to bindings users as the native hash types are not currently mapped
456457
pub sha256::Hash);
457458

@@ -468,25 +469,25 @@ impl Sha256 {
468469
///
469470
/// # Invariants
470471
/// The description can be at most 639 __bytes__ long
471-
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
472+
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
472473
pub struct Description(String);
473474

474475
/// Payee public key
475-
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
476+
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
476477
pub struct PayeePubKey(pub PublicKey);
477478

478479
/// Positive duration that defines when (relatively to the timestamp) in the future the invoice
479480
/// expires
480-
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
481+
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
481482
pub struct ExpiryTime(Duration);
482483

483484
/// `min_final_cltv_expiry_delta` to use for the last HTLC in the route
484-
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
485+
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
485486
pub struct MinFinalCltvExpiryDelta(pub u64);
486487

487488
/// Fallback address in case no LN payment is possible
488489
#[allow(missing_docs)]
489-
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
490+
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
490491
pub enum Fallback {
491492
SegWitProgram {
492493
version: WitnessVersion,
@@ -500,12 +501,24 @@ pub enum Fallback {
500501
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
501502
pub struct InvoiceSignature(pub RecoverableSignature);
502503

504+
impl PartialOrd for InvoiceSignature {
505+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
506+
self.0.serialize_compact().1.partial_cmp(&other.0.serialize_compact().1)
507+
}
508+
}
509+
510+
impl Ord for InvoiceSignature {
511+
fn cmp(&self, other: &Self) -> Ordering {
512+
self.0.serialize_compact().1.cmp(&other.0.serialize_compact().1)
513+
}
514+
}
515+
503516
/// Private routing information
504517
///
505518
/// # Invariants
506519
/// The encoded route has to be <1024 5bit characters long (<=639 bytes or <=12 hops)
507520
///
508-
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
521+
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
509522
pub struct PrivateRoute(RouteHint);
510523

511524
/// Tag constants as specified in BOLT11

lightning/src/ln/features.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,16 @@ impl<T: sealed::Context> PartialEq for Features<T> {
451451
self.flags.eq(&o.flags)
452452
}
453453
}
454+
impl<T: sealed::Context> PartialOrd for Features<T> {
455+
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
456+
self.flags.partial_cmp(&other.flags)
457+
}
458+
}
459+
impl<T: sealed::Context + Eq> Ord for Features<T> {
460+
fn cmp(&self, other: &Self) -> cmp::Ordering {
461+
self.flags.cmp(&other.flags)
462+
}
463+
}
454464
impl<T: sealed::Context> fmt::Debug for Features<T> {
455465
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
456466
self.flags.fmt(fmt)

lightning/src/ln/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,17 @@ pub use self::peer_channel_encryptor::LN_MAX_MSG_LEN;
7272
/// payment_hash type, use to cross-lock hop
7373
///
7474
/// This is not exported to bindings users as we just use [u8; 32] directly
75-
#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
76-
#[cfg_attr(test, derive(PartialOrd, Ord))]
75+
#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug, Ord, PartialOrd)]
7776
pub struct PaymentHash(pub [u8; 32]);
7877
/// payment_preimage type, use to route payment between hop
7978
///
8079
/// This is not exported to bindings users as we just use [u8; 32] directly
81-
#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
82-
#[cfg_attr(test, derive(PartialOrd, Ord))]
80+
#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug, Ord, PartialOrd)]
8381
pub struct PaymentPreimage(pub [u8; 32]);
8482
/// payment_secret type, use to authenticate sender to the receiver and tie MPP HTLCs together
8583
///
8684
/// This is not exported to bindings users as we just use [u8; 32] directly
87-
#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
85+
#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug, Ord, PartialOrd)]
8886
pub struct PaymentSecret(pub [u8; 32]);
8987

9088
use crate::prelude::*;

lightning/src/routing/gossip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ impl EffectiveCapacity {
10561056
}
10571057

10581058
/// Fees for routing via a given channel or a node
1059-
#[derive(Eq, PartialEq, Copy, Clone, Debug, Hash)]
1059+
#[derive(Eq, PartialEq, Copy, Clone, Debug, Hash, Ord, PartialOrd)]
10601060
pub struct RoutingFees {
10611061
/// Flat routing fee in millisatoshis.
10621062
pub base_msat: u32,

lightning/src/routing/router.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ impl ReadableArgs<bool> for Features {
789789
}
790790

791791
/// A list of hops along a payment path terminating with a channel to the recipient.
792-
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
792+
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
793793
pub struct RouteHint(pub Vec<RouteHintHop>);
794794

795795
impl Writeable for RouteHint {
@@ -814,7 +814,7 @@ impl Readable for RouteHint {
814814
}
815815

816816
/// A channel descriptor for a hop along a payment path.
817-
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
817+
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
818818
pub struct RouteHintHop {
819819
/// The node_id of the non-target end of the route
820820
pub src_node_id: PublicKey,

0 commit comments

Comments
 (0)