linera_service/
storage.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use async_trait::async_trait;
5use linera_client::config::GenesisConfig;
6use linera_storage::DbStorage;
7pub use linera_storage::StorageCacheConfig;
8pub use linera_storage_runtime::{
9    AssertStorageV1, CommonStorageOptions, InnerStorageConfig, Runnable, RunnableWithStore,
10    StorageConfig, StorageMigration, StoreConfig,
11};
12use linera_views::store::{KeyValueDatabase, KeyValueStore};
13
14struct InitializeStorageJob<'a>(&'a GenesisConfig);
15
16#[async_trait]
17impl RunnableWithStore for InitializeStorageJob<'_> {
18    type Output = ();
19
20    async fn run<D>(
21        self,
22        config: D::Config,
23        namespace: String,
24        cache_sizes: StorageCacheConfig,
25    ) -> Result<Self::Output, anyhow::Error>
26    where
27        D: KeyValueDatabase + Clone + Send + Sync + 'static,
28        D::Store: KeyValueStore + Clone + Send + Sync + 'static,
29        D::Error: Send + Sync,
30    {
31        let mut storage =
32            DbStorage::<D, _>::maybe_create_and_connect(&config, &namespace, None, cache_sizes)
33                .await?;
34        self.0.initialize_storage(&mut storage).await?;
35        Ok(())
36    }
37}
38
39/// Initializes storage by running migration and then writing the genesis config.
40pub async fn initialize(
41    store_config: StoreConfig,
42    cache_sizes: StorageCacheConfig,
43    config: &GenesisConfig,
44) -> Result<(), anyhow::Error> {
45    store_config
46        .clone()
47        .run_with_store(cache_sizes, StorageMigration)
48        .await?;
49    store_config
50        .run_with_store(cache_sizes, InitializeStorageJob(config))
51        .await
52}