From d9331794f1559376f47a71a4800e163c4d465223 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 5 Aug 2024 07:38:33 +1000 Subject: [PATCH 1/3] bip158: Fix typo --- bitcoin/src/bip158.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bitcoin/src/bip158.rs b/bitcoin/src/bip158.rs index f5a9d8e29..5b9084f7c 100644 --- a/bitcoin/src/bip158.rs +++ b/bitcoin/src/bip158.rs @@ -400,7 +400,7 @@ impl<'a, W: Write> GcsFilterWriter<'a, W> { // write number of elements as varint let mut wrote = VarInt::from(mapped.len()).consensus_encode(self.writer)?; - // write out deltas of sorted values into a Golonb-Rice coded bit stream + // write out deltas of sorted values into a Golomb-Rice coded bit stream let mut writer = BitStreamWriter::new(self.writer); let mut last = 0; for data in mapped { From 342fe18ad0604e3acb1e73c7ae628315a7ceb674 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 5 Aug 2024 07:29:36 +1000 Subject: [PATCH 2/3] Use From in map_to_range In an effort to remove unnecessary casts use `u128::from` to convert from `u64`s. Leave the cast to `u64` in there because it is right after a shift right and is brain-dead obvious. --- bitcoin/src/bip158.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bitcoin/src/bip158.rs b/bitcoin/src/bip158.rs index 5b9084f7c..d737f22fa 100644 --- a/bitcoin/src/bip158.rs +++ b/bitcoin/src/bip158.rs @@ -362,7 +362,7 @@ impl GcsFilterReader { } /// Fast reduction of hash to [0, nm) range. -fn map_to_range(hash: u64, nm: u64) -> u64 { ((hash as u128 * nm as u128) >> 64) as u64 } +fn map_to_range(hash: u64, nm: u64) -> u64 { ((u128::from(hash) * u128::from(nm)) >> 64) as u64 } /// Golomb-Rice encoded filter writer. pub struct GcsFilterWriter<'a, W> { From ea9f6e8f9761576ce53f8defee1891b0c82f60d9 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 5 Aug 2024 07:43:58 +1000 Subject: [PATCH 3/3] Cast after calling min In an effort to reduce the cognitive load of reading code we are removing casts unless they are useful or obvious. Move the cast onto the call to `min` and comment it for good measure. This allows us to call infallible `from` for conversion when needed. Refactor only, no logic changes. --- bitcoin/src/bip158.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bitcoin/src/bip158.rs b/bitcoin/src/bip158.rs index d737f22fa..d5b61a549 100644 --- a/bitcoin/src/bip158.rs +++ b/bitcoin/src/bip158.rs @@ -435,9 +435,9 @@ impl GcsFilter { let mut wrote = 0; let mut q = n >> self.p; while q > 0 { - let nbits = cmp::min(q, 64); - wrote += writer.write(!0u64, nbits as u8)?; - q -= nbits; + let nbits = cmp::min(q, 64) as u8; // cast ok, 64 fits into a `u8` + wrote += writer.write(!0u64, nbits)?; + q -= u64::from(nbits); } wrote += writer.write(0, 1)?; wrote += writer.write(n, self.p)?;