Skip to main content

linera_rpc/simple/
node_provider.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use std::str::FromStr as _;
5
6use linera_core::node::{NodeError, ValidatorNodeProvider};
7
8use super::SimpleClient;
9use crate::{config::ValidatorPublicNetworkPreConfig, node_provider::NodeOptions};
10
11/// A client without an address - serves as a client factory.
12#[derive(Copy, Clone)]
13pub struct SimpleNodeProvider(NodeOptions);
14
15impl SimpleNodeProvider {
16    /// Creates a new [`SimpleNodeProvider`] with the given node options.
17    pub fn new(options: NodeOptions) -> Self {
18        Self(options)
19    }
20}
21
22impl ValidatorNodeProvider for SimpleNodeProvider {
23    type Node = SimpleClient;
24
25    fn make_node(&self, address: &str) -> Result<Self::Node, NodeError> {
26        let network = ValidatorPublicNetworkPreConfig::from_str(address).map_err(|_| {
27            NodeError::CannotResolveValidatorAddress {
28                address: address.to_string(),
29            }
30        })?;
31
32        let client = SimpleClient::new(network, self.0.send_timeout, self.0.recv_timeout);
33
34        Ok(client)
35    }
36}