Skip to content

Commit f3d38da

Browse files
elmatticarajasek
andauthored
Market actor unit tests (part 3) (#240)
Co-authored-by: Aayush Rajasekaran <[email protected]>
1 parent 593449d commit f3d38da

File tree

4 files changed

+37
-53
lines changed

4 files changed

+37
-53
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

actors/market/tests/market_actor_test.rs

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use fvm_shared::address::Address;
2727
use fvm_shared::bigint::bigint_ser::BigIntDe;
2828
use fvm_shared::bigint::BigInt;
2929
use fvm_shared::clock::{ChainEpoch, EPOCH_UNDEFINED};
30-
use fvm_shared::commcid::FIL_COMMITMENT_UNSEALED;
3130
use fvm_shared::crypto::signature::Signature;
3231
use fvm_shared::deal::DealID;
3332
use fvm_shared::econ::TokenAmount;
@@ -39,8 +38,6 @@ use fvm_shared::smooth::FilterEstimate;
3938
use fvm_shared::{HAMT_BIT_WIDTH, METHOD_CONSTRUCTOR, METHOD_SEND};
4039

4140
use cid::Cid;
42-
use multihash::derive::Multihash;
43-
use multihash::MultihashDigest;
4441
use num_traits::FromPrimitive;
4542

4643
const OWNER_ID: u64 = 101;
@@ -49,22 +46,6 @@ const WORKER_ID: u64 = 103;
4946
const CLIENT_ID: u64 = 104;
5047
const CONTROL_ID: u64 = 200;
5148

52-
// TODO: move this out in some utils? (MhCode and make_piece_cid come from miner/tests)
53-
// multihash library doesn't support poseidon hashing, so we fake it
54-
#[derive(Clone, Copy, Debug, Eq, Multihash, PartialEq)]
55-
#[mh(alloc_size = 64)]
56-
enum MhCode {
57-
#[mh(code = 0xb401, hasher = multihash::Sha2_256)]
58-
PoseidonFake,
59-
#[mh(code = 0x1012, hasher = multihash::Sha2_256)]
60-
Sha256TruncPaddedFake,
61-
}
62-
63-
fn make_piece_cid(input: &[u8]) -> Cid {
64-
let h = MhCode::Sha256TruncPaddedFake.digest(input);
65-
Cid::new_v1(FIL_COMMITMENT_UNSEALED, h)
66-
}
67-
6849
fn setup() -> MockRuntime {
6950
let mut actor_code_cids = HashMap::default();
7051
actor_code_cids.insert(Address::new_id(OWNER_ID), *ACCOUNT_ACTOR_CODE_ID);
@@ -546,14 +527,7 @@ fn simple_deal() {
546527
end_epoch,
547528
);
548529
rt.set_caller(*ACCOUNT_ACTOR_CODE_ID, worker_addr);
549-
publish_deals(
550-
&mut rt,
551-
provider_addr,
552-
owner_addr,
553-
worker_addr,
554-
control_addr,
555-
&[PublishDealReq { deal: deal1 }],
556-
);
530+
publish_deals(&mut rt, provider_addr, owner_addr, worker_addr, control_addr, &[deal1]);
557531

558532
// Publish from miner control address.
559533
let deal2 = generate_deal_and_add_funds(
@@ -566,14 +540,7 @@ fn simple_deal() {
566540
end_epoch + 1,
567541
);
568542
rt.set_caller(*ACCOUNT_ACTOR_CODE_ID, control_addr);
569-
publish_deals(
570-
&mut rt,
571-
provider_addr,
572-
owner_addr,
573-
worker_addr,
574-
control_addr,
575-
&[PublishDealReq { deal: deal2 }],
576-
);
543+
publish_deals(&mut rt, provider_addr, owner_addr, worker_addr, control_addr, &[deal2]);
577544
// TODO: actor.checkState(rt)
578545
}
579546

@@ -820,7 +787,7 @@ fn generate_and_publish_deal(
820787
let deal =
821788
generate_deal_and_add_funds(rt, client, provider, owner, worker, start_epoch, end_epoch);
822789
rt.set_caller(*ACCOUNT_ACTOR_CODE_ID, worker);
823-
let deal_ids = publish_deals(rt, provider, owner, worker, control, &[PublishDealReq { deal }]);
790+
let deal_ids = publish_deals(rt, provider, owner, worker, control, &[deal]);
824791
deal_ids[0]
825792
}
826793

@@ -862,7 +829,7 @@ fn generate_and_publish_deal_for_piece(
862829

863830
// publish
864831
rt.set_caller(*ACCOUNT_ACTOR_CODE_ID, worker);
865-
let deal_ids = publish_deals(rt, provider, owner, worker, control, &[PublishDealReq { deal }]);
832+
let deal_ids = publish_deals(rt, provider, owner, worker, control, &[deal]);
866833
deal_ids[0]
867834
}
868835

@@ -925,17 +892,13 @@ fn generate_deal_proposal(
925892
)
926893
}
927894

928-
struct PublishDealReq {
929-
deal: DealProposal,
930-
}
931-
932895
fn publish_deals(
933896
rt: &mut MockRuntime,
934897
provider: Address,
935898
owner: Address,
936899
worker: Address,
937900
control: Address,
938-
publish_deal_reqs: &[PublishDealReq],
901+
publish_deals: &[DealProposal],
939902
) -> Vec<DealID> {
940903
rt.expect_validate_caller_type((*CALLER_TYPES_SIGNABLE).clone());
941904

@@ -957,25 +920,25 @@ fn publish_deals(
957920

958921
let mut params: PublishStorageDealsParams = PublishStorageDealsParams { deals: vec![] };
959922

960-
for pdr in publish_deal_reqs {
923+
for deal in publish_deals {
961924
// create a client proposal with a valid signature
962-
let buf = RawBytes::serialize(pdr.deal.clone()).expect("failed to marshal deal proposal");
925+
let buf = RawBytes::serialize(deal.clone()).expect("failed to marshal deal proposal");
963926
let sig = Signature::new_bls("does not matter".as_bytes().to_vec());
964927
let client_proposal =
965-
ClientDealProposal { proposal: pdr.deal.clone(), client_signature: sig.clone() };
928+
ClientDealProposal { proposal: deal.clone(), client_signature: sig.clone() };
966929
params.deals.push(client_proposal);
967930

968931
// expect a call to verify the above signature
969932
rt.expect_verify_signature(ExpectedVerifySig {
970933
sig,
971-
signer: pdr.deal.client,
934+
signer: deal.client,
972935
plaintext: buf.to_vec(),
973936
result: Ok(()),
974937
});
975-
if pdr.deal.verified_deal {
938+
if deal.verified_deal {
976939
let param = RawBytes::serialize(UseBytesParams {
977-
address: pdr.deal.client,
978-
deal_size: BigInt::from(pdr.deal.piece_size.0),
940+
address: deal.client,
941+
deal_size: BigInt::from(deal.piece_size.0),
979942
})
980943
.unwrap();
981944

@@ -1000,11 +963,11 @@ fn publish_deals(
1000963
.unwrap();
1001964
rt.verify();
1002965

1003-
assert_eq!(ret.ids.len(), publish_deal_reqs.len());
966+
assert_eq!(ret.ids.len(), publish_deals.len());
1004967

1005968
// assert state after publishing the deals
1006969
for (i, deal_id) in ret.ids.iter().enumerate() {
1007-
let expected = &publish_deal_reqs[i].deal;
970+
let expected = &publish_deals[i];
1008971
let p = get_deal_proposal(rt, *deal_id);
1009972

1010973
assert_eq!(expected, &p);

actors/runtime/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ fvm_sdk = { version = "0.6.0", optional = true }
3131
blake2b_simd = "1.0"
3232
fvm_ipld_blockstore = { version = "0.1" }
3333
fvm_ipld_encoding = "0.1.0"
34+
multihash = { version = "0.16.1", default-features = false }
3435

3536
[dev-dependencies]
3637
derive_builder = "0.10.2"

actors/runtime/src/test_utils.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::cell::RefCell;
66
use std::collections::{BTreeMap, HashMap, VecDeque};
77

88
use anyhow::anyhow;
9-
use cid::multihash::{Code, Multihash};
9+
use cid::multihash::{Code, Multihash as OtherMultihash};
1010
use cid::Cid;
1111
use fvm_ipld_blockstore::MemoryBlockstore;
1212
use fvm_ipld_encoding::de::DeserializeOwned;
@@ -15,6 +15,7 @@ use fvm_shared::actor::builtin::Type;
1515
use fvm_shared::address::{Address, Protocol};
1616
use fvm_shared::clock::ChainEpoch;
1717

18+
use fvm_shared::commcid::FIL_COMMITMENT_UNSEALED;
1819
use fvm_shared::consensus::ConsensusFault;
1920
use fvm_shared::crypto::randomness::DomainSeparationTag;
2021
use fvm_shared::crypto::signature::Signature;
@@ -29,6 +30,9 @@ use fvm_shared::sector::{
2930
use fvm_shared::version::NetworkVersion;
3031
use fvm_shared::{ActorID, MethodNum};
3132

33+
use multihash::derive::Multihash;
34+
use multihash::MultihashDigest;
35+
3236
use crate::runtime::{ActorCode, MessageInfo, Policy, Runtime, RuntimePolicy, Syscalls};
3337
use crate::{actor_error, ActorError};
3438

@@ -67,7 +71,7 @@ const IPLD_RAW: u64 = 0x55;
6771

6872
/// Returns an identity CID for bz.
6973
pub fn make_builtin(bz: &[u8]) -> Cid {
70-
Cid::new_v1(IPLD_RAW, Multihash::wrap(0, bz).expect("name too long"))
74+
Cid::new_v1(IPLD_RAW, OtherMultihash::wrap(0, bz).expect("name too long"))
7175
}
7276

7377
pub struct MockRuntime {
@@ -1143,3 +1147,18 @@ pub fn blake2b_256(data: &[u8]) -> [u8; 32] {
11431147
.try_into()
11441148
.unwrap()
11451149
}
1150+
1151+
// multihash library doesn't support poseidon hashing, so we fake it
1152+
#[derive(Clone, Copy, Debug, Eq, Multihash, PartialEq)]
1153+
#[mh(alloc_size = 64)]
1154+
enum MhCode {
1155+
#[mh(code = 0xb401, hasher = multihash::Sha2_256)]
1156+
PoseidonFake,
1157+
#[mh(code = 0x1012, hasher = multihash::Sha2_256)]
1158+
Sha256TruncPaddedFake,
1159+
}
1160+
1161+
pub fn make_piece_cid(input: &[u8]) -> Cid {
1162+
let h = MhCode::Sha256TruncPaddedFake.digest(input);
1163+
Cid::new_v1(FIL_COMMITMENT_UNSEALED, h)
1164+
}

0 commit comments

Comments
 (0)