Skip to content

Commit 6330856

Browse files
committed
remove old gas metering
1 parent 1703332 commit 6330856

File tree

2 files changed

+19
-104
lines changed

2 files changed

+19
-104
lines changed

packages/vm/src/lib.rs

Lines changed: 14 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::sync::Arc;
1313
use wasmer::Universal;
1414
use wasmer_middlewares::metering::{get_remaining_points, MeteringPoints};
1515

16-
use pwasm_utils::{self, rules};
16+
use pwasm_utils::{self};
1717

1818
use wasmer::Singlepass;
1919

@@ -79,11 +79,11 @@ fn inject_stack_height(module: Module) -> Result<Module, Error> {
7979
.map_err(|_| Error::StackHeightInjectionError)
8080
}
8181

82-
fn inject_gas(module: Module) -> Result<Module, Error> {
83-
// Simple gas rule. Every opcode and memory growth costs 1 gas.
84-
let gas_rules = rules::Set::new(1, Default::default()).with_grow_cost(1);
85-
pwasm_utils::inject_gas_counter(module, &gas_rules).map_err(|_| Error::GasCounterInjectionError)
86-
}
82+
// fn inject_gas(module: Module) -> Result<Module, Error> {
83+
// // Simple gas rule. Every opcode and memory growth costs 1 gas.
84+
// let gas_rules = rules::Set::new(1, Default::default()).with_grow_cost(1);
85+
// pwasm_utils::inject_gas_counter(module, &gas_rules).map_err(|_| Error::GasCounterInjectionError)
86+
// }
8787

8888
fn check_wasm_exports(module: &Module) -> Result<(), Error> {
8989
let available_exports: Vec<&str> = module.export_section().map_or(vec![], |export_section| {
@@ -122,22 +122,6 @@ pub fn compile(code: &[u8]) -> Result<Vec<u8>, Error> {
122122
// Check that the given Wasm code is indeed a valid Wasm.
123123
wasmparser::validate(code).map_err(|_| Error::ValidationError)?;
124124

125-
// Start the compiling chains.
126-
let module = elements::deserialize_buffer(code).map_err(|_| Error::DeserializationError)?;
127-
check_wasm_exports(&module)?;
128-
check_wasm_imports(&module)?;
129-
let module = inject_memory(module)?;
130-
let module = inject_gas(module)?;
131-
let module = inject_stack_height(module)?;
132-
133-
// Serialize the final Wasm code back to bytes.
134-
elements::serialize(module).map_err(|_| Error::SerializationError)
135-
}
136-
137-
pub fn new_compile(code: &[u8]) -> Result<Vec<u8>, Error> {
138-
// Check that the given Wasm code is indeed a valid Wasm.
139-
wasmparser::validate(code).map_err(|_| Error::ValidationError)?;
140-
141125
// Start the compiling chains.
142126
let module = elements::deserialize_buffer(code).map_err(|_| Error::DeserializationError)?;
143127
check_wasm_exports(&module)?;
@@ -177,7 +161,7 @@ pub fn run<E>(
177161
where
178162
E: vm::Env + 'static,
179163
{
180-
let owasm_env = Environment::new(env, gas);
164+
let owasm_env = Environment::new(env);
181165

182166
let mut compiler = Singlepass::new();
183167
let metering = Arc::new(Metering::new(0, cost));
@@ -187,10 +171,7 @@ where
187171

188172
let import_object = imports! {
189173
"env" => {
190-
"gas" => Function::new_native_with_env(&store, owasm_env.clone(), |env: &Environment<E>, gas: u32| {
191-
env.with_mut_vm(|vm| {
192-
vm.consume_gas(gas)
193-
})
174+
"gas" => Function::new_native_with_env(&store, owasm_env.clone(), |_env: &Environment<E>, _gas: u32| {
194175
}),
195176
"get_span_size" => Function::new_native_with_env(&store, owasm_env.clone(), |env: &Environment<E>| {
196177
env.with_vm(|vm| {
@@ -201,7 +182,6 @@ where
201182
env.with_mut_vm(|vm| -> Result<i64, Error>{
202183
let span_size = vm.env.get_span_size();
203184
// consume gas equal size of span when read calldata
204-
vm.consume_gas(span_size as u32)?;
205185
env.decrease_gas_left(span_size as u32)?;
206186

207187
let memory = env.memory()?;
@@ -225,7 +205,6 @@ where
225205
}
226206

227207
// consume gas equal size of span when save data to memory
228-
vm.consume_gas(span_size as u32)?;
229208
env.decrease_gas_left(span_size as u32)?;
230209

231210
let memory = env.memory()?;
@@ -288,7 +267,6 @@ where
288267
env.with_mut_vm(|vm| -> Result<i64, Error>{
289268
let span_size = vm.env.get_span_size();
290269
// consume gas equal size of span when read data from report
291-
vm.consume_gas(span_size as u32)?;
292270
env.decrease_gas_left(span_size as u32)?;
293271

294272
let memory = env.memory()?;
@@ -368,7 +346,6 @@ mod test {
368346
use parity_wasm::elements;
369347
use std::io::{Read, Write};
370348
use std::process::Command;
371-
use std::time::Instant;
372349
use tempfile::NamedTempFile;
373350

374351
pub struct MockEnv {}
@@ -469,19 +446,13 @@ mod test {
469446
r#"(module
470447
(type (;0;) (func (param i64 i64 i32 i64) (result i64)))
471448
(type (;1;) (func))
472-
(type (;2;) (func (param i32)))
473449
(import "env" "ask_external_data" (func (;0;) (type 0)))
474-
(import "env" "gas" (func (;1;) (type 2)))
475-
(func (;2;) (type 1)
450+
(func (;1;) (type 1)
476451
(local i32)
477-
i32.const 4
478-
call 1
479452
i32.const 0
480453
local.set 0
481454
block ;; label = @1
482455
loop ;; label = @2
483-
i32.const 8
484-
call 1
485456
local.get 0
486457
i32.const 1
487458
i32.add
@@ -492,8 +463,8 @@ mod test {
492463
br_if 0 (;@2;)
493464
end
494465
end)
495-
(func (;3;) (type 1))
496-
(func (;4;) (type 1)
466+
(func (;2;) (type 1))
467+
(func (;3;) (type 1)
497468
global.get 0
498469
i32.const 3
499470
i32.add
@@ -504,15 +475,15 @@ mod test {
504475
if ;; label = @1
505476
unreachable
506477
end
507-
call 2
478+
call 1
508479
global.get 0
509480
i32.const 3
510481
i32.sub
511482
global.set 0)
512483
(memory (;0;) 17 512)
513484
(global (;0;) (mut i32) (i32.const 0))
514485
(export "prepare" (func 0))
515-
(export "execute" (func 4))
486+
(export "execute" (func 3))
516487
(data (;0;) (i32.const 1048576) "beeb"))"#,
517488
);
518489
assert_eq!(code, expected);
@@ -545,7 +516,7 @@ mod test {
545516
let mut cache = Cache::new(CacheOptions { cache_size: 10000 });
546517
let env = MockEnv {};
547518
let gas_used = run(&mut cache, &code, 4294967290, true, env).unwrap();
548-
assert_eq!(gas_used, 1000015 as u64);
519+
assert_eq!(gas_used, 800013 as u64);
549520
}
550521

551522
// #[test]
@@ -584,46 +555,6 @@ mod test {
584555
// assert_eq!(gas_used, 1000015 as u32);
585556
// }
586557

587-
#[test]
588-
fn test_run_time() {
589-
let wasm = wat2wasm(
590-
r#"(module
591-
(type (func (param i64 i64 i64 i64) (result)))
592-
(func
593-
(local $idx i32)
594-
(local.set $idx (i32.const 0))
595-
(block
596-
(loop
597-
(local.set $idx (local.get $idx) (i32.const 1) (i32.add) )
598-
(br_if 0 (i32.lt_u (local.get $idx) (i32.const 1000000)))
599-
)
600-
)
601-
)
602-
(func (;"execute": Resolves with result "beeb";)
603-
)
604-
(memory 17)
605-
(data (i32.const 1048576) "beeb") (;str = "beeb";)
606-
(export "prepare" (func 0))
607-
(export "execute" (func 1)))
608-
"#,
609-
);
610-
let code = compile(&wasm).unwrap();
611-
let new_code = new_compile(&wasm).unwrap();
612-
let mut cache = Cache::new(CacheOptions { cache_size: 10000 });
613-
let env = MockEnv {};
614-
615-
let now = Instant::now();
616-
let _gas_used = run(&mut cache, &code, 4294967290, true, env).unwrap();
617-
let elapsed_time = now.elapsed();
618-
619-
let env = MockEnv {};
620-
621-
let now = Instant::now();
622-
let _gas_used = run(&mut cache, &new_code, 4294967290, true, env).unwrap();
623-
let elapsed_time_2 = now.elapsed();
624-
assert_eq!(true, elapsed_time.as_micros() > elapsed_time_2.as_micros());
625-
}
626-
627558
#[test]
628559
fn test_inject_memory_no_memory() {
629560
let wasm = wat2wasm("(module)");

packages/vm/src/vm.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,16 @@ pub struct VMLogic<E>
3737
where
3838
E: Env,
3939
{
40-
pub env: E, // The execution environment.
41-
pub gas_limit: u64, // Amount of gas allowed for total execution.
42-
pub gas_used: u64, // Amount of gas used in this execution.
40+
pub env: E, // The execution environment.
4341
}
4442

4543
impl<E> VMLogic<E>
4644
where
4745
E: Env,
4846
{
4947
/// Creates a new `VMLogic` instance.
50-
pub fn new(env: E, gas: u64) -> Self {
51-
Self { env: env, gas_limit: gas, gas_used: 0 }
52-
}
53-
54-
/// Consumes the given amount of gas. Return `OutOfGasError` error if run out of gas.
55-
pub fn consume_gas(&mut self, gas: u32) -> Result<(), Error> {
56-
self.gas_used = self.gas_used.saturating_add(gas as u64);
57-
if self.out_of_gas() {
58-
Err(Error::OutOfGasError)
59-
} else {
60-
Ok(())
61-
}
62-
}
63-
64-
pub fn out_of_gas(&self) -> bool {
65-
self.gas_used > self.gas_limit
48+
pub fn new(env: E) -> Self {
49+
Self { env: env }
6650
}
6751
}
6852

@@ -98,9 +82,9 @@ impl<E> Environment<E>
9882
where
9983
E: Env + 'static,
10084
{
101-
pub fn new(e: E, gas: u64) -> Self {
85+
pub fn new(e: E) -> Self {
10286
Self {
103-
vm: Arc::new(Mutex::new(VMLogic::<E>::new(e, gas))),
87+
vm: Arc::new(Mutex::new(VMLogic::<E>::new(e))),
10488
data: Arc::new(RwLock::new(ContextData::new())),
10589
}
10690
}

0 commit comments

Comments
 (0)