diff --git a/Cargo.toml b/Cargo.toml index e398bd941388..455331394117 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,9 @@ license = "MIT OR Apache-2.0" description = "Rust Filecoin implementation." exclude = [".config", ".github", ".maintain", "documentation", "scripts", "interop-tests", "go.work*"] +[lints] +workspace = true + [lib] name = "forest" @@ -358,6 +361,10 @@ required-features = ["benchmark-private"] # See https://docs.rs/about/metadata rustdoc-args = ["--document-private-items"] +[workspace.lints.clippy] +ref_option = "deny" +ref_option_ref = "deny" + [workspace] members = ["interop-tests"] resolver = "3" diff --git a/interop-tests/Cargo.toml b/interop-tests/Cargo.toml index c07527cc8277..d94fec9baa2e 100644 --- a/interop-tests/Cargo.toml +++ b/interop-tests/Cargo.toml @@ -8,6 +8,9 @@ license = "MIT OR Apache-2.0" description = "Interop tests for Forest." publish = false +[lints] +workspace = true + [dependencies] [dev-dependencies] diff --git a/src/chain/tests.rs b/src/chain/tests.rs index 23905f5f184d..d3350e516e03 100644 --- a/src/chain/tests.rs +++ b/src/chain/tests.rs @@ -75,12 +75,12 @@ async fn test_export_inner(version: FilecoinSnapshotVersion) -> anyhow::Result<( match version { FilecoinSnapshotVersion::V1 => { - assert_eq!(car.metadata(), &None); + assert_eq!(car.metadata(), None); } FilecoinSnapshotVersion::V2 => { assert_eq!( car.metadata(), - &Some(FilecoinSnapshotMetadata { + Some(&FilecoinSnapshotMetadata { version, head_tipset_key: head_key_cids, f3_data: None, diff --git a/src/cli/subcommands/mpool_cmd.rs b/src/cli/subcommands/mpool_cmd.rs index 2cd302107a2f..e486b42e83cf 100644 --- a/src/cli/subcommands/mpool_cmd.rs +++ b/src/cli/subcommands/mpool_cmd.rs @@ -49,8 +49,8 @@ pub enum MpoolCommands { fn filter_messages( messages: Vec, local_addrs: Option>, - to: &Option, - from: &Option, + to: Option<&StrictAddress>, + from: Option<&StrictAddress>, ) -> anyhow::Result> { use crate::message::Message; @@ -61,8 +61,10 @@ fn filter_messages( .as_ref() .map(|addrs| addrs.contains(&msg.from())) .unwrap_or(true) - && to.map(|addr| msg.to() == addr.into()).unwrap_or(true) - && from.map(|addr| msg.from() == addr.into()).unwrap_or(true) + && to.map(|addr| msg.to() == (*addr).into()).unwrap_or(true) + && from + .map(|addr| msg.from() == (*addr).into()) + .unwrap_or(true) }) .collect(); @@ -218,7 +220,8 @@ impl MpoolCommands { None }; - let filtered_messages = filter_messages(messages, local_addrs, &to, &from)?; + let filtered_messages = + filter_messages(messages, local_addrs, to.as_ref(), from.as_ref())?; for msg in filtered_messages { if cids { @@ -250,7 +253,7 @@ impl MpoolCommands { None }; - let messages: Vec = filter_messages(messages, local_addrs, &None, &None)? + let messages: Vec = filter_messages(messages, local_addrs, None, None)? .into_iter() .map(|it| it.message) .collect(); @@ -304,7 +307,7 @@ mod tests { let smsg_json_vec = smsg_vec.clone().into_iter().collect_vec(); // No filtering is set up - let smsg_filtered: Vec = filter_messages(smsg_json_vec, None, &None, &None) + let smsg_filtered: Vec = filter_messages(smsg_json_vec, None, None, None) .unwrap() .into_iter() .collect(); @@ -346,7 +349,7 @@ mod tests { // Filter local addresses let smsg_filtered: Vec = - filter_messages(smsg_json_vec, Some(local_addrs), &None, &None) + filter_messages(smsg_json_vec, Some(local_addrs), None, None) .unwrap() .into_iter() .collect(); @@ -379,7 +382,7 @@ mod tests { // Filtering messages from sender2 let smsg_filtered: Vec = - filter_messages(smsg_json_vec, None, &None, &Some(sender2.into())) + filter_messages(smsg_json_vec, None, None, Some(&sender2.into())) .unwrap() .into_iter() .collect(); @@ -412,7 +415,7 @@ mod tests { // Filtering messages to target2 let smsg_filtered: Vec = - filter_messages(smsg_json_vec, None, &Some(target2.into()), &None) + filter_messages(smsg_json_vec, None, Some(&target2.into()), None) .unwrap() .into_iter() .collect(); diff --git a/src/daemon/db_util.rs b/src/daemon/db_util.rs index 88533ed8d3da..1ef93db810a2 100644 --- a/src/daemon/db_util.rs +++ b/src/daemon/db_util.rs @@ -243,7 +243,7 @@ pub async fn import_chain_as_forest_car( let forest_car = ForestCar::try_from(forest_car_db_path.as_path())?; - if let Some(f3_cid) = forest_car.metadata().as_ref().and_then(|m| m.f3_data) { + if let Some(f3_cid) = forest_car.metadata().and_then(|m| m.f3_data) { if crate::f3::get_f3_sidecar_params(chain_config) .initial_power_table .is_none() diff --git a/src/db/car/any.rs b/src/db/car/any.rs index ccd5174d5a36..97fa3e60c874 100644 --- a/src/db/car/any.rs +++ b/src/db/car/any.rs @@ -54,7 +54,7 @@ impl AnyCar { )) } - pub fn metadata(&self) -> &Option { + pub fn metadata(&self) -> Option<&FilecoinSnapshotMetadata> { match self { AnyCar::Forest(forest) => forest.metadata(), AnyCar::Plain(plain) => plain.metadata(), diff --git a/src/db/car/forest.rs b/src/db/car/forest.rs index 54a026bf4ab3..28512d926a40 100644 --- a/src/db/car/forest.rs +++ b/src/db/car/forest.rs @@ -118,18 +118,20 @@ impl ForestCar { }) } - pub fn metadata(&self) -> &Option { - self.metadata.get_or_init(|| { - if self.header.roots.len() == super::V2_SNAPSHOT_ROOT_COUNT { - let maybe_metadata_cid = self.header.roots.first(); - if let Ok(Some(metadata)) = - self.get_cbor::(maybe_metadata_cid) - { - return Some(metadata); + pub fn metadata(&self) -> Option<&FilecoinSnapshotMetadata> { + self.metadata + .get_or_init(|| { + if self.header.roots.len() == super::V2_SNAPSHOT_ROOT_COUNT { + let maybe_metadata_cid = self.header.roots.first(); + if let Ok(Some(metadata)) = + self.get_cbor::(maybe_metadata_cid) + { + return Some(metadata); + } } - } - None - }) + None + }) + .as_ref() } pub fn is_valid(reader: &ReaderT) -> bool { diff --git a/src/db/car/plain.rs b/src/db/car/plain.rs index 9672818a2da1..fedd044f0ff6 100644 --- a/src/db/car/plain.rs +++ b/src/db/car/plain.rs @@ -173,18 +173,20 @@ impl PlainCar { } } - pub fn metadata(&self) -> &Option { - self.metadata.get_or_init(|| { - if self.header_v1.roots.len() == super::V2_SNAPSHOT_ROOT_COUNT { - let maybe_metadata_cid = self.header_v1.roots.first(); - if let Ok(Some(metadata)) = - self.get_cbor::(maybe_metadata_cid) - { - return Some(metadata); + pub fn metadata(&self) -> Option<&FilecoinSnapshotMetadata> { + self.metadata + .get_or_init(|| { + if self.header_v1.roots.len() == super::V2_SNAPSHOT_ROOT_COUNT { + let maybe_metadata_cid = self.header_v1.roots.first(); + if let Ok(Some(metadata)) = + self.get_cbor::(maybe_metadata_cid) + { + return Some(metadata); + } } - } - None - }) + None + }) + .as_ref() } pub fn head_tipset_key(&self) -> &NonEmpty { diff --git a/src/eth/eip_1559_transaction.rs b/src/eth/eip_1559_transaction.rs index 76a223829e64..78c58c4b7659 100644 --- a/src/eth/eip_1559_transaction.rs +++ b/src/eth/eip_1559_transaction.rs @@ -108,7 +108,7 @@ impl EthEip1559TxArgs { .append(&format_bigint(&self.max_priority_fee_per_gas)?) .append(&format_bigint(&self.max_fee_per_gas)?) .append(&format_u64(self.gas_limit)) - .append(&format_address(&self.to)) + .append(&format_address(self.to.as_ref())) .append(&format_bigint(&self.value)?) .append(&self.input) .append_list(access_list); @@ -156,7 +156,7 @@ impl EthEip1559TxArgs { self.chain_id, eth_chain_id ); - let method_info = get_filecoin_method_info(&self.to, &self.input)?; + let method_info = get_filecoin_method_info(self.to.as_ref(), &self.input)?; Ok(Message { version: 0, from, diff --git a/src/eth/eip_155_transaction.rs b/src/eth/eip_155_transaction.rs index ddc4c6d23b1b..16753c0feb92 100644 --- a/src/eth/eip_155_transaction.rs +++ b/src/eth/eip_155_transaction.rs @@ -191,7 +191,7 @@ impl EthLegacyEip155TxArgs { .append(&format_u64(self.nonce)) .append(&format_bigint(&self.gas_price)?) .append(&format_u64(self.gas_limit)) - .append(&format_address(&self.to)) + .append(&format_address(self.to.as_ref())) .append(&format_bigint(&self.value)?) .append(&self.input); Ok(stream) @@ -240,7 +240,7 @@ impl EthLegacyEip155TxArgs { validate_eip155_chain_id(eth_chain_id, &self.v).is_ok(), "Failed to validate EIP155 chain Id" ); - let method_info = get_filecoin_method_info(&self.to, &self.input)?; + let method_info = get_filecoin_method_info(self.to.as_ref(), &self.input)?; Ok(Message { version: 0, from, diff --git a/src/eth/homestead_transaction.rs b/src/eth/homestead_transaction.rs index e0700de81e6d..68150a05675b 100644 --- a/src/eth/homestead_transaction.rs +++ b/src/eth/homestead_transaction.rs @@ -162,7 +162,7 @@ impl EthLegacyHomesteadTxArgs { .append(&format_u64(self.nonce)) .append(&format_bigint(&self.gas_price)?) .append(&format_u64(self.gas_limit)) - .append(&format_address(&self.to)) + .append(&format_address(self.to.as_ref())) .append(&format_bigint(&self.value)?) .append(&self.input); Ok(stream) @@ -195,7 +195,7 @@ impl EthLegacyHomesteadTxArgs { /// Constructs an unsigned message using legacy homestead transaction args pub fn get_unsigned_message(&self, from: Address) -> anyhow::Result { - let method_info = get_filecoin_method_info(&self.to, &self.input)?; + let method_info = get_filecoin_method_info(self.to.as_ref(), &self.input)?; Ok(Message { version: 0, from, diff --git a/src/eth/transaction.rs b/src/eth/transaction.rs index c480aa4ea2ef..0e7a199ea480 100644 --- a/src/eth/transaction.rs +++ b/src/eth/transaction.rs @@ -299,7 +299,7 @@ pub fn format_bigint(value: &BigInt) -> anyhow::Result { }) } -pub fn format_address(value: &Option) -> Bytes { +pub fn format_address(value: Option<&EthAddress>) -> Bytes { if let Some(addr) = value { addr.0.as_bytes().to_vec().into() } else { @@ -470,7 +470,7 @@ pub struct MethodInfo { /// Retrieves method info pub fn get_filecoin_method_info( - recipient: &Option, + recipient: Option<&EthAddress>, input: &[u8], ) -> anyhow::Result { let params = if !input.is_empty() { diff --git a/src/ipld/tests/selector_explore.rs b/src/ipld/tests/selector_explore.rs index cae397fba3ae..5771ee7e309d 100644 --- a/src/ipld/tests/selector_explore.rs +++ b/src/ipld/tests/selector_explore.rs @@ -21,22 +21,22 @@ struct TestVector { // Just needed because cannot deserialize the current selector position in // recursive selectors -fn test_equal(s1: &Option, s2: &Option) -> bool { +fn test_equal(s1: Option<&Selector>, s2: Option<&Selector>) -> bool { use Selector::*; if let ( - &Some(ExploreRecursive { + Some(ExploreRecursive { current: _, sequence: s1, limit: l1, stop_at: st1, }), - &Some(ExploreRecursive { + Some(ExploreRecursive { current: _, sequence: s2, limit: l2, stop_at: st2, }), - ) = (&s1, &s2) + ) = (s1, s2) { s1 == s2 && l1 == l2 && st1 == st2 } else { @@ -60,7 +60,7 @@ fn selector_explore_tests() { for tv in vectors { let result = process_vector(tv.initial_selector, tv.explore); assert!( - test_equal(&result, &tv.result_selector), + test_equal(result.as_ref(), tv.result_selector.as_ref()), "({}) Failed:\nExpected: {:?}\nFound: {:?}", tv.description .unwrap_or_else(|| "Unnamed test case".to_owned()), diff --git a/src/rpc/methods/chain.rs b/src/rpc/methods/chain.rs index 136c386cecdb..bb76787fd011 100644 --- a/src/rpc/methods/chain.rs +++ b/src/rpc/methods/chain.rs @@ -1066,7 +1066,7 @@ pub enum ChainGetTipSetV2 {} impl ChainGetTipSetV2 { pub async fn get_tipset_by_anchor( ctx: &Ctx, - anchor: &Option, + anchor: Option<&TipsetAnchor>, ) -> anyhow::Result { if let Some(anchor) = anchor { match (&anchor.key.0, &anchor.tag) { @@ -1162,7 +1162,7 @@ impl ChainGetTipSetV2 { } // Get tipset by height. if let Some(height) = &selector.height { - let anchor = Self::get_tipset_by_anchor(ctx, &height.anchor).await?; + let anchor = Self::get_tipset_by_anchor(ctx, height.anchor.as_ref()).await?; let ts = ctx.chain_index().tipset_by_height( height.at, anchor, diff --git a/src/rpc/methods/eth.rs b/src/rpc/methods/eth.rs index 75514171ef38..6029427b5aa6 100644 --- a/src/rpc/methods/eth.rs +++ b/src/rpc/methods/eth.rs @@ -3790,7 +3790,7 @@ async fn trace_filter( for block_trace in block_traces { if block_trace .trace - .match_filter_criteria(&filter.from_address, &filter.to_address)? + .match_filter_criteria(filter.from_address.as_ref(), filter.to_address.as_ref())? { trace_counter += 1; if let Some(after) = filter.after diff --git a/src/rpc/methods/eth/filter/mod.rs b/src/rpc/methods/eth/filter/mod.rs index 5769590ddb46..dec8bec0c854 100644 --- a/src/rpc/methods/eth/filter/mod.rs +++ b/src/rpc/methods/eth/filter/mod.rs @@ -176,7 +176,7 @@ impl EthEventHandler { fn install_filter( &self, - filter_manager: &Option>, + filter_manager: Option<&dyn FilterManager>, ) -> Result { if let Some(manager) = filter_manager { let filter = manager.install().context("Installation error")?; @@ -194,20 +194,12 @@ impl EthEventHandler { // Installs an eth block filter pub fn eth_new_block_filter(&self) -> Result { - let filter_manager: Option> = self - .tipset_filter_manager - .as_ref() - .map(|fm| Arc::clone(fm) as Arc); - self.install_filter(&filter_manager) + self.install_filter(self.tipset_filter_manager.as_deref().map(|fm| fm as _)) } // Installs an eth pending transaction filter pub fn eth_new_pending_transaction_filter(&self) -> Result { - let filter_manager: Option> = self - .mempool_filter_manager - .as_ref() - .map(|fm| Arc::clone(fm) as Arc); - self.install_filter(&filter_manager) + self.install_filter(self.mempool_filter_manager.as_deref().map(|fm| fm as _)) } fn uninstall_filter(&self, filter: Arc) -> Result<(), Error> { diff --git a/src/rpc/methods/eth/types.rs b/src/rpc/methods/eth/types.rs index ef5759995d9a..3c01deb9645e 100644 --- a/src/rpc/methods/eth/types.rs +++ b/src/rpc/methods/eth/types.rs @@ -728,8 +728,8 @@ lotus_json_with_self!(EthTraceFilterCriteria); impl EthTrace { pub fn match_filter_criteria( &self, - from_decoded_addresses: &Option, - to_decoded_addresses: &Option, + from_decoded_addresses: Option<&EthAddressList>, + to_decoded_addresses: Option<&EthAddressList>, ) -> Result { let (trace_to, trace_from) = match &self.action { TraceAction::Call(action) => (action.to, action.from), diff --git a/src/rpc/types/tipset_selector.rs b/src/rpc/types/tipset_selector.rs index c3f0632ee8bd..7ea07f85c182 100644 --- a/src/rpc/types/tipset_selector.rs +++ b/src/rpc/types/tipset_selector.rs @@ -67,7 +67,7 @@ impl TipsetHeight { self.at >= 0, "invalid tipset height: epoch cannot be less than zero" ); - TipsetAnchor::validate(&self.anchor)?; + TipsetAnchor::validate(self.anchor.as_ref())?; Ok(()) } @@ -100,7 +100,7 @@ impl TipsetAnchor { /// /// Note that a [`None`] anchor is valid, and is considered to be /// equivalent to the default anchor, which is the tipset tagged as [`TipsetTag::Finalized`]. - pub fn validate(anchor: &Option) -> anyhow::Result<()> { + pub fn validate(anchor: Option<&Self>) -> anyhow::Result<()> { if let Some(anchor) = anchor { anyhow::ensure!( anchor.key.0.is_none() || anchor.tag.is_none(), diff --git a/src/shim/actors/convert.rs b/src/shim/actors/convert.rs index bbd203bce4c6..237d3c9e53ae 100644 --- a/src/shim/actors/convert.rs +++ b/src/shim/actors/convert.rs @@ -90,8 +90,8 @@ pub fn from_token_v4_to_v2(token: &TokenAmountV4) -> TokenAmountV2 { TokenAmountV2::from_atto(token.atto().clone()) } -pub fn from_opt_token_v4_to_v2(token: &Option) -> TokenAmountV2 { - token.as_ref().map_or(TokenAmountV2::default(), |t| { +pub fn from_opt_token_v4_to_v2(token: Option<&TokenAmountV4>) -> TokenAmountV2 { + token.map_or(TokenAmountV2::default(), |t| { TokenAmountV2::from_atto(t.atto().clone()) }) } diff --git a/src/utils/net/download_file.rs b/src/utils/net/download_file.rs index c50eaad793b4..0e004cb01628 100644 --- a/src/utils/net/download_file.rs +++ b/src/utils/net/download_file.rs @@ -72,7 +72,7 @@ fn gen_tmp_download_path(dst_path: &Path) -> PathBuf { /// Call user-provided callback with progress percentage fn call_progress_callback( - callback: &Option>, + callback: Option<&(dyn Fn(String) + Sync + Send)>, downloaded: u64, total_size: u64, ) { @@ -388,7 +388,7 @@ async fn download_http_parallel( } // Also call user callback if provided (for RPC state tracking) - call_progress_callback(&callback, downloaded, total_size); + call_progress_callback(callback.as_deref(), downloaded, total_size); } file.flush().await?;