From 2b3b22f5594c4699ee238477d56aa98282ff3666 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Fri, 16 Jul 2021 15:05:15 +0200 Subject: [PATCH] impl Encodable for Annex to avoid allocation --- src/consensus/encode.rs | 2 +- src/util/sighash.rs | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/consensus/encode.rs b/src/consensus/encode.rs index cdf4496d..a796c60b 100644 --- a/src/consensus/encode.rs +++ b/src/consensus/encode.rs @@ -600,7 +600,7 @@ impl_vec!(u64); #[cfg(feature = "std")] impl_vec!((u32, Address)); #[cfg(feature = "std")] impl_vec!(AddrV2Message); -fn consensus_encode_with_size(data: &[u8], mut s: S) -> Result { +pub(crate) fn consensus_encode_with_size(data: &[u8], mut s: S) -> Result { let vi_len = VarInt(data.len() as u64).consensus_encode(&mut s)?; s.emit_slice(&data)?; Ok(vi_len + data.len()) diff --git a/src/util/sighash.rs b/src/util/sighash.rs index d049e37d..5557fa0a 100644 --- a/src/util/sighash.rs +++ b/src/util/sighash.rs @@ -20,7 +20,7 @@ //! pub use blockdata::transaction::SigHashType as LegacySigHashType; -use consensus::Encodable; +use consensus::{encode, Encodable}; use core::fmt; use core::ops::{Deref, DerefMut}; use hashes::{sha256, sha256d, Hash}; @@ -344,7 +344,7 @@ impl> SigHashCache { // includes the mandatory 0x50 prefix. if let Some(annex) = annex { let mut enc = sha256::Hash::engine(); - annex.as_bytes().to_vec().consensus_encode(&mut enc)?; + annex.consensus_encode(&mut enc)?; let hash = sha256::Hash::from_engine(enc); hash.consensus_encode(&mut writer)?; } @@ -626,6 +626,12 @@ impl<'a> Annex<'a> { } } +impl<'a> Encodable for Annex<'a> { + fn consensus_encode(&self, writer: W) -> Result { + encode::consensus_encode_with_size(&self.0, writer) + } +} + #[cfg(test)] mod tests { use consensus::deserialize;