@@ -3,18 +3,254 @@ pub mod logic;
3
3
pub mod profile;
4
4
5
5
use std:: str:: FromStr as _;
6
+ use std:: sync:: MutexGuard ;
6
7
7
8
use js_sys:: ArrayBuffer ;
9
+ use logic:: mocks:: mock_external:: MockedValuePtr ;
8
10
pub use logic:: with_ext_cost_counter;
9
- use logic:: { gas_counter, ExecutionResultState , External , GasCounter , MemSlice , VMContext } ;
11
+ use logic:: {
12
+ gas_counter, ExecutionResultState , External , GasCounter , MemSlice , VMContext , VMLogicError ,
13
+ ValuePtr ,
14
+ } ;
10
15
use logic:: { mocks:: mock_external, types:: PromiseIndex } ;
11
16
use near_parameters:: vm:: Config ;
12
17
pub use near_primitives_core:: code:: ContractCode ;
13
- use near_primitives_core:: types:: { AccountId , EpochHeight , Gas , StorageUsage } ;
18
+ use near_primitives_core:: types:: { AccountId , Balance , EpochHeight , Gas , StorageUsage } ;
14
19
pub use profile:: ProfileDataV3 ;
15
20
use serde:: Serialize as _;
21
+ use std:: result:: Result as SResult ;
16
22
use wasm_bindgen:: prelude:: * ;
17
23
24
+ fn js_serializer ( ) -> serde_wasm_bindgen:: Serializer {
25
+ serde_wasm_bindgen:: Serializer :: new ( )
26
+ . serialize_missing_as_null ( true )
27
+ . serialize_large_number_types_as_bigints ( true )
28
+ . serialize_bytes_as_arrays ( false )
29
+ }
30
+
31
+ #[ wasm_bindgen]
32
+ #[ derive( Clone ) ]
33
+ pub struct Store ( std:: sync:: Arc < std:: sync:: Mutex < std:: collections:: HashMap < Vec < u8 > , Vec < u8 > > > > ) ;
34
+
35
+ #[ wasm_bindgen]
36
+ impl Store {
37
+ #[ wasm_bindgen( constructor) ]
38
+ pub fn new ( ) -> Self {
39
+ Self ( Default :: default ( ) )
40
+ }
41
+
42
+ fn guard ( & self ) -> MutexGuard < std:: collections:: HashMap < Vec < u8 > , Vec < u8 > > > {
43
+ self . 0 . lock ( ) . unwrap_or_else ( |e| e. into_inner ( ) )
44
+ }
45
+
46
+ pub fn to_value ( & self ) -> Result < JsValue > {
47
+ self . guard ( ) . serialize ( & js_serializer ( ) ) . map_err ( Into :: into)
48
+ }
49
+
50
+ pub fn set ( & self , key : & [ u8 ] , value : & [ u8 ] ) {
51
+ self . guard ( ) . insert ( key. to_vec ( ) , value. to_vec ( ) ) ;
52
+ }
53
+
54
+ pub fn get ( & self , key : & [ u8 ] ) -> Option < Vec < u8 > > {
55
+ self . guard ( ) . get ( key) . cloned ( )
56
+ }
57
+
58
+ pub fn remove ( & self , key : & [ u8 ] ) {
59
+ self . guard ( ) . remove ( key) ;
60
+ }
61
+
62
+ pub fn remove_subtree ( & self , prefix : & [ u8 ] ) {
63
+ self . guard ( ) . retain ( |key, _| !key. starts_with ( prefix) ) ;
64
+ }
65
+
66
+ pub fn has_key ( & self , key : & [ u8 ] ) -> bool {
67
+ self . guard ( ) . contains_key ( key)
68
+ }
69
+ }
70
+
71
+ #[ wasm_bindgen]
72
+ pub struct DebugExternal {
73
+ store : Store ,
74
+ }
75
+
76
+ #[ wasm_bindgen]
77
+ impl DebugExternal {
78
+ #[ wasm_bindgen( constructor) ]
79
+ pub fn new ( store : & Store ) -> Self {
80
+ Self {
81
+ store : store. clone ( ) ,
82
+ }
83
+ }
84
+ }
85
+
86
+ impl External for DebugExternal {
87
+ fn storage_set ( & mut self , key : & [ u8 ] , value : & [ u8 ] ) -> SResult < ( ) , VMLogicError > {
88
+ self . store . set ( key, value) ;
89
+ Ok ( ( ) )
90
+ }
91
+
92
+ fn storage_get < ' a > (
93
+ & ' a self ,
94
+ key : & [ u8 ] ,
95
+ _: near_parameters:: vm:: StorageGetMode ,
96
+ ) -> SResult < Option < Box < dyn logic:: ValuePtr + ' a > > , VMLogicError > {
97
+ let v = self . store . get ( key) ;
98
+ Ok ( v. map ( |v| Box :: new ( MockedValuePtr :: new ( & v) ) as Box < _ > ) )
99
+ }
100
+
101
+ fn storage_remove ( & mut self , key : & [ u8 ] ) -> SResult < ( ) , VMLogicError > {
102
+ self . store . remove ( key) ;
103
+ Ok ( ( ) )
104
+ }
105
+
106
+ fn storage_remove_subtree ( & mut self , prefix : & [ u8 ] ) -> SResult < ( ) , VMLogicError > {
107
+ self . store . remove_subtree ( prefix) ;
108
+ Ok ( ( ) )
109
+ }
110
+
111
+ fn storage_has_key (
112
+ & mut self ,
113
+ key : & [ u8 ] ,
114
+ _: near_parameters:: vm:: StorageGetMode ,
115
+ ) -> SResult < bool , VMLogicError > {
116
+ Ok ( self . store . has_key ( key) )
117
+ }
118
+
119
+ fn generate_data_id ( & mut self ) -> near_primitives_core:: hash:: CryptoHash {
120
+ todo ! ( )
121
+ }
122
+
123
+ fn get_trie_nodes_count ( & self ) -> logic:: TrieNodesCount {
124
+ logic:: TrieNodesCount { db_reads : 0 , mem_reads : 0 }
125
+ }
126
+
127
+ fn get_recorded_storage_size ( & self ) -> usize {
128
+ 0
129
+ }
130
+
131
+ fn validator_stake ( & self , account_id : & AccountId ) -> SResult < Option < Balance > , VMLogicError > {
132
+ todo ! ( )
133
+ }
134
+
135
+ fn validator_total_stake ( & self ) -> SResult < Balance , VMLogicError > {
136
+ todo ! ( )
137
+ }
138
+
139
+ fn create_action_receipt (
140
+ & mut self ,
141
+ receipt_indices : Vec < logic:: types:: ReceiptIndex > ,
142
+ receiver_id : AccountId ,
143
+ ) -> SResult < logic:: types:: ReceiptIndex , logic:: VMLogicError > {
144
+ todo ! ( )
145
+ }
146
+
147
+ fn create_promise_yield_receipt (
148
+ & mut self ,
149
+ receiver_id : AccountId ,
150
+ ) -> SResult <
151
+ (
152
+ logic:: types:: ReceiptIndex ,
153
+ near_primitives_core:: hash:: CryptoHash ,
154
+ ) ,
155
+ logic:: VMLogicError ,
156
+ > {
157
+ todo ! ( )
158
+ }
159
+
160
+ fn submit_promise_resume_data (
161
+ & mut self ,
162
+ data_id : near_primitives_core:: hash:: CryptoHash ,
163
+ data : Vec < u8 > ,
164
+ ) -> SResult < bool , logic:: VMLogicError > {
165
+ todo ! ( )
166
+ }
167
+
168
+ fn append_action_create_account (
169
+ & mut self ,
170
+ receipt_index : logic:: types:: ReceiptIndex ,
171
+ ) -> SResult < ( ) , logic:: VMLogicError > {
172
+ todo ! ( )
173
+ }
174
+
175
+ fn append_action_deploy_contract (
176
+ & mut self ,
177
+ receipt_index : logic:: types:: ReceiptIndex ,
178
+ code : Vec < u8 > ,
179
+ ) -> SResult < ( ) , logic:: VMLogicError > {
180
+ todo ! ( )
181
+ }
182
+
183
+ fn append_action_function_call_weight (
184
+ & mut self ,
185
+ receipt_index : logic:: types:: ReceiptIndex ,
186
+ method_name : Vec < u8 > ,
187
+ args : Vec < u8 > ,
188
+ attached_deposit : Balance ,
189
+ prepaid_gas : Gas ,
190
+ gas_weight : near_primitives_core:: types:: GasWeight ,
191
+ ) -> SResult < ( ) , logic:: VMLogicError > {
192
+ todo ! ( )
193
+ }
194
+
195
+ fn append_action_transfer (
196
+ & mut self ,
197
+ receipt_index : logic:: types:: ReceiptIndex ,
198
+ deposit : Balance ,
199
+ ) -> SResult < ( ) , logic:: VMLogicError > {
200
+ todo ! ( )
201
+ }
202
+
203
+ fn append_action_stake (
204
+ & mut self ,
205
+ receipt_index : logic:: types:: ReceiptIndex ,
206
+ stake : Balance ,
207
+ public_key : near_crypto:: PublicKey ,
208
+ ) {
209
+ todo ! ( )
210
+ }
211
+
212
+ fn append_action_add_key_with_full_access (
213
+ & mut self ,
214
+ receipt_index : logic:: types:: ReceiptIndex ,
215
+ public_key : near_crypto:: PublicKey ,
216
+ nonce : near_primitives_core:: types:: Nonce ,
217
+ ) {
218
+ todo ! ( )
219
+ }
220
+
221
+ fn append_action_add_key_with_function_call (
222
+ & mut self ,
223
+ receipt_index : logic:: types:: ReceiptIndex ,
224
+ public_key : near_crypto:: PublicKey ,
225
+ nonce : near_primitives_core:: types:: Nonce ,
226
+ allowance : Option < Balance > ,
227
+ receiver_id : AccountId ,
228
+ method_names : Vec < Vec < u8 > > ,
229
+ ) -> SResult < ( ) , logic:: VMLogicError > {
230
+ todo ! ( )
231
+ }
232
+
233
+ fn append_action_delete_key (
234
+ & mut self ,
235
+ receipt_index : logic:: types:: ReceiptIndex ,
236
+ public_key : near_crypto:: PublicKey ,
237
+ ) {
238
+ todo ! ( )
239
+ }
240
+
241
+ fn append_action_delete_account (
242
+ & mut self ,
243
+ receipt_index : logic:: types:: ReceiptIndex ,
244
+ beneficiary_id : AccountId ,
245
+ ) -> SResult < ( ) , logic:: VMLogicError > {
246
+ todo ! ( )
247
+ }
248
+
249
+ fn get_receipt_receiver ( & self , receipt_index : logic:: types:: ReceiptIndex ) -> & AccountId {
250
+ todo ! ( )
251
+ }
252
+ }
253
+
18
254
#[ wasm_bindgen]
19
255
pub struct Context ( VMContext ) ;
20
256
@@ -60,7 +296,7 @@ type Result<T> = std::result::Result<T, JsError>;
60
296
#[ wasm_bindgen]
61
297
impl Logic {
62
298
#[ wasm_bindgen( constructor) ]
63
- pub fn new ( context : Context , memory : js_sys:: WebAssembly :: Memory ) -> Self {
299
+ pub fn new ( context : Context , memory : js_sys:: WebAssembly :: Memory , store : & Store ) -> Self {
64
300
let max_gas_burnt = u64:: max_value ( ) ;
65
301
let prepaid_gas = u64:: max_value ( ) ;
66
302
let is_view = false ;
@@ -75,10 +311,10 @@ impl Logic {
75
311
) ;
76
312
let result_state =
77
313
ExecutionResultState :: new ( & context. 0 , gas_counter, config. wasm_config . clone ( ) ) ;
78
- let mock_ext = Box :: new ( mock_external :: MockedExternal :: new ( ) ) ;
314
+ let ext = Box :: new ( DebugExternal :: new ( store ) ) ;
79
315
Self {
80
316
logic : logic:: VMLogic :: new (
81
- mock_ext ,
317
+ ext ,
82
318
context. 0 ,
83
319
config. fees . clone ( ) ,
84
320
result_state,
@@ -87,17 +323,10 @@ impl Logic {
87
323
}
88
324
}
89
325
90
- fn js_serializer ( & self ) -> serde_wasm_bindgen:: Serializer {
91
- serde_wasm_bindgen:: Serializer :: new ( )
92
- . serialize_missing_as_null ( true )
93
- . serialize_large_number_types_as_bigints ( true )
94
- . serialize_bytes_as_arrays ( false )
95
- }
96
-
97
326
pub fn context ( & self ) -> Result < JsValue > {
98
327
self . logic
99
328
. context
100
- . serialize ( & self . js_serializer ( ) )
329
+ . serialize ( & js_serializer ( ) )
101
330
. map_err ( Into :: into)
102
331
}
103
332
@@ -106,12 +335,12 @@ impl Logic {
106
335
. result_state
107
336
. clone ( )
108
337
. compute_outcome ( )
109
- . serialize ( & self . js_serializer ( ) )
338
+ . serialize ( & js_serializer ( ) )
110
339
. map_err ( Into :: into)
111
340
}
112
341
113
342
pub fn registers ( & mut self ) -> Result < JsValue > {
114
- let s = self . js_serializer ( ) ;
343
+ let s = js_serializer ( ) ;
115
344
self . logic . registers ( ) . serialize ( & s) . map_err ( Into :: into)
116
345
}
117
346
0 commit comments