Skip to content

Commit

Permalink
Copy ReadExt/WriteExt from bitcoin
Browse files Browse the repository at this point in the history
Otherwise we miss them when upgrading to 0.32
  • Loading branch information
RCasatta committed Aug 19, 2024
1 parent 4aad862 commit d29bf4d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 84 deletions.
73 changes: 11 additions & 62 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ use crate::transaction::{Transaction, TxIn, TxOut};

pub use bitcoin::{self, consensus::encode::MAX_VEC_SIZE};

// Use the ReadExt/WriteExt traits as is from upstream
pub use bitcoin::consensus::encode::{ReadExt, WriteExt};

use crate::taproot::TapLeafHash;

/// Encoding error
Expand Down Expand Up @@ -207,7 +204,7 @@ impl Decodable for sha256::Midstate {
}
}

pub(crate) fn consensus_encode_with_size<S: io::Write>(
pub(crate) fn consensus_encode_with_size<S: crate::WriteExt>(
data: &[u8],
mut s: S,
) -> Result<usize, Error> {
Expand Down Expand Up @@ -245,64 +242,16 @@ impl Decodable for crate::locktime::Time {
}
}

// TODO reuse bitcoin's `WriteExt::emit_varint`, `ReadExt::read_varint` when available

/// A variable sized integer.
pub struct VarInt(pub u64);
impl Encodable for VarInt {
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, Error> {
match self.0 {
i @ 0..=0xFC => {
e.emit_u8(i as u8)?;
Ok(1)
}
i @ 0xFD..=0xFFFF => {
e.emit_u8(0xFD)?;
e.emit_u16(i as u16)?;
Ok(3)
}
i @ 0x10000..=0xFFFFFFFF => {
e.emit_u8(0xFE)?;
e.emit_u32(i as u32)?;
Ok(5)
}
i => {
e.emit_u8(0xFF)?;
e.emit_u64(i)?;
Ok(9)
}
}
fn consensus_encode<W: crate::WriteExt>(&self, mut e: W) -> Result<usize, Error> {
Ok(e.emit_varint(self.0)?)
}
}
impl Decodable for VarInt {
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, Error> {
match d.read_u8()? {
0xFF => {
let x = d.read_u64()?;
if x < 0x100000000 {
Err(Error::NonMinimalVarInt)
} else {
Ok(VarInt(x))
}
}
0xFE => {
let x = d.read_u32()?;
if x < 0x10000 {
Err(Error::NonMinimalVarInt)
} else {
Ok(VarInt(x as u64))
}
}
0xFD => {
let x = d.read_u16()?;
if x < 0xFD {
Err(Error::NonMinimalVarInt)
} else {
Ok(VarInt(x as u64))
}
}
n => Ok(VarInt(n as u64)),
}
fn consensus_decode<D: crate::ReadExt>(mut d: D) -> Result<Self, Error> {
Ok(VarInt(d.read_varint()?))
}
}
impl VarInt {
Expand All @@ -321,14 +270,14 @@ impl VarInt {
macro_rules! impl_int {
($ty:ident, $meth_dec:ident, $meth_enc:ident) => {
impl Encodable for $ty {
fn consensus_encode<W: io::Write>(&self, mut w: W) -> Result<usize, Error> {
fn consensus_encode<W: crate::WriteExt>(&self, mut w: W) -> Result<usize, Error> {
w.$meth_enc(*self)?;
Ok(mem::size_of::<$ty>())
}
}
impl Decodable for $ty {
fn consensus_decode<R: io::Read>(mut r: R) -> Result<Self, Error> {
Ok(ReadExt::$meth_dec(&mut r)?)
fn consensus_decode<R: crate::ReadExt>(mut r: R) -> Result<Self, Error> {
Ok(crate::ReadExt::$meth_dec(&mut r)?)
}
}
};
Expand Down Expand Up @@ -411,7 +360,7 @@ macro_rules! impl_array {
( $size:literal ) => {
impl Encodable for [u8; $size] {
#[inline]
fn consensus_encode<W: WriteExt>(
fn consensus_encode<W: crate::WriteExt>(
&self,
mut w: W,
) -> core::result::Result<usize, Error> {
Expand All @@ -422,7 +371,7 @@ macro_rules! impl_array {

impl Decodable for [u8; $size] {
#[inline]
fn consensus_decode<R: ReadExt>(mut r: R) -> core::result::Result<Self, Error> {
fn consensus_decode<R: crate::ReadExt>(mut r: R) -> core::result::Result<Self, Error> {
let mut ret = [0; $size];
r.read_slice(&mut ret)?;
Ok(ret)
Expand Down Expand Up @@ -452,7 +401,7 @@ impl Encodable for Vec<u8> {
}
}
impl Decodable for Vec<u8> {
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, Error> {
fn consensus_decode<D: crate::ReadExt>(mut d: D) -> Result<Self, Error> {
let s = VarInt::consensus_decode(&mut d)?.0 as usize;
let mut v = vec![0; s];
d.read_slice(&mut v)?;
Expand Down
51 changes: 33 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,53 +31,68 @@ pub extern crate bitcoin;
/// Re-export of secp256k1-zkp crate
pub extern crate secp256k1_zkp;
/// Re-export of serde crate
#[cfg(feature = "serde")] #[macro_use] pub extern crate actual_serde as serde;
#[cfg(all(test, feature = "serde"))] extern crate serde_test;
#[cfg(feature = "serde")]
#[macro_use]
pub extern crate actual_serde as serde;
#[cfg(all(test, feature = "serde"))]
extern crate serde_test;

#[cfg(test)] extern crate rand;
#[cfg(test)] extern crate bincode;
#[cfg(any(test, feature = "serde_json"))] extern crate serde_json;
#[cfg(test)]
extern crate bincode;
#[cfg(test)]
extern crate rand;
#[cfg(any(test, feature = "serde_json"))]
extern crate serde_json;

#[macro_use] mod internal_macros;
#[macro_use]
mod internal_macros;
pub mod address;
pub mod blech32;
mod blind;
mod block;
pub mod confidential;
pub mod dynafed;
pub mod encode;
mod error;
mod ext;
mod fast_merkle_root;
pub mod hash_types;
pub mod hex;
pub mod locktime;
pub mod issuance;
pub mod locktime;
pub mod opcodes;
pub mod script;
mod transaction;
mod blind;
mod parse;
pub mod sighash;
pub mod pset;
pub mod taproot;
pub mod schnorr;
pub mod script;
#[cfg(feature = "serde")]
mod serde_utils;
pub mod sighash;
pub mod taproot;
mod transaction;
// consider making upstream public
mod endian;
// re-export bitcoin deps which we re-use
pub use bitcoin::bech32;
pub use bitcoin::hashes;
// export everything at the top level so it can be used as `elements::Transaction` etc.
pub use crate::address::{Address, AddressParams, AddressError};
pub use crate::transaction::{OutPoint, PeginData, PegoutData, EcdsaSighashType, TxIn, TxOut, TxInWitness, TxOutWitness, Transaction, AssetIssuance};
pub use crate::transaction::Sequence;
pub use crate::blind::{ConfidentialTxOutError, TxOutSecrets, SurjectionInput, TxOutError, VerificationError, BlindError, UnblindError, BlindValueProofs, BlindAssetProofs, RangeProofMessage};
pub use crate::block::{BlockHeader, Block};
pub use crate::address::{Address, AddressError, AddressParams};
pub use crate::blind::{
BlindAssetProofs, BlindError, BlindValueProofs, ConfidentialTxOutError, RangeProofMessage,
SurjectionInput, TxOutError, TxOutSecrets, UnblindError, VerificationError,
};
pub use crate::block::ExtData as BlockExtData;
pub use crate::block::{Block, BlockHeader};
pub use crate::ext::{ReadExt, WriteExt};
pub use crate::fast_merkle_root::fast_merkle_root;
pub use crate::hash_types::*;
pub use crate::issuance::{AssetId, ContractHash};
pub use crate::locktime::LockTime;
pub use crate::schnorr::{SchnorrSig, SchnorrSigError};
pub use crate::script::Script;
pub use crate::sighash::SchnorrSighashType;
pub use crate::schnorr::{SchnorrSig, SchnorrSigError};
pub use crate::transaction::Sequence;
pub use crate::transaction::{
AssetIssuance, EcdsaSighashType, OutPoint, PeginData, PegoutData, Transaction, TxIn,
TxInWitness, TxOut, TxOutWitness,
};
6 changes: 2 additions & 4 deletions src/pset/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
use std::{fmt, io};

use super::Error;
use crate::encode::{
self, deserialize, serialize, Decodable, Encodable, VarInt, WriteExt, MAX_VEC_SIZE,
};
use crate::encode::{self, deserialize, serialize, Decodable, Encodable, VarInt, MAX_VEC_SIZE};
use crate::hex;
/// A PSET key in its raw byte form.
#[derive(Debug, PartialEq, Hash, Eq, Clone, Ord, PartialOrd)]
Expand Down Expand Up @@ -181,7 +179,7 @@ impl<Subtype> Encodable for ProprietaryKey<Subtype>
where
Subtype: Copy + From<u8> + Into<u8>,
{
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, encode::Error> {
fn consensus_encode<W: crate::WriteExt>(&self, mut e: W) -> Result<usize, encode::Error> {
let mut len = self.prefix.consensus_encode(&mut e)? + 1;
e.emit_u8(self.subtype.into())?;
len += e.write(&self.key)?;
Expand Down

0 comments on commit d29bf4d

Please sign in to comment.