-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathbuilders.rs
More file actions
300 lines (262 loc) · 9.12 KB
/
Copy pathbuilders.rs
File metadata and controls
300 lines (262 loc) · 9.12 KB
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
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use rand::RngCore;
use witnet_util::timestamp::get_timestamp;
use crate::{
chain::{
Block, BlockHeader, BlockTransactions, InventoryEntry, KeyedSignature, SuperBlock,
SuperBlockVote,
},
error::BuildersError,
transaction::Transaction,
types::{
Address, Command, GetPeers, InventoryAnnouncement, InventoryRequest, IpAddress, LastBeacon,
Message, Peers, RegisteredApiKeys, SignedRegisteredApiKeys, Verack, Version,
},
};
////////////////////////////////////////////////////////////////////////////////////////
// PROTOCOL MESSAGES CONSTANTS
////////////////////////////////////////////////////////////////////////////////////////
/// Protocol version (used in handshake)
pub const PROTOCOL_VERSION: u32 = 0x0000_0001;
/// Capabilities
pub const CAPABILITIES: u64 = 0x0000_0000_0000_0001;
////////////////////////////////////////////////////////////////////////////////////////
// BUILDER PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////
impl Message {
/// Function to build GetPeers messages
pub fn build_get_peers(magic: u16) -> Message {
Message::build_message(magic, Command::GetPeers(GetPeers))
}
/// Function to build Peers messages
pub fn build_peers(magic: u16, peers: &[SocketAddr]) -> Message {
// Cast all peers to witnet's address struct
let mut casted_peers = Vec::new();
peers.iter().for_each(|peer| {
casted_peers.push(to_address(*peer));
});
Message::build_message(
magic,
Command::Peers(Peers {
peers: casted_peers,
}),
)
}
/// Function to build Version messages
pub fn build_version(
magic: u16,
sender_addr: Option<SocketAddr>,
receiver_addr: SocketAddr,
beacon: LastBeacon,
) -> Message {
let addr = sender_addr.map(to_address);
Message::build_message(
magic,
Command::Version(Version {
version: PROTOCOL_VERSION,
timestamp: get_timestamp(),
capabilities: CAPABILITIES,
sender_address: addr.unwrap_or_default(),
receiver_address: to_address(receiver_addr),
user_agent: user_agent(),
nonce: random_nonce(),
beacon,
}),
)
}
/// Function to build Verack messages
pub fn build_verack(magic: u16) -> Message {
Message::build_message(magic, Command::Verack(Verack))
}
/// Function to build InventoryAnnouncement messages
pub fn build_inventory_announcement(
magic: u16,
inv_entries: Vec<InventoryEntry>,
) -> Result<Message, anyhow::Error> {
// Check there are some inventory vectors to be added to the message
if inv_entries.is_empty() {
return Err(BuildersError::NoInvVectorsAnnouncement.into());
}
// Build the message
Ok(Message::build_message(
magic,
Command::InventoryAnnouncement(InventoryAnnouncement {
inventory: inv_entries,
}),
))
}
/// Function to build GetData messages
pub fn build_inventory_request(
magic: u16,
inv_entries: Vec<InventoryEntry>,
) -> Result<Message, anyhow::Error> {
// Check there are some inventory vectors to be added to the message
if inv_entries.is_empty() {
return Err(BuildersError::NoInvVectorsRequest.into());
}
// Build the message
Ok(Message::build_message(
magic,
Command::InventoryRequest(InventoryRequest {
inventory: inv_entries,
}),
))
}
/// Function to build Block message
pub fn build_block(
magic: u16,
block_header: BlockHeader,
block_sig: KeyedSignature,
txns: BlockTransactions,
) -> Message {
Message::build_message(
magic,
Command::Block(Block::new(block_header, block_sig, txns)),
)
}
/// Function to build SuperBlock message
pub fn build_superblock(magic: u16, superblock: SuperBlock) -> Message {
Message::build_message(magic, Command::SuperBlock(superblock))
}
/// Function to build Transaction message
pub fn build_transaction(magic: u16, transaction: Transaction) -> Message {
Message::build_message(magic, Command::Transaction(transaction))
}
/// Function to build LastBeacon messages
pub fn build_last_beacon(magic: u16, last_beacon: LastBeacon) -> Message {
Message::build_message(magic, Command::LastBeacon(last_beacon))
}
/// Function to build SuperBlockVote messages
pub fn build_superblock_vote(magic: u16, superblock_vote: SuperBlockVote) -> Message {
Message::build_message(magic, Command::SuperBlockVote(superblock_vote))
}
/// Function to build SignedRegisteredApiKeys messages
pub fn build_api_keys_message(
magic: u16,
keys: RegisteredApiKeys,
signature: KeyedSignature,
) -> Message {
Message::build_message(
magic,
Command::SignedRegisteredApiKeys(SignedRegisteredApiKeys { keys, signature }),
)
}
/// Function to build a message from a command
fn build_message(magic: u16, command: Command) -> Message {
Message {
kind: command,
magic,
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
// AUX FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////
/// Function to get a random nonce
fn random_nonce() -> u64 {
rand::thread_rng().next_u64()
}
/// Function that returns the dynamic user agent
pub fn user_agent() -> String {
let release = option_env!("CARGO_PKG_VERSION").unwrap_or_else(|| "unspecified");
format!("witnet-rust {release}")
}
fn u128_to_be_u32(x: u128) -> [u32; 4] {
let ip = x.to_be_bytes();
[
u32::from_be_bytes([ip[0], ip[1], ip[2], ip[3]]),
u32::from_be_bytes([ip[4], ip[5], ip[6], ip[7]]),
u32::from_be_bytes([ip[8], ip[9], ip[10], ip[11]]),
u32::from_be_bytes([ip[12], ip[13], ip[14], ip[15]]),
]
}
/// Function to build address witnet type from socket addr
fn to_address(socket_addr: SocketAddr) -> Address {
match socket_addr {
SocketAddr::V4(addr) => Address {
ip: {
let ip = u32::from(addr.ip().to_owned());
IpAddress::Ipv4 { ip }
},
port: addr.port(),
},
SocketAddr::V6(addr) => Address {
ip: {
let [ip0, ip1, ip2, ip3] = u128_to_be_u32(u128::from(addr.ip().to_owned()));
IpAddress::Ipv6 { ip0, ip1, ip2, ip3 }
},
port: addr.port(),
},
}
}
/// Function to build a [SocketAddr](std::net::SocketAddr) from a
/// Witnet [Address](types::Address)
pub fn from_address(addr: &Address) -> SocketAddr {
let ip: IpAddr = addr.ip.into();
SocketAddr::from((ip, addr.port))
}
impl From<IpAddress> for IpAddr {
fn from(addr: IpAddress) -> Self {
match addr {
IpAddress::Ipv4 { ip } => IpAddr::V4(Ipv4Addr::from(ip)),
IpAddress::Ipv6 { ip0, ip1, ip2, ip3 } => {
let ip = u128::from(ip0) << 96
| u128::from(ip1) << 64
| u128::from(ip2) << 32
| u128::from(ip3);
IpAddr::V6(Ipv6Addr::from(ip))
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_to_address_ipv4() {
let socket_addr: SocketAddr = "127.0.0.1:3000".parse().unwrap();
let witnet_addr: Address = to_address(socket_addr);
assert_eq!(witnet_addr.ip, IpAddress::Ipv4 { ip: 2_130_706_433 });
assert_eq!(witnet_addr.port, 3000);
}
#[test]
fn test_to_address_ipv6() {
let socket_addr: SocketAddr = "[::1]:3000".parse().unwrap();
let witnet_addr: Address = to_address(socket_addr);
assert_eq!(
witnet_addr.ip,
IpAddress::Ipv6 {
ip0: 0,
ip1: 0,
ip2: 0,
ip3: 1
}
);
assert_eq!(witnet_addr.port, 3000);
}
#[test]
fn test_from_address_ipv4() {
let witnet_addr: Address = Address {
ip: IpAddress::Ipv4 { ip: 2_130_706_433 },
port: 3000,
};
let socket_addr: SocketAddr = from_address(&witnet_addr);
let expected = "127.0.0.1:3000".parse().unwrap();
assert_eq!(socket_addr, expected);
}
#[test]
fn test_from_address_ipv6() {
let witnet_addr: Address = Address {
ip: IpAddress::Ipv6 {
ip0: 0,
ip1: 0,
ip2: 0,
ip3: 1,
},
port: 3000,
};
let socket_addr: SocketAddr = from_address(&witnet_addr);
let expected = "[::1]:3000".parse().unwrap();
assert_eq!(socket_addr, expected);
}
}