Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(randomness): use BABE's AuthorVRF as a randomness source #633

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,6 @@ frame-try-runtime = { git = "https://github.com/paritytech/polkadot-sdk", tag =
pallet-aura = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2409-2", default-features = false }
pallet-authorship = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2409-2", default-features = false }
pallet-balances = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2409-2", default-features = false }
# TODO(#458,@cernicc,17/10/2024): Use secure randomness
pallet-insecure-randomness-collective-flip = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2409-2", default-features = false }
pallet-message-queue = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2409-2", default-features = false }
pallet-session = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2409-2", default-features = false }
pallet-sudo = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-stable2409-2", default-features = false }
Expand Down
10 changes: 0 additions & 10 deletions docs/src/architecture/pallets/randomness.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,3 @@ The pallet does not emit any events.
The Randomness Pallet actions can fail with the following errors:

- `SeedNotAvailable` - the seed for the given block number is not available, which means the randomness pallet has not gathered randomness for this block yet.

## Constants

The Randomness Pallet has the following constants:

| Name | Description | Value |
| ----------------- | ----------------------------------------------------------------- | ------- |
| `CleanupInterval` | Clean-up interval specified in number of blocks between cleanups. | 1 Day |
| `SeedAgeLimit` | The number of blocks after which the seed is cleaned up. | 30 Days |

22 changes: 16 additions & 6 deletions pallets/market/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use frame_support::{
PalletId,
};
use frame_system::{self as system, pallet_prelude::BlockNumberFor};
use primitives::{pallets::Randomness, proofs::RegisteredPoStProof};
use primitives::proofs::RegisteredPoStProof;
use sp_core::Pair;
use sp_runtime::{
traits::{ConstU32, ConstU64, IdentifyAccount, IdentityLookup, Verify},
Expand Down Expand Up @@ -84,16 +84,26 @@ impl crate::Config for Test {
}

/// Randomness generator used by tests.
pub struct DummyRandomnessGenerator;
impl<BlockNumber> Randomness<BlockNumber> for DummyRandomnessGenerator {
fn get_randomness(_: BlockNumber) -> Result<[u8; 32], sp_runtime::DispatchError> {
Ok([0; 32])
pub struct DummyRandomnessGenerator<C>(core::marker::PhantomData<C>)
where
C: frame_system::Config;

impl<C> frame_support::traits::Randomness<C::Hash, BlockNumberFor<C>>
for DummyRandomnessGenerator<C>
where
C: frame_system::Config,
{
fn random(_subject: &[u8]) -> (C::Hash, BlockNumberFor<C>) {
(
Default::default(),
<frame_system::Pallet<C>>::block_number(),
)
}
}

impl pallet_storage_provider::Config for Test {
type RuntimeEvent = RuntimeEvent;
type Randomness = DummyRandomnessGenerator;
type Randomness = DummyRandomnessGenerator<Self>;
type PeerId = BoundedVec<u8, ConstU32<32>>; // Max length of SHA256 hash
type Currency = Balances;
type Market = Market;
Expand Down
18 changes: 15 additions & 3 deletions pallets/randomness/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { features = ["derive"], workspace = true }
frame-benchmarking = { optional = true, workspace = true }
frame-support.workspace = true
frame-system.workspace = true
log = { workspace = true }
pallet-insecure-randomness-collective-flip = { workspace = true, default-features = false }
primitives = { workspace = true }
scale-info = { features = ["derive"], workspace = true }
sp-core = { workspace = true, default-features = false }
sp-inherents.workspace = true
sp-runtime.workspace = true

# Optional
async-trait = { workspace = true, optional = true }
frame-benchmarking = { workspace = true, optional = true }

[dev-dependencies]
sp-io = { default-features = true, workspace = true }

Expand All @@ -38,5 +41,14 @@ runtime-benchmarks = [
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
std = ["codec/std", "frame-benchmarking?/std", "frame-support/std", "frame-system/std", "primitives/std", "scale-info/std", "sp-runtime/std"]
std = [
"async-trait",
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"primitives/std",
"scale-info/std",
"sp-runtime/std",
]
try-runtime = ["frame-support/try-runtime", "frame-system/try-runtime", "sp-runtime/try-runtime"]
53 changes: 53 additions & 0 deletions pallets/randomness/src/inherent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use codec::Encode;
use sp_inherents::{InherentIdentifier, IsFatalError};
use sp_runtime::RuntimeString;

/// BABE VRF Inherent Identifier
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"babe_vrf";

#[derive(Encode)]
#[cfg_attr(feature = "std", derive(Debug, codec::Decode))]
pub enum InherentError {
Other(RuntimeString),
}

impl IsFatalError for InherentError {
fn is_fatal_error(&self) -> bool {
true
}
}

impl InherentError {
/// Try to create an instance ouf of the given identifier and data.
#[cfg(feature = "std")]
pub fn try_from(id: &InherentIdentifier, data: &[u8]) -> Option<Self> {
if id == &INHERENT_IDENTIFIER {
<InherentError as codec::Decode>::decode(&mut &*data).ok()
} else {
None
}
}
}

#[cfg(feature = "std")]
pub struct InherentDataProvider;

#[cfg(feature = "std")]
#[async_trait::async_trait]
impl sp_inherents::InherentDataProvider for InherentDataProvider {
async fn provide_inherent_data(
&self,
inherent_data: &mut sp_inherents::InherentData,
) -> Result<(), sp_inherents::Error> {
inherent_data.put_data(INHERENT_IDENTIFIER, &())
}

async fn try_handle_error(
&self,
_identifier: &InherentIdentifier,
_error: &[u8],
) -> Option<Result<(), sp_inherents::Error>> {
// Most substrate inherents return None
None
}
}
Loading
Loading