use std::fmt;
use std::path::PathBuf;
use sha2::{Digest, Sha256};
use crate::{CertificateLimits, RelativeInterfaceCertificate};
#[derive(Debug)]
pub struct DistributedInterfaceError {
message: String,
}
impl DistributedInterfaceError {
pub(super) fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
}
}
pub fn message(&self) -> &str {
&self.message
}
}
impl fmt::Display for DistributedInterfaceError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "distributed interface: {}", self.message)
}
}
impl std::error::Error for DistributedInterfaceError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ArtifactId(pub(super) [u8; 32]);
impl ArtifactId {
pub fn for_bytes(bytes: &[u8]) -> Self {
Self(Sha256::digest(bytes).into())
}
pub fn from_bytes(bytes: [u8; 32]) -> Self {
Self(bytes)
}
pub fn as_bytes(&self) -> &[u8; 32] {
&self.0
}
pub fn to_hex(self) -> String {
let mut output = String::with_capacity(64);
for byte in self.0 {
use std::fmt::Write as _;
write!(output, "{byte:02x}").expect("writing to a string cannot fail");
}
output
}
}
impl fmt::Display for ArtifactId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.to_hex())
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct DistributedInterfaceWork {
pub shards: usize,
pub shards_loaded: usize,
pub folds_reused: usize,
pub folds_computed: usize,
pub bytes_read: usize,
pub bytes_written: usize,
pub peak_artifact_bytes: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DistributedInterfaceManifest {
pub(super) job: ArtifactId,
pub(super) max_dim: usize,
pub(super) modulus: u32,
pub(super) separator_vertices: Vec<usize>,
pub(super) output_protected_vertices: Vec<usize>,
pub(super) shards: Vec<ArtifactId>,
pub(super) folds: Vec<ArtifactId>,
pub(super) result: ArtifactId,
}
#[derive(Debug, Clone)]
pub struct DistributedInterfaceCommit {
pub(super) manifest: DistributedInterfaceManifest,
pub(super) certificate: RelativeInterfaceCertificate,
pub(super) work: DistributedInterfaceWork,
}
impl DistributedInterfaceCommit {
pub fn manifest(&self) -> &DistributedInterfaceManifest {
&self.manifest
}
pub fn certificate(&self) -> &RelativeInterfaceCertificate {
&self.certificate
}
pub fn work(&self) -> DistributedInterfaceWork {
self.work
}
}
#[derive(Debug, Clone)]
pub struct DurableInterfaceStore {
pub(super) root: PathBuf,
}
pub(super) struct CommitPlan<'a> {
pub(super) job: ArtifactId,
pub(super) shard_ids: &'a [ArtifactId],
pub(super) separator_vertices: Vec<usize>,
pub(super) output_protected_vertices: Vec<usize>,
pub(super) intermediate_protected: Vec<usize>,
pub(super) limits: CertificateLimits,
}
pub(super) struct FoldState {
pub(super) accumulator: RelativeInterfaceCertificate,
pub(super) folds: Vec<ArtifactId>,
pub(super) next: usize,
pub(super) accumulator_bytes: Vec<u8>,
}
pub(super) struct PreparedCommit<'a> {
pub(super) plan: CommitPlan<'a>,
pub(super) first_id: ArtifactId,
pub(super) first: RelativeInterfaceCertificate,
}
pub(super) struct Progress {
pub(super) prefix: usize,
pub(super) accumulator: ArtifactId,
pub(super) folds: Vec<ArtifactId>,
}