Skip to content

Commit

Permalink
create and send extrinsic finishGame when game finished
Browse files Browse the repository at this point in the history
* detect game state for finish game

* detect end of game

* send finish game extrinsic

* GitHub comments

* change call filter algorithm

* refactoring

Co-authored-by: Gaudenz Kessler <[email protected]>
  • Loading branch information
gaudenzkessler and gkessler-scs authored Mar 9, 2022
1 parent 74ff735 commit 24e2f2d
Show file tree
Hide file tree
Showing 17 changed files with 342 additions and 29 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2841,7 +2841,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 @@ -2861,6 +2863,7 @@ dependencies = [
"its-top-pool-executor",
"its-validateer-fetch",
"log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)",
"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 @@ -108,6 +108,7 @@ pub mod node {

pub static GAME_REGISTRY_MODULE: u8 = 13u8;
pub static ACK_GAME: u8 = 2u8;
pub static FINISH_GAME: u8 = 4u8;
// bump this to be consistent with integritee-node runtime
pub static RUNTIME_SPEC_VERSION: u32 = 100;
pub static RUNTIME_TRANSACTION_VERSION: u32 = 1;
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 @@ -202,7 +202,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, TrustedCallSigned, TrustedGetterSigned,
};
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 sidechain_ocall_api_mock;
pub mod trusted_operation_pool_mock;
Expand Down
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 @@ -84,13 +84,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 @@ -536,6 +536,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 @@ -1656,7 +1657,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 @@ -1673,6 +1676,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 @@ -91,6 +91,8 @@ pub type EnclaveSidechainBlockImporter = SidechainBlockImporter<
Aes,
EnclaveTopPoolOperationHandler,
EnclaveParentchainBlockImportDispatcher,
EnclaveExtrinsicsFactory,
ValidatorAccessor<ParentchainBlock>,
>;
pub type EnclaveSidechainBlockImportQueue = BlockImportQueue<SignedSidechainBlock>;
pub type EnclaveSidechainBlockSyncer =
Expand Down
6 changes: 4 additions & 2 deletions enclave-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,10 +535,10 @@ pub unsafe extern "C" fn init_light_client(
let indirect_calls_executor =
Arc::new(IndirectCallsExecutor::new(shielding_key, stf_executor.clone()));
let parentchain_block_importer = ParentchainBlockImporter::new(
validator_access,
validator_access.clone(),
ocall_api.clone(),
stf_executor.clone(),
extrinsics_factory,
extrinsics_factory.clone(),
indirect_calls_executor,
file_state_handler.clone(),
);
Expand All @@ -561,6 +561,8 @@ pub unsafe extern "C" fn init_light_client(
top_pool_executor,
parentchain_block_import_dispatcher,
ocall_api.clone(),
extrinsics_factory,
validator_access,
));

let sidechain_block_syncer =
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::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_types::{Block as ParentchainBlock, SignedBlock as SignedParentchainBlock};
use its_sidechain::{
Expand Down Expand Up @@ -54,7 +56,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 @@ -85,4 +87,6 @@ pub type TestBlockImporter = BlockImporter<
Aes,
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 @@ -95,6 +95,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, rpc_author.clone()));
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 = "5.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 @@ -37,6 +39,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-node.git", branch = "update-substrate-5" }

[dev-dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
env_logger = "0.9.0"
Expand Down Expand Up @@ -68,6 +72,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 24e2f2d

Please sign in to comment.