Skip to content

Commit

Permalink
Merge pull request #37 from OffchainLabs/call-flush
Browse files Browse the repository at this point in the history
add `.flush()` and `.clear()` to `RawCall`
  • Loading branch information
rachel-bousfield authored Sep 5, 2023
2 parents 3e8693c + 228834d commit b401b50
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions stylus-sdk/src/call/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::{
contract::read_return_data,
hostio::{self, RETURN_DATA_SIZE},
storage::StorageCache,
tx, ArbResult,
};
use alloy_primitives::{Address, B256, U256};
Expand All @@ -17,6 +18,7 @@ pub struct RawCall {
gas: Option<u64>,
offset: usize,
size: Option<usize>,
cache_policy: CachePolicy,
}

/// What kind of call to perform.
Expand All @@ -28,6 +30,14 @@ enum CallKind {
Static,
}

#[derive(Clone, Default, PartialEq, PartialOrd)]
enum CachePolicy {
#[default]
DoNothing,
Flush,
Clear,
}

#[derive(Copy, Clone)]
#[repr(C)]
struct RustVec {
Expand Down Expand Up @@ -111,6 +121,20 @@ impl RawCall {
self.limit_return_data(0, 0)
}

/// Write all cached values to persistent storage before the call.
pub fn flush_storage_cache(mut self) -> Self {
if self.cache_policy < CachePolicy::Flush {
self.cache_policy = CachePolicy::Flush;
}
self
}

/// Flush and clear the storage cache before the call.
pub fn clear_storage_cache(mut self) -> Self {
self.cache_policy = CachePolicy::Clear;
self
}

/// Performs a raw call to another contract at the given address with the given `calldata`.
///
/// # Safety
Expand All @@ -124,6 +148,11 @@ impl RawCall {
let gas = self.gas.unwrap_or(u64::MAX); // will be clamped by 63/64 rule
let value = B256::from(self.callvalue);
let status = unsafe {
match self.cache_policy {
CachePolicy::Clear => StorageCache::clear(),
CachePolicy::Flush => StorageCache::flush(),
CachePolicy::DoNothing => {}
}
match self.kind {
CallKind::Basic => hostio::call_contract(
contract.as_ptr(),
Expand Down

0 comments on commit b401b50

Please sign in to comment.