-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |