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 all commits
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
14 changes: 7 additions & 7 deletions demo/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,19 @@ where
}

fn validators(&self, at: &BlockId) -> Result<Vec<AccountId>> {
self.call_api(at, "validators", &())
self.call_api_at(at, "validators", &())
}

fn random_seed(&self, at: &BlockId) -> Result<Hash> {
self.call_api(at, "random_seed", &())
self.call_api_at(at, "random_seed", &())
}

fn timestamp(&self, at: &BlockId) -> Result<Timestamp> {
self.call_api(at, "timestamp", &())
self.call_api_at(at, "timestamp", &())
}

fn evaluate_block(&self, at: &BlockId, block: Block) -> Result<bool> {
let res: Result<()> = self.call_api(at, "execute_block", &block);
let res: Result<()> = self.call_api_at(at, "execute_block", &block);
match res {
Ok(()) => Ok(true),
Err(err) => match err.kind() {
Expand All @@ -131,11 +131,11 @@ where
}

fn index(&self, at: &BlockId, account: AccountId) -> Result<Index> {
self.call_api(at, "account_nonce", &account)
self.call_api_at(at, "account_nonce", &account)
}

fn lookup(&self, at: &BlockId, address: Address) -> Result<Option<AccountId>> {
self.call_api(at, "lookup_address", &address)
self.call_api_at(at, "lookup_address", &address)
}

fn build_block(&self, at: &BlockId, inherent_data: InherentData) -> Result<Self::BlockBuilder> {
Expand All @@ -149,7 +149,7 @@ where

fn inherent_extrinsics(&self, at: &BlockId, inherent_data: InherentData) -> Result<Vec<UncheckedExtrinsic>> {
let runtime_version = self.runtime_version_at(at)?;
self.call_api(at, "inherent_extrinsics", &(inherent_data, runtime_version.spec_version))
self.call_api_at(at, "inherent_extrinsics", &(inherent_data, runtime_version.spec_version))
}
}

Binary file not shown.
Binary file not shown.
44 changes: 16 additions & 28 deletions substrate/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use primitives::{Blake2Hasher, RlpCodec};
use primitives::storage::{StorageKey, StorageData};
use codec::{Encode, Decode};
use state_machine::{
Ext, OverlayedChanges, Backend as StateBackend, CodeExecutor,
Backend as StateBackend, CodeExecutor,
ExecutionStrategy, ExecutionManager, prove_read
};

Expand All @@ -37,7 +37,7 @@ use blockchain::{self, Info as ChainInfo, Backend as ChainBackend, HeaderBackend
use call_executor::{CallExecutor, LocalCallExecutor};
use executor::{RuntimeVersion, RuntimeInfo};
use notifications::{StorageNotifications, StorageEventStream};
use {cht, error, in_mem, block_builder, runtime_io, bft, genesis};
use {cht, error, in_mem, block_builder, bft, genesis};

/// Type that implements `futures::Stream` of block import events.
pub type BlockchainEventStream<Block> = mpsc::UnboundedReceiver<BlockImportNotification<Block>>;
Expand Down Expand Up @@ -308,23 +308,6 @@ impl<B, E, Block> Client<B, E, Block> where
Ok((header, proof))
}

/// Set up the native execution environment to call into a native runtime code.
pub fn using_environment<F: FnOnce() -> T, T>(
&self, f: F
) -> error::Result<T> {
self.using_environment_at(&BlockId::Number(self.info()?.chain.best_number), &mut Default::default(), f)
}

/// Set up the native execution environment to call into a native runtime code.
pub fn using_environment_at<F: FnOnce() -> T, T>(
&self,
id: &BlockId<Block>,
overlay: &mut OverlayedChanges,
f: F
) -> error::Result<T> {
Ok(runtime_io::with_externalities(&mut Ext::new(overlay, &self.state_at(id)?), f))
}

/// Create a new block, built on the head of the chain.
pub fn new_block(&self) -> error::Result<block_builder::BlockBuilder<B, E, Block, Blake2Hasher, RlpCodec>>
where E: Clone
Expand All @@ -339,11 +322,16 @@ impl<B, E, Block> Client<B, E, Block> where
block_builder::BlockBuilder::at_block(parent, &self)
}

/// Set up the native execution environment to call into a native runtime code.
pub fn call_api<A, R>(&self, function: &'static str, args: &A) -> error::Result<R>
where A: Encode, R: Decode
{
self.call_api_at(&BlockId::Number(self.info()?.chain.best_number), function, args)
}

/// Call a runtime function at given block.
pub fn call_api<A, R>(&self, at: &BlockId<Block>, function: &'static str, args: &A) -> error::Result<R>
where
A: Encode,
R: Decode,
pub fn call_api_at<A, R>(&self, at: &BlockId<Block>, function: &'static str, args: &A) -> error::Result<R>
where A: Encode, R: Decode
{
let parent = at;
let header = <<Block as BlockT>::Header as HeaderT>::new(
Expand Down Expand Up @@ -686,15 +674,15 @@ mod tests {
use test_client::{self, TestClient};
use test_client::client::BlockOrigin;
use test_client::client::backend::Backend as TestBackend;
use test_client::{runtime as test_runtime, BlockBuilderExt};
use test_client::BlockBuilderExt;
use test_client::runtime::Transfer;

#[test]
fn client_initialises_from_genesis_ok() {
let client = test_client::new();

assert_eq!(client.using_environment(|| test_runtime::system::balance_of(Keyring::Alice.to_raw_public().into())).unwrap(), 1000);
assert_eq!(client.using_environment(|| test_runtime::system::balance_of(Keyring::Ferdie.to_raw_public().into())).unwrap(), 0);
assert_eq!(client.call_api::<_, u64>("balance_of", &Keyring::Alice.to_raw_public()).unwrap(), 1000);
assert_eq!(client.call_api::<_, u64>("balance_of", &Keyring::Ferdie.to_raw_public()).unwrap(), 0);
}

#[test]
Expand Down Expand Up @@ -737,8 +725,8 @@ mod tests {

assert_eq!(client.info().unwrap().chain.best_number, 1);
assert!(client.state_at(&BlockId::Number(1)).unwrap() != client.state_at(&BlockId::Number(0)).unwrap());
assert_eq!(client.using_environment(|| test_runtime::system::balance_of(Keyring::Alice.to_raw_public().into())).unwrap(), 958);
assert_eq!(client.using_environment(|| test_runtime::system::balance_of(Keyring::Ferdie.to_raw_public().into())).unwrap(), 42);
assert_eq!(client.call_api::<_, u64>("balance_of", &Keyring::Alice.to_raw_public()).unwrap(), 958);
assert_eq!(client.call_api::<_, u64>("balance_of", &Keyring::Ferdie.to_raw_public()).unwrap(), 42);
}

#[test]
Expand Down
3 changes: 2 additions & 1 deletion substrate/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ pub mod api {
initialise_block => |header| system::initialise_block(header),
execute_block => |block| system::execute_block(block),
apply_extrinsic => |utx| system::execute_transaction(utx),
finalise_block => |()| system::finalise_block()
finalise_block => |()| system::finalise_block(),
balance_of => |a| system::balance_of(a)
);
}
Binary file not shown.
Binary file not shown.