Skip to main content

linera_core/environment/
mod.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The runtime environment (storage, network, signer and wallet) used by the client.
5
6/// The wallet abstraction tracking the set of chains followed by the client.
7pub mod wallet;
8
9use linera_base::util::traits::AutoTraits;
10
11trait_set::trait_set! {
12    /// The provider of validator nodes used to communicate with the network.
13    pub trait Network = crate::node::ValidatorNodeProvider + AutoTraits;
14    /// The signer used to sign blocks and other messages on behalf of chain owners.
15    pub trait Signer = linera_base::crypto::Signer + AutoTraits;
16    /// The persistent storage backend for chains and certificates.
17    pub trait Storage = linera_storage::Storage + Clone + AutoTraits;
18    /// The wallet tracking the set of chains followed by the client.
19    pub trait Wallet = wallet::Wallet + AutoTraits;
20}
21
22/// The collection of services (storage, network, signer and wallet) available to the client.
23pub trait Environment: AutoTraits {
24    /// The persistent storage backend.
25    type Storage: Storage<Context = Self::StorageContext>;
26    /// The provider of validator nodes.
27    type Network: Network<Node = Self::ValidatorNode>;
28    /// The signer used to sign on behalf of chain owners.
29    type Signer: Signer;
30    /// The wallet tracking the chains followed by the client.
31    type Wallet: Wallet;
32
33    /// The validator node type produced by the network provider.
34    type ValidatorNode: crate::node::ValidatorNode + AutoTraits + Clone;
35    /// The storage context used by the storage backend.
36    type StorageContext: linera_views::context::Context<Extra: linera_execution::ExecutionRuntimeContext>
37        + AutoTraits;
38
39    /// Returns a reference to the storage backend.
40    fn storage(&self) -> &Self::Storage;
41    /// Returns a reference to the network provider.
42    fn network(&self) -> &Self::Network;
43    /// Returns a reference to the signer.
44    fn signer(&self) -> &Self::Signer;
45    /// Returns a reference to the wallet.
46    fn wallet(&self) -> &Self::Wallet;
47}
48
49/// A concrete [`Environment`] assembled from its individual components.
50pub struct Impl<
51    Storage,
52    Network,
53    Signer = linera_base::crypto::InMemorySigner,
54    Wallet = wallet::Memory,
55> {
56    /// The persistent storage backend.
57    pub storage: Storage,
58    /// The provider of validator nodes.
59    pub network: Network,
60    /// The signer used to sign on behalf of chain owners.
61    pub signer: Signer,
62    /// The wallet tracking the chains followed by the client.
63    pub wallet: Wallet,
64}
65
66impl<St: Storage, N: Network, Si: Signer, W: Wallet> Environment for Impl<St, N, Si, W> {
67    type Storage = St;
68    type Network = N;
69    type Signer = Si;
70    type Wallet = W;
71
72    type ValidatorNode = N::Node;
73    type StorageContext = St::Context;
74
75    fn storage(&self) -> &St {
76        &self.storage
77    }
78
79    fn network(&self) -> &N {
80        &self.network
81    }
82
83    fn signer(&self) -> &Si {
84        &self.signer
85    }
86
87    fn wallet(&self) -> &W {
88        &self.wallet
89    }
90}
91
92cfg_if::cfg_if! {
93    if #[cfg(with_testing)] {
94        /// An in-memory storage backend used in tests.
95        pub type TestStorage = linera_storage::DbStorage<linera_views::memory::MemoryDatabase, linera_storage::TestClock>;
96        /// A network provider backed by in-memory test nodes.
97        pub type TestNetwork = crate::test_utils::NodeProvider<TestStorage>;
98        /// An in-memory signer used in tests.
99        pub type TestSigner = linera_base::crypto::InMemorySigner;
100        /// An in-memory wallet used in tests.
101        pub type TestWallet = crate::wallet::Memory;
102        /// A fully in-memory [`Environment`] used in tests.
103        pub type Test = Impl<TestStorage, TestNetwork, TestSigner, TestWallet>;
104    }
105}