From fff6c3719c1c6d102cb58e28cdc7b7ade34844be Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Wed, 2 Oct 2024 16:28:17 +0300 Subject: [PATCH 1/6] Remove re-exports in `subspace-core-primitives` --- crates/pallet-subspace/src/lib.rs | 15 +++++++++------ crates/pallet-subspace/src/mock.rs | 11 +++++++---- crates/pallet-subspace/src/tests.rs | 6 +++--- crates/sc-consensus-subspace-rpc/src/lib.rs | 5 +++-- crates/sc-consensus-subspace/src/archiver.rs | 5 ++--- .../sc-consensus-subspace/src/archiver/tests.rs | 5 ++--- crates/sc-consensus-subspace/src/block_import.rs | 5 ++--- crates/sp-consensus-subspace/src/digests.rs | 5 ++--- crates/sp-consensus-subspace/src/lib.rs | 6 +++--- crates/subspace-archiving/benches/archiving.rs | 2 +- crates/subspace-archiving/src/archiver.rs | 8 +++++--- .../archiver/incremental_record_commitments.rs | 2 +- .../subspace-archiving/src/piece_reconstructor.rs | 3 ++- crates/subspace-archiving/src/reconstructor.rs | 7 +++++-- .../tests/integration/archiver.rs | 7 +++++-- .../tests/integration/piece_reconstruction.rs | 5 ++--- .../tests/integration/reconstructor.rs | 7 ++++--- crates/subspace-core-primitives/benches/kzg.rs | 2 +- crates/subspace-core-primitives/src/lib.rs | 13 +++++-------- crates/subspace-core-primitives/src/objects.rs | 3 ++- crates/subspace-core-primitives/src/pieces.rs | 5 +++-- crates/subspace-core-primitives/src/segments.rs | 2 ++ .../benches/commitments.rs | 2 +- crates/subspace-fake-runtime-api/src/lib.rs | 5 ++--- .../benches/auditing.rs | 6 +++--- .../benches/plotting.rs | 4 +++- .../subspace-farmer-components/benches/proving.rs | 7 +++---- .../subspace-farmer-components/benches/reading.rs | 6 +++--- crates/subspace-farmer-components/src/auditing.rs | 3 ++- crates/subspace-farmer-components/src/lib.rs | 3 ++- crates/subspace-farmer-components/src/plotting.rs | 7 +++---- crates/subspace-farmer-components/src/proving.rs | 6 ++---- crates/subspace-farmer-components/src/reading.rs | 3 ++- crates/subspace-farmer-components/src/sector.rs | 7 ++++--- .../src/segment_reconstruction.rs | 3 ++- .../src/bin/subspace-farmer/commands/benchmark.rs | 3 ++- .../subspace-farmer/commands/cluster/farmer.rs | 3 ++- .../subspace-farmer/commands/cluster/plotter.rs | 2 +- .../src/bin/subspace-farmer/commands/farm.rs | 3 ++- crates/subspace-farmer/src/cluster/cache.rs | 2 +- crates/subspace-farmer/src/cluster/controller.rs | 4 +++- crates/subspace-farmer/src/cluster/farmer.rs | 3 ++- crates/subspace-farmer/src/disk_piece_cache.rs | 3 ++- .../subspace-farmer/src/disk_piece_cache/tests.rs | 2 +- crates/subspace-farmer/src/farm.rs | 4 +++- crates/subspace-farmer/src/farm/plotted_pieces.rs | 3 ++- crates/subspace-farmer/src/farmer_cache.rs | 4 +++- .../src/farmer_cache/piece_cache_state.rs | 2 +- crates/subspace-farmer/src/farmer_cache/tests.rs | 6 +++--- crates/subspace-farmer/src/farmer_piece_getter.rs | 2 +- .../src/farmer_piece_getter/piece_validator.rs | 2 +- crates/subspace-farmer/src/node_client.rs | 4 +++- .../src/node_client/caching_proxy_node_client.rs | 4 +++- .../src/node_client/rpc_node_client.rs | 4 +++- crates/subspace-farmer/src/plotter/gpu/cuda.rs | 4 +++- crates/subspace-farmer/src/single_disk_farm.rs | 6 +++--- .../src/single_disk_farm/farming.rs | 6 +++--- .../src/single_disk_farm/piece_cache.rs | 2 +- .../src/single_disk_farm/piece_reader.rs | 3 ++- .../src/single_disk_farm/plot_cache.rs | 3 ++- .../src/single_disk_farm/plot_cache/tests.rs | 4 +++- .../src/single_disk_farm/plotted_sectors.rs | 3 ++- .../src/single_disk_farm/plotting.rs | 7 +++---- crates/subspace-networking/examples/benchmark.rs | 2 +- .../subspace-networking/examples/random-walker.rs | 2 +- crates/subspace-networking/src/constructor.rs | 3 ++- .../request_response/handlers/piece_by_index.rs | 2 +- .../request_response/handlers/segment_header.rs | 3 ++- crates/subspace-networking/src/utils/multihash.rs | 2 +- .../src/utils/piece_provider.rs | 2 +- crates/subspace-runtime/src/lib.rs | 6 ++++-- crates/subspace-service/src/lib.rs | 3 ++- crates/subspace-service/src/sync_from_dsn.rs | 4 +++- .../src/sync_from_dsn/import_blocks.rs | 6 ++++-- .../src/sync_from_dsn/piece_validator.rs | 2 +- .../sync_from_dsn/segment_header_downloader.rs | 3 ++- .../src/sync_from_dsn/snap_sync.rs | 3 ++- crates/subspace-verification/src/lib.rs | 7 ++++--- .../subspace-data-retrieval/src/object_fetcher.rs | 5 ++--- .../subspace-data-retrieval/src/piece_fetcher.rs | 2 +- .../subspace-data-retrieval/src/piece_getter.rs | 2 +- .../src/segment_fetcher.rs | 5 +++-- shared/subspace-proof-of-space-gpu/src/cuda.rs | 3 ++- .../subspace-proof-of-space-gpu/src/cuda/tests.rs | 4 +++- test/subspace-test-client/src/lib.rs | 5 +++-- test/subspace-test-runtime/src/lib.rs | 5 +++-- 86 files changed, 213 insertions(+), 157 deletions(-) diff --git a/crates/pallet-subspace/src/lib.rs b/crates/pallet-subspace/src/lib.rs index c6e2c0499f..f2e7887fed 100644 --- a/crates/pallet-subspace/src/lib.rs +++ b/crates/pallet-subspace/src/lib.rs @@ -58,10 +58,11 @@ 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::segments::{ArchivedHistorySegment, HistorySize, SegmentIndex}; use subspace_core_primitives::{ - ArchivedHistorySegment, BlockHash, HistorySize, PieceOffset, PublicKey, RewardSignature, - SectorId, SectorIndex, SegmentHeader, SegmentIndex, SlotNumber, SolutionRange, - REWARD_SIGNING_CONTEXT, + BlockHash, PublicKey, RewardSignature, SectorId, SectorIndex, SegmentHeader, SlotNumber, + SolutionRange, REWARD_SIGNING_CONTEXT, }; use subspace_verification::{ check_reward_signature, derive_next_solution_range, derive_pot_entropy, PieceCheckParams, @@ -110,9 +111,11 @@ pub mod pallet { 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::segments::{HistorySize, SegmentIndex}; use subspace_core_primitives::{ - Blake3Hash, HistorySize, PieceOffset, PotCheckpoints, PublicKey, Randomness, - RewardSignature, SectorIndex, SegmentHeader, SegmentIndex, SolutionRange, + Blake3Hash, PotCheckpoints, PublicKey, Randomness, RewardSignature, SectorIndex, + SegmentHeader, SolutionRange, }; pub(super) struct InitialSolutionRanges { @@ -414,7 +417,7 @@ pub mod pallet { _, Twox64Concat, SegmentIndex, - subspace_core_primitives::SegmentCommitment, + subspace_core_primitives::segments::SegmentCommitment, >; /// Whether the segment headers inherent has been processed in this block (temporary value). diff --git a/crates/pallet-subspace/src/mock.rs b/crates/pallet-subspace/src/mock.rs index b1c793f9f8..6630bab153 100644 --- a/crates/pallet-subspace/src/mock.rs +++ b/crates/pallet-subspace/src/mock.rs @@ -36,11 +36,14 @@ 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::segments::{ + ArchivedHistorySegment, HistorySize, RecordedHistorySegment, SegmentCommitment, SegmentIndex, +}; use subspace_core_primitives::{ - ArchivedBlockProgress, ArchivedHistorySegment, Blake3Hash, BlockNumber, HistorySize, - LastArchivedBlock, Piece, PieceOffset, PosSeed, PotOutput, PublicKey, Record, - RecordedHistorySegment, RewardSignature, SectorId, SegmentCommitment, SegmentHeader, - SegmentIndex, SlotNumber, Solution, SolutionRange, REWARD_SIGNING_CONTEXT, + ArchivedBlockProgress, Blake3Hash, BlockNumber, LastArchivedBlock, PosSeed, PotOutput, + PublicKey, RewardSignature, SectorId, SegmentHeader, SlotNumber, Solution, SolutionRange, + REWARD_SIGNING_CONTEXT, }; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::auditing::audit_sector_sync; diff --git a/crates/pallet-subspace/src/tests.rs b/crates/pallet-subspace/src/tests.rs index 7f5f6697a6..bdc0c5259f 100644 --- a/crates/pallet-subspace/src/tests.rs +++ b/crates/pallet-subspace/src/tests.rs @@ -42,9 +42,9 @@ use std::collections::BTreeMap; use std::num::NonZeroU32; use std::sync::{Arc, Mutex}; use subspace_core_primitives::crypto::Scalar; -use subspace_core_primitives::{ - PieceOffset, PotOutput, PublicKey, RewardSignature, SegmentIndex, SolutionRange, -}; +use subspace_core_primitives::pieces::PieceOffset; +use subspace_core_primitives::segments::SegmentIndex; +use subspace_core_primitives::{PotOutput, PublicKey, RewardSignature, SolutionRange}; use subspace_runtime_primitives::{FindBlockRewardAddress, FindVotingRewardAddresses}; #[test] diff --git a/crates/sc-consensus-subspace-rpc/src/lib.rs b/crates/sc-consensus-subspace-rpc/src/lib.rs index 094fd4f8a4..7122bf9b5e 100644 --- a/crates/sc-consensus-subspace-rpc/src/lib.rs +++ b/crates/sc-consensus-subspace-rpc/src/lib.rs @@ -55,9 +55,10 @@ 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, SegmentIndex}; use subspace_core_primitives::{ - Blake3Hash, BlockHash, HistorySize, Piece, PieceIndex, PublicKey, SegmentHeader, SegmentIndex, - SlotNumber, Solution, + Blake3Hash, BlockHash, PublicKey, SegmentHeader, SlotNumber, Solution, }; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::FarmerProtocolInfo; diff --git a/crates/sc-consensus-subspace/src/archiver.rs b/crates/sc-consensus-subspace/src/archiver.rs index 92668433bc..bef268e9a2 100644 --- a/crates/sc-consensus-subspace/src/archiver.rs +++ b/crates/sc-consensus-subspace/src/archiver.rs @@ -78,9 +78,8 @@ 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::{ - BlockNumber, PublicKey, RecordedHistorySegment, SegmentHeader, SegmentIndex, -}; +use subspace_core_primitives::segments::{RecordedHistorySegment, SegmentIndex}; +use subspace_core_primitives::{BlockNumber, PublicKey, SegmentHeader}; use subspace_erasure_coding::ErasureCoding; use tracing::{debug, info, trace, warn}; diff --git a/crates/sc-consensus-subspace/src/archiver/tests.rs b/crates/sc-consensus-subspace/src/archiver/tests.rs index 32ad96046d..ebeb43ee47 100644 --- a/crates/sc-consensus-subspace/src/archiver/tests.rs +++ b/crates/sc-consensus-subspace/src/archiver/tests.rs @@ -3,9 +3,8 @@ use parking_lot::RwLock; use sc_client_api::AuxStore; use std::collections::HashMap; use std::sync::Arc; -use subspace_core_primitives::{ - ArchivedBlockProgress, LastArchivedBlock, SegmentHeader, SegmentIndex, -}; +use subspace_core_primitives::segments::SegmentIndex; +use subspace_core_primitives::{ArchivedBlockProgress, LastArchivedBlock, SegmentHeader}; struct MemAuxStore { store: RwLock, Vec>>, diff --git a/crates/sc-consensus-subspace/src/block_import.rs b/crates/sc-consensus-subspace/src/block_import.rs index 9a2bd306c2..0623ef206d 100644 --- a/crates/sc-consensus-subspace/src/block_import.rs +++ b/crates/sc-consensus-subspace/src/block_import.rs @@ -52,9 +52,8 @@ use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor, One}; use sp_runtime::Justifications; use std::marker::PhantomData; use std::sync::Arc; -use subspace_core_primitives::{ - BlockNumber, HistorySize, PublicKey, SectorId, SegmentHeader, SegmentIndex, SolutionRange, -}; +use subspace_core_primitives::segments::{HistorySize, SegmentIndex}; +use subspace_core_primitives::{BlockNumber, PublicKey, SectorId, SegmentHeader, SolutionRange}; use subspace_proof_of_space::Table; use subspace_verification::{calculate_block_weight, PieceCheckParams, VerifySolutionParams}; use tracing::warn; diff --git a/crates/sp-consensus-subspace/src/digests.rs b/crates/sp-consensus-subspace/src/digests.rs index 875528ea7c..357ab4b4cf 100644 --- a/crates/sp-consensus-subspace/src/digests.rs +++ b/crates/sp-consensus-subspace/src/digests.rs @@ -25,9 +25,8 @@ use sp_runtime::DigestItem; use sp_std::collections::btree_map::{BTreeMap, Entry}; use sp_std::fmt; use sp_std::num::NonZeroU32; -use subspace_core_primitives::{ - PotOutput, PublicKey, RewardSignature, SegmentCommitment, SegmentIndex, Solution, SolutionRange, -}; +use subspace_core_primitives::segments::{SegmentCommitment, SegmentIndex}; +use subspace_core_primitives::{PotOutput, PublicKey, RewardSignature, Solution, SolutionRange}; /// A Subspace pre-runtime digest. This contains all data required to validate a block and for the /// Subspace runtime module. diff --git a/crates/sp-consensus-subspace/src/lib.rs b/crates/sp-consensus-subspace/src/lib.rs index 902a7b6da1..e94d9f7226 100644 --- a/crates/sp-consensus-subspace/src/lib.rs +++ b/crates/sp-consensus-subspace/src/lib.rs @@ -42,10 +42,10 @@ use sp_runtime_interface::{pass_by, runtime_interface}; use sp_std::num::NonZeroU32; #[cfg(feature = "std")] use subspace_core_primitives::crypto::kzg::Kzg; +use subspace_core_primitives::segments::{HistorySize, SegmentCommitment, SegmentIndex}; use subspace_core_primitives::{ - Blake3Hash, BlockHash, BlockNumber, HistorySize, PotCheckpoints, PotOutput, PotSeed, PublicKey, - RewardSignature, SegmentCommitment, SegmentHeader, SegmentIndex, SlotNumber, Solution, - SolutionRange, + Blake3Hash, BlockHash, BlockNumber, PotCheckpoints, PotOutput, PotSeed, PublicKey, + RewardSignature, SegmentHeader, SlotNumber, Solution, SolutionRange, }; #[cfg(feature = "std")] use subspace_proof_of_space::chia::ChiaTable; diff --git a/crates/subspace-archiving/benches/archiving.rs b/crates/subspace-archiving/benches/archiving.rs index c6491d867d..7d53ffe382 100644 --- a/crates/subspace-archiving/benches/archiving.rs +++ b/crates/subspace-archiving/benches/archiving.rs @@ -4,7 +4,7 @@ use std::num::NonZeroUsize; use subspace_archiving::archiver::Archiver; use subspace_core_primitives::crypto::kzg; use subspace_core_primitives::crypto::kzg::Kzg; -use subspace_core_primitives::Record; +use subspace_core_primitives::pieces::Record; use subspace_erasure_coding::ErasureCoding; const AMOUNT_OF_DATA: usize = 5 * 1024 * 1024; diff --git a/crates/subspace-archiving/src/archiver.rs b/crates/subspace-archiving/src/archiver.rs index dccec16038..b7b2aab826 100644 --- a/crates/subspace-archiving/src/archiver.rs +++ b/crates/subspace-archiving/src/archiver.rs @@ -34,10 +34,12 @@ use subspace_core_primitives::crypto::{blake3_254_hash_to_scalar, Scalar}; use subspace_core_primitives::objects::{ BlockObject, BlockObjectMapping, GlobalObject, PieceObject, PieceObjectMapping, }; +use subspace_core_primitives::pieces::{PieceArray, RawRecord, RecordWitness}; +use subspace_core_primitives::segments::{ + ArchivedHistorySegment, RecordedHistorySegment, SegmentCommitment, SegmentIndex, +}; use subspace_core_primitives::{ - ArchivedBlockProgress, ArchivedHistorySegment, Blake3Hash, BlockNumber, LastArchivedBlock, - PieceArray, RawRecord, RecordWitness, RecordedHistorySegment, SegmentCommitment, SegmentHeader, - SegmentIndex, + ArchivedBlockProgress, Blake3Hash, BlockNumber, LastArchivedBlock, SegmentHeader, }; use subspace_erasure_coding::ErasureCoding; diff --git a/crates/subspace-archiving/src/archiver/incremental_record_commitments.rs b/crates/subspace-archiving/src/archiver/incremental_record_commitments.rs index 7fd263020a..6e1f7e4a6c 100644 --- a/crates/subspace-archiving/src/archiver/incremental_record_commitments.rs +++ b/crates/subspace-archiving/src/archiver/incremental_record_commitments.rs @@ -10,7 +10,7 @@ use parity_scale_codec::{Encode, Output}; use rayon::prelude::*; use subspace_core_primitives::crypto::kzg::{Commitment, Kzg}; use subspace_core_primitives::crypto::Scalar; -use subspace_core_primitives::RawRecord; +use subspace_core_primitives::pieces::RawRecord; /// State of incremental record commitments, encapsulated to hide implementation details and /// encapsulate tricky logic diff --git a/crates/subspace-archiving/src/piece_reconstructor.rs b/crates/subspace-archiving/src/piece_reconstructor.rs index e56608ddaf..e88d65565f 100644 --- a/crates/subspace-archiving/src/piece_reconstructor.rs +++ b/crates/subspace-archiving/src/piece_reconstructor.rs @@ -9,7 +9,8 @@ use alloc::vec::Vec; use rayon::prelude::*; use subspace_core_primitives::crypto::kzg::{Commitment, Kzg, Polynomial}; use subspace_core_primitives::crypto::{blake3_254_hash_to_scalar, Scalar}; -use subspace_core_primitives::{ArchivedHistorySegment, Piece, RawRecord}; +use subspace_core_primitives::pieces::{Piece, RawRecord}; +use subspace_core_primitives::segments::ArchivedHistorySegment; use subspace_erasure_coding::ErasureCoding; /// Reconstructor-related instantiation error diff --git a/crates/subspace-archiving/src/reconstructor.rs b/crates/subspace-archiving/src/reconstructor.rs index 3a4f3f198f..db3e0b0f10 100644 --- a/crates/subspace-archiving/src/reconstructor.rs +++ b/crates/subspace-archiving/src/reconstructor.rs @@ -9,9 +9,12 @@ use alloc::vec::Vec; use core::mem; use parity_scale_codec::Decode; use subspace_core_primitives::crypto::Scalar; +use subspace_core_primitives::pieces::{Piece, RawRecord}; +use subspace_core_primitives::segments::{ + ArchivedHistorySegment, RecordedHistorySegment, SegmentIndex, +}; use subspace_core_primitives::{ - ArchivedBlockProgress, ArchivedHistorySegment, BlockNumber, LastArchivedBlock, Piece, - RawRecord, RecordedHistorySegment, SegmentHeader, SegmentIndex, + ArchivedBlockProgress, BlockNumber, LastArchivedBlock, SegmentHeader, }; use subspace_erasure_coding::ErasureCoding; diff --git a/crates/subspace-archiving/tests/integration/archiver.rs b/crates/subspace-archiving/tests/integration/archiver.rs index 0c49450c6e..a94935a3b5 100644 --- a/crates/subspace-archiving/tests/integration/archiver.rs +++ b/crates/subspace-archiving/tests/integration/archiver.rs @@ -10,9 +10,12 @@ use subspace_archiving::archiver; use subspace_archiving::archiver::{Archiver, ArchiverInstantiationError, SegmentItem}; use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg}; use subspace_core_primitives::objects::{BlockObject, BlockObjectMapping, PieceObject}; +use subspace_core_primitives::pieces::{Piece, Record}; +use subspace_core_primitives::segments::{ + ArchivedHistorySegment, RecordedHistorySegment, SegmentCommitment, SegmentIndex, +}; use subspace_core_primitives::{ - ArchivedBlockProgress, ArchivedHistorySegment, Blake3Hash, LastArchivedBlock, Piece, Record, - RecordedHistorySegment, SegmentCommitment, SegmentHeader, SegmentIndex, + ArchivedBlockProgress, Blake3Hash, LastArchivedBlock, SegmentHeader, }; use subspace_erasure_coding::ErasureCoding; diff --git a/crates/subspace-archiving/tests/integration/piece_reconstruction.rs b/crates/subspace-archiving/tests/integration/piece_reconstruction.rs index 8a1abe8a63..1617fe9bd7 100644 --- a/crates/subspace-archiving/tests/integration/piece_reconstruction.rs +++ b/crates/subspace-archiving/tests/integration/piece_reconstruction.rs @@ -6,9 +6,8 @@ use subspace_archiving::archiver::Archiver; use subspace_archiving::piece_reconstructor::{PiecesReconstructor, ReconstructorError}; use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg}; use subspace_core_primitives::objects::BlockObjectMapping; -use subspace_core_primitives::{ - ArchivedHistorySegment, FlatPieces, Piece, Record, RecordedHistorySegment, -}; +use subspace_core_primitives::pieces::{FlatPieces, Piece, Record}; +use subspace_core_primitives::segments::{ArchivedHistorySegment, RecordedHistorySegment}; use subspace_erasure_coding::ErasureCoding; fn pieces_to_option_of_pieces(pieces: &FlatPieces) -> Vec> { diff --git a/crates/subspace-archiving/tests/integration/reconstructor.rs b/crates/subspace-archiving/tests/integration/reconstructor.rs index 701cba89d3..e1ff9f6e15 100644 --- a/crates/subspace-archiving/tests/integration/reconstructor.rs +++ b/crates/subspace-archiving/tests/integration/reconstructor.rs @@ -6,10 +6,11 @@ use subspace_archiving::archiver::Archiver; use subspace_archiving::reconstructor::{Reconstructor, ReconstructorError}; use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg}; use subspace_core_primitives::objects::BlockObjectMapping; -use subspace_core_primitives::{ - ArchivedBlockProgress, ArchivedHistorySegment, FlatPieces, LastArchivedBlock, Piece, Record, - RecordedHistorySegment, SegmentIndex, +use subspace_core_primitives::pieces::{FlatPieces, Piece, Record}; +use subspace_core_primitives::segments::{ + ArchivedHistorySegment, RecordedHistorySegment, SegmentIndex, }; +use subspace_core_primitives::{ArchivedBlockProgress, LastArchivedBlock}; use subspace_erasure_coding::ErasureCoding; fn pieces_to_option_of_pieces(pieces: &FlatPieces) -> Vec> { diff --git a/crates/subspace-core-primitives/benches/kzg.rs b/crates/subspace-core-primitives/benches/kzg.rs index bb812ca57b..5956de3dcc 100644 --- a/crates/subspace-core-primitives/benches/kzg.rs +++ b/crates/subspace-core-primitives/benches/kzg.rs @@ -1,7 +1,7 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg}; use subspace_core_primitives::crypto::Scalar; -use subspace_core_primitives::RawRecord; +use subspace_core_primitives::pieces::RawRecord; fn criterion_benchmark(c: &mut Criterion) { let values = (0..RawRecord::NUM_CHUNKS) diff --git a/crates/subspace-core-primitives/src/lib.rs b/crates/subspace-core-primitives/src/lib.rs index 861de92454..fa8e91dbbf 100644 --- a/crates/subspace-core-primitives/src/lib.rs +++ b/crates/subspace-core-primitives/src/lib.rs @@ -31,8 +31,8 @@ pub mod checksum; pub mod crypto; pub mod objects; -mod pieces; -mod segments; +pub mod pieces; +pub mod segments; #[cfg(feature = "serde")] mod serde; #[cfg(test)] @@ -53,14 +53,11 @@ use derive_more::{Add, AsMut, AsRef, Deref, DerefMut, Display, Div, From, Into, use hex::FromHex; use num_traits::{WrappingAdd, WrappingSub}; use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; -pub use pieces::{ - ChunkWitness, FlatPieces, Piece, PieceArray, PieceIndex, PieceOffset, RawRecord, Record, - RecordCommitment, RecordWitness, SBucket, +use pieces::{ + ChunkWitness, PieceIndex, PieceOffset, Record, RecordCommitment, RecordWitness, SBucket, }; use scale_info::TypeInfo; -pub use segments::{ - ArchivedHistorySegment, HistorySize, RecordedHistorySegment, SegmentCommitment, SegmentIndex, -}; +use segments::{HistorySize, SegmentCommitment, SegmentIndex}; use static_assertions::{const_assert, const_assert_eq}; // Refuse to compile on lower than 32-bit platforms diff --git a/crates/subspace-core-primitives/src/objects.rs b/crates/subspace-core-primitives/src/objects.rs index 9196bdd368..df98fa6857 100644 --- a/crates/subspace-core-primitives/src/objects.rs +++ b/crates/subspace-core-primitives/src/objects.rs @@ -23,7 +23,8 @@ #[cfg(not(feature = "std"))] extern crate alloc; -use crate::{Blake3Hash, PieceIndex}; +use crate::pieces::PieceIndex; +use crate::Blake3Hash; #[cfg(not(feature = "std"))] use alloc::vec::Vec; use core::default::Default; diff --git a/crates/subspace-core-primitives/src/pieces.rs b/crates/subspace-core-primitives/src/pieces.rs index a8f6a5baf2..71e7d3458b 100644 --- a/crates/subspace-core-primitives/src/pieces.rs +++ b/crates/subspace-core-primitives/src/pieces.rs @@ -1,3 +1,5 @@ +//! Pieces-related data structures. + #[cfg(feature = "serde")] mod serde; #[cfg(test)] @@ -7,8 +9,7 @@ extern crate alloc; use crate::crypto::kzg::{Commitment, Witness}; use crate::crypto::Scalar; -use crate::segments::{ArchivedHistorySegment, SegmentIndex}; -use crate::RecordedHistorySegment; +use crate::segments::{ArchivedHistorySegment, RecordedHistorySegment, SegmentIndex}; #[cfg(feature = "serde")] use ::serde::{Deserialize, Serialize}; #[cfg(not(feature = "std"))] diff --git a/crates/subspace-core-primitives/src/segments.rs b/crates/subspace-core-primitives/src/segments.rs index b6668f70c3..c32c1c4cab 100644 --- a/crates/subspace-core-primitives/src/segments.rs +++ b/crates/subspace-core-primitives/src/segments.rs @@ -1,3 +1,5 @@ +//! Segments-related data structures. + use crate::crypto::kzg::Commitment; use crate::pieces::{FlatPieces, Piece, PieceIndex, RawRecord}; #[cfg(not(feature = "std"))] diff --git a/crates/subspace-erasure-coding/benches/commitments.rs b/crates/subspace-erasure-coding/benches/commitments.rs index 438d2a38ee..6283bf0e64 100644 --- a/crates/subspace-erasure-coding/benches/commitments.rs +++ b/crates/subspace-erasure-coding/benches/commitments.rs @@ -3,7 +3,7 @@ use kzg::G1; use rust_kzg_blst::types::g1::FsG1; use std::num::NonZeroUsize; use subspace_core_primitives::crypto::kzg::Commitment; -use subspace_core_primitives::ArchivedHistorySegment; +use subspace_core_primitives::segments::ArchivedHistorySegment; use subspace_erasure_coding::ErasureCoding; fn criterion_benchmark(c: &mut Criterion) { diff --git a/crates/subspace-fake-runtime-api/src/lib.rs b/crates/subspace-fake-runtime-api/src/lib.rs index aa3c65f986..5900d8464f 100644 --- a/crates/subspace-fake-runtime-api/src/lib.rs +++ b/crates/subspace-fake-runtime-api/src/lib.rs @@ -40,9 +40,8 @@ use sp_version::RuntimeVersion; use std::collections::btree_map::BTreeMap; use std::collections::btree_set::BTreeSet; use subspace_core_primitives::objects::BlockObjectMapping; -use subspace_core_primitives::{ - HistorySize, PublicKey, Randomness, SegmentCommitment, SegmentHeader, SegmentIndex, U256, -}; +use subspace_core_primitives::segments::{HistorySize, SegmentCommitment, SegmentIndex}; +use subspace_core_primitives::{PublicKey, Randomness, SegmentHeader, U256}; use subspace_runtime_primitives::opaque::Block; use subspace_runtime_primitives::{AccountId, Balance, BlockNumber, Moment, Nonce}; diff --git a/crates/subspace-farmer-components/benches/auditing.rs b/crates/subspace-farmer-components/benches/auditing.rs index 682e5cf1a9..16336d3427 100644 --- a/crates/subspace-farmer-components/benches/auditing.rs +++ b/crates/subspace-farmer-components/benches/auditing.rs @@ -9,9 +9,9 @@ use std::{env, fs, slice}; use subspace_archiving::archiver::Archiver; use subspace_core_primitives::crypto::kzg; use subspace_core_primitives::crypto::kzg::Kzg; -use subspace_core_primitives::{ - Blake3Hash, HistorySize, PublicKey, Record, RecordedHistorySegment, SectorId, SolutionRange, -}; +use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::segments::{HistorySize, RecordedHistorySegment}; +use subspace_core_primitives::{Blake3Hash, PublicKey, SectorId, SolutionRange}; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::auditing::audit_plot_sync; use subspace_farmer_components::file_ext::{FileExt, OpenOptionsExt}; diff --git a/crates/subspace-farmer-components/benches/plotting.rs b/crates/subspace-farmer-components/benches/plotting.rs index 8a6e55b235..14eff2d522 100644 --- a/crates/subspace-farmer-components/benches/plotting.rs +++ b/crates/subspace-farmer-components/benches/plotting.rs @@ -6,7 +6,9 @@ use std::num::{NonZeroU64, NonZeroUsize}; use subspace_archiving::archiver::Archiver; use subspace_core_primitives::crypto::kzg; use subspace_core_primitives::crypto::kzg::Kzg; -use subspace_core_primitives::{HistorySize, PublicKey, Record, RecordedHistorySegment}; +use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::segments::{HistorySize, RecordedHistorySegment}; +use subspace_core_primitives::PublicKey; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::plotting::{plot_sector, CpuRecordsEncoder, PlotSectorOptions}; use subspace_farmer_components::sector::sector_size; diff --git a/crates/subspace-farmer-components/benches/proving.rs b/crates/subspace-farmer-components/benches/proving.rs index f3f69ea2a2..f769b85c93 100644 --- a/crates/subspace-farmer-components/benches/proving.rs +++ b/crates/subspace-farmer-components/benches/proving.rs @@ -13,10 +13,9 @@ use std::{env, fs, slice}; use subspace_archiving::archiver::Archiver; use subspace_core_primitives::crypto::kzg; use subspace_core_primitives::crypto::kzg::Kzg; -use subspace_core_primitives::{ - Blake3Hash, HistorySize, PosSeed, PublicKey, Record, RecordedHistorySegment, SectorId, - SolutionRange, -}; +use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::segments::{HistorySize, RecordedHistorySegment}; +use subspace_core_primitives::{Blake3Hash, PosSeed, PublicKey, SectorId, SolutionRange}; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::auditing::audit_plot_sync; use subspace_farmer_components::file_ext::{FileExt, OpenOptionsExt}; diff --git a/crates/subspace-farmer-components/benches/reading.rs b/crates/subspace-farmer-components/benches/reading.rs index 2f0d8c98fd..cffa57d235 100644 --- a/crates/subspace-farmer-components/benches/reading.rs +++ b/crates/subspace-farmer-components/benches/reading.rs @@ -10,9 +10,9 @@ use std::{env, fs, slice}; use subspace_archiving::archiver::Archiver; use subspace_core_primitives::crypto::kzg; use subspace_core_primitives::crypto::kzg::Kzg; -use subspace_core_primitives::{ - HistorySize, PieceOffset, PublicKey, Record, RecordedHistorySegment, SectorId, -}; +use subspace_core_primitives::pieces::{PieceOffset, Record}; +use subspace_core_primitives::segments::{HistorySize, RecordedHistorySegment}; +use subspace_core_primitives::{PublicKey, SectorId}; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::file_ext::{FileExt, OpenOptionsExt}; use subspace_farmer_components::plotting::{ diff --git a/crates/subspace-farmer-components/src/auditing.rs b/crates/subspace-farmer-components/src/auditing.rs index 5ad0c322cd..935148f786 100644 --- a/crates/subspace-farmer-components/src/auditing.rs +++ b/crates/subspace-farmer-components/src/auditing.rs @@ -11,8 +11,9 @@ use rayon::prelude::*; use std::collections::HashSet; use std::io; use subspace_core_primitives::crypto::Scalar; +use subspace_core_primitives::pieces::SBucket; use subspace_core_primitives::{ - Blake3Hash, PublicKey, SBucket, SectorId, SectorIndex, SectorSlotChallenge, SolutionRange, + Blake3Hash, PublicKey, SectorId, SectorIndex, SectorSlotChallenge, SolutionRange, }; use subspace_verification::is_within_solution_range; use thiserror::Error; diff --git a/crates/subspace-farmer-components/src/lib.rs b/crates/subspace-farmer-components/src/lib.rs index aaa902fc8e..6fd0c97119 100644 --- a/crates/subspace-farmer-components/src/lib.rs +++ b/crates/subspace-farmer-components/src/lib.rs @@ -33,7 +33,8 @@ use std::fs::File; use std::future::Future; use std::io; use std::sync::Arc; -use subspace_core_primitives::{ArchivedHistorySegment, HistorySize, Piece, PieceIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; +use subspace_core_primitives::segments::{ArchivedHistorySegment, HistorySize}; /// Trait representing a way to get pieces #[async_trait] diff --git a/crates/subspace-farmer-components/src/plotting.rs b/crates/subspace-farmer-components/src/plotting.rs index 2e1a5e9c1f..ca85198551 100644 --- a/crates/subspace-farmer-components/src/plotting.rs +++ b/crates/subspace-farmer-components/src/plotting.rs @@ -26,10 +26,9 @@ use std::sync::Arc; use std::time::Duration; use subspace_core_primitives::crypto::kzg::Kzg; use subspace_core_primitives::crypto::{blake3_hash, blake3_hash_parallel, Scalar}; -use subspace_core_primitives::{ - Blake3Hash, HistorySize, PieceIndex, PieceOffset, PosSeed, PublicKey, Record, SBucket, - SectorId, SectorIndex, -}; +use subspace_core_primitives::pieces::{PieceIndex, PieceOffset, Record, SBucket}; +use subspace_core_primitives::segments::HistorySize; +use subspace_core_primitives::{Blake3Hash, PosSeed, PublicKey, SectorId, SectorIndex}; use subspace_erasure_coding::ErasureCoding; use subspace_proof_of_space::{Table, TableGenerator}; use thiserror::Error; diff --git a/crates/subspace-farmer-components/src/proving.rs b/crates/subspace-farmer-components/src/proving.rs index e82fe29000..d0fed75827 100644 --- a/crates/subspace-farmer-components/src/proving.rs +++ b/crates/subspace-farmer-components/src/proving.rs @@ -15,10 +15,8 @@ use futures::FutureExt; use std::collections::VecDeque; use std::io; use subspace_core_primitives::crypto::kzg::Kzg; -use subspace_core_primitives::{ - ChunkWitness, PieceOffset, PosSeed, PublicKey, Record, SBucket, SectorId, Solution, - SolutionRange, -}; +use subspace_core_primitives::pieces::{ChunkWitness, PieceOffset, Record, SBucket}; +use subspace_core_primitives::{PosSeed, PublicKey, SectorId, Solution, SolutionRange}; use subspace_erasure_coding::ErasureCoding; use subspace_proof_of_space::Table; use thiserror::Error; diff --git a/crates/subspace-farmer-components/src/reading.rs b/crates/subspace-farmer-components/src/reading.rs index 0265677136..5e9cb7af57 100644 --- a/crates/subspace-farmer-components/src/reading.rs +++ b/crates/subspace-farmer-components/src/reading.rs @@ -18,7 +18,8 @@ use std::simd::Simd; use std::str::FromStr; use std::{fmt, io}; use subspace_core_primitives::crypto::{blake3_hash, Scalar}; -use subspace_core_primitives::{Piece, PieceOffset, Record, SBucket, SectorId}; +use subspace_core_primitives::pieces::{Piece, PieceOffset, Record, SBucket}; +use subspace_core_primitives::SectorId; use subspace_erasure_coding::ErasureCoding; use subspace_proof_of_space::{Table, TableGenerator}; use thiserror::Error; diff --git a/crates/subspace-farmer-components/src/sector.rs b/crates/subspace-farmer-components/src/sector.rs index 53c39247ed..d961231786 100644 --- a/crates/subspace-farmer-components/src/sector.rs +++ b/crates/subspace-farmer-components/src/sector.rs @@ -14,10 +14,11 @@ use std::ops::{Deref, DerefMut}; use std::{mem, slice}; use subspace_core_primitives::checksum::Blake3Checksummed; use subspace_core_primitives::crypto::blake3_hash; -use subspace_core_primitives::{ - Blake3Hash, HistorySize, PieceOffset, Record, RecordCommitment, RecordWitness, SBucket, - SectorIndex, SegmentIndex, +use subspace_core_primitives::pieces::{ + PieceOffset, Record, RecordCommitment, RecordWitness, SBucket, }; +use subspace_core_primitives::segments::{HistorySize, SegmentIndex}; +use subspace_core_primitives::{Blake3Hash, SectorIndex}; use thiserror::Error; use tracing::debug; diff --git a/crates/subspace-farmer-components/src/segment_reconstruction.rs b/crates/subspace-farmer-components/src/segment_reconstruction.rs index 78db118d11..84439f542f 100644 --- a/crates/subspace-farmer-components/src/segment_reconstruction.rs +++ b/crates/subspace-farmer-components/src/segment_reconstruction.rs @@ -4,7 +4,8 @@ use futures::StreamExt; use std::sync::atomic::{AtomicUsize, Ordering}; use subspace_archiving::piece_reconstructor::{PiecesReconstructor, ReconstructorError}; use subspace_core_primitives::crypto::kzg::Kzg; -use subspace_core_primitives::{ArchivedHistorySegment, Piece, PieceIndex, RecordedHistorySegment}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; +use subspace_core_primitives::segments::{ArchivedHistorySegment, RecordedHistorySegment}; use subspace_erasure_coding::ErasureCoding; use thiserror::Error; use tokio::sync::Semaphore; diff --git a/crates/subspace-farmer/src/bin/subspace-farmer/commands/benchmark.rs b/crates/subspace-farmer/src/bin/subspace-farmer/commands/benchmark.rs index 72031b306d..81ef277678 100644 --- a/crates/subspace-farmer/src/bin/subspace-farmer/commands/benchmark.rs +++ b/crates/subspace-farmer/src/bin/subspace-farmer/commands/benchmark.rs @@ -9,7 +9,8 @@ use std::fs::OpenOptions; use std::num::NonZeroUsize; use std::path::PathBuf; use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg}; -use subspace_core_primitives::{Blake3Hash, Record, SolutionRange}; +use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::{Blake3Hash, SolutionRange}; use subspace_erasure_coding::ErasureCoding; use subspace_farmer::single_disk_farm::direct_io_file::DirectIoFile; use subspace_farmer::single_disk_farm::farming::rayon_files::RayonFiles; diff --git a/crates/subspace-farmer/src/bin/subspace-farmer/commands/cluster/farmer.rs b/crates/subspace-farmer/src/bin/subspace-farmer/commands/cluster/farmer.rs index 43421edd11..b72322f920 100644 --- a/crates/subspace-farmer/src/bin/subspace-farmer/commands/cluster/farmer.rs +++ b/crates/subspace-farmer/src/bin/subspace-farmer/commands/cluster/farmer.rs @@ -17,7 +17,8 @@ use std::pin::{pin, Pin}; use std::sync::Arc; use std::time::Duration; use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg}; -use subspace_core_primitives::{PublicKey, Record}; +use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::PublicKey; use subspace_erasure_coding::ErasureCoding; use subspace_farmer::cluster::controller::ClusterNodeClient; use subspace_farmer::cluster::farmer::farmer_service; diff --git a/crates/subspace-farmer/src/bin/subspace-farmer/commands/cluster/plotter.rs b/crates/subspace-farmer/src/bin/subspace-farmer/commands/cluster/plotter.rs index b2b3354e70..e59a2d5792 100644 --- a/crates/subspace-farmer/src/bin/subspace-farmer/commands/cluster/plotter.rs +++ b/crates/subspace-farmer/src/bin/subspace-farmer/commands/cluster/plotter.rs @@ -9,7 +9,7 @@ use std::pin::Pin; use std::sync::Arc; use std::time::Duration; use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg}; -use subspace_core_primitives::Record; +use subspace_core_primitives::pieces::Record; use subspace_erasure_coding::ErasureCoding; use subspace_farmer::cluster::controller::ClusterPieceGetter; use subspace_farmer::cluster::nats_client::NatsClient; diff --git a/crates/subspace-farmer/src/bin/subspace-farmer/commands/farm.rs b/crates/subspace-farmer/src/bin/subspace-farmer/commands/farm.rs index 68d7d540ad..dbb85e93b6 100644 --- a/crates/subspace-farmer/src/bin/subspace-farmer/commands/farm.rs +++ b/crates/subspace-farmer/src/bin/subspace-farmer/commands/farm.rs @@ -19,7 +19,8 @@ use std::str::FromStr; use std::sync::Arc; use std::time::Duration; use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg}; -use subspace_core_primitives::{PublicKey, Record}; +use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::PublicKey; use subspace_erasure_coding::ErasureCoding; use subspace_farmer::farm::plotted_pieces::PlottedPieces; use subspace_farmer::farm::{PlottedSectors, SectorPlottingDetails, SectorUpdate}; diff --git a/crates/subspace-farmer/src/cluster/cache.rs b/crates/subspace-farmer/src/cluster/cache.rs index 04b9a08dfd..a5bb7574e6 100644 --- a/crates/subspace-farmer/src/cluster/cache.rs +++ b/crates/subspace-farmer/src/cluster/cache.rs @@ -20,7 +20,7 @@ use parity_scale_codec::{Decode, Encode}; use std::future::{pending, Future}; use std::pin::{pin, Pin}; use std::time::{Duration, Instant}; -use subspace_core_primitives::{Piece, PieceIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; use tokio::time::MissedTickBehavior; use tracing::{debug, error, info, info_span, trace, warn, Instrument}; diff --git a/crates/subspace-farmer/src/cluster/controller.rs b/crates/subspace-farmer/src/cluster/controller.rs index ddc4730917..8827779914 100644 --- a/crates/subspace-farmer/src/cluster/controller.rs +++ b/crates/subspace-farmer/src/cluster/controller.rs @@ -24,7 +24,9 @@ use std::error::Error; use std::num::NonZeroUsize; use std::pin::Pin; use std::sync::Arc; -use subspace_core_primitives::{Piece, PieceIndex, SegmentHeader, SegmentIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; +use subspace_core_primitives::segments::SegmentIndex; +use subspace_core_primitives::SegmentHeader; use subspace_farmer_components::PieceGetter; use subspace_rpc_primitives::{ FarmerAppInfo, RewardSignatureResponse, RewardSigningInfo, SlotInfo, SolutionResponse, diff --git a/crates/subspace-farmer/src/cluster/farmer.rs b/crates/subspace-farmer/src/cluster/farmer.rs index 483f12413e..d94f812138 100644 --- a/crates/subspace-farmer/src/cluster/farmer.rs +++ b/crates/subspace-farmer/src/cluster/farmer.rs @@ -28,7 +28,8 @@ use std::pin::{pin, Pin}; use std::sync::Arc; use std::time::{Duration, Instant}; use subspace_core_primitives::crypto::blake3_hash_list; -use subspace_core_primitives::{Blake3Hash, Piece, PieceOffset, SectorIndex}; +use subspace_core_primitives::pieces::{Piece, PieceOffset}; +use subspace_core_primitives::{Blake3Hash, SectorIndex}; use subspace_farmer_components::plotting::PlottedSector; use subspace_rpc_primitives::SolutionResponse; use tokio::time::MissedTickBehavior; diff --git a/crates/subspace-farmer/src/disk_piece_cache.rs b/crates/subspace-farmer/src/disk_piece_cache.rs index 0bdbcd5dfd..927a56bd0f 100644 --- a/crates/subspace-farmer/src/disk_piece_cache.rs +++ b/crates/subspace-farmer/src/disk_piece_cache.rs @@ -20,7 +20,8 @@ use std::sync::Arc; use std::task::Poll; use std::{fs, io}; use subspace_core_primitives::crypto::blake3_hash_list; -use subspace_core_primitives::{Blake3Hash, Piece, PieceIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; +use subspace_core_primitives::Blake3Hash; use subspace_farmer_components::file_ext::FileExt; use thiserror::Error; use tokio::runtime::Handle; diff --git a/crates/subspace-farmer/src/disk_piece_cache/tests.rs b/crates/subspace-farmer/src/disk_piece_cache/tests.rs index 4db64b695c..4f89861876 100644 --- a/crates/subspace-farmer/src/disk_piece_cache/tests.rs +++ b/crates/subspace-farmer/src/disk_piece_cache/tests.rs @@ -1,7 +1,7 @@ use crate::disk_piece_cache::{DiskPieceCache, DiskPieceCacheError, PieceCacheOffset}; use rand::prelude::*; use std::assert_matches::assert_matches; -use subspace_core_primitives::{Piece, PieceIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; use tempfile::tempdir; #[test] diff --git a/crates/subspace-farmer/src/farm.rs b/crates/subspace-farmer/src/farm.rs index c755c2d343..5eaa5df4ff 100644 --- a/crates/subspace-farmer/src/farm.rs +++ b/crates/subspace-farmer/src/farm.rs @@ -18,7 +18,9 @@ use std::pin::Pin; use std::sync::Arc; use std::time::Duration; use std::{fmt, io}; -use subspace_core_primitives::{Piece, PieceIndex, PieceOffset, SectorIndex, SegmentIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex, PieceOffset}; +use subspace_core_primitives::segments::SegmentIndex; +use subspace_core_primitives::SectorIndex; use subspace_farmer_components::auditing::AuditingError; use subspace_farmer_components::plotting::PlottedSector; use subspace_farmer_components::proving::ProvingError; diff --git a/crates/subspace-farmer/src/farm/plotted_pieces.rs b/crates/subspace-farmer/src/farm/plotted_pieces.rs index 084200f465..ae861e7105 100644 --- a/crates/subspace-farmer/src/farm/plotted_pieces.rs +++ b/crates/subspace-farmer/src/farm/plotted_pieces.rs @@ -10,7 +10,8 @@ use std::fmt; use std::future::Future; use std::hash::Hash; use std::sync::Arc; -use subspace_core_primitives::{Piece, PieceIndex, PieceOffset, SectorIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex, PieceOffset}; +use subspace_core_primitives::SectorIndex; use subspace_farmer_components::plotting::PlottedSector; use tracing::{trace, warn}; diff --git a/crates/subspace-farmer/src/farmer_cache.rs b/crates/subspace-farmer/src/farmer_cache.rs index c9fd45bf41..d24de3f490 100644 --- a/crates/subspace-farmer/src/farmer_cache.rs +++ b/crates/subspace-farmer/src/farmer_cache.rs @@ -25,7 +25,9 @@ use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; use std::time::Duration; use std::{fmt, mem}; -use subspace_core_primitives::{Piece, PieceIndex, SegmentHeader, SegmentIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; +use subspace_core_primitives::segments::SegmentIndex; +use subspace_core_primitives::SegmentHeader; use subspace_farmer_components::PieceGetter; use subspace_networking::libp2p::kad::{ProviderRecord, RecordKey}; use subspace_networking::libp2p::PeerId; diff --git a/crates/subspace-farmer/src/farmer_cache/piece_cache_state.rs b/crates/subspace-farmer/src/farmer_cache/piece_cache_state.rs index 1e2824c472..978d92d6cb 100644 --- a/crates/subspace-farmer/src/farmer_cache/piece_cache_state.rs +++ b/crates/subspace-farmer/src/farmer_cache/piece_cache_state.rs @@ -3,7 +3,7 @@ use std::collections::btree_map::Values; use std::collections::{BTreeMap, HashMap, VecDeque}; use std::fmt; use std::hash::Hash; -use subspace_core_primitives::PieceIndex; +use subspace_core_primitives::pieces::PieceIndex; use subspace_networking::libp2p::PeerId; use subspace_networking::utils::multihash::ToMultihash; use subspace_networking::KeyWithDistance; diff --git a/crates/subspace-farmer/src/farmer_cache/tests.rs b/crates/subspace-farmer/src/farmer_cache/tests.rs index c7a3c900ad..b2bcb8c426 100644 --- a/crates/subspace-farmer/src/farmer_cache/tests.rs +++ b/crates/subspace-farmer/src/farmer_cache/tests.rs @@ -18,9 +18,9 @@ use std::pin::Pin; use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::Arc; use std::time::Duration; -use subspace_core_primitives::{ - HistorySize, LastArchivedBlock, Piece, PieceIndex, SegmentHeader, SegmentIndex, -}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; +use subspace_core_primitives::segments::{HistorySize, SegmentIndex}; +use subspace_core_primitives::{LastArchivedBlock, SegmentHeader}; use subspace_farmer_components::{FarmerProtocolInfo, PieceGetter}; use subspace_networking::libp2p::identity; use subspace_networking::libp2p::kad::RecordKey; diff --git a/crates/subspace-farmer/src/farmer_piece_getter.rs b/crates/subspace-farmer/src/farmer_piece_getter.rs index 5ced33ac58..24306f07fa 100644 --- a/crates/subspace-farmer/src/farmer_piece_getter.rs +++ b/crates/subspace-farmer/src/farmer_piece_getter.rs @@ -18,7 +18,7 @@ use std::hash::Hash; use std::num::NonZeroUsize; use std::sync::atomic::{AtomicU32, Ordering}; use std::sync::{Arc, Weak}; -use subspace_core_primitives::{Piece, PieceIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; use subspace_farmer_components::PieceGetter; use subspace_networking::utils::multihash::ToMultihash; use subspace_networking::utils::piece_provider::{PieceProvider, PieceValidator}; diff --git a/crates/subspace-farmer/src/farmer_piece_getter/piece_validator.rs b/crates/subspace-farmer/src/farmer_piece_getter/piece_validator.rs index 8ddd42cf41..b99bbc3ff7 100644 --- a/crates/subspace-farmer/src/farmer_piece_getter/piece_validator.rs +++ b/crates/subspace-farmer/src/farmer_piece_getter/piece_validator.rs @@ -4,7 +4,7 @@ use crate::node_client::NodeClient; use async_trait::async_trait; use subspace_archiving::archiver::is_piece_valid; use subspace_core_primitives::crypto::kzg::Kzg; -use subspace_core_primitives::{Piece, PieceIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; use subspace_networking::libp2p::PeerId; use subspace_networking::utils::piece_provider::PieceValidator; use subspace_networking::Node; diff --git a/crates/subspace-farmer/src/node_client.rs b/crates/subspace-farmer/src/node_client.rs index 04815aa392..15f5299ee7 100644 --- a/crates/subspace-farmer/src/node_client.rs +++ b/crates/subspace-farmer/src/node_client.rs @@ -15,7 +15,9 @@ use async_trait::async_trait; use futures::Stream; use std::fmt; use std::pin::Pin; -use subspace_core_primitives::{Piece, PieceIndex, SegmentHeader, SegmentIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; +use subspace_core_primitives::segments::SegmentIndex; +use subspace_core_primitives::SegmentHeader; use subspace_rpc_primitives::{ FarmerAppInfo, RewardSignatureResponse, RewardSigningInfo, SlotInfo, SolutionResponse, }; diff --git a/crates/subspace-farmer/src/node_client/caching_proxy_node_client.rs b/crates/subspace-farmer/src/node_client/caching_proxy_node_client.rs index 8cd8a63f0e..6e9ab08a69 100644 --- a/crates/subspace-farmer/src/node_client/caching_proxy_node_client.rs +++ b/crates/subspace-farmer/src/node_client/caching_proxy_node_client.rs @@ -9,7 +9,9 @@ use futures::{select, FutureExt, Stream, StreamExt}; use std::pin::Pin; use std::sync::Arc; use std::time::{Duration, Instant}; -use subspace_core_primitives::{Piece, PieceIndex, SegmentHeader, SegmentIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; +use subspace_core_primitives::segments::SegmentIndex; +use subspace_core_primitives::SegmentHeader; use subspace_rpc_primitives::{ FarmerAppInfo, RewardSignatureResponse, RewardSigningInfo, SlotInfo, SolutionResponse, MAX_SEGMENT_HEADERS_PER_REQUEST, diff --git a/crates/subspace-farmer/src/node_client/rpc_node_client.rs b/crates/subspace-farmer/src/node_client/rpc_node_client.rs index 9247d5dfdb..6a728961b0 100644 --- a/crates/subspace-farmer/src/node_client/rpc_node_client.rs +++ b/crates/subspace-farmer/src/node_client/rpc_node_client.rs @@ -9,7 +9,9 @@ use jsonrpsee::rpc_params; use jsonrpsee::ws_client::{WsClient, WsClientBuilder}; use std::pin::Pin; use std::sync::Arc; -use subspace_core_primitives::{Piece, PieceIndex, SegmentHeader, SegmentIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; +use subspace_core_primitives::segments::SegmentIndex; +use subspace_core_primitives::SegmentHeader; use subspace_rpc_primitives::{ FarmerAppInfo, RewardSignatureResponse, RewardSigningInfo, SlotInfo, SolutionResponse, }; diff --git a/crates/subspace-farmer/src/plotter/gpu/cuda.rs b/crates/subspace-farmer/src/plotter/gpu/cuda.rs index ffdc9aab7d..813d0e3e9d 100644 --- a/crates/subspace-farmer/src/plotter/gpu/cuda.rs +++ b/crates/subspace-farmer/src/plotter/gpu/cuda.rs @@ -6,7 +6,9 @@ use parking_lot::Mutex; use rayon::{ThreadPool, ThreadPoolBuildError, ThreadPoolBuilder}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; -use subspace_core_primitives::{HistorySize, PieceOffset, Record, SectorId}; +use subspace_core_primitives::pieces::{PieceOffset, Record}; +use subspace_core_primitives::segments::HistorySize; +use subspace_core_primitives::SectorId; use subspace_farmer_components::plotting::RecordsEncoder; use subspace_farmer_components::sector::SectorContentsMap; use subspace_proof_of_space_gpu::cuda::CudaDevice; diff --git a/crates/subspace-farmer/src/single_disk_farm.rs b/crates/subspace-farmer/src/single_disk_farm.rs index 57a8617182..2cf3ae818e 100644 --- a/crates/subspace-farmer/src/single_disk_farm.rs +++ b/crates/subspace-farmer/src/single_disk_farm.rs @@ -68,9 +68,9 @@ use std::time::{Duration, Instant}; use std::{fmt, fs, io, mem}; use subspace_core_primitives::crypto::kzg::Kzg; use subspace_core_primitives::crypto::{blake3_hash, Scalar}; -use subspace_core_primitives::{ - Blake3Hash, HistorySize, PublicKey, Record, SectorIndex, SegmentIndex, -}; +use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::segments::{HistorySize, SegmentIndex}; +use subspace_core_primitives::{Blake3Hash, PublicKey, SectorIndex}; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::file_ext::FileExt; use subspace_farmer_components::reading::ReadSectorRecordChunksMode; diff --git a/crates/subspace-farmer/src/single_disk_farm/farming.rs b/crates/subspace-farmer/src/single_disk_farm/farming.rs index c22f6f646a..b184b8cf28 100644 --- a/crates/subspace-farmer/src/single_disk_farm/farming.rs +++ b/crates/subspace-farmer/src/single_disk_farm/farming.rs @@ -20,9 +20,9 @@ use std::collections::HashSet; use std::sync::Arc; use std::time::Instant; use subspace_core_primitives::crypto::kzg::Kzg; -use subspace_core_primitives::{ - HistorySize, PosSeed, PublicKey, Record, SectorIndex, SegmentIndex, Solution, SolutionRange, -}; +use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::segments::{HistorySize, SegmentIndex}; +use subspace_core_primitives::{PosSeed, PublicKey, SectorIndex, Solution, SolutionRange}; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::auditing::{audit_plot_sync, AuditingError}; use subspace_farmer_components::proving::{ProvableSolutions, ProvingError}; diff --git a/crates/subspace-farmer/src/single_disk_farm/piece_cache.rs b/crates/subspace-farmer/src/single_disk_farm/piece_cache.rs index fb1d82434f..9a3d243b1b 100644 --- a/crates/subspace-farmer/src/single_disk_farm/piece_cache.rs +++ b/crates/subspace-farmer/src/single_disk_farm/piece_cache.rs @@ -5,7 +5,7 @@ use crate::farm; use crate::farm::{FarmError, PieceCacheId, PieceCacheOffset}; use async_trait::async_trait; use futures::{stream, Stream}; -use subspace_core_primitives::{Piece, PieceIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; /// Dedicated piece cache stored on one disk, is used both to accelerate DSN queries and to plot /// faster diff --git a/crates/subspace-farmer/src/single_disk_farm/piece_reader.rs b/crates/subspace-farmer/src/single_disk_farm/piece_reader.rs index 80ff2cb834..56ace08444 100644 --- a/crates/subspace-farmer/src/single_disk_farm/piece_reader.rs +++ b/crates/subspace-farmer/src/single_disk_farm/piece_reader.rs @@ -9,7 +9,8 @@ use futures::{SinkExt, StreamExt}; use std::collections::HashSet; use std::future::Future; use std::sync::Arc; -use subspace_core_primitives::{Piece, PieceOffset, PublicKey, SectorId, SectorIndex}; +use subspace_core_primitives::pieces::{Piece, PieceOffset}; +use subspace_core_primitives::{PublicKey, SectorId, SectorIndex}; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::reading::ReadSectorRecordChunksMode; use subspace_farmer_components::sector::{sector_size, SectorMetadataChecksummed}; diff --git a/crates/subspace-farmer/src/single_disk_farm/plot_cache.rs b/crates/subspace-farmer/src/single_disk_farm/plot_cache.rs index 2ed66e02dc..757d9dd434 100644 --- a/crates/subspace-farmer/src/single_disk_farm/plot_cache.rs +++ b/crates/subspace-farmer/src/single_disk_farm/plot_cache.rs @@ -14,7 +14,8 @@ use std::collections::HashMap; use std::sync::{Arc, Weak}; use std::{io, mem}; use subspace_core_primitives::crypto::blake3_hash_list; -use subspace_core_primitives::{Blake3Hash, Piece, PieceIndex, SectorIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; +use subspace_core_primitives::{Blake3Hash, SectorIndex}; use subspace_farmer_components::file_ext::FileExt; use subspace_farmer_components::sector::SectorMetadataChecksummed; use subspace_networking::libp2p::kad::RecordKey; diff --git a/crates/subspace-farmer/src/single_disk_farm/plot_cache/tests.rs b/crates/subspace-farmer/src/single_disk_farm/plot_cache/tests.rs index d3b12d7e18..f907235b58 100644 --- a/crates/subspace-farmer/src/single_disk_farm/plot_cache/tests.rs +++ b/crates/subspace-farmer/src/single_disk_farm/plot_cache/tests.rs @@ -11,7 +11,9 @@ use rand::prelude::*; use std::assert_matches::assert_matches; use std::num::NonZeroU64; use std::sync::Arc; -use subspace_core_primitives::{HistorySize, Piece, PieceIndex, Record, SectorIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex, Record}; +use subspace_core_primitives::segments::HistorySize; +use subspace_core_primitives::SectorIndex; use subspace_farmer_components::file_ext::FileExt; use subspace_farmer_components::sector::{SectorMetadata, SectorMetadataChecksummed}; use subspace_networking::libp2p::kad::RecordKey; diff --git a/crates/subspace-farmer/src/single_disk_farm/plotted_sectors.rs b/crates/subspace-farmer/src/single_disk_farm/plotted_sectors.rs index 618fb7f89c..9585a2ce3d 100644 --- a/crates/subspace-farmer/src/single_disk_farm/plotted_sectors.rs +++ b/crates/subspace-farmer/src/single_disk_farm/plotted_sectors.rs @@ -3,7 +3,8 @@ use async_lock::RwLock as AsyncRwLock; use async_trait::async_trait; use futures::{stream, Stream}; use std::sync::Arc; -use subspace_core_primitives::{PieceOffset, PublicKey, SectorId}; +use subspace_core_primitives::pieces::PieceOffset; +use subspace_core_primitives::{PublicKey, SectorId}; use subspace_farmer_components::plotting::PlottedSector; use subspace_farmer_components::sector::SectorMetadataChecksummed; use subspace_farmer_components::FarmerProtocolInfo; diff --git a/crates/subspace-farmer/src/single_disk_farm/plotting.rs b/crates/subspace-farmer/src/single_disk_farm/plotting.rs index 605e3164dc..3cee9a5ac3 100644 --- a/crates/subspace-farmer/src/single_disk_farm/plotting.rs +++ b/crates/subspace-farmer/src/single_disk_farm/plotting.rs @@ -18,10 +18,9 @@ use std::ops::Range; use std::pin::pin; use std::sync::Arc; use std::time::{Duration, Instant}; -use subspace_core_primitives::{ - Blake3Hash, HistorySize, PieceOffset, PublicKey, SectorId, SectorIndex, SegmentHeader, - SegmentIndex, -}; +use subspace_core_primitives::pieces::PieceOffset; +use subspace_core_primitives::segments::{HistorySize, SegmentIndex}; +use subspace_core_primitives::{Blake3Hash, PublicKey, SectorId, SectorIndex, SegmentHeader}; use subspace_farmer_components::file_ext::FileExt; use subspace_farmer_components::plotting::PlottedSector; use subspace_farmer_components::sector::SectorMetadataChecksummed; diff --git a/crates/subspace-networking/examples/benchmark.rs b/crates/subspace-networking/examples/benchmark.rs index 850c7dab41..4b28620176 100644 --- a/crates/subspace-networking/examples/benchmark.rs +++ b/crates/subspace-networking/examples/benchmark.rs @@ -19,7 +19,7 @@ use std::error::Error; use std::sync::atomic::{AtomicU32, Ordering}; use std::sync::Arc; use std::time::{Duration, Instant}; -use subspace_core_primitives::{Piece, PieceIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; use subspace_networking::utils::piece_provider::{NoPieceValidator, PieceProvider, PieceValidator}; use subspace_networking::{Config, Node, PieceByIndexRequestHandler}; use tokio::sync::Semaphore; diff --git a/crates/subspace-networking/examples/random-walker.rs b/crates/subspace-networking/examples/random-walker.rs index 30ec25e3de..ff829853ff 100644 --- a/crates/subspace-networking/examples/random-walker.rs +++ b/crates/subspace-networking/examples/random-walker.rs @@ -15,7 +15,7 @@ use parking_lot::Mutex; use std::collections::HashMap; use std::sync::Arc; use std::time::{Duration, Instant}; -use subspace_core_primitives::PieceIndex; +use subspace_core_primitives::pieces::PieceIndex; use subspace_networking::{ Config, Multihash, Node, PeerDiscovered, PieceByIndexRequest, PieceByIndexRequestHandler, PieceByIndexResponse, SendRequestError, diff --git a/crates/subspace-networking/src/constructor.rs b/crates/subspace-networking/src/constructor.rs index 76d1ffc7eb..66b91b052f 100644 --- a/crates/subspace-networking/src/constructor.rs +++ b/crates/subspace-networking/src/constructor.rs @@ -38,7 +38,8 @@ use std::iter::Empty; use std::sync::Arc; use std::time::{Duration, Instant}; use std::{fmt, io, iter}; -use subspace_core_primitives::{crypto, Piece}; +use subspace_core_primitives::crypto; +use subspace_core_primitives::pieces::Piece; use thiserror::Error; use tracing::{debug, error, info}; diff --git a/crates/subspace-networking/src/protocols/request_response/handlers/piece_by_index.rs b/crates/subspace-networking/src/protocols/request_response/handlers/piece_by_index.rs index 38f330b92f..6580574daa 100644 --- a/crates/subspace-networking/src/protocols/request_response/handlers/piece_by_index.rs +++ b/crates/subspace-networking/src/protocols/request_response/handlers/piece_by_index.rs @@ -5,7 +5,7 @@ use super::generic_request_handler::{GenericRequest, GenericRequestHandler}; use parity_scale_codec::{Decode, Encode}; -use subspace_core_primitives::{Piece, PieceIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; /// Piece-by-hash protocol request. #[derive(Debug, Clone, Copy, Eq, PartialEq, Encode, Decode)] diff --git a/crates/subspace-networking/src/protocols/request_response/handlers/segment_header.rs b/crates/subspace-networking/src/protocols/request_response/handlers/segment_header.rs index a06e5eccd0..0fb829b969 100644 --- a/crates/subspace-networking/src/protocols/request_response/handlers/segment_header.rs +++ b/crates/subspace-networking/src/protocols/request_response/handlers/segment_header.rs @@ -5,7 +5,8 @@ use super::generic_request_handler::{GenericRequest, GenericRequestHandler}; use parity_scale_codec::{Decode, Encode}; -use subspace_core_primitives::{SegmentHeader, SegmentIndex}; +use subspace_core_primitives::segments::SegmentIndex; +use subspace_core_primitives::SegmentHeader; /// Segment header by segment indexes protocol request. #[derive(Debug, Clone, Eq, PartialEq, Encode, Decode)] diff --git a/crates/subspace-networking/src/utils/multihash.rs b/crates/subspace-networking/src/utils/multihash.rs index f342444c3d..7315b53478 100644 --- a/crates/subspace-networking/src/utils/multihash.rs +++ b/crates/subspace-networking/src/utils/multihash.rs @@ -1,7 +1,7 @@ //! Defines multihash codes for Subspace DSN. use std::error::Error; -use subspace_core_primitives::PieceIndex; +use subspace_core_primitives::pieces::PieceIndex; /// Type alias for libp2p Multihash. Constant 64 was copied from libp2p protocols. pub type Multihash = libp2p::multihash::Multihash<64>; diff --git a/crates/subspace-networking/src/utils/piece_provider.rs b/crates/subspace-networking/src/utils/piece_provider.rs index 849b58b451..06679a51d6 100644 --- a/crates/subspace-networking/src/utils/piece_provider.rs +++ b/crates/subspace-networking/src/utils/piece_provider.rs @@ -8,7 +8,7 @@ use libp2p::kad::RecordKey; use libp2p::PeerId; use std::collections::HashSet; use std::fmt; -use subspace_core_primitives::{Piece, PieceIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; use tracing::{debug, trace, warn}; /// Validates piece against using its commitment. diff --git a/crates/subspace-runtime/src/lib.rs b/crates/subspace-runtime/src/lib.rs index e7f54eefaa..9cf91d1654 100644 --- a/crates/subspace-runtime/src/lib.rs +++ b/crates/subspace-runtime/src/lib.rs @@ -99,9 +99,11 @@ use sp_subspace_mmr::ConsensusChainMmrLeafProof; use sp_version::RuntimeVersion; use static_assertions::const_assert; use subspace_core_primitives::objects::BlockObjectMapping; +use subspace_core_primitives::pieces::Piece; +use subspace_core_primitives::segments::{HistorySize, SegmentCommitment, SegmentIndex}; use subspace_core_primitives::{ - pieces_to_solution_range, solution_range_to_pieces, HistorySize, Piece, PublicKey, Randomness, - SegmentCommitment, SegmentHeader, SegmentIndex, SlotNumber, SolutionRange, U256, + pieces_to_solution_range, solution_range_to_pieces, PublicKey, Randomness, SegmentHeader, + SlotNumber, SolutionRange, U256, }; use subspace_runtime_primitives::{ maximum_normal_block_length, AccountId, Balance, BlockNumber, FindBlockRewardAddress, Hash, diff --git a/crates/subspace-service/src/lib.rs b/crates/subspace-service/src/lib.rs index 89565a28c2..eed719ac56 100644 --- a/crates/subspace-service/src/lib.rs +++ b/crates/subspace-service/src/lib.rs @@ -126,7 +126,8 @@ use std::num::NonZeroUsize; use std::sync::Arc; use std::time::Duration; use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg}; -use subspace_core_primitives::{BlockNumber, PotSeed, PublicKey, Record, REWARD_SIGNING_CONTEXT}; +use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::{BlockNumber, PotSeed, PublicKey, REWARD_SIGNING_CONTEXT}; use subspace_erasure_coding::ErasureCoding; use subspace_networking::libp2p::multiaddr::Protocol; use subspace_networking::utils::piece_provider::PieceProvider; diff --git a/crates/subspace-service/src/sync_from_dsn.rs b/crates/subspace-service/src/sync_from_dsn.rs index 2bf44e85b9..193fd120af 100644 --- a/crates/subspace-service/src/sync_from_dsn.rs +++ b/crates/subspace-service/src/sync_from_dsn.rs @@ -24,7 +24,9 @@ use std::future::Future; use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; use std::sync::Arc; use std::time::{Duration, Instant}; -use subspace_core_primitives::{Piece, PieceIndex, PublicKey, SegmentIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; +use subspace_core_primitives::segments::SegmentIndex; +use subspace_core_primitives::PublicKey; use subspace_erasure_coding::ErasureCoding; use subspace_networking::utils::piece_provider::{PieceProvider, PieceValidator}; use subspace_networking::Node; diff --git a/crates/subspace-service/src/sync_from_dsn/import_blocks.rs b/crates/subspace-service/src/sync_from_dsn/import_blocks.rs index 8d719ef4d3..fea5dfc551 100644 --- a/crates/subspace-service/src/sync_from_dsn/import_blocks.rs +++ b/crates/subspace-service/src/sync_from_dsn/import_blocks.rs @@ -31,9 +31,11 @@ use sp_runtime::Saturating; use std::sync::{Arc, Mutex}; use std::time::Duration; use subspace_archiving::reconstructor::Reconstructor; -use subspace_core_primitives::{ - ArchivedHistorySegment, BlockNumber, Piece, RecordedHistorySegment, SegmentIndex, +use subspace_core_primitives::pieces::Piece; +use subspace_core_primitives::segments::{ + ArchivedHistorySegment, RecordedHistorySegment, SegmentIndex, }; +use subspace_core_primitives::BlockNumber; use subspace_erasure_coding::ErasureCoding; use subspace_networking::utils::multihash::ToMultihash; use tokio::sync::Semaphore; diff --git a/crates/subspace-service/src/sync_from_dsn/piece_validator.rs b/crates/subspace-service/src/sync_from_dsn/piece_validator.rs index 35ca09da2a..d19b7d61a0 100644 --- a/crates/subspace-service/src/sync_from_dsn/piece_validator.rs +++ b/crates/subspace-service/src/sync_from_dsn/piece_validator.rs @@ -3,7 +3,7 @@ use sc_client_api::AuxStore; use sc_consensus_subspace::archiver::SegmentHeadersStore; use subspace_archiving::archiver::is_piece_valid; use subspace_core_primitives::crypto::kzg::Kzg; -use subspace_core_primitives::{Piece, PieceIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; use subspace_networking::libp2p::PeerId; use subspace_networking::utils::piece_provider::PieceValidator; use subspace_networking::Node; diff --git a/crates/subspace-service/src/sync_from_dsn/segment_header_downloader.rs b/crates/subspace-service/src/sync_from_dsn/segment_header_downloader.rs index 09831fdcdb..5ada1c44f4 100644 --- a/crates/subspace-service/src/sync_from_dsn/segment_header_downloader.rs +++ b/crates/subspace-service/src/sync_from_dsn/segment_header_downloader.rs @@ -3,7 +3,8 @@ use futures::StreamExt; use std::collections::{BTreeSet, HashMap}; use std::error::Error; use std::pin::pin; -use subspace_core_primitives::{SegmentHeader, SegmentIndex}; +use subspace_core_primitives::segments::SegmentIndex; +use subspace_core_primitives::SegmentHeader; use subspace_networking::libp2p::PeerId; use subspace_networking::{Node, SegmentHeaderRequest, SegmentHeaderResponse}; use tracing::{debug, error, trace, warn}; diff --git a/crates/subspace-service/src/sync_from_dsn/snap_sync.rs b/crates/subspace-service/src/sync_from_dsn/snap_sync.rs index 2ff976b07c..2b18b875d3 100644 --- a/crates/subspace-service/src/sync_from_dsn/snap_sync.rs +++ b/crates/subspace-service/src/sync_from_dsn/snap_sync.rs @@ -24,7 +24,8 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; use std::time::Duration; use subspace_archiving::reconstructor::Reconstructor; -use subspace_core_primitives::{BlockNumber, PublicKey, SegmentIndex}; +use subspace_core_primitives::segments::SegmentIndex; +use subspace_core_primitives::{BlockNumber, PublicKey}; use subspace_erasure_coding::ErasureCoding; use subspace_networking::Node; use tokio::time::sleep; diff --git a/crates/subspace-verification/src/lib.rs b/crates/subspace-verification/src/lib.rs index 7f4cb31e24..beac5d7a56 100644 --- a/crates/subspace-verification/src/lib.rs +++ b/crates/subspace-verification/src/lib.rs @@ -30,10 +30,11 @@ use subspace_core_primitives::crypto::kzg::{Commitment, Kzg, Witness}; use subspace_core_primitives::crypto::{ blake3_254_hash_to_scalar, blake3_hash_list, blake3_hash_with_key, Scalar, }; +use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::segments::{HistorySize, SegmentCommitment}; use subspace_core_primitives::{ - Blake3Hash, BlockNumber, BlockWeight, HistorySize, PotOutput, PublicKey, Record, - RewardSignature, SectorId, SectorSlotChallenge, SegmentCommitment, SlotNumber, Solution, - SolutionRange, + Blake3Hash, BlockNumber, BlockWeight, PotOutput, PublicKey, RewardSignature, SectorId, + SectorSlotChallenge, SlotNumber, Solution, SolutionRange, }; use subspace_proof_of_space::Table; diff --git a/shared/subspace-data-retrieval/src/object_fetcher.rs b/shared/subspace-data-retrieval/src/object_fetcher.rs index 26b67d95fa..9a1feed12a 100644 --- a/shared/subspace-data-retrieval/src/object_fetcher.rs +++ b/shared/subspace-data-retrieval/src/object_fetcher.rs @@ -21,9 +21,8 @@ use crate::segment_fetcher::{download_segment, SegmentGetterError}; use parity_scale_codec::{Compact, CompactLen, Decode, Encode}; use std::sync::Arc; use subspace_archiving::archiver::{Segment, SegmentItem}; -use subspace_core_primitives::{ - Piece, PieceIndex, RawRecord, RecordedHistorySegment, SegmentIndex, -}; +use subspace_core_primitives::pieces::{Piece, PieceIndex, RawRecord}; +use subspace_core_primitives::segments::{RecordedHistorySegment, SegmentIndex}; use subspace_erasure_coding::ErasureCoding; use tracing::{debug, trace}; diff --git a/shared/subspace-data-retrieval/src/piece_fetcher.rs b/shared/subspace-data-retrieval/src/piece_fetcher.rs index acd9829cd3..1984af93e0 100644 --- a/shared/subspace-data-retrieval/src/piece_fetcher.rs +++ b/shared/subspace-data-retrieval/src/piece_fetcher.rs @@ -19,7 +19,7 @@ use crate::object_fetcher::Error; use crate::piece_getter::{BoxError, ObjectPieceGetter}; use futures::stream::FuturesOrdered; use futures::TryStreamExt; -use subspace_core_primitives::{Piece, PieceIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; use tracing::{debug, trace}; /// Concurrently downloads the exact pieces in `piece_indexes`, returning them in that order. diff --git a/shared/subspace-data-retrieval/src/piece_getter.rs b/shared/subspace-data-retrieval/src/piece_getter.rs index 7f0fa894ba..70f470de70 100644 --- a/shared/subspace-data-retrieval/src/piece_getter.rs +++ b/shared/subspace-data-retrieval/src/piece_getter.rs @@ -19,7 +19,7 @@ use async_trait::async_trait; use std::fmt; use std::sync::Arc; use subspace_archiving::archiver::NewArchivedSegment; -use subspace_core_primitives::{Piece, PieceIndex}; +use subspace_core_primitives::pieces::{Piece, PieceIndex}; /// A type-erased error pub type BoxError = Box; diff --git a/shared/subspace-data-retrieval/src/segment_fetcher.rs b/shared/subspace-data-retrieval/src/segment_fetcher.rs index 99b28aa2ec..686f5e7290 100644 --- a/shared/subspace-data-retrieval/src/segment_fetcher.rs +++ b/shared/subspace-data-retrieval/src/segment_fetcher.rs @@ -21,8 +21,9 @@ use futures::stream::FuturesUnordered; use futures::StreamExt; use subspace_archiving::archiver::Segment; use subspace_archiving::reconstructor::{Reconstructor, ReconstructorError}; -use subspace_core_primitives::{ - ArchivedHistorySegment, Piece, RecordedHistorySegment, SegmentIndex, +use subspace_core_primitives::pieces::Piece; +use subspace_core_primitives::segments::{ + ArchivedHistorySegment, RecordedHistorySegment, SegmentIndex, }; use subspace_erasure_coding::ErasureCoding; use tokio::task::spawn_blocking; diff --git a/shared/subspace-proof-of-space-gpu/src/cuda.rs b/shared/subspace-proof-of-space-gpu/src/cuda.rs index b61aa4014f..3441244644 100644 --- a/shared/subspace-proof-of-space-gpu/src/cuda.rs +++ b/shared/subspace-proof-of-space-gpu/src/cuda.rs @@ -8,7 +8,8 @@ mod tests; use rust_kzg_blst::types::fr::FsFr; use std::ops::DerefMut; use subspace_core_primitives::crypto::Scalar; -use subspace_core_primitives::{PosProof, PosSeed, Record}; +use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::{PosProof, PosSeed}; extern "C" { /// # Returns diff --git a/shared/subspace-proof-of-space-gpu/src/cuda/tests.rs b/shared/subspace-proof-of-space-gpu/src/cuda/tests.rs index e2aca30de0..727c72553b 100644 --- a/shared/subspace-proof-of-space-gpu/src/cuda/tests.rs +++ b/shared/subspace-proof-of-space-gpu/src/cuda/tests.rs @@ -2,7 +2,9 @@ use crate::cuda::cuda_devices; use std::num::NonZeroUsize; use std::slice; use subspace_core_primitives::crypto::{blake3_254_hash_to_scalar, blake3_hash}; -use subspace_core_primitives::{HistorySize, PieceOffset, Record, SectorId}; +use subspace_core_primitives::pieces::{PieceOffset, Record}; +use subspace_core_primitives::segments::HistorySize; +use subspace_core_primitives::SectorId; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::plotting::{CpuRecordsEncoder, RecordsEncoder}; use subspace_farmer_components::sector::SectorContentsMap; diff --git a/test/subspace-test-client/src/lib.rs b/test/subspace-test-client/src/lib.rs index 1af0990923..7c5674c412 100644 --- a/test/subspace-test-client/src/lib.rs +++ b/test/subspace-test-client/src/lib.rs @@ -36,9 +36,10 @@ use std::slice; use std::sync::Arc; use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg}; use subspace_core_primitives::objects::BlockObjectMapping; +use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::segments::{HistorySize, SegmentIndex}; use subspace_core_primitives::{ - HistorySize, PosSeed, PublicKey, Record, RewardSignature, SegmentIndex, Solution, - REWARD_SIGNING_CONTEXT, + PosSeed, PublicKey, RewardSignature, Solution, REWARD_SIGNING_CONTEXT, }; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::auditing::audit_sector_sync; diff --git a/test/subspace-test-runtime/src/lib.rs b/test/subspace-test-runtime/src/lib.rs index b19d6f4ed1..37dc69bedc 100644 --- a/test/subspace-test-runtime/src/lib.rs +++ b/test/subspace-test-runtime/src/lib.rs @@ -91,9 +91,10 @@ use sp_subspace_mmr::ConsensusChainMmrLeafProof; use sp_version::RuntimeVersion; use static_assertions::const_assert; use subspace_core_primitives::objects::{BlockObject, BlockObjectMapping}; +use subspace_core_primitives::pieces::Piece; +use subspace_core_primitives::segments::{HistorySize, SegmentCommitment, SegmentIndex}; use subspace_core_primitives::{ - crypto, HistorySize, Piece, PublicKey, Randomness, SegmentCommitment, SegmentHeader, - SegmentIndex, SlotNumber, SolutionRange, U256, + crypto, PublicKey, Randomness, SegmentHeader, SlotNumber, SolutionRange, U256, }; use subspace_runtime_primitives::{ AccountId, Balance, BlockNumber, FindBlockRewardAddress, Hash, HoldIdentifier, Moment, Nonce, From e1d65c4483a4105f9e0d71515d9e588e8f1247cb Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Wed, 2 Oct 2024 16:37:27 +0300 Subject: [PATCH 2/6] Move segment-related data structures into `segments` module --- crates/pallet-subspace/src/lib.rs | 12 +- crates/pallet-subspace/src/mock.rs | 8 +- crates/sc-consensus-subspace-rpc/src/lib.rs | 6 +- crates/sc-consensus-subspace/src/archiver.rs | 4 +- .../src/archiver/tests.rs | 5 +- .../sc-consensus-subspace/src/block_import.rs | 4 +- crates/sp-consensus-subspace/src/inherents.rs | 2 +- crates/sp-consensus-subspace/src/lib.rs | 6 +- crates/subspace-archiving/src/archiver.rs | 7 +- .../subspace-archiving/src/reconstructor.rs | 7 +- .../tests/integration/archiver.rs | 7 +- .../tests/integration/reconstructor.rs | 4 +- crates/subspace-core-primitives/src/lib.rs | 132 ----------------- .../subspace-core-primitives/src/segments.rs | 134 ++++++++++++++++++ crates/subspace-fake-runtime-api/src/lib.rs | 6 +- .../subspace-farmer/src/cluster/controller.rs | 3 +- crates/subspace-farmer/src/farmer_cache.rs | 3 +- .../subspace-farmer/src/farmer_cache/tests.rs | 5 +- crates/subspace-farmer/src/node_client.rs | 3 +- .../node_client/caching_proxy_node_client.rs | 3 +- .../src/node_client/rpc_node_client.rs | 3 +- .../src/single_disk_farm/plotting.rs | 4 +- .../handlers/segment_header.rs | 3 +- crates/subspace-runtime/src/lib.rs | 8 +- .../segment_header_downloader.rs | 3 +- test/subspace-test-runtime/src/lib.rs | 6 +- 26 files changed, 194 insertions(+), 194 deletions(-) diff --git a/crates/pallet-subspace/src/lib.rs b/crates/pallet-subspace/src/lib.rs index f2e7887fed..6771971165 100644 --- a/crates/pallet-subspace/src/lib.rs +++ b/crates/pallet-subspace/src/lib.rs @@ -59,10 +59,12 @@ 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::segments::{ArchivedHistorySegment, HistorySize, SegmentIndex}; +use subspace_core_primitives::segments::{ + ArchivedHistorySegment, HistorySize, SegmentHeader, SegmentIndex, +}; use subspace_core_primitives::{ - BlockHash, PublicKey, RewardSignature, SectorId, SectorIndex, SegmentHeader, SlotNumber, - SolutionRange, REWARD_SIGNING_CONTEXT, + BlockHash, PublicKey, RewardSignature, SectorId, SectorIndex, SlotNumber, SolutionRange, + REWARD_SIGNING_CONTEXT, }; use subspace_verification::{ check_reward_signature, derive_next_solution_range, derive_pot_entropy, PieceCheckParams, @@ -112,10 +114,10 @@ pub mod pallet { use sp_std::prelude::*; use subspace_core_primitives::crypto::Scalar; use subspace_core_primitives::pieces::PieceOffset; - use subspace_core_primitives::segments::{HistorySize, SegmentIndex}; + use subspace_core_primitives::segments::{HistorySize, SegmentHeader, SegmentIndex}; use subspace_core_primitives::{ Blake3Hash, PotCheckpoints, PublicKey, Randomness, RewardSignature, SectorIndex, - SegmentHeader, SolutionRange, + SolutionRange, }; pub(super) struct InitialSolutionRanges { diff --git a/crates/pallet-subspace/src/mock.rs b/crates/pallet-subspace/src/mock.rs index 6630bab153..9f509655d8 100644 --- a/crates/pallet-subspace/src/mock.rs +++ b/crates/pallet-subspace/src/mock.rs @@ -38,12 +38,12 @@ 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::segments::{ - ArchivedHistorySegment, HistorySize, RecordedHistorySegment, SegmentCommitment, SegmentIndex, + ArchivedBlockProgress, ArchivedHistorySegment, HistorySize, LastArchivedBlock, + RecordedHistorySegment, SegmentCommitment, SegmentHeader, SegmentIndex, }; use subspace_core_primitives::{ - ArchivedBlockProgress, Blake3Hash, BlockNumber, LastArchivedBlock, PosSeed, PotOutput, - PublicKey, RewardSignature, SectorId, SegmentHeader, SlotNumber, Solution, SolutionRange, - REWARD_SIGNING_CONTEXT, + Blake3Hash, BlockNumber, PosSeed, PotOutput, PublicKey, RewardSignature, SectorId, SlotNumber, + Solution, SolutionRange, REWARD_SIGNING_CONTEXT, }; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::auditing::audit_sector_sync; diff --git a/crates/sc-consensus-subspace-rpc/src/lib.rs b/crates/sc-consensus-subspace-rpc/src/lib.rs index 7122bf9b5e..1dc2b1fd47 100644 --- a/crates/sc-consensus-subspace-rpc/src/lib.rs +++ b/crates/sc-consensus-subspace-rpc/src/lib.rs @@ -56,10 +56,8 @@ 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, SegmentIndex}; -use subspace_core_primitives::{ - Blake3Hash, BlockHash, PublicKey, SegmentHeader, SlotNumber, Solution, -}; +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_networking::libp2p::Multiaddr; diff --git a/crates/sc-consensus-subspace/src/archiver.rs b/crates/sc-consensus-subspace/src/archiver.rs index bef268e9a2..0f46456d25 100644 --- a/crates/sc-consensus-subspace/src/archiver.rs +++ b/crates/sc-consensus-subspace/src/archiver.rs @@ -78,8 +78,8 @@ 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, SegmentIndex}; -use subspace_core_primitives::{BlockNumber, PublicKey, SegmentHeader}; +use subspace_core_primitives::segments::{RecordedHistorySegment, SegmentHeader, SegmentIndex}; +use subspace_core_primitives::{BlockNumber, PublicKey}; use subspace_erasure_coding::ErasureCoding; use tracing::{debug, info, trace, warn}; diff --git a/crates/sc-consensus-subspace/src/archiver/tests.rs b/crates/sc-consensus-subspace/src/archiver/tests.rs index ebeb43ee47..06304eb542 100644 --- a/crates/sc-consensus-subspace/src/archiver/tests.rs +++ b/crates/sc-consensus-subspace/src/archiver/tests.rs @@ -3,8 +3,9 @@ use parking_lot::RwLock; use sc_client_api::AuxStore; use std::collections::HashMap; use std::sync::Arc; -use subspace_core_primitives::segments::SegmentIndex; -use subspace_core_primitives::{ArchivedBlockProgress, LastArchivedBlock, SegmentHeader}; +use subspace_core_primitives::segments::{ + ArchivedBlockProgress, LastArchivedBlock, SegmentHeader, SegmentIndex, +}; struct MemAuxStore { store: RwLock, Vec>>, diff --git a/crates/sc-consensus-subspace/src/block_import.rs b/crates/sc-consensus-subspace/src/block_import.rs index 0623ef206d..72c441b187 100644 --- a/crates/sc-consensus-subspace/src/block_import.rs +++ b/crates/sc-consensus-subspace/src/block_import.rs @@ -52,8 +52,8 @@ use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor, One}; use sp_runtime::Justifications; use std::marker::PhantomData; use std::sync::Arc; -use subspace_core_primitives::segments::{HistorySize, SegmentIndex}; -use subspace_core_primitives::{BlockNumber, PublicKey, SectorId, SegmentHeader, SolutionRange}; +use subspace_core_primitives::segments::{HistorySize, SegmentHeader, SegmentIndex}; +use subspace_core_primitives::{BlockNumber, PublicKey, SectorId, SolutionRange}; use subspace_proof_of_space::Table; use subspace_verification::{calculate_block_weight, PieceCheckParams, VerifySolutionParams}; use tracing::warn; diff --git a/crates/sp-consensus-subspace/src/inherents.rs b/crates/sp-consensus-subspace/src/inherents.rs index 7a58aa0a82..402f3b4550 100644 --- a/crates/sp-consensus-subspace/src/inherents.rs +++ b/crates/sp-consensus-subspace/src/inherents.rs @@ -23,7 +23,7 @@ extern crate alloc; use alloc::vec::Vec; use codec::{Decode, Encode}; use sp_inherents::{Error, InherentData, InherentIdentifier, IsFatalError}; -use subspace_core_primitives::SegmentHeader; +use subspace_core_primitives::segments::SegmentHeader; /// The Subspace inherent identifier. pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"subspace"; diff --git a/crates/sp-consensus-subspace/src/lib.rs b/crates/sp-consensus-subspace/src/lib.rs index e94d9f7226..fbaff60a92 100644 --- a/crates/sp-consensus-subspace/src/lib.rs +++ b/crates/sp-consensus-subspace/src/lib.rs @@ -42,10 +42,12 @@ use sp_runtime_interface::{pass_by, runtime_interface}; use sp_std::num::NonZeroU32; #[cfg(feature = "std")] use subspace_core_primitives::crypto::kzg::Kzg; -use subspace_core_primitives::segments::{HistorySize, SegmentCommitment, SegmentIndex}; +use subspace_core_primitives::segments::{ + HistorySize, SegmentCommitment, SegmentHeader, SegmentIndex, +}; use subspace_core_primitives::{ Blake3Hash, BlockHash, BlockNumber, PotCheckpoints, PotOutput, PotSeed, PublicKey, - RewardSignature, SegmentHeader, SlotNumber, Solution, SolutionRange, + RewardSignature, SlotNumber, Solution, SolutionRange, }; #[cfg(feature = "std")] use subspace_proof_of_space::chia::ChiaTable; diff --git a/crates/subspace-archiving/src/archiver.rs b/crates/subspace-archiving/src/archiver.rs index b7b2aab826..b6ecd5411b 100644 --- a/crates/subspace-archiving/src/archiver.rs +++ b/crates/subspace-archiving/src/archiver.rs @@ -36,11 +36,10 @@ use subspace_core_primitives::objects::{ }; use subspace_core_primitives::pieces::{PieceArray, RawRecord, RecordWitness}; use subspace_core_primitives::segments::{ - ArchivedHistorySegment, RecordedHistorySegment, SegmentCommitment, SegmentIndex, -}; -use subspace_core_primitives::{ - ArchivedBlockProgress, Blake3Hash, BlockNumber, LastArchivedBlock, SegmentHeader, + ArchivedBlockProgress, ArchivedHistorySegment, LastArchivedBlock, RecordedHistorySegment, + SegmentCommitment, SegmentHeader, SegmentIndex, }; +use subspace_core_primitives::{Blake3Hash, BlockNumber}; use subspace_erasure_coding::ErasureCoding; const INITIAL_LAST_ARCHIVED_BLOCK: LastArchivedBlock = LastArchivedBlock { diff --git a/crates/subspace-archiving/src/reconstructor.rs b/crates/subspace-archiving/src/reconstructor.rs index db3e0b0f10..c20da90caf 100644 --- a/crates/subspace-archiving/src/reconstructor.rs +++ b/crates/subspace-archiving/src/reconstructor.rs @@ -11,11 +11,10 @@ use parity_scale_codec::Decode; use subspace_core_primitives::crypto::Scalar; use subspace_core_primitives::pieces::{Piece, RawRecord}; use subspace_core_primitives::segments::{ - ArchivedHistorySegment, RecordedHistorySegment, SegmentIndex, -}; -use subspace_core_primitives::{ - ArchivedBlockProgress, BlockNumber, LastArchivedBlock, SegmentHeader, + ArchivedBlockProgress, ArchivedHistorySegment, LastArchivedBlock, RecordedHistorySegment, + SegmentHeader, SegmentIndex, }; +use subspace_core_primitives::BlockNumber; use subspace_erasure_coding::ErasureCoding; /// Reconstructor-related instantiation error diff --git a/crates/subspace-archiving/tests/integration/archiver.rs b/crates/subspace-archiving/tests/integration/archiver.rs index a94935a3b5..5998da9104 100644 --- a/crates/subspace-archiving/tests/integration/archiver.rs +++ b/crates/subspace-archiving/tests/integration/archiver.rs @@ -12,11 +12,10 @@ use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg}; use subspace_core_primitives::objects::{BlockObject, BlockObjectMapping, PieceObject}; use subspace_core_primitives::pieces::{Piece, Record}; use subspace_core_primitives::segments::{ - ArchivedHistorySegment, RecordedHistorySegment, SegmentCommitment, SegmentIndex, -}; -use subspace_core_primitives::{ - ArchivedBlockProgress, Blake3Hash, LastArchivedBlock, SegmentHeader, + ArchivedBlockProgress, ArchivedHistorySegment, LastArchivedBlock, RecordedHistorySegment, + SegmentCommitment, SegmentHeader, SegmentIndex, }; +use subspace_core_primitives::Blake3Hash; use subspace_erasure_coding::ErasureCoding; fn extract_data>(data: &[u8], offset: O) -> &[u8] { diff --git a/crates/subspace-archiving/tests/integration/reconstructor.rs b/crates/subspace-archiving/tests/integration/reconstructor.rs index e1ff9f6e15..054d0770ca 100644 --- a/crates/subspace-archiving/tests/integration/reconstructor.rs +++ b/crates/subspace-archiving/tests/integration/reconstructor.rs @@ -8,9 +8,9 @@ use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg}; use subspace_core_primitives::objects::BlockObjectMapping; use subspace_core_primitives::pieces::{FlatPieces, Piece, Record}; use subspace_core_primitives::segments::{ - ArchivedHistorySegment, RecordedHistorySegment, SegmentIndex, + ArchivedBlockProgress, ArchivedHistorySegment, LastArchivedBlock, RecordedHistorySegment, + SegmentIndex, }; -use subspace_core_primitives::{ArchivedBlockProgress, LastArchivedBlock}; use subspace_erasure_coding::ErasureCoding; fn pieces_to_option_of_pieces(pieces: &FlatPieces) -> Vec> { diff --git a/crates/subspace-core-primitives/src/lib.rs b/crates/subspace-core-primitives/src/lib.rs index fa8e91dbbf..9fd3f06aec 100644 --- a/crates/subspace-core-primitives/src/lib.rs +++ b/crates/subspace-core-primitives/src/lib.rs @@ -567,138 +567,6 @@ impl RewardSignature { pub const SIZE: usize = 64; } -/// Progress of an archived block. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] -pub enum ArchivedBlockProgress { - /// The block has been fully archived. - Complete, - - /// Number of partially archived bytes of a block. - Partial(u32), -} - -impl Default for ArchivedBlockProgress { - /// We assume a block can always fit into the segment initially, but it can definitely possible - /// to be transitioned into the partial state after some overflow checkings. - #[inline] - fn default() -> Self { - Self::Complete - } -} - -impl ArchivedBlockProgress { - /// Return the number of partially archived bytes if the progress is not complete. - pub fn partial(&self) -> Option { - match self { - Self::Complete => None, - Self::Partial(number) => Some(*number), - } - } - - /// Sets new number of partially archived bytes. - pub fn set_partial(&mut self, new_partial: u32) { - *self = Self::Partial(new_partial); - } -} - -/// Last archived block -#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] -pub struct LastArchivedBlock { - /// Block number - pub number: BlockNumber, - /// Progress of an archived block. - pub archived_progress: ArchivedBlockProgress, -} - -impl LastArchivedBlock { - /// Returns the number of partially archived bytes for a block. - pub fn partial_archived(&self) -> Option { - self.archived_progress.partial() - } - - /// Sets new number of partially archived bytes. - pub fn set_partial_archived(&mut self, new_partial: BlockNumber) { - self.archived_progress.set_partial(new_partial); - } - - /// Sets the archived state of this block to [`ArchivedBlockProgress::Complete`]. - pub fn set_complete(&mut self) { - self.archived_progress = ArchivedBlockProgress::Complete; - } -} - -/// Segment header for a specific segment. -/// -/// Each segment will have corresponding [`SegmentHeader`] included as the first item in the next -/// segment. Each `SegmentHeader` includes hash of the previous one and all together form a chain of -/// segment headers that is used for quick and efficient verification that some [`Piece`] -/// corresponds to the actual archival history of the blockchain. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode, TypeInfo, Hash)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] -pub enum SegmentHeader { - /// V0 of the segment header data structure - #[codec(index = 0)] - #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] - V0 { - /// Segment index - segment_index: SegmentIndex, - /// Root of commitments of all records in a segment. - segment_commitment: SegmentCommitment, - /// Hash of the segment header of the previous segment - prev_segment_header_hash: Blake3Hash, - /// Last archived block - last_archived_block: LastArchivedBlock, - }, -} - -impl SegmentHeader { - /// Hash of the whole segment header - pub fn hash(&self) -> Blake3Hash { - blake3_hash(&self.encode()) - } - - /// Segment index - pub fn segment_index(&self) -> SegmentIndex { - match self { - Self::V0 { segment_index, .. } => *segment_index, - } - } - - /// Segment commitment of the records in a segment. - pub fn segment_commitment(&self) -> SegmentCommitment { - match self { - Self::V0 { - segment_commitment, .. - } => *segment_commitment, - } - } - - /// Hash of the segment header of the previous segment - pub fn prev_segment_header_hash(&self) -> Blake3Hash { - match self { - Self::V0 { - prev_segment_header_hash, - .. - } => *prev_segment_header_hash, - } - } - - /// Last archived block - pub fn last_archived_block(&self) -> LastArchivedBlock { - match self { - Self::V0 { - last_archived_block, - .. - } => *last_archived_block, - } - } -} - /// Sector index in consensus pub type SectorIndex = u16; diff --git a/crates/subspace-core-primitives/src/segments.rs b/crates/subspace-core-primitives/src/segments.rs index c32c1c4cab..406ed31bb3 100644 --- a/crates/subspace-core-primitives/src/segments.rs +++ b/crates/subspace-core-primitives/src/segments.rs @@ -1,7 +1,9 @@ //! Segments-related data structures. +use crate::crypto::blake3_hash; use crate::crypto::kzg::Commitment; use crate::pieces::{FlatPieces, Piece, PieceIndex, RawRecord}; +use crate::{Blake3Hash, BlockNumber}; #[cfg(not(feature = "std"))] use alloc::boxed::Box; #[cfg(not(feature = "std"))] @@ -269,6 +271,138 @@ impl HistorySize { } } +/// Progress of an archived block. +#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +pub enum ArchivedBlockProgress { + /// The block has been fully archived. + Complete, + + /// Number of partially archived bytes of a block. + Partial(u32), +} + +impl Default for ArchivedBlockProgress { + /// We assume a block can always fit into the segment initially, but it is definitely possible + /// to be transitioned into the partial state after some overflow checking. + #[inline] + fn default() -> Self { + Self::Complete + } +} + +impl ArchivedBlockProgress { + /// Return the number of partially archived bytes if the progress is not complete. + pub fn partial(&self) -> Option { + match self { + Self::Complete => None, + Self::Partial(number) => Some(*number), + } + } + + /// Sets new number of partially archived bytes. + pub fn set_partial(&mut self, new_partial: u32) { + *self = Self::Partial(new_partial); + } +} + +/// Last archived block +#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +pub struct LastArchivedBlock { + /// Block number + pub number: BlockNumber, + /// Progress of an archived block. + pub archived_progress: ArchivedBlockProgress, +} + +impl LastArchivedBlock { + /// Returns the number of partially archived bytes for a block. + pub fn partial_archived(&self) -> Option { + self.archived_progress.partial() + } + + /// Sets new number of partially archived bytes. + pub fn set_partial_archived(&mut self, new_partial: BlockNumber) { + self.archived_progress.set_partial(new_partial); + } + + /// Sets the archived state of this block to [`ArchivedBlockProgress::Complete`]. + pub fn set_complete(&mut self) { + self.archived_progress = ArchivedBlockProgress::Complete; + } +} + +/// Segment header for a specific segment. +/// +/// Each segment will have corresponding [`SegmentHeader`] included as the first item in the next +/// segment. Each `SegmentHeader` includes hash of the previous one and all together form a chain of +/// segment headers that is used for quick and efficient verification that some [`Piece`] +/// corresponds to the actual archival history of the blockchain. +#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode, TypeInfo, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] +pub enum SegmentHeader { + /// V0 of the segment header data structure + #[codec(index = 0)] + #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] + V0 { + /// Segment index + segment_index: SegmentIndex, + /// Root of commitments of all records in a segment. + segment_commitment: SegmentCommitment, + /// Hash of the segment header of the previous segment + prev_segment_header_hash: Blake3Hash, + /// Last archived block + last_archived_block: LastArchivedBlock, + }, +} + +impl SegmentHeader { + /// Hash of the whole segment header + pub fn hash(&self) -> Blake3Hash { + blake3_hash(&self.encode()) + } + + /// Segment index + pub fn segment_index(&self) -> SegmentIndex { + match self { + Self::V0 { segment_index, .. } => *segment_index, + } + } + + /// Segment commitment of the records in a segment. + pub fn segment_commitment(&self) -> SegmentCommitment { + match self { + Self::V0 { + segment_commitment, .. + } => *segment_commitment, + } + } + + /// Hash of the segment header of the previous segment + pub fn prev_segment_header_hash(&self) -> Blake3Hash { + match self { + Self::V0 { + prev_segment_header_hash, + .. + } => *prev_segment_header_hash, + } + } + + /// Last archived block + pub fn last_archived_block(&self) -> LastArchivedBlock { + match self { + Self::V0 { + last_archived_block, + .. + } => *last_archived_block, + } + } +} + /// Recorded history segment before archiving is applied. /// /// NOTE: This is a stack-allocated data structure and can cause stack overflow! diff --git a/crates/subspace-fake-runtime-api/src/lib.rs b/crates/subspace-fake-runtime-api/src/lib.rs index 5900d8464f..3cb36c652a 100644 --- a/crates/subspace-fake-runtime-api/src/lib.rs +++ b/crates/subspace-fake-runtime-api/src/lib.rs @@ -40,8 +40,10 @@ use sp_version::RuntimeVersion; use std::collections::btree_map::BTreeMap; use std::collections::btree_set::BTreeSet; use subspace_core_primitives::objects::BlockObjectMapping; -use subspace_core_primitives::segments::{HistorySize, SegmentCommitment, SegmentIndex}; -use subspace_core_primitives::{PublicKey, Randomness, SegmentHeader, U256}; +use subspace_core_primitives::segments::{ + HistorySize, SegmentCommitment, SegmentHeader, SegmentIndex, +}; +use subspace_core_primitives::{PublicKey, Randomness, U256}; use subspace_runtime_primitives::opaque::Block; use subspace_runtime_primitives::{AccountId, Balance, BlockNumber, Moment, Nonce}; diff --git a/crates/subspace-farmer/src/cluster/controller.rs b/crates/subspace-farmer/src/cluster/controller.rs index 8827779914..4bfd786657 100644 --- a/crates/subspace-farmer/src/cluster/controller.rs +++ b/crates/subspace-farmer/src/cluster/controller.rs @@ -25,8 +25,7 @@ use std::num::NonZeroUsize; use std::pin::Pin; use std::sync::Arc; use subspace_core_primitives::pieces::{Piece, PieceIndex}; -use subspace_core_primitives::segments::SegmentIndex; -use subspace_core_primitives::SegmentHeader; +use subspace_core_primitives::segments::{SegmentHeader, SegmentIndex}; use subspace_farmer_components::PieceGetter; use subspace_rpc_primitives::{ FarmerAppInfo, RewardSignatureResponse, RewardSigningInfo, SlotInfo, SolutionResponse, diff --git a/crates/subspace-farmer/src/farmer_cache.rs b/crates/subspace-farmer/src/farmer_cache.rs index d24de3f490..8d56ceee0f 100644 --- a/crates/subspace-farmer/src/farmer_cache.rs +++ b/crates/subspace-farmer/src/farmer_cache.rs @@ -26,8 +26,7 @@ use std::sync::Arc; use std::time::Duration; use std::{fmt, mem}; use subspace_core_primitives::pieces::{Piece, PieceIndex}; -use subspace_core_primitives::segments::SegmentIndex; -use subspace_core_primitives::SegmentHeader; +use subspace_core_primitives::segments::{SegmentHeader, SegmentIndex}; use subspace_farmer_components::PieceGetter; use subspace_networking::libp2p::kad::{ProviderRecord, RecordKey}; use subspace_networking::libp2p::PeerId; diff --git a/crates/subspace-farmer/src/farmer_cache/tests.rs b/crates/subspace-farmer/src/farmer_cache/tests.rs index b2bcb8c426..9b964efd96 100644 --- a/crates/subspace-farmer/src/farmer_cache/tests.rs +++ b/crates/subspace-farmer/src/farmer_cache/tests.rs @@ -19,8 +19,9 @@ use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::Arc; use std::time::Duration; use subspace_core_primitives::pieces::{Piece, PieceIndex}; -use subspace_core_primitives::segments::{HistorySize, SegmentIndex}; -use subspace_core_primitives::{LastArchivedBlock, SegmentHeader}; +use subspace_core_primitives::segments::{ + HistorySize, LastArchivedBlock, SegmentHeader, SegmentIndex, +}; use subspace_farmer_components::{FarmerProtocolInfo, PieceGetter}; use subspace_networking::libp2p::identity; use subspace_networking::libp2p::kad::RecordKey; diff --git a/crates/subspace-farmer/src/node_client.rs b/crates/subspace-farmer/src/node_client.rs index 15f5299ee7..a68bd52f1f 100644 --- a/crates/subspace-farmer/src/node_client.rs +++ b/crates/subspace-farmer/src/node_client.rs @@ -16,8 +16,7 @@ use futures::Stream; use std::fmt; use std::pin::Pin; use subspace_core_primitives::pieces::{Piece, PieceIndex}; -use subspace_core_primitives::segments::SegmentIndex; -use subspace_core_primitives::SegmentHeader; +use subspace_core_primitives::segments::{SegmentHeader, SegmentIndex}; use subspace_rpc_primitives::{ FarmerAppInfo, RewardSignatureResponse, RewardSigningInfo, SlotInfo, SolutionResponse, }; diff --git a/crates/subspace-farmer/src/node_client/caching_proxy_node_client.rs b/crates/subspace-farmer/src/node_client/caching_proxy_node_client.rs index 6e9ab08a69..b9f4ab40a8 100644 --- a/crates/subspace-farmer/src/node_client/caching_proxy_node_client.rs +++ b/crates/subspace-farmer/src/node_client/caching_proxy_node_client.rs @@ -10,8 +10,7 @@ use std::pin::Pin; use std::sync::Arc; use std::time::{Duration, Instant}; use subspace_core_primitives::pieces::{Piece, PieceIndex}; -use subspace_core_primitives::segments::SegmentIndex; -use subspace_core_primitives::SegmentHeader; +use subspace_core_primitives::segments::{SegmentHeader, SegmentIndex}; use subspace_rpc_primitives::{ FarmerAppInfo, RewardSignatureResponse, RewardSigningInfo, SlotInfo, SolutionResponse, MAX_SEGMENT_HEADERS_PER_REQUEST, diff --git a/crates/subspace-farmer/src/node_client/rpc_node_client.rs b/crates/subspace-farmer/src/node_client/rpc_node_client.rs index 6a728961b0..b4eb430ee6 100644 --- a/crates/subspace-farmer/src/node_client/rpc_node_client.rs +++ b/crates/subspace-farmer/src/node_client/rpc_node_client.rs @@ -10,8 +10,7 @@ use jsonrpsee::ws_client::{WsClient, WsClientBuilder}; use std::pin::Pin; use std::sync::Arc; use subspace_core_primitives::pieces::{Piece, PieceIndex}; -use subspace_core_primitives::segments::SegmentIndex; -use subspace_core_primitives::SegmentHeader; +use subspace_core_primitives::segments::{SegmentHeader, SegmentIndex}; use subspace_rpc_primitives::{ FarmerAppInfo, RewardSignatureResponse, RewardSigningInfo, SlotInfo, SolutionResponse, }; diff --git a/crates/subspace-farmer/src/single_disk_farm/plotting.rs b/crates/subspace-farmer/src/single_disk_farm/plotting.rs index 3cee9a5ac3..faeef4f217 100644 --- a/crates/subspace-farmer/src/single_disk_farm/plotting.rs +++ b/crates/subspace-farmer/src/single_disk_farm/plotting.rs @@ -19,8 +19,8 @@ use std::pin::pin; use std::sync::Arc; use std::time::{Duration, Instant}; use subspace_core_primitives::pieces::PieceOffset; -use subspace_core_primitives::segments::{HistorySize, SegmentIndex}; -use subspace_core_primitives::{Blake3Hash, PublicKey, SectorId, SectorIndex, SegmentHeader}; +use subspace_core_primitives::segments::{HistorySize, SegmentHeader, SegmentIndex}; +use subspace_core_primitives::{Blake3Hash, PublicKey, SectorId, SectorIndex}; use subspace_farmer_components::file_ext::FileExt; use subspace_farmer_components::plotting::PlottedSector; use subspace_farmer_components::sector::SectorMetadataChecksummed; diff --git a/crates/subspace-networking/src/protocols/request_response/handlers/segment_header.rs b/crates/subspace-networking/src/protocols/request_response/handlers/segment_header.rs index 0fb829b969..717ba5333b 100644 --- a/crates/subspace-networking/src/protocols/request_response/handlers/segment_header.rs +++ b/crates/subspace-networking/src/protocols/request_response/handlers/segment_header.rs @@ -5,8 +5,7 @@ use super::generic_request_handler::{GenericRequest, GenericRequestHandler}; use parity_scale_codec::{Decode, Encode}; -use subspace_core_primitives::segments::SegmentIndex; -use subspace_core_primitives::SegmentHeader; +use subspace_core_primitives::segments::{SegmentHeader, SegmentIndex}; /// Segment header by segment indexes protocol request. #[derive(Debug, Clone, Eq, PartialEq, Encode, Decode)] diff --git a/crates/subspace-runtime/src/lib.rs b/crates/subspace-runtime/src/lib.rs index 9cf91d1654..b3d1f5b987 100644 --- a/crates/subspace-runtime/src/lib.rs +++ b/crates/subspace-runtime/src/lib.rs @@ -100,10 +100,12 @@ use sp_version::RuntimeVersion; use static_assertions::const_assert; use subspace_core_primitives::objects::BlockObjectMapping; use subspace_core_primitives::pieces::Piece; -use subspace_core_primitives::segments::{HistorySize, SegmentCommitment, SegmentIndex}; +use subspace_core_primitives::segments::{ + HistorySize, SegmentCommitment, SegmentHeader, SegmentIndex, +}; use subspace_core_primitives::{ - pieces_to_solution_range, solution_range_to_pieces, PublicKey, Randomness, SegmentHeader, - SlotNumber, SolutionRange, U256, + pieces_to_solution_range, solution_range_to_pieces, PublicKey, Randomness, SlotNumber, + SolutionRange, U256, }; use subspace_runtime_primitives::{ maximum_normal_block_length, AccountId, Balance, BlockNumber, FindBlockRewardAddress, Hash, diff --git a/crates/subspace-service/src/sync_from_dsn/segment_header_downloader.rs b/crates/subspace-service/src/sync_from_dsn/segment_header_downloader.rs index 5ada1c44f4..6338104048 100644 --- a/crates/subspace-service/src/sync_from_dsn/segment_header_downloader.rs +++ b/crates/subspace-service/src/sync_from_dsn/segment_header_downloader.rs @@ -3,8 +3,7 @@ use futures::StreamExt; use std::collections::{BTreeSet, HashMap}; use std::error::Error; use std::pin::pin; -use subspace_core_primitives::segments::SegmentIndex; -use subspace_core_primitives::SegmentHeader; +use subspace_core_primitives::segments::{SegmentHeader, SegmentIndex}; use subspace_networking::libp2p::PeerId; use subspace_networking::{Node, SegmentHeaderRequest, SegmentHeaderResponse}; use tracing::{debug, error, trace, warn}; diff --git a/test/subspace-test-runtime/src/lib.rs b/test/subspace-test-runtime/src/lib.rs index 37dc69bedc..3dfd13bd9c 100644 --- a/test/subspace-test-runtime/src/lib.rs +++ b/test/subspace-test-runtime/src/lib.rs @@ -92,10 +92,10 @@ use sp_version::RuntimeVersion; use static_assertions::const_assert; use subspace_core_primitives::objects::{BlockObject, BlockObjectMapping}; use subspace_core_primitives::pieces::Piece; -use subspace_core_primitives::segments::{HistorySize, SegmentCommitment, SegmentIndex}; -use subspace_core_primitives::{ - crypto, PublicKey, Randomness, SegmentHeader, SlotNumber, SolutionRange, U256, +use subspace_core_primitives::segments::{ + HistorySize, SegmentCommitment, SegmentHeader, SegmentIndex, }; +use subspace_core_primitives::{crypto, PublicKey, Randomness, SlotNumber, SolutionRange, U256}; use subspace_runtime_primitives::{ AccountId, Balance, BlockNumber, FindBlockRewardAddress, Hash, HoldIdentifier, Moment, Nonce, Signature, MIN_REPLICATION_FACTOR, From 13b7e30fc7032c2f734ad528544059d5d875f639 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Wed, 2 Oct 2024 16:55:35 +0300 Subject: [PATCH 3/6] Move sector-related data structures into `sectors` module --- crates/pallet-subspace/src/lib.rs | 8 +- crates/pallet-subspace/src/mock.rs | 5 +- .../sc-consensus-subspace/src/block_import.rs | 3 +- .../sc-consensus-subspace/src/slot_worker.rs | 5 +- crates/subspace-core-primitives/src/lib.rs | 159 +---------- crates/subspace-core-primitives/src/pieces.rs | 93 ------- .../subspace-core-primitives/src/sectors.rs | 256 ++++++++++++++++++ .../src/{pieces => sectors}/tests.rs | 2 +- .../benches/auditing.rs | 3 +- .../benches/proving.rs | 3 +- .../benches/reading.rs | 3 +- .../src/auditing.rs | 6 +- .../src/plotting.rs | 5 +- .../subspace-farmer-components/src/proving.rs | 5 +- .../subspace-farmer-components/src/reading.rs | 4 +- .../subspace-farmer-components/src/sector.rs | 7 +- .../commands/cluster/controller/farms.rs | 3 +- crates/subspace-farmer/src/cluster/farmer.rs | 3 +- crates/subspace-farmer/src/cluster/plotter.rs | 3 +- crates/subspace-farmer/src/farm.rs | 2 +- .../src/farm/plotted_pieces.rs | 2 +- crates/subspace-farmer/src/plotter.rs | 3 +- crates/subspace-farmer/src/plotter/cpu.rs | 3 +- crates/subspace-farmer/src/plotter/gpu.rs | 3 +- .../subspace-farmer/src/plotter/gpu/cuda.rs | 2 +- crates/subspace-farmer/src/plotter/pool.rs | 3 +- .../subspace-farmer/src/single_disk_farm.rs | 3 +- .../src/single_disk_farm/farming.rs | 3 +- .../src/single_disk_farm/metrics.rs | 2 +- .../src/single_disk_farm/piece_reader.rs | 3 +- .../src/single_disk_farm/plot_cache.rs | 3 +- .../src/single_disk_farm/plot_cache/tests.rs | 2 +- .../src/single_disk_farm/plotted_sectors.rs | 3 +- .../src/single_disk_farm/plotting.rs | 3 +- crates/subspace-verification/src/lib.rs | 5 +- .../src/cuda/tests.rs | 2 +- 36 files changed, 330 insertions(+), 293 deletions(-) create mode 100644 crates/subspace-core-primitives/src/sectors.rs rename crates/subspace-core-primitives/src/{pieces => sectors}/tests.rs (89%) diff --git a/crates/pallet-subspace/src/lib.rs b/crates/pallet-subspace/src/lib.rs index 6771971165..d2f3012c1d 100644 --- a/crates/pallet-subspace/src/lib.rs +++ b/crates/pallet-subspace/src/lib.rs @@ -59,12 +59,12 @@ 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, SectorId, SectorIndex, SlotNumber, SolutionRange, - REWARD_SIGNING_CONTEXT, + BlockHash, PublicKey, RewardSignature, SlotNumber, SolutionRange, REWARD_SIGNING_CONTEXT, }; use subspace_verification::{ check_reward_signature, derive_next_solution_range, derive_pot_entropy, PieceCheckParams, @@ -114,10 +114,10 @@ pub mod pallet { use sp_std::prelude::*; use subspace_core_primitives::crypto::Scalar; use subspace_core_primitives::pieces::PieceOffset; + use subspace_core_primitives::sectors::SectorIndex; use subspace_core_primitives::segments::{HistorySize, SegmentHeader, SegmentIndex}; use subspace_core_primitives::{ - Blake3Hash, PotCheckpoints, PublicKey, Randomness, RewardSignature, SectorIndex, - SolutionRange, + Blake3Hash, PotCheckpoints, PublicKey, Randomness, RewardSignature, SolutionRange, }; pub(super) struct InitialSolutionRanges { diff --git a/crates/pallet-subspace/src/mock.rs b/crates/pallet-subspace/src/mock.rs index 9f509655d8..8b6a5b9216 100644 --- a/crates/pallet-subspace/src/mock.rs +++ b/crates/pallet-subspace/src/mock.rs @@ -37,13 +37,14 @@ 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::sectors::SectorId; use subspace_core_primitives::segments::{ ArchivedBlockProgress, ArchivedHistorySegment, HistorySize, LastArchivedBlock, RecordedHistorySegment, SegmentCommitment, SegmentHeader, SegmentIndex, }; use subspace_core_primitives::{ - Blake3Hash, BlockNumber, PosSeed, PotOutput, PublicKey, RewardSignature, SectorId, SlotNumber, - Solution, SolutionRange, REWARD_SIGNING_CONTEXT, + Blake3Hash, BlockNumber, PosSeed, PotOutput, PublicKey, RewardSignature, SlotNumber, Solution, + SolutionRange, REWARD_SIGNING_CONTEXT, }; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::auditing::audit_sector_sync; diff --git a/crates/sc-consensus-subspace/src/block_import.rs b/crates/sc-consensus-subspace/src/block_import.rs index 72c441b187..e2792d9886 100644 --- a/crates/sc-consensus-subspace/src/block_import.rs +++ b/crates/sc-consensus-subspace/src/block_import.rs @@ -52,8 +52,9 @@ use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor, One}; use sp_runtime::Justifications; use std::marker::PhantomData; use std::sync::Arc; +use subspace_core_primitives::sectors::SectorId; use subspace_core_primitives::segments::{HistorySize, SegmentHeader, SegmentIndex}; -use subspace_core_primitives::{BlockNumber, PublicKey, SectorId, SolutionRange}; +use subspace_core_primitives::{BlockNumber, PublicKey, SolutionRange}; use subspace_proof_of_space::Table; use subspace_verification::{calculate_block_weight, PieceCheckParams, VerifySolutionParams}; use tracing::warn; diff --git a/crates/sc-consensus-subspace/src/slot_worker.rs b/crates/sc-consensus-subspace/src/slot_worker.rs index e051f00ef0..5c5edb87b4 100644 --- a/crates/sc-consensus-subspace/src/slot_worker.rs +++ b/crates/sc-consensus-subspace/src/slot_worker.rs @@ -66,9 +66,10 @@ use std::marker::PhantomData; use std::pin::Pin; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; +use subspace_core_primitives::sectors::SectorId; use subspace_core_primitives::{ - BlockNumber, PotCheckpoints, PotOutput, PublicKey, RewardSignature, SectorId, Solution, - SolutionRange, REWARD_SIGNING_CONTEXT, + BlockNumber, PotCheckpoints, PotOutput, PublicKey, RewardSignature, Solution, SolutionRange, + REWARD_SIGNING_CONTEXT, }; use subspace_proof_of_space::Table; use subspace_verification::{ diff --git a/crates/subspace-core-primitives/src/lib.rs b/crates/subspace-core-primitives/src/lib.rs index 9fd3f06aec..c22d62e63f 100644 --- a/crates/subspace-core-primitives/src/lib.rs +++ b/crates/subspace-core-primitives/src/lib.rs @@ -32,6 +32,7 @@ pub mod checksum; pub mod crypto; pub mod objects; pub mod pieces; +pub mod sectors; pub mod segments; #[cfg(feature = "serde")] mod serde; @@ -41,24 +42,22 @@ mod tests; #[cfg(not(feature = "std"))] extern crate alloc; -use crate::crypto::{blake3_hash, blake3_hash_list, blake3_hash_with_key, Scalar}; +use crate::crypto::{blake3_hash, blake3_hash_list, Scalar}; +use crate::pieces::{ChunkWitness, PieceOffset, Record, RecordCommitment, RecordWitness}; +use crate::sectors::SectorIndex; +use crate::segments::{HistorySize, SegmentIndex}; #[cfg(feature = "serde")] use ::serde::{Deserialize, Serialize}; use core::array::TryFromSliceError; use core::fmt; -use core::num::{NonZeroU64, NonZeroU8}; -use core::simd::Simd; +use core::num::NonZeroU8; use core::str::FromStr; use derive_more::{Add, AsMut, AsRef, Deref, DerefMut, Display, Div, From, Into, Mul, Rem, Sub}; use hex::FromHex; use num_traits::{WrappingAdd, WrappingSub}; use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; -use pieces::{ - ChunkWitness, PieceIndex, PieceOffset, Record, RecordCommitment, RecordWitness, SBucket, -}; use scale_info::TypeInfo; -use segments::{HistorySize, SegmentCommitment, SegmentIndex}; -use static_assertions::{const_assert, const_assert_eq}; +use static_assertions::const_assert; // Refuse to compile on lower than 32-bit platforms const_assert!(core::mem::size_of::() >= core::mem::size_of::()); @@ -567,9 +566,6 @@ impl RewardSignature { pub const SIZE: usize = 64; } -/// Sector index in consensus -pub type SectorIndex = u16; - /// Farmer solution for slot challenge. #[derive(Clone, Debug, Eq, PartialEq, Encode, Decode, TypeInfo)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] @@ -881,144 +877,3 @@ impl Default for U256 { Self::zero() } } - -/// Challenge used for a particular sector for particular slot -#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deref)] -pub struct SectorSlotChallenge(Blake3Hash); - -impl SectorSlotChallenge { - /// Index of s-bucket within sector to be audited - #[inline] - pub fn s_bucket_audit_index(&self) -> SBucket { - // As long as number of s-buckets is 2^16, we can pick first two bytes instead of actually - // calculating `U256::from_le_bytes(self.0) % Record::NUM_S_BUCKETS)` - const_assert_eq!(Record::NUM_S_BUCKETS, 1 << u16::BITS as usize); - SBucket::from(u16::from_le_bytes([self.0[0], self.0[1]])) - } -} - -/// Data structure representing sector ID in farmer's plot -#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct SectorId(#[cfg_attr(feature = "serde", serde(with = "hex"))] Blake3Hash); - -impl AsRef<[u8]> for SectorId { - #[inline] - fn as_ref(&self) -> &[u8] { - self.0.as_ref() - } -} - -impl SectorId { - /// Create new sector ID by deriving it from public key and sector index - pub fn new(public_key_hash: Blake3Hash, sector_index: SectorIndex) -> Self { - Self(blake3_hash_with_key( - &public_key_hash, - §or_index.to_le_bytes(), - )) - } - - /// Derive piece index that should be stored in sector at `piece_offset` for specified size of - /// blockchain history - pub fn derive_piece_index( - &self, - piece_offset: PieceOffset, - history_size: HistorySize, - max_pieces_in_sector: u16, - recent_segments: HistorySize, - recent_history_fraction: (HistorySize, HistorySize), - ) -> PieceIndex { - let recent_segments_in_pieces = recent_segments.in_pieces().get(); - // Recent history must be at most `recent_history_fraction` of all history to use separate - // policy for recent pieces - let min_history_size_in_pieces = recent_segments_in_pieces - * recent_history_fraction.1.in_pieces().get() - / recent_history_fraction.0.in_pieces().get(); - let input_hash = { - let piece_offset_bytes = piece_offset.to_bytes(); - let mut key = [0; 32]; - key[..piece_offset_bytes.len()].copy_from_slice(&piece_offset_bytes); - U256::from_le_bytes(*blake3_hash_with_key(&key, self.as_ref())) - }; - let history_size_in_pieces = history_size.in_pieces().get(); - let num_interleaved_pieces = 1.max( - u64::from(max_pieces_in_sector) * recent_history_fraction.0.in_pieces().get() - / recent_history_fraction.1.in_pieces().get() - * 2, - ); - - let piece_index = if history_size_in_pieces > min_history_size_in_pieces - && u64::from(piece_offset) < num_interleaved_pieces - && u16::from(piece_offset) % 2 == 1 - { - // For odd piece offsets at the beginning of the sector pick pieces at random from - // recent history only - input_hash % U256::from(recent_segments_in_pieces) - + U256::from(history_size_in_pieces - recent_segments_in_pieces) - } else { - input_hash % U256::from(history_size_in_pieces) - }; - - PieceIndex::from(u64::try_from(piece_index).expect( - "Remainder of division by PieceIndex is guaranteed to fit into PieceIndex; qed", - )) - } - - /// Derive sector slot challenge for this sector from provided global challenge - pub fn derive_sector_slot_challenge( - &self, - global_challenge: &Blake3Hash, - ) -> SectorSlotChallenge { - let sector_slot_challenge = Simd::from(*self.0) ^ Simd::from(**global_challenge); - SectorSlotChallenge(sector_slot_challenge.to_array().into()) - } - - /// Derive evaluation seed - pub fn derive_evaluation_seed( - &self, - piece_offset: PieceOffset, - history_size: HistorySize, - ) -> PosSeed { - let evaluation_seed = blake3_hash_list(&[ - self.as_ref(), - &piece_offset.to_bytes(), - &history_size.get().to_le_bytes(), - ]); - - PosSeed::from(*evaluation_seed) - } - - /// Derive history size when sector created at `history_size` expires. - /// - /// Returns `None` on overflow. - pub fn derive_expiration_history_size( - &self, - history_size: HistorySize, - sector_expiration_check_segment_commitment: &SegmentCommitment, - min_sector_lifetime: HistorySize, - ) -> Option { - let sector_expiration_check_history_size = - history_size.sector_expiration_check(min_sector_lifetime)?; - - let input_hash = U256::from_le_bytes(*blake3_hash_list(&[ - self.as_ref(), - sector_expiration_check_segment_commitment.as_ref(), - ])); - - let last_possible_expiration = - min_sector_lifetime.checked_add(history_size.get().checked_mul(4u64)?)?; - let expires_in = input_hash - % U256::from( - last_possible_expiration - .get() - .checked_sub(sector_expiration_check_history_size.get())?, - ); - let expires_in = u64::try_from(expires_in).expect("Number modulo u64 fits into u64; qed"); - - let expiration_history_size = sector_expiration_check_history_size.get() + expires_in; - let expiration_history_size = NonZeroU64::try_from(expiration_history_size).expect( - "History size is not zero, so result is not zero even if expires immediately; qed", - ); - Some(HistorySize::from(expiration_history_size)) - } -} diff --git a/crates/subspace-core-primitives/src/pieces.rs b/crates/subspace-core-primitives/src/pieces.rs index 71e7d3458b..0ab07ab63f 100644 --- a/crates/subspace-core-primitives/src/pieces.rs +++ b/crates/subspace-core-primitives/src/pieces.rs @@ -2,8 +2,6 @@ #[cfg(feature = "serde")] mod serde; -#[cfg(test)] -mod tests; extern crate alloc; @@ -25,7 +23,6 @@ use bytes::{Bytes, BytesMut}; use core::array::TryFromSliceError; use core::hash::{Hash, Hasher}; use core::iter::Step; -use core::num::TryFromIntError; use core::{mem, slice}; use derive_more::{ Add, AddAssign, AsMut, AsRef, Deref, DerefMut, Display, Div, DivAssign, From, Into, Mul, @@ -37,96 +34,6 @@ use rayon::prelude::*; use scale_info::build::Fields; use scale_info::{Path, Type, TypeInfo}; -/// S-bucket used in consensus -#[derive( - Debug, - Display, - Default, - Copy, - Clone, - Ord, - PartialOrd, - Eq, - PartialEq, - Hash, - Encode, - Decode, - Add, - AddAssign, - Sub, - SubAssign, - Mul, - MulAssign, - Div, - DivAssign, - TypeInfo, - MaxEncodedLen, -)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[repr(transparent)] -pub struct SBucket(u16); - -impl Step for SBucket { - #[inline] - fn steps_between(start: &Self, end: &Self) -> Option { - u16::steps_between(&start.0, &end.0) - } - - #[inline] - fn forward_checked(start: Self, count: usize) -> Option { - u16::forward_checked(start.0, count).map(Self) - } - - #[inline] - fn backward_checked(start: Self, count: usize) -> Option { - u16::backward_checked(start.0, count).map(Self) - } -} - -impl TryFrom for SBucket { - type Error = TryFromIntError; - - #[inline] - fn try_from(value: usize) -> Result { - Ok(Self(u16::try_from(value)?)) - } -} - -impl From for SBucket { - #[inline] - fn from(original: u16) -> Self { - Self(original) - } -} - -impl From for u16 { - #[inline] - fn from(original: SBucket) -> Self { - original.0 - } -} - -impl From for u32 { - #[inline] - fn from(original: SBucket) -> Self { - u32::from(original.0) - } -} - -impl From for usize { - #[inline] - fn from(original: SBucket) -> Self { - usize::from(original.0) - } -} - -impl SBucket { - /// S-bucket 0. - pub const ZERO: SBucket = SBucket(0); - /// Max s-bucket index - pub const MAX: SBucket = SBucket((Record::NUM_S_BUCKETS - 1) as u16); -} - /// Piece index in consensus #[derive( Debug, diff --git a/crates/subspace-core-primitives/src/sectors.rs b/crates/subspace-core-primitives/src/sectors.rs new file mode 100644 index 0000000000..a7b507ba11 --- /dev/null +++ b/crates/subspace-core-primitives/src/sectors.rs @@ -0,0 +1,256 @@ +//! Sectors-related data structures. + +#[cfg(test)] +mod tests; + +use crate::crypto::{blake3_hash_list, blake3_hash_with_key}; +use crate::pieces::{PieceIndex, PieceOffset, Record}; +use crate::segments::{HistorySize, SegmentCommitment}; +use crate::{Blake3Hash, PosSeed, U256}; +#[cfg(feature = "serde")] +use ::serde::{Deserialize, Serialize}; +use core::hash::Hash; +use core::iter::Step; +use core::num::{NonZeroU64, TryFromIntError}; +use core::simd::Simd; +use derive_more::{ + Add, AddAssign, AsRef, Deref, Display, Div, DivAssign, From, Into, Mul, MulAssign, Sub, + SubAssign, +}; +use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; +use scale_info::TypeInfo; +use static_assertions::const_assert_eq; + +/// Sector index in consensus +pub type SectorIndex = u16; + +/// Challenge used for a particular sector for particular slot +#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deref)] +pub struct SectorSlotChallenge(Blake3Hash); + +impl SectorSlotChallenge { + /// Index of s-bucket within sector to be audited + #[inline] + pub fn s_bucket_audit_index(&self) -> SBucket { + // As long as number of s-buckets is 2^16, we can pick first two bytes instead of actually + // calculating `U256::from_le_bytes(self.0) % Record::NUM_S_BUCKETS)` + const_assert_eq!(Record::NUM_S_BUCKETS, 1 << u16::BITS as usize); + SBucket::from(u16::from_le_bytes([self.0[0], self.0[1]])) + } +} + +/// Data structure representing sector ID in farmer's plot +#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +pub struct SectorId(#[cfg_attr(feature = "serde", serde(with = "hex"))] Blake3Hash); + +impl AsRef<[u8]> for SectorId { + #[inline] + fn as_ref(&self) -> &[u8] { + self.0.as_ref() + } +} + +impl SectorId { + /// Create new sector ID by deriving it from public key and sector index + pub fn new(public_key_hash: Blake3Hash, sector_index: SectorIndex) -> Self { + Self(blake3_hash_with_key( + &public_key_hash, + §or_index.to_le_bytes(), + )) + } + + /// Derive piece index that should be stored in sector at `piece_offset` for specified size of + /// blockchain history + pub fn derive_piece_index( + &self, + piece_offset: PieceOffset, + history_size: HistorySize, + max_pieces_in_sector: u16, + recent_segments: HistorySize, + recent_history_fraction: (HistorySize, HistorySize), + ) -> PieceIndex { + let recent_segments_in_pieces = recent_segments.in_pieces().get(); + // Recent history must be at most `recent_history_fraction` of all history to use separate + // policy for recent pieces + let min_history_size_in_pieces = recent_segments_in_pieces + * recent_history_fraction.1.in_pieces().get() + / recent_history_fraction.0.in_pieces().get(); + let input_hash = { + let piece_offset_bytes = piece_offset.to_bytes(); + let mut key = [0; 32]; + key[..piece_offset_bytes.len()].copy_from_slice(&piece_offset_bytes); + U256::from_le_bytes(*blake3_hash_with_key(&key, self.as_ref())) + }; + let history_size_in_pieces = history_size.in_pieces().get(); + let num_interleaved_pieces = 1.max( + u64::from(max_pieces_in_sector) * recent_history_fraction.0.in_pieces().get() + / recent_history_fraction.1.in_pieces().get() + * 2, + ); + + let piece_index = if history_size_in_pieces > min_history_size_in_pieces + && u64::from(piece_offset) < num_interleaved_pieces + && u16::from(piece_offset) % 2 == 1 + { + // For odd piece offsets at the beginning of the sector pick pieces at random from + // recent history only + input_hash % U256::from(recent_segments_in_pieces) + + U256::from(history_size_in_pieces - recent_segments_in_pieces) + } else { + input_hash % U256::from(history_size_in_pieces) + }; + + PieceIndex::from(u64::try_from(piece_index).expect( + "Remainder of division by PieceIndex is guaranteed to fit into PieceIndex; qed", + )) + } + + /// Derive sector slot challenge for this sector from provided global challenge + pub fn derive_sector_slot_challenge( + &self, + global_challenge: &Blake3Hash, + ) -> SectorSlotChallenge { + let sector_slot_challenge = Simd::from(*self.0) ^ Simd::from(**global_challenge); + SectorSlotChallenge(sector_slot_challenge.to_array().into()) + } + + /// Derive evaluation seed + pub fn derive_evaluation_seed( + &self, + piece_offset: PieceOffset, + history_size: HistorySize, + ) -> PosSeed { + let evaluation_seed = blake3_hash_list(&[ + self.as_ref(), + &piece_offset.to_bytes(), + &history_size.get().to_le_bytes(), + ]); + + PosSeed::from(*evaluation_seed) + } + + /// Derive history size when sector created at `history_size` expires. + /// + /// Returns `None` on overflow. + pub fn derive_expiration_history_size( + &self, + history_size: HistorySize, + sector_expiration_check_segment_commitment: &SegmentCommitment, + min_sector_lifetime: HistorySize, + ) -> Option { + let sector_expiration_check_history_size = + history_size.sector_expiration_check(min_sector_lifetime)?; + + let input_hash = U256::from_le_bytes(*blake3_hash_list(&[ + self.as_ref(), + sector_expiration_check_segment_commitment.as_ref(), + ])); + + let last_possible_expiration = + min_sector_lifetime.checked_add(history_size.get().checked_mul(4u64)?)?; + let expires_in = input_hash + % U256::from( + last_possible_expiration + .get() + .checked_sub(sector_expiration_check_history_size.get())?, + ); + let expires_in = u64::try_from(expires_in).expect("Number modulo u64 fits into u64; qed"); + + let expiration_history_size = sector_expiration_check_history_size.get() + expires_in; + let expiration_history_size = NonZeroU64::try_from(expiration_history_size).expect( + "History size is not zero, so result is not zero even if expires immediately; qed", + ); + Some(HistorySize::from(expiration_history_size)) + } +} + +/// S-bucket used in consensus +#[derive( + Debug, + Display, + Default, + Copy, + Clone, + Ord, + PartialOrd, + Eq, + PartialEq, + Hash, + Encode, + Decode, + Add, + AddAssign, + Sub, + SubAssign, + Mul, + MulAssign, + Div, + DivAssign, + TypeInfo, + MaxEncodedLen, +)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[repr(transparent)] +pub struct SBucket(u16); + +impl Step for SBucket { + #[inline] + fn steps_between(start: &Self, end: &Self) -> Option { + u16::steps_between(&start.0, &end.0) + } + + #[inline] + fn forward_checked(start: Self, count: usize) -> Option { + u16::forward_checked(start.0, count).map(Self) + } + + #[inline] + fn backward_checked(start: Self, count: usize) -> Option { + u16::backward_checked(start.0, count).map(Self) + } +} + +impl TryFrom for SBucket { + type Error = TryFromIntError; + + #[inline] + fn try_from(value: usize) -> Result { + Ok(Self(u16::try_from(value)?)) + } +} + +impl From for SBucket { + #[inline] + fn from(original: u16) -> Self { + Self(original) + } +} + +impl From for u16 { + #[inline] + fn from(original: SBucket) -> Self { + original.0 + } +} + +impl From for u32 { + #[inline] + fn from(original: SBucket) -> Self { + u32::from(original.0) + } +} + +impl From for usize { + #[inline] + fn from(original: SBucket) -> Self { + usize::from(original.0) + } +} + +impl SBucket { + /// S-bucket 0. + pub const ZERO: SBucket = SBucket(0); + /// Max s-bucket index + pub const MAX: SBucket = SBucket((Record::NUM_S_BUCKETS - 1) as u16); +} diff --git a/crates/subspace-core-primitives/src/pieces/tests.rs b/crates/subspace-core-primitives/src/sectors/tests.rs similarity index 89% rename from crates/subspace-core-primitives/src/pieces/tests.rs rename to crates/subspace-core-primitives/src/sectors/tests.rs index cfd178be3e..7d82319a7c 100644 --- a/crates/subspace-core-primitives/src/pieces/tests.rs +++ b/crates/subspace-core-primitives/src/sectors/tests.rs @@ -1,4 +1,4 @@ -use crate::pieces::SBucket; +use crate::sectors::SBucket; use crate::Record; // Statically validate that we can store all possible s-buckets in SBucket data structure diff --git a/crates/subspace-farmer-components/benches/auditing.rs b/crates/subspace-farmer-components/benches/auditing.rs index 16336d3427..5baee4ce55 100644 --- a/crates/subspace-farmer-components/benches/auditing.rs +++ b/crates/subspace-farmer-components/benches/auditing.rs @@ -10,8 +10,9 @@ use subspace_archiving::archiver::Archiver; use subspace_core_primitives::crypto::kzg; use subspace_core_primitives::crypto::kzg::Kzg; use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::sectors::SectorId; use subspace_core_primitives::segments::{HistorySize, RecordedHistorySegment}; -use subspace_core_primitives::{Blake3Hash, PublicKey, SectorId, SolutionRange}; +use subspace_core_primitives::{Blake3Hash, PublicKey, SolutionRange}; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::auditing::audit_plot_sync; use subspace_farmer_components::file_ext::{FileExt, OpenOptionsExt}; diff --git a/crates/subspace-farmer-components/benches/proving.rs b/crates/subspace-farmer-components/benches/proving.rs index f769b85c93..4a23c38abf 100644 --- a/crates/subspace-farmer-components/benches/proving.rs +++ b/crates/subspace-farmer-components/benches/proving.rs @@ -14,8 +14,9 @@ use subspace_archiving::archiver::Archiver; use subspace_core_primitives::crypto::kzg; use subspace_core_primitives::crypto::kzg::Kzg; use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::sectors::SectorId; use subspace_core_primitives::segments::{HistorySize, RecordedHistorySegment}; -use subspace_core_primitives::{Blake3Hash, PosSeed, PublicKey, SectorId, SolutionRange}; +use subspace_core_primitives::{Blake3Hash, PosSeed, PublicKey, SolutionRange}; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::auditing::audit_plot_sync; use subspace_farmer_components::file_ext::{FileExt, OpenOptionsExt}; diff --git a/crates/subspace-farmer-components/benches/reading.rs b/crates/subspace-farmer-components/benches/reading.rs index cffa57d235..7908b94123 100644 --- a/crates/subspace-farmer-components/benches/reading.rs +++ b/crates/subspace-farmer-components/benches/reading.rs @@ -11,8 +11,9 @@ use subspace_archiving::archiver::Archiver; use subspace_core_primitives::crypto::kzg; use subspace_core_primitives::crypto::kzg::Kzg; use subspace_core_primitives::pieces::{PieceOffset, Record}; +use subspace_core_primitives::sectors::SectorId; use subspace_core_primitives::segments::{HistorySize, RecordedHistorySegment}; -use subspace_core_primitives::{PublicKey, SectorId}; +use subspace_core_primitives::PublicKey; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::file_ext::{FileExt, OpenOptionsExt}; use subspace_farmer_components::plotting::{ diff --git a/crates/subspace-farmer-components/src/auditing.rs b/crates/subspace-farmer-components/src/auditing.rs index 935148f786..8b7de1c0be 100644 --- a/crates/subspace-farmer-components/src/auditing.rs +++ b/crates/subspace-farmer-components/src/auditing.rs @@ -11,10 +11,8 @@ use rayon::prelude::*; use std::collections::HashSet; use std::io; use subspace_core_primitives::crypto::Scalar; -use subspace_core_primitives::pieces::SBucket; -use subspace_core_primitives::{ - Blake3Hash, PublicKey, SectorId, SectorIndex, SectorSlotChallenge, SolutionRange, -}; +use subspace_core_primitives::sectors::{SBucket, SectorId, SectorIndex, SectorSlotChallenge}; +use subspace_core_primitives::{Blake3Hash, PublicKey, SolutionRange}; use subspace_verification::is_within_solution_range; use thiserror::Error; diff --git a/crates/subspace-farmer-components/src/plotting.rs b/crates/subspace-farmer-components/src/plotting.rs index ca85198551..786c2a39b1 100644 --- a/crates/subspace-farmer-components/src/plotting.rs +++ b/crates/subspace-farmer-components/src/plotting.rs @@ -26,9 +26,10 @@ use std::sync::Arc; use std::time::Duration; use subspace_core_primitives::crypto::kzg::Kzg; use subspace_core_primitives::crypto::{blake3_hash, blake3_hash_parallel, Scalar}; -use subspace_core_primitives::pieces::{PieceIndex, PieceOffset, Record, SBucket}; +use subspace_core_primitives::pieces::{PieceIndex, PieceOffset, Record}; +use subspace_core_primitives::sectors::{SBucket, SectorId, SectorIndex}; use subspace_core_primitives::segments::HistorySize; -use subspace_core_primitives::{Blake3Hash, PosSeed, PublicKey, SectorId, SectorIndex}; +use subspace_core_primitives::{Blake3Hash, PosSeed, PublicKey}; use subspace_erasure_coding::ErasureCoding; use subspace_proof_of_space::{Table, TableGenerator}; use thiserror::Error; diff --git a/crates/subspace-farmer-components/src/proving.rs b/crates/subspace-farmer-components/src/proving.rs index d0fed75827..66295fb316 100644 --- a/crates/subspace-farmer-components/src/proving.rs +++ b/crates/subspace-farmer-components/src/proving.rs @@ -15,8 +15,9 @@ use futures::FutureExt; use std::collections::VecDeque; use std::io; use subspace_core_primitives::crypto::kzg::Kzg; -use subspace_core_primitives::pieces::{ChunkWitness, PieceOffset, Record, SBucket}; -use subspace_core_primitives::{PosSeed, PublicKey, SectorId, Solution, SolutionRange}; +use subspace_core_primitives::pieces::{ChunkWitness, PieceOffset, Record}; +use subspace_core_primitives::sectors::{SBucket, SectorId}; +use subspace_core_primitives::{PosSeed, PublicKey, Solution, SolutionRange}; use subspace_erasure_coding::ErasureCoding; use subspace_proof_of_space::Table; use thiserror::Error; diff --git a/crates/subspace-farmer-components/src/reading.rs b/crates/subspace-farmer-components/src/reading.rs index 5e9cb7af57..efd5f85d73 100644 --- a/crates/subspace-farmer-components/src/reading.rs +++ b/crates/subspace-farmer-components/src/reading.rs @@ -18,8 +18,8 @@ use std::simd::Simd; use std::str::FromStr; use std::{fmt, io}; use subspace_core_primitives::crypto::{blake3_hash, Scalar}; -use subspace_core_primitives::pieces::{Piece, PieceOffset, Record, SBucket}; -use subspace_core_primitives::SectorId; +use subspace_core_primitives::pieces::{Piece, PieceOffset, Record}; +use subspace_core_primitives::sectors::{SBucket, SectorId}; use subspace_erasure_coding::ErasureCoding; use subspace_proof_of_space::{Table, TableGenerator}; use thiserror::Error; diff --git a/crates/subspace-farmer-components/src/sector.rs b/crates/subspace-farmer-components/src/sector.rs index d961231786..a0e8d92c97 100644 --- a/crates/subspace-farmer-components/src/sector.rs +++ b/crates/subspace-farmer-components/src/sector.rs @@ -14,11 +14,10 @@ use std::ops::{Deref, DerefMut}; use std::{mem, slice}; use subspace_core_primitives::checksum::Blake3Checksummed; use subspace_core_primitives::crypto::blake3_hash; -use subspace_core_primitives::pieces::{ - PieceOffset, Record, RecordCommitment, RecordWitness, SBucket, -}; +use subspace_core_primitives::pieces::{PieceOffset, Record, RecordCommitment, RecordWitness}; +use subspace_core_primitives::sectors::{SBucket, SectorIndex}; use subspace_core_primitives::segments::{HistorySize, SegmentIndex}; -use subspace_core_primitives::{Blake3Hash, SectorIndex}; +use subspace_core_primitives::Blake3Hash; use thiserror::Error; use tracing::debug; diff --git a/crates/subspace-farmer/src/bin/subspace-farmer/commands/cluster/controller/farms.rs b/crates/subspace-farmer/src/bin/subspace-farmer/commands/cluster/controller/farms.rs index e8efec6895..a11663d41c 100644 --- a/crates/subspace-farmer/src/bin/subspace-farmer/commands/cluster/controller/farms.rs +++ b/crates/subspace-farmer/src/bin/subspace-farmer/commands/cluster/controller/farms.rs @@ -19,7 +19,8 @@ use std::mem; use std::pin::{pin, Pin}; use std::sync::Arc; use std::time::Instant; -use subspace_core_primitives::{Blake3Hash, SectorIndex}; +use subspace_core_primitives::sectors::SectorIndex; +use subspace_core_primitives::Blake3Hash; use subspace_farmer::cluster::controller::ClusterControllerFarmerIdentifyBroadcast; use subspace_farmer::cluster::farmer::{ClusterFarm, ClusterFarmerIdentifyFarmBroadcast}; use subspace_farmer::cluster::nats_client::NatsClient; diff --git a/crates/subspace-farmer/src/cluster/farmer.rs b/crates/subspace-farmer/src/cluster/farmer.rs index d94f812138..02892780b6 100644 --- a/crates/subspace-farmer/src/cluster/farmer.rs +++ b/crates/subspace-farmer/src/cluster/farmer.rs @@ -29,7 +29,8 @@ use std::sync::Arc; use std::time::{Duration, Instant}; use subspace_core_primitives::crypto::blake3_hash_list; use subspace_core_primitives::pieces::{Piece, PieceOffset}; -use subspace_core_primitives::{Blake3Hash, SectorIndex}; +use subspace_core_primitives::sectors::SectorIndex; +use subspace_core_primitives::Blake3Hash; use subspace_farmer_components::plotting::PlottedSector; use subspace_rpc_primitives::SolutionResponse; use tokio::time::MissedTickBehavior; diff --git a/crates/subspace-farmer/src/cluster/plotter.rs b/crates/subspace-farmer/src/cluster/plotter.rs index fe332b8d66..32bda46ce1 100644 --- a/crates/subspace-farmer/src/cluster/plotter.rs +++ b/crates/subspace-farmer/src/cluster/plotter.rs @@ -27,7 +27,8 @@ use std::num::NonZeroUsize; use std::pin::{pin, Pin}; use std::sync::Arc; use std::time::{Duration, Instant}; -use subspace_core_primitives::{PublicKey, SectorIndex}; +use subspace_core_primitives::sectors::SectorIndex; +use subspace_core_primitives::PublicKey; use subspace_farmer_components::plotting::PlottedSector; use subspace_farmer_components::FarmerProtocolInfo; use tokio::sync::{OwnedSemaphorePermit, Semaphore}; diff --git a/crates/subspace-farmer/src/farm.rs b/crates/subspace-farmer/src/farm.rs index 5eaa5df4ff..f80f51bf07 100644 --- a/crates/subspace-farmer/src/farm.rs +++ b/crates/subspace-farmer/src/farm.rs @@ -19,8 +19,8 @@ use std::sync::Arc; use std::time::Duration; use std::{fmt, io}; use subspace_core_primitives::pieces::{Piece, PieceIndex, PieceOffset}; +use subspace_core_primitives::sectors::SectorIndex; use subspace_core_primitives::segments::SegmentIndex; -use subspace_core_primitives::SectorIndex; use subspace_farmer_components::auditing::AuditingError; use subspace_farmer_components::plotting::PlottedSector; use subspace_farmer_components::proving::ProvingError; diff --git a/crates/subspace-farmer/src/farm/plotted_pieces.rs b/crates/subspace-farmer/src/farm/plotted_pieces.rs index ae861e7105..a16a8e6527 100644 --- a/crates/subspace-farmer/src/farm/plotted_pieces.rs +++ b/crates/subspace-farmer/src/farm/plotted_pieces.rs @@ -11,7 +11,7 @@ use std::future::Future; use std::hash::Hash; use std::sync::Arc; use subspace_core_primitives::pieces::{Piece, PieceIndex, PieceOffset}; -use subspace_core_primitives::SectorIndex; +use subspace_core_primitives::sectors::SectorIndex; use subspace_farmer_components::plotting::PlottedSector; use tracing::{trace, warn}; diff --git a/crates/subspace-farmer/src/plotter.rs b/crates/subspace-farmer/src/plotter.rs index a7d87b400b..e1c6f5e1f0 100644 --- a/crates/subspace-farmer/src/plotter.rs +++ b/crates/subspace-farmer/src/plotter.rs @@ -17,7 +17,8 @@ use std::fmt; use std::pin::Pin; use std::sync::Arc; use std::time::Duration; -use subspace_core_primitives::{PublicKey, SectorIndex}; +use subspace_core_primitives::sectors::SectorIndex; +use subspace_core_primitives::PublicKey; use subspace_farmer_components::plotting::PlottedSector; use subspace_farmer_components::FarmerProtocolInfo; diff --git a/crates/subspace-farmer/src/plotter/cpu.rs b/crates/subspace-farmer/src/plotter/cpu.rs index 0d77736cc4..5929c0b951 100644 --- a/crates/subspace-farmer/src/plotter/cpu.rs +++ b/crates/subspace-farmer/src/plotter/cpu.rs @@ -24,7 +24,8 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use std::time::Instant; use subspace_core_primitives::crypto::kzg::Kzg; -use subspace_core_primitives::{PublicKey, SectorIndex}; +use subspace_core_primitives::sectors::SectorIndex; +use subspace_core_primitives::PublicKey; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::plotting::{ download_sector, encode_sector, write_sector, CpuRecordsEncoder, DownloadSectorOptions, diff --git a/crates/subspace-farmer/src/plotter/gpu.rs b/crates/subspace-farmer/src/plotter/gpu.rs index b090f15e90..d7a48f778a 100644 --- a/crates/subspace-farmer/src/plotter/gpu.rs +++ b/crates/subspace-farmer/src/plotter/gpu.rs @@ -25,7 +25,8 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use std::time::Instant; use subspace_core_primitives::crypto::kzg::Kzg; -use subspace_core_primitives::{PublicKey, SectorIndex}; +use subspace_core_primitives::sectors::SectorIndex; +use subspace_core_primitives::PublicKey; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::plotting::{ download_sector, encode_sector, write_sector, DownloadSectorOptions, EncodeSectorOptions, diff --git a/crates/subspace-farmer/src/plotter/gpu/cuda.rs b/crates/subspace-farmer/src/plotter/gpu/cuda.rs index 813d0e3e9d..c120aa2235 100644 --- a/crates/subspace-farmer/src/plotter/gpu/cuda.rs +++ b/crates/subspace-farmer/src/plotter/gpu/cuda.rs @@ -7,8 +7,8 @@ use rayon::{ThreadPool, ThreadPoolBuildError, ThreadPoolBuilder}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use subspace_core_primitives::pieces::{PieceOffset, Record}; +use subspace_core_primitives::sectors::SectorId; use subspace_core_primitives::segments::HistorySize; -use subspace_core_primitives::SectorId; use subspace_farmer_components::plotting::RecordsEncoder; use subspace_farmer_components::sector::SectorContentsMap; use subspace_proof_of_space_gpu::cuda::CudaDevice; diff --git a/crates/subspace-farmer/src/plotter/pool.rs b/crates/subspace-farmer/src/plotter/pool.rs index 0d9a5e25a6..b3ad2fdd7c 100644 --- a/crates/subspace-farmer/src/plotter/pool.rs +++ b/crates/subspace-farmer/src/plotter/pool.rs @@ -5,7 +5,8 @@ use async_trait::async_trait; use futures::channel::mpsc; use std::any::type_name_of_val; use std::time::Duration; -use subspace_core_primitives::{PublicKey, SectorIndex}; +use subspace_core_primitives::sectors::SectorIndex; +use subspace_core_primitives::PublicKey; use subspace_farmer_components::FarmerProtocolInfo; use tracing::{error, trace}; diff --git a/crates/subspace-farmer/src/single_disk_farm.rs b/crates/subspace-farmer/src/single_disk_farm.rs index 2cf3ae818e..77400b485a 100644 --- a/crates/subspace-farmer/src/single_disk_farm.rs +++ b/crates/subspace-farmer/src/single_disk_farm.rs @@ -69,8 +69,9 @@ use std::{fmt, fs, io, mem}; use subspace_core_primitives::crypto::kzg::Kzg; use subspace_core_primitives::crypto::{blake3_hash, Scalar}; use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::sectors::SectorIndex; use subspace_core_primitives::segments::{HistorySize, SegmentIndex}; -use subspace_core_primitives::{Blake3Hash, PublicKey, SectorIndex}; +use subspace_core_primitives::{Blake3Hash, PublicKey}; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::file_ext::FileExt; use subspace_farmer_components::reading::ReadSectorRecordChunksMode; diff --git a/crates/subspace-farmer/src/single_disk_farm/farming.rs b/crates/subspace-farmer/src/single_disk_farm/farming.rs index b184b8cf28..9631d9a2ca 100644 --- a/crates/subspace-farmer/src/single_disk_farm/farming.rs +++ b/crates/subspace-farmer/src/single_disk_farm/farming.rs @@ -21,8 +21,9 @@ use std::sync::Arc; use std::time::Instant; use subspace_core_primitives::crypto::kzg::Kzg; use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::sectors::SectorIndex; use subspace_core_primitives::segments::{HistorySize, SegmentIndex}; -use subspace_core_primitives::{PosSeed, PublicKey, SectorIndex, Solution, SolutionRange}; +use subspace_core_primitives::{PosSeed, PublicKey, Solution, SolutionRange}; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::auditing::{audit_plot_sync, AuditingError}; use subspace_farmer_components::proving::{ProvableSolutions, ProvingError}; diff --git a/crates/subspace-farmer/src/single_disk_farm/metrics.rs b/crates/subspace-farmer/src/single_disk_farm/metrics.rs index 9bc2807673..f6123308c6 100644 --- a/crates/subspace-farmer/src/single_disk_farm/metrics.rs +++ b/crates/subspace-farmer/src/single_disk_farm/metrics.rs @@ -9,7 +9,7 @@ use prometheus_client::registry::{Registry, Unit}; use std::fmt; use std::sync::atomic::{AtomicI64, AtomicU64}; use std::time::Duration; -use subspace_core_primitives::SectorIndex; +use subspace_core_primitives::sectors::SectorIndex; #[derive(Debug, Copy, Clone)] pub(super) enum SectorState { diff --git a/crates/subspace-farmer/src/single_disk_farm/piece_reader.rs b/crates/subspace-farmer/src/single_disk_farm/piece_reader.rs index 56ace08444..e6a8be734b 100644 --- a/crates/subspace-farmer/src/single_disk_farm/piece_reader.rs +++ b/crates/subspace-farmer/src/single_disk_farm/piece_reader.rs @@ -10,7 +10,8 @@ use std::collections::HashSet; use std::future::Future; use std::sync::Arc; use subspace_core_primitives::pieces::{Piece, PieceOffset}; -use subspace_core_primitives::{PublicKey, SectorId, SectorIndex}; +use subspace_core_primitives::sectors::{SectorId, SectorIndex}; +use subspace_core_primitives::PublicKey; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::reading::ReadSectorRecordChunksMode; use subspace_farmer_components::sector::{sector_size, SectorMetadataChecksummed}; diff --git a/crates/subspace-farmer/src/single_disk_farm/plot_cache.rs b/crates/subspace-farmer/src/single_disk_farm/plot_cache.rs index 757d9dd434..4f552dfd7f 100644 --- a/crates/subspace-farmer/src/single_disk_farm/plot_cache.rs +++ b/crates/subspace-farmer/src/single_disk_farm/plot_cache.rs @@ -15,7 +15,8 @@ use std::sync::{Arc, Weak}; use std::{io, mem}; use subspace_core_primitives::crypto::blake3_hash_list; use subspace_core_primitives::pieces::{Piece, PieceIndex}; -use subspace_core_primitives::{Blake3Hash, SectorIndex}; +use subspace_core_primitives::sectors::SectorIndex; +use subspace_core_primitives::Blake3Hash; use subspace_farmer_components::file_ext::FileExt; use subspace_farmer_components::sector::SectorMetadataChecksummed; use subspace_networking::libp2p::kad::RecordKey; diff --git a/crates/subspace-farmer/src/single_disk_farm/plot_cache/tests.rs b/crates/subspace-farmer/src/single_disk_farm/plot_cache/tests.rs index f907235b58..cc877bab58 100644 --- a/crates/subspace-farmer/src/single_disk_farm/plot_cache/tests.rs +++ b/crates/subspace-farmer/src/single_disk_farm/plot_cache/tests.rs @@ -12,8 +12,8 @@ use std::assert_matches::assert_matches; use std::num::NonZeroU64; use std::sync::Arc; use subspace_core_primitives::pieces::{Piece, PieceIndex, Record}; +use subspace_core_primitives::sectors::SectorIndex; use subspace_core_primitives::segments::HistorySize; -use subspace_core_primitives::SectorIndex; use subspace_farmer_components::file_ext::FileExt; use subspace_farmer_components::sector::{SectorMetadata, SectorMetadataChecksummed}; use subspace_networking::libp2p::kad::RecordKey; diff --git a/crates/subspace-farmer/src/single_disk_farm/plotted_sectors.rs b/crates/subspace-farmer/src/single_disk_farm/plotted_sectors.rs index 9585a2ce3d..573a26fd14 100644 --- a/crates/subspace-farmer/src/single_disk_farm/plotted_sectors.rs +++ b/crates/subspace-farmer/src/single_disk_farm/plotted_sectors.rs @@ -4,7 +4,8 @@ use async_trait::async_trait; use futures::{stream, Stream}; use std::sync::Arc; use subspace_core_primitives::pieces::PieceOffset; -use subspace_core_primitives::{PublicKey, SectorId}; +use subspace_core_primitives::sectors::SectorId; +use subspace_core_primitives::PublicKey; use subspace_farmer_components::plotting::PlottedSector; use subspace_farmer_components::sector::SectorMetadataChecksummed; use subspace_farmer_components::FarmerProtocolInfo; diff --git a/crates/subspace-farmer/src/single_disk_farm/plotting.rs b/crates/subspace-farmer/src/single_disk_farm/plotting.rs index faeef4f217..9e654b281e 100644 --- a/crates/subspace-farmer/src/single_disk_farm/plotting.rs +++ b/crates/subspace-farmer/src/single_disk_farm/plotting.rs @@ -19,8 +19,9 @@ use std::pin::pin; use std::sync::Arc; use std::time::{Duration, Instant}; use subspace_core_primitives::pieces::PieceOffset; +use subspace_core_primitives::sectors::{SectorId, SectorIndex}; use subspace_core_primitives::segments::{HistorySize, SegmentHeader, SegmentIndex}; -use subspace_core_primitives::{Blake3Hash, PublicKey, SectorId, SectorIndex}; +use subspace_core_primitives::{Blake3Hash, PublicKey}; use subspace_farmer_components::file_ext::FileExt; use subspace_farmer_components::plotting::PlottedSector; use subspace_farmer_components::sector::SectorMetadataChecksummed; diff --git a/crates/subspace-verification/src/lib.rs b/crates/subspace-verification/src/lib.rs index beac5d7a56..ec558e858e 100644 --- a/crates/subspace-verification/src/lib.rs +++ b/crates/subspace-verification/src/lib.rs @@ -31,10 +31,11 @@ use subspace_core_primitives::crypto::{ blake3_254_hash_to_scalar, blake3_hash_list, blake3_hash_with_key, Scalar, }; use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::sectors::{SectorId, SectorSlotChallenge}; use subspace_core_primitives::segments::{HistorySize, SegmentCommitment}; use subspace_core_primitives::{ - Blake3Hash, BlockNumber, BlockWeight, PotOutput, PublicKey, RewardSignature, SectorId, - SectorSlotChallenge, SlotNumber, Solution, SolutionRange, + Blake3Hash, BlockNumber, BlockWeight, PotOutput, PublicKey, RewardSignature, SlotNumber, + Solution, SolutionRange, }; use subspace_proof_of_space::Table; diff --git a/shared/subspace-proof-of-space-gpu/src/cuda/tests.rs b/shared/subspace-proof-of-space-gpu/src/cuda/tests.rs index 727c72553b..1633764345 100644 --- a/shared/subspace-proof-of-space-gpu/src/cuda/tests.rs +++ b/shared/subspace-proof-of-space-gpu/src/cuda/tests.rs @@ -3,8 +3,8 @@ use std::num::NonZeroUsize; use std::slice; use subspace_core_primitives::crypto::{blake3_254_hash_to_scalar, blake3_hash}; use subspace_core_primitives::pieces::{PieceOffset, Record}; +use subspace_core_primitives::sectors::SectorId; use subspace_core_primitives::segments::HistorySize; -use subspace_core_primitives::SectorId; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::plotting::{CpuRecordsEncoder, RecordsEncoder}; use subspace_farmer_components::sector::SectorContentsMap; From 0395e9a48ef18df9b065b474be25632eec302ad2 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Wed, 2 Oct 2024 17:13:41 +0300 Subject: [PATCH 4/6] Move pos/pot data structures into corresponding modules --- crates/pallet-domains/src/lib.rs | 3 +- crates/pallet-subspace/src/lib.rs | 3 +- crates/pallet-subspace/src/mock.rs | 6 +- crates/pallet-subspace/src/tests.rs | 3 +- .../sc-consensus-subspace/src/slot_worker.rs | 4 +- crates/sc-proof-of-time/src/lib.rs | 3 +- crates/sc-proof-of-time/src/source.rs | 3 +- crates/sc-proof-of-time/src/source/gossip.rs | 2 +- crates/sc-proof-of-time/src/source/state.rs | 2 +- .../sc-proof-of-time/src/source/timekeeper.rs | 2 +- crates/sc-proof-of-time/src/verifier.rs | 2 +- crates/sc-proof-of-time/src/verifier/tests.rs | 3 +- crates/sp-consensus-subspace/src/digests.rs | 3 +- crates/sp-consensus-subspace/src/lib.rs | 5 +- crates/sp-domains/src/lib.rs | 3 +- crates/subspace-core-primitives/src/lib.rs | 246 +----------------- crates/subspace-core-primitives/src/pos.rs | 72 +++++ .../src/{ => pos}/serde.rs | 2 +- crates/subspace-core-primitives/src/pot.rs | 189 ++++++++++++++ .../subspace-core-primitives/src/sectors.rs | 3 +- .../benches/proving.rs | 3 +- .../src/plotting.rs | 3 +- .../subspace-farmer-components/src/proving.rs | 3 +- .../src/single_disk_farm/farming.rs | 3 +- .../src/malicious_bundle_producer.rs | 2 +- crates/subspace-node/src/chain_spec.rs | 3 +- crates/subspace-proof-of-space/benches/pos.rs | 2 +- crates/subspace-proof-of-space/src/chia.rs | 2 +- crates/subspace-proof-of-space/src/lib.rs | 2 +- crates/subspace-proof-of-space/src/shim.rs | 3 +- .../benches/pot-compare-cpu-cores.rs | 2 +- crates/subspace-proof-of-time/benches/pot.rs | 2 +- crates/subspace-proof-of-time/src/aes.rs | 2 +- .../subspace-proof-of-time/src/aes/x86_64.rs | 2 +- crates/subspace-proof-of-time/src/lib.rs | 2 +- crates/subspace-service/src/lib.rs | 3 +- crates/subspace-verification/src/lib.rs | 5 +- .../src/bundle_producer_election_solver.rs | 2 +- domains/client/domain-operator/src/lib.rs | 2 +- domains/client/domain-operator/src/tests.rs | 2 +- domains/client/domain-operator/src/utils.rs | 2 +- domains/service/src/domain.rs | 2 +- .../subspace-proof-of-space-gpu/src/cuda.rs | 2 +- test/subspace-test-client/src/lib.rs | 5 +- test/subspace-test-service/src/lib.rs | 3 +- 45 files changed, 332 insertions(+), 291 deletions(-) create mode 100644 crates/subspace-core-primitives/src/pos.rs rename crates/subspace-core-primitives/src/{ => pos}/serde.rs (99%) create mode 100644 crates/subspace-core-primitives/src/pot.rs diff --git a/crates/pallet-domains/src/lib.rs b/crates/pallet-domains/src/lib.rs index 19e3d536ae..6fabde4473 100644 --- a/crates/pallet-domains/src/lib.rs +++ b/crates/pallet-domains/src/lib.rs @@ -84,7 +84,8 @@ use sp_runtime::transaction_validity::TransactionPriority; use sp_runtime::{RuntimeAppPublic, SaturatedConversion, Saturating}; use sp_subspace_mmr::{ConsensusChainMmrLeafProof, MmrProofVerifier}; pub use staking::OperatorConfig; -use subspace_core_primitives::{BlockHash, PotOutput, SlotNumber, U256}; +use subspace_core_primitives::pot::PotOutput; +use subspace_core_primitives::{BlockHash, SlotNumber, U256}; /// Maximum number of nominators to slash within a give operator at a time. pub const MAX_NOMINATORS_TO_SLASH: u32 = 10; diff --git a/crates/pallet-subspace/src/lib.rs b/crates/pallet-subspace/src/lib.rs index d2f3012c1d..fde51ff178 100644 --- a/crates/pallet-subspace/src/lib.rs +++ b/crates/pallet-subspace/src/lib.rs @@ -114,10 +114,11 @@ pub mod pallet { 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, PotCheckpoints, PublicKey, Randomness, RewardSignature, SolutionRange, + Blake3Hash, PublicKey, Randomness, RewardSignature, SolutionRange, }; pub(super) struct InitialSolutionRanges { diff --git a/crates/pallet-subspace/src/mock.rs b/crates/pallet-subspace/src/mock.rs index 8b6a5b9216..34e25c9ef2 100644 --- a/crates/pallet-subspace/src/mock.rs +++ b/crates/pallet-subspace/src/mock.rs @@ -37,14 +37,16 @@ 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; use subspace_core_primitives::sectors::SectorId; use subspace_core_primitives::segments::{ ArchivedBlockProgress, ArchivedHistorySegment, HistorySize, LastArchivedBlock, RecordedHistorySegment, SegmentCommitment, SegmentHeader, SegmentIndex, }; use subspace_core_primitives::{ - Blake3Hash, BlockNumber, PosSeed, PotOutput, PublicKey, RewardSignature, SlotNumber, Solution, - SolutionRange, REWARD_SIGNING_CONTEXT, + Blake3Hash, BlockNumber, PublicKey, RewardSignature, SlotNumber, Solution, SolutionRange, + REWARD_SIGNING_CONTEXT, }; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::auditing::audit_sector_sync; diff --git a/crates/pallet-subspace/src/tests.rs b/crates/pallet-subspace/src/tests.rs index bdc0c5259f..e061e43ba4 100644 --- a/crates/pallet-subspace/src/tests.rs +++ b/crates/pallet-subspace/src/tests.rs @@ -43,8 +43,9 @@ 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::{PotOutput, PublicKey, RewardSignature, SolutionRange}; +use subspace_core_primitives::{PublicKey, RewardSignature, SolutionRange}; use subspace_runtime_primitives::{FindBlockRewardAddress, FindVotingRewardAddresses}; #[test] diff --git a/crates/sc-consensus-subspace/src/slot_worker.rs b/crates/sc-consensus-subspace/src/slot_worker.rs index 5c5edb87b4..3485c8f9b2 100644 --- a/crates/sc-consensus-subspace/src/slot_worker.rs +++ b/crates/sc-consensus-subspace/src/slot_worker.rs @@ -66,10 +66,10 @@ use std::marker::PhantomData; use std::pin::Pin; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; +use subspace_core_primitives::pot::{PotCheckpoints, PotOutput}; use subspace_core_primitives::sectors::SectorId; use subspace_core_primitives::{ - BlockNumber, PotCheckpoints, PotOutput, PublicKey, RewardSignature, Solution, SolutionRange, - REWARD_SIGNING_CONTEXT, + BlockNumber, PublicKey, RewardSignature, Solution, SolutionRange, REWARD_SIGNING_CONTEXT, }; use subspace_proof_of_space::Table; use subspace_verification::{ diff --git a/crates/sc-proof-of-time/src/lib.rs b/crates/sc-proof-of-time/src/lib.rs index dfde337509..a226b215b9 100644 --- a/crates/sc-proof-of-time/src/lib.rs +++ b/crates/sc-proof-of-time/src/lib.rs @@ -17,7 +17,8 @@ use sp_consensus_subspace::SubspaceApi; use sp_inherents::CreateInherentDataProviders; use sp_runtime::traits::Block as BlockT; use std::sync::Arc; -use subspace_core_primitives::{PotCheckpoints, PublicKey}; +use subspace_core_primitives::pot::PotCheckpoints; +use subspace_core_primitives::PublicKey; use tokio::sync::broadcast::error::RecvError; use tracing::{debug, error, info, trace}; diff --git a/crates/sc-proof-of-time/src/source.rs b/crates/sc-proof-of-time/src/source.rs index 0b6f922a04..b399c67077 100644 --- a/crates/sc-proof-of-time/src/source.rs +++ b/crates/sc-proof-of-time/src/source.rs @@ -24,7 +24,8 @@ use std::collections::HashSet; use std::marker::PhantomData; use std::sync::Arc; use std::thread; -use subspace_core_primitives::{PotCheckpoints, PublicKey}; +use subspace_core_primitives::pot::PotCheckpoints; +use subspace_core_primitives::PublicKey; use thread_priority::{set_current_thread_priority, ThreadPriority}; use tokio::sync::broadcast; use tracing::{debug, error, trace, warn}; diff --git a/crates/sc-proof-of-time/src/source/gossip.rs b/crates/sc-proof-of-time/src/source/gossip.rs index a3dbbdd460..69af934507 100644 --- a/crates/sc-proof-of-time/src/source/gossip.rs +++ b/crates/sc-proof-of-time/src/source/gossip.rs @@ -23,7 +23,7 @@ use std::future::poll_fn; use std::num::NonZeroU32; use std::pin::pin; use std::sync::Arc; -use subspace_core_primitives::{PotCheckpoints, PotSeed}; +use subspace_core_primitives::pot::{PotCheckpoints, PotSeed}; use tracing::{debug, error, trace, warn}; /// How many slots can proof be before it is too far diff --git a/crates/sc-proof-of-time/src/source/state.rs b/crates/sc-proof-of-time/src/source/state.rs index 6607fd0526..3638147800 100644 --- a/crates/sc-proof-of-time/src/source/state.rs +++ b/crates/sc-proof-of-time/src/source/state.rs @@ -2,7 +2,7 @@ use crate::verifier::PotVerifier; use parking_lot::Mutex; use sp_consensus_slots::Slot; use sp_consensus_subspace::{PotNextSlotInput, PotParametersChange}; -use subspace_core_primitives::PotOutput; +use subspace_core_primitives::pot::PotOutput; #[derive(Debug, Copy, Clone, Eq, PartialEq)] struct InnerState { diff --git a/crates/sc-proof-of-time/src/source/timekeeper.rs b/crates/sc-proof-of-time/src/source/timekeeper.rs index 8cebc54b17..88b65dd48e 100644 --- a/crates/sc-proof-of-time/src/source/timekeeper.rs +++ b/crates/sc-proof-of-time/src/source/timekeeper.rs @@ -6,7 +6,7 @@ use futures::SinkExt; use sp_consensus_slots::Slot; use std::num::NonZeroU32; use std::sync::Arc; -use subspace_core_primitives::{PotCheckpoints, PotSeed}; +use subspace_core_primitives::pot::{PotCheckpoints, PotSeed}; use subspace_proof_of_time::PotError; use tracing::{debug, trace}; diff --git a/crates/sc-proof-of-time/src/verifier.rs b/crates/sc-proof-of-time/src/verifier.rs index e9926a95e4..a670e87f25 100644 --- a/crates/sc-proof-of-time/src/verifier.rs +++ b/crates/sc-proof-of-time/src/verifier.rs @@ -9,7 +9,7 @@ use sp_consensus_slots::Slot; use sp_consensus_subspace::{PotNextSlotInput, PotParametersChange}; use std::num::NonZeroU32; use std::sync::Arc; -use subspace_core_primitives::{PotCheckpoints, PotOutput, PotSeed}; +use subspace_core_primitives::pot::{PotCheckpoints, PotOutput, PotSeed}; #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] struct CacheKey { diff --git a/crates/sc-proof-of-time/src/verifier/tests.rs b/crates/sc-proof-of-time/src/verifier/tests.rs index 47b33fbadd..de3d98f7ec 100644 --- a/crates/sc-proof-of-time/src/verifier/tests.rs +++ b/crates/sc-proof-of-time/src/verifier/tests.rs @@ -2,7 +2,8 @@ use crate::verifier::PotVerifier; use sp_consensus_slots::Slot; use sp_consensus_subspace::{PotNextSlotInput, PotParametersChange}; use std::num::NonZeroU32; -use subspace_core_primitives::{Blake3Hash, PotSeed}; +use subspace_core_primitives::pot::PotSeed; +use subspace_core_primitives::Blake3Hash; const SEED: [u8; 16] = [ 0xd6, 0x66, 0xcc, 0xd8, 0xd5, 0x93, 0xc2, 0x3d, 0xa8, 0xdb, 0x6b, 0x5b, 0x14, 0x13, 0xb1, 0x3a, diff --git a/crates/sp-consensus-subspace/src/digests.rs b/crates/sp-consensus-subspace/src/digests.rs index 357ab4b4cf..d34cd0955b 100644 --- a/crates/sp-consensus-subspace/src/digests.rs +++ b/crates/sp-consensus-subspace/src/digests.rs @@ -25,8 +25,9 @@ use sp_runtime::DigestItem; use sp_std::collections::btree_map::{BTreeMap, Entry}; use sp_std::fmt; use sp_std::num::NonZeroU32; +use subspace_core_primitives::pot::PotOutput; use subspace_core_primitives::segments::{SegmentCommitment, SegmentIndex}; -use subspace_core_primitives::{PotOutput, PublicKey, RewardSignature, Solution, SolutionRange}; +use subspace_core_primitives::{PublicKey, RewardSignature, Solution, SolutionRange}; /// A Subspace pre-runtime digest. This contains all data required to validate a block and for the /// Subspace runtime module. diff --git a/crates/sp-consensus-subspace/src/lib.rs b/crates/sp-consensus-subspace/src/lib.rs index fbaff60a92..b831f402fa 100644 --- a/crates/sp-consensus-subspace/src/lib.rs +++ b/crates/sp-consensus-subspace/src/lib.rs @@ -42,12 +42,13 @@ use sp_runtime_interface::{pass_by, runtime_interface}; use sp_std::num::NonZeroU32; #[cfg(feature = "std")] use subspace_core_primitives::crypto::kzg::Kzg; +use subspace_core_primitives::pot::{PotCheckpoints, PotOutput, PotSeed}; use subspace_core_primitives::segments::{ HistorySize, SegmentCommitment, SegmentHeader, SegmentIndex, }; use subspace_core_primitives::{ - Blake3Hash, BlockHash, BlockNumber, PotCheckpoints, PotOutput, PotSeed, PublicKey, - RewardSignature, SlotNumber, Solution, SolutionRange, + Blake3Hash, BlockHash, BlockNumber, PublicKey, RewardSignature, SlotNumber, Solution, + SolutionRange, }; #[cfg(feature = "std")] use subspace_proof_of_space::chia::ChiaTable; diff --git a/crates/sp-domains/src/lib.rs b/crates/sp-domains/src/lib.rs index 1a649d9dac..f4378d0fa8 100644 --- a/crates/sp-domains/src/lib.rs +++ b/crates/sp-domains/src/lib.rs @@ -68,7 +68,8 @@ use sp_weights::Weight; #[cfg(feature = "std")] use std::collections::BTreeSet; use subspace_core_primitives::crypto::blake3_hash; -use subspace_core_primitives::{bidirectional_distance, Blake3Hash, PotOutput, Randomness, U256}; +use subspace_core_primitives::pot::PotOutput; +use subspace_core_primitives::{bidirectional_distance, Blake3Hash, Randomness, U256}; use subspace_runtime_primitives::{Balance, Moment}; /// Key type for Operator. diff --git a/crates/subspace-core-primitives/src/lib.rs b/crates/subspace-core-primitives/src/lib.rs index c22d62e63f..13d6d13b3a 100644 --- a/crates/subspace-core-primitives/src/lib.rs +++ b/crates/subspace-core-primitives/src/lib.rs @@ -32,10 +32,10 @@ pub mod checksum; pub mod crypto; pub mod objects; pub mod pieces; +pub mod pos; +pub mod pot; pub mod sectors; pub mod segments; -#[cfg(feature = "serde")] -mod serde; #[cfg(test)] mod tests; @@ -44,14 +44,13 @@ extern crate alloc; use crate::crypto::{blake3_hash, blake3_hash_list, Scalar}; use crate::pieces::{ChunkWitness, PieceOffset, Record, RecordCommitment, RecordWitness}; +use crate::pos::PosProof; use crate::sectors::SectorIndex; use crate::segments::{HistorySize, SegmentIndex}; #[cfg(feature = "serde")] use ::serde::{Deserialize, Serialize}; use core::array::TryFromSliceError; use core::fmt; -use core::num::NonZeroU8; -use core::str::FromStr; use derive_more::{Add, AsMut, AsRef, Deref, DerefMut, Display, Div, From, Into, Mul, Rem, Sub}; use hex::FromHex; use num_traits::{WrappingAdd, WrappingSub}; @@ -251,245 +250,6 @@ const_assert!(solution_range_to_pieces(pieces_to_solution_range(5, (1, 6)), (1, /// The closer solution's tag is to the target, the heavier it is. pub type BlockWeight = u128; -/// Proof of space seed. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Deref)] -pub struct PosSeed([u8; Self::SIZE]); - -impl From<[u8; PosSeed::SIZE]> for PosSeed { - #[inline] - fn from(value: [u8; Self::SIZE]) -> Self { - Self(value) - } -} - -impl From for [u8; PosSeed::SIZE] { - #[inline] - fn from(value: PosSeed) -> Self { - value.0 - } -} - -impl PosSeed { - /// Size of proof of space seed in bytes. - pub const SIZE: usize = 32; -} - -/// Proof of space proof bytes. -#[derive( - Debug, Copy, Clone, Eq, PartialEq, Deref, DerefMut, Encode, Decode, TypeInfo, MaxEncodedLen, -)] -pub struct PosProof([u8; Self::SIZE]); - -impl From<[u8; PosProof::SIZE]> for PosProof { - #[inline] - fn from(value: [u8; Self::SIZE]) -> Self { - Self(value) - } -} - -impl From for [u8; PosProof::SIZE] { - #[inline] - fn from(value: PosProof) -> Self { - value.0 - } -} - -impl Default for PosProof { - #[inline] - fn default() -> Self { - Self([0; Self::SIZE]) - } -} - -impl PosProof { - /// Constant K used for proof of space - pub const K: u8 = 20; - /// Size of proof of space proof in bytes. - pub const SIZE: usize = Self::K as usize * 8; - - /// Proof hash. - pub fn hash(&self) -> Blake3Hash { - blake3_hash(&self.0) - } -} - -/// Proof of time key(input to the encryption). -#[derive( - Debug, - Default, - Copy, - Clone, - Eq, - PartialEq, - From, - AsRef, - AsMut, - Deref, - DerefMut, - Encode, - Decode, - TypeInfo, - MaxEncodedLen, -)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct PotKey(#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; Self::SIZE]); - -impl fmt::Display for PotKey { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", hex::encode(self.0)) - } -} - -impl FromStr for PotKey { - type Err = hex::FromHexError; - - #[inline] - fn from_str(s: &str) -> Result { - let mut key = Self::default(); - hex::decode_to_slice(s, key.as_mut())?; - - Ok(key) - } -} - -impl PotKey { - /// Size of proof of time key in bytes - pub const SIZE: usize = 16; -} - -/// Proof of time seed -#[derive( - Debug, - Default, - Copy, - Clone, - Eq, - PartialEq, - Hash, - From, - AsRef, - AsMut, - Deref, - DerefMut, - Encode, - Decode, - TypeInfo, - MaxEncodedLen, -)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct PotSeed(#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; Self::SIZE]); - -impl fmt::Display for PotSeed { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", hex::encode(self.0)) - } -} - -impl PotSeed { - /// Size of proof of time seed in bytes - pub const SIZE: usize = 16; - - /// Derive initial PoT seed from genesis block hash - #[inline] - pub fn from_genesis(genesis_block_hash: &[u8], external_entropy: &[u8]) -> Self { - let hash = blake3_hash_list(&[genesis_block_hash, external_entropy]); - let mut seed = Self::default(); - seed.copy_from_slice(&hash[..Self::SIZE]); - seed - } - - /// Derive key from proof of time seed - #[inline] - pub fn key(&self) -> PotKey { - let mut key = PotKey::default(); - key.copy_from_slice(&blake3_hash(&self.0)[..Self::SIZE]); - key - } -} - -/// Proof of time output, can be intermediate checkpoint or final slot output -#[derive( - Debug, - Default, - Copy, - Clone, - Eq, - PartialEq, - Hash, - From, - AsRef, - AsMut, - Deref, - DerefMut, - Encode, - Decode, - TypeInfo, - MaxEncodedLen, -)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct PotOutput(#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; Self::SIZE]); - -impl fmt::Display for PotOutput { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", hex::encode(self.0)) - } -} - -impl PotOutput { - /// Size of proof of time proof in bytes - pub const SIZE: usize = 16; - - /// Derives the global randomness from the output - #[inline] - pub fn derive_global_randomness(&self) -> Randomness { - Randomness::from(*blake3_hash(&self.0)) - } - - /// Derive seed from proof of time in case entropy injection is not needed - #[inline] - pub fn seed(&self) -> PotSeed { - PotSeed(self.0) - } - - /// Derive seed from proof of time with entropy injection - #[inline] - pub fn seed_with_entropy(&self, entropy: &Blake3Hash) -> PotSeed { - let hash = blake3_hash_list(&[entropy.as_ref(), &self.0]); - let mut seed = PotSeed::default(); - seed.copy_from_slice(&hash[..Self::SIZE]); - seed - } -} - -/// Proof of time checkpoints, result of proving -#[derive( - Debug, - Default, - Copy, - Clone, - Eq, - PartialEq, - Hash, - Deref, - DerefMut, - Encode, - Decode, - TypeInfo, - MaxEncodedLen, -)] -pub struct PotCheckpoints([PotOutput; Self::NUM_CHECKPOINTS.get() as usize]); - -impl PotCheckpoints { - /// Number of PoT checkpoints produced (used to optimize verification) - pub const NUM_CHECKPOINTS: NonZeroU8 = NonZeroU8::new(8).expect("Not zero; qed"); - - /// Get proof of time output out of checkpoints (last checkpoint) - #[inline] - pub fn output(&self) -> PotOutput { - self.0[Self::NUM_CHECKPOINTS.get() as usize - 1] - } -} - /// A Ristretto Schnorr public key as bytes produced by `schnorrkel` crate. #[derive( Debug, diff --git a/crates/subspace-core-primitives/src/pos.rs b/crates/subspace-core-primitives/src/pos.rs new file mode 100644 index 0000000000..b07e1eddf1 --- /dev/null +++ b/crates/subspace-core-primitives/src/pos.rs @@ -0,0 +1,72 @@ +//! Proof of space-related data structures. + +#[cfg(feature = "serde")] +mod serde; + +use crate::crypto::blake3_hash; +use crate::Blake3Hash; +use derive_more::{Deref, DerefMut, From}; +use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; +use scale_info::TypeInfo; + +/// Proof of space seed. +#[derive(Debug, Copy, Clone, Eq, PartialEq, Deref)] +pub struct PosSeed([u8; Self::SIZE]); + +impl From<[u8; PosSeed::SIZE]> for PosSeed { + #[inline] + fn from(value: [u8; Self::SIZE]) -> Self { + Self(value) + } +} + +impl From for [u8; PosSeed::SIZE] { + #[inline] + fn from(value: PosSeed) -> Self { + value.0 + } +} + +impl PosSeed { + /// Size of proof of space seed in bytes. + pub const SIZE: usize = 32; +} + +/// Proof of space proof bytes. +#[derive( + Debug, Copy, Clone, Eq, PartialEq, Deref, DerefMut, Encode, Decode, TypeInfo, MaxEncodedLen, +)] +pub struct PosProof([u8; Self::SIZE]); + +impl From<[u8; PosProof::SIZE]> for PosProof { + #[inline] + fn from(value: [u8; Self::SIZE]) -> Self { + Self(value) + } +} + +impl From for [u8; PosProof::SIZE] { + #[inline] + fn from(value: PosProof) -> Self { + value.0 + } +} + +impl Default for PosProof { + #[inline] + fn default() -> Self { + Self([0; Self::SIZE]) + } +} + +impl PosProof { + /// Constant K used for proof of space + pub const K: u8 = 20; + /// Size of proof of space proof in bytes. + pub const SIZE: usize = Self::K as usize * 8; + + /// Proof hash. + pub fn hash(&self) -> Blake3Hash { + blake3_hash(&self.0) + } +} diff --git a/crates/subspace-core-primitives/src/serde.rs b/crates/subspace-core-primitives/src/pos/serde.rs similarity index 99% rename from crates/subspace-core-primitives/src/serde.rs rename to crates/subspace-core-primitives/src/pos/serde.rs index 78f1c59d88..01b7a8db61 100644 --- a/crates/subspace-core-primitives/src/serde.rs +++ b/crates/subspace-core-primitives/src/pos/serde.rs @@ -1,4 +1,4 @@ -use crate::PosProof; +use crate::pos::PosProof; use hex::{decode_to_slice, FromHex, FromHexError}; use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; diff --git a/crates/subspace-core-primitives/src/pot.rs b/crates/subspace-core-primitives/src/pot.rs new file mode 100644 index 0000000000..020789559b --- /dev/null +++ b/crates/subspace-core-primitives/src/pot.rs @@ -0,0 +1,189 @@ +//! Proof of time-related data structures. + +use crate::crypto::{blake3_hash, blake3_hash_list}; +use crate::{Blake3Hash, Randomness}; +#[cfg(feature = "serde")] +use ::serde::{Deserialize, Serialize}; +use core::fmt; +use core::num::NonZeroU8; +use core::str::FromStr; +use derive_more::{AsMut, AsRef, Deref, DerefMut, From}; +use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; +use scale_info::TypeInfo; + +/// Proof of time key(input to the encryption). +#[derive( + Debug, + Default, + Copy, + Clone, + Eq, + PartialEq, + From, + AsRef, + AsMut, + Deref, + DerefMut, + Encode, + Decode, + TypeInfo, + MaxEncodedLen, +)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +pub struct PotKey(#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; Self::SIZE]); + +impl fmt::Display for PotKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", hex::encode(self.0)) + } +} + +impl FromStr for PotKey { + type Err = hex::FromHexError; + + #[inline] + fn from_str(s: &str) -> Result { + let mut key = Self::default(); + hex::decode_to_slice(s, key.as_mut())?; + + Ok(key) + } +} + +impl PotKey { + /// Size of proof of time key in bytes + pub const SIZE: usize = 16; +} + +/// Proof of time seed +#[derive( + Debug, + Default, + Copy, + Clone, + Eq, + PartialEq, + Hash, + From, + AsRef, + AsMut, + Deref, + DerefMut, + Encode, + Decode, + TypeInfo, + MaxEncodedLen, +)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +pub struct PotSeed(#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; Self::SIZE]); + +impl fmt::Display for PotSeed { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", hex::encode(self.0)) + } +} + +impl PotSeed { + /// Size of proof of time seed in bytes + pub const SIZE: usize = 16; + + /// Derive initial PoT seed from genesis block hash + #[inline] + pub fn from_genesis(genesis_block_hash: &[u8], external_entropy: &[u8]) -> Self { + let hash = blake3_hash_list(&[genesis_block_hash, external_entropy]); + let mut seed = Self::default(); + seed.copy_from_slice(&hash[..Self::SIZE]); + seed + } + + /// Derive key from proof of time seed + #[inline] + pub fn key(&self) -> PotKey { + let mut key = PotKey::default(); + key.copy_from_slice(&blake3_hash(&self.0)[..Self::SIZE]); + key + } +} + +/// Proof of time output, can be intermediate checkpoint or final slot output +#[derive( + Debug, + Default, + Copy, + Clone, + Eq, + PartialEq, + Hash, + From, + AsRef, + AsMut, + Deref, + DerefMut, + Encode, + Decode, + TypeInfo, + MaxEncodedLen, +)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +pub struct PotOutput(#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; Self::SIZE]); + +impl fmt::Display for PotOutput { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", hex::encode(self.0)) + } +} + +impl PotOutput { + /// Size of proof of time proof in bytes + pub const SIZE: usize = 16; + + /// Derives the global randomness from the output + #[inline] + pub fn derive_global_randomness(&self) -> Randomness { + Randomness::from(*blake3_hash(&self.0)) + } + + /// Derive seed from proof of time in case entropy injection is not needed + #[inline] + pub fn seed(&self) -> PotSeed { + PotSeed(self.0) + } + + /// Derive seed from proof of time with entropy injection + #[inline] + pub fn seed_with_entropy(&self, entropy: &Blake3Hash) -> PotSeed { + let hash = blake3_hash_list(&[entropy.as_ref(), &self.0]); + let mut seed = PotSeed::default(); + seed.copy_from_slice(&hash[..Self::SIZE]); + seed + } +} + +/// Proof of time checkpoints, result of proving +#[derive( + Debug, + Default, + Copy, + Clone, + Eq, + PartialEq, + Hash, + Deref, + DerefMut, + Encode, + Decode, + TypeInfo, + MaxEncodedLen, +)] +pub struct PotCheckpoints([PotOutput; Self::NUM_CHECKPOINTS.get() as usize]); + +impl PotCheckpoints { + /// Number of PoT checkpoints produced (used to optimize verification) + pub const NUM_CHECKPOINTS: NonZeroU8 = NonZeroU8::new(8).expect("Not zero; qed"); + + /// Get proof of time output out of checkpoints (last checkpoint) + #[inline] + pub fn output(&self) -> PotOutput { + self.0[Self::NUM_CHECKPOINTS.get() as usize - 1] + } +} diff --git a/crates/subspace-core-primitives/src/sectors.rs b/crates/subspace-core-primitives/src/sectors.rs index a7b507ba11..4bc36ce371 100644 --- a/crates/subspace-core-primitives/src/sectors.rs +++ b/crates/subspace-core-primitives/src/sectors.rs @@ -5,8 +5,9 @@ mod tests; use crate::crypto::{blake3_hash_list, blake3_hash_with_key}; use crate::pieces::{PieceIndex, PieceOffset, Record}; +use crate::pos::PosSeed; use crate::segments::{HistorySize, SegmentCommitment}; -use crate::{Blake3Hash, PosSeed, U256}; +use crate::{Blake3Hash, U256}; #[cfg(feature = "serde")] use ::serde::{Deserialize, Serialize}; use core::hash::Hash; diff --git a/crates/subspace-farmer-components/benches/proving.rs b/crates/subspace-farmer-components/benches/proving.rs index 4a23c38abf..06fa60a96f 100644 --- a/crates/subspace-farmer-components/benches/proving.rs +++ b/crates/subspace-farmer-components/benches/proving.rs @@ -14,9 +14,10 @@ use subspace_archiving::archiver::Archiver; use subspace_core_primitives::crypto::kzg; use subspace_core_primitives::crypto::kzg::Kzg; use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::pos::PosSeed; use subspace_core_primitives::sectors::SectorId; use subspace_core_primitives::segments::{HistorySize, RecordedHistorySegment}; -use subspace_core_primitives::{Blake3Hash, PosSeed, PublicKey, SolutionRange}; +use subspace_core_primitives::{Blake3Hash, PublicKey, SolutionRange}; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::auditing::audit_plot_sync; use subspace_farmer_components::file_ext::{FileExt, OpenOptionsExt}; diff --git a/crates/subspace-farmer-components/src/plotting.rs b/crates/subspace-farmer-components/src/plotting.rs index 786c2a39b1..354399327c 100644 --- a/crates/subspace-farmer-components/src/plotting.rs +++ b/crates/subspace-farmer-components/src/plotting.rs @@ -27,9 +27,10 @@ use std::time::Duration; use subspace_core_primitives::crypto::kzg::Kzg; use subspace_core_primitives::crypto::{blake3_hash, blake3_hash_parallel, Scalar}; use subspace_core_primitives::pieces::{PieceIndex, PieceOffset, Record}; +use subspace_core_primitives::pos::PosSeed; use subspace_core_primitives::sectors::{SBucket, SectorId, SectorIndex}; use subspace_core_primitives::segments::HistorySize; -use subspace_core_primitives::{Blake3Hash, PosSeed, PublicKey}; +use subspace_core_primitives::{Blake3Hash, PublicKey}; use subspace_erasure_coding::ErasureCoding; use subspace_proof_of_space::{Table, TableGenerator}; use thiserror::Error; diff --git a/crates/subspace-farmer-components/src/proving.rs b/crates/subspace-farmer-components/src/proving.rs index 66295fb316..8e3bd83bbc 100644 --- a/crates/subspace-farmer-components/src/proving.rs +++ b/crates/subspace-farmer-components/src/proving.rs @@ -16,8 +16,9 @@ use std::collections::VecDeque; use std::io; use subspace_core_primitives::crypto::kzg::Kzg; use subspace_core_primitives::pieces::{ChunkWitness, PieceOffset, Record}; +use subspace_core_primitives::pos::PosSeed; use subspace_core_primitives::sectors::{SBucket, SectorId}; -use subspace_core_primitives::{PosSeed, PublicKey, Solution, SolutionRange}; +use subspace_core_primitives::{PublicKey, Solution, SolutionRange}; use subspace_erasure_coding::ErasureCoding; use subspace_proof_of_space::Table; use thiserror::Error; diff --git a/crates/subspace-farmer/src/single_disk_farm/farming.rs b/crates/subspace-farmer/src/single_disk_farm/farming.rs index 9631d9a2ca..728c969288 100644 --- a/crates/subspace-farmer/src/single_disk_farm/farming.rs +++ b/crates/subspace-farmer/src/single_disk_farm/farming.rs @@ -21,9 +21,10 @@ use std::sync::Arc; use std::time::Instant; use subspace_core_primitives::crypto::kzg::Kzg; use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::pos::PosSeed; use subspace_core_primitives::sectors::SectorIndex; use subspace_core_primitives::segments::{HistorySize, SegmentIndex}; -use subspace_core_primitives::{PosSeed, PublicKey, Solution, SolutionRange}; +use subspace_core_primitives::{PublicKey, Solution, SolutionRange}; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::auditing::{audit_plot_sync, AuditingError}; use subspace_farmer_components::proving::{ProvableSolutions, ProvingError}; diff --git a/crates/subspace-malicious-operator/src/malicious_bundle_producer.rs b/crates/subspace-malicious-operator/src/malicious_bundle_producer.rs index fa1fa02219..9a66c0a694 100644 --- a/crates/subspace-malicious-operator/src/malicious_bundle_producer.rs +++ b/crates/subspace-malicious-operator/src/malicious_bundle_producer.rs @@ -30,7 +30,7 @@ use sp_runtime::{generic, RuntimeAppPublic}; use sp_transaction_pool::runtime_api::TaggedTransactionQueue; use std::error::Error; use std::sync::Arc; -use subspace_core_primitives::PotOutput; +use subspace_core_primitives::pot::PotOutput; use subspace_runtime::{ CheckHistorySeeder, DisablePallets, Runtime, RuntimeCall, SignedExtra, UncheckedExtrinsic, }; diff --git a/crates/subspace-node/src/chain_spec.rs b/crates/subspace-node/src/chain_spec.rs index 4d073de16c..5a334d9738 100644 --- a/crates/subspace-node/src/chain_spec.rs +++ b/crates/subspace-node/src/chain_spec.rs @@ -29,7 +29,8 @@ use sp_domains::PermissionedActionAllowedBy; use sp_runtime::{BoundedVec, Percent}; use std::marker::PhantomData; use std::num::NonZeroU32; -use subspace_core_primitives::{PotKey, PublicKey}; +use subspace_core_primitives::pot::PotKey; +use subspace_core_primitives::PublicKey; use subspace_runtime::{ AllowAuthoringBy, BalancesConfig, CouncilConfig, DemocracyConfig, DomainsConfig, EnableRewardsAt, HistorySeedingConfig, MaxDomainBlockSize, MaxDomainBlockWeight, RewardPoint, diff --git a/crates/subspace-proof-of-space/benches/pos.rs b/crates/subspace-proof-of-space/benches/pos.rs index a47738afa0..95166d09cb 100644 --- a/crates/subspace-proof-of-space/benches/pos.rs +++ b/crates/subspace-proof-of-space/benches/pos.rs @@ -3,7 +3,7 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; #[cfg(feature = "parallel")] use rayon::ThreadPoolBuilder; -use subspace_core_primitives::PosSeed; +use subspace_core_primitives::pos::PosSeed; use subspace_proof_of_space::{Table, TableGenerator}; fn pos_bench( diff --git a/crates/subspace-proof-of-space/src/chia.rs b/crates/subspace-proof-of-space/src/chia.rs index 0fb0418a4c..daee0ad525 100644 --- a/crates/subspace-proof-of-space/src/chia.rs +++ b/crates/subspace-proof-of-space/src/chia.rs @@ -2,7 +2,7 @@ use crate::chiapos::{Tables, TablesCache}; use crate::{PosTableType, Table, TableGenerator}; use core::mem; -use subspace_core_primitives::{PosProof, PosSeed}; +use subspace_core_primitives::pos::{PosProof, PosSeed}; const K: u8 = PosProof::K; diff --git a/crates/subspace-proof-of-space/src/lib.rs b/crates/subspace-proof-of-space/src/lib.rs index 4c2a1185a2..d4db08849a 100644 --- a/crates/subspace-proof-of-space/src/lib.rs +++ b/crates/subspace-proof-of-space/src/lib.rs @@ -18,7 +18,7 @@ pub mod chiapos; pub mod shim; use core::fmt; -use subspace_core_primitives::{PosProof, PosSeed}; +use subspace_core_primitives::pos::{PosProof, PosSeed}; /// Proof of space table type #[derive(Debug, Clone, Copy)] diff --git a/crates/subspace-proof-of-space/src/shim.rs b/crates/subspace-proof-of-space/src/shim.rs index eb3ee5f40e..ff8287c7a3 100644 --- a/crates/subspace-proof-of-space/src/shim.rs +++ b/crates/subspace-proof-of-space/src/shim.rs @@ -4,7 +4,8 @@ use crate::{PosTableType, Table, TableGenerator}; use core::iter; use subspace_core_primitives::crypto::blake3_hash; -use subspace_core_primitives::{PosProof, PosSeed, U256}; +use subspace_core_primitives::pos::{PosProof, PosSeed}; +use subspace_core_primitives::U256; /// Subspace proof of space table generator. /// diff --git a/crates/subspace-proof-of-time/benches/pot-compare-cpu-cores.rs b/crates/subspace-proof-of-time/benches/pot-compare-cpu-cores.rs index e9ed5859c5..c2c0ecc3e8 100644 --- a/crates/subspace-proof-of-time/benches/pot-compare-cpu-cores.rs +++ b/crates/subspace-proof-of-time/benches/pot-compare-cpu-cores.rs @@ -1,7 +1,7 @@ use core::num::NonZeroU32; use criterion::{black_box, criterion_group, criterion_main, Criterion}; use rand::{thread_rng, Rng}; -use subspace_core_primitives::PotSeed; +use subspace_core_primitives::pot::PotSeed; use subspace_proof_of_time::prove; fn criterion_benchmark(c: &mut Criterion) { diff --git a/crates/subspace-proof-of-time/benches/pot.rs b/crates/subspace-proof-of-time/benches/pot.rs index b2f0a82dc7..5e4dfa5f26 100644 --- a/crates/subspace-proof-of-time/benches/pot.rs +++ b/crates/subspace-proof-of-time/benches/pot.rs @@ -1,7 +1,7 @@ use core::num::NonZeroU32; use criterion::{black_box, criterion_group, criterion_main, Criterion}; use rand::{thread_rng, Rng}; -use subspace_core_primitives::PotSeed; +use subspace_core_primitives::pot::PotSeed; use subspace_proof_of_time::{prove, verify}; fn criterion_benchmark(c: &mut Criterion) { diff --git a/crates/subspace-proof-of-time/src/aes.rs b/crates/subspace-proof-of-time/src/aes.rs index 677e215f45..e4669dee49 100644 --- a/crates/subspace-proof-of-time/src/aes.rs +++ b/crates/subspace-proof-of-time/src/aes.rs @@ -11,7 +11,7 @@ use aes::cipher::{BlockCipherDecrypt, BlockCipherEncrypt, KeyInit}; use aes::Aes128; #[cfg(not(feature = "std"))] use alloc::vec::Vec; -use subspace_core_primitives::{PotCheckpoints, PotKey, PotOutput, PotSeed}; +use subspace_core_primitives::pot::{PotCheckpoints, PotKey, PotOutput, PotSeed}; /// Creates the AES based proof. #[inline(always)] diff --git a/crates/subspace-proof-of-time/src/aes/x86_64.rs b/crates/subspace-proof-of-time/src/aes/x86_64.rs index c3deefe6b5..f1873ee899 100644 --- a/crates/subspace-proof-of-time/src/aes/x86_64.rs +++ b/crates/subspace-proof-of-time/src/aes/x86_64.rs @@ -1,6 +1,6 @@ use core::arch::x86_64::*; use core::mem; -use subspace_core_primitives::PotCheckpoints; +use subspace_core_primitives::pot::PotCheckpoints; /// Create PoT proof with checkpoints #[target_feature(enable = "aes")] diff --git a/crates/subspace-proof-of-time/src/lib.rs b/crates/subspace-proof-of-time/src/lib.rs index af9a0704e1..66b237ddb4 100644 --- a/crates/subspace-proof-of-time/src/lib.rs +++ b/crates/subspace-proof-of-time/src/lib.rs @@ -4,7 +4,7 @@ mod aes; use core::num::NonZeroU32; -use subspace_core_primitives::{PotCheckpoints, PotOutput, PotSeed}; +use subspace_core_primitives::pot::{PotCheckpoints, PotOutput, PotSeed}; /// Proof of time error #[derive(Debug)] diff --git a/crates/subspace-service/src/lib.rs b/crates/subspace-service/src/lib.rs index eed719ac56..e8b461153e 100644 --- a/crates/subspace-service/src/lib.rs +++ b/crates/subspace-service/src/lib.rs @@ -127,7 +127,8 @@ use std::sync::Arc; use std::time::Duration; use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg}; use subspace_core_primitives::pieces::Record; -use subspace_core_primitives::{BlockNumber, PotSeed, PublicKey, REWARD_SIGNING_CONTEXT}; +use subspace_core_primitives::pot::PotSeed; +use subspace_core_primitives::{BlockNumber, PublicKey, REWARD_SIGNING_CONTEXT}; use subspace_erasure_coding::ErasureCoding; use subspace_networking::libp2p::multiaddr::Protocol; use subspace_networking::utils::piece_provider::PieceProvider; diff --git a/crates/subspace-verification/src/lib.rs b/crates/subspace-verification/src/lib.rs index ec558e858e..b1457753b6 100644 --- a/crates/subspace-verification/src/lib.rs +++ b/crates/subspace-verification/src/lib.rs @@ -31,11 +31,12 @@ use subspace_core_primitives::crypto::{ blake3_254_hash_to_scalar, blake3_hash_list, blake3_hash_with_key, Scalar, }; use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::pot::PotOutput; use subspace_core_primitives::sectors::{SectorId, SectorSlotChallenge}; use subspace_core_primitives::segments::{HistorySize, SegmentCommitment}; use subspace_core_primitives::{ - Blake3Hash, BlockNumber, BlockWeight, PotOutput, PublicKey, RewardSignature, SlotNumber, - Solution, SolutionRange, + Blake3Hash, BlockNumber, BlockWeight, PublicKey, RewardSignature, SlotNumber, Solution, + SolutionRange, }; use subspace_proof_of_space::Table; diff --git a/domains/client/domain-operator/src/bundle_producer_election_solver.rs b/domains/client/domain-operator/src/bundle_producer_election_solver.rs index 7c3bf0c083..fdc99277ac 100644 --- a/domains/client/domain-operator/src/bundle_producer_election_solver.rs +++ b/domains/client/domain-operator/src/bundle_producer_election_solver.rs @@ -13,7 +13,7 @@ use sp_runtime::traits::Block as BlockT; use sp_runtime::RuntimeAppPublic; use std::marker::PhantomData; use std::sync::Arc; -use subspace_core_primitives::PotOutput; +use subspace_core_primitives::pot::PotOutput; use subspace_runtime_primitives::Balance; use tracing::log; diff --git a/domains/client/domain-operator/src/lib.rs b/domains/client/domain-operator/src/lib.rs index af221cb3f6..dd1bfc7dba 100644 --- a/domains/client/domain-operator/src/lib.rs +++ b/domains/client/domain-operator/src/lib.rs @@ -97,7 +97,7 @@ use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor}; use sp_runtime::DigestItem; use std::marker::PhantomData; use std::sync::Arc; -use subspace_core_primitives::PotOutput; +use subspace_core_primitives::pot::PotOutput; use subspace_runtime_primitives::Balance; pub type ExecutionReceiptFor = ExecutionReceipt< diff --git a/domains/client/domain-operator/src/tests.rs b/domains/client/domain-operator/src/tests.rs index 56f8d8f629..f4c0eacff3 100644 --- a/domains/client/domain-operator/src/tests.rs +++ b/domains/client/domain-operator/src/tests.rs @@ -63,7 +63,7 @@ use sp_transaction_pool::runtime_api::TaggedTransactionQueue; use sp_weights::Weight; use std::collections::{BTreeMap, VecDeque}; use std::sync::Arc; -use subspace_core_primitives::PotOutput; +use subspace_core_primitives::pot::PotOutput; use subspace_runtime_primitives::opaque::Block as CBlock; use subspace_runtime_primitives::{Balance, SSC}; use subspace_test_service::{ diff --git a/domains/client/domain-operator/src/utils.rs b/domains/client/domain-operator/src/utils.rs index 5293b79d4a..cb1c410154 100644 --- a/domains/client/domain-operator/src/utils.rs +++ b/domains/client/domain-operator/src/utils.rs @@ -3,7 +3,7 @@ use sc_utils::mpsc::{TracingUnboundedReceiver, TracingUnboundedSender}; use sp_consensus_slots::Slot; use sp_runtime::traits::{Block as BlockT, NumberFor}; use std::sync::Arc; -use subspace_core_primitives::PotOutput; +use subspace_core_primitives::pot::PotOutput; /// Data required to produce bundles on executor node. #[derive(PartialEq, Clone, Debug)] diff --git a/domains/service/src/domain.rs b/domains/service/src/domain.rs index 9d26b5779e..b9420fd59b 100644 --- a/domains/service/src/domain.rs +++ b/domains/service/src/domain.rs @@ -47,7 +47,7 @@ use std::fmt::{Debug, Display}; use std::marker::PhantomData; use std::str::FromStr; use std::sync::Arc; -use subspace_core_primitives::PotOutput; +use subspace_core_primitives::pot::PotOutput; use subspace_runtime_primitives::Nonce; use substrate_frame_rpc_system::AccountNonceApi; diff --git a/shared/subspace-proof-of-space-gpu/src/cuda.rs b/shared/subspace-proof-of-space-gpu/src/cuda.rs index 3441244644..43c599d9d0 100644 --- a/shared/subspace-proof-of-space-gpu/src/cuda.rs +++ b/shared/subspace-proof-of-space-gpu/src/cuda.rs @@ -9,7 +9,7 @@ use rust_kzg_blst::types::fr::FsFr; use std::ops::DerefMut; use subspace_core_primitives::crypto::Scalar; use subspace_core_primitives::pieces::Record; -use subspace_core_primitives::{PosProof, PosSeed}; +use subspace_core_primitives::pos::{PosProof, PosSeed}; extern "C" { /// # Returns diff --git a/test/subspace-test-client/src/lib.rs b/test/subspace-test-client/src/lib.rs index 7c5674c412..916b9c8ccc 100644 --- a/test/subspace-test-client/src/lib.rs +++ b/test/subspace-test-client/src/lib.rs @@ -37,10 +37,9 @@ use std::sync::Arc; use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg}; use subspace_core_primitives::objects::BlockObjectMapping; use subspace_core_primitives::pieces::Record; +use subspace_core_primitives::pos::PosSeed; use subspace_core_primitives::segments::{HistorySize, SegmentIndex}; -use subspace_core_primitives::{ - PosSeed, PublicKey, RewardSignature, Solution, REWARD_SIGNING_CONTEXT, -}; +use subspace_core_primitives::{PublicKey, RewardSignature, Solution, REWARD_SIGNING_CONTEXT}; use subspace_erasure_coding::ErasureCoding; use subspace_farmer_components::auditing::audit_sector_sync; use subspace_farmer_components::plotting::{ diff --git a/test/subspace-test-service/src/lib.rs b/test/subspace-test-service/src/lib.rs index e2a531452c..2304a7d116 100644 --- a/test/subspace-test-service/src/lib.rs +++ b/test/subspace-test-service/src/lib.rs @@ -83,7 +83,8 @@ use std::marker::PhantomData; use std::pin::Pin; use std::sync::Arc; use std::time; -use subspace_core_primitives::{BlockNumber, PotOutput, PublicKey, Solution}; +use subspace_core_primitives::pot::PotOutput; +use subspace_core_primitives::{BlockNumber, PublicKey, Solution}; use subspace_runtime_primitives::opaque::Block; use subspace_runtime_primitives::{AccountId, Balance, Hash, Signature}; use subspace_service::transaction_pool::FullPool; From c9d52ce6f740e1f11f9d591124a72ed3c1cb1ac0 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Wed, 2 Oct 2024 17:47:44 +0300 Subject: [PATCH 5/6] Move `ChunkWitness` out of `pieces` module --- crates/subspace-core-primitives/src/lib.rs | 88 ++++++++++++++++++- crates/subspace-core-primitives/src/pieces.rs | 83 ----------------- .../subspace-farmer-components/src/proving.rs | 4 +- 3 files changed, 89 insertions(+), 86 deletions(-) diff --git a/crates/subspace-core-primitives/src/lib.rs b/crates/subspace-core-primitives/src/lib.rs index 13d6d13b3a..779753c135 100644 --- a/crates/subspace-core-primitives/src/lib.rs +++ b/crates/subspace-core-primitives/src/lib.rs @@ -42,13 +42,16 @@ mod tests; #[cfg(not(feature = "std"))] extern crate alloc; +use crate::crypto::kzg::Witness; use crate::crypto::{blake3_hash, blake3_hash_list, Scalar}; -use crate::pieces::{ChunkWitness, PieceOffset, Record, RecordCommitment, RecordWitness}; +use crate::pieces::{PieceOffset, Record, RecordCommitment, RecordWitness}; use crate::pos::PosProof; use crate::sectors::SectorIndex; use crate::segments::{HistorySize, SegmentIndex}; #[cfg(feature = "serde")] use ::serde::{Deserialize, Serialize}; +#[cfg(not(feature = "std"))] +use alloc::string::String; use core::array::TryFromSliceError; use core::fmt; use derive_more::{Add, AsMut, AsRef, Deref, DerefMut, Display, Div, From, Into, Mul, Rem, Sub}; @@ -326,6 +329,89 @@ impl RewardSignature { pub const SIZE: usize = 64; } +/// Witness for chunk contained within a record. +#[derive( + Debug, + Copy, + Clone, + Eq, + PartialEq, + Hash, + Deref, + DerefMut, + From, + Into, + Encode, + Decode, + TypeInfo, + MaxEncodedLen, +)] +#[repr(transparent)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +pub struct ChunkWitness( + #[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; ChunkWitness::SIZE], +); + +impl Default for ChunkWitness { + #[inline] + fn default() -> Self { + Self([0; Self::SIZE]) + } +} + +impl TryFrom<&[u8]> for ChunkWitness { + type Error = TryFromSliceError; + + #[inline] + fn try_from(slice: &[u8]) -> Result { + <[u8; Self::SIZE]>::try_from(slice).map(Self) + } +} + +impl AsRef<[u8]> for ChunkWitness { + #[inline] + fn as_ref(&self) -> &[u8] { + &self.0 + } +} + +impl AsMut<[u8]> for ChunkWitness { + #[inline] + fn as_mut(&mut self) -> &mut [u8] { + &mut self.0 + } +} + +impl ChunkWitness { + /// Size of chunk witness in bytes. + pub const SIZE: usize = 48; +} + +impl From for ChunkWitness { + #[inline] + fn from(witness: Witness) -> Self { + Self(witness.to_bytes()) + } +} + +impl TryFrom<&ChunkWitness> for Witness { + type Error = String; + + #[inline] + fn try_from(witness: &ChunkWitness) -> Result { + Witness::try_from(&witness.0) + } +} + +impl TryFrom for Witness { + type Error = String; + + #[inline] + fn try_from(witness: ChunkWitness) -> Result { + Witness::try_from(witness.0) + } +} + /// Farmer solution for slot challenge. #[derive(Clone, Debug, Eq, PartialEq, Encode, Decode, TypeInfo)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] diff --git a/crates/subspace-core-primitives/src/pieces.rs b/crates/subspace-core-primitives/src/pieces.rs index 0ab07ab63f..be84f10c3e 100644 --- a/crates/subspace-core-primitives/src/pieces.rs +++ b/crates/subspace-core-primitives/src/pieces.rs @@ -829,89 +829,6 @@ impl TryFrom for Witness { } } -/// Witness for chunk contained within a record. -#[derive( - Debug, - Copy, - Clone, - Eq, - PartialEq, - Hash, - Deref, - DerefMut, - From, - Into, - Encode, - Decode, - TypeInfo, - MaxEncodedLen, -)] -#[repr(transparent)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct ChunkWitness( - #[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; ChunkWitness::SIZE], -); - -impl Default for ChunkWitness { - #[inline] - fn default() -> Self { - Self([0; Self::SIZE]) - } -} - -impl TryFrom<&[u8]> for ChunkWitness { - type Error = TryFromSliceError; - - #[inline] - fn try_from(slice: &[u8]) -> Result { - <[u8; Self::SIZE]>::try_from(slice).map(Self) - } -} - -impl AsRef<[u8]> for ChunkWitness { - #[inline] - fn as_ref(&self) -> &[u8] { - &self.0 - } -} - -impl AsMut<[u8]> for ChunkWitness { - #[inline] - fn as_mut(&mut self) -> &mut [u8] { - &mut self.0 - } -} - -impl ChunkWitness { - /// Size of chunk witness in bytes. - pub const SIZE: usize = 48; -} - -impl From for ChunkWitness { - #[inline] - fn from(witness: Witness) -> Self { - Self(witness.to_bytes()) - } -} - -impl TryFrom<&ChunkWitness> for Witness { - type Error = String; - - #[inline] - fn try_from(witness: &ChunkWitness) -> Result { - Witness::try_from(&witness.0) - } -} - -impl TryFrom for Witness { - type Error = String; - - #[inline] - fn try_from(witness: ChunkWitness) -> Result { - Witness::try_from(witness.0) - } -} - #[derive(Debug)] enum CowBytes { Shared(Bytes), diff --git a/crates/subspace-farmer-components/src/proving.rs b/crates/subspace-farmer-components/src/proving.rs index 8e3bd83bbc..4530e58e6c 100644 --- a/crates/subspace-farmer-components/src/proving.rs +++ b/crates/subspace-farmer-components/src/proving.rs @@ -15,10 +15,10 @@ use futures::FutureExt; use std::collections::VecDeque; use std::io; use subspace_core_primitives::crypto::kzg::Kzg; -use subspace_core_primitives::pieces::{ChunkWitness, PieceOffset, Record}; +use subspace_core_primitives::pieces::{PieceOffset, Record}; use subspace_core_primitives::pos::PosSeed; use subspace_core_primitives::sectors::{SBucket, SectorId}; -use subspace_core_primitives::{PublicKey, Solution, SolutionRange}; +use subspace_core_primitives::{ChunkWitness, PublicKey, Solution, SolutionRange}; use subspace_erasure_coding::ErasureCoding; use subspace_proof_of_space::Table; use thiserror::Error; From 14e2858636e53f8b897fc7b0f57bb42a954d25df Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Wed, 2 Oct 2024 18:32:11 +0300 Subject: [PATCH 6/6] Fix clippy for benchmarks --- crates/pallet-subspace/src/benchmarking.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/pallet-subspace/src/benchmarking.rs b/crates/pallet-subspace/src/benchmarking.rs index 256aeb4a61..27b9bdd856 100644 --- a/crates/pallet-subspace/src/benchmarking.rs +++ b/crates/pallet-subspace/src/benchmarking.rs @@ -21,9 +21,12 @@ mod benchmarks { use sp_consensus_subspace::{SignedVote, Vote}; use sp_std::boxed::Box; use sp_std::num::NonZeroU32; + use subspace_core_primitives::pot::{PotCheckpoints, PotOutput}; + use subspace_core_primitives::segments::{ + ArchivedBlockProgress, LastArchivedBlock, SegmentHeader, SegmentIndex, + }; use subspace_core_primitives::{ - ArchivedBlockProgress, Blake3Hash, LastArchivedBlock, PotCheckpoints, PotOutput, PublicKey, - RewardSignature, SegmentHeader, SegmentIndex, Solution, SolutionRange, + Blake3Hash, PublicKey, RewardSignature, Solution, SolutionRange, }; const SEED: u32 = 0; @@ -153,7 +156,7 @@ mod benchmarks { fn create_segment_header(segment_index: SegmentIndex) -> SegmentHeader { SegmentHeader::V0 { segment_index, - segment_commitment: subspace_core_primitives::SegmentCommitment::default(), + segment_commitment: subspace_core_primitives::segments::SegmentCommitment::default(), prev_segment_header_hash: Blake3Hash::default(), last_archived_block: LastArchivedBlock { number: 0,