Skip to content

Commit

Permalink
change: Append block producer to log on block finalization
Browse files Browse the repository at this point in the history
  • Loading branch information
AmbientTea committed Feb 25, 2025
1 parent b299346 commit a209b5f
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion toolkit/pallets/block-production-log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pub mod pallet {
#[pallet::unbounded]
pub type Log<T: Config> = StorageValue<_, Vec<(Slot, T::BlockProducerId)>, ValueQuery>;

#[pallet::storage]
pub type CurrentProducer<T: Config> = StorageValue<_, T::BlockProducerId, OptionQuery>;

/// This storage is used to prevent calling `append` multiple times for the same block or for past blocks.
#[pallet::storage]
pub type LatestBlock<T: Config> = StorageValue<_, BlockNumberFor<T>, OptionQuery>;
Expand Down Expand Up @@ -86,7 +89,19 @@ pub mod pallet {
}?;
LatestBlock::<T>::put(current_block);

Ok(Log::<T>::append((T::current_slot(), block_producer_id)))
Ok(CurrentProducer::<T>::put(block_producer_id))
}
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_finalize(block: BlockNumberFor<T>) {
let block_producer_id =
CurrentProducer::<T>::take().expect("Author is set before on_finalize; qed");

log::info!("👷 Block {block:?} producer is {block_producer_id:?}");

Log::<T>::append((T::current_slot(), block_producer_id))
}
}

Expand All @@ -96,5 +111,15 @@ pub mod pallet {
Log::<T>::put(to_retain);
to_return
}

pub fn peek_prefix<'a>(slot: Slot) -> impl Iterator<Item = (Slot, T::BlockProducerId)> {
Log::<T>::get().into_iter().filter(move |(s, _)| s <= &slot)
}

pub fn drop_prefix<'a>(slot: Slot) {
let entries_left: Vec<_> =
Log::<T>::get().into_iter().filter(move |(s, _)| s > &slot).collect();
Log::<T>::put(entries_left);
}
}
}

0 comments on commit a209b5f

Please sign in to comment.