Skip to content

Commit

Permalink
Update to Rust 1.59 and 2021 edition (#3038)
Browse files Browse the repository at this point in the history
## Proposed Changes

Lots of lint updates related to `flat_map`, `unwrap_or_else` and string patterns. I did a little more creative refactoring in the op pool, but otherwise followed Clippy's suggestions.

## Additional Info

We need this PR to unblock CI.
  • Loading branch information
michaelsproul committed Feb 25, 2022
1 parent c1df5d2 commit 5e1f8a8
Show file tree
Hide file tree
Showing 115 changed files with 173 additions and 188 deletions.
2 changes: 1 addition & 1 deletion account_manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "account_manager"
version = "0.3.5"
authors = ["Paul Hauner <[email protected]>", "Luke Anderson <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
bls = { path = "../crypto/bls" }
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "beacon_node"
version = "2.1.3"
authors = ["Paul Hauner <[email protected]>", "Age Manning <[email protected]"]
edition = "2018"
edition = "2021"

[lib]
name = "beacon_node"
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "beacon_chain"
version = "0.2.0"
authors = ["Paul Hauner <[email protected]>", "Age Manning <[email protected]>"]
edition = "2018"
edition = "2021"
autotests = false # using a single test binary compiles faster

[features]
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2289,7 +2289,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
.position(|(_root, block)| {
block.slot().epoch(T::EthSpec::slots_per_epoch()) > start_epoch
})
.unwrap_or_else(|| filtered_chain_segment.len());
.unwrap_or(filtered_chain_segment.len());

// Split off the first section blocks that are all either within the current epoch of
// the first block. These blocks can all be signature-verified with the same
Expand Down
5 changes: 1 addition & 4 deletions beacon_node/beacon_chain/src/naive_aggregation_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,7 @@ impl<T: AggregateMap> NaiveAggregationPool<T> {

/// Iterate all items in all slots of `self`.
pub fn iter(&self) -> impl Iterator<Item = &T::Value> {
self.maps
.iter()
.map(|(_slot, map)| map.get_map().iter().map(|(_key, value)| value))
.flatten()
self.maps.values().flat_map(|map| map.get_map().values())
}

/// Removes any items with a slot lower than `current_slot` and bars any future
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ pub fn verify_sync_committee_message<T: BeaconChainTypes>(
let pubkey = pubkey_cache
.get_pubkey_from_pubkey_bytes(pubkey_bytes)
.map(Cow::Borrowed)
.ok_or_else(|| Error::UnknownValidatorPubkey(*pubkey_bytes))?;
.ok_or(Error::UnknownValidatorPubkey(*pubkey_bytes))?;

let next_slot_epoch = (sync_message.get_slot() + 1).epoch(T::EthSpec::slots_per_epoch());
let fork = chain.spec.fork_at_epoch(next_slot_epoch);
Expand Down
13 changes: 5 additions & 8 deletions beacon_node/beacon_chain/src/validator_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,11 @@ impl MonitoredValidator {
/// as the value recorded by the validator monitor ignores skip slots.
fn min_inclusion_distance(&self, epoch: &Epoch) -> Option<u64> {
let summaries = self.summaries.read();
summaries
.get(epoch)
.map(|summary| {
summary
.attestation_min_block_inclusion_distance
.map(Into::into)
})
.flatten()
summaries.get(epoch).and_then(|summary| {
summary
.attestation_min_block_inclusion_distance
.map(Into::into)
})
}

/// Maps `func` across the `self.summaries`.
Expand Down
4 changes: 1 addition & 3 deletions beacon_node/beacon_chain/src/validator_pubkey_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ impl<T: BeaconChainTypes> ValidatorPubkeyCache<T> {

/// Get the `PublicKey` for a validator with `PublicKeyBytes`.
pub fn get_pubkey_from_pubkey_bytes(&self, pubkey: &PublicKeyBytes) -> Option<&PublicKey> {
self.get_index(pubkey)
.map(|index| self.get(index))
.flatten()
self.get_index(pubkey).and_then(|index| self.get(index))
}

/// Get the public key (in bytes form) for a validator with index `i`.
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/beacon_chain/tests/sync_committee_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ fn aggregated_gossip_verification() {
get_valid_sync_contribution(&harness, RelativeSyncCommittee::Current);

macro_rules! assert_invalid {
($desc: tt, $attn_getter: expr, $($error: pat) |+ $( if $guard: expr )?) => {
($desc: tt, $attn_getter: expr, $($error: pat_param) |+ $( if $guard: expr )?) => {
assert!(
matches!(
harness
Expand Down Expand Up @@ -505,7 +505,7 @@ fn unaggregated_gossip_verification() {
get_valid_sync_committee_message(&harness, current_slot, RelativeSyncCommittee::Current);

macro_rules! assert_invalid {
($desc: tt, $attn_getter: expr, $subnet_getter: expr, $($error: pat) |+ $( if $guard: expr )?) => {
($desc: tt, $attn_getter: expr, $subnet_getter: expr, $($error: pat_param) |+ $( if $guard: expr )?) => {
assert!(
matches!(
harness
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "client"
version = "0.2.0"
authors = ["Sigma Prime <[email protected]>"]
edition = "2018"
edition = "2021"

[dev-dependencies]
toml = "0.5.6"
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/eth1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "eth1"
version = "0.2.0"
authors = ["Paul Hauner <[email protected]>"]
edition = "2018"
edition = "2021"

[dev-dependencies]
eth1_test_rig = { path = "../../testing/eth1_test_rig" }
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/eth1/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ fn response_result_or_error(response: &str) -> Result<Value, RpcError> {
let json = serde_json::from_str::<Value>(response)
.map_err(|e| RpcError::InvalidJson(e.to_string()))?;

if let Some(error) = json.get("error").map(|e| e.get("message")).flatten() {
if let Some(error) = json.get("error").and_then(|e| e.get("message")) {
let error = error.to_string();
if error.contains(EIP155_ERROR_STR) {
Err(RpcError::Eip155Error)
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/execution_layer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "execution_layer"
version = "0.1.0"
edition = "2018"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion beacon_node/genesis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "genesis"
version = "0.2.0"
authors = ["Paul Hauner <[email protected]>"]
edition = "2018"
edition = "2021"

[dev-dependencies]
eth1_test_rig = { path = "../../testing/eth1_test_rig" }
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/http_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "http_api"
version = "0.1.0"
authors = ["Paul Hauner <[email protected]>"]
edition = "2018"
edition = "2021"
autotests = false # using a single test binary compiles faster

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/http_metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "http_metrics"
version = "0.1.0"
authors = ["Paul Hauner <[email protected]>"]
edition = "2018"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion beacon_node/lighthouse_network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "lighthouse_network"
version = "0.2.0"
authors = ["Sigma Prime <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
discv5 = { version = "0.1.0-beta.13", features = ["libp2p"] }
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/lighthouse_network/src/behaviour/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ impl<TSpec: EthSpec> Behaviour<TSpec> {
GossipKind::Attestation(subnet_id) => {
if let Some(v) = metrics::get_int_gauge(
&metrics::FAILED_ATTESTATION_PUBLISHES_PER_SUBNET,
&[&subnet_id.to_string()],
&[subnet_id.as_ref()],
) {
v.inc()
};
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "network"
version = "0.2.0"
authors = ["Sigma Prime <[email protected]>"]
edition = "2018"
edition = "2021"

[dev-dependencies]
sloggers = { version = "2.1.1", features = ["json"] }
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/operation_pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "operation_pool"
version = "0.2.0"
authors = ["Michael Sproul <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
derivative = "2.1.1"
Expand Down
13 changes: 6 additions & 7 deletions beacon_node/operation_pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,9 +560,8 @@ impl<T: EthSpec> OperationPool<T> {
pub fn get_all_attestations(&self) -> Vec<Attestation<T>> {
self.attestations
.read()
.iter()
.map(|(_, attns)| attns.iter().cloned())
.flatten()
.values()
.flat_map(|attns| attns.iter().cloned())
.collect()
}

Expand All @@ -575,10 +574,10 @@ impl<T: EthSpec> OperationPool<T> {
{
self.attestations
.read()
.iter()
.map(|(_, attns)| attns.iter().cloned())
.flatten()
.filter(filter)
.values()
.flat_map(|attns| attns.iter())
.filter(|attn| filter(*attn))
.cloned()
.collect()
}

Expand Down
15 changes: 7 additions & 8 deletions beacon_node/operation_pool/src/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,19 @@ impl<T: EthSpec> PersistedOperationPool<T> {
/// Reconstruct an `OperationPool`. Sets `sync_contributions` to its `Default` if `self` matches
/// `PersistedOperationPool::Base`.
pub fn into_operation_pool(self) -> Result<OperationPool<T>, OpPoolError> {
let attestations = RwLock::new(self.attestations().to_vec().into_iter().collect());
let attester_slashings =
RwLock::new(self.attester_slashings().to_vec().into_iter().collect());
let attestations = RwLock::new(self.attestations().iter().cloned().collect());
let attester_slashings = RwLock::new(self.attester_slashings().iter().cloned().collect());
let proposer_slashings = RwLock::new(
self.proposer_slashings()
.to_vec()
.into_iter()
.iter()
.cloned()
.map(|slashing| (slashing.signed_header_1.message.proposer_index, slashing))
.collect(),
);
let voluntary_exits = RwLock::new(
self.voluntary_exits()
.to_vec()
.into_iter()
.iter()
.cloned()
.map(|exit| (exit.message.validator_index, exit))
.collect(),
);
Expand All @@ -125,7 +124,7 @@ impl<T: EthSpec> PersistedOperationPool<T> {
},
PersistedOperationPool::Altair(_) => {
let sync_contributions =
RwLock::new(self.sync_contributions()?.to_vec().into_iter().collect());
RwLock::new(self.sync_contributions()?.iter().cloned().collect());

OperationPool {
attestations,
Expand Down
16 changes: 9 additions & 7 deletions beacon_node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,13 +705,15 @@ pub fn set_network_config(
// Appending enr-port to the dns hostname to appease `to_socket_addrs()` parsing.
// Since enr-update is disabled with a dns address, not setting the enr-udp-port
// will make the node undiscoverable.
if let Some(enr_udp_port) = config.enr_udp_port.or_else(|| {
if use_listening_port_as_enr_port_by_default {
Some(config.discovery_port)
} else {
None
}
}) {
if let Some(enr_udp_port) =
config
.enr_udp_port
.or(if use_listening_port_as_enr_port_by_default {
Some(config.discovery_port)
} else {
None
})
{
addr.push_str(&format!(":{}", enr_udp_port));
} else {
return Err(
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "store"
version = "0.2.0"
authors = ["Paul Hauner <[email protected]>"]
edition = "2018"
edition = "2021"

[dev-dependencies]
tempfile = "3.1.0"
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/store/src/hot_cold_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> HotColdDB<E, Hot, Cold>
let partial_state_bytes = self
.cold_db
.get_bytes(DBColumn::BeaconState.into(), state_root.as_bytes())?
.ok_or_else(|| HotColdDBError::MissingRestorePoint(*state_root))?;
.ok_or(HotColdDBError::MissingRestorePoint(*state_root))?;
let mut partial_state: PartialBeaconState<E> =
PartialBeaconState::from_ssz_bytes(&partial_state_bytes, &self.spec)?;

Expand Down
2 changes: 1 addition & 1 deletion beacon_node/timer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "timer"
version = "0.2.0"
authors = ["Sigma Prime <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
beacon_chain = { path = "../beacon_chain" }
Expand Down
2 changes: 1 addition & 1 deletion boot_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "boot_node"
version = "2.1.3"
authors = ["Sigma Prime <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
beacon_node = { path = "../beacon_node" }
Expand Down
2 changes: 1 addition & 1 deletion common/account_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "account_utils"
version = "0.1.0"
authors = ["Paul Hauner <[email protected]>"]
edition = "2018"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion common/clap_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "clap_utils"
version = "0.1.0"
authors = ["Paul Hauner <[email protected]>"]
edition = "2018"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion common/clap_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn get_eth2_network_config(cli_args: &ArgMatches) -> Result<Eth2NetworkConfi

if let Some(string) = parse_optional::<String>(cli_args, "terminal-total-difficulty-override")?
{
let stripped = string.replace(",", "");
let stripped = string.replace(',', "");
let terminal_total_difficulty = Uint256::from_dec_str(&stripped).map_err(|e| {
format!(
"Could not parse --terminal-total-difficulty-override as decimal value: {:?}",
Expand Down
2 changes: 1 addition & 1 deletion common/compare_fields/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "compare_fields"
version = "0.2.0"
authors = ["Paul Hauner <[email protected]>"]
edition = "2018"
edition = "2021"

[dev-dependencies]
compare_fields_derive = { path = "../compare_fields_derive" }
Expand Down
2 changes: 1 addition & 1 deletion common/compare_fields_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "compare_fields_derive"
version = "0.2.0"
authors = ["Paul Hauner <[email protected]>"]
edition = "2018"
edition = "2021"

[lib]
proc-macro = true
Expand Down
2 changes: 1 addition & 1 deletion common/compare_fields_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use syn::{parse_macro_input, DeriveInput};
fn is_slice(field: &syn::Field) -> bool {
field.attrs.iter().any(|attr| {
attr.path.is_ident("compare_fields")
&& attr.tokens.to_string().replace(" ", "") == "(as_slice)"
&& attr.tokens.to_string().replace(' ', "") == "(as_slice)"
})
}

Expand Down
2 changes: 1 addition & 1 deletion common/deposit_contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "deposit_contract"
version = "0.2.0"
authors = ["Paul Hauner <[email protected]>"]
edition = "2018"
edition = "2021"

build = "build.rs"

Expand Down
2 changes: 1 addition & 1 deletion common/directory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "directory"
version = "0.1.0"
authors = ["pawan <[email protected]>"]
edition = "2018"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
Loading

0 comments on commit 5e1f8a8

Please sign in to comment.