|
| 1 | +// Copyright (C) 2021 Subspace Labs, Inc. |
| 2 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | + |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +//! Domain specific Host functions and Extension factory |
| 18 | +
|
| 19 | +use sc_client_api::execution_extensions::ExtensionsFactory as ExtensionsFactoryT; |
| 20 | +use sc_executor::RuntimeVersionOf; |
| 21 | +use sp_api::ProvideRuntimeApi; |
| 22 | +use sp_blockchain::HeaderBackend; |
| 23 | +use sp_core::traits::CodeExecutor; |
| 24 | +use sp_core::H256; |
| 25 | +use sp_domains::DomainsApi; |
| 26 | +use sp_externalities::Extensions; |
| 27 | +use sp_messenger_host_functions::{MessengerApi, MessengerExtension, MessengerHostFunctionsImpl}; |
| 28 | +use sp_runtime::traits::{Block as BlockT, NumberFor}; |
| 29 | +use sp_subspace_mmr::host_functions::{MmrApi, SubspaceMmrExtension, SubspaceMmrHostFunctionsImpl}; |
| 30 | +use std::marker::PhantomData; |
| 31 | +use std::sync::Arc; |
| 32 | + |
| 33 | +/// Host functions required for Subspace domain |
| 34 | +#[cfg(not(feature = "runtime-benchmarks"))] |
| 35 | +pub type HostFunctions = ( |
| 36 | + sp_io::SubstrateHostFunctions, |
| 37 | + sp_messenger_host_functions::HostFunctions, |
| 38 | + sp_subspace_mmr::DomainHostFunctions, |
| 39 | +); |
| 40 | + |
| 41 | +/// Host functions required for Subspace domain |
| 42 | +#[cfg(feature = "runtime-benchmarks")] |
| 43 | +pub type HostFunctions = ( |
| 44 | + sp_io::SubstrateHostFunctions, |
| 45 | + sp_messenger_host_functions::HostFunctions, |
| 46 | + sp_subspace_mmr::DomainHostFunctions, |
| 47 | + frame_benchmarking::benchmarking::HostFunctions, |
| 48 | +); |
| 49 | + |
| 50 | +/// Runtime executor for Domains |
| 51 | +pub type RuntimeExecutor = sc_executor::WasmExecutor<HostFunctions>; |
| 52 | + |
| 53 | +/// Extensions factory for subspace domains. |
| 54 | +pub struct ExtensionsFactory<CClient, CBlock, Block, Executor> { |
| 55 | + consensus_client: Arc<CClient>, |
| 56 | + executor: Arc<Executor>, |
| 57 | + _marker: PhantomData<(CBlock, Block)>, |
| 58 | +} |
| 59 | + |
| 60 | +impl<CClient, CBlock, Block, Executor> ExtensionsFactory<CClient, CBlock, Block, Executor> { |
| 61 | + pub fn new(consensus_client: Arc<CClient>, executor: Arc<Executor>) -> Self { |
| 62 | + Self { |
| 63 | + consensus_client, |
| 64 | + executor, |
| 65 | + _marker: Default::default(), |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl<CClient, CBlock, Block, Executor> ExtensionsFactoryT<Block> |
| 71 | + for ExtensionsFactory<CClient, CBlock, Block, Executor> |
| 72 | +where |
| 73 | + Block: BlockT, |
| 74 | + CBlock: BlockT, |
| 75 | + CBlock::Hash: From<H256>, |
| 76 | + CClient: HeaderBackend<CBlock> + ProvideRuntimeApi<CBlock> + 'static, |
| 77 | + CClient::Api: MmrApi<CBlock, H256, NumberFor<CBlock>> |
| 78 | + + MessengerApi<CBlock, NumberFor<CBlock>> |
| 79 | + + DomainsApi<CBlock, Block::Header>, |
| 80 | + Executor: CodeExecutor + RuntimeVersionOf, |
| 81 | +{ |
| 82 | + fn extensions_for( |
| 83 | + &self, |
| 84 | + _block_hash: Block::Hash, |
| 85 | + _block_number: NumberFor<Block>, |
| 86 | + ) -> Extensions { |
| 87 | + let mut exts = Extensions::new(); |
| 88 | + exts.register(SubspaceMmrExtension::new(Arc::new( |
| 89 | + SubspaceMmrHostFunctionsImpl::<CBlock, _>::new(self.consensus_client.clone()), |
| 90 | + ))); |
| 91 | + |
| 92 | + exts.register(MessengerExtension::new(Arc::new( |
| 93 | + MessengerHostFunctionsImpl::<CBlock, _, Block, _>::new( |
| 94 | + self.consensus_client.clone(), |
| 95 | + self.executor.clone(), |
| 96 | + ), |
| 97 | + ))); |
| 98 | + |
| 99 | + exts |
| 100 | + } |
| 101 | +} |
0 commit comments