Skip to content

Commit

Permalink
change parentchain init params enum, and add full code for initializa…
Browse files Browse the repository at this point in the history
…tion.
  • Loading branch information
clangenb committed Aug 17, 2023
1 parent 2b3e2b9 commit ba29650
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 46 deletions.
17 changes: 17 additions & 0 deletions core/parentchain/light-client/src/light_client_init_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,24 @@ pub struct GrandpaParams<Header> {
pub authorities: AuthorityList,
pub authority_proof: Vec<Vec<u8>>,
}

impl<Header> GrandpaParams<Header> {
pub fn new(
genesis_header: Header,
authorities: AuthorityList,
authority_proof: Vec<Vec<u8>>,
) -> Self {
Self { genesis_header, authorities, authority_proof }
}
}

#[derive(Encode, Decode, Clone)]
pub struct SimpleParams<Header> {
pub genesis_header: Header,
}

impl<Header> SimpleParams<Header> {
pub fn new(genesis_header: Header) -> Self {
Self { genesis_header }
}
}
29 changes: 22 additions & 7 deletions core/parentchain/parentchain-crate/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,26 @@ pub type ParachainParams = SimpleParams<ParachainHeader>;
/// 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,
}
66 changes: 35 additions & 31 deletions enclave-runtime/src/initialization/parentchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -47,37 +47,41 @@ pub(crate) fn init_parentchain_components<WorkerModeProvider: ProvideWorkerMode>
encoded_params: Vec<u8>,
) -> Result<Vec<u8>> {
match ParentchainInitParams::decode(&mut encoded_params.as_slice())? {
ParentchainInitParams::Parachain { params } => {
let handler = FullParachainHandler::init::<WorkerModeProvider>(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::<WorkerModeProvider>(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::<WorkerModeProvider>(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::<WorkerModeProvider>(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::<WorkerModeProvider>(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::<WorkerModeProvider>(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::<WorkerModeProvider>(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::<WorkerModeProvider>(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())
},
},
}
}
2 changes: 2 additions & 0 deletions service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -426,6 +427,7 @@ fn start_worker<E, T, D, InitializationHandler, WorkerModeProvider>(
ParentchainHandler::new_with_automatic_light_client_allocation(
node_api.clone(),
enclave.clone(),
ParentchainId::Teerex,
)
.unwrap(),
);
Expand Down
12 changes: 4 additions & 8 deletions service/src/parentchain_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,6 +77,7 @@ where
pub fn new_with_automatic_light_client_allocation(
parentchain_api: ParentchainApi,
enclave_api: Arc<EnclaveApi>,
id: ParentchainId,
) -> ServiceResult<Self> {
let genesis_hash = parentchain_api.get_genesis_hash()?;
let genesis_header =
Expand All @@ -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))
Expand Down

0 comments on commit ba29650

Please sign in to comment.