Skip to content

Commit

Permalink
feat(sp): Add batch event for slashed sectors
Browse files Browse the repository at this point in the history
  • Loading branch information
aidan46 committed Nov 26, 2024
1 parent 326213b commit 5e1a404
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
Binary file modified cli/artifacts/metadata.scale
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,17 @@ impl std::fmt::Display for Event {
"Sectors Proven: {{ owner: {}, sectors: {:?} }}",
owner, sectors,
)),
Event::SectorSlashed {
Event::SectorsSlashed {
owner,
sector_number,
sector_numbers,
} => f.write_fmt(format_args!(
"Sector Slashed: {{ owner: {}, sector_number: {} }}",
owner, sector_number,
owner,
itertools::Itertools::intersperse(
sector_numbers.iter().map(|sector| format!("{}", sector)),
", ".to_string()
)
.collect::<String>(),
)),
Event::ValidPoStSubmitted { owner } => {
f.write_fmt(format_args!("Valid PoSt Submitted: {{ owner: {} }}", owner,))
Expand Down
4 changes: 2 additions & 2 deletions docs/src/architecture/pallets/storage-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@ The Storage Provider Pallet emits the following events:
- `sector_number` - The sector number that is proven.
- `partition_number` - The partition number the proven sector is in.
- `deadline_idx` - The deadline index assigned to the proven sector.
- `SectorSlashed` - A previously pre-committed sector, but not proven, has been slashed by the system because it has expired.
- `SectorsSlashed` - Previously pre-committed sectors, but not proven, have been slashed by the system because it has expired.
- `owner` - SS58 address of the storage provider.
- `sector_number` - The sector number that has been slashed because of expiry.
- `sector_numbers` - The sector numbers that hav been slashed because of expiry.
- `ValidPoStSubmitted` - A valid PoSt has been submitted by a storage provider.
- `owner` - SS58 address of the storage provider.
- `FaultsDeclared` - A storage provider has declared some sectors as faulty.
Expand Down
19 changes: 11 additions & 8 deletions pallets/storage-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,10 @@ pub mod pallet {
sectors: BoundedVec<ProveCommitResult, ConstU32<MAX_SECTORS_PER_CALL>>,
},
/// Emitted when a sector was pre-committed, but not proven, so it got slashed in the pre-commit hook.
SectorSlashed {
SectorsSlashed {
owner: T::AccountId,
sector_number: SectorNumber,
// No need for a bounded collection as we produce the output ourselves.
sector_numbers: Vec<SectorNumber>,
},
/// Emitted when an SP submits a valid PoSt
ValidPoStSubmitted { owner: T::AccountId },
Expand Down Expand Up @@ -1104,18 +1105,16 @@ pub mod pallet {
return;
}

let mut removed_sectors = Vec::new();
log::info!(target: LOG_TARGET, "found {} expired pre committed sectors for {:?}", expired.len(), storage_provider);
for sector_number in expired {
// Expired sectors should be removed, because in other case they'd be processed twice in the next block.
let Ok(()) = state.remove_pre_committed_sector(sector_number) else {
if let Ok(()) = state.remove_pre_committed_sector(sector_number.clone()) {
removed_sectors.push(sector_number)
} else {
log::error!(target: LOG_TARGET, "catastrophe, failed to remove sector {} for {:?}", sector_number, storage_provider);
continue;
};

Self::deposit_event(Event::<T>::SectorSlashed {
sector_number,
owner: storage_provider.clone(),
});
}

let Some(slashed_deposits) = state.pre_commit_deposits.checked_sub(&slash_amount)
Expand All @@ -1132,6 +1131,10 @@ pub mod pallet {
};

StorageProviders::<T>::insert(&storage_provider, state);
Self::deposit_event(Event::<T>::SectorsSlashed {
owner: storage_provider,
sector_numbers: removed_sectors,
})
}
}

Expand Down
14 changes: 7 additions & 7 deletions pallets/storage-provider/src/tests/pre_commit_sector_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use crate::{
pallet::{Event, StorageProviders},
sector::ProveCommitSector,
tests::{
account, events, publish_deals, register_storage_provider, run_to_block, sector_set,
Balances, RuntimeEvent, RuntimeOrigin, SectorPreCommitInfoBuilder, StorageProvider, System,
Test, CHARLIE,
account, events, publish_deals, register_storage_provider, run_to_block, Balances,
RuntimeEvent, RuntimeOrigin, SectorPreCommitInfoBuilder, StorageProvider, System, Test,
CHARLIE,
},
};

Expand Down Expand Up @@ -89,10 +89,6 @@ fn pre_commit_hook_slashed_deal() {
BTreeSet::from([SectorNumber::new(2).unwrap()])
)]),
}),
RuntimeEvent::StorageProvider(Event::<Test>::SectorSlashed {
owner: account(storage_provider),
sector_number: 1.into(),
}),
// the slash -> withdraw is related to the usage of slash_and_burn
// when slashing the SP for a failed pre_commit
// this usage may need review for a proper economic balance
Expand All @@ -110,6 +106,10 @@ fn pre_commit_hook_slashed_deal() {
RuntimeEvent::Balances(pallet_balances::Event::<Test>::Rescinded {
amount: deal_precommit_deposit
}),
RuntimeEvent::StorageProvider(Event::<Test>::SectorsSlashed {
owner: account(storage_provider),
sector_numbers: vec![1.into()],
}),
]
);
});
Expand Down

0 comments on commit 5e1a404

Please sign in to comment.