1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

//! Helper module to call the binaries of `linera-service` with appropriate command-line
//! arguments.

#[cfg(feature = "kubernetes")]
/// How to run Docker operations
pub mod docker;

#[cfg(feature = "kubernetes")]
/// How to run helmfile operations
mod helmfile;
#[cfg(feature = "kubernetes")]
/// How to run kind operations
mod kind;
#[cfg(feature = "kubernetes")]
/// How to run `kubectl` operations
mod kubectl;
#[cfg(feature = "kubernetes")]
/// How to run Linera validators locally as a Kubernetes deployment.
pub mod local_kubernetes_net;
/// How to run Linera validators locally as native processes.
pub mod local_net;
#[cfg(all(with_testing, feature = "remote-net"))]
/// How to connect to running GCP devnet.
pub mod remote_net;
#[cfg(feature = "kubernetes")]
/// Utility functions for the wrappers
mod util;
/// How to run a Linera wallet and its GraphQL service.
mod wallet;

use anyhow::Result;
use async_trait::async_trait;
pub use linera_faucet_client::Faucet;
pub use wallet::{
    ApplicationWrapper, ClientWrapper, FaucetOption, FaucetService, NodeService, OnClientDrop,
};

/// The information needed to start a Linera net of a particular kind.
#[async_trait]
pub trait LineraNetConfig {
    type Net: LineraNet + Sized + Send + Sync + 'static;

    async fn instantiate(self) -> Result<(Self::Net, ClientWrapper)>;
}

/// A running Linera net.
#[async_trait]
pub trait LineraNet {
    async fn ensure_is_running(&mut self) -> Result<()>;

    async fn make_client(&mut self) -> ClientWrapper;

    async fn terminate(&mut self) -> Result<()>;
}

/// Network protocol in use
#[derive(Copy, Clone)]
pub enum Network {
    Grpc,
    Grpcs,
    Tcp,
    Udp,
}

/// Network protocol in use outside and inside a Linera net.
#[derive(Copy, Clone)]
pub struct NetworkConfig {
    /// The internal network (e.g. proxy to validator)
    pub internal: Network,
    /// The external network (e.g. proxy to the exterior)
    pub external: Network,
}

impl Network {
    fn toml(&self) -> &'static str {
        match self {
            Network::Grpc => "{ Grpc = \"ClearText\" }",
            Network::Grpcs => "{ Grpc = \"Tls\" }",
            Network::Tcp => "{ Simple = \"Tcp\" }",
            Network::Udp => "{ Simple = \"Udp\" }",
        }
    }

    pub fn short(&self) -> &'static str {
        match self {
            Network::Grpc => "grpc",
            Network::Grpcs => "grpcs",
            Network::Tcp => "tcp",
            Network::Udp => "udp",
        }
    }

    pub fn drop_tls(&self) -> Self {
        match self {
            Network::Grpc => Network::Grpc,
            Network::Grpcs => Network::Grpc,
            Network::Tcp => Network::Tcp,
            Network::Udp => Network::Udp,
        }
    }

    pub fn localhost(&self) -> &'static str {
        match self {
            Network::Grpc | Network::Grpcs => "localhost",
            Network::Tcp | Network::Udp => "127.0.0.1",
        }
    }

    /// Returns the protocol schema to use in the node address tuple.
    pub fn schema(&self) -> &'static str {
        match self {
            Network::Grpc | Network::Grpcs => "grpc",
            Network::Tcp => "tcp",
            Network::Udp => "udp",
        }
    }
}