Skip to content

Commit

Permalink
Add initial seed to PotConfig just like initial key
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed Aug 22, 2023
1 parent 0a40ded commit 53e9c09
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
12 changes: 10 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 @@ -21,8 +21,10 @@ pub use time_keeper::TimeKeeper;
// 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 @@ -53,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 @@ -76,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: 2 additions & 1 deletion crates/subspace-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ use std::marker::PhantomData;
use std::num::{NonZeroU32, NonZeroU8};
use std::sync::Arc;
use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg};
use subspace_core_primitives::PotKey;
use subspace_core_primitives::{PotKey, PotSeed};
use subspace_fraud_proof::verifier_api::VerifierClient;
use subspace_networking::libp2p::multiaddr::Protocol;
use subspace_networking::libp2p::Multiaddr;
Expand Down Expand Up @@ -398,6 +398,7 @@ where
// CPU and take less than 1 sec to produce per proof
// during the initial testing.
PotConfig {
initial_seed: PotSeed::from_genesis_block_hash(client.info().genesis_hash.into()),
initial_key: pot_config.initial_key,
randomness_update_interval_blocks: 18,
injection_depth_blocks: 90,
Expand Down

0 comments on commit 53e9c09

Please sign in to comment.