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
use std::{
collections::HashSet,
fmt::{self, Display, Formatter},
};
use derive_more::From;
use serde::Serialize;
use casper_types::DisplayIter;
use super::GossipItem;
use crate::{
effect::{incoming::GossiperIncoming, requests::BeginGossipRequest, GossipTarget},
types::NodeId,
utils::Source,
};
/// `Gossiper` events.
#[derive(Debug, From, Serialize)]
pub(crate) enum Event<T: GossipItem> {
/// A request to gossip an item has been made.
#[from]
BeginGossipRequest(BeginGossipRequest<T>),
/// A new item has been received to be gossiped.
ItemReceived {
item_id: T::Id,
source: Source,
target: GossipTarget,
},
/// The network component gossiped to the included peers.
GossipedTo {
item_id: T::Id,
requested_count: usize,
peers: HashSet<NodeId>,
},
/// The timeout for waiting for a gossip response has elapsed and we should check the response
/// arrived.
CheckGossipTimeout { item_id: T::Id, peer: NodeId },
/// The timeout for waiting for the full item has elapsed and we should check the response
/// arrived.
CheckGetFromPeerTimeout { item_id: T::Id, peer: NodeId },
/// An incoming gossip network message.
#[from]
Incoming(GossiperIncoming<T>),
/// The timeout for waiting for a different component to validate and store the item has
/// elapsed and we should check that `ItemReceived` has been called by now.
CheckItemReceivedTimeout { item_id: T::Id },
/// The result of the gossiper checking if an item exists in storage.
IsStoredResult {
item_id: T::Id,
sender: NodeId,
result: bool,
},
/// The result of the gossiper getting an item from storage. If the result is `Some`, the item
/// should be sent to the requesting peer.
GetFromStorageResult {
item_id: T::Id,
requester: NodeId,
maybe_item: Option<Box<T>>,
},
}
impl<T: GossipItem> Display for Event<T> {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
match self {
Event::BeginGossipRequest(BeginGossipRequest {
item_id, source, ..
}) => {
write!(
formatter,
"begin gossping new item {} received from {}",
item_id, source
)
}
Event::ItemReceived {
item_id, source, ..
} => {
write!(formatter, "new item {} received from {}", item_id, source)
}
Event::GossipedTo { item_id, peers, .. } => write!(
formatter,
"gossiped {} to {}",
item_id,
DisplayIter::new(peers)
),
Event::CheckGossipTimeout { item_id, peer } => write!(
formatter,
"check gossip timeout for {} with {}",
item_id, peer
),
Event::CheckGetFromPeerTimeout { item_id, peer } => write!(
formatter,
"check get from peer timeout for {} with {}",
item_id, peer
),
Event::Incoming(incoming) => {
write!(formatter, "incoming: {}", incoming)
}
Event::CheckItemReceivedTimeout { item_id } => {
write!(formatter, "check item received timeout for {}", item_id,)
}
Event::IsStoredResult {
item_id,
sender,
result,
} => {
write!(
formatter,
"{} is stored for gossip message from {}: {}",
item_id, sender, result
)
}
Event::GetFromStorageResult {
item_id,
maybe_item,
..
} => {
if maybe_item.is_some() {
write!(formatter, "got {} from storage", item_id)
} else {
write!(formatter, "failed to get {} from storage", item_id)
}
}
}
}
}