-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
worker.rs
145 lines (119 loc) · 7.39 KB
/
worker.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
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkOS library.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::{
common::CurrentNetwork,
components::{sample_ledger, sample_worker},
};
use snarkos_node_bft::helpers::max_redundant_requests;
use snarkvm::{
ledger::{committee::Committee, narwhal::TransmissionID},
prelude::{Network, TestRng},
};
use std::net::SocketAddr;
#[tokio::test]
#[rustfmt::skip]
async fn test_resend_transmission_request() {
const NUM_NODES: u16 = Committee::<CurrentNetwork>::MAX_COMMITTEE_SIZE;
// Initialize the RNG.
let rng = &mut TestRng::default();
// Sample a ledger.
let ledger = sample_ledger(NUM_NODES, rng);
// Sample a worker.
let worker = sample_worker(0, ledger.clone());
// Determine the maximum number of redundant requests.
let max_redundancy = max_redundant_requests(ledger.clone(), 0);
assert_eq!(max_redundancy, 34, "Update me if the formula changes");
// Prepare a dummy transmission ID.
let peer_ip = SocketAddr::from(([127, 0, 0, 1], 1234));
let transmission_id = TransmissionID::Transaction(<CurrentNetwork as Network>::TransactionID::default());
// Ensure the worker does not have the dummy transmission ID.
assert!(!worker.contains_transmission(transmission_id), "Transmission should not exist");
// Send a request to fetch the dummy transmission.
let worker_ = worker.clone();
tokio::spawn(async move { worker_.get_or_fetch_transmission(peer_ip, transmission_id).await });
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
let pending = worker.pending();
// Ensure the transmission ID exists in the pending queue.
assert!(pending.contains(transmission_id), "Missing a transmission in the pending queue");
// Ensure the peer IP is in the pending queue for the transmission ID.
assert!(pending.contains_peer(transmission_id, peer_ip), "Missing a peer IP for transmission in the pending queue");
assert_eq!(pending.get(transmission_id), Some([peer_ip].into_iter().collect()), "Missing a peer IP for transmission in the pending queue");
// Ensure the number of callbacks is correct.
assert_eq!(pending.num_callbacks(transmission_id), 1, "Incorrect number of callbacks for transmission");
// Ensure the number of sent requests is correct.
assert_eq!(pending.num_sent_requests(transmission_id), 1, "Incorrect number of sent requests for transmission");
// Rebroadcast the same request to fetch the dummy transmission.
for i in 1..=10 {
let worker_ = worker.clone();
tokio::spawn(async move { worker_.get_or_fetch_transmission(peer_ip, transmission_id).await });
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
// Ensure the transmission ID exists in the pending queue.
assert!(pending.contains(transmission_id), "Missing a transmission in the pending queue");
// Ensure the peer IP is in the pending queue for the transmission ID.
assert!(pending.contains_peer(transmission_id, peer_ip), "Missing a peer IP for transmission in the pending queue");
assert_eq!(pending.get(transmission_id), Some([peer_ip].into_iter().collect()), "Missing a peer IP for transmission in the pending queue");
// Ensure the number of callbacks is correct.
assert_eq!(pending.num_callbacks(transmission_id), 1 + i, "Incorrect number of callbacks for transmission");
// Ensure the number of sent requests is correct.
assert_eq!(pending.num_sent_requests(transmission_id), (1 + i).min(max_redundancy), "Incorrect number of sent requests for transmission");
}
}
#[tokio::test]
#[rustfmt::skip]
async fn test_flood_transmission_requests() {
const NUM_NODES: u16 = Committee::<CurrentNetwork>::MAX_COMMITTEE_SIZE;
// Initialize the RNG.
let rng = &mut TestRng::default();
// Sample a ledger.
let ledger = sample_ledger(NUM_NODES, rng);
// Sample a worker.
let worker = sample_worker(0, ledger.clone());
// Determine the maximum number of redundant requests.
let max_redundancy = max_redundant_requests(ledger.clone(), 0);
assert_eq!(max_redundancy, 34, "Update me if the formula changes");
// Prepare a dummy transmission ID.
let peer_ip = SocketAddr::from(([127, 0, 0, 1], 1234));
let transmission_id = TransmissionID::Transaction(<CurrentNetwork as Network>::TransactionID::default());
// Ensure the worker does not have the dummy transmission ID.
assert!(!worker.contains_transmission(transmission_id), "Transmission should not exist");
// Send the maximum number of redundant requests to fetch the dummy transmission.
for _ in 0..max_redundancy {
let worker_ = worker.clone();
tokio::spawn(async move { worker_.get_or_fetch_transmission(peer_ip, transmission_id).await });
}
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
let pending = worker.pending();
// Ensure the transmission ID exists in the pending queue.
assert!(pending.contains(transmission_id), "Missing a transmission in the pending queue");
// Ensure the peer IP is in the pending queue for the transmission ID.
assert!(pending.contains_peer(transmission_id, peer_ip), "Missing a peer IP for transmission in the pending queue");
assert_eq!(pending.get(transmission_id), Some([peer_ip].into_iter().collect()), "Missing a peer IP for transmission in the pending queue");
// Ensure the number of callbacks is correct.
assert_eq!(pending.num_callbacks(transmission_id), max_redundancy, "Incorrect number of callbacks for transmission");
// Ensure the number of sent requests is correct.
assert_eq!(pending.num_sent_requests(transmission_id), max_redundancy, "Incorrect number of sent requests for transmission");
// Ensure any further redundant requests are not sent.
for i in 1..=20 {
let worker_ = worker.clone();
tokio::spawn(async move { worker_.get_or_fetch_transmission(peer_ip, transmission_id).await });
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
// Ensure the transmission ID exists in the pending queue.
assert!(pending.contains(transmission_id), "Missing a transmission in the pending queue");
// Ensure the peer IP is in the pending queue for the transmission ID.
assert!(pending.contains_peer(transmission_id, peer_ip), "Missing a peer IP for transmission in the pending queue");
assert_eq!(pending.get(transmission_id), Some([peer_ip].into_iter().collect()), "Missing a peer IP for transmission in the pending queue");
// Ensure the number of callbacks is correct.
assert_eq!(pending.num_callbacks(transmission_id), max_redundancy + i, "Incorrect number of callbacks for transmission");
// Ensure the number of sent requests is correct.
assert_eq!(pending.num_sent_requests(transmission_id), max_redundancy, "Incorrect number of sent requests for transmission");
}
}