Skip to content

Commit

Permalink
Simply deal activation return and intermediate data flow
Browse files Browse the repository at this point in the history
  • Loading branch information
anorth committed Jan 12, 2024
1 parent 6e3b5ca commit f18a8b7
Show file tree
Hide file tree
Showing 16 changed files with 234 additions and 251 deletions.
32 changes: 8 additions & 24 deletions actors/market/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,9 +600,7 @@ impl Actor {
validated_proposals.push(proposal);
}

let mut verified_infos = vec![];
let mut unverified_infos: Vec<UnVerifiedDealInfo> = vec![];
let mut nonverified_deal_space: BigInt = BigInt::zero();
let mut activated = vec![];
// Given that all deals validated, prepare the state updates for them all.
// There's no continue below here to ensure updates are consistent.
// Any error must abort.
Expand All @@ -611,21 +609,12 @@ impl Actor {
// Extract and remove any verified allocation ID for the pending deal.
let alloc_id =
pending_deal_allocation_ids.delete(deal_id)?.unwrap_or(NO_ALLOCATION_ID);

if alloc_id != NO_ALLOCATION_ID {
verified_infos.push(VerifiedDealInfo {
client: proposal.client.id().unwrap(),
allocation_id: alloc_id,
data: proposal.piece_cid,
size: proposal.piece_size,
})
} else {
unverified_infos.push(UnVerifiedDealInfo {
data: proposal.piece_cid,
size: proposal.piece_size,
});
nonverified_deal_space += proposal.piece_size.0;
}
activated.push(ActivatedDeal {
client: proposal.client.id().unwrap(),
allocation_id: alloc_id,
data: proposal.piece_cid,
size: proposal.piece_size,
});

// Prepare initial deal state.
deal_states.push((
Expand All @@ -648,12 +637,7 @@ impl Actor {

sectors_deals
.push((sector.sector_number, SectorDealIDs { deals: sector.deal_ids.clone() }));
activations.push(SectorDealActivation {
nonverified_deal_space,
verified_infos,
unverified_infos,
unsealed_cid: data_commitment,
});
activations.push(SectorDealActivation { activated, unsealed_cid: data_commitment });

for (deal_id, proposal) in sector.deal_ids.iter().zip(&validated_proposals) {
emit::deal_activated(
Expand Down
21 changes: 7 additions & 14 deletions actors/market/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,11 @@ pub struct BatchActivateDealsParams {
pub compute_cid: bool,
}

// Information about a un-verified deal that has been activated.
// Information about a 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 {
pub struct ActivatedDeal {
pub client: ActorID,
pub allocation_id: AllocationID,
pub allocation_id: AllocationID, // NO_ALLOCATION_ID for unverified deals.
pub data: Cid,
pub size: PaddedPieceSize,
}
Expand All @@ -122,12 +115,12 @@ pub struct VerifiedDealInfo {
#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)]
pub struct SectorDealActivation {
/// The total size of the non-verified deals activated.
#[serde(with = "bigint_ser")]
pub nonverified_deal_space: BigInt,
// #[serde(with = "bigint_ser")]
// pub nonverified_deal_space: BigInt,
/// Information about each verified deal activated.
pub verified_infos: Vec<VerifiedDealInfo>,
pub activated: Vec<ActivatedDeal>,
/// Information about each un-verified deal activated.
pub unverified_infos: Vec<UnVerifiedDealInfo>,
// 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
2 changes: 1 addition & 1 deletion actors/market/tests/activate_deal_failures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn assert_activation_failure(
deal_ids: vec![deal_id],
}],
false,
vec![],
&[],
)
.unwrap();
let res: BatchActivateDealsResult =
Expand Down
12 changes: 5 additions & 7 deletions actors/market/tests/batch_activate_deals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ fn sectors_fail_and_succeed_independently_during_batch_activation() {
},
];

let res =
batch_activate_deals_raw(&rt, PROVIDER_ADDR, sectors_deals, false, vec![id_4]).unwrap();
let res = batch_activate_deals_raw(&rt, PROVIDER_ADDR, sectors_deals, false, &[id_4]).unwrap();
let res: BatchActivateDealsResult =
res.unwrap().deserialize().expect("VerifyDealsForActivation failed!");

Expand Down Expand Up @@ -228,8 +227,7 @@ fn handles_sectors_empty_of_deals_gracefully() {
SectorDeals { sector_number: 3, deal_ids: vec![], sector_type, sector_expiry: END_EPOCH },
];

let res =
batch_activate_deals_raw(&rt, PROVIDER_ADDR, sectors_deals, false, vec![id_1]).unwrap();
let res = batch_activate_deals_raw(&rt, PROVIDER_ADDR, sectors_deals, false, &[id_1]).unwrap();
let res: BatchActivateDealsResult =
res.unwrap().deserialize().expect("VerifyDealsForActivation failed!");

Expand Down Expand Up @@ -275,7 +273,7 @@ fn fails_to_activate_single_sector_duplicate_deals() {
sector_expiry: END_EPOCH,
},
];
let res = batch_activate_deals_raw(&rt, PROVIDER_ADDR, sectors_deals, false, vec![]).unwrap();
let res = batch_activate_deals_raw(&rt, PROVIDER_ADDR, sectors_deals, false, &[]).unwrap();
let res: BatchActivateDealsResult =
res.unwrap().deserialize().expect("VerifyDealsForActivation failed!");

Expand Down Expand Up @@ -330,8 +328,8 @@ fn fails_to_activate_cross_sector_duplicate_deals() {
},
];

let res = batch_activate_deals_raw(&rt, PROVIDER_ADDR, sectors_deals, false, vec![id_1, id_3])
.unwrap();
let res =
batch_activate_deals_raw(&rt, PROVIDER_ADDR, sectors_deals, false, &[id_1, id_3]).unwrap();
let res: BatchActivateDealsResult =
res.unwrap().deserialize().expect("VerifyDealsForActivation failed!");

Expand Down
13 changes: 7 additions & 6 deletions actors/market/tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ pub fn activate_deals(
current_epoch,
sector_number,
deal_ids,
deal_ids.into(),
deal_ids,
)
}

Expand All @@ -344,7 +344,7 @@ pub fn activate_deals_for(
current_epoch: ChainEpoch,
sector_number: SectorNumber,
deal_ids: &[DealID],
expected_deal_activations: Vec<DealID>,
expected_deal_activations: &[DealID],
) -> BatchActivateDealsResult {
rt.set_epoch(current_epoch);
let compute_cid = false;
Expand Down Expand Up @@ -397,7 +397,8 @@ pub fn batch_activate_deals(
let deal_ids =
sectors.iter().flat_map(|(_, _, deal_ids)| deal_ids).cloned().collect::<Vec<_>>();

let ret = batch_activate_deals_raw(rt, provider, sectors_deals, compute_cid, deal_ids).unwrap();
let ret =
batch_activate_deals_raw(rt, provider, sectors_deals, compute_cid, &deal_ids).unwrap();

let ret: BatchActivateDealsResult =
ret.unwrap().deserialize().expect("VerifyDealsForActivation failed!");
Expand All @@ -413,20 +414,20 @@ pub fn batch_activate_deals_raw(
provider: Address,
sectors_deals: Vec<SectorDeals>,
compute_cid: bool,
expected_activated_deals: Vec<DealID>,
expected_activated_deals: &[DealID],
) -> Result<Option<IpldBlock>, ActorError> {
rt.set_caller(*MINER_ACTOR_CODE_ID, provider);
rt.expect_validate_caller_type(vec![Type::Miner]);

let params = BatchActivateDealsParams { sectors: sectors_deals, compute_cid };

for deal_id in expected_activated_deals {
let dp = get_deal_proposal(rt, deal_id);
let dp = get_deal_proposal(rt, *deal_id);

expect_emitted(
rt,
"deal-activated",
deal_id,
*deal_id,
dp.client.id().unwrap(),
dp.provider.id().unwrap(),
);
Expand Down
6 changes: 3 additions & 3 deletions actors/market/tests/market_actor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,7 @@ fn fail_when_current_epoch_greater_than_start_epoch_of_deal() {
deal_ids: vec![deal_id],
}],
false,
vec![],
&[],
)
.unwrap();

Expand Down Expand Up @@ -1763,7 +1763,7 @@ fn fail_when_end_epoch_of_deal_greater_than_sector_expiry() {
deal_ids: vec![deal_id],
}],
false,
vec![],
&[],
)
.unwrap();

Expand Down Expand Up @@ -1817,7 +1817,7 @@ fn fail_to_activate_all_deals_if_one_deal_fails() {
deal_ids: vec![deal_id1, deal_id2],
}],
false,
vec![],
&[],
)
.unwrap();
let res: BatchActivateDealsResult =
Expand Down
2 changes: 1 addition & 1 deletion actors/market/tests/random_cron_epoch_during_publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fn activation_after_deal_start_epoch_but_before_it_is_processed_fails() {
curr_epoch,
SECTOR_NUMBER,
&[deal_id],
vec![],
&[],
);
assert_eq!(res.activation_results.codes(), vec![EX_DEAL_EXPIRED]);
check_state(&rt);
Expand Down
83 changes: 43 additions & 40 deletions actors/market/tests/verify_deals_for_activation_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@

use fvm_ipld_encoding::ipld_block::IpldBlock;
use fvm_shared::address::Address;
use fvm_shared::bigint::BigInt;
use fvm_shared::clock::ChainEpoch;
use fvm_shared::econ::TokenAmount;
use fvm_shared::error::ExitCode;
use fvm_shared::piece::PieceInfo;
use fvm_shared::sector::RegisteredSealProof;
use num_traits::Zero;

use fil_actor_market::{
Actor as MarketActor, Method, SectorDeals, UnVerifiedDealInfo, VerifyDealsForActivationParams,
ActivatedDeal, Actor as MarketActor, Method, SectorDeals, VerifyDealsForActivationParams,
NO_ALLOCATION_ID,
};
use fil_actors_runtime::runtime::builtins::Type;
use fil_actors_runtime::test_utils::{
Expand Down Expand Up @@ -58,14 +57,15 @@ fn verify_deal_and_activate_to_get_deal_space_for_unverified_deal_proposal() {
let s_response = a_response.activations.get(0).unwrap();
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!(1, s_response.activated.len());
assert_eq!(
UnVerifiedDealInfo { data: deal_proposal.piece_cid, size: deal_proposal.piece_size },
*info
ActivatedDeal {
client: CLIENT_ADDR.id().unwrap(),
allocation_id: NO_ALLOCATION_ID,
data: deal_proposal.piece_cid,
size: deal_proposal.piece_size
},
*s_response.activated.get(0).unwrap()
);

check_state(&rt);
Expand Down Expand Up @@ -103,15 +103,11 @@ 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);
assert_eq!(next_allocation_id, s_response.verified_infos[0].allocation_id);

assert_eq!(BigInt::zero(), s_response.nonverified_deal_space);

assert_eq!(1, s_response.activated.len());
assert_eq!(deal_proposal.piece_size, s_response.activated[0].size);
assert_eq!(deal_proposal.client.id().unwrap(), s_response.activated[0].client);
assert_eq!(deal_proposal.piece_cid, s_response.activated[0].data);
assert_eq!(next_allocation_id, s_response.activated[0].allocation_id);
check_state(&rt);
}

Expand Down Expand Up @@ -139,7 +135,7 @@ fn verification_and_weights_for_verified_and_unverified_deals() {
assert_eq!(4, deal_ids.len());

let sector_number = 7;
let response = verify_deals_for_activation(
verify_deals_for_activation(
&rt,
PROVIDER_ADDR,
vec![SectorDeals {
Expand All @@ -158,38 +154,45 @@ fn verification_and_weights_for_verified_and_unverified_deals() {
},
);

let verified_space = BigInt::from(verified_deal_1.piece_size.0 + verified_deal_2.piece_size.0);
let unverified_space =
BigInt::from(unverified_deal_1.piece_size.0 + unverified_deal_2.piece_size.0);

let a_response =
activate_deals(&rt, SECTOR_EXPIRY, PROVIDER_ADDR, CURR_EPOCH, sector_number, &deal_ids);
let s_response = a_response.activations.get(0).unwrap();

assert_eq!(1, response.unsealed_cids.len());
let returned_verified_space: BigInt =
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!(4, s_response.activated.len());
assert_eq!(
&ActivatedDeal {
client: CLIENT_ADDR.id().unwrap(),
allocation_id: 1,
data: verified_deal_1.piece_cid,
size: verified_deal_1.piece_size,
},
s_response.activated.get(0).unwrap()
);
assert_eq!(
UnVerifiedDealInfo {
&ActivatedDeal {
client: CLIENT_ADDR.id().unwrap(),
allocation_id: 2,
data: verified_deal_2.piece_cid,
size: verified_deal_2.piece_size,
},
s_response.activated.get(1).unwrap()
);
assert_eq!(
&ActivatedDeal {
client: CLIENT_ADDR.id().unwrap(),
allocation_id: NO_ALLOCATION_ID,
data: unverified_deal_1.piece_cid,
size: unverified_deal_1.piece_size,
},
*resp_unverified_deal_1
s_response.activated.get(2).unwrap()
);

assert_eq!(
UnVerifiedDealInfo {
&ActivatedDeal {
client: CLIENT_ADDR.id().unwrap(),
allocation_id: NO_ALLOCATION_ID,
data: unverified_deal_2.piece_cid,
size: unverified_deal_2.piece_size,
},
*resp_unverified_deal_2
s_response.activated.get(3).unwrap()
);

check_state(&rt);
Expand Down
Loading

0 comments on commit f18a8b7

Please sign in to comment.