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    pub fn new(options: NodeOptions) -> Self {
17        Self(options)
18    }
19}
20
21impl ValidatorNodeProvider for SimpleNodeProvider {
22    type Node = SimpleClient;
23
24    fn make_node(&self, address: &str) -> Result<Self::Node, NodeError> {
25        let network = ValidatorPublicNetworkPreConfig::from_str(address).map_err(|_| {
26            NodeError::CannotResolveValidatorAddress {
27                address: address.to_string(),
28            }
29        })?;
30
31        let client = SimpleClient::new(network, self.0.send_timeout, self.0.recv_timeout);
32
33        Ok(client)
34    }
35}