Skip to content

Commit

Permalink
Support useTicks host function
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Sep 10, 2019
1 parent 4884551 commit 0b9882e
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use wasmi::memory_units::Pages;
use wasmi::{
Error as InterpreterError, Externals, FuncInstance, FuncRef, ImportsBuilder, MemoryInstance,
MemoryRef, Module, ModuleImportResolver, ModuleInstance, NopExternals, RuntimeArgs,
RuntimeValue, Signature, Trap, ValueType,
RuntimeValue, Signature, Trap, TrapKind, ValueType,
};

mod types;
Expand All @@ -20,8 +20,10 @@ const BLOCKDATASIZE_FUNC_INDEX: usize = 1;
const BLOCKDATACOPY_FUNC_INDEX: usize = 2;
const SAVEPOSTSTATEROOT_FUNC_INDEX: usize = 3;
const PUSHNEWDEPOSIT_FUNC_INDEX: usize = 4;
const USETICKS_FUNC_INDEX: usize = 5;

struct Runtime<'a> {
ticks_left: u32,
memory: Option<MemoryRef>,
pre_state: &'a Bytes32,
block_data: &'a ShardBlockBody,
Expand All @@ -35,6 +37,7 @@ impl<'a> Runtime<'a> {
memory: Option<MemoryRef>,
) -> Runtime<'a> {
Runtime {
ticks_left: 10_000_000, // FIXME: make this configurable
memory: if memory.is_some() {
memory
} else {
Expand All @@ -59,6 +62,15 @@ impl<'a> Externals for Runtime<'a> {
args: RuntimeArgs,
) -> Result<Option<RuntimeValue>, Trap> {
match index {
USETICKS_FUNC_INDEX => {
let ticks: u32 = args.nth(0);
if self.ticks_left < ticks {
// FIXME: use TrapKind::Host
return Err(Trap::new(TrapKind::Unreachable));
}
self.ticks_left -= ticks;
Ok(None)
}
LOADPRESTATEROOT_FUNC_INDEX => {
let ptr: u32 = args.nth(0);
println!("loadprestateroot to {}", ptr);
Expand Down Expand Up @@ -124,6 +136,10 @@ impl<'a> ModuleImportResolver for RuntimeModuleImportResolver {
_signature: &Signature,
) -> Result<FuncRef, InterpreterError> {
let func_ref = match field_name {
"eth2_useTicks" => FuncInstance::alloc_host(
Signature::new(&[ValueType::I32][..], None),
USETICKS_FUNC_INDEX,
),
"eth2_loadPreStateRoot" => FuncInstance::alloc_host(
Signature::new(&[ValueType::I32][..], None),
LOADPRESTATEROOT_FUNC_INDEX,
Expand Down

0 comments on commit 0b9882e

Please sign in to comment.