Skip to content

Commit 63b5307

Browse files
authored
Merge pull request #109 from alexanderwiederin/cb-outpoint
Feat(core): Add `is_all_zeros()` and `is_null()` Methods to identify Coinbase outpoints
2 parents 72d6174 + 477e077 commit 63b5307

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/core/transaction.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,11 @@ pub trait TxOutPointExt: AsPtr<btck_TransactionOutPoint> {
553553
let ptr = unsafe { btck_transaction_out_point_get_txid(self.as_ptr()) };
554554
unsafe { TxidRef::from_ptr(ptr) }
555555
}
556+
557+
/// Returns true if this OutPoint is the "null" coinbase OutPoint.
558+
fn is_null(&self) -> bool {
559+
self.index() == u32::MAX && self.txid().is_all_zeros()
560+
}
556561
}
557562

558563
/// A reference to a specific output in a previous transaction.
@@ -655,6 +660,11 @@ pub trait TxidExt: AsPtr<btck_Txid> {
655660
}
656661
bytes
657662
}
663+
664+
/// Returns true if all bytes of the txid are zero (null txid).
665+
fn is_all_zeros(&self) -> bool {
666+
self.to_bytes().iter().all(|&b| b == 0)
667+
}
658668
}
659669

660670
pub struct Txid {
@@ -835,6 +845,21 @@ mod tests {
835845
(tx1, tx2)
836846
}
837847

848+
fn get_test_coinbase_transactions() -> (Transaction, Transaction) {
849+
let block_data = read_block_data();
850+
let tx1 = Block::new(&block_data[204])
851+
.unwrap()
852+
.transaction(0)
853+
.unwrap()
854+
.to_owned();
855+
let tx2 = Block::new(&block_data[205])
856+
.unwrap()
857+
.transaction(0)
858+
.unwrap()
859+
.to_owned();
860+
(tx1, tx2)
861+
}
862+
838863
fn get_test_txids() -> (Txid, Txid) {
839864
let (tx1, tx2) = get_test_transactions();
840865
(tx1.txid().to_owned(), tx2.txid().to_owned())
@@ -1187,6 +1212,33 @@ mod tests {
11871212
assert_eq!(index, 0);
11881213
}
11891214

1215+
#[test]
1216+
fn test_txoutpoint_coinbase_is_null() {
1217+
let (tx, _) = get_test_coinbase_transactions();
1218+
let txin = tx.input(0).unwrap();
1219+
let outpoint_ref = txin.outpoint();
1220+
let outpoint = outpoint_ref.to_owned();
1221+
1222+
assert!(outpoint_ref.is_null());
1223+
assert_eq!(outpoint_ref.index(), u32::MAX);
1224+
assert!(outpoint_ref.txid().is_all_zeros());
1225+
1226+
assert!(outpoint.is_null());
1227+
assert_eq!(outpoint.index(), u32::MAX);
1228+
assert!(outpoint.txid().is_all_zeros());
1229+
}
1230+
1231+
#[test]
1232+
fn test_txoutpoint_is_null() {
1233+
let (tx, _) = get_test_transactions();
1234+
let txin = tx.input(0).unwrap();
1235+
let outpoint_ref = txin.outpoint();
1236+
let outpoint = outpoint_ref.to_owned();
1237+
1238+
assert!(!outpoint_ref.is_null());
1239+
assert!(!outpoint.is_null());
1240+
}
1241+
11901242
#[test]
11911243
fn test_txoutpoint_txid() {
11921244
let (tx, _) = get_test_transactions();
@@ -1281,6 +1333,16 @@ mod tests {
12811333
assert_eq!(txid.to_bytes(), owned_txid.to_bytes());
12821334
}
12831335

1336+
#[test]
1337+
fn test_txid_is_all_zeros() {
1338+
let (tx, _) = get_test_transactions();
1339+
let txid = tx.txid().to_owned();
1340+
let txid_ref = txid.as_ref();
1341+
1342+
assert!(!txid.is_all_zeros());
1343+
assert!(!txid_ref.is_all_zeros());
1344+
}
1345+
12841346
// Polymorphism tests
12851347
#[test]
12861348
fn test_transaction_polymorphism() {

0 commit comments

Comments
 (0)