Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: check diff before commiting it to storage #92

Merged
merged 2 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ license = "CC0-1.0"
[workspace.dependencies]
actix = "0.13"
anyhow = "1"
aurora-engine = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "2.10.2", default-features = false, features = ["std", "tracing", "log", "impl-serde"] }
aurora-engine-transactions = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "2.10.2", default-features = false, features = ["std", "impl-serde"] }
aurora-engine-types = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "2.10.2", default-features = false, features = ["std", "impl-serde"] }
aurora-engine-sdk = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "2.10.2", default-features = false, features = ["std"] }
aurora-engine-modexp = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "2.10.2", default-features = false, features = ["std"] }
aurora-engine = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.0.0", default-features = false, features = ["std", "tracing", "log", "impl-serde"] }
aurora-engine-transactions = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.0.0", default-features = false, features = ["std", "impl-serde"] }
aurora-engine-types = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.0.0", default-features = false, features = ["std", "impl-serde"] }
aurora-engine-sdk = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.0.0", default-features = false, features = ["std"] }
aurora-engine-modexp = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.0.0", default-features = false, features = ["std"] }
borsh = "0.10"
byteorder = "1"
clap = { version = "4", features = ["derive"] }
derive_builder = "0.12"
engine-standalone-storage = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "2.10.2", default-features = false }
engine-standalone-tracing = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "2.10.2", default-features = false, features = ["impl-serde"] }
engine-standalone-storage = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.0.0", default-features = false }
engine-standalone-tracing = { git = "https://github.com/aurora-is-near/aurora-engine.git", tag = "3.0.0", default-features = false, features = ["impl-serde"] }
fixed-hash = "0.8"
futures = "0.3"
hex = "0.4"
Expand Down
69 changes: 69 additions & 0 deletions engine/src/batch_tx_processing.rs
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
}
}
1 change: 1 addition & 0 deletions engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::collections::HashMap;
use std::num::NonZeroUsize;
use std::path::Path;

mod batch_tx_processing;
pub mod gas;
pub mod sync;
#[cfg(test)]
Expand Down
Loading
Loading