mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-20 03:31:03 +00:00
Offchain execution extensions (#4145)
* Pass Extensions instead of individual objects. * Move TransactionPool to a separate ExternalitiesExtension. * Fix compilation.? * Clean up. * Refactor testing utilities. * Add docs, fix tests. * Fix doctest. * Fix formatting and add some logs. * Add some docs. * Remove unused files.
This commit is contained in:
committed by
Gavin Wood
parent
f000392cc0
commit
86b6ac5571
@@ -5,13 +5,13 @@ authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
client-api = { package = "substrate-client-api", path = "api" }
|
||||
block-builder = { package = "substrate-block-builder", path = "block-builder" }
|
||||
client-api = { package = "substrate-client-api", path = "api" }
|
||||
codec = { package = "parity-scale-codec", version = "1.0.0", features = ["derive"] }
|
||||
consensus = { package = "substrate-consensus-common", path = "../primitives/consensus/common" }
|
||||
kvdb = { git = "https://github.com/paritytech/parity-common", rev="b0317f649ab2c665b7987b8475878fc4d2e1f81d" }
|
||||
derive_more = { version = "0.15.0" }
|
||||
executor = { package = "substrate-executor", path = "executor" }
|
||||
externalities = { package = "substrate-externalities", path = "../primitives/externalities" }
|
||||
fnv = { version = "1.0.6" }
|
||||
futures = { version = "0.3.1", features = ["compat"] }
|
||||
hash-db = { version = "0.15.2" }
|
||||
@@ -19,6 +19,7 @@ header-metadata = { package = "substrate-header-metadata", path = "header-metada
|
||||
hex-literal = { version = "0.2.1" }
|
||||
inherents = { package = "substrate-inherents", path = "../primitives/inherents" }
|
||||
keyring = { package = "substrate-keyring", path = "../primitives/keyring" }
|
||||
kvdb = { git = "https://github.com/paritytech/parity-common", rev="b0317f649ab2c665b7987b8475878fc4d2e1f81d" }
|
||||
log = { version = "0.4.8" }
|
||||
parking_lot = { version = "0.9.0" }
|
||||
primitives = { package = "substrate-primitives", path = "../primitives/core" }
|
||||
|
||||
@@ -10,6 +10,7 @@ codec = { package = "parity-scale-codec", version = "1.0.0", default-features =
|
||||
consensus = { package = "substrate-consensus-common", path = "../../primitives/consensus/common" }
|
||||
derive_more = { version = "0.15.0" }
|
||||
executor = { package = "substrate-executor", path = "../executor" }
|
||||
externalities = { package = "substrate-externalities", path = "../../primitives/externalities" }
|
||||
fnv = { version = "1.0.6" }
|
||||
futures = { version = "0.3.1" }
|
||||
hash-db = { version = "0.15.2", default-features = false }
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
use std::sync::Arc;
|
||||
use std::collections::HashMap;
|
||||
use primitives::ChangesTrieConfiguration;
|
||||
use primitives::offchain::OffchainStorage;
|
||||
use sr_primitives::{generic::BlockId, Justification, StorageOverlay, ChildrenStorageOverlay};
|
||||
use sr_primitives::traits::{Block as BlockT, NumberFor};
|
||||
use state_machine::backend::Backend as StateBackend;
|
||||
@@ -27,7 +28,6 @@ use crate::{
|
||||
blockchain::{
|
||||
Backend as BlockchainBackend, well_known_cache_keys
|
||||
},
|
||||
offchain::OffchainStorage,
|
||||
error,
|
||||
light::RemoteBlockchain,
|
||||
};
|
||||
@@ -41,12 +41,22 @@ pub type StorageCollection = Vec<(Vec<u8>, Option<Vec<u8>>)>;
|
||||
/// In memory arrays of storage values for multiple child tries.
|
||||
pub type ChildStorageCollection = Vec<(Vec<u8>, StorageCollection)>;
|
||||
|
||||
/// Import operation summary.
|
||||
///
|
||||
/// Contains information about the block that just got imported,
|
||||
/// including storage changes, reorged blocks, etc.
|
||||
pub struct ImportSummary<Block: BlockT> {
|
||||
/// Block hash of the imported block.
|
||||
pub hash: Block::Hash,
|
||||
/// Import origin.
|
||||
pub origin: BlockOrigin,
|
||||
/// Header of the imported block.
|
||||
pub header: Block::Header,
|
||||
/// Is this block a new best block.
|
||||
pub is_new_best: bool,
|
||||
/// Optional storage changes.
|
||||
pub storage_changes: Option<(StorageCollection, ChildStorageCollection)>,
|
||||
/// Blocks that got retracted because of this one got imported.
|
||||
pub retracted: Vec<Block::Hash>,
|
||||
}
|
||||
|
||||
@@ -56,8 +66,11 @@ pub struct ClientImportOperation<
|
||||
H: Hasher<Out=Block::Hash>,
|
||||
B: Backend<Block, H>,
|
||||
> {
|
||||
/// DB Operation.
|
||||
pub op: B::BlockImportOperation,
|
||||
/// Summary of imported block.
|
||||
pub notify_imported: Option<ImportSummary<Block>>,
|
||||
/// A list of hashes of blocks that got finalized.
|
||||
pub notify_finalized: Vec<Block::Hash>,
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! A method call executor interface.
|
||||
|
||||
use std::{cmp::Ord, panic::UnwindSafe, result, cell::RefCell};
|
||||
use codec::{Encode, Decode};
|
||||
use sr_primitives::{
|
||||
@@ -24,8 +26,9 @@ use state_machine::{
|
||||
ChangesTrieTransaction, StorageProof,
|
||||
};
|
||||
use executor::{RuntimeVersion, NativeVersion};
|
||||
use externalities::Extensions;
|
||||
use hash_db::Hasher;
|
||||
use primitives::{offchain::OffchainExt, Blake2Hasher, NativeOrEncoded};
|
||||
use primitives::{Blake2Hasher, NativeOrEncoded};
|
||||
|
||||
use sr_api::{ProofRecorder, InitializeBlock};
|
||||
use crate::error;
|
||||
@@ -49,7 +52,7 @@ where
|
||||
method: &str,
|
||||
call_data: &[u8],
|
||||
strategy: ExecutionStrategy,
|
||||
side_effects_handler: Option<OffchainExt>,
|
||||
extensions: Option<Extensions>,
|
||||
) -> Result<Vec<u8>, error::Error>;
|
||||
|
||||
/// Execute a contextual call on top of state in a block of a given hash.
|
||||
@@ -76,9 +79,8 @@ where
|
||||
initialize_block: InitializeBlock<'a, B>,
|
||||
execution_manager: ExecutionManager<EM>,
|
||||
native_call: Option<NC>,
|
||||
side_effects_handler: Option<OffchainExt>,
|
||||
proof_recorder: &Option<ProofRecorder<B>>,
|
||||
enable_keystore: bool,
|
||||
extensions: Option<Extensions>,
|
||||
) -> error::Result<NativeOrEncoded<R>> where ExecutionManager<EM>: Clone;
|
||||
|
||||
/// Extract RuntimeVersion of given block
|
||||
@@ -104,7 +106,7 @@ where
|
||||
call_data: &[u8],
|
||||
manager: ExecutionManager<F>,
|
||||
native_call: Option<NC>,
|
||||
side_effects_handler: Option<OffchainExt>,
|
||||
extensions: Option<Extensions>,
|
||||
) -> Result<
|
||||
(
|
||||
NativeOrEncoded<R>,
|
||||
|
||||
@@ -14,10 +14,11 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! A set of APIs supported by the client along with their primitives.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use futures::channel::mpsc;
|
||||
use primitives::storage::StorageKey;
|
||||
use state_machine::ExecutionStrategy;
|
||||
use sr_primitives::{
|
||||
traits::{Block as BlockT, NumberFor},
|
||||
generic::BlockId
|
||||
@@ -39,33 +40,6 @@ pub type FinalityNotifications<Block> = mpsc::UnboundedReceiver<FinalityNotifica
|
||||
/// This may be used as chain spec extension to filter out known, unwanted forks.
|
||||
pub type ForkBlocks<Block> = Option<HashMap<NumberFor<Block>, <Block as BlockT>::Hash>>;
|
||||
|
||||
/// Execution strategies settings.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ExecutionStrategies {
|
||||
/// Execution strategy used when syncing.
|
||||
pub syncing: ExecutionStrategy,
|
||||
/// Execution strategy used when importing blocks.
|
||||
pub importing: ExecutionStrategy,
|
||||
/// Execution strategy used when constructing blocks.
|
||||
pub block_construction: ExecutionStrategy,
|
||||
/// Execution strategy used for offchain workers.
|
||||
pub offchain_worker: ExecutionStrategy,
|
||||
/// Execution strategy used in other cases.
|
||||
pub other: ExecutionStrategy,
|
||||
}
|
||||
|
||||
impl Default for ExecutionStrategies {
|
||||
fn default() -> ExecutionStrategies {
|
||||
ExecutionStrategies {
|
||||
syncing: ExecutionStrategy::NativeElseWasm,
|
||||
importing: ExecutionStrategy::NativeElseWasm,
|
||||
block_construction: ExecutionStrategy::AlwaysWasm,
|
||||
offchain_worker: ExecutionStrategy::NativeWhenPossible,
|
||||
other: ExecutionStrategy::NativeElseWasm,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Figure out the block type for a given type (for now, just a `Client`).
|
||||
pub trait BlockOf {
|
||||
/// The type of the block.
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
// Copyright 2019 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/>.
|
||||
|
||||
//! Execution extensions for runtime calls.
|
||||
//!
|
||||
//! This module is responsible for defining the execution
|
||||
//! strategy for the runtime calls and provide the right `Externalities`
|
||||
//! extensions to support APIs for particular execution context & capabilities.
|
||||
|
||||
use std::sync::{Weak, Arc};
|
||||
use codec::Decode;
|
||||
use primitives::{
|
||||
ExecutionContext,
|
||||
offchain::{self, OffchainExt, TransactionPoolExt},
|
||||
traits::{BareCryptoStorePtr, KeystoreExt},
|
||||
};
|
||||
use sr_primitives::{
|
||||
generic::BlockId,
|
||||
traits,
|
||||
offchain::{TransactionPool},
|
||||
};
|
||||
use state_machine::{ExecutionStrategy, ExecutionManager, DefaultHandler};
|
||||
use externalities::Extensions;
|
||||
use parking_lot::RwLock;
|
||||
|
||||
/// Execution strategies settings.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ExecutionStrategies {
|
||||
/// Execution strategy used when syncing.
|
||||
pub syncing: ExecutionStrategy,
|
||||
/// Execution strategy used when importing blocks.
|
||||
pub importing: ExecutionStrategy,
|
||||
/// Execution strategy used when constructing blocks.
|
||||
pub block_construction: ExecutionStrategy,
|
||||
/// Execution strategy used for offchain workers.
|
||||
pub offchain_worker: ExecutionStrategy,
|
||||
/// Execution strategy used in other cases.
|
||||
pub other: ExecutionStrategy,
|
||||
}
|
||||
|
||||
impl Default for ExecutionStrategies {
|
||||
fn default() -> ExecutionStrategies {
|
||||
ExecutionStrategies {
|
||||
syncing: ExecutionStrategy::NativeElseWasm,
|
||||
importing: ExecutionStrategy::NativeElseWasm,
|
||||
block_construction: ExecutionStrategy::AlwaysWasm,
|
||||
offchain_worker: ExecutionStrategy::NativeWhenPossible,
|
||||
other: ExecutionStrategy::NativeElseWasm,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A producer of execution extensions for offchain calls.
|
||||
///
|
||||
/// This crate aggregates extensions available for the offchain calls
|
||||
/// and is responsbile to produce a right `Extensions` object
|
||||
/// for each call, based on required `Capabilities`.
|
||||
pub struct ExecutionExtensions<Block: traits::Block> {
|
||||
strategies: ExecutionStrategies,
|
||||
keystore: Option<BareCryptoStorePtr>,
|
||||
transaction_pool: RwLock<Option<Weak<dyn TransactionPool<Block>>>>,
|
||||
}
|
||||
|
||||
impl<Block: traits::Block> Default for ExecutionExtensions<Block> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
strategies: Default::default(),
|
||||
keystore: None,
|
||||
transaction_pool: RwLock::new(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block: traits::Block> ExecutionExtensions<Block> {
|
||||
/// Create new `ExecutionExtensions` given a `keystore` and `ExecutionStrategies`.
|
||||
pub fn new(
|
||||
strategies: ExecutionStrategies,
|
||||
keystore: Option<BareCryptoStorePtr>,
|
||||
) -> Self {
|
||||
let transaction_pool = RwLock::new(None);
|
||||
Self { strategies, keystore, transaction_pool }
|
||||
}
|
||||
|
||||
/// Get a reference to the execution strategies.
|
||||
pub fn strategies(&self) -> &ExecutionStrategies {
|
||||
&self.strategies
|
||||
}
|
||||
|
||||
/// Register transaction pool extension.
|
||||
///
|
||||
/// To break retain cycle between `Client` and `TransactionPool` we require this
|
||||
/// extension to be a `Weak` reference.
|
||||
/// That's also the reason why it's being registered lazily instead of
|
||||
/// during initialisation.
|
||||
pub fn register_transaction_pool(&self, pool: Weak<dyn TransactionPool<Block>>) {
|
||||
*self.transaction_pool.write() = Some(pool);
|
||||
}
|
||||
|
||||
/// Create `ExecutionManager` and `Extensions` for given offchain call.
|
||||
///
|
||||
/// Based on the execution context and capabilities it produces
|
||||
/// the right manager and extensions object to support desired set of APIs.
|
||||
pub fn manager_and_extensions<E: std::fmt::Debug, R: codec::Codec>(
|
||||
&self,
|
||||
at: &BlockId<Block>,
|
||||
context: ExecutionContext,
|
||||
) -> (
|
||||
ExecutionManager<DefaultHandler<R, E>>,
|
||||
Extensions,
|
||||
) {
|
||||
let manager = match context {
|
||||
ExecutionContext::BlockConstruction =>
|
||||
self.strategies.block_construction.get_manager(),
|
||||
ExecutionContext::Syncing =>
|
||||
self.strategies.syncing.get_manager(),
|
||||
ExecutionContext::Importing =>
|
||||
self.strategies.importing.get_manager(),
|
||||
ExecutionContext::OffchainCall(Some((_, capabilities))) if capabilities.has_all() =>
|
||||
self.strategies.offchain_worker.get_manager(),
|
||||
ExecutionContext::OffchainCall(_) =>
|
||||
self.strategies.other.get_manager(),
|
||||
};
|
||||
|
||||
let capabilities = context.capabilities();
|
||||
|
||||
let mut extensions = Extensions::new();
|
||||
|
||||
if capabilities.has(offchain::Capability::Keystore) {
|
||||
if let Some(keystore) = self.keystore.as_ref() {
|
||||
extensions.register(KeystoreExt(keystore.clone()));
|
||||
}
|
||||
}
|
||||
|
||||
if capabilities.has(offchain::Capability::TransactionPool) {
|
||||
if let Some(pool) = self.transaction_pool.read().as_ref().and_then(|x| x.upgrade()) {
|
||||
extensions.register(TransactionPoolExt(Box::new(TransactionPoolAdapter {
|
||||
at: *at,
|
||||
pool,
|
||||
}) as _));
|
||||
}
|
||||
}
|
||||
|
||||
if let ExecutionContext::OffchainCall(Some(ext)) = context {
|
||||
extensions.register(
|
||||
OffchainExt::new(offchain::LimitedExternalities::new(capabilities, ext.0))
|
||||
)
|
||||
}
|
||||
|
||||
(manager, extensions)
|
||||
}
|
||||
}
|
||||
|
||||
/// A wrapper type to pass `BlockId` to the actual transaction pool.
|
||||
struct TransactionPoolAdapter<Block: traits::Block> {
|
||||
at: BlockId<Block>,
|
||||
pool: Arc<dyn TransactionPool<Block>>,
|
||||
}
|
||||
|
||||
impl<Block: traits::Block> offchain::TransactionPool for TransactionPoolAdapter<Block> {
|
||||
fn submit_transaction(&mut self, data: Vec<u8>) -> Result<(), ()> {
|
||||
let xt = match Block::Extrinsic::decode(&mut &*data) {
|
||||
Ok(xt) => xt,
|
||||
Err(e) => {
|
||||
log::warn!("Unable to decode extrinsic: {:?}: {}", data, e.what());
|
||||
return Err(());
|
||||
},
|
||||
};
|
||||
|
||||
self.pool.submit_at(&self.at, xt)
|
||||
}
|
||||
}
|
||||
@@ -15,25 +15,25 @@
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Substrate client interfaces.
|
||||
#![warn(missing_docs)]
|
||||
|
||||
// TODO: make internal
|
||||
pub mod error;
|
||||
pub mod backend;
|
||||
pub mod blockchain;
|
||||
pub mod light;
|
||||
pub mod notifications;
|
||||
pub mod call_executor;
|
||||
pub mod client;
|
||||
pub mod offchain;
|
||||
pub mod error;
|
||||
pub mod execution_extensions;
|
||||
pub mod light;
|
||||
pub mod notifications;
|
||||
|
||||
pub use error::*;
|
||||
// TODO: avoid re-exports
|
||||
pub use backend::*;
|
||||
pub use blockchain::*;
|
||||
pub use call_executor::*;
|
||||
pub use client::*;
|
||||
pub use error::*;
|
||||
pub use light::*;
|
||||
pub use notifications::*;
|
||||
pub use call_executor::*;
|
||||
pub use offchain::*;
|
||||
pub use client::*;
|
||||
|
||||
pub use state_machine::{StorageProof, ExecutionStrategy};
|
||||
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
// Copyright 2019 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/>.
|
||||
|
||||
use std::collections::hash_map::{HashMap, Entry};
|
||||
|
||||
/// Offchain workers local storage.
|
||||
pub trait OffchainStorage: Clone + Send + Sync {
|
||||
/// Persist a value in storage under given key and prefix.
|
||||
fn set(&mut self, prefix: &[u8], key: &[u8], value: &[u8]);
|
||||
|
||||
/// Retrieve a value from storage under given key and prefix.
|
||||
fn get(&self, prefix: &[u8], key: &[u8]) -> Option<Vec<u8>>;
|
||||
|
||||
/// Replace the value in storage if given old_value matches the current one.
|
||||
///
|
||||
/// Returns `true` if the value has been set and false otherwise.
|
||||
fn compare_and_set(
|
||||
&mut self,
|
||||
prefix: &[u8],
|
||||
key: &[u8],
|
||||
old_value: Option<&[u8]>,
|
||||
new_value: &[u8],
|
||||
) -> bool;
|
||||
}
|
||||
|
||||
/// In-memory storage for offchain workers.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct InMemOffchainStorage {
|
||||
storage: HashMap<Vec<u8>, Vec<u8>>,
|
||||
}
|
||||
|
||||
impl OffchainStorage for InMemOffchainStorage {
|
||||
fn set(&mut self, prefix: &[u8], key: &[u8], value: &[u8]) {
|
||||
let key = prefix.iter().chain(key).cloned().collect();
|
||||
self.storage.insert(key, value.to_vec());
|
||||
}
|
||||
|
||||
fn get(&self, prefix: &[u8], key: &[u8]) -> Option<Vec<u8>> {
|
||||
let key: Vec<u8> = prefix.iter().chain(key).cloned().collect();
|
||||
self.storage.get(&key).cloned()
|
||||
}
|
||||
|
||||
fn compare_and_set(
|
||||
&mut self,
|
||||
prefix: &[u8],
|
||||
key: &[u8],
|
||||
old_value: Option<&[u8]>,
|
||||
new_value: &[u8],
|
||||
) -> bool {
|
||||
let key = prefix.iter().chain(key).cloned().collect();
|
||||
|
||||
match self.storage.entry(key) {
|
||||
Entry::Vacant(entry) => if old_value.is_none() {
|
||||
entry.insert(new_value.to_vec());
|
||||
true
|
||||
} else { false },
|
||||
Entry::Occupied(ref mut entry) if Some(entry.get().as_slice()) == old_value => {
|
||||
entry.insert(new_value.to_vec());
|
||||
true
|
||||
},
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,7 @@ mod execution_strategy;
|
||||
pub mod error;
|
||||
pub mod informant;
|
||||
|
||||
use client_api::ExecutionStrategies;
|
||||
use client_api::execution_extensions::ExecutionStrategies;
|
||||
use service::{
|
||||
config::{Configuration, DatabaseConfig},
|
||||
ServiceBuilderExport, ServiceBuilderImport, ServiceBuilderRevert,
|
||||
|
||||
@@ -39,11 +39,12 @@ use std::path::PathBuf;
|
||||
use std::io;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use client_api::ForkBlocks;
|
||||
use client_api::backend::NewBlockState;
|
||||
use client_api::blockchain::{well_known_cache_keys, HeaderBackend};
|
||||
use client_api::{ForkBlocks, ExecutionStrategies};
|
||||
use client_api::backend::{StorageCollection, ChildStorageCollection};
|
||||
use client_api::blockchain::{well_known_cache_keys, HeaderBackend};
|
||||
use client_api::error::{Result as ClientResult, Error as ClientError};
|
||||
use client_api::execution_extensions::ExecutionExtensions;
|
||||
use codec::{Decode, Encode};
|
||||
use hash_db::{Hasher, Prefix};
|
||||
use kvdb::{KeyValueDB, DBTransaction};
|
||||
@@ -224,8 +225,7 @@ pub fn new_client<E, S, Block, RA>(
|
||||
executor: E,
|
||||
genesis_storage: S,
|
||||
fork_blocks: ForkBlocks<Block>,
|
||||
execution_strategies: ExecutionStrategies,
|
||||
keystore: Option<primitives::traits::BareCryptoStorePtr>,
|
||||
execution_extensions: ExecutionExtensions<Block>,
|
||||
) -> Result<(
|
||||
client::Client<
|
||||
Backend<Block>,
|
||||
@@ -243,9 +243,9 @@ pub fn new_client<E, S, Block, RA>(
|
||||
S: BuildStorage,
|
||||
{
|
||||
let backend = Arc::new(Backend::new(settings, CANONICALIZATION_DELAY)?);
|
||||
let executor = client::LocalCallExecutor::new(backend.clone(), executor, keystore);
|
||||
let executor = client::LocalCallExecutor::new(backend.clone(), executor);
|
||||
Ok((
|
||||
client::Client::new(backend.clone(), executor, genesis_storage, fork_blocks, execution_strategies)?,
|
||||
client::Client::new(backend.clone(), executor, genesis_storage, fork_blocks, execution_extensions)?,
|
||||
backend,
|
||||
))
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ impl LocalStorage {
|
||||
}
|
||||
}
|
||||
|
||||
impl client_api::OffchainStorage for LocalStorage {
|
||||
impl primitives::offchain::OffchainStorage for LocalStorage {
|
||||
fn set(&mut self, prefix: &[u8], key: &[u8], value: &[u8]) {
|
||||
let key: Vec<u8> = prefix.iter().chain(key).cloned().collect();
|
||||
let mut tx = self.db.transaction();
|
||||
@@ -117,7 +117,7 @@ impl client_api::OffchainStorage for LocalStorage {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use client_api::OffchainStorage;
|
||||
use primitives::offchain::OffchainStorage;
|
||||
|
||||
#[test]
|
||||
fn should_compare_and_set_and_clear_the_locks_map() {
|
||||
|
||||
@@ -19,12 +19,12 @@ mod sandbox;
|
||||
use codec::{Encode, Decode};
|
||||
use hex_literal::hex;
|
||||
use primitives::{
|
||||
Blake2Hasher, blake2_128, blake2_256, ed25519, sr25519, map, Pair, offchain::OffchainExt,
|
||||
Blake2Hasher, blake2_128, blake2_256, ed25519, sr25519, map, Pair,
|
||||
offchain::{OffchainExt, testing},
|
||||
traits::Externalities,
|
||||
};
|
||||
use runtime_test::WASM_BINARY;
|
||||
use state_machine::TestExternalities as CoreTestExternalities;
|
||||
use substrate_offchain::testing;
|
||||
use test_case::test_case;
|
||||
use trie::{TrieConfiguration, trie_types::Layout};
|
||||
|
||||
@@ -401,7 +401,7 @@ fn ordered_trie_root_should_work(wasm_method: WasmExecutionMethod) {
|
||||
#[test_case(WasmExecutionMethod::Interpreted)]
|
||||
#[cfg_attr(feature = "wasmtime", test_case(WasmExecutionMethod::Compiled))]
|
||||
fn offchain_local_storage_should_work(wasm_method: WasmExecutionMethod) {
|
||||
use client_api::OffchainStorage;
|
||||
use primitives::offchain::OffchainStorage;
|
||||
|
||||
let mut ext = TestExternalities::default();
|
||||
let (offchain, state) = testing::TestOffchainExt::new();
|
||||
|
||||
@@ -23,7 +23,6 @@ parking_lot = "0.9.0"
|
||||
primitives = { package = "substrate-primitives", path = "../../primitives/core" }
|
||||
rand = "0.7.2"
|
||||
sr-primitives = { path = "../../primitives/sr-primitives" }
|
||||
transaction_pool = { package = "substrate-transaction-pool", path = "../transaction-pool" }
|
||||
network = { package = "substrate-network", path = "../network" }
|
||||
keystore = { package = "substrate-keystore", path = "../keystore" }
|
||||
|
||||
@@ -32,10 +31,11 @@ hyper = "0.12.35"
|
||||
hyper-rustls = "0.17.1"
|
||||
|
||||
[dev-dependencies]
|
||||
env_logger = "0.7.0"
|
||||
client-db = { package = "substrate-client-db", path = "../db/", default-features = true }
|
||||
env_logger = "0.7.0"
|
||||
test-client = { package = "substrate-test-runtime-client", path = "../../test/utils/runtime/client" }
|
||||
tokio = "0.1.22"
|
||||
transaction_pool = { package = "substrate-transaction-pool", path = "../transaction-pool" }
|
||||
|
||||
[features]
|
||||
default = []
|
||||
|
||||
@@ -21,9 +21,9 @@ use std::{
|
||||
thread::sleep,
|
||||
};
|
||||
|
||||
use client_api::OffchainStorage;
|
||||
use futures::{StreamExt as _, Future, FutureExt as _, future, channel::mpsc};
|
||||
use log::{info, debug, warn, error};
|
||||
use primitives::offchain::OffchainStorage;
|
||||
use futures::Future;
|
||||
use log::error;
|
||||
use network::{PeerId, Multiaddr, NetworkStateInfo};
|
||||
use codec::{Encode, Decode};
|
||||
use primitives::offchain::{
|
||||
@@ -31,8 +31,6 @@ use primitives::offchain::{
|
||||
OpaqueNetworkState, OpaquePeerId, OpaqueMultiaddr, StorageKind,
|
||||
};
|
||||
pub use offchain_primitives::STORAGE_PREFIX;
|
||||
use sr_primitives::{generic::BlockId, traits::{self, Extrinsic}};
|
||||
use transaction_pool::txpool::{Pool, ChainApi};
|
||||
|
||||
#[cfg(not(target_os = "unknown"))]
|
||||
mod http;
|
||||
@@ -44,19 +42,14 @@ mod http_dummy;
|
||||
|
||||
mod timestamp;
|
||||
|
||||
/// A message between the offchain extension and the processing thread.
|
||||
enum ExtMessage {
|
||||
SubmitExtrinsic(Vec<u8>),
|
||||
}
|
||||
|
||||
/// Asynchronous offchain API.
|
||||
///
|
||||
/// NOTE this is done to prevent recursive calls into the runtime (which are not supported currently).
|
||||
pub(crate) struct Api<Storage, Block: traits::Block> {
|
||||
sender: mpsc::UnboundedSender<ExtMessage>,
|
||||
pub(crate) struct Api<Storage> {
|
||||
/// Offchain Workers database.
|
||||
db: Storage,
|
||||
/// A NetworkState provider.
|
||||
network_state: Arc<dyn NetworkStateInfo + Send + Sync>,
|
||||
_at: BlockId<Block>,
|
||||
/// Is this node a potential validator?
|
||||
is_validator: bool,
|
||||
/// Everything HTTP-related is handled by a different struct.
|
||||
@@ -73,22 +66,11 @@ fn unavailable_yet<R: Default>(name: &str) -> R {
|
||||
|
||||
const LOCAL_DB: &str = "LOCAL (fork-aware) DB";
|
||||
|
||||
impl<Storage, Block> OffchainExt for Api<Storage, Block>
|
||||
where
|
||||
Storage: OffchainStorage,
|
||||
Block: traits::Block,
|
||||
{
|
||||
impl<Storage: OffchainStorage> OffchainExt for Api<Storage> {
|
||||
fn is_validator(&self) -> bool {
|
||||
self.is_validator
|
||||
}
|
||||
|
||||
fn submit_transaction(&mut self, ext: Vec<u8>) -> Result<(), ()> {
|
||||
self.sender
|
||||
.unbounded_send(ExtMessage::SubmitExtrinsic(ext))
|
||||
.map(|_| ())
|
||||
.map_err(|_| ())
|
||||
}
|
||||
|
||||
fn network_state(&self) -> Result<OpaqueNetworkState, ()> {
|
||||
let external_addresses = self.network_state.external_addresses();
|
||||
|
||||
@@ -260,40 +242,28 @@ impl TryFrom<OpaqueNetworkState> for NetworkState {
|
||||
/// Offchain extensions implementation API
|
||||
///
|
||||
/// This is the asynchronous processing part of the API.
|
||||
pub(crate) struct AsyncApi<A: ChainApi> {
|
||||
receiver: Option<mpsc::UnboundedReceiver<ExtMessage>>,
|
||||
transaction_pool: Arc<Pool<A>>,
|
||||
at: BlockId<A::Block>,
|
||||
pub(crate) struct AsyncApi {
|
||||
/// Everything HTTP-related is handled by a different struct.
|
||||
http: Option<http::HttpWorker>,
|
||||
}
|
||||
|
||||
impl<A: ChainApi> AsyncApi<A> {
|
||||
impl AsyncApi {
|
||||
/// Creates new Offchain extensions API implementation an the asynchronous processing part.
|
||||
pub fn new<S: OffchainStorage>(
|
||||
transaction_pool: Arc<Pool<A>>,
|
||||
db: S,
|
||||
at: BlockId<A::Block>,
|
||||
network_state: Arc<dyn NetworkStateInfo + Send + Sync>,
|
||||
is_validator: bool,
|
||||
) -> (Api<S, A::Block>, AsyncApi<A>) {
|
||||
let (sender, rx) = mpsc::unbounded();
|
||||
|
||||
) -> (Api<S>, AsyncApi) {
|
||||
let (http_api, http_worker) = http::http();
|
||||
|
||||
let api = Api {
|
||||
sender,
|
||||
db,
|
||||
network_state,
|
||||
_at: at,
|
||||
is_validator,
|
||||
http: http_api,
|
||||
};
|
||||
|
||||
let async_api = AsyncApi {
|
||||
receiver: Some(rx),
|
||||
transaction_pool,
|
||||
at,
|
||||
http: Some(http_worker),
|
||||
};
|
||||
|
||||
@@ -302,35 +272,9 @@ impl<A: ChainApi> AsyncApi<A> {
|
||||
|
||||
/// Run a processing task for the API
|
||||
pub fn process(mut self) -> impl Future<Output = ()> {
|
||||
let receiver = self.receiver.take().expect("Take invoked only once.");
|
||||
let http = self.http.take().expect("Take invoked only once.");
|
||||
|
||||
let extrinsics = receiver.for_each(move |msg| {
|
||||
match msg {
|
||||
ExtMessage::SubmitExtrinsic(ext) => self.submit_extrinsic(ext),
|
||||
}
|
||||
});
|
||||
|
||||
future::join(extrinsics, http)
|
||||
.map(|((), ())| ())
|
||||
}
|
||||
|
||||
fn submit_extrinsic(&mut self, ext: Vec<u8>) -> impl Future<Output = ()> {
|
||||
let xt = match <A::Block as traits::Block>::Extrinsic::decode(&mut &*ext) {
|
||||
Ok(xt) => xt,
|
||||
Err(e) => {
|
||||
warn!("Unable to decode extrinsic: {:?}: {}", ext, e.what());
|
||||
return future::Either::Left(future::ready(()))
|
||||
},
|
||||
};
|
||||
|
||||
info!("Submitting transaction to the pool: {:?} (isSigned: {:?})", xt, xt.is_signed());
|
||||
future::Either::Right(self.transaction_pool
|
||||
.submit_one(&self.at, xt.clone())
|
||||
.map(|result| match result {
|
||||
Ok(hash) => { debug!("[{:?}] Offchain transaction added to the pool.", hash); },
|
||||
Err(e) => { warn!("Couldn't submit offchain transaction: {:?}", e); },
|
||||
}))
|
||||
http
|
||||
}
|
||||
}
|
||||
|
||||
@@ -338,10 +282,8 @@ impl<A: ChainApi> AsyncApi<A> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::{convert::{TryFrom, TryInto}, time::SystemTime};
|
||||
use sr_primitives::traits::Zero;
|
||||
use client_db::offchain::LocalStorage;
|
||||
use network::PeerId;
|
||||
use test_client::runtime::Block;
|
||||
|
||||
struct MockNetworkStateInfo();
|
||||
|
||||
@@ -355,19 +297,13 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn offchain_api() -> (Api<LocalStorage, Block>, AsyncApi<impl ChainApi>) {
|
||||
fn offchain_api() -> (Api<LocalStorage>, AsyncApi) {
|
||||
let _ = env_logger::try_init();
|
||||
let db = LocalStorage::new_test();
|
||||
let client = Arc::new(test_client::new());
|
||||
let pool = Arc::new(
|
||||
Pool::new(Default::default(), transaction_pool::FullChainApi::new(client.clone()))
|
||||
);
|
||||
|
||||
let mock = Arc::new(MockNetworkStateInfo());
|
||||
|
||||
AsyncApi::new(
|
||||
pool,
|
||||
db,
|
||||
BlockId::Number(Zero::zero()),
|
||||
mock,
|
||||
false,
|
||||
)
|
||||
|
||||
@@ -41,15 +41,11 @@ use sr_api::ApiExt;
|
||||
use futures::future::Future;
|
||||
use log::{debug, warn};
|
||||
use network::NetworkStateInfo;
|
||||
use primitives::{offchain, ExecutionContext};
|
||||
use primitives::{offchain::{self, OffchainStorage}, ExecutionContext};
|
||||
use sr_primitives::{generic::BlockId, traits::{self, ProvideRuntimeApi}};
|
||||
use transaction_pool::txpool::{Pool, ChainApi};
|
||||
use client_api::{OffchainStorage};
|
||||
|
||||
mod api;
|
||||
|
||||
pub mod testing;
|
||||
|
||||
pub use offchain_primitives::{OffchainWorkerApi, STORAGE_PREFIX};
|
||||
|
||||
/// An offchain workers manager.
|
||||
@@ -94,13 +90,12 @@ impl<Client, Storage, Block> OffchainWorkers<
|
||||
{
|
||||
/// Start the offchain workers after given block.
|
||||
#[must_use]
|
||||
pub fn on_block_imported<A>(
|
||||
pub fn on_block_imported(
|
||||
&self,
|
||||
number: &<Block::Header as traits::Header>::Number,
|
||||
pool: &Arc<Pool<A>>,
|
||||
network_state: Arc<dyn NetworkStateInfo + Send + Sync>,
|
||||
is_validator: bool,
|
||||
) -> impl Future<Output = ()> where A: ChainApi<Block=Block> + 'static {
|
||||
) -> impl Future<Output = ()> {
|
||||
let runtime = self.client.runtime_api();
|
||||
let at = BlockId::number(*number);
|
||||
let has_api = runtime.has_api::<dyn OffchainWorkerApi<Block, Error = ()>>(&at);
|
||||
@@ -108,9 +103,7 @@ impl<Client, Storage, Block> OffchainWorkers<
|
||||
|
||||
if has_api.unwrap_or(false) {
|
||||
let (api, runner) = api::AsyncApi::new(
|
||||
pool.clone(),
|
||||
self.db.clone(),
|
||||
at.clone(),
|
||||
network_state.clone(),
|
||||
is_validator,
|
||||
);
|
||||
@@ -153,6 +146,8 @@ impl<Client, Storage, Block> OffchainWorkers<
|
||||
mod tests {
|
||||
use super::*;
|
||||
use network::{Multiaddr, PeerId};
|
||||
use std::sync::Arc;
|
||||
use transaction_pool::txpool::Pool;
|
||||
|
||||
struct MockNetworkStateInfo();
|
||||
|
||||
@@ -171,13 +166,18 @@ mod tests {
|
||||
// given
|
||||
let _ = env_logger::try_init();
|
||||
let client = Arc::new(test_client::new());
|
||||
let pool = Arc::new(Pool::new(Default::default(), transaction_pool::FullChainApi::new(client.clone())));
|
||||
let pool = Arc::new(Pool::new(
|
||||
Default::default(),
|
||||
transaction_pool::FullChainApi::new(client.clone())
|
||||
));
|
||||
client.execution_extensions()
|
||||
.register_transaction_pool(Arc::downgrade(&pool.clone()) as _);
|
||||
let db = client_db::offchain::LocalStorage::new_test();
|
||||
let network_state = Arc::new(MockNetworkStateInfo());
|
||||
|
||||
// when
|
||||
let offchain = OffchainWorkers::new(client, db);
|
||||
futures::executor::block_on(offchain.on_block_imported(&0u64, &pool, network_state, false));
|
||||
futures::executor::block_on(offchain.on_block_imported(&0u64, network_state, false));
|
||||
|
||||
// then
|
||||
assert_eq!(pool.status().ready, 1);
|
||||
|
||||
@@ -1,307 +0,0 @@
|
||||
// Copyright 2019 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/>.
|
||||
|
||||
//! Offchain Externalities implementation for tests.
|
||||
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
sync::Arc,
|
||||
};
|
||||
use client_api::{OffchainStorage, InMemOffchainStorage};
|
||||
use parking_lot::RwLock;
|
||||
use primitives::offchain::{
|
||||
self,
|
||||
HttpError,
|
||||
HttpRequestId as RequestId,
|
||||
HttpRequestStatus as RequestStatus,
|
||||
Timestamp,
|
||||
StorageKind,
|
||||
OpaqueNetworkState,
|
||||
};
|
||||
|
||||
/// Pending request.
|
||||
#[derive(Debug, Default, PartialEq, Eq)]
|
||||
pub struct PendingRequest {
|
||||
/// HTTP method
|
||||
pub method: String,
|
||||
/// URI
|
||||
pub uri: String,
|
||||
/// Encoded Metadata
|
||||
pub meta: Vec<u8>,
|
||||
/// Request headers
|
||||
pub headers: Vec<(String, String)>,
|
||||
/// Request body
|
||||
pub body: Vec<u8>,
|
||||
/// Has the request been sent already.
|
||||
pub sent: bool,
|
||||
/// Response body
|
||||
pub response: Option<Vec<u8>>,
|
||||
/// Number of bytes already read from the response body.
|
||||
pub read: usize,
|
||||
/// Response headers
|
||||
pub response_headers: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
/// Internal state of the externalities.
|
||||
///
|
||||
/// This can be used in tests to respond or assert stuff about interactions.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct State {
|
||||
/// A list of pending requests.
|
||||
pub requests: BTreeMap<RequestId, PendingRequest>,
|
||||
expected_requests: BTreeMap<RequestId, PendingRequest>,
|
||||
/// Persistent local storage
|
||||
pub persistent_storage: InMemOffchainStorage,
|
||||
/// Local storage
|
||||
pub local_storage: InMemOffchainStorage,
|
||||
/// A vector of transactions submitted from the runtime.
|
||||
pub transactions: Vec<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl State {
|
||||
/// Asserts that pending request has been submitted and fills it's response.
|
||||
pub fn fulfill_pending_request(
|
||||
&mut self,
|
||||
id: u16,
|
||||
expected: PendingRequest,
|
||||
response: impl Into<Vec<u8>>,
|
||||
response_headers: impl IntoIterator<Item=(String, String)>,
|
||||
) {
|
||||
match self.requests.get_mut(&RequestId(id)) {
|
||||
None => {
|
||||
panic!("Missing pending request: {:?}.\n\nAll: {:?}", id, self.requests);
|
||||
}
|
||||
Some(req) => {
|
||||
assert_eq!(
|
||||
*req,
|
||||
expected,
|
||||
);
|
||||
req.response = Some(response.into());
|
||||
req.response_headers = response_headers.into_iter().collect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn fulfill_expected(&mut self, id: u16) {
|
||||
if let Some(mut req) = self.expected_requests.remove(&RequestId(id)) {
|
||||
let response = req.response.take().expect("Response checked while added.");
|
||||
let headers = std::mem::replace(&mut req.response_headers, vec![]);
|
||||
self.fulfill_pending_request(id, req, response, headers);
|
||||
}
|
||||
}
|
||||
|
||||
/// Add expected HTTP request.
|
||||
///
|
||||
/// This method can be used to initialize expected HTTP requests and their responses
|
||||
/// before running the actual code that utilizes them (for instance before calling into runtime).
|
||||
/// Expected request has to be fulfilled before this struct is dropped,
|
||||
/// the `response` and `response_headers` fields will be used to return results to the callers.
|
||||
pub fn expect_request(&mut self, id: u16, expected: PendingRequest) {
|
||||
if expected.response.is_none() {
|
||||
panic!("Expected request needs to have a response.");
|
||||
}
|
||||
self.expected_requests.insert(RequestId(id), expected);
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for State {
|
||||
fn drop(&mut self) {
|
||||
// If we panic! while we are already in a panic, the test dies with an illegal instruction.
|
||||
if !self.expected_requests.is_empty() && !std::thread::panicking() {
|
||||
panic!("Unfulfilled expected requests: {:?}", self.expected_requests);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Implementation of offchain externalities used for tests.
|
||||
#[derive(Clone, Default, Debug)]
|
||||
pub struct TestOffchainExt(pub Arc<RwLock<State>>);
|
||||
|
||||
impl TestOffchainExt {
|
||||
/// Create new `TestOffchainExt` and a reference to the internal state.
|
||||
pub fn new() -> (Self, Arc<RwLock<State>>) {
|
||||
let ext = Self::default();
|
||||
let state = ext.0.clone();
|
||||
(ext, state)
|
||||
}
|
||||
}
|
||||
|
||||
impl offchain::Externalities for TestOffchainExt {
|
||||
fn is_validator(&self) -> bool {
|
||||
unimplemented!("not needed in tests so far")
|
||||
}
|
||||
|
||||
fn submit_transaction(&mut self, ex: Vec<u8>) -> Result<(), ()> {
|
||||
let mut state = self.0.write();
|
||||
state.transactions.push(ex);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn network_state(&self) -> Result<OpaqueNetworkState, ()> {
|
||||
Ok(OpaqueNetworkState {
|
||||
peer_id: Default::default(),
|
||||
external_addresses: vec![],
|
||||
})
|
||||
}
|
||||
|
||||
fn timestamp(&mut self) -> Timestamp {
|
||||
unimplemented!("not needed in tests so far")
|
||||
}
|
||||
|
||||
fn sleep_until(&mut self, _deadline: Timestamp) {
|
||||
unimplemented!("not needed in tests so far")
|
||||
}
|
||||
|
||||
fn random_seed(&mut self) -> [u8; 32] {
|
||||
unimplemented!("not needed in tests so far")
|
||||
}
|
||||
|
||||
fn local_storage_set(&mut self, kind: StorageKind, key: &[u8], value: &[u8]) {
|
||||
let mut state = self.0.write();
|
||||
match kind {
|
||||
StorageKind::LOCAL => &mut state.local_storage,
|
||||
StorageKind::PERSISTENT => &mut state.persistent_storage,
|
||||
}.set(b"", key, value);
|
||||
}
|
||||
|
||||
fn local_storage_compare_and_set(
|
||||
&mut self,
|
||||
kind: StorageKind,
|
||||
key: &[u8],
|
||||
old_value: Option<&[u8]>,
|
||||
new_value: &[u8]
|
||||
) -> bool {
|
||||
let mut state = self.0.write();
|
||||
match kind {
|
||||
StorageKind::LOCAL => &mut state.local_storage,
|
||||
StorageKind::PERSISTENT => &mut state.persistent_storage,
|
||||
}.compare_and_set(b"", key, old_value, new_value)
|
||||
}
|
||||
|
||||
fn local_storage_get(&mut self, kind: StorageKind, key: &[u8]) -> Option<Vec<u8>> {
|
||||
let state = self.0.read();
|
||||
match kind {
|
||||
StorageKind::LOCAL => &state.local_storage,
|
||||
StorageKind::PERSISTENT => &state.persistent_storage,
|
||||
}.get(b"", key)
|
||||
}
|
||||
|
||||
fn http_request_start(&mut self, method: &str, uri: &str, meta: &[u8]) -> Result<RequestId, ()> {
|
||||
let mut state = self.0.write();
|
||||
let id = RequestId(state.requests.len() as u16);
|
||||
state.requests.insert(id.clone(), PendingRequest {
|
||||
method: method.into(),
|
||||
uri: uri.into(),
|
||||
meta: meta.into(),
|
||||
..Default::default()
|
||||
});
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
fn http_request_add_header(
|
||||
&mut self,
|
||||
request_id: RequestId,
|
||||
name: &str,
|
||||
value: &str,
|
||||
) -> Result<(), ()> {
|
||||
let mut state = self.0.write();
|
||||
if let Some(req) = state.requests.get_mut(&request_id) {
|
||||
req.headers.push((name.into(), value.into()));
|
||||
Ok(())
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
fn http_request_write_body(
|
||||
&mut self,
|
||||
request_id: RequestId,
|
||||
chunk: &[u8],
|
||||
_deadline: Option<Timestamp>
|
||||
) -> Result<(), HttpError> {
|
||||
let mut state = self.0.write();
|
||||
|
||||
let sent = {
|
||||
let req = state.requests.get_mut(&request_id).ok_or(HttpError::IoError)?;
|
||||
req.body.extend(chunk);
|
||||
if chunk.is_empty() {
|
||||
req.sent = true;
|
||||
}
|
||||
req.sent
|
||||
};
|
||||
|
||||
if sent {
|
||||
state.fulfill_expected(request_id.0);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn http_response_wait(
|
||||
&mut self,
|
||||
ids: &[RequestId],
|
||||
_deadline: Option<Timestamp>,
|
||||
) -> Vec<RequestStatus> {
|
||||
let state = self.0.read();
|
||||
|
||||
ids.iter().map(|id| match state.requests.get(id) {
|
||||
Some(req) if req.response.is_none() =>
|
||||
panic!("No `response` provided for request with id: {:?}", id),
|
||||
None => RequestStatus::Invalid,
|
||||
_ => RequestStatus::Finished(200),
|
||||
}).collect()
|
||||
}
|
||||
|
||||
fn http_response_headers(&mut self, request_id: RequestId) -> Vec<(Vec<u8>, Vec<u8>)> {
|
||||
let state = self.0.read();
|
||||
if let Some(req) = state.requests.get(&request_id) {
|
||||
req.response_headers
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k.into_bytes(), v.into_bytes()))
|
||||
.collect()
|
||||
} else {
|
||||
Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn http_response_read_body(
|
||||
&mut self,
|
||||
request_id: RequestId,
|
||||
buffer: &mut [u8],
|
||||
_deadline: Option<Timestamp>
|
||||
) -> Result<usize, HttpError> {
|
||||
let mut state = self.0.write();
|
||||
if let Some(req) = state.requests.get_mut(&request_id) {
|
||||
let response = req.response
|
||||
.as_mut()
|
||||
.expect(&format!("No response provided for request: {:?}", request_id));
|
||||
|
||||
if req.read >= response.len() {
|
||||
// Remove the pending request as per spec.
|
||||
state.requests.remove(&request_id);
|
||||
Ok(0)
|
||||
} else {
|
||||
let read = std::cmp::min(buffer.len(), response[req.read..].len());
|
||||
buffer[0..read].copy_from_slice(&response[req.read..read]);
|
||||
req.read += read;
|
||||
Ok(read)
|
||||
}
|
||||
} else {
|
||||
Err(HttpError::IoError)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -195,13 +195,17 @@ where TGen: RuntimeGenesis, TCSExt: Extension {
|
||||
},
|
||||
};
|
||||
|
||||
let extensions = client_api::execution_extensions::ExecutionExtensions::new(
|
||||
config.execution_strategies.clone(),
|
||||
Some(keystore.clone()),
|
||||
);
|
||||
|
||||
client_db::new_client(
|
||||
db_config,
|
||||
executor,
|
||||
&config.chain_spec,
|
||||
fork_blocks,
|
||||
config.execution_strategies.clone(),
|
||||
Some(keystore.clone()),
|
||||
extensions,
|
||||
)?
|
||||
};
|
||||
|
||||
@@ -831,6 +835,10 @@ ServiceBuilder<
|
||||
"best" => ?chain_info.best_hash
|
||||
);
|
||||
|
||||
// make transaction pool available for off-chain runtime calls.
|
||||
client.execution_extensions()
|
||||
.register_transaction_pool(Arc::downgrade(&transaction_pool) as _);
|
||||
|
||||
let transaction_pool_adapter = Arc::new(TransactionPoolAdapter {
|
||||
imports_external_transactions: !config.roles.is_light(),
|
||||
pool: transaction_pool.clone(),
|
||||
@@ -911,8 +919,8 @@ ServiceBuilder<
|
||||
}
|
||||
|
||||
let offchain = offchain.as_ref().and_then(|o| o.upgrade());
|
||||
if let (Some(txpool), Some(offchain)) = (txpool, offchain) {
|
||||
let future = offchain.on_block_imported(&number, &txpool, network_state_info.clone(), is_validator)
|
||||
if let Some(offchain) = offchain {
|
||||
let future = offchain.on_block_imported(&number, network_state_info.clone(), is_validator)
|
||||
.map(|()| Ok(()));
|
||||
let _ = to_spawn_tx_.unbounded_send(Box::new(Compat::new(future)));
|
||||
}
|
||||
|
||||
@@ -24,12 +24,12 @@ use state_machine::{
|
||||
backend::Backend as _, ChangesTrieTransaction, StorageProof,
|
||||
};
|
||||
use executor::{RuntimeVersion, RuntimeInfo, NativeVersion};
|
||||
use externalities::Extensions;
|
||||
use hash_db::Hasher;
|
||||
use primitives::{
|
||||
offchain::OffchainExt, H256, Blake2Hasher, NativeOrEncoded, NeverNativeValue,
|
||||
traits::{CodeExecutor, KeystoreExt},
|
||||
H256, Blake2Hasher, NativeOrEncoded, NeverNativeValue,
|
||||
traits::CodeExecutor,
|
||||
};
|
||||
|
||||
use sr_api::{ProofRecorder, InitializeBlock};
|
||||
use client_api::{
|
||||
error, backend, call_executor::CallExecutor,
|
||||
@@ -40,7 +40,6 @@ use client_api::{
|
||||
pub struct LocalCallExecutor<B, E> {
|
||||
backend: Arc<B>,
|
||||
executor: E,
|
||||
keystore: Option<primitives::traits::BareCryptoStorePtr>,
|
||||
}
|
||||
|
||||
impl<B, E> LocalCallExecutor<B, E> {
|
||||
@@ -48,12 +47,10 @@ impl<B, E> LocalCallExecutor<B, E> {
|
||||
pub fn new(
|
||||
backend: Arc<B>,
|
||||
executor: E,
|
||||
keystore: Option<primitives::traits::BareCryptoStorePtr>,
|
||||
) -> Self {
|
||||
LocalCallExecutor {
|
||||
backend,
|
||||
executor,
|
||||
keystore,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,7 +60,6 @@ impl<B, E> Clone for LocalCallExecutor<B, E> where E: Clone {
|
||||
LocalCallExecutor {
|
||||
backend: self.backend.clone(),
|
||||
executor: self.executor.clone(),
|
||||
keystore: self.keystore.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -82,19 +78,18 @@ where
|
||||
method: &str,
|
||||
call_data: &[u8],
|
||||
strategy: ExecutionStrategy,
|
||||
side_effects_handler: Option<OffchainExt>,
|
||||
extensions: Option<Extensions>,
|
||||
) -> error::Result<Vec<u8>> {
|
||||
let mut changes = OverlayedChanges::default();
|
||||
let state = self.backend.state_at(*id)?;
|
||||
let return_data = StateMachine::new(
|
||||
&state,
|
||||
self.backend.changes_trie_storage(),
|
||||
side_effects_handler,
|
||||
&mut changes,
|
||||
&self.executor,
|
||||
method,
|
||||
call_data,
|
||||
self.keystore.clone().map(KeystoreExt),
|
||||
extensions.unwrap_or_default(),
|
||||
).execute_using_consensus_failure_handler::<_, NeverNativeValue, fn() -> _>(
|
||||
strategy.get_manager(),
|
||||
false,
|
||||
@@ -124,9 +119,8 @@ where
|
||||
initialize_block: InitializeBlock<'a, Block>,
|
||||
execution_manager: ExecutionManager<EM>,
|
||||
native_call: Option<NC>,
|
||||
side_effects_handler: Option<OffchainExt>,
|
||||
recorder: &Option<ProofRecorder<Block>>,
|
||||
enable_keystore: bool,
|
||||
extensions: Option<Extensions>,
|
||||
) -> Result<NativeOrEncoded<R>, error::Error> where ExecutionManager<EM>: Clone {
|
||||
match initialize_block {
|
||||
InitializeBlock::Do(ref init_block)
|
||||
@@ -137,12 +131,6 @@ where
|
||||
_ => {},
|
||||
}
|
||||
|
||||
let keystore = if enable_keystore {
|
||||
self.keystore.clone().map(KeystoreExt)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut state = self.backend.state_at(*at)?;
|
||||
|
||||
let result = match recorder {
|
||||
@@ -161,12 +149,11 @@ where
|
||||
StateMachine::new(
|
||||
&backend,
|
||||
self.backend.changes_trie_storage(),
|
||||
side_effects_handler,
|
||||
&mut *changes.borrow_mut(),
|
||||
&self.executor,
|
||||
method,
|
||||
call_data,
|
||||
keystore,
|
||||
extensions.unwrap_or_default(),
|
||||
)
|
||||
.execute_using_consensus_failure_handler(
|
||||
execution_manager,
|
||||
@@ -179,12 +166,11 @@ where
|
||||
None => StateMachine::new(
|
||||
&state,
|
||||
self.backend.changes_trie_storage(),
|
||||
side_effects_handler,
|
||||
&mut *changes.borrow_mut(),
|
||||
&self.executor,
|
||||
method,
|
||||
call_data,
|
||||
keystore,
|
||||
extensions.unwrap_or_default(),
|
||||
)
|
||||
.execute_using_consensus_failure_handler(
|
||||
execution_manager,
|
||||
@@ -227,7 +213,7 @@ where
|
||||
call_data: &[u8],
|
||||
manager: ExecutionManager<F>,
|
||||
native_call: Option<NC>,
|
||||
side_effects_handler: Option<OffchainExt>,
|
||||
extensions: Option<Extensions>,
|
||||
) -> error::Result<(
|
||||
NativeOrEncoded<R>,
|
||||
(S::Transaction, <Blake2Hasher as Hasher>::Out),
|
||||
@@ -236,12 +222,11 @@ where
|
||||
StateMachine::new(
|
||||
state,
|
||||
self.backend.changes_trie_storage(),
|
||||
side_effects_handler,
|
||||
changes,
|
||||
&self.executor,
|
||||
method,
|
||||
call_data,
|
||||
self.keystore.clone().map(KeystoreExt),
|
||||
extensions.unwrap_or_default(),
|
||||
).execute_using_consensus_failure_handler(
|
||||
manager,
|
||||
true,
|
||||
@@ -268,7 +253,9 @@ where
|
||||
&self.executor,
|
||||
method,
|
||||
call_data,
|
||||
self.keystore.clone().map(KeystoreExt),
|
||||
// Passing `None` here, since we don't really want to prove anything
|
||||
// about our local keys.
|
||||
None,
|
||||
)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
@@ -26,9 +26,10 @@ use parking_lot::{Mutex, RwLock};
|
||||
use codec::{Encode, Decode};
|
||||
use hash_db::{Hasher, Prefix};
|
||||
use primitives::{
|
||||
Blake2Hasher, H256, ChangesTrieConfiguration, convert_hash, NeverNativeValue, ExecutionContext,
|
||||
NativeOrEncoded, storage::{StorageKey, StorageData, well_known_keys},
|
||||
offchain::{OffchainExt, self}, traits::CodeExecutor,
|
||||
Blake2Hasher, H256, ChangesTrieConfiguration, convert_hash,
|
||||
NeverNativeValue, ExecutionContext, NativeOrEncoded,
|
||||
storage::{StorageKey, StorageData, well_known_keys},
|
||||
traits::CodeExecutor,
|
||||
};
|
||||
use substrate_telemetry::{telemetry, SUBSTRATE_INFO};
|
||||
use sr_primitives::{
|
||||
@@ -68,13 +69,14 @@ pub use client_api::{
|
||||
},
|
||||
client::{
|
||||
ImportNotifications, FinalityNotification, FinalityNotifications, BlockImportNotification,
|
||||
ClientInfo, BlockchainEvents, BlockBody, ProvideUncles, ExecutionStrategies, ForkBlocks,
|
||||
ClientInfo, BlockchainEvents, BlockBody, ProvideUncles, ForkBlocks,
|
||||
BlockOf,
|
||||
},
|
||||
execution_extensions::{ExecutionExtensions, ExecutionStrategies},
|
||||
notifications::{StorageNotifications, StorageEventStream},
|
||||
error::Error,
|
||||
error,
|
||||
CallExecutor
|
||||
CallExecutor,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
@@ -100,7 +102,7 @@ pub struct Client<B, E, Block, RA> where Block: BlockT {
|
||||
// holds the block hash currently being imported. TODO: replace this with block queue
|
||||
importing_block: RwLock<Option<Block::Hash>>,
|
||||
fork_blocks: ForkBlocks<Block>,
|
||||
execution_strategies: ExecutionStrategies,
|
||||
execution_extensions: ExecutionExtensions<Block>,
|
||||
_phantom: PhantomData<RA>,
|
||||
}
|
||||
|
||||
@@ -171,8 +173,9 @@ pub fn new_with_backend<B, E, Block, S, RA>(
|
||||
Block: BlockT<Hash=H256>,
|
||||
B: backend::LocalBackend<Block, Blake2Hasher>
|
||||
{
|
||||
let call_executor = LocalCallExecutor::new(backend.clone(), executor, keystore);
|
||||
Client::new(backend, call_executor, build_genesis_storage, Default::default(), Default::default())
|
||||
let call_executor = LocalCallExecutor::new(backend.clone(), executor);
|
||||
let extensions = ExecutionExtensions::new(Default::default(), keystore);
|
||||
Client::new(backend, call_executor, build_genesis_storage, Default::default(), extensions)
|
||||
}
|
||||
|
||||
impl<B, E, Block, RA> BlockOf for Client<B, E, Block, RA> where
|
||||
@@ -194,7 +197,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
|
||||
executor: E,
|
||||
build_genesis_storage: S,
|
||||
fork_blocks: ForkBlocks<Block>,
|
||||
execution_strategies: ExecutionStrategies
|
||||
execution_extensions: ExecutionExtensions<Block>,
|
||||
) -> error::Result<Self> {
|
||||
if backend.blockchain().header(BlockId::Number(Zero::zero()))?.is_none() {
|
||||
let (genesis_storage, children_genesis_storage) = build_genesis_storage.build_storage()?;
|
||||
@@ -223,14 +226,14 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
|
||||
finality_notification_sinks: Default::default(),
|
||||
importing_block: Default::default(),
|
||||
fork_blocks,
|
||||
execution_strategies,
|
||||
execution_extensions,
|
||||
_phantom: Default::default(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Get a reference to the execution strategies.
|
||||
pub fn execution_strategies(&self) -> &ExecutionStrategies {
|
||||
&self.execution_strategies
|
||||
/// Get a reference to the execution extensions.
|
||||
pub fn execution_extensions(&self) -> &ExecutionExtensions<Block> {
|
||||
&self.execution_extensions
|
||||
}
|
||||
|
||||
/// Get a reference to the state at a given block.
|
||||
@@ -993,9 +996,9 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
|
||||
&encoded_block,
|
||||
match origin {
|
||||
BlockOrigin::NetworkInitialSync => get_execution_manager(
|
||||
self.execution_strategies().syncing,
|
||||
self.execution_extensions().strategies().syncing,
|
||||
),
|
||||
_ => get_execution_manager(self.execution_strategies().importing),
|
||||
_ => get_execution_manager(self.execution_extensions().strategies().importing),
|
||||
},
|
||||
None,
|
||||
None,
|
||||
@@ -1395,26 +1398,7 @@ impl<B, E, Block, RA> CallRuntimeAt<Block> for Client<B, E, Block, RA> where
|
||||
context: ExecutionContext,
|
||||
recorder: &Option<ProofRecorder<Block>>,
|
||||
) -> error::Result<NativeOrEncoded<R>> {
|
||||
let manager = match context {
|
||||
ExecutionContext::BlockConstruction =>
|
||||
self.execution_strategies.block_construction.get_manager(),
|
||||
ExecutionContext::Syncing =>
|
||||
self.execution_strategies.syncing.get_manager(),
|
||||
ExecutionContext::Importing =>
|
||||
self.execution_strategies.importing.get_manager(),
|
||||
ExecutionContext::OffchainCall(Some((_, capabilities))) if capabilities.has_all() =>
|
||||
self.execution_strategies.offchain_worker.get_manager(),
|
||||
ExecutionContext::OffchainCall(_) =>
|
||||
self.execution_strategies.other.get_manager(),
|
||||
};
|
||||
|
||||
let capabilities = context.capabilities();
|
||||
let offchain_extensions = if let ExecutionContext::OffchainCall(Some(ext)) = context {
|
||||
Some(OffchainExt::new(offchain::LimitedExternalities::new(capabilities, ext.0)))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let (manager, extensions) = self.execution_extensions.manager_and_extensions(at, context);
|
||||
self.executor.contextual_call::<_, fn(_,_) -> _,_,_>(
|
||||
|| core_api.initialize_block(at, &self.prepare_environment_block(at)?),
|
||||
at,
|
||||
@@ -1424,9 +1408,8 @@ impl<B, E, Block, RA> CallRuntimeAt<Block> for Client<B, E, Block, RA> where
|
||||
initialize_block,
|
||||
manager,
|
||||
native_call,
|
||||
offchain_extensions,
|
||||
recorder,
|
||||
capabilities.has(offchain::Capability::Keystore),
|
||||
Some(extensions),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -93,12 +93,11 @@ mod tests {
|
||||
StateMachine::new(
|
||||
backend,
|
||||
Some(&InMemoryChangesTrieStorage::<_, u64>::new()),
|
||||
None,
|
||||
&mut overlay,
|
||||
&executor(),
|
||||
"Core_initialize_block",
|
||||
&header.encode(),
|
||||
None,
|
||||
Default::default(),
|
||||
).execute(
|
||||
ExecutionStrategy::NativeElseWasm,
|
||||
).unwrap();
|
||||
@@ -107,12 +106,11 @@ mod tests {
|
||||
StateMachine::new(
|
||||
backend,
|
||||
Some(&InMemoryChangesTrieStorage::<_, u64>::new()),
|
||||
None,
|
||||
&mut overlay,
|
||||
&executor(),
|
||||
"BlockBuilder_apply_extrinsic",
|
||||
&tx.encode(),
|
||||
None,
|
||||
Default::default(),
|
||||
).execute(
|
||||
ExecutionStrategy::NativeElseWasm,
|
||||
).unwrap();
|
||||
@@ -121,12 +119,11 @@ mod tests {
|
||||
let (ret_data, _, _) = StateMachine::new(
|
||||
backend,
|
||||
Some(&InMemoryChangesTrieStorage::<_, u64>::new()),
|
||||
None,
|
||||
&mut overlay,
|
||||
&executor(),
|
||||
"BlockBuilder_finalize_block",
|
||||
&[],
|
||||
None,
|
||||
Default::default(),
|
||||
).execute(
|
||||
ExecutionStrategy::NativeElseWasm,
|
||||
).unwrap();
|
||||
@@ -169,12 +166,11 @@ mod tests {
|
||||
let _ = StateMachine::new(
|
||||
&backend,
|
||||
Some(&InMemoryChangesTrieStorage::<_, u64>::new()),
|
||||
None,
|
||||
&mut overlay,
|
||||
&executor(),
|
||||
"Core_execute_block",
|
||||
&b1data,
|
||||
None,
|
||||
Default::default(),
|
||||
).execute(
|
||||
ExecutionStrategy::NativeElseWasm,
|
||||
).unwrap();
|
||||
@@ -199,12 +195,11 @@ mod tests {
|
||||
let _ = StateMachine::new(
|
||||
&backend,
|
||||
Some(&InMemoryChangesTrieStorage::<_, u64>::new()),
|
||||
None,
|
||||
&mut overlay,
|
||||
&executor(),
|
||||
"Core_execute_block",
|
||||
&b1data,
|
||||
None,
|
||||
Default::default(),
|
||||
).execute(
|
||||
ExecutionStrategy::AlwaysWasm,
|
||||
).unwrap();
|
||||
@@ -229,12 +224,11 @@ mod tests {
|
||||
let r = StateMachine::new(
|
||||
&backend,
|
||||
Some(&InMemoryChangesTrieStorage::<_, u64>::new()),
|
||||
None,
|
||||
&mut overlay,
|
||||
&executor(),
|
||||
"Core_execute_block",
|
||||
&b1data,
|
||||
None,
|
||||
Default::default(),
|
||||
).execute(
|
||||
ExecutionStrategy::NativeElseWasm,
|
||||
);
|
||||
|
||||
@@ -20,6 +20,9 @@ use std::collections::{HashMap, HashSet};
|
||||
use std::sync::Arc;
|
||||
use parking_lot::{RwLock, Mutex};
|
||||
use primitives::{ChangesTrieConfiguration, storage::well_known_keys};
|
||||
use primitives::offchain::storage::{
|
||||
InMemOffchainStorage as OffchainStorage
|
||||
};
|
||||
use sr_primitives::generic::{BlockId, DigestItem};
|
||||
use sr_primitives::traits::{Block as BlockT, Header as HeaderT, Zero, NumberFor};
|
||||
use sr_primitives::{Justification, StorageOverlay, ChildrenStorageOverlay};
|
||||
@@ -35,9 +38,6 @@ use client_api::{
|
||||
blockchain::{
|
||||
self, BlockStatus, HeaderBackend, well_known_cache_keys::Id as CacheKeyId
|
||||
},
|
||||
offchain::{
|
||||
InMemOffchainStorage as OffchainStorage
|
||||
}
|
||||
};
|
||||
use crate::leaves::LeafSet;
|
||||
|
||||
@@ -811,7 +811,7 @@ pub fn check_genesis_storage(top: &StorageOverlay, children: &ChildrenStorageOve
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use client_api::offchain::{OffchainStorage, InMemOffchainStorage};
|
||||
use primitives::offchain::{OffchainStorage, storage::InMemOffchainStorage};
|
||||
use std::sync::Arc;
|
||||
use test_client;
|
||||
use primitives::Blake2Hasher;
|
||||
|
||||
@@ -63,7 +63,6 @@
|
||||
//! LocalCallExecutor::new(
|
||||
//! backend.clone(),
|
||||
//! NativeExecutor::<LocalExecutor>::new(WasmExecutionMethod::Interpreted, None),
|
||||
//! None,
|
||||
//! ),
|
||||
//! // This parameter provides the storage for the chain genesis.
|
||||
//! <(StorageOverlay, ChildrenStorageOverlay)>::default(),
|
||||
@@ -93,13 +92,15 @@ pub use client_api::{
|
||||
call_executor::CallExecutor,
|
||||
utils,
|
||||
};
|
||||
pub use crate::call_executor::LocalCallExecutor;
|
||||
pub use crate::client::{
|
||||
new_with_backend,
|
||||
new_in_mem,
|
||||
BlockBody, ImportNotifications, FinalityNotifications, BlockchainEvents,
|
||||
BlockImportNotification, Client, ClientInfo, ExecutionStrategies, FinalityNotification,
|
||||
LongestChain, BlockOf, ProvideUncles, ForkBlocks, apply_aux,
|
||||
pub use crate::{
|
||||
call_executor::LocalCallExecutor,
|
||||
client::{
|
||||
new_with_backend,
|
||||
new_in_mem,
|
||||
BlockBody, ImportNotifications, FinalityNotifications, BlockchainEvents,
|
||||
BlockImportNotification, Client, ClientInfo, ExecutionStrategies, FinalityNotification,
|
||||
LongestChain, BlockOf, ProvideUncles, ForkBlocks, apply_aux,
|
||||
},
|
||||
leaves::LeafSet,
|
||||
};
|
||||
pub use state_machine::{ExecutionStrategy, StorageProof};
|
||||
pub use crate::leaves::LeafSet;
|
||||
|
||||
@@ -21,8 +21,9 @@ use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use parking_lot::{RwLock, Mutex};
|
||||
|
||||
use sr_primitives::{generic::BlockId, Justification, StorageOverlay, ChildrenStorageOverlay};
|
||||
use state_machine::{Backend as StateBackend, TrieBackend, backend::InMemory as InMemoryState, ChangesTrieTransaction};
|
||||
use primitives::offchain::storage::InMemOffchainStorage;
|
||||
use sr_primitives::{generic::BlockId, Justification, StorageOverlay, ChildrenStorageOverlay};
|
||||
use sr_primitives::traits::{Block as BlockT, NumberFor, Zero, Header};
|
||||
use crate::in_mem::{self, check_genesis_storage};
|
||||
use client_api::{
|
||||
@@ -37,9 +38,8 @@ use client_api::{
|
||||
Error as ClientError, Result as ClientResult
|
||||
},
|
||||
light::Storage as BlockchainStorage,
|
||||
InMemOffchainStorage,
|
||||
};
|
||||
use crate::light::blockchain::{Blockchain};
|
||||
use crate::light::blockchain::Blockchain;
|
||||
use hash_db::Hasher;
|
||||
use trie::MemoryDB;
|
||||
|
||||
|
||||
@@ -22,12 +22,13 @@ use std::{
|
||||
|
||||
use codec::{Encode, Decode};
|
||||
use primitives::{
|
||||
offchain::OffchainExt, H256, Blake2Hasher, convert_hash, NativeOrEncoded,
|
||||
H256, Blake2Hasher, convert_hash, NativeOrEncoded,
|
||||
traits::CodeExecutor,
|
||||
};
|
||||
use sr_primitives::{
|
||||
generic::BlockId, traits::{One, Block as BlockT, Header as HeaderT, NumberFor},
|
||||
};
|
||||
use externalities::Extensions;
|
||||
use state_machine::{
|
||||
self, Backend as StateBackend, OverlayedChanges, ExecutionStrategy, create_proof_check_backend,
|
||||
execution_proof_check_on_trie_backend, ExecutionManager, ChangesTrieTransaction, StorageProof,
|
||||
@@ -84,10 +85,10 @@ impl<Block, B, Local> CallExecutor<Block, Blake2Hasher> for
|
||||
method: &str,
|
||||
call_data: &[u8],
|
||||
strategy: ExecutionStrategy,
|
||||
side_effects_handler: Option<OffchainExt>,
|
||||
extensions: Option<Extensions>,
|
||||
) -> ClientResult<Vec<u8>> {
|
||||
match self.backend.is_local_state_available(id) {
|
||||
true => self.local.call(id, method, call_data, strategy, side_effects_handler),
|
||||
true => self.local.call(id, method, call_data, strategy, extensions),
|
||||
false => Err(ClientError::NotAvailableOnLightClient),
|
||||
}
|
||||
}
|
||||
@@ -111,9 +112,8 @@ impl<Block, B, Local> CallExecutor<Block, Blake2Hasher> for
|
||||
initialize_block: InitializeBlock<'a, Block>,
|
||||
_manager: ExecutionManager<EM>,
|
||||
native_call: Option<NC>,
|
||||
side_effects_handler: Option<OffchainExt>,
|
||||
recorder: &Option<ProofRecorder<Block>>,
|
||||
enable_keystore: bool,
|
||||
extensions: Option<Extensions>,
|
||||
) -> ClientResult<NativeOrEncoded<R>> where ExecutionManager<EM>: Clone {
|
||||
// there's no actual way/need to specify native/wasm execution strategy on light node
|
||||
// => we can safely ignore passed values
|
||||
@@ -137,9 +137,8 @@ impl<Block, B, Local> CallExecutor<Block, Blake2Hasher> for
|
||||
initialize_block,
|
||||
ExecutionManager::NativeWhenPossible,
|
||||
native_call,
|
||||
side_effects_handler,
|
||||
recorder,
|
||||
enable_keystore,
|
||||
extensions,
|
||||
).map_err(|e| ClientError::Execution(Box::new(e.to_string()))),
|
||||
false => Err(ClientError::NotAvailableOnLightClient),
|
||||
}
|
||||
@@ -167,7 +166,7 @@ impl<Block, B, Local> CallExecutor<Block, Blake2Hasher> for
|
||||
_call_data: &[u8],
|
||||
_manager: ExecutionManager<FF>,
|
||||
_native_call: Option<NC>,
|
||||
_side_effects_handler: Option<OffchainExt>,
|
||||
_extensions: Option<Extensions>,
|
||||
) -> ClientResult<(
|
||||
NativeOrEncoded<R>,
|
||||
(S::Transaction, <Blake2Hasher as Hasher>::Out),
|
||||
@@ -313,7 +312,7 @@ mod tests {
|
||||
_method: &str,
|
||||
_call_data: &[u8],
|
||||
_strategy: ExecutionStrategy,
|
||||
_side_effects_handler: Option<OffchainExt>,
|
||||
_extensions: Option<Extensions>,
|
||||
) -> Result<Vec<u8>, ClientError> {
|
||||
Ok(vec![42])
|
||||
}
|
||||
@@ -337,9 +336,8 @@ mod tests {
|
||||
_initialize_block: InitializeBlock<'a, Block>,
|
||||
_execution_manager: ExecutionManager<EM>,
|
||||
_native_call: Option<NC>,
|
||||
_side_effects_handler: Option<OffchainExt>,
|
||||
_proof_recorder: &Option<ProofRecorder<Block>>,
|
||||
_enable_keystore: bool,
|
||||
_extensions: Option<Extensions>,
|
||||
) -> ClientResult<NativeOrEncoded<R>> where ExecutionManager<EM>: Clone {
|
||||
unreachable!()
|
||||
}
|
||||
@@ -363,7 +361,7 @@ mod tests {
|
||||
_call_data: &[u8],
|
||||
_manager: ExecutionManager<F>,
|
||||
_native_call: Option<NC>,
|
||||
_side_effects_handler: Option<OffchainExt>,
|
||||
_extensions: Option<Extensions>,
|
||||
) -> Result<
|
||||
(
|
||||
NativeOrEncoded<R>,
|
||||
|
||||
@@ -68,7 +68,7 @@ pub fn new_light<B, S, GS, RA, E>(
|
||||
GS: BuildStorage,
|
||||
E: CodeExecutor + RuntimeInfo,
|
||||
{
|
||||
let local_executor = LocalCallExecutor::new(backend.clone(), code_executor, None);
|
||||
let local_executor = LocalCallExecutor::new(backend.clone(), code_executor);
|
||||
let executor = GenesisCallExecutor::new(backend.clone(), local_executor);
|
||||
Client::new(backend, executor, genesis_storage, Default::default(), Default::default())
|
||||
}
|
||||
|
||||
@@ -391,6 +391,29 @@ impl<B: ChainApi> Clone for Pool<B> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: ChainApi> sr_primitives::offchain::TransactionPool<A::Block> for Pool<A> {
|
||||
fn submit_at(
|
||||
&self,
|
||||
at: &BlockId<A::Block>,
|
||||
extrinsic: <A::Block as sr_primitives::traits::Block>::Extrinsic,
|
||||
) -> Result<(), ()> {
|
||||
log::debug!(
|
||||
target: "txpool",
|
||||
"(offchain call) Submitting a transaction to the pool: {:?}",
|
||||
extrinsic
|
||||
);
|
||||
|
||||
let result = futures::executor::block_on(self.submit_one(&at, extrinsic));
|
||||
|
||||
result.map(|_| ())
|
||||
.map_err(|e| log::warn!(
|
||||
target: "txpool",
|
||||
"(offchain call) Error submitting a transaction to the pool: {:?}",
|
||||
e
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::{
|
||||
|
||||
Reference in New Issue
Block a user