From b38c583f06760ff5d45897589e727542673632f0 Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Thu, 28 Nov 2024 19:39:41 +0800 Subject: [PATCH] extend multihash_codetable::Code --- Cargo.toml | 2 +- src/blocks/tipset.rs | 6 +-- src/chain/store/chain_store.rs | 6 +-- src/db/car/forest.rs | 4 +- src/db/car/forest/index/hash.rs | 9 ++-- src/db/gc/mod.rs | 6 +-- src/ipld/tests/cbor_test.rs | 2 +- src/ipld/util.rs | 2 +- src/libp2p/hello/message.rs | 2 +- src/libp2p_bitswap/internals/prefix.rs | 6 +-- src/shim/actors/version.rs | 2 +- src/state_migration/nv17/miner.rs | 2 +- src/state_migration/nv21/miner.rs | 2 +- src/tool/subcommands/car_cmd.rs | 2 +- src/utils/db/car_stream.rs | 12 ++--- src/utils/db/mod.rs | 3 +- src/utils/multihash.rs | 61 ++++++++++++++++++++++++-- 17 files changed, 85 insertions(+), 44 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0051a910c6a..8a16d1a8e0f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ flume = "0.11" futures = "0.3" libp2p = { version = "0.54", default-features = false } libp2p-swarm-test = "0.4" -multihash-codetable = "0.1" +multihash-codetable = { version = "0.1", features = ["blake2b", "blake2s", "blake3", "sha2", "sha3", "strobe"] } rust2go = "0.3" tokio = "1" diff --git a/src/blocks/tipset.rs b/src/blocks/tipset.rs index d811a3c37a0..83f51d8c54e 100644 --- a/src/blocks/tipset.rs +++ b/src/blocks/tipset.rs @@ -767,12 +767,12 @@ mod test { fn ensure_state_roots_are_equal() { let h0 = RawBlockHeader { miner_address: Address::new_id(0), - state_root: Cid::new_v1(DAG_CBOR, MultihashCodeLegacy::Identity.digest(&[])), + state_root: Cid::new_v1(DAG_CBOR, MultihashCode::Identity.digest(&[])), ..Default::default() }; let h1 = RawBlockHeader { miner_address: Address::new_id(1), - state_root: Cid::new_v1(DAG_CBOR, MultihashCodeLegacy::Identity.digest(&[1])), + state_root: Cid::new_v1(DAG_CBOR, MultihashCode::Identity.digest(&[1])), ..Default::default() }; assert_eq!( @@ -791,7 +791,7 @@ mod test { miner_address: Address::new_id(1), parents: TipsetKey::from(nonempty![Cid::new_v1( DAG_CBOR, - MultihashCodeLegacy::Identity.digest(&[]) + MultihashCode::Identity.digest(&[]) )]), ..Default::default() }; diff --git a/src/chain/store/chain_store.rs b/src/chain/store/chain_store.rs index 007007a3237..7eea6054aaf 100644 --- a/src/chain/store/chain_store.rs +++ b/src/chain/store/chain_store.rs @@ -689,11 +689,11 @@ mod tests { let gen_block = CachingBlockHeader::new(RawBlockHeader { miner_address: Address::new_id(0), - state_root: Cid::new_v1(DAG_CBOR, MultihashCodeLegacy::Identity.digest(&[])), + state_root: Cid::new_v1(DAG_CBOR, MultihashCode::Identity.digest(&[])), epoch: 1, weight: 2u32.into(), - messages: Cid::new_v1(DAG_CBOR, MultihashCodeLegacy::Identity.digest(&[])), - message_receipts: Cid::new_v1(DAG_CBOR, MultihashCodeLegacy::Identity.digest(&[])), + messages: Cid::new_v1(DAG_CBOR, MultihashCode::Identity.digest(&[])), + message_receipts: Cid::new_v1(DAG_CBOR, MultihashCode::Identity.digest(&[])), ..Default::default() }); let cs = diff --git a/src/db/car/forest.rs b/src/db/car/forest.rs index 071b87b2eea..ec1c44a52e4 100644 --- a/src/db/car/forest.rs +++ b/src/db/car/forest.rs @@ -503,8 +503,8 @@ mod tests { use crate::utils::multihash::prelude::*; // Distinct CIDs may map to the same hash value - let cid_a = Cid::new_v1(0, MultihashCodeLegacy::Identity.digest(&[10])); - let cid_b = Cid::new_v1(0, MultihashCodeLegacy::Identity.digest(&[0])); + let cid_a = Cid::new_v1(0, MultihashCode::Identity.digest(&[10])); + let cid_b = Cid::new_v1(0, MultihashCode::Identity.digest(&[0])); // A and B are _not_ the same... assert_ne!(cid_a, cid_b); // ... but they map to the same hash: diff --git a/src/db/car/forest/index/hash.rs b/src/db/car/forest/index/hash.rs index 5b62f50f316..693a4da9795 100644 --- a/src/db/car/forest/index/hash.rs +++ b/src/db/car/forest/index/hash.rs @@ -123,18 +123,15 @@ mod tests { Cid::from_cbor_blake2b256(&"haskell").unwrap(), 10392497608425502268, ), + (Cid::new_v1(0xAB, MultihashCode::Identity.digest(&[])), 170), ( - Cid::new_v1(0xAB, MultihashCodeLegacy::Identity.digest(&[])), - 170, - ), - ( - Cid::new_v1(0xAC, MultihashCodeLegacy::Identity.digest(&[1, 2, 3, 4])), + Cid::new_v1(0xAC, MultihashCode::Identity.digest(&[1, 2, 3, 4])), 171, ), ( Cid::new_v1( 0xAD, - MultihashCodeLegacy::Identity.digest(&[1, 2, 3, 4, 5, 6, 7, 8]), + MultihashCode::Identity.digest(&[1, 2, 3, 4, 5, 6, 7, 8]), ), 578437695752307371, ), diff --git a/src/db/gc/mod.rs b/src/db/gc/mod.rs index f6b0a2c2f0a..73df5716919 100644 --- a/src/db/gc/mod.rs +++ b/src/db/gc/mod.rs @@ -490,10 +490,8 @@ mod test { let current_epoch = current_epoch as ChainEpochDelta; let persistent_data = [1, 55]; - let persistent_cid = Cid::new_v1( - DAG_CBOR, - MultihashCodeLegacy::Identity.digest(&persistent_data), - ); + let persistent_cid = + Cid::new_v1(DAG_CBOR, MultihashCode::Identity.digest(&persistent_data)); // Make sure we run enough epochs to initiate GC. tester.run_epochs(current_epoch); diff --git a/src/ipld/tests/cbor_test.rs b/src/ipld/tests/cbor_test.rs index 76522834a70..10282f239d9 100644 --- a/src/ipld/tests/cbor_test.rs +++ b/src/ipld/tests/cbor_test.rs @@ -55,7 +55,7 @@ fn cid_conversions_ipld() { assert_eq!(to_ipld(cid).unwrap(), Ipld::Link(cid)); // Test with identity hash (different length prefix for cbor) - let cid = Cid::new_v1(DAG_CBOR, MultihashCodeLegacy::Identity.digest(&[1, 2])); + let cid = Cid::new_v1(DAG_CBOR, MultihashCode::Identity.digest(&[1, 2])); let ipld = ipld!(Ipld::Link(cid)); let ipld2 = to_ipld(cid).unwrap(); assert_eq!(ipld, ipld2); diff --git a/src/ipld/util.rs b/src/ipld/util.rs index 32ffcaa6577..e819475852a 100644 --- a/src/ipld/util.rs +++ b/src/ipld/util.rs @@ -28,7 +28,7 @@ fn should_save_block_to_snapshot(cid: Cid) -> bool { // Don't include identity CIDs. // We only include raw and dagcbor, for now. // Raw for "code" CIDs. - if cid.hash().code() == u64::from(MultihashCodeLegacy::Identity) { + if cid.hash().code() == u64::from(MultihashCode::Identity) { false } else { matches!( diff --git a/src/libp2p/hello/message.rs b/src/libp2p/hello/message.rs index 3a9226b0a36..9c50477a853 100644 --- a/src/libp2p/hello/message.rs +++ b/src/libp2p/hello/message.rs @@ -35,7 +35,7 @@ mod tests { #[test] fn hello_default_ser() { let orig_msg = HelloRequest { - genesis_cid: Cid::new_v1(DAG_CBOR, MultihashCodeLegacy::Identity.digest(&[])), + genesis_cid: Cid::new_v1(DAG_CBOR, MultihashCode::Identity.digest(&[])), heaviest_tipset_weight: Default::default(), heaviest_tipset_height: Default::default(), heaviest_tip_set: NonEmpty::of(Default::default()), diff --git a/src/libp2p_bitswap/internals/prefix.rs b/src/libp2p_bitswap/internals/prefix.rs index 0b9aba6fd45..aa7112a9422 100644 --- a/src/libp2p_bitswap/internals/prefix.rs +++ b/src/libp2p_bitswap/internals/prefix.rs @@ -56,11 +56,7 @@ impl Prefix { /// Create a CID out of the prefix and some data that will be hashed pub fn to_cid(&self, data: &[u8]) -> anyhow::Result { - let mh = if self.mh_type == 0 { - MultihashCodeLegacy::try_from(self.mh_type)?.digest(data) - } else { - MultihashCode::try_from(self.mh_type)?.digest(data) - }; + let mh = MultihashCode::try_from(self.mh_type)?.digest(data); Ok(Cid::new(self.version, self.codec, mh)?) } } diff --git a/src/shim/actors/version.rs b/src/shim/actors/version.rs index 1b0bf26abe5..a9586296784 100644 --- a/src/shim/actors/version.rs +++ b/src/shim/actors/version.rs @@ -24,7 +24,7 @@ static [<$actor:upper _ACTOR_CIDS>]: Lazy> = Lazy::new(|| { // we need to add manually init actors for V0. if Type::$actor_type == Type::Init { - let init = Cid::new_v1(fvm_ipld_encoding::IPLD_RAW, MultihashCodeLegacy::Identity.digest(b"fil/1/init")); + let init = Cid::new_v1(fvm_ipld_encoding::IPLD_RAW, MultihashCode::Identity.digest(b"fil/1/init")); actors.push((0, init)); } actors diff --git a/src/state_migration/nv17/miner.rs b/src/state_migration/nv17/miner.rs index ba3dc4190e0..bd84bfea628 100644 --- a/src/state_migration/nv17/miner.rs +++ b/src/state_migration/nv17/miner.rs @@ -900,7 +900,7 @@ mod tests { "verifiedregistry", "datacap", ] { - let hash = MultihashCodeLegacy::Identity.digest(format!("{prefix}{name}").as_bytes()); + let hash = MultihashCode::Identity.digest(format!("{prefix}{name}").as_bytes()); let code_cid = Cid::new_v1(IPLD_RAW, hash); manifest_data.push((name, code_cid)); } diff --git a/src/state_migration/nv21/miner.rs b/src/state_migration/nv21/miner.rs index 08ec00644fb..b22439e20c7 100644 --- a/src/state_migration/nv21/miner.rs +++ b/src/state_migration/nv21/miner.rs @@ -453,7 +453,7 @@ mod tests { "verifiedregistry", "datacap", ] { - let hash = MultihashCodeLegacy::Identity.digest(format!("{prefix}{name}").as_bytes()); + let hash = MultihashCode::Identity.digest(format!("{prefix}{name}").as_bytes()); let code_cid = Cid::new_v1(IPLD_RAW, hash); manifest_data.push((name, code_cid)); } diff --git a/src/tool/subcommands/car_cmd.rs b/src/tool/subcommands/car_cmd.rs index 0200cea475f..3bcb234da9e 100644 --- a/src/tool/subcommands/car_cmd.rs +++ b/src/tool/subcommands/car_cmd.rs @@ -180,7 +180,7 @@ mod tests { } fn invalid_block(msg: &str) -> CarBlock { - let cid = Cid::new_v1(0, MultihashCodeLegacy::Identity.digest(&[])); + let cid = Cid::new_v1(0, MultihashCode::Identity.digest(&[])); let data = msg.as_bytes().to_vec(); CarBlock { cid, data } } diff --git a/src/utils/db/car_stream.rs b/src/utils/db/car_stream.rs index 55b9b86dbd1..24f688e2335 100644 --- a/src/utils/db/car_stream.rs +++ b/src/utils/db/car_stream.rs @@ -67,15 +67,9 @@ impl CarBlock { } pub fn validate(&self) -> anyhow::Result<()> { - let actual = match self.cid.hash().code() { - 0 => { - let code = MultihashCodeLegacy::try_from(0)?; - Cid::new_v1(self.cid.codec(), code.digest(&self.data)) - } - hash_code => { - let code = MultihashCode::try_from(hash_code)?; - Cid::new_v1(self.cid.codec(), code.digest(&self.data)) - } + let actual = { + let code = MultihashCode::try_from(self.cid.hash().code())?; + Cid::new_v1(self.cid.codec(), code.digest(&self.data)) }; anyhow::ensure!( actual == self.cid, diff --git a/src/utils/db/mod.rs b/src/utils/db/mod.rs index a77e169e556..0c95a596185 100644 --- a/src/utils/db/mod.rs +++ b/src/utils/db/mod.rs @@ -4,12 +4,13 @@ pub mod car_stream; pub mod car_util; +use crate::utils::multihash::prelude::*; use anyhow::Context as _; use cid::Cid; use fvm_ipld_blockstore::Blockstore; use fvm_ipld_encoding::CborStore; use fvm_ipld_encoding::{to_vec, DAG_CBOR}; -use multihash_codetable::{Code, MultihashDigest as _}; +use multihash_codetable::Code; use serde::ser::Serialize; diff --git a/src/utils/multihash.rs b/src/utils/multihash.rs index ce8991e0383..3580a93c95b 100644 --- a/src/utils/multihash.rs +++ b/src/utils/multihash.rs @@ -8,17 +8,72 @@ //! pub mod prelude { - pub use super::MultihashCodeLegacy; - pub use multihash_codetable::{Code as MultihashCode, MultihashDigest as _}; + pub use super::MultihashCode; + pub use multihash_codetable::MultihashDigest as _; } use multihash_derive::MultihashDigest; +/// Extends [`multihash_codetable::Code`] with `Identity` #[derive(Clone, Copy, Debug, Eq, MultihashDigest, PartialEq)] #[mh(alloc_size = 64)] -pub enum MultihashCodeLegacy { +pub enum MultihashCode { #[mh(code = 0x0, hasher = IdentityHasher::<64>)] Identity, + /// SHA-256 (32-byte hash size) + #[mh(code = 0x12, hasher = multihash_codetable::Sha2_256)] + Sha2_256, + /// SHA-512 (64-byte hash size) + #[mh(code = 0x13, hasher = multihash_codetable::Sha2_512)] + Sha2_512, + /// SHA3-224 (28-byte hash size) + #[mh(code = 0x17, hasher = multihash_codetable::Sha3_224)] + Sha3_224, + /// SHA3-256 (32-byte hash size) + #[mh(code = 0x16, hasher = multihash_codetable::Sha3_256)] + Sha3_256, + /// SHA3-384 (48-byte hash size) + #[mh(code = 0x15, hasher = multihash_codetable::Sha3_384)] + Sha3_384, + /// SHA3-512 (64-byte hash size) + #[mh(code = 0x14, hasher = multihash_codetable::Sha3_512)] + Sha3_512, + /// Keccak-224 (28-byte hash size) + #[mh(code = 0x1a, hasher = multihash_codetable::Keccak224)] + Keccak224, + /// Keccak-256 (32-byte hash size) + #[mh(code = 0x1b, hasher = multihash_codetable::Keccak256)] + Keccak256, + /// Keccak-384 (48-byte hash size) + #[mh(code = 0x1c, hasher = multihash_codetable::Keccak384)] + Keccak384, + /// Keccak-512 (64-byte hash size) + #[mh(code = 0x1d, hasher = multihash_codetable::Keccak512)] + Keccak512, + /// BLAKE2b-256 (32-byte hash size) + #[mh(code = 0xb220, hasher = multihash_codetable::Blake2b256)] + Blake2b256, + /// BLAKE2b-512 (64-byte hash size) + #[mh(code = 0xb240, hasher = multihash_codetable::Blake2b512)] + Blake2b512, + /// BLAKE2s-128 (16-byte hash size) + #[mh(code = 0xb250, hasher = multihash_codetable::Blake2s128)] + Blake2s128, + /// BLAKE2s-256 (32-byte hash size) + #[mh(code = 0xb260, hasher = multihash_codetable::Blake2s256)] + Blake2s256, + /// BLAKE3-256 (32-byte hash size) + #[mh(code = 0x1e, hasher = multihash_codetable::Blake3_256)] + Blake3_256, + /// RIPEMD-160 (20-byte hash size) + #[mh(code = 0x1053, hasher = multihash_codetable::Ripemd160)] + Ripemd160, + /// RIPEMD-256 (32-byte hash size) + #[mh(code = 0x1054, hasher = multihash_codetable::Ripemd256)] + Ripemd256, + /// RIPEMD-320 (40-byte hash size) + #[mh(code = 0x1055, hasher = multihash_codetable::Ripemd320)] + Ripemd320, } /// Identity hasher with a maximum size.