subspace_farmer/
plotter.rspub mod cpu;
#[cfg(feature = "_gpu")]
pub mod gpu;
pub mod pool;
use async_trait::async_trait;
use bytes::Bytes;
use futures::channel::mpsc;
use futures::Stream;
use std::fmt;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use subspace_core_primitives::sectors::SectorIndex;
use subspace_core_primitives::PublicKey;
use subspace_farmer_components::plotting::PlottedSector;
use subspace_farmer_components::FarmerProtocolInfo;
pub enum SectorPlottingProgress {
Downloading,
Downloaded(Duration),
Encoding,
Encoded(Duration),
Finished {
plotted_sector: PlottedSector,
time: Duration,
sector: Pin<Box<dyn Stream<Item = Result<Bytes, String>> + Send + Sync>>,
},
Error {
error: String,
},
}
impl fmt::Debug for SectorPlottingProgress {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SectorPlottingProgress::Downloading => fmt::Formatter::write_str(f, "Downloading"),
SectorPlottingProgress::Downloaded(time) => {
f.debug_tuple_field1_finish("Downloaded", &time)
}
SectorPlottingProgress::Encoding => fmt::Formatter::write_str(f, "Encoding"),
SectorPlottingProgress::Encoded(time) => f.debug_tuple_field1_finish("Encoded", &time),
SectorPlottingProgress::Finished {
plotted_sector,
time,
sector: _,
} => f.debug_struct_field3_finish(
"Finished",
"plotted_sector",
plotted_sector,
"time",
time,
"sector",
&"<stream>",
),
SectorPlottingProgress::Error { error } => {
f.debug_struct_field1_finish("Error", "error", &error)
}
}
}
}
#[async_trait]
pub trait Plotter: fmt::Debug {
async fn has_free_capacity(&self) -> Result<bool, String>;
async fn plot_sector(
&self,
public_key: PublicKey,
sector_index: SectorIndex,
farmer_protocol_info: FarmerProtocolInfo,
pieces_in_sector: u16,
replotting: bool,
progress_sender: mpsc::Sender<SectorPlottingProgress>,
);
async fn try_plot_sector(
&self,
public_key: PublicKey,
sector_index: SectorIndex,
farmer_protocol_info: FarmerProtocolInfo,
pieces_in_sector: u16,
replotting: bool,
progress_sender: mpsc::Sender<SectorPlottingProgress>,
) -> bool;
}
#[async_trait]
impl<P> Plotter for Arc<P>
where
P: Plotter + Send + Sync,
{
#[inline]
async fn has_free_capacity(&self) -> Result<bool, String> {
self.as_ref().has_free_capacity().await
}
#[inline]
async fn plot_sector(
&self,
public_key: PublicKey,
sector_index: SectorIndex,
farmer_protocol_info: FarmerProtocolInfo,
pieces_in_sector: u16,
replotting: bool,
progress_sender: mpsc::Sender<SectorPlottingProgress>,
) {
self.as_ref()
.plot_sector(
public_key,
sector_index,
farmer_protocol_info,
pieces_in_sector,
replotting,
progress_sender,
)
.await
}
#[inline]
async fn try_plot_sector(
&self,
public_key: PublicKey,
sector_index: SectorIndex,
farmer_protocol_info: FarmerProtocolInfo,
pieces_in_sector: u16,
replotting: bool,
progress_sender: mpsc::Sender<SectorPlottingProgress>,
) -> bool {
self.as_ref()
.try_plot_sector(
public_key,
sector_index,
farmer_protocol_info,
pieces_in_sector,
replotting,
progress_sender,
)
.await
}
}