From 20a3b455ba29f721c6b0758561c28d9a99d96b8b Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Thu, 16 May 2024 15:19:59 +1000 Subject: [PATCH] fix(market): clean up provider_sectors when empty --- actors/market/src/state.rs | 44 ++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/actors/market/src/state.rs b/actors/market/src/state.rs index 963b2b3ab..922f66d97 100644 --- a/actors/market/src/state.rs +++ b/actors/market/src/state.rs @@ -651,6 +651,7 @@ impl State { ) -> Result<(), ActorError> { let mut provider_sectors = self.load_provider_sectors(store)?; for (provider, sector_deal_ids) in provider_sector_deal_ids { + let mut flush = false; let mut sector_deals = load_provider_sector_deals(store, &provider_sectors, *provider)?; for (sector_number, deals_to_remove) in sector_deal_ids { let existing_deal_ids = sector_deals @@ -662,20 +663,51 @@ impl State { // pretty fast. // Loading into a HashSet could be an improvement for large collections of deals // in a single sector being removed at one time. - let new_deals = existing_deal_ids + let new_deals: Vec<_> = existing_deal_ids .iter() .filter(|deal_id| !deals_to_remove.contains(*deal_id)) .cloned() .collect(); - - sector_deals - .set(sector_number, new_deals) + flush = true; + + if new_deals.is_empty() { + sector_deals.delete(sector_number).with_context_code( + ExitCode::USR_ILLEGAL_STATE, + || { + format!( + "failed to delete sector deals for {} {}", + provider, sector_number + ) + }, + )?; + } else { + sector_deals.set(sector_number, new_deals).with_context_code( + ExitCode::USR_ILLEGAL_STATE, + || { + format!( + "failed to set sector deals for {} {}", + provider, sector_number + ) + }, + )?; + } + } + } + if flush { + if sector_deals.is_empty() { + provider_sectors + .delete(provider) .with_context_code(ExitCode::USR_ILLEGAL_STATE, || { - format!("failed to set sector deals for {} {}", provider, sector_number) + format!("failed to delete sector deals for {}", provider) })?; + } else { + save_provider_sector_deals( + &mut provider_sectors, + *provider, + &mut sector_deals, + )?; } } - save_provider_sector_deals(&mut provider_sectors, *provider, &mut sector_deals)?; } self.save_provider_sectors(&mut provider_sectors)?; Ok(())