From d798e1d616c4f5243234278d66fbb73dde689517 Mon Sep 17 00:00:00 2001 From: teor Date: Tue, 7 Jan 2025 12:12:56 +1000 Subject: [PATCH] Use runtime_upgrades() API to simplify inherents & fraud proofs --- .../block-preprocessor/src/inherents.rs | 33 ++++--------------- .../client/domain-operator/src/fraud_proof.rs | 18 ++-------- 2 files changed, 9 insertions(+), 42 deletions(-) diff --git a/domains/client/block-preprocessor/src/inherents.rs b/domains/client/block-preprocessor/src/inherents.rs index fdf0c88543..42817ebb2a 100644 --- a/domains/client/block-preprocessor/src/inherents.rs +++ b/domains/client/block-preprocessor/src/inherents.rs @@ -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; @@ -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. @@ -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(|| { diff --git a/domains/client/domain-operator/src/fraud_proof.rs b/domains/client/domain-operator/src/fraud_proof.rs index f5bf59ae8b..d15838ffcc 100644 --- a/domains/client/domain-operator/src/fraud_proof.rs +++ b/domains/client/domain-operator/src/fraud_proof.rs @@ -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::{ @@ -432,13 +431,6 @@ where domain_id: DomainId, at: CBlock::Hash, ) -> Result, 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() @@ -446,13 +438,9 @@ where .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)) }