From 3f7744398b9e2c5bf8759c063899d689bd4373f9 Mon Sep 17 00:00:00 2001 From: Frank Dai Date: Fri, 20 Jan 2023 18:55:24 -0800 Subject: [PATCH] build: bump the CRC crate from 1.x to 3.x (#930) --- crates/primitives/Cargo.toml | 2 +- crates/primitives/src/forkid.rs | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index 22430c58a9..d217401638 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -39,7 +39,7 @@ secp256k1 = { version = "0.24.2", default-features = false, features = [ ] } # used for forkid -crc = "1" +crc = "3" # misc bytes = "1.2" diff --git a/crates/primitives/src/forkid.rs b/crates/primitives/src/forkid.rs index 94dbee8400..43d02658f4 100644 --- a/crates/primitives/src/forkid.rs +++ b/crates/primitives/src/forkid.rs @@ -4,7 +4,7 @@ #![deny(missing_docs)] use crate::{BlockNumber, H256}; -use crc::crc32; +use crc::*; use reth_codecs::derive_arbitrary; use reth_rlp::*; use serde::{Deserialize, Serialize}; @@ -15,6 +15,8 @@ use std::{ }; use thiserror::Error; +const CRC_32_IEEE: Crc = Crc::::new(&CRC_32_ISO_HDLC); + /// `CRC32` hash of all previous forks starting from genesis block. #[derive_arbitrary(rlp)] #[derive( @@ -39,14 +41,18 @@ impl fmt::Debug for ForkHash { impl From for ForkHash { fn from(genesis: H256) -> Self { - Self(crc32::checksum_ieee(&genesis[..]).to_be_bytes()) + Self(CRC_32_IEEE.checksum(&genesis[..]).to_be_bytes()) } } impl AddAssign for ForkHash { fn add_assign(&mut self, block: BlockNumber) { let blob = block.to_be_bytes(); - self.0 = crc32::update(u32::from_be_bytes(self.0), &crc32::IEEE_TABLE, &blob).to_be_bytes(); + let digest = CRC_32_IEEE.digest_with_initial(u32::from_be_bytes(self.0)); + let value = digest.finalize(); + let mut digest = CRC_32_IEEE.digest_with_initial(value); + digest.update(&blob); + self.0 = digest.finalize().to_be_bytes(); } }