Skip to content

Commit

Permalink
resolves #999 (#1050)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhizming-zhong authored Dec 21, 2022
1 parent c628c9a commit e205eb3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 18 deletions.
10 changes: 10 additions & 0 deletions tee-worker/core/parentchain/light-client/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ where

if genesis_hash == params.genesis_header.hash() {
validator.set_state(validation_state);
// The init_parachain_validator function clear the state every time,
// so we should write the state again.
LightClientStateSeal::<B, LightValidationState<B>>::seal_to_static_file(
validator.get_state(),
)?;
info!("Found already initialized light client with Genesis Hash: {:?}", genesis_hash);
}
info!("light client state: {:?}", validator);
Expand Down Expand Up @@ -107,6 +112,11 @@ where

if genesis_hash == params.genesis_header.hash() {
validator.set_state(validation_state);
// The init_parachain_validator function clear the state every time,
// so we should write the state again.
LightClientStateSeal::<B, LightValidationState<B>>::seal_to_static_file(
validator.get_state(),
)?;
info!("Found already initialized light client with Genesis Hash: {:?}", genesis_hash);
}
info!("light client state: {:?}", validator);
Expand Down
71 changes: 59 additions & 12 deletions tee-worker/service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ use std::{
thread,
time::Duration,
};
use substrate_api_client::{utils::FromHexString, Header as HeaderTrait, XtStatus};
use teerex_primitives::ShardIdentifier;
use substrate_api_client::{
utils::{storage_key, FromHexString},
Header as HeaderTrait, StorageKey, XtStatus,
};
use teerex_primitives::{Enclave as TeerexEnclave, ShardIdentifier};
extern crate config as rs_config;

mod account_funding;
Expand Down Expand Up @@ -506,15 +509,59 @@ fn start_worker<E, T, D, InitializationHandler, WorkerModeProvider>(
return
}

println!("[>] Register the enclave (send the extrinsic)");
let register_enclave_xt_hash = node_api.send_extrinsic(xthex, XtStatus::Finalized).unwrap();
println!("[<] Extrinsic got finalized. Hash: {:?}\n", register_enclave_xt_hash);

let register_enclave_xt_header =
node_api.get_header(register_enclave_xt_hash).unwrap().unwrap();
let mut register_enclave_xt_header: Option<Header> = None;
let mut we_are_primary_validateer: bool = false;

// litentry, Check if the enclave is already registered
match node_api.get_keys(storage_key("Teerex", "EnclaveRegistry"), None) {
Ok(Some(keys)) => {
let trusted_url = trusted_url.as_bytes().to_vec();
let mrenclave = mrenclave.to_vec();
let mut found = false;
for key in keys {
let key = if key.starts_with("0x") {
let bytes = &key.as_bytes()[b"0x".len()..];
hex::decode(bytes).unwrap()
} else {
hex::decode(key.as_bytes()).unwrap()
};
match node_api.get_storage_by_key_hash::<TeerexEnclave<AccountId32, Vec<u8>>>(
StorageKey(key.clone()),
None,
) {
Ok(Some(value)) => {
if value.mr_enclave.to_vec() == mrenclave && value.url == trusted_url {
// After calling the perform_ra function, the nonce will be incremented by 1,
// so enclave is already registered, we should reset the nonce_cache
enclave
.set_nonce(nonce)
.expect("Could not set nonce of enclave. Returning here...");
found = true;
info!("fond enclave: {:?}", value);
break
}
},
Ok(None) => {
warn!("not found from key: {:?}", key);
},
Err(_) => {},
}
}
if !found {
println!("[>] Register the enclave (send the extrinsic)");
let register_enclave_xt_hash =
node_api.send_extrinsic(xthex, XtStatus::Finalized).unwrap();
println!("[<] Extrinsic got finalized. Hash: {:?}\n", register_enclave_xt_hash);
register_enclave_xt_header = node_api.get_header(register_enclave_xt_hash).unwrap();
}
},
_ => panic!("unknown error"),
}

let we_are_primary_validateer =
we_are_primary_validateer(&node_api, &register_enclave_xt_header).unwrap();
if let Some(register_enclave_xt_header) = register_enclave_xt_header.clone() {
we_are_primary_validateer =
check_we_are_primary_validateer(&node_api, &register_enclave_xt_header).unwrap();
}

if we_are_primary_validateer {
println!("[+] We are the primary validateer");
Expand Down Expand Up @@ -555,7 +602,7 @@ fn start_worker<E, T, D, InitializationHandler, WorkerModeProvider>(
if WorkerModeProvider::worker_mode() == WorkerMode::Sidechain {
last_synced_header = sidechain_init_block_production(
enclave,
&register_enclave_xt_header,
register_enclave_xt_header,
we_are_primary_validateer,
parentchain_handler.clone(),
sidechain_storage,
Expand Down Expand Up @@ -805,7 +852,7 @@ fn enclave_account<E: EnclaveBase>(enclave_api: &E) -> AccountId32 {
}

/// Checks if we are the first validateer to register on the parentchain.
fn we_are_primary_validateer(
fn check_we_are_primary_validateer(
node_api: &ParentchainApi,
register_enclave_xt_header: &Header,
) -> Result<bool, Error> {
Expand Down
11 changes: 5 additions & 6 deletions tee-worker/service/src/sidechain_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub(crate) fn sidechain_start_untrusted_rpc_server<Enclave, SidechainStorage>(

pub(crate) fn sidechain_init_block_production<Enclave, SidechainStorage, ParentchainHandler>(
enclave: Arc<Enclave>,
register_enclave_xt_header: &Header,
register_enclave_xt_header: Option<Header>,
we_are_primary_validateer: bool,
parentchain_handler: Arc<ParentchainHandler>,
sidechain_storage: Arc<SidechainStorage>,
Expand All @@ -74,11 +74,10 @@ where
info!(
"We're the first validateer to be registered, syncing parentchain blocks until the one we have registered ourselves on."
);
updated_header =
Some(parentchain_handler.sync_and_import_parentchain_until(
last_synced_header,
register_enclave_xt_header,
)?);
updated_header = Some(parentchain_handler.sync_and_import_parentchain_until(
last_synced_header,
&register_enclave_xt_header.unwrap(),
)?);
}

// ------------------------------------------------------------------------
Expand Down

0 comments on commit e205eb3

Please sign in to comment.