Skip to content

Commit

Permalink
Merge pull request #1861 from subspace/pot-bootstrapping-initial-seed
Browse files Browse the repository at this point in the history
PoT bootstrapping initial seed
  • Loading branch information
nazar-pc authored Aug 25, 2023
2 parents 634075c + 53e9c09 commit 7c2914d
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 104 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

13 changes: 11 additions & 2 deletions crates/sc-proof-of-time/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod time_keeper;
use crate::state_manager::{init_pot_state, PotProtocolState};
use core::num::{NonZeroU32, NonZeroU8};
use std::sync::Arc;
use subspace_core_primitives::{BlockNumber, PotKey, SlotNumber};
use subspace_core_primitives::{BlockNumber, PotKey, PotSeed, SlotNumber};
use subspace_proof_of_time::ProofOfTime;

pub use state_manager::{
Expand All @@ -18,10 +18,13 @@ pub use state_manager::{
pub use time_keeper::TimeKeeper;

// TODO: change the fields that can't be zero to NonZero types.
// TODO: CLean up unused fields
#[derive(Debug, Clone)]
pub struct PotConfig {
/// PoT seed used initially when PoT chain starts.
pub initial_seed: PotSeed,

/// PoT key used initially when PoT chain starts.
// TODO: Also add seed field here
pub initial_key: PotKey,

/// Frequency of entropy injection from consensus.
Expand Down Expand Up @@ -52,6 +55,10 @@ pub struct PotConfig {
/// Components initialized during the new_partial() phase of set up.
#[derive(Debug)]
pub struct PotComponents {
/// PoT seed used initially when PoT chain starts.
// TODO: Remove this from here, shouldn't be necessary eventually
pub(crate) initial_seed: PotSeed,

/// PoT key used initially when PoT chain starts.
// TODO: Remove this from here, shouldn't be necessary eventually
pub(crate) initial_key: PotKey,
Expand All @@ -75,10 +82,12 @@ impl PotComponents {
let proof_of_time = ProofOfTime::new(config.pot_iterations, config.num_checkpoints)
// TODO: Proper error handling or proof
.expect("Failed to initialize proof of time");
let initial_seed = config.initial_seed;
let initial_key = config.initial_key;
let (protocol_state, consensus_state) = init_pot_state(config, proof_of_time);

Self {
initial_seed,
initial_key,
is_time_keeper,
proof_of_time,
Expand Down
20 changes: 17 additions & 3 deletions crates/sc-proof-of-time/src/state_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,18 @@ impl InternalState {
let tip = match self.chain.tip() {
Some(tip) => tip.clone(),
None => {
if proof.seed != self.config.initial_seed {
return Err(PotProtocolStateError::InvalidSeed {
expected: self.config.initial_seed,
actual: proof.seed,
});
}
if proof.key != self.config.initial_key {
return Err(PotProtocolStateError::InvalidKey {
expected: self.config.initial_key,
actual: proof.key,
});
}
// TODO: Check initial seed
// Chain is empty, possible first proof.
return Ok(());
}
Expand Down Expand Up @@ -659,7 +664,17 @@ impl StateManager {
}
})?;
} else {
// TODO: This is ugly, but we need initial key here right now
// TODO: This is ugly, but we need initial seed and key here right now
let initial_seed = self.state.lock().config.initial_seed;
if proof.seed != initial_seed {
return Err(PotVerifyBlockProofsError::UnexpectedSeed {
summary: summary.clone(),
block_number,
slot: slot_number,
parent_slot: parent_slot_number,
error_slot: proof.slot_number,
});
}
let initial_key = self.state.lock().config.initial_key;
if proof.key != initial_key {
return Err(PotVerifyBlockProofsError::UnexpectedKey {
Expand All @@ -670,7 +685,6 @@ impl StateManager {
error_slot: proof.slot_number,
});
}
// TODO: Check initial seed
}
to_add.push(proof.clone());
prev_proof = Some(proof.clone());
Expand Down
5 changes: 4 additions & 1 deletion crates/sc-proof-of-time/src/time_keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const PROOFS_CHANNEL_SIZE: usize = 12; // 2 * reveal lag.

/// The time keeper manages the protocol: periodic proof generation/verification, gossip.
pub struct TimeKeeper<Block, Client> {
// TODO: Remove this from here, shouldn't be necessary eventually
initial_seed: PotSeed,
// TODO: Remove this from here, shouldn't be necessary eventually
initial_key: PotKey,
proof_of_time: ProofOfTime,
Expand All @@ -51,6 +53,7 @@ where
gossip_sender: mpsc::Sender<PotProof>,
) -> Self {
Self {
initial_seed: components.initial_seed,
initial_key: components.initial_key,
proof_of_time: components.proof_of_time,
pot_state: Arc::clone(&components.protocol_state),
Expand Down Expand Up @@ -115,7 +118,7 @@ where
// proofs to exist
// No proof of time means genesis block, produce the very first proof
let proof = self.proof_of_time.create(
PotSeed::from_genesis_block_hash(best_hash.into()),
self.initial_seed,
self.initial_key,
0,
best_hash.into(),
Expand Down
3 changes: 1 addition & 2 deletions crates/subspace-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ sc-consensus-slots = { version = "0.10.0-dev", git = "https://github.com/subspac
sc-consensus-subspace = { version = "0.1.0", path = "../sc-consensus-subspace" }
sc-subspace-chain-specs = { version = "0.1.0", path = "../sc-subspace-chain-specs" }
sc-executor = { version = "0.10.0-dev", git = "https://github.com/subspace/substrate", rev = "55c157cff49b638a59d81a9f971f0f9a66829c71" }
sc-proof-of-time = { version = "0.1.0", path = "../sc-proof-of-time" }
sc-service = { version = "0.10.0-dev", git = "https://github.com/subspace/substrate", rev = "55c157cff49b638a59d81a9f971f0f9a66829c71", default-features = false }
sc-storage-monitor = { version = "0.1.0", git = "https://github.com/subspace/substrate", rev = "55c157cff49b638a59d81a9f971f0f9a66829c71", default-features = false }
sc-telemetry = { version = "4.0.0-dev", git = "https://github.com/subspace/substrate", rev = "55c157cff49b638a59d81a9f971f0f9a66829c71" }
Expand Down Expand Up @@ -79,9 +78,9 @@ substrate-build-script-utils = { version = "3.0.0", git = "https://github.com/su
default = ["do-not-enforce-cost-of-storage"]
pot = [
"sc-consensus-subspace/pot",
"sc-proof-of-time/pot",
"sp-consensus-subspace/pot",
"subspace-runtime/pot",
"subspace-service/pot",
]
do-not-enforce-cost-of-storage = [
"subspace-runtime/do-not-enforce-cost-of-storage"
Expand Down
34 changes: 10 additions & 24 deletions crates/subspace-node/src/bin/subspace-node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ use futures::future::TryFutureExt;
use log::warn;
use sc_cli::{ChainSpec, CliConfiguration, SubstrateCli};
use sc_consensus_slots::SlotProportion;
use sc_proof_of_time::{PotComponents, PotConfig};
use sc_service::PartialComponents;
use sc_storage_monitor::StorageMonitorService;
use sp_core::crypto::Ss58AddressFormat;
use sp_core::traits::SpawnEssentialNamed;
use sp_domains::GenerateGenesisStateRoot;
use std::num::{NonZeroU32, NonZeroU8};
use std::sync::Arc;
use subspace_node::domain::{
AccountId32ToAccountId20Converter, DomainCli, DomainGenesisBlockBuilder, DomainInstanceStarter,
Expand All @@ -38,7 +36,7 @@ use subspace_node::domain::{
use subspace_node::{Cli, ExecutorDispatch, Subcommand};
use subspace_proof_of_space::chia::ChiaTable;
use subspace_runtime::{Block, RuntimeApi};
use subspace_service::{DsnConfig, SubspaceConfiguration, SubspaceNetworking};
use subspace_service::{DsnConfig, PotPartialConfig, SubspaceConfiguration, SubspaceNetworking};

type PosTable = ChiaTable;

Expand Down Expand Up @@ -378,25 +376,13 @@ fn main() -> Result<(), Error> {
different explicit value"
);
}
let pot_components = if cli.pot_role.is_pot_enabled() {
Some(PotComponents::new(
cli.pot_role.is_time_keeper(),
// TODO: fill proper values. These are set to use less
// CPU and take less than 1 sec to produce per proof
// during the initial testing.
PotConfig {
initial_key: maybe_chain_spec_pot_initial_key
.or(cli.pot_initial_key)
.unwrap_or_default(),
randomness_update_interval_blocks: 18,
injection_depth_blocks: 90,
global_randomness_reveal_lag_slots: 6,
pot_injection_lag_slots: 6,
max_future_slots: 10,
pot_iterations: NonZeroU32::new(4 * 1_000).expect("Not zero; qed"),
num_checkpoints: NonZeroU8::new(4).expect("Not zero; qed"),
},
))
let pot_config = if cli.pot_role.is_pot_enabled() {
Some(PotPartialConfig {
is_timekeeper: cli.pot_role.is_time_keeper(),
initial_key: maybe_chain_spec_pot_initial_key
.or(cli.pot_initial_key)
.unwrap_or_default(),
})
} else {
None
};
Expand Down Expand Up @@ -486,15 +472,15 @@ fn main() -> Result<(), Error> {
subspace_service::new_partial::<PosTable, RuntimeApi, ExecutorDispatch>(
&consensus_chain_config,
Some(&construct_domain_genesis_block_builder),
pot_components,
pot_config,
)
.map_err(|error| {
sc_service::Error::Other(format!(
"Failed to build a full subspace node: {error:?}"
))
})?;

subspace_service::new_full::<PosTable, _, _, _>(
subspace_service::new_full::<PosTable, _, _>(
consensus_chain_config,
partial_components,
true,
Expand Down
Loading

0 comments on commit 7c2914d

Please sign in to comment.