Skip to content

Commit

Permalink
Move registration of ReadRuntimeVersionExt to ExecutionExtension (p…
Browse files Browse the repository at this point in the history
…aritytech#13820)

Instead of registering `ReadRuntimeVersionExt` in `sp-state-machine` it is moved to
`ExecutionExtension` which provides the default extensions.
  • Loading branch information
bkchr authored and nathanwhit committed Jul 19, 2023
1 parent 3e2d43f commit b09d5c3
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 27 deletions.
9 changes: 7 additions & 2 deletions bin/node/testing/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 6 additions & 12 deletions client/api/src/execution_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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> {
Expand All @@ -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(());
Expand All @@ -202,6 +193,7 @@ impl<Block: BlockT> ExecutionExtensions<Block> {
offchain_db,
extensions_factory: RwLock::new(extensions_factory),
transaction_pool,
read_runtime_version,
}
}

Expand Down Expand Up @@ -271,6 +263,8 @@ impl<Block: BlockT> ExecutionExtensions<Block> {
)));
}

extensions.register(ReadRuntimeVersionExt::new(self.read_runtime_version.clone()));

extensions
}

Expand Down
1 change: 1 addition & 0 deletions client/service/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions client/service/src/client/call_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ mod tests {
Default::default(),
None,
None,
Arc::new(executor.clone()),
)),
};

Expand Down
1 change: 1 addition & 0 deletions client/service/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ where
Default::default(),
keystore,
sc_offchain::OffchainDb::factory_from_backend(&*backend),
Arc::new(executor.clone()),
);

let call_executor =
Expand Down
10 changes: 10 additions & 0 deletions primitives/core/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>);
Expand Down
6 changes: 2 additions & 4 deletions primitives/state-machine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion test-utils/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
3 changes: 2 additions & 1 deletion utils/frame/benchmarking-cli/src/pallet/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
};

Expand Down
2 changes: 1 addition & 1 deletion utils/frame/try-runtime/cli/src/commands/execute_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ where
&executor,
"TryRuntime_execute_block",
&payload,
full_extensions(),
full_extensions(executor.clone()),
shared.export_proof,
)?;

Expand Down
4 changes: 2 additions & 2 deletions utils/frame/try-runtime/cli/src/commands/fast_forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?)
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion utils/frame/try-runtime/cli/src/commands/follow_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ where
&executor,
"OffchainWorkerApi_offchain_worker",
&payload,
full_extensions(),
full_extensions(executor.clone()),
)?;

Ok(())
Expand Down
5 changes: 3 additions & 2 deletions utils/frame/try-runtime/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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
}
Expand Down

0 comments on commit b09d5c3

Please sign in to comment.