Skip to content

Commit

Permalink
Cleanups around PoT code (#1993)
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc authored Sep 25, 2023
1 parent d410201 commit 65a27a3
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 98 deletions.
1 change: 0 additions & 1 deletion crates/sc-consensus-subspace/src/slot_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,6 @@ where
import_block.body = Some(body);
import_block.state_action =
StateAction::ApplyChanges(StorageChanges::Changes(storage_changes));

import_block
.justifications
.replace(Justifications::from(Justification::from(justification)));
Expand Down
107 changes: 52 additions & 55 deletions crates/sc-consensus-subspace/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,17 @@ pub enum VerificationError<Header: HeaderT> {
VerificationError(Slot, subspace_verification::Error),
}

// TODO: Make this non-enum
/// A header which has been checked
enum CheckedHeader<H, S> {
/// A header which is fully checked, including signature. This is the pre-header
/// accompanied by the seal components.
struct CheckedHeader<H> {
/// A header which is fully checked, including signature. This is the pre-header accompanied by
/// the seal components.
///
/// Includes the digest item that encoded the seal.
Checked(H, S),
pre_header: H,
/// Pre-digest
pre_digest: PreDigest<FarmerPublicKey, FarmerPublicKey>,
/// Seal (signature)
seal: DigestItem,
}

/// Subspace verification parameters
Expand All @@ -81,14 +84,6 @@ where
verify_solution_params: &'a VerifySolutionParams,
}

/// Information from verified header
struct VerifiedHeaderInfo {
/// Pre-digest
pre_digest: PreDigest<FarmerPublicKey, FarmerPublicKey>,
/// Seal (signature)
seal: DigestItem,
}

/// Options for Subspace block verifier
pub struct SubspaceVerifierOptions<Block, Client, SelectChain, SlotNow>
where
Expand Down Expand Up @@ -233,8 +228,7 @@ where
>,
full_pot_verification: bool,
justifications: &Option<Justifications>,
) -> Result<CheckedHeader<Block::Header, VerifiedHeaderInfo>, VerificationError<Block::Header>>
{
) -> Result<CheckedHeader<Block::Header>, VerificationError<Block::Header>> {
let VerificationParams {
mut header,
verify_solution_params,
Expand Down Expand Up @@ -399,10 +393,11 @@ where
)
.map_err(|error| VerificationError::VerificationError(slot, error))?;

Ok(CheckedHeader::Checked(
header,
VerifiedHeaderInfo { pre_digest, seal },
))
Ok(CheckedHeader {
pre_header: header,
pre_digest,
seal,
})
}

async fn check_and_report_equivocation(
Expand Down Expand Up @@ -571,44 +566,46 @@ where
Box::new(full_pot_verification),
);

match checked_header {
CheckedHeader::Checked(pre_header, verified_info) => {
let slot = verified_info.pre_digest.slot();

// the header is valid but let's check if there was something else already
// proposed at the same slot by the given author. if there was, we will
// report the equivocation to the runtime.
if let Err(err) = self
.check_and_report_equivocation(
slot_now,
slot,
&block.header,
&verified_info.pre_digest.solution().public_key,
&block.origin,
)
.await
{
warn!(
target: "subspace",
"Error checking/reporting Subspace equivocation: {}",
err
);
}
let CheckedHeader {
pre_header,
pre_digest,
seal,
} = checked_header;

trace!(target: "subspace", "Checked {:?}; importing.", pre_header);
telemetry!(
self.telemetry;
CONSENSUS_TRACE;
"subspace.checked_and_importing";
"pre_header" => ?pre_header,
);

block.header = pre_header;
block.post_digests.push(verified_info.seal);
block.post_hash = Some(hash);
let slot = pre_digest.slot();

Ok(block)
}
// the header is valid but let's check if there was something else already proposed at the
// same slot by the given author. if there was, we will report the equivocation to the
// runtime.
if let Err(err) = self
.check_and_report_equivocation(
slot_now,
slot,
&block.header,
&pre_digest.solution().public_key,
&block.origin,
)
.await
{
warn!(
target: "subspace",
"Error checking/reporting Subspace equivocation: {}",
err
);
}

trace!(target: "subspace", "Checked {:?}; importing.", pre_header);
telemetry!(
self.telemetry;
CONSENSUS_TRACE;
"subspace.checked_and_importing";
"pre_header" => ?pre_header,
);

block.header = pre_header;
block.post_digests.push(seal);
block.post_hash = Some(hash);

Ok(block)
}
}
46 changes: 21 additions & 25 deletions crates/sc-proof-of-time/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,30 @@ where
GossipSync: GossipSyncing<Block> + 'static,
SO: SyncOracle + Send + Sync + 'static,
{
let chain_constants;
let mut maybe_next_parameters_change;
let start_slot;
let best_hash = client.info().best_hash;
let runtime_api = client.runtime_api();
let chain_constants = runtime_api.chain_constants(best_hash)?;

let best_header = client
.header(best_hash)?
.ok_or_else(|| ApiError::UnknownBlock(format!("Parent block {best_hash} not found")))?;
let best_pre_digest = extract_pre_digest(&best_header)
.map_err(|error| ApiError::Application(error.into()))?;

let start_slot = if best_header.number().is_zero() {
Slot::from(1)
} else {
// Next slot after the best one seen
best_pre_digest.slot() + chain_constants.block_authoring_delay() + Slot::from(1)
};

let pot_parameters = runtime_api.pot_parameters(best_hash)?;
let mut maybe_next_parameters_change = pot_parameters.next_parameters_change();

let start_seed;
let slot_iterations;
{
let best_hash = client.info().best_hash;
let runtime_api = client.runtime_api();
chain_constants = runtime_api.chain_constants(best_hash)?;

let best_header = client.header(best_hash)?.ok_or_else(|| {
ApiError::UnknownBlock(format!("Parent block {best_hash} not found"))
})?;
let best_pre_digest = extract_pre_digest(&best_header)
.map_err(|error| ApiError::Application(error.into()))?;

start_slot = if best_header.number().is_zero() {
Slot::from(1)
} else {
// Next slot after the best one seen
best_pre_digest.slot() + chain_constants.block_authoring_delay() + Slot::from(1)
};

let pot_parameters = runtime_api.pot_parameters(best_hash)?;
maybe_next_parameters_change = pot_parameters.next_parameters_change();

if let Some(parameters_change) = maybe_next_parameters_change
if let Some(parameters_change) = maybe_next_parameters_change
&& parameters_change.slot == start_slot
{
start_seed = best_pre_digest.pot_info().future_proof_of_time().seed_with_entropy(&parameters_change.entropy);
Expand All @@ -123,7 +120,6 @@ where
};
slot_iterations = pot_parameters.slot_iterations();
}
}

let state = Arc::new(PotState::new(
NextSlotInput {
Expand Down
33 changes: 16 additions & 17 deletions crates/subspace-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,24 +974,23 @@ where
let block_importing_notification_stream = subspace_link.block_importing_notification_stream();
let archived_segment_notification_stream = subspace_link.archived_segment_notification_stream();

let pot_slot_info_stream = {
let (pot_source_worker, pot_gossip_worker, pot_slot_info_stream) = PotSourceWorker::new(
config.is_timekeeper,
config.timekeeper_cpu_cores,
client.clone(),
pot_verifier.clone(),
network_service.clone(),
sync_service.clone(),
sync_oracle.clone(),
)
.map_err(|error| Error::Other(error.into()))?;
let spawn_essential_handle = task_manager.spawn_essential_handle();

spawn_essential_handle.spawn("pot-source", Some("pot"), pot_source_worker.run());
spawn_essential_handle.spawn("pot-gossip", Some("pot"), pot_gossip_worker.run());
let (pot_source_worker, pot_gossip_worker, pot_slot_info_stream) = PotSourceWorker::new(
config.is_timekeeper,
config.timekeeper_cpu_cores,
client.clone(),
pot_verifier.clone(),
network_service.clone(),
sync_service.clone(),
sync_oracle.clone(),
)
.map_err(|error| Error::Other(error.into()))?;

pot_slot_info_stream
};
task_manager
.spawn_essential_handle()
.spawn("pot-source", Some("pot"), pot_source_worker.run());
task_manager
.spawn_essential_handle()
.spawn("pot-gossip", Some("pot"), pot_gossip_worker.run());

if config.base.role.is_authority() || config.force_new_slot_notifications {
let proposer_factory = ProposerFactory::new(
Expand Down

0 comments on commit 65a27a3

Please sign in to comment.