Skip to content

Commit

Permalink
batch activate deals should return piece info
Browse files Browse the repository at this point in the history
  • Loading branch information
aarshkshah1992 committed Jan 10, 2024
1 parent 4a1ecdf commit aef0fdd
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 100 deletions.
6 changes: 6 additions & 0 deletions actors/market/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ impl Actor {
}

let mut verified_infos = vec![];
let mut unverified_infos: Vec<UnVerifiedDealInfo> = vec![];
let mut nonverified_deal_space: BigInt = BigInt::zero();
// Given that all deals validated, prepare the state updates for them all.
// There's no continue below here to ensure updates are consistent.
Expand All @@ -619,6 +620,10 @@ impl Actor {
size: proposal.piece_size,
})
} else {
unverified_infos.push(UnVerifiedDealInfo {
data: proposal.piece_cid,
size: proposal.piece_size,
});
nonverified_deal_space += proposal.piece_size.0;
}

Expand Down Expand Up @@ -646,6 +651,7 @@ impl Actor {
activations.push(SectorDealActivation {
nonverified_deal_space,
verified_infos,
unverified_infos,
unsealed_cid: data_commitment,
});

Expand Down
9 changes: 9 additions & 0 deletions actors/market/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ pub struct BatchActivateDealsParams {
pub compute_cid: bool,
}

// Information about a un-verified deal that has been activated.
#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)]
pub struct UnVerifiedDealInfo {
pub data: Cid,
pub size: PaddedPieceSize,
}

// Information about a verified deal that has been activated.
#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)]
pub struct VerifiedDealInfo {
Expand All @@ -119,6 +126,8 @@ pub struct SectorDealActivation {
pub nonverified_deal_space: BigInt,
/// Information about each verified deal activated.
pub verified_infos: Vec<VerifiedDealInfo>,
/// Information about each un-verified deal activated.
pub unverified_infos: Vec<UnVerifiedDealInfo>,
/// Unsealed CID computed from the deals specified for the sector.
/// A None indicates no deals were specified, or the computation was not requested.
pub unsealed_cid: Option<Cid>,
Expand Down
32 changes: 31 additions & 1 deletion actors/market/tests/verify_deals_for_activation_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use fvm_shared::piece::PieceInfo;
use fvm_shared::sector::RegisteredSealProof;
use num_traits::Zero;

use fil_actor_market::{Actor as MarketActor, Method, SectorDeals, VerifyDealsForActivationParams};
use fil_actor_market::{
Actor as MarketActor, Method, SectorDeals, UnVerifiedDealInfo, VerifyDealsForActivationParams,
};
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,
Expand Down Expand Up @@ -57,8 +59,15 @@ fn verify_deal_and_activate_to_get_deal_space_for_unverified_deal_proposal() {
assert_eq!(1, v_response.unsealed_cids.len());
assert_eq!(Some(make_piece_cid("1".as_bytes())), v_response.unsealed_cids[0]);
assert!(s_response.verified_infos.is_empty());
assert!(!s_response.unverified_infos.is_empty());
assert_eq!(BigInt::from(deal_proposal.piece_size.0), s_response.nonverified_deal_space);

let info = s_response.unverified_infos.get(0).unwrap();
assert_eq!(
UnVerifiedDealInfo { data: deal_proposal.piece_cid, size: deal_proposal.piece_size },
*info
);

check_state(&rt);
}

Expand Down Expand Up @@ -95,6 +104,7 @@ fn verify_deal_and_activate_to_get_deal_space_for_verified_deal_proposal() {
assert_eq!(1, response.unsealed_cids.len());
assert_eq!(Some(make_piece_cid("1".as_bytes())), response.unsealed_cids[0]);
assert_eq!(1, s_response.verified_infos.len());
assert!(s_response.unverified_infos.is_empty());
assert_eq!(deal_proposal.piece_size, s_response.verified_infos[0].size);
assert_eq!(deal_proposal.client.id().unwrap(), s_response.verified_infos[0].client);
assert_eq!(deal_proposal.piece_cid, s_response.verified_infos[0].data);
Expand Down Expand Up @@ -161,6 +171,26 @@ fn verification_and_weights_for_verified_and_unverified_deals() {
s_response.verified_infos.iter().map(|info| BigInt::from(info.size.0)).sum();
assert_eq!(verified_space, returned_verified_space);
assert_eq!(unverified_space, s_response.nonverified_deal_space);
assert_eq!(2, s_response.unverified_infos.len());

let resp_unverified_deal_1 = s_response.unverified_infos.get(0).unwrap();
let resp_unverified_deal_2 = s_response.unverified_infos.get(1).unwrap();

assert_eq!(
UnVerifiedDealInfo {
data: unverified_deal_1.piece_cid,
size: unverified_deal_1.piece_size,
},
*resp_unverified_deal_1
);

assert_eq!(
UnVerifiedDealInfo {
data: unverified_deal_2.piece_cid,
size: unverified_deal_2.piece_size,
},
*resp_unverified_deal_2
);

check_state(&rt);
}
Expand Down
20 changes: 7 additions & 13 deletions actors/miner/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub mod account {
pub mod market {
use super::*;
use fvm_ipld_bitfield::BitField;

pub const VERIFY_DEALS_FOR_ACTIVATION_METHOD: u64 = 5;
pub const BATCH_ACTIVATE_DEALS_METHOD: u64 = 6;
pub const ON_MINER_SECTORS_TERMINATE_METHOD: u64 = 7;
Expand Down Expand Up @@ -59,11 +58,18 @@ pub mod market {
}
}

#[derive(Serialize_tuple, Deserialize_tuple, Clone)]
pub struct UnVerifiedDealInfo {
pub data: Cid,
pub size: PaddedPieceSize,
}

#[derive(Serialize_tuple, Deserialize_tuple, Clone)]
pub struct SectorDealActivation {
#[serde(with = "bigint_ser")]
pub nonverified_deal_space: BigInt,
pub verified_infos: Vec<VerifiedDealInfo>,
pub unverified_infos: Vec<UnVerifiedDealInfo>,
pub unsealed_cid: Option<Cid>,
}

Expand Down Expand Up @@ -94,18 +100,6 @@ pub mod market {
pub struct VerifyDealsForActivationReturn {
pub unsealed_cids: Vec<Option<Cid>>,
}

#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)]
#[serde(transparent)]
pub struct GetDealDataCommitmentParamsRef {
pub id: DealID,
}

#[derive(Serialize_tuple, Deserialize_tuple, Clone)]
pub struct GetDealDataCommitmentReturn {
pub data: Cid,
pub size: PaddedPieceSize,
}
}

pub mod power {
Expand Down
91 changes: 48 additions & 43 deletions actors/miner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ impl Actor {
Therefore CommD on precommit has already been provided and checked so no further processing needed
*/
let compute_commd = false;
let (batch_return, activated_data) =
let (batch_return, activated_data, pieces) =
activate_sectors_deals(rt, &data_activation_inputs, compute_commd)?;
let activated_precommits = batch_return.successes(&valid_precommits);

Expand All @@ -834,10 +834,14 @@ impl Actor {
&info,
)?;

for pc in activated_precommits {
for (i, pc) in activated_precommits.iter().enumerate() {
let unsealed_cid = pc.info.unsealed_cid.get_cid(pc.info.seal_proof)?;
let pieces = get_piece_manifest_from_deals(rt, &pc.info.deal_ids)?;
emit::sector_activated(rt, pc.info.sector_number, &unsealed_cid, &pieces)?;
emit::sector_activated(
rt,
pc.info.sector_number,
&unsealed_cid,
pieces.get(i).unwrap(),
)?;
}
// The aggregate fee is paid on the sectors successfully proven.
pay_aggregate_seal_proof_fee(rt, valid_precommits.len())?;
Expand Down Expand Up @@ -921,7 +925,7 @@ impl Actor {
- no CommD was specified on input so it must be computed for the first time here
*/
let compute_commd = true;
let (batch_return, data_activations) =
let (batch_return, data_activations, pieces) =
activate_sectors_deals(rt, &data_activation_inputs, compute_commd)?;

// associate the successfully activated sectors with the ReplicaUpdateInner and SectorOnChainInfo
Expand All @@ -940,7 +944,7 @@ impl Actor {
let mut updated_sectors: Vec<SectorNumber> = Vec::new();
let mut decls_by_deadline = BTreeMap::<u64, Vec<ReplicaUpdateStateInputs>>::new();
let mut deadlines_to_load = Vec::<u64>::new();
for (usi, data_activation) in &validated_updates {
for (i, (usi, data_activation)) in validated_updates.iter().enumerate() {
updated_sectors.push(usi.update.sector_number);
let dl = usi.update.deadline;
if !decls_by_deadline.contains_key(&dl) {
Expand Down Expand Up @@ -978,8 +982,12 @@ impl Actor {
activated_data,
});

let pieces = get_piece_manifest_from_deals(rt, &usi.update.deals)?;
emit::sector_updated(rt, usi.update.sector_number, &computed_commd, &pieces)?;
emit::sector_updated(
rt,
usi.update.sector_number,
&computed_commd,
pieces.get(i).unwrap(),
)?;
}

let (power_delta, pledge_delta) = update_replica_states(
Expand Down Expand Up @@ -2000,7 +2008,7 @@ impl Actor {
Therefore CommD on precommit has already been provided and checked so no further processing needed
*/
let compute_commd = false;
let (batch_return, data_activations) =
let (batch_return, data_activations, pieces) =
activate_sectors_deals(rt, &data_activations, compute_commd)?;
let successful_activations = batch_return.successes(&precommited_sectors);

Expand All @@ -2018,10 +2026,14 @@ impl Actor {
&info,
)?;

for pc in successful_activations.iter() {
let pieces = get_piece_manifest_from_deals(rt, &pc.info.deal_ids)?;
for (i, pc) in successful_activations.iter().enumerate() {
let unsealed_cid = pc.info.unsealed_cid.get_cid(pc.info.seal_proof)?;
emit::sector_activated(rt, pc.info.sector_number, &unsealed_cid, &pieces)?;
emit::sector_activated(
rt,
pc.info.sector_number,
&unsealed_cid,
pieces.get(i).unwrap(),
)?;
}
Ok(())
}
Expand Down Expand Up @@ -4848,31 +4860,6 @@ fn verify_deals(
))?)
}

fn get_piece_manifest_from_deals(
rt: &impl Runtime,
deal_ids: &Vec<DealID>,
) -> Result<Vec<(Cid, PaddedPieceSize)>, ActorError> {
let mut pieces: Vec<(Cid, PaddedPieceSize)> = vec![];

for deal_id in deal_ids {
let ret = get_deal_data_commitment(rt, deal_id)?;
pieces.push((ret.data, ret.size));
}
Ok(pieces)
}

fn get_deal_data_commitment(
rt: &impl Runtime,
deal_id: &DealID,
) -> Result<ext::market::GetDealDataCommitmentReturn, ActorError> {
deserialize_block(extract_send_result(rt.send_simple(
&STORAGE_MARKET_ACTOR_ADDR,
ext::market::GET_DEAL_DATA_COMMITMENT,
IpldBlock::serialize_cbor(&ext::market::GetDealDataCommitmentParamsRef { id: *deal_id })?,
TokenAmount::zero(),
))?)
}

/// Requests the current epoch target block reward from the reward actor.
/// return value includes reward, smoothed estimate of reward, and baseline power
fn request_current_epoch_block_reward(
Expand Down Expand Up @@ -5505,18 +5492,21 @@ fn activate_sectors_pieces(
/// Deals and claims are grouped by sectors
/// Successfully activated sectors have their DealSpaces returned
/// Failure to claim datacap for any verified deal results in the whole batch failing
#[allow(clippy::complexity)]
fn activate_sectors_deals(
rt: &impl Runtime,
activation_infos: &[DealsActivationInput],
compute_unsealed_cid: bool,
) -> Result<(BatchReturn, Vec<DataActivationOutput>), ActorError> {
) -> Result<(BatchReturn, Vec<DataActivationOutput>, Vec<Vec<(Cid, PaddedPieceSize)>>), ActorError>
{
let batch_activation_res = match activation_infos.iter().all(|p| p.deal_ids.is_empty()) {
true => ext::market::BatchActivateDealsResult {
// if all sectors are empty of deals, skip calling the market actor
activations: vec![
ext::market::SectorDealActivation {
nonverified_deal_space: BigInt::default(),
verified_infos: Vec::default(),
unverified_infos: Vec::default(),
unsealed_cid: None,
};
activation_infos.len()
Expand Down Expand Up @@ -5551,6 +5541,8 @@ fn activate_sectors_deals(
return Err(actor_error!(illegal_argument, "all deals failed to activate"));
}

let mut piece_infos: Vec<Vec<(Cid, PaddedPieceSize)>> = vec![];

// Filter the DealsActivationInfo for successfully activated sectors
let successful_activation_infos =
batch_activation_res.activation_results.successes(activation_infos);
Expand Down Expand Up @@ -5591,15 +5583,28 @@ fn activate_sectors_deals(
.activations
.iter()
.zip(claim_res.sector_claims)
.map(|(sector_deals, sector_claim)| DataActivationOutput {
unverified_space: sector_deals.nonverified_deal_space.clone(),
verified_space: sector_claim.claimed_space,
unsealed_cid: sector_deals.unsealed_cid,
.map(|(sector_deals, sector_claim)| {
let mut piece_infos_for_sector: Vec<(Cid, PaddedPieceSize)> = vec![];

for verified_info in &sector_deals.verified_infos {
piece_infos_for_sector.push((verified_info.data, verified_info.size));
}

for unverified_info in &sector_deals.unverified_infos {
piece_infos_for_sector.push((unverified_info.data, unverified_info.size));
}
piece_infos.push(piece_infos_for_sector);

DataActivationOutput {
unverified_space: sector_deals.nonverified_deal_space.clone(),
verified_space: sector_claim.claimed_space,
unsealed_cid: sector_deals.unsealed_cid,
}
})
.collect();

// Return the deal spaces for activated sectors only
Ok((batch_activation_res.activation_results, activation_and_claim_results))
Ok((batch_activation_res.activation_results, activation_and_claim_results, piece_infos))
}

fn batch_claim_allocations(
Expand Down
8 changes: 7 additions & 1 deletion actors/miner/tests/extend_sector_expiration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use fil_actors_runtime::{
EPOCHS_IN_DAY,
};
use fvm_ipld_bitfield::BitField;
use fvm_shared::deal::DealID;
use fvm_shared::{
address::Address,
clock::ChainEpoch,
Expand Down Expand Up @@ -899,11 +900,16 @@ fn commit_sector_verified_deals(
let mut pcc = ProveCommitConfig::empty();
pcc.add_verified_deals(h.next_sector_no, verified_deals.clone());

let mut deal_ids: Vec<DealID> = vec![];
for i in 0..verified_deals.len() {
deal_ids.push(i as u64);
}

let sector_info = &h.commit_and_prove_sectors_with_cfgs(
rt,
1,
DEFAULT_SECTOR_EXPIRATION as u64,
vec![vec![42]],
vec![deal_ids],
true,
pcc,
)[0];
Expand Down
4 changes: 2 additions & 2 deletions actors/miner/tests/prove_commit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::HashMap;

use fvm_shared::{
bigint::{BigInt, Zero},
clock::ChainEpoch,
Expand All @@ -8,6 +6,7 @@ use fvm_shared::{
sector::{StoragePower, MAX_SECTOR_NUMBER},
smooth::FilterEstimate,
};
use std::collections::HashMap;

use fil_actor_miner::{
initial_pledge_for_power, max_prove_commit_duration, pre_commit_deposit_for_power,
Expand Down Expand Up @@ -508,6 +507,7 @@ fn drop_invalid_prove_commit_while_processing_valid_one() {

let conf = ProveCommitConfig {
verify_deals_exit: HashMap::from([(sector_no_a, ExitCode::USR_ILLEGAL_ARGUMENT)]),
verified_deal_infos: HashMap::from([(sector_no_b, vec![test_verified_deal(100)])]),
..Default::default()
};
h.confirm_sector_proofs_valid_for(
Expand Down
Loading

0 comments on commit aef0fdd

Please sign in to comment.