Skip to content

Commit

Permalink
Market actor unit tests (part 11) (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemmih authored Apr 22, 2022
1 parent ec878ed commit 7ac7cea
Showing 1 changed file with 198 additions and 3 deletions.
201 changes: 198 additions & 3 deletions actors/market/tests/market_actor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::collections::HashMap;
use fil_actor_market::balance_table::{BalanceTable, BALANCE_TABLE_BITWIDTH};
use fil_actor_market::{
ext, ActivateDealsParams, Actor as MarketActor, ClientDealProposal, DealArray, DealMetaArray,
DealProposal, DealState, Label, Method, PublishStorageDealsParams, PublishStorageDealsReturn,
State, WithdrawBalanceParams, WithdrawBalanceReturn, PROPOSALS_AMT_BITWIDTH,
STATES_AMT_BITWIDTH,
DealProposal, DealState, Label, Method, OnMinerSectorsTerminateParams,
PublishStorageDealsParams, PublishStorageDealsReturn, State, WithdrawBalanceParams,
WithdrawBalanceReturn, PROPOSALS_AMT_BITWIDTH, STATES_AMT_BITWIDTH,
};
use fil_actor_power::{CurrentTotalPowerReturn, Method as PowerMethod};
use fil_actor_reward::Method as RewardMethod;
Expand Down Expand Up @@ -591,6 +591,171 @@ fn publish_a_deal_after_activating_a_previous_deal_which_has_a_start_epoch_far_i
// TODO: actor.checkState(rt)
}

// Converted from https://github.com/filecoin-project/specs-actors/blob/d56b240af24517443ce1f8abfbdab7cb22d331f1/actors/builtin/market/market_test.go#L1274
#[test]
fn terminate_multiple_deals_from_multiple_providers() {
use std::convert::TryInto;
let start_epoch = 10;
let end_epoch = start_epoch + 200 * EPOCHS_IN_DAY;
let sector_expiry = end_epoch + 100;
let current_epoch = 5;
let owner_addr = Address::new_id(OWNER_ID);
let provider_addr = Address::new_id(PROVIDER_ID);
let worker_addr = Address::new_id(WORKER_ID);
let client_addr = Address::new_id(CLIENT_ID);
let control_addr = Address::new_id(CONTROL_ID);

let provider2 = Address::new_id(501);

let mut rt = setup();
rt.set_epoch(current_epoch);

let [deal1, deal2, deal3]: [DealID; 3] = (end_epoch..end_epoch + 3)
.map(|epoch| {
generate_and_publish_deal(
&mut rt,
client_addr,
provider_addr,
owner_addr,
worker_addr,
control_addr,
start_epoch,
epoch,
)
})
.collect::<Vec<DealID>>()
.try_into()
.unwrap();
activate_deals(&mut rt, sector_expiry, provider_addr, current_epoch, &[deal1, deal2, deal3]);

let deal4 = generate_and_publish_deal(
&mut rt,
client_addr,
provider2,
owner_addr,
worker_addr,
control_addr,
start_epoch,
end_epoch,
);
let deal5 = generate_and_publish_deal(
&mut rt,
client_addr,
provider2,
owner_addr,
worker_addr,
control_addr,
start_epoch,
end_epoch + 1,
);
activate_deals(&mut rt, sector_expiry, provider2, current_epoch, &[deal4, deal5]);

terminate_deals(&mut rt, provider_addr, &[deal1]);
assert_deals_terminated(&mut rt, current_epoch, &[deal1]);
assert_deals_not_terminated(&mut rt, &[deal2, deal3, deal4, deal5]);

terminate_deals(&mut rt, provider2, &[deal5]);
assert_deals_terminated(&mut rt, current_epoch, &[deal5]);
assert_deals_not_terminated(&mut rt, &[deal2, deal3, deal4]);

terminate_deals(&mut rt, provider_addr, &[deal2, deal3]);
assert_deals_terminated(&mut rt, current_epoch, &[deal2, deal3]);
assert_deals_not_terminated(&mut rt, &[deal4]);

terminate_deals(&mut rt, provider2, &[deal4]);
assert_deals_terminated(&mut rt, current_epoch, &[deal4]);
}

// Converted from: https://github.com/filecoin-project/specs-actors/blob/d56b240af24517443ce1f8abfbdab7cb22d331f1/actors/builtin/market/market_test.go#L1312
#[test]
fn ignore_deal_proposal_that_does_not_exist() {
let start_epoch = 10;
let end_epoch = start_epoch + 200 * EPOCHS_IN_DAY;
let sector_expiry = end_epoch + 100;
let current_epoch = 5;
let owner_addr = Address::new_id(OWNER_ID);
let provider_addr = Address::new_id(PROVIDER_ID);
let worker_addr = Address::new_id(WORKER_ID);
let client_addr = Address::new_id(CLIENT_ID);
let control_addr = Address::new_id(CONTROL_ID);

let mut rt = setup();
rt.set_epoch(current_epoch);

let deal1 = generate_and_publish_deal(
&mut rt,
client_addr,
provider_addr,
owner_addr,
worker_addr,
control_addr,
start_epoch,
end_epoch,
);
activate_deals(&mut rt, sector_expiry, provider_addr, current_epoch, &[deal1]);

terminate_deals(&mut rt, provider_addr, &[deal1, 42]);

let s = get_deal_state(&mut rt, deal1);
assert_eq!(s.slash_epoch, current_epoch);
}

// Converted from: https://github.com/filecoin-project/specs-actors/blob/d56b240af24517443ce1f8abfbdab7cb22d331f1/actors/builtin/market/market_test.go#L1326
#[test]
fn terminate_valid_deals_along_with_just_expired_deal() {
let start_epoch = 10;
let end_epoch = start_epoch + 200 * EPOCHS_IN_DAY;
let sector_expiry = end_epoch + 100;
let current_epoch = 5;
let owner_addr = Address::new_id(OWNER_ID);
let provider_addr = Address::new_id(PROVIDER_ID);
let worker_addr = Address::new_id(WORKER_ID);
let client_addr = Address::new_id(CLIENT_ID);
let control_addr = Address::new_id(CONTROL_ID);

let mut rt = setup();
rt.set_epoch(current_epoch);

let deal1 = generate_and_publish_deal(
&mut rt,
client_addr,
provider_addr,
owner_addr,
worker_addr,
control_addr,
start_epoch,
end_epoch,
);
let deal2 = generate_and_publish_deal(
&mut rt,
client_addr,
provider_addr,
owner_addr,
worker_addr,
control_addr,
start_epoch,
end_epoch + 1,
);
let deal3 = generate_and_publish_deal(
&mut rt,
client_addr,
provider_addr,
owner_addr,
worker_addr,
control_addr,
start_epoch,
end_epoch - 1,
);
activate_deals(&mut rt, sector_expiry, provider_addr, current_epoch, &[deal1, deal2, deal3]);

let new_epoch = end_epoch - 1;
rt.set_epoch(new_epoch);

terminate_deals(&mut rt, provider_addr, &[deal1, deal2, deal3]);
assert_deals_terminated(&mut rt, new_epoch, &[deal1, deal2]);
assert_deals_not_terminated(&mut rt, &[deal3]);
}

#[test]
fn publish_a_deal_with_enough_collateral_when_circulating_supply_is_superior_to_zero() {
let policy = Policy::default();
Expand Down Expand Up @@ -1250,6 +1415,22 @@ fn generate_deal_proposal(
)
}

fn terminate_deals(rt: &mut MockRuntime, miner_addr: Address, deal_ids: &[DealID]) {
rt.set_caller(*MINER_ACTOR_CODE_ID, miner_addr);
rt.expect_validate_caller_type(vec![*MINER_ACTOR_CODE_ID]);

let params = OnMinerSectorsTerminateParams { epoch: rt.epoch, deal_ids: deal_ids.to_vec() };

let ret = rt
.call::<MarketActor>(
Method::OnMinerSectorsTerminate as u64,
&RawBytes::serialize(params).unwrap(),
)
.unwrap();
assert_eq!(ret, RawBytes::default());
rt.verify();
}

fn publish_deals(
rt: &mut MockRuntime,
provider: Address,
Expand Down Expand Up @@ -1394,3 +1575,17 @@ where
.unwrap();
assert_eq!(n, count, "unexpected deal count at epoch {}", epoch);
}

fn assert_deals_terminated(rt: &mut MockRuntime, epoch: ChainEpoch, deal_ids: &[DealID]) {
for &deal_id in deal_ids {
let s = get_deal_state(rt, deal_id);
assert_eq!(s.slash_epoch, epoch);
}
}

fn assert_deals_not_terminated(rt: &mut MockRuntime, deal_ids: &[DealID]) {
for &deal_id in deal_ids {
let s = get_deal_state(rt, deal_id);
assert_eq!(s.slash_epoch, EPOCH_UNDEFINED);
}
}

0 comments on commit 7ac7cea

Please sign in to comment.