Skip to content

Commit

Permalink
Introduce notion of iterations for proof of time into `pallet-subspac…
Browse files Browse the repository at this point in the history
…e` and tune it slightly lower for local/devnet for better dev experience
  • Loading branch information
nazar-pc committed Aug 28, 2023
1 parent a491db2 commit ddf6b21
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 45 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ sha2 = { opt-level = 3 }
sha3 = { opt-level = 3 }
smallvec = { opt-level = 3 }
snow = { opt-level = 3 }
sc-proof-of-time = { opt-level = 3 }
subspace-archiving = { opt-level = 3 }
subspace-chiapos = { opt-level = 3 }
subspace-core-primitives = { opt-level = 3 }
subspace-erasure-coding = { opt-level = 3 }
subspace-farmer-components = { opt-level = 3 }
subspace-proof-of-space = { opt-level = 3 }
subspace-proof-of-time = { opt-level = 3 }
twox-hash = { opt-level = 3 }
uint = { opt-level = 3 }
x25519-dalek = { opt-level = 3 }
Expand Down
37 changes: 32 additions & 5 deletions crates/pallet-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ use sp_runtime::transaction_validity::{
};
use sp_runtime::DispatchError;
use sp_std::collections::btree_map::BTreeMap;
use sp_std::num::NonZeroU32;
use sp_std::prelude::*;
use subspace_core_primitives::crypto::Scalar;
#[cfg(feature = "pot")]
Expand Down Expand Up @@ -148,6 +149,7 @@ mod pallet {
use sp_consensus_subspace::{EquivocationProof, FarmerPublicKey, FarmerSignature, SignedVote};
use sp_runtime::DigestItem;
use sp_std::collections::btree_map::BTreeMap;
use sp_std::num::NonZeroU32;
use sp_std::prelude::*;
use subspace_core_primitives::crypto::Scalar;
use subspace_core_primitives::{
Expand Down Expand Up @@ -299,16 +301,16 @@ mod pallet {
pub enable_storage_access: bool,
/// Who can author blocks at genesis.
pub allow_authoring_by: AllowAuthoringBy,
/// Number of iterations for proof of time per slot
pub pot_slot_iterations: NonZeroU32,
}

impl Default for GenesisConfig {
#[inline]
fn default() -> Self {
Self {
enable_rewards: true,
enable_storage_access: true,
allow_authoring_by: AllowAuthoringBy::Anyone,
}
// TODO: Remove once https://github.com/paritytech/polkadot-sdk/pull/1221 is in our
// fork
unreachable!("Config must be initialized explicitly");
}
}

Expand All @@ -331,6 +333,7 @@ mod pallet {
RootPlotPublicKey::<T>::put(root_farmer.clone());
}
}
PotSlotIterations::<T>::put(self.pot_slot_iterations.get());
}
}

Expand Down Expand Up @@ -376,6 +379,22 @@ mod pallet {
pub(super) type GlobalRandomnesses<T> =
StorageValue<_, sp_consensus_subspace::GlobalRandomnesses, ValueQuery>;

pub(super) struct DefaultPotSlotIterations {}

// TODO: Replace with `NonZeroU32` once we can use it:
// https://github.com/paritytech/parity-scale-codec/pull/505
impl Get<u32> for DefaultPotSlotIterations {
fn get() -> u32 {
unreachable!("Always instantiated during genesis; qed");
}
}

/// Number of iterations for proof of time per slot
#[pallet::storage]
// #[pallet::getter(fn pot_slot_iterations)]
pub(super) type PotSlotIterations<T> =
StorageValue<_, u32, ValueQuery, DefaultPotSlotIterations>;

/// Solution ranges used for challenges.
#[pallet::storage]
#[pallet::getter(fn solution_ranges)]
Expand Down Expand Up @@ -1064,6 +1083,14 @@ impl<T: Config> Pallet<T> {
Some(())
}

/// Number of iterations for proof of time per slot
// TODO: Remove once we can use `NonZeroU32` directly:
// https://github.com/paritytech/parity-scale-codec/pull/505
pub fn pot_slot_iterations() -> NonZeroU32 {
NonZeroU32::new(PotSlotIterations::<T>::get())
.expect("Always initialized to non-zero value; qed")
}

/// Check if `farmer_public_key` is in block list (due to equivocation)
pub fn is_in_block_list(farmer_public_key: &FarmerPublicKey) -> bool {
BlockList::<T>::contains_key(farmer_public_key)
Expand Down
13 changes: 9 additions & 4 deletions crates/pallet-subspace/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

use crate::equivocation::EquivocationHandler;
use crate::{
self as pallet_subspace, Config, CurrentSlot, FarmerPublicKey, NormalEraChange,
NormalGlobalRandomnessInterval,
self as pallet_subspace, AllowAuthoringBy, Config, CurrentSlot, FarmerPublicKey,
NormalEraChange, NormalGlobalRandomnessInterval,
};
use frame_support::pallet_prelude::Weight;
use frame_support::parameter_types;
Expand All @@ -39,7 +39,7 @@ use sp_runtime::testing::{Digest, DigestItem, Header, TestXt};
use sp_runtime::traits::{Block as BlockT, Header as _, IdentityLookup};
use sp_runtime::Perbill;
use std::iter;
use std::num::NonZeroU64;
use std::num::{NonZeroU32, NonZeroU64};
use std::sync::Once;
use subspace_archiving::archiver::{Archiver, NewArchivedSegment};
use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg};
Expand Down Expand Up @@ -286,7 +286,12 @@ pub fn new_test_ext() -> TestExternalities {
.unwrap();

GenesisBuild::<Test>::assimilate_storage(
&pallet_subspace::GenesisConfig::default(),
&pallet_subspace::GenesisConfig {
enable_rewards: true,
enable_storage_access: true,
allow_authoring_by: AllowAuthoringBy::Anyone,
pot_slot_iterations: NonZeroU32::new(100_000).unwrap(),
},
&mut storage,
)
.unwrap();
Expand Down
8 changes: 3 additions & 5 deletions crates/sc-consensus-subspace/src/slot_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ use sp_runtime::DigestItem;
use std::collections::BTreeMap;
use std::future::Future;
use std::marker::PhantomData;
#[cfg(feature = "pot")]
use std::num::NonZeroU32;
use std::pin::Pin;
use std::sync::Arc;
#[cfg(feature = "pot")]
Expand Down Expand Up @@ -299,9 +297,10 @@ where
let (solution_range, voting_solution_range) =
extract_solution_ranges_for_block(self.client.as_ref(), parent_hash).ok()?;

#[cfg(feature = "pot")]
let pot_slot_iterations = runtime_api.pot_slot_iterations(parent_hash).ok()?;
let maybe_root_plot_public_key = runtime_api.root_plot_public_key(parent_hash).ok()?;

// TODO: Store `new_checkpoints`
#[cfg(feature = "pot")]
let (proof_of_time, future_proof_of_time, new_checkpoints) = {
let mut pot_checkpoints = self.pot_checkpoints.lock();
Expand Down Expand Up @@ -473,8 +472,7 @@ where
solution,
#[cfg(feature = "pot")]
pot_info: PreDigestPotInfo::Regular {
// TODO: Replace with correct value from runtime state
iterations: NonZeroU32::MIN,
iterations: pot_slot_iterations,
proof_of_time,
future_proof_of_time,
},
Expand Down
4 changes: 0 additions & 4 deletions crates/sc-proof-of-time/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ pub async fn start_slot_worker<Block, Client, SC, Worker, SO, CIDP>(

if let Some(slot_info) = slot_info_producer.produce_slot_info(slot_to_claim).await {
let _ = worker.on_slot(slot_info).await;

// TODO: Remove this hack, it restricts slot production with extremely low number of
// iterations
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}
}
}
49 changes: 39 additions & 10 deletions crates/sc-proof-of-time/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ use derive_more::{Deref, DerefMut, From};
use futures::channel::mpsc;
use futures::executor::block_on;
use futures::SinkExt;
use sp_api::{ApiError, ProvideRuntimeApi};
use sp_blockchain::HeaderBackend;
use sp_consensus_slots::Slot;
use sp_consensus_subspace::{FarmerPublicKey, SubspaceApi as SubspaceRuntimeApi};
use sp_runtime::traits::Block as BlockT;
use std::marker::PhantomData;
use std::num::NonZeroU32;
use std::sync::Arc;
use std::thread;
use subspace_core_primitives::{PotCheckpoints, PotKey, PotSeed, SlotNumber};
use subspace_proof_of_time::PotError;
Expand Down Expand Up @@ -35,18 +41,40 @@ pub struct PotSourceConfig {
/// Depending on configuration may produce proofs of time locally, send/receive via gossip and keep
/// up to day with blockchain reorgs.
#[derive(Debug)]
pub struct PotSource {
// TODO
pub struct PotSource<Block, Client> {
// TODO: Use this in `fn run`
#[allow(dead_code)]
client: Arc<Client>,
_block: PhantomData<Block>,
}

impl PotSource {
pub fn new(config: PotSourceConfig) -> (Self, PotSlotInfoStream) {
impl<Block, Client> PotSource<Block, Client>
where
Block: BlockT,
Client: ProvideRuntimeApi<Block> + HeaderBackend<Block>,
Client::Api: SubspaceRuntimeApi<Block, FarmerPublicKey>,
{
pub fn new(
config: PotSourceConfig,
client: Arc<Client>,
) -> Result<(Self, PotSlotInfoStream), ApiError> {
let PotSourceConfig {
// TODO: Respect this boolean flag
is_timekeeper: _,
initial_key,
} = config;
// TODO: All 3 are incorrect and should be able to continue after node restart
let start_slot = SlotNumber::MIN;
let start_seed = PotSeed::default();
let start_key = config.initial_key;
// TODO: Change to correct values taken from blockchain
let iterations = NonZeroU32::new(1024).expect("Not zero; qed");
let start_key = initial_key;
#[cfg(feature = "pot")]
let best_hash = client.info().best_hash;
#[cfg(feature = "pot")]
let runtime_api = client.runtime_api();
#[cfg(feature = "pot")]
let iterations = runtime_api.pot_slot_iterations(best_hash)?;
#[cfg(not(feature = "pot"))]
let iterations = NonZeroU32::new(100_000_000).expect("Not zero; qed");

// TODO: Correct capacity
let (slot_sender, slot_receiver) = mpsc::channel(10);
Expand All @@ -61,12 +89,13 @@ impl PotSource {
})
.expect("Thread creation must not panic");

(
Ok((
Self {
// TODO
client,
_block: PhantomData,
},
PotSlotInfoStream(slot_receiver),
)
))
}

/// Run proof of time source
Expand Down
5 changes: 5 additions & 0 deletions crates/sp-consensus-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ use sp_io::hashing;
use sp_runtime::{ConsensusEngineId, DigestItem, Justification};
use sp_runtime_interface::pass_by::PassBy;
use sp_runtime_interface::{pass_by, runtime_interface};
#[cfg(feature = "pot")]
use sp_std::num::NonZeroU32;
use sp_std::vec::Vec;
use subspace_core_primitives::crypto::kzg::Kzg;
#[cfg(not(feature = "pot"))]
Expand Down Expand Up @@ -621,6 +623,9 @@ sp_api::decl_runtime_apis! {
/// The slot duration in milliseconds for Subspace.
fn slot_duration() -> SlotDuration;

/// Number of iterations for proof of time per slot
fn pot_slot_iterations() -> NonZeroU32;

/// Solution ranges.
fn solution_ranges() -> SolutionRanges;

Expand Down
9 changes: 9 additions & 0 deletions crates/subspace-node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use sp_consensus_subspace::FarmerPublicKey;
use sp_core::crypto::{Ss58Codec, UncheckedFrom};
use sp_domains::RuntimeType;
use sp_runtime::Percent;
use std::num::NonZeroU32;
use subspace_core_primitives::PotKey;
use subspace_runtime::{
AllowAuthoringBy, BalancesConfig, DomainsConfig, GenesisConfig, MaxDomainBlockSize,
Expand Down Expand Up @@ -81,6 +82,7 @@ struct GenesisParams {
enable_rewards: bool,
enable_storage_access: bool,
allow_authoring_by: AllowAuthoringBy,
pot_slot_iterations: NonZeroU32,
enable_domains: bool,
enable_transfer: bool,
confirmation_depth_k: u32,
Expand Down Expand Up @@ -149,6 +151,8 @@ pub fn gemini_3f_compiled() -> Result<ConsensusChainSpec<GenesisConfig>, String>
"8aecbcf0b404590ddddc01ebacb205a562d12fdb5c2aa6a4035c1a20f23c9515"
)),
),
// TODO: Adjust once we bench PoT on faster hardware
pot_slot_iterations: NonZeroU32::new(183_270_000).expect("Not zero; qed"),
enable_domains: true,
enable_transfer: false,
confirmation_depth_k: 100, // TODO: Proper value here
Expand Down Expand Up @@ -246,6 +250,7 @@ pub fn devnet_config_compiled() -> Result<ConsensusChainSpec<GenesisConfig>, Str
enable_rewards: false,
enable_storage_access: false,
allow_authoring_by: AllowAuthoringBy::FirstFarmer,
pot_slot_iterations: NonZeroU32::new(100_000_000).expect("Not zero; qed"),
enable_domains: true,
enable_transfer: true,
confirmation_depth_k: 100, // TODO: Proper value here
Expand Down Expand Up @@ -303,6 +308,7 @@ pub fn dev_config() -> Result<ConsensusChainSpec<GenesisConfig>, String> {
enable_rewards: false,
enable_storage_access: false,
allow_authoring_by: AllowAuthoringBy::Anyone,
pot_slot_iterations: NonZeroU32::new(150_000_000).expect("Not zero; qed"),
enable_domains: true,
enable_transfer: true,
confirmation_depth_k: 5,
Expand Down Expand Up @@ -365,6 +371,7 @@ pub fn local_config() -> Result<ConsensusChainSpec<GenesisConfig>, String> {
enable_rewards: false,
enable_storage_access: false,
allow_authoring_by: AllowAuthoringBy::Anyone,
pot_slot_iterations: NonZeroU32::new(100_000_000).expect("Not zero; qed"),
enable_domains: true,
enable_transfer: true,
confirmation_depth_k: 1,
Expand Down Expand Up @@ -406,6 +413,7 @@ fn subspace_genesis_config(
enable_rewards,
enable_storage_access,
allow_authoring_by,
pot_slot_iterations,
enable_domains,
enable_transfer,
confirmation_depth_k,
Expand Down Expand Up @@ -433,6 +441,7 @@ fn subspace_genesis_config(
enable_rewards,
enable_storage_access,
allow_authoring_by,
pot_slot_iterations,
},
vesting: VestingConfig { vesting },
runtime_configs: RuntimeConfigsConfig {
Expand Down
22 changes: 14 additions & 8 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ use sp_runtime::transaction_validity::{TransactionSource, TransactionValidity};
use sp_runtime::{
create_runtime_str, generic, AccountId32, ApplyExtrinsicResult, Perbill, SaturatedConversion,
};
#[cfg(feature = "pot")]
use sp_std::num::NonZeroU32;
use sp_std::prelude::*;
#[cfg(feature = "std")]
use sp_version::NativeVersion;
Expand Down Expand Up @@ -1093,18 +1095,14 @@ impl_runtime_apis! {
}

impl sp_consensus_subspace::SubspaceApi<Block, FarmerPublicKey> for Runtime {
fn history_size() -> HistorySize {
<pallet_subspace::Pallet<Runtime>>::history_size()
}

fn max_pieces_in_sector() -> u16 {
MAX_PIECES_IN_SECTOR
}

fn slot_duration() -> SlotDuration {
SlotDuration::from_millis(SLOT_DURATION)
}

fn pot_slot_iterations() -> NonZeroU32 {
Subspace::pot_slot_iterations()
}

fn solution_ranges() -> SolutionRanges {
Subspace::solution_ranges()
}
Expand Down Expand Up @@ -1143,6 +1141,14 @@ impl_runtime_apis! {
Subspace::is_in_block_list(farmer_public_key)
}

fn history_size() -> HistorySize {
<pallet_subspace::Pallet<Runtime>>::history_size()
}

fn max_pieces_in_sector() -> u16 {
MAX_PIECES_IN_SECTOR
}

fn segment_commitment(segment_index: SegmentIndex) -> Option<SegmentCommitment> {
Subspace::segment_commitment(segment_index)
}
Expand Down
4 changes: 3 additions & 1 deletion crates/subspace-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,9 @@ where

if config.role.is_authority() || config.force_new_slot_notifications {
#[cfg(feature = "pot")]
let (pot_source, pot_slot_info_stream) = PotSource::new(config.pot_source_config);
let (pot_source, pot_slot_info_stream) =
PotSource::new(config.pot_source_config, client.clone())
.map_err(|error| Error::Other(error.into()))?;
#[cfg(feature = "pot")]
{
task_manager.spawn_essential_handle().spawn_blocking(
Expand Down
Loading

0 comments on commit ddf6b21

Please sign in to comment.