Skip to content

Commit

Permalink
With dummy TxOutV2 serialization handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouzo committed Jan 22, 2024
1 parent d540306 commit c1a3d00
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
55 changes: 52 additions & 3 deletions bitcoin/src/blockdata/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,25 @@ fn size_from_script_pubkey(script_pubkey: &Script) -> usize {
Amount::SIZE + VarInt::from(len).size() + len
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))]
pub struct TxOutV2 {
/// The value of the output, in satoshis.
pub value: Amount,
/// The script which must be satisfied for the output to be spent.
pub script_pubkey: ScriptBuf,
}

impl From<TxOut> for TxOutV2 {
fn from(tx: TxOut) -> Self { TxOutV2 { value: tx.value, script_pubkey: tx.script_pubkey } }
}

impl From<TxOutV2> for TxOut {
fn from(tx: TxOutV2) -> Self {
TxOut { value: tx.value, script_pubkey: tx.script_pubkey, unused_token_id: 0 }
}
}
/// Bitcoin transaction.
///
/// An authenticated movement of coins.
Expand Down Expand Up @@ -993,6 +1012,9 @@ impl Version {
/// The second Bitcoin transaction version (post-BIP-68).
pub const TWO: Self = Self(2);

/// The Fourth Defichain transaction version.
pub const FOUR: Self = Self(4);

/// Creates a non-standard transaction version.
pub fn non_standard(version: i32) -> Version { Self(version) }

Expand All @@ -1013,6 +1035,7 @@ impl Decodable for Version {
}

impl_consensus_encoding!(TxOut, value, script_pubkey, unused_token_id);
impl_consensus_encoding!(TxOutV2, value, script_pubkey);

impl Encodable for OutPoint {
fn consensus_encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
Expand Down Expand Up @@ -1078,7 +1101,17 @@ impl Encodable for Transaction {
len += SEGWIT_MARKER.consensus_encode(w)?;
len += SEGWIT_FLAG.consensus_encode(w)?;
len += self.input.consensus_encode(w)?;
len += self.output.consensus_encode(w)?;

len += if self.version == Version::TWO {
self.output.iter().try_fold(0usize, |acc, tx| {
let value_len = tx.value.consensus_encode(w)?;
let script_len = tx.script_pubkey.consensus_encode(w)?;
Ok::<usize, io::Error>(acc + value_len + script_len)
})?
} else {
self.output.consensus_encode(w)?
};

for input in &self.input {
len += input.witness.consensus_encode(w)?;
}
Expand All @@ -1101,7 +1134,14 @@ impl Decodable for Transaction {
// BIP144 input witnesses
1 => {
let mut input = Vec::<TxIn>::consensus_decode_from_finite_reader(r)?;
let output = Vec::<TxOut>::consensus_decode_from_finite_reader(r)?;
let output = if version == Version::TWO {
Vec::<TxOutV2>::consensus_decode_from_finite_reader(r)?
.into_iter()
.map(Into::into)
.collect()
} else {
Decodable::consensus_decode_from_finite_reader(r)?
};
for txin in input.iter_mut() {
txin.witness = Decodable::consensus_decode_from_finite_reader(r)?;
}
Expand All @@ -1124,7 +1164,16 @@ impl Decodable for Transaction {
Ok(Transaction {
version,
input,
output: Decodable::consensus_decode_from_finite_reader(r)?,
output: {
if version == Version::TWO {
Vec::<TxOutV2>::consensus_decode_from_finite_reader(r)?
.into_iter()
.map(Into::into)
.collect()
} else {
Decodable::consensus_decode_from_finite_reader(r)?
}
},
lock_time: Decodable::consensus_decode_from_finite_reader(r)?,
})
}
Expand Down
2 changes: 2 additions & 0 deletions bitcoin/src/consensus/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::p2p::{
};
use crate::prelude::*;
use crate::taproot::TapLeafHash;
use crate::transaction::TxOutV2;

/// Encoding error.
#[derive(Debug)]
Expand Down Expand Up @@ -628,6 +629,7 @@ impl_vec!(FilterHeader);
impl_vec!(TxMerkleNode);
impl_vec!(Transaction);
impl_vec!(TxOut);
impl_vec!(TxOutV2);
impl_vec!(TxIn);
impl_vec!(Vec<u8>);
impl_vec!(u64);
Expand Down

0 comments on commit c1a3d00

Please sign in to comment.