From 4bdb1d18219333c31e93929ba11b26154c2cd0df Mon Sep 17 00:00:00 2001 From: Gonza Montiel Date: Mon, 8 Sep 2025 21:02:27 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=94=A7=20=20align=20BabeDataGetter?= =?UTF-8?q?=20implementation=20for=20all=20runtimes=20(#152)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Based on [this comment](https://github.com/datahaven-xyz/datahaven/pull/148#pullrequestreview-3195433668), I took the SH [implementation](https://github.com/Moonsong-Labs/storage-hub/blob/3e5f774c64a944fc4d4754c239a7af3aad5f5ec5/runtime/solochain-evm/src/configs/mod.rs#L1124) of the `BabeDataGetter` and aligned all three runtimes with this implementation. --- .../mainnet/src/configs/storagehub/mod.rs | 18 ++++++++++++------ .../stagenet/src/configs/storagehub/mod.rs | 16 ++++++++++++---- .../testnet/src/configs/storagehub/mod.rs | 16 ++++++++++++---- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/operator/runtime/mainnet/src/configs/storagehub/mod.rs b/operator/runtime/mainnet/src/configs/storagehub/mod.rs index 9389d2c5..c683983d 100644 --- a/operator/runtime/mainnet/src/configs/storagehub/mod.rs +++ b/operator/runtime/mainnet/src/configs/storagehub/mod.rs @@ -12,7 +12,7 @@ use frame_support::pallet_prelude::DispatchClass; use frame_support::traits::AsEnsureOriginWithArg; use frame_support::{ parameter_types, - traits::{ConstU128, ConstU32, ConstU64}, + traits::{ConstU128, ConstU32, ConstU64, Randomness}, weights::Weight, }; use frame_system::pallet_prelude::BlockNumberFor; @@ -109,15 +109,21 @@ impl pallet_randomness::Config for Runtime { pub struct BabeDataGetter; impl pallet_randomness::GetBabeData for BabeDataGetter { fn get_epoch_index() -> u64 { - pallet_babe::Pallet::::current_epoch().epoch_index + pallet_babe::Pallet::::epoch_index() } fn get_epoch_randomness() -> Hash { - sp_core::H256::from(pallet_babe::Pallet::::current_epoch().randomness) + // We use `RandomnessFromOneEpochAgo` implementation of the `Randomness` trait here, which hashes the `NextRandomness` + // stored by the BABE pallet, and is valid for commitments until the last block of the last epoch (`_n`). The hashed + // received is the hash of `NextRandomness` concatenated with the `subject` parameter provided (in this case empty). + let (h, _n) = pallet_babe::RandomnessFromOneEpochAgo::::random(b""); + h } fn get_parent_randomness() -> Hash { - sp_core::H256::from( - pallet_babe::Pallet::::author_vrf_randomness().unwrap_or_default(), - ) + // We use `ParentBlockRandomness` implementation of the `Randomness` trait here, which hashes the `AuthorVrfRandomness` + // stored by the BABE pallet, and is valid for commitments until the parent block (`_n`). The hashed received is the + // hash of `AuthorVrfRandomness` concatenated with the `subject` parameter provided (in this case empty). + let (h_opt, _n) = pallet_babe::ParentBlockRandomness::::random(b""); + h_opt.unwrap_or_default() } } diff --git a/operator/runtime/stagenet/src/configs/storagehub/mod.rs b/operator/runtime/stagenet/src/configs/storagehub/mod.rs index 312040bc..c683983d 100644 --- a/operator/runtime/stagenet/src/configs/storagehub/mod.rs +++ b/operator/runtime/stagenet/src/configs/storagehub/mod.rs @@ -12,7 +12,7 @@ use frame_support::pallet_prelude::DispatchClass; use frame_support::traits::AsEnsureOriginWithArg; use frame_support::{ parameter_types, - traits::{ConstU128, ConstU32, ConstU64}, + traits::{ConstU128, ConstU32, ConstU64, Randomness}, weights::Weight, }; use frame_system::pallet_prelude::BlockNumberFor; @@ -109,13 +109,21 @@ impl pallet_randomness::Config for Runtime { pub struct BabeDataGetter; impl pallet_randomness::GetBabeData for BabeDataGetter { fn get_epoch_index() -> u64 { - pallet_babe::Pallet::::current_epoch().epoch_index + pallet_babe::Pallet::::epoch_index() } fn get_epoch_randomness() -> Hash { - sp_core::H256::from(pallet_babe::Pallet::::current_epoch().randomness) + // We use `RandomnessFromOneEpochAgo` implementation of the `Randomness` trait here, which hashes the `NextRandomness` + // stored by the BABE pallet, and is valid for commitments until the last block of the last epoch (`_n`). The hashed + // received is the hash of `NextRandomness` concatenated with the `subject` parameter provided (in this case empty). + let (h, _n) = pallet_babe::RandomnessFromOneEpochAgo::::random(b""); + h } fn get_parent_randomness() -> Hash { - sp_core::H256::from(pallet_babe::Pallet::::current_epoch().randomness) + // We use `ParentBlockRandomness` implementation of the `Randomness` trait here, which hashes the `AuthorVrfRandomness` + // stored by the BABE pallet, and is valid for commitments until the parent block (`_n`). The hashed received is the + // hash of `AuthorVrfRandomness` concatenated with the `subject` parameter provided (in this case empty). + let (h_opt, _n) = pallet_babe::ParentBlockRandomness::::random(b""); + h_opt.unwrap_or_default() } } diff --git a/operator/runtime/testnet/src/configs/storagehub/mod.rs b/operator/runtime/testnet/src/configs/storagehub/mod.rs index 312040bc..c683983d 100644 --- a/operator/runtime/testnet/src/configs/storagehub/mod.rs +++ b/operator/runtime/testnet/src/configs/storagehub/mod.rs @@ -12,7 +12,7 @@ use frame_support::pallet_prelude::DispatchClass; use frame_support::traits::AsEnsureOriginWithArg; use frame_support::{ parameter_types, - traits::{ConstU128, ConstU32, ConstU64}, + traits::{ConstU128, ConstU32, ConstU64, Randomness}, weights::Weight, }; use frame_system::pallet_prelude::BlockNumberFor; @@ -109,13 +109,21 @@ impl pallet_randomness::Config for Runtime { pub struct BabeDataGetter; impl pallet_randomness::GetBabeData for BabeDataGetter { fn get_epoch_index() -> u64 { - pallet_babe::Pallet::::current_epoch().epoch_index + pallet_babe::Pallet::::epoch_index() } fn get_epoch_randomness() -> Hash { - sp_core::H256::from(pallet_babe::Pallet::::current_epoch().randomness) + // We use `RandomnessFromOneEpochAgo` implementation of the `Randomness` trait here, which hashes the `NextRandomness` + // stored by the BABE pallet, and is valid for commitments until the last block of the last epoch (`_n`). The hashed + // received is the hash of `NextRandomness` concatenated with the `subject` parameter provided (in this case empty). + let (h, _n) = pallet_babe::RandomnessFromOneEpochAgo::::random(b""); + h } fn get_parent_randomness() -> Hash { - sp_core::H256::from(pallet_babe::Pallet::::current_epoch().randomness) + // We use `ParentBlockRandomness` implementation of the `Randomness` trait here, which hashes the `AuthorVrfRandomness` + // stored by the BABE pallet, and is valid for commitments until the parent block (`_n`). The hashed received is the + // hash of `AuthorVrfRandomness` concatenated with the `subject` parameter provided (in this case empty). + let (h_opt, _n) = pallet_babe::ParentBlockRandomness::::random(b""); + h_opt.unwrap_or_default() } }