Skip to content

Commit

Permalink
error: defaults for type parameters are only allowed in struct, `en…
Browse files Browse the repository at this point in the history
…um`, `type`, or `trait` definitions

  --> cli/src/trusted_operation.rs:44:41
   |
44 | pub(crate) fn perform_trusted_operation<TG = TrustedGetter>(
   |                                         ^^^^^^^^^^^^^^^^^^
   |
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #36887 <rust-lang/rust#36887>
   = note: `#[deny(invalid_type_param_default)]` on by default
  • Loading branch information
OverOrion committed Nov 30, 2022
1 parent 09b07c8 commit 467840f
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 73 deletions.
2 changes: 1 addition & 1 deletion cli/src/evm/commands/evm_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
};
use codec::Decode;
use ita_stf::{Index, KeyPair, TrustedCall, TrustedOperation};
use itp_stf_primitives::getter::TrustedGetter;
use itp_stf_primitives::evm_helpers::TrustedGetterEvm;
use itp_types::AccountId;
use log::*;
use sp_core::{crypto::Ss58Codec, Pair, H160, U256};
Expand Down
9 changes: 5 additions & 4 deletions cli/src/evm/commands/evm_command_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
#[macro_export]
macro_rules! get_layer_two_evm_nonce {
($signer_pair:ident, $cli:ident, $trusted_args:ident ) => {{
let top: TrustedOperation = TrustedGetter::evm_nonce($signer_pair.public().into())
.sign(&KeyPair::Sr25519(Box::new($signer_pair.clone())))
.into();
let res = perform_trusted_operation($cli, $trusted_args, &top);
let top: TrustedOperation<TrustedGetterEvm> =
TrustedGetterEvm::evm_nonce($signer_pair.public().into())
.sign(&KeyPair::Sr25519(Box::new($signer_pair.clone())))
.into();
let res = perform_trusted_operation::<TrustedGetterEvm>($cli, $trusted_args, &top);
let nonce: Index = if let Some(n) = res {
if let Ok(nonce) = Index::decode(&mut n.as_slice()) {
nonce
Expand Down
6 changes: 4 additions & 2 deletions cli/src/evm/commands/evm_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ use crate::{
};
use codec::Decode;
use itp_stf_primitives::{
evm_helpers::evm_create_address, getter::TrustedGetter, trusted_call::TrustedCall,
types::KeyPair, Index, TrustedOperation,
evm_helpers::evm_create_address, trusted_call::TrustedCall, types::KeyPair, Index,
TrustedOperation,
};

use ita_stf::evm_helpers::TrustedGetterEvm;
use itp_types::AccountId;
use log::*;
use pallet_evm::{AddressMapping, HashedAddressMapping};
Expand Down
4 changes: 2 additions & 2 deletions cli/src/evm/commands/evm_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
};
use codec::Decode;
use ita_stf::{KeyPair, TrustedOperation};
use itp_stf_primitives::getter::TrustedGetter;
use itp_stf_primitives::evm_helpers::TrustedGetterEvm;
use itp_types::AccountId;
use log::*;
use sp_core::{crypto::Ss58Codec, Pair, H160, H256};
Expand Down Expand Up @@ -55,7 +55,7 @@ impl EvmReadCommands {
H160::from_slice(&Vec::from_hex(self.execution_address.to_string()).unwrap());

let top: TrustedOperation =
TrustedGetter::evm_account_storages(sender_acc, execution_address, H256::zero())
TrustedGetterEvm::evm_account_storages(sender_acc, execution_address, H256::zero())
.sign(&KeyPair::Sr25519(Box::new(sender)))
.into();
let res = perform_trusted_operation(cli, trusted_args, &top);
Expand Down
3 changes: 2 additions & 1 deletion cli/src/trusted_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use itc_rpc_client::direct_client::{DirectApi, DirectClient};
use itp_node_api::api_client::TEEREX;
use itp_rpc::{RpcRequest, RpcResponse, RpcReturnValue};
use itp_sgx_crypto::ShieldingCryptoEncrypt;
use itp_stf_primitives::getter::TrustedGetter;
use itp_types::{BlockNumber, DirectRequestStatus, Header, TrustedOperationStatus};
use itp_utils::{FromHexPrefixed, ToHexPrefixed};
use log::*;
Expand All @@ -40,7 +41,7 @@ use std::{
use substrate_api_client::{compose_extrinsic, XtStatus};
use teerex_primitives::Request;

pub(crate) fn perform_trusted_operation(
pub(crate) fn perform_trusted_operation<TG = TrustedGetter>(
cli: &Cli,
trusted_args: &TrustedArgs,
top: &TrustedOperation,
Expand Down
82 changes: 80 additions & 2 deletions core-primitives/stf-primitives/src/evm_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,79 @@
*/
use crate::{
helpers::{get_storage_double_map, get_storage_map},
types::AccountId,
Index,
types::{AccountId, KeyPair},
Getter, Index, TrustedGetterSigned, TrustedOperation,
};
use codec::{Decode, Encode};
use ita_sgx_runtime::System;
use itp_stf_interface::ExecuteGetter;
use itp_storage::StorageHasher;
use log::*;
use sha3::{Digest, Keccak256};
use sp_core::{H160, H256};
use std::prelude::v1::*;

//use crate::evm_helpers::{get_evm_account, get_evm_account_codes, get_evm_account_storages};
use ita_sgx_runtime::{AddressMapping, HashedAddressMapping};

#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq)]
#[allow(non_camel_case_types)]
pub enum TrustedGetterEvm {
evm_nonce(AccountId),
evm_account_codes(AccountId, H160),
evm_account_storages(AccountId, H160, H256),
}

impl TrustedGetterEvm {
pub fn sender_account(&self) -> &AccountId {
match self {
TrustedGetterEvm::evm_nonce(sender_account) => sender_account,
TrustedGetterEvm::evm_account_codes(sender_account, _) => sender_account,
TrustedGetterEvm::evm_account_storages(sender_account, ..) => sender_account,
}
}
pub fn sign(&self, pair: &KeyPair) -> TrustedGetterSigned<TrustedGetterEvm> {
let signature = pair.sign(self.encode().as_slice());
TrustedGetterSigned { getter: self.clone(), signature }
}
}

impl ExecuteGetter for TrustedGetterEvm {
fn execute(self) -> Option<Vec<u8>> {
match self {
TrustedGetterEvm::evm_nonce(who) => {
let evm_account = get_evm_account(&who);
let evm_account = HashedAddressMapping::into_account_id(evm_account);
let nonce = System::account_nonce(&evm_account);
debug!("TrustedGetterEvm evm_nonce");
debug!("Account nonce is {}", nonce);
Some(nonce.encode())
},
TrustedGetterEvm::evm_account_codes(_who, evm_account) =>
// TODO: This probably needs some security check if who == evm_account (or assosciated)
if let Some(info) = get_evm_account_codes(&evm_account) {
debug!("TrustedGetterEvm Evm Account Codes");
debug!("AccountCodes for {} is {:?}", evm_account, info);
Some(info) // TOOD: encoded?
} else {
None
},
TrustedGetterEvm::evm_account_storages(_who, evm_account, index) =>
// TODO: This probably needs some security check if who == evm_account (or assosciated)
if let Some(value) = get_evm_account_storages(&evm_account, &index) {
debug!("TrustedGetterEvm Evm Account Storages");
debug!("AccountStorages for {} is {:?}", evm_account, value);
Some(value.encode())
} else {
None
},
}
}
fn get_storage_hashes_to_update(self) -> Vec<Vec<u8>> {
Vec::new()
}
}

pub fn get_evm_account_codes(evm_account: &H160) -> Option<Vec<u8>> {
get_storage_map("Evm", "AccountCodes", evm_account, &StorageHasher::Blake2_128Concat)
}
Expand Down Expand Up @@ -66,3 +131,16 @@ pub fn get_evm_account(account: &AccountId) -> H160 {
evm_acc_slice.copy_from_slice((<[u8; 32]>::from(account.clone())).get(0..20).unwrap());
evm_acc_slice.into()
}

impl From<TrustedGetterSigned<TrustedGetterEvm>> for TrustedOperation<TrustedGetterEvm> {
fn from(item: TrustedGetterSigned<TrustedGetterEvm>) -> Self {
let getter: Getter<TrustedGetterEvm> = item.into();
TrustedOperation::get(getter)
}
}

impl From<TrustedGetterSigned<TrustedGetterEvm>> for Getter<TrustedGetterEvm> {
fn from(item: TrustedGetterSigned<TrustedGetterEvm>) -> Self {
Getter::trusted(item)
}
}
60 changes: 5 additions & 55 deletions core-primitives/stf-primitives/src/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,11 @@ use log::*;
use sp_runtime::traits::Verify;
use std::prelude::v1::*;

#[cfg(feature = "evm")]
use ita_sgx_runtime::{AddressMapping, HashedAddressMapping};

#[cfg(feature = "evm")]
use crate::evm_helpers::{get_evm_account, get_evm_account_codes, get_evm_account_storages};

#[cfg(feature = "evm")]
use sp_core::{H160, H256};

#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq)]
#[allow(non_camel_case_types)]
pub enum Getter {
pub enum Getter<TG = TrustedGetter> {
public(PublicGetter),
trusted(TrustedGetterSigned),
trusted(TrustedGetterSigned<TG>),
}

impl From<PublicGetter> for Getter {
Expand All @@ -64,12 +55,6 @@ pub enum TrustedGetter {
free_balance(AccountId),
reserved_balance(AccountId),
nonce(AccountId),
#[cfg(feature = "evm")]
evm_nonce(AccountId),
#[cfg(feature = "evm")]
evm_account_codes(AccountId, H160),
#[cfg(feature = "evm")]
evm_account_storages(AccountId, H160, H256),
}

impl TrustedGetter {
Expand All @@ -78,12 +63,6 @@ impl TrustedGetter {
TrustedGetter::free_balance(sender_account) => sender_account,
TrustedGetter::reserved_balance(sender_account) => sender_account,
TrustedGetter::nonce(sender_account) => sender_account,
#[cfg(feature = "evm")]
TrustedGetter::evm_nonce(sender_account) => sender_account,
#[cfg(feature = "evm")]
TrustedGetter::evm_account_codes(sender_account, _) => sender_account,
#[cfg(feature = "evm")]
TrustedGetter::evm_account_storages(sender_account, ..) => sender_account,
}
}

Expand All @@ -94,12 +73,12 @@ impl TrustedGetter {
}

#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq)]
pub struct TrustedGetterSigned {
pub getter: TrustedGetter,
pub struct TrustedGetterSigned<TG = TrustedGetter> {
pub getter: TG,
pub signature: Signature,
}

impl TrustedGetterSigned {
impl TrustedGetterSigned<TrustedGetter> {
pub fn new(getter: TrustedGetter, signature: Signature) -> Self {
TrustedGetterSigned { getter, signature }
}
Expand Down Expand Up @@ -135,35 +114,6 @@ impl ExecuteGetter for Getter {
debug!("Account nonce is {}", nonce);
Some(nonce.encode())
},
#[cfg(feature = "evm")]
TrustedGetter::evm_nonce(who) => {
let evm_account = get_evm_account(who);
let evm_account = HashedAddressMapping::into_account_id(evm_account);
let nonce = System::account_nonce(&evm_account);
debug!("TrustedGetter evm_nonce");
debug!("Account nonce is {}", nonce);
Some(nonce.encode())
},
#[cfg(feature = "evm")]
TrustedGetter::evm_account_codes(_who, evm_account) =>
// TODO: This probably needs some security check if who == evm_account (or assosciated)
if let Some(info) = get_evm_account_codes(evm_account) {
debug!("TrustedGetter Evm Account Codes");
debug!("AccountCodes for {} is {:?}", evm_account, info);
Some(info) // TOOD: encoded?
} else {
None
},
#[cfg(feature = "evm")]
TrustedGetter::evm_account_storages(_who, evm_account, index) =>
// TODO: This probably needs some security check if who == evm_account (or assosciated)
if let Some(value) = get_evm_account_storages(evm_account, index) {
debug!("TrustedGetter Evm Account Storages");
debug!("AccountStorages for {} is {:?}", evm_account, value);
Some(value.encode())
} else {
None
},
},
Getter::public(g) => match g {
PublicGetter::some_value => Some(42u32.encode()),
Expand Down
10 changes: 6 additions & 4 deletions core-primitives/stf-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
limitations under the License.
*/
#![feature(stmt_expr_attributes)]
#![cfg_attr(all(not(target_env = "sgx"), not(feature = "std")), no_std)]
#![cfg_attr(target_env = "sgx", feature(rustc_private))]

#[cfg(all(not(feature = "std"), feature = "sgx"))]
extern crate sgx_tstd as std;

use getter::TrustedGetter;
#[cfg(feature = "sgx")]
pub use ita_sgx_runtime::{Balance, Index};
#[cfg(feature = "std")]
Expand All @@ -45,10 +47,10 @@ pub const ENCLAVE_ACCOUNT_KEY: &str = "Enclave_Account_Key";
use crate::trusted_call::TrustedCallSigned;
#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq)]
#[allow(non_camel_case_types)]
pub enum TrustedOperation {
pub enum TrustedOperation<T = TrustedGetter> {
indirect_call(TrustedCallSigned),
direct_call(TrustedCallSigned),
get(Getter),
get(Getter<T>),
}

impl From<TrustedCallSigned> for TrustedOperation {
Expand All @@ -63,8 +65,8 @@ impl From<Getter> for TrustedOperation {
}
}

impl From<TrustedGetterSigned> for TrustedOperation {
fn from(item: TrustedGetterSigned) -> Self {
impl From<TrustedGetterSigned<TrustedGetter>> for TrustedOperation {
fn from(item: TrustedGetterSigned<TrustedGetter>) -> Self {
TrustedOperation::get(item.into())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ use itp_node_api::{
};
use itp_sgx_crypto::{key_repository::AccessKey, ShieldingCryptoDecrypt, ShieldingCryptoEncrypt};
use itp_stf_executor::traits::StfEnclaveSigning;
use itp_stf_primitives::{trusted_call::TrustedCall, types::AccountId, TrustedOperation};
use itp_stf_primitives::{
getter::TrustedGetter, trusted_call::TrustedCall, types::AccountId, TrustedOperation,
};
use itp_top_pool_author::traits::AuthorApi;
use itp_types::{CallWorkerFn, OpaqueCall, ShardIdentifier, ShieldFundsFn, H256};
use log::*;
Expand Down Expand Up @@ -107,7 +109,8 @@ where
let trusted_call = TrustedCall::balance_shield(enclave_account_id, account, amount);
let signed_trusted_call =
self.stf_enclave_signer.sign_call_with_self(&trusted_call, &shard)?;
let trusted_operation = TrustedOperation::indirect_call(signed_trusted_call);
let trusted_operation: TrustedOperation<TrustedGetter> =
TrustedOperation::indirect_call(signed_trusted_call);

let encrypted_trusted_call = shielding_key.encrypt(&trusted_operation.encode())?;
self.submit_trusted_call(shard, encrypted_trusted_call);
Expand Down

0 comments on commit 467840f

Please sign in to comment.