Skip to content

Commit

Permalink
Market actor unit tests (part 3) (#240)
Browse files Browse the repository at this point in the history
Co-authored-by: Aayush Rajasekaran <arajasek94@gmail.com>
  • Loading branch information
elmattic and arajasek committed Apr 22, 2022
1 parent 593449d commit f3d38da
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 53 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 14 additions & 51 deletions actors/market/tests/market_actor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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(
Expand All @@ -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)
}

Expand Down Expand Up @@ -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]
}

Expand Down Expand Up @@ -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]
}

Expand Down Expand Up @@ -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<DealID> {
rt.expect_validate_caller_type((*CALLER_TYPES_SIGNABLE).clone());

Expand All @@ -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();

Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions actors/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 21 additions & 2 deletions actors/runtime/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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};

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

0 comments on commit f3d38da

Please sign in to comment.