Skip to content

Commit

Permalink
batch_deal_activation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexytsu committed Jun 20, 2023
1 parent 65e185d commit 1541e83
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 12 deletions.
161 changes: 161 additions & 0 deletions actors/market/tests/batch_activate_deals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
mod harness;

use fil_actor_market::ext::account::AuthenticateMessageParams;
use fil_actor_market::{
Actor as MarketActor, BatchActivateDealsResult, DealMetaArray, MarketNotifyDealParams, Method,
SectorDeals, State, VerifyDealsForActivationParams, NO_ALLOCATION_ID,
};
use fil_actors_runtime::runtime::builtins::Type;
use fil_actors_runtime::test_utils::{
expect_abort, expect_abort_contains_message, make_piece_cid, ACCOUNT_ACTOR_CODE_ID,
MINER_ACTOR_CODE_ID,
};
use fil_actors_runtime::EPOCHS_IN_DAY;
use fvm_ipld_encoding::ipld_block::IpldBlock;
use fvm_ipld_encoding::CBOR;
use fvm_shared::address::Address;
use fvm_shared::bigint::BigInt;
use fvm_shared::clock::ChainEpoch;
use fvm_shared::econ::TokenAmount;
use fvm_shared::error::ExitCode;
use fvm_shared::piece::PieceInfo;
use fvm_shared::sector::RegisteredSealProof;
use harness::*;
use num_traits::Zero;

const START_EPOCH: ChainEpoch = 10;
const CURR_EPOCH: ChainEpoch = START_EPOCH;
const END_EPOCH: ChainEpoch = 200 * EPOCHS_IN_DAY;
const SECTOR_EXPIRY: ChainEpoch = END_EPOCH + 200;
const MINER_ADDRESSES: MinerAddresses = MinerAddresses {
owner: OWNER_ADDR,
worker: WORKER_ADDR,
provider: PROVIDER_ADDR,
control: vec![],
};

#[test]
fn activate_deals_across_multiple_sectors() {
let rt = setup();
let create_deal = |end_epoch, verified| {
create_deal(&rt, CLIENT_ADDR, &MINER_ADDRESSES, START_EPOCH, end_epoch, verified)
};
let verified_deal_1 = create_deal(END_EPOCH, true);
let unverified_deal_1 = create_deal(END_EPOCH, false);
let verified_deal_2 = create_deal(END_EPOCH + 1, true);
let unverified_deal_2 = create_deal(END_EPOCH + 2, false);

let deals =
[verified_deal_1.clone(), unverified_deal_1, verified_deal_2.clone(), unverified_deal_2];

let next_allocation_id = 1;
let datacap_required =
TokenAmount::from_whole(verified_deal_1.piece_size.0 + verified_deal_2.piece_size.0);
rt.set_caller(*ACCOUNT_ACTOR_CODE_ID, WORKER_ADDR);
let deal_ids =
publish_deals(&rt, &MINER_ADDRESSES, &deals, datacap_required, next_allocation_id);
assert_eq!(4, deal_ids.len());

let verified_deal_1_id = deal_ids[0];
let unverified_deal_1_id = deal_ids[1];
let verified_deal_2_id = deal_ids[2];
let unverified_deal_2_id = deal_ids[3];

// group into sectors
let sectors = [
(END_EPOCH, vec![verified_deal_1_id, unverified_deal_1_id]), // contains both verified and unverified deals
(END_EPOCH + 1, vec![verified_deal_2_id]), // contains verified deal only
(END_EPOCH + 2, vec![unverified_deal_2_id]), // contains unverified deal only
];

let res = batch_activate_deals(&rt, PROVIDER_ADDR, &sectors);

// three sectors activated successfully
assert!(res.activation_results.all_ok());
assert_eq!(vec![ExitCode::OK, ExitCode::OK, ExitCode::OK], res.activation_results.codes());

// all four deals were activated
let verified_deal_1 = get_deal_state(&rt, verified_deal_1_id);
let unverified_deal_1 = get_deal_state(&rt, unverified_deal_1_id);
let verified_deal_2 = get_deal_state(&rt, verified_deal_2_id);
let unverified_deal_2 = get_deal_state(&rt, unverified_deal_2_id);

// allocations were claimed successfully
assert_eq!(next_allocation_id, verified_deal_1.verified_claim);
assert_eq!(next_allocation_id + 1, verified_deal_2.verified_claim);
assert_eq!(NO_ALLOCATION_ID, unverified_deal_1.verified_claim);
assert_eq!(NO_ALLOCATION_ID, unverified_deal_2.verified_claim);

// all activated during same epoch
assert_eq!(0, verified_deal_1.sector_start_epoch);
assert_eq!(0, verified_deal_2.sector_start_epoch);
assert_eq!(0, unverified_deal_1.sector_start_epoch);
assert_eq!(0, unverified_deal_2.sector_start_epoch);

check_state(&rt);
}

#[test]
fn sectors_fail_and_succeed_independently_during_batch_activation() {
let rt = setup();
let deal_1 = create_deal(&rt, CLIENT_ADDR, &MINER_ADDRESSES, START_EPOCH, END_EPOCH, false);
let deal_2 = create_deal(&rt, CLIENT_ADDR, &MINER_ADDRESSES, START_EPOCH, END_EPOCH, true);
let deal_3 = create_deal(&rt, CLIENT_ADDR, &MINER_ADDRESSES, START_EPOCH, END_EPOCH + 1, false);
let deal_4 = create_deal(&rt, CLIENT_ADDR, &MINER_ADDRESSES, START_EPOCH, END_EPOCH + 2, false);

let deals = [deal_1, deal_2.clone(), deal_3, deal_4];

let next_allocation_id = 1;
rt.set_caller(*ACCOUNT_ACTOR_CODE_ID, WORKER_ADDR);
let deal_ids = publish_deals(
&rt,
&MINER_ADDRESSES,
&deals,
TokenAmount::from_whole(deal_2.piece_size.0),
next_allocation_id,
);
assert_eq!(4, deal_ids.len());

let id_1 = deal_ids[0];
let id_2 = deal_ids[1];
let id_3 = deal_ids[2];
let id_4 = deal_ids[3];

// activate the first deal so it will fail later
activate_deals(&rt, END_EPOCH, PROVIDER_ADDR, 0, &[id_1]);

let sector_type = RegisteredSealProof::StackedDRG8MiBV1;
// group into sectors
let sectors_deals = vec![
SectorDeals { deal_ids: vec![id_1, id_2], sector_type, sector_expiry: END_EPOCH }, // entire sector should fail
SectorDeals { deal_ids: vec![id_3], sector_type, sector_expiry: END_EPOCH + 1 }, // contains verified deal only
SectorDeals { deal_ids: vec![id_4], sector_type, sector_expiry: END_EPOCH + 2 }, // contains unverified deal only
];

let res = batch_activate_deals_raw(&rt, PROVIDER_ADDR, sectors_deals).unwrap();
let res: BatchActivateDealsResult =
res.unwrap().deserialize().expect("VerifyDealsForActivation failed!");

// first sector should have failed
assert_eq!(2, res.activation_results.success_count);
assert_eq!(
vec![ExitCode::USR_ILLEGAL_ARGUMENT, ExitCode::OK, ExitCode::OK],
res.activation_results.codes()
);

// two following sectors should have succeeded (and the deals within them are activated)
let deal_3 = get_deal_state(&rt, id_3);
let deal_4 = get_deal_state(&rt, id_4);

// all activated during same epoch
assert_eq!(0, deal_3.sector_start_epoch);
assert_eq!(0, deal_4.sector_start_epoch);

// no state for deal2 means deal2 activation has failed
let st: State = rt.get_state();
let states = DealMetaArray::load(&st.states, &rt.store).unwrap();
let s = states.get(id_2).unwrap();
assert!(s.is_none());

check_state(&rt);
}
23 changes: 15 additions & 8 deletions actors/market/tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,20 @@ pub fn withdraw_client_balance(
);
}

pub fn create_deal(
rt: &MockRuntime,
client_addr: Address,
miner_addrs: &MinerAddresses,
start_epoch: ChainEpoch,
end_epoch: ChainEpoch,
verified: bool,
) -> DealProposal {
let mut deal =
generate_deal_and_add_funds(rt, client_addr, miner_addrs, start_epoch, end_epoch);
deal.verified_deal = verified;
deal
}

/// Activate a single sector of deals
pub fn activate_deals(
rt: &MockRuntime,
Expand Down Expand Up @@ -353,14 +367,7 @@ pub fn batch_activate_deals(
ret.unwrap().deserialize().expect("VerifyDealsForActivation failed!");

// check all deals were activated correctly
if ret.activation_results.all_ok() {
for (epoch, deal_ids) in sectors {
for deal in deal_ids {
let s = get_deal_state(rt, *deal);
assert_eq!(epoch, &s.sector_start_epoch);
}
}
}
assert!(ret.activation_results.all_ok());

ret
}
Expand Down
5 changes: 1 addition & 4 deletions actors/market/tests/verify_deals_for_activation_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,7 @@ fn verify_deal_and_activate_to_get_deal_space_for_verified_deal_proposal() {
fn verification_and_weights_for_verified_and_unverified_deals() {
let rt = setup();
let create_deal = |end_epoch, verified| {
let mut deal =
generate_deal_and_add_funds(&rt, CLIENT_ADDR, &MINER_ADDRESSES, START_EPOCH, end_epoch);
deal.verified_deal = verified;
deal
create_deal(&rt, CLIENT_ADDR, &MINER_ADDRESSES, START_EPOCH, end_epoch, verified)
};

let verified_deal_1 = create_deal(END_EPOCH, true);
Expand Down

0 comments on commit 1541e83

Please sign in to comment.