1use std::{num::NonZeroUsize, time::Duration};
2
3use hotshot_types::{
4 network::{
5 BuilderType, CombinedNetworkConfig, Libp2pConfig, NetworkConfig, RandomBuilderConfig,
6 },
7 HotShotConfig, PeerConfig, ValidatorConfig,
8};
9use serde::{Deserialize, Serialize};
10use tide_disco::Url;
11use vec1::Vec1;
12
13use crate::{PubKey, SeqTypes};
14
15#[derive(Clone, Debug, Deserialize, Serialize)]
18pub struct PublicValidatorConfig {
19 public_key: PubKey,
20 stake_value: u64,
21 is_da: bool,
22 private_key: String,
23 state_public_key: String,
24 state_key_pair: String,
25}
26
27impl From<ValidatorConfig<SeqTypes>> for PublicValidatorConfig {
28 fn from(v: ValidatorConfig<SeqTypes>) -> Self {
29 let ValidatorConfig::<SeqTypes> {
30 public_key,
31 private_key: _,
32 stake_value,
33 state_public_key,
34 state_private_key: _,
35 is_da,
36 } = v;
37
38 Self {
39 public_key,
40 stake_value: stake_value.to::<u64>(),
41 is_da,
42 state_public_key: state_public_key.to_string(),
43 private_key: "*****".into(),
44 state_key_pair: "*****".into(),
45 }
46 }
47}
48
49#[derive(Clone, Debug, Deserialize, Serialize)]
53pub struct PublicHotShotConfig {
54 start_threshold: (u64, u64),
55 num_nodes_with_stake: NonZeroUsize,
56 known_nodes_with_stake: Vec<PeerConfig<SeqTypes>>,
57 known_da_nodes: Vec<PeerConfig<SeqTypes>>,
58 da_staked_committee_size: usize,
59 fixed_leader_for_gpuvid: usize,
60 next_view_timeout: u64,
61 view_sync_timeout: Duration,
62 num_bootstrap: usize,
63 builder_timeout: Duration,
64 data_request_delay: Duration,
65 builder_urls: Vec1<Url>,
66 start_proposing_view: u64,
67 stop_proposing_view: u64,
68 start_voting_view: u64,
69 stop_voting_view: u64,
70 start_proposing_time: u64,
71 stop_proposing_time: u64,
72 start_voting_time: u64,
73 stop_voting_time: u64,
74 epoch_height: u64,
75 epoch_start_block: u64,
76}
77
78impl From<HotShotConfig<SeqTypes>> for PublicHotShotConfig {
79 fn from(v: HotShotConfig<SeqTypes>) -> Self {
80 let HotShotConfig::<SeqTypes> {
84 start_threshold,
85 num_nodes_with_stake,
86 known_nodes_with_stake,
87 known_da_nodes,
88 da_staked_committee_size,
89 fixed_leader_for_gpuvid,
90 next_view_timeout,
91 view_sync_timeout,
92 num_bootstrap,
93 builder_timeout,
94 data_request_delay,
95 builder_urls,
96 start_proposing_view,
97 stop_proposing_view,
98 start_voting_view,
99 stop_voting_view,
100 start_proposing_time,
101 stop_proposing_time,
102 start_voting_time,
103 stop_voting_time,
104 epoch_height,
105 epoch_start_block,
106 } = v;
107
108 Self {
109 start_threshold,
110 num_nodes_with_stake,
111 known_nodes_with_stake,
112 known_da_nodes,
113 da_staked_committee_size,
114 fixed_leader_for_gpuvid,
115 next_view_timeout,
116 view_sync_timeout,
117 num_bootstrap,
118 builder_timeout,
119 data_request_delay,
120 builder_urls,
121 start_proposing_view,
122 stop_proposing_view,
123 start_voting_view,
124 stop_voting_view,
125 start_proposing_time,
126 stop_proposing_time,
127 start_voting_time,
128 stop_voting_time,
129 epoch_height,
130 epoch_start_block,
131 }
132 }
133}
134
135impl PublicHotShotConfig {
136 pub fn into_hotshot_config(self) -> HotShotConfig<SeqTypes> {
137 HotShotConfig {
138 start_threshold: self.start_threshold,
139 num_nodes_with_stake: self.num_nodes_with_stake,
140 known_nodes_with_stake: self.known_nodes_with_stake,
141 known_da_nodes: self.known_da_nodes,
142 da_staked_committee_size: self.da_staked_committee_size,
143 fixed_leader_for_gpuvid: self.fixed_leader_for_gpuvid,
144 next_view_timeout: self.next_view_timeout,
145 view_sync_timeout: self.view_sync_timeout,
146 num_bootstrap: self.num_bootstrap,
147 builder_timeout: self.builder_timeout,
148 data_request_delay: self.data_request_delay,
149 builder_urls: self.builder_urls,
150 start_proposing_view: self.start_proposing_view,
151 stop_proposing_view: self.stop_proposing_view,
152 start_voting_view: self.start_voting_view,
153 stop_voting_view: self.stop_voting_view,
154 start_proposing_time: self.start_proposing_time,
155 stop_proposing_time: self.stop_proposing_time,
156 start_voting_time: self.start_voting_time,
157 stop_voting_time: self.stop_voting_time,
158 epoch_height: self.epoch_height,
159 epoch_start_block: self.epoch_start_block,
160 }
161 }
162
163 pub fn known_nodes_with_stake(&self) -> Vec<PeerConfig<SeqTypes>> {
164 self.known_nodes_with_stake.clone()
165 }
166
167 pub fn known_da_nodes(&self) -> Vec<PeerConfig<SeqTypes>> {
168 self.known_da_nodes.clone()
169 }
170 pub fn blocks_per_epoch(&self) -> u64 {
171 self.epoch_height
172 }
173 pub fn epoch_start_block(&self) -> u64 {
174 self.epoch_start_block
175 }
176}
177
178#[derive(Clone, Debug, Deserialize, Serialize)]
179pub struct PublicNetworkConfig {
180 rounds: usize,
181 indexed_da: bool,
182 transactions_per_round: usize,
183 manual_start_password: Option<String>,
184 num_bootrap: usize,
185 next_view_timeout: u64,
186 view_sync_timeout: Duration,
187 builder_timeout: Duration,
188 data_request_delay: Duration,
189 node_index: u64,
190 seed: [u8; 32],
191 transaction_size: usize,
192 key_type_name: String,
193 libp2p_config: Option<Libp2pConfig>,
194 config: PublicHotShotConfig,
195 cdn_marshal_address: Option<String>,
196 combined_network_config: Option<CombinedNetworkConfig>,
197 commit_sha: String,
198 builder: BuilderType,
199 random_builder: Option<RandomBuilderConfig>,
200}
201
202impl From<NetworkConfig<SeqTypes>> for PublicNetworkConfig {
203 fn from(cfg: NetworkConfig<SeqTypes>) -> Self {
204 Self {
205 rounds: cfg.rounds,
206 indexed_da: cfg.indexed_da,
207 transactions_per_round: cfg.transactions_per_round,
208 manual_start_password: Some("*****".into()),
209 num_bootrap: cfg.num_bootrap,
210 next_view_timeout: cfg.next_view_timeout,
211 view_sync_timeout: cfg.view_sync_timeout,
212 builder_timeout: cfg.builder_timeout,
213 data_request_delay: cfg.data_request_delay,
214 node_index: cfg.node_index,
215 seed: cfg.seed,
216 transaction_size: cfg.transaction_size,
217 key_type_name: cfg.key_type_name,
218 libp2p_config: cfg.libp2p_config,
219 config: cfg.config.into(),
220 cdn_marshal_address: cfg.cdn_marshal_address,
221 combined_network_config: cfg.combined_network_config,
222 commit_sha: cfg.commit_sha,
223 builder: cfg.builder,
224 random_builder: cfg.random_builder,
225 }
226 }
227}
228
229impl PublicNetworkConfig {
230 pub fn into_network_config(
231 self,
232 my_own_validator_config: ValidatorConfig<SeqTypes>,
233 ) -> anyhow::Result<NetworkConfig<SeqTypes>> {
234 let node_index = self
235 .config
236 .known_nodes_with_stake
237 .iter()
238 .position(|peer| peer.stake_table_entry.stake_key == my_own_validator_config.public_key)
239 .unwrap_or(0) as u64;
240
241 Ok(NetworkConfig {
242 rounds: self.rounds,
243 indexed_da: self.indexed_da,
244 transactions_per_round: self.transactions_per_round,
245 manual_start_password: self.manual_start_password,
246 num_bootrap: self.num_bootrap,
247 next_view_timeout: self.next_view_timeout,
248 view_sync_timeout: self.view_sync_timeout,
249 builder_timeout: self.builder_timeout,
250 data_request_delay: self.data_request_delay,
251 node_index,
252 seed: self.seed,
253 transaction_size: self.transaction_size,
254 key_type_name: self.key_type_name,
255 libp2p_config: self.libp2p_config,
256 config: self.config.into_hotshot_config(),
257 cdn_marshal_address: self.cdn_marshal_address,
258 combined_network_config: self.combined_network_config,
259 commit_sha: self.commit_sha,
260 builder: self.builder,
261 random_builder: self.random_builder,
262 public_keys: Vec::new(),
263 })
264 }
265
266 pub fn hotshot_config(&self) -> PublicHotShotConfig {
267 self.config.clone()
268 }
269}