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
/*
 * ‌
 * Hedera Rust SDK
 * ​
 * Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC
 * ​
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ‍
 */

use std::borrow::Cow;
use std::time::Duration;

use parking_lot::RwLock;
use tonic::transport::{
    Channel,
    Endpoint,
};

pub(crate) const MAINNET: &str = "mainnet-public.mirrornode.hedera.com:443";

pub(crate) const TESTNET: &str = "hcs.testnet.mirrornode.hedera.com:5600";

pub(crate) const PREVIEWNET: &str = "hcs.previewnet.mirrornode.hedera.com:5600";

pub(crate) struct MirrorNetwork {
    addresses: Vec<Cow<'static, str>>,
    channel: RwLock<Option<Channel>>,
}

impl MirrorNetwork {
    pub(super) fn mainnet() -> Self {
        Self::from_static(&[MAINNET])
    }

    pub(super) fn testnet() -> Self {
        Self::from_static(&[TESTNET])
    }

    pub(super) fn previewnet() -> Self {
        Self::from_static(&[PREVIEWNET])
    }

    pub(crate) fn from_static(network: &[&'static str]) -> Self {
        let mut addresses = Vec::with_capacity(network.len());

        for address in network {
            addresses.push(Cow::Borrowed(*address));
        }

        Self { addresses, channel: RwLock::new(None) }
    }

    pub(crate) fn channel(&self) -> Channel {
        if let Some(channel) = &*self.channel.read_recursive() {
            return channel.clone();
        }

        let mut slot = self.channel.write();

        let endpoints = self.addresses.iter().map(|address| {
            let uri = format!("tcp://{address}");
            Endpoint::from_shared(uri)
                .unwrap()
                .keep_alive_timeout(Duration::from_secs(10))
                .keep_alive_while_idle(true)
                .tcp_keepalive(Some(Duration::from_secs(10)))
                .connect_timeout(Duration::from_secs(10))
        });

        let channel = Channel::balance_list(endpoints);

        *slot = Some(channel.clone());

        channel
    }
}