It is indeed a bit too much to react to all of it. Maybe we just collect ideas in here and then create dedicated discussions for each part.
I like this idea. Imagine writing the invariants actually as code and not as comment:
#[pallet::storage]
pub type Map = StorageMap<u32, u32>;
#[pallet::storage]
#[pallet::invariant(
event: changed(_previous, current) {
assert!(current == Map::iter().count());
},
check_on: StorageLayerCommit,
// Could think of when to check these conditions; per Extrinsic, per Block, Always…
)]
pub type Counter = StorageValue<u32>;