Skip to content

Commit

Permalink
Fix outstanding bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kixunil committed Jan 20, 2024
1 parent 5d75b16 commit 35a7ba3
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Cargo-minimal.lock
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,9 @@ dependencies = [

[[package]]
name = "push_decode"
version = "0.4.1"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "704d18e38492fac1966cc154028b33512154b6eb0aa19412297c8a0d46007b1e"
checksum = "ace085aaf6d87413b78ccf0e629176e6f6b33019e740cd20400decb7bebc877e"
dependencies = [
"either",
]
Expand Down
4 changes: 2 additions & 2 deletions Cargo-recent.lock
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ dependencies = [

[[package]]
name = "push_decode"
version = "0.4.1"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "704d18e38492fac1966cc154028b33512154b6eb0aa19412297c8a0d46007b1e"
checksum = "ace085aaf6d87413b78ccf0e629176e6f6b33019e740cd20400decb7bebc877e"
dependencies = [
"either",
]
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
bech32 = { version = "0.10.0-beta", default-features = false, features = ["alloc"] }
consensus-encoding = { package = "bitcoin-consensus-encoding", path = "../consensus-encoding", features = ["hashes"] }
consensus-encoding = { package = "bitcoin-consensus-encoding", path = "../consensus-encoding", default-features = false, features = ["hashes"] }
hashes = { package = "bitcoin_hashes", version = "0.13.0", default-features = false, features = ["alloc", "io"] }
hex = { package = "hex-conservative", version = "0.1.1", default-features = false, features = ["alloc"] }
hex_lit = "0.1.1"
Expand Down
5 changes: 4 additions & 1 deletion bitcoin/src/blockdata/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1235,12 +1235,14 @@ impl<'a> TransactionEncoder<'a> {
}
}

type WitnessesEncoder<'a> = UnprefixedIterEncoder<'a, Witness, core::iter::Map<core::slice::Iter<'a, TxIn>, for<'b> fn(&'b TxIn) -> &'b Witness>>;

enum TransactionEncoderState<'a> {
Version(VersionEncoder),
SegWitMarker,
Inputs(SliceEncoder<'a, TxIn>),
Outputs(SliceEncoder<'a, TxOut>),
Witnesses(UnprefixedIterEncoder<'a, Witness, core::iter::Map<core::slice::Iter<'a, TxIn>, for<'b> fn(&'b TxIn) -> &'b Witness>>),
Witnesses(WitnessesEncoder<'a>),
LockTime(LockTimeEncoder),
}

Expand Down Expand Up @@ -1494,6 +1496,7 @@ impl fmt::Display for TransactionDecodeError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for TransactionDecodeError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Expand Down
9 changes: 5 additions & 4 deletions bitcoin/src/blockdata/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,11 @@ impl Decode for Witness {
type Decoder = WitnessDecoder;
}

type WitnessInnerDecoder = ThenTry<WitnessDecodeError, VarIntDecoder, WitnessDataDecoder, fn(u64) -> Result<WitnessDataDecoder, WitnessDecodeError>>;

/// A decoder used to decode [`Witness`].
#[derive(Debug)]
pub struct WitnessDecoder(ThenTry<WitnessDecodeError, VarIntDecoder, WitnessDataDecoder, fn(u64) -> Result<WitnessDataDecoder, WitnessDecodeError,>>);
pub struct WitnessDecoder(WitnessInnerDecoder);

impl Decoder for WitnessDecoder {
type Value = Witness;
Expand Down Expand Up @@ -184,9 +186,8 @@ impl WitnessDataDecoder {
// to avoid wasting space without reallocating
let capacity = witness_elements * 5 + 128;
let mut buf = Vec::with_capacity(capacity);
for _ in 0..(witness_elements * 4) {
buf.push(0);
}
// just to fill the beginning with zeroes, we still have more reserved
buf.resize(witness_elements * 4, 0);

Ok(WitnessDataDecoder {
witness_elements,
Expand Down
2 changes: 1 addition & 1 deletion consensus-encoding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ std = ["alloc", "push_decode/std"]
alloc = ["push_decode/alloc"]

[dependencies]
push_decode = { version = "0.4.1", default-features = false }
push_decode = { version = "0.4.2", default-features = false }
hashes = { package = "bitcoin_hashes", version = "0.13", optional = true, default-features = false }
internals = { package = "bitcoin-internals", version = "0.2.0", default-features = false }
7 changes: 4 additions & 3 deletions consensus-encoding/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{Encoder, VarIntEncoder, BufWrite};

/// Associated type constructor for the [`Encode`] trait.
///
/// **You shouldn't need to worry about this, use [`gat_like`] macro to just use GAT syntax.**
/// **You shouldn't need to worry about this, use [`gat_like`](crate::gat_like) macro to just use the GAT syntax.**
///
/// Since we want to support older MSRV we workaround lack of generics by implementing a separate
/// supertrait that provides the associated type. We then reference this in the main `Encode`
Expand Down Expand Up @@ -35,8 +35,9 @@ pub trait Encode: for<'a> EncodeTc<'a> {
/// subtracting how many steps this function took to compute the value.
fn dyn_encoded_len(&self, max_steps: usize) -> (usize, usize);

/// Returns the recommended value to use in [`Vec::with_capacity`]/[`Vec::reserve`] when
/// serializing the value into a `Vec<u8>`.
/// Returns the recommended value to use in
/// [`Vec::with_capacity`](alloc::vec::Vec::with_capacity)/[`Vec::reserve`](alloc::vec::Vec::reserve)
/// when serializing the value into a `Vec<u8>`.
///
/// `max_steps` represents the maximum cost of computation. In general the slower your
/// allocator is the higher value should be chosen but the exact values should be determined by
Expand Down
9 changes: 8 additions & 1 deletion consensus-encoding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ ints!(u16, u32, u64, u128, i16, i32, i64, i128);

#[cfg(test)]
mod tests {
use super::{Decode, Encode};
use super::{Decode, Encode, impl_struct_decode, impl_struct_encode};
use core::fmt;

#[test]
fn impl_struct_de() {
Expand All @@ -87,6 +88,12 @@ mod tests {
}
}

impl fmt::Display for FooDecodeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "fee error")
}
}

let foo = Foo::consensus_decode_slice(&[0x2a, 0x00, 0x00, 0x00, 0x00, 0x40, 0x07, 0x5a, 0xf0, 0x75, 0x07, 0x00]).unwrap();
assert_eq!(foo.bar, 42);
assert_eq!(foo.baz, 2100000000000000);
Expand Down

0 comments on commit 35a7ba3

Please sign in to comment.