Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: enable fmt linting for grouped imports by std external and crates #1603

Merged
merged 3 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions book/src/developers/contributing/rust/imports.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
# Imports

- In `*.rs` files, imports should be split into 3 groups [src](https://github.com/rust-dev-tools/fmt-rfcs/issues/131) and separated by a single line. Within a single group, imported items should be sorted alphabetically.
- Imports from `'std'`
- Imports from external crates
- Imports from crates within trin
- In `*.rs` files, imports should be split into 3 groups [src](https://rust-lang.github.io/rustfmt/?version=v1.6.0&search=#StdExternalCrate) and separated by a single line. Within a single group, imported items should be sorted alphabetically.
1. std, core and alloc,
2. external crates,
3. self, super and crate imports.
- Alphabetize imports in `Cargo.toml`

```rust,ignore
use alloc::alloc::Layout;
use core::f32;
use std::sync::Arc;

use broker::database::PooledConnection;
use chrono::Utc;
use juniper::{FieldError, FieldResult};
use uuid::Uuid;

use super::schema::{Context, Payload};
use super::update::convert_publish_payload;
use crate::models::Event;
```
3 changes: 2 additions & 1 deletion e2store/src/e2store/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ impl E2StoreMemory {

#[cfg(test)]
mod test {
use super::*;
use ethportal_api::utils::bytes::{hex_decode, hex_encode};

use super::*;

// test cases sourced from: https://github.com/ethereum/go-ethereum/pull/26621/

#[test]
Expand Down
3 changes: 1 addition & 2 deletions e2store/src/e2store/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ mod tests {
use rand::Rng;
use trin_utils::dir::create_temp_test_dir;

use crate::e2store::types::VersionEntry;

use super::*;
use crate::e2store::types::VersionEntry;

#[test]
fn test_e2store_stream_write_and_read() -> anyhow::Result<()> {
Expand Down
14 changes: 8 additions & 6 deletions e2store/src/era.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use crate::e2store::{
memory::E2StoreMemory,
types::{Entry, Header, VersionEntry},
use std::{
fs,
io::{Read, Write},
};

use anyhow::{anyhow, ensure};
use ethportal_api::consensus::{
beacon_block::SignedBeaconBlock, beacon_state::BeaconState, fork::ForkName,
};
use ssz::Encode;
use std::{
fs,
io::{Read, Write},

use crate::e2store::{
memory::E2StoreMemory,
types::{Entry, Header, VersionEntry},
};

pub const SLOTS_PER_HISTORICAL_ROOT: usize = 8192;
Expand Down
20 changes: 11 additions & 9 deletions e2store/src/era1.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
use crate::{
e2store::{
memory::E2StoreMemory,
types::{Entry, VersionEntry},
},
types::HeaderEntry,
use std::{
fs,
io::{Read, Write},
};

use alloy::{
primitives::{B256, U256},
rlp::Decodable,
};
use anyhow::ensure;
use ethportal_api::types::execution::{block_body::BlockBody, receipts::Receipts};
use std::{
fs,
io::{Read, Write},

use crate::{
e2store::{
memory::E2StoreMemory,
types::{Entry, VersionEntry},
},
types::HeaderEntry,
};

// <config-name>-<era-number>-<era-count>-<short-historical-root>.era
Expand Down
3 changes: 1 addition & 2 deletions e2store/src/era2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,8 @@ mod tests {
use alloy::primitives::{Address, Bloom, B64};
use trin_utils::dir::create_temp_test_dir;

use crate::e2store::types::VersionEntry;

use super::*;
use crate::e2store::types::VersionEntry;

#[test]
fn test_era2_stream_write_and_read() -> anyhow::Result<()> {
Expand Down
7 changes: 4 additions & 3 deletions ethportal-api/src/beacon.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use alloy::primitives::B256;
use discv5::enr::NodeId;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};

use crate::{
consensus::header::BeaconBlockHeader,
light_client::store::LightClientStore,
Expand All @@ -12,9 +16,6 @@ use crate::{
},
RawContentValue, RoutingTableInfo,
};
use alloy::primitives::B256;
use discv5::enr::NodeId;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};

/// Portal Beacon JSON-RPC endpoints
#[rpc(client, server, namespace = "portal")]
Expand Down
3 changes: 2 additions & 1 deletion ethportal-api/src/dashboard/grafana.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::fs;

use base64;
use nanotemplate::template;
use serde::Deserialize;
use std::fs;
use ureq;

pub const DASHBOARD_TEMPLATES: &[&str] =
Expand Down
5 changes: 3 additions & 2 deletions ethportal-api/src/discv5.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use discv5::enr::NodeId;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};

use crate::types::{
discv5::{NodeInfo, RoutingTableInfo},
enr::Enr,
};
use discv5::enr::NodeId;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};

/// Discv5 JSON-RPC endpoints
#[rpc(client, server, namespace = "discv5")]
Expand Down
5 changes: 3 additions & 2 deletions ethportal-api/src/history.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use discv5::enr::NodeId;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};

use crate::{
types::{
content_key::history::HistoryContentKey,
Expand All @@ -10,8 +13,6 @@ use crate::{
},
RawContentValue, RoutingTableInfo,
};
use discv5::enr::NodeId;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};

/// Portal History JSON-RPC endpoints
#[rpc(client, server, namespace = "portal")]
Expand Down
8 changes: 3 additions & 5 deletions ethportal-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ pub use beacon::{BeaconNetworkApiClient, BeaconNetworkApiServer};
pub use discv5::{Discv5ApiClient, Discv5ApiServer};
pub use eth::{EthApiClient, EthApiServer};
pub use history::{HistoryNetworkApiClient, HistoryNetworkApiServer};
// Re-exports jsonrpsee crate
pub use jsonrpsee;
pub use state::{StateNetworkApiClient, StateNetworkApiServer};
pub use web3::{Web3ApiClient, Web3ApiServer};

pub use types::{
consensus,
consensus::light_client,
Expand All @@ -47,8 +47,6 @@ pub use types::{
node_id::*,
portal::{RawContentKey, RawContentValue},
};

// Re-exports jsonrpsee crate
pub use jsonrpsee;
pub use web3::{Web3ApiClient, Web3ApiServer};

shadow_rs::shadow!(build_info);
5 changes: 3 additions & 2 deletions ethportal-api/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use discv5::enr::NodeId;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};

use crate::{
types::{
content_key::state::StateContentKey,
Expand All @@ -10,8 +13,6 @@ use crate::{
},
RawContentValue, RoutingTableInfo,
};
use discv5::enr::NodeId;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};

/// Portal State JSON-RPC endpoints
#[rpc(client, server, namespace = "portal")]
Expand Down
3 changes: 2 additions & 1 deletion ethportal-api/src/types/bootnodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ impl FromStr for Bootnodes {
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod test {
use rstest::rstest;

use super::*;
use crate::types::cli::TrinConfig;
use rstest::rstest;

#[test_log::test]
fn test_bootnodes_default_with_default_bootnodes() {
Expand Down
7 changes: 4 additions & 3 deletions ethportal-api/src/types/cli.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::{env, ffi::OsString, fmt, net::SocketAddr, path::PathBuf, str::FromStr, sync::Arc};

use alloy::primitives::B256;
use clap::{
arg,
builder::ArgPredicate,
error::{Error, ErrorKind},
Args, Parser, Subcommand,
};
use std::{env, ffi::OsString, fmt, net::SocketAddr, path::PathBuf, str::FromStr, sync::Arc};
use url::Url;

use crate::{
Expand All @@ -24,9 +25,8 @@ pub const DEFAULT_NETWORK: &str = "mainnet";
pub const DEFAULT_STORAGE_CAPACITY_MB: &str = "1000";
pub const DEFAULT_WEB3_TRANSPORT: &str = "ipc";

use crate::dashboard::grafana::{GrafanaAPI, DASHBOARD_TEMPLATES};

use super::portal_wire::{NetworkSpec, ANGELFOOD, MAINNET};
use crate::dashboard::grafana::{GrafanaAPI, DASHBOARD_TEMPLATES};

#[derive(Debug, PartialEq, Clone)]
pub enum Web3TransportType {
Expand Down Expand Up @@ -555,6 +555,7 @@ pub fn create_dashboard(
#[cfg(test)]
mod tests {
use std::net::{IpAddr, Ipv4Addr};

use test_log::test;

use super::*;
Expand Down
19 changes: 11 additions & 8 deletions ethportal-api/src/types/consensus/beacon_block.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
use crate::consensus::{
body::{BeaconBlockBodyBellatrix, BeaconBlockBodyCapella, BeaconBlockBodyDeneb},
fork::ForkName,
signature::BlsSignature,
};
use alloy::primitives::B256;
use jsonrpsee::core::Serialize;
use rs_merkle::{algorithms::Sha256, MerkleTree};
Expand All @@ -14,6 +9,12 @@ use superstruct::superstruct;
use tree_hash::TreeHash;
use tree_hash_derive::TreeHash;

use crate::consensus::{
body::{BeaconBlockBodyBellatrix, BeaconBlockBodyCapella, BeaconBlockBodyDeneb},
fork::ForkName,
signature::BlsSignature,
};

/// A block of the `BeaconChain`.
#[superstruct(
variants(Bellatrix, Capella, Deneb),
Expand Down Expand Up @@ -190,12 +191,14 @@ impl SignedBeaconBlock {
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod test {
use super::*;
use crate::consensus::fork::ForkName;
use std::str::FromStr;

use ::ssz::Encode;
use rstest::rstest;
use serde_json::Value;
use std::str::FromStr;

use super::*;
use crate::consensus::fork::ForkName;

#[rstest]
#[case("case_0")]
Expand Down
34 changes: 19 additions & 15 deletions ethportal-api/src/types/consensus/beacon_state.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
use crate::consensus::{
body::{Checkpoint, Eth1Data},
execution_payload::{
ExecutionPayloadHeaderBellatrix, ExecutionPayloadHeaderCapella, ExecutionPayloadHeaderDeneb,
},
fork::ForkName,
header::BeaconBlockHeader,
historical_summaries::HistoricalSummaries,
participation_flags::ParticipationFlags,
pubkey::PubKey,
sync_committee::SyncCommittee,
};
use std::sync::Arc;

use alloy::primitives::B256;
use discv5::enr::k256::elliptic_curve::consts::{U1099511627776, U2048, U4, U65536, U8192};
use jsonrpsee::core::Serialize;
Expand All @@ -20,11 +10,23 @@ use serde_utils;
use ssz::{Decode, DecodeError, Encode};
use ssz_derive::{Decode, Encode};
use ssz_types::{typenum::U16777216, BitVector, FixedVector, VariableList};
use std::sync::Arc;
use superstruct::superstruct;
use tree_hash::{Hash256, TreeHash};
use tree_hash_derive::TreeHash;

use crate::consensus::{
body::{Checkpoint, Eth1Data},
execution_payload::{
ExecutionPayloadHeaderBellatrix, ExecutionPayloadHeaderCapella, ExecutionPayloadHeaderDeneb,
},
fork::ForkName,
header::BeaconBlockHeader,
historical_summaries::HistoricalSummaries,
participation_flags::ParticipationFlags,
pubkey::PubKey,
sync_committee::SyncCommittee,
};

type SlotsPerHistoricalRoot = U8192; // uint64(2**13) (= 8,192)
type HistoricalRootsLimit = U16777216; // uint64(2**24) (= 16,777,216)
type ValidatorRegistryLimit = U1099511627776;
Expand Down Expand Up @@ -342,11 +344,13 @@ impl HistoricalBatch {
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod test {
use super::*;
use std::str::FromStr;

use ::ssz::Encode;
use rstest::rstest;
use serde_json::Value;
use std::str::FromStr;

use super::*;

#[rstest]
#[case("case_0")]
Expand Down
Loading
Loading