From 34454fd585d7e5e015e53c80de5eb4bf86227adc Mon Sep 17 00:00:00 2001 From: teor Date: Tue, 14 Nov 2023 15:23:37 +1000 Subject: [PATCH 1/6] Remove unnecessary try_into() with cargo clippy --fix --- zebra-chain/src/sapling/arbitrary.rs | 2 +- zebra-consensus/src/primitives/halo2/tests.rs | 5 ++--- zebra-network/src/constants.rs | 3 +-- zebra-script/src/lib.rs | 2 +- zebra-state/src/service/check/utxo.rs | 2 +- .../src/service/finalized_state/disk_format/block.rs | 6 ++---- zebrad/src/components/mempool/crawler/tests/timing.rs | 3 +-- zebrad/src/components/sync/tests/timing.rs | 3 +-- 8 files changed, 10 insertions(+), 16 deletions(-) diff --git a/zebra-chain/src/sapling/arbitrary.rs b/zebra-chain/src/sapling/arbitrary.rs index 1012cfb8a6e..5ac235d276f 100644 --- a/zebra-chain/src/sapling/arbitrary.rs +++ b/zebra-chain/src/sapling/arbitrary.rs @@ -85,7 +85,7 @@ impl Arbitrary for Output { cv: ExtendedPoint::generator().try_into().unwrap(), cm_u: NoteCommitment(AffinePoint::identity()).extract_u(), ephemeral_key: keys::EphemeralPublicKey( - ExtendedPoint::generator().try_into().unwrap(), + ExtendedPoint::generator().into(), ), enc_ciphertext, out_ciphertext, diff --git a/zebra-consensus/src/primitives/halo2/tests.rs b/zebra-consensus/src/primitives/halo2/tests.rs index fe039fae863..dcdd765d680 100644 --- a/zebra-consensus/src/primitives/halo2/tests.rs +++ b/zebra-consensus/src/primitives/halo2/tests.rs @@ -75,7 +75,7 @@ fn generate_test_vectors() { let action = zebra_chain::orchard::Action { cv: a.cv_net().to_bytes().try_into().unwrap(), nullifier: a.nullifier().to_bytes().try_into().unwrap(), - rk: <[u8; 32]>::from(a.rk()).try_into().unwrap(), + rk: <[u8; 32]>::from(a.rk()).into(), cm_x: pallas::Base::from_repr(a.cmx().into()).unwrap(), ephemeral_key: a.encrypted_note().epk_bytes.try_into().unwrap(), enc_ciphertext: a.encrypted_note().enc_ciphertext.into(), @@ -90,8 +90,7 @@ fn generate_test_vectors() { .try_into() .unwrap(), binding_sig: <[u8; 64]>::from(bundle.authorization().binding_signature()) - .try_into() - .unwrap(), + .into(), } }) .collect(); diff --git a/zebra-network/src/constants.rs b/zebra-network/src/constants.rs index 32c1c477599..961f08a219f 100644 --- a/zebra-network/src/constants.rs +++ b/zebra-network/src/constants.rs @@ -525,8 +525,7 @@ mod tests { INVENTORY_ROTATION_INTERVAL < Duration::from_secs( POST_BLOSSOM_POW_TARGET_SPACING - .try_into() - .expect("non-negative"), + .into(), ), "we should expire inventory every time 1-2 new blocks get generated" ); diff --git a/zebra-script/src/lib.rs b/zebra-script/src/lib.rs index 1d0e88756f4..43ea2fa7328 100644 --- a/zebra-script/src/lib.rs +++ b/zebra-script/src/lib.rs @@ -221,7 +221,7 @@ impl CachedFfiTransaction { }; if err == zcash_script_error_t_zcash_script_ERR_OK { - let ret = ret.try_into().expect("c_uint fits in a u64"); + let ret = ret.into(); Ok(ret) } else { Err(Error::from(err)) diff --git a/zebra-state/src/service/check/utxo.rs b/zebra-state/src/service/check/utxo.rs index 186f89d83af..1f8478c0e61 100644 --- a/zebra-state/src/service/check/utxo.rs +++ b/zebra-state/src/service/check/utxo.rs @@ -197,7 +197,7 @@ pub fn transparent_coinbase_spend( match spend_restriction { OnlyShieldedOutputs { spend_height } => { let min_spend_height = - utxo.height + MIN_TRANSPARENT_COINBASE_MATURITY.try_into().unwrap(); + utxo.height + MIN_TRANSPARENT_COINBASE_MATURITY.into(); let min_spend_height = min_spend_height.expect("valid UTXOs have coinbase heights far below Height::MAX"); if spend_height >= min_spend_height { diff --git a/zebra-state/src/service/finalized_state/disk_format/block.rs b/zebra-state/src/service/finalized_state/disk_format/block.rs index 03a7f648053..cc04a31b3dd 100644 --- a/zebra-state/src/service/finalized_state/disk_format/block.rs +++ b/zebra-state/src/service/finalized_state/disk_format/block.rs @@ -92,8 +92,7 @@ impl TransactionIndex { /// Returns this index as a `usize` pub fn as_usize(&self) -> usize { self.0 - .try_into() - .expect("the maximum valid index fits in usize") + .into() } /// Creates a transaction index from a `u64`. @@ -109,8 +108,7 @@ impl TransactionIndex { #[allow(dead_code)] pub fn as_u64(&self) -> u64 { self.0 - .try_into() - .expect("the maximum valid index fits in u64") + .into() } } diff --git a/zebrad/src/components/mempool/crawler/tests/timing.rs b/zebrad/src/components/mempool/crawler/tests/timing.rs index 381d6638806..091f85750f7 100644 --- a/zebrad/src/components/mempool/crawler/tests/timing.rs +++ b/zebrad/src/components/mempool/crawler/tests/timing.rs @@ -12,8 +12,7 @@ fn ensure_timing_consistent() { assert!( RATE_LIMIT_DELAY.as_secs() < POST_BLOSSOM_POW_TARGET_SPACING - .try_into() - .expect("not negative"), + .into(), "a mempool crawl should complete before most new blocks" ); diff --git a/zebrad/src/components/sync/tests/timing.rs b/zebrad/src/components/sync/tests/timing.rs index 006c08c4e51..a48df9c3aa5 100644 --- a/zebrad/src/components/sync/tests/timing.rs +++ b/zebrad/src/components/sync/tests/timing.rs @@ -81,8 +81,7 @@ fn ensure_timeouts_consistent() { assert!( SYNC_RESTART_DELAY.as_secs() < POST_BLOSSOM_POW_TARGET_SPACING - .try_into() - .expect("not negative"), + .into(), "a syncer tip crawl should complete before most new blocks" ); From d9bf0055fb0cfc6ae4f12bce85ed17b294d31e23 Mon Sep 17 00:00:00 2001 From: teor Date: Tue, 14 Nov 2023 15:32:02 +1000 Subject: [PATCH 2/6] Manually remove some unnecessary try_from()s --- zebra-consensus/src/block/subsidy/general.rs | 4 ++-- zebra-utils/src/bin/zebra-checkpoints/main.rs | 3 +-- zebrad/tests/common/checkpoints.rs | 3 +-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/zebra-consensus/src/block/subsidy/general.rs b/zebra-consensus/src/block/subsidy/general.rs index b3cb0231734..5be97f3a990 100644 --- a/zebra-consensus/src/block/subsidy/general.rs +++ b/zebra-consensus/src/block/subsidy/general.rs @@ -44,8 +44,8 @@ pub fn halving_divisor(height: Height, network: Network) -> Option { Some(halving_div) } else { let pre_blossom_height = blossom_height - SLOW_START_SHIFT; - let scaled_pre_blossom_height = pre_blossom_height - * HeightDiff::try_from(BLOSSOM_POW_TARGET_SPACING_RATIO).expect("constant is positive"); + let scaled_pre_blossom_height = + pre_blossom_height * HeightDiff::from(BLOSSOM_POW_TARGET_SPACING_RATIO); let post_blossom_height = height - blossom_height; diff --git a/zebra-utils/src/bin/zebra-checkpoints/main.rs b/zebra-utils/src/bin/zebra-checkpoints/main.rs index a8d2d4e3e60..cff158b5ca2 100644 --- a/zebra-utils/src/bin/zebra-checkpoints/main.rs +++ b/zebra-utils/src/bin/zebra-checkpoints/main.rs @@ -164,8 +164,7 @@ async fn main() -> Result<()> { // Checkpoints must be on the main chain, so we skip blocks that are within the // Zcash reorg limit. - let height_limit = height_limit - - HeightDiff::try_from(MIN_TRANSPARENT_COINBASE_MATURITY).expect("constant fits in i32"); + let height_limit = height_limit - HeightDiff::from(MIN_TRANSPARENT_COINBASE_MATURITY); let height_limit = height_limit .ok_or_else(|| { eyre!( diff --git a/zebrad/tests/common/checkpoints.rs b/zebrad/tests/common/checkpoints.rs index a1aa1dbb43c..cb1db8d0530 100644 --- a/zebrad/tests/common/checkpoints.rs +++ b/zebrad/tests/common/checkpoints.rs @@ -396,8 +396,7 @@ pub fn wait_for_zebra_checkpoints_generation< test_type: TestType, show_zebrad_logs: bool, ) -> Result<(TestChild, TestChild

)> { - let last_checkpoint_gap = HeightDiff::try_from(MIN_TRANSPARENT_COINBASE_MATURITY) - .expect("constant fits in HeightDiff") + let last_checkpoint_gap = HeightDiff::from(MIN_TRANSPARENT_COINBASE_MATURITY) + HeightDiff::try_from(MAX_CHECKPOINT_HEIGHT_GAP).expect("constant fits in HeightDiff"); let expected_final_checkpoint_height = (zebra_tip_height - last_checkpoint_gap).expect("network tip is high enough"); From cece230ad077369efbf4542b24a24eea2efadfa4 Mon Sep 17 00:00:00 2001 From: teor Date: Tue, 14 Nov 2023 15:37:11 +1000 Subject: [PATCH 3/6] impl From for pallas::Affine instead of TryFrom --- zebra-chain/src/orchard/keys.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/zebra-chain/src/orchard/keys.rs b/zebra-chain/src/orchard/keys.rs index 3fd9e0ce67a..1e01ec0b185 100644 --- a/zebra-chain/src/orchard/keys.rs +++ b/zebra-chain/src/orchard/keys.rs @@ -87,17 +87,12 @@ impl PartialEq<[u8; 11]> for Diversifier { } } -impl TryFrom for pallas::Affine { - type Error = &'static str; - +impl From for pallas::Affine { /// Get a diversified base point from a diversifier value in affine /// representation. - fn try_from(d: Diversifier) -> Result { - if let Ok(projective_point) = pallas::Point::try_from(d) { - Ok(projective_point.into()) - } else { - Err("Invalid Diversifier -> pallas::Affine") - } + fn from(d: Diversifier) -> Self { + let projective_point = pallas::Point::from(d); + projective_point.into() } } From 0f64671123a7fbc4ddb6d2a0822899eb1122afb5 Mon Sep 17 00:00:00 2001 From: teor Date: Tue, 14 Nov 2023 15:37:50 +1000 Subject: [PATCH 4/6] Remove unused imports --- zebrad/src/components/mempool/crawler/tests/timing.rs | 2 +- zebrad/src/components/sync/tests/timing.rs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/zebrad/src/components/mempool/crawler/tests/timing.rs b/zebrad/src/components/mempool/crawler/tests/timing.rs index 091f85750f7..9a5e4608a76 100644 --- a/zebrad/src/components/mempool/crawler/tests/timing.rs +++ b/zebrad/src/components/mempool/crawler/tests/timing.rs @@ -1,6 +1,6 @@ //! Timing tests for the mempool crawler. -use std::convert::TryInto; + use zebra_chain::parameters::POST_BLOSSOM_POW_TARGET_SPACING; use zebra_network::constants::{DEFAULT_CRAWL_NEW_PEER_INTERVAL, HANDSHAKE_TIMEOUT}; diff --git a/zebrad/src/components/sync/tests/timing.rs b/zebrad/src/components/sync/tests/timing.rs index a48df9c3aa5..e250f974bd3 100644 --- a/zebrad/src/components/sync/tests/timing.rs +++ b/zebrad/src/components/sync/tests/timing.rs @@ -1,7 +1,6 @@ //! Check the relationship between various sync timeouts and delays. use std::{ - convert::TryInto, sync::{ atomic::{AtomicU8, Ordering}, Arc, From 8a0665669061a2e40e2f26efbde0f83c0208a952 Mon Sep 17 00:00:00 2001 From: teor Date: Tue, 14 Nov 2023 15:42:39 +1000 Subject: [PATCH 5/6] cargo fmt --all --- zebra-chain/src/sapling/arbitrary.rs | 4 +--- zebra-consensus/src/primitives/halo2/tests.rs | 3 +-- zebra-network/src/constants.rs | 5 +---- zebra-state/src/service/check/utxo.rs | 3 +-- .../src/service/finalized_state/disk_format/block.rs | 6 ++---- .../src/components/mempool/crawler/tests/timing.rs | 6 +----- zebrad/src/components/sync/tests/timing.rs | 12 ++++-------- 7 files changed, 11 insertions(+), 28 deletions(-) diff --git a/zebra-chain/src/sapling/arbitrary.rs b/zebra-chain/src/sapling/arbitrary.rs index 5ac235d276f..403633a1b8e 100644 --- a/zebra-chain/src/sapling/arbitrary.rs +++ b/zebra-chain/src/sapling/arbitrary.rs @@ -84,9 +84,7 @@ impl Arbitrary for Output { .prop_map(|(enc_ciphertext, out_ciphertext, zkproof)| Self { cv: ExtendedPoint::generator().try_into().unwrap(), cm_u: NoteCommitment(AffinePoint::identity()).extract_u(), - ephemeral_key: keys::EphemeralPublicKey( - ExtendedPoint::generator().into(), - ), + ephemeral_key: keys::EphemeralPublicKey(ExtendedPoint::generator().into()), enc_ciphertext, out_ciphertext, zkproof, diff --git a/zebra-consensus/src/primitives/halo2/tests.rs b/zebra-consensus/src/primitives/halo2/tests.rs index dcdd765d680..2c2da9c2a0f 100644 --- a/zebra-consensus/src/primitives/halo2/tests.rs +++ b/zebra-consensus/src/primitives/halo2/tests.rs @@ -89,8 +89,7 @@ fn generate_test_vectors() { .collect::>() .try_into() .unwrap(), - binding_sig: <[u8; 64]>::from(bundle.authorization().binding_signature()) - .into(), + binding_sig: <[u8; 64]>::from(bundle.authorization().binding_signature()).into(), } }) .collect(); diff --git a/zebra-network/src/constants.rs b/zebra-network/src/constants.rs index 961f08a219f..ab9249b5d04 100644 --- a/zebra-network/src/constants.rs +++ b/zebra-network/src/constants.rs @@ -523,10 +523,7 @@ mod tests { assert!( INVENTORY_ROTATION_INTERVAL - < Duration::from_secs( - POST_BLOSSOM_POW_TARGET_SPACING - .into(), - ), + < Duration::from_secs(POST_BLOSSOM_POW_TARGET_SPACING.into(),), "we should expire inventory every time 1-2 new blocks get generated" ); } diff --git a/zebra-state/src/service/check/utxo.rs b/zebra-state/src/service/check/utxo.rs index 1f8478c0e61..324efa3c035 100644 --- a/zebra-state/src/service/check/utxo.rs +++ b/zebra-state/src/service/check/utxo.rs @@ -196,8 +196,7 @@ pub fn transparent_coinbase_spend( match spend_restriction { OnlyShieldedOutputs { spend_height } => { - let min_spend_height = - utxo.height + MIN_TRANSPARENT_COINBASE_MATURITY.into(); + let min_spend_height = utxo.height + MIN_TRANSPARENT_COINBASE_MATURITY.into(); let min_spend_height = min_spend_height.expect("valid UTXOs have coinbase heights far below Height::MAX"); if spend_height >= min_spend_height { diff --git a/zebra-state/src/service/finalized_state/disk_format/block.rs b/zebra-state/src/service/finalized_state/disk_format/block.rs index cc04a31b3dd..c3db39b4423 100644 --- a/zebra-state/src/service/finalized_state/disk_format/block.rs +++ b/zebra-state/src/service/finalized_state/disk_format/block.rs @@ -91,8 +91,7 @@ impl TransactionIndex { /// Returns this index as a `usize` pub fn as_usize(&self) -> usize { - self.0 - .into() + self.0.into() } /// Creates a transaction index from a `u64`. @@ -107,8 +106,7 @@ impl TransactionIndex { /// Returns this index as a `u64` #[allow(dead_code)] pub fn as_u64(&self) -> u64 { - self.0 - .into() + self.0.into() } } diff --git a/zebrad/src/components/mempool/crawler/tests/timing.rs b/zebrad/src/components/mempool/crawler/tests/timing.rs index 9a5e4608a76..e0f58a431b9 100644 --- a/zebrad/src/components/mempool/crawler/tests/timing.rs +++ b/zebrad/src/components/mempool/crawler/tests/timing.rs @@ -1,7 +1,5 @@ //! Timing tests for the mempool crawler. - - use zebra_chain::parameters::POST_BLOSSOM_POW_TARGET_SPACING; use zebra_network::constants::{DEFAULT_CRAWL_NEW_PEER_INTERVAL, HANDSHAKE_TIMEOUT}; @@ -10,9 +8,7 @@ use crate::components::mempool::crawler::RATE_LIMIT_DELAY; #[test] fn ensure_timing_consistent() { assert!( - RATE_LIMIT_DELAY.as_secs() - < POST_BLOSSOM_POW_TARGET_SPACING - .into(), + RATE_LIMIT_DELAY.as_secs() < POST_BLOSSOM_POW_TARGET_SPACING.into(), "a mempool crawl should complete before most new blocks" ); diff --git a/zebrad/src/components/sync/tests/timing.rs b/zebrad/src/components/sync/tests/timing.rs index e250f974bd3..61d5bac60ea 100644 --- a/zebrad/src/components/sync/tests/timing.rs +++ b/zebrad/src/components/sync/tests/timing.rs @@ -1,10 +1,8 @@ //! Check the relationship between various sync timeouts and delays. -use std::{ - sync::{ - atomic::{AtomicU8, Ordering}, - Arc, - }, +use std::sync::{ + atomic::{AtomicU8, Ordering}, + Arc, }; use futures::future; @@ -78,9 +76,7 @@ fn ensure_timeouts_consistent() { ); assert!( - SYNC_RESTART_DELAY.as_secs() - < POST_BLOSSOM_POW_TARGET_SPACING - .into(), + SYNC_RESTART_DELAY.as_secs() < POST_BLOSSOM_POW_TARGET_SPACING.into(), "a syncer tip crawl should complete before most new blocks" ); From 9f8381f0ae06da61448c6bcbec9668195d87c73e Mon Sep 17 00:00:00 2001 From: teor Date: Tue, 14 Nov 2023 15:48:47 +1000 Subject: [PATCH 6/6] Remove redundant comma --- zebra-network/src/constants.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zebra-network/src/constants.rs b/zebra-network/src/constants.rs index ab9249b5d04..db90e4c55db 100644 --- a/zebra-network/src/constants.rs +++ b/zebra-network/src/constants.rs @@ -523,7 +523,7 @@ mod tests { assert!( INVENTORY_ROTATION_INTERVAL - < Duration::from_secs(POST_BLOSSOM_POW_TARGET_SPACING.into(),), + < Duration::from_secs(POST_BLOSSOM_POW_TARGET_SPACING.into()), "we should expire inventory every time 1-2 new blocks get generated" ); }