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    type Net: LineraNet + Sized + Send + Sync + 'static;
26
27    async fn instantiate(self) -> Result<(Self::Net, ClientWrapper)>;
28}
29
30/// A running Linera net.
31#[async_trait]
32pub trait LineraNet {
33    async fn ensure_is_running(&mut self) -> Result<()>;
34
35    async fn make_client(&mut self) -> ClientWrapper;
36
37    async fn terminate(&mut self) -> Result<()>;
38}
39
40/// Network protocol in use
41#[derive(Copy, Clone)]
42pub enum Network {
43    Grpc,
44    Grpcs,
45    Tcp,
46    Udp,
47}
48
49/// Network protocol in use outside and inside a Linera net.
50#[derive(Copy, Clone)]
51pub struct NetworkConfig {
52    /// The internal network (e.g. proxy to validator)
53    pub internal: Network,
54    /// The external network (e.g. proxy to the exterior)
55    pub external: Network,
56}
57
58impl Network {
59    fn toml(&self) -> &'static str {
60        match self {
61            Network::Grpc => "{ Grpc = \"ClearText\" }",
62            Network::Grpcs => "{ Grpc = \"Tls\" }",
63            Network::Tcp => "{ Simple = \"Tcp\" }",
64            Network::Udp => "{ Simple = \"Udp\" }",
65        }
66    }
67
68    pub fn short(&self) -> &'static str {
69        match self {
70            Network::Grpc => "grpc",
71            Network::Grpcs => "grpcs",
72            Network::Tcp => "tcp",
73            Network::Udp => "udp",
74        }
75    }
76
77    pub fn drop_tls(&self) -> Self {
78        match self {
79            Network::Grpc => Network::Grpc,
80            Network::Grpcs => Network::Grpc,
81            Network::Tcp => Network::Tcp,
82            Network::Udp => Network::Udp,
83        }
84    }
85
86    pub fn localhost(&self) -> &'static str {
87        match self {
88            Network::Grpc | Network::Grpcs => "localhost",
89            Network::Tcp | Network::Udp => "127.0.0.1",
90        }
91    }
92
93    /// Returns the protocol schema to use in the node address tuple.
94    pub fn schema(&self) -> &'static str {
95        match self {
96            Network::Grpc | Network::Grpcs => "grpc",
97            Network::Tcp => "tcp",
98            Network::Udp => "udp",
99        }
100    }
101}