Skip to content

Commit

Permalink
Merge pull request #1805 from subspace/rsub/pot-random
Browse files Browse the repository at this point in the history
Derive global randomness from PoT
  • Loading branch information
nazar-pc authored Aug 15, 2023
2 parents 34bf29d + 494029a commit 9ffd8de
Show file tree
Hide file tree
Showing 15 changed files with 502 additions and 194 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 30 additions & 15 deletions crates/pallet-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ mod pallet {

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;

/// If true, use the proof of time in the pre digest as source of global
/// randomness.
#[pallet::constant]
type IsPotEnabled: Get<bool>;
}

#[derive(Debug, Default, Encode, Decode, TypeInfo)]
Expand Down Expand Up @@ -835,11 +840,13 @@ impl<T: Config> Pallet<T> {
>::default());

// If global randomness was updated in previous block, set it as current.
if let Some(next_randomness) = GlobalRandomnesses::<T>::get().next {
GlobalRandomnesses::<T>::put(sp_consensus_subspace::GlobalRandomnesses {
current: next_randomness,
next: None,
});
if !T::IsPotEnabled::get() {
if let Some(next_randomness) = GlobalRandomnesses::<T>::get().next {
GlobalRandomnesses::<T>::put(sp_consensus_subspace::GlobalRandomnesses {
current: next_randomness,
next: None,
});
}
}

// If solution range was updated in previous block, set it as current.
Expand All @@ -863,24 +870,30 @@ impl<T: Config> Pallet<T> {
PorRandomness::<T>::put(por_randomness);

// Deposit global randomness data such that light client can validate blocks later.
frame_system::Pallet::<T>::deposit_log(DigestItem::global_randomness(
GlobalRandomnesses::<T>::get().current,
));
if !T::IsPotEnabled::get() {
frame_system::Pallet::<T>::deposit_log(DigestItem::global_randomness(
GlobalRandomnesses::<T>::get().current,
));
}
// Deposit solution range data such that light client can validate blocks later.
frame_system::Pallet::<T>::deposit_log(DigestItem::solution_range(
SolutionRanges::<T>::get().current,
));

// Enact global randomness update, if necessary.
T::GlobalRandomnessIntervalTrigger::trigger::<T>(block_number, por_randomness);
if !T::IsPotEnabled::get() {
// Enact global randomness update, if necessary.
T::GlobalRandomnessIntervalTrigger::trigger::<T>(block_number, por_randomness);
}
// Enact era change, if necessary.
T::EraChangeTrigger::trigger::<T>(block_number);

if let Some(next_global_randomness) = GlobalRandomnesses::<T>::get().next {
// Deposit next global randomness data such that light client can validate blocks later.
frame_system::Pallet::<T>::deposit_log(DigestItem::next_global_randomness(
next_global_randomness,
));
if !T::IsPotEnabled::get() {
if let Some(next_global_randomness) = GlobalRandomnesses::<T>::get().next {
// Deposit next global randomness data such that light client can validate blocks later.
frame_system::Pallet::<T>::deposit_log(DigestItem::next_global_randomness(
next_global_randomness,
));
}
}
}

Expand Down Expand Up @@ -1064,6 +1077,7 @@ impl<T: Config> Pallet<T> {
T::RecentHistoryFraction::get().1,
),
min_sector_lifetime: T::MinSectorLifetime::get(),
is_pot_enabled: T::IsPotEnabled::get(),
}
}
}
Expand Down Expand Up @@ -1173,6 +1187,7 @@ impl<T: Config> Pallet<T> {
/// initialization has already happened) or from `validate_unsigned` by transaction pool (meaning
/// block initialization didn't happen yet).
fn current_vote_verification_data<T: Config>(is_block_initialized: bool) -> VoteVerificationData {
// TODO: factor PoT in the vote process.
let global_randomnesses = GlobalRandomnesses::<T>::get();
let solution_ranges = SolutionRanges::<T>::get();
VoteVerificationData {
Expand Down
2 changes: 2 additions & 0 deletions crates/pallet-subspace/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ parameter_types! {
pub const ReplicationFactor: u16 = 1;
pub const ReportLongevity: u64 = 34;
pub const ShouldAdjustSolutionRange: bool = false;
pub const IsPotEnabled: bool = false;
}

impl Config for Test {
Expand All @@ -193,6 +194,7 @@ impl Config for Test {
type HandleEquivocation = EquivocationHandler<OffencesSubspace, ReportLongevity>;

type WeightInfo = ();
type IsPotEnabled = IsPotEnabled;
}

pub fn go_to_block(
Expand Down
Loading

0 comments on commit 9ffd8de

Please sign in to comment.