Skip to content

Commit 3eecfad

Browse files
committed
Implement Ord for Transaction
This haven't any consensuns meaning but it's useful to have transactions in ordered collections
1 parent ba4aad3 commit 3eecfad

3 files changed

Lines changed: 29 additions & 9 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ json-contract = [ "serde_json" ]
2222

2323
[dependencies]
2424
bitcoin = "0.30.0"
25-
secp256k1-zkp = { version = "0.9.1", features = [ "global-context", "bitcoin_hashes" ] }
25+
secp256k1-zkp = { version = "0.9.2", features = [ "global-context", "bitcoin_hashes" ] }
2626
slip21 = "0.2.0"
2727

2828
# Used for ContractHash::from_json_contract.

src/confidential.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::encode::{self, Decodable, Encodable};
3333
use crate::issuance::AssetId;
3434

3535
/// A CT commitment to an amount
36-
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
36+
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
3737
pub enum Value {
3838
/// No value
3939
Null,
@@ -252,7 +252,7 @@ impl<'de> Deserialize<'de> for Value {
252252
}
253253

254254
/// A CT commitment to an asset
255-
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
255+
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
256256
pub enum Asset {
257257
/// No value
258258
Null,

src/transaction.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//! # Transactions
1616
//!
1717
18-
use std::{io, fmt, str};
18+
use std::{io, fmt, str, cmp};
1919
use std::collections::HashMap;
2020
use std::convert::TryFrom;
2121

@@ -34,7 +34,7 @@ use secp256k1_zkp::{
3434
};
3535

3636
/// Description of an asset issuance in a transaction input
37-
#[derive(Copy, Clone, Debug, Default, Eq, Hash, PartialEq)]
37+
#[derive(Copy, Clone, Debug, Default, Eq, Hash, PartialEq, PartialOrd, Ord)]
3838
pub struct AssetIssuance {
3939
/// Zero for a new asset issuance; otherwise a blinding factor for the input
4040
pub asset_blinding_nonce: Tweak,
@@ -343,7 +343,7 @@ impl std::error::Error for RelativeLockTimeError {
343343

344344

345345
/// Transaction input witness
346-
#[derive(Clone, Default, PartialEq, Eq, Debug, Hash)]
346+
#[derive(Clone, Default, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)]
347347
pub struct TxInWitness {
348348
/// Amount rangeproof
349349
pub amount_rangeproof: Option<Box<RangeProof>>,
@@ -443,7 +443,7 @@ impl<'tx> PeginData<'tx> {
443443
}
444444

445445
/// A transaction input, which defines old coins to be consumed
446-
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
446+
#[derive(Clone, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)]
447447
pub struct TxIn {
448448
/// The reference to the previous output that is being used an an input
449449
pub previous_output: OutPoint,
@@ -604,7 +604,7 @@ impl TxIn {
604604
}
605605

606606
/// Transaction output witness
607-
#[derive(Clone, Default, PartialEq, Eq, Debug, Hash)]
607+
#[derive(Clone, Default, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)]
608608
pub struct TxOutWitness {
609609
/// Surjection proof showing that the asset commitment is legitimate
610610
// We Box it because surjection proof internally is an array [u8; N] that
@@ -651,7 +651,7 @@ pub struct PegoutData<'txo> {
651651
}
652652

653653
/// Transaction output
654-
#[derive(Clone, Default, PartialEq, Eq, Debug, Hash)]
654+
#[derive(Clone, Default, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)]
655655
pub struct TxOut {
656656
/// Committed asset
657657
pub asset: confidential::Asset,
@@ -1067,6 +1067,26 @@ impl Decodable for Transaction {
10671067
}
10681068
}
10691069
}
1070+
1071+
impl cmp::PartialOrd for Transaction {
1072+
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
1073+
Some(self.cmp(other))
1074+
}
1075+
}
1076+
impl cmp::Ord for Transaction {
1077+
fn cmp(&self, other: &Self) -> cmp::Ordering {
1078+
self.version
1079+
.cmp(&other.version)
1080+
.then(
1081+
self.lock_time
1082+
.to_consensus_u32()
1083+
.cmp(&other.lock_time.to_consensus_u32()),
1084+
)
1085+
.then(self.input.cmp(&other.input))
1086+
.then(self.output.cmp(&other.output))
1087+
}
1088+
}
1089+
10701090
/// Hashtype of a transaction, encoded in the last byte of a signature
10711091
/// Fixed values so they can be casted as integer types for encoding
10721092
#[derive(PartialEq, Eq, Debug, Copy, Clone)]

0 commit comments

Comments
 (0)