Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cli calls to play connectfour #22

Merged
merged 8 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ dependencies = [
[[package]]
name = "ajuna-runtime"
version = "4.0.0-dev"
source = "git+https://github.com/ajuna-network/ajuna-node.git?branch=update-substrate-5#8cafe65bbac82d8aa05de0ccec0d003e4441b43a"
source = "git+https://github.com/ajuna-network/ajuna-node.git?branch=update-substrate-5#3a337fc5b20b10aedef9a28bd72cb6680750dda6"
dependencies = [
"frame-executive",
"frame-support",
Expand Down Expand Up @@ -2210,6 +2210,7 @@ dependencies = [
"its-primitives",
"its-state",
"log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)",
"pallet-ajuna-connectfour",
"pallet-balances",
"parity-scale-codec",
"sc-keystore",
Expand Down Expand Up @@ -4077,7 +4078,7 @@ dependencies = [
[[package]]
name = "pallet-ajuna-connectfour"
version = "4.0.0-dev"
source = "git+https://github.com/ajuna-network/ajuna-node.git?branch=update-substrate-5#8cafe65bbac82d8aa05de0ccec0d003e4441b43a"
source = "git+https://github.com/ajuna-network/ajuna-node.git?branch=update-substrate-5#3a337fc5b20b10aedef9a28bd72cb6680750dda6"
dependencies = [
"frame-benchmarking",
"frame-support",
Expand All @@ -4092,7 +4093,7 @@ dependencies = [
[[package]]
name = "pallet-ajuna-gameregistry"
version = "4.0.0-dev"
source = "git+https://github.com/ajuna-network/ajuna-node.git?branch=update-substrate-5#8cafe65bbac82d8aa05de0ccec0d003e4441b43a"
source = "git+https://github.com/ajuna-network/ajuna-node.git?branch=update-substrate-5#3a337fc5b20b10aedef9a28bd72cb6680750dda6"
dependencies = [
"frame-benchmarking",
"frame-support",
Expand All @@ -4107,7 +4108,7 @@ dependencies = [
[[package]]
name = "pallet-ajuna-matchmaker"
version = "4.0.0-dev"
source = "git+https://github.com/ajuna-network/ajuna-node.git?branch=update-substrate-5#8cafe65bbac82d8aa05de0ccec0d003e4441b43a"
source = "git+https://github.com/ajuna-network/ajuna-node.git?branch=update-substrate-5#3a337fc5b20b10aedef9a28bd72cb6680750dda6"
dependencies = [
"frame-support",
"frame-system",
Expand Down
7 changes: 5 additions & 2 deletions app-libs/stf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ std = [
"sp-io/std",
"substrate-api-client",
"substrate-client-keystore",
"pallet-ajuna-connectfour/std",
]
test = []

Expand Down Expand Up @@ -74,13 +75,16 @@ sp-application-crypto = { version = "5.0.0", default-features = false, features
sp-runtime = { version = "5.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master" }
sc-keystore = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "master", optional = true }

# scs / integritee
# scs / integritee / ajuna
my-node-runtime = { package = "ajuna-runtime", git = "https://github.com/ajuna-network/ajuna-node.git", branch = "update-substrate-5", optional = true }
sgx-externalities = { default-features = false, git = "https://github.com/ajuna-network/sgx-runtime", branch = "integritee-network-master" }
sgx-runtime = { default-features = false, git = "https://github.com/ajuna-network/sgx-runtime", branch = "integritee-network-master", optional = true }
sp-io = { default-features = false, git = "https://github.com/ajuna-network/sgx-runtime", branch = "integritee-network-master", features = ["disable_oom", "disable_panic_handler", "disable_allocator"], optional = true }
substrate-api-client = { git = "https://github.com/scs/substrate-api-client", branch = "master", optional = true }
substrate-client-keystore = { git = "https://github.com/scs/substrate-api-client", branch = "master", optional = true }
pallet-ajuna-connectfour = { default-features = false, git = "https://github.com/ajuna-network/ajuna-node.git", branch = "update-substrate-5" }



[dev-dependencies]
sp-keyring = { version = "5.0.0", git = "https://github.com/paritytech/substrate.git", branch = "master" }
Expand All @@ -89,4 +93,3 @@ sp-keyring = { version = "5.0.0", git = "https://github.com/paritytech/substrate
#sgx-runtime = { path = "../../../ajuna-sgx-runtime/runtime" }
#sp-io = { path = "../../../ajuna-sgx-runtime/substrate-sgx/sp-io" }
#sgx-externalities = { path = "../../../ajuna-sgx-runtime/substrate-sgx/externalities" }

106 changes: 106 additions & 0 deletions app-libs/stf/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,112 @@ pub fn cmd<'a>(
Ok(())
}),
)
.add_cmd(
Command::new("play-turn")
.description("Player turn of connect four")
.options(|app| {
app.setting(AppSettings::ColoredHelp)
.arg(
Arg::with_name("player")
.takes_value(true)
.required(true)
.value_name("SS58")
.help("player's AccountId in ss58check format"),
)
.arg(
Arg::with_name("column")
.takes_value(true)
.required(true)
.value_name("u8")
.help("play stone in column, must be in the range of 1 to 7"),
)
})
.runner(move |_args: &str, matches: &ArgMatches<'_>| {
let arg_player = matches.value_of("player").unwrap();
let column = matches
.value_of("column")
.unwrap()
.parse()
.expect("amount can be converted to u8");

if !(1..=7).contains(&column) {
panic!("Game only allows columns in the range of 1 to 7");
}

let player = get_pair_from_str(matches, arg_player);
let direct: bool = matches.is_present("direct");

info!("player ss58 is {}", player.public().to_ss58check());
info!("column choice is {:?}", column);

println!(
"send trusted call play-turn from {} with column {:?}",
player.public(),
column
);
let (mrenclave, shard) = get_identifiers(matches);
// get nonce
let top: TrustedOperation = TrustedGetter::nonce(player.public().into())
.sign(&KeyPair::Sr25519(player.clone()))
.into();
let res = perform_operation(matches, &top);
let nonce: Index = if let Some(n) = res {
if let Ok(nonce) = Index::decode(&mut n.as_slice()) {
nonce
} else {
info!("could not decode value. maybe hasn't been set? {:x?}", n);
0
}
} else {
0
};
debug!("got nonce: {:?}", nonce);
let top: TrustedOperation = TrustedCall::connectfour_play_turn(
sr25519_core::Public::from(player.public()).into(),
column,
)
.sign(&KeyPair::Sr25519(player), nonce, &mrenclave, &shard)
.into_trusted_operation(direct);
let _ = perform_operation(matches, &top);
Ok(())
}),
)
.add_cmd(
Command::new("get-board")
.description("query board state for account in keystore")
.options(|app| {
app.arg(
Arg::with_name("accountid")
.takes_value(true)
.required(true)
.value_name("SS58")
.help("AccountId in ss58check format"),
)
})
.runner(move |_args: &str, matches: &ArgMatches<'_>| {
let arg_who = matches.value_of("accountid").unwrap();
debug!("arg_who = {:?}", arg_who);
let who = get_pair_from_str(matches, arg_who);
let key_pair = sr25519_core::Pair::from(who.clone());
let top: TrustedOperation =
TrustedGetter::board(sr25519_core::Public::from(who.public()).into())
.sign(&KeyPair::Sr25519(key_pair))
.into();
let res = perform_operation(matches, &top);
debug!("received result for board");
if let Some(v) = res {
if let Ok(board) = crate::SgxBoardStruct::decode(&mut v.as_slice()) {
println!("Found board {:?}", board);
} else {
println!("could not decode board. maybe hasn't been set? {:x?}", v);
}
} else {
println!("could not fetch board");
};

Ok(())
}),
)
.into_cmd("trusted")
}

Expand Down
29 changes: 28 additions & 1 deletion app-libs/stf/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
limitations under the License.

*/
use crate::{stf_sgx_primitives::types::*, AccountId, Index, StfError, StfResult, H256};
use crate::{
stf_sgx_primitives::types::*, AccountId, Hash, Index, SgxBoardStruct, StfError, StfResult, H256,
};
use codec::{Decode, Encode};
use itp_storage::{storage_double_map_key, storage_map_key, storage_value_key, StorageHasher};
use log::*;
Expand Down Expand Up @@ -150,3 +152,28 @@ pub fn ensure_root(account: AccountId) -> StfResult<()> {
Err(StfError::MissingPrivileges(account))
}
}

pub fn get_board_for(who: AccountId) -> Option<SgxBoardStruct> {
if let Some(board_id) = get_storage_map::<AccountId, Hash>(
"ConnectFour",
"PlayerBoard",
&who,
&StorageHasher::Identity,
) {
if let Some(board) = get_storage_map::<Hash, SgxBoardStruct>(
"ConnectFour",
"Boards",
&board_id,
&StorageHasher::Identity,
) {
info!("Retrieved board of player {:?} is: {:?}", who.encode(), board);
Some(board)
} else {
debug!("could not read board");
None
}
} else {
debug!("could not read board id");
None
}
}
9 changes: 9 additions & 0 deletions app-libs/stf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub use my_node_runtime::Index;

use codec::{Compact, Decode, Encode};
use derive_more::Display;
use itp_types::BlockNumber;
use pallet_ajuna_connectfour::{BoardState, BoardStruct};
use sp_core::{crypto::AccountId32, ed25519, sr25519, Pair, H256};
use sp_runtime::{traits::Verify, MultiSignature};
use std::string::String;
Expand All @@ -45,6 +47,9 @@ pub type AccountId = AccountId32;
pub type Hash = sp_core::H256;
pub type BalanceTransferFn = ([u8; 2], AccountId, Compact<u128>);

pub type SgxBoardState = BoardState<AccountId>;
pub type SgxBoardStruct = BoardStruct<Hash, AccountId, BlockNumber, SgxBoardState>;

pub type ShardIdentifier = H256;

pub type StfResult<T> = Result<T, StfError>;
Expand Down Expand Up @@ -173,6 +178,7 @@ pub enum TrustedCall {
balance_transfer(AccountId, AccountId, Balance),
balance_unshield(AccountId, AccountId, Balance, ShardIdentifier), // (AccountIncognito, BeneficiaryPublicAccount, Amount, Shard)
balance_shield(AccountId, AccountId, Balance), // (Root, AccountIncognito, Amount)
connectfour_play_turn(AccountId, u8),
}

impl TrustedCall {
Expand All @@ -182,6 +188,7 @@ impl TrustedCall {
TrustedCall::balance_transfer(account, _, _) => account,
TrustedCall::balance_unshield(account, _, _, _) => account,
TrustedCall::balance_shield(account, _, _) => account,
TrustedCall::connectfour_play_turn(account, _) => account,
}
}

Expand All @@ -207,6 +214,7 @@ pub enum TrustedGetter {
free_balance(AccountId),
reserved_balance(AccountId),
nonce(AccountId),
board(AccountId),
}

impl TrustedGetter {
Expand All @@ -215,6 +223,7 @@ impl TrustedGetter {
TrustedGetter::free_balance(account) => account,
TrustedGetter::reserved_balance(account) => account,
TrustedGetter::nonce(account) => account,
TrustedGetter::board(account) => account,
}
}

Expand Down
22 changes: 20 additions & 2 deletions app-libs/stf/src/stf_sgx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use crate::test_genesis::test_genesis_setup;

use crate::{
helpers::{
account_data, account_nonce, ensure_root, get_account_info, increment_nonce, root,
validate_nonce,
account_data, account_nonce, ensure_root, get_account_info, get_board_for, increment_nonce,
root, validate_nonce,
},
AccountData, AccountId, Getter, Index, ParentchainHeader, PublicGetter, ShardIdentifier, State,
StateTypeDiff, Stf, StfError, StfResult, TrustedCall, TrustedCallSigned, TrustedGetter,
Expand Down Expand Up @@ -101,6 +101,12 @@ impl Stf {
} else {
None
},
TrustedGetter::board(who) =>
if let Some(game) = get_board_for(who) {
Some(game.encode())
} else {
None
},
},
Getter::public(g) => match g {
PublicGetter::some_value => Some(42u32.encode()),
Expand Down Expand Up @@ -176,6 +182,17 @@ impl Stf {
Self::shield_funds(who, value)?;
Ok(())
},
TrustedCall::connectfour_play_turn(sender, column) => {
let origin = sgx_runtime::Origin::signed(sender.clone());
debug!("connectfour choose ({:x?}, {:?})", sender.encode(), column);
sgx_runtime::ConnectfourCall::<Runtime>::play_turn { column }
.dispatch_bypass_filter(origin.clone())
.map_err(|e| {
error!("dispatch error {:?}", e);
StfError::Dispatch("rps_choose".to_string())
})?;
Ok(())
},
}?;
increment_nonce(&sender);
Ok(())
Expand Down Expand Up @@ -253,6 +270,7 @@ impl Stf {
TrustedCall::balance_transfer(_, _, _) => debug!("No storage updates needed..."),
TrustedCall::balance_unshield(_, _, _, _) => debug!("No storage updates needed..."),
TrustedCall::balance_shield(_, _, _) => debug!("No storage updates needed..."),
TrustedCall::connectfour_play_turn(_, _) => debug!("No storage updates needed..."),
};
key_hashes
}
Expand Down
Loading