Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 62 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,25 @@ const DEBUG_PRINTMEMHEX_FUNC: usize = 9;
const BIGNUM_ADD256_FUNC: usize = 10;
const BIGNUM_SUB256_FUNC: usize = 11;

fn load_import(code: &[u8]) -> Result<wasmi::ModuleRef, ScoutError> {
let module = Module::from_buffer(&code)?;
let imports = ImportsBuilder::new();
let instance = ModuleInstance::new(&module, &imports)?.run_start(&mut NopExternals)?;
Ok(instance)
}

#[derive(Default, PartialEq, Clone, Debug)]
pub struct Library {
name: String,
code: Vec<u8>,
}

// TODO: move elsehwere?
type DepositBlob = Vec<u8>;

struct Runtime<'a> {
code: &'a [u8],
libraries: &'a Vec<Library>,
ticks_left: u32,
memory: Option<MemoryRef>,
pre_state: &'a Bytes32,
Expand All @@ -94,9 +108,15 @@ struct Runtime<'a> {
}

impl<'a> Runtime<'a> {
fn new(code: &'a [u8], pre_state: &'a Bytes32, block_data: &'a ShardBlockBody) -> Runtime<'a> {
fn new(
code: &'a [u8],
libraries: &'a Vec<Library>,
pre_state: &'a Bytes32,
block_data: &'a ShardBlockBody,
) -> Runtime<'a> {
Runtime {
code: code,
libraries: libraries,
ticks_left: 10_000_000, // FIXME: make this configurable
memory: None,
pre_state: pre_state,
Expand All @@ -115,6 +135,21 @@ impl<'a> Runtime<'a> {
imports.push_resolver("bignum", &BignumImportResolver);
imports.push_resolver("debug", &DebugImportResolver);

// Load all libraries
// NOTE: creating this variable here to track lifetime
let libraries: Result<Vec<(String, wasmi::ModuleRef)>, ScoutError> = self
.libraries
.iter()
.map(|ref library| Ok((library.name.to_string(), load_import(&library.code)?)))
.collect();
let libraries = libraries?;

// Link them to the current instance
for library in &libraries {
debug!("Attaching library: {}", &library.0);
imports.push_resolver(&library.0, &library.1);
}

let instance = ModuleInstance::new(&module, &imports)?.run_start(&mut NopExternals)?;

// FIXME: pass through errors here and not use .expect()
Expand Down Expand Up @@ -561,6 +596,7 @@ pub struct ExecutionScript {
#[derive(Default, PartialEq, Clone, Debug)]
pub struct BeaconState {
execution_scripts: Vec<ExecutionScript>,
libraries: Vec<Library>,
}

/// Shards are Phase 1 structures.
Expand Down Expand Up @@ -623,6 +659,7 @@ impl fmt::Display for ShardState {

pub fn execute_code(
code: &[u8],
libraries: &Vec<Library>,
pre_state: &Bytes32,
block_data: &ShardBlockBody,
) -> Result<(Bytes32, Vec<DepositBlob>), ScoutError> {
Expand All @@ -632,7 +669,7 @@ pub fn execute_code(
block_data
);

let mut runtime = Runtime::new(&code, pre_state, block_data);
let mut runtime = Runtime::new(&code, &libraries, pre_state, block_data);
runtime.execute()
}

Expand All @@ -659,7 +696,8 @@ pub fn process_shard_block(
// state.exec_env_states.push(ZERO_HASH)
// }
let pre_state = &state.exec_env_states[env];
let (post_state, deposits) = execute_code(code, pre_state, &block.data)?;
let (post_state, deposits) =
execute_code(code, &beacon_state.libraries, pre_state, &block.data)?;
state.exec_env_states[env] = post_state;

// Decode deposits.
Expand All @@ -683,9 +721,16 @@ pub fn process_shard_block(
Ok(deposit_receipts)
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct TestLibrary {
name: String,
file: String,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct TestBeaconState {
execution_scripts: Vec<String>,
libraries: Option<Vec<TestLibrary>>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -773,8 +818,22 @@ impl TryFrom<TestBeaconState> for BeaconState {
})
})
.collect();
let libraries: Result<Vec<Library>, ScoutError> = if let Some(libraries) = input.libraries {
libraries
.iter()
.map(|library| {
Ok(Library {
name: library.name.to_string(),
code: std::fs::read(&library.file)?,
})
})
.collect()
} else {
Ok(Vec::new())
};
Ok(BeaconState {
execution_scripts: scripts?,
libraries: libraries?,
})
}
}
Expand Down