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
+ } ;
18
14
use enigma_types:: ContractAddress ;
19
15
use epoch_keeper_t:: epoch_t:: { Epoch , EpochNonce } ;
20
- use enigma_tools_m :: keeper_types :: { decode , InputWorkerParams , RawEncodable } ;
16
+ use ethereum_types :: { H160 , U256 } ;
21
17
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 } ;
24
21
25
22
pub mod epoch_t;
26
23
27
24
const INIT_NONCE : uint32_t = 0 ;
28
25
const EPOCH_DIR : & str = "epoch" ;
29
26
30
27
// 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
+ }
32
31
33
32
/// The epoch root path is guaranteed to exist of the enclave was initialized
34
33
fn get_epoch_root_path ( ) -> path:: PathBuf {
@@ -37,44 +36,40 @@ fn get_epoch_root_path() -> path::PathBuf {
37
36
path_buf
38
37
}
39
38
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" ) }
43
40
44
41
fn get_epoch ( epoch_map : & HashMap < U256 , Epoch > , block_number : Option < U256 > ) -> Result < Option < Epoch > , EnclaveError > {
45
42
println ! ( "Getting epoch for block number: {:?}" , block_number) ;
46
43
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 ( ) {
50
46
println ! ( "Epoch not found" ) ;
51
47
let nonce_path = get_epoch_nonce_path ( ) ;
52
48
if is_document ( & nonce_path) {
53
49
println ! ( "Unsealing epoch nonce" ) ;
54
50
let mut sealed_log_out = [ 0u8 ; SEAL_LOG_SIZE ] ;
55
51
load_sealed_document ( & nonce_path, & mut sealed_log_out) ?;
56
52
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
64
57
}
65
58
}
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) )
67
65
}
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) )
72
66
}
73
67
74
68
/// 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 > {
76
71
let mut marker_doc: SealedDocumentStorage < EpochNonce > = SealedDocumentStorage {
77
- version : 0x1234 , //TODO: what's this?
72
+ version : 0x1234 , // TODO: what's this?
78
73
data : [ 0 ; 32 ] ,
79
74
} ;
80
75
marker_doc. data = nonce. into ( ) ;
@@ -85,12 +80,8 @@ fn new_epoch(nonce_map: &mut HashMap<U256, Epoch>, worker_params: &InputWorkerPa
85
80
save_sealed_document ( & marker_path, & sealed_log_in) ?;
86
81
println ! ( "Sealed the epoch marker: {:?}" , marker_path) ;
87
82
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
94
85
println ! ( "Storing epoch: {:?}" , epoch) ;
95
86
match nonce_map. insert ( nonce, epoch. clone ( ) ) {
96
87
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
106
97
println ! ( "Successfully decoded RLP worker parameters" ) ;
107
98
let mut guard = EPOCH . lock_expect ( "Epoch" ) ;
108
99
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 ) ;
111
101
112
102
println ! ( "Generated a nonce by incrementing the previous by 1 {:?}" , nonce) ;
113
103
* nonce_out = EpochNonce :: from ( nonce) ;
@@ -129,7 +119,9 @@ pub(crate) fn ecall_get_epoch_worker_internal(sc_addr: ContractAddress, block_nu
129
119
let epoch = match get_epoch ( & guard, block_number) ? {
130
120
Some ( epoch) => epoch,
131
121
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
+ } ) ) ;
133
125
}
134
126
} ;
135
127
println ! ( "Running worker selection using Epoch: {:?}" , epoch) ;
@@ -138,22 +130,17 @@ pub(crate) fn ecall_get_epoch_worker_internal(sc_addr: ContractAddress, block_nu
138
130
}
139
131
140
132
pub mod tests {
141
- use ethereum_types:: { H160 , U256 } ;
142
-
143
133
use super :: * ;
134
+ use ethereum_types:: { H160 , U256 } ;
144
135
145
- //noinspection RsTypeCheck
136
+ // noinspection RsTypeCheck
146
137
pub fn test_get_epoch_worker_internal ( ) {
147
138
let worker_params = InputWorkerParams {
148
139
block_number : U256 :: from ( 1 ) ,
149
140
workers : vec ! [ H160 :: from( 0 ) , H160 :: from( 1 ) , H160 :: from( 2 ) , H160 :: from( 3 ) ] ,
150
141
stakes : vec ! [ U256 :: from( 1 ) , U256 :: from( 1 ) , U256 :: from( 1 ) , U256 :: from( 1 ) ] ,
151
142
} ;
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 } ;
157
144
println ! ( "The epoch: {:?}" , epoch) ;
158
145
let sc_addr = ContractAddress :: from ( [ 1u8 ; 32 ] ) ;
159
146
let worker = epoch. get_selected_worker ( sc_addr) . unwrap ( ) ;
0 commit comments