Skip to content
This repository was archived by the owner on Jun 21, 2020. It is now read-only.

Commit 75ce7f9

Browse files
committed
MAINT: Rustfmt enigma-principal/enclave and removed unnecessary dependencies
1 parent 996df8a commit 75ce7f9

File tree

5 files changed

+110
-159
lines changed

5 files changed

+110
-159
lines changed

enigma-principal/enclave/Cargo.toml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,17 @@ authors = ["Enigma <[email protected]>"]
77
name = "enigma_principal_enclave"
88
crate-type = ["staticlib"]
99

10-
[features]
11-
default = []
12-
1310
[dependencies]
1411
enigma-tools-t = { path = "../../enigma-tools-t" }
1512
enigma-tools-m = { path = "../../enigma-tools-m", default-features = false, features = ["sgx"] }
1613
enigma-types = { path = "../../enigma-types", default-features = false, features = ["sgx"] }
1714
enigma-crypto = { path = "../../enigma-crypto", default-features = false, features = ["sgx", "asymmetric"] }
1815

1916
lazy_static = {version = "=1.2.0", features = ["spin_no_std"] }
20-
serde = { git = "https://github.com/baidu/rust-sgx-sdk.git", rev = "v1.0.4" }
21-
serde_derive = { git = "https://github.com/baidu/rust-sgx-sdk.git", rev = "v1.0.4" }
22-
serde_json = { git = "https://github.com/baidu/rust-sgx-sdk.git", rev = "v1.0.4" }
2317
ethabi = { git = "https://github.com/enigmampc/ethabi.git", rev = "sgx-v6.1.0", default-features = false}
2418
ethereum-types = { git = "https://github.com/enigmampc/primitives.git", rev = "sgx-v0.4.0", default-features = false}
25-
rustc-hex = { git = "https://github.com/enigmampc/rustc-hex.git", rev = "sgx-2.0.1-v1.0.4"}
26-
etcommon-hexutil = { version = "0.2", default-features = false }
27-
libsecp256k1 = "0.2.1"
28-
etcommon-bigint = { version = "0.2", default-features = false, features = ["rlp"] }
29-
etcommon-rlp = {version = "0.2", default-features = false}
3019

3120
sgx_types = { git = "https://github.com/baidu/rust-sgx-sdk.git", rev = "v1.0.4" }
3221
sgx_tstd = { git = "https://github.com/baidu/rust-sgx-sdk.git", rev = "v1.0.4" }
3322
sgx_trts = { git = "https://github.com/baidu/rust-sgx-sdk.git", rev = "v1.0.4" }
34-
sgx_tse = { git = "https://github.com/baidu/rust-sgx-sdk.git", rev = "v1.0.4" }
35-
sgx_tdh = { git = "https://github.com/baidu/rust-sgx-sdk.git", rev = "v1.0.4" }
36-
sgx_tservice = { git = "https://github.com/baidu/rust-sgx-sdk.git", rev = "v1.0.4" }
37-
sgx_alloc = { git = "https://github.com/baidu/rust-sgx-sdk.git", rev = "v1.0.4" }
38-
sgx_tseal = { git = "https://github.com/baidu/rust-sgx-sdk.git", rev = "v1.0.4" }
39-
sgx_rand = { git = "https://github.com/baidu/rust-sgx-sdk.git", rev = "v1.0.4" }
4023
sgx_tunittest = { git = "https://github.com/baidu/rust-sgx-sdk.git", rev = "v1.0.4" }

enigma-principal/enclave/src/epoch_keeper_t/epoch_t.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
use ethabi::{Bytes, encode, Token};
2-
use ethereum_types::{H160, U256};
3-
use enigma_types::ContractAddress;
4-
use enigma_tools_t::common::errors_t::{EnclaveError::{self, SystemError}, EnclaveSystemError};
51
use enigma_tools_m::keeper_types::{InputWorkerParams, RawEncodable};
2+
use enigma_tools_t::common::errors_t::{
3+
EnclaveError::{self, SystemError},
4+
EnclaveSystemError,
5+
};
6+
use enigma_types::ContractAddress;
7+
use ethabi::{encode, Bytes, Token};
8+
use ethereum_types::{H160, U256};
69
use std::string::ToString;
710

8-
911
pub type EpochNonce = [u8; 32];
1012

1113
#[derive(Debug, Clone)]
@@ -17,8 +19,9 @@ pub struct Epoch {
1719

1820
impl Epoch {
1921
pub fn get_selected_worker(&self, sc_addr: ContractAddress) -> Result<H160, EnclaveError> {
20-
self.worker_params.get_selected_worker(sc_addr, self.seed)
21-
.ok_or(SystemError(EnclaveSystemError::WorkerAuthError { err: "Worker selection returns nothing.".to_string() }))
22+
self.worker_params
23+
.get_selected_worker(sc_addr, self.seed)
24+
.ok_or_else(|| SystemError(EnclaveSystemError::WorkerAuthError { err: "Worker selection returns nothing.".to_string() }))
2225
}
2326
}
2427

enigma-principal/enclave/src/epoch_keeper_t/mod.rs

Lines changed: 45 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
1-
use ethabi::Token;
2-
use ethereum_types::{H256, H160, U256};
3-
use sgx_trts::trts::rsgx_read_rand;
4-
use sgx_types::*;
5-
use std::collections::HashMap;
6-
use std::path;
7-
use std::str;
8-
use std::string::ToString;
9-
use std::sync::SgxMutex;
10-
use std::sync::SgxMutexGuard;
11-
12-
use enigma_crypto::hash::Keccak256;
13-
use enigma_tools_t::common::errors_t::{EnclaveError, EnclaveError::*, EnclaveSystemError::*};
14-
use enigma_tools_m::utils::EthereumAddress;
15-
use enigma_tools_t::common::ToHex;
16-
use enigma_tools_t::common::utils_t::LockExpectMutex;
17-
use enigma_tools_t::document_storage_t::{is_document, load_sealed_document, save_sealed_document, SEAL_LOG_SIZE, SealedDocumentStorage};
1+
use crate::SIGNING_KEY;
2+
use enigma_tools_m::keeper_types::{decode, InputWorkerParams, RawEncodable};
3+
use enigma_tools_t::{
4+
common::{
5+
errors_t::{
6+
EnclaveError::{self, *},
7+
EnclaveSystemError::*,
8+
},
9+
utils_t::LockExpectMutex,
10+
ToHex,
11+
},
12+
document_storage_t::{is_document, load_sealed_document, save_sealed_document, SealedDocumentStorage, SEAL_LOG_SIZE},
13+
};
1814
use enigma_types::ContractAddress;
1915
use epoch_keeper_t::epoch_t::{Epoch, EpochNonce};
20-
use enigma_tools_m::keeper_types::{decode, InputWorkerParams, RawEncodable};
16+
use ethereum_types::{H160, U256};
2117
use ocalls_t;
22-
23-
use crate::SIGNING_KEY;
18+
use sgx_trts::trts::rsgx_read_rand;
19+
use sgx_types::*;
20+
use std::{collections::HashMap, path, str, string::ToString, sync::SgxMutex};
2421

2522
pub mod epoch_t;
2623

2724
const INIT_NONCE: uint32_t = 0;
2825
const EPOCH_DIR: &str = "epoch";
2926

3027
// The epoch seed contains the seeds + a nonce that must match the Ethereum tx
31-
lazy_static! { pub static ref EPOCH: SgxMutex< HashMap<U256, Epoch >> = SgxMutex::new(HashMap::new()); }
28+
lazy_static! {
29+
pub static ref EPOCH: SgxMutex<HashMap<U256, Epoch>> = SgxMutex::new(HashMap::new());
30+
}
3231

3332
/// The epoch root path is guaranteed to exist of the enclave was initialized
3433
fn get_epoch_root_path() -> path::PathBuf {
@@ -37,44 +36,40 @@ fn get_epoch_root_path() -> path::PathBuf {
3736
path_buf
3837
}
3938

40-
fn get_epoch_nonce_path() -> path::PathBuf {
41-
get_epoch_root_path().join("nonce.sealed")
42-
}
39+
fn get_epoch_nonce_path() -> path::PathBuf { get_epoch_root_path().join("nonce.sealed") }
4340

4441
fn get_epoch(epoch_map: &HashMap<U256, Epoch>, block_number: Option<U256>) -> Result<Option<Epoch>, EnclaveError> {
4542
println!("Getting epoch for block number: {:?}", block_number);
4643
if block_number.is_some() {
47-
return Err(SystemError(WorkerAuthError { err: "Epoch lookup by block number not implemented.".to_string() }));
48-
}
49-
if epoch_map.is_empty() {
44+
Err(SystemError(WorkerAuthError { err: "Epoch lookup by block number not implemented.".to_string() }))
45+
} else if epoch_map.is_empty() {
5046
println!("Epoch not found");
5147
let nonce_path = get_epoch_nonce_path();
5248
if is_document(&nonce_path) {
5349
println!("Unsealing epoch nonce");
5450
let mut sealed_log_out = [0u8; SEAL_LOG_SIZE];
5551
load_sealed_document(&nonce_path, &mut sealed_log_out)?;
5652
let doc = SealedDocumentStorage::<EpochNonce>::unseal(&mut sealed_log_out)?;
57-
match doc {
58-
Some(doc) => {
59-
let nonce = Some(doc.data);
60-
println!("found epoch marker: {:?}", nonce);
61-
//TODO: unseal the epoch
62-
}
63-
None => ()
53+
if let Some(doc) = doc {
54+
let nonce = Some(doc.data);
55+
println!("found epoch marker: {:?}", nonce);
56+
// TODO: unseal the epoch
6457
}
6558
}
66-
return Ok(None);
59+
Ok(None)
60+
} else {
61+
// The epoch map cannot be empty here
62+
let nonce = epoch_map.keys().max().unwrap();
63+
let epoch: Epoch = epoch_map.get(nonce).unwrap().clone();
64+
Ok(Some(epoch))
6765
}
68-
// The epoch map cannot be empty here
69-
let nonce = epoch_map.keys().max().unwrap().clone();
70-
let epoch: Epoch = epoch_map.get(&nonce).unwrap().clone();
71-
Ok(Some(epoch))
7266
}
7367

7468
/// Creates new epoch both in the cache and as sealed documents
75-
fn new_epoch(nonce_map: &mut HashMap<U256, Epoch>, worker_params: &InputWorkerParams, nonce: U256, seed: U256) -> Result<Epoch, EnclaveError> {
69+
fn new_epoch(nonce_map: &mut HashMap<U256, Epoch>, worker_params:
70+
&InputWorkerParams, nonce: U256, seed: U256) -> Result<Epoch, EnclaveError> {
7671
let mut marker_doc: SealedDocumentStorage<EpochNonce> = SealedDocumentStorage {
77-
version: 0x1234, //TODO: what's this?
72+
version: 0x1234, // TODO: what's this?
7873
data: [0; 32],
7974
};
8075
marker_doc.data = nonce.into();
@@ -85,12 +80,8 @@ fn new_epoch(nonce_map: &mut HashMap<U256, Epoch>, worker_params: &InputWorkerPa
8580
save_sealed_document(&marker_path, &sealed_log_in)?;
8681
println!("Sealed the epoch marker: {:?}", marker_path);
8782

88-
let epoch = Epoch {
89-
nonce,
90-
seed,
91-
worker_params: worker_params.clone(),
92-
};
93-
//TODO: seal the epoch
83+
let epoch = Epoch { nonce, seed, worker_params: worker_params.clone() };
84+
// TODO: seal the epoch
9485
println!("Storing epoch: {:?}", epoch);
9586
match nonce_map.insert(nonce, epoch.clone()) {
9687
Some(prev) => println!("New epoch stored successfully, previous epoch: {:?}", prev),
@@ -106,8 +97,7 @@ pub(crate) fn ecall_set_worker_params_internal(worker_params_rlp: &[u8], rand_ou
10697
println!("Successfully decoded RLP worker parameters");
10798
let mut guard = EPOCH.lock_expect("Epoch");
10899

109-
let nonce: U256 = get_epoch(&*guard, None)?
110-
.map_or_else(|| INIT_NONCE.into(), |_| guard.keys().max().unwrap() + 1);
100+
let nonce: U256 = get_epoch(&*guard, None)?.map_or_else(|| INIT_NONCE.into(), |_| guard.keys().max().unwrap() + 1);
111101

112102
println!("Generated a nonce by incrementing the previous by 1 {:?}", nonce);
113103
*nonce_out = EpochNonce::from(nonce);
@@ -129,7 +119,9 @@ pub(crate) fn ecall_get_epoch_worker_internal(sc_addr: ContractAddress, block_nu
129119
let epoch = match get_epoch(&guard, block_number)? {
130120
Some(epoch) => epoch,
131121
None => {
132-
return Err(SystemError(WorkerAuthError { err: format!("No epoch found for block number (None == latest): {:?}", block_number) }));
122+
return Err(SystemError(WorkerAuthError {
123+
err: format!("No epoch found for block number (None == latest): {:?}", block_number),
124+
}));
133125
}
134126
};
135127
println!("Running worker selection using Epoch: {:?}", epoch);
@@ -138,22 +130,17 @@ pub(crate) fn ecall_get_epoch_worker_internal(sc_addr: ContractAddress, block_nu
138130
}
139131

140132
pub mod tests {
141-
use ethereum_types::{H160, U256};
142-
143133
use super::*;
134+
use ethereum_types::{H160, U256};
144135

145-
//noinspection RsTypeCheck
136+
// noinspection RsTypeCheck
146137
pub fn test_get_epoch_worker_internal() {
147138
let worker_params = InputWorkerParams {
148139
block_number: U256::from(1),
149140
workers: vec![H160::from(0), H160::from(1), H160::from(2), H160::from(3)],
150141
stakes: vec![U256::from(1), U256::from(1), U256::from(1), U256::from(1)],
151142
};
152-
let epoch = Epoch {
153-
nonce: U256::from(0),
154-
seed: U256::from(1),
155-
worker_params,
156-
};
143+
let epoch = Epoch { nonce: U256::from(0), seed: U256::from(1), worker_params };
157144
println!("The epoch: {:?}", epoch);
158145
let sc_addr = ContractAddress::from([1u8; 32]);
159146
let worker = epoch.get_selected_worker(sc_addr).unwrap();

enigma-principal/enclave/src/keys_keeper_t/mod.rs

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
use ethabi::{decode, ParamType};
2-
use sgx_trts::trts::rsgx_read_rand;
3-
use std::{collections::HashMap, sync::SgxMutex, sync::SgxMutexGuard, vec::Vec};
4-
use std::path;
5-
use std::string::ToString;
6-
7-
use enigma_crypto::asymmetric::KeyPair;
8-
use enigma_crypto::Encryption;
9-
use enigma_crypto::hash::Keccak256;
10-
use enigma_tools_t::common::ToHex;
11-
use enigma_tools_m::utils::EthereumAddress;
12-
use enigma_tools_t::common::errors_t::{EnclaveError, EnclaveError::*, EnclaveSystemError::*};
13-
use enigma_tools_t::common::utils_t::LockExpectMutex;
14-
use enigma_tools_t::document_storage_t::{is_document, load_sealed_document, save_sealed_document, SEAL_LOG_SIZE, SealedDocumentStorage};
15-
use enigma_tools_m::primitives::km_primitives::{PrincipalMessage, PrincipalMessageType};
1+
use crate::SIGNING_KEY;
2+
use enigma_crypto::{asymmetric::KeyPair, Encryption};
3+
use enigma_tools_m::{
4+
primitives::km_primitives::{PrincipalMessage, PrincipalMessageType},
5+
utils::EthereumAddress,
6+
};
7+
use enigma_tools_t::{
8+
common::{
9+
errors_t::{
10+
EnclaveError::{self, *},
11+
EnclaveSystemError::*,
12+
},
13+
utils_t::LockExpectMutex,
14+
ToHex,
15+
},
16+
document_storage_t::{is_document, load_sealed_document, save_sealed_document, SealedDocumentStorage, SEAL_LOG_SIZE},
17+
};
1618
use enigma_types::{ContractAddress, Hash256, StateKey};
1719
use ocalls_t;
18-
19-
use crate::SIGNING_KEY;
20+
use sgx_trts::trts::rsgx_read_rand;
21+
use std::{collections::HashMap, path, sync::SgxMutex, vec::Vec};
2022

2123
const STATE_KEYS_DIR: &str = "state-keys";
2224

23-
lazy_static! { pub static ref STATE_KEY_STORE: SgxMutex< HashMap<ContractAddress, StateKey> > = SgxMutex::new(HashMap::new()); }
25+
lazy_static! {
26+
pub static ref STATE_KEY_STORE: SgxMutex<HashMap<ContractAddress, StateKey>> = SgxMutex::new(HashMap::new());
27+
}
2428

2529
/// The state keys root path is guaranteed to exist of the enclave was initialized
2630
fn get_state_keys_root_path() -> path::PathBuf {
@@ -35,7 +39,8 @@ fn get_document_path(sc_addr: &ContractAddress) -> path::PathBuf {
3539

3640
/// Read state keys from the cache and sealed documents.
3741
/// Adds keys to the cache after unsealing.
38-
fn get_state_keys(keys_map: &mut HashMap<ContractAddress, StateKey>, sc_addrs: &[ContractAddress]) -> Result<Vec<Option<StateKey>>, EnclaveError> {
42+
fn get_state_keys(keys_map: &mut HashMap<ContractAddress, StateKey>,
43+
sc_addrs: &[ContractAddress]) -> Result<Vec<Option<StateKey>>, EnclaveError> {
3944
let mut results: Vec<Option<StateKey>> = Vec::new();
4045
for &addr in sc_addrs {
4146
let key = match keys_map.get(&addr) {
@@ -69,11 +74,12 @@ fn get_state_keys(keys_map: &mut HashMap<ContractAddress, StateKey>, sc_addrs: &
6974
}
7075

7176
/// Creates new state keys both in the cache and as sealed documents
72-
fn new_state_keys(keys_map: &mut HashMap<ContractAddress, StateKey>, sc_addrs: &Vec<ContractAddress>) -> Result<Vec<StateKey>, EnclaveError> {
77+
fn new_state_keys(keys_map: &mut HashMap<ContractAddress, StateKey>,
78+
sc_addrs: &[ContractAddress]) -> Result<Vec<StateKey>, EnclaveError> {
7379
let mut results: Vec<StateKey> = Vec::new();
7480
for &addr in sc_addrs {
7581
let mut doc: SealedDocumentStorage<StateKey> = SealedDocumentStorage {
76-
version: 0x1234, //TODO: what's this?
82+
version: 0x1234, // TODO: what's this?
7783
data: [0; 32],
7884
};
7985
// Generate a new key randomly
@@ -109,7 +115,7 @@ fn build_get_state_keys_response(sc_addrs: Vec<ContractAddress>) -> Result<Vec<(
109115
}
110116
}
111117
new_state_keys(&mut guard, &new_addrs)?; // If the vector is empty this won't do anything.
112-
//Now we have keys for all addresses in cache
118+
// Now we have keys for all addresses in cache
113119
for addr in sc_addrs {
114120
match guard.get(&addr) {
115121
Some(&key) => response_data.push((addr, key)),
@@ -123,7 +129,9 @@ fn build_get_state_keys_response(sc_addrs: Vec<ContractAddress>) -> Result<Vec<(
123129

124130
/// Get encrypted state keys
125131
126-
pub(crate) fn ecall_get_enc_state_keys_internal(msg_bytes: &[u8], addrs_bytes: Vec<ContractAddress>, sig: [u8; 65], sig_out: &mut [u8; 65]) -> Result<Vec<u8>, EnclaveError> {
132+
pub(crate) fn ecall_get_enc_state_keys_internal(
133+
msg_bytes: &[u8], addrs_bytes: Vec<ContractAddress>, sig: [u8; 65], sig_out: &mut [u8; 65],
134+
) -> Result<Vec<u8>, EnclaveError> {
127135
let msg = PrincipalMessage::from_message(msg_bytes)?;
128136
let user_pubkey = msg.get_pubkey();
129137
let msg_id = msg.get_id();
@@ -157,11 +165,10 @@ pub(crate) fn ecall_get_enc_state_keys_internal(msg_bytes: &[u8], addrs_bytes: V
157165
}
158166

159167
pub mod tests {
160-
use enigma_tools_t::common::FromHex;
161-
162168
use super::*;
169+
use enigma_tools_t::common::FromHex;
163170

164-
//noinspection RsTypeCheck
171+
// noinspection RsTypeCheck
165172
pub fn test_state_keys_storage() {
166173
let data = vec![
167174
"9F86D081884C7D659A2FEAA0C55AD015A3BF4F1B2B0B822CD15D6C15B0F00A08".from_hex().unwrap(),
@@ -176,7 +183,8 @@ pub mod tests {
176183
let mut guard = STATE_KEY_STORE.lock_expect("State Key Store");
177184
let new_keys = new_state_keys(&mut guard, &sc_addrs).expect("Unable to store state keys");
178185

179-
let cached_keys = get_state_keys(&mut guard, &sc_addrs).expect("Unable to get state keys from cache")
186+
let cached_keys = get_state_keys(&mut guard, &sc_addrs)
187+
.expect("Unable to get state keys from cache")
180188
.iter()
181189
.map(|k| k.unwrap())
182190
.collect::<Vec<StateKey>>();
@@ -185,7 +193,8 @@ pub mod tests {
185193
// Clearing the cache to test retrieval of sealed keys form disk
186194
guard.clear();
187195

188-
let stored_keys = get_state_keys(&mut guard, &sc_addrs).expect("Unable to get state keys from sealed files")
196+
let stored_keys = get_state_keys(&mut guard, &sc_addrs)
197+
.expect("Unable to get state keys from sealed files")
189198
.iter()
190199
.map(|k| k.unwrap())
191200
.collect::<Vec<StateKey>>();

0 commit comments

Comments
 (0)