diff --git a/core/parentchain/light-client/src/light_client_init_params.rs b/core/parentchain/light-client/src/light_client_init_params.rs index 61baf247ee..114d684382 100644 --- a/core/parentchain/light-client/src/light_client_init_params.rs +++ b/core/parentchain/light-client/src/light_client_init_params.rs @@ -26,7 +26,24 @@ pub struct GrandpaParams
{ pub authorities: AuthorityList, pub authority_proof: Vec>, } + +impl
GrandpaParams
{ + pub fn new( + genesis_header: Header, + authorities: AuthorityList, + authority_proof: Vec>, + ) -> Self { + Self { genesis_header, authorities, authority_proof } + } +} + #[derive(Encode, Decode, Clone)] pub struct SimpleParams
{ pub genesis_header: Header, } + +impl
SimpleParams
{ + pub fn new(genesis_header: Header) -> Self { + Self { genesis_header } + } +} diff --git a/core/parentchain/parentchain-crate/src/primitives.rs b/core/parentchain/parentchain-crate/src/primitives.rs index 44918c668b..61f3757753 100644 --- a/core/parentchain/parentchain-crate/src/primitives.rs +++ b/core/parentchain/parentchain-crate/src/primitives.rs @@ -33,11 +33,26 @@ pub type ParachainParams = SimpleParams; /// Allows to use a single E-call for the initialization of different parentchain types. #[derive(Encode, Decode, Clone)] pub enum ParentchainInitParams { - Solochain { params: SolochainParams }, - Parachain { params: ParachainParams }, - // Initialize a secondary parentchain handler. - // HOwever, it always needs a first one too, - // where the enclave registers itself. - Solochain2 { params: SolochainParams }, - Parachain2 { params: ParachainParams }, + Solochain { id: ParentchainId, params: SolochainParams }, + Parachain { id: ParentchainId, params: ParachainParams }, +} + +impl From<(ParentchainId, SolochainParams)> for ParentchainInitParams { + fn from(value: (ParentchainId, SolochainParams)) -> Self { + Self::Solochain { id: value.0, params: value.1 } + } +} + +impl From<(ParentchainId, ParachainParams)> for ParentchainInitParams { + fn from(value: (ParentchainId, ParachainParams)) -> Self { + Self::Parachain { id: value.0, params: value.1 } + } +} + +#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq)] +pub enum ParentchainId { + /// Primary parentchain containing the teerex pallet. + Teerex, + /// Some secondary parentchain contaning custom business logic. + Secondary, } diff --git a/enclave-runtime/src/initialization/parentchain/mod.rs b/enclave-runtime/src/initialization/parentchain/mod.rs index 320e8b86ed..4167fd002e 100644 --- a/enclave-runtime/src/initialization/parentchain/mod.rs +++ b/enclave-runtime/src/initialization/parentchain/mod.rs @@ -28,7 +28,7 @@ use crate::{ use codec::{Decode, Encode}; use itc_parentchain::{ light_client::{concurrent_access::ValidatorAccess, LightClientState}, - primitives::ParentchainInitParams, + primitives::{ParentchainId, ParentchainInitParams}, }; use itp_component_container::ComponentInitializer; use itp_settings::worker_mode::ProvideWorkerMode; @@ -47,37 +47,41 @@ pub(crate) fn init_parentchain_components encoded_params: Vec, ) -> Result> { match ParentchainInitParams::decode(&mut encoded_params.as_slice())? { - ParentchainInitParams::Parachain { params } => { - let handler = FullParachainHandler::init::(base_path, params)?; - let header = handler - .validator_accessor - .execute_on_validator(|v| v.latest_finalized_header())?; - GLOBAL_FULL_PARACHAIN_HANDLER_COMPONENT.initialize(handler.into()); - Ok(header.encode()) + ParentchainInitParams::Parachain { id, params } => match id { + ParentchainId::Teerex => { + let handler = FullParachainHandler::init::(base_path, params)?; + let header = handler + .validator_accessor + .execute_on_validator(|v| v.latest_finalized_header())?; + GLOBAL_FULL_PARACHAIN_HANDLER_COMPONENT.initialize(handler.into()); + Ok(header.encode()) + }, + ParentchainId::Secondary => { + let handler = FullParachainHandler2::init::(base_path, params)?; + let header = handler + .validator_accessor + .execute_on_validator(|v| v.latest_finalized_header())?; + GLOBAL_FULL_PARACHAIN2_HANDLER_COMPONENT.initialize(handler.into()); + Ok(header.encode()) + }, }, - ParentchainInitParams::Solochain { params } => { - let handler = FullSolochainHandler::init::(base_path, params)?; - let header = handler - .validator_accessor - .execute_on_validator(|v| v.latest_finalized_header())?; - GLOBAL_FULL_SOLOCHAIN_HANDLER_COMPONENT.initialize(handler.into()); - Ok(header.encode()) - }, - ParentchainInitParams::Parachain2 { params } => { - let handler = FullParachainHandler2::init::(base_path, params)?; - let header = handler - .validator_accessor - .execute_on_validator(|v| v.latest_finalized_header())?; - GLOBAL_FULL_PARACHAIN2_HANDLER_COMPONENT.initialize(handler.into()); - Ok(header.encode()) - }, - ParentchainInitParams::Solochain2 { params } => { - let handler = FullSolochainHandler2::init::(base_path, params)?; - let header = handler - .validator_accessor - .execute_on_validator(|v| v.latest_finalized_header())?; - GLOBAL_FULL_SOLOCHAIN2_HANDLER_COMPONENT.initialize(handler.into()); - Ok(header.encode()) + ParentchainInitParams::Solochain { id, params } => match id { + ParentchainId::Teerex => { + let handler = FullSolochainHandler::init::(base_path, params)?; + let header = handler + .validator_accessor + .execute_on_validator(|v| v.latest_finalized_header())?; + GLOBAL_FULL_SOLOCHAIN_HANDLER_COMPONENT.initialize(handler.into()); + Ok(header.encode()) + }, + ParentchainId::Secondary => { + let handler = FullSolochainHandler2::init::(base_path, params)?; + let header = handler + .validator_accessor + .execute_on_validator(|v| v.latest_finalized_header())?; + GLOBAL_FULL_SOLOCHAIN2_HANDLER_COMPONENT.initialize(handler.into()); + Ok(header.encode()) + }, }, } } diff --git a/service/src/main.rs b/service/src/main.rs index cab77e7230..930053ff96 100644 --- a/service/src/main.rs +++ b/service/src/main.rs @@ -81,6 +81,7 @@ use teerex_primitives::AnySigner; use sgx_verify::extract_tcb_info_from_raw_dcap_quote; use enclave_bridge_primitives::ShardIdentifier; +use itc_parentchain::primitives::ParentchainId; use sp_core::crypto::{AccountId32, Ss58Codec}; use sp_keyring::AccountKeyring; use sp_runtime::MultiSigner; @@ -426,6 +427,7 @@ fn start_worker( ParentchainHandler::new_with_automatic_light_client_allocation( node_api.clone(), enclave.clone(), + ParentchainId::Teerex, ) .unwrap(), ); diff --git a/service/src/parentchain_handler.rs b/service/src/parentchain_handler.rs index b0a9c44395..90a29a62b6 100644 --- a/service/src/parentchain_handler.rs +++ b/service/src/parentchain_handler.rs @@ -19,7 +19,7 @@ use crate::error::{Error, ServiceResult}; use itc_parentchain::{ light_client::light_client_init_params::{GrandpaParams, SimpleParams}, - primitives::ParentchainInitParams, + primitives::{ParentchainId, ParentchainInitParams}, }; use itp_enclave_api::{enclave_base::EnclaveBase, sidechain::Sidechain}; use itp_node_api::api_client::ChainApi; @@ -77,6 +77,7 @@ where pub fn new_with_automatic_light_client_allocation( parentchain_api: ParentchainApi, enclave_api: Arc, + id: ParentchainId, ) -> ServiceResult { let genesis_hash = parentchain_api.get_genesis_hash()?; let genesis_header = @@ -92,14 +93,9 @@ where let authority_list = VersionedAuthorityList::from(grandpas); - GrandpaParams { - genesis_header, - authorities: authority_list.into(), - authority_proof: grandpa_proof, - } - .into() + (id, GrandpaParams::new(genesis_header, authority_list.into(), grandpa_proof)).into() } else { - SimpleParams { genesis_header }.into() + (id, SimpleParams::new(genesis_header)).into() }; Ok(Self::new(parentchain_api, enclave_api, parentchain_init_params))