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
use std::{fmt, net::SocketAddr};
use snarkos_storage::Digest;
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum Direction {
Inbound(SocketAddr),
Outbound(SocketAddr),
}
impl fmt::Display for Direction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Inbound(addr) => write!(f, "from {}", addr),
Self::Outbound(addr) => write!(f, "to {}", addr),
}
}
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Message {
pub direction: Direction,
pub payload: Payload,
}
impl Message {
pub fn new(direction: Direction, payload: Payload) -> Self {
Self { direction, payload }
}
pub fn receiver(&self) -> SocketAddr {
match self.direction {
Direction::Outbound(addr) => addr,
_ => unreachable!("Message::receiver used on a non-outbound Message!"),
}
}
}
impl fmt::Display for Message {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {}", self.payload, self.direction)
}
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum Payload {
#[doc = include_str!("../../documentation/network_messages/block.md")]
Block(Vec<u8>, Option<u32>),
#[doc = include_str!("../../documentation/network_messages/get_blocks.md")]
GetBlocks(Vec<Digest>),
#[doc = include_str!("../../documentation/network_messages/get_memory_pool.md")]
GetMemoryPool,
#[doc = include_str!("../../documentation/network_messages/get_peers.md")]
GetPeers,
#[doc = include_str!("../../documentation/network_messages/get_sync.md")]
GetSync(Vec<Digest>),
#[doc = include_str!("../../documentation/network_messages/memory_pool.md")]
MemoryPool(Vec<Vec<u8>>),
#[doc = include_str!("../../documentation/network_messages/peers.md")]
Peers(Vec<SocketAddr>),
#[doc = include_str!("../../documentation/network_messages/ping.md")]
Ping(u32),
#[doc = include_str!("../../documentation/network_messages/pong.md")]
Pong,
#[doc = include_str!("../../documentation/network_messages/sync.md")]
Sync(Vec<Digest>),
#[doc = include_str!("../../documentation/network_messages/sync_block.md")]
SyncBlock(Vec<u8>, Option<u32>),
#[doc = include_str!("../../documentation/network_messages/transaction.md")]
Transaction(Vec<u8>),
#[doc(hidden)]
Unknown,
}
impl fmt::Display for Payload {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let str = match self {
Self::Block(..) => "block",
Self::GetBlocks(..) => "getblocks",
Self::GetMemoryPool => "getmempool",
Self::GetPeers => "getpeers",
Self::GetSync(..) => "getsync",
Self::MemoryPool(..) => "memorypool",
Self::Peers(..) => "peers",
Self::Ping(..) => "ping",
Self::Pong => "pong",
Self::Sync(..) => "sync",
Self::SyncBlock(..) => "syncblock",
Self::Transaction(..) => "transaction",
Self::Unknown => "unknown",
};
f.write_str(str)
}
}