From 2be7ff9de239d9a6750d6fe49d19f94c64f02901 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Thu, 19 Oct 2023 04:56:57 +0300 Subject: [PATCH] Restore `audit_sector` as `audit_sector_sync` for usage in tests --- crates/pallet-subspace/src/mock.rs | 11 ++-- .../src/auditing.rs | 56 +++++++++++++++++++ test/subspace-test-client/src/lib.rs | 10 +--- 3 files changed, 64 insertions(+), 13 deletions(-) diff --git a/crates/pallet-subspace/src/mock.rs b/crates/pallet-subspace/src/mock.rs index 9f59f82ed4..fb71e88efc 100644 --- a/crates/pallet-subspace/src/mock.rs +++ b/crates/pallet-subspace/src/mock.rs @@ -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; @@ -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, }; @@ -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; }; diff --git a/crates/subspace-farmer-components/src/auditing.rs b/crates/subspace-farmer-components/src/auditing.rs index e5839b90ad..3c09bc84b0 100644 --- a/crates/subspace-farmer-components/src/auditing.rs +++ b/crates/subspace-farmer-components/src/auditing.rs @@ -45,6 +45,62 @@ pub(crate) struct ChunkCandidate { pub(crate) audit_chunks: Vec, } +/// 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>> +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, + §or_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, diff --git a/test/subspace-test-client/src/lib.rs b/test/subspace-test-client/src/lib.rs index 8e5fa4b82a..a3f4d50c63 100644 --- a/test/subspace-test-client/src/lib.rs +++ b/test/subspace-test-client/src/lib.rs @@ -31,7 +31,6 @@ 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; @@ -39,7 +38,7 @@ 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, }; @@ -189,18 +188,15 @@ async fn start_farming( 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, §or, - 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| {