Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/core/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,11 @@ pub trait TxOutPointExt: AsPtr<btck_TransactionOutPoint> {
let ptr = unsafe { btck_transaction_out_point_get_txid(self.as_ptr()) };
unsafe { TxidRef::from_ptr(ptr) }
}

/// Returns true if this OutPoint is the "null" coinbase OutPoint.
fn is_null(&self) -> bool {
self.index() == u32::MAX && self.txid().is_all_zeros()
}
}

/// A reference to a specific output in a previous transaction.
Expand Down Expand Up @@ -655,6 +660,11 @@ pub trait TxidExt: AsPtr<btck_Txid> {
}
bytes
}

/// Returns true if all bytes of the txid are zero (null txid).
fn is_all_zeros(&self) -> bool {
self.to_bytes().iter().all(|&b| b == 0)
}
}

pub struct Txid {
Expand Down Expand Up @@ -835,6 +845,21 @@ mod tests {
(tx1, tx2)
}

fn get_test_coinbase_transactions() -> (Transaction, Transaction) {
let block_data = read_block_data();
let tx1 = Block::new(&block_data[204])
.unwrap()
.transaction(0)
.unwrap()
.to_owned();
let tx2 = Block::new(&block_data[205])
.unwrap()
.transaction(0)
.unwrap()
.to_owned();
(tx1, tx2)
}

fn get_test_txids() -> (Txid, Txid) {
let (tx1, tx2) = get_test_transactions();
(tx1.txid().to_owned(), tx2.txid().to_owned())
Expand Down Expand Up @@ -1187,6 +1212,33 @@ mod tests {
assert_eq!(index, 0);
}

#[test]
fn test_txoutpoint_coinbase_is_null() {
let (tx, _) = get_test_coinbase_transactions();
let txin = tx.input(0).unwrap();
let outpoint_ref = txin.outpoint();
let outpoint = outpoint_ref.to_owned();

assert!(outpoint_ref.is_null());
assert_eq!(outpoint_ref.index(), u32::MAX);
assert!(outpoint_ref.txid().is_all_zeros());

assert!(outpoint.is_null());
assert_eq!(outpoint.index(), u32::MAX);
assert!(outpoint.txid().is_all_zeros());
}

#[test]
fn test_txoutpoint_is_null() {
let (tx, _) = get_test_transactions();
let txin = tx.input(0).unwrap();
let outpoint_ref = txin.outpoint();
let outpoint = outpoint_ref.to_owned();

assert!(!outpoint_ref.is_null());
assert!(!outpoint.is_null());
}

#[test]
fn test_txoutpoint_txid() {
let (tx, _) = get_test_transactions();
Expand Down Expand Up @@ -1281,6 +1333,16 @@ mod tests {
assert_eq!(txid.to_bytes(), owned_txid.to_bytes());
}

#[test]
fn test_txid_is_all_zeros() {
let (tx, _) = get_test_transactions();
let txid = tx.txid().to_owned();
let txid_ref = txid.as_ref();

assert!(!txid.is_all_zeros());
assert!(!txid_ref.is_all_zeros());
}

// Polymorphism tests
#[test]
fn test_transaction_polymorphism() {
Expand Down
Loading