-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: check diff before commiting it to storage (#92)
(cherry picked from commit 5c92fae)
- Loading branch information
Showing
4 changed files
with
169 additions
and
67 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use aurora_engine_sdk::io::IO; | ||
use engine_standalone_storage::{ | ||
engine_state::{EngineStateAccess, EngineStorageValue}, | ||
Diff, | ||
}; | ||
use std::cell::RefCell; | ||
|
||
#[derive(Clone, Copy)] | ||
pub struct BatchIO<'db, 'local> { | ||
pub fallback: EngineStateAccess<'db, 'db, 'db>, | ||
pub cumulative_diff: &'local Diff, | ||
pub current_diff: &'local RefCell<Diff>, | ||
} | ||
|
||
impl<'db, 'local> IO for BatchIO<'db, 'local> { | ||
type StorageValue = EngineStorageValue<'db>; | ||
|
||
fn read_input(&self) -> Self::StorageValue { | ||
self.fallback.read_input() | ||
} | ||
|
||
fn return_output(&mut self, value: &[u8]) { | ||
self.fallback.return_output(value) | ||
} | ||
|
||
fn read_storage(&self, key: &[u8]) -> Option<Self::StorageValue> { | ||
if let Some(diff) = self | ||
.current_diff | ||
.borrow() | ||
.get(key) | ||
.or_else(|| self.cumulative_diff.get(key)) | ||
{ | ||
return diff | ||
.value() | ||
.map(|bytes| EngineStorageValue::Vec(bytes.to_vec())); | ||
} | ||
self.fallback.read_storage(key) | ||
} | ||
|
||
fn storage_has_key(&self, key: &[u8]) -> bool { | ||
self.read_storage(key).is_some() | ||
} | ||
|
||
fn write_storage(&mut self, key: &[u8], value: &[u8]) -> Option<Self::StorageValue> { | ||
let original_value = self.read_storage(key); | ||
|
||
self.current_diff | ||
.borrow_mut() | ||
.modify(key.to_vec(), value.to_vec()); | ||
|
||
original_value | ||
} | ||
|
||
fn write_storage_direct( | ||
&mut self, | ||
key: &[u8], | ||
value: Self::StorageValue, | ||
) -> Option<Self::StorageValue> { | ||
self.write_storage(key, value.as_ref()) | ||
} | ||
|
||
fn remove_storage(&mut self, key: &[u8]) -> Option<Self::StorageValue> { | ||
let original_value = self.read_storage(key); | ||
|
||
self.current_diff.borrow_mut().delete(key.to_vec()); | ||
|
||
original_value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters