Skip to content

Commit

Permalink
Use runtime_upgrades() API to simplify inherents & fraud proofs
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Jan 7, 2025
1 parent 2bc0f32 commit d798e1d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 42 deletions.
33 changes: 6 additions & 27 deletions domains/client/block-preprocessor/src/inherents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_domains::{DomainId, DomainsApi, DomainsDigestItem};
use sp_domains::{DomainId, DomainsApi};
use sp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvider};
use sp_messenger::MessengerApi;
use sp_runtime::traits::{Block as BlockT, Header, NumberFor};
use sp_runtime::traits::{Block as BlockT, NumberFor};
use sp_timestamp::InherentType;
use std::error::Error;
use std::sync::Arc;
Expand Down Expand Up @@ -69,25 +69,15 @@ where
CBlock: BlockT,
Block: BlockT,
{
let header = consensus_client.header(consensus_block_hash)?.ok_or(
sp_blockchain::Error::MissingHeader(format!(
"No header found for {consensus_block_hash:?}"
)),
)?;

let runtime_api = consensus_client.runtime_api();
let runtime_id = runtime_api
.runtime_id(consensus_block_hash, domain_id)?
.ok_or(sp_blockchain::Error::Application(Box::from(format!(
"No RuntimeId found for {domain_id:?}"
))))?;
let runtime_upgrades = runtime_api.runtime_upgrades(consensus_block_hash)?;

Ok(header
.digest()
.logs
.iter()
.filter_map(|log| log.as_domain_runtime_upgrade())
.any(|upgraded_runtime_id| upgraded_runtime_id == runtime_id))
Ok(runtime_upgrades.contains(&runtime_id))
}

/// Returns new upgraded runtime if upgraded did happen in the provided consensus block.
Expand All @@ -102,26 +92,15 @@ where
CBlock: BlockT,
Block: BlockT,
{
let header = consensus_client.header(consensus_block_hash)?.ok_or(
sp_blockchain::Error::MissingHeader(format!(
"No header found for {consensus_block_hash:?}"
)),
)?;

let runtime_api = consensus_client.runtime_api();
let runtime_id = runtime_api
.runtime_id(consensus_block_hash, domain_id)?
.ok_or(sp_blockchain::Error::Application(Box::from(format!(
"No RuntimeId found for {domain_id:?}"
))))?;
let runtime_upgrades = runtime_api.runtime_upgrades(consensus_block_hash)?;

if header
.digest()
.logs
.iter()
.filter_map(|log| log.as_domain_runtime_upgrade())
.any(|upgraded_runtime_id| upgraded_runtime_id == runtime_id)
{
if runtime_upgrades.contains(&runtime_id) {
let new_domain_runtime = runtime_api
.domain_runtime_code(consensus_block_hash, domain_id)?
.ok_or_else(|| {
Expand Down
18 changes: 3 additions & 15 deletions domains/client/domain-operator/src/fraud_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use sp_domain_digests::AsPredigest;
use sp_domains::core_api::DomainCoreApi;
use sp_domains::proof_provider_and_verifier::StorageProofProvider;
use sp_domains::{
DomainId, DomainsApi, DomainsDigestItem, ExtrinsicDigest, HeaderHashingFor, InvalidBundleType,
RuntimeId,
DomainId, DomainsApi, ExtrinsicDigest, HeaderHashingFor, InvalidBundleType, RuntimeId,
};
use sp_domains_fraud_proof::execution_prover::ExecutionProver;
use sp_domains_fraud_proof::fraud_proof::{
Expand Down Expand Up @@ -432,27 +431,16 @@ where
domain_id: DomainId,
at: CBlock::Hash,
) -> Result<Option<RuntimeId>, FraudProofError> {
let header =
self.consensus_client
.header(at)?
.ok_or(sp_blockchain::Error::MissingHeader(format!(
"No header found for {at:?}"
)))?;

let runtime_id = self
.consensus_client
.runtime_api()
.runtime_id(at, domain_id)?
.ok_or(sp_blockchain::Error::Application(Box::from(format!(
"No RuntimeId found for {domain_id:?}"
))))?;
let runtime_upgrades = self.consensus_client.runtime_api().runtime_upgrades(at)?;

let is_runtime_upgraded = header
.digest()
.logs
.iter()
.filter_map(|log| log.as_domain_runtime_upgrade())
.any(|upgraded_runtime_id| upgraded_runtime_id == runtime_id);
let is_runtime_upgraded = runtime_upgrades.contains(&runtime_id);

Ok(is_runtime_upgraded.then_some(runtime_id))
}
Expand Down

0 comments on commit d798e1d

Please sign in to comment.