Merge pull request #422 from rust-bitcoin/2020-04-remove-alloc

Remove some needless allocations
This commit is contained in:
Elichai Turkel 2020-05-19 13:20:34 +03:00 committed by GitHub
commit 1c88be4df5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 18 deletions

View File

@ -33,7 +33,7 @@ use util::endian;
use blockdata::constants::WITNESS_SCALE_FACTOR; use blockdata::constants::WITNESS_SCALE_FACTOR;
#[cfg(feature="bitcoinconsensus")] use blockdata::script; #[cfg(feature="bitcoinconsensus")] use blockdata::script;
use blockdata::script::Script; use blockdata::script::Script;
use consensus::{encode, serialize, Decodable, Encodable}; use consensus::{encode, Decodable, Encodable};
use hash_types::*; use hash_types::*;
use VarInt; use VarInt;
@ -378,9 +378,11 @@ impl Transaction {
_ => unreachable!() _ => unreachable!()
}; };
// hash the result // hash the result
let mut raw_vec = serialize(&tx); let mut engine = SigHash::engine();
raw_vec.extend_from_slice(&endian::u32_to_array_le(sighash_u32)); tx.consensus_encode(&mut engine).unwrap();
SigHash::hash(&raw_vec) let sighash_arr = endian::u32_to_array_le(sighash_u32);
sighash_arr.consensus_encode(&mut engine).unwrap();
SigHash::from_engine(engine)
} }
/// Gets the "weight" of this transaction, as defined by BIP141. For transactions with an empty /// Gets the "weight" of this transaction, as defined by BIP141. For transactions with an empty
@ -441,7 +443,7 @@ impl Transaction {
/// The lambda spent should not return the same TxOut twice! /// The lambda spent should not return the same TxOut twice!
pub fn verify<S>(&self, mut spent: S) -> Result<(), script::Error> pub fn verify<S>(&self, mut spent: S) -> Result<(), script::Error>
where S: FnMut(&OutPoint) -> Option<TxOut> { where S: FnMut(&OutPoint) -> Option<TxOut> {
let tx = serialize(&*self); let tx = encode::serialize(&*self);
for (idx, input) in self.input.iter().enumerate() { for (idx, input) in self.input.iter().enumerate() {
if let Some(output) = spent(&input.previous_output) { if let Some(output) = spent(&input.previous_output) {
output.script_pubkey.verify(idx, output.value, tx.as_slice())?; output.script_pubkey.verify(idx, output.value, tx.as_slice())?;

View File

@ -149,10 +149,10 @@ impl From<psbt::Error> for Error {
/// Encode an object into a vector /// Encode an object into a vector
pub fn serialize<T: Encodable + ?Sized>(data: &T) -> Vec<u8> { pub fn serialize<T: Encodable + ?Sized>(data: &T) -> Vec<u8> {
let mut encoder = Cursor::new(vec![]); let mut encoder = Vec::new();
let len = data.consensus_encode(&mut encoder).unwrap(); let len = data.consensus_encode(&mut encoder).unwrap();
assert_eq!(len, encoder.get_ref().len()); assert_eq!(len, encoder.len());
encoder.into_inner() encoder
} }
/// Encode an object into a hex-encoded string /// Encode an object into a hex-encoded string

View File

@ -16,9 +16,9 @@
//! //!
//! Various utility functions //! Various utility functions
use hashes::{sha256d, Hash}; use hashes::{sha256d, Hash, HashEngine};
use blockdata::opcodes; use blockdata::opcodes;
use consensus::encode; use consensus::{encode, Encodable};
static MSG_SIGN_PREFIX: &[u8] = b"\x18Bitcoin Signed Message:\n"; static MSG_SIGN_PREFIX: &[u8] = b"\x18Bitcoin Signed Message:\n";
@ -59,14 +59,14 @@ pub fn script_find_and_remove(haystack: &mut Vec<u8>, needle: &[u8]) -> usize {
/// Hash message for signature using Bitcoin's message signing format /// Hash message for signature using Bitcoin's message signing format
pub fn signed_msg_hash(msg: &str) -> sha256d::Hash { pub fn signed_msg_hash(msg: &str) -> sha256d::Hash {
sha256d::Hash::hash(
&[ let mut engine = sha256d::Hash::engine();
MSG_SIGN_PREFIX, engine.input(MSG_SIGN_PREFIX);
&encode::serialize(&encode::VarInt(msg.len() as u64)), let msg_len = encode::VarInt(msg.len() as u64);
msg.as_bytes(), msg_len.consensus_encode(&mut engine).unwrap();
] engine.input(msg.as_bytes());
.concat(),
) sha256d::Hash::from_engine(engine)
} }
#[cfg(test)] #[cfg(test)]