Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove runtime version check #3066

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 0 additions & 1 deletion crates/sp-domains-fraud-proof/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ impl PassBy for StatelessDomainRuntimeCall {

sp_api::decl_runtime_apis! {
/// API necessary for fraud proof.
#[api_version(2)]
pub trait FraudProofApi<DomainHeader: HeaderT> {
/// Submit the fraud proof via an unsigned extrinsic.
fn submit_fraud_proof_unsigned(fraud_proof: FraudProof<NumberFor<Block>, Block::Hash, DomainHeader, H256>);
Expand Down
21 changes: 6 additions & 15 deletions crates/sp-domains-fraud-proof/src/storage_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ impl DomainInherentExtrinsicDataProof {
domain_id: DomainId,
block_hash: Block::Hash,
maybe_runtime_id: Option<RuntimeId>,
should_include_domain_sudo_call: bool,
) -> Result<Self, GenerationError> {
let timestamp_proof =
TimestampStorageProof::generate(proof_provider, block_hash, (), storage_key_provider)?;
Expand Down Expand Up @@ -465,20 +464,12 @@ impl DomainInherentExtrinsicDataProof {
storage_key_provider,
)?;

// Domain sudo call is optional since both Consensus and domain runtimes needs to have the functionality.
// If only consensus runtime is upgraded but not Domain, the storage proof will never contain the data
// Since sudo call extrinsic on Consensus will never go through.
// but it can still generate empty storage proof in this case
let maybe_domain_sudo_call_proof = if should_include_domain_sudo_call {
Some(DomainSudoCallStorageProof::generate(
proof_provider,
block_hash,
domain_id,
storage_key_provider,
)?)
} else {
None
};
let maybe_domain_sudo_call_proof = Some(DomainSudoCallStorageProof::generate(
nazar-pc marked this conversation as resolved.
Show resolved Hide resolved
proof_provider,
block_hash,
domain_id,
storage_key_provider,
)?);

Ok(Self {
timestamp_proof,
Expand Down
1 change: 0 additions & 1 deletion crates/sp-domains/src/core_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use subspace_runtime_primitives::Moment;

sp_api::decl_runtime_apis! {
/// Base API that every domain runtime must implement.
#[api_version(2)]
pub trait DomainCoreApi {
/// Extracts the optional signer per extrinsic.
fn extract_signer(
Expand Down
1 change: 0 additions & 1 deletion crates/sp-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,6 @@ impl<Balance> OnChainRewards<Balance> for () {

sp_api::decl_runtime_apis! {
/// API necessary for domains pallet.
#[api_version(6)]
pub trait DomainsApi<DomainHeader: HeaderT> {
/// Submits the transaction bundle via an unsigned extrinsic.
fn submit_bundle_unsigned(opaque_bundle: OpaqueBundle<NumberFor<Block>, Block::Hash, DomainHeader, Balance>);
Expand Down
18 changes: 1 addition & 17 deletions crates/subspace-service/src/domains/request_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use parity_scale_codec::{Decode, Encode};
use sc_client_api::{BlockBackend, ProofProvider};
use sc_network::request_responses::{IncomingRequest, OutgoingResponse};
use sc_network::{NetworkBackend, PeerId};
use sp_api::{ApiExt, ProvideRuntimeApi};
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_domains::{DomainId, DomainsApi, ExecutionReceiptFor};
use sp_domains_fraud_proof::FraudProofApi;
Expand Down Expand Up @@ -174,19 +174,6 @@ where
info.best_hash
};

let consensus_api_version = self
.client
.runtime_api()
.api_version::<dyn DomainsApi<Block, Block::Header>>(target_block_hash)
.map_err(sp_blockchain::Error::RuntimeApiError)?
.ok_or_else(|| HandleRequestError::ApiVersionNotSupported)?;

if consensus_api_version < 6 {
debug!("Incorrect API version to support the last confirmed block request: {consensus_api_version}");

return Err(HandleRequestError::ApiVersionNotSupported);
}

let last_confirmed_block_receipt = self
.client
.runtime_api()
Expand Down Expand Up @@ -245,9 +232,6 @@ enum HandleRequestError {
#[error("Failed to send response.")]
SendResponse,

#[error("Api version is not supported.")]
ApiVersionNotSupported,

#[error("Failed to decode request: {0}.")]
Decode(#[from] codec::Error),

Expand Down
15 changes: 1 addition & 14 deletions domains/client/block-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,20 +176,7 @@ where
api.record_proof();
}

let core_version = api
.api_version::<dyn Core<Block>>(parent_hash)?
.ok_or_else(|| Error::VersionInvalid("Core".to_string()))?;

if core_version >= 5 {
if api.initialize_block(parent_hash, &header).is_err() {
// TODO: Hack for Subspace fork caused by
// https://github.com/subspace/polkadot-sdk/commit/447bbc765020674614e9ac982163f7e11e5b03ea
// Replace with error propagation before next network
}
} else {
#[allow(deprecated)]
api.initialize_block_before_version_5(parent_hash, &header)?;
}
api.initialize_block(parent_hash, &header)?;
nazar-pc marked this conversation as resolved.
Show resolved Hide resolved

if let Some(inherent_data) = maybe_inherent_data {
let inherent_extrinsics = Self::create_inherents(parent_hash, &api, inherent_data)?;
Expand Down
31 changes: 5 additions & 26 deletions domains/client/block-preprocessor/src/inherents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! Deriving these extrinsics during fraud proof verification should be possible since
//! verification environment will have access to consensus chain.

use sp_api::{ApiExt, ProvideRuntimeApi};
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_domains::{DomainId, DomainsApi, DomainsDigestItem};
use sp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvider};
Expand Down Expand Up @@ -215,36 +215,15 @@ where
let storage_price_provider =
sp_block_fees::InherentDataProvider::new(consensus_chain_byte_fee);

// TODO: remove version check before next network
let messenger_api_version = runtime_api
.api_version::<dyn MessengerApi<CBlock, NumberFor<CBlock>, CBlock::Hash>>(
consensus_block_hash,
)?
// safe to return default version as 1 since there will always be version 1.
.unwrap_or(1);

let domain_chains_allowlist_update = if messenger_api_version >= 3 {
runtime_api.domain_chains_allowlist_update(consensus_block_hash, self.domain_id)?
} else {
None
};

let domain_chains_allowlist_update =
runtime_api.domain_chains_allowlist_update(consensus_block_hash, self.domain_id)?;
let messenger_inherent_provider =
sp_messenger::InherentDataProvider::new(sp_messenger::InherentType {
maybe_updates: domain_chains_allowlist_update,
});

// TODO: remove version check before next network
let domain_api_version = runtime_api
.api_version::<dyn DomainsApi<CBlock, Block::Header>>(consensus_block_hash)?
// safe to return default version as 1 since there will always be version 1.
.unwrap_or(1);

let maybe_domain_sudo_call = if domain_api_version >= 5 {
runtime_api.domain_sudo_call(consensus_block_hash, self.domain_id)?
} else {
None
};
let maybe_domain_sudo_call =
runtime_api.domain_sudo_call(consensus_block_hash, self.domain_id)?;
let domain_sudo_call_inherent_provider =
sp_domain_sudo::InherentDataProvider::new(maybe_domain_sudo_call);

Expand Down
44 changes: 16 additions & 28 deletions domains/client/block-preprocessor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::inherents::is_runtime_upgraded;
use codec::Encode;
use domain_runtime_primitives::opaque::AccountId;
use sc_client_api::BlockBackend;
use sp_api::{ApiError, ApiExt, Core, ProvideRuntimeApi};
use sp_api::{ApiError, Core, ProvideRuntimeApi};
use sp_blockchain::HeaderBackend;
use sp_core::H256;
use sp_domains::core_api::DomainCoreApi;
Expand Down Expand Up @@ -327,16 +327,6 @@ where

let runtime_api = self.client.runtime_api();
let consensus_runtime_api = self.consensus_client.runtime_api();
let api_version = runtime_api
.api_version::<dyn MessengerApi<Block, NumberFor<CBlock>, CBlock::Hash>>(
parent_domain_hash,
)
.map_err(sp_blockchain::Error::RuntimeApiError)?
.ok_or_else(|| {
sp_blockchain::Error::RuntimeApiError(ApiError::Application(
format!("MessengerApi not found at: {:?}", parent_domain_hash).into(),
))
})?;

// Check the validity of each extrinsic
//
Expand Down Expand Up @@ -381,24 +371,22 @@ where
));
}

if api_version >= 4 {
if let Some(xdm_mmr_proof) =
runtime_api.extract_xdm_mmr_proof(parent_domain_hash, &extrinsic)?
if let Some(xdm_mmr_proof) =
runtime_api.extract_xdm_mmr_proof(parent_domain_hash, &extrinsic)?
{
let ConsensusChainMmrLeafProof {
opaque_mmr_leaf,
proof,
..
} = xdm_mmr_proof;

if consensus_runtime_api
.verify_proof(at_consensus_hash, vec![opaque_mmr_leaf], proof)?
.is_err()
{
let ConsensusChainMmrLeafProof {
opaque_mmr_leaf,
proof,
..
} = xdm_mmr_proof;

if consensus_runtime_api
.verify_proof(at_consensus_hash, vec![opaque_mmr_leaf], proof)?
.is_err()
{
return Ok(BundleValidity::Invalid(InvalidBundleType::InvalidXDM(
index as u32,
)));
}
return Ok(BundleValidity::Invalid(InvalidBundleType::InvalidXDM(
index as u32,
)));
}
}

Expand Down
28 changes: 2 additions & 26 deletions domains/client/block-preprocessor/src/stateless_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use domain_runtime_primitives::opaque::AccountId;
use domain_runtime_primitives::{Balance, CheckExtrinsicsValidityError, DecodeExtrinsicError};
use sc_client_api::execution_extensions::ExtensionsFactory;
use sc_executor::RuntimeVersionOf;
use sp_api::{ApiError, Core, RuntimeApiInfo};
use sp_api::{ApiError, Core};
use sp_core::traits::{CallContext, CodeExecutor, FetchRuntimeCode, RuntimeCode};
use sp_core::Hasher;
use sp_domain_sudo::DomainSudoApi;
Expand Down Expand Up @@ -357,31 +357,7 @@ where
}

pub fn block_fees_storage_key(&self) -> Result<Vec<u8>, ApiError> {
let runtime_version = {
let mut ext = BasicExternalities::new(self.storage.clone());
let ext_extensions = ext.extensions();
ext_extensions.merge(
self.extension_factory
.extensions_for(Default::default(), Default::default()),
);
let runtime_code = self.runtime_code();
self.executor
.runtime_version(&mut ext, &runtime_code)
.map_err(|err| {
ApiError::Application(Box::from(format!(
"failed to read domain runtime version: {err}"
)))
})?
};
let has_runtime_api = runtime_version
.api_version(&<dyn DomainCoreApi<Block>>::ID)
.map_or(false, |runtime_api_version| runtime_api_version >= 2);

if has_runtime_api {
<Self as DomainCoreApi<Block>>::block_fees_storage_key(self, Default::default())
} else {
Ok(sp_domains::operator_block_fees_final_key())
}
<Self as DomainCoreApi<Block>>::block_fees_storage_key(self, Default::default())
}

pub fn extrinsic_weight(&self, extrinsic: &Block::Extrinsic) -> Result<Weight, ApiError> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use sc_client_api::AuxStore;
use sc_executor::RuntimeVersionOf;
use sc_network::NetworkPeers;
use sc_transaction_pool_api::{TransactionPool, TransactionSource};
use sp_api::{ApiError, ApiExt, ProvideRuntimeApi, StorageProof};
use sp_api::{ApiError, ProvideRuntimeApi, StorageProof};
use sp_blockchain::HeaderBackend;
use sp_consensus::SyncOracle;
use sp_core::crypto::AccountId32;
Expand Down Expand Up @@ -155,26 +155,13 @@ pub async fn start_cross_chain_message_listener<

handle_xdm_message(&client, &tx_pool, chain_id, ext).await;
}
MessageData::ChannelUpdate(channel_update) => {
// TODO: remove api version check before next network.
let is_runtime_apis_available =
is_runtime_apis_available(&consensus_client, &client).unwrap_or(false);

if !is_runtime_apis_available {
tracing::debug!(
target: LOG_TARGET,
"Runtime apis not available. Skipping channel update..."
);
continue;
}
handle_channel_update::<_, _, _, Block>(
chain_id,
channel_update,
&consensus_client,
domain_executor.clone(),
&mut domain_storage_key_cache,
)
}
MessageData::ChannelUpdate(channel_update) => handle_channel_update::<_, _, _, Block>(
chain_id,
channel_update,
&consensus_client,
domain_executor.clone(),
&mut domain_storage_key_cache,
),
}
}
}
Expand Down Expand Up @@ -473,70 +460,6 @@ where
Ok(())
}

fn is_runtime_apis_available<CClient, CBlock, Client, Block>(
consensus_client: &Arc<CClient>,
domain_client: &Arc<Client>,
) -> Result<bool, sp_blockchain::Error>
where
CBlock: BlockT,
Block: BlockT,
CClient: HeaderBackend<CBlock> + ProvideRuntimeApi<CBlock>,
Client: HeaderBackend<Block> + ProvideRuntimeApi<Block>,
{
let best_hash = consensus_client.info().best_hash;
let consensus_runtime_api = consensus_client.runtime_api();
let api_version = consensus_runtime_api
.api_version::<dyn DomainsApi<CBlock, Block::Header>>(best_hash)
.map_err(sp_blockchain::Error::RuntimeApiError)?
.ok_or_else(|| {
sp_blockchain::Error::RuntimeApiError(ApiError::Application(
format!("DomainsApi not found at: {:?}", best_hash).into(),
))
})?;

// Domains api must be atleast version 4
if api_version < 4 {
return Ok(false);
}

let api_version = consensus_runtime_api
.api_version::<dyn RelayerApi<CBlock, NumberFor<CBlock>, NumberFor<CBlock>, CBlock::Hash>>(
best_hash,
)
.map_err(sp_blockchain::Error::RuntimeApiError)?
.ok_or_else(|| {
sp_blockchain::Error::RuntimeApiError(ApiError::Application(
format!("RelayerApi not found at: {:?}", best_hash).into(),
))
})?;

// consensus relayer api must be atleast version 2
if api_version < 2 {
return Ok(false);
}

let domain_best_hash = domain_client.info().best_hash;
let domain_runtime_api = domain_client.runtime_api();

let api_version = domain_runtime_api
.api_version::<dyn RelayerApi<Block, NumberFor<Block>, NumberFor<CBlock>, CBlock::Hash>>(
domain_best_hash,
)
.map_err(sp_blockchain::Error::RuntimeApiError)?
.ok_or_else(|| {
sp_blockchain::Error::RuntimeApiError(ApiError::Application(
format!("RelayerApi not found at: {:?}", domain_best_hash).into(),
))
})?;

// domain relayer api must be atleast version 2
if api_version < 2 {
return Ok(false);
}

Ok(true)
}

async fn handle_xdm_message<TxPool, Client>(
client: &Arc<Client>,
tx_pool: &Arc<TxPool>,
Expand Down
Loading