Skip to content

Commit

Permalink
Update sgxruntime and wasm builder (#32, #34)
Browse files Browse the repository at this point in the history
* update sgx-runtime

* wasm-builder

Cherry-pick: 74ff735, 24e2f2d

Co-authored-by: Gaudenz Kessler <[email protected]>
  • Loading branch information
2 people authored and cowboy-bebug committed Jun 8, 2022
1 parent a1b2877 commit 53db942
Show file tree
Hide file tree
Showing 16 changed files with 334 additions and 21 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2900,7 +2900,9 @@ dependencies = [
"frame-support",
"ita-stf",
"itc-parentchain-block-import-dispatcher",
"itc-parentchain-light-client",
"itp-enclave-metrics",
"itp-extrinsics-factory",
"itp-ocall-api",
"itp-settings",
"itp-sgx-crypto",
Expand All @@ -2919,6 +2921,7 @@ dependencies = [
"its-top-pool-executor",
"its-validateer-fetch",
"log 0.4.17",
"pallet-ajuna-connectfour",
"parity-scale-codec",
"sgx-externalities",
"sgx_tstd",
Expand Down
1 change: 1 addition & 0 deletions core-primitives/settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub mod node {
pub static UNSHIELD_FUNDS: u8 = 5u8;
pub static GAME_REGISTRY_MODULE: u8 = 13u8;
pub static ACK_GAME: u8 = 2u8;
pub static FINISH_GAME: u8 = 3u8;
// Sidechain module values
pub static SIDECHAIN_MODULE: u8 = 20u8;
pub static PROPOSED_SIDECHAIN_BLOCK: u8 = 0u8;
Expand Down
1 change: 0 additions & 1 deletion core-primitives/stf-executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ where

fn execute_new_game<ParentchainBlock>(
&self,
game_engine: GameEngine,
game: H256,
shard: &ShardIdentifier,
block: &ParentchainBlock,
Expand Down
2 changes: 0 additions & 2 deletions core-primitives/stf-executor/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use ita_stf::{
AccountId, ParentchainHeader, ShardIdentifier, TrustedGetterSigned, TrustedOperation,
};
use itp_types::{Amount, OpaqueCall, H256};
use pallet_ajuna_gameregistry::game::GameEngine;
use sgx_externalities::SgxExternalitiesTrait;
use sp_runtime::traits::{Block as ParentchainBlockTrait, Header as HeaderTrait};
use std::{fmt::Debug, result::Result as StdResult, time::Duration, vec::Vec};
Expand All @@ -43,7 +42,6 @@ pub trait StfExecuteShieldFunds {

fn execute_new_game<ParentchainBlock>(
&self,
game_engine: GameEngine,
game: H256,
shard: &ShardIdentifier,
block: &ParentchainBlock,
Expand Down
1 change: 1 addition & 0 deletions core-primitives/test/src/mock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

pub mod handle_state_mock;
pub mod metrics_ocall_mock;
pub mod ocall_api_mock;
pub mod onchain_mock;
pub mod shielding_crypto_mock;
pub mod sidechain_ocall_api_mock;
164 changes: 164 additions & 0 deletions core-primitives/test/src/mock/ocall_api_mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
Copyright 2021 Integritee AG and Supercomputing Systems AG
Copyright (C) 2017-2019 Baidu, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

use codec::{Decode, Encode};
use core::fmt::Debug;
use itp_ocall_api::{
EnclaveAttestationOCallApi, EnclaveMetricsOCallApi, EnclaveOnChainOCallApi,
EnclaveSidechainOCallApi,
};
use itp_teerex_storage::{TeeRexStorage, TeerexStorageKeys};
use itp_types::{BlockHash, Enclave, ShardIdentifier, WorkerRequest, WorkerResponse};
use sgx_types::{
sgx_epid_group_id_t, sgx_measurement_t, sgx_platform_info_t, sgx_quote_nonce_t,
sgx_quote_sign_type_t, sgx_report_t, sgx_spid_t, sgx_target_info_t, sgx_update_info_bit_t,
SgxResult, SGX_HASH_SIZE,
};
use sp_runtime::{AccountId32, OpaqueExtrinsic};
use sp_std::prelude::*;
use std::collections::HashMap;

/// This struct is the same as OnchainMock but
/// implements EnclaveOnChainOCallApi instead of GetStorageVerified.
#[derive(Default, Clone, Debug)]
pub struct OcallApiMock {
inner: HashMap<Vec<u8>, Vec<u8>>,
mr_enclave: [u8; SGX_HASH_SIZE],
}

impl OcallApiMock {
pub fn with_storage_entries<V: Encode>(mut self, entries: Vec<(Vec<u8>, V)>) -> Self {
for (k, v) in entries.into_iter() {
self.inner.insert(k, v.encode());
}
self
}

pub fn with_validateer_set(mut self, set: Option<Vec<Enclave>>) -> Self {
let set = set.unwrap_or_else(validateer_set);
self.inner.insert(TeeRexStorage::enclave_count(), (set.len() as u64).encode());
self.with_storage_entries(into_key_value_storage(set))
}

pub fn with_mr_enclave(mut self, mr_enclave: [u8; SGX_HASH_SIZE]) -> Self {
self.mr_enclave = mr_enclave;
self
}

pub fn insert(&mut self, key: Vec<u8>, value: Vec<u8>) {
self.inner.insert(key, value);
}

pub fn get(&self, key: &[u8]) -> Option<&Vec<u8>> {
self.inner.get(key)
}
}

impl EnclaveAttestationOCallApi for OcallApiMock {
fn sgx_init_quote(&self) -> SgxResult<(sgx_target_info_t, sgx_epid_group_id_t)> {
todo!()
}

fn get_ias_socket(&self) -> SgxResult<i32> {
Ok(42)
}

fn get_quote(
&self,
_sig_rl: Vec<u8>,
_report: sgx_report_t,
_sign_type: sgx_quote_sign_type_t,
_spid: sgx_spid_t,
_quote_nonce: sgx_quote_nonce_t,
) -> SgxResult<(sgx_report_t, Vec<u8>)> {
todo!()
}

fn get_update_info(
&self,
_platform_info: sgx_platform_info_t,
_enclave_trusted: i32,
) -> SgxResult<sgx_update_info_bit_t> {
todo!()
}

fn get_mrenclave_of_self(&self) -> SgxResult<sgx_measurement_t> {
Ok(sgx_measurement_t { m: self.mr_enclave })
}
}

impl EnclaveSidechainOCallApi for OcallApiMock {
fn propose_sidechain_blocks<SignedSidechainBlock: Encode>(
&self,
_signed_blocks: Vec<SignedSidechainBlock>,
) -> SgxResult<()> {
Ok(())
}

fn store_sidechain_blocks<SignedSidechainBlock: Encode>(
&self,
_signed_blocks: Vec<SignedSidechainBlock>,
) -> SgxResult<()> {
Ok(())
}

fn fetch_sidechain_blocks_from_peer<SignedSidechainBlock: Decode>(
&self,
_last_known_block_hash: BlockHash,
_shard_identifier: ShardIdentifier,
) -> SgxResult<Vec<SignedSidechainBlock>> {
Ok(Vec::new())
}
}

impl EnclaveMetricsOCallApi for OcallApiMock {
fn update_metric<Metric: Encode>(&self, _metric: Metric) -> SgxResult<()> {
Ok(())
}
}

impl EnclaveOnChainOCallApi for OcallApiMock {
fn send_to_parentchain(&self, _extrinsics: Vec<OpaqueExtrinsic>) -> SgxResult<()> {
Ok(())
}

fn worker_request<V: Encode + Decode>(
&self,
_req: Vec<WorkerRequest>,
) -> SgxResult<Vec<WorkerResponse<V>>> {
Ok(Vec::new())
}
}

pub fn validateer_set() -> Vec<Enclave> {
let default_enclave = Enclave::new(
AccountId32::from([0; 32]),
Default::default(),
Default::default(),
Default::default(),
);
vec![default_enclave.clone(), default_enclave.clone(), default_enclave.clone(), default_enclave]
}

fn into_key_value_storage(validateers: Vec<Enclave>) -> Vec<(Vec<u8>, Enclave)> {
validateers
.into_iter()
.enumerate()
.map(|(i, e)| (TeeRexStorage::enclave(i as u64 + 1), e))
.collect()
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,12 @@ where
where
ParentchainBlock: ParentchainBlockTrait<Hash = H256>,
{
let (_call, game_engine, games, shard) = &xt.function;
let (_call, _game_engine, games, shard) = &xt.function;

info!("found {:?} games", games.len());

for game in games {
self.stf_executor
.execute_new_game(game_engine.clone(), game.clone(), shard, block);
self.stf_executor.execute_new_game(game.clone(), shard, block)?;
}
Ok(())
}
Expand Down
4 changes: 4 additions & 0 deletions enclave-runtime/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ dependencies = [
"ita-stf",
"itc-direct-rpc-server",
"itc-parentchain",
"itc-parentchain-light-client",
"itc-tls-websocket-server",
"itertools",
"itp-block-import-queue",
Expand Down Expand Up @@ -1633,7 +1634,9 @@ dependencies = [
"frame-support",
"ita-stf",
"itc-parentchain-block-import-dispatcher",
"itc-parentchain-light-client",
"itp-enclave-metrics",
"itp-extrinsics-factory",
"itp-ocall-api",
"itp-settings",
"itp-sgx-crypto",
Expand All @@ -1649,6 +1652,7 @@ dependencies = [
"its-top-pool-executor",
"its-validateer-fetch",
"log 0.4.14 (git+https://github.com/mesalock-linux/log-sgx)",
"pallet-ajuna-connectfour",
"sgx-externalities",
"sgx_tstd",
"sp-core",
Expand Down
1 change: 1 addition & 0 deletions enclave-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ ita-stf = { path = "../app-libs/stf", default-features = false, features = ["sgx
itc-tls-websocket-server = { path = "../core/tls-websocket-server", default-features = false, features = ["sgx"] }
itc-direct-rpc-server = { path = "../core/direct-rpc-server", default-features = false, features = ["sgx"] }
itc-parentchain = { path = "../core/parentchain/parentchain-crate", default-features = false, features = ["sgx"] }
itc-parentchain-light-client = { path = "../core/parentchain/light-client", default-features = false }
itp-block-import-queue = { path = "../core-primitives/block-import-queue", default-features = false, features = ["sgx"] }
itp-component-container = { path = "../core-primitives/component-container", default-features = false, features = ["sgx"] }
itp-extrinsics-factory = { path = "../core-primitives/extrinsics-factory", default-features = false, features = ["sgx"]}
Expand Down
2 changes: 2 additions & 0 deletions enclave-runtime/src/global_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ pub type EnclaveSidechainBlockImporter = SidechainBlockImporter<
EnclaveStateKeyRepository,
EnclaveTopPoolOperationHandler,
EnclaveParentchainBlockImportDispatcher,
EnclaveExtrinsicsFactory,
ValidatorAccessor<ParentchainBlock>,
>;
pub type EnclaveSidechainBlockImportQueue = BlockImportQueue<SignedSidechainBlock>;
pub type EnclaveSidechainBlockSyncer = PeerBlockSync<
Expand Down
8 changes: 6 additions & 2 deletions enclave-runtime/src/test/mocks/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

use crate::test::mocks::rpc_responder_mock::RpcResponderMock;
use itc_parentchain::block_import_dispatcher::trigger_parentchain_block_import_mock::TriggerParentchainBlockImportMock;
use itc_parentchain_light_client::mocks::validator_access_mock::ValidatorAccessMock;
use itp_extrinsics_factory::mock::ExtrinsicsFactoryMock;
use itp_sgx_crypto::{mocks::KeyRepositoryMock, Aes};
use itp_stf_executor::executor::StfExecutor;
use itp_test::mock::{
handle_state_mock::HandleStateMock, metrics_ocall_mock::MetricsOCallMock,
onchain_mock::OnchainMock,
ocall_api_mock::OcallApiMock,
};
use itp_top_pool::basic_pool::BasicPool;
use itp_top_pool_author::{
Expand Down Expand Up @@ -56,7 +58,7 @@ pub type TestStateHandler = HandleStateMock;

pub type TestSidechainDb = SidechainDB<SidechainBlock, SgxExternalities>;

pub type TestOCallApi = OnchainMock;
pub type TestOCallApi = OcallApiMock;

pub type TestParentchainBlockImportTrigger =
TriggerParentchainBlockImportMock<SignedParentchainBlock>;
Expand Down Expand Up @@ -91,4 +93,6 @@ pub type TestBlockImporter = BlockImporter<
TestStateKeyRepo,
TestTopPoolExecutor,
TestParentchainBlockImportTrigger,
ExtrinsicsFactoryMock,
ValidatorAccessMock,
>;
2 changes: 2 additions & 0 deletions enclave-runtime/src/test/sidechain_aura_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ pub fn produce_sidechain_block_and_import_it() {
top_pool_operation_handler.clone(),
parentchain_block_import_trigger.clone(),
ocall_api.clone(),
Arc::new(ExtrinsicsFactoryMock::default()),
Arc::new(ValidatorAccessMock::default()),
));
let block_composer = Arc::new(TestBlockComposer::new(signer.clone(), state_key_repo.clone()));
let proposer_environment =
Expand Down
5 changes: 5 additions & 0 deletions sidechain/consensus/aura/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ sp-runtime = { version = "6.0.0", default-features = false, git = "https://githu
# local deps
ita-stf = { path = "../../../app-libs/stf", default-features = false }
itc-parentchain-block-import-dispatcher = { path = "../../../core/parentchain/block-import-dispatcher", default-features = false }
itc-parentchain-light-client = { path = "../../../core/parentchain/light-client", default-features = false }
itp-enclave-metrics = { path = "../../../core-primitives/enclave-metrics", default-features = false }
itp-extrinsics-factory = { path = "../../../core-primitives/extrinsics-factory", default-features = false }
itp-ocall-api = { path = "../../../core-primitives/ocall-api", default-features = false }
itp-settings = { path = "../../../core-primitives/settings" }
itp-sgx-crypto = { path = "../../../core-primitives/sgx/crypto", default-features = false }
Expand All @@ -36,6 +38,8 @@ its-state = { path = "../../state", default-features = false }
its-top-pool-executor = { path = "../../top-pool-executor", default-features = false }
its-validateer-fetch = { path = "../../validateer-fetch", default-features = false }

pallet-ajuna-connectfour = { default-features = false, git = "https://github.com/ajuna-network/Ajuna", rev = "polkadot-v0.9.20" }

[dev-dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false }
env_logger = "0.9.0"
Expand Down Expand Up @@ -66,6 +70,7 @@ std = [
"its-top-pool-executor/std",
"its-validateer-fetch/std",
"log/std",
"pallet-ajuna-connectfour/std",
"sgx-externalities/std",
]
sgx = [
Expand Down
Loading

0 comments on commit 53db942

Please sign in to comment.