Skip to content

Commit 1703332

Browse files
committed
remove vrf & change gas to u64
1 parent 3ee8a74 commit 1703332

File tree

3 files changed

+45
-36
lines changed

3 files changed

+45
-36
lines changed

packages/vm/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "owasm-vm"
33
version = "0.1.12"
44
authors = ["Band Protocol <[email protected]>"]
5-
edition = "2018"
5+
edition = "2021"
66
license = "Apache-2.0"
77
repository = "https://github.com/bandprotocol/owasm/tree/master/packages/vm"
88
description = "Oracle script VM runtime"
@@ -15,7 +15,7 @@ parity-wasm = "0.41"
1515
pwasm-utils = "0.12"
1616
wasmer = { version = "2.3.0", default-features = false, features = ["singlepass", "compiler", "universal"] }
1717
wasmer-middlewares = "2.3.0"
18-
owasm-crypto = { path = "../crypto", version = "0.1.13" }
18+
# owasm-crypto = { path = "../crypto", version = "0.1.13" }
1919
tempfile = "3.1.0"
2020
clru = "0.2.0"
2121
cosmwasm-vm = "0.13.2"

packages/vm/src/lib.rs

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use wasmer::Singlepass;
2020
use wasmer::{imports, wasmparser, wasmparser::Operator, CompilerConfig, Function, Store};
2121
use wasmer_middlewares::Metering;
2222

23-
use owasm_crypto::ecvrf;
23+
// use owasm_crypto::ecvrf;
2424

2525
// inspired by https://github.com/CosmWasm/cosmwasm/issues/81
2626
// 512 pages = 32mb
@@ -156,11 +156,11 @@ fn require_mem_range(max_range: usize, require_range: usize) -> Result<(), Error
156156
Ok(())
157157
}
158158

159-
fn get_from_mem<E: vm::Env>(env: &Environment<E>, ptr: i64, len: i64) -> Result<Vec<u8>, Error> {
160-
let memory = env.memory()?;
161-
require_mem_range(memory.size().bytes().0, (ptr + len) as usize)?;
162-
Ok(memory.view()[ptr as usize..(ptr + len) as usize].iter().map(|cell| cell.get()).collect())
163-
}
159+
// fn get_from_mem<E: vm::Env>(env: &Environment<E>, ptr: i64, len: i64) -> Result<Vec<u8>, Error> {
160+
// let memory = env.memory()?;
161+
// require_mem_range(memory.size().bytes().0, (ptr + len) as usize)?;
162+
// Ok(memory.view()[ptr as usize..(ptr + len) as usize].iter().map(|cell| cell.get()).collect())
163+
// }
164164

165165
fn cost(_operator: &Operator) -> u64 {
166166
// A flat fee for each operation
@@ -170,18 +170,17 @@ fn cost(_operator: &Operator) -> u64 {
170170
pub fn run<E>(
171171
cache: &mut Cache,
172172
code: &[u8],
173-
gas: u32,
173+
gas: u64,
174174
is_prepare: bool,
175175
env: E,
176-
) -> Result<u32, Error>
176+
) -> Result<u64, Error>
177177
where
178178
E: vm::Env + 'static,
179179
{
180180
let owasm_env = Environment::new(env, gas);
181181

182182
let mut compiler = Singlepass::new();
183-
184-
let metering = Arc::new(Metering::new(4294967290, cost));
183+
let metering = Arc::new(Metering::new(0, cost));
185184
compiler.push_middleware(metering);
186185
let engine = Universal::new(compiler).engine();
187186
let store = Store::new(&engine);
@@ -270,8 +269,8 @@ where
270269
}
271270

272271
// consume gas equal size of span when write calldata for raw request
273-
vm.consume_gas(span_size as u32)?;
274-
env.decrease_gas_left(span_size as u32)?;
272+
// vm.consume_gas(span_size as u32)?;
273+
// env.decrease_gas_left(span_size as u32)?;
275274

276275
let memory = env.memory()?;
277276
require_mem_range(memory.size().bytes().0, (ptr + span_size) as usize)?;
@@ -304,24 +303,32 @@ where
304303
Ok(data.len() as i64)
305304
})
306305
}),
307-
"ecvrf_verify" => Function::new_native_with_env(&store, owasm_env.clone(), |env: &Environment<E>, y_ptr: i64, y_len: i64, pi_ptr: i64, pi_len: i64, alpha_ptr: i64, alpha_len: i64| {
308-
env.with_mut_vm(|vm| -> Result<u32, Error>{
309-
// consume gas relatively to the function running time (~12ms)
310-
vm.consume_gas(500000)?;
311-
env.decrease_gas_left(500000)?;
312-
313-
let y: Vec<u8> = get_from_mem(env, y_ptr, y_len)?;
314-
let pi: Vec<u8>= get_from_mem(env, pi_ptr, pi_len)?;
315-
let alpha: Vec<u8> = get_from_mem(env, alpha_ptr, alpha_len)?;
316-
Ok(ecvrf::ecvrf_verify(&y, &pi, &alpha) as u32)
317-
})
318-
}),
306+
// "ecvrf_verify" => Function::new_native_with_env(&store, owasm_env.clone(), |env: &Environment<E>, y_ptr: i64, y_len: i64, pi_ptr: i64, pi_len: i64, alpha_ptr: i64, alpha_len: i64| {
307+
// env.with_mut_vm(|vm| -> Result<u32, Error>{
308+
// // consume gas relatively to the function running time (~12ms)
309+
// vm.consume_gas(500000)?;
310+
// env.decrease_gas_left(500000)?;
311+
312+
// let y: Vec<u8> = get_from_mem(env, y_ptr, y_len)?;
313+
// let pi: Vec<u8>= get_from_mem(env, pi_ptr, pi_len)?;
314+
// let alpha: Vec<u8> = get_from_mem(env, alpha_ptr, alpha_len)?;
315+
// Ok(ecvrf::ecvrf_verify(&y, &pi, &alpha) as u32)
316+
// })
317+
// }),
319318
},
320319
};
321320

322321
let instance = cache.get_instance(code, &store, &import_object)?;
323322
let instance_ptr = NonNull::from(&instance);
324323
owasm_env.set_wasmer_instance(Some(instance_ptr));
324+
owasm_env.set_gas_left(gas);
325+
326+
match get_remaining_points(&instance) {
327+
MeteringPoints::Remaining(count) => {
328+
println!("gas {:?}, count {:?}, gas_used {:?}", gas, count, gas.saturating_sub(count));
329+
}
330+
MeteringPoints::Exhausted => {}
331+
}
325332

326333
// get function and exec
327334
let entry = if is_prepare { "prepare" } else { "execute" };
@@ -344,10 +351,12 @@ where
344351
})?;
345352

346353
match get_remaining_points(&instance) {
347-
MeteringPoints::Remaining(count) => return Ok(gas - (count as u32)),
348-
MeteringPoints::Exhausted => return Ok(gas),
349-
};
350-
// Ok(owasm_env.with_vm(|vm| vm.gas_used))
354+
MeteringPoints::Remaining(count) => {
355+
println!("gas {:?}, count {:?}, gas_used {:?}", gas, count, gas.saturating_sub(count));
356+
Ok(gas.saturating_sub(count))
357+
}
358+
MeteringPoints::Exhausted => Err(Error::OutOfGasError),
359+
}
351360
}
352361

353362
#[cfg(test)]
@@ -536,7 +545,7 @@ mod test {
536545
let mut cache = Cache::new(CacheOptions { cache_size: 10000 });
537546
let env = MockEnv {};
538547
let gas_used = run(&mut cache, &code, 4294967290, true, env).unwrap();
539-
assert_eq!(gas_used, 1000015 as u32);
548+
assert_eq!(gas_used, 1000015 as u64);
540549
}
541550

542551
// #[test]

packages/vm/src/vm.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,22 @@ where
3838
E: Env,
3939
{
4040
pub env: E, // The execution environment.
41-
pub gas_limit: u32, // Amount of gas allowed for total execution.
42-
pub gas_used: u32, // Amount of gas used in this execution.
41+
pub gas_limit: u64, // Amount of gas allowed for total execution.
42+
pub gas_used: u64, // Amount of gas used in this execution.
4343
}
4444

4545
impl<E> VMLogic<E>
4646
where
4747
E: Env,
4848
{
4949
/// Creates a new `VMLogic` instance.
50-
pub fn new(env: E, gas: u32) -> Self {
50+
pub fn new(env: E, gas: u64) -> Self {
5151
Self { env: env, gas_limit: gas, gas_used: 0 }
5252
}
5353

5454
/// Consumes the given amount of gas. Return `OutOfGasError` error if run out of gas.
5555
pub fn consume_gas(&mut self, gas: u32) -> Result<(), Error> {
56-
self.gas_used = self.gas_used.saturating_add(gas);
56+
self.gas_used = self.gas_used.saturating_add(gas as u64);
5757
if self.out_of_gas() {
5858
Err(Error::OutOfGasError)
5959
} else {
@@ -98,7 +98,7 @@ impl<E> Environment<E>
9898
where
9999
E: Env + 'static,
100100
{
101-
pub fn new(e: E, gas: u32) -> Self {
101+
pub fn new(e: E, gas: u64) -> Self {
102102
Self {
103103
vm: Arc::new(Mutex::new(VMLogic::<E>::new(e, gas))),
104104
data: Arc::new(RwLock::new(ContextData::new())),

0 commit comments

Comments
 (0)