Skip to content

Commit

Permalink
Remove simple state storage APIs (#1455)
Browse files Browse the repository at this point in the history
* Remove simple storage API from contract system API

Guests can use the view APIs instead.

* Remove simple storage API from service system API

Guests should use the view APIs instead.

* Remove simple storage APIs from execution runtimes

All applications should now use the view APIs instead.

* Remove simple storage execution actor requests

Applications will only use the view APIs from now on.

* Remove simple state storage from execution state

Keep only the view storage state, renaming it to just `users`.

* Update Wasm fuel test fixtures

And the fuel consumption values.
  • Loading branch information
jvff authored Jan 9, 2024
1 parent 5525193 commit fdaa315
Show file tree
Hide file tree
Showing 11 changed files with 12 additions and 157 deletions.
7 changes: 2 additions & 5 deletions linera-execution/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use linera_views::{
common::Context,
key_value_store_view::KeyValueStoreView,
reentrant_collection_view::ReentrantCollectionView,
register_view::RegisterView,
views::{View, ViewError},
};
use linera_views_derive::CryptoHashView;
Expand All @@ -33,10 +32,8 @@ use {
pub struct ExecutionStateView<C> {
/// System application.
pub system: SystemExecutionStateView<C>,
/// User applications (Simple based).
pub simple_users: ReentrantCollectionView<C, UserApplicationId, RegisterView<C, Vec<u8>>>,
/// User applications (View based).
pub view_users: ReentrantCollectionView<C, UserApplicationId, KeyValueStoreView<C>>,
/// User applications.
pub users: ReentrantCollectionView<C, UserApplicationId, KeyValueStoreView<C>>,
}

#[cfg(any(test, feature = "test"))]
Expand Down
47 changes: 6 additions & 41 deletions linera-execution/src/execution_state_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,44 +58,20 @@ where
callback.respond(timestamp);
}

ReadSimpleUserState { id, callback } => {
let state = self
.simple_users
.try_load_entry_mut(&id)
.await
.unwrap()
.get()
.to_vec();
callback.respond(state);
}

SaveSimpleUserState {
id,
bytes,
callback,
} => {
self.simple_users
.try_load_entry_mut(&id)
.await
.unwrap()
.set(bytes);
callback.respond(());
}

ContainsKey { id, key, callback } => {
let view = self.view_users.try_load_entry(&id).await?;
let view = self.users.try_load_entry(&id).await?;
let result = view.contains_key(&key).await?;
callback.respond(result);
}

ReadMultiValuesBytes { id, keys, callback } => {
let view = self.view_users.try_load_entry(&id).await?;
let view = self.users.try_load_entry(&id).await?;
let values = view.multi_get(keys).await?;
callback.respond(values);
}

ReadValueBytes { id, key, callback } => {
let view = self.view_users.try_load_entry(&id).await?;
let view = self.users.try_load_entry(&id).await?;
let result = view.get(&key).await?;
callback.respond(result);
}
Expand All @@ -105,7 +81,7 @@ where
key_prefix,
callback,
} => {
let view = self.view_users.try_load_entry(&id).await?;
let view = self.users.try_load_entry(&id).await?;
let result = view.find_keys_by_prefix(&key_prefix).await?;
callback.respond(result);
}
Expand All @@ -115,7 +91,7 @@ where
key_prefix,
callback,
} => {
let view = self.view_users.try_load_entry(&id).await?;
let view = self.users.try_load_entry(&id).await?;
let result = view.find_key_values_by_prefix(&key_prefix).await?;
callback.respond(result);
}
Expand All @@ -125,7 +101,7 @@ where
batch,
callback,
} => {
let mut view = self.view_users.try_load_entry_mut(&id).await?;
let mut view = self.users.try_load_entry_mut(&id).await?;
view.write_batch(batch).await?;
callback.respond(());
}
Expand Down Expand Up @@ -155,17 +131,6 @@ pub enum Request {
callback: Sender<Timestamp>,
},

ReadSimpleUserState {
id: UserApplicationId,
callback: Sender<Vec<u8>>,
},

SaveSimpleUserState {
id: UserApplicationId,
bytes: Vec<u8>,
callback: Sender<()>,
},

ReadValueBytes {
id: UserApplicationId,
key: Vec<u8>,
Expand Down
19 changes: 0 additions & 19 deletions linera-execution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,21 +320,6 @@ pub trait BaseRuntime {
/// Reads the system timestamp.
fn read_system_timestamp(&mut self) -> Result<Timestamp, ExecutionError>;

// TODO(#1152): remove
/// Reads the application state.
fn try_read_my_state(&mut self) -> Result<Vec<u8>, ExecutionError> {
let promise = self.try_read_my_state_new()?;
self.try_read_my_state_wait(&promise)
}

// TODO(#1152): remove
/// Reads the application state (new).
fn try_read_my_state_new(&mut self) -> Result<Self::Read, ExecutionError>;

// TODO(#1152): remove
/// Reads the application state (wait).
fn try_read_my_state_wait(&mut self, promise: &Self::Read) -> Result<Vec<u8>, ExecutionError>;

/// Tests whether a key exists in the key-value store
#[cfg(feature = "test")]
fn contains_key(&mut self, key: Vec<u8>) -> Result<bool, ExecutionError> {
Expand Down Expand Up @@ -455,10 +440,6 @@ pub trait ContractRuntime: BaseRuntime {
/// Sets the amount of execution fuel remaining before execution is aborted.
fn set_remaining_fuel(&mut self, remaining_fuel: u64) -> Result<(), ExecutionError>;

// TODO(#1152): remove
/// Saves the application state and allows reading/loading the state again.
fn save_my_state(&mut self, state: Vec<u8>) -> Result<bool, ExecutionError>;

/// Calls another application. Forwarded sessions will now be visible to
/// `callee_id` (but not to the caller any more).
fn try_call_application(
Expand Down
45 changes: 0 additions & 45 deletions linera-execution/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use linera_views::batch::Batch;
use oneshot::Receiver;
use std::{
collections::{BTreeMap, HashSet},
ops::DerefMut,
sync::{Arc, Mutex},
};

Expand Down Expand Up @@ -462,14 +461,6 @@ impl<const W: bool> BaseRuntime for SyncRuntime<W> {
self.as_inner().read_system_timestamp()
}

fn try_read_my_state_new(&mut self) -> Result<Self::Read, ExecutionError> {
self.as_inner().try_read_my_state_new()
}

fn try_read_my_state_wait(&mut self, promise: &Self::Read) -> Result<Vec<u8>, ExecutionError> {
self.as_inner().try_read_my_state_wait(promise)
}

fn write_batch(&mut self, batch: Batch) -> Result<(), ExecutionError> {
self.as_inner().write_batch(batch)
}
Expand Down Expand Up @@ -571,27 +562,6 @@ impl<const W: bool> BaseRuntime for SyncRuntimeInternal<W> {
.recv_response()
}

fn try_read_my_state_new(&mut self) -> Result<Self::Read, ExecutionError> {
let id = self.application_id()?;
let state = self.simple_user_states.entry(id).or_default();
let receiver = self
.execution_state_sender
.send_request(|callback| Request::ReadSimpleUserState { id, callback })?;
state.pending_query = Some(receiver);
Ok(())
}

fn try_read_my_state_wait(&mut self, _promise: &Self::Read) -> Result<Vec<u8>, ExecutionError> {
let id = self.application_id()?;
let state = self
.simple_user_states
.get_mut(&id)
.ok_or(ExecutionError::InvalidPromise)?;
let receiver =
std::mem::take(&mut state.pending_query).ok_or(ExecutionError::InvalidPromise)?;
receiver.recv_response()
}

fn write_batch(&mut self, batch: Batch) -> Result<(), ExecutionError> {
let id = self.application_id()?;
let state = self.view_user_states.entry(id).or_default();
Expand Down Expand Up @@ -817,21 +787,6 @@ impl ContractRuntime for ContractSyncRuntime {
Ok(())
}

fn save_my_state(&mut self, bytes: Vec<u8>) -> Result<bool, ExecutionError> {
let mut this = self.as_inner();
let this = this.deref_mut();
let id = this.application_id()?;
let receiver =
this.execution_state_sender
.send_request(|callback| Request::SaveSimpleUserState {
id,
bytes,
callback,
})?;
receiver.recv_response()?;
Ok(true)
}

fn try_call_application(
&mut self,
authenticated: bool,
Expand Down
27 changes: 0 additions & 27 deletions linera-execution/src/wasm/system_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,6 @@ macro_rules! impl_contract_system_api {
BaseRuntime::read_system_timestamp(self).map(|timestamp| timestamp.micros())
}

// TODO(#1152): remove
fn load(&mut self) -> Result<Vec<u8>, Self::Error> {
self.try_read_my_state()
}

// TODO(#1152): remove
fn store(&mut self, state: &[u8]) -> Result<bool, Self::Error> {
self.save_my_state(state.to_vec())
}

fn try_call_application(
&mut self,
authenticated: bool,
Expand Down Expand Up @@ -123,8 +113,6 @@ macro_rules! impl_service_system_api {
{
type Error = ExecutionError;

type Load = <Self as BaseRuntime>::Read;

fn error_to_trap(&mut self, error: Self::Error) -> $trap {
error.into()
}
Expand All @@ -151,21 +139,6 @@ macro_rules! impl_service_system_api {
BaseRuntime::read_system_timestamp(self).map(|timestamp| timestamp.micros())
}

// TODO(#1152): remove
fn load_new(&mut self) -> Result<Self::Load, Self::Error> {
self.try_read_my_state_new()
}

// TODO(#1152): remove
fn load_wait(
&mut self,
promise: &Self::Load,
) -> Result<Result<Vec<u8>, String>, Self::Error> {
self.try_read_my_state_wait(promise)
// TODO(#1153): remove
.map(Ok)
}

fn try_query_application(
&mut self,
application: service_system_api::ApplicationId,
Expand Down
11 changes: 3 additions & 8 deletions linera-execution/src/wasm/wasmtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ mod conversions_from_wit;
#[path = "conversions_to_wit.rs"]
mod conversions_to_wit;

use self::{
contract::ContractData, service::ServiceData, service_system_api::ServiceSystemApiTables,
view_system_api::ViewSystemApiTables,
};
use self::{contract::ContractData, service::ServiceData, view_system_api::ViewSystemApiTables};
use super::{module_cache::ModuleCache, WasmExecutionError};
use crate::{
wasm::{WasmContractModule, WasmServiceModule},
Expand Down Expand Up @@ -220,7 +217,6 @@ where
{
data: ServiceData,
runtime: Runtime,
system_tables: ServiceSystemApiTables<Runtime>,
views_tables: ViewSystemApiTables<Runtime>,
}

Expand Down Expand Up @@ -266,7 +262,6 @@ where
Self {
data: ServiceData::default(),
runtime,
system_tables: ServiceSystemApiTables::default(),
views_tables: ViewSystemApiTables::default(),
}
}
Expand All @@ -277,8 +272,8 @@ where
}

/// Obtains the data required by the runtime to export the system API.
pub fn system_api(&mut self) -> (&mut Runtime, &mut ServiceSystemApiTables<Runtime>) {
(&mut self.runtime, &mut self.system_tables)
pub fn system_api(&mut self) -> &mut Runtime {
&mut self.runtime
}

/// Obtains the data required by the runtime to export the views API.
Expand Down
Binary file modified linera-execution/tests/fixtures/counter_contract.wasm
Binary file not shown.
Binary file modified linera-execution/tests/fixtures/counter_service.wasm
Binary file not shown.
5 changes: 1 addition & 4 deletions linera-execution/tests/test_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,10 +523,7 @@ where
Arc::new(TestModule::<false>::new(owner)),
);

let mut caller_application_state = state
.view_users
.try_load_entry_mut(&application_ids[0])
.await?;
let mut caller_application_state = state.users.try_load_entry_mut(&application_ids[0]).await?;
let callee_id =
bcs::to_bytes(&application_ids[1]).expect("Failed to serialize application ID to call");

Expand Down
3 changes: 0 additions & 3 deletions linera-sdk/contract_system_api.wit
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ enum log-level {
error,
}

load: func() -> list<u8>
store: func(value: list<u8>) -> bool

try-call-application: func(
authenticated: bool,
application: application-id,
Expand Down
5 changes: 0 additions & 5 deletions linera-sdk/service_system_api.wit
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ enum log-level {
error,
}

resource load {
static new: func() -> load
wait: func() -> result<list<u8>, string>
}

try-query-application: func(
application: application-id,
query: list<u8>,
Expand Down

0 comments on commit fdaa315

Please sign in to comment.