Skip to content

Commit 2c0b666

Browse files
committed
Insertion of the has_non_trivial_storage code.
1 parent 7ba8e2c commit 2c0b666

File tree

8 files changed

+86
-0
lines changed

8 files changed

+86
-0
lines changed

linera-execution/src/execution_state_actor.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,21 @@ where
659659
.to_round()?;
660660
callback.respond(validation_round);
661661
}
662+
663+
HasNonTrivialStorage {
664+
application,
665+
callback,
666+
} => {
667+
let view = self.state.users.try_load_entry(&application).await?;
668+
let result = match view {
669+
Some(view) => {
670+
let total_size = view.total_size();
671+
(total_size.key, total_size.value)
672+
}
673+
None => (0, 0),
674+
};
675+
callback.respond(result);
676+
}
662677
}
663678

664679
Ok(())
@@ -1206,4 +1221,10 @@ pub enum ExecutionRequest {
12061221
#[debug(skip)]
12071222
callback: Sender<Option<u32>>,
12081223
},
1224+
1225+
HasNonTrivialStorage {
1226+
application: ApplicationId,
1227+
#[debug(skip)]
1228+
callback: Sender<(u32, u32)>,
1229+
},
12091230
}

linera-execution/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,12 @@ pub trait BaseRuntime {
668668

669669
/// Asserts the existence of a data blob with the given hash.
670670
fn assert_data_blob_exists(&mut self, hash: DataBlobHash) -> Result<(), ExecutionError>;
671+
672+
/// Returns true if the corresponding contract uses a non-zero amount of storage.
673+
fn has_non_trivial_storage(
674+
&mut self,
675+
application: ApplicationId,
676+
) -> Result<bool, ExecutionError>;
671677
}
672678

673679
pub trait ServiceRuntime: BaseRuntime {

linera-execution/src/runtime.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,21 @@ where
897897
.send_request(|callback| ExecutionRequest::AssertBlobExists { blob_id, callback })?
898898
.recv_response()
899899
}
900+
901+
fn has_non_trivial_storage(
902+
&mut self,
903+
application: ApplicationId,
904+
) -> Result<bool, ExecutionError> {
905+
let this = self.inner();
906+
let (key_size, value_size) = this
907+
.execution_state_sender
908+
.send_request(move |callback| ExecutionRequest::HasNonTrivialStorage {
909+
application,
910+
callback,
911+
})?
912+
.recv_response()?;
913+
Ok(key_size > 0 || value_size > 0)
914+
}
900915
}
901916

902917
/// An extension trait to determine in compile time the different behaviors between contract and

linera-execution/src/wasm/runtime_api.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,18 @@ where
231231
.map_err(|error| RuntimeError::Custom(error.into()))
232232
}
233233

234+
/// Returns true if the corresponding contract uses a non-zero amount of storage.
235+
fn has_non_trivial_storage(
236+
caller: &mut Caller,
237+
application: ApplicationId,
238+
) -> Result<bool, RuntimeError> {
239+
caller
240+
.user_data_mut()
241+
.runtime
242+
.has_non_trivial_storage(application)
243+
.map_err(|error| RuntimeError::Custom(error.into()))
244+
}
245+
234246
/// Logs a `message` with the provided information `level`.
235247
fn log(_caller: &mut Caller, message: String, level: log::Level) -> Result<(), RuntimeError> {
236248
match level {

linera-sdk/src/contract/runtime.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ where
164164
pub fn assert_data_blob_exists(&mut self, hash: DataBlobHash) {
165165
base_wit::assert_data_blob_exists(hash.into())
166166
}
167+
168+
/// Returns true if the corresponding contract uses a non-zero amount of storage.
169+
pub fn has_non_trivial_storage(&mut self, application: ApplicationId) -> bool {
170+
base_wit::has_non_trivial_storage(application.into())
171+
}
167172
}
168173

169174
impl<Application> ContractRuntime<Application>

linera-sdk/src/contract/test_runtime.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ where
7878
expected_http_requests: VecDeque<(http::Request, http::Response)>,
7979
expected_read_data_blob_requests: VecDeque<(DataBlobHash, Vec<u8>)>,
8080
expected_assert_data_blob_exists_requests: VecDeque<(DataBlobHash, Option<()>)>,
81+
expected_has_non_trivial_storage_requests: VecDeque<(ApplicationId, bool)>,
8182
expected_open_chain_calls: VecDeque<(ChainOwnership, ApplicationPermissions, Amount, ChainId)>,
8283
expected_publish_module_calls: VecDeque<ExpectedPublishModuleCall>,
8384
expected_create_application_calls: VecDeque<ExpectedCreateApplicationCall>,
@@ -127,6 +128,7 @@ where
127128
expected_http_requests: VecDeque::new(),
128129
expected_read_data_blob_requests: VecDeque::new(),
129130
expected_assert_data_blob_exists_requests: VecDeque::new(),
131+
expected_has_non_trivial_storage_requests: VecDeque::new(),
130132
expected_open_chain_calls: VecDeque::new(),
131133
expected_publish_module_calls: VecDeque::new(),
132134
expected_create_application_calls: VecDeque::new(),
@@ -936,6 +938,16 @@ where
936938
.push_back((hash, response));
937939
}
938940

941+
/// Adds an expected `has_non_trivial_storage` call, and the response it should return in the test.
942+
pub fn add_expected_has_non_trivial_storage_requests(
943+
&mut self,
944+
application: ApplicationId,
945+
response: bool,
946+
) {
947+
self.expected_has_non_trivial_storage_requests
948+
.push_back((application, response));
949+
}
950+
939951
/// Queries an application service as an oracle and returns the response.
940952
///
941953
/// Should only be used with queries where it is very likely that all validators will compute
@@ -997,6 +1009,15 @@ where
9971009
response.expect("Blob does not exist!");
9981010
}
9991011

1012+
/// Returns true if the corresponding contract uses a non-zero amount of storage.
1013+
pub fn has_non_trivial_storage(&mut self, application: ApplicationId) -> bool {
1014+
let maybe_request = self.expected_has_non_trivial_storage_requests.pop_front();
1015+
let (expected_application_id, response) =
1016+
maybe_request.expect("Unexpected has_non_trivial_storage request");
1017+
assert_eq!(application, expected_application_id);
1018+
response
1019+
}
1020+
10001021
/// Returns the round in which this block was validated.
10011022
pub fn validation_round(&mut self) -> Option<u32> {
10021023
self.round

linera-sdk/src/service/runtime.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ where
159159
pub fn assert_data_blob_exists(&self, hash: DataBlobHash) {
160160
base_wit::assert_data_blob_exists(hash.into())
161161
}
162+
163+
/// Returns true if the corresponding contract uses a non-zero amount of storage.
164+
pub fn has_non_trivial_storage(&self, application: ApplicationId) -> bool {
165+
base_wit::has_non_trivial_storage(application.into())
166+
}
162167
}
163168

164169
impl<Application> ServiceRuntime<Application>

linera-sdk/wit/base-runtime-api.wit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ interface base-runtime-api {
1616
assert-before: func(timestamp: timestamp);
1717
read-data-blob: func(hash: data-blob-hash) -> list<u8>;
1818
assert-data-blob-exists: func(hash: data-blob-hash);
19+
has-non-trivial-storage: func(application: application-id) -> bool;
1920
log: func(message: string, level: log-level);
2021
contains-key-new: func(key: list<u8>) -> u32;
2122
contains-key-wait: func(promise-id: u32) -> bool;

0 commit comments

Comments
 (0)