diem_config/config/upstream_config.rs
1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4// Copyright 2021 Conflux Foundation. All rights reserved.
5// Conflux is free software and distributed under GNU General Public License.
6// See http://www.gnu.org/licenses/
7
8use crate::network_id::{NetworkId, NodeNetworkId};
9use diem_types::PeerId;
10use serde::{Deserialize, Serialize};
11use short_hex_str::AsShortHexStr;
12use std::fmt;
13
14/// If a node considers a network 'upstream', the node will broadcast
15/// transactions (via mempool) to and send sync requests (via state sync) to all
16/// its peers in this network. For validators, it is unnecessary to declare
17/// their validator network as their upstream network in this config Otherwise,
18/// any non-validator network not declared here will be treated as a downstream
19/// network (i.e. transactions will not be broadcast to and sync requests will
20/// not be sent to such networks)
21#[derive(Clone, Default, Debug, Deserialize, PartialEq, Serialize)]
22#[serde(default, deny_unknown_fields)]
23pub struct UpstreamConfig {
24 // list of upstream networks for this node, ordered by preference
25 // A validator's primary upstream network is their validator network, and
26 // for a FN, it is the first network defined here. If the primary
27 // upstream network goes down, the node will fall back to the networks
28 // specified here, in this order
29 pub networks: Vec<NetworkId>,
30}
31
32impl UpstreamConfig {
33 /// Returns the upstream network preference of a network according to this
34 /// config if network is not an upstream network, returns `None`
35 /// else, returns `Some<ranking>`, where `ranking` is zero-indexed and zero
36 /// represents the highest preference
37 pub fn get_upstream_preference(&self, network: NetworkId) -> Option<usize> {
38 if network == NetworkId::Validator {
39 // validator network is always highest priority
40 Some(0)
41 } else {
42 self.networks
43 .iter()
44 .position(|upstream_network| upstream_network == &network)
45 }
46 }
47
48 /// Returns the number of upstream networks possible for a node with this
49 /// config
50 pub fn upstream_count(&self) -> usize {
51 // `self.networks.len()` is not enough because for validators, this is
52 // empty but their unspecified validator network is considered
53 // upstream by default
54 std::cmp::max(1, self.networks.len())
55 }
56}
57
58#[derive(Clone, Deserialize, Eq, Hash, PartialEq, Serialize)]
59/// Identifier of a node, represented as (network_id, peer_id)
60pub struct PeerNetworkId(pub NodeNetworkId, pub PeerId);
61
62impl PeerNetworkId {
63 pub fn network_id(&self) -> NodeNetworkId { self.0.clone() }
64
65 pub fn raw_network_id(&self) -> NetworkId { self.0.network_id() }
66
67 pub fn peer_id(&self) -> PeerId { self.1 }
68
69 #[cfg(any(test, feature = "fuzzing"))]
70 pub fn random() -> Self {
71 Self(
72 NodeNetworkId::new(NetworkId::default(), 0),
73 PeerId::random(),
74 )
75 }
76
77 #[cfg(any(test, feature = "fuzzing"))]
78 pub fn random_validator() -> Self {
79 Self(
80 NodeNetworkId::new(NetworkId::Validator, 0),
81 PeerId::random(),
82 )
83 }
84}
85
86impl fmt::Debug for PeerNetworkId {
87 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88 write!(f, "{}", self)
89 }
90}
91
92impl fmt::Display for PeerNetworkId {
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94 write!(
95 f,
96 "PeerId:{}, NodeNetworkId:({})",
97 self.peer_id().short_str(),
98 self.raw_network_id()
99 )
100 }
101}