From faaacda0cffce8d3c9895c866fc5018d05fff1cb Mon Sep 17 00:00:00 2001 From: connor mullett Date: Wed, 22 Jun 2022 16:42:05 -0400 Subject: [PATCH 1/2] remove commcid in favor of the fvm commcid --- Cargo.lock | 14 +--- Cargo.toml | 2 - blockchain/chain_sync/Cargo.toml | 1 - ipld/blockstore/Cargo.toml | 3 - ipld/blockstore/src/buffered.rs | 2 +- types/Cargo.toml | 3 +- types/src/verifier/mod.rs | 2 +- utils/commcid/Cargo.toml | 15 ---- utils/commcid/src/lib.rs | 113 -------------------------- utils/commcid/tests/commcid_tests.rs | 117 --------------------------- vm/runtime/Cargo.toml | 2 +- vm/runtime/src/lib.rs | 2 +- 12 files changed, 6 insertions(+), 270 deletions(-) delete mode 100644 utils/commcid/Cargo.toml delete mode 100644 utils/commcid/src/lib.rs delete mode 100644 utils/commcid/tests/commcid_tests.rs diff --git a/Cargo.lock b/Cargo.lock index 09e1d4e45a3c..5cf99616498d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1162,7 +1162,6 @@ dependencies = [ "base64 0.13.0", "beacon", "chain", - "commcid", "fil_clock", "fil_types", "forest_address", @@ -1350,15 +1349,6 @@ dependencies = [ "winapi 0.3.9", ] -[[package]] -name = "commcid" -version = "0.1.1" -dependencies = [ - "forest_cid", - "multihash 0.16.2", - "rand 0.8.5", -] - [[package]] name = "concurrent-queue" version = "1.2.2" @@ -2922,7 +2912,6 @@ version = "0.2.2" dependencies = [ "async-std", "base64 0.13.0", - "commcid", "fil_clock", "filecoin-proofs-api", "forest_address", @@ -3410,7 +3399,6 @@ name = "forest_runtime" version = "0.2.1" dependencies = [ "base64 0.13.0", - "commcid", "fil_clock", "fil_types", "filecoin-proofs-api", @@ -3420,6 +3408,7 @@ dependencies = [ "forest_db", "forest_encoding", "forest_vm", + "fvm_shared 0.7.1", "interpreter", "ipld_blockstore", "log", @@ -4428,7 +4417,6 @@ version = "0.1.1" dependencies = [ "anyhow", "byteorder 1.4.3", - "commcid", "dashmap", "forest_cid", "forest_db", diff --git a/Cargo.toml b/Cargo.toml index 0817459d159f..1caf2c4bafca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,6 @@ members = [ "utils/auth", "utils/bigint", "utils/bitfield", - "utils/commcid", "utils/genesis", "utils/hash_utils", "utils/json_utils", @@ -61,7 +60,6 @@ forest_ipld = { path = "./ipld" } forest_encoding = { path = "./encoding" } forest_hash_utils = { path = "./utils/hash_utils" } forest_json_utils = { path = "./utils/json_utils" } -commcid = { path = "./utils/commcid" } forest_vm = { path = "./vm" } forest_runtime = { path = "./vm/runtime" } forest_message = { path = "./vm/message" } diff --git a/blockchain/chain_sync/Cargo.toml b/blockchain/chain_sync/Cargo.toml index 862f1657b9cc..7723bd48ca89 100644 --- a/blockchain/chain_sync/Cargo.toml +++ b/blockchain/chain_sync/Cargo.toml @@ -39,7 +39,6 @@ lru = "0.7.2" thiserror = "1.0" num-traits = "0.2" fil_types = "0.2" -commcid = { path = "../../utils/commcid" } clock = { package = "fil_clock", path = "../../node/clock" } serde = { version = "1.0", features = ["derive", "rc"] } tokio = { version = "1.0", features = ["sync"] } diff --git a/ipld/blockstore/Cargo.toml b/ipld/blockstore/Cargo.toml index 4890fff44210..dd2b6690e061 100644 --- a/ipld/blockstore/Cargo.toml +++ b/ipld/blockstore/Cargo.toml @@ -18,9 +18,6 @@ forest_ipld = { optional = true, version = "0.1" } byteorder = "1.3" dashmap = "5.0" -[dev-dependencies] -commcid = { path = "../../utils/commcid" } - [features] rocksdb = ["db/rocksdb"] buffered = ["forest_ipld"] diff --git a/ipld/blockstore/src/buffered.rs b/ipld/blockstore/src/buffered.rs index 561d2f4ccdbd..7df14dbde7d5 100644 --- a/ipld/blockstore/src/buffered.rs +++ b/ipld/blockstore/src/buffered.rs @@ -262,8 +262,8 @@ where mod tests { use super::*; use cid::{multihash::MultihashDigest, Code, RAW}; - use commcid::commitment_to_cid; use forest_ipld::{ipld, Ipld}; + use fvm_shared::commcid::commitment_to_cid; #[test] fn basic_buffered_store() { diff --git a/types/Cargo.toml b/types/Cargo.toml index c1eecf536dd6..caefa9983213 100644 --- a/types/Cargo.toml +++ b/types/Cargo.toml @@ -16,7 +16,6 @@ address = { package = "forest_address", features = ["json"], version time = { version = "0.3", features = ["serde", "serde-well-known"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -commcid = { path = "../utils/commcid", version = "0.1", optional = true } filecoin-proofs-api = { version = "11.0", default_features = false, optional = true } vm = { package = "forest_vm", version = "0.3" } cid = { package = "forest_cid", features = ["cbor"], version = "0.3" } @@ -34,4 +33,4 @@ rayon = "1.5" [features] json = ["base64", "forest_json_utils", "num-bigint/json"] -proofs = ["filecoin-proofs-api", "commcid"] +proofs = ["filecoin-proofs-api"] diff --git a/types/src/verifier/mod.rs b/types/src/verifier/mod.rs index 87a112f85a52..cb3f499c22f2 100644 --- a/types/src/verifier/mod.rs +++ b/types/src/verifier/mod.rs @@ -10,13 +10,13 @@ use crate::{ SectorInfo, }; use address::Address; -use commcid::{cid_to_data_commitment_v1, cid_to_replica_commitment_v1}; use encoding::bytes_32; use filecoin_proofs_api::{self as proofs, ProverId, SectorId}; use filecoin_proofs_api::{ post, seal::verify_aggregate_seal_commit_proofs, seal::verify_seal as proofs_verify_seal, PublicReplicaInfo, }; +use fvm_shared::commcid::{cid_to_data_commitment_v1, cid_to_replica_commitment_v1}; use proofs::seal; use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use std::collections::BTreeMap; diff --git a/utils/commcid/Cargo.toml b/utils/commcid/Cargo.toml deleted file mode 100644 index 0390fd83ff19..000000000000 --- a/utils/commcid/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -name = "commcid" -description = "Filecoin commitment to Cid conversion utilities" -version = "0.1.1" -license = "MIT OR Apache-2.0" -authors = ["ChainSafe Systems "] -edition = "2021" -repository = "https://github.com/ChainSafe/forest" - -[dependencies] -cid = { package = "forest_cid", version = "0.3" } -multihash = "0.16" - -[dev-dependencies] -rand = "0.8" diff --git a/utils/commcid/src/lib.rs b/utils/commcid/src/lib.rs deleted file mode 100644 index 8aeb8b0bdc9a..000000000000 --- a/utils/commcid/src/lib.rs +++ /dev/null @@ -1,113 +0,0 @@ -// Copyright 2019-2022 ChainSafe Systems -// SPDX-License-Identifier: Apache-2.0, MIT - -use cid::{ - multihash::Multihash, Cid, FIL_COMMITMENT_SEALED, FIL_COMMITMENT_UNSEALED, - POSEIDON_BLS12_381_A1_FC1, SHA2_256_TRUNC254_PADDED, -}; - -pub type Commitment = [u8; 32]; - -/// CommitmentToCID converts a raw commitment hash to a CID -/// by adding: -/// - the given filecoin codec type -/// - the given filecoin e -pub fn commitment_to_cid(mc: u64, mh: u64, commitment: &Commitment) -> Result { - validate_filecoin_cid_segments(mc, mh, commitment)?; - - let mh = Multihash::wrap(mh, commitment).map_err(|_| "failed to wrap commitment cid")?; - - Ok(Cid::new_v1(mc, mh)) -} - -/// CIDToCommitment extracts the raw commitment bytes, the FilMultiCodec and -/// FilMultiHash from a CID, after validating that the codec and hash type are -/// consistent -pub fn cid_to_commitment(c: &Cid) -> Result<(u64, u64, Commitment), &'static str> { - validate_filecoin_cid_segments(c.codec(), c.hash().code(), c.hash().digest())?; - - let mut comm = Commitment::default(); - comm.copy_from_slice(c.hash().digest()); - - Ok((c.codec(), c.hash().code(), comm)) -} - -/// DataCommitmentV1ToCID converts a raw data commitment to a CID -/// by adding: -/// - codec: cid.FilCommitmentUnsealed -/// - hash type: multihash.Sha2256Truncated256Padded -pub fn data_commitment_v1_to_cid(comm_d: &Commitment) -> Result { - commitment_to_cid(FIL_COMMITMENT_UNSEALED, SHA2_256_TRUNC254_PADDED, comm_d) -} - -/// cid_to_data_commitment_v1 extracts the raw data commitment from a CID -/// assuming that it has the correct hashing function and -/// serialization types -pub fn cid_to_data_commitment_v1(c: &Cid) -> Result { - let (codec, _, comm_d) = cid_to_commitment(c)?; - - if codec != FIL_COMMITMENT_UNSEALED { - return Err("data commitment codec must be Unsealed"); - } - - Ok(comm_d) -} - -/// ReplicaCommitmentV1ToCID converts a raw data commitment to a CID -/// by adding: -/// - codec: cid.FilCommitmentSealed -/// - hash type: multihash.PoseidonBls12381A1Fc1 -pub fn replica_commitment_v1_to_cid(comm_r: &Commitment) -> Result { - commitment_to_cid(FIL_COMMITMENT_SEALED, POSEIDON_BLS12_381_A1_FC1, comm_r) -} - -/// cid_to_replica_commitment_v1 extracts the raw replica commitment from a CID -/// assuming that it has the correct hashing function and -/// serialization types -pub fn cid_to_replica_commitment_v1(c: &Cid) -> Result { - let (codec, _, comm_r) = cid_to_commitment(c)?; - - if codec != FIL_COMMITMENT_SEALED { - return Err("data commitment codec must be Sealed"); - } - - Ok(comm_r) -} - -/// ValidateFilecoinCidSegments returns an error if the provided CID parts -/// conflict with each other. -fn validate_filecoin_cid_segments(mc: u64, mh: u64, comm_x: &[u8]) -> Result<(), &'static str> { - match mc { - FIL_COMMITMENT_UNSEALED => { - if mh != SHA2_256_TRUNC254_PADDED { - return Err("Incorrect hash function for unsealed commitment"); - } - } - FIL_COMMITMENT_SEALED => { - if mh != POSEIDON_BLS12_381_A1_FC1 { - return Err("Incorrect hash function for sealed commitment"); - } - } - _ => return Err("Invalid Codec, expected sealed or unsealed commitment codec"), - } - - if comm_x.len() != 32 { - Err("commitments must be 32 bytes long") - } else { - Ok(()) - } -} - -/// piece_commitment_v1_to_cid converts a comm_p to a CID -/// -- it is just a helper function that is equivalent to -/// data_commitment_v1_to_cid. -pub fn piece_commitment_v1_to_cid(comm_p: &Commitment) -> Result { - data_commitment_v1_to_cid(comm_p) -} - -/// cid_to_piece_commitment_v1 converts a CID to a comm_p -/// -- it is just a helper function that is equivalent to -/// cid_to_data_commitment_v1. -pub fn cid_to_piece_commitment_v1(c: &Cid) -> Result { - cid_to_data_commitment_v1(c) -} diff --git a/utils/commcid/tests/commcid_tests.rs b/utils/commcid/tests/commcid_tests.rs deleted file mode 100644 index 31316146630a..000000000000 --- a/utils/commcid/tests/commcid_tests.rs +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright 2019-2022 ChainSafe Systems -// SPDX-License-Identifier: Apache-2.0, MIT - -use cid::{multihash::MultihashDigest, Cid, Code}; -use commcid::*; -use rand::thread_rng; -use rand::Rng; - -fn rand_comm() -> Commitment { - let mut rng = thread_rng(); - - let mut comm = Commitment::default(); - for b in comm.iter_mut() { - *b = rng.gen(); - } - comm -} - -#[test] -fn comm_d_to_cid() { - let comm = rand_comm(); - - let cid = data_commitment_v1_to_cid(&comm).unwrap(); - - assert_eq!(cid.codec(), cid::FIL_COMMITMENT_UNSEALED); - assert_eq!(cid.hash().code(), cid::SHA2_256_TRUNC254_PADDED); - assert_eq!(cid.hash().digest(), comm); -} - -#[test] -fn cid_to_comm_d() { - let comm = rand_comm(); - - // Correct hash format - let mh = cid::multihash::Multihash::wrap(cid::SHA2_256_TRUNC254_PADDED, &comm).unwrap(); - let c = Cid::new_v1(cid::FIL_COMMITMENT_UNSEALED, mh); - let decoded = cid_to_data_commitment_v1(&c).unwrap(); - assert_eq!(decoded, comm); - - // Should fail with incorrect codec - let c = Cid::new_v1(cid::FIL_COMMITMENT_SEALED, mh); - assert!(cid_to_data_commitment_v1(&c).is_err()); - - // Incorrect hash format - let mh = Code::Blake2b256.digest(&comm); - let c = Cid::new_v1(cid::FIL_COMMITMENT_UNSEALED, mh); - assert!(cid_to_data_commitment_v1(&c).is_err()); -} - -#[test] -fn comm_r_to_cid() { - let comm = rand_comm(); - - let cid = replica_commitment_v1_to_cid(&comm).unwrap(); - - assert_eq!(cid.codec(), cid::FIL_COMMITMENT_SEALED); - assert_eq!(cid.hash().code(), cid::POSEIDON_BLS12_381_A1_FC1); - assert_eq!(cid.hash().digest(), comm); -} - -#[test] -fn cid_to_comm_r() { - let comm = rand_comm(); - - // Correct hash format - let mh = cid::multihash::Multihash::wrap(cid::POSEIDON_BLS12_381_A1_FC1, &comm).unwrap(); - let c = Cid::new_v1(cid::FIL_COMMITMENT_SEALED, mh); - let decoded = cid_to_replica_commitment_v1(&c).unwrap(); - assert_eq!(decoded, comm); - - // Should fail with incorrect codec - let c = Cid::new_v1(cid::FIL_COMMITMENT_UNSEALED, mh); - assert!(cid_to_replica_commitment_v1(&c).is_err()); - - // Incorrect hash format - let mh = Code::Blake2b256.digest(&comm); - let c = Cid::new_v1(cid::FIL_COMMITMENT_SEALED, mh); - assert!(cid_to_replica_commitment_v1(&c).is_err()); -} - -#[test] -fn symmetric_conversion() { - let comm = rand_comm(); - - // data - let cid = data_commitment_v1_to_cid(&comm).unwrap(); - assert_eq!( - cid_to_commitment(&cid).unwrap(), - ( - cid::FIL_COMMITMENT_UNSEALED, - cid::SHA2_256_TRUNC254_PADDED, - comm - ) - ); - - // replica - let cid = replica_commitment_v1_to_cid(&comm).unwrap(); - assert_eq!( - cid_to_commitment(&cid).unwrap(), - ( - cid::FIL_COMMITMENT_SEALED, - cid::POSEIDON_BLS12_381_A1_FC1, - comm - ) - ); - - // piece - let cid = piece_commitment_v1_to_cid(&comm).unwrap(); - assert_eq!( - cid_to_commitment(&cid).unwrap(), - ( - cid::FIL_COMMITMENT_UNSEALED, - cid::SHA2_256_TRUNC254_PADDED, - comm - ) - ); -} diff --git a/vm/runtime/Cargo.toml b/vm/runtime/Cargo.toml index 7c8137c90688..ca7e617a6ebb 100644 --- a/vm/runtime/Cargo.toml +++ b/vm/runtime/Cargo.toml @@ -15,11 +15,11 @@ cid = { package = "forest_cid", version = "0.3" } ipld_blockstore = "0.1" clock = { package = "fil_clock", path = "../../node/clock", version = "0.1" } forest_encoding = "0.2" -commcid = { path = "../../utils/commcid", version = "0.1" } filecoin-proofs-api = { version = "11.0", default_features = false } base64 = "0.13" fil_types = { features = ["proofs"], version = "0.2" } log = "0.4" +fvm_shared = { version = "0.7.1", default-features = false } [dev-dependencies] interpreter = { path = "../interpreter/" } diff --git a/vm/runtime/src/lib.rs b/vm/runtime/src/lib.rs index 11b8f3588f0c..b165b3a93739 100644 --- a/vm/runtime/src/lib.rs +++ b/vm/runtime/src/lib.rs @@ -8,7 +8,6 @@ pub use self::actor_code::*; use address::Address; use cid::Cid; use clock::ChainEpoch; -use commcid::data_commitment_v1_to_cid; use crypto::{DomainSeparationTag, Signature}; use fil_types::{ zero_piece_commitment, NetworkVersion, PaddedPieceSize, PieceInfo, Randomness, @@ -17,6 +16,7 @@ use fil_types::{ use filecoin_proofs_api::seal::compute_comm_d; use filecoin_proofs_api::{self as proofs}; use forest_encoding::{blake2b_256, de, Cbor}; +use fvm_shared::commcid::data_commitment_v1_to_cid; use ipld_blockstore::BlockStore; use std::collections::HashMap; use std::convert::{TryFrom, TryInto}; From 0bc03df5cc1d8f59bc883f391401bedfe6d0e0e5 Mon Sep 17 00:00:00 2001 From: connor mullett Date: Thu, 23 Jun 2022 11:34:22 -0400 Subject: [PATCH 2/2] remove commcid in makefile --- Makefile | 1 - 1 file changed, 1 deletion(-) diff --git a/Makefile b/Makefile index 5e54f0844622..26176a39f361 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,6 @@ clean: @cargo clean -p ipld_amt @cargo clean -p forest_bigint @cargo clean -p forest_bitfield - @cargo clean -p commcid @cargo clean -p fil_types @cargo clean -p ipld_blockstore @cargo clean -p rpc