Move serialize::BitcoinHash to util:#️⃣:BitcoinHash
- Use Sha256dEncoder for calculating merkle root - Remove BitcoinHash implementation for Vec<u8>
This commit is contained in:
parent
97937b1b5f
commit
8e0e4eb55a
|
@ -22,10 +22,9 @@
|
||||||
|
|
||||||
use util;
|
use util;
|
||||||
use util::Error::{SpvBadTarget, SpvBadProofOfWork};
|
use util::Error::{SpvBadTarget, SpvBadProofOfWork};
|
||||||
use util::hash::Sha256dHash;
|
use util::hash::{BitcoinHash, Sha256dHash};
|
||||||
use util::uint::Uint256;
|
use util::uint::Uint256;
|
||||||
use network::encodable::VarInt;
|
use network::encodable::VarInt;
|
||||||
use network::serialize::BitcoinHash;
|
|
||||||
use network::constants::Network;
|
use network::constants::Network;
|
||||||
use blockdata::transaction::Transaction;
|
use blockdata::transaction::Transaction;
|
||||||
use blockdata::constants::max_target;
|
use blockdata::constants::max_target;
|
||||||
|
|
|
@ -143,9 +143,10 @@ mod test {
|
||||||
use hex::decode as hex_decode;
|
use hex::decode as hex_decode;
|
||||||
|
|
||||||
use network::constants::Network;
|
use network::constants::Network;
|
||||||
use network::serialize::{BitcoinHash, serialize};
|
use network::serialize::serialize;
|
||||||
use blockdata::constants::{genesis_block, bitcoin_genesis_tx};
|
use blockdata::constants::{genesis_block, bitcoin_genesis_tx};
|
||||||
use blockdata::constants::{MAX_SEQUENCE, COIN_VALUE};
|
use blockdata::constants::{MAX_SEQUENCE, COIN_VALUE};
|
||||||
|
use util::hash::BitcoinHash;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn bitcoin_genesis_first_transaction() {
|
fn bitcoin_genesis_first_transaction() {
|
||||||
|
|
|
@ -28,10 +28,10 @@ use std::default::Default;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
#[cfg(feature="bitcoinconsensus")] use std::collections::HashMap;
|
#[cfg(feature="bitcoinconsensus")] use std::collections::HashMap;
|
||||||
|
|
||||||
use util::hash::Sha256dHash;
|
use util::hash::{BitcoinHash, Sha256dHash};
|
||||||
#[cfg(feature="bitcoinconsensus")] use blockdata::script;
|
#[cfg(feature="bitcoinconsensus")] use blockdata::script;
|
||||||
use blockdata::script::Script;
|
use blockdata::script::Script;
|
||||||
use network::serialize::{self, serialize, BitcoinHash, SimpleEncoder, SimpleDecoder};
|
use network::serialize::{self, serialize, SimpleEncoder, SimpleDecoder};
|
||||||
use network::encodable::{ConsensusEncodable, ConsensusDecodable, VarInt};
|
use network::encodable::{ConsensusEncodable, ConsensusDecodable, VarInt};
|
||||||
|
|
||||||
/// A reference to a transaction output
|
/// A reference to a transaction output
|
||||||
|
@ -499,11 +499,10 @@ mod tests {
|
||||||
use super::{Transaction, TxIn};
|
use super::{Transaction, TxIn};
|
||||||
|
|
||||||
use blockdata::script::Script;
|
use blockdata::script::Script;
|
||||||
use network::serialize::BitcoinHash;
|
|
||||||
#[cfg(all(feature = "serde", feature = "strason"))]
|
#[cfg(all(feature = "serde", feature = "strason"))]
|
||||||
use network::serialize::serialize;
|
use network::serialize::serialize;
|
||||||
use network::serialize::deserialize;
|
use network::serialize::deserialize;
|
||||||
use util::hash::Sha256dHash;
|
use util::hash::{BitcoinHash, Sha256dHash};
|
||||||
use util::misc::hex_bytes;
|
use util::misc::hex_bytes;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -72,9 +72,9 @@ pub use blockdata::transaction::TxOut;
|
||||||
pub use blockdata::transaction::OutPoint;
|
pub use blockdata::transaction::OutPoint;
|
||||||
pub use blockdata::transaction::SigHashType;
|
pub use blockdata::transaction::SigHashType;
|
||||||
pub use network::encodable::VarInt;
|
pub use network::encodable::VarInt;
|
||||||
pub use network::serialize::BitcoinHash;
|
|
||||||
pub use util::Error;
|
pub use util::Error;
|
||||||
pub use util::address::Address;
|
pub use util::address::Address;
|
||||||
|
pub use util::hash::BitcoinHash;
|
||||||
pub use util::privkey::Privkey;
|
pub use util::privkey::Privkey;
|
||||||
pub use util::decimal::Decimal;
|
pub use util::decimal::Decimal;
|
||||||
pub use util::decimal::UDecimal;
|
pub use util::decimal::UDecimal;
|
||||||
|
|
|
@ -30,7 +30,6 @@ use bitcoin_bech32;
|
||||||
|
|
||||||
use network::encodable::{ConsensusDecodable, ConsensusEncodable};
|
use network::encodable::{ConsensusDecodable, ConsensusEncodable};
|
||||||
use util::base58;
|
use util::base58;
|
||||||
use util::hash::Sha256dHash;
|
|
||||||
|
|
||||||
/// Serialization error
|
/// Serialization error
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -158,19 +157,6 @@ impl From<io::Error> for Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Objects which are referred to by hash
|
|
||||||
pub trait BitcoinHash {
|
|
||||||
/// Produces a Sha256dHash which can be used to refer to the object
|
|
||||||
fn bitcoin_hash(&self) -> Sha256dHash;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BitcoinHash for Vec<u8> {
|
|
||||||
#[inline]
|
|
||||||
fn bitcoin_hash(&self) -> Sha256dHash {
|
|
||||||
Sha256dHash::from_data(&self[..])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Encode an object into a vector
|
/// Encode an object into a vector
|
||||||
pub fn serialize<T: ?Sized>(data: &T) -> Result<Vec<u8>, Error>
|
pub fn serialize<T: ?Sized>(data: &T) -> Result<Vec<u8>, Error>
|
||||||
where T: ConsensusEncodable<RawEncoder<Cursor<Vec<u8>>>>,
|
where T: ConsensusEncodable<RawEncoder<Cursor<Vec<u8>>>>,
|
||||||
|
|
|
@ -20,7 +20,6 @@ use std::cmp::min;
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::error;
|
use std::error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::io::Cursor;
|
|
||||||
use std::mem;
|
use std::mem;
|
||||||
#[cfg(feature = "serde")] use serde;
|
#[cfg(feature = "serde")] use serde;
|
||||||
|
|
||||||
|
@ -29,7 +28,7 @@ use crypto::digest::Digest;
|
||||||
use crypto::ripemd160::Ripemd160;
|
use crypto::ripemd160::Ripemd160;
|
||||||
|
|
||||||
use network::encodable::{ConsensusDecodable, ConsensusEncodable};
|
use network::encodable::{ConsensusDecodable, ConsensusEncodable};
|
||||||
use network::serialize::{self, SimpleEncoder, RawEncoder, BitcoinHash};
|
use network::serialize::{self, SimpleEncoder};
|
||||||
use util::uint::Uint256;
|
use util::uint::Uint256;
|
||||||
|
|
||||||
#[cfg(feature="fuzztarget")] use util::sha2::Sha256;
|
#[cfg(feature="fuzztarget")] use util::sha2::Sha256;
|
||||||
|
@ -462,10 +461,10 @@ pub fn bitcoin_merkle_root(data: Vec<Sha256dHash>) -> Sha256dHash {
|
||||||
for idx in 0..((data.len() + 1) / 2) {
|
for idx in 0..((data.len() + 1) / 2) {
|
||||||
let idx1 = 2 * idx;
|
let idx1 = 2 * idx;
|
||||||
let idx2 = min(idx1 + 1, data.len() - 1);
|
let idx2 = min(idx1 + 1, data.len() - 1);
|
||||||
let mut encoder = RawEncoder::new(Cursor::new(vec![]));
|
let mut encoder = Sha256dEncoder::new();
|
||||||
data[idx1].consensus_encode(&mut encoder).unwrap();
|
data[idx1].consensus_encode(&mut encoder).unwrap();
|
||||||
data[idx2].consensus_encode(&mut encoder).unwrap();
|
data[idx2].consensus_encode(&mut encoder).unwrap();
|
||||||
next.push(encoder.into_inner().into_inner().bitcoin_hash());
|
next.push(encoder.into_hash());
|
||||||
}
|
}
|
||||||
bitcoin_merkle_root(next)
|
bitcoin_merkle_root(next)
|
||||||
}
|
}
|
||||||
|
@ -482,6 +481,11 @@ impl <T: BitcoinHash> MerkleRoot for Vec<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Objects which are referred to by hash
|
||||||
|
pub trait BitcoinHash {
|
||||||
|
/// Produces a Sha256dHash which can be used to refer to the object
|
||||||
|
fn bitcoin_hash(&self) -> Sha256dHash;
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
Loading…
Reference in New Issue