mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-30 09:37:55 +00:00
Drop ClientProvider (#5823)
This commit is contained in:
@@ -54,7 +54,7 @@ use sc_network::{NetworkService, network_state::NetworkState, PeerId, ReportHand
|
||||
use log::{log, warn, debug, error, Level};
|
||||
use codec::{Encode, Decode};
|
||||
use sp_runtime::generic::BlockId;
|
||||
use sp_runtime::traits::{NumberFor, Block as BlockT, BlockIdTo};
|
||||
use sp_runtime::traits::{NumberFor, Block as BlockT};
|
||||
use parity_util_mem::MallocSizeOf;
|
||||
use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
|
||||
|
||||
@@ -80,16 +80,11 @@ pub use sc_network::config::{FinalityProofProvider, OnDemand, BoxFinalityProofRe
|
||||
pub use sc_tracing::TracingReceiver;
|
||||
pub use task_manager::SpawnTaskHandle;
|
||||
use task_manager::TaskManager;
|
||||
use sp_blockchain::{HeaderBackend, HeaderMetadata, ProvideCache};
|
||||
use sp_api::{ProvideRuntimeApi, CallApiAt, ApiExt, ConstructRuntimeApi, ApiErrorExt};
|
||||
use sp_blockchain::{HeaderBackend, HeaderMetadata};
|
||||
use sp_api::{ApiExt, ConstructRuntimeApi, ApiErrorExt};
|
||||
use sc_client_api::{
|
||||
LockImportRun, Backend as BackendT, ProofProvider, ProvideUncles,
|
||||
StorageProvider, ExecutorProvider, Finalizer, AuxStore, Backend,
|
||||
BlockBackend, BlockchainEvents, CallExecutor, TransactionFor,
|
||||
UsageProvider,
|
||||
Backend as BackendT, BlockchainEvents, CallExecutor, UsageProvider,
|
||||
};
|
||||
use sc_block_builder::BlockBuilderProvider;
|
||||
use sp_consensus::{block_validation::Chain, BlockImport};
|
||||
use sp_block_builder::BlockBuilder;
|
||||
|
||||
const DEFAULT_PROTOCOL_ID: &str = "sup";
|
||||
@@ -131,85 +126,6 @@ pub struct Service<TBl, TCl, TSc, TNetStatus, TNet, TTxPool, TOc> {
|
||||
|
||||
impl<TBl, TCl, TSc, TNetStatus, TNet, TTxPool, TOc> Unpin for Service<TBl, TCl, TSc, TNetStatus, TNet, TTxPool, TOc> {}
|
||||
|
||||
/// Client super trait, use this instead of the concrete Client type.
|
||||
pub trait ClientProvider<
|
||||
Block: BlockT,
|
||||
Backend: BackendT<Block>,
|
||||
Executor: CallExecutor<Block>,
|
||||
Runtime: ConstructRuntimeApi<Block, Self>,
|
||||
>:
|
||||
HeaderBackend<Block>
|
||||
+ ProvideRuntimeApi<
|
||||
Block,
|
||||
Api = <Runtime as ConstructRuntimeApi<Block, Self>>::RuntimeApi
|
||||
>
|
||||
+ LockImportRun<Block, Backend>
|
||||
+ ProofProvider<Block>
|
||||
+ BlockBuilderProvider<Backend, Block, Self>
|
||||
+ ProvideUncles<Block>
|
||||
+ StorageProvider<Block, Backend>
|
||||
+ Chain<Block>
|
||||
+ HeaderMetadata<Block, Error = sp_blockchain::Error>
|
||||
+ ExecutorProvider<Block, Executor = Executor>
|
||||
+ ProvideCache<Block>
|
||||
+ BlockIdTo<Block, Error = sp_blockchain::Error>
|
||||
+ CallApiAt<
|
||||
Block,
|
||||
Error = sp_blockchain::Error,
|
||||
StateBackend = <Backend as BackendT<Block>>::State
|
||||
>
|
||||
+ BlockImport<
|
||||
Block,
|
||||
Error = sp_consensus::Error,
|
||||
Transaction = TransactionFor<Backend, Block>
|
||||
>
|
||||
+ Finalizer<Block, Backend>
|
||||
+ BlockchainEvents<Block>
|
||||
+ BlockBackend<Block>
|
||||
+ UsageProvider<Block>
|
||||
+ AuxStore
|
||||
{}
|
||||
|
||||
impl<Block, Backend, Executor, Runtime> ClientProvider<Block, Backend, Executor, Runtime>
|
||||
for
|
||||
Client<Backend, Executor, Block, Runtime>
|
||||
where
|
||||
Block: BlockT,
|
||||
Backend: BackendT<Block>,
|
||||
Executor: CallExecutor<Block>,
|
||||
Runtime: ConstructRuntimeApi<Block, Self>,
|
||||
Self: HeaderBackend<Block>
|
||||
+ ProvideRuntimeApi<
|
||||
Block,
|
||||
Api = <Runtime as ConstructRuntimeApi<Block, Self>>::RuntimeApi
|
||||
>
|
||||
+ LockImportRun<Block, Backend>
|
||||
+ ProofProvider<Block>
|
||||
+ BlockBuilderProvider<Backend, Block, Self>
|
||||
+ ProvideUncles<Block>
|
||||
+ StorageProvider<Block, Backend>
|
||||
+ Chain<Block>
|
||||
+ HeaderMetadata<Block, Error = sp_blockchain::Error>
|
||||
+ ExecutorProvider<Block, Executor = Executor>
|
||||
+ ProvideCache<Block>
|
||||
+ BlockIdTo<Block, Error = sp_blockchain::Error>
|
||||
+ CallApiAt<
|
||||
Block,
|
||||
Error = sp_blockchain::Error,
|
||||
StateBackend = <Backend as BackendT<Block>>::State
|
||||
>
|
||||
+ BlockImport<
|
||||
Block,
|
||||
Error = sp_consensus::Error,
|
||||
Transaction = TransactionFor<Backend, Block>
|
||||
>
|
||||
+ Finalizer<Block, Backend>
|
||||
+ BlockchainEvents<Block>
|
||||
+ BlockBackend<Block>
|
||||
+ UsageProvider<Block>
|
||||
+ AuxStore
|
||||
{}
|
||||
|
||||
/// Abstraction over a Substrate service.
|
||||
pub trait AbstractService: Future<Output = Result<(), Error>> + Send + Unpin + Spawn + 'static {
|
||||
/// Type of block of this chain.
|
||||
@@ -296,7 +212,7 @@ impl<TBl, TBackend, TExec, TRtApi, TSc, TExPool, TOc> AbstractService for
|
||||
NetworkService<TBl, TBl::Hash>, TExPool, TOc>
|
||||
where
|
||||
TBl: BlockT,
|
||||
TBackend: 'static + Backend<TBl>,
|
||||
TBackend: 'static + BackendT<TBl>,
|
||||
TExec: 'static + CallExecutor<TBl, Backend = TBackend> + Send + Sync + Clone,
|
||||
TRtApi: 'static + Send + Sync + ConstructRuntimeApi<TBl, Client<TBackend, TExec, TBl, TRtApi>>,
|
||||
<TRtApi as ConstructRuntimeApi<TBl, Client<TBackend, TExec, TBl, TRtApi>>>::RuntimeApi:
|
||||
|
||||
@@ -1,270 +0,0 @@
|
||||
// Copyright 2017-2020 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/>.
|
||||
|
||||
//! Tool for creating the genesis block.
|
||||
|
||||
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Hash as HashT, Zero};
|
||||
|
||||
/// Create a genesis block, given the initial storage.
|
||||
pub fn construct_genesis_block<
|
||||
Block: BlockT
|
||||
> (
|
||||
state_root: Block::Hash
|
||||
) -> Block {
|
||||
let extrinsics_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
|
||||
Vec::new(),
|
||||
);
|
||||
|
||||
Block::new(
|
||||
<<Block as BlockT>::Header as HeaderT>::new(
|
||||
Zero::zero(),
|
||||
extrinsics_root,
|
||||
state_root,
|
||||
Default::default(),
|
||||
Default::default()
|
||||
),
|
||||
Default::default()
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use codec::{Encode, Decode, Joiner};
|
||||
use sc_executor::native_executor_instance;
|
||||
use sp_state_machine::{
|
||||
StateMachine, OverlayedChanges, ExecutionStrategy,
|
||||
InMemoryBackend,
|
||||
};
|
||||
use substrate_test_runtime_client::{
|
||||
runtime::genesismap::{GenesisConfig, insert_genesis_block},
|
||||
runtime::{Hash, Transfer, Block, BlockNumber, Header, Digest},
|
||||
AccountKeyring, Sr25519Keyring,
|
||||
};
|
||||
use sp_runtime::traits::BlakeTwo256;
|
||||
use sp_core::tasks::executor as tasks_executor;
|
||||
use sp_core::offchain::storage::OffchainOverlayedChanges;
|
||||
use hex_literal::*;
|
||||
|
||||
native_executor_instance!(
|
||||
Executor,
|
||||
substrate_test_runtime_client::runtime::api::dispatch,
|
||||
substrate_test_runtime_client::runtime::native_version,
|
||||
);
|
||||
|
||||
fn executor() -> sc_executor::NativeExecutor<Executor> {
|
||||
sc_executor::NativeExecutor::new(sc_executor::WasmExecutionMethod::Interpreted, None, 8)
|
||||
}
|
||||
|
||||
fn construct_block(
|
||||
backend: &InMemoryBackend<BlakeTwo256>,
|
||||
number: BlockNumber,
|
||||
parent_hash: Hash,
|
||||
state_root: Hash,
|
||||
txs: Vec<Transfer>
|
||||
) -> (Vec<u8>, Hash) {
|
||||
use sp_trie::{TrieConfiguration, trie_types::Layout};
|
||||
|
||||
let transactions = txs.into_iter().map(|tx| tx.into_signed_tx()).collect::<Vec<_>>();
|
||||
|
||||
let iter = transactions.iter().map(Encode::encode);
|
||||
let extrinsics_root = Layout::<BlakeTwo256>::ordered_trie_root(iter).into();
|
||||
|
||||
let mut header = Header {
|
||||
parent_hash,
|
||||
number,
|
||||
state_root,
|
||||
extrinsics_root,
|
||||
digest: Digest { logs: vec![], },
|
||||
};
|
||||
let hash = header.hash();
|
||||
let mut overlay = OverlayedChanges::default();
|
||||
let mut offchain_overlay = OffchainOverlayedChanges::default();
|
||||
let backend_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&backend);
|
||||
let runtime_code = backend_runtime_code.runtime_code().expect("Code is part of the backend");
|
||||
|
||||
StateMachine::new(
|
||||
backend,
|
||||
sp_state_machine::disabled_changes_trie_state::<_, u64>(),
|
||||
&mut overlay,
|
||||
&mut offchain_overlay,
|
||||
&executor(),
|
||||
"Core_initialize_block",
|
||||
&header.encode(),
|
||||
Default::default(),
|
||||
&runtime_code,
|
||||
tasks_executor(),
|
||||
).execute(
|
||||
ExecutionStrategy::NativeElseWasm,
|
||||
).unwrap();
|
||||
|
||||
for tx in transactions.iter() {
|
||||
StateMachine::new(
|
||||
backend,
|
||||
sp_state_machine::disabled_changes_trie_state::<_, u64>(),
|
||||
&mut overlay,
|
||||
&mut offchain_overlay,
|
||||
&executor(),
|
||||
"BlockBuilder_apply_extrinsic",
|
||||
&tx.encode(),
|
||||
Default::default(),
|
||||
&runtime_code,
|
||||
tasks_executor(),
|
||||
).execute(
|
||||
ExecutionStrategy::NativeElseWasm,
|
||||
).unwrap();
|
||||
}
|
||||
|
||||
let ret_data = StateMachine::new(
|
||||
backend,
|
||||
sp_state_machine::disabled_changes_trie_state::<_, u64>(),
|
||||
&mut overlay,
|
||||
&mut offchain_overlay,
|
||||
&executor(),
|
||||
"BlockBuilder_finalize_block",
|
||||
&[],
|
||||
Default::default(),
|
||||
&runtime_code,
|
||||
tasks_executor(),
|
||||
).execute(
|
||||
ExecutionStrategy::NativeElseWasm,
|
||||
).unwrap();
|
||||
header = Header::decode(&mut &ret_data[..]).unwrap();
|
||||
|
||||
(vec![].and(&Block { header, extrinsics: transactions }), hash)
|
||||
}
|
||||
|
||||
fn block1(genesis_hash: Hash, backend: &InMemoryBackend<BlakeTwo256>) -> (Vec<u8>, Hash) {
|
||||
construct_block(
|
||||
backend,
|
||||
1,
|
||||
genesis_hash,
|
||||
hex!("25e5b37074063ab75c889326246640729b40d0c86932edc527bc80db0e04fe5c").into(),
|
||||
vec![Transfer {
|
||||
from: AccountKeyring::One.into(),
|
||||
to: AccountKeyring::Two.into(),
|
||||
amount: 69,
|
||||
nonce: 0,
|
||||
}]
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn construct_genesis_should_work_with_native() {
|
||||
let mut storage = GenesisConfig::new(
|
||||
None,
|
||||
vec![Sr25519Keyring::One.public().into(), Sr25519Keyring::Two.public().into()],
|
||||
vec![AccountKeyring::One.into(), AccountKeyring::Two.into()],
|
||||
1000,
|
||||
None,
|
||||
Default::default(),
|
||||
).genesis_map();
|
||||
let genesis_hash = insert_genesis_block(&mut storage);
|
||||
|
||||
let backend = InMemoryBackend::from(storage);
|
||||
let (b1data, _b1hash) = block1(genesis_hash, &backend);
|
||||
let backend_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&backend);
|
||||
let runtime_code = backend_runtime_code.runtime_code().expect("Code is part of the backend");
|
||||
|
||||
let mut overlay = OverlayedChanges::default();
|
||||
let mut offchain_overlay = OffchainOverlayedChanges::default();
|
||||
|
||||
let _ = StateMachine::new(
|
||||
&backend,
|
||||
sp_state_machine::disabled_changes_trie_state::<_, u64>(),
|
||||
&mut overlay,
|
||||
&mut offchain_overlay,
|
||||
&executor(),
|
||||
"Core_execute_block",
|
||||
&b1data,
|
||||
Default::default(),
|
||||
&runtime_code,
|
||||
tasks_executor(),
|
||||
).execute(
|
||||
ExecutionStrategy::NativeElseWasm,
|
||||
).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn construct_genesis_should_work_with_wasm() {
|
||||
let mut storage = GenesisConfig::new(None,
|
||||
vec![Sr25519Keyring::One.public().into(), Sr25519Keyring::Two.public().into()],
|
||||
vec![AccountKeyring::One.into(), AccountKeyring::Two.into()],
|
||||
1000,
|
||||
None,
|
||||
Default::default(),
|
||||
).genesis_map();
|
||||
let genesis_hash = insert_genesis_block(&mut storage);
|
||||
|
||||
let backend = InMemoryBackend::from(storage);
|
||||
let (b1data, _b1hash) = block1(genesis_hash, &backend);
|
||||
let backend_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&backend);
|
||||
let runtime_code = backend_runtime_code.runtime_code().expect("Code is part of the backend");
|
||||
|
||||
let mut overlay = OverlayedChanges::default();
|
||||
let mut offchain_overlay = OffchainOverlayedChanges::default();
|
||||
|
||||
let _ = StateMachine::new(
|
||||
&backend,
|
||||
sp_state_machine::disabled_changes_trie_state::<_, u64>(),
|
||||
&mut overlay,
|
||||
&mut offchain_overlay,
|
||||
&executor(),
|
||||
"Core_execute_block",
|
||||
&b1data,
|
||||
Default::default(),
|
||||
&runtime_code,
|
||||
tasks_executor(),
|
||||
).execute(
|
||||
ExecutionStrategy::AlwaysWasm,
|
||||
).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn construct_genesis_with_bad_transaction_should_panic() {
|
||||
let mut storage = GenesisConfig::new(None,
|
||||
vec![Sr25519Keyring::One.public().into(), Sr25519Keyring::Two.public().into()],
|
||||
vec![AccountKeyring::One.into(), AccountKeyring::Two.into()],
|
||||
68,
|
||||
None,
|
||||
Default::default(),
|
||||
).genesis_map();
|
||||
let genesis_hash = insert_genesis_block(&mut storage);
|
||||
|
||||
let backend = InMemoryBackend::from(storage);
|
||||
let (b1data, _b1hash) = block1(genesis_hash, &backend);
|
||||
let backend_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&backend);
|
||||
let runtime_code = backend_runtime_code.runtime_code().expect("Code is part of the backend");
|
||||
|
||||
let mut overlay = OverlayedChanges::default();
|
||||
let mut offchain_overlay = OffchainOverlayedChanges::default();
|
||||
|
||||
let r = StateMachine::new(
|
||||
&backend,
|
||||
sp_state_machine::disabled_changes_trie_state::<_, u64>(),
|
||||
&mut overlay,
|
||||
&mut offchain_overlay,
|
||||
&executor(),
|
||||
"Core_execute_block",
|
||||
&b1data,
|
||||
Default::default(),
|
||||
&runtime_code,
|
||||
tasks_executor(),
|
||||
).execute(
|
||||
ExecutionStrategy::NativeElseWasm,
|
||||
);
|
||||
assert!(r.is_err());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user