Skip to content

Commit

Permalink
miner actor events
Browse files Browse the repository at this point in the history
  • Loading branch information
aarshkshah1992 committed Jan 3, 2024
1 parent 6e5d70c commit 9a22c67
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 26 deletions.
32 changes: 32 additions & 0 deletions actors/miner/src/emit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// A namespace for helpers that build and emit provider Actor events.
use fil_actors_runtime::runtime::Runtime;
use fil_actors_runtime::{ActorError, EventBuilder};
use fvm_shared::sector::SectorNumber;

/// Indicates a sector has been pre-committed.
pub fn sector_precommitted(rt: &impl Runtime, sector: SectorNumber) -> Result<(), ActorError> {
rt.emit_event(
&EventBuilder::new().typ("sector-precommitted").field_indexed("sector", &sector).build()?,
)
}

/// Indicates a sector has been activated.
pub fn sector_activated(rt: &impl Runtime, sector: SectorNumber) -> Result<(), ActorError> {
rt.emit_event(
&EventBuilder::new().typ("sector-activated").field_indexed("sector", &sector).build()?,
)
}

/// Indicates a sector has been updated.
pub fn sector_updated(rt: &impl Runtime, sector: SectorNumber) -> Result<(), ActorError> {
rt.emit_event(
&EventBuilder::new().typ("sector-updated").field_indexed("sector", &sector).build()?,
)
}

/// Indicates a sector has been terminated.
pub fn sector_terminated(rt: &impl Runtime, sector: SectorNumber) -> Result<(), ActorError> {
rt.emit_event(
&EventBuilder::new().typ("sector-terminated").field_indexed("sector", &sector).build()?,
)
}
22 changes: 22 additions & 0 deletions actors/miner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ pub mod testing;
mod types;
mod vesting_state;

pub mod emit;

/// Storage Miner actor methods available
#[derive(FromPrimitive)]
#[repr(u64)]
Expand Down Expand Up @@ -1657,6 +1659,10 @@ impl Actor {
.map_err(|e| {
e.downcast_default(ExitCode::USR_ILLEGAL_STATE, "failed to add pre-commit expiry to queue")
})?;

for sector_num in sector_numbers.iter() {
emit::sector_precommitted(rt, sector_num)?;
}
// Activate miner cron
needs_cron = !state.deadline_cron_active;
state.deadline_cron_active = true;
Expand Down Expand Up @@ -3972,6 +3978,8 @@ where
expected_count
));
}
let updated_sector_nums =
new_sectors.iter().map(|x| x.sector_number).collect::<Vec<SectorNumber>>();

// Overwrite sector infos.
sectors.store(new_sectors).map_err(|e| {
Expand All @@ -3985,6 +3993,10 @@ where
e.downcast_default(ExitCode::USR_ILLEGAL_STATE, "failed to save deadlines")
})?;

for sector in updated_sector_nums {
emit::sector_updated(rt, sector)?;
}

// Update pledge.
let current_balance = rt.current_balance();
if pledge_delta.is_positive() {
Expand Down Expand Up @@ -4080,6 +4092,7 @@ fn process_early_terminations(
reward_smoothed: &FilterEstimate,
quality_adj_power_smoothed: &FilterEstimate,
) -> Result</* more */ bool, ActorError> {
let mut terminated_sector_nums = vec![];
let mut sectors_with_data = vec![];
let (result, more, penalty, pledge_delta) = rt.transaction(|state: &mut State, rt| {
let store = rt.store();
Expand Down Expand Up @@ -4118,6 +4131,7 @@ fn process_early_terminations(
for sector in &sectors {
total_initial_pledge += &sector.initial_pledge;
let sector_power = qa_power_for_sector(info.sector_size, sector);
terminated_sector_nums.push(sector.sector_number);
total_penalty += pledge_penalty_for_termination(
&sector.expected_day_reward,
epoch - sector.power_base_epoch,
Expand Down Expand Up @@ -4184,6 +4198,10 @@ fn process_early_terminations(
.context_code(ExitCode::USR_ILLEGAL_STATE, "invalid sector number")?;
request_terminate_deals(rt, rt.curr_epoch(), &terminated_data)?;

for sector in terminated_sector_nums {
emit::sector_terminated(rt, sector)?;
}

// reschedule cron worker, if necessary.
Ok(more)
}
Expand Down Expand Up @@ -5258,6 +5276,10 @@ fn activate_new_sector_infos(

state.check_balance_invariants(&rt.current_balance()).map_err(balance_invariants_broken)?;

for sector in new_sector_numbers {
emit::sector_activated(rt, sector)?;
}

Ok((total_pledge, newly_vested))
})?;
// Request pledge update for activated sectors.
Expand Down
1 change: 1 addition & 0 deletions actors/miner/tests/compact_sector_numbers_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ mod compact_sector_numbers_test {
ExitCode::USR_ILLEGAL_ARGUMENT,
h.pre_commit_sector(&rt, precommit, util::PreCommitConfig::default(), false),
);
rt.reset();
}

{
Expand Down
3 changes: 2 additions & 1 deletion actors/miner/tests/prove_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ fn drop_invalid_prove_commit_while_processing_valid_one() {
verify_deals_exit: HashMap::from([(sector_no_a, ExitCode::USR_ILLEGAL_ARGUMENT)]),
..Default::default()
};
h.confirm_sector_proofs_valid(&rt, conf, vec![pre_commit_a, pre_commit_b]).unwrap();
h.confirm_sector_proofs_valid_for(&rt, conf, vec![pre_commit_a, pre_commit_b], vec![sector_no_b]).unwrap();
let st = h.get_state(&rt);
assert!(st.get_sector(&rt.store, sector_no_a).unwrap().is_none());
assert!(st.get_sector(&rt.store, sector_no_b).unwrap().is_some());
Expand Down Expand Up @@ -572,6 +572,7 @@ fn sector_with_non_positive_lifetime_fails_in_confirmation() {
h.confirm_sector_proofs_valid(&rt, ProveCommitConfig::empty(), vec![precommit]),
);
h.check_state(&rt);
rt.reset();
}

#[test]
Expand Down
13 changes: 9 additions & 4 deletions actors/miner/tests/prove_commit2_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ fn expired_precommit_dropped_batch() {

let cfg = ProveCommitSectors2Config { validation_failure: vec![0], ..Default::default() };
let (result, claims, notifications) =
h.prove_commit_sectors2(&rt, &manifests, false, false, false, cfg).unwrap();
h.prove_commit_sectors2_for(&rt, &manifests, false, false, false,
cfg, vec![snos[1]]).unwrap();
assert_commit_result(&[ExitCode::USR_ILLEGAL_ARGUMENT, ExitCode::OK], &result);

// Sector 0: not committed
Expand Down Expand Up @@ -370,7 +371,9 @@ fn expired_precommit_dropped_aggregate() {
];

let cfg = ProveCommitSectors2Config { validation_failure: vec![0], ..Default::default() };
let (result, _, _) = h.prove_commit_sectors2(&rt, &manifests, false, false, true, cfg).unwrap();
let (result, _, _) = h.prove_commit_sectors2_for(&rt, &manifests, false,
false, true, cfg,
vec![snos[1], snos[2], snos[3]]).unwrap();
assert_commit_result(
&[ExitCode::USR_ILLEGAL_ARGUMENT, ExitCode::OK, ExitCode::OK, ExitCode::OK],
&result,
Expand Down Expand Up @@ -402,7 +405,8 @@ fn invalid_proof_dropped() {

let cfg = ProveCommitSectors2Config { proof_failure: vec![0], ..Default::default() };
let (result, _, _) =
h.prove_commit_sectors2(&rt, &manifests, false, false, false, cfg).unwrap();
h.prove_commit_sectors2_for(&rt, &manifests, false, false, false,
cfg, vec![snos[1]]).unwrap();
assert_commit_result(&[ExitCode::USR_ILLEGAL_ARGUMENT, ExitCode::OK], &result);

// Sector 0: not committed
Expand All @@ -428,7 +432,8 @@ fn invalid_claim_dropped() {

let cfg = ProveCommitSectors2Config { claim_failure: vec![0], ..Default::default() };
let (result, _, _) =
h.prove_commit_sectors2(&rt, &manifests, false, false, false, cfg).unwrap();
h.prove_commit_sectors2_for(&rt, &manifests, false, false, false,
cfg, vec![snos[1]]).unwrap();
assert_commit_result(&[ExitCode::USR_ILLEGAL_ARGUMENT, ExitCode::OK], &result);

// Sector 0: not committed
Expand Down
9 changes: 6 additions & 3 deletions actors/miner/tests/prove_replica_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@ fn invalid_update_dropped() {

let cfg = ProveReplicaUpdatesConfig { validation_failure: vec![0], ..Default::default() };
let (result, claims, notifications) =
h.prove_replica_updates2_batch(&rt, &sector_updates, false, false, cfg).unwrap();
h.prove_replica_updates2_batch_for(&rt, &sector_updates, false, false,
cfg, vec![snos[1]]).unwrap();
assert_update_result(&[ExitCode::USR_ILLEGAL_ARGUMENT, ExitCode::OK], &result);

// Sector 0: no change.
Expand Down Expand Up @@ -369,7 +370,8 @@ fn invalid_proof_dropped() {

let cfg = ProveReplicaUpdatesConfig { proof_failure: vec![0], ..Default::default() };
let (result, _, _) =
h.prove_replica_updates2_batch(&rt, &sector_updates, false, false, cfg).unwrap();
h.prove_replica_updates2_batch_for(&rt, &sector_updates, false, false,
cfg, vec![snos[1]]).unwrap();
assert_update_result(&[ExitCode::USR_ILLEGAL_ARGUMENT, ExitCode::OK], &result);

verify_weights(&rt, &h, snos[0], 0, 0);
Expand All @@ -391,7 +393,8 @@ fn invalid_claim_dropped() {

let cfg = ProveReplicaUpdatesConfig { claim_failure: vec![0], ..Default::default() };
let (result, _, _) =
h.prove_replica_updates2_batch(&rt, &sector_updates, false, false, cfg).unwrap();
h.prove_replica_updates2_batch_for(&rt, &sector_updates, false, false,
cfg, vec![snos[1]]).unwrap();
assert_update_result(&[ExitCode::USR_ILLEGAL_ARGUMENT, ExitCode::OK], &result);

verify_weights(&rt, &h, snos[0], 0, 0);
Expand Down
Loading

0 comments on commit 9a22c67

Please sign in to comment.