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

fix(pallet-randomness): bring back the getter API #646

Merged
merged 5 commits into from
Dec 19, 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ coverage/
*.post.params
*.post.vk
*.post.vk.scale

# File logs from the SP
logs/
13 changes: 13 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ tokio-util = "0.7.11"
tower = "0.4.13"
tower-http = "0.5.2"
tracing = "0.1.40"
tracing-appender = "0.2.3"
tracing-subscriber = "0.3.18"
url = "2.5.0"
uuid = "1.8.0"
Expand Down
23 changes: 19 additions & 4 deletions pallets/market/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use frame_support::{
traits::{OnFinalize, OnInitialize},
PalletId,
};
use frame_system::{self as system, pallet_prelude::BlockNumberFor};
use frame_system::pallet_prelude::BlockNumberFor;
use primitives::proofs::RegisteredPoStProof;
use sp_core::Pair;
use sp_runtime::{
traits::{ConstU32, ConstU64, IdentifyAccount, IdentityLookup, Verify},
traits::{ConstU32, ConstU64, IdentifyAccount, IdentityLookup, Verify, Zero},
AccountId32, BuildStorage, MultiSignature, MultiSigner,
};

Expand Down Expand Up @@ -37,7 +37,7 @@ pub type AccountPublic = <Signature as Verify>::Signer;
pub type AccountId = <AccountPublic as IdentifyAccount>::AccountId;

#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl system::Config for Test {
impl frame_system::Config for Test {
type Block = Block;
type AccountData = pallet_balances::AccountData<u64>;
type AccountId = AccountId;
Expand Down Expand Up @@ -101,9 +101,24 @@ where
}
}

impl<C> primitives::randomness::AuthorVrfHistory<BlockNumberFor<C>, C::Hash>
for DummyRandomnessGenerator<C>
where
C: frame_system::Config,
{
fn author_vrf_history(block_number: BlockNumberFor<C>) -> Option<C::Hash> {
if block_number == <BlockNumberFor<C> as Zero>::zero() {
None
} else {
Some(Default::default())
}
}
}

impl pallet_storage_provider::Config for Test {
type RuntimeEvent = RuntimeEvent;
type Randomness = DummyRandomnessGenerator<Self>;
type AuthorVrfHistory = DummyRandomnessGenerator<Self>;
type PeerId = BoundedVec<u8, ConstU32<32>>; // Max length of SHA256 hash
type Currency = Balances;
type Market = Market;
Expand Down Expand Up @@ -172,7 +187,7 @@ pub const INITIAL_FUNDS: u64 = 1000;
/// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
let _ = env_logger::try_init();
let mut t = system::GenesisConfig::<Test>::default()
let mut t = frame_system::GenesisConfig::<Test>::default()
.build_storage()
.unwrap()
.into();
Expand Down
8 changes: 8 additions & 0 deletions pallets/randomness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,12 @@ pub mod pallet {
(randomness, block_number)
}
}

impl<T: Config> primitives::randomness::AuthorVrfHistory<BlockNumberFor<T>, T::Hash> for Pallet<T> {
fn author_vrf_history(block_number: BlockNumberFor<T>) -> Option<T::Hash> {
// We only query the history to keep consistency with the original implementation,
// ideally, we would implement proper hashing along with a subject.
AuthorVrfHistory::<T>::get(block_number)
}
}
}
22 changes: 16 additions & 6 deletions pallets/storage-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub mod pallet {
extern crate alloc;

use alloc::{collections::BTreeMap, vec, vec::Vec};
use core::{convert::AsRef, fmt::Debug};
use core::fmt::Debug;

use cid::Cid;
use codec::{Decode, Encode};
Expand All @@ -62,7 +62,7 @@ pub mod pallet {
StorageProviderValidation,
},
proofs::{derive_prover_id, PublicReplicaInfo, RegisteredPoStProof},
randomness::{draw_randomness, DomainSeparationTag},
randomness::{draw_randomness, AuthorVrfHistory, DomainSeparationTag},
sector::SectorNumber,
PartitionNumber, MAX_PARTITIONS_PER_DEADLINE, MAX_SEAL_PROOF_BYTES, MAX_SECTORS,
MAX_SECTORS_PER_CALL,
Expand Down Expand Up @@ -119,6 +119,9 @@ pub mod pallet {
/// Proof verification trait implementation for verifying proofs
type ProofVerification: ProofVerification;

/// Trait for AuthorVRF querying.
type AuthorVrfHistory: AuthorVrfHistory<BlockNumberFor<Self>, Self::Hash>;

/// Window PoSt proving period — equivalent to 24 hours worth of blocks.
///
/// During the proving period, storage providers submit Spacetime proofs over smaller
Expand Down Expand Up @@ -369,6 +372,8 @@ pub mod pallet {
CannotTerminateImmutableDeadline,
/// Emitted when trying to submit PoSt with partitions containing too many sectors (>2349).
TooManyReplicas,
/// AuthorVRF lookup failed.
MissingAuthorVRF,
/// Inner pallet errors
GeneralPalletError(crate::error::GeneralPalletError),
}
Expand Down Expand Up @@ -1691,7 +1696,9 @@ pub mod pallet {
entropy: &[u8],
) -> Result<[u8; 32], DispatchError> {
// Get randomness from chain
let (digest, _) = T::Randomness::random(&personalization.as_bytes());
let Some(randomness) = T::AuthorVrfHistory::author_vrf_history(block_number) else {
return Err(Error::<T>::MissingAuthorVRF.into());
};

// Converting block_height to the type accepted by draw_randomness
let block_number = block_number
Expand All @@ -1701,10 +1708,13 @@ pub mod pallet {
Error::<T>::ConversionError
})?;

let mut sized_digest = [0; 32];
sized_digest.copy_from_slice(&digest.as_ref());
// HACK: convert an unsized slice to a sized one
// SAFETY: we know that the output is 32 bytes since we control the chain config
let mut sized_randomness = [0; 32];
sized_randomness.copy_from_slice(randomness.as_ref());

// Randomness with the bias
let randomness = draw_randomness(&sized_digest, personalization, block_number, entropy);
let randomness = draw_randomness(&sized_randomness, personalization, block_number, entropy);

Ok(randomness)
}
Expand Down
52 changes: 34 additions & 18 deletions pallets/storage-provider/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use primitives::{
DealId, PartitionNumber, CID_SIZE_IN_BYTES, MAX_DEALS_PER_SECTOR, MAX_PARTITIONS_PER_DEADLINE,
MAX_POST_PROOF_BYTES, MAX_SEAL_PROOF_BYTES, MAX_SECTORS_PER_PROOF, MAX_TERMINATIONS_PER_CALL,
};
use sp_arithmetic::traits::Zero;
use sp_core::{bounded_vec, Pair};
use sp_runtime::{
traits::{IdentifyAccount, IdentityLookup, Verify},
Expand Down Expand Up @@ -117,24 +118,6 @@ impl ProofVerification for DummyProofsVerification {
}
}

/// Randomness generator used by tests.
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_market::Config for Test {
type RuntimeEvent = RuntimeEvent;
type PalletId = MarketPalletId;
Expand Down Expand Up @@ -173,9 +156,42 @@ parameter_types! {
pub const MaxDealDuration: u64 = 30 * MINUTES;
}

/// Randomness generator used by tests.
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<C> primitives::randomness::AuthorVrfHistory<BlockNumberFor<C>, C::Hash>
for DummyRandomnessGenerator<C>
where
C: frame_system::Config,
{
fn author_vrf_history(block_number: BlockNumberFor<C>) -> Option<C::Hash> {
if block_number == <BlockNumberFor<C> as Zero>::zero() {
None
} else {
Some(Default::default())
}
}
}

impl pallet_storage_provider::Config for Test {
type RuntimeEvent = RuntimeEvent;
type Randomness = DummyRandomnessGenerator<Self>;
type AuthorVrfHistory = DummyRandomnessGenerator<Self>;
type PeerId = BoundedVec<u8, ConstU32<32>>; // Max length of SHA256 hash
type Currency = Balances;
type Market = Market;
Expand Down
8 changes: 8 additions & 0 deletions primitives/src/randomness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ use alloc::vec::Vec;

use sp_core::blake2_256;

pub trait AuthorVrfHistory<BlockNumber, Hash> {
/// Query the AuthorVRF history for a given block.
///
/// Returns `None` if the block isn't present,
/// this can happen because the block is in the future or past the lookback window.
fn author_vrf_history(block_number: BlockNumber) -> Option<Hash>;
}

/// Specifies a domain for randomness generation.
#[derive(PartialEq, Eq, Copy, Clone, Debug, Hash)]
pub enum DomainSeparationTag {
Expand Down
1 change: 1 addition & 0 deletions runtime/src/configs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ parameter_types! {
impl pallet_storage_provider::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Randomness = crate::Randomness;
type AuthorVrfHistory = crate::Randomness;
type PeerId = BoundedVec<u8, ConstU32<32>>; // Max length of SHA256 hash
type Currency = Balances;
type Market = crate::Market;
Expand Down
1 change: 1 addition & 0 deletions storage-provider/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ tokio-util = { workspace = true, features = ["rt"] }
tower = { workspace = true }
tower-http = { workspace = true, features = ["trace"] }
tracing = { workspace = true }
tracing-appender = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
url = { workspace = true }
uuid = { workspace = true, features = ["v4"] }
Expand Down
4 changes: 4 additions & 0 deletions storage-provider/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,12 @@ struct SetupOutput {
}
fn main() -> Result<(), ServerError> {
// Logger initialization.
let file_appender = tracing_appender::rolling::daily("logs", "sp_server");
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);

tracing_subscriber::registry()
.with(fmt::layer())
.with(fmt::layer().with_writer(non_blocking).with_ansi(false))
.with(
EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
Expand Down
Loading