Skip to content

Commit ef9a11e

Browse files
committed
Support useTicks host function
1 parent 417baff commit ef9a11e

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/main.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use wasmi::memory_units::Pages;
99
use wasmi::{
1010
Error as InterpreterError, Externals, FuncInstance, FuncRef, ImportsBuilder, MemoryInstance,
1111
MemoryRef, Module, ModuleImportResolver, ModuleInstance, NopExternals, RuntimeArgs,
12-
RuntimeValue, Signature, Trap, ValueType,
12+
RuntimeValue, Signature, Trap, TrapKind, ValueType,
1313
};
1414

1515
mod types;
@@ -20,8 +20,10 @@ const BLOCKDATASIZE_FUNC_INDEX: usize = 1;
2020
const BLOCKDATACOPY_FUNC_INDEX: usize = 2;
2121
const SAVEPOSTSTATEROOT_FUNC_INDEX: usize = 3;
2222
const PUSHNEWDEPOSIT_FUNC_INDEX: usize = 4;
23+
const USETICKS_FUNC_INDEX: usize = 5;
2324

2425
struct Runtime<'a> {
26+
ticks_left: u32,
2527
memory: Option<MemoryRef>,
2628
pre_state: &'a Bytes32,
2729
block_data: &'a ShardBlockBody,
@@ -35,6 +37,7 @@ impl<'a> Runtime<'a> {
3537
memory: Option<MemoryRef>,
3638
) -> Runtime<'a> {
3739
Runtime {
40+
ticks_left: 10_000_000, // FIXME: make this configurable
3841
memory: if memory.is_some() {
3942
memory
4043
} else {
@@ -59,6 +62,15 @@ impl<'a> Externals for Runtime<'a> {
5962
args: RuntimeArgs,
6063
) -> Result<Option<RuntimeValue>, Trap> {
6164
match index {
65+
USETICKS_FUNC_INDEX => {
66+
let ticks: u32 = args.nth(0);
67+
if self.ticks_left < ticks {
68+
// FIXME: use TrapKind::Host
69+
return Err(Trap::new(TrapKind::Unreachable));
70+
}
71+
self.ticks_left -= ticks;
72+
Ok(None)
73+
}
6274
LOADPRESTATEROOT_FUNC_INDEX => {
6375
let ptr: u32 = args.nth(0);
6476
println!("loadprestateroot to {}", ptr);
@@ -124,6 +136,10 @@ impl<'a> ModuleImportResolver for RuntimeModuleImportResolver {
124136
_signature: &Signature,
125137
) -> Result<FuncRef, InterpreterError> {
126138
let func_ref = match field_name {
139+
"eth2_useTicks" => FuncInstance::alloc_host(
140+
Signature::new(&[ValueType::I32][..], None),
141+
USETICKS_FUNC_INDEX,
142+
),
127143
"eth2_loadPreStateRoot" => FuncInstance::alloc_host(
128144
Signature::new(&[ValueType::I32][..], None),
129145
LOADPRESTATEROOT_FUNC_INDEX,

0 commit comments

Comments
 (0)