Skip to content

Commit

Permalink
Miner Actor pre-commit event
Browse files Browse the repository at this point in the history
  • Loading branch information
aarshkshah1992 committed Oct 25, 2023
1 parent 7500bab commit e0bf67e
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 39 deletions.
16 changes: 16 additions & 0 deletions actors/miner/src/emit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// A namespace for helpers that build and emit Miner Actor events.

use fil_actors_runtime::runtime::Runtime;
use fil_actors_runtime::{ActorError, EventBuilder};
use fvm_shared::sector::SectorID;

/// Indicates a sector has been pre-committed.
pub fn sector_precommited(rt: &impl Runtime, id: SectorID) -> Result<(), ActorError> {
rt.emit_event(
&EventBuilder::new()
.typ("sector-precommited")
.field_indexed("miner", &id.miner)
.field_indexed("sector", &id.number)
.build()?,
)
}
42 changes: 41 additions & 1 deletion actors/miner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ pub mod testing;
mod types;
mod vesting_state;

pub mod emit;

// The first 1000 actor-specific codes are left open for user error, i.e. things that might
// actually happen without programming error in the actor code.

Expand Down Expand Up @@ -1422,6 +1424,16 @@ impl Actor {
rt: &impl Runtime,
params: PreCommitSectorBatchParams,
) -> Result<(), ActorError> {
let miner_actor_id: u64 = if let Payload::ID(i) = rt.message().receiver().payload() {
*i
} else {
return Err(actor_error!(
illegal_state,
"runtime provided non ID receiver address {}",
rt.message().receiver()
));
};

let sectors = params
.sectors
.into_iter()
Expand All @@ -1446,7 +1458,7 @@ impl Actor {
}
})
.collect::<Result<_, _>>()?;
Self::pre_commit_sector_batch_inner(rt, sectors)
Self::pre_commit_sector_batch_inner(rt, sectors, miner_actor_id)
}

/// Pledges the miner to seal and commit some new sectors.
Expand All @@ -1459,6 +1471,16 @@ impl Actor {
rt: &impl Runtime,
params: PreCommitSectorBatchParams2,
) -> Result<(), ActorError> {
let miner_actor_id: u64 = if let Payload::ID(i) = rt.message().receiver().payload() {
*i
} else {
return Err(actor_error!(
illegal_state,
"runtime provided non ID receiver address {}",
rt.message().receiver()
));
};

Self::pre_commit_sector_batch_inner(
rt,
params
Expand All @@ -1475,6 +1497,7 @@ impl Actor {
unsealed_cid: Some(spci.unsealed_cid),
})
.collect(),
miner_actor_id,
)
}

Expand All @@ -1483,6 +1506,7 @@ impl Actor {
fn pre_commit_sector_batch_inner(
rt: &impl Runtime,
sectors: Vec<SectorPreCommitInfoInner>,
miner_actor_id: ActorID,
) -> Result<(), ActorError> {
let curr_epoch = rt.curr_epoch();
{
Expand Down Expand Up @@ -1635,6 +1659,8 @@ impl Actor {
let sector_weight_for_deposit = qa_power_max(info.sector_size);
let deposit_req = pre_commit_deposit_for_power(&reward_stats.this_epoch_reward_smoothed, &power_total.quality_adj_power_smoothed, &sector_weight_for_deposit);

let mut precommited_sectors = vec![];

for (i, precommit) in sectors.into_iter().enumerate() {
// Sector must have the same Window PoSt proof type as the miner's recorded seal type.
let sector_wpost_proof = precommit.seal_proof
Expand Down Expand Up @@ -1696,6 +1722,8 @@ impl Actor {
// ConfirmSectorProofsValid would fail to find it.
let clean_up_bound = curr_epoch + msd + rt.policy().expired_pre_commit_clean_up_delay;
clean_up_events.push((clean_up_bound, precommit.sector_number));

precommited_sectors.push(precommit.sector_number);
}
// Batch update actor state.
if available_balance < total_deposit_required {
Expand All @@ -1720,9 +1748,20 @@ impl Actor {
.map_err(|e| {
e.downcast_default(ExitCode::USR_ILLEGAL_STATE, "failed to add pre-commit expiry to queue")
})?;


// Activate miner cron
needs_cron = !state.deadline_cron_active;
state.deadline_cron_active = true;

for sector_num in precommited_sectors.into_iter() {
let sector_id = SectorID{
miner: miner_actor_id,
number: sector_num,
};
emit::sector_precommited(rt, sector_id)?;
}

Ok(())
})?;
burn_funds(rt, fee_to_burn)?;
Expand All @@ -1736,6 +1775,7 @@ impl Actor {
CronEventPayload { event_type: CRON_EVENT_PROVING_DEADLINE },
)?;
}

Ok(())
}

Expand Down
4 changes: 4 additions & 0 deletions actors/miner/tests/batch_method_network_fees_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ fn insufficient_funds_for_batch_precommit_network_fee() {
PreCommitSectorBatchParams { sectors: precommits },
&PreCommitBatchConfig { first_for_miner: true, ..Default::default() },
&base_fee,
false,
);

// state untouched
Expand Down Expand Up @@ -156,6 +157,7 @@ fn insufficient_funds_for_batch_precommit_in_combination_of_fee_debt_and_network
PreCommitSectorBatchParams { sectors: precommits },
&PreCommitBatchConfig { first_for_miner: true, ..Default::default() },
&base_fee,
false,
);

// state untouched
Expand Down Expand Up @@ -211,6 +213,7 @@ fn enough_funds_for_fee_debt_and_network_fee_but_not_for_pcd() {
PreCommitSectorBatchParams { sectors: precommits },
&PreCommitBatchConfig { first_for_miner: true, ..Default::default() },
&base_fee,
false,
);

expect_abort_contains_message(
Expand Down Expand Up @@ -274,6 +277,7 @@ fn enough_funds_for_everything() {
PreCommitSectorBatchParams { sectors: precommits },
&PreCommitBatchConfig { first_for_miner: true, ..Default::default() },
&base_fee,
true,
)
.unwrap();

Expand Down
5 changes: 3 additions & 2 deletions actors/miner/tests/compact_sector_numbers_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mod compact_sector_numbers_test {
);
expect_abort(
ExitCode::USR_ILLEGAL_ARGUMENT,
h.pre_commit_sector(&rt, precommit, util::PreCommitConfig::default(), false),
h.pre_commit_sector(&rt, precommit, util::PreCommitConfig::default(), false, false),
);
}

Expand All @@ -60,7 +60,8 @@ mod compact_sector_numbers_test {
expiration,
vec![],
);
h.pre_commit_sector(&rt, precommit, util::PreCommitConfig::default(), false).unwrap();
h.pre_commit_sector(&rt, precommit, util::PreCommitConfig::default(), false, true)
.unwrap();
}
check_state_invariants_from_mock_runtime(&rt);
}
Expand Down
Loading

0 comments on commit e0bf67e

Please sign in to comment.