Skip to content

Commit

Permalink
Merge pull request #2252 from subspace/cleanup_abstractions
Browse files Browse the repository at this point in the history
Remove RuntimeAPI Full and Light abstractions
  • Loading branch information
vedhavyas authored Nov 20, 2023
2 parents 86a3084 + 19b5d56 commit dcee91e
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 344 deletions.
106 changes: 40 additions & 66 deletions crates/sp-domains-fraud-proof/src/host_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ use crate::{
};
use codec::{Decode, Encode};
use domain_block_preprocessor::inherents::extract_domain_runtime_upgrade_code;
use domain_block_preprocessor::runtime_api::{
SetCodeConstructor, SignerExtractor, TimestampExtrinsicConstructor,
};
use domain_block_preprocessor::runtime_api_light::RuntimeApiLight;
use domain_block_preprocessor::stateless_runtime::StatelessRuntime;
use sc_client_api::BlockBackend;
use sc_executor::RuntimeVersionOf;
use sp_api::{BlockT, HashT, ProvideRuntimeApi};
Expand Down Expand Up @@ -121,18 +118,13 @@ where
let runtime_code = self.get_domain_runtime_code(consensus_block_hash, domain_id)?;
let timestamp = runtime_api.timestamp(consensus_block_hash.into()).ok()?;

let domain_runtime_api_light =
RuntimeApiLight::new(self.executor.clone(), runtime_code.into());
let domain_stateless_runtime =
StatelessRuntime::<DomainBlock, _>::new(self.executor.clone(), runtime_code.into());

TimestampExtrinsicConstructor::<DomainBlock>::construct_timestamp_extrinsic(
&domain_runtime_api_light,
// We do not care about the domain hash since this is stateless call into
// domain runtime,
Default::default(),
timestamp,
)
.ok()
.map(|ext| ext.encode())
domain_stateless_runtime
.construct_timestamp_extrinsic(timestamp)
.ok()
.map(|ext| ext.encode())
}

fn get_domain_bundle_body(
Expand Down Expand Up @@ -174,18 +166,13 @@ where

if let Some(upgraded_runtime) = maybe_upgraded_runtime {
let runtime_code = self.get_domain_runtime_code(consensus_block_hash, domain_id)?;
let domain_runtime_api_light =
RuntimeApiLight::new(self.executor.clone(), runtime_code.into());

SetCodeConstructor::<DomainBlock>::construct_set_code_extrinsic(
&domain_runtime_api_light,
// We do not care about the domain hash since this is stateless call into
// domain runtime,
Default::default(),
upgraded_runtime,
)
.ok()
.map(|ext| SetCodeExtrinsic::EncodedExtrinsic(ext.encode()))
let domain_stateless_runtime =
StatelessRuntime::<DomainBlock, _>::new(self.executor.clone(), runtime_code.into());

domain_stateless_runtime
.construct_set_code_extrinsic(upgraded_runtime)
.ok()
.map(|ext| SetCodeExtrinsic::EncodedExtrinsic(ext.encode()))
} else {
Some(SetCodeExtrinsic::None)
}
Expand Down Expand Up @@ -238,22 +225,16 @@ where
let bundle_vrf_hash =
U256::from_be_bytes(bundle.sealed_header.header.proof_of_election.vrf_hash());

let domain_runtime_api_light =
RuntimeApiLight::new(self.executor.clone(), runtime_code.into());
let domain_stateless_runtime =
StatelessRuntime::<DomainBlock, _>::new(self.executor.clone(), runtime_code.into());

let encoded_extrinsic = opaque_extrinsic.encode();
let extrinsic =
<DomainBlock as BlockT>::Extrinsic::decode(&mut encoded_extrinsic.as_slice()).ok()?;

<RuntimeApiLight<Executor> as domain_runtime_primitives::DomainCoreApi<
DomainBlock,
>>::is_within_tx_range(
&domain_runtime_api_light,
Default::default(), // Doesn't matter for RuntimeApiLight
&extrinsic,
&bundle_vrf_hash,
&domain_tx_range,
).ok()
domain_stateless_runtime
.is_within_tx_range(&extrinsic, &bundle_vrf_hash, &domain_tx_range)
.ok()
}

fn is_inherent_extrinsic(
Expand All @@ -264,20 +245,16 @@ where
) -> Option<bool> {
let runtime_code = self.get_domain_runtime_code(consensus_block_hash, domain_id)?;

let domain_runtime_api_light =
RuntimeApiLight::new(self.executor.clone(), runtime_code.into());
let domain_stateless_runtime =
StatelessRuntime::<DomainBlock, _>::new(self.executor.clone(), runtime_code.into());

let encoded_extrinsic = opaque_extrinsic.encode();
let extrinsic =
<DomainBlock as BlockT>::Extrinsic::decode(&mut encoded_extrinsic.as_slice()).ok()?;

<RuntimeApiLight<Executor> as domain_runtime_primitives::DomainCoreApi<
DomainBlock,
>>::is_inherent_extrinsic(
&domain_runtime_api_light,
Default::default(), // Doesn't matter for RuntimeApiLight
&extrinsic
).ok()
domain_stateless_runtime
.is_inherent_extrinsic(&extrinsic)
.ok()
}

fn get_domain_election_params(
Expand Down Expand Up @@ -410,25 +387,22 @@ where
}

let domain_runtime_code = self.get_domain_runtime_code(consensus_block_hash, domain_id)?;
let domain_runtime_api_light =
RuntimeApiLight::new(self.executor.clone(), domain_runtime_code.into());

let ext_signers: Vec<_> = SignerExtractor::<DomainBlock>::extract_signer(
&domain_runtime_api_light,
// `extract_signer` is a stateless runtime api thus it is okay to use
// default block hash
Default::default(),
extrinsics,
)
.ok()?
.into_iter()
.map(|(signer, tx)| {
(
signer,
<DomainBlock::Header as HeaderT>::Hashing::hash_of(&tx),
)
})
.collect();
let domain_stateless_runtime = StatelessRuntime::<DomainBlock, _>::new(
self.executor.clone(),
domain_runtime_code.into(),
);

let ext_signers: Vec<_> = domain_stateless_runtime
.extract_signer(extrinsics)
.ok()?
.into_iter()
.map(|(signer, tx)| {
(
signer,
<DomainBlock::Header as HeaderT>::Hashing::hash_of(&tx),
)
})
.collect();

Some(<DomainBlock::Header as HeaderT>::Hashing::hash_of(&ext_signers).into())
}
Expand Down
28 changes: 15 additions & 13 deletions crates/sp-domains-fraud-proof/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use codec::Encode;
use domain_block_preprocessor::runtime_api_light::RuntimeApiLight;
use domain_block_preprocessor::stateless_runtime::StatelessRuntime;
use domain_runtime_primitives::{CheckTxValidityError, DomainCoreApi};
use domain_test_service::domain::EvmDomainClient as DomainClient;
use domain_test_service::evm_domain_test_runtime::Runtime as TestRuntime;
Expand All @@ -17,7 +17,6 @@ use sp_trie::{read_trie_value, LayoutV1};
use std::collections::BTreeMap;
use std::sync::Arc;
use subspace_runtime_primitives::opaque::Block;
use subspace_test_client::TestExecutorDispatch;
use subspace_test_service::{produce_blocks, MockConsensusNode};
use tempfile::TempDir;

Expand Down Expand Up @@ -188,8 +187,8 @@ async fn check_tx_validity_runtime_api_should_work() {
.unwrap()
.unwrap();

let mut runtime_api_light =
RuntimeApiLight::new(ferdie.executor.clone().into(), wasm_bundle.into());
let mut domain_stateless_runtime =
StatelessRuntime::<Block, _>::new(ferdie.executor.clone().into(), wasm_bundle.into());

let db = storage_proof.into_memory_db::<BlakeTwo256>();
let mut top_storage_map = BTreeMap::new();
Expand All @@ -211,15 +210,18 @@ async fn check_tx_validity_runtime_api_should_work() {
children_default: Default::default(),
};

runtime_api_light.set_storage(storage);

assert_eq!(<RuntimeApiLight<sc_executor::NativeElseWasmExecutor<TestExecutorDispatch>> as DomainCoreApi<Block>>::check_transaction_validity(
&runtime_api_light,
Default::default(),
&opaque_extrinsic,
bob.client.as_ref().info().best_number,
bob.client.as_ref().info().best_hash
).unwrap(), expected_out);
domain_stateless_runtime.set_storage(storage);

assert_eq!(
domain_stateless_runtime
.check_transaction_validity(
&opaque_extrinsic,
bob.client.as_ref().info().best_number,
bob.client.as_ref().info().best_hash
)
.unwrap(),
expected_out
);
}
}

Expand Down
27 changes: 8 additions & 19 deletions domains/client/block-preprocessor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,14 @@
#![warn(rust_2018_idioms)]

pub mod inherents;
pub mod runtime_api;
pub mod runtime_api_full;
pub mod runtime_api_light;
pub mod stateless_runtime;
pub mod xdm_verifier;

use crate::inherents::is_runtime_upgraded;
use crate::runtime_api::{SetCodeConstructor, SignerExtractor, StateRootExtractor};
use crate::xdm_verifier::is_valid_xdm;
use codec::{Decode, Encode};
use domain_runtime_primitives::opaque::AccountId;
use domain_runtime_primitives::DomainCoreApi;
use runtime_api::TimestampExtrinsicConstructor;
use sc_client_api::BlockBackend;
use sp_api::{HashT, ProvideRuntimeApi};
use sp_blockchain::HeaderBackend;
Expand Down Expand Up @@ -74,24 +70,22 @@ pub struct PreprocessResult<Block: BlockT> {
pub bundles: Vec<InboxedBundle<Block::Hash>>,
}

pub struct DomainBlockPreprocessor<Block, CBlock, Client, CClient, RuntimeApi, ReceiptValidator> {
pub struct DomainBlockPreprocessor<Block, CBlock, Client, CClient, ReceiptValidator> {
domain_id: DomainId,
client: Arc<Client>,
consensus_client: Arc<CClient>,
runtime_api: RuntimeApi,
receipt_validator: ReceiptValidator,
_phantom_data: PhantomData<(Block, CBlock)>,
}

impl<Block, CBlock, Client, CClient, RuntimeApi: Clone, ReceiptValidator: Clone> Clone
for DomainBlockPreprocessor<Block, CBlock, Client, CClient, RuntimeApi, ReceiptValidator>
impl<Block, CBlock, Client, CClient, ReceiptValidator: Clone> Clone
for DomainBlockPreprocessor<Block, CBlock, Client, CClient, ReceiptValidator>
{
fn clone(&self) -> Self {
Self {
domain_id: self.domain_id,
client: self.client.clone(),
consensus_client: self.consensus_client.clone(),
runtime_api: self.runtime_api.clone(),
receipt_validator: self.receipt_validator.clone(),
_phantom_data: self._phantom_data,
}
Expand All @@ -115,18 +109,14 @@ where
) -> sp_blockchain::Result<ReceiptValidity>;
}

impl<Block, CBlock, Client, CClient, RuntimeApi, ReceiptValidator>
DomainBlockPreprocessor<Block, CBlock, Client, CClient, RuntimeApi, ReceiptValidator>
impl<Block, CBlock, Client, CClient, ReceiptValidator>
DomainBlockPreprocessor<Block, CBlock, Client, CClient, ReceiptValidator>
where
Block: BlockT,
Block::Hash: Into<H256>,
CBlock: BlockT,
CBlock::Hash: From<Block::Hash>,
NumberFor<CBlock>: From<NumberFor<Block>>,
RuntimeApi: SignerExtractor<Block>
+ StateRootExtractor<Block>
+ SetCodeConstructor<Block>
+ TimestampExtrinsicConstructor<Block>,
Client: HeaderBackend<Block> + ProvideRuntimeApi<Block> + 'static,
Client::Api: DomainCoreApi<Block> + MessengerApi<Block, NumberFor<Block>>,
CClient: HeaderBackend<CBlock>
Expand All @@ -142,14 +132,12 @@ where
domain_id: DomainId,
client: Arc<Client>,
consensus_client: Arc<CClient>,
runtime_api: RuntimeApi,
receipt_validator: ReceiptValidator,
) -> Self {
Self {
domain_id,
client,
consensus_client,
runtime_api,
receipt_validator,
_phantom_data: Default::default(),
}
Expand Down Expand Up @@ -214,11 +202,12 @@ where
let mut inboxed_bundles = Vec::with_capacity(bundles.len());
let mut valid_extrinsics = Vec::new();

let runtime_api = self.client.runtime_api();
for bundle in bundles {
let extrinsic_root = bundle.extrinsics_root();
match self.check_bundle_validity(&bundle, &tx_range, at)? {
BundleValidity::Valid(extrinsics) => {
let extrinsics: Vec<_> = match self.runtime_api.extract_signer(at, extrinsics) {
let extrinsics: Vec<_> = match runtime_api.extract_signer(at, extrinsics) {
Ok(res) => res,
Err(e) => {
tracing::error!(error = ?e, "Error at calling runtime api: extract_signer");
Expand Down
61 changes: 0 additions & 61 deletions domains/client/block-preprocessor/src/runtime_api.rs

This file was deleted.

Loading

0 comments on commit dcee91e

Please sign in to comment.