From f6e73ff118ce1b0941a34fa451c6ddc28cf58ff9 Mon Sep 17 00:00:00 2001 From: conduition Date: Thu, 9 Nov 2023 19:09:40 +0000 Subject: [PATCH 1/5] fix: FeeRate::checked_mul_by_weight should scale output down by 1000 --- bitcoin/src/blockdata/fee_rate.rs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/bitcoin/src/blockdata/fee_rate.rs b/bitcoin/src/blockdata/fee_rate.rs index 1c3b8a9e..aa8d3c44 100644 --- a/bitcoin/src/blockdata/fee_rate.rs +++ b/bitcoin/src/blockdata/fee_rate.rs @@ -82,10 +82,12 @@ impl FeeRate { /// Checked weight multiplication. /// - /// Computes `self * rhs` where rhs is of type Weight. `None` is returned if an overflow - /// occurred. + /// Computes the absolute fee amount for a given [`Weight`] at this fee rate. + /// + /// `None` is returned if an overflow occurred. pub fn checked_mul_by_weight(self, rhs: Weight) -> Option { - self.0.checked_mul(rhs.to_wu()).map(Amount::from_sat) + let sats = self.0.checked_mul(rhs.to_wu())?.checked_add(999)? / 1000; + Some(Amount::from_sat(sats)) } /// Calculates fee by multiplying this fee rate by weight, in weight units, returning `None` @@ -95,13 +97,14 @@ impl FeeRate { /// /// # Examples /// - /// ```no_run + /// ``` /// # use bitcoin::{absolute, transaction, FeeRate, Transaction}; /// # // Dummy transaction. /// # let tx = Transaction { version: transaction::Version::ONE, lock_time: absolute::LockTime::ZERO, input: vec![], output: vec![] }; /// /// let rate = FeeRate::from_sat_per_vb(1).expect("1 sat/vbyte is valid"); - /// let fee = rate.fee_wu(tx.weight()); + /// let fee = rate.fee_wu(tx.weight()).unwrap(); + /// assert_eq!(fee.to_sat(), tx.vsize() as u64); /// ``` pub fn fee_wu(self, weight: Weight) -> Option { self.checked_mul_by_weight(weight) } @@ -209,12 +212,20 @@ mod tests { #[test] fn checked_weight_mul_test() { - let weight = Weight::from_wu(10); - let fee: Amount = FeeRate(10).checked_mul_by_weight(weight).expect("expected Amount"); + let weight = Weight::from_vb(10).unwrap(); + let fee: Amount = FeeRate::from_sat_per_vb(10) + .unwrap() + .checked_mul_by_weight(weight) + .expect("expected Amount"); assert_eq!(Amount::from_sat(100), fee); let fee = FeeRate(10).checked_mul_by_weight(Weight::MAX); assert!(fee.is_none()); + + let weight = Weight::from_vb(3).unwrap(); + let fee_rate = FeeRate::from_sat_per_vb(3).unwrap(); + let fee = fee_rate.checked_mul_by_weight(weight).unwrap(); + assert_eq!(Amount::from_sat(9), fee); } #[test] From ad5b119ee5db6891d3a90ba5ebe5fc4cb45ac918 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 8 Dec 2023 06:11:40 +1100 Subject: [PATCH 2/5] Add deprecated bip32 types back in In the 0.31.0 release we renamed the bip32 extended key types without leaving the originals in there marked as deprecated. This makes for a bad experience for devs, add them back in. --- bitcoin/src/bip32.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bitcoin/src/bip32.rs b/bitcoin/src/bip32.rs index 7a2c4be9..08caf36c 100644 --- a/bitcoin/src/bip32.rs +++ b/bitcoin/src/bip32.rs @@ -36,6 +36,14 @@ const VERSION_BYTES_TESTNETS_PUBLIC: [u8; 4] = [0x04, 0x35, 0x87, 0xCF]; /// Version bytes for extended private keys on any of the testnet networks. const VERSION_BYTES_TESTNETS_PRIVATE: [u8; 4] = [0x04, 0x35, 0x83, 0x94]; +/// The old name for xpub, extended public key. +#[deprecated(since = "0.31.0", note = "use xpub instead")] +pub type ExtendendPubKey = Xpub; + +/// The old name for xpriv, extended public key. +#[deprecated(since = "0.31.0", note = "use xpriv instead")] +pub type ExtendendPrivKey = Xpriv; + /// A chain code #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ChainCode([u8; 32]); From cfa018c903b70aa8d60bee063f73fe93246b5caf Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 9 Jan 2024 13:22:23 +1100 Subject: [PATCH 3/5] Bump version to 0.31.1 In preparation for a point release add a changelog and bump the version number to v0.31.1 Note that the changelog had an incorrect version number for the last release, fix that while we are at it. --- Cargo-minimal.lock | 2 +- Cargo-recent.lock | 2 +- bitcoin/CHANGELOG.md | 7 ++++++- bitcoin/Cargo.toml | 2 +- fuzz/Cargo.toml | 2 +- fuzz/generate-files.sh | 2 +- 6 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Cargo-minimal.lock b/Cargo-minimal.lock index 0b1392fd..8a486efb 100644 --- a/Cargo-minimal.lock +++ b/Cargo-minimal.lock @@ -32,7 +32,7 @@ dependencies = [ [[package]] name = "bitcoin" -version = "0.31.0" +version = "0.31.1" dependencies = [ "base64", "bech32", diff --git a/Cargo-recent.lock b/Cargo-recent.lock index 307e739e..355a16fb 100644 --- a/Cargo-recent.lock +++ b/Cargo-recent.lock @@ -31,7 +31,7 @@ dependencies = [ [[package]] name = "bitcoin" -version = "0.31.0" +version = "0.31.1" dependencies = [ "base64", "bech32", diff --git a/bitcoin/CHANGELOG.md b/bitcoin/CHANGELOG.md index ebc8f9fb..d7593982 100644 --- a/bitcoin/CHANGELOG.md +++ b/bitcoin/CHANGELOG.md @@ -1,4 +1,9 @@ -# 0.31.1 - 2023-10-18 +# 0.31.1 - 2024-01-09 + +- Fix bug in `FeeRate::checked_mul_by_weight` [#2128](https://github.com/rust-bitcoin/rust-bitcoin/pull/2182) +- Add BIP-32 types remove in 0.31 back in and mark as deprecated [#2258](https://github.com/rust-bitcoin/rust-bitcoin/pull/2258) + +# 0.31.0 - 2023-10-18 - Bump MSRV to Rust 1.48.0 [#1729](https://github.com/rust-bitcoin/rust-bitcoin/pull/1729) - Add new example code for signature verification [#1776](https://github.com/rust-bitcoin/rust-bitcoin/pull/1776) diff --git a/bitcoin/Cargo.toml b/bitcoin/Cargo.toml index 1f1926a4..cb1cd787 100644 --- a/bitcoin/Cargo.toml +++ b/bitcoin/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bitcoin" -version = "0.31.0" +version = "0.31.1" authors = ["Andrew Poelstra "] license = "CC0-1.0" repository = "https://github.com/rust-bitcoin/rust-bitcoin/" diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index c40270cc..d41ac971 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -10,7 +10,7 @@ cargo-fuzz = true [dependencies] honggfuzz = { version = "0.5.55", default-features = false } -bitcoin = { version = "0.31.0", features = [ "serde" ] } +bitcoin = { version = "0.31.1", features = [ "serde" ] } serde = { version = "1.0.103", features = [ "derive" ] } serde_json = "1.0" diff --git a/fuzz/generate-files.sh b/fuzz/generate-files.sh index 25af8989..e04516a4 100755 --- a/fuzz/generate-files.sh +++ b/fuzz/generate-files.sh @@ -21,7 +21,7 @@ cargo-fuzz = true [dependencies] honggfuzz = { version = "0.5.55", default-features = false } -bitcoin = { version = "0.31.0", features = [ "serde" ] } +bitcoin = { version = "0.31.1", features = [ "serde" ] } serde = { version = "1.0.103", features = [ "derive" ] } serde_json = "1.0" From 866781da1d3b4cfa3b3160b2ed97520559220f21 Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Sun, 17 Mar 2024 22:30:20 +0000 Subject: [PATCH 4/5] merkle_block: add resource limit check during deserialization Fixes #2606 --- bitcoin/src/merkle_tree/block.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/bitcoin/src/merkle_tree/block.rs b/bitcoin/src/merkle_tree/block.rs index ae880660..3eb1f9b3 100644 --- a/bitcoin/src/merkle_tree/block.rs +++ b/bitcoin/src/merkle_tree/block.rs @@ -46,7 +46,7 @@ use self::MerkleBlockError::*; use crate::blockdata::block::{self, Block}; use crate::blockdata::transaction::Transaction; use crate::blockdata::weight::Weight; -use crate::consensus::encode::{self, Decodable, Encodable}; +use crate::consensus::encode::{self, Decodable, Encodable, MAX_VEC_SIZE}; use crate::hash_types::{TxMerkleNode, Txid}; use crate::io; use crate::prelude::*; @@ -460,6 +460,12 @@ impl Decodable for PartialMerkleTree { let hashes: Vec = Decodable::consensus_decode(r)?; let nb_bytes_for_bits = encode::VarInt::consensus_decode(r)?.0 as usize; + if nb_bytes_for_bits > MAX_VEC_SIZE { + return Err(encode::Error::OversizedVectorAllocation { + requested: nb_bytes_for_bits, + max: MAX_VEC_SIZE, + }); + } let mut bits = vec![false; nb_bytes_for_bits * 8]; for chunk in bits.chunks_mut(8) { let byte = u8::consensus_decode(r)?; @@ -821,4 +827,18 @@ mod tests { tree_width_20, 7, 2, 2; tree_width_21, 7, 3, 1; } + + #[test] + fn regression_2606() { + // Attempt + let bytes = hex!( + "000006000000000000000004ee00000004c7f1ccb1000000ffff000000010000\ + 0000ffffffffff1f000000000400000000000002000000000500000000000000\ + 000000000300000000000003000000000200000000ff00000000c7f1ccb10407\ + 00000000000000ccb100c76538b100000004bfa9c251681b1b00040000000025\ + 00000004bfaac251681b1b25\ + "); + let deser = crate::consensus::deserialize::(&bytes); + assert!(deser.is_err()); + } } From a5650d88b78e8524444733bbc00499857d5421b9 Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Tue, 2 Apr 2024 13:29:07 +0000 Subject: [PATCH 5/5] release 0.31.1 --- bitcoin/CHANGELOG.md | 4 ++++ bitcoin/Cargo.toml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/bitcoin/CHANGELOG.md b/bitcoin/CHANGELOG.md index d7593982..254a941a 100644 --- a/bitcoin/CHANGELOG.md +++ b/bitcoin/CHANGELOG.md @@ -1,3 +1,7 @@ +# 0.31.2 - 2024-04-01 + +- Fix parsing bug in `merkle_block::deserialize` [#2614](https://github.com/rust-bitcoin/rust-bitcoin/pull/2614) + # 0.31.1 - 2024-01-09 - Fix bug in `FeeRate::checked_mul_by_weight` [#2128](https://github.com/rust-bitcoin/rust-bitcoin/pull/2182) diff --git a/bitcoin/Cargo.toml b/bitcoin/Cargo.toml index cb1cd787..dccef7f4 100644 --- a/bitcoin/Cargo.toml +++ b/bitcoin/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bitcoin" -version = "0.31.1" +version = "0.31.2" authors = ["Andrew Poelstra "] license = "CC0-1.0" repository = "https://github.com/rust-bitcoin/rust-bitcoin/"