Skip to content

Commit

Permalink
add makefile target to purge state
Browse files Browse the repository at this point in the history
  • Loading branch information
fborello-lambda committed Nov 1, 2024
1 parent 1f70cfe commit a1569d3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 34 deletions.
38 changes: 35 additions & 3 deletions crates/l2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,40 @@ down-l2: ## 🛑 Shuts down the L2 Lambda Ethereum Rust Client

restart-l2: down-l2 init-l2 ## 🔄 Restarts the L2 Lambda Ethereum Rust Client

init-l2-prover: ## 🚀 Initializes the Prover
init-l2-prover: FORCE_PURGE := 1
init-l2-prover: purge_prover_state ## 🚀 Initializes the Prover
cargo run --release --features build_zkvm --manifest-path ../../Cargo.toml --bin ethereum_rust_prover

init-l2-prover-gpu: ## 🚀 Initializes the Prover with GPU support
cargo run --release --features "build_zkvm,cuda" --manifest-path ../../Cargo.toml --bin ethereum_rust_prover
init-l2-prover-gpu: FORCE_PURGE := 1
init-l2-prover-gpu: purge_prover_state ## 🚀 Initializes the Prover with GPU support
cargo run --release --features "build_zkvm,gpu" --manifest-path ../../Cargo.toml --bin ethereum_rust_prover

# Purge L2

UNAME_S := $(shell uname -s)
PROJECT_NAME := ethereum_rust_l2

ifeq ($(UNAME_S),Linux)
PROJECT_PATH := $(HOME)/.local/share/${PROJECT_NAME}
else ifeq ($(UNAME_S),Darwin)
PROJECT_PATH := $(HOME)/Library/Application\ Support/${PROJECT_NAME}
else
$(error Unsupported platform: $(UNAME_S))
endif

.PHONY: purge_prover_state
purge_prover_state:
@if [ "$(FORCE_PURGE)" -eq "1" ]; then \
echo "Force purging the directory: $(PROJECT_PATH)"; \
rm -rf "$(PROJECT_PATH)"; \
echo "Directory deleted."; \
else \
echo "Are you sure you want to delete the directory: $(PROJECT_PATH)? [y/n]"; \
read answer; \
if [ "$$answer" != "y" ]; then \
echo "Operation canceled."; \
exit 1; \
fi; \
rm -rf "$(PROJECT_PATH)"; \
echo "Directory deleted."; \
fi
5 changes: 4 additions & 1 deletion crates/l2/prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use ethereum_rust_l2::utils::config::prover_client::ProverClientConfig;
use tracing::warn;

pub async fn init_client(config: ProverClientConfig) {
prover_client::start_proof_data_client(config).await;
// TODO: panicking if the client fails. Improve error handling
prover_client::start_proof_data_client(config)
.await
.unwrap();
warn!("Prover finished!");
}
32 changes: 19 additions & 13 deletions crates/l2/prover/src/prover_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ use ethereum_rust_l2::{
utils::config::prover_client::ProverClientConfig,
};

use crate::utils::prover_state::{self, persist_block_in_prover_state, read_block_in_prover_state};
use crate::utils::save_prover_state::{
self, persist_block_in_prover_state, read_block_in_prover_state,
};

use super::prover::Prover;

pub async fn start_proof_data_client(config: ProverClientConfig) {
let mut proof_data_client = ProverClient::new(config);
pub async fn start_proof_data_client(
config: ProverClientConfig,
) -> Result<(), Box<dyn std::error::Error>> {
let mut proof_data_client = ProverClient::new(config)?;
proof_data_client.start().await;
Ok(())
}

struct ProverClient {
Expand All @@ -28,21 +33,22 @@ struct ProverClient {
}

impl ProverClient {
pub fn new(config: ProverClientConfig) -> Self {
let prover_state_file_path = config
.prover_state_file_path
// TODO: rm unwrap
.unwrap_or_else(|| prover_state::get_default_prover_state_file_path().unwrap());
pub fn new(config: ProverClientConfig) -> Result<Self, Box<dyn std::error::Error>> {
let prover_state_file_path = match config.prover_state_file_path {
Some(path) => path,
None => save_prover_state::get_default_prover_state_file_path()?,
};

let block_number_to_request = match read_block_in_prover_state(&prover_state_file_path) {
Ok(ps) => ps.block_header.number,
Ok(ps) => ps.block_header.number + 1,
Err(_) => 1,
};
Self {

Ok(Self {
prover_server_endpoint: config.prover_server_endpoint,
prover_state_file_path,
block_number_to_request,
}
})
}

pub async fn start(&mut self) {
Expand Down Expand Up @@ -117,9 +123,9 @@ impl ProverClient {
"Received submit ack for block_number: {}",
block_header.number
);
// After submission, add 1 so that in the next request, the prover_client receives the subsequent block.
// After submission, add 1 to request for the subsequent block.
self.block_number_to_request += 1;
// Persist the State
// Persist the State in a file.
persist_block_in_prover_state(&self.prover_state_file_path, block_header)
.map_err(|e| format!("Error while persisting state: {e}"))?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/l2/prover/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod prover_state;
pub mod save_prover_state;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ethereum_rust_core::types::BlockHeader;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::{
fs::{self, create_dir_all},
fs::create_dir_all,
io::{BufReader, BufWriter, Read, Write},
path::Path,
};
Expand All @@ -14,15 +14,15 @@ pub struct ProverState {
}

fn create_prover_state_file_path(file_name: &str) -> Result<String, Box<dyn std::error::Error>> {
let project_dir =
ProjectDirs::from("", "", "ethereum_rust_l2").expect("Couldn't find home directory");
// TODO: rm unwrap
Ok(project_dir
.data_local_dir()
.join(file_name)
let project_dir = ProjectDirs::from("", "", "ethereum_rust_l2")
.ok_or_else(|| Box::<dyn std::error::Error>::from("Couldn't get project_dir."))?;

let binding = project_dir.data_local_dir().join(file_name);
let path_str = binding
.to_str()
.unwrap()
.to_owned())
.ok_or_else(|| Box::<dyn std::error::Error>::from("Couldn't convert path to str."))?;

Ok(path_str.to_string())
}

pub fn get_default_prover_state_file_path() -> Result<String, Box<dyn std::error::Error>> {
Expand All @@ -37,12 +37,6 @@ pub fn create_prover_state_file() -> Result<File, Box<dyn std::error::Error>> {
File::create(file_path).map_err(Into::into)
}

pub fn remove_default_prover_state_file() -> Result<(), Box<dyn std::error::Error>> {
let file_path = get_default_prover_state_file_path()?;
fs::remove_file(file_path)?;
Ok(())
}

pub fn persist_block_in_prover_state(
file_path: &str,
block_header: BlockHeader,
Expand Down Expand Up @@ -75,7 +69,7 @@ pub fn read_block_in_prover_state(
mod tests {
use super::*;
use ethereum_rust_l2::utils::test_data_io;
use fs::create_dir_all;
use std::fs::{self, create_dir_all};
use std::path::{Path, PathBuf};

#[test]
Expand Down

0 comments on commit a1569d3

Please sign in to comment.