Skip to content
This repository has been archived by the owner on Jun 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #384 from input-output-hk/mockchain-block
Browse files Browse the repository at this point in the history
Introduces SignedBlock for the mockchain.
  • Loading branch information
NicolasDP committed Jan 10, 2019
2 parents 2f8d57f + ef17b3f commit 7ba6f46
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
98 changes: 98 additions & 0 deletions chain-impl-mockchain/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Representation of the block in the mockchain.
use crate::key::*;
use crate::transaction::*;
use bincode;
use chain_core::property;

/// Non unique identifier of the transaction position in the
Expand Down Expand Up @@ -75,12 +76,99 @@ impl property::HasTransaction<SignedTransaction> for Block {
}
}

/// `Block` is an element of the blockchain it contains multiple
/// transaction and a reference to the parent block. Alongside
/// with the position of that block in the chain.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct SignedBlock {
/// Internal block.
block: Block,
/// Public key used to sign the block.
public_key: PublicKey,
/// List of cryptographic signatures that verifies the block.
signature: Signature,
}

impl SignedBlock {
/// Create a new signed block.
pub fn new(block: Block, pkey: PrivateKey) -> Self {
use chain_core::property::Block;
let block_id = block.id();
SignedBlock {
block: block,
public_key: pkey.public(),
signature: pkey.sign(block_id.as_ref()),
}
}

/// Verify if block is correctly signed by the key.
/// Return `false` if there is no such signature or
/// if it can't be verified.
pub fn verify(&self) -> bool {
use chain_core::property::Block;
let block_id = self.block.id();
self.public_key.verify(block_id.as_ref(), &self.signature)
}
}

impl property::Serialize for SignedBlock {
type Error = bincode::Error;

fn serialize<W: std::io::Write>(&self, writer: W) -> Result<(), Self::Error> {
bincode::serialize_into(writer, self)
}
}

impl property::Deserialize for SignedBlock {
type Error = bincode::Error;

fn deserialize<R: std::io::Read>(reader: R) -> Result<Self, bincode::Error> {
bincode::deserialize_from(reader)
}
}

impl property::Block for SignedBlock {
type Id = <Block as property::Block>::Id;
type Date = <Block as property::Block>::Date;
type Header = <Block as property::Block>::Header;

/// Identifier of the block, currently the hash of the
/// serialized transaction.
fn id(&self) -> Self::Id {
self.block.id()
}

fn header(&self) -> Self::Header {
self.block.header()
}

/// Id of the parent block.
fn parent_id(&self) -> Self::Id {
self.block.parent_id()
}

/// Date of the block.
fn date(&self) -> Self::Date {
self.block.date()
}
}

#[cfg(test)]
mod test {

use super::*;
use quickcheck::{Arbitrary, Gen};

quickcheck! {
fn block_serialization_bijection(b: Block) -> bool {
property::testing::serialization_bijection(b)
}

fn signed_block_serialization_bijection(b: SignedBlock) -> bool {
property::testing::serialization_bijection(b)
}
}

impl Arbitrary for Block {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
Block {
Expand All @@ -91,6 +179,16 @@ mod test {
}
}

impl Arbitrary for SignedBlock {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
SignedBlock {
block: Arbitrary::arbitrary(g),
public_key: Arbitrary::arbitrary(g),
signature: Arbitrary::arbitrary(g),
}
}
}

impl Arbitrary for SlotId {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
SlotId(Arbitrary::arbitrary(g), Arbitrary::arbitrary(g))
Expand Down
2 changes: 1 addition & 1 deletion chain-impl-mockchain/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct Environment {
impl Environment {
/// Create new environment.
pub fn new() -> Self {
let g = StdGen::new(thread_rng(), 10);
let g = StdGen::new(rand::thread_rng(), 10);
Environment {
ledger: Ledger::new(HashMap::new()),
gen: g,
Expand Down

0 comments on commit 7ba6f46

Please sign in to comment.