This will avoid confusion later on in chapter 22 when we have to move everything over to transaction.rs and use the compactSize to determine if there are zero inputs in order to run segwit logic.
impl Decodable for Transaction {
fn consensus_decode<R: Read>(r: &mut R) -> Result<Self, Error> {
let version = Version::consensus_decode(r)?;
let inputs = Vec::<TxIn>::consensus_decode(r)?;
if inputs.is_empty() {
let segwit_flag = u8::consensus_decode(r)?;
match segwit_flag {
1 => {
let mut inputs = Vec::<TxIn>::consensus_decode(r)?;
let outputs = Vec::<TxOut>::consensus_decode(r)?;
for txin in inputs.iter_mut() {
txin.witness = Witness::consensus_decode(r)?;
}
if !inputs.is_empty() && inputs.iter().all(|input| input.witness.is_empty()) {
Err(Error::ParseFailed("witness flag set but no witnesses present"))
} else {
Ok(Transaction {
version,
inputs,
outputs,
lock_time: u32::consensus_decode(r)?,
})
}
}
// We don't support anything else
x => Err(Error::UnsupportedSegwitFlag(x)),
}
// non-segwit
} else {
Ok(Transaction {
version,
inputs,
outputs: Vec::<TxOut>::consensus_decode(r)?,
lock_time: u32::consensus_decode(r)?,
})
}
}
}
Resolves https://github.com/sb1752/bitcoiner-intro-to-rust/issues/27
cargo run
on every code snapshotcargo test
on every code snapshotThis will avoid confusion later on in chapter 22 when we have to move everything over to
transaction.rs
and use thecompactSize
to determine if there are zero inputs in order to run segwit logic.