Skip to content

Privatize storage handling #141

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

Merged
merged 3 commits into from
Feb 3, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
`<module>.<name>`.
- Make gas limit immutable in `cosmwasm_vm::instance::Instance`. It is passed
once at construction time and cannot publicly be manipulated anymore.
- Remove `take_storage`/`leave_storage` from `cosmwasm_vm::Instance`.

## 0.6

Expand Down
10 changes: 4 additions & 6 deletions lib/vm/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,12 @@ where
pub fn store_instance(&mut self, id: &[u8], instance: Instance<S, A>) -> Option<Extern<S, A>> {
if let Some(cache) = &mut self.instances {
let hash = WasmHash::generate(&id);
let storage = instance.take_storage();
let (wasmer_instance, api) = Instance::recycle(instance);
let (wasmer_instance, ext) = Instance::recycle(instance);
cache.put(hash, wasmer_instance);
if let Some(storage) = storage {
return Some(Extern { storage, api });
}
ext
} else {
None
}
None
}
}

Expand Down
27 changes: 13 additions & 14 deletions lib/vm/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,26 @@ where
gas_limit: u64,
) -> Self {
set_gas(&mut wasmer_instance, gas_limit);
let res = Instance {
leave_storage(wasmer_instance.context(), Some(deps.storage));
Instance {
wasmer_instance: wasmer_instance,
api: deps.api,
type_storage: PhantomData::<S> {},
};
res.leave_storage(Some(deps.storage));
res
}
}

/// Takes ownership of instance and decomposes it into its components.
/// The components we want to preserve are returned, the rest is dropped.
pub fn recycle(instance: Self) -> (wasmer_runtime_core::Instance, A) {
(instance.wasmer_instance, instance.api)
pub fn recycle(instance: Self) -> (wasmer_runtime_core::Instance, Option<Extern<S, A>>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes. I know this just moved the same code blob down one level, but I prefer this to keep the public api simpler.

let ext = if let Some(storage) = take_storage(instance.wasmer_instance.context()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this is totally valid. You can also use a match statement here. I'm not sure which is "better" here. I like let when there is one option, and match for 3 or more. For 2, personal choice

Some(Extern {
storage: storage,
api: instance.api,
})
} else {
None
};
(instance.wasmer_instance, ext)
}

pub fn get_gas(&self) -> u64 {
Expand All @@ -104,14 +111,6 @@ where
with_storage_from_context(self.wasmer_instance.context(), func)
}

pub fn take_storage(&self) -> Option<S> {
take_storage(self.wasmer_instance.context())
}

pub fn leave_storage(&self, storage: Option<S>) {
leave_storage(self.wasmer_instance.context(), storage);
}

pub fn memory(&self, ptr: u32) -> Vec<u8> {
read_region(self.wasmer_instance.context(), ptr)
}
Expand Down