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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use enum_dispatch::enum_dispatch;
use std::collections::{BTreeMap, HashMap};

use crate::base_types::{AuthorityName, EpochId, SuiAddress};
use crate::committee::{Committee, CommitteeWithNetworkMetadata, NetworkMetadata, StakeUnit};
use crate::multiaddr::Multiaddr;
use anemo::types::{PeerAffinity, PeerInfo};
use anemo::PeerId;
use consensus_config::{
    Authority, AuthorityPublicKey, Committee as ConsensusCommittee, NetworkPublicKey,
    ProtocolPublicKey,
};
use narwhal_config::{Committee as NarwhalCommittee, CommitteeBuilder, WorkerCache, WorkerIndex};
use serde::{Deserialize, Serialize};
use sui_protocol_config::ProtocolVersion;
use tracing::{error, warn};

#[enum_dispatch]
pub trait EpochStartSystemStateTrait {
    fn epoch(&self) -> EpochId;
    fn protocol_version(&self) -> ProtocolVersion;
    fn reference_gas_price(&self) -> u64;
    fn safe_mode(&self) -> bool;
    fn epoch_start_timestamp_ms(&self) -> u64;
    fn epoch_duration_ms(&self) -> u64;
    fn get_validator_addresses(&self) -> Vec<SuiAddress>;
    fn get_sui_committee(&self) -> Committee;
    fn get_sui_committee_with_network_metadata(&self) -> CommitteeWithNetworkMetadata;
    fn get_narwhal_committee(&self) -> NarwhalCommittee;
    fn get_mysticeti_committee(&self) -> ConsensusCommittee;
    fn get_validator_as_p2p_peers(&self, excluding_self: AuthorityName) -> Vec<PeerInfo>;
    fn get_authority_names_to_peer_ids(&self) -> HashMap<AuthorityName, PeerId>;
    fn get_authority_names_to_hostnames(&self) -> HashMap<AuthorityName, String>;
    fn get_narwhal_worker_cache(&self, transactions_address: &Multiaddr) -> WorkerCache;
}

/// This type captures the minimum amount of information from SuiSystemState needed by a validator
/// to run the protocol. This allows us to decouple from the actual SuiSystemState type, and hence
/// do not need to evolve it when we upgrade the SuiSystemState type.
/// Evolving EpochStartSystemState is also a lot easier in that we could add optional fields
/// and fill them with None for older versions. When we absolutely must delete fields, we could
/// also add new db tables to store the new version. This is OK because we only store one copy of
/// this as part of EpochStartConfiguration for the most recent epoch in the db.
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
#[enum_dispatch(EpochStartSystemStateTrait)]
pub enum EpochStartSystemState {
    V1(EpochStartSystemStateV1),
}

impl EpochStartSystemState {
    pub fn new_v1(
        epoch: EpochId,
        protocol_version: u64,
        reference_gas_price: u64,
        safe_mode: bool,
        epoch_start_timestamp_ms: u64,
        epoch_duration_ms: u64,
        active_validators: Vec<EpochStartValidatorInfoV1>,
    ) -> Self {
        Self::V1(EpochStartSystemStateV1 {
            epoch,
            protocol_version,
            reference_gas_price,
            safe_mode,
            epoch_start_timestamp_ms,
            epoch_duration_ms,
            active_validators,
        })
    }

    pub fn new_for_testing_with_epoch(epoch: EpochId) -> Self {
        Self::V1(EpochStartSystemStateV1::new_for_testing_with_epoch(epoch))
    }
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct EpochStartSystemStateV1 {
    epoch: EpochId,
    protocol_version: u64,
    reference_gas_price: u64,
    safe_mode: bool,
    epoch_start_timestamp_ms: u64,
    epoch_duration_ms: u64,
    active_validators: Vec<EpochStartValidatorInfoV1>,
}

impl EpochStartSystemStateV1 {
    pub fn new_for_testing() -> Self {
        Self::new_for_testing_with_epoch(0)
    }

    pub fn new_for_testing_with_epoch(epoch: EpochId) -> Self {
        Self {
            epoch,
            protocol_version: ProtocolVersion::MAX.as_u64(),
            reference_gas_price: crate::transaction::DEFAULT_VALIDATOR_GAS_PRICE,
            safe_mode: false,
            epoch_start_timestamp_ms: 0,
            epoch_duration_ms: 1000,
            active_validators: vec![],
        }
    }
}

impl EpochStartSystemStateTrait for EpochStartSystemStateV1 {
    fn epoch(&self) -> EpochId {
        self.epoch
    }

    fn protocol_version(&self) -> ProtocolVersion {
        ProtocolVersion::new(self.protocol_version)
    }

    fn reference_gas_price(&self) -> u64 {
        self.reference_gas_price
    }

    fn safe_mode(&self) -> bool {
        self.safe_mode
    }

    fn epoch_start_timestamp_ms(&self) -> u64 {
        self.epoch_start_timestamp_ms
    }

    fn epoch_duration_ms(&self) -> u64 {
        self.epoch_duration_ms
    }

    fn get_validator_addresses(&self) -> Vec<SuiAddress> {
        self.active_validators
            .iter()
            .map(|validator| validator.sui_address)
            .collect()
    }

    fn get_sui_committee_with_network_metadata(&self) -> CommitteeWithNetworkMetadata {
        let (voting_rights, network_metadata) = self
            .active_validators
            .iter()
            .map(|validator| {
                (
                    (validator.authority_name(), validator.voting_power),
                    (
                        validator.authority_name(),
                        NetworkMetadata {
                            network_address: validator.sui_net_address.clone(),
                            narwhal_primary_address: validator.narwhal_primary_address.clone(),
                        },
                    ),
                )
            })
            .unzip();

        CommitteeWithNetworkMetadata {
            committee: Committee::new(self.epoch, voting_rights),
            network_metadata,
        }
    }

    fn get_sui_committee(&self) -> Committee {
        let voting_rights = self
            .active_validators
            .iter()
            .map(|validator| (validator.authority_name(), validator.voting_power))
            .collect();
        Committee::new(self.epoch, voting_rights)
    }

    fn get_narwhal_committee(&self) -> NarwhalCommittee {
        let mut committee_builder = CommitteeBuilder::new(self.epoch as narwhal_config::Epoch);

        for validator in self.active_validators.iter() {
            committee_builder = committee_builder.add_authority(
                validator.protocol_pubkey.clone(),
                validator.voting_power as narwhal_config::Stake,
                validator.narwhal_primary_address.clone(),
                validator.narwhal_network_pubkey.clone(),
                validator.hostname.clone(),
            );
        }

        committee_builder.build()
    }

    fn get_mysticeti_committee(&self) -> ConsensusCommittee {
        let mut authorities = vec![];
        for validator in self.active_validators.iter() {
            authorities.push(Authority {
                stake: validator.voting_power as consensus_config::Stake,
                // TODO(mysticeti): Add EpochStartValidatorInfoV2 with new field for mysticeti address.
                address: validator.narwhal_primary_address.clone(),
                hostname: validator.hostname.clone(),
                authority_key: AuthorityPublicKey::new(validator.protocol_pubkey.clone()),
                protocol_key: ProtocolPublicKey::new(validator.narwhal_worker_pubkey.clone()),
                network_key: NetworkPublicKey::new(validator.narwhal_network_pubkey.clone()),
            });
        }

        // Sort the authorities by their protocol (public) key in ascending order, same as the order
        // in the Sui committee returned from get_sui_committee().
        authorities.sort_by(|a1, a2| a1.authority_key.cmp(&a2.authority_key));

        for ((i, mysticeti_authority), sui_authority_name) in authorities
            .iter()
            .enumerate()
            .zip(self.get_sui_committee().names())
        {
            if sui_authority_name.0 != mysticeti_authority.authority_key.to_bytes() {
                error!(
                    "Mismatched authority order between Sui and Mysticeti! Index {}, Mysticeti authority {:?}\nSui authority name {}",
                    i, mysticeti_authority, sui_authority_name
                );
            }
        }

        ConsensusCommittee::new(self.epoch as consensus_config::Epoch, authorities)
    }

    fn get_validator_as_p2p_peers(&self, excluding_self: AuthorityName) -> Vec<PeerInfo> {
        self.active_validators
            .iter()
            .filter(|validator| validator.authority_name() != excluding_self)
            .map(|validator| {
                let address = validator
                    .p2p_address
                    .to_anemo_address()
                    .into_iter()
                    .collect::<Vec<_>>();
                let peer_id = PeerId(validator.narwhal_network_pubkey.0.to_bytes());
                if address.is_empty() {
                    warn!(
                        ?peer_id,
                        "Peer has invalid p2p address: {}", &validator.p2p_address
                    );
                }
                PeerInfo {
                    peer_id,
                    affinity: PeerAffinity::High,
                    address,
                }
            })
            .collect()
    }

    fn get_authority_names_to_peer_ids(&self) -> HashMap<AuthorityName, PeerId> {
        self.active_validators
            .iter()
            .map(|validator| {
                let name = validator.authority_name();
                let peer_id = PeerId(validator.narwhal_network_pubkey.0.to_bytes());

                (name, peer_id)
            })
            .collect()
    }

    fn get_authority_names_to_hostnames(&self) -> HashMap<AuthorityName, String> {
        self.active_validators
            .iter()
            .map(|validator| {
                let name = validator.authority_name();
                let hostname = validator.hostname.clone();

                (name, hostname)
            })
            .collect()
    }

    #[allow(clippy::mutable_key_type)]
    fn get_narwhal_worker_cache(&self, transactions_address: &Multiaddr) -> WorkerCache {
        let workers: BTreeMap<narwhal_crypto::PublicKey, WorkerIndex> = self
            .active_validators
            .iter()
            .map(|validator| {
                let workers = [(
                    0,
                    narwhal_config::WorkerInfo {
                        name: validator.narwhal_worker_pubkey.clone(),
                        transactions: transactions_address.clone(),
                        worker_address: validator.narwhal_worker_address.clone(),
                    },
                )]
                .into_iter()
                .collect();
                let worker_index = WorkerIndex(workers);

                (validator.protocol_pubkey.clone(), worker_index)
            })
            .collect();
        WorkerCache {
            workers,
            epoch: self.epoch,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct EpochStartValidatorInfoV1 {
    pub sui_address: SuiAddress,
    pub protocol_pubkey: narwhal_crypto::PublicKey,
    pub narwhal_network_pubkey: narwhal_crypto::NetworkPublicKey,
    pub narwhal_worker_pubkey: narwhal_crypto::NetworkPublicKey,
    pub sui_net_address: Multiaddr,
    pub p2p_address: Multiaddr,
    pub narwhal_primary_address: Multiaddr,
    pub narwhal_worker_address: Multiaddr,
    pub voting_power: StakeUnit,
    pub hostname: String,
}

impl EpochStartValidatorInfoV1 {
    pub fn authority_name(&self) -> AuthorityName {
        (&self.protocol_pubkey).into()
    }
}

#[cfg(test)]
mod test {
    use crate::base_types::SuiAddress;
    use crate::committee::CommitteeTrait;
    use crate::crypto::{get_key_pair, AuthorityKeyPair};
    use crate::sui_system_state::epoch_start_sui_system_state::{
        EpochStartSystemStateTrait, EpochStartSystemStateV1, EpochStartValidatorInfoV1,
    };
    use fastcrypto::traits::KeyPair;
    use mysten_network::Multiaddr;
    use narwhal_crypto::NetworkKeyPair;
    use rand::thread_rng;
    use sui_protocol_config::ProtocolVersion;

    #[test]
    fn test_sui_and_mysticeti_committee_are_same() {
        // GIVEN
        let mut active_validators = vec![];

        for i in 0..10 {
            let (sui_address, protocol_key): (SuiAddress, AuthorityKeyPair) = get_key_pair();
            let narwhal_network_key = NetworkKeyPair::generate(&mut thread_rng());

            active_validators.push(EpochStartValidatorInfoV1 {
                sui_address,
                protocol_pubkey: protocol_key.public().clone(),
                narwhal_network_pubkey: narwhal_network_key.public().clone(),
                narwhal_worker_pubkey: narwhal_network_key.public().clone(),
                sui_net_address: Multiaddr::empty(),
                p2p_address: Multiaddr::empty(),
                narwhal_primary_address: Multiaddr::empty(),
                narwhal_worker_address: Multiaddr::empty(),
                voting_power: 1_000,
                hostname: format!("host-{i}").to_string(),
            })
        }

        let state = EpochStartSystemStateV1 {
            epoch: 10,
            protocol_version: ProtocolVersion::MAX.as_u64(),
            reference_gas_price: 0,
            safe_mode: false,
            epoch_start_timestamp_ms: 0,
            epoch_duration_ms: 0,
            active_validators,
        };

        // WHEN
        let sui_committee = state.get_sui_committee();
        let mysticeti_committee = state.get_mysticeti_committee();

        // THEN
        // assert the validators details
        assert_eq!(sui_committee.num_members(), 10);
        assert_eq!(sui_committee.num_members(), mysticeti_committee.size());
        assert_eq!(
            sui_committee.validity_threshold(),
            mysticeti_committee.validity_threshold()
        );
        assert_eq!(
            sui_committee.quorum_threshold(),
            mysticeti_committee.quorum_threshold()
        );
        assert_eq!(state.epoch, mysticeti_committee.epoch());

        for (authority_index, mysticeti_authority) in mysticeti_committee.authorities() {
            let sui_authority_name = sui_committee
                .authority_by_index(authority_index.value() as u32)
                .unwrap();

            assert_eq!(
                mysticeti_authority.authority_key.to_bytes(),
                sui_authority_name.0,
                "Mysten & SUI committee member of same index correspond to different public key"
            );
            assert_eq!(
                mysticeti_authority.stake,
                sui_committee.weight(sui_authority_name),
                "Mysten & SUI committee member stake differs"
            );
        }
    }
}