linera_views/views/
mod.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use std::{fmt::Debug, future::Future, io::Write};
5
6use linera_base::crypto::CryptoHash;
7pub use linera_views_derive::{
8    ClonableView, CryptoHashRootView, CryptoHashView, HashableView, RootView, View,
9};
10use serde::Serialize;
11
12use crate::{batch::Batch, common::HasherOutput, ViewError};
13
14#[cfg(test)]
15#[path = "unit_tests/views.rs"]
16mod tests;
17
18/// The `RegisterView` implements a register for a single value.
19pub mod register_view;
20
21/// The `LogView` implements a log list that can be pushed.
22pub mod log_view;
23
24/// The `BucketQueueView` implements a queue that can push on the back and delete on the front and group data in buckets.
25pub mod bucket_queue_view;
26
27/// The `QueueView` implements a queue that can push on the back and delete on the front.
28pub mod queue_view;
29
30/// The `MapView` implements a map with ordered keys.
31pub mod map_view;
32
33/// The `SetView` implements a set with ordered entries.
34pub mod set_view;
35
36/// The `CollectionView` implements a map structure whose keys are ordered and the values are views.
37pub mod collection_view;
38
39/// The `ReentrantCollectionView` implements a map structure whose keys are ordered and the values are views with concurrent access.
40pub mod reentrant_collection_view;
41
42/// The implementation of a key-value store view.
43pub mod key_value_store_view;
44
45/// Wrapping a view to memoize hashing.
46pub mod hashable_wrapper;
47
48/// Wrapping a view to compute hash based on the history of modifications to the view.
49pub mod historical_hash_wrapper;
50
51/// The minimum value for the view tags. Values in `0..MIN_VIEW_TAG` are used for other purposes.
52pub const MIN_VIEW_TAG: u8 = 1;
53
54/// A view gives exclusive access to read and write the data stored at an underlying
55/// address in storage.
56#[cfg_attr(not(web), trait_variant::make(Send + Sync))]
57pub trait View: Sized {
58    /// The number of keys used for the initialization
59    const NUM_INIT_KEYS: usize;
60
61    /// The type of context stored in this view
62    type Context: crate::context::Context;
63
64    /// Obtains a mutable reference to the internal context.
65    fn context(&self) -> &Self::Context;
66
67    /// Creates the keys needed for loading the view
68    fn pre_load(context: &Self::Context) -> Result<Vec<Vec<u8>>, ViewError>;
69
70    /// Loads a view from the values
71    fn post_load(context: Self::Context, values: &[Option<Vec<u8>>]) -> Result<Self, ViewError>;
72
73    /// Loads a view
74    fn load(context: Self::Context) -> impl Future<Output = Result<Self, ViewError>> {
75        async {
76            if Self::NUM_INIT_KEYS == 0 {
77                Self::post_load(context, &[])
78            } else {
79                use crate::{context::Context, store::ReadableKeyValueStore};
80                let keys = Self::pre_load(&context)?;
81                let values = context.store().read_multi_values_bytes(&keys).await?;
82                Self::post_load(context, &values)
83            }
84        }
85    }
86
87    /// Discards all pending changes. After that `flush` should have no effect to storage.
88    fn rollback(&mut self);
89
90    /// Returns [`true`] if flushing this view would result in changes to the persistent storage.
91    async fn has_pending_changes(&self) -> bool;
92
93    /// Clears the view. That can be seen as resetting to default. If the clear is followed
94    /// by a flush then all the relevant data is removed on the storage.
95    fn clear(&mut self);
96
97    /// Computes the batch of operations to persist changes to storage without modifying the view.
98    /// Crash-resistant storage implementations accumulate the desired changes in the `batch` variable.
99    /// The returned boolean indicates whether the operation removes the view or not.
100    fn pre_save(&self, batch: &mut Batch) -> Result<bool, ViewError>;
101
102    /// Updates the view state after the batch has been executed in the database.
103    /// This should be called after `pre_save` and after the batch has been successfully written to storage.
104    /// This leaves the view in a clean state with no pending changes.
105    ///
106    /// May panic if `pre_save` was not called right before on `self`.
107    fn post_save(&mut self);
108
109    /// Builds a trivial view that is already deleted
110    fn new(context: Self::Context) -> Result<Self, ViewError> {
111        let values = vec![None; Self::NUM_INIT_KEYS];
112        let mut view = Self::post_load(context, &values)?;
113        view.clear();
114        Ok(view)
115    }
116}
117
118/// A view which can have its context replaced.
119pub trait ReplaceContext<C: crate::context::Context>: View {
120    /// The type returned after replacing the context.
121    type Target: View<Context = C>;
122
123    /// Returns a view with a replaced context.
124    async fn with_context(&mut self, ctx: impl FnOnce(&Self::Context) -> C + Clone)
125        -> Self::Target;
126}
127
128/// A view that supports hashing its values.
129#[cfg_attr(not(web), trait_variant::make(Send))]
130pub trait HashableView: View {
131    /// How to compute hashes.
132    type Hasher: Hasher;
133
134    /// Computes the hash of the values.
135    ///
136    /// Implementations do not need to include a type tag. However, the usual precautions
137    /// to enforce collision resistance must be applied (e.g. including the length of a
138    /// collection of values).
139    async fn hash(&self) -> Result<<Self::Hasher as Hasher>::Output, ViewError>;
140
141    /// Same as `hash` but guaranteed to be wait-free.
142    async fn hash_mut(&mut self) -> Result<<Self::Hasher as Hasher>::Output, ViewError>;
143}
144
145/// The requirement for the hasher type in [`HashableView`].
146pub trait Hasher: Default + Write + Send + Sync + 'static {
147    /// The output type.
148    type Output: Debug + Clone + Eq + AsRef<[u8]> + 'static;
149
150    /// Finishes the hashing process and returns its output.
151    fn finalize(self) -> Self::Output;
152
153    /// Serializes a value with BCS and includes it in the hash.
154    fn update_with_bcs_bytes(&mut self, value: &impl Serialize) -> Result<(), ViewError> {
155        bcs::serialize_into(self, value)?;
156        Ok(())
157    }
158
159    /// Includes bytes in the hash.
160    fn update_with_bytes(&mut self, value: &[u8]) -> Result<(), ViewError> {
161        self.write_all(value)?;
162        Ok(())
163    }
164}
165
166impl Hasher for sha3::Sha3_256 {
167    type Output = HasherOutput;
168
169    fn finalize(self) -> Self::Output {
170        <sha3::Sha3_256 as sha3::Digest>::finalize(self)
171    }
172}
173
174/// A [`View`] whose staged modifications can be saved in storage.
175#[cfg_attr(not(web), trait_variant::make(Send))]
176pub trait RootView: View {
177    /// Saves the root view to the database context
178    async fn save(&mut self) -> Result<(), ViewError>;
179}
180
181/// A [`View`] that also supports crypto hash
182#[cfg_attr(not(web), trait_variant::make(Send))]
183pub trait CryptoHashView: HashableView {
184    /// Computing the hash and attributing the type to it. May require locking.
185    async fn crypto_hash(&self) -> Result<CryptoHash, ViewError>;
186
187    /// Same as `crypto_hash` but guaranteed to be wait-free.
188    async fn crypto_hash_mut(&mut self) -> Result<CryptoHash, ViewError>;
189}
190
191/// A [`RootView`] that also supports crypto hash
192#[cfg_attr(not(web), trait_variant::make(Send))]
193pub trait CryptoHashRootView: RootView + CryptoHashView {}
194
195/// A view that can be shared (unsafely) by cloning it.
196///
197/// Note: Calling `flush` on any of the shared views will break the other views. Therefore,
198/// cloning views is only safe if `flush` only ever happens after all the copies but one
199/// have been dropped.
200pub trait ClonableView: View {
201    /// Creates a clone of this view, sharing the underlying storage context but prone to
202    /// data races which can corrupt the view state.
203    fn clone_unchecked(&mut self) -> Result<Self, ViewError>;
204}