Skip to content

Commit e0fe46d

Browse files
committed
Replace SingleDiskFarm::on_sector_[plotting|plotted]() with SingleDiskFarm::on_sector_update() that will be extended later
1 parent 031cbf5 commit e0fe46d

File tree

4 files changed

+58
-41
lines changed

4 files changed

+58
-41
lines changed

crates/subspace-farmer-components/src/plotting.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use backoff::future::retry;
1010
use backoff::{Error as BackoffError, ExponentialBackoff};
1111
use futures::stream::FuturesUnordered;
1212
use futures::StreamExt;
13-
use parity_scale_codec::Encode;
13+
use parity_scale_codec::{Decode, Encode};
1414
use rayon::prelude::*;
1515
use std::error::Error;
1616
use std::mem;
@@ -97,7 +97,7 @@ impl PieceGetter for ArchivedHistorySegment {
9797
}
9898

9999
/// Information about sector that was plotted
100-
#[derive(Debug, Clone)]
100+
#[derive(Debug, Clone, Encode, Decode)]
101101
pub struct PlottedSector {
102102
/// Sector ID
103103
pub sector_id: SectorId,

crates/subspace-farmer/src/bin/subspace-farmer/commands/farm.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use subspace_core_primitives::{PublicKey, Record, SectorIndex};
2323
use subspace_erasure_coding::ErasureCoding;
2424
use subspace_farmer::piece_cache::PieceCache;
2525
use subspace_farmer::single_disk_farm::{
26-
SingleDiskFarm, SingleDiskFarmError, SingleDiskFarmOptions,
26+
SectorPlottingDetails, SectorUpdate, SingleDiskFarm, SingleDiskFarmError, SingleDiskFarmOptions,
2727
};
2828
use subspace_farmer::utils::farmer_piece_getter::FarmerPieceGetter;
2929
use subspace_farmer::utils::piece_validator::SegmentCommitmentPieceValidator;
@@ -633,10 +633,8 @@ where
633633

634634
// Collect newly plotted pieces
635635
let on_plotted_sector_callback =
636-
move |(plotted_sector, maybe_old_plotted_sector): &(
637-
PlottedSector,
638-
Option<PlottedSector>,
639-
)| {
636+
move |plotted_sector: &PlottedSector,
637+
maybe_old_plotted_sector: &Option<PlottedSector>| {
640638
let _span_guard = span.enter();
641639

642640
{
@@ -645,7 +643,7 @@ where
645643
.as_mut()
646644
.expect("Initial value was populated above; qed");
647645

648-
if let Some(old_plotted_sector) = maybe_old_plotted_sector {
646+
if let Some(old_plotted_sector) = &maybe_old_plotted_sector {
649647
readers_and_pieces.delete_sector(disk_farm_index, old_plotted_sector);
650648
}
651649
readers_and_pieces.add_sector(disk_farm_index, plotted_sector);
@@ -659,7 +657,15 @@ where
659657
};
660658

661659
single_disk_farm
662-
.on_sector_plotted(Arc::new(on_plotted_sector_callback))
660+
.on_sector_update(Arc::new(move |(_sector_index, sector_state)| {
661+
if let SectorUpdate::Plotting(SectorPlottingDetails::Finished {
662+
plotted_sector,
663+
old_plotted_sector,
664+
}) = sector_state
665+
{
666+
on_plotted_sector_callback(plotted_sector, old_plotted_sector);
667+
}
668+
}))
663669
.detach();
664670

665671
single_disk_farm

crates/subspace-farmer/src/single_disk_farm.rs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -539,21 +539,36 @@ type HandlerFn<A> = Arc<dyn Fn(&A) + Send + Sync + 'static>;
539539
type Handler<A> = Bag<HandlerFn<A>, A>;
540540

541541
/// Details about sector currently being plotted
542-
pub struct SectorPlottingDetails {
543-
/// Sector index
544-
pub sector_index: SectorIndex,
545-
/// Progress so far in % (not including this sector)
546-
pub progress: f32,
547-
/// Whether sector is being replotted
548-
pub replotting: bool,
549-
/// Whether this is the last sector queued so far
550-
pub last_queued: bool,
542+
#[derive(Debug, Clone, Encode, Decode)]
543+
pub enum SectorPlottingDetails {
544+
/// Starting plotting of a sector
545+
Starting {
546+
/// Progress so far in % (not including this sector)
547+
progress: f32,
548+
/// Whether sector is being replotted
549+
replotting: bool,
550+
/// Whether this is the last sector queued so far
551+
last_queued: bool,
552+
},
553+
/// Finished plotting
554+
Finished {
555+
/// Information about plotted sector
556+
plotted_sector: PlottedSector,
557+
/// Information about old plotted sector that was replaced
558+
old_plotted_sector: Option<PlottedSector>,
559+
},
560+
}
561+
562+
/// Various sector updates
563+
#[derive(Debug, Clone, Encode, Decode)]
564+
pub enum SectorUpdate {
565+
/// Sector is is being plotted
566+
Plotting(SectorPlottingDetails),
551567
}
552568

553569
#[derive(Default, Debug)]
554570
struct Handlers {
555-
sector_plotting: Handler<SectorPlottingDetails>,
556-
sector_plotted: Handler<(PlottedSector, Option<PlottedSector>)>,
571+
sector_update: Handler<(SectorIndex, SectorUpdate)>,
557572
solution: Handler<SolutionResponse>,
558573
plot_audited: Handler<AuditEvent>,
559574
}
@@ -1316,17 +1331,9 @@ impl SingleDiskFarm {
13161331
self.piece_reader.clone()
13171332
}
13181333

1319-
/// Subscribe to sector plotting notification
1320-
pub fn on_sector_plotting(&self, callback: HandlerFn<SectorPlottingDetails>) -> HandlerId {
1321-
self.handlers.sector_plotting.add(callback)
1322-
}
1323-
1324-
/// Subscribe to notification about plotted sectors
1325-
pub fn on_sector_plotted(
1326-
&self,
1327-
callback: HandlerFn<(PlottedSector, Option<PlottedSector>)>,
1328-
) -> HandlerId {
1329-
self.handlers.sector_plotted.add(callback)
1334+
/// Subscribe to sector updates
1335+
pub fn on_sector_update(&self, callback: HandlerFn<(SectorIndex, SectorUpdate)>) -> HandlerId {
1336+
self.handlers.sector_update.add(callback)
13301337
}
13311338

13321339
/// Subscribe to notification about audited plots

crates/subspace-farmer/src/single_disk_farm/plotting.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::single_disk_farm::{
2-
BackgroundTaskError, Handlers, PlotMetadataHeader, SectorPlottingDetails,
2+
BackgroundTaskError, Handlers, PlotMetadataHeader, SectorPlottingDetails, SectorUpdate,
33
RESERVED_PLOT_METADATA,
44
};
55
use crate::thread_pool_manager::PlottingThreadPoolManager;
@@ -184,14 +184,14 @@ where
184184
info!(%sector_index, "Plotting sector ({progress:.2}% complete)");
185185
}
186186

187+
let sector_state = SectorUpdate::Plotting(SectorPlottingDetails::Starting {
188+
progress,
189+
replotting,
190+
last_queued,
191+
});
187192
handlers
188-
.sector_plotting
189-
.call_simple(&SectorPlottingDetails {
190-
sector_index,
191-
progress,
192-
replotting,
193-
last_queued,
194-
});
193+
.sector_update
194+
.call_simple(&(sector_index, sector_state));
195195

196196
// This `loop` is a workaround for edge-case in local setup if expiration is configured to
197197
// 1. In that scenario we get replotting notification essentially straight from block import
@@ -403,9 +403,13 @@ where
403403
}
404404
}
405405

406+
let sector_state = SectorUpdate::Plotting(SectorPlottingDetails::Finished {
407+
plotted_sector,
408+
old_plotted_sector: maybe_old_plotted_sector,
409+
});
406410
handlers
407-
.sector_plotted
408-
.call_simple(&(plotted_sector, maybe_old_plotted_sector));
411+
.sector_update
412+
.call_simple(&(sector_index, sector_state));
409413
}
410414

411415
Ok(())

0 commit comments

Comments
 (0)