11// Copyright (c) Zefchain Labs, Inc.
22// SPDX-License-Identifier: Apache-2.0
33
4- use std:: { collections:: BTreeMap , vec} ;
4+ use std:: {
5+ collections:: BTreeMap ,
6+ ops:: { Deref , DerefMut } ,
7+ vec,
8+ } ;
59
610use allocative:: Allocative ;
711use futures:: { FutureExt , StreamExt } ;
@@ -15,8 +19,8 @@ use linera_views::{
1519 context:: Context ,
1620 historical_hash_wrapper:: HistoricallyHashableView ,
1721 key_value_store_view:: KeyValueStoreView ,
18- reentrant_collection_view:: HistoricallyHashedReentrantCollectionView ,
19- views:: { ClonableView , Hasher as _ , ReplaceContext , View } ,
22+ reentrant_collection_view:: ReentrantCollectionView ,
23+ views:: { ClonableView , ReplaceContext , View } ,
2024 ViewError ,
2125} ;
2226#[ cfg( with_testing) ]
@@ -38,14 +42,49 @@ use crate::{
3842 ServiceSyncRuntime , Timestamp , TransactionTracker ,
3943} ;
4044
41- /// A view accessing the execution state of a chain.
45+ /// An inner view accessing the execution state of a chain, for hashing purposes .
4246#[ derive( Debug , ClonableView , View , Allocative ) ]
4347#[ allocative( bound = "C" ) ]
44- pub struct ExecutionStateView < C > {
48+ pub struct ExecutionStateViewInner < C > {
4549 /// System application.
46- pub system : HistoricallyHashableView < C , SystemExecutionStateView < C > > ,
50+ pub system : SystemExecutionStateView < C > ,
4751 /// User applications.
48- pub users : HistoricallyHashedReentrantCollectionView < C , ApplicationId , KeyValueStoreView < C > > ,
52+ pub users : ReentrantCollectionView < C , ApplicationId , KeyValueStoreView < C > > ,
53+ }
54+
55+ impl < C : Context , C2 : Context > ReplaceContext < C2 > for ExecutionStateViewInner < C > {
56+ type Target = ExecutionStateViewInner < C2 > ;
57+
58+ async fn with_context (
59+ & mut self ,
60+ ctx : impl FnOnce ( & Self :: Context ) -> C2 + Clone ,
61+ ) -> Self :: Target {
62+ ExecutionStateViewInner {
63+ system : self . system . with_context ( ctx. clone ( ) ) . await ,
64+ users : self . users . with_context ( ctx. clone ( ) ) . await ,
65+ }
66+ }
67+ }
68+
69+ /// A view accessing the execution state of a chain.
70+ #[ derive( Debug , ClonableView , View , Allocative ) ]
71+ #[ allocative( bound = "C" ) ]
72+ pub struct ExecutionStateView < C > {
73+ inner : HistoricallyHashableView < C , ExecutionStateViewInner < C > > ,
74+ }
75+
76+ impl < C > Deref for ExecutionStateView < C > {
77+ type Target = ExecutionStateViewInner < C > ;
78+
79+ fn deref ( & self ) -> & ExecutionStateViewInner < C > {
80+ self . inner . deref ( )
81+ }
82+ }
83+
84+ impl < C > DerefMut for ExecutionStateView < C > {
85+ fn deref_mut ( & mut self ) -> & mut ExecutionStateViewInner < C > {
86+ self . inner . deref_mut ( )
87+ }
4988}
5089
5190impl < C > ExecutionStateView < C >
5796 #[ derive( Serialize , Deserialize ) ]
5897 struct ExecutionStateViewHash ( [ u8 ; 32 ] ) ;
5998 impl BcsHashable < ' _ > for ExecutionStateViewHash { }
60- let ExecutionStateView { system, users } = self ;
61- let mut hasher = linera_views:: sha3:: Sha3_256 :: default ( ) ;
62- hasher. update_with_bytes ( & system. historical_hash ( ) . await ?) ?;
63- hasher. update_with_bytes ( & users. historical_hash ( ) . await ?) ?;
64- let output = hasher. finalize ( ) ;
65- Ok ( CryptoHash :: new ( & ExecutionStateViewHash ( output. into ( ) ) ) )
99+ let hash = self . inner . historical_hash ( ) . await ?;
100+ Ok ( CryptoHash :: new ( & ExecutionStateViewHash ( hash. into ( ) ) ) )
66101 }
67102}
68103
@@ -74,8 +109,7 @@ impl<C: Context, C2: Context> ReplaceContext<C2> for ExecutionStateView<C> {
74109 ctx : impl FnOnce ( & Self :: Context ) -> C2 + Clone ,
75110 ) -> Self :: Target {
76111 ExecutionStateView {
77- system : self . system . with_context ( ctx. clone ( ) ) . await ,
78- users : self . users . with_context ( ctx. clone ( ) ) . await ,
112+ inner : self . inner . with_context ( ctx. clone ( ) ) . await ,
79113 }
80114 }
81115}
0 commit comments