Skip to content

Commit

Permalink
emit event during settling payments
Browse files Browse the repository at this point in the history
  • Loading branch information
aarshkshah1992 committed Jan 31, 2024
1 parent a35b1cc commit f0a0631
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 27 deletions.
29 changes: 28 additions & 1 deletion actors/market/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,15 @@ impl Actor {
.entry(state.sector_number)
.or_default()
.push(deal_id);

if !completed {
emit::deal_terminated(
rt,
deal_id,
deal_proposal.client.id().unwrap(),
deal_proposal.provider.id().unwrap(),
)?;
}
} else {
if !slash_amount.is_zero() {
return Err(actor_error!(
Expand Down Expand Up @@ -1273,7 +1282,7 @@ impl Actor {
));
}

let (_, payment_amount, _, remove_deal) = match st.process_deal_update(
let (_, payment_amount, completed, remove_deal) = match st.process_deal_update(
rt.store(),
&deal_state,
&deal_proposal,
Expand All @@ -1295,6 +1304,15 @@ impl Actor {
.entry(deal_state.sector_number)
.or_default()
.push(deal_id);

if !completed {
emit::deal_terminated(
rt,
deal_id,
deal_proposal.client.id().unwrap(),
deal_proposal.provider.id().unwrap(),
)?;
}
} else {
deal_state.last_updated_epoch = curr_epoch;
new_deal_states.push((deal_id, deal_state));
Expand All @@ -1305,6 +1323,15 @@ impl Actor {
payment: payment_amount,
});
batch_gen.add_success();

if completed {
emit::deal_completed(
rt,
deal_id,
deal_proposal.client.id().unwrap(),
deal_proposal.provider.id().unwrap(),
)?;
}
}

st.put_deal_states(rt.store(), &new_deal_states)?;
Expand Down
4 changes: 2 additions & 2 deletions actors/market/tests/deal_termination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ fn settle_payments_then_terminate_deal_in_the_same_epoch() {
// settle payments then terminate
rt.set_epoch(termination_epoch);
let expected_payment = deal_duration * &proposal.storage_price_per_epoch;
let ret = settle_deal_payments(&rt, PROVIDER_ADDR, &[deal_id]);
let ret = settle_deal_payments(&rt, PROVIDER_ADDR, &[deal_id], &[], &[]);
assert_eq!(
ret.settlements.get(0).unwrap(),
&DealSettlementSummary { completed: false, payment: expected_payment.clone() }
Expand Down Expand Up @@ -243,7 +243,7 @@ fn terminate_a_deal_then_settle_it_in_the_same_epoch() {
&[sector_number],
&[deal_id],
);
let ret = settle_deal_payments(&rt, PROVIDER_ADDR, &[deal_id]);
let ret = settle_deal_payments(&rt, PROVIDER_ADDR, &[deal_id], &[], &[]);
assert_eq!(ret.results.codes(), vec![EX_DEAL_EXPIRED]);
assert_deal_deleted(&rt, deal_id, &proposal, sector_number);

Expand Down
38 changes: 36 additions & 2 deletions actors/market/tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,8 @@ pub fn settle_deal_payments(
rt: &MockRuntime,
caller: Address,
deal_ids: &[DealID],
completed_deals: &[DealID],
terminated_deals: &[DealID],
) -> SettleDealPaymentsReturn {
let mut deal_id_bitfield = BitField::new();
for deal_id in deal_ids {
Expand All @@ -956,6 +958,29 @@ pub fn settle_deal_payments(

rt.set_caller(*ACCOUNT_ACTOR_CODE_ID, caller);
rt.expect_validate_caller_any();

for deal_id in completed_deals {
let deal = get_deal_proposal(rt, *deal_id);
expect_emitted(
rt,
"deal-completed",
*deal_id,
deal.client.id().unwrap(),
deal.provider.id().unwrap(),
);
}

for deal_id in terminated_deals {
let deal = get_deal_proposal(rt, *deal_id);
expect_emitted(
rt,
"deal-terminated",
*deal_id,
deal.client.id().unwrap(),
deal.provider.id().unwrap(),
);
}

let res =
rt.call::<MarketActor>(Method::SettleDealPaymentsExported as u64, params).unwrap().unwrap();
let res: SettleDealPaymentsReturn = res.deserialize().unwrap();
Expand All @@ -978,7 +1003,7 @@ pub fn settle_deal_payments_no_change(
let client_acct = get_balance(rt, &client_addr);
let provider_acct = get_balance(rt, &provider_addr);

settle_deal_payments(rt, caller, deal_ids);
settle_deal_payments(rt, caller, deal_ids, &[], &[]);

let st: State = rt.get_state();
let new_client_acct = get_balance(rt, &client_addr);
Expand All @@ -994,6 +1019,7 @@ pub fn settle_deal_payments_and_assert_balances(
provider_addr: Address,
current_epoch: ChainEpoch,
deal_id: DealID,
already_terminated: bool,
) -> (TokenAmount, TokenAmount) {
// fetch current client escrow balances
let c_acct = get_balance(rt, &client_addr);
Expand Down Expand Up @@ -1045,7 +1071,15 @@ pub fn settle_deal_payments_and_assert_balances(
updated_provider_locked = TokenAmount::zero();
}

settle_deal_payments(rt, provider_addr, &[deal_id]);
let mut completed: Vec<DealID> = vec![];
let mut terminated: Vec<DealID> = vec![];
if is_deal_expired {
completed.push(deal_id);
} else if s.slash_epoch != EPOCH_UNDEFINED && !already_terminated {
terminated.push(deal_id);
}

settle_deal_payments(rt, provider_addr, &[deal_id], &completed, &terminated);

let client_acct = get_balance(rt, &client_addr);
let provider_acct = get_balance(rt, &provider_addr);
Expand Down
2 changes: 1 addition & 1 deletion actors/market/tests/market_actor_legacy_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fn settling_deal_fails_when_deal_update_epoch_is_in_the_future() {
// set current epoch of the deal to the end epoch so it's picked up for "processing" in the next cron tick.
rt.set_epoch(end_epoch);
expect_abort(ExitCode::USR_ILLEGAL_STATE, cron_tick_raw(&rt));
let ret = settle_deal_payments(&rt, MinerAddresses::default().provider, &[deal_id]);
let ret = settle_deal_payments(&rt, MinerAddresses::default().provider, &[deal_id], &[], &[]);
assert_eq!(ret.results.codes(), &[ExitCode::USR_ILLEGAL_STATE]);

check_state_with_expected(
Expand Down
9 changes: 5 additions & 4 deletions actors/market/tests/market_actor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,7 @@ fn settling_deal_fails_when_deal_update_epoch_is_in_the_future() {

// set current epoch of the deal to the end epoch so it's picked up for "processing" in the next cron tick.
rt.set_epoch(end_epoch);
let ret = settle_deal_payments(&rt, MinerAddresses::default().provider, &[deal_id]);
let ret = settle_deal_payments(&rt, MinerAddresses::default().provider, &[deal_id], &[], &[]);
assert_eq!(ret.results.codes(), &[ExitCode::USR_ILLEGAL_STATE]);

check_state_with_expected(
Expand Down Expand Up @@ -1435,6 +1435,7 @@ fn settling_payments_for_a_deal_at_its_start_epoch_results_in_zero_payment_and_n
MinerAddresses::default().provider,
start_epoch,
deal_id,
false,
);
assert_eq!(TokenAmount::zero(), pay);
assert_eq!(TokenAmount::zero(), slashed);
Expand Down Expand Up @@ -1715,7 +1716,7 @@ fn locked_fund_tracking_states() {
None,
ExitCode::OK,
);
settle_deal_payments(&rt, OWNER_ADDR, &[deal_id1, deal_id2, deal_id3]);
settle_deal_payments(&rt, OWNER_ADDR, &[deal_id1, deal_id2, deal_id3], &[], &[]);
let duration = curr - start_epoch;
let payment: TokenAmount = 2 * &d1.storage_price_per_epoch * duration;
let mut csf = (csf - payment) - d3.total_storage_fee();
Expand All @@ -1728,7 +1729,7 @@ fn locked_fund_tracking_states() {
let duration = curr - last_payment_epoch;
let payment = 2 * d1.storage_price_per_epoch * duration;
csf -= payment;
settle_deal_payments(&rt, OWNER_ADDR, &[deal_id1, deal_id2, deal_id3]);
settle_deal_payments(&rt, OWNER_ADDR, &[deal_id1, deal_id2, deal_id3], &[], &[]);
assert_locked_fund_states(&rt, csf.clone(), plc.clone(), clc.clone());

// terminate deal1
Expand All @@ -1740,7 +1741,7 @@ fn locked_fund_tracking_states() {
csf = TokenAmount::zero();
clc = TokenAmount::zero();
plc = TokenAmount::zero();
settle_deal_payments(&rt, OWNER_ADDR, &[deal_id1, deal_id2, deal_id3]);
settle_deal_payments(&rt, OWNER_ADDR, &[deal_id1, deal_id2, deal_id3], &[deal_id2], &[]);
assert_locked_fund_states(&rt, csf, plc, clc);
check_state(&rt);
}
Expand Down
6 changes: 4 additions & 2 deletions actors/market/tests/settle_deal_payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn timedout_deal_is_slashed_and_deleted() {
);

// settle deal payments -> should time out and get slashed
settle_deal_payments(&rt, CLIENT_ADDR, &[deal_id]);
settle_deal_payments(&rt, CLIENT_ADDR, &[deal_id], &[], &[]);

let client_acct = get_balance(&rt, &CLIENT_ADDR);
assert_eq!(c_escrow, client_acct.balance);
Expand Down Expand Up @@ -77,7 +77,7 @@ fn can_manually_settle_deals_in_the_cron_queue() {
rt.set_epoch(START_EPOCH + 100);

// manually call settle_deal_payments
let ret = settle_deal_payments(&rt, addrs.provider, &[deal_id]);
let ret = settle_deal_payments(&rt, addrs.provider, &[deal_id], &[], &[]);
let payment = ret.settlements[0].payment.clone();
assert_eq!(&payment, &(&deal_proposal.storage_price_per_epoch * 100));

Expand Down Expand Up @@ -236,6 +236,8 @@ fn batch_settlement_of_deals_allows_partial_success() {
&rt,
addrs.provider,
&[continuing_id, finished_id, terminated_id, unactivated_id, 9999],
&[finished_id],
&[],
);

assert_eq!(
Expand Down
9 changes: 8 additions & 1 deletion actors/market/tests/transient_marked_for_termination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,21 @@ fn deal_scheduled_for_termination_cannot_be_settled_manually() {
None,
ExitCode::OK,
);
expect_emitted(
&rt,
"deal-terminated",
slashed_deal,
slashed_prop.client.id().unwrap(),
slashed_prop.provider.id().unwrap(),
);
cron_tick(&rt);

// assert that the slashed deal was terminated
assert_deal_deleted(&rt, slashed_deal, &slashed_prop, sector_number);

// attempt to settle payment for both deals again - partially succeeds because not found deals are ignored
rt.set_epoch(scheduled_epoch + 1);
let ret = settle_deal_payments(&rt, PROVIDER_ADDR, &[deal_id_1, slashed_deal]);
let ret = settle_deal_payments(&rt, PROVIDER_ADDR, &[deal_id_1, slashed_deal], &[], &[]);
let expected_payment =
deal_1_prop.storage_price_per_epoch * (scheduled_epoch + 1 - START_EPOCH);
assert_eq!(ret.results.codes(), vec![ExitCode::OK, EX_DEAL_EXPIRED]);
Expand Down
15 changes: 2 additions & 13 deletions actors/miner/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,16 @@ pub mod market {
}

#[derive(Serialize_tuple, Deserialize_tuple, Clone)]
pub struct ActivateDealInfo {
pub struct ActivateDeal {
pub client: ActorID,
pub allocation_id: u64,
pub data: Cid,
pub size: PaddedPieceSize,
}

impl Default for ActivateDealInfo {
fn default() -> ActivateDealInfo {
ActivateDealInfo {
size: PaddedPieceSize(0),
client: 0,
allocation_id: 0,
data: Default::default(),
}
}
}

#[derive(Serialize_tuple, Deserialize_tuple, Clone)]
pub struct SectorDealActivation {
pub activated: Vec<ActivateDealInfo>,
pub activated: Vec<ActivateDeal>,
pub unsealed_cid: Option<Cid>,
}

Expand Down
1 change: 0 additions & 1 deletion integration_tests/src/expects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ impl Expect {
pub fn market_activate_deals(
from: ActorID,
deals: Vec<DealID>,
// TODO: A more future-proof one would take the deals as a Vec<(DealID, ActorId)>
client_id: ActorID,
sector_number: SectorNumber,
sector_expiry: ChainEpoch,
Expand Down

0 comments on commit f0a0631

Please sign in to comment.