primitives: switch BlockHash to array ref encoding
What changed, and why it matters
This tiny change switches how a 32-byte Bitcoin block hash is encoded internally. Instead of copying the hash bytes into a new owned array before encoding, it now encodes directly from a borrowed reference to the existing bytes. This is a routine performance and memory-efficiency cleanup. There is no direct evidence in the commit that it fixes a security bug.
Treat as a normal code-quality/performance improvement. No security response is indicated by the available evidence. If this change is being backported, verify that `ArrayRefEncoder` correctly preserves the same wire encoding as `ArrayEncoder` (i.e., 32 raw bytes without length prefix) and that the lifetime `'e` is respected to avoid use-after-free in downstream callers.
Security signals we found
Eliminates an unnecessary 32-byte copy during BlockHash encoding
Switches from owned-array encoder to borrowed-reference encoder
No explicit security claim in commit message or diff
Evidence from the diff
The patch replaces encoding::ArrayEncoder<32> with encoding::ArrayRefEncoder<'e, 32> for BlockHashEncoder, and changes the construction from ArrayEncoder::without_length_prefix(self.to_byte_array()) to ArrayRefEncoder::without_length_prefix(self.as_byte_array()). The difference is that to_byte_array() likely copies the 32 bytes into a new array, while as_byte_array() returns a reference to the existing bytes. The encoder now borrows the bytes instead of owning a copy. This avoids an unnecessary copy and may reduce stack/heap allocations during serialization.
Changed components
primitives/src/hash_types/block_hash.rsBlockHashEncoderBlockHash::encoder() implementationInspect captured patch +2 / −2
diff --git a/primitives/src/hash_types/block_hash.rs b/primitives/src/hash_types/block_hash.rs
index 99c666d8..233e11ff 100644
--- a/primitives/src/hash_types/block_hash.rs
+++ b/primitives/src/hash_types/block_hash.rs
@@ -31,13 +31,13 @@ include!("./generic.rs");
encoding::encoder_newtype_exact! {
/// The encoder for the [`BlockHash`] type.
- pub struct BlockHashEncoder<'e>(encoding::ArrayEncoder<32>);
+ pub struct BlockHashEncoder<'e>(encoding::ArrayRefEncoder<'e, 32>);
}
impl Encodable for BlockHash {
type Encoder<'e> = BlockHashEncoder<'e>;
fn encoder(&self) -> Self::Encoder<'_> {
- BlockHashEncoder::new(encoding::ArrayEncoder::without_length_prefix(self.to_byte_array()))
+ BlockHashEncoder::new(encoding::ArrayRefEncoder::without_length_prefix(self.as_byte_array()))
}
}
Why this scored 16/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.