iroh_gossip/proto/state.rs
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
//! The protocol state of the `iroh-gossip` protocol.
use std::collections::{hash_map, HashMap, HashSet};
use iroh_metrics::{inc, inc_by};
use n0_future::time::{Duration, Instant};
use rand::Rng;
use serde::{Deserialize, Serialize};
use tracing::trace;
use crate::{
metrics::Metrics,
proto::{
topic::{self, Command},
util::idbytes_impls,
Config, PeerData, PeerIdentity,
},
};
/// The identifier for a topic
#[derive(Clone, Copy, Eq, PartialEq, Hash, Serialize, Ord, PartialOrd, Deserialize)]
pub struct TopicId([u8; 32]);
idbytes_impls!(TopicId, "TopicId");
/// Protocol wire message
///
/// This is the wire frame of the `iroh-gossip` protocol.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Message<PI> {
topic: TopicId,
message: topic::Message<PI>,
}
impl<PI> Message<PI> {
/// Get the kind of this message
pub fn kind(&self) -> MessageKind {
self.message.kind()
}
}
/// Whether this is a control or data message
#[derive(Debug)]
pub enum MessageKind {
/// A data message.
Data,
/// A control message.
Control,
}
impl<PI: Serialize> Message<PI> {
/// Get the encoded size of this message
pub fn size(&self) -> postcard::Result<usize> {
postcard::experimental::serialized_size(&self)
}
}
/// A timer to be registered into the runtime
///
/// As the implementation of the protocol is an IO-less state machine, registering timers does not
/// happen within the protocol implementation. Instead, these `Timer` structs are emitted as
/// [`OutEvent`]s. The implementer must register the timer in its runtime to be emitted on the specified [`Instant`],
/// and once triggered inject an [`InEvent::TimerExpired`] into the protocol state.
#[derive(Clone, Debug)]
pub struct Timer<PI> {
topic: TopicId,
timer: topic::Timer<PI>,
}
/// Input event to the protocol state.
#[derive(Clone, Debug)]
pub enum InEvent<PI> {
/// Message received from the network.
RecvMessage(PI, Message<PI>),
/// Execute a command from the application.
Command(TopicId, Command<PI>),
/// Trigger a previously scheduled timer.
TimerExpired(Timer<PI>),
/// Peer disconnected on the network level.
PeerDisconnected(PI),
/// Update the opaque peer data about yourself.
UpdatePeerData(PeerData),
}
/// Output event from the protocol state.
#[derive(Debug, Clone)]
pub enum OutEvent<PI> {
/// Send a message on the network
SendMessage(PI, Message<PI>),
/// Emit an event to the application.
EmitEvent(TopicId, topic::Event<PI>),
/// Schedule a timer. The runtime is responsible for sending an [InEvent::TimerExpired]
/// after the duration.
ScheduleTimer(Duration, Timer<PI>),
/// Close the connection to a peer on the network level.
DisconnectPeer(PI),
/// Updated peer data
PeerData(PI, PeerData),
}
type ConnsMap<PI> = HashMap<PI, HashSet<TopicId>>;
type Outbox<PI> = Vec<OutEvent<PI>>;
enum InEventMapped<PI> {
All(topic::InEvent<PI>),
TopicEvent(TopicId, topic::InEvent<PI>),
}
impl<PI> From<InEvent<PI>> for InEventMapped<PI> {
fn from(event: InEvent<PI>) -> InEventMapped<PI> {
match event {
InEvent::RecvMessage(from, Message { topic, message }) => {
Self::TopicEvent(topic, topic::InEvent::RecvMessage(from, message))
}
InEvent::Command(topic, command) => {
Self::TopicEvent(topic, topic::InEvent::Command(command))
}
InEvent::TimerExpired(Timer { topic, timer }) => {
Self::TopicEvent(topic, topic::InEvent::TimerExpired(timer))
}
InEvent::PeerDisconnected(peer) => Self::All(topic::InEvent::PeerDisconnected(peer)),
InEvent::UpdatePeerData(data) => Self::All(topic::InEvent::UpdatePeerData(data)),
}
}
}
/// The state of the `iroh-gossip` protocol.
///
/// The implementation works as an IO-less state machine. The implementer injects events through
/// [`Self::handle`], which returns an iterator of [`OutEvent`]s to be processed.
///
/// This struct contains a map of [`topic::State`] for each topic that was joined. It mostly acts as
/// a forwarder of [`InEvent`]s to matching topic state. Each topic's state is completely
/// independent; thus the actual protocol logic lives with [`topic::State`].
#[derive(Debug)]
pub struct State<PI, R> {
me: PI,
me_data: Option<PeerData>,
config: Config,
rng: R,
states: HashMap<TopicId, topic::State<PI, R>>,
outbox: Outbox<PI>,
peer_topics: ConnsMap<PI>,
}
impl<PI: PeerIdentity, R: Rng + Clone> State<PI, R> {
/// Create a new protocol state instance.
///
/// `me` is the [`PeerIdentity`] of the local node, `peer_data` is the initial [`PeerData`]
/// (which can be updated over time).
/// For the protocol to perform as recommended in the papers, the [`Config`] should be
/// identical for all nodes in the network.
pub fn new(me: PI, me_data: Option<PeerData>, config: Config, rng: R) -> Self {
Self {
me,
me_data,
config,
rng,
states: Default::default(),
outbox: Default::default(),
peer_topics: Default::default(),
}
}
/// Get a reference to the node's [`PeerIdentity`]
pub fn me(&self) -> &PI {
&self.me
}
/// Get a reference to the protocol state for a topic.
pub fn state(&self, topic: &TopicId) -> Option<&topic::State<PI, R>> {
self.states.get(topic)
}
/// Get a reference to the protocol state for a topic.
#[cfg(test)]
pub fn state_mut(&mut self, topic: &TopicId) -> Option<&mut topic::State<PI, R>> {
self.states.get_mut(topic)
}
/// Get an iterator of all joined topics.
pub fn topics(&self) -> impl Iterator<Item = &TopicId> {
self.states.keys()
}
/// Get an iterator for the states of all joined topics.
pub fn states(&self) -> impl Iterator<Item = (&TopicId, &topic::State<PI, R>)> {
self.states.iter()
}
/// Check if a topic has any active (connected) peers.
pub fn has_active_peers(&self, topic: &TopicId) -> bool {
self.state(topic)
.map(|s| s.has_active_peers())
.unwrap_or(false)
}
/// Returns the maximum message size configured in the gossip protocol.
pub fn max_message_size(&self) -> usize {
self.config.max_message_size
}
/// Handle an [`InEvent`]
///
/// This returns an iterator of [`OutEvent`]s that must be processed.
pub fn handle(
&mut self,
event: InEvent<PI>,
now: Instant,
) -> impl Iterator<Item = OutEvent<PI>> + '_ {
trace!("in : {event:?}");
track_in_event(&event);
let event: InEventMapped<PI> = event.into();
match event {
InEventMapped::TopicEvent(topic, event) => {
// when receiving a join command, initialize state if it doesn't exist
if matches!(&event, topic::InEvent::Command(Command::Join(_peers))) {
if let hash_map::Entry::Vacant(e) = self.states.entry(topic) {
e.insert(topic::State::with_rng(
self.me,
self.me_data.clone(),
self.config.clone(),
self.rng.clone(),
));
}
}
// when receiving a quit command, note this and drop the topic state after
// processing this last event
let quit = matches!(event, topic::InEvent::Command(Command::Quit));
// pass the event to the state handler
if let Some(state) = self.states.get_mut(&topic) {
// when receiving messages, update our conn map to take note that this topic state may want
// to keep this connection
if let topic::InEvent::RecvMessage(from, _message) = &event {
self.peer_topics.entry(*from).or_default().insert(topic);
}
let out = state.handle(event, now);
for event in out {
handle_out_event(topic, event, &mut self.peer_topics, &mut self.outbox);
}
}
if quit {
self.states.remove(&topic);
}
}
// when a peer disconnected on the network level, forward event to all states
InEventMapped::All(event) => {
if let topic::InEvent::UpdatePeerData(data) = &event {
self.me_data = Some(data.clone());
}
for (topic, state) in self.states.iter_mut() {
let out = state.handle(event.clone(), now);
for event in out {
handle_out_event(*topic, event, &mut self.peer_topics, &mut self.outbox);
}
}
}
}
// track metrics
track_out_events(&self.outbox);
self.outbox.drain(..)
}
}
fn handle_out_event<PI: PeerIdentity>(
topic: TopicId,
event: topic::OutEvent<PI>,
conns: &mut ConnsMap<PI>,
outbox: &mut Outbox<PI>,
) {
trace!("out: {event:?}");
match event {
topic::OutEvent::SendMessage(to, message) => {
outbox.push(OutEvent::SendMessage(to, Message { topic, message }))
}
topic::OutEvent::EmitEvent(event) => outbox.push(OutEvent::EmitEvent(topic, event)),
topic::OutEvent::ScheduleTimer(delay, timer) => {
outbox.push(OutEvent::ScheduleTimer(delay, Timer { topic, timer }))
}
topic::OutEvent::DisconnectPeer(peer) => {
let empty = conns
.get_mut(&peer)
.map(|list| list.remove(&topic) && list.is_empty())
.unwrap_or(false);
if empty {
conns.remove(&peer);
outbox.push(OutEvent::DisconnectPeer(peer));
}
}
topic::OutEvent::PeerData(peer, data) => outbox.push(OutEvent::PeerData(peer, data)),
}
}
fn track_out_events<PI: Serialize>(events: &[OutEvent<PI>]) {
for event in events {
match event {
OutEvent::SendMessage(_to, message) => match message.kind() {
MessageKind::Data => {
inc!(Metrics, msgs_data_sent);
inc_by!(
Metrics,
msgs_data_sent_size,
message.size().unwrap_or(0) as u64
);
}
MessageKind::Control => {
inc!(Metrics, msgs_ctrl_sent);
inc_by!(
Metrics,
msgs_ctrl_sent_size,
message.size().unwrap_or(0) as u64
);
}
},
OutEvent::EmitEvent(_topic, event) => match event {
super::Event::NeighborUp(_peer) => inc!(Metrics, neighbor_up),
super::Event::NeighborDown(_peer) => inc!(Metrics, neighbor_down),
_ => {}
},
_ => {}
}
}
}
fn track_in_event<PI: Serialize>(event: &InEvent<PI>) {
if let InEvent::RecvMessage(_from, message) = event {
match message.kind() {
MessageKind::Data => {
inc!(Metrics, msgs_data_recv);
inc_by!(
Metrics,
msgs_data_recv_size,
message.size().unwrap_or(0) as u64
);
}
MessageKind::Control => {
inc!(Metrics, msgs_ctrl_recv);
inc_by!(
Metrics,
msgs_ctrl_recv_size,
message.size().unwrap_or(0) as u64
);
}
}
}
}