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
// 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/>.

//! [`sync_with_runtime`] synchronises the session status and mixnode sets from the blockchain
//! runtime to the core mixnet state. It is called every time a block is finalised.

use super::peer_id::from_core_peer_id;
use log::{debug, info};
use mixnet::core::{
	Mixnet, Mixnode as CoreMixnode, MixnodesErr as CoreMixnodesErr, RelSessionIndex,
	SessionPhase as CoreSessionPhase, SessionStatus as CoreSessionStatus,
};
use sc_network_types::{
	multiaddr::{multiaddr, Multiaddr, Protocol},
	PeerId,
};
use sp_api::{ApiError, ApiRef};
use sp_mixnet::{
	runtime_api::MixnetApi,
	types::{
		Mixnode as RuntimeMixnode, MixnodesErr as RuntimeMixnodesErr,
		SessionPhase as RuntimeSessionPhase, SessionStatus as RuntimeSessionStatus,
	},
};
use sp_runtime::traits::Block;

const LOG_TARGET: &str = "mixnet";

/// Convert a [`RuntimeSessionStatus`] to a [`CoreSessionStatus`].
///
/// The [`RuntimeSessionStatus`] and [`CoreSessionStatus`] types are effectively the same.
/// [`RuntimeSessionStatus`] is used in the runtime to avoid depending on the [`mixnet`] crate
/// there.
fn to_core_session_status(status: RuntimeSessionStatus) -> CoreSessionStatus {
	CoreSessionStatus {
		current_index: status.current_index,
		phase: match status.phase {
			RuntimeSessionPhase::CoverToCurrent => CoreSessionPhase::CoverToCurrent,
			RuntimeSessionPhase::RequestsToCurrent => CoreSessionPhase::RequestsToCurrent,
			RuntimeSessionPhase::CoverToPrev => CoreSessionPhase::CoverToPrev,
			RuntimeSessionPhase::DisconnectFromPrev => CoreSessionPhase::DisconnectFromPrev,
		},
	}
}

fn parse_external_addresses(external_addresses: Vec<Vec<u8>>) -> Vec<Multiaddr> {
	external_addresses
		.into_iter()
		.flat_map(|addr| {
			let addr = match String::from_utf8(addr) {
				Ok(addr) => addr,
				Err(addr) => {
					debug!(
						target: LOG_TARGET,
						"Mixnode external address {:x?} is not valid UTF-8",
						addr.into_bytes(),
					);
					return None
				},
			};
			match addr.parse() {
				Ok(addr) => Some(addr),
				Err(err) => {
					debug!(
						target: LOG_TARGET,
						"Could not parse mixnode address {addr}: {err}",
					);
					None
				},
			}
		})
		.collect()
}

/// Modify `external_addresses` such that there is at least one address and the final component of
/// each address matches `peer_id`.
fn fixup_external_addresses(external_addresses: &mut Vec<Multiaddr>, peer_id: &PeerId) {
	// Ensure the final component of each address matches peer_id
	external_addresses.retain_mut(|addr| match PeerId::try_from_multiaddr(addr) {
		Some(addr_peer_id) if addr_peer_id == *peer_id => true,
		Some(_) => {
			debug!(
				target: LOG_TARGET,
				"Mixnode address {} does not match mixnode peer ID {}, ignoring",
				addr,
				peer_id
			);
			false
		},
		None if matches!(addr.iter().last(), Some(Protocol::P2p(_))) => {
			debug!(
				target: LOG_TARGET,
				"Mixnode address {} has unrecognised P2P protocol, ignoring",
				addr
			);
			false
		},
		None => {
			addr.push(Protocol::P2p(*peer_id.as_ref()));
			true
		},
	});

	// If there are no addresses, insert one consisting of just the peer ID
	if external_addresses.is_empty() {
		external_addresses.push(multiaddr!(P2p(*peer_id.as_ref())));
	}
}

/// Convert a [`RuntimeMixnode`] to a [`CoreMixnode`]. If the conversion fails, an error message is
/// logged, but a [`CoreMixnode`] is still returned.
///
/// It would be possible to handle conversion failure in a better way, but this would complicate
/// things for what should be a rare case. Note that even if the conversion here succeeds, there is
/// no guarantee that we will be able to connect to the mixnode or send packets to it. The most
/// common failure case is expected to be that a mixnode is simply unreachable over the network.
fn into_core_mixnode(mixnode: RuntimeMixnode) -> CoreMixnode<Vec<Multiaddr>> {
	let external_addresses = if let Some(peer_id) = from_core_peer_id(&mixnode.peer_id) {
		let mut external_addresses = parse_external_addresses(mixnode.external_addresses);
		fixup_external_addresses(&mut external_addresses, &peer_id);
		external_addresses
	} else {
		debug!(
			target: LOG_TARGET,
			"Failed to convert mixnet peer ID {:x?} to libp2p peer ID",
			mixnode.peer_id,
		);
		Vec::new()
	};

	CoreMixnode {
		kx_public: mixnode.kx_public,
		peer_id: mixnode.peer_id,
		extra: external_addresses,
	}
}

fn maybe_set_mixnodes(
	mixnet: &mut Mixnet<Vec<Multiaddr>>,
	rel_session_index: RelSessionIndex,
	mixnodes: &dyn Fn() -> Result<Result<Vec<RuntimeMixnode>, RuntimeMixnodesErr>, ApiError>,
) {
	let current_session_index = mixnet.session_status().current_index;
	mixnet.maybe_set_mixnodes(rel_session_index, &mut || {
		// Note that RelSessionIndex::Prev + 0 would panic, but this closure will not get called in
		// that case so we are fine. Do not move this out of the closure!
		let session_index = rel_session_index + current_session_index;
		match mixnodes() {
			Ok(Ok(mixnodes)) => Ok(mixnodes.into_iter().map(into_core_mixnode).collect()),
			Ok(Err(err)) => {
				info!(target: LOG_TARGET, "Session {session_index}: Mixnet disabled: {err}");
				Err(CoreMixnodesErr::Permanent) // Disable the session slot
			},
			Err(err) => {
				debug!(
					target: LOG_TARGET,
					"Session {session_index}: Error getting mixnodes from runtime: {err}"
				);
				Err(CoreMixnodesErr::Transient) // Just leave the session slot empty; try again next block
			},
		}
	});
}

pub fn sync_with_runtime<B, A>(mixnet: &mut Mixnet<Vec<Multiaddr>>, api: ApiRef<A>, hash: B::Hash)
where
	B: Block,
	A: MixnetApi<B>,
{
	let session_status = match api.session_status(hash) {
		Ok(session_status) => session_status,
		Err(err) => {
			debug!(target: LOG_TARGET, "Error getting session status from runtime: {err}");
			return
		},
	};
	mixnet.set_session_status(to_core_session_status(session_status));

	maybe_set_mixnodes(mixnet, RelSessionIndex::Prev, &|| api.prev_mixnodes(hash));
	maybe_set_mixnodes(mixnet, RelSessionIndex::Current, &|| api.current_mixnodes(hash));
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn fixup_empty_external_addresses() {
		let peer_id = PeerId::random();
		let mut external_addresses = Vec::new();
		fixup_external_addresses(&mut external_addresses, &peer_id);
		assert_eq!(external_addresses, vec![multiaddr!(P2p(peer_id))]);
	}

	#[test]
	fn fixup_misc_external_addresses() {
		let peer_id = PeerId::random();
		let other_peer_id = PeerId::random();
		let mut external_addresses = vec![
			multiaddr!(Tcp(0u16), P2p(peer_id)),
			multiaddr!(Tcp(1u16), P2p(other_peer_id)),
			multiaddr!(Tcp(2u16)),
			Multiaddr::empty(),
		];
		fixup_external_addresses(&mut external_addresses, &peer_id);
		assert_eq!(
			external_addresses,
			vec![
				multiaddr!(Tcp(0u16), P2p(peer_id)),
				multiaddr!(Tcp(2u16), P2p(peer_id)),
				multiaddr!(P2p(peer_id)),
			]
		);
	}
}