|
| 1 | +use std::marker::PhantomData; |
| 2 | +use tokio::sync::{mpsc, oneshot}; |
| 3 | + |
| 4 | +/// TODO: what is the exact type of the work result |
| 5 | +pub type WorkResult = Result<(), ()>; |
| 6 | + |
| 7 | +/// Struct that captures the net work done by several work requests on a worker. |
| 8 | +/// This should include AT LEAST: |
| 9 | +/// - per-tx gas usage |
| 10 | +/// - events |
| 11 | +/// - any other necessary information |
| 12 | +/// - bundle state |
| 13 | +pub struct TotalWork {} |
| 14 | + |
| 15 | +/// Request for work. Worker should simulate the bundle, and send back |
| 16 | +/// a result indicating validity and outcome. |
| 17 | +pub struct WorkRequest<T> { |
| 18 | + /// the work to be done. |
| 19 | + work: T, |
| 20 | + /// The result of doing work. |
| 21 | + // TODO: what is the exact type of the response |
| 22 | + rx: oneshot::Receiver<WorkResult>, |
| 23 | +} |
| 24 | + |
| 25 | +/// Handle to a worker. Used to issue work (bundles) to a worker |
| 26 | +/// and manage their lifecycle. |
| 27 | +pub struct WorkerHandle<T> { |
| 28 | + /// Sender fo |
| 29 | + tx: mpsc::Sender<WorkRequest<T>>, |
| 30 | + |
| 31 | + /// Used to shutdown the worker. Either drop to discard, or send to |
| 32 | + /// get the `TotalWork` |
| 33 | + shutdown: oneshot::Sender<()>, |
| 34 | + |
| 35 | + /// Used to receive the when accepting |
| 36 | + outcome: oneshot::Receiver<TotalWork>, |
| 37 | +} |
| 38 | + |
| 39 | +impl<T> WorkerHandle<T> { |
| 40 | + /// Apply a bundle to the worker's inner state. |
| 41 | + async fn do_work(&self, work: WorkRequest<T>) -> WorkResult { |
| 42 | + todo!() |
| 43 | + } |
| 44 | + |
| 45 | + /// Accept the accumulated work. |
| 46 | + async fn accept(self) -> TotalWork { |
| 47 | + todo!() |
| 48 | + } |
| 49 | + |
| 50 | + /// Reject and discard the accumulated work. |
| 51 | + fn reject(self) { |
| 52 | + drop(self); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/// Worker contains the following: |
| 57 | +/// - receiver for work requests |
| 58 | +/// - shutdown channel |
| 59 | +/// - if dropped, indicates discard |
| 60 | +/// - if triggered, indicates acceptance |
| 61 | +/// - outcome channel to send execution results |
| 62 | +pub struct Worker<T> { |
| 63 | + rx: mpsc::Receiver<WorkRequest<T>>, |
| 64 | + shutdown: oneshot::Receiver<()>, |
| 65 | + |
| 66 | + outcome: oneshot::Sender<TotalWork>, |
| 67 | +} |
| 68 | + |
| 69 | +/// Spawns workers, by wrapping the root DB in cache DB. |
| 70 | +pub struct Factory<Ext, Db> { |
| 71 | + db: Db, |
| 72 | + _pd: PhantomData<fn() -> (Ext, Db)>, |
| 73 | +} |
| 74 | + |
| 75 | +impl<Ext, Db> Factory<Ext, Db> { |
| 76 | + /// Instantiate a worker with an empty cache |
| 77 | + fn worker<T>(&self) -> WorkerHandle<T> { |
| 78 | + todo!() |
| 79 | + } |
| 80 | + |
| 81 | + /// Instantiate a worker with a base state |
| 82 | + fn worker_with<T>(&self, based_on: &TotalWork) -> WorkerHandle<T> { |
| 83 | + todo!() |
| 84 | + } |
| 85 | +} |
0 commit comments