Skip to content

Commit

Permalink
Merge pull request #1642 from subspace/farmer-refactoring-and-cleanups
Browse files Browse the repository at this point in the history
Farmer refactoring and cleanups
  • Loading branch information
nazar-pc authored Jul 6, 2023
2 parents 6e7f8c7 + 1262a79 commit ba0ee8b
Show file tree
Hide file tree
Showing 19 changed files with 192 additions and 311 deletions.
4 changes: 1 addition & 3 deletions crates/pallet-subspace/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,12 @@ pub fn create_signed_vote(
let pieces_in_sector = farmer_protocol_info.max_pieces_in_sector;
let sector_size = sector_size(pieces_in_sector);

for (sector_offset, sector_index) in iter::from_fn(|| Some(rand::random())).enumerate() {
for sector_index in iter::from_fn(|| Some(rand::random())) {
let mut plotted_sector_bytes = vec![0; sector_size];
let mut plotted_sector_metadata_bytes = vec![0; SectorMetadata::encoded_size()];

let plotted_sector = block_on(plot_sector::<_, PosTable>(
&public_key,
sector_offset,
sector_index,
archived_history_segment,
PieceGetterRetryPolicy::default(),
Expand All @@ -426,7 +425,6 @@ pub fn create_signed_vote(
pieces_in_sector,
&mut plotted_sector_bytes,
&mut plotted_sector_metadata_bytes,
Default::default(),
))
.unwrap();

Expand Down
4 changes: 1 addition & 3 deletions crates/sp-lightclient/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,12 @@ fn valid_header(
let pieces_in_sector = farmer_parameters.farmer_protocol_info.max_pieces_in_sector;
let sector_size = sector_size(pieces_in_sector);

for (sector_offset, sector_index) in iter::from_fn(|| Some(rand::random())).enumerate() {
for sector_index in iter::from_fn(|| Some(rand::random())) {
let mut plotted_sector_bytes = vec![0; sector_size];
let mut plotted_sector_metadata_bytes = vec![0; SectorMetadata::encoded_size()];

let plotted_sector = block_on(plot_sector::<_, PosTable>(
&public_key,
sector_offset,
sector_index,
&farmer_parameters.archived_segment.pieces,
PieceGetterRetryPolicy::default(),
Expand All @@ -169,7 +168,6 @@ fn valid_header(
pieces_in_sector,
&mut plotted_sector_bytes,
&mut plotted_sector_metadata_bytes,
Default::default(),
))
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-core-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ impl SegmentHeader {
}

/// Sector index in consensus
pub type SectorIndex = u64;
pub type SectorIndex = u16;

// TODO: Versioned solution enum
/// Farmer solution for slot challenge.
Expand Down
9 changes: 3 additions & 6 deletions crates/subspace-farmer-components/benches/auditing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use subspace_archiving::archiver::Archiver;
use subspace_core_primitives::crypto::kzg;
use subspace_core_primitives::crypto::kzg::Kzg;
use subspace_core_primitives::{
Blake2b256Hash, HistorySize, PublicKey, Record, RecordedHistorySegment, SectorId, SegmentIndex,
SolutionRange,
Blake2b256Hash, HistorySize, PublicKey, Record, RecordedHistorySegment, SectorId, SectorIndex,
SegmentIndex, SolutionRange,
};
use subspace_erasure_coding::ErasureCoding;
use subspace_farmer_components::auditing::audit_sector;
Expand Down Expand Up @@ -42,7 +42,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {
.unwrap_or(10);

let public_key = PublicKey::default();
let sector_offset = 0;
let sector_index = 0;
let mut input = RecordedHistorySegment::new_boxed();
StdRng::seed_from_u64(42).fill(AsMut::<[u8]>::as_mut(input.as_mut()));
Expand Down Expand Up @@ -116,7 +115,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {

let plotted_sector = block_on(plot_sector::<_, PosTable>(
&public_key,
sector_offset,
sector_index,
&archived_history_segment,
PieceGetterRetryPolicy::default(),
Expand All @@ -126,7 +124,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {
pieces_in_sector,
&mut plotted_sector_bytes,
&mut plotted_sector_metadata_bytes,
Default::default(),
))
.unwrap();

Expand Down Expand Up @@ -196,7 +193,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
for (sector_index, sector) in plot_mmap
.chunks_exact(sector_size)
.enumerate()
.map(|(sector_index, sector)| (sector_index as u64, sector))
.map(|(sector_index, sector)| (sector_index as SectorIndex, sector))
{
audit_sector(
black_box(&public_key),
Expand Down
3 changes: 0 additions & 3 deletions crates/subspace-farmer-components/benches/plotting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ fn criterion_benchmark(c: &mut Criterion) {
.unwrap_or_else(|_error| MAX_PIECES_IN_SECTOR);

let public_key = PublicKey::default();
let sector_offset = 0;
let sector_index = 0;
let mut input = RecordedHistorySegment::new_boxed();
StdRng::seed_from_u64(42).fill(AsMut::<[u8]>::as_mut(input.as_mut()));
Expand Down Expand Up @@ -67,7 +66,6 @@ fn criterion_benchmark(c: &mut Criterion) {
b.iter(|| {
block_on(plot_sector::<_, PosTable>(
black_box(&public_key),
black_box(sector_offset),
black_box(sector_index),
black_box(&archived_history_segment),
black_box(PieceGetterRetryPolicy::default()),
Expand All @@ -77,7 +75,6 @@ fn criterion_benchmark(c: &mut Criterion) {
black_box(pieces_in_sector),
black_box(&mut sector_bytes),
black_box(&mut sector_metadata_bytes),
Default::default(),
))
.unwrap();
})
Expand Down
3 changes: 0 additions & 3 deletions crates/subspace-farmer-components/benches/proving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {

let keypair = Keypair::from_bytes(&[0; 96]).unwrap();
let public_key = PublicKey::from(keypair.public.to_bytes());
let sector_offset = 0;
let sector_index = 0;
let mut input = RecordedHistorySegment::new_boxed();
let mut rng = StdRng::seed_from_u64(42);
Expand Down Expand Up @@ -119,7 +118,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {

let plotted_sector = block_on(plot_sector::<_, PosTable>(
&public_key,
sector_offset,
sector_index,
&archived_history_segment,
PieceGetterRetryPolicy::default(),
Expand All @@ -129,7 +127,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {
pieces_in_sector,
&mut plotted_sector_bytes,
&mut plotted_sector_metadata_bytes,
Default::default(),
))
.unwrap();

Expand Down
3 changes: 0 additions & 3 deletions crates/subspace-farmer-components/benches/reading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {
.unwrap_or(10);

let public_key = PublicKey::default();
let sector_offset = 0;
let sector_index = 0;
let mut input = RecordedHistorySegment::new_boxed();
StdRng::seed_from_u64(42).fill(AsMut::<[u8]>::as_mut(input.as_mut()));
Expand Down Expand Up @@ -113,7 +112,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {

let plotted_sector = block_on(plot_sector::<_, PosTable>(
&public_key,
sector_offset,
sector_index,
&archived_history_segment,
PieceGetterRetryPolicy::default(),
Expand All @@ -123,7 +121,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {
pieces_in_sector,
&mut plotted_sector_bytes,
&mut plotted_sector_metadata_bytes,
Default::default(),
))
.unwrap();

Expand Down
4 changes: 2 additions & 2 deletions crates/subspace-farmer-components/src/auditing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::sector::{SectorContentsMap, SectorMetadata};
use std::collections::VecDeque;
use std::mem;
use subspace_core_primitives::crypto::Scalar;
use subspace_core_primitives::{Blake2b256Hash, PublicKey, SectorId, SolutionRange};
use subspace_core_primitives::{Blake2b256Hash, PublicKey, SectorId, SectorIndex, SolutionRange};
use subspace_verification::is_within_solution_range;

#[derive(Debug, Clone)]
Expand All @@ -19,7 +19,7 @@ pub(crate) struct ChunkCandidate {
/// and seek back afterwards if necessary).
pub fn audit_sector<'a>(
public_key: &'a PublicKey,
sector_index: u64,
sector_index: SectorIndex,
global_challenge: &Blake2b256Hash,
solution_range: SolutionRange,
sector: &'a [u8],
Expand Down
1 change: 0 additions & 1 deletion crates/subspace-farmer-components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

pub mod auditing;
pub mod file_ext;
pub mod piece_caching;
pub mod plotting;
pub mod proving;
pub mod reading;
Expand Down
51 changes: 0 additions & 51 deletions crates/subspace-farmer-components/src/piece_caching.rs

This file was deleted.

23 changes: 3 additions & 20 deletions crates/subspace-farmer-components/src/plotting.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::piece_caching::PieceMemoryCache;
use crate::sector::{
sector_record_chunks_size, sector_size, RawSector, RecordMetadata, SectorContentsMap,
SectorMetadata,
Expand Down Expand Up @@ -161,8 +160,7 @@ pub enum PlottingError {
#[allow(clippy::too_many_arguments)]
pub async fn plot_sector<PG, PosTable>(
public_key: &PublicKey,
sector_offset: usize,
sector_index: u64,
sector_index: SectorIndex,
piece_getter: &PG,
piece_getter_retry_policy: PieceGetterRetryPolicy,
farmer_protocol_info: &FarmerProtocolInfo,
Expand All @@ -171,7 +169,6 @@ pub async fn plot_sector<PG, PosTable>(
pieces_in_sector: u16,
sector_output: &mut [u8],
sector_metadata_output: &mut [u8],
piece_memory_cache: PieceMemoryCache,
) -> Result<PlottedSector, PlottingError>
where
PG: PieceGetter,
Expand Down Expand Up @@ -224,18 +221,14 @@ where

if let Err(error) = download_sector(
&mut raw_sector,
sector_offset,
sector_index,
piece_getter,
piece_getter_retry_policy,
kzg,
&piece_indexes,
piece_memory_cache.clone(),
)
.await
{
warn!(
%sector_offset,
%sector_index,
%error,
"Sector plotting attempt failed, will retry later"
Expand All @@ -244,6 +237,8 @@ where
return Err(BackoffError::transient(error));
}

debug!(%sector_index, "Sector downloaded successfully");

Ok(())
})
.await?;
Expand Down Expand Up @@ -400,16 +395,12 @@ where
})
}

#[allow(clippy::too_many_arguments)]
async fn download_sector<PG: PieceGetter>(
raw_sector: &mut RawSector,
sector_offset: usize,
sector_index: u64,
piece_getter: &PG,
piece_getter_retry_policy: PieceGetterRetryPolicy,
kzg: &Kzg,
piece_indexes: &[PieceIndex],
piece_memory_cache: PieceMemoryCache,
) -> Result<(), PlottingError> {
// TODO: Make configurable, likely allowing user to specify RAM usage expectations and inferring
// concurrency from there
Expand All @@ -420,10 +411,6 @@ async fn download_sector<PG: PieceGetter>(
.map(|piece_index| async {
let piece_index = *piece_index;

if let Some(piece) = piece_memory_cache.get_piece(&piece_index.hash()) {
return (piece_index, Ok(Some(piece)));
}

let piece_result = piece_getter
.get_piece(piece_index, piece_getter_retry_policy)
.await;
Expand Down Expand Up @@ -469,11 +456,7 @@ async fn download_sector<PG: PieceGetter>(
commitment: *commitment,
witness: *witness,
});

piece_memory_cache.add_piece(piece_index.hash(), piece);
}

debug!(%sector_offset, %sector_index, "Sector downloaded successfully");

Ok(())
}
Loading

0 comments on commit ba0ee8b

Please sign in to comment.