This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Block builder (substrate) * Fix wasm build * Bulid on any block * Test for block builder. * Block import tests for client. * Tidy ups * clean up block builder instantiation * Propert block generation for tests * Fixed rpc tests
- Loading branch information
Showing
25 changed files
with
422 additions
and
136 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file modified
BIN
-4.36 KB
(94%)
polkadot/runtime/wasm/target/wasm32-unknown-unknown/release/polkadot_runtime.compact.wasm
Binary file not shown.
Binary file modified
BIN
-4.39 KB
(94%)
polkadot/runtime/wasm/target/wasm32-unknown-unknown/release/polkadot_runtime.wasm
Binary file not shown.
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
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
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,90 @@ | ||
// Copyright 2017 Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate. | ||
|
||
// Substrate 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. | ||
|
||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
//! Utility struct to build a block. | ||
use std::vec::Vec; | ||
use codec::{Joiner, Slicable}; | ||
use state_machine::{self, CodeExecutor}; | ||
use primitives::{Header, Block}; | ||
use primitives::block::Transaction; | ||
use {backend, error, BlockId, Client}; | ||
use triehash::ordered_trie_root; | ||
|
||
/// Utility for building new (valid) blocks from a stream of transactions. | ||
pub struct BlockBuilder<B, E> where | ||
B: backend::Backend, | ||
E: CodeExecutor + Clone, | ||
error::Error: From<<<B as backend::Backend>::State as state_machine::backend::Backend>::Error>, | ||
{ | ||
header: Header, | ||
transactions: Vec<Transaction>, | ||
executor: E, | ||
state: B::State, | ||
changes: state_machine::OverlayedChanges, | ||
} | ||
|
||
impl<B, E> BlockBuilder<B, E> where | ||
B: backend::Backend, | ||
E: CodeExecutor + Clone, | ||
error::Error: From<<<B as backend::Backend>::State as state_machine::backend::Backend>::Error>, | ||
{ | ||
/// Create a new instance of builder from the given client, building on the latest block. | ||
pub fn new(client: &Client<B, E>) -> error::Result<Self> { | ||
client.info().and_then(|i| Self::at_block(&BlockId::Hash(i.chain.best_hash), client)) | ||
} | ||
|
||
/// Create a new instance of builder from the given client using a particular block's ID to | ||
/// build upon. | ||
pub fn at_block(block_id: &BlockId, client: &Client<B, E>) -> error::Result<Self> { | ||
Ok(BlockBuilder { | ||
header: Header { | ||
number: client.block_number_from_id(block_id)?.ok_or(error::ErrorKind::UnknownBlock(*block_id))? + 1, | ||
parent_hash: client.block_hash_from_id(block_id)?.ok_or(error::ErrorKind::UnknownBlock(*block_id))?, | ||
state_root: Default::default(), | ||
transaction_root: Default::default(), | ||
digest: Default::default(), | ||
}, | ||
transactions: Default::default(), | ||
executor: client.clone_executor(), | ||
state: client.state_at(block_id)?, | ||
changes: Default::default(), | ||
}) | ||
} | ||
|
||
/// Push a transaction onto the block's list of transactions. This will ensure the transaction | ||
/// can be validly executed (by executing it); if it is invalid, it'll be returned along with | ||
/// the error. Otherwise, it will return a mutable reference to self (in order to chain). | ||
pub fn push(&mut self, tx: Transaction) -> error::Result<()> { | ||
let output = state_machine::execute(&self.state, &mut self.changes, &self.executor, "execute_transaction", | ||
&vec![].and(&self.header).and(&tx))?; | ||
self.header = Header::decode(&mut &output[..]).expect("Header came straight out of runtime so must be valid"); | ||
self.transactions.push(tx); | ||
Ok(()) | ||
} | ||
|
||
/// Consume the builder to return a valid `Block` containing all pushed transactions. | ||
pub fn bake(mut self) -> error::Result<Block> { | ||
self.header.transaction_root = ordered_trie_root(self.transactions.iter().map(Slicable::encode)).0.into(); | ||
let output = state_machine::execute(&self.state, &mut self.changes, &self.executor, "finalise_block", | ||
&self.header.encode())?; | ||
self.header = Header::decode(&mut &output[..]).expect("Header came straight out of runtime so must be valid"); | ||
Ok(Block { | ||
header: self.header, | ||
transactions: self.transactions, | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.