From f3d38da03cc09aebba29ddb5081c05e91fbf341a Mon Sep 17 00:00:00 2001 From: Guillaume Potier Date: Fri, 22 Apr 2022 12:05:32 +0200 Subject: [PATCH] Market actor unit tests (part 3) (#240) Co-authored-by: Aayush Rajasekaran --- Cargo.lock | 1 + actors/market/tests/market_actor_test.rs | 65 +++++------------------- actors/runtime/Cargo.toml | 1 + actors/runtime/src/test_utils.rs | 23 ++++++++- 4 files changed, 37 insertions(+), 53 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 255be5cc0..0aadbf6e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -784,6 +784,7 @@ dependencies = [ "integer-encoding", "lazy_static", "log", + "multihash", "num-derive", "num-traits", "serde", diff --git a/actors/market/tests/market_actor_test.rs b/actors/market/tests/market_actor_test.rs index 3f17d55bb..26a905b0b 100644 --- a/actors/market/tests/market_actor_test.rs +++ b/actors/market/tests/market_actor_test.rs @@ -27,7 +27,6 @@ use fvm_shared::address::Address; use fvm_shared::bigint::bigint_ser::BigIntDe; use fvm_shared::bigint::BigInt; use fvm_shared::clock::{ChainEpoch, EPOCH_UNDEFINED}; -use fvm_shared::commcid::FIL_COMMITMENT_UNSEALED; use fvm_shared::crypto::signature::Signature; use fvm_shared::deal::DealID; use fvm_shared::econ::TokenAmount; @@ -39,8 +38,6 @@ use fvm_shared::smooth::FilterEstimate; use fvm_shared::{HAMT_BIT_WIDTH, METHOD_CONSTRUCTOR, METHOD_SEND}; use cid::Cid; -use multihash::derive::Multihash; -use multihash::MultihashDigest; use num_traits::FromPrimitive; const OWNER_ID: u64 = 101; @@ -49,22 +46,6 @@ const WORKER_ID: u64 = 103; const CLIENT_ID: u64 = 104; const CONTROL_ID: u64 = 200; -// TODO: move this out in some utils? (MhCode and make_piece_cid come from miner/tests) -// multihash library doesn't support poseidon hashing, so we fake it -#[derive(Clone, Copy, Debug, Eq, Multihash, PartialEq)] -#[mh(alloc_size = 64)] -enum MhCode { - #[mh(code = 0xb401, hasher = multihash::Sha2_256)] - PoseidonFake, - #[mh(code = 0x1012, hasher = multihash::Sha2_256)] - Sha256TruncPaddedFake, -} - -fn make_piece_cid(input: &[u8]) -> Cid { - let h = MhCode::Sha256TruncPaddedFake.digest(input); - Cid::new_v1(FIL_COMMITMENT_UNSEALED, h) -} - fn setup() -> MockRuntime { let mut actor_code_cids = HashMap::default(); actor_code_cids.insert(Address::new_id(OWNER_ID), *ACCOUNT_ACTOR_CODE_ID); @@ -546,14 +527,7 @@ fn simple_deal() { end_epoch, ); rt.set_caller(*ACCOUNT_ACTOR_CODE_ID, worker_addr); - publish_deals( - &mut rt, - provider_addr, - owner_addr, - worker_addr, - control_addr, - &[PublishDealReq { deal: deal1 }], - ); + publish_deals(&mut rt, provider_addr, owner_addr, worker_addr, control_addr, &[deal1]); // Publish from miner control address. let deal2 = generate_deal_and_add_funds( @@ -566,14 +540,7 @@ fn simple_deal() { end_epoch + 1, ); rt.set_caller(*ACCOUNT_ACTOR_CODE_ID, control_addr); - publish_deals( - &mut rt, - provider_addr, - owner_addr, - worker_addr, - control_addr, - &[PublishDealReq { deal: deal2 }], - ); + publish_deals(&mut rt, provider_addr, owner_addr, worker_addr, control_addr, &[deal2]); // TODO: actor.checkState(rt) } @@ -820,7 +787,7 @@ fn generate_and_publish_deal( let deal = generate_deal_and_add_funds(rt, client, provider, owner, worker, start_epoch, end_epoch); rt.set_caller(*ACCOUNT_ACTOR_CODE_ID, worker); - let deal_ids = publish_deals(rt, provider, owner, worker, control, &[PublishDealReq { deal }]); + let deal_ids = publish_deals(rt, provider, owner, worker, control, &[deal]); deal_ids[0] } @@ -862,7 +829,7 @@ fn generate_and_publish_deal_for_piece( // publish rt.set_caller(*ACCOUNT_ACTOR_CODE_ID, worker); - let deal_ids = publish_deals(rt, provider, owner, worker, control, &[PublishDealReq { deal }]); + let deal_ids = publish_deals(rt, provider, owner, worker, control, &[deal]); deal_ids[0] } @@ -925,17 +892,13 @@ fn generate_deal_proposal( ) } -struct PublishDealReq { - deal: DealProposal, -} - fn publish_deals( rt: &mut MockRuntime, provider: Address, owner: Address, worker: Address, control: Address, - publish_deal_reqs: &[PublishDealReq], + publish_deals: &[DealProposal], ) -> Vec { rt.expect_validate_caller_type((*CALLER_TYPES_SIGNABLE).clone()); @@ -957,25 +920,25 @@ fn publish_deals( let mut params: PublishStorageDealsParams = PublishStorageDealsParams { deals: vec![] }; - for pdr in publish_deal_reqs { + for deal in publish_deals { // create a client proposal with a valid signature - let buf = RawBytes::serialize(pdr.deal.clone()).expect("failed to marshal deal proposal"); + let buf = RawBytes::serialize(deal.clone()).expect("failed to marshal deal proposal"); let sig = Signature::new_bls("does not matter".as_bytes().to_vec()); let client_proposal = - ClientDealProposal { proposal: pdr.deal.clone(), client_signature: sig.clone() }; + ClientDealProposal { proposal: deal.clone(), client_signature: sig.clone() }; params.deals.push(client_proposal); // expect a call to verify the above signature rt.expect_verify_signature(ExpectedVerifySig { sig, - signer: pdr.deal.client, + signer: deal.client, plaintext: buf.to_vec(), result: Ok(()), }); - if pdr.deal.verified_deal { + if deal.verified_deal { let param = RawBytes::serialize(UseBytesParams { - address: pdr.deal.client, - deal_size: BigInt::from(pdr.deal.piece_size.0), + address: deal.client, + deal_size: BigInt::from(deal.piece_size.0), }) .unwrap(); @@ -1000,11 +963,11 @@ fn publish_deals( .unwrap(); rt.verify(); - assert_eq!(ret.ids.len(), publish_deal_reqs.len()); + assert_eq!(ret.ids.len(), publish_deals.len()); // assert state after publishing the deals for (i, deal_id) in ret.ids.iter().enumerate() { - let expected = &publish_deal_reqs[i].deal; + let expected = &publish_deals[i]; let p = get_deal_proposal(rt, *deal_id); assert_eq!(expected, &p); diff --git a/actors/runtime/Cargo.toml b/actors/runtime/Cargo.toml index 3c3c80fa5..fa5899cf3 100644 --- a/actors/runtime/Cargo.toml +++ b/actors/runtime/Cargo.toml @@ -31,6 +31,7 @@ fvm_sdk = { version = "0.6.0", optional = true } blake2b_simd = "1.0" fvm_ipld_blockstore = { version = "0.1" } fvm_ipld_encoding = "0.1.0" +multihash = { version = "0.16.1", default-features = false } [dev-dependencies] derive_builder = "0.10.2" diff --git a/actors/runtime/src/test_utils.rs b/actors/runtime/src/test_utils.rs index 820fb3546..fca2cc869 100644 --- a/actors/runtime/src/test_utils.rs +++ b/actors/runtime/src/test_utils.rs @@ -6,7 +6,7 @@ use std::cell::RefCell; use std::collections::{BTreeMap, HashMap, VecDeque}; use anyhow::anyhow; -use cid::multihash::{Code, Multihash}; +use cid::multihash::{Code, Multihash as OtherMultihash}; use cid::Cid; use fvm_ipld_blockstore::MemoryBlockstore; use fvm_ipld_encoding::de::DeserializeOwned; @@ -15,6 +15,7 @@ use fvm_shared::actor::builtin::Type; use fvm_shared::address::{Address, Protocol}; use fvm_shared::clock::ChainEpoch; +use fvm_shared::commcid::FIL_COMMITMENT_UNSEALED; use fvm_shared::consensus::ConsensusFault; use fvm_shared::crypto::randomness::DomainSeparationTag; use fvm_shared::crypto::signature::Signature; @@ -29,6 +30,9 @@ use fvm_shared::sector::{ use fvm_shared::version::NetworkVersion; use fvm_shared::{ActorID, MethodNum}; +use multihash::derive::Multihash; +use multihash::MultihashDigest; + use crate::runtime::{ActorCode, MessageInfo, Policy, Runtime, RuntimePolicy, Syscalls}; use crate::{actor_error, ActorError}; @@ -67,7 +71,7 @@ const IPLD_RAW: u64 = 0x55; /// Returns an identity CID for bz. pub fn make_builtin(bz: &[u8]) -> Cid { - Cid::new_v1(IPLD_RAW, Multihash::wrap(0, bz).expect("name too long")) + Cid::new_v1(IPLD_RAW, OtherMultihash::wrap(0, bz).expect("name too long")) } pub struct MockRuntime { @@ -1143,3 +1147,18 @@ pub fn blake2b_256(data: &[u8]) -> [u8; 32] { .try_into() .unwrap() } + +// multihash library doesn't support poseidon hashing, so we fake it +#[derive(Clone, Copy, Debug, Eq, Multihash, PartialEq)] +#[mh(alloc_size = 64)] +enum MhCode { + #[mh(code = 0xb401, hasher = multihash::Sha2_256)] + PoseidonFake, + #[mh(code = 0x1012, hasher = multihash::Sha2_256)] + Sha256TruncPaddedFake, +} + +pub fn make_piece_cid(input: &[u8]) -> Cid { + let h = MhCode::Sha256TruncPaddedFake.digest(input); + Cid::new_v1(FIL_COMMITMENT_UNSEALED, h) +}