referrerpolicy=no-referrer-when-downgrade

sc_consensus_beefy/communication/
peers.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Logic for keeping track of BEEFY peers.
20
21use sc_network::ReputationChange;
22use sc_network_types::PeerId;
23use sp_runtime::traits::{Block, NumberFor, Zero};
24use std::collections::{HashMap, VecDeque};
25
26/// Report specifying a reputation change for a given peer.
27#[derive(Debug, PartialEq)]
28pub struct PeerReport {
29	pub who: PeerId,
30	pub cost_benefit: ReputationChange,
31}
32
33struct PeerData<B: Block> {
34	last_voted_on: NumberFor<B>,
35}
36
37impl<B: Block> Default for PeerData<B> {
38	fn default() -> Self {
39		PeerData { last_voted_on: Zero::zero() }
40	}
41}
42
43/// Keep a simple map of connected peers
44/// and the most recent voting round they participated in.
45pub struct KnownPeers<B: Block> {
46	live: HashMap<PeerId, PeerData<B>>,
47}
48
49impl<B: Block> KnownPeers<B> {
50	pub fn new() -> Self {
51		Self { live: HashMap::new() }
52	}
53
54	/// Note vote round number for `peer`.
55	pub fn note_vote_for(&mut self, peer: PeerId, round: NumberFor<B>) {
56		let data = self.live.entry(peer).or_default();
57		data.last_voted_on = round.max(data.last_voted_on);
58	}
59
60	/// Remove connected `peer`.
61	pub fn remove(&mut self, peer: &PeerId) {
62		self.live.remove(peer);
63	}
64
65	/// Return _filtered and cloned_ list of peers that have voted on higher than `block`.
66	pub fn further_than(&self, block: NumberFor<B>) -> VecDeque<PeerId> {
67		self.live
68			.iter()
69			.filter_map(|(k, v)| (v.last_voted_on > block).then_some(k))
70			.cloned()
71			.collect()
72	}
73
74	/// Answer whether `peer` is part of `KnownPeers` set.
75	pub fn contains(&self, peer: &PeerId) -> bool {
76		self.live.contains_key(peer)
77	}
78
79	/// Number of peers in the set.
80	pub fn len(&self) -> usize {
81		self.live.len()
82	}
83}
84
85#[cfg(test)]
86mod tests {
87	use super::*;
88
89	#[test]
90	fn should_track_known_peers_progress() {
91		let (alice, bob, charlie) = (PeerId::random(), PeerId::random(), PeerId::random());
92		let mut peers = KnownPeers::<sc_network_test::Block>::new();
93		assert!(peers.live.is_empty());
94
95		// 'Tracked' Bob seen voting for 5.
96		peers.note_vote_for(bob, 5);
97		// Previously unseen Charlie now seen voting for 10.
98		peers.note_vote_for(charlie, 10);
99
100		assert_eq!(peers.live.len(), 2);
101		assert!(!peers.contains(&alice));
102		assert!(peers.contains(&bob));
103		assert!(peers.contains(&charlie));
104
105		// Get peers at block > 4
106		let further_than_4 = peers.further_than(4);
107		// Should be Bob and Charlie
108		assert_eq!(further_than_4.len(), 2);
109		assert!(further_than_4.contains(&bob));
110		assert!(further_than_4.contains(&charlie));
111
112		// 'Tracked' Alice seen voting for 10.
113		peers.note_vote_for(alice, 10);
114
115		// Get peers at block > 9
116		let further_than_9 = peers.further_than(9);
117		// Should be Charlie and Alice
118		assert_eq!(further_than_9.len(), 2);
119		assert!(further_than_9.contains(&charlie));
120		assert!(further_than_9.contains(&alice));
121
122		// Remove Alice
123		peers.remove(&alice);
124		assert_eq!(peers.live.len(), 2);
125		assert!(!peers.contains(&alice));
126
127		// Get peers at block >= 9
128		let further_than_9 = peers.further_than(9);
129		// Now should be just Charlie
130		assert_eq!(further_than_9.len(), 1);
131		assert!(further_than_9.contains(&charlie));
132	}
133}