Skip to content

Commit ca78fee

Browse files
authored
Merge pull request #2432 from subspace/farmer-detailed-sector-updates
Farmer detailed sector updates
2 parents 031cbf5 + 1a45992 commit ca78fee

File tree

4 files changed

+204
-61
lines changed

4 files changed

+204
-61
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: 14 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,16 @@ 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+
..
665+
}) = sector_state
666+
{
667+
on_plotted_sector_callback(plotted_sector, old_plotted_sector);
668+
}
669+
}))
663670
.detach();
664671

665672
single_disk_farm

crates/subspace-farmer/src/single_disk_farm.rs

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -539,21 +539,66 @@ 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+
/// Downloading sector pieces
554+
Downloading,
555+
/// Downloaded sector pieces
556+
Downloaded(Duration),
557+
/// Encoding sector pieces
558+
Encoding,
559+
/// Encoded sector pieces
560+
Encoded(Duration),
561+
/// Writing sector
562+
Writing,
563+
/// Wrote sector
564+
Wrote(Duration),
565+
/// Finished plotting
566+
Finished {
567+
/// Information about plotted sector
568+
plotted_sector: PlottedSector,
569+
/// Information about old plotted sector that was replaced
570+
old_plotted_sector: Option<PlottedSector>,
571+
/// How much time it took to plot a sector
572+
time: Duration,
573+
},
574+
}
575+
576+
/// Details about sector expiration
577+
#[derive(Debug, Clone, Encode, Decode)]
578+
pub enum SectorExpirationDetails {
579+
/// Sector expiration became known
580+
Determined {
581+
/// Segment index at which sector expires
582+
expires_at: SegmentIndex,
583+
},
584+
/// Sector will expire at the next segment index and should be replotted
585+
AboutToExpire,
586+
/// Sector already expired
587+
Expired,
588+
}
589+
590+
/// Various sector updates
591+
#[derive(Debug, Clone, Encode, Decode)]
592+
pub enum SectorUpdate {
593+
/// Sector is is being plotted
594+
Plotting(SectorPlottingDetails),
595+
/// Sector expiration information updated
596+
Expiration(SectorExpirationDetails),
551597
}
552598

553599
#[derive(Default, Debug)]
554600
struct Handlers {
555-
sector_plotting: Handler<SectorPlottingDetails>,
556-
sector_plotted: Handler<(PlottedSector, Option<PlottedSector>)>,
601+
sector_update: Handler<(SectorIndex, SectorUpdate)>,
557602
solution: Handler<SolutionResponse>,
558603
plot_audited: Handler<AuditEvent>,
559604
}
@@ -984,6 +1029,7 @@ impl SingleDiskFarm {
9841029
last_archived_segment_index: farmer_app_info.protocol_info.history_size.segment_index(),
9851030
min_sector_lifetime: farmer_app_info.protocol_info.min_sector_lifetime,
9861031
node_client: node_client.clone(),
1032+
handlers: Arc::clone(&handlers),
9871033
sectors_metadata: Arc::clone(&sectors_metadata),
9881034
sectors_to_plot_sender,
9891035
initial_plotting_finished: farming_delay_sender,
@@ -1316,17 +1362,9 @@ impl SingleDiskFarm {
13161362
self.piece_reader.clone()
13171363
}
13181364

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)
1365+
/// Subscribe to sector updates
1366+
pub fn on_sector_update(&self, callback: HandlerFn<(SectorIndex, SectorUpdate)>) -> HandlerId {
1367+
self.handlers.sector_update.add(callback)
13301368
}
13311369

13321370
/// Subscribe to notification about audited plots

0 commit comments

Comments
 (0)