Skip to content

Commit

Permalink
fixes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
Alain Brenzikofer committed Jun 23, 2020
1 parent 69d90e7 commit c779b08
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 114 deletions.
12 changes: 6 additions & 6 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use encointer_currencies::{CurrencyIdentifier, CurrencyPropertiesType, Location,

use substratee_stf::{
cli::get_identifiers, ShardIdentifier, TrustedCallSigned, TrustedGetterSigned,
TrustedOperationSigned,
TrustedOperation, Getter
};
use substratee_worker_api::Api as WorkerApi;

Expand Down Expand Up @@ -521,18 +521,18 @@ fn get_worker_api(matches: &ArgMatches<'_>) -> WorkerApi {

fn perform_trusted_operation(
matches: &ArgMatches<'_>,
top: &TrustedOperationSigned,
top: &TrustedOperation,
) -> Option<Vec<u8>> {
match top {
TrustedOperationSigned::call(call) => send_request(matches, call.clone()),
TrustedOperationSigned::get(getter) => get_state(matches, getter.clone()),
TrustedOperation::call(call) => send_request(matches, call.clone()),
TrustedOperation::get(getter) => get_state(matches, getter.clone()),
}
}

fn get_state(matches: &ArgMatches<'_>, getter: TrustedGetterSigned) -> Option<Vec<u8>> {
fn get_state(matches: &ArgMatches<'_>, getter: Getter) -> Option<Vec<u8>> {
let worker_api = get_worker_api(matches);
let (_mrenclave, shard) = get_identifiers(matches);
debug!("calling workerapi to get state value, {:?}", getter.getter);
debug!("calling workerapi to get state value, {:?}", getter);
let ret = worker_api
.get_stf_state(getter, &shard)
.expect("getting value failed");
Expand Down
24 changes: 14 additions & 10 deletions enclave/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use sgx_types::{sgx_epid_group_id_t, sgx_status_t, sgx_target_info_t, size_t, Sg
use substrate_api_client::{compose_extrinsic_offline, utils::storage_key};
use substratee_node_primitives::CallWorkerFn;
use substratee_stf::{
ShardIdentifier, Stf, TrustedCallSigned, TrustedGetterSigned,
ShardIdentifier, Stf, TrustedCallSigned, Getter,
};

use codec::{Decode, Encode};
Expand Down Expand Up @@ -210,12 +210,14 @@ pub unsafe extern "C" fn get_state(
let shard = ShardIdentifier::from_slice(slice::from_raw_parts(shard, shard_size as usize));
let mut trusted_op_slice = slice::from_raw_parts(trusted_op, trusted_op_size as usize);
let value_slice = slice::from_raw_parts_mut(value, value_size as usize);
let tusted_getter_signed = TrustedGetterSigned::decode(&mut trusted_op_slice).unwrap();

debug!("verifying signature of TrustedCallSigned");
if let false = tusted_getter_signed.verify_signature() {
error!("bad signature");
return sgx_status_t::SGX_ERROR_UNEXPECTED;
let getter = Getter::decode(&mut trusted_op_slice).unwrap();

if let Getter::trusted(trusted_getter_signed) = getter.clone() {
debug!("verifying signature of TrustedGetterSigned");
if let false = trusted_getter_signed.verify_signature() {
error!("bad signature");
return sgx_status_t::SGX_ERROR_UNEXPECTED;
}
}

if !state::exists(&shard) {
Expand All @@ -237,9 +239,11 @@ pub unsafe extern "C" fn get_state(

let latest_header = validator.latest_header(validator.num_relays).unwrap();

// FIXME: not sure we will ever need this as we are querying trusted state, not onchain state
// i.e. demurrage could be correctly applied with this, but the client could do that too.
debug!("Update STF storage!");
let requests: Vec<WorkerRequest> =
Stf::get_storage_hashes_to_update_for_getter(&tusted_getter_signed)
Stf::get_storage_hashes_to_update_for_getter(&getter)
.into_iter()
.map(|key| WorkerRequest::ChainStorage(key, Some(latest_header.hash())))
.collect();
Expand All @@ -259,7 +263,7 @@ pub unsafe extern "C" fn get_state(
}

debug!("calling into STF to get state");
let value_opt = Stf::get_state(&mut state, tusted_getter_signed.getter);
let value_opt = Stf::get_state(&mut state, getter);

debug!("returning getter result");
write_slice_and_whitespace_pad(value_slice, value_opt.encode());
Expand Down Expand Up @@ -356,7 +360,7 @@ pub unsafe extern "C" fn sync_chain_relay(
}

if update_states(signed_block.block.header.clone()).is_err() {
error!("Error performing state updates upon block import")
error!("Error performing state updates upon block import");
return sgx_status_t::SGX_ERROR_UNEXPECTED;
}

Expand Down
115 changes: 54 additions & 61 deletions stf/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

use crate::{AccountId, ShardIdentifier, TrustedCall, TrustedGetter, TrustedOperationSigned, Attestation};
use crate::{AccountId, ShardIdentifier, TrustedCall, TrustedGetter, PublicGetter, TrustedOperation, Attestation};
use base58::{FromBase58, ToBase58};
use clap::{Arg, ArgMatches};
use clap_nested::{Command, Commander, MultiCommand};
Expand All @@ -38,7 +38,7 @@ type Moment = u64;
const KEYSTORE_PATH: &str = "my_trusted_keystore";

pub fn cmd<'a>(
perform_operation: &'a dyn Fn(&ArgMatches<'_>, &TrustedOperationSigned) -> Option<Vec<u8>>,
perform_operation: &'a dyn Fn(&ArgMatches<'_>, &TrustedOperation) -> Option<Vec<u8>>,
) -> MultiCommand<'a, str, str> {
Commander::new()
.options(|app| {
Expand Down Expand Up @@ -146,24 +146,22 @@ pub fn cmd<'a>(
info!("from ss58 is {}", from.public().to_ss58check());
info!("to ss58 is {}", to.to_ss58check());

let (mrenclave, shard) = get_identifiers(matches);

let tcall = TrustedCall::balance_transfer(
sr25519_core::Public::from(from.public()),
to,
shard, // for encointer we assume that every currency has its own shard. so shard == cid
BalanceType::from_num(amount),
);
let nonce = 0; // FIXME: hard coded for now
let tscall =
tcall.sign(&sr25519_core::Pair::from(from), nonce, &mrenclave, &shard);
println!(
"send trusted call transfer from {} to {}: {}",
tscall.call.account(),
from.public(),
to,
amount
);
let _ = perform_operation(matches, &TrustedOperationSigned::call(tscall));
let (mrenclave, shard) = get_identifiers(matches);
let nonce = 0; // FIXME: hard coded for now
let top: TrustedOperation = TrustedCall::balance_transfer(
sr25519_core::Public::from(from.public()),
to,
shard, // for encointer we assume that every currency has its own shard. so shard == cid
BalanceType::from_num(amount))
.sign(&sr25519_core::Pair::from(from), nonce, &mrenclave, &shard)
.into();
let _ = perform_operation(matches, &top);
Ok(())
}),
)
Expand All @@ -184,10 +182,10 @@ pub fn cmd<'a>(
println!("arg_who = {:?}", arg_who);
let who = get_pair_from_str(matches, arg_who);
let (_mrenclave, shard) = get_identifiers(matches);
let tgetter =
TrustedGetter::balance(sr25519_core::Public::from(who.public()), shard);
let tsgetter = tgetter.sign(&sr25519_core::Pair::from(who));
let res = perform_operation(matches, &TrustedOperationSigned::get(tsgetter));
let top: TrustedOperation = TrustedGetter::balance(sr25519_core::Public::from(who.public()), shard)
.sign(&sr25519_core::Pair::from(who))
.into();
let res = perform_operation(matches, &top);
let bal = if let Some(v) = res {
if let Ok(vd) = <BalanceType>::decode(&mut v.as_slice()) {
vd
Expand Down Expand Up @@ -218,19 +216,18 @@ pub fn cmd<'a>(
let arg_who = matches.value_of("accountid").unwrap();
let who = get_pair_from_str(matches, arg_who);
let (mrenclave, shard) = get_identifiers(matches);
let tcall = TrustedCall::ceremonies_register_participant(
sr25519_core::Public::from(who.public()),
shard, // for encointer we assume that every currency has its own shard. so shard == cid
None
);
let nonce = 0; // FIXME: hard coded for now
let tscall =
tcall.sign(&sr25519_core::Pair::from(who), nonce, &mrenclave, &shard);
println!(
"send TrustedCall::register_participant for {}",
tscall.call.account(),
who.public(),
);
perform_operation(matches, &TrustedOperationSigned::call(tscall));
let top: TrustedOperation = TrustedCall::ceremonies_register_participant(
sr25519_core::Public::from(who.public()),
shard, // for encointer we assume that every currency has its own shard. so shard == cid
None)
.sign(&sr25519_core::Pair::from(who), nonce, &mrenclave, &shard)
.into();
perform_operation(matches, &top);
Ok(())
}),
)
Expand All @@ -250,18 +247,17 @@ pub fn cmd<'a>(
let arg_who = matches.value_of("accountid").unwrap();
let who = get_pair_from_str(matches, arg_who);
let (_mrenclave, shard) = get_identifiers(matches);
let tgetter = TrustedGetter::registration(
sr25519_core::Public::from(who.public()),
shard, // for encointer we assume that every currency has its own shard. so shard == cid
);
let tsgetter =
tgetter.sign(&sr25519_core::Pair::from(who));
println!(
"send TrustedGetter::get_registration for {}",
tsgetter.getter.account(),
who.public()
);

let part = perform_operation(matches, &TrustedOperationSigned::get(tsgetter)).unwrap();
let top: TrustedOperation = TrustedGetter::registration(
sr25519_core::Public::from(who.public()),
shard, // for encointer we assume that every currency has its own shard. so shard == cid
)
.sign(&sr25519_core::Pair::from(who))
.into();
let part = perform_operation(matches, &top).unwrap();
let participant: ParticipantIndexType = Decode::decode(&mut part.as_slice()).unwrap();
println!("Participant index: {:?}", participant);
Ok(())
Expand Down Expand Up @@ -290,26 +286,24 @@ pub fn cmd<'a>(
let arg_who = matches.value_of("accountid").unwrap();
let who = get_pair_from_str(matches, arg_who);
let (mrenclave, shard) = get_identifiers(matches);

let nonce = 0; // FIXME: hard coded for now
let attestation_args: Vec<_> = matches.values_of("attestations").unwrap().collect();
let mut attestations: Vec<Attestation<MultiSignature, AccountId32, Moment>> = vec![];
for arg in attestation_args.iter() {
let w = Attestation::decode(&mut &hex::decode(arg).unwrap()[..]).unwrap();
attestations.push(w);
}

let tcall = TrustedCall::ceremonies_register_attestations(
sr25519_core::Public::from(who.public()),
attestations
);
let nonce = 0; // FIXME: hard coded for now
let tscall =
tcall.sign(&sr25519_core::Pair::from(who), nonce, &mrenclave, &shard);
println!(
"send TrustedCall::register_attestations for {}",
tscall.call.account(),
who.public()
);
perform_operation(matches, &TrustedOperationSigned::call(tscall));
let top: TrustedOperation = TrustedCall::ceremonies_register_attestations(
sr25519_core::Public::from(who.public()),
attestations
)
.sign(&sr25519_core::Pair::from(who), nonce, &mrenclave, &shard)
.into();
perform_operation(matches, &top);
Ok(())
}),
)
Expand All @@ -329,18 +323,17 @@ pub fn cmd<'a>(
let arg_who = matches.value_of("accountid").unwrap();
let who = get_pair_from_str(matches, arg_who);
let (_mrenclave, shard) = get_identifiers(matches);
let tgetter = TrustedGetter::attestations(
sr25519_core::Public::from(who.public()),
shard, // for encointer we assume that every currency has its own shard. so shard == cid
);
let tsgetter =
tgetter.sign(&sr25519_core::Pair::from(who));
println!(
"send TrustedGetter::get_attestations for {}",
tsgetter.getter.account(),
who.public(),
);

let attestations = perform_operation(matches, &TrustedOperationSigned::get(tsgetter)).unwrap();
let top: TrustedOperation = TrustedGetter::attestations(
sr25519_core::Public::from(who.public()),
shard, // for encointer we assume that every currency has its own shard. so shard == cid
)
.sign(&sr25519_core::Pair::from(who))
.into();
let attestations = perform_operation(matches, &top).unwrap();
println!("Attestations: {:?}", hex::encode(attestations));
Ok(())
}),
Expand Down Expand Up @@ -374,11 +367,11 @@ pub fn cmd<'a>(
.unwrap();

let (_mrenclave, shard) = get_identifiers(matches);
let top: TrustedOperation = TrustedGetter::meetup_index_time_and_location(who.public().into(), shard)
.sign(&sr25519_core::Pair::from(who.clone()))
.into();

let tgetter = TrustedGetter::meetup_index_time_and_location(who.public().into(), shard);
let tsgetter = tgetter.sign(&sr25519_core::Pair::from(who.clone()));

let res = perform_operation(matches, &TrustedOperationSigned::get(tsgetter)).unwrap();
let res = perform_operation(matches, &top).unwrap();
let (mindex, mlocation, mtime): (MeetupIndexType, Option<Location>, Option<Moment>) = Decode::decode(&mut res.as_slice()).unwrap();
info!("got mindex: {:?}", mindex);
info!("got time: {:?}", mtime);
Expand Down
57 changes: 54 additions & 3 deletions stf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,60 @@ pub type State = sp_io::SgxExternalities;

#[derive(Encode, Decode, Clone)]
#[allow(non_camel_case_types)]
pub enum TrustedOperationSigned {
pub enum TrustedOperation {
call(TrustedCallSigned),
get(TrustedGetterSigned),
get(Getter),
}

impl From<TrustedCallSigned> for TrustedOperation {
fn from(item: TrustedCallSigned) -> Self {
TrustedOperation::call(item)
}
}

impl From<Getter> for TrustedOperation {
fn from(item: Getter) -> Self {
TrustedOperation::get(item)
}
}

impl From<TrustedGetterSigned> for TrustedOperation {
fn from(item: TrustedGetterSigned) -> Self {
TrustedOperation::get(item.into())
}
}

impl From<PublicGetter> for TrustedOperation {
fn from(item: PublicGetter) -> Self {
TrustedOperation::get(item.into())
}
}



#[derive(Encode, Decode, Clone, Debug)]
#[allow(non_camel_case_types)]
pub enum Getter {
public(PublicGetter),
trusted(TrustedGetterSigned)
}

impl From<PublicGetter> for Getter {
fn from(item: PublicGetter) -> Self {
Getter::public(item)
}
}

impl From<TrustedGetterSigned> for Getter {
fn from(item: TrustedGetterSigned) -> Self {
Getter::trusted(item)
}
}

#[derive(Encode, Decode, Clone, Debug)]
#[allow(non_camel_case_types)]
pub enum PublicGetter {
total_issuance(CurrencyIdentifier),
}

#[derive(Encode, Decode, Clone, Debug)]
Expand Down Expand Up @@ -113,7 +164,7 @@ pub enum TrustedGetter {
balance(AccountId, CurrencyIdentifier),
registration(AccountId, CurrencyIdentifier),
meetup_index_time_and_location(AccountId, CurrencyIdentifier),
attestations(AccountId, CurrencyIdentifier)
attestations(AccountId, CurrencyIdentifier),
}

impl TrustedGetter {
Expand Down
Loading

0 comments on commit c779b08

Please sign in to comment.