Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions client/api/src/execution_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ impl Default for ExecutionStrategies {
}
}

/// Generate the starting set of ExternalitiesExtensions based upon the given capabilities
pub trait ExtensionsFactory: Send + Sync {
/// Make `Extensions` for given Capapbilities
fn extensions_for(&self, capabilities: offchain::Capabilities) -> Extensions;
}

impl ExtensionsFactory for () {
fn extensions_for(&self, _capabilities: offchain::Capabilities) -> Extensions {
Extensions::new()
}
}

/// A producer of execution extensions for offchain calls.
///
/// This crate aggregates extensions available for the offchain calls
Expand All @@ -70,7 +82,10 @@ impl Default for ExecutionStrategies {
pub struct ExecutionExtensions<Block: traits::Block> {
strategies: ExecutionStrategies,
keystore: Option<BareCryptoStorePtr>,
// FIXME: these two are only RwLock because of https://github.com/paritytech/substrate/issues/4587
// remove when fixed.
transaction_pool: RwLock<Option<Weak<dyn sp_transaction_pool::OffchainSubmitTransaction<Block>>>>,
extensions_factory: RwLock<Box<dyn ExtensionsFactory>>,
}

impl<Block: traits::Block> Default for ExecutionExtensions<Block> {
Expand All @@ -79,6 +94,7 @@ impl<Block: traits::Block> Default for ExecutionExtensions<Block> {
strategies: Default::default(),
keystore: None,
transaction_pool: RwLock::new(None),
extensions_factory: RwLock::new(Box::new(())),
}
}
}
Expand All @@ -90,14 +106,20 @@ impl<Block: traits::Block> ExecutionExtensions<Block> {
keystore: Option<BareCryptoStorePtr>,
) -> Self {
let transaction_pool = RwLock::new(None);
Self { strategies, keystore, transaction_pool }
let extensions_factory = Box::new(());
Self { strategies, keystore, extensions_factory: RwLock::new(extensions_factory), transaction_pool }
}

/// Get a reference to the execution strategies.
pub fn strategies(&self) -> &ExecutionStrategies {
&self.strategies
}

/// Set the new extensions_factory
pub fn set_extensions_factory(&self, maker: Box<dyn ExtensionsFactory>) {
*self.extensions_factory.write() = maker;
}

/// Register transaction pool extension.
///
/// To break retain cycle between `Client` and `TransactionPool` we require this
Expand Down Expand Up @@ -135,7 +157,7 @@ impl<Block: traits::Block> ExecutionExtensions<Block> {

let capabilities = context.capabilities();

let mut extensions = Extensions::new();
let mut extensions = self.extensions_factory.read().extensions_for(capabilities);

if capabilities.has(offchain::Capability::Keystore) {
if let Some(keystore) = self.keystore.as_ref() {
Expand Down
8 changes: 8 additions & 0 deletions client/service/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use sc_client_api::{
self,
BlockchainEvents,
backend::RemoteBackend, light::RemoteBlockchain,
execution_extensions::ExtensionsFactory,
};
use sc_client::Client;
use sc_chain_spec::{RuntimeGenesis, Extension};
Expand Down Expand Up @@ -746,6 +747,13 @@ ServiceBuilder<
+ TransactionPoolMaintainer<Block=TBl, Hash = <TBl as BlockT>::Hash>,
TRpc: sc_rpc::RpcExtension<sc_rpc::Metadata> + Clone,
{

/// Set an ExecutionExtensionsFactory
pub fn with_execution_extensions_factory(self, execution_extensions_factory: Box<dyn ExtensionsFactory>) -> Result<Self, Error> {
self.client.execution_extensions().set_extensions_factory(execution_extensions_factory);
Ok(self)
}

/// Builds the service.
pub fn build(self) -> Result<Service<
TBl,
Expand Down