Skip to content

Commit bb40306

Browse files
committed
refactor: early return for LATEST_MAX_CERTIFICATES where possible
Signed-off-by: ljedrz <ljedrz@users.noreply.github.com>
1 parent 8fff3da commit bb40306

File tree

7 files changed

+24
-19
lines changed

7 files changed

+24
-19
lines changed

node/bft/src/gateway.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl<N: Network> Gateway<N> {
154154
(Some(ip), _) => ip,
155155
};
156156
// Initialize the TCP stack.
157-
let tcp = Tcp::new(Config::new(ip, N::MAX_CERTIFICATES.last().unwrap().1));
157+
let tcp = Tcp::new(Config::new(ip, N::LATEST_MAX_CERTIFICATES()?));
158158
// Return the gateway.
159159
Ok(Self {
160160
account,

node/bft/src/helpers/pending.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515

1616
use crate::MAX_FETCH_TIMEOUT_IN_MS;
1717
use snarkos_node_bft_ledger_service::LedgerService;
18-
use snarkvm::console::network::{Network, consensus_config_value};
18+
use snarkvm::{
19+
console::network::{Network, consensus_config_value},
20+
prelude::Result,
21+
};
1922

23+
use anyhow::anyhow;
2024
use parking_lot::RwLock;
2125
use std::{
2226
collections::{HashMap, HashSet},
@@ -32,21 +36,21 @@ use tokio::sync::oneshot;
3236
pub(crate) const CALLBACK_EXPIRATION_IN_SECS: i64 = MAX_FETCH_TIMEOUT_IN_MS.div_ceil(1000) as i64;
3337

3438
/// Returns the maximum number of redundant requests for the number of validators in the specified round.
35-
pub fn max_redundant_requests<N: Network>(ledger: Arc<dyn LedgerService<N>>, round: u64) -> usize {
39+
pub fn max_redundant_requests<N: Network>(ledger: Arc<dyn LedgerService<N>>, round: u64) -> Result<usize> {
3640
// Determine the number of validators in the committee lookback for the given round.
3741
let num_validators =
38-
ledger.get_committee_lookback_for_round(round).map(|committee| committee.num_members()).ok().unwrap_or_else(
39-
|| {
40-
let max_committee_size =
41-
consensus_config_value!(N, MAX_CERTIFICATES, ledger.latest_block_height()).unwrap();
42-
max_committee_size as usize
43-
},
44-
);
42+
if let Ok(n) = ledger.get_committee_lookback_for_round(round).map(|committee| committee.num_members()) {
43+
n
44+
} else {
45+
let max_committee_size = consensus_config_value!(N, MAX_CERTIFICATES, ledger.latest_block_height())
46+
.ok_or_else(|| anyhow!("Couldn't obtain MAX_CERTIFICATES"))?;
47+
max_committee_size as usize
48+
};
4549

4650
// Note: It is adequate to set this value to the availability threshold,
4751
// as with high probability one will respond honestly (in the best and worst case
4852
// with stake spread across the validators evenly and unevenly, respectively).
49-
1 + num_validators.saturating_div(3)
53+
Ok(1 + num_validators.saturating_div(3))
5054
}
5155

5256
#[derive(Debug)]

node/bft/src/helpers/proposal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl<N: Network> FromBytes for Proposal<N> {
210210
// Read the number of signatures.
211211
let num_signatures = u32::read_le(&mut reader)?;
212212
// Ensure the number of signatures is within bounds (this is an early safety check).
213-
if num_signatures as usize > N::LATEST_MAX_CERTIFICATES().unwrap() as usize {
213+
if num_signatures as usize > N::LATEST_MAX_CERTIFICATES().map_err(error)? as usize {
214214
return Err(error("Invalid number of signatures in the proposal"));
215215
}
216216
// Read the signatures.

node/bft/src/sync/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ impl<N: Network> Sync<N> {
607607
// Determine if we've already sent a request to the peer.
608608
let contains_peer_with_sent_request = self.pending.contains_peer_with_sent_request(certificate_id, peer_ip);
609609
// Determine the maximum number of redundant requests.
610-
let num_redundant_requests = max_redundant_requests(self.ledger.clone(), self.storage.current_round());
610+
let num_redundant_requests = max_redundant_requests(self.ledger.clone(), self.storage.current_round())?;
611611
// Determine if we should send a certificate request to the peer.
612612
// We send at most `num_redundant_requests` requests and each peer can only receive one request at a time.
613613
let should_send_request = num_sent_requests < num_redundant_requests && !contains_peer_with_sent_request;

node/bft/src/worker.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ impl<N: Network> Worker<N> {
446446
// Determine if we've already sent a request to the peer.
447447
let contains_peer_with_sent_request = self.pending.contains_peer_with_sent_request(transmission_id, peer_ip);
448448
// Determine the maximum number of redundant requests.
449-
let num_redundant_requests = max_redundant_requests(self.ledger.clone(), self.storage.current_round());
449+
let num_redundant_requests = max_redundant_requests(self.ledger.clone(), self.storage.current_round())?;
450450
// Determine if we should send a transmission request to the peer.
451451
// We send at most `num_redundant_requests` requests and each peer can only receive one request at a time.
452452
let should_send_request = num_sent_requests < num_redundant_requests && !contains_peer_with_sent_request;
@@ -623,7 +623,7 @@ mod tests {
623623
let ledger: Arc<dyn LedgerService<CurrentNetwork>> = Arc::new(mock_ledger);
624624

625625
// Ensure the maximum number of redundant requests is correct and consistent across iterations.
626-
assert_eq!(max_redundant_requests(ledger, 0), 6, "Update me if the formula changes");
626+
assert_eq!(max_redundant_requests(ledger, 0).unwrap(), 6, "Update me if the formula changes");
627627
}
628628

629629
#[tokio::test]
@@ -881,7 +881,8 @@ mod tests {
881881
let transmission_id = TransmissionID::Transaction(transaction_id, checksum);
882882

883883
// Determine the number of redundant requests are sent.
884-
let num_redundant_requests = max_redundant_requests(worker.ledger.clone(), worker.storage.current_round());
884+
let num_redundant_requests =
885+
max_redundant_requests(worker.ledger.clone(), worker.storage.current_round()).unwrap();
885886
let num_flood_requests = num_redundant_requests * 10;
886887
let mut peer_ips =
887888
(0..num_flood_requests).map(|i| SocketAddr::from(([127, 0, 0, 1], 1234 + i as u16))).collect_vec();

node/bft/tests/components/pending.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ fn test_max_redundant_requests() {
2828
// Sample a ledger.
2929
let ledger = sample_ledger(&accounts, &committee, &mut rng);
3030
// Ensure the maximum number of redundant requests is correct and consistent across iterations.
31-
assert_eq!(max_redundant_requests(ledger, 0), 6, "Update me if the formula changes");
31+
assert_eq!(max_redundant_requests(ledger, 0).unwrap(), 6, "Update me if the formula changes");
3232
}

node/bft/tests/components/worker.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async fn test_resend_transmission_request() {
4141
let worker = sample_worker(0, accounts[0].clone(), ledger.clone());
4242

4343
// Determine the maximum number of redundant requests.
44-
let max_redundancy = max_redundant_requests(ledger.clone(), 0);
44+
let max_redundancy = max_redundant_requests(ledger.clone(), 0).unwrap();
4545
assert_eq!(max_redundancy, 6, "Update me if the formula changes");
4646

4747
// Prepare peer ips.
@@ -123,7 +123,7 @@ async fn test_flood_transmission_requests() {
123123
let worker = sample_worker(0, accounts[0].clone(), ledger.clone());
124124

125125
// Determine the maximum number of redundant requests.
126-
let max_redundancy = max_redundant_requests(ledger.clone(), 0);
126+
let max_redundancy = max_redundant_requests(ledger.clone(), 0).unwrap();
127127
assert_eq!(max_redundancy, 6, "Update me if the formula changes");
128128

129129
// Prepare peer ips.

0 commit comments

Comments
 (0)