Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subspace-core-primitives refactoring (part 2) #3088

Merged
merged 6 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion crates/pallet-subspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ rand = { version = "0.8.5", features = ["min_const_gen"] }
sp-io = { git = "https://github.com/subspace/polkadot-sdk", rev = "5871818e1d736f1843eb9078f886290695165c42" }
subspace-archiving = { version = "0.1.0", path = "../subspace-archiving" }
subspace-core-primitives = { version = "0.1.0", path = "../subspace-core-primitives" }
subspace-farmer-components = { version = "0.1.0", path = "../subspace-farmer-components" }
subspace-erasure-coding = { version = "0.1.0", path = "../subspace-erasure-coding" }
subspace-farmer-components = { version = "0.1.0", path = "../subspace-farmer-components" }
subspace-kzg = { version = "0.1.0", path = "../../shared/subspace-kzg" }
subspace-proof-of-space = { version = "0.1.0", path = "../subspace-proof-of-space" }

[features]
Expand Down
19 changes: 9 additions & 10 deletions crates/pallet-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ use sp_runtime::transaction_validity::{
};
use sp_std::collections::btree_map::BTreeMap;
use sp_std::prelude::*;
use subspace_core_primitives::crypto::Scalar;
use subspace_core_primitives::pieces::PieceOffset;
use subspace_core_primitives::sectors::{SectorId, SectorIndex};
use subspace_core_primitives::segments::{
ArchivedHistorySegment, HistorySize, SegmentHeader, SegmentIndex,
};
use subspace_core_primitives::{
BlockHash, PublicKey, RewardSignature, SlotNumber, SolutionRange, REWARD_SIGNING_CONTEXT,
BlockHash, PublicKey, RewardSignature, ScalarBytes, SlotNumber, SolutionRange,
REWARD_SIGNING_CONTEXT,
};
use subspace_verification::{
check_reward_signature, derive_next_solution_range, derive_pot_entropy, PieceCheckParams,
Expand Down Expand Up @@ -112,13 +112,12 @@ pub mod pallet {
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::pieces::PieceOffset;
use subspace_core_primitives::pot::PotCheckpoints;
use subspace_core_primitives::sectors::SectorIndex;
use subspace_core_primitives::segments::{HistorySize, SegmentHeader, SegmentIndex};
use subspace_core_primitives::{
Blake3Hash, PublicKey, Randomness, RewardSignature, SolutionRange,
Blake3Hash, PublicKey, Randomness, RewardSignature, ScalarBytes, SolutionRange,
};

pub(super) struct InitialSolutionRanges<T: Config> {
Expand Down Expand Up @@ -437,7 +436,7 @@ pub mod pallet {
/// Parent block author information.
#[pallet::storage]
pub(super) type ParentBlockAuthorInfo<T> =
StorageValue<_, (PublicKey, SectorIndex, PieceOffset, Scalar, Slot)>;
StorageValue<_, (PublicKey, SectorIndex, PieceOffset, ScalarBytes, Slot)>;

/// Enable rewards since specified block number.
#[pallet::storage]
Expand All @@ -455,7 +454,7 @@ pub mod pallet {
PublicKey,
SectorIndex,
PieceOffset,
Scalar,
ScalarBytes,
Slot,
Option<T::AccountId>,
),
Expand All @@ -466,7 +465,7 @@ pub mod pallet {
pub(super) type ParentBlockVoters<T: Config> = StorageValue<
_,
BTreeMap<
(PublicKey, SectorIndex, PieceOffset, Scalar, Slot),
(PublicKey, SectorIndex, PieceOffset, ScalarBytes, Slot),
(Option<T::AccountId>, RewardSignature),
>,
ValueQuery,
Expand All @@ -477,7 +476,7 @@ pub mod pallet {
pub(super) type CurrentBlockVoters<T: Config> = StorageValue<
_,
BTreeMap<
(PublicKey, SectorIndex, PieceOffset, Scalar, Slot),
(PublicKey, SectorIndex, PieceOffset, ScalarBytes, Slot),
(Option<T::AccountId>, RewardSignature),
>,
>;
Expand Down Expand Up @@ -846,7 +845,7 @@ impl<T: Config> Pallet<T> {
}
}
CurrentBlockVoters::<T>::put(BTreeMap::<
(PublicKey, SectorIndex, PieceOffset, Scalar, Slot),
(PublicKey, SectorIndex, PieceOffset, ScalarBytes, Slot),
(Option<T::AccountId>, RewardSignature),
>::default());

Expand Down Expand Up @@ -920,7 +919,7 @@ impl<T: Config> Pallet<T> {

if (block_number % pot_entropy_injection_interval).is_zero() {
let current_block_entropy = derive_pot_entropy(
pre_digest.solution().chunk,
&pre_digest.solution().chunk,
pre_digest.pot_info().proof_of_time(),
);
// Collect entropy every `T::PotEntropyInjectionInterval` blocks
Expand Down
9 changes: 4 additions & 5 deletions crates/pallet-subspace/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use std::simd::Simd;
use std::sync::{Once, OnceLock};
use std::{iter, slice};
use subspace_archiving::archiver::{Archiver, NewArchivedSegment};
use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg};
use subspace_core_primitives::pieces::{Piece, PieceOffset, Record};
use subspace_core_primitives::pos::PosSeed;
use subspace_core_primitives::pot::PotOutput;
Expand All @@ -53,6 +52,7 @@ use subspace_farmer_components::auditing::audit_sector_sync;
use subspace_farmer_components::plotting::{plot_sector, CpuRecordsEncoder, PlotSectorOptions};
use subspace_farmer_components::reading::ReadSectorRecordChunksMode;
use subspace_farmer_components::FarmerProtocolInfo;
use subspace_kzg::Kzg;
use subspace_proof_of_space::shim::ShimTable;
use subspace_proof_of_space::{Table, TableGenerator};
use subspace_verification::is_within_solution_range;
Expand All @@ -67,7 +67,7 @@ const MAX_PIECES_IN_SECTOR: u16 = 1;
fn kzg_instance() -> &'static Kzg {
static KZG: OnceLock<Kzg> = OnceLock::new();

KZG.get_or_init(|| Kzg::new(embedded_kzg_settings()))
KZG.get_or_init(Kzg::new)
}

fn erasure_coding_instance() -> &'static ErasureCoding {
Expand Down Expand Up @@ -388,9 +388,8 @@ pub fn create_signed_vote(
solution.sector_index,
);
let sector_slot_challenge = sector_id.derive_sector_slot_challenge(&global_challenge);
let masked_chunk = (Simd::from(solution.chunk.to_bytes())
^ Simd::from(*solution.proof_of_space.hash()))
.to_array();
let masked_chunk =
(Simd::from(*solution.chunk) ^ Simd::from(*solution.proof_of_space.hash())).to_array();

// Check that solution quality is not too high
if is_within_solution_range(
Expand Down
7 changes: 3 additions & 4 deletions crates/pallet-subspace/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ use std::assert_matches::assert_matches;
use std::collections::BTreeMap;
use std::num::NonZeroU32;
use std::sync::{Arc, Mutex};
use subspace_core_primitives::crypto::Scalar;
use subspace_core_primitives::pieces::PieceOffset;
use subspace_core_primitives::pot::PotOutput;
use subspace_core_primitives::segments::SegmentIndex;
use subspace_core_primitives::{PublicKey, RewardSignature, SolutionRange};
use subspace_core_primitives::{PublicKey, RewardSignature, ScalarBytes, SolutionRange};
use subspace_runtime_primitives::{FindBlockRewardAddress, FindVotingRewardAddresses};

#[test]
Expand Down Expand Up @@ -1277,7 +1276,7 @@ fn enabling_block_rewards_works() {
PublicKey::from(Keypair::generate().public.to_bytes()),
0,
PieceOffset::ZERO,
Scalar::default(),
ScalarBytes::default(),
Subspace::current_slot(),
Some(1),
));
Expand All @@ -1288,7 +1287,7 @@ fn enabling_block_rewards_works() {
PublicKey::from(Keypair::generate().public.to_bytes()),
0,
PieceOffset::ZERO,
Scalar::default(),
ScalarBytes::default(),
Subspace::current_slot(),
),
(Some(2), RewardSignature::from([0; 64])),
Expand Down
1 change: 1 addition & 0 deletions crates/sc-consensus-subspace-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ subspace-archiving = { version = "0.1.0", path = "../subspace-archiving" }
subspace-core-primitives = { version = "0.1.0", path = "../subspace-core-primitives" }
subspace-erasure-coding = { version = "0.1.0", path = "../subspace-erasure-coding" }
subspace-farmer-components = { version = "0.1.0", path = "../subspace-farmer-components" }
subspace-kzg = { version = "0.1.0", path = "../../shared/subspace-kzg" }
subspace-networking = { version = "0.1.0", path = "../subspace-networking" }
subspace-rpc-primitives = { version = "0.1.0", path = "../subspace-rpc-primitives" }
thiserror = "1.0.64"
Expand Down
2 changes: 1 addition & 1 deletion crates/sc-consensus-subspace-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Weak};
use std::time::Duration;
use subspace_archiving::archiver::NewArchivedSegment;
use subspace_core_primitives::crypto::kzg::Kzg;
use subspace_core_primitives::objects::GlobalObjectMapping;
use subspace_core_primitives::pieces::{Piece, PieceIndex};
use subspace_core_primitives::segments::{HistorySize, SegmentHeader, SegmentIndex};
use subspace_core_primitives::{Blake3Hash, BlockHash, PublicKey, SlotNumber, Solution};
use subspace_erasure_coding::ErasureCoding;
use subspace_farmer_components::FarmerProtocolInfo;
use subspace_kzg::Kzg;
use subspace_networking::libp2p::Multiaddr;
use subspace_rpc_primitives::{
FarmerAppInfo, RewardSignatureResponse, RewardSigningInfo, SlotInfo, SolutionResponse,
Expand Down
1 change: 1 addition & 0 deletions crates/sc-consensus-subspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ sp-runtime = { git = "https://github.com/subspace/polkadot-sdk", rev = "5871818e
subspace-archiving = { version = "0.1.0", path = "../subspace-archiving" }
subspace-core-primitives = { version = "0.1.0", path = "../subspace-core-primitives" }
subspace-erasure-coding = { version = "0.1.0", path = "../subspace-erasure-coding" }
subspace-kzg = { version = "0.1.0", path = "../../shared/subspace-kzg" }
subspace-proof-of-space = { version = "0.1.0", path = "../subspace-proof-of-space" }
subspace-verification = { version = "0.1.0", path = "../subspace-verification" }
thiserror = "1.0.64"
Expand Down
2 changes: 1 addition & 1 deletion crates/sc-consensus-subspace/src/archiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ use std::sync::atomic::{AtomicU16, Ordering};
use std::sync::Arc;
use std::time::Duration;
use subspace_archiving::archiver::{Archiver, NewArchivedSegment};
use subspace_core_primitives::crypto::kzg::Kzg;
use subspace_core_primitives::objects::BlockObjectMapping;
use subspace_core_primitives::segments::{RecordedHistorySegment, SegmentHeader, SegmentIndex};
use subspace_core_primitives::{BlockNumber, PublicKey};
use subspace_erasure_coding::ErasureCoding;
use subspace_kzg::Kzg;
use tracing::{debug, info, trace, warn};

/// Number of WASM instances is 8, this is a bit lower to avoid warnings exceeding number of
Expand Down
4 changes: 4 additions & 0 deletions crates/sc-consensus-subspace/src/block_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ pub enum Error<Header: HeaderT> {
/// Invalid audit chunk offset
#[error("Invalid audit chunk offset")]
InvalidAuditChunkOffset,
/// Invalid chunk
#[error("Invalid chunk: {0}")]
InvalidChunk(String),
/// Invalid chunk witness
#[error("Invalid chunk witness")]
InvalidChunkWitness,
Expand Down Expand Up @@ -242,6 +245,7 @@ where
VerificationPrimitiveError::InvalidAuditChunkOffset => {
Error::InvalidAuditChunkOffset
}
VerificationPrimitiveError::InvalidChunk(error) => Error::InvalidChunk(error),
VerificationPrimitiveError::InvalidChunkWitness => Error::InvalidChunkWitness,
VerificationPrimitiveError::SectorExpired {
expiration_history_size,
Expand Down
2 changes: 1 addition & 1 deletion crates/sc-consensus-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ use crate::notification::{SubspaceNotificationSender, SubspaceNotificationStream
use crate::slot_worker::{NewSlotNotification, RewardSigningNotification};
use sp_consensus_subspace::ChainConstants;
use sp_runtime::traits::Block as BlockT;
use subspace_core_primitives::crypto::kzg::Kzg;
use subspace_erasure_coding::ErasureCoding;
use subspace_kzg::Kzg;

/// State that must be shared between various consensus components.
#[derive(Clone)]
Expand Down
Loading
Loading