Skip to content

Commit 27ccb7d

Browse files
authored
Merge pull request #5341 from igor-casper/core-204
[VM2] Extract 'executor' from Context to the outer WasmerEnv
2 parents d0edaff + 588d677 commit 27ccb7d

File tree

5 files changed

+78
-73
lines changed

5 files changed

+78
-73
lines changed

executor/wasm/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,6 @@ impl ExecutorV2 {
924924
callee: callee_key,
925925
transferred_value,
926926
tracking_copy,
927-
executor: self.clone(),
928927
address_generator: Arc::clone(&address_generator),
929928
transaction_hash,
930929
chain_name,
@@ -958,7 +957,7 @@ impl ExecutorV2 {
958957
})?;
959958

960959
let mut instance = vm
961-
.instantiate(wasm_bytes, context, wasm_instance_config)
960+
.instantiate(wasm_bytes, self.clone(), context, wasm_instance_config)
962961
.map_err(ExecuteError::WasmPreparation)?;
963962

964963
self.push_execution_stack(execution_kind.clone());

executor/wasm_host/src/context.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::{collections::BTreeSet, sync::Arc};
22

33
use bytes::Bytes;
4-
use casper_executor_wasm_interface::executor::Executor;
54
use casper_storage::{
65
global_state::GlobalStateReader, AddressGenerator, RuntimeNativeConfig, TrackingCopy,
76
};
@@ -12,7 +11,7 @@ use casper_types::{
1211
use parking_lot::RwLock;
1312

1413
/// Container that holds all relevant modules necessary to process an execution request.
15-
pub struct Context<S: GlobalStateReader, E: Executor> {
14+
pub struct Context<S: GlobalStateReader> {
1615
/// The address of the account that initiated the contract or session code.
1716
pub initiator: AccountHash,
1817
/// The address of the addressable entity that is currently executing the contract or session
@@ -32,7 +31,6 @@ pub struct Context<S: GlobalStateReader, E: Executor> {
3231
pub baseline_motes_amount: u64,
3332
pub message_limits: MessageLimits,
3433
pub tracking_copy: TrackingCopy<S>,
35-
pub executor: E,
3634
pub transaction_hash: TransactionHash,
3735
pub address_generator: Arc<RwLock<AddressGenerator>>,
3836
pub chain_name: Arc<str>,

executor/wasm_host/src/host.rs

Lines changed: 46 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,17 @@ where
8585
}
8686

8787
/// Consumes imputed amount of gas.
88-
fn charge_gas<S: GlobalStateReader, E: Executor>(
89-
caller: &mut impl Caller<Context = Context<S, E>>,
88+
fn charge_gas<S: GlobalStateReader>(
89+
caller: &mut impl Caller<Context = Context<S>>,
9090
imputed: u64,
9191
) -> VMResult<()> {
9292
caller.consume_gas(imputed)?;
9393
Ok(())
9494
}
9595

9696
/// Consumes a set amount of gas for the specified storage value.
97-
fn charge_gas_storage<S: GlobalStateReader, E: Executor>(
98-
caller: &mut impl Caller<Context = Context<S, E>>,
97+
fn charge_gas_storage<S: GlobalStateReader>(
98+
caller: &mut impl Caller<Context = Context<S>>,
9999
size_bytes: usize,
100100
) -> VMResult<()> {
101101
let storage_costs = &caller.context().storage_costs;
@@ -106,14 +106,13 @@ fn charge_gas_storage<S: GlobalStateReader, E: Executor>(
106106
}
107107

108108
/// Consumes a set amount of gas for the specified host function and weights
109-
fn charge_host_function_call<S, E, const N: usize>(
110-
caller: &mut impl Caller<Context = Context<S, E>>,
109+
fn charge_host_function_call<S, const N: usize>(
110+
caller: &mut impl Caller<Context = Context<S>>,
111111
host_function: &HostFunctionV2<[u64; N]>,
112112
weights: [u64; N],
113113
) -> VMResult<()>
114114
where
115115
S: GlobalStateReader,
116-
E: Executor,
117116
{
118117
let Some(cost) = host_function.calculate_gas_cost(weights) else {
119118
// Overflowing gas calculation means gas limit was exceeded
@@ -125,8 +124,8 @@ where
125124
}
126125

127126
/// Writes a message to the global state and charges for storage used.
128-
fn metered_write<S: GlobalStateReader, E: Executor>(
129-
caller: &mut impl Caller<Context = Context<S, E>>,
127+
fn metered_write<S: GlobalStateReader>(
128+
caller: &mut impl Caller<Context = Context<S>>,
130129
key: Key,
131130
value: StoredValue,
132131
) -> VMResult<()> {
@@ -140,8 +139,8 @@ fn metered_write<S: GlobalStateReader, E: Executor>(
140139
}
141140

142141
/// Write value under a key.
143-
pub fn casper_write<S: GlobalStateReader, E: Executor>(
144-
mut caller: impl Caller<Context = Context<S, E>>,
142+
pub fn casper_write<S: GlobalStateReader>(
143+
mut caller: impl Caller<Context = Context<S>>,
145144
key_space: u64,
146145
key_ptr: u32,
147146
key_size: u32,
@@ -312,8 +311,8 @@ pub fn casper_write<S: GlobalStateReader, E: Executor>(
312311
///
313312
/// The name for this host function is `remove` to keep it simple and consistent with read/write
314313
/// verbs, and also consistent with the rust stdlib vocabulary i.e. `V`
315-
pub fn casper_remove<S: GlobalStateReader, E: Executor>(
316-
mut caller: impl Caller<Context = Context<S, E>>,
314+
pub fn casper_remove<S: GlobalStateReader>(
315+
mut caller: impl Caller<Context = Context<S>>,
317316
key_space: u64,
318317
key_ptr: u32,
319318
key_size: u32,
@@ -400,8 +399,8 @@ pub fn casper_remove<S: GlobalStateReader, E: Executor>(
400399
Ok(HOST_ERROR_SUCCESS)
401400
}
402401

403-
pub fn casper_print<S: GlobalStateReader, E: Executor>(
404-
mut caller: impl Caller<Context = Context<S, E>>,
402+
pub fn casper_print<S: GlobalStateReader>(
403+
mut caller: impl Caller<Context = Context<S>>,
405404
message_ptr: u32,
406405
message_size: u32,
407406
) -> VMResult<()> {
@@ -422,8 +421,8 @@ pub fn casper_print<S: GlobalStateReader, E: Executor>(
422421
}
423422

424423
/// Write value under a key.
425-
pub fn casper_read<S: GlobalStateReader, E: Executor>(
426-
mut caller: impl Caller<Context = Context<S, E>>,
424+
pub fn casper_read<S: GlobalStateReader>(
425+
mut caller: impl Caller<Context = Context<S>>,
427426
key_tag: u64,
428427
key_ptr: u32,
429428
key_size: u32,
@@ -616,8 +615,8 @@ pub fn casper_read<S: GlobalStateReader, E: Executor>(
616615
Ok(HOST_ERROR_SUCCESS)
617616
}
618617

619-
fn keyspace_to_global_state_key<S: GlobalStateReader, E: Executor>(
620-
context: &Context<S, E>,
618+
fn keyspace_to_global_state_key<S: GlobalStateReader>(
619+
context: &Context<S>,
621620
keyspace: Keyspace<'_>,
622621
) -> Option<Key> {
623622
let entity_addr = context_to_entity_addr(context);
@@ -655,9 +654,7 @@ fn keyspace_to_global_state_key<S: GlobalStateReader, E: Executor>(
655654
}
656655
}
657656

658-
fn context_to_entity_addr<S: GlobalStateReader, E: Executor>(
659-
context: &Context<S, E>,
660-
) -> EntityAddr {
657+
fn context_to_entity_addr<S: GlobalStateReader>(context: &Context<S>) -> EntityAddr {
661658
match context.callee {
662659
Key::Account(account_hash) => EntityAddr::new_account(account_hash.value()),
663660
Key::Hash(hash_addr) => EntityAddr::SmartContract(hash_addr),
@@ -669,8 +666,8 @@ fn context_to_entity_addr<S: GlobalStateReader, E: Executor>(
669666
}
670667
}
671668

672-
pub fn casper_copy_input<S: GlobalStateReader, E: Executor>(
673-
mut caller: impl Caller<Context = Context<S, E>>,
669+
pub fn casper_copy_input<S: GlobalStateReader>(
670+
mut caller: impl Caller<Context = Context<S>>,
674671
cb_alloc: u32,
675672
alloc_ctx: u32,
676673
) -> VMResult<u32> {
@@ -705,8 +702,8 @@ pub fn casper_copy_input<S: GlobalStateReader, E: Executor>(
705702
}
706703

707704
/// Returns from the execution of a smart contract with an optional flags.
708-
pub fn casper_return<S: GlobalStateReader, E: Executor>(
709-
mut caller: impl Caller<Context = Context<S, E>>,
705+
pub fn casper_return<S: GlobalStateReader>(
706+
mut caller: impl Caller<Context = Context<S>>,
710707
flags: u32,
711708
data_ptr: u32,
712709
data_len: u32,
@@ -747,8 +744,8 @@ pub fn casper_return<S: GlobalStateReader, E: Executor>(
747744
}
748745

749746
#[allow(clippy::too_many_arguments)]
750-
pub fn casper_create<S: GlobalStateReader + 'static, E: Executor + 'static>(
751-
mut caller: impl Caller<Context = Context<S, E>>,
747+
pub fn casper_create<S: GlobalStateReader + 'static>(
748+
mut caller: impl Caller<Context = Context<S>>,
752749
code_ptr: u32,
753750
code_len: u32,
754751
transferred_value: u64,
@@ -1037,8 +1034,7 @@ pub fn casper_create<S: GlobalStateReader + 'static, E: Executor + 'static>(
10371034
let tracking_copy_for_ctor = caller.context().tracking_copy.fork2();
10381035

10391036
match caller
1040-
.context()
1041-
.executor
1037+
.executor()
10421038
.execute(tracking_copy_for_ctor, execute_request)
10431039
{
10441040
Ok(ExecuteResult {
@@ -1087,8 +1083,8 @@ pub fn casper_create<S: GlobalStateReader + 'static, E: Executor + 'static>(
10871083
}
10881084

10891085
#[allow(clippy::too_many_arguments)]
1090-
pub fn casper_system<S: GlobalStateReader + 'static, E: Executor + 'static>(
1091-
mut caller: impl Caller<Context = Context<S, E>>,
1086+
pub fn casper_system<S: GlobalStateReader + 'static>(
1087+
mut caller: impl Caller<Context = Context<S>>,
10921088
system_contract_opt: u32,
10931089
input_ptr: u32,
10941090
input_len: u32,
@@ -1189,8 +1185,8 @@ pub fn casper_system<S: GlobalStateReader + 'static, E: Executor + 'static>(
11891185
}
11901186

11911187
#[allow(clippy::too_many_arguments)]
1192-
pub fn casper_call<S: GlobalStateReader + 'static, E: Executor + 'static>(
1193-
mut caller: impl Caller<Context = Context<S, E>>,
1188+
pub fn casper_call<S: GlobalStateReader + 'static>(
1189+
mut caller: impl Caller<Context = Context<S>>,
11941190
address_ptr: u32,
11951191
address_len: u32,
11961192
transferred_value: u64,
@@ -1294,19 +1290,15 @@ pub fn casper_call<S: GlobalStateReader + 'static, E: Executor + 'static>(
12941290
ret
12951291
}
12961292

1297-
fn exec<S: GlobalStateReader + 'static, E: Executor + 'static>(
1298-
mut caller: impl Caller<Context = Context<S, E>>,
1293+
fn exec<S: GlobalStateReader + 'static>(
1294+
mut caller: impl Caller<Context = Context<S>>,
12991295
execute_request: ExecuteRequest,
13001296
cb_alloc: u32,
13011297
cb_ctx: u32,
13021298
) -> VMResult<u32> {
13031299
let tracking_copy = caller.context().tracking_copy.fork2();
13041300

1305-
let (gas_usage, host_result) = match caller
1306-
.context()
1307-
.executor
1308-
.execute(tracking_copy, execute_request)
1309-
{
1301+
let (gas_usage, host_result) = match caller.executor().execute(tracking_copy, execute_request) {
13101302
Ok(ExecuteResult {
13111303
host_error,
13121304
output,
@@ -1360,8 +1352,8 @@ fn exec<S: GlobalStateReader + 'static, E: Executor + 'static>(
13601352
Ok(u32_from_host_result(host_result))
13611353
}
13621354

1363-
pub fn casper_env_balance<S: GlobalStateReader, E: Executor>(
1364-
mut caller: impl Caller<Context = Context<S, E>>,
1355+
pub fn casper_env_balance<S: GlobalStateReader>(
1356+
mut caller: impl Caller<Context = Context<S>>,
13651357
entity_kind: u32,
13661358
entity_addr_ptr: u32,
13671359
entity_addr_len: u32,
@@ -1525,8 +1517,8 @@ pub fn casper_env_balance<S: GlobalStateReader, E: Executor>(
15251517
Ok(HOST_ERROR_NOT_FOUND)
15261518
}
15271519

1528-
pub fn casper_upgrade<S: GlobalStateReader + 'static, E: Executor>(
1529-
mut caller: impl Caller<Context = Context<S, E>>,
1520+
pub fn casper_upgrade<S: GlobalStateReader + 'static>(
1521+
mut caller: impl Caller<Context = Context<S>>,
15301522
code_ptr: u32,
15311523
code_size: u32,
15321524
entry_point_ptr: u32,
@@ -1874,8 +1866,7 @@ pub fn casper_upgrade<S: GlobalStateReader + 'static, E: Executor>(
18741866
let tracking_copy_for_ctor = caller.context().tracking_copy.fork2();
18751867

18761868
match caller
1877-
.context()
1878-
.executor
1869+
.executor()
18791870
.execute(tracking_copy_for_ctor, execute_request)
18801871
{
18811872
Ok(ExecuteResult {
@@ -1924,8 +1915,8 @@ pub fn casper_upgrade<S: GlobalStateReader + 'static, E: Executor>(
19241915
Ok(CALLEE_SUCCEEDED)
19251916
}
19261917

1927-
pub fn casper_env_info<S: GlobalStateReader, E: Executor>(
1928-
mut caller: impl Caller<Context = Context<S, E>>,
1918+
pub fn casper_env_info<S: GlobalStateReader>(
1919+
mut caller: impl Caller<Context = Context<S>>,
19291920
info_ptr: u32,
19301921
info_size: u32,
19311922
) -> VMResult<u32> {
@@ -1982,8 +1973,8 @@ pub fn casper_env_info<S: GlobalStateReader, E: Executor>(
19821973
Ok(HOST_ERROR_SUCCESS)
19831974
}
19841975

1985-
pub fn casper_emit<S: GlobalStateReader, E: Executor>(
1986-
mut caller: impl Caller<Context = Context<S, E>>,
1976+
pub fn casper_emit<S: GlobalStateReader>(
1977+
mut caller: impl Caller<Context = Context<S>>,
19871978
topic_name_ptr: u32,
19881979
topic_name_size: u32,
19891980
payload_ptr: u32,
@@ -2199,8 +2190,8 @@ pub fn casper_emit<S: GlobalStateReader, E: Executor>(
21992190
/// * `in_size` - size of output pointer
22002191
/// * `hash_algo_type` - integer representation of HashAlgorithm enum variant
22012192
/// * `out_ptr` - pointer to the location where argument bytes will be copied to the host side
2202-
pub fn casper_generic_hash<S: GlobalStateReader, E: Executor>(
2203-
mut caller: impl Caller<Context = Context<S, E>>,
2193+
pub fn casper_generic_hash<S: GlobalStateReader>(
2194+
mut caller: impl Caller<Context = Context<S>>,
22042195
in_ptr: u32,
22052196
in_size: u32,
22062197
hash_algorithm: u32,
@@ -2282,8 +2273,8 @@ pub fn casper_generic_hash<S: GlobalStateReader, E: Executor>(
22822273
/// multiplication 𝑘×𝑮 odd?
22832274
/// - Hi bit (3/4): did the affine x-coordinate of 𝑘×𝑮 overflow the order of the scalar field,
22842275
/// requiring a reduction when computing r?
2285-
pub fn casper_recover_secp256k1<S: GlobalStateReader, E: Executor>(
2286-
mut caller: impl Caller<Context = Context<S, E>>,
2276+
pub fn casper_recover_secp256k1<S: GlobalStateReader>(
2277+
mut caller: impl Caller<Context = Context<S>>,
22872278
message_ptr: u32,
22882279
message_size: u32,
22892280
signature_ptr: u32,

executor/wasm_interface/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ impl MeteringPoints {
264264
/// instance, wasm linear memory access, etc.
265265
pub trait Caller {
266266
type Context;
267+
type Executor: crate::executor::Executor;
267268

268269
fn context(&self) -> &Self::Context;
269270
fn context_mut(&mut self) -> &mut Self::Context;
@@ -284,6 +285,8 @@ pub trait Caller {
284285
fn get_remaining_points(&mut self) -> VMResult<MeteringPoints>;
285286
/// Check for gas exhaustion, then reduce remaining by amount if able.
286287
fn consume_gas(&mut self, value: u64) -> VMResult<()>;
288+
/// Returns a reference to the executor used by the current instance.
289+
fn executor(&self) -> &Self::Executor;
287290
}
288291

289292
#[derive(Debug, Error)]

0 commit comments

Comments
 (0)