Move registration of ReadRuntimeVersionExt to ExecutionExtension (#13820)

Instead of registering `ReadRuntimeVersionExt` in `sp-state-machine` it is moved to
`ExecutionExtension` which provides the default extensions.
This commit is contained in:
Bastian Köcher
2023-04-05 14:27:26 +02:00
committed by GitHub
parent d3f5c70dde
commit 495773f96d
14 changed files with 40 additions and 27 deletions
+7 -2
View File
@@ -411,11 +411,16 @@ impl BenchDb {
let client = sc_service::new_client(
backend.clone(),
executor,
executor.clone(),
genesis_block_builder,
None,
None,
ExecutionExtensions::new(profile.into_execution_strategies(), None, None),
ExecutionExtensions::new(
profile.into_execution_strategies(),
None,
None,
Arc::new(executor),
),
Box::new(task_executor.clone()),
None,
None,
@@ -27,6 +27,7 @@ use parking_lot::RwLock;
use sc_transaction_pool_api::OffchainSubmitTransaction;
use sp_core::{
offchain::{self, OffchainDbExt, OffchainWorkerExt, TransactionPoolExt},
traits::{ReadRuntimeVersion, ReadRuntimeVersionExt},
ExecutionContext,
};
use sp_externalities::{Extension, Extensions};
@@ -173,18 +174,7 @@ pub struct ExecutionExtensions<Block: BlockT> {
// during initialization.
transaction_pool: RwLock<Option<Weak<dyn OffchainSubmitTransaction<Block>>>>,
extensions_factory: RwLock<Box<dyn ExtensionsFactory<Block>>>,
}
impl<Block: BlockT> Default for ExecutionExtensions<Block> {
fn default() -> Self {
Self {
strategies: Default::default(),
keystore: None,
offchain_db: None,
transaction_pool: RwLock::new(None),
extensions_factory: RwLock::new(Box::new(())),
}
}
read_runtime_version: Arc<dyn ReadRuntimeVersion>,
}
impl<Block: BlockT> ExecutionExtensions<Block> {
@@ -193,6 +183,7 @@ impl<Block: BlockT> ExecutionExtensions<Block> {
strategies: ExecutionStrategies,
keystore: Option<KeystorePtr>,
offchain_db: Option<Box<dyn DbExternalitiesFactory>>,
read_runtime_version: Arc<dyn ReadRuntimeVersion>,
) -> Self {
let transaction_pool = RwLock::new(None);
let extensions_factory = Box::new(());
@@ -202,6 +193,7 @@ impl<Block: BlockT> ExecutionExtensions<Block> {
offchain_db,
extensions_factory: RwLock::new(extensions_factory),
transaction_pool,
read_runtime_version,
}
}
@@ -271,6 +263,8 @@ impl<Block: BlockT> ExecutionExtensions<Block> {
)));
}
extensions.register(ReadRuntimeVersionExt::new(self.read_runtime_version.clone()));
extensions
}
+1
View File
@@ -181,6 +181,7 @@ where
config.execution_strategies.clone(),
Some(keystore_container.keystore()),
sc_offchain::OffchainDb::factory_from_backend(&*backend),
Arc::new(executor.clone()),
);
let wasm_runtime_substitutes = config
@@ -427,6 +427,7 @@ mod tests {
Default::default(),
None,
None,
Arc::new(executor.clone()),
)),
};
@@ -243,6 +243,7 @@ where
Default::default(),
keystore,
sc_offchain::OffchainDb::factory_from_backend(&*backend),
Arc::new(executor.clone()),
);
let call_executor =
+10
View File
@@ -157,6 +157,16 @@ pub trait ReadRuntimeVersion: Send + Sync {
) -> Result<Vec<u8>, String>;
}
impl ReadRuntimeVersion for std::sync::Arc<dyn ReadRuntimeVersion> {
fn read_runtime_version(
&self,
wasm_code: &[u8],
ext: &mut dyn Externalities,
) -> Result<Vec<u8>, String> {
(**self).read_runtime_version(wasm_code, ext)
}
}
sp_externalities::decl_extension! {
/// An extension that provides functionality to read version information from a given wasm blob.
pub struct ReadRuntimeVersionExt(Box<dyn ReadRuntimeVersion>);
@@ -163,7 +163,7 @@ mod execution {
use sp_core::{
hexdisplay::HexDisplay,
storage::{ChildInfo, ChildType, PrefixedStorageKey},
traits::{CallContext, CodeExecutor, ReadRuntimeVersionExt, RuntimeCode},
traits::{CallContext, CodeExecutor, RuntimeCode},
};
use sp_externalities::Extensions;
use std::{
@@ -322,12 +322,10 @@ mod execution {
exec: &'a Exec,
method: &'a str,
call_data: &'a [u8],
mut extensions: Extensions,
extensions: Extensions,
runtime_code: &'a RuntimeCode,
context: CallContext,
) -> Self {
extensions.register(ReadRuntimeVersionExt::new(exec.clone()));
Self {
backend,
exec,
+2 -1
View File
@@ -290,12 +290,13 @@ impl<Block: BlockT, D, Backend, G: GenesisInit>
});
let executor = LocalCallExecutor::new(
self.backend.clone(),
executor,
executor.clone(),
Default::default(),
ExecutionExtensions::new(
self.execution_strategies.clone(),
self.keystore.clone(),
sc_offchain::OffchainDb::factory_from_backend(&*self.backend),
Arc::new(executor),
),
)
.expect("Creates LocalCallExecutor");
@@ -35,7 +35,7 @@ use sp_core::{
testing::{TestOffchainExt, TestTransactionPoolExt},
OffchainDbExt, OffchainWorkerExt, TransactionPoolExt,
},
traits::CallContext,
traits::{CallContext, ReadRuntimeVersionExt},
};
use sp_externalities::Extensions;
use sp_keystore::{testing::MemoryKeystore, KeystoreExt};
@@ -225,6 +225,7 @@ impl PalletCmd {
extensions.register(OffchainWorkerExt::new(offchain.clone()));
extensions.register(OffchainDbExt::new(offchain));
extensions.register(TransactionPoolExt::new(pool));
extensions.register(ReadRuntimeVersionExt::new(executor.clone()));
extensions
};
@@ -133,7 +133,7 @@ where
&executor,
"TryRuntime_execute_block",
&payload,
full_extensions(),
full_extensions(executor.clone()),
shared.export_proof,
)?;
@@ -103,7 +103,7 @@ async fn dry_run<T: Decode, Block: BlockT, HostFns: HostFunctions>(
executor,
method,
data,
full_extensions(),
full_extensions(executor.clone()),
)?;
Ok(<T>::decode(&mut &*result)?)
@@ -121,7 +121,7 @@ async fn run<Block: BlockT, HostFns: HostFunctions>(
executor,
method,
data,
full_extensions(),
full_extensions(executor.clone()),
)?;
let storage_changes = changes.drain_storage_changes(
@@ -149,7 +149,7 @@ where
&executor,
"TryRuntime_execute_block",
(block, command.state_root_check, command.try_state.clone()).encode().as_ref(),
full_extensions(),
full_extensions(executor.clone()),
shared
.export_proof
.as_ref()
@@ -97,7 +97,7 @@ where
&executor,
"OffchainWorkerApi_offchain_worker",
&payload,
full_extensions(),
full_extensions(executor.clone()),
)?;
Ok(())
@@ -377,7 +377,7 @@ use sp_core::{
OffchainDbExt, OffchainWorkerExt, TransactionPoolExt,
},
storage::well_known_keys,
traits::{CallContext, ReadRuntimeVersion},
traits::{CallContext, ReadRuntimeVersion, ReadRuntimeVersionExt},
twox_128, H256,
};
use sp_externalities::Extensions;
@@ -810,7 +810,7 @@ where
}
/// Build all extensions that we typically use.
pub(crate) fn full_extensions() -> Extensions {
pub(crate) fn full_extensions<H: HostFunctions>(wasm_executor: WasmExecutor<H>) -> Extensions {
let mut extensions = Extensions::default();
let (offchain, _offchain_state) = TestOffchainExt::new();
let (pool, _pool_state) = TestTransactionPoolExt::new();
@@ -819,6 +819,7 @@ pub(crate) fn full_extensions() -> Extensions {
extensions.register(OffchainWorkerExt::new(offchain));
extensions.register(KeystoreExt::new(keystore));
extensions.register(TransactionPoolExt::new(pool));
extensions.register(ReadRuntimeVersionExt::new(wasm_executor));
extensions
}