linera_rpc/
node_provider.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use linera_base::time::Duration;
5use linera_core::node::{NodeError, ValidatorNodeProvider};
6
7#[cfg(with_simple_network)]
8use crate::simple::SimpleNodeProvider;
9use crate::{client::Client, grpc::GrpcNodeProvider};
10
11/// A general node provider which delegates node provision to the underlying
12/// node provider according to the `ValidatorPublicNetworkConfig`.
13#[derive(Clone)]
14pub struct NodeProvider {
15    grpc: GrpcNodeProvider,
16    #[cfg(with_simple_network)]
17    simple: SimpleNodeProvider,
18}
19
20impl NodeProvider {
21    pub fn new(options: NodeOptions) -> Self {
22        Self {
23            grpc: GrpcNodeProvider::new(options),
24            #[cfg(with_simple_network)]
25            simple: SimpleNodeProvider::new(options),
26        }
27    }
28}
29
30impl ValidatorNodeProvider for NodeProvider {
31    type Node = Client;
32
33    fn make_node(&self, address: &str) -> anyhow::Result<Self::Node, NodeError> {
34        let address = address.to_lowercase();
35
36        #[cfg(with_simple_network)]
37        if address.starts_with("tcp") || address.starts_with("udp") {
38            return Ok(Client::Simple(self.simple.make_node(&address)?));
39        }
40
41        if address.starts_with("grpc") {
42            return Ok(Client::Grpc(self.grpc.make_node(&address)?));
43        }
44
45        Err(NodeError::CannotResolveValidatorAddress { address })
46    }
47}
48
49#[derive(Copy, Clone)]
50pub struct NodeOptions {
51    pub send_timeout: Duration,
52    pub recv_timeout: Duration,
53    pub retry_delay: Duration,
54    pub max_retries: u32,
55}