Skip to content

Commit 287d37d

Browse files
committed
Move Wasm execution as part of Runtime and simplify execute_code helper
1 parent 77aae90 commit 287d37d

File tree

1 file changed

+31
-37
lines changed

1 file changed

+31
-37
lines changed

src/main.rs

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ const BIGNUM_SUB256_FUNC: usize = 11;
8484
type DepositBlob = Vec<u8>;
8585

8686
struct Runtime<'a> {
87+
code: &'a [u8],
8788
ticks_left: u32,
8889
memory: Option<MemoryRef>,
8990
pre_state: &'a Bytes32,
@@ -93,28 +94,45 @@ struct Runtime<'a> {
9394
}
9495

9596
impl<'a> Runtime<'a> {
96-
fn new(
97-
pre_state: &'a Bytes32,
98-
block_data: &'a ShardBlockBody,
99-
memory: MemoryRef,
100-
) -> Runtime<'a> {
97+
fn new(code: &'a [u8], pre_state: &'a Bytes32, block_data: &'a ShardBlockBody) -> Runtime<'a> {
10198
Runtime {
99+
code: code,
102100
ticks_left: 10_000_000, // FIXME: make this configurable
103-
memory: Some(memory),
101+
memory: None,
104102
pre_state: pre_state,
105103
block_data: block_data,
106104
post_state: Bytes32::default(),
107105
deposits: vec![],
108106
}
109107
}
110108

111-
fn get_post_state(&self) -> Bytes32 {
112-
self.post_state
113-
}
109+
fn execute(&mut self) -> Result<(Bytes32, Vec<DepositBlob>), ScoutError> {
110+
let module = Module::from_buffer(&self.code)?;
111+
let mut imports = ImportsBuilder::new();
112+
// TODO: remove this and rely on Eth2ImportResolver and DebugImportResolver
113+
imports.push_resolver("env", &RuntimeModuleImportResolver);
114+
imports.push_resolver("eth2", &Eth2ImportResolver);
115+
imports.push_resolver("bignum", &BignumImportResolver);
116+
imports.push_resolver("debug", &DebugImportResolver);
117+
118+
let instance = ModuleInstance::new(&module, &imports)?.run_start(&mut NopExternals)?;
119+
120+
// FIXME: pass through errors here and not use .expect()
121+
let internal_mem = instance
122+
.export_by_name("memory")
123+
.expect("Module expected to have 'memory' export")
124+
.as_memory()
125+
.cloned()
126+
.expect("'memory' export should be a memory");
127+
128+
self.memory = Some(internal_mem);
129+
130+
let result = instance.invoke_export("main", &[], self)?;
131+
132+
info!("Result: {:?}", result);
114133

115-
fn get_deposits(&self) -> Vec<DepositBlob> {
116134
// TODO: avoid cloning here
117-
self.deposits.clone()
135+
Ok((self.post_state, self.deposits.clone()))
118136
}
119137
}
120138

@@ -614,32 +632,8 @@ pub fn execute_code(
614632
block_data
615633
);
616634

617-
let module = Module::from_buffer(&code)?;
618-
let mut imports = ImportsBuilder::new();
619-
// TODO: remove this and rely on Eth2ImportResolver and DebugImportResolver
620-
imports.push_resolver("env", &RuntimeModuleImportResolver);
621-
imports.push_resolver("eth2", &Eth2ImportResolver);
622-
imports.push_resolver("bignum", &BignumImportResolver);
623-
imports.push_resolver("debug", &DebugImportResolver);
624-
625-
let instance = ModuleInstance::new(&module, &imports)?.run_start(&mut NopExternals)?;
626-
627-
// FIXME: pass through errors here and not use .expect()
628-
let internal_mem = instance
629-
.export_by_name("memory")
630-
.expect("Module expected to have 'memory' export")
631-
.as_memory()
632-
.cloned()
633-
.expect("'memory' export should be a memory");
634-
635-
let mut runtime = Runtime::new(pre_state, block_data, internal_mem);
636-
637-
let result = instance.invoke_export("main", &[], &mut runtime)?;
638-
639-
info!("Result: {:?}", result);
640-
info!("Execution finished");
641-
642-
Ok((runtime.get_post_state(), runtime.get_deposits()))
635+
let mut runtime = Runtime::new(&code, pre_state, block_data);
636+
runtime.execute()
643637
}
644638

645639
pub fn process_shard_block(

0 commit comments

Comments
 (0)