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
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use futures::prelude::*;
use libp2p::core::upgrade::{InboundUpgrade, UpgradeInfo};
use std::{
	pin::Pin,
	task::{Context, Poll},
	vec,
};

// TODO: move this to libp2p => https://github.com/libp2p/rust-libp2p/issues/1445

/// Upgrade that combines multiple upgrades of the same type into one. Supports all the protocols
/// supported by either sub-upgrade.
#[derive(Debug, Clone)]
pub struct UpgradeCollec<T>(pub Vec<T>);

impl<T> From<Vec<T>> for UpgradeCollec<T> {
	fn from(list: Vec<T>) -> Self {
		Self(list)
	}
}

impl<T> FromIterator<T> for UpgradeCollec<T> {
	fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
		Self(iter.into_iter().collect())
	}
}

impl<T: UpgradeInfo> UpgradeInfo for UpgradeCollec<T> {
	type Info = ProtoNameWithUsize<T::Info>;
	type InfoIter = vec::IntoIter<Self::Info>;

	fn protocol_info(&self) -> Self::InfoIter {
		self.0
			.iter()
			.enumerate()
			.flat_map(|(n, p)| p.protocol_info().into_iter().map(move |i| ProtoNameWithUsize(i, n)))
			.collect::<Vec<_>>()
			.into_iter()
	}
}

impl<T, C> InboundUpgrade<C> for UpgradeCollec<T>
where
	T: InboundUpgrade<C>,
{
	type Output = (T::Output, usize);
	type Error = (T::Error, usize);
	type Future = FutWithUsize<T::Future>;

	fn upgrade_inbound(mut self, sock: C, info: Self::Info) -> Self::Future {
		let fut = self.0.remove(info.1).upgrade_inbound(sock, info.0);
		FutWithUsize(fut, info.1)
	}
}

/// Groups a `ProtocolName` with a `usize`.
#[derive(Debug, Clone, PartialEq)]
pub struct ProtoNameWithUsize<T>(T, usize);

impl<T: AsRef<str>> AsRef<str> for ProtoNameWithUsize<T> {
	fn as_ref(&self) -> &str {
		self.0.as_ref()
	}
}

/// Equivalent to `fut.map_ok(|v| (v, num)).map_err(|e| (e, num))`, where `fut` and `num` are
/// the two fields of this struct.
#[pin_project::pin_project]
pub struct FutWithUsize<T>(#[pin] T, usize);

impl<T: Future<Output = Result<O, E>>, O, E> Future for FutWithUsize<T> {
	type Output = Result<(O, usize), (E, usize)>;

	fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
		let this = self.project();
		match Future::poll(this.0, cx) {
			Poll::Ready(Ok(v)) => Poll::Ready(Ok((v, *this.1))),
			Poll::Ready(Err(e)) => Poll::Ready(Err((e, *this.1))),
			Poll::Pending => Poll::Pending,
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::types::ProtocolName as ProtoName;
	use libp2p::core::upgrade::UpgradeInfo;

	// TODO: move to mocks
	mockall::mock! {
		pub ProtocolUpgrade<T> {}

		impl<T: Clone + AsRef<str>> UpgradeInfo for ProtocolUpgrade<T> {
			type Info = T;
			type InfoIter = vec::IntoIter<T>;
			fn protocol_info(&self) -> vec::IntoIter<T>;
		}
	}

	#[test]
	fn protocol_info() {
		let upgrades = (1..=3)
			.map(|i| {
				let mut upgrade = MockProtocolUpgrade::<ProtoNameWithUsize<ProtoName>>::new();
				upgrade.expect_protocol_info().return_once(move || {
					vec![ProtoNameWithUsize(ProtoName::from(format!("protocol{i}")), i)].into_iter()
				});
				upgrade
			})
			.collect::<Vec<_>>();

		let upgrade: UpgradeCollec<_> = upgrades.into_iter().collect::<UpgradeCollec<_>>();
		let protos = vec![
			ProtoNameWithUsize(ProtoNameWithUsize(ProtoName::from("protocol1".to_string()), 1), 0),
			ProtoNameWithUsize(ProtoNameWithUsize(ProtoName::from("protocol2".to_string()), 2), 1),
			ProtoNameWithUsize(ProtoNameWithUsize(ProtoName::from("protocol3".to_string()), 3), 2),
		];
		let upgrades = upgrade.protocol_info().collect::<Vec<_>>();

		assert_eq!(upgrades, protos,);
	}

	#[test]
	fn nested_protocol_info() {
		let mut upgrades = (1..=2)
			.map(|i| {
				let mut upgrade = MockProtocolUpgrade::<ProtoNameWithUsize<ProtoName>>::new();
				upgrade.expect_protocol_info().return_once(move || {
					vec![ProtoNameWithUsize(ProtoName::from(format!("protocol{i}")), i)].into_iter()
				});
				upgrade
			})
			.collect::<Vec<_>>();

		upgrades.push({
			let mut upgrade = MockProtocolUpgrade::<ProtoNameWithUsize<ProtoName>>::new();
			upgrade.expect_protocol_info().return_once(move || {
				vec![
					ProtoNameWithUsize(ProtoName::from("protocol22".to_string()), 1),
					ProtoNameWithUsize(ProtoName::from("protocol33".to_string()), 2),
					ProtoNameWithUsize(ProtoName::from("protocol44".to_string()), 3),
				]
				.into_iter()
			});
			upgrade
		});

		let upgrade: UpgradeCollec<_> = upgrades.into_iter().collect::<UpgradeCollec<_>>();
		let protos = vec![
			ProtoNameWithUsize(ProtoNameWithUsize(ProtoName::from("protocol1".to_string()), 1), 0),
			ProtoNameWithUsize(ProtoNameWithUsize(ProtoName::from("protocol2".to_string()), 2), 1),
			ProtoNameWithUsize(ProtoNameWithUsize(ProtoName::from("protocol22".to_string()), 1), 2),
			ProtoNameWithUsize(ProtoNameWithUsize(ProtoName::from("protocol33".to_string()), 2), 2),
			ProtoNameWithUsize(ProtoNameWithUsize(ProtoName::from("protocol44".to_string()), 3), 2),
		];
		let upgrades = upgrade.protocol_info().collect::<Vec<_>>();
		assert_eq!(upgrades, protos,);
	}
}