Skip to content

Commit

Permalink
feat: add EigenZkvm's abi and impl the settlement's EigenZkvm interfa…
Browse files Browse the repository at this point in the history
…tes (#16)

* feat: add EigenZkvm's abi and impl the settlement's EigenZkvm interfates

* feat: determine contract data structure, prepare for testing

* feat: add L2Watcher to fetch latest l2 block, refactor the settler's loop, add verify_worker and proof_worker

* fix: update prover.pb, fix prover-service, add log for verify_worker

* fix: add test for proof parser, fix parse_public_input, fix zkvm contract client

* style: fix fmt

* fix: fix the param and gas of the verify_batches, add log
  • Loading branch information
captainlee1024 authored May 10, 2024
1 parent e2372c9 commit 63120f8
Show file tree
Hide file tree
Showing 21 changed files with 1,074 additions and 238 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ jsonrpsee = { version = "0.20", features = ["server", "macros"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.94"
serial_test = "2.0.0"
hex = "0.4.3"

# revm
#revm = { version = "7.1.0", features = ["std", "secp256k1"] }
Expand Down
10 changes: 5 additions & 5 deletions configs/settlement.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[ethereum_settlement_config]
provider_url = "http://localhost:8545"
provider_url = "http://localhost:8547"

[ethereum_settlement_config.local_wallet]
private_key = "0x76af8cc59ecfabf983d423e2054b07c11212cabc532062da0bd8067c59cf4a40"
private_key = "0xbe825d459385bbb3ba169ac8d59bd05b099b190d8a89aace7e5bc3518e2f1a9c"
chain_id = 12345

[ethereum_settlement_config.l1_contracts_addr]
bridge = "0x732200433EE79cCBf5842F9b5aD8fda6BF569F01"
global_exit = "0xAC97e12d0Ae20B2E0BF94e8Bd5752494577fC799"
zkvm = "0x12bfb8B59144b96bE5E74ECbC9896667261c004A"
bridge = "0xF93a9C25d4fa383ffCC8b9411A3813bFd131e2e8"
global_exit = "0xeA7ad75F6F685FdF0B8Bf6C6c254011018697D2d"
zkvm = "0x83C27Cb6C2Aa253A6C501E61F0d35E545FC0C68E"
1 change: 1 addition & 0 deletions contracts/EigenZkvm.json

Large diffs are not rendered by default.

30 changes: 14 additions & 16 deletions proto/prover/v1/prover.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ message ProverResponse
// batch proof

message GenBatchProofRequest {
string id = 1;
string batch_id = 1;
Batch batch = 2;
uint64 chain_id = 3;
string program_name = 4;
Expand All @@ -50,7 +50,7 @@ message Batch {
}

message GenBatchProofResponse {
string id = 1;
string batch_id = 1;
ProofResultCode result_code = 2;
BatchProofResult batch_proof_result = 3;
string error_message = 4;
Expand All @@ -70,12 +70,13 @@ message ChunkProof {
// aggregated proof

message GenAggregatedProofRequest {
string recursive_proof_1 = 1;
string recursive_proof_2 = 2;
string batch_id = 1;
string recursive_proof_1 = 2;
string recursive_proof_2 = 3;
}

message GenAggregatedProofResponse {
string id = 1;
string batch_id = 1;
ProofResultCode result_code = 2;
string result_string = 3;
string error_message = 4;
Expand All @@ -84,13 +85,14 @@ message GenAggregatedProofResponse {
// final proof

message GenFinalProofRequest {
string recursive_proof = 1;
string curve_name = 2;
string aggregator_addr = 3;
string batch_id = 1;
string recursive_proof = 2;
string curve_name = 3;
string aggregator_addr = 4;
}

message GenFinalProofResponse {
string id = 1;
string batch_id = 1;
ProofResultCode result_code = 2;
string result_string = 3;
FinalProof final_proof = 4;
Expand All @@ -99,13 +101,9 @@ message GenFinalProofResponse {

message FinalProof {
string proof = 1;
PublicInputs public = 2;
}

message PublicInputs {
bytes pre_state_root = 1;
bytes post_state_root = 2;
bytes batch_data = 3;
string public_input = 2;
bytes pre_state_root = 3;
bytes post_state_root = 4;
}

// proof result
Expand Down
86 changes: 86 additions & 0 deletions src/batch_proposer/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,87 @@
use crate::db::keys;
use crate::db::Database;
use anyhow::{anyhow, bail, Result};
use ethers_providers::{Http, Middleware, Provider};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::broadcast;
use tokio::{select, time};

const L2_WATCHER_INTERVAL: Duration = Duration::from_secs(1);
pub struct L2Watcher {
pub db: Arc<Box<dyn Database>>,
pub l2provider: Provider<Http>,
// stop_rx: broadcast::Receiver<()>,
stop_tx: broadcast::Sender<()>,
// pub ticker: time::Interval,
}

impl L2Watcher {
pub fn new(db: Arc<Box<dyn Database>>, l2provider: Provider<Http>) -> Self {
let (stop_tx, _) = broadcast::channel(1);

L2Watcher {
db,
l2provider,
// stop_rx,
stop_tx,
// ticker: time::interval(L2_WATCHER_INTERVAL),
}
}

pub async fn start(&mut self) -> Result<()> {
// TODO: restart
let mut ticker = time::interval(L2_WATCHER_INTERVAL);
let mut stop_rx = self.stop_tx.subscribe();
let db = self.db.clone();
let l2provider = self.l2provider.clone();
let mut stop_fetch_tx = self.stop_tx.subscribe();

tokio::spawn(async move {
loop {
select! {
_ = stop_rx.recv() => {
log::info!("L2Watcher stopped");
break;
}
_ = ticker.tick() => {
if let Err(e) = Self::fetch_latest_block(&db, &l2provider, &mut stop_fetch_tx).await{
log::error!("L2Watcher failed to fetch latest block, try again later, err: {:?}", e);
}
}
}
}
});

log::info!("L2Watcher started");
Ok(())
}

pub async fn stop(&self) -> Result<()> {
self.stop_tx
.send(())
.map_err(|e| anyhow!("Failed to stop the L2Watcher: {:?}", e))
.map(|_| log::info!("Exit signal successfully sent"))
}

pub async fn fetch_latest_block(
db: &Arc<Box<dyn Database>>,
l2provider: &Provider<Http>,
stop_rx: &mut broadcast::Receiver<()>,
) -> Result<()> {
select! {
result = l2provider.get_block_number() => {
result
.map_err(|e| anyhow!("{:?}", e))
.map(|number| {
log::info!("L2Watcher fetched block({})", number);
db.put(keys::KEY_LAST_SEQUENCE_FINALITY_BLOCK_NUMBER.to_vec(), number.as_u64().to_be_bytes().to_vec())
})
},
_ = stop_rx.recv() => {
log::info!("Early exit signal received during block fetch.");
bail!("Early exit signal received during block fetch.");
}
}
}
}
28 changes: 18 additions & 10 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tokio::sync::mpsc;
use crate::config::env::GLOBAL_ENV;
use crate::custom_reth;
use crate::db::lfs;
use crate::operator;
use crate::operator::Operator;
use crate::settlement::ethereum::EthereumSettlementConfig;
use crate::settlement::NetworkSpec;

Expand Down Expand Up @@ -182,14 +182,14 @@ impl RunCmd {
);

// Initialize the operator
let mut op = operator::Operator::new(
&GLOBAL_ENV.l1addr,
&GLOBAL_ENV.prover_addr,
settlement_spec,
db_config,
aggregator_addr,
)
.unwrap();
// let mut op = operator::Operator::new(
// &GLOBAL_ENV.l2addr,
// &GLOBAL_ENV.prover_addr,
// settlement_spec,
// db_config,
// aggregator_addr,
// )
// .unwrap();

let mut sigterm = signal(SignalKind::terminate()).unwrap();
let mut sigint = signal(SignalKind::interrupt()).unwrap();
Expand Down Expand Up @@ -220,6 +220,14 @@ impl RunCmd {
custom_reth::launch_custom_node().await?;

// Run the operator
op.run(stop_rx).await
Operator::run(
&GLOBAL_ENV.l2addr,
&GLOBAL_ENV.prover_addr,
settlement_spec,
db_config,
aggregator_addr,
stop_rx,
)
.await
}
}
4 changes: 2 additions & 2 deletions src/config/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::string::ToString;

/// EigenZethEnv is a struct that holds the environment variables
pub struct GlobalEnv {
pub l1addr: String,
pub l2addr: String,
pub prover_addr: String,
pub curve_type: String,
pub host: String,
Expand All @@ -17,7 +17,7 @@ pub struct GlobalEnv {
/// GLOBAL_ENV is a global variable that holds the environment variables,
/// it is lazy loaded and thread safe
pub static GLOBAL_ENV: Lazy<GlobalEnv> = Lazy::new(|| GlobalEnv {
l1addr: std::env::var("ZETH_L2_ADDR").unwrap_or("http://localhost:8546".to_string()),
l2addr: std::env::var("ZETH_L2_ADDR").unwrap_or("http://localhost:8546".to_string()),
prover_addr: std::env::var("PROVER_ADDR").unwrap_or("http://127.0.0.1:50061".to_string()),
curve_type: std::env::var("CURVE_TYPE").unwrap_or("BN128".to_string()),
host: std::env::var("HOST").unwrap_or("0.0.0.0:8182".to_string()),
Expand Down
14 changes: 5 additions & 9 deletions src/db/lfs/libmdbx.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
// TODO: Fix me
#![allow(dead_code)]
#![allow(unused_imports)]

use crate::db::lfs::libmdbx;
use crate::db::Database as EigenDB;
use anyhow::{anyhow, Result};
use config::{Config as UtilConfig, File};
use config::File;
use reth_libmdbx::*;
use serde::Deserialize;
use std::fs;
Expand Down Expand Up @@ -84,14 +80,14 @@ impl EigenDB for Db {
value
}

fn put(&mut self, key: Vec<u8>, value: Vec<u8>) {
fn put(&self, key: Vec<u8>, value: Vec<u8>) {
let txn = self.0.env.begin_rw_txn().unwrap();
txn.put(self.0.default_db.dbi(), key, value, WriteFlags::empty())
.unwrap();
txn.commit().unwrap();
}

fn del(&mut self, key: Vec<u8>) -> Option<Vec<u8>> {
fn del(&self, key: Vec<u8>) -> Option<Vec<u8>> {
let txn = self.0.env.begin_rw_txn().unwrap();
let value: Option<Vec<u8>> = txn.get(self.0.default_db.dbi(), &key).unwrap();
let success = txn.del(self.0.default_db.dbi(), &key, None).unwrap();
Expand Down Expand Up @@ -130,7 +126,7 @@ mod tests {
path: path.to_string(),
max_dbs,
};
let mut db = open_mdbx_db(config).unwrap();
let db = open_mdbx_db(config).unwrap();

let key = b"key";
// let key = string::String::from("value").into_bytes();
Expand Down Expand Up @@ -164,7 +160,7 @@ mod tests {

#[test]
fn test_reth_libmdbx() {
use reth_libmdbx::{EnvironmentBuilder, WriteFlags};
use reth_libmdbx::WriteFlags;

// path to the database
let path = "tmp/test_mdbx";
Expand Down
17 changes: 11 additions & 6 deletions src/db/lfs/mem.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
use crate::db::lfs::mem;
use crate::db::Database as EigenDB;
use std::collections::HashMap;
use std::sync::RwLock;

#[derive(Default)]
pub struct Db(HashMap<Vec<u8>, Vec<u8>>);
pub struct Db(RwLock<HashMap<Vec<u8>, Vec<u8>>>);

impl EigenDB for Db {
fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
self.0.get(key).cloned()
let read_guard = self.0.read().unwrap();
read_guard.get(key).cloned()
}

fn put(&mut self, key: Vec<u8>, value: Vec<u8>) {
self.0.insert(key, value);
fn put(&self, key: Vec<u8>, value: Vec<u8>) {
let mut write_guard = self.0.write().unwrap();

write_guard.insert(key, value);
}

fn del(&mut self, key: Vec<u8>) -> Option<Vec<u8>> {
self.0.remove(&key)
fn del(&self, key: Vec<u8>) -> Option<Vec<u8>> {
let mut write_guard = self.0.write().unwrap();
write_guard.remove(&key)
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/db/lfs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// TODO: Fix me
#![allow(dead_code)]

pub(crate) mod libmdbx;
mod mem;

Expand Down
18 changes: 15 additions & 3 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ pub(crate) mod lfs;

/// TODO: we need a trait to abstract the database operations in order to support multiple databases

pub trait Database {
pub trait Database: Send + Sync {
fn get(&self, key: &[u8]) -> Option<Vec<u8>>;
fn put(&mut self, key: Vec<u8>, value: Vec<u8>);
fn del(&mut self, key: Vec<u8>) -> Option<Vec<u8>>;
fn put(&self, key: Vec<u8>, value: Vec<u8>);
fn del(&self, key: Vec<u8>) -> Option<Vec<u8>>;
}

/// Used to represent different tables or columns or databases
Expand All @@ -25,3 +25,15 @@ pub(crate) mod columns {
/// The column for DATA_AVAILABILITY
pub const DATA_AVAILABILITY: usize = 1;
}

pub(crate) mod keys {
pub const KEY_LAST_SEQUENCE_FINALITY_BLOCK_NUMBER: &[u8] =
b"LAST_SEQUENCE_FINALITY_BLOCK_NUMBER";
pub const KEY_NEXT_BATCH: &[u8] = b"NEXT_BATCH";
pub const KEY_LAST_PROVEN_BLOCK_NUMBER: &[u8] = b"LAST_PROVEN_BLOCK_NUMBER";
pub const KEY_LAST_VERIFIED_BLOCK_NUMBER: &[u8] = b"LAST_VERIFIED_BLOCK_NUMBER";
}

pub(crate) mod prefix {
pub const PREFIX_BATCH_PROOF: &[u8] = b"BATCH_PROOF_";
}
Loading

0 comments on commit 63120f8

Please sign in to comment.