Skip to main content

linera_service/cli_wrappers/
mod.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Helper module to call the binaries of `linera-service` with appropriate command-line
5//! arguments.
6
7/// How to run Linera validators locally as native processes.
8pub mod local_net;
9#[cfg(all(with_testing, feature = "remote-net"))]
10/// How to connect to running GCP devnet.
11pub mod remote_net;
12/// How to run a Linera wallet and its GraphQL service.
13mod wallet;
14
15use anyhow::Result;
16use async_trait::async_trait;
17pub use linera_faucet_client::Faucet;
18#[cfg(with_testing)]
19pub use wallet::NotificationsExt;
20pub use wallet::{ApplicationWrapper, ClientWrapper, FaucetService, NodeService, OnClientDrop};
21
22/// The information needed to start a Linera net of a particular kind.
23#[async_trait]
24pub trait LineraNetConfig {
25    /// The type of Linera net produced by this configuration.
26    type Net: LineraNet + Sized + Send + Sync + 'static;
27
28    /// Starts the Linera net and returns it together with a client to interact with it.
29    async fn instantiate(self) -> Result<(Self::Net, ClientWrapper)>;
30}
31
32/// A running Linera net.
33#[async_trait]
34pub trait LineraNet {
35    /// Checks that the net is still running and returns an error otherwise.
36    async fn ensure_is_running(&mut self) -> Result<()>;
37
38    /// Creates a new client to interact with the net.
39    async fn make_client(&mut self) -> ClientWrapper;
40
41    /// Shuts down the net.
42    async fn terminate(&mut self) -> Result<()>;
43}
44
45/// Network protocol in use
46#[derive(Copy, Clone)]
47pub enum Network {
48    /// gRPC over cleartext (no TLS).
49    Grpc,
50    /// gRPC secured with TLS.
51    Grpcs,
52    /// Simple transport over TCP.
53    Tcp,
54    /// Simple transport over UDP.
55    Udp,
56}
57
58/// Network protocol in use outside and inside a Linera net.
59#[derive(Copy, Clone)]
60pub struct NetworkConfig {
61    /// The internal network (e.g. proxy to validator)
62    pub internal: Network,
63    /// The external network (e.g. proxy to the exterior)
64    pub external: Network,
65}
66
67impl Network {
68    fn toml(&self) -> &'static str {
69        match self {
70            Network::Grpc => "{ Grpc = \"ClearText\" }",
71            Network::Grpcs => "{ Grpc = \"Tls\" }",
72            Network::Tcp => "{ Simple = \"Tcp\" }",
73            Network::Udp => "{ Simple = \"Udp\" }",
74        }
75    }
76
77    /// Returns a short lowercase name for the network protocol.
78    pub fn short(&self) -> &'static str {
79        match self {
80            Network::Grpc => "grpc",
81            Network::Grpcs => "grpcs",
82            Network::Tcp => "tcp",
83            Network::Udp => "udp",
84        }
85    }
86
87    /// Returns the equivalent network protocol without TLS.
88    pub fn drop_tls(&self) -> Self {
89        match self {
90            Network::Grpc => Network::Grpc,
91            Network::Grpcs => Network::Grpc,
92            Network::Tcp => Network::Tcp,
93            Network::Udp => Network::Udp,
94        }
95    }
96
97    /// Returns the localhost address to use for this network protocol.
98    pub fn localhost(&self) -> &'static str {
99        match self {
100            Network::Grpc | Network::Grpcs => "localhost",
101            Network::Tcp | Network::Udp => "127.0.0.1",
102        }
103    }
104
105    /// Returns the protocol schema to use in the node address tuple.
106    pub fn schema(&self) -> &'static str {
107        match self {
108            Network::Grpc | Network::Grpcs => "grpc",
109            Network::Tcp => "tcp",
110            Network::Udp => "udp",
111        }
112    }
113}