Skip to content

Commit 0485a86

Browse files
committed
Merge #1473: Enrich UDP core Event::UdpAnnounce event
e9ec15a chore: remove print statment from tests (Jose Celano) 925fc93 test: [#1384,#1376] add comment to mock time (Jose Celano) f5bdec5 refactor: [#1376] rename function (Jose Celano) 1477975 feat: [#1384] enrich bittorrent_udp_tracker_core::event::Event::UdpAnnounce (Jose Celano) 2670a0a refactor: [#1384] rename fields (Jose Celano) Pull request description: Enrich UDP core `Event::UdpAnnounce` event. ```rust pub enum Event { UdpConnect { connection: ConnectionContext, }, UdpAnnounce { connection: ConnectionContext, info_hash: InfoHash, // New field announcement: PeerAnnouncement, // New field }, UdpScrape { connection: ConnectionContext, }, } ``` ACKs for top commit: josecelano: ACK e9ec15a Tree-SHA512: 76b39614dd0642f62e2a37d17cdc46a443c924a8486541b5037746ce0e6bef21c4d3000f01858ba04781c553944b27d037f3a11c436ee8ea3a5cd9a932efc50c
2 parents c4dc5e3 + e9ec15a commit 0485a86

File tree

11 files changed

+164
-39
lines changed

11 files changed

+164
-39
lines changed

packages/http-tracker-core/src/event/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub mod test {
108108
use crate::tests::sample_info_hash;
109109

110110
#[must_use]
111-
pub fn events_match(event: &Event, expected_event: &Event) -> bool {
111+
pub fn announce_events_match(event: &Event, expected_event: &Event) -> bool {
112112
match (event, expected_event) {
113113
(
114114
Event::TcpAnnounce {
@@ -124,7 +124,17 @@ pub mod test {
124124
) => {
125125
*connection == *expected_connection
126126
&& *info_hash == *expected_info_hash
127+
&& announcement.peer_id == expected_announcement.peer_id
127128
&& announcement.peer_addr == expected_announcement.peer_addr
129+
// Events can't be compared due to the `updated` field.
130+
// The `announcement.uploaded` contains the current time
131+
// when the test is executed.
132+
// todo: mock time
133+
//&& announcement.updated == expected_announcement.updated
134+
&& announcement.uploaded == expected_announcement.uploaded
135+
&& announcement.downloaded == expected_announcement.downloaded
136+
&& announcement.left == expected_announcement.left
137+
&& announcement.event == expected_announcement.event
128138
}
129139
_ => false,
130140
}

packages/http-tracker-core/src/services/announce.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ mod tests {
312312
use torrust_tracker_test_helpers::configuration;
313313

314314
use crate::event;
315-
use crate::event::test::events_match;
315+
use crate::event::test::announce_events_match;
316316
use crate::event::{ConnectionContext, Event};
317317
use crate::services::announce::tests::{
318318
initialize_core_tracker_services, initialize_core_tracker_services_with_config, sample_announce_request_for_peer,
@@ -388,7 +388,7 @@ mod tests {
388388
announcement,
389389
};
390390

391-
events_match(event, &expected_event)
391+
announce_events_match(event, &expected_event)
392392
}))
393393
.times(1)
394394
.returning(|_| Box::pin(future::ready(Some(Ok(1)))));
@@ -465,7 +465,7 @@ mod tests {
465465
announcement: peer_announcement,
466466
};
467467

468-
events_match(event, &expected_event)
468+
announce_events_match(event, &expected_event)
469469
}))
470470
.times(1)
471471
.returning(|_| Box::pin(future::ready(Some(Ok(1)))));
@@ -514,7 +514,7 @@ mod tests {
514514
info_hash: sample_info_hash(),
515515
announcement: peer,
516516
};
517-
events_match(event, &expected_event)
517+
announce_events_match(event, &expected_event)
518518
}))
519519
.times(1)
520520
.returning(|_| Box::pin(future::ready(Some(Ok(1)))));

packages/udp-tracker-core/src/event/mod.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
use std::net::SocketAddr;
22

3+
use bittorrent_primitives::info_hash::InfoHash;
34
use torrust_tracker_metrics::label::{LabelSet, LabelValue};
45
use torrust_tracker_metrics::label_name;
6+
use torrust_tracker_primitives::peer::PeerAnnouncement;
57
use torrust_tracker_primitives::service_binding::ServiceBinding;
68

79
pub mod sender;
810

911
/// A UDP core event.
1012
#[derive(Debug, PartialEq, Eq, Clone)]
1113
pub enum Event {
12-
UdpConnect { context: ConnectionContext },
13-
UdpAnnounce { context: ConnectionContext },
14-
UdpScrape { context: ConnectionContext },
14+
UdpConnect {
15+
connection: ConnectionContext,
16+
},
17+
UdpAnnounce {
18+
connection: ConnectionContext,
19+
info_hash: InfoHash,
20+
announcement: PeerAnnouncement,
21+
},
22+
UdpScrape {
23+
connection: ConnectionContext,
24+
},
1525
}
1626

1727
#[derive(Debug, PartialEq, Eq, Clone)]

packages/udp-tracker-core/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,18 @@ pub fn initialize_static() {
4242
// Initialize the Zeroed Cipher
4343
lazy_static::initialize(&ephemeral_instance_keys::ZEROED_TEST_CIPHER_BLOWFISH);
4444
}
45+
46+
#[cfg(test)]
47+
pub(crate) mod tests {
48+
use bittorrent_primitives::info_hash::InfoHash;
49+
50+
/// # Panics
51+
///
52+
/// Will panic if the string representation of the info hash is not a valid info hash.
53+
#[must_use]
54+
pub fn sample_info_hash() -> InfoHash {
55+
"3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0" // DevSkim: ignore DS173237
56+
.parse::<InfoHash>()
57+
.expect("String should be a valid info hash")
58+
}
59+
}

packages/udp-tracker-core/src/services/announce.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use bittorrent_tracker_core::error::{AnnounceError, WhitelistError};
1818
use bittorrent_tracker_core::whitelist;
1919
use bittorrent_udp_tracker_protocol::peer_builder;
2020
use torrust_tracker_primitives::core::AnnounceData;
21+
use torrust_tracker_primitives::peer::PeerAnnouncement;
2122
use torrust_tracker_primitives::service_binding::ServiceBinding;
2223

2324
use crate::connection_cookie::{check, gen_remote_fingerprint, ConnectionCookieError};
@@ -80,7 +81,8 @@ impl AnnounceService {
8081
.announce(&info_hash, &mut peer, &remote_client_ip, &peers_wanted)
8182
.await?;
8283

83-
self.send_event(client_socket_addr, server_service_binding).await;
84+
self.send_event(info_hash, peer, client_socket_addr, server_service_binding)
85+
.await;
8486

8587
Ok(announce_data)
8688
}
@@ -101,13 +103,25 @@ impl AnnounceService {
101103
self.whitelist_authorization.authorize(info_hash).await
102104
}
103105

104-
async fn send_event(&self, client_socket_addr: SocketAddr, server_service_binding: ServiceBinding) {
106+
async fn send_event(
107+
&self,
108+
info_hash: InfoHash,
109+
announcement: PeerAnnouncement,
110+
client_socket_addr: SocketAddr,
111+
server_service_binding: ServiceBinding,
112+
) {
105113
if let Some(udp_stats_event_sender) = self.opt_udp_core_stats_event_sender.as_deref() {
106-
udp_stats_event_sender
107-
.send_event(Event::UdpAnnounce {
108-
context: ConnectionContext::new(client_socket_addr, server_service_binding),
109-
})
110-
.await;
114+
let event = Event::UdpAnnounce {
115+
connection: ConnectionContext::new(client_socket_addr, server_service_binding),
116+
info_hash,
117+
announcement,
118+
};
119+
120+
tracing::debug!(target = crate::UDP_TRACKER_LOG_TARGET, "Sending UdpAnnounce event: {event:?}");
121+
122+
println!("Sending UdpAnnounce event: {event:?}");
123+
124+
udp_stats_event_sender.send_event(event).await;
111125
}
112126
}
113127
}

packages/udp-tracker-core/src/services/connect.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl ConnectService {
4343
if let Some(udp_stats_event_sender) = self.opt_udp_core_stats_event_sender.as_deref() {
4444
udp_stats_event_sender
4545
.send_event(Event::UdpConnect {
46-
context: ConnectionContext::new(client_socket_addr, server_service_binding),
46+
connection: ConnectionContext::new(client_socket_addr, server_service_binding),
4747
})
4848
.await;
4949
}
@@ -144,7 +144,7 @@ mod tests {
144144
udp_stats_event_sender_mock
145145
.expect_send_event()
146146
.with(eq(Event::UdpConnect {
147-
context: ConnectionContext::new(client_socket_addr, server_service_binding.clone()),
147+
connection: ConnectionContext::new(client_socket_addr, server_service_binding.clone()),
148148
}))
149149
.times(1)
150150
.returning(|_| Box::pin(future::ready(Some(Ok(1)))));
@@ -168,7 +168,7 @@ mod tests {
168168
udp_stats_event_sender_mock
169169
.expect_send_event()
170170
.with(eq(Event::UdpConnect {
171-
context: ConnectionContext::new(client_socket_addr, server_service_binding.clone()),
171+
connection: ConnectionContext::new(client_socket_addr, server_service_binding.clone()),
172172
}))
173173
.times(1)
174174
.returning(|_| Box::pin(future::ready(Some(Ok(1)))));

packages/udp-tracker-core/src/services/scrape.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,13 @@ impl ScrapeService {
8585

8686
async fn send_event(&self, client_socket_addr: SocketAddr, server_service_binding: ServiceBinding) {
8787
if let Some(udp_stats_event_sender) = self.opt_udp_stats_event_sender.as_deref() {
88-
udp_stats_event_sender
89-
.send_event(Event::UdpScrape {
90-
context: ConnectionContext::new(client_socket_addr, server_service_binding),
91-
})
92-
.await;
88+
let event = Event::UdpScrape {
89+
connection: ConnectionContext::new(client_socket_addr, server_service_binding),
90+
};
91+
92+
tracing::debug!(target = crate::UDP_TRACKER_LOG_TARGET, "Sending UdpScrape event: {event:?}");
93+
94+
udp_stats_event_sender.send_event(event).await;
9395
}
9496
}
9597
}

packages/udp-tracker-core/src/statistics/event/handler.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::statistics::UDP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL;
1111
/// This function panics if the IP version does not match the event type.
1212
pub async fn handle_event(event: Event, stats_repository: &Repository, now: DurationSinceUnixEpoch) {
1313
match event {
14-
Event::UdpConnect { context } => {
14+
Event::UdpConnect { connection: context } => {
1515
// Global fixed metrics
1616

1717
match context.client_socket_addr.ip() {
@@ -36,7 +36,7 @@ pub async fn handle_event(event: Event, stats_repository: &Repository, now: Dura
3636
Err(err) => tracing::error!("Failed to increase the counter: {}", err),
3737
};
3838
}
39-
Event::UdpAnnounce { context } => {
39+
Event::UdpAnnounce { connection: context, .. } => {
4040
// Global fixed metrics
4141

4242
match context.client_socket_addr.ip() {
@@ -61,7 +61,7 @@ pub async fn handle_event(event: Event, stats_repository: &Repository, now: Dura
6161
Err(err) => tracing::error!("Failed to increase the counter: {}", err),
6262
};
6363
}
64-
Event::UdpScrape { context } => {
64+
Event::UdpScrape { connection: context } => {
6565
// Global fixed metrics
6666

6767
match context.client_socket_addr.ip() {
@@ -96,11 +96,13 @@ mod tests {
9696
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
9797

9898
use torrust_tracker_clock::clock::Time;
99+
use torrust_tracker_primitives::peer::PeerAnnouncement;
99100
use torrust_tracker_primitives::service_binding::{Protocol, ServiceBinding};
100101

101102
use crate::event::{ConnectionContext, Event};
102103
use crate::statistics::event::handler::handle_event;
103104
use crate::statistics::repository::Repository;
105+
use crate::tests::sample_info_hash;
104106
use crate::CurrentClock;
105107

106108
#[tokio::test]
@@ -109,7 +111,7 @@ mod tests {
109111

110112
handle_event(
111113
Event::UdpConnect {
112-
context: ConnectionContext::new(
114+
connection: ConnectionContext::new(
113115
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 195)), 8080),
114116
ServiceBinding::new(
115117
Protocol::UDP,
@@ -134,14 +136,16 @@ mod tests {
134136

135137
handle_event(
136138
Event::UdpAnnounce {
137-
context: ConnectionContext::new(
139+
connection: ConnectionContext::new(
138140
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 195)), 8080),
139141
ServiceBinding::new(
140142
Protocol::UDP,
141143
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 196)), 6969),
142144
)
143145
.unwrap(),
144146
),
147+
info_hash: sample_info_hash(),
148+
announcement: PeerAnnouncement::default(),
145149
},
146150
&stats_repository,
147151
CurrentClock::now(),
@@ -159,7 +163,7 @@ mod tests {
159163

160164
handle_event(
161165
Event::UdpScrape {
162-
context: ConnectionContext::new(
166+
connection: ConnectionContext::new(
163167
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 195)), 8080),
164168
ServiceBinding::new(
165169
Protocol::UDP,
@@ -184,7 +188,7 @@ mod tests {
184188

185189
handle_event(
186190
Event::UdpConnect {
187-
context: ConnectionContext::new(
191+
connection: ConnectionContext::new(
188192
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 203, 0, 113, 195)), 8080),
189193
ServiceBinding::new(
190194
Protocol::UDP,
@@ -209,14 +213,16 @@ mod tests {
209213

210214
handle_event(
211215
Event::UdpAnnounce {
212-
context: ConnectionContext::new(
216+
connection: ConnectionContext::new(
213217
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 203, 0, 113, 195)), 8080),
214218
ServiceBinding::new(
215219
Protocol::UDP,
216220
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 203, 0, 113, 196)), 6969),
217221
)
218222
.unwrap(),
219223
),
224+
info_hash: sample_info_hash(),
225+
announcement: PeerAnnouncement::default(),
220226
},
221227
&stats_repository,
222228
CurrentClock::now(),
@@ -234,7 +240,7 @@ mod tests {
234240

235241
handle_event(
236242
Event::UdpScrape {
237-
context: ConnectionContext::new(
243+
connection: ConnectionContext::new(
238244
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 203, 0, 113, 195)), 8080),
239245
ServiceBinding::new(
240246
Protocol::UDP,

packages/udp-tracker-server/src/handlers/announce.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ mod tests {
824824
use bittorrent_udp_tracker_core::connection_cookie::{gen_remote_fingerprint, make};
825825
use bittorrent_udp_tracker_core::services::announce::AnnounceService;
826826
use bittorrent_udp_tracker_core::{self, event as core_event};
827-
use mockall::predicate::eq;
827+
use mockall::predicate::{self, eq};
828828
use torrust_tracker_primitives::service_binding::{Protocol, ServiceBinding};
829829

830830
use crate::event::{self, ConnectionContext, Event, UdpRequestKind};
@@ -834,6 +834,7 @@ mod tests {
834834
sample_cookie_valid_range, sample_issue_time, MockUdpCoreStatsEventSender, MockUdpServerStatsEventSender,
835835
TrackerConfigurationBuilder,
836836
};
837+
use crate::tests::{announce_events_match, sample_peer};
837838

838839
#[tokio::test]
839840
async fn the_peer_ip_should_be_changed_to_the_external_ip_in_the_tracker_configuration() {
@@ -848,6 +849,9 @@ mod tests {
848849

849850
let info_hash = AquaticInfoHash([0u8; 20]);
850851
let peer_id = AquaticPeerId([255u8; 20]);
852+
let mut announcement = sample_peer();
853+
announcement.peer_id = peer_id;
854+
announcement.peer_addr = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x7e00, 1)), client_port);
851855

852856
let client_socket_addr = SocketAddr::new(IpAddr::V6(client_ip_v6), client_port);
853857
let mut server_socket_addr = config.udp_trackers.clone().unwrap()[0].bind_address;
@@ -856,6 +860,7 @@ mod tests {
856860
server_socket_addr.set_port(6969);
857861
}
858862
let server_service_binding = ServiceBinding::new(Protocol::UDP, server_socket_addr).unwrap();
863+
let server_service_binding_clone = server_service_binding.clone();
859864

860865
let database = initialize_database(&config.core);
861866
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
@@ -867,8 +872,17 @@ mod tests {
867872
let mut udp_core_stats_event_sender_mock = MockUdpCoreStatsEventSender::new();
868873
udp_core_stats_event_sender_mock
869874
.expect_send_event()
870-
.with(eq(core_event::Event::UdpAnnounce {
871-
context: core_event::ConnectionContext::new(client_socket_addr, server_service_binding.clone()),
875+
.with(predicate::function(move |event| {
876+
let expected_event = core_event::Event::UdpAnnounce {
877+
connection: core_event::ConnectionContext::new(
878+
client_socket_addr,
879+
server_service_binding.clone(),
880+
),
881+
info_hash: info_hash.into(),
882+
announcement,
883+
};
884+
885+
announce_events_match(event, &expected_event)
872886
}))
873887
.times(1)
874888
.returning(|_| Box::pin(future::ready(Some(Ok(1)))));
@@ -879,7 +893,7 @@ mod tests {
879893
udp_server_stats_event_sender_mock
880894
.expect_send_event()
881895
.with(eq(Event::UdpRequestAccepted {
882-
context: ConnectionContext::new(client_socket_addr, server_service_binding.clone()),
896+
context: ConnectionContext::new(client_socket_addr, server_service_binding_clone.clone()),
883897
kind: UdpRequestKind::Announce,
884898
}))
885899
.times(1)
@@ -913,7 +927,7 @@ mod tests {
913927
handle_announce(
914928
&announce_service,
915929
client_socket_addr,
916-
server_service_binding,
930+
server_service_binding_clone,
917931
&request,
918932
&core_config,
919933
&udp_server_stats_event_sender,

0 commit comments

Comments
 (0)