Skip to content

Commit

Permalink
implement the ModuleCache for MoveOSCache
Browse files Browse the repository at this point in the history
  • Loading branch information
steelgeek091 committed Jan 9, 2025
1 parent 6f5d6d4 commit d86715b
Show file tree
Hide file tree
Showing 7 changed files with 738 additions and 240 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ inferno = "0.11.21"
handlebars = "4.2.2"
ambassador = "0.4.1"
triomphe = "0.1.14"
hashbrown = "0.15.2"

# Note: the BEGIN and END comments below are required for external tooling. Do not remove.
# BEGIN MOVE DEPENDENCIES
Expand Down
166 changes: 0 additions & 166 deletions moveos/moveos-store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,169 +410,3 @@ pub fn load_feature_store_object<Resolver: StateResolver>(
}
}
}

/// Additional data stored alongside deserialized or verified modules.
#[derive(Clone)]
pub struct RoochModuleExtension {
/// Serialized representation of the module.
bytes: Bytes,
/// Module's hash.
hash: [u8; 32],
/// The state value metadata associated with the module, when read from or
/// written to storage.
state_value_metadata: StateValueMetadata,
}

impl RoochModuleExtension {
/*
/// Creates new extension based on [StateValue].
pub fn new(state_value: StateValue) -> Self {
let (state_value_metadata, bytes) = state_value.unpack();
let hash = sha3_256(&bytes);
Self {
bytes,
hash,
state_value_metadata,
}
}
*/

/// Returns the state value metadata stored in extension.
pub fn state_value_metadata(&self) -> &StateValueMetadata {
&self.state_value_metadata
}
}

impl WithBytes for RoochModuleExtension {
fn bytes(&self) -> &Bytes {
&self.bytes
}
}

impl WithHash for RoochModuleExtension {
fn hash(&self) -> &[u8; 32] {
&self.hash
}
}

#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
struct StateValueMetadataInner {
slot_deposit: u64,
bytes_deposit: u64,
creation_time_usecs: u64,
}

#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct StateValueMetadata {
inner: Option<StateValueMetadataInner>,
}

impl StateValueMetadata {
/*
pub fn into_persistable(self) -> Option<PersistedStateValueMetadata> {
self.inner.map(|inner| {
let StateValueMetadataInner {
slot_deposit,
bytes_deposit,
creation_time_usecs,
} = inner;
if bytes_deposit == 0 {
PersistedStateValueMetadata::V0 {
deposit: slot_deposit,
creation_time_usecs,
}
} else {
PersistedStateValueMetadata::V1 {
slot_deposit,
bytes_deposit,
creation_time_usecs,
}
}
})
}
*/

pub fn new(
slot_deposit: u64,
bytes_deposit: u64,
creation_time_usecs: &CurrentTimeMicroseconds,
) -> Self {
Self::new_impl(
slot_deposit,
bytes_deposit,
creation_time_usecs.microseconds,
)
}

pub fn legacy(slot_deposit: u64, creation_time_usecs: &CurrentTimeMicroseconds) -> Self {
Self::new(slot_deposit, 0, creation_time_usecs)
}

pub fn placeholder(creation_time_usecs: &CurrentTimeMicroseconds) -> Self {
Self::legacy(0, creation_time_usecs)
}

pub fn none() -> Self {
Self { inner: None }
}

fn new_impl(slot_deposit: u64, bytes_deposit: u64, creation_time_usecs: u64) -> Self {
Self {
inner: Some(StateValueMetadataInner {
slot_deposit,
bytes_deposit,
creation_time_usecs,
}),
}
}

pub fn is_none(&self) -> bool {
self.inner.is_none()
}

fn inner(&self) -> Option<&StateValueMetadataInner> {
self.inner.as_ref()
}

pub fn creation_time_usecs(&self) -> u64 {
self.inner().map_or(0, |v1| v1.creation_time_usecs)
}

pub fn slot_deposit(&self) -> u64 {
self.inner().map_or(0, |v1| v1.slot_deposit)
}

pub fn bytes_deposit(&self) -> u64 {
self.inner().map_or(0, |v1| v1.bytes_deposit)
}

pub fn total_deposit(&self) -> u64 {
self.slot_deposit() + self.bytes_deposit()
}

pub fn maybe_upgrade(&mut self) -> &mut Self {
*self = Self::new_impl(
self.slot_deposit(),
self.bytes_deposit(),
self.creation_time_usecs(),
);
self
}

fn expect_upgraded(&mut self) -> &mut StateValueMetadataInner {
self.inner.as_mut().expect("State metadata is None.")
}

pub fn set_slot_deposit(&mut self, amount: u64) {
self.expect_upgraded().slot_deposit = amount;
}

pub fn set_bytes_deposit(&mut self, amount: u64) {
self.expect_upgraded().bytes_deposit = amount;
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CurrentTimeMicroseconds {
pub microseconds: u64,
}
1 change: 1 addition & 0 deletions moveos/moveos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ tracing-subscriber = { workspace = true }
ambassador = { workspace = true }
bytes = { workspace = true }
sha3 = { workspace = true }
hashbrown = { workspace = true }

move-binary-format = { workspace = true }
move-core-types = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions moveos/moveos/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod data_cache;
pub mod moveos_vm;
pub mod tx_argument_resolver;
pub mod vm_status_explainer;
pub mod module_cache;

#[cfg(test)]
mod unit_tests;
Loading

0 comments on commit d86715b

Please sign in to comment.