Skip to content

Commit

Permalink
Restore audit_sector as audit_sector_sync for usage in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed Oct 19, 2023
1 parent e64e915 commit 2be7ff9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
11 changes: 5 additions & 6 deletions crates/pallet-subspace/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use std::marker::PhantomData;
use std::num::{NonZeroU32, NonZeroU64, NonZeroUsize};
use std::simd::Simd;
use std::sync::{Once, OnceLock};
use std::{iter, mem, slice};
use std::{iter, mem};
use subspace_archiving::archiver::{Archiver, NewArchivedSegment};
use subspace_core_primitives::crypto::kzg::{embedded_kzg_settings, Kzg};
use subspace_core_primitives::crypto::Scalar;
Expand All @@ -55,7 +55,7 @@ use subspace_core_primitives::{
Solution, SolutionRange, REWARD_SIGNING_CONTEXT,
};
use subspace_erasure_coding::ErasureCoding;
use subspace_farmer_components::auditing::audit_plot_sync;
use subspace_farmer_components::auditing::audit_sector_sync;
use subspace_farmer_components::plotting::{
plot_sector, PieceGetterRetryPolicy, PlotSectorOptions,
};
Expand Down Expand Up @@ -479,16 +479,15 @@ pub fn create_signed_vote(
.derive_global_randomness()
.derive_global_challenge(slot.into());

let maybe_audit_result = audit_plot_sync(
let maybe_audit_result = audit_sector_sync(
&public_key,
&global_challenge,
vote_solution_range,
&plotted_sector_bytes,
slice::from_ref(&plotted_sector.sector_metadata),
None,
&plotted_sector.sector_metadata,
);

let Some(audit_result) = maybe_audit_result.into_iter().next() else {
let Some(audit_result) = maybe_audit_result else {
// Sector didn't have any solutions
continue;
};
Expand Down
56 changes: 56 additions & 0 deletions crates/subspace-farmer-components/src/auditing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,62 @@ pub(crate) struct ChunkCandidate {
pub(crate) audit_chunks: Vec<AuditChunkCandidate>,
}

/// Audit a single sector and generate a stream of solutions.
///
/// This is primarily helpful in test environment, prefer [`audit_plot_sync`] and
/// [`audit_plot_async`] for auditing real plots.
pub fn audit_sector_sync<'a, Sector>(
public_key: &'a PublicKey,
global_challenge: &Blake3Hash,
solution_range: SolutionRange,
sector: Sector,
sector_metadata: &'a SectorMetadataChecksummed,
) -> Option<AuditResult<'a, ReadAt<Sector, !>>>
where
Sector: ReadAtSync + 'a,
{
let SectorAuditingDetails {
sector_id,
sector_slot_challenge,
s_bucket_audit_index,
s_bucket_audit_size,
s_bucket_audit_offset_in_sector,
} = collect_sector_auditing_details(public_key, global_challenge, sector_metadata);

let mut s_bucket = vec![0; s_bucket_audit_size];
let read_s_bucket_result = sector.read_at(&mut s_bucket, s_bucket_audit_offset_in_sector);

if let Err(error) = read_s_bucket_result {
warn!(
%error,
sector_index = %sector_metadata.sector_index,
%s_bucket_audit_index,
"Failed read s-bucket",
);
return None;
}

let (winning_chunks, best_solution_distance) = map_winning_chunks(
&s_bucket,
global_challenge,
&sector_slot_challenge,
solution_range,
)?;

Some(AuditResult {
sector_index: sector_metadata.sector_index,
solution_candidates: SolutionCandidates::new(
public_key,
sector_id,
s_bucket_audit_index,
ReadAt::from_sync(sector),
sector_metadata,
winning_chunks.into(),
),
best_solution_distance,
})
}

/// Audit the whole plot and generate streams of solutions
pub fn audit_plot_sync<'a, Plot>(
public_key: &'a PublicKey,
Expand Down
10 changes: 3 additions & 7 deletions test/subspace-test-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ use sp_api::ProvideRuntimeApi;
use sp_consensus_subspace::{FarmerPublicKey, FarmerSignature, SubspaceApi};
use sp_core::{Decode, Encode};
use std::num::{NonZeroU64, NonZeroUsize};
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::{
HistorySize, PosSeed, PublicKey, Record, SegmentIndex, Solution, REWARD_SIGNING_CONTEXT,
};
use subspace_erasure_coding::ErasureCoding;
use subspace_farmer_components::auditing::audit_plot_sync;
use subspace_farmer_components::auditing::audit_sector_sync;
use subspace_farmer_components::plotting::{
plot_sector, PieceGetterRetryPolicy, PlotSectorOptions, PlottedSector,
};
Expand Down Expand Up @@ -189,18 +188,15 @@ async fn start_farming<PosTable, Client>(
let global_challenge = new_slot_info
.global_randomness
.derive_global_challenge(new_slot_info.slot.into());
let audit_result = audit_plot_sync(
let audit_result = audit_sector_sync(
&public_key,
&global_challenge,
new_slot_info.solution_range,
&sector,
slice::from_ref(&plotted_sector.sector_metadata),
None,
&plotted_sector.sector_metadata,
);

let solution = audit_result
.into_iter()
.next()
.unwrap()
.solution_candidates
.into_solutions(&public_key, &kzg, &erasure_coding, |seed: &PosSeed| {
Expand Down

0 comments on commit 2be7ff9

Please sign in to comment.