From 17f0d29d0845e033a0a3647cfbf112b6328e5522 Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Thu, 20 Oct 2022 15:24:17 +0000 Subject: [PATCH] add some comments to `Transaction::weight` FIxes #1159 --- bitcoin/src/blockdata/transaction.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index 9569c5b7..106ed904 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -717,6 +717,16 @@ impl Transaction { /// For transactions with an empty witness, this is simply the consensus-serialized size times /// four. For transactions with a witness, this is the non-witness consensus-serialized size /// multiplied by three plus the with-witness consensus-serialized size. + /// + /// For transactions with no inputs, this function will return a value 2 less than the actual + /// weight of the serialized transaction. The reason is that zero-input transactions, post-segwit, + /// cannot be unambiguously serialized; we make a choice that adds two extra bytes. For more + /// details see [BIP 141](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki) + /// which uses a "input count" of `0x00` as a `marker` for a Segwit-encoded transaction. + /// + /// If you need to use 0-input transactions, we strongly recommend you do so using the PSBT + /// API. The unsigned transaction encoded within PSBT is always a non-segwit transaction + /// and can therefore avoid this ambiguity. #[inline] pub fn weight(&self) -> usize { self.scaled_size(WITNESS_SCALE_FACTOR)