forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.rs
123 lines (113 loc) · 3.74 KB
/
config.rs
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
121
122
123
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use crate::rpc_gateway_client::RpcGatewayClient;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::{
collections::BTreeMap,
fmt::{Display, Formatter, Write},
path::PathBuf,
time::Duration,
};
use sui_config::Config;
use sui_config::ValidatorInfo;
use sui_core::{
authority_client::NetworkAuthorityClient,
gateway_state::{GatewayClient, GatewayState},
};
use sui_types::{
base_types::AuthorityName,
committee::{Committee, EpochId},
};
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum GatewayType {
Embedded(GatewayConfig),
RPC(String),
}
impl Display for GatewayType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut writer = String::new();
match self {
GatewayType::Embedded(config) => {
writeln!(writer, "Gateway Type : Embedded")?;
writeln!(
writer,
"Gateway state DB folder path : {:?}",
config.db_folder_path
)?;
let authorities = config
.validator_set
.iter()
.map(|info| info.network_address());
writeln!(
writer,
"Authorities : {:?}",
authorities.collect::<Vec<_>>()
)?;
}
GatewayType::RPC(url) => {
writeln!(writer, "Gateway Type : JSON-RPC")?;
writeln!(writer, "Gateway URL : {}", url)?;
}
}
write!(f, "{}", writer)
}
}
impl GatewayType {
pub fn init(&self) -> Result<GatewayClient, anyhow::Error> {
Ok(match self {
GatewayType::Embedded(config) => {
let path = config.db_folder_path.clone();
let committee = config.make_committee();
let authority_clients = config.make_authority_clients();
Arc::new(GatewayState::new(path, committee, authority_clients)?)
}
GatewayType::RPC(url) => Arc::new(RpcGatewayClient::new(url.clone())?),
})
}
}
#[derive(Serialize, Deserialize)]
pub struct GatewayConfig {
pub epoch: EpochId,
pub validator_set: Vec<ValidatorInfo>,
pub send_timeout: Duration,
pub recv_timeout: Duration,
pub buffer_size: usize,
pub db_folder_path: PathBuf,
}
impl Config for GatewayConfig {}
impl GatewayConfig {
pub fn make_committee(&self) -> Committee {
let voting_rights = self
.validator_set
.iter()
.map(|validator| (validator.public_key(), validator.stake()))
.collect();
Committee::new(self.epoch, voting_rights)
}
pub fn make_authority_clients(&self) -> BTreeMap<AuthorityName, NetworkAuthorityClient> {
let mut authority_clients = BTreeMap::new();
let mut config = mysten_network::config::Config::new();
config.connect_timeout = Some(self.send_timeout);
config.request_timeout = Some(self.recv_timeout);
for authority in &self.validator_set {
let channel = config.connect_lazy(authority.network_address()).unwrap();
let client = NetworkAuthorityClient::new(channel);
authority_clients.insert(authority.public_key(), client);
}
authority_clients
}
}
impl Default for GatewayConfig {
fn default() -> Self {
Self {
epoch: 0,
validator_set: vec![],
send_timeout: Duration::from_micros(4000000),
recv_timeout: Duration::from_micros(4000000),
buffer_size: 650000,
db_folder_path: Default::default(),
}
}
}