diff --git a/Cargo.toml b/Cargo.toml index fe2a94c..f6940d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -97,6 +97,7 @@ uuid = { version = "1.2", features = ["v4", "fast-rng", "macro-diagnostics"] } # eth alloy-chains = { version = "0.1" } +reqwest = "0.12.5" [dev-dependencies] pretty_assertions = "1.4.0" diff --git a/src/config/env.rs b/src/config/env.rs index 20b8674..fe0fe93 100644 --- a/src/config/env.rs +++ b/src/config/env.rs @@ -10,6 +10,7 @@ pub struct GlobalEnv { pub curve_type: String, pub chain_id: u64, pub program_name: String, + pub bridge_service_addr: String, } /// GLOBAL_ENV is a global variable that holds the environment variables, @@ -25,4 +26,6 @@ pub static GLOBAL_ENV: Lazy = Lazy::new(|| GlobalEnv { program_name: std::env::var("PROGRAM_NAME") .unwrap_or("EVM".to_string()) .to_lowercase(), + bridge_service_addr: std::env::var("BRIDGE_SERVICE_ADDR") + .unwrap_or("http://localhost:8001".to_string()), }); diff --git a/src/settlement/worker.rs b/src/settlement/worker.rs index 25c4d36..e6bfccd 100644 --- a/src/settlement/worker.rs +++ b/src/settlement/worker.rs @@ -1,3 +1,4 @@ +use crate::config::env::GLOBAL_ENV; use crate::db::{keys, prefix, Database, ProofResult, Status}; use crate::prover::ProverChannel; use crate::settlement::{BatchData, Settlement}; @@ -7,6 +8,7 @@ use ethers::prelude::U64; use ethers_core::types::{BlockId, BlockNumber, Transaction}; use ethers_providers::{Http, Middleware, Provider}; use prost::bytes; +use reqwest::Client; use reth_primitives::{Bytes, TransactionKind, TxLegacy}; use std::sync::Arc; use std::time::Duration; @@ -180,8 +182,26 @@ impl Settler { if let Some(proof_bytes) = db.get(next_proof_key.as_bytes()) { let proof_data: ProofResult = serde_json::from_slice(&proof_bytes).unwrap(); // verify the proof - // TODO: update the new_local_exit_root - let zeth_last_rollup_exit_root = settlement_provider.get_zeth_last_rollup_exit_root().await.map_err(|e| anyhow!("failed to get zeth last rollup exit root, err: {:?}", e))?; + + let bridge_service_client = Client::new(); + let respose = bridge_service_client + .get(format!("{}/get-root", GLOBAL_ENV.bridge_service_addr.clone())) + .query(&[("block_num", proof_data.block_number)]) + .send() + .await?; + + let mut zeth_last_rollup_exit_root = [0u8; 32]; + if respose.status().is_success() { + let body = respose.text().await?; + let parsed_json: serde_json::Value = serde_json::from_str(&body).unwrap(); + let rollup_exit_root = parsed_json["rollup_exit_root"].as_str().unwrap_or_default(); + let rollup_exit_root_bytes = hex::decode(&rollup_exit_root[2..]).expect("Failed to decode hex string"); + + zeth_last_rollup_exit_root.copy_from_slice(&rollup_exit_root_bytes); + log::debug!("rollup_exit_root: {}", rollup_exit_root); + } else { + log::error!("Request failed, response: {:?}", respose); + } match settlement_provider.verify_batches( 0, @@ -551,4 +571,36 @@ mod tests { fs::remove_dir_all(path).unwrap(); } + + #[tokio::test] + async fn test_bridge_service() -> Result<(), Box> { + let bridge_service_client = Client::new(); + let respose = bridge_service_client + .get(format!( + "{}/get-root", + GLOBAL_ENV.bridge_service_addr.clone() + )) + .query(&[("block_num", 1)]) + .send() + .await?; + + if respose.status().is_success() { + let body = respose.text().await?; + let parsed_json: serde_json::Value = serde_json::from_str(&body).unwrap(); + let rollup_exit_root = parsed_json["rollup_exit_root"].as_str().unwrap_or_default(); + let rollup_exit_root_bytes = + hex::decode(&rollup_exit_root[2..]).expect("Failed to decode hex string"); + let mut zeth_last_rollup_exit_root = [0u8; 32]; + zeth_last_rollup_exit_root.copy_from_slice(&rollup_exit_root_bytes); + println!("rollup_exit_root: {}", rollup_exit_root); + println!( + "zeth_last_rollup_exit_root: {:?}", + zeth_last_rollup_exit_root + ); + Ok(()) + } else { + println!("Request failed with status: {}", respose.status()); + Err("Request failed".into()) + } + } }