This repository was archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #562 from ethcore/chain_generator
Finished blockchain generator.
- Loading branch information
Showing
8 changed files
with
294 additions
and
131 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2015, 2016 Ethcore (UK) Ltd. | ||
// This file is part of Parity. | ||
|
||
// Parity is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Parity is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Parity. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use util::rlp::*; | ||
use util::{H256, H2048}; | ||
use util::U256; | ||
use util::bytes::Bytes; | ||
use header::Header; | ||
use transaction::SignedTransaction; | ||
|
||
use super::fork::Forkable; | ||
use super::bloom::WithBloom; | ||
use super::complete::CompleteBlock; | ||
|
||
/// Helper structure, used for encoding blocks. | ||
#[derive(Default)] | ||
pub struct Block { | ||
pub header: Header, | ||
pub transactions: Vec<SignedTransaction>, | ||
pub uncles: Vec<Header> | ||
} | ||
|
||
impl Encodable for Block { | ||
fn rlp_append(&self, s: &mut RlpStream) { | ||
s.begin_list(3); | ||
s.append(&self.header); | ||
s.append(&self.transactions); | ||
s.append(&self.uncles); | ||
} | ||
} | ||
|
||
impl Forkable for Block { | ||
fn fork(mut self, fork_number: usize) -> Self where Self: Sized { | ||
self.header.difficulty = self.header.difficulty - U256::from(fork_number); | ||
self | ||
} | ||
} | ||
|
||
impl WithBloom for Block { | ||
fn with_bloom(mut self, bloom: H2048) -> Self where Self: Sized { | ||
self.header.log_bloom = bloom; | ||
self | ||
} | ||
} | ||
|
||
impl CompleteBlock for Block { | ||
fn complete(mut self, parent_hash: H256) -> Bytes { | ||
self.header.parent_hash = parent_hash; | ||
encode(&self).to_vec() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2015, 2016 Ethcore (UK) Ltd. | ||
// This file is part of Parity. | ||
|
||
// Parity is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Parity is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Parity. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use util::hash::H2048; | ||
|
||
pub trait WithBloom { | ||
fn with_bloom(self, bloom: H2048) -> Self where Self: Sized; | ||
} | ||
|
||
pub struct Bloom<'a, I> where I: 'a { | ||
pub iter: &'a mut I, | ||
pub bloom: H2048, | ||
} | ||
|
||
impl<'a, I> Iterator for Bloom<'a, I> where I: Iterator, <I as Iterator>::Item: WithBloom { | ||
type Item = <I as Iterator>::Item; | ||
|
||
#[inline] | ||
fn next(&mut self) -> Option<Self::Item> { | ||
self.iter.next().map(|item| item.with_bloom(self.bloom.clone())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2015, 2016 Ethcore (UK) Ltd. | ||
// This file is part of Parity. | ||
|
||
// Parity is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Parity is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Parity. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use util::hash::H256; | ||
use util::bytes::Bytes; | ||
use util::sha3::Hashable; | ||
use views::BlockView; | ||
|
||
#[derive(Default, Clone)] | ||
pub struct BlockFinalizer { | ||
parent_hash: H256 | ||
} | ||
|
||
impl BlockFinalizer { | ||
pub fn fork(&self) -> Self { | ||
self.clone() | ||
} | ||
} | ||
|
||
pub trait CompleteBlock { | ||
fn complete(self, parent_hash: H256) -> Bytes; | ||
} | ||
|
||
pub struct Complete<'a, I> where I: 'a { | ||
pub iter: &'a mut I, | ||
pub finalizer: &'a mut BlockFinalizer, | ||
} | ||
|
||
impl<'a, I> Iterator for Complete<'a, I> where I: Iterator, <I as Iterator>::Item: CompleteBlock { | ||
type Item = Bytes; | ||
|
||
#[inline] | ||
fn next(&mut self) -> Option<Self::Item> { | ||
self.iter.next().map(|item| { | ||
let rlp = item.complete(self.finalizer.parent_hash.clone()); | ||
self.finalizer.parent_hash = BlockView::new(&rlp).header_view().sha3(); | ||
rlp | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2015, 2016 Ethcore (UK) Ltd. | ||
// This file is part of Parity. | ||
|
||
// Parity is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Parity is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Parity. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
pub trait Forkable { | ||
fn fork(self, fork_number: usize) -> Self where Self: Sized; | ||
} | ||
|
||
pub struct Fork<I> { | ||
pub iter: I, | ||
pub fork_number: usize, | ||
} | ||
|
||
impl<I> Clone for Fork<I> where I: Iterator + Clone { | ||
fn clone(&self) -> Self { | ||
Fork { | ||
iter: self.iter.clone(), | ||
fork_number: self.fork_number | ||
} | ||
} | ||
} | ||
|
||
impl<I> Iterator for Fork<I> where I: Iterator, <I as Iterator>::Item: Forkable { | ||
type Item = <I as Iterator>::Item; | ||
|
||
#[inline] | ||
fn next(&mut self) -> Option<Self::Item> { | ||
self.iter.next().map(|item| item.fork(self.fork_number)) | ||
} | ||
} |
Oops, something went wrong.