PMMRable cleanup (#1910)

* cleanup pmmrable and len()
introduce FixedLength trait with a const LEN
make Hash impl FixedLength for consistency

* rustfmt

* store tests cleanup

* rustfmt

* whats going on with those comments and rustfmt?
This commit is contained in:
Antioch Peverell
2018-11-01 20:14:46 +00:00
committed by GitHub
parent 9cebbf24b8
commit d23dec73d0
14 changed files with 81 additions and 112 deletions
+4 -12
View File
@@ -35,7 +35,7 @@ use core::{
use global;
use keychain::{self, BlindingFactor};
use pow::{Difficulty, Proof, ProofOfWork};
use ser::{self, PMMRable, Readable, Reader, Writeable, Writer};
use ser::{self, HashOnlyPMMRable, Readable, Reader, Writeable, Writer};
use util::{secp, static_secp_instance};
/// Errors thrown by Block validation
@@ -147,11 +147,9 @@ fn fixed_size_of_serialized_header(_version: u16) -> usize {
size += mem::size_of::<u16>(); // version
size += mem::size_of::<u64>(); // height
size += mem::size_of::<i64>(); // timestamp
// prev_hash, prev_root, output_root, range_proof_root, kernel_root
size += 5 * mem::size_of::<Hash>();
size += 5 * mem::size_of::<Hash>(); // prev_hash, prev_root, output_root, range_proof_root, kernel_root
size += mem::size_of::<BlindingFactor>(); // total_kernel_offset
// output_mmr_size, kernel_mmr_size
size += 2 * mem::size_of::<u64>();
size += 2 * mem::size_of::<u64>(); // output_mmr_size, kernel_mmr_size
size += mem::size_of::<Difficulty>(); // total_difficulty
size += mem::size_of::<u32>(); // secondary_scaling
size += mem::size_of::<u64>(); // nonce
@@ -190,13 +188,7 @@ impl Default for BlockHeader {
}
}
/// Block header hashes are maintained in the header MMR
/// but we store the data itself in the db.
impl PMMRable for BlockHeader {
fn len() -> usize {
0
}
}
impl HashOnlyPMMRable for BlockHeader {}
/// Serialization of a block header
impl Writeable for BlockHeader {
+7 -5
View File
@@ -26,7 +26,7 @@ use std::{fmt, ops};
use blake2::blake2b::Blake2b;
use consensus;
use ser::{self, AsFixedBytes, Error, Readable, Reader, Writeable, Writer};
use ser::{self, AsFixedBytes, Error, FixedLength, Readable, Reader, Writeable, Writer};
use util;
/// A hash consisting of all zeroes, used as a sentinel. No known preimage.
@@ -52,15 +52,17 @@ impl fmt::Display for Hash {
}
}
impl Hash {
impl FixedLength for Hash {
/// Size of a hash in bytes.
pub const SIZE: usize = 32;
const LEN: usize = 32;
}
impl Hash {
/// Builds a Hash from a byte vector. If the vector is too short, it will be
/// completed by zeroes. If it's too long, it will be truncated.
pub fn from_vec(v: &[u8]) -> Hash {
let mut h = [0; Hash::SIZE];
let copy_size = min(v.len(), Hash::SIZE);
let mut h = [0; Hash::LEN];
let copy_size = min(v.len(), Hash::LEN);
h[..copy_size].copy_from_slice(&v[..copy_size]);
Hash(h)
}
+1 -4
View File
@@ -34,10 +34,7 @@ pub trait HashOnlyBackend {
/// The PMMR itself does not need the Backend to be accurate on the existence
/// of an element (i.e. remove could be a no-op) but layers above can
/// depend on an accurate Backend to check existence.
pub trait Backend<T>
where
T: PMMRable,
{
pub trait Backend<T: PMMRable> {
/// Append the provided Hashes to the backend storage, and optionally an
/// associated data element to flatfile storage (for leaf nodes only). The
/// position of the first element of the Vec in the MMR is provided to
+3 -3
View File
@@ -18,12 +18,12 @@ use std::marker;
use core::hash::{Hash, ZERO_HASH};
use core::pmmr::{bintree_postorder_height, is_leaf, peak_map_height, peaks, HashOnlyBackend};
use ser::{PMMRIndexHashable, PMMRable};
use ser::{HashOnlyPMMRable, PMMRIndexHashable};
/// Database backed MMR.
pub struct DBPMMR<'a, T, B>
where
T: PMMRable,
T: HashOnlyPMMRable,
B: 'a + HashOnlyBackend,
{
/// The last position in the PMMR
@@ -36,7 +36,7 @@ where
impl<'a, T, B> DBPMMR<'a, T, B>
where
T: PMMRable + ::std::fmt::Debug,
T: HashOnlyPMMRable,
B: 'a + HashOnlyBackend,
{
/// Build a new db backed MMR.
+1 -1
View File
@@ -47,7 +47,7 @@ where
impl<'a, T, B> PMMR<'a, T, B>
where
T: PMMRable + ::std::fmt::Debug,
T: PMMRable,
B: 'a + Backend<T>,
{
/// Build a new prunable Merkle Mountain Range using the provided backend.
+1 -1
View File
@@ -35,7 +35,7 @@ where
impl<'a, T, B> ReadonlyPMMR<'a, T, B>
where
T: PMMRable + ::std::fmt::Debug,
T: PMMRable,
B: 'a + Backend<T>,
{
/// Build a new readonly PMMR.
+1 -1
View File
@@ -37,7 +37,7 @@ where
impl<'a, T, B> RewindablePMMR<'a, T, B>
where
T: PMMRable + ::std::fmt::Debug,
T: PMMRable,
B: 'a + Backend<T>,
{
/// Build a new readonly PMMR.
+11 -11
View File
@@ -26,7 +26,7 @@ use core::hash::Hashed;
use core::verifier_cache::VerifierCache;
use core::{committed, Committed};
use keychain::{self, BlindingFactor};
use ser::{self, read_multi, PMMRable, Readable, Reader, Writeable, Writer};
use ser::{self, read_multi, FixedLength, PMMRable, Readable, Reader, Writeable, Writer};
use util;
use util::secp::pedersen::{Commitment, RangeProof};
use util::secp::{self, Message, Signature};
@@ -240,13 +240,14 @@ impl TxKernel {
}
}
impl PMMRable for TxKernel {
fn len() -> usize {
17 + // features plus fee and lock_height
secp::constants::PEDERSEN_COMMITMENT_SIZE + secp::constants::AGG_SIGNATURE_SIZE
}
impl FixedLength for TxKernel {
const LEN: usize = 17 // features plus fee and lock_height
+ secp::constants::PEDERSEN_COMMITMENT_SIZE
+ secp::constants::AGG_SIGNATURE_SIZE;
}
impl PMMRable for TxKernel {}
/// TransactionBody is a common abstraction for transaction and block
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TransactionBody {
@@ -1172,13 +1173,12 @@ impl OutputIdentifier {
}
}
/// Ensure this is implemented to centralize hashing with indexes
impl PMMRable for OutputIdentifier {
fn len() -> usize {
1 + secp::constants::PEDERSEN_COMMITMENT_SIZE
}
impl FixedLength for OutputIdentifier {
const LEN: usize = 1 + secp::constants::PEDERSEN_COMMITMENT_SIZE;
}
impl PMMRable for OutputIdentifier {}
impl Writeable for OutputIdentifier {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
writer.write_u8(self.features.bits())?;
+16 -16
View File
@@ -23,6 +23,7 @@ use byteorder::{BigEndian, ByteOrder, ReadBytesExt};
use consensus;
use core::hash::{Hash, Hashed};
use keychain::{BlindingFactor, Identifier, IDENTIFIER_SIZE};
use std::fmt::Debug;
use std::io::{self, Read, Write};
use std::{cmp, error, fmt, mem};
use util::secp::constants::{
@@ -371,12 +372,12 @@ impl Readable for RangeProof {
}
}
impl PMMRable for RangeProof {
fn len() -> usize {
MAX_PROOF_SIZE + 8
}
impl FixedLength for RangeProof {
const LEN: usize = MAX_PROOF_SIZE + 8;
}
impl PMMRable for RangeProof {}
impl Readable for Signature {
fn read(reader: &mut Reader) -> Result<Signature, Error> {
let a = reader.read_fixed_bytes(AGG_SIGNATURE_SIZE)?;
@@ -535,31 +536,30 @@ impl Writeable for [u8; 4] {
}
}
/// Trait for types that can serialize and report their size
pub trait PMMRable: Readable + Writeable + Clone {
/// Length in bytes
fn len() -> usize;
/// Trait for types that serialize to a known fixed length.
pub trait FixedLength {
/// The length in bytes
const LEN: usize;
}
/// Trait for types that can be added to a "hash only" PMMR (block headers for example).
pub trait HashOnlyPMMRable: Writeable + Clone + Debug {}
/// Trait for types that can be added to a PMMR.
pub trait PMMRable: FixedLength + Readable + Writeable + Clone + Debug {}
/// Generic trait to ensure PMMR elements can be hashed with an index
pub trait PMMRIndexHashable {
/// Hash with a given index
fn hash_with_index(&self, index: u64) -> Hash;
}
impl<T: PMMRable> PMMRIndexHashable for T {
impl<T: Writeable> PMMRIndexHashable for T {
fn hash_with_index(&self, index: u64) -> Hash {
(index, self).hash()
}
}
// Convenient way to hash two existing hashes together with an index.
impl PMMRIndexHashable for (Hash, Hash) {
fn hash_with_index(&self, index: u64) -> Hash {
(index, &self.0, &self.1).hash()
}
}
/// Useful marker trait on types that can be sized byte slices
pub trait AsFixedBytes: Sized + AsRef<[u8]> {
/// The length in bytes