-
Notifications
You must be signed in to change notification settings - Fork 3
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
6 changed files
with
119 additions
and
85 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
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
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 |
---|---|---|
@@ -1,58 +1,80 @@ | ||
//! Initialize all components of the eigen-zeth full node. | ||
//! They will be launched in Run CMD. | ||
use crate::prover::ProverProvider; | ||
|
||
use std::{ | ||
borrow::Cow, | ||
io::Write, | ||
sync::{Arc, Barrier}, | ||
thread::{self, JoinHandle}, | ||
}; | ||
use tokio::select; | ||
use tokio::signal::unix::signal; | ||
use tokio::signal::unix::SignalKind; | ||
use tokio::sync::{ | ||
mpsc::{self, Receiver, Sender}, | ||
oneshot, watch, | ||
}; | ||
use tokio::task; | ||
use tokio::time::{interval, sleep, Duration, Instant}; | ||
use ethers_providers::{Provider, Http, Middleware, Ws}; | ||
use ethers_core::types::Address; | ||
|
||
use crate::db::{Database, lfs}; | ||
use crate::prover::ProverChannel; | ||
use ethers_providers::{Http, Provider}; | ||
|
||
use tokio::sync::mpsc::{self, Receiver}; | ||
use tokio::time::{interval, Duration}; | ||
|
||
use crate::db::{lfs, Database}; | ||
|
||
pub(crate) struct Operator { | ||
db: Box<dyn Database>, | ||
prover: ProverProvider, | ||
prover: ProverChannel, | ||
settler: Provider<Http>, | ||
rx_proof: Receiver<Vec<u8>>, | ||
} | ||
|
||
impl Operator { | ||
pub fn new(dir: &str, l1addr: &str) -> Self { | ||
let prover = ProverProvider::new(); | ||
pub fn new(_db_path: &str, l1addr: &str, prover_addr: &str) -> Self { | ||
let (sx, rx) = mpsc::channel(10); | ||
let prover = ProverChannel::new(&prover_addr, sx); | ||
let db = lfs::open_db(lfs::DBConfig::Memory).unwrap(); | ||
// TODO: abstract this in the settlement | ||
let settler = Provider::<Http>::try_from(l1addr).unwrap(); | ||
Operator{ prover, db, settler } | ||
|
||
Operator { | ||
prover, | ||
db, | ||
settler, | ||
rx_proof: rx, | ||
} | ||
} | ||
|
||
pub async fn start(&mut self) { | ||
pub async fn run(&mut self, mut stop_channel: Receiver<()>) { | ||
let mut ticker = interval(Duration::from_millis(1000)); | ||
let batch_key = "next_batch".to_string().as_bytes().to_vec(); | ||
let proof_key = "batch_proof".to_string().as_bytes().to_vec(); | ||
|
||
loop { | ||
select! { | ||
tokio::select! { | ||
_ = ticker.tick() => { | ||
let block_or_none = self.settler.get_block(self.prover.current_batch()).await.unwrap(); | ||
if let Some(block) = block_or_none { | ||
//write the new block to DB | ||
if let Some(no) = block.number { | ||
self.prover.prove(no.as_u64()).await.unwrap(); | ||
} | ||
} | ||
let next_batch = self.db.get(&batch_key); | ||
let current_batch = self.prover.get_current_batch(); | ||
log::debug!("fetch block {:?}, {:?}", next_batch, current_batch); | ||
|
||
match (next_batch, current_batch){ | ||
(None, None) => { | ||
// insert the first block | ||
self.db.put(batch_key.clone(), 1_u64.to_be_bytes().to_vec()); | ||
}, | ||
(Some(no), None) => { | ||
let block_no = u64::from_be_bytes(no.try_into().unwrap()); | ||
self.prover.execute(block_no).await.unwrap(); | ||
}, | ||
(None, Some(no)) => todo!("Invalid branch, block: {no}"), | ||
(Some(next), Some(cur)) => { | ||
let block_no = u64::from_be_bytes(next.try_into().unwrap()); | ||
log::debug!("next: {block_no}, current: {cur}"); | ||
}, | ||
}; | ||
} | ||
proof_data = self.rx_proof.recv() => { | ||
log::debug!("fetch proof: {:?}", proof_data); | ||
self.db.put(proof_key.clone(), proof_data.unwrap()); | ||
|
||
} | ||
// trigger the next task | ||
if let Some(current_batch) = self.db.get(&batch_key) { | ||
let block_no = u64::from_be_bytes(current_batch.try_into().unwrap()); | ||
self.prover.execute(block_no).await.unwrap(); | ||
} else { | ||
log::debug!("Wait for the new task coming in"); | ||
} | ||
} | ||
_ = stop_channel.recv() => { | ||
break; | ||
} | ||
}; | ||
} | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
pub(crate) mod provider; | ||
pub use provider::ProverProvider; | ||
pub use provider::ProverChannel; |
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
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 @@ | ||
|