Skip to content

Commit

Permalink
Replace NonZeroU* with NonZero (#1965)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaka authored Sep 20, 2024
1 parent da8ef32 commit 837d9c3
Show file tree
Hide file tree
Showing 44 changed files with 232 additions and 277 deletions.
15 changes: 7 additions & 8 deletions full-node/src/consensus_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

use crate::{database_thread, jaeger_service, network_service, LogCallback, LogLevel};

use core::num::NonZeroU32;
use futures_channel::{mpsc, oneshot};
use futures_lite::FutureExt as _;
use futures_util::{
Expand Down Expand Up @@ -56,7 +55,7 @@ use std::{
cmp,
future::Future,
iter,
num::{NonZeroU64, NonZeroUsize},
num::NonZero,
pin::Pin,
sync::Arc,
time::{Duration, Instant, SystemTime},
Expand Down Expand Up @@ -145,7 +144,7 @@ enum ToBackground {
SubscribeAll {
buffer_size: usize,
// TODO: unused field
_max_finalized_pinned_blocks: NonZeroUsize,
_max_finalized_pinned_blocks: NonZero<usize>,
result_tx: oneshot::Sender<SubscribeAll>,
},
GetSyncState {
Expand Down Expand Up @@ -302,14 +301,14 @@ impl ConsensusService {
1024
},
max_disjoint_headers: 1024,
max_requests_per_block: NonZeroU32::new(3).unwrap(),
max_requests_per_block: NonZero::<u32>::new(3).unwrap(),
download_ahead_blocks: {
// Assuming a verification speed of 1k blocks/sec and a 99th download time
// percentile of two second, the number of blocks to download ahead of time
// in order to not block is 2000.
// In practice, however, the verification speed and download speed depend on
// the chain and the machine of the user.
NonZeroU32::new(2000).unwrap()
NonZero::<u32>::new(2000).unwrap()
},
download_bodies: true,
// We ask for all the chain-information-related storage proofs and call proofs to be
Expand Down Expand Up @@ -416,7 +415,7 @@ impl ConsensusService {
pub async fn subscribe_all(
&self,
buffer_size: usize,
max_finalized_pinned_blocks: NonZeroUsize,
max_finalized_pinned_blocks: NonZero<usize>,
) -> SubscribeAll {
let (result_tx, result_rx) = oneshot::channel();
let _ = self
Expand Down Expand Up @@ -1500,7 +1499,7 @@ impl SyncBackground {
} => {
// Before notifying the syncing of the request, clamp the number of blocks to
// the number of blocks we expect to receive.
let num_blocks = NonZeroU64::new(cmp::min(num_blocks.get(), 64)).unwrap();
let num_blocks = NonZero::<u64>::new(cmp::min(num_blocks.get(), 64)).unwrap();

let peer_id = {
let info = self.sync[source_id].clone().unwrap();
Expand All @@ -1516,7 +1515,7 @@ impl SyncBackground {
self.network_chain_id,
network::codec::BlocksRequestConfig {
start: network::codec::BlocksRequestConfigStart::Hash(first_block_hash),
desired_count: NonZeroU32::new(
desired_count: NonZero::<u32>::new(
u32::try_from(num_blocks.get()).unwrap_or(u32::MAX),
)
.unwrap(),
Expand Down
9 changes: 4 additions & 5 deletions full-node/src/jaeger_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
use smol::{future, net::UdpSocket};
use smoldot::libp2p::PeerId;
use std::{
convert::TryFrom as _, future::Future, io, net::SocketAddr, num::NonZeroU128, pin::Pin,
sync::Arc,
convert::TryFrom as _, future::Future, io, net::SocketAddr, num::NonZero, pin::Pin, sync::Arc,
};

/// Configuration for a [`JaegerService`].
Expand Down Expand Up @@ -200,10 +199,10 @@ impl JaegerService {
block_hash: &[u8; 32],
operation_name: impl Into<String>,
) -> mick_jaeger::Span {
let trace_id = NonZeroU128::new(u128::from_be_bytes(
let trace_id = NonZero::<u128>::new(u128::from_be_bytes(
<[u8; 16]>::try_from(&block_hash[16..]).unwrap(),
))
.unwrap_or_else(|| NonZeroU128::new(1u128).unwrap());
.unwrap_or_else(|| NonZero::<u128>::new(1u128).unwrap());
self.traces_in.span(trace_id, operation_name)
}

Expand All @@ -230,7 +229,7 @@ impl JaegerService {
buf[8..].copy_from_slice(&local_peer_id[local_peer_id.len() - 8..]);
};

let trace_id = NonZeroU128::new(u128::from_be_bytes(buf)).unwrap();
let trace_id = NonZero::<u128>::new(u128::from_be_bytes(buf)).unwrap();
self.traces_in.span(trace_id, operation_name)
}
}
Expand Down
8 changes: 4 additions & 4 deletions full-node/src/json_rpc_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::{
future::Future,
io, mem,
net::SocketAddr,
num::{NonZeroU32, NonZeroUsize},
num::NonZero,
pin::Pin,
sync::{
atomic::{AtomicU32, Ordering},
Expand Down Expand Up @@ -149,7 +149,7 @@ impl JsonRpcService {
let (virtual_client_main_task, virtual_client_io) =
service::client_main_task(service::Config {
max_active_subscriptions: u32::MAX,
max_pending_requests: NonZeroU32::new(u32::MAX).unwrap(),
max_pending_requests: NonZero::<u32>::new(u32::MAX).unwrap(),
});

spawn_client_main_task(
Expand All @@ -164,7 +164,7 @@ impl JsonRpcService {
runtime_caches_service::Config {
tasks_executor: config.tasks_executor.clone(),
database: config.database.clone(),
num_cache_entries: NonZeroUsize::new(16).unwrap(), // TODO: configurable?
num_cache_entries: NonZero::<usize>::new(16).unwrap(), // TODO: configurable?
},
));

Expand Down Expand Up @@ -349,7 +349,7 @@ impl JsonRpcBackground {
);
let (client_main_task, io) = service::client_main_task(service::Config {
max_active_subscriptions: 128,
max_pending_requests: NonZeroU32::new(64).unwrap(),
max_pending_requests: NonZero::<u32>::new(64).unwrap(),
});
spawn_client_io_task(
&self.tasks_executor,
Expand Down
4 changes: 2 additions & 2 deletions full-node/src/json_rpc_service/chain_head_subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use smoldot::{
};
use std::{
future::Future,
num::NonZeroUsize,
num::NonZero,
pin::{self, Pin},
sync::Arc,
};
Expand Down Expand Up @@ -77,7 +77,7 @@ pub async fn spawn_chain_head_subscription_task(config: Config) -> String {
tasks_executor(Box::pin(async move {
let consensus_service_subscription = config
.consensus_service
.subscribe_all(32, NonZeroUsize::new(32).unwrap())
.subscribe_all(32, NonZero::<usize>::new(32).unwrap())
.await;
let mut consensus_service_subscription_new_blocks =
pin::pin!(consensus_service_subscription.new_blocks);
Expand Down
12 changes: 6 additions & 6 deletions full-node/src/json_rpc_service/legacy_api_subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use smoldot::{
executor::{host::HostVmPrototype, CoreVersion},
trie,
};
use std::{collections::BTreeSet, iter, mem, num::NonZeroUsize, ops, pin::Pin, sync::Arc};
use std::{collections::BTreeSet, iter, mem, num::NonZero, ops, pin::Pin, sync::Arc};

use crate::{consensus_service, database_thread};

Expand Down Expand Up @@ -58,7 +58,7 @@ impl SubscribeAllHeads {
None => {
let subscribe_all = self
.consensus_service
.subscribe_all(32, NonZeroUsize::new(usize::MAX).unwrap())
.subscribe_all(32, NonZero::<usize>::new(usize::MAX).unwrap())
.await;

let blocks_to_unpin = iter::once(subscribe_all.finalized_block_hash)
Expand Down Expand Up @@ -137,7 +137,7 @@ impl SubscribeFinalizedHeads {
None => {
let subscribe_all = self
.consensus_service
.subscribe_all(32, NonZeroUsize::new(usize::MAX).unwrap())
.subscribe_all(32, NonZero::<usize>::new(usize::MAX).unwrap())
.await;

let mut pinned_blocks = HashMap::with_capacity(
Expand Down Expand Up @@ -248,7 +248,7 @@ impl SubscribeNewHeads {
if self.subscription.is_none() {
let subscribe_all = self
.consensus_service
.subscribe_all(32, NonZeroUsize::new(usize::MAX).unwrap())
.subscribe_all(32, NonZero::<usize>::new(usize::MAX).unwrap())
.await;

let mut pinned_blocks = HashMap::with_capacity(
Expand Down Expand Up @@ -413,7 +413,7 @@ impl SubscribeRuntimeVersion {
if self.subscription.is_none() {
let subscribe_all = self
.consensus_service
.subscribe_all(32, NonZeroUsize::new(usize::MAX).unwrap())
.subscribe_all(32, NonZero::<usize>::new(usize::MAX).unwrap())
.await;

let mut pinned_blocks = HashMap::with_capacity(
Expand Down Expand Up @@ -674,7 +674,7 @@ impl SubscribeStorage {
subscription @ None => {
let subscribe_all = self
.consensus_service
.subscribe_all(32, NonZeroUsize::new(usize::MAX).unwrap())
.subscribe_all(32, NonZero::<usize>::new(usize::MAX).unwrap())
.await;

let mut pinned_blocks_by_hash = HashMap::with_capacity(
Expand Down
4 changes: 2 additions & 2 deletions full-node/src/json_rpc_service/runtime_caches_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use smol::lock::Mutex;
use smoldot::{executor, trie};
use std::{
iter,
num::NonZeroUsize,
num::NonZero,
pin::{self, Pin},
sync::Arc,
};
Expand All @@ -37,7 +37,7 @@ pub struct Config {
pub database: Arc<database_thread::DatabaseThread>,

/// Number of entries in the cache of runtimes.
pub num_cache_entries: NonZeroUsize,
pub num_cache_entries: NonZero<usize>,
}

/// A running runtime caches service.
Expand Down
4 changes: 2 additions & 2 deletions lib/src/author/aura.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use crate::header;
use core::{num::NonZeroU64, time::Duration};
use core::{num::NonZero, time::Duration};

/// Configuration for [`next_slot_claim`].
pub struct Config<'a, TLocAuth> {
Expand All @@ -25,7 +25,7 @@ pub struct Config<'a, TLocAuth> {
pub now_from_unix_epoch: Duration,

/// Duration, in milliseconds, of an Aura slot.
pub slot_duration: NonZeroU64,
pub slot_duration: NonZero<u64>,

/// List of the Aura authorities allowed to produce a block. This is either the same as the
/// ones of the current best block, or a new list if the current best block contains an
Expand Down
4 changes: 2 additions & 2 deletions lib/src/author/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
};

use alloc::vec::Vec;
use core::{num::NonZeroU64, time::Duration};
use core::{num::NonZero, time::Duration};

pub use runtime::{Nibble, StorageChanges, TrieEntryVersion};

Expand All @@ -44,7 +44,7 @@ pub enum ConfigConsensus<'a, TLocAuth> {
now_from_unix_epoch: Duration,

/// Duration, in milliseconds, of an Aura slot.
slot_duration: NonZeroU64,
slot_duration: NonZero<u64>,

/// List of the Aura authorities allowed to produce a block. This is either the same as
/// the ones of the current best block, or a new list if the current best block contains
Expand Down
6 changes: 3 additions & 3 deletions lib/src/chain/blocks_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ use alloc::{
sync::Arc,
vec::Vec,
};
use core::{cmp, fmt, mem, num::NonZeroU64, ops, time::Duration};
use core::{cmp, fmt, mem, num::NonZero, ops, time::Duration};
use hashbrown::HashMap;

mod finality;
Expand Down Expand Up @@ -549,7 +549,7 @@ enum FinalizedConsensus {
authorities_list: Arc<Vec<header::AuraAuthority>>,

/// Duration, in milliseconds, of a slot.
slot_duration: NonZeroU64,
slot_duration: NonZero<u64>,
},
Babe {
/// See [`chain_information::ChainInformationConsensus::Babe::finalized_block_epoch_information`].
Expand All @@ -559,7 +559,7 @@ enum FinalizedConsensus {
next_epoch_transition: Arc<chain_information::BabeEpochInformation>,

/// See [`chain_information::ChainInformationConsensus::Babe::slots_per_epoch`].
slots_per_epoch: NonZeroU64,
slots_per_epoch: NonZero<u64>,
},
}

Expand Down
Loading

0 comments on commit 837d9c3

Please sign in to comment.