Skip to content

Commit

Permalink
feat(sp): Add batch event for faulty partitions
Browse files Browse the repository at this point in the history
  • Loading branch information
aidan46 committed Nov 26, 2024
1 parent d40b1b8 commit 326213b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 33 deletions.
Binary file modified cli/artifacts/metadata.scale
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,22 @@ impl std::fmt::Display for Event {
)
.collect::<String>()
)),
Event::PartitionFaulty {
Event::PartitionsFaulty {
owner,
partition,
sectors,
faulty_partitions,
} => f.write_fmt(format_args!(
"Faulty Partition: {{ owner: {}, partition: {}, sectors: [{}] }}",
"Faulty Partitions: {{ owner: {}, faulty_partitions: [{}] }}",
owner,
partition,
itertools::Itertools::intersperse(
sectors.0.iter().map(|recovery| format!("{}", recovery)),
faulty_partitions.iter().map(|(partition, sectors)| format!(
"{{ partition: {}, sectors: {} }}",
partition,
itertools::Itertools::intersperse(
sectors.iter().map(|sector| format!("{}", sector)),
", ".to_string()
)
.collect::<String>()
)),
", ".to_string()
)
.collect::<String>()
Expand Down
5 changes: 2 additions & 3 deletions docs/src/architecture/pallets/storage-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,9 @@ The Storage Provider Pallet emits the following events:
- `deadline` - The deadline to which the recovered sectors are assigned.
- `partition` - Partition number within the deadline containing the recovered sectors.
- `sectors` - Sectors in the partition being declared as recovered.
- `PartitionFaulty` - It was detected that a storage provider has not submitted their PoSt on time and has marked some sectors as faulty.
- `PartitionsFaulty` - It was detected that a storage provider has not submitted their PoSt on time and has marked some partitions and sectors as faulty.
- `owner` - SS58 address of the storage provider.
- `partition` - Partition number for which the PoSt was missed.
- `sectors` - The sectors in the partition declared faulty by the system.
- `faulty_partitions` - A map with partition numbers and the sectors associated with the partition number that are faulty.
- `SectorsTerminated` - A storage provider has terminated some sectors.
- `owner` - SS58 address of the storage provider.
- `terminations` - An array with information about the terminated sectors. This information includes:
Expand Down
40 changes: 23 additions & 17 deletions pallets/storage-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ pub mod pallet {

extern crate alloc;

use alloc::{vec, vec::Vec};
use alloc::{
collections::{BTreeMap, BTreeSet},
vec,
vec::Vec,
};
use core::fmt::Debug;

use cid::Cid;
Expand Down Expand Up @@ -293,10 +297,10 @@ pub mod pallet {
recoveries: BoundedVec<RecoveryDeclaration, ConstU32<DECLARATIONS_MAX>>,
},
/// Emitted when an SP doesn't submit Windowed PoSt in time and PoSt hook marks partitions as faulty
PartitionFaulty {
PartitionsFaulty {
owner: T::AccountId,
partition: PartitionNumber,
sectors: BoundedBTreeSet<SectorNumber, ConstU32<MAX_SECTORS>>,
// No need to be bounded as we are creating the sets ourselves
faulty_partitions: BTreeMap<PartitionNumber, BTreeSet<SectorNumber>>,
},
/// Emitted when an SP terminates some sectors.
SectorsTerminated {
Expand Down Expand Up @@ -1231,9 +1235,9 @@ pub mod pallet {
}

log::info!(target: LOG_TARGET, "block: {:?}, checking storage provider {:?} deadline: {:?}",
current_block,
storage_provider,
current_deadline.idx,
current_block,
storage_provider,
current_deadline.idx,
);

let Ok(deadline) =
Expand All @@ -1244,7 +1248,10 @@ pub mod pallet {
continue;
};

let mut faulty_partitions = 0;
let mut faulty_partitions_amount = 0;
// Create collection for fault partitions, 1 event per SP
let mut faulty_partitions: BTreeMap<PartitionNumber, BTreeSet<SectorNumber>> =
BTreeMap::new();
for (partition_number, partition) in deadline.partitions.iter_mut() {
if partition.sectors.len() == 0 {
continue;
Expand Down Expand Up @@ -1278,24 +1285,23 @@ pub mod pallet {
current_block, storage_provider, partition_number, new_faults.len());

if new_faults.len() > 0 {
Self::deposit_event(Event::PartitionFaulty {
owner: storage_provider.clone(),
partition: *partition_number,
sectors: new_faults.try_into()
.expect("new_faults.len() <= MAX_SECTORS, cannot be more new faults than all of the sectors in partition"),
});
faulty_partitions += 1;
faulty_partitions.insert(*partition_number, new_faults);
faulty_partitions_amount += 1;
}
}

// TODO(@th7nder,[#106,#187],08/08/2024): figure out slashing amounts (for continued faults, new faults).
if faulty_partitions > 0 {
if faulty_partitions_amount > 0 {
log::warn!(target: LOG_TARGET, "block: {:?}, sp: {:?}, deadline: {:?} - should have slashed {} partitions...",
current_block,
storage_provider,
current_deadline.idx,
faulty_partitions,
faulty_partitions_amount,
);
Self::deposit_event(Event::PartitionsFaulty {
owner: storage_provider.clone(),
faulty_partitions,
})
} else {
log::info!(target: LOG_TARGET, "block: {:?}, sp: {:?}, deadline: {:?} - all proofs submitted on time.",
current_block,
Expand Down
12 changes: 8 additions & 4 deletions pallets/storage-provider/src/tests/post_hook.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extern crate alloc;

use alloc::collections::{BTreeMap, BTreeSet};

use frame_support::{assert_ok, pallet_prelude::Get};
use primitives_proofs::{DealId, SectorNumber};
use sp_core::bounded_vec;
Expand Down Expand Up @@ -68,16 +70,18 @@ fn marks_partitions_as_faulty() {
let partition = &deadline.partitions[&0];
let expected_sectors =
sector_set::<MAX_SECTORS>(&[first_sector_number, second_sector_number]);

let faulty_sectors = BTreeSet::from([
SectorNumber::new(first_sector_number).unwrap(),
SectorNumber::new(second_sector_number).unwrap(),
]);
assert_eq!(partition.faults.len(), 2);
assert_eq!(expected_sectors, partition.faults);
assert_eq!(
events(),
[RuntimeEvent::StorageProvider(
Event::<Test>::PartitionFaulty {
Event::<Test>::PartitionsFaulty {
owner: account(storage_provider),
partition: 0,
sectors: expected_sectors.clone()
faulty_partitions: BTreeMap::from([(0u32, faulty_sectors)]),
}
),]
);
Expand Down
11 changes: 8 additions & 3 deletions pallets/storage-provider/src/tests/pre_commit_sector_hook.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
extern crate alloc;

use alloc::collections::{BTreeMap, BTreeSet};

use primitives_proofs::SectorNumber;
use sp_core::bounded_vec;

use super::new_test_ext;
Expand Down Expand Up @@ -79,10 +82,12 @@ fn pre_commit_hook_slashed_deal() {
assert_eq!(
events(),
[
RuntimeEvent::StorageProvider(Event::<Test>::PartitionFaulty {
RuntimeEvent::StorageProvider(Event::<Test>::PartitionsFaulty {
owner: account(storage_provider),
partition: 0,
sectors: sector_set(&[2])
faulty_partitions: BTreeMap::from([(
0,
BTreeSet::from([SectorNumber::new(2).unwrap()])
)]),
}),
RuntimeEvent::StorageProvider(Event::<Test>::SectorSlashed {
owner: account(storage_provider),
Expand Down

0 comments on commit 326213b

Please sign in to comment.