Permalink
Browse files

BlockId: Use Serialize + Deserialize

  • Loading branch information...
edolstra committed Jan 11, 2019
1 parent 0ffa0f8 commit 7fe2c72b4e7bf93f29d038a19dafc034ffc5f8da
Showing with 42 additions and 28 deletions.
  1. +16 −6 cardano/src/block/types.rs
  2. +1 −6 chain-core/src/property.rs
  3. +16 −7 chain-impl-mockchain/src/key.rs
  4. +9 −9 chain-storage-sqlite/src/lib.rs
@@ -86,15 +86,25 @@ impl FromStr for HeaderHash {
}
}

impl chain_core::property::BlockId for HeaderHash {
fn try_from_slice(slice: &[u8]) -> Option<Self> {
match TryFromSlice::try_from_slice(slice) {
Ok(x) => Some(x),
Err(_) => None,
}
impl chain_core::property::Serialize for HeaderHash {
type Error = std::io::Error;
fn serialize<W: std::io::Write>(&self, mut writer: W) -> Result<(), Self::Error> {
writer.write(self.0.as_hash_bytes())?;
Ok(())
}
}

impl chain_core::property::Deserialize for HeaderHash {
type Error = std::io::Error;
fn deserialize<R: std::io::BufRead>(mut reader: R) -> Result<Self, Self::Error> {
let mut buffer = [0; Blake2b256::HASH_SIZE];
reader.read_exact(&mut buffer)?;
Ok(HeaderHash(Blake2b256::from(buffer)))
}
}

impl chain_core::property::BlockId for HeaderHash { }

#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
pub struct BlockVersion(u16, u16, u8);
impl BlockVersion {
@@ -39,12 +39,7 @@ use std::fmt::Debug;
use std::hash::Hash;

/// Trait identifying the block identifier type.
pub trait BlockId: Eq + Ord + Clone + Debug + Hash + AsRef<[u8]> {
// FIXME: constant representing id length?

/// Construct a BlockId from a slice. FIXME: use TryFromSlice trait.
fn try_from_slice(slice: &[u8]) -> Option<Self>;
}
pub trait BlockId: Eq + Ord + Clone + Debug + Hash + Serialize + Deserialize {}

/// A trait representing block dates. Dates can be compared, ordered
/// and serialized as integers.
@@ -3,7 +3,6 @@
//!
use cardano::hash;
use cardano::hdwallet as crypto;
use cardano::util::try_from_slice::TryFromSlice;
use chain_core::property;

// TODO: this public key contains the chain code in it too
@@ -58,15 +57,25 @@ impl AsRef<[u8]> for Hash {
}
}

impl property::BlockId for Hash {
fn try_from_slice(slice: &[u8]) -> Option<Self> {
match hash::Blake2b256::try_from_slice(slice) {
Ok(x) => Some(Hash(x)),
Err(_) => None,
}
impl property::Serialize for Hash {
type Error = std::io::Error;
fn serialize<W: std::io::Write>(&self, mut writer: W) -> Result<(), Self::Error> {
writer.write(self.0.as_hash_bytes())?;
Ok(())
}
}

impl property::Deserialize for Hash {
type Error = std::io::Error;
fn deserialize<R: std::io::BufRead>(mut reader: R) -> Result<Self, Self::Error> {
let mut buffer = [0; hash::Blake2b256::HASH_SIZE];
reader.read_exact(&mut buffer)?;
Ok(Hash(hash::Blake2b256::from(buffer)))
}
}

impl property::BlockId for Hash { }

/// Cryptographic signature.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct Signature(pub crypto::Signature<()>);
@@ -2,7 +2,7 @@ extern crate chain_core;
extern crate chain_storage;
extern crate sqlite;

use chain_core::property::{Block, BlockDate, BlockId};
use chain_core::property::{Block, BlockDate, BlockId, Serialize};
use chain_storage::{
error::Error,
store::{BackLink, BlockInfo, BlockStore},
@@ -106,7 +106,7 @@ where
}

fn blob_to_hash<Id: BlockId>(blob: Vec<u8>) -> Id {
Id::try_from_slice(&blob[..]).unwrap()
Id::deserialize(&blob[..]).unwrap()
}

impl<B> Drop for SQLiteBlockStore<B>
@@ -134,7 +134,7 @@ where
let mut stmt_insert_block = self.stmt_insert_block.borrow_mut();
stmt_insert_block.reset().unwrap();
stmt_insert_block
.bind(1, &block_info.block_hash.as_ref()[..])
.bind(1, &block_info.block_hash.serialize_as_vec().unwrap()[..])
.unwrap();
stmt_insert_block
.bind(2, &block.serialize_as_vec().unwrap()[..])
@@ -144,7 +144,7 @@ where
let mut stmt_insert_block_info = self.stmt_insert_block_info.borrow_mut();
stmt_insert_block_info.reset().unwrap();
stmt_insert_block_info
.bind(1, &block_info.block_hash.as_ref()[..])
.bind(1, &block_info.block_hash.serialize_as_vec().unwrap()[..])
.unwrap();
stmt_insert_block_info
.bind(2, block_info.block_date.serialize() as i64)
@@ -158,15 +158,15 @@ where
.find(|x| x.distance == 1)
.unwrap();
stmt_insert_block_info
.bind(4, &parent.block_hash.as_ref()[..])
.bind(4, &parent.block_hash.serialize_as_vec().unwrap()[..])
.unwrap();
match block_info.back_links.iter().find(|x| x.distance != 1) {
Some(fast_link) => {
stmt_insert_block_info
.bind(5, fast_link.distance as i64)
.unwrap();
stmt_insert_block_info
.bind(6, &fast_link.block_hash.as_ref()[..])
.bind(6, &fast_link.block_hash.serialize_as_vec().unwrap()[..])
.unwrap();
}
None => {
@@ -183,7 +183,7 @@ where
let mut stmt_get_block = self.stmt_get_block.borrow_mut();
stmt_get_block.reset().unwrap();

stmt_get_block.bind(1, &block_hash.as_ref()[..]).unwrap();
stmt_get_block.bind(1, &block_hash.serialize_as_vec().unwrap()[..]).unwrap();

match stmt_get_block.next().unwrap() {
sqlite::State::Done => Err(Error::BlockNotFound),
@@ -199,7 +199,7 @@ where
stmt_get_block_info.reset().unwrap();

stmt_get_block_info
.bind(1, &block_hash.as_ref()[..])
.bind(1, &block_hash.serialize_as_vec().unwrap()[..])
.unwrap();

match stmt_get_block_info.next().unwrap() {
@@ -234,7 +234,7 @@ where
let mut stmt_put_tag = self.stmt_put_tag.borrow_mut();
stmt_put_tag.reset().unwrap();
stmt_put_tag.bind(1, tag_name).unwrap();
stmt_put_tag.bind(2, &block_hash.as_ref()[..]).unwrap();
stmt_put_tag.bind(2, &block_hash.serialize_as_vec().unwrap()[..]).unwrap();
stmt_put_tag.next().unwrap();
Ok(())
}

0 comments on commit 7fe2c72

Please sign in to comment.