Skip to content

Commit 6571307

Browse files
committed
transaction: Add Coinbase newtype to distinguish coinbase transactions
Coinbase transactions have unique consensus rules and exceptions that distinguish them from regular txs. We represent them using a dedicated Coinbase(Transaction) new type We use the transparent_newtype macro from internals for safe reference conversion (&Transaction -> &Coinbase) without manual unsafe code also it automatically adds #[repr(transparent)] which guarantees that Coinbase has the exact same memory layout as Transaction
1 parent f7274a5 commit 6571307

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

bitcoin/src/blockdata/transaction.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,21 @@ impl InputWeightPrediction {
11541154
}
11551155
}
11561156

1157+
internals::transparent_newtype! {
1158+
/// A wrapper type for the coinbase transaction of a block.
1159+
///
1160+
/// This type exists to distinguish coinbase transactions from regular ones at the type level.
1161+
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
1162+
pub struct Coinbase(Transaction);
1163+
1164+
impl Coinbase {
1165+
/// Creates a reference to `Coinbase` from a reference to the inner `Transaction`.
1166+
///
1167+
/// This method does not validate that the transaction is actually a coinbase transaction.
1168+
/// The caller must ensure that the transaction is indeed a valid coinbase transaction
1169+
pub fn assume_coinbase_ref(inner: &_) -> &Self;
1170+
}
1171+
}
11571172
mod sealed {
11581173
pub trait Sealed {}
11591174
impl Sealed for super::Transaction {}

0 commit comments

Comments
 (0)