-
Notifications
You must be signed in to change notification settings - Fork 27
Implement transient get/set bytes32 hostios #185
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## stylus #185 +/- ##
==========================================
- Coverage 58.13% 57.87% -0.27%
==========================================
Files 282 282
Lines 43331 43443 +112
==========================================
- Hits 25190 25141 -49
- Misses 15694 15832 +138
- Partials 2447 2470 +23 |
arbitrator/stylus/src/host.rs
Outdated
dest: u32, | ||
) -> MaybeEscape { | ||
let mut env = WasmEnv::start(&mut env, 2 * PTR_INK + EVM_API_INK)?; | ||
env.buy_gas(TRANSIENT_BYTES32_GAS)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to map errors here to be more informative. To know if it failed at buying gas, at reading, or at transient get
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the error messages bubbled up are already informative enough
let gas_cost = api.set_bytes32(key, value).unwrap(); | ||
program.buy_gas(gas_cost).unwrap(); | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn user_host__storage_transient_load_bytes32(key: usize, dest: usize) { | ||
let program = Program::start(2 * PTR_INK + EVM_API_INK); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does the magic number 2
come from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reading two ptr inputs (key, dest)
let value = wavm::read_bytes32(value); | ||
|
||
let api = &mut program.evm_api; | ||
api.transient_set_bytes32(key, value).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we move wavm::read_bytes32(key) and value into the api.transient_set_bytes32(key, value).unwrap();
to avoid declaring two variables?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code has been replaced by macro. In general, better to keep things explicit so that the amount of gas used is as clear as possible
768c397
to
745a078
Compare
d80817e
to
185d5bd
Compare
change names and update Makefile to cleanup generated files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! Some minor suggests as I continue reviewing
arbitrator/stylus/src/evm_api.rs
Outdated
let value = call!(self, transient_get_bytes32, key); | ||
value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's drop the let value =
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
arbitrator/arbutil/src/evm/api.rs
Outdated
@@ -57,6 +59,17 @@ pub trait EvmApi: Send + 'static { | |||
/// Analogous to `vm.SSTORE`. | |||
fn set_bytes32(&mut self, key: Bytes32, value: Bytes32) -> Result<u64>; | |||
|
|||
/// Reads the 32-byte value in the EVM state trie at offset `key`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
Reads the 32-byte value from transient storage at offset
key
.
And drop the next line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
arbitrator/arbutil/src/evm/mod.rs
Outdated
// params.WarmStorageReadCostEIP2929 | ||
pub const TRANSIENT_BYTES32_GAS: u64 = 100; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's call it TRANSIENT_OP_GAS
or similar
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
#[no_mangle] | ||
pub unsafe extern "C" fn vm_hooks__transient_storage_load_bytes32(key: u32, dest: u32) { | ||
let mut program = Program::start(2 * PTR_INK + EVM_API_INK); | ||
let key = wavm::read_bytes32(key); | ||
|
||
let value = TRANSIENT_KEYS.lock().get(&key).cloned().unwrap_or_default(); | ||
program.buy_gas(evm::TRANSIENT_BYTES32_GAS).unwrap(); | ||
wavm::write_bytes32(dest, value); | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn vm_hooks__transient_storage_store_bytes32(key: u32, value: u32) { | ||
let mut program = Program::start(2 * PTR_INK + EVM_API_INK); | ||
program.buy_gas(evm::TRANSIENT_BYTES32_GAS).unwrap(); // pretend the worst case | ||
|
||
let key = wavm::read_bytes32(key); | ||
let value = wavm::read_bytes32(value); | ||
TRANSIENT_KEYS.lock().insert(key, value); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's drop these since they're unused
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
arbitrator/stylus/src/test/api.rs
Outdated
let api = TestEvmApi { | ||
contracts: Arc::new(Mutex::new(HashMap::new())), | ||
storage: Arc::new(Mutex::new(storage)), | ||
transient_storage: Arc::new(Mutex::new(transient_storage)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's drop this and related funcs until we have a test framework with a notion of transactions (all tests are currently just isolated calls, so there's no meaningful distinction between normal and transient storage)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
implemented in different PR |
Depends on:
OffchainLabs/stylus-sdk-c#15
OffchainLabs/stylus-sdk-rs#92