Skip to content

Commit

Permalink
Merge #167: Implement Ord for Transaction
Browse files Browse the repository at this point in the history
3eecfad Implement Ord for Transaction (Riccardo Casatta)

Pull request description:

  This haven't any consensuns meaning but it's useful to have transactions in ordered collections

  ~~depends on BlockstreamResearch/rust-secp256k1-zkp#68 merged

  ~~draft because needs rust-secp256k1-zkp release BlockstreamResearch/rust-secp256k1-zkp#70

ACKs for top commit:
  apoelstra:
    ACK 3eecfad

Tree-SHA512: ba4a207baa1a1994dd53b86019f814195e1b40c85ed9e294badd6d4eb3cbd7611347bb1afcb6ab3dc944245526bfe02f420d798bcfa831058211e797dba5ec68
  • Loading branch information
apoelstra committed Jul 18, 2023
2 parents f3f7640 + 3eecfad commit b9db351
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ json-contract = [ "serde_json" ]

[dependencies]
bitcoin = "0.30.0"
secp256k1-zkp = { version = "0.9.1", features = [ "global-context", "bitcoin_hashes" ] }
secp256k1-zkp = { version = "0.9.2", features = [ "global-context", "bitcoin_hashes" ] }
slip21 = "0.2.0"

# Used for ContractHash::from_json_contract.
Expand Down
4 changes: 2 additions & 2 deletions src/confidential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::encode::{self, Decodable, Encodable};
use crate::issuance::AssetId;

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

/// A CT commitment to an asset
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub enum Asset {
/// No value
Null,
Expand Down
32 changes: 26 additions & 6 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//! # Transactions
//!

use std::{io, fmt, str};
use std::{io, fmt, str, cmp};
use std::collections::HashMap;
use std::convert::TryFrom;

Expand All @@ -34,7 +34,7 @@ use secp256k1_zkp::{
};

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


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

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

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

/// Transaction output
#[derive(Clone, Default, PartialEq, Eq, Debug, Hash)]
#[derive(Clone, Default, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)]
pub struct TxOut {
/// Committed asset
pub asset: confidential::Asset,
Expand Down Expand Up @@ -1067,6 +1067,26 @@ impl Decodable for Transaction {
}
}
}

impl cmp::PartialOrd for Transaction {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl cmp::Ord for Transaction {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.version
.cmp(&other.version)
.then(
self.lock_time
.to_consensus_u32()
.cmp(&other.lock_time.to_consensus_u32()),
)
.then(self.input.cmp(&other.input))
.then(self.output.cmp(&other.output))
}
}

/// Hashtype of a transaction, encoded in the last byte of a signature
/// Fixed values so they can be casted as integer types for encoding
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
Expand Down

0 comments on commit b9db351

Please sign in to comment.