Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'stylus/stylus' into onchain-compiled
Browse files Browse the repository at this point in the history
  • Loading branch information
rachel-bousfield committed Oct 3, 2023
2 parents c275d99 + 15391f9 commit 84435df
Show file tree
Hide file tree
Showing 32 changed files with 108 additions and 229 deletions.
12 changes: 4 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,16 @@ stylus_test_dir = arbitrator/stylus/tests
stylus_cargo = arbitrator/stylus/tests/.cargo/config.toml

rust_sdk = arbitrator/langs/rust
c_sdk = arbitrator/langs/c
stylus_lang_rust = $(wildcard $(rust_sdk)/*/src/*.rs $(rust_sdk)/*/src/*/*.rs $(rust_sdk)/*/*.toml)
stylus_lang_c = $(wildcard arbitrator/langs/c/*.c arbitrator/langs/c/*.h)
stylus_lang_c = $(wildcard $(c_sdk)/*/*.c $(c_sdk)/*/*.h)
stylus_lang_bf = $(wildcard arbitrator/langs/bf/src/*.* arbitrator/langs/bf/src/*.toml)

cargo_nightly = cargo +nightly build -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort

get_stylus_test_wasm = $(stylus_test_dir)/$(1)/$(wasm32_unknown)/$(1).wasm
get_stylus_test_rust = $(wildcard $(stylus_test_dir)/$(1)/*.toml $(stylus_test_dir)/$(1)/src/*.rs) $(stylus_cargo) $(stylus_lang_rust)
get_stylus_test_c = $(wildcard $(stylus_test_dir)/$(1)/*.c $(stylus_test_dir)/$(1)/*.h) $(stylus_lang_c)
get_stylus_test_c = $(wildcard $(c_sdk)/examples/$(1)/*.c $(c_sdk)/examples/$(1)/*.h) $(stylus_lang_c)
stylus_test_bfs = $(wildcard $(stylus_test_dir)/bf/*.b)

stylus_test_keccak_wasm = $(call get_stylus_test_wasm,keccak)
Expand All @@ -127,10 +128,8 @@ stylus_test_erc20_wasm = $(call get_stylus_test_wasm,erc20)
stylus_test_erc20_src = $(call get_stylus_test_rust,erc20)
stylus_test_read-return-data_wasm = $(call get_stylus_test_wasm,read-return-data)
stylus_test_read-return-data_src = $(call get_stylus_test_rust,read-return-data)
stylus_test_siphash_wasm = $(stylus_test_dir)/siphash/siphash.wasm
stylus_test_siphash_src = $(call get_stylus_test_c,siphash)

stylus_test_wasms = $(stylus_test_keccak_wasm) $(stylus_test_keccak-100_wasm) $(stylus_test_fallible_wasm) $(stylus_test_storage_wasm) $(stylus_test_siphash_wasm) $(stylus_test_multicall_wasm) $(stylus_test_log_wasm) $(stylus_test_create_wasm) $(stylus_test_sdk-storage_wasm) $(stylus_test_erc20_wasm) $(stylus_test_read-return-data_wasm) $(stylus_test_evm-data_wasm) $(stylus_test_bfs:.b=.wasm)
stylus_test_wasms = $(stylus_test_keccak_wasm) $(stylus_test_keccak-100_wasm) $(stylus_test_fallible_wasm) $(stylus_test_storage_wasm) $(stylus_test_multicall_wasm) $(stylus_test_log_wasm) $(stylus_test_create_wasm) $(stylus_test_sdk-storage_wasm) $(stylus_test_erc20_wasm) $(stylus_test_read-return-data_wasm) $(stylus_test_evm-data_wasm) $(stylus_test_bfs:.b=.wasm)
stylus_benchmarks = $(wildcard $(stylus_dir)/*.toml $(stylus_dir)/src/*.rs) $(stylus_test_wasms)
stylus_files = $(wildcard $(stylus_dir)/*.toml $(stylus_dir)/src/*.rs) $(rust_prover_files)

Expand Down Expand Up @@ -410,9 +409,6 @@ $(stylus_test_erc20_wasm): $(stylus_test_erc20_src)
$(cargo_nightly) --manifest-path $< --release --config $(stylus_cargo)
@touch -c $@ # cargo might decide to not rebuild the binary

$(stylus_test_siphash_wasm): $(stylus_test_siphash_src)
clang $(filter %.c, $^) -o $@ --target=wasm32 --no-standard-libraries -Wl,--no-entry -Oz

contracts/test/prover/proofs/float%.json: $(arbitrator_cases)/float%.wasm $(prover_bin) $(output_latest)/soft-float.wasm
$(prover_bin) $< -l $(output_latest)/soft-float.wasm -o $@ -b --allow-hostapi --require-success --always-merkleize

Expand Down
2 changes: 1 addition & 1 deletion arbitrator/arbutil/src/evm/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,5 @@ pub trait EvmApi: Send + 'static {
fn add_pages(&mut self, pages: u16) -> u64;

/// Captures tracing information for hostio invocations during native execution.
fn capture_hostio(&self, name: &str, args: &[u8], outs: &[u8], ink: u64);
fn capture_hostio(&self, name: &str, args: &[u8], outs: &[u8], start_ink: u64, end_ink: u64);
}
4 changes: 2 additions & 2 deletions arbitrator/arbutil/src/evm/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,11 @@ impl<T: JsCallIntoGo> EvmApi for JsEvmApi<T> {
cost.assert_u64()
}

fn capture_hostio(&self, name: &str, args: &[u8], outs: &[u8], ink: u64) {
fn capture_hostio(&self, name: &str, args: &[u8], outs: &[u8], start_ink: u64, _end_ink: u64) {
let args = hex::encode(args);
let outs = hex::encode(outs);
println!(
"Error: unexpected hostio tracing info for {name} while proving: {args}, {outs}, {ink}"
"Error: unexpected hostio tracing info for {name} while proving: {args}, {outs}, {start_ink}"
);
}
}
2 changes: 1 addition & 1 deletion arbitrator/langs/bf
21 changes: 16 additions & 5 deletions arbitrator/stylus/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,24 @@ impl<E: EvmApi> WasmEnv<E> {
env: &'a mut WasmEnvMut<'_, E>,
ink: u64,
) -> Result<HostioInfo<'a, E>, Escape> {
let mut info = Self::start_free(env);
let mut info = Self::start_free(env)?;
info.buy_ink(pricing::HOSTIO_INK + ink)?;
Ok(info)
}

pub fn start_free<'a>(env: &'a mut WasmEnvMut<'_, E>) -> HostioInfo<'a, E> {
pub fn start_free<'a>(env: &'a mut WasmEnvMut<'_, E>) -> Result<HostioInfo<'a, E>, Escape> {
let (env, store) = env.data_and_store_mut();
let memory = env.memory.clone().unwrap();
HostioInfo { env, memory, store }
let mut info = HostioInfo {
env,
memory,
store,
start_ink: 0,
};
if info.env.evm_data.tracing {
info.start_ink = info.ink_ready()?;
}
Ok(info)
}

pub fn meter(&mut self) -> &mut MeterData {
Expand Down Expand Up @@ -125,6 +134,7 @@ pub struct HostioInfo<'a, E: EvmApi> {
pub env: &'a mut WasmEnv<E>,
pub memory: Memory,
pub store: StoreMut<'a>,
pub start_ink: u64,
}

impl<'a, E: EvmApi> HostioInfo<'a, E> {
Expand Down Expand Up @@ -199,8 +209,9 @@ impl<'a, E: EvmApi> HostioInfo<'a, E> {
Ok(())
}

pub fn trace(&self, name: &str, args: &[u8], outs: &[u8], ink: u64) {
self.evm_api.capture_hostio(name, args, outs, ink);
pub fn trace(&self, name: &str, args: &[u8], outs: &[u8], start_ink: u64, end_ink: u64) {
self.evm_api
.capture_hostio(name, args, outs, start_ink, end_ink);
}
}

Expand Down
8 changes: 5 additions & 3 deletions arbitrator/stylus/src/evm_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ pub struct GoEvmApi {
name: *mut RustVec,
args: *mut RustSlice,
outs: *mut RustSlice,
ink: u64,
start_ink: u64,
end_ink: u64,
),
pub id: usize,
}
Expand Down Expand Up @@ -258,14 +259,15 @@ impl EvmApi for GoEvmApi {
call!(self, add_pages, pages)
}

fn capture_hostio(&self, name: &str, args: &[u8], outs: &[u8], ink: u64) {
fn capture_hostio(&self, name: &str, args: &[u8], outs: &[u8], start_ink: u64, end_ink: u64) {
call!(
self,
capture_hostio,
ptr!(RustVec::new(name.as_bytes().to_vec())),
ptr!(RustSlice::new(args)),
ptr!(RustSlice::new(outs)),
ink
start_ink,
end_ink
)
}
}
60 changes: 32 additions & 28 deletions arbitrator/stylus/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ macro_rules! be {
macro_rules! trace {
($name:expr, $env:expr, [$($args:expr),+], [$($outs:expr),+], $ret:expr) => {{
if $env.evm_data.tracing {
let ink = $env.ink_ready()?;
let start_ink = $env.start_ink;
let end_ink = $env.ink_ready()?;
let mut args = vec![];
$(args.extend($args);)*
let mut outs = vec![];
$(outs.extend($outs);)*
$env.trace($name, &args, &outs, ink);
$env.trace($name, &args, &outs, start_ink, end_ink);
}
Ok($ret)
}};
Expand All @@ -49,14 +50,14 @@ pub(crate) fn read_args<E: EvmApi>(mut env: WasmEnvMut<E>, ptr: u32) -> MaybeEsc
let mut env = WasmEnv::start(&mut env, 0)?;
env.pay_for_write(env.args.len() as u64)?;
env.write_slice(ptr, &env.args)?;
trace!("read_args", env, &env.args, &[])
trace!("read_args", env, &[], &env.args)
}

pub(crate) fn write_result<E: EvmApi>(mut env: WasmEnvMut<E>, ptr: u32, len: u32) -> MaybeEscape {
let mut env = WasmEnv::start(&mut env, 0)?;
env.pay_for_read(len.into())?;
env.outs = env.read_slice(ptr, len)?;
trace!("write_result", env, &[], &env.outs)
trace!("write_result", env, &env.outs, &[])
}

pub(crate) fn storage_load_bytes32<E: EvmApi>(
Expand Down Expand Up @@ -150,6 +151,8 @@ where
{
let mut env = WasmEnv::start(&mut env, 3 * PTR_INK + EVM_API_INK)?;
env.pay_for_read(calldata_len.into())?;

let gas_passed = gas;
gas = gas.min(env.gas_left()?); // provide no more than what the user has

let contract = env.read_bytes20(contract)?;
Expand All @@ -166,10 +169,11 @@ where
if env.evm_data.tracing {
let underscore = (!name.is_empty()).then_some("_").unwrap_or_default();
let name = format!("{name}{underscore}call_contract");
let value = value.into_iter().flatten();
return trace!(
&name,
env,
[contract, &input, be!(gas), value.unwrap_or_default()],
[contract, be!(gas_passed), value, &input],
[be!(outs_len), be!(status)],
status
);
Expand Down Expand Up @@ -261,12 +265,12 @@ where
env.write_u32(revert_data_len, ret_len)?;
env.write_bytes20(contract, result)?;

let salt = salt.into_iter().flat_map(|x| x);
let salt = salt.into_iter().flatten();
trace!(
name,
env,
[code_copy.unwrap(), endowment, salt, be!(gas)],
[result, be!(ret_len), be!(gas_cost)],
[endowment, salt, code_copy.unwrap()],
[result, be!(ret_len)],
()
)
}
Expand All @@ -285,7 +289,7 @@ pub(crate) fn read_return_data<E: EvmApi>(
env.write_slice(dest, &data)?;

let len = data.len() as u32;
trace!("read_return_data", env, [be!(dest), be!(offset)], data, len)
trace!("read_return_data", env, [be!(offset), be!(size)], data, len)
}

pub(crate) fn return_data_size<E: EvmApi>(mut env: WasmEnvMut<E>) -> Result<u32, Escape> {
Expand All @@ -309,7 +313,7 @@ pub(crate) fn emit_log<E: EvmApi>(

let data = env.read_slice(data, len)?;
env.evm_api.emit_log(data.clone(), topics)?;
trace!("emit_log", env, [data, be!(topics)], &[])
trace!("emit_log", env, [be!(topics), data], &[])
}

pub(crate) fn account_balance<E: EvmApi>(
Expand All @@ -323,7 +327,7 @@ pub(crate) fn account_balance<E: EvmApi>(
let (balance, gas_cost) = env.evm_api.account_balance(address);
env.buy_gas(gas_cost)?;
env.write_bytes32(ptr, balance)?;
trace!("account_balance", env, &[], balance)
trace!("account_balance", env, address, balance)
}

pub(crate) fn account_codehash<E: EvmApi>(
Expand All @@ -337,19 +341,7 @@ pub(crate) fn account_codehash<E: EvmApi>(
let (hash, gas_cost) = env.evm_api.account_codehash(address);
env.buy_gas(gas_cost)?;
env.write_bytes32(ptr, hash)?;
trace!("account_codehash", env, &[], hash)
}

pub(crate) fn evm_gas_left<E: EvmApi>(mut env: WasmEnvMut<E>) -> Result<u64, Escape> {
let mut env = WasmEnv::start(&mut env, 0)?;
let gas = env.gas_left()?;
trace!("evm_gas_left", env, be!(gas), &[], gas)
}

pub(crate) fn evm_ink_left<E: EvmApi>(mut env: WasmEnvMut<E>) -> Result<u64, Escape> {
let mut env = WasmEnv::start(&mut env, 0)?;
let ink = env.ink_ready()?;
trace!("evm_ink_left", env, be!(ink), &[], ink)
trace!("account_codehash", env, address, hash)
}

pub(crate) fn block_basefee<E: EvmApi>(mut env: WasmEnvMut<E>, ptr: u32) -> MaybeEscape {
Expand Down Expand Up @@ -394,6 +386,18 @@ pub(crate) fn contract_address<E: EvmApi>(mut env: WasmEnvMut<E>, ptr: u32) -> M
trace!("contract_address", env, &[], env.evm_data.contract_address)
}

pub(crate) fn evm_gas_left<E: EvmApi>(mut env: WasmEnvMut<E>) -> Result<u64, Escape> {
let mut env = WasmEnv::start(&mut env, 0)?;
let gas = env.gas_left()?;
trace!("evm_gas_left", env, be!(gas), &[], gas)
}

pub(crate) fn evm_ink_left<E: EvmApi>(mut env: WasmEnvMut<E>) -> Result<u64, Escape> {
let mut env = WasmEnv::start(&mut env, 0)?;
let ink = env.ink_ready()?;
trace!("evm_ink_left", env, be!(ink), &[], ink)
}

pub(crate) fn msg_reentrant<E: EvmApi>(mut env: WasmEnvMut<E>) -> Result<u32, Escape> {
let mut env = WasmEnv::start(&mut env, 0)?;
let reentrant = env.evm_data.reentrant;
Expand Down Expand Up @@ -446,7 +450,7 @@ pub(crate) fn tx_origin<E: EvmApi>(mut env: WasmEnvMut<E>, ptr: u32) -> MaybeEsc
}

pub(crate) fn memory_grow<E: EvmApi>(mut env: WasmEnvMut<E>, pages: u16) -> MaybeEscape {
let mut env = WasmEnv::start_free(&mut env);
let mut env = WasmEnv::start_free(&mut env)?;
if pages == 0 {
env.buy_ink(HOSTIO_INK)?;
return Ok(());
Expand All @@ -461,7 +465,7 @@ pub(crate) fn console_log_text<E: EvmApi>(
ptr: u32,
len: u32,
) -> MaybeEscape {
let mut env = WasmEnv::start_free(&mut env);
let mut env = WasmEnv::start_free(&mut env)?;
let text = env.read_slice(ptr, len)?;
env.say(String::from_utf8_lossy(&text));
trace!("console_log_text", env, text, &[])
Expand All @@ -471,7 +475,7 @@ pub(crate) fn console_log<E: EvmApi, T: Into<Value>>(
mut env: WasmEnvMut<E>,
value: T,
) -> MaybeEscape {
let mut env = WasmEnv::start_free(&mut env);
let mut env = WasmEnv::start_free(&mut env)?;
let value = value.into();
env.say(value);
trace!("console_log", env, [format!("{value}").as_bytes()], &[])
Expand All @@ -481,7 +485,7 @@ pub(crate) fn console_tee<E: EvmApi, T: Into<Value> + Copy>(
mut env: WasmEnvMut<E>,
value: T,
) -> Result<T, Escape> {
let env = WasmEnv::start_free(&mut env);
let env = WasmEnv::start_free(&mut env)?;
env.say(value.into());
Ok(value)
}
Expand Down
5 changes: 3 additions & 2 deletions arbitrator/stylus/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ impl<E: EvmApi> RunProgram for NativeInstance<E> {
env.config = Some(config);

if env.evm_data.tracing {
let args_len = args.len() as u32;
env.evm_api
.capture_hostio(STYLUS_ENTRY_POINT, &args.len().to_be_bytes(), &[], ink);
.capture_hostio(STYLUS_ENTRY_POINT, &args_len.to_be_bytes(), &[], ink, ink);
}

let exports = &self.instance.exports;
Expand Down Expand Up @@ -109,7 +110,7 @@ impl<E: EvmApi> RunProgram for NativeInstance<E> {
let env = self.env();
if env.evm_data.tracing {
env.evm_api
.capture_hostio("user_returned", &[], &status.to_be_bytes(), ink);
.capture_hostio("user_returned", &[], &status.to_be_bytes(), ink, ink);
}

let outs = self.env().outs.clone();
Expand Down
9 changes: 8 additions & 1 deletion arbitrator/stylus/src/test/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,14 @@ impl EvmApi for TestEvmApi {
model.gas_cost(new, open, ever)
}

fn capture_hostio(&self, _name: &str, _args: &[u8], _outs: &[u8], _ink: u64) {
fn capture_hostio(
&self,
_name: &str,
_args: &[u8],
_outs: &[u8],
_start_ink: u64,
_end_ink: u64,
) {
unimplemented!()
}
}
Loading

0 comments on commit 84435df

Please sign in to comment.