Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Expose BlockOrigin and clean up the API.
  • Loading branch information
tomusdrw committed Apr 19, 2018
commit e69a2d24b40c0a5cf20e9aef664d94a836a5709e
5 changes: 3 additions & 2 deletions substrate/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ mod tests {
use keyring::Keyring;
use primitives::block::Extrinsic as PrimitiveExtrinsic;
use test_client::{self, TestClient};
use test_client::client::BlockOrigin;
use test_client::runtime as test_runtime;
use test_client::runtime::{UncheckedTransaction, Transaction};

Expand Down Expand Up @@ -485,7 +486,7 @@ mod tests {

let builder = client.new_block().unwrap();

client.import_own_block(builder).unwrap();
client.justify_and_import(BlockOrigin::Own, builder.bake().unwrap()).unwrap();

assert_eq!(client.info().unwrap().chain.best_number, 1);
assert_eq!(client.using_environment(|| test_runtime::system::latest_block_hash()).unwrap(), client.block_hash(1).unwrap().unwrap());
Expand Down Expand Up @@ -514,7 +515,7 @@ mod tests {
nonce: 0
}.signed()).unwrap();

client.import_own_block(builder).unwrap();
client.justify_and_import(BlockOrigin::Own, builder.bake().unwrap()).unwrap();

assert_eq!(client.info().unwrap().chain.best_number, 1);
assert!(client.state_at(&BlockId::Number(1)).unwrap() != client.state_at(&BlockId::Number(0)).unwrap());
Expand Down
7 changes: 4 additions & 3 deletions substrate/client/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ mod tests {
use codec::{Slicable, Joiner};
use runtime_support::Hashable;
use keyring::Keyring;
use test_runtime::genesismap::{GenesisConfig, additional_storage_with_genesis};
use executor::WasmExecutor;
use state_machine::{execute, OverlayedChanges};
use state_machine::backend::InMemory;
use test_runtime::{self, Hash, Block, BlockNumber, Header, Digest, Transaction,
use test_client;
use test_client::runtime::genesismap::{GenesisConfig, additional_storage_with_genesis};
use test_client::runtime::{Hash, Block, BlockNumber, Header, Digest, Transaction,
UncheckedTransaction};
use ed25519::{Public, Pair};

native_executor_instance!(Executor, test_runtime::api::dispatch, include_bytes!("../../test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm"));
native_executor_instance!(Executor, test_client::runtime::api::dispatch, include_bytes!("../../test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm"));

fn construct_block(backend: &InMemory, number: BlockNumber, parent_hash: Hash, state_root: Hash, txs: Vec<Transaction>) -> (Vec<u8>, Hash) {
use triehash::ordered_trie_root;
Expand Down
2 changes: 1 addition & 1 deletion substrate/network/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl Peer {
edit_block(&mut builder);
let block = builder.bake().unwrap();
trace!("Generating {}, (#{})", primitives::block::HeaderHash::from(block.header.blake2_256()), block.header.number);
self.client.import_file_block(block).unwrap();
self.client.justify_and_import(client::BlockOrigin::File, block).unwrap();
}
}

Expand Down
3 changes: 2 additions & 1 deletion substrate/rpc/src/chain/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use super::*;
use jsonrpc_macros::pubsub;
use client::BlockOrigin;
use test_client::{self, TestClient};

#[test]
Expand Down Expand Up @@ -63,7 +64,7 @@ fn should_notify_about_latest_block() {
assert_eq!(core.run(id), Ok(Ok(SubscriptionId::Number(0))));

let builder = api.client.new_block().unwrap();
api.client.import_own_block(builder).unwrap();
api.client.justify_and_import(BlockOrigin::Own, builder.bake().unwrap()).unwrap();
}

// assert notification send to transport
Expand Down
22 changes: 4 additions & 18 deletions substrate/test-client/src/client_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

use codec::Slicable;
use client::{self, Client};
use client::block_builder::BlockBuilder;
use keyring::Keyring;
use runtime_support::Hashable;
use runtime::genesismap::{GenesisConfig, additional_storage_with_genesis};
Expand All @@ -31,11 +30,8 @@ pub trait TestClient {
/// Crates new client instance for tests.
fn new_for_tests() -> Self;

/// Bakes the block from the builder, fake-justifies it and imports to the chain.
fn import_own_block(&self, builder: BlockBuilder<Backend, Executor>) -> client::error::Result<()>;

/// Imports pre-baked block with `BlockOrigin::File`
fn import_file_block(&self, block: block::Block) -> client::error::Result<()>;
/// Justify and import block to the chain.
fn justify_and_import(&self, origin: client::BlockOrigin, block: block::Block) -> client::error::Result<()>;

/// Returns hash of the genesis block.
fn genesis_hash(&self) -> block::HeaderHash;
Expand All @@ -46,20 +42,10 @@ impl TestClient for Client<Backend, Executor> {
client::new_in_mem(NativeExecutor::new(), prepare_genesis).unwrap()
}

fn import_own_block(&self, builder: BlockBuilder<Backend, Executor>) -> client::error::Result<()> {
let block = builder.bake()?;

let justification = fake_justify(&block.header);
let justified = self.check_justification(block.header, justification)?;
self.import_block(client::BlockOrigin::Own, justified, Some(block.transactions))?;

Ok(())
}

fn import_file_block(&self, block: block::Block) -> client::error::Result<()> {
fn justify_and_import(&self, origin: client::BlockOrigin, block: block::Block) -> client::error::Result<()> {
let justification = fake_justify(&block.header);
let justified = self.check_justification(block.header, justification)?;
self.import_block(client::BlockOrigin::File, justified, Some(block.transactions))?;
self.import_block(origin, justified, Some(block.transactions))?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion substrate/test-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
#![warn(missing_docs)]

extern crate substrate_bft as bft;
extern crate substrate_client as client;
extern crate substrate_codec as codec;
extern crate substrate_keyring as keyring;
extern crate substrate_primitives as primitives;
extern crate substrate_runtime_support as runtime_support;
#[macro_use] extern crate substrate_executor as executor;

pub extern crate substrate_test_runtime as runtime;
pub extern crate substrate_client as client;

mod client_ext;

Expand Down