Skip to content

Commit

Permalink
init parentchain genesis hash (#1621)
Browse files Browse the repository at this point in the history
  • Loading branch information
brenzi authored Oct 24, 2024
1 parent 58ba77e commit cbc5486
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 15 deletions.
38 changes: 36 additions & 2 deletions app-libs/stf/src/stf_sgx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use itp_stf_interface::{
};
use itp_stf_primitives::{error::StfError, traits::TrustedCallVerification};
use itp_storage::storage_value_key;
use itp_types::parentchain::{AccountId, ParentchainCall, ParentchainId};
use itp_types::parentchain::{AccountId, Hash, ParentchainCall, ParentchainId};
use itp_utils::stringify::account_id_to_string;
use log::*;
use sp_runtime::traits::StaticLookup;
Expand Down Expand Up @@ -280,7 +280,7 @@ impl<TCS, G, State, Runtime, ParentchainHeader>
ParentchainPalletInstancesInterface<State, ParentchainHeader> for Stf<TCS, G, State, Runtime>
where
State: SgxExternalitiesTrait,
Runtime: frame_system::Config<Header = ParentchainHeader, AccountId = AccountId>
Runtime: frame_system::Config<Header = ParentchainHeader, AccountId = AccountId, Hash = Hash>
+ pallet_parentchain::Config<ParentchainInstanceIntegritee>
+ pallet_parentchain::Config<ParentchainInstanceTargetA>
+ pallet_parentchain::Config<ParentchainInstanceTargetB>,
Expand Down Expand Up @@ -431,6 +431,40 @@ where
Ok(())
}

fn set_genesis_hash(
state: &mut State,
genesis_hash: Hash,
parentchain_id: ParentchainId,
) -> Result<(), Self::Error> {
state.execute_with(|| match parentchain_id {
ParentchainId::Integritee => pallet_parentchain::Call::<
Runtime,
ParentchainInstanceIntegritee,
>::init_parentchain_genesis_hash {
genesis: genesis_hash,
}
.dispatch_bypass_filter(Runtime::RuntimeOrigin::root())
.map_err(|e| Self::Error::Dispatch(format!("Init genesis hash error: {:?}", e.error))),
ParentchainId::TargetA => pallet_parentchain::Call::<
Runtime,
ParentchainInstanceTargetA,
>::init_parentchain_genesis_hash {
genesis: genesis_hash,
}
.dispatch_bypass_filter(Runtime::RuntimeOrigin::root())
.map_err(|e| Self::Error::Dispatch(format!("Init genesis hash error: {:?}", e.error))),
ParentchainId::TargetB => pallet_parentchain::Call::<
Runtime,
ParentchainInstanceTargetB,
>::init_parentchain_genesis_hash {
genesis: genesis_hash,
}
.dispatch_bypass_filter(Runtime::RuntimeOrigin::root())
.map_err(|e| Self::Error::Dispatch(format!("Init genesis hash error: {:?}", e.error))),
})?;
Ok(())
}

fn get_shard_vault_ensure_single_parentchain(
state: &mut State,
) -> Result<Option<(AccountId, ParentchainId)>, Self::Error> {
Expand Down
8 changes: 7 additions & 1 deletion core-primitives/stf-interface/src/parentchain_pallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

use itp_types::parentchain::{AccountId, ParentchainId};
use itp_types::parentchain::{AccountId, Hash, ParentchainId};

/// Interface trait of the parentchain pallet.
pub trait ParentchainPalletInstancesInterface<State, ParentchainHeader> {
Expand Down Expand Up @@ -51,6 +51,12 @@ pub trait ParentchainPalletInstancesInterface<State, ParentchainHeader> {
parentchain_id: ParentchainId,
) -> Result<(), Self::Error>;

fn set_genesis_hash(
state: &mut State,
genesis_hash: Hash,
parentchain_id: ParentchainId,
) -> Result<(), Self::Error>;

fn get_shard_vault_ensure_single_parentchain(
state: &mut State,
) -> Result<Option<(AccountId, ParentchainId)>, Self::Error>;
Expand Down
1 change: 1 addition & 0 deletions core-primitives/types/src/parentchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl std::fmt::Display for ParentchainId {

pub trait IdentifyParentchain {
fn parentchain_id(&self) -> ParentchainId;
fn genesis_hash(&self) -> Option<Hash>;
}

pub trait FilterEvents {
Expand Down
12 changes: 9 additions & 3 deletions core/parentchain/light-client/src/concurrent_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::{
LightValidationState, Validator as ValidatorTrait,
};
use finality_grandpa::BlockNumberOps;
use itp_types::parentchain::{IdentifyParentchain, ParentchainId};
use itp_types::parentchain::{Hash, IdentifyParentchain, ParentchainId};
use sp_runtime::traits::{Block as ParentchainBlockTrait, NumberFor};
use std::{marker::PhantomData, sync::Arc};

Expand Down Expand Up @@ -80,12 +80,18 @@ impl<Validator, ParentchainBlock, LightClientSeal>
}
}

impl<Validator, ParentchainBlock, LightClientSeal: IdentifyParentchain> IdentifyParentchain
for ValidatorAccessor<Validator, ParentchainBlock, LightClientSeal>
impl<
Validator: IdentifyParentchain,
ParentchainBlock,
LightClientSeal: IdentifyParentchain + LightClientSealing,
> IdentifyParentchain for ValidatorAccessor<Validator, ParentchainBlock, LightClientSeal>
{
fn parentchain_id(&self) -> ParentchainId {
(*self.seal).parentchain_id()
}
fn genesis_hash(&self) -> Option<Hash> {
self.light_validation.read().unwrap().genesis_hash()
}
}

impl<Validator, ParentchainBlock, Seal> ValidatorAccess<ParentchainBlock>
Expand Down
8 changes: 7 additions & 1 deletion core/parentchain/light-client/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use codec::{Decode, Encode};
use core::{fmt::Debug, marker::PhantomData};
use itp_ocall_api::EnclaveOnChainOCallApi;
use itp_sgx_io::{seal, unseal};
use itp_types::parentchain::{IdentifyParentchain, ParentchainId};
use itp_types::parentchain::{Hash, IdentifyParentchain, ParentchainId};
use log::*;
use sp_runtime::traits::{Block, Header};
use std::{
Expand Down Expand Up @@ -93,6 +93,9 @@ impl<B, L> IdentifyParentchain for LightClientStateSeal<B, L> {
fn parentchain_id(&self) -> ParentchainId {
self.parentchain_id
}
fn genesis_hash(&self) -> Option<Hash> {
None
}
}

impl<B: Block, LightClientState: Decode + Encode + Debug> LightClientSealing
Expand Down Expand Up @@ -194,6 +197,9 @@ impl<B, LightClientState> IdentifyParentchain for LightClientStateSealSync<B, Li
fn parentchain_id(&self) -> ParentchainId {
self.seal.parentchain_id
}
fn genesis_hash(&self) -> Option<Hash> {
None
}
}

impl<B: Block, LightClientState: Decode + Encode + Debug> LightClientSealing
Expand Down
12 changes: 8 additions & 4 deletions core/parentchain/light-client/src/light_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ use codec::Encode;
use core::iter::Iterator;
use itp_ocall_api::EnclaveOnChainOCallApi;
use itp_storage::{Error as StorageError, StorageProof, StorageProofChecker};
use itp_types::parentchain::{IdentifyParentchain, ParentchainId};
use itp_types::parentchain::{Hash, IdentifyParentchain, ParentchainId};
use log::error;
use sp_runtime::{
generic::SignedBlock,
traits::{Block as ParentchainBlockTrait, Header as HeaderTrait},
traits::{Block as ParentchainBlockTrait, Block as BlockT, Header as HeaderTrait},
Justifications, OpaqueExtrinsic,
};
use std::{boxed::Box, fmt, sync::Arc, vec::Vec};
Expand All @@ -42,12 +42,16 @@ pub struct LightValidation<Block: ParentchainBlockTrait, OcallApi> {
finality: Arc<Box<dyn Finality<Block> + Sync + Send + 'static>>,
}

impl<Block: ParentchainBlockTrait, OcallApi> IdentifyParentchain
for LightValidation<Block, OcallApi>
impl<Block, OcallApi> IdentifyParentchain for LightValidation<Block, OcallApi>
where
Block: BlockT<Hash = Hash> + ParentchainBlockTrait,
{
fn parentchain_id(&self) -> ParentchainId {
self.parentchain_id
}
fn genesis_hash(&self) -> Option<Hash> {
self.light_validation_state.genesis_hash().ok()
}
}

impl<Block: ParentchainBlockTrait, OcallApi: EnclaveOnChainOCallApi>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
mocks::validator_mock::ValidatorMock,
};
use itp_types::{
parentchain::{IdentifyParentchain, ParentchainId},
parentchain::{Hash, IdentifyParentchain, ParentchainId},
Block,
};

Expand Down Expand Up @@ -63,4 +63,7 @@ impl IdentifyParentchain for ValidatorAccessMock {
fn parentchain_id(&self) -> ParentchainId {
ParentchainId::Integritee
}
fn genesis_hash(&self) -> Option<Hash> {
Some(Hash::default())
}
}
22 changes: 21 additions & 1 deletion enclave-runtime/src/shard_creation_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ use crate::{
use codec::{Decode, Encode};
use itp_component_container::ComponentGetter;

use crate::utils::{
get_validator_accessor_from_integritee_solo_or_parachain,
get_validator_accessor_from_target_a_solo_or_parachain,
get_validator_accessor_from_target_b_solo_or_parachain,
};
use itp_stf_interface::{
parentchain_pallet::ParentchainPalletInstancesInterface, ShardCreationInfo, ShardCreationQuery,
};
use itp_stf_state_handler::{handle_state::HandleState, query_shard_state::QueryShardState};
use itp_types::{
parentchain::{Header, ParentchainId},
parentchain::{Hash, Header, IdentifyParentchain, ParentchainId},
ShardIdentifier,
};
use itp_utils::write_slice_and_whitespace_pad;
Expand Down Expand Up @@ -100,14 +105,29 @@ fn init_shard_creation_parentchain_header_internal(
};

let (state_lock, mut state) = state_handler.load_for_mutation(&shard)?;

EnclaveStf::set_creation_block(&mut state, header, parentchain_id)
.map_err(|e| Error::Stf(e.to_string()))?;

let genesis_hash = get_genesis_hash(parentchain_id)?;
EnclaveStf::set_genesis_hash(&mut state, genesis_hash, parentchain_id)
.map_err(|e| Error::Stf(e.to_string()))?;

state_handler.write_after_mutation(state, state_lock, &shard)?;

shard_config::init_shard_config(shard)?;
Ok(())
}

fn get_genesis_hash(parentchain_id: ParentchainId) -> EnclaveResult<Hash> {
let va = match parentchain_id {
ParentchainId::Integritee => get_validator_accessor_from_integritee_solo_or_parachain(),
ParentchainId::TargetA => get_validator_accessor_from_target_a_solo_or_parachain(),
ParentchainId::TargetB => get_validator_accessor_from_target_b_solo_or_parachain(),
}?;
va.genesis_hash()
.ok_or_else(|| Error::Other("genesis hash missing for parentchain".into()))
}
/// reads the shard vault account id form state if it has been initialized previously
pub(crate) fn get_shard_creation_info_internal(
shard: ShardIdentifier,
Expand Down
5 changes: 3 additions & 2 deletions service/src/main_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,12 +778,13 @@ where
// TODO: #1451: Fix api-client type hacks
let head = Header::decode(&mut api_head.encode().as_slice())
.expect("Can decode previously encoded header; qed");
// we ignore failure
let _ = enclave.init_shard_creation_parentchain_header(shard, &parentchain_id, &head);

let (parentchain_handler, last_synched_header) =
init_parentchain(enclave, &node_api, tee_account_id, parentchain_id, shard);

// we ignore failure
let _ = enclave.init_shard_creation_parentchain_header(shard, &parentchain_id, &head);

if WorkerModeProvider::worker_mode() != WorkerMode::Teeracle {
println!(
"[{:?}] Finished initializing light client, syncing parentchain...",
Expand Down

0 comments on commit cbc5486

Please sign in to comment.