Skip to content

Commit 555334f

Browse files
authored
Use crypto_hash_mut whenever possible (#4818)
## Motivation `hash_mut` has fewer locks + I'm looking to simplify the implementation of the historical hash ## Proposal Use `crypto_hash_mut` whenever possible I'm tempted to remove the non-mut version but I suppose that's a different discussion. ## Test Plan CI ## Release Plan - These changes should be backported to the latest `devnet` branch, then - be released in a new SDK,
1 parent 055086a commit 555334f

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

linera-chain/src/chain.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ where
502502
return Ok(());
503503
}
504504
// Recompute the state hash.
505-
let hash = self.execution_state.crypto_hash().await?;
505+
let hash = self.execution_state.crypto_hash_mut().await?;
506506
self.execution_state_hash.set(Some(hash));
507507
let maybe_committee = self.execution_state.system.current_committee().into_iter();
508508
// Last, reset the consensus state based on the current ownership.
@@ -867,7 +867,7 @@ where
867867
let state_hash = {
868868
#[cfg(with_metrics)]
869869
let _hash_latency = metrics::STATE_HASH_COMPUTATION_LATENCY.measure_latency();
870-
chain.crypto_hash().await?
870+
chain.crypto_hash_mut().await?
871871
};
872872

873873
let (messages, oracle_responses, events, blobs, operation_results) =

linera-core/src/unit_tests/wasm_worker_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ where
218218
previous_message_blocks: BTreeMap::new(),
219219
previous_event_blocks: BTreeMap::new(),
220220
events: vec![Vec::new()],
221-
state_hash: creator_state.crypto_hash().await?,
221+
state_hash: creator_state.crypto_hash_mut().await?,
222222
oracle_responses: vec![vec![
223223
OracleResponse::Blob(contract_blob_id),
224224
OracleResponse::Blob(service_blob_id),
@@ -298,7 +298,7 @@ where
298298
previous_event_blocks: BTreeMap::new(),
299299
events: vec![Vec::new()],
300300
blobs: vec![Vec::new()],
301-
state_hash: creator_state.crypto_hash().await?,
301+
state_hash: creator_state.crypto_hash_mut().await?,
302302
oracle_responses: vec![vec![]],
303303
operation_results: vec![OperationResult(bcs::to_bytes(&15u64)?)],
304304
}

linera-execution/src/test_utils/system_execution_state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ impl SystemExecutionState {
7070
}
7171

7272
pub async fn into_hash(self) -> CryptoHash {
73-
let view = self.into_view().await;
74-
view.crypto_hash()
73+
let mut view = self.into_view().await;
74+
view.crypto_hash_mut()
7575
.await
7676
.expect("hashing from memory should not fail")
7777
}

linera-views/tests/random_container_tests.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ async fn classic_collection_view_check() -> Result<()> {
4848
let nmax: u8 = 25;
4949
for _ in 0..n {
5050
let mut view = CollectionStateView::load(context.clone()).await?;
51-
let hash = view.crypto_hash().await?;
51+
let hash = view.crypto_hash_mut().await?;
5252
let save = rng.gen::<bool>();
5353
//
5454
let count_oper = rng.gen_range(0..25);
@@ -102,7 +102,7 @@ async fn classic_collection_view_check() -> Result<()> {
102102
new_map = map.clone();
103103
}
104104
// Checking the hash
105-
let new_hash = view.crypto_hash().await?;
105+
let new_hash = view.crypto_hash_mut().await?;
106106
if map == new_map {
107107
assert_eq!(new_hash, hash);
108108
} else {
@@ -262,7 +262,7 @@ async fn run_map_view_mutability<R: RngCore + Clone>(rng: &mut R) -> Result<()>
262262
let mut view = ByteMapStateView::load(context.clone()).await?;
263263
let save = rng.gen::<bool>();
264264
let read_state = view.map.key_values().await?;
265-
let read_hash = view.crypto_hash().await?;
265+
let read_hash = view.crypto_hash_mut().await?;
266266
let state_vec = state_map.clone().into_iter().collect::<Vec<_>>();
267267
assert_eq!(state_vec, read_state);
268268
//
@@ -351,7 +351,7 @@ async fn run_map_view_mutability<R: RngCore + Clone>(rng: &mut R) -> Result<()>
351351
new_state_map.insert(key, new_value);
352352
}
353353
new_state_vec = new_state_map.clone().into_iter().collect();
354-
let new_hash = view.crypto_hash().await?;
354+
let new_hash = view.crypto_hash_mut().await?;
355355
if state_vec == new_state_vec {
356356
assert_eq!(new_hash, read_hash);
357357
} else {
@@ -415,7 +415,7 @@ async fn bucket_queue_view_mutability_check() -> Result<()> {
415415
let n = 200;
416416
for _ in 0..n {
417417
let mut view = BucketQueueStateView::load(context.clone()).await?;
418-
let hash = view.crypto_hash().await?;
418+
let hash = view.crypto_hash_mut().await?;
419419
let save = rng.gen::<bool>();
420420
let elements = view.queue.elements().await?;
421421
assert_eq!(elements, vector);
@@ -469,7 +469,7 @@ async fn bucket_queue_view_mutability_check() -> Result<()> {
469469
new_vector.clone_from(&vector);
470470
}
471471
let new_elements = view.queue.elements().await?;
472-
let new_hash = view.crypto_hash().await?;
472+
let new_hash = view.crypto_hash_mut().await?;
473473
if elements == new_elements {
474474
assert_eq!(new_hash, hash);
475475
} else {
@@ -557,7 +557,7 @@ async fn nested_collection_map_view_check() -> Result<()> {
557557
let n = 20;
558558
for _ in 0..n {
559559
let mut view = NestedCollectionMapView::load(context.clone()).await?;
560-
let hash = view.crypto_hash().await?;
560+
let hash = view.crypto_hash_mut().await?;
561561
let save = rng.gen::<bool>();
562562

563563
let count_oper = rng.gen_range(0..25);
@@ -614,7 +614,7 @@ async fn nested_collection_map_view_check() -> Result<()> {
614614
state_view, new_state_map,
615615
"state_view should match new_state_map"
616616
);
617-
let new_hash = view.crypto_hash().await?;
617+
let new_hash = view.crypto_hash_mut().await?;
618618
if state_map == new_state_map {
619619
assert_eq!(new_hash, hash);
620620
} else {
@@ -653,7 +653,7 @@ async fn queue_view_mutability_check() -> Result<()> {
653653
let n = 20;
654654
for _ in 0..n {
655655
let mut view = QueueStateView::load(context.clone()).await?;
656-
let hash = view.crypto_hash().await?;
656+
let hash = view.crypto_hash_mut().await?;
657657
let save = rng.gen::<bool>();
658658
let elements = view.queue.elements().await?;
659659
assert_eq!(elements, vector);
@@ -711,7 +711,7 @@ async fn queue_view_mutability_check() -> Result<()> {
711711
let front2 = new_vector.first().copied();
712712
assert_eq!(front1, front2);
713713
let new_elements = view.queue.elements().await?;
714-
let new_hash = view.crypto_hash().await?;
714+
let new_hash = view.crypto_hash_mut().await?;
715715
if elements == new_elements {
716716
assert_eq!(new_hash, hash);
717717
} else {
@@ -762,7 +762,7 @@ async fn reentrant_collection_view_check() -> Result<()> {
762762
let nmax: u8 = 25;
763763
for _ in 0..n {
764764
let mut view = ReentrantCollectionStateView::load(context.clone()).await?;
765-
let hash = view.crypto_hash().await?;
765+
let hash = view.crypto_hash_mut().await?;
766766
let key_values = view.key_values().await?;
767767
assert_eq!(key_values, map);
768768
//
@@ -861,7 +861,7 @@ async fn reentrant_collection_view_check() -> Result<()> {
861861
}
862862
}
863863
// Checking the hash
864-
let new_hash = view.crypto_hash().await?;
864+
let new_hash = view.crypto_hash_mut().await?;
865865
if new_map == map {
866866
assert_eq!(hash, new_hash);
867867
} else {

0 commit comments

Comments
 (0)