-
Notifications
You must be signed in to change notification settings - Fork 27
Implement transient get/set bytes32 hostios #185
Changes from 1 commit
185d5bd
03a4698
b45f0c0
92c815c
00cc0f0
e7a6287
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,9 @@ pub mod user; | |
// params.SstoreSentryGasEIP2200 | ||
pub const SSTORE_SENTRY_GAS: u64 = 2300; | ||
|
||
// params.WarmStorageReadCostEIP2929 | ||
pub const TRANSIENT_BYTES32_GAS: u64 = 100; | ||
|
||
|
||
// params.LogGas and params.LogDataGas | ||
pub const LOG_TOPIC_GAS: u64 = 375; | ||
pub const LOG_DATA_GAS: u64 = 8; | ||
|
+1 −0 | .github/workflows/smoke-test.yml | |
+1 −1 | ci/smoke_test.sh | |
+4 −2 | examples/erc20/Cargo.lock | |
+1 −1 | examples/erc20/src/main.rs | |
+18 −0 | stylus-sdk/src/hostio.rs | |
+20 −0 | stylus-sdk/src/storage/mod.rs | |
+2 −2 | stylus-sdk/src/types.rs |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,13 @@ pub struct GoEvmApi { | |
gas_cost: *mut u64, | ||
error: *mut RustBytes, | ||
) -> EvmApiStatus, | ||
pub transient_get_bytes32: unsafe extern "C" fn(id: usize, key: Bytes32) -> Bytes32, // value | ||
pub transient_set_bytes32: unsafe extern "C" fn( | ||
id: usize, | ||
key: Bytes32, | ||
value: Bytes32, | ||
error: *mut RustBytes, | ||
) -> EvmApiStatus, | ||
pub contract_call: unsafe extern "C" fn( | ||
id: usize, | ||
contract: Bytes20, | ||
|
@@ -117,6 +124,21 @@ impl EvmApi for GoEvmApi { | |
} | ||
} | ||
|
||
fn transient_get_bytes32(&mut self, key: Bytes32) -> Bytes32 { | ||
let value = call!(self, transient_get_bytes32, key); | ||
value | ||
|
||
} | ||
|
||
fn transient_set_bytes32(&mut self, key: Bytes32, value: Bytes32) -> Result<()> { | ||
let mut error = RustBytes::new(vec![]); | ||
let api_status = call!(self, transient_set_bytes32, key, value, ptr!(error)); | ||
let error = into_vec!(error); // done here to always drop | ||
match api_status { | ||
EvmApiStatus::Success => Ok(()), | ||
EvmApiStatus::Failure => Err(error!(error)), | ||
} | ||
} | ||
|
||
fn contract_call( | ||
&mut self, | ||
contract: Bytes20, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ use super::TestInstance; | |
pub(crate) struct TestEvmApi { | ||
contracts: Arc<Mutex<HashMap<Bytes20, Vec<u8>>>>, | ||
storage: Arc<Mutex<HashMap<Bytes20, HashMap<Bytes32, Bytes32>>>>, | ||
transient_storage: Arc<Mutex<HashMap<Bytes20, HashMap<Bytes32, Bytes32>>>>, | ||
program: Bytes20, | ||
write_result: Arc<Mutex<Vec<u8>>>, | ||
compile: CompileConfig, | ||
|
@@ -33,9 +34,12 @@ impl TestEvmApi { | |
let mut storage = HashMap::new(); | ||
storage.insert(program, HashMap::new()); | ||
|
||
let mut transient_storage = HashMap::new(); | ||
|
||
let api = TestEvmApi { | ||
contracts: Arc::new(Mutex::new(HashMap::new())), | ||
storage: Arc::new(Mutex::new(storage)), | ||
transient_storage: Arc::new(Mutex::new(transient_storage)), | ||
|
||
program, | ||
write_result: Arc::new(Mutex::new(vec![])), | ||
compile, | ||
|
@@ -77,6 +81,20 @@ impl EvmApi for TestEvmApi { | |
Ok(22100) // pretend worst case | ||
} | ||
|
||
fn transient_get_bytes32(&mut self, key: Bytes32) -> Bytes32 { | ||
let transient_storage = &mut self.transient_storage.lock(); | ||
let transient_storage = transient_storage.get_mut(&self.program).unwrap(); | ||
let value = transient_storage.get(&key).cloned().unwrap_or_default(); | ||
value | ||
} | ||
|
||
fn transient_set_bytes32(&mut self, key: Bytes32, value: Bytes32) -> Result<()> { | ||
let transient_storage = &mut self.transient_storage.lock(); | ||
let transient_storage = transient_storage.get_mut(&self.program).unwrap(); | ||
transient_storage.insert(key, value); | ||
Ok(()) | ||
} | ||
|
||
/// Simulates a contract call. | ||
/// Note: this call function is for testing purposes only and deviates from onchain behavior. | ||
fn contract_call( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[build] | ||
target = "wasm32-unknown-unknown" |
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
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.
👍