Skip to content

Commit

Permalink
Merge pull request #1796 from subspace/conditionally-compiled-pot-sup…
Browse files Browse the repository at this point in the history
…port

Conditionally compiled PoT support
  • Loading branch information
nazar-pc authored Aug 11, 2023
2 parents 0663bdc + 6b2eed7 commit 6b7ed31
Show file tree
Hide file tree
Showing 17 changed files with 67 additions and 10 deletions.
3 changes: 3 additions & 0 deletions crates/pallet-subspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ subspace-proof-of-space = { version = "0.1.0", path = "../subspace-proof-of-spac

[features]
default = ["std"]
pot = [
"sp-consensus-subspace/pot",
]
std = [
"codec/std",
"frame-benchmarking?/std",
Expand Down
1 change: 1 addition & 0 deletions crates/pallet-subspace/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ pub fn make_pre_digest(
let log = DigestItem::subspace_pre_digest(&PreDigest {
slot,
solution,
#[cfg(feature = "pot")]
proof_of_time: Default::default(),
});
Digest { logs: vec![log] }
Expand Down
6 changes: 6 additions & 0 deletions crates/sc-consensus-subspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ thiserror = "1.0.38"
#substrate-test-runtime = { version = "2.0.0", path = "../../substrate/substrate-test-runtime" }
#substrate-test-runtime-client = { version = "2.0.0", path = "../../substrate/substrate-test-runtime-client" }
#tokio = "1.27.0"

[features]
pot = [
"sc-proof-of-time/pot",
"sp-consensus-subspace/pot",
]
7 changes: 5 additions & 2 deletions crates/sc-consensus-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ use sp_consensus::{
BlockOrigin, Environment, Error as ConsensusError, Proposer, SelectChain, SyncOracle,
};
use sp_consensus_slots::{Slot, SlotDuration};
#[cfg(feature = "pot")]
use sp_consensus_subspace::digests::PreDigest;
use sp_consensus_subspace::digests::{
extract_pre_digest, extract_subspace_digest_items, Error as DigestError, PreDigest,
SubspaceDigestItems,
extract_pre_digest, extract_subspace_digest_items, Error as DigestError, SubspaceDigestItems,
};
use sp_consensus_subspace::{
check_header, ChainConstants, CheckedHeader, FarmerPublicKey, FarmerSignature, SubspaceApi,
Expand Down Expand Up @@ -975,6 +976,7 @@ where
None => parent_subspace_digest_items.solution_range,
};

#[cfg(feature = "pot")]
if let Some(proof_of_time) = self.proof_of_time.as_ref() {
let ret = self.proof_of_time_verification(
proof_of_time.as_ref(),
Expand Down Expand Up @@ -1116,6 +1118,7 @@ where

/// Verifies the proof of time in the received block.
#[allow(clippy::too_many_arguments)]
#[cfg(feature = "pot")]
fn proof_of_time_verification(
&self,
proof_of_time: &dyn PotConsensusState,
Expand Down
26 changes: 18 additions & 8 deletions crates/sc-consensus-subspace/src/slot_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,42 @@ use sc_consensus::{JustificationSyncLink, StorageChanges};
use sc_consensus_slots::{
BackoffAuthoringBlocksStrategy, SimpleSlotWorker, SlotInfo, SlotLenienceType, SlotProportion,
};
use sc_proof_of_time::{PotConsensusState, PotGetBlockProofsError};
use sc_proof_of_time::PotConsensusState;
#[cfg(feature = "pot")]
use sc_proof_of_time::PotGetBlockProofsError;
use sc_telemetry::TelemetryHandle;
use sc_utils::mpsc::tracing_unbounded;
use schnorrkel::context::SigningContext;
use sp_api::{ApiError, NumberFor, ProvideRuntimeApi, TransactionFor};
use sp_blockchain::{Error as ClientError, HeaderBackend, HeaderMetadata};
use sp_consensus::{BlockOrigin, Environment, Error as ConsensusError, Proposer, SyncOracle};
use sp_consensus_slots::Slot;
use sp_consensus_subspace::digests::{
extract_pre_digest, CompatibleDigestItem, PotPreDigest, PreDigest,
};
#[cfg(feature = "pot")]
use sp_consensus_subspace::digests::PotPreDigest;
use sp_consensus_subspace::digests::{extract_pre_digest, CompatibleDigestItem, PreDigest};
use sp_consensus_subspace::{FarmerPublicKey, FarmerSignature, SignedVote, SubspaceApi, Vote};
use sp_core::crypto::ByteArray;
use sp_core::H256;
use sp_runtime::traits::{
Block as BlockT, Header, NumberFor as BlockNumberFor, One, Saturating, Zero,
};
#[cfg(feature = "pot")]
use sp_runtime::traits::NumberFor as BlockNumberFor;
use sp_runtime::traits::{Block as BlockT, Header, One, Saturating, Zero};
use sp_runtime::DigestItem;
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::Arc;
#[cfg(feature = "pot")]
use subspace_core_primitives::SlotNumber;
use subspace_core_primitives::{
BlockNumber, PublicKey, Randomness, RewardSignature, SectorId, SlotNumber, Solution,
BlockNumber, PublicKey, Randomness, RewardSignature, SectorId, Solution,
};
use subspace_proof_of_space::Table;
use subspace_verification::{
check_reward_signature, verify_solution, PieceCheckParams, VerifySolutionParams,
};

/// Errors while building the block proof of time.
#[cfg(feature = "pot")]
#[derive(Debug, thiserror::Error)]
pub enum PotCreateError<Block: BlockT> {
/// Parent block has no proof of time digest.
Expand Down Expand Up @@ -132,6 +137,8 @@ where
pub(super) max_block_proposal_slot_portion: Option<SlotProportion>,
pub(super) telemetry: Option<TelemetryHandle>,
pub(super) segment_headers_store: SegmentHeadersStore<AS>,
// TODO: Un-suppress once we enable PoT unconditionally
#[allow(dead_code)]
pub(super) proof_of_time: Option<Arc<dyn PotConsensusState>>,
pub(super) _pos_table: PhantomData<PosTable>,
}
Expand Down Expand Up @@ -350,12 +357,14 @@ where
// block reward is claimed
if maybe_pre_digest.is_none() && solution_distance <= solution_range / 2 {
info!(target: "subspace", "🚜 Claimed block at slot {slot}");
#[cfg(feature = "pot")]
let proof_of_time = self
.build_block_pot(parent_header, &parent_pre_digest, slot.into())
.ok()?;
maybe_pre_digest.replace(PreDigest {
solution,
slot,
#[cfg(feature = "pot")]
proof_of_time,
});
} else if !parent_header.number().is_zero() {
Expand Down Expand Up @@ -586,6 +595,7 @@ where
}

/// Builds the proof of time for the block being proposed.
#[cfg(feature = "pot")]
fn build_block_pot(
&self,
parent_header: &Block::Header,
Expand Down
3 changes: 3 additions & 0 deletions crates/sc-proof-of-time/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ parking_lot = "0.12.1"
thiserror = "1.0.38"
tokio = { version = "1.28.2", features = ["time"] }
tracing = "0.1.37"

[features]
pot = []
3 changes: 3 additions & 0 deletions crates/sc-proof-of-time/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ where
block_hash: info.best_hash.to_fixed_bytes(),
block_number: info.best_number,
slot_number: pre_digest.slot.into(),
#[cfg(feature = "pot")]
pot_pre_digest: pre_digest.proof_of_time.unwrap_or_default(),
#[cfg(not(feature = "pot"))]
pot_pre_digest: Default::default(),
})
}
1 change: 1 addition & 0 deletions crates/sp-consensus-subspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ thiserror = { version = "1.0.38", optional = true }

[features]
default = ["std"]
pot = []
std = [
"async-trait",
"codec/std",
Expand Down
2 changes: 2 additions & 0 deletions crates/sp-consensus-subspace/src/digests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub struct PreDigest<PublicKey, RewardAddress> {
pub slot: Slot,
/// Solution (includes PoR)
pub solution: Solution<PublicKey, RewardAddress>,
#[cfg(feature = "pot")]
/// Proof of time included in the block
/// TODO: It is Option<> for now for testing, to be removed
/// when PoT feature is permanently enabled.
Expand Down Expand Up @@ -648,6 +649,7 @@ where
FarmerPublicKey::unchecked_from([0u8; 32]),
FarmerPublicKey::unchecked_from([0u8; 32]),
),
#[cfg(feature = "pot")]
proof_of_time: Default::default(),
});
}
Expand Down
2 changes: 2 additions & 0 deletions crates/sp-consensus-subspace/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fn test_is_equivocation_proof_valid() {
logs: vec![DigestItem::subspace_pre_digest(&PreDigest {
slot,
solution: solution.clone(),
#[cfg(feature = "pot")]
proof_of_time: Default::default(),
})],
},
Expand All @@ -67,6 +68,7 @@ fn test_is_equivocation_proof_valid() {
logs: vec![DigestItem::subspace_pre_digest(&PreDigest {
slot,
solution,
#[cfg(feature = "pot")]
proof_of_time: Default::default(),
})],
},
Expand Down
3 changes: 3 additions & 0 deletions crates/sp-lightclient/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ subspace-proof-of-space = { version = "0.1.0", path = "../subspace-proof-of-spac

[features]
default = ["std"]
pot = [
"sp-consensus-subspace/pot",
]
std = [
"codec/std",
"scale-info/std",
Expand Down
1 change: 1 addition & 0 deletions crates/sp-lightclient/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ fn valid_header(
let pre_digest = PreDigest {
slot: slot.into(),
solution,
#[cfg(feature = "pot")]
proof_of_time: Default::default(),
};
let digests = vec![
Expand Down
6 changes: 6 additions & 0 deletions crates/subspace-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ substrate-build-script-utils = { version = "3.0.0", git = "https://github.com/su

[features]
default = ["do-not-enforce-cost-of-storage"]
pot = [
"sc-consensus-subspace/pot",
"sc-proof-of-time/pot",
"sp-consensus-subspace/pot",
"subspace-runtime/pot",
]
do-not-enforce-cost-of-storage = [
"subspace-runtime/do-not-enforce-cost-of-storage"
]
Expand Down
4 changes: 4 additions & 0 deletions crates/subspace-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ hex-literal = "0.4.0"

[features]
default = ["std"]
pot = [
"pallet-subspace/pot",
"sp-consensus-subspace/pot",
]
std = [
"codec/std",
"domain-runtime-primitives/std",
Expand Down
5 changes: 5 additions & 0 deletions crates/subspace-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,8 @@ pallet-transaction-payment-rpc-runtime-api = { version = "4.0.0-dev", git = "htt

[features]
default = []
pot = [
"sc-consensus-subspace/pot",
"sc-proof-of-time/pot",
"sp-consensus-subspace/pot",
]
3 changes: 3 additions & 0 deletions test/subspace-test-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,6 @@ tracing = "0.1.37"

[dev-dependencies]
sp-keyring = { git = "https://github.com/subspace/substrate", rev = "55c157cff49b638a59d81a9f971f0f9a66829c71" }

[features]
pot = []
1 change: 1 addition & 0 deletions test/subspace-test-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ impl MockConsensusNode {
let pre_digest: PreDigest<FarmerPublicKey, AccountId> = PreDigest {
slot,
solution: self.mock_solution.clone(),
#[cfg(feature = "pot")]
proof_of_time: Default::default(),
};
let mut digest = Digest::default();
Expand Down

0 comments on commit 6b7ed31

Please sign in to comment.