Skip to content

Commit

Permalink
prover abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Apr 12, 2024
1 parent 114e965 commit 19461e2
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
16 changes: 16 additions & 0 deletions crates/prover/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "sharp-p2p-prover"
version.workspace = true
edition.workspace = true
repository.workspace = true
license-file.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tokio.workspace = true
tracing.workspace = true
futures.workspace= true
thiserror.workspace = true
async-process.workspace = true
sharp-p2p-common.workspace = true
13 changes: 13 additions & 0 deletions crates/prover/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum ProverControllerError {
#[error("task not found")]
TaskNotFound,

#[error("task not found")]
TaskTerminated,

#[error("io")]
Io(#[from] std::io::Error),
}
5 changes: 5 additions & 0 deletions crates/prover/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod errors;
#[allow(async_fn_in_trait)]
pub mod traits;

pub mod stone_prover;
60 changes: 60 additions & 0 deletions crates/prover/src/stone_prover/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::collections::HashMap;

use crate::{
errors::ProverControllerError,
traits::{Prover, ProverController},
};
use sharp_p2p_common::{job::Job, job_witness::JobWitness};
use tokio::process::{Child, Command};
use tracing::{debug, trace};

pub struct StoneProver {
tasks: HashMap<Job, Child>,
}

impl Prover for StoneProver {
fn init() -> impl ProverController {
Self { tasks: HashMap::new() }
}
}

impl ProverController for StoneProver {
async fn prove(&mut self, job: Job) -> Result<JobWitness, ProverControllerError> {
let task = Command::new("sleep 20").spawn()?;
debug!("task {} spawned", job);
self.tasks.insert(job.to_owned(), task);

let task_status =
self.tasks.get_mut(&job).ok_or(ProverControllerError::TaskNotFound)?.wait().await?;

trace!("task {} woke up", job);
if !task_status.success() {
debug!("task terminated {}", job);
return Err(ProverControllerError::TaskTerminated);
}

let task_output = self
.tasks
.remove(&job)
.ok_or(ProverControllerError::TaskNotFound)?
.wait_with_output()
.await?;
trace!("task {} output {:?}", job, task_output);

todo!()
}

async fn terminate(&mut self, job: &Job) -> Result<(), ProverControllerError> {
self.tasks.get_mut(job).ok_or(ProverControllerError::TaskNotFound)?.start_kill()?;
trace!("task scheduled for termination {}", job);
Ok(())
}

async fn drop(mut self) -> Result<(), ProverControllerError> {
let keys: Vec<Job> = self.tasks.keys().cloned().collect();
for job in keys.iter() {
self.terminate(job).await?;
}
Ok(())
}
}
13 changes: 13 additions & 0 deletions crates/prover/src/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use sharp_p2p_common::{job::Job, job_witness::JobWitness};

use crate::errors::ProverControllerError;

pub trait Prover {
fn init() -> impl ProverController;
}

pub trait ProverController {
async fn prove(&mut self, job: Job) -> Result<JobWitness, ProverControllerError>;
async fn terminate(&mut self, job: &Job) -> Result<(), ProverControllerError>;
async fn drop(self) -> Result<(), ProverControllerError>;
}

0 comments on commit 19461e2

Please sign in to comment.