Skip to content

Commit c59deef

Browse files
committed
feat: separate proving from verification in two commands
1 parent 3588e09 commit c59deef

13 files changed

Lines changed: 203 additions & 91 deletions

File tree

examples/l2/Cargo.lock

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/l2/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[workspace]
22
resolver = "2"
3-
members = ["crates/l2", "crates/types"]
3+
members = ["./cmd/", "crates/l2", "crates/types"]
44

55
[workspace.package]
66
version = "0.1.0"

examples/l2/Makefile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
SHELL := /bin/bash
44

5+
6+
__CONFIG__:
57
gen_devnet_owner_wallet:
68
rm -f ./contract-owner.keystore.json
79
cast wallet import -k . contract-owner.keystore.json --private-key 0x47e179ec197488593b187f80a00eb0da91f1b9d0b13f8733639f19c30a34926a --unsafe-password "<!w5Y%In94Vw"
@@ -19,9 +21,6 @@ gen_env_l2_holesky:
1921
gen_env_l2_devnet:
2022
@cp .env.devnet .env
2123

22-
run_l2:
23-
@cd crates/l2 && cargo run --release --bin main
24-
2524
generate_program_id:
2625
@cd crates/l2 && cargo run --release --bin write_program_id
2726
@cat ./crates/l2/programs_ids.json
@@ -37,3 +36,10 @@ deploy_contract:
3736

3837
clean_db:
3938
rm -f examples/l2/crates/l2/db
39+
40+
__L2_PROGRAM__:
41+
update_state_transition:
42+
@cd cmd && cargo run --release --bin update_state_transition
43+
44+
verify_state_transition_on_chain:
45+
@cd cmd && cargo run --release --bin verify_state_transition_on_chain

examples/l2/cmd/Cargo.toml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
name = "l2_cmd"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
serde = { workspace = true }
8+
serde_json = { workspace = true }
9+
tracing = { version = "0.1", features = ["log"] }
10+
tracing-subscriber = { version = "0.3.0", features = ["env-filter"] }
11+
bincode = "1.3.3"
12+
tokio = "1.44"
13+
dotenv = "0.15"
14+
l2 = { path = "../crates/l2/" }
15+
aligned-sdk = { path = "../../../batcher/aligned-sdk" }
16+
17+
[lib]
18+
name = "l2_cmd"
19+
path = "./lib.rs"
20+
21+
[[bin]]
22+
name = "update_state_transition"
23+
path = "./update_state_transition.rs"
24+
25+
[[bin]]
26+
name = "verify_state_transition_on_chain"
27+
path = "./verify_state_transition_on_chain.rs"

examples/l2/cmd/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod utils;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::{fs::File, io::Write};
2+
3+
use l2::l2::L2;
4+
use tracing::info;
5+
use tracing_subscriber::FmtSubscriber;
6+
7+
use l2_cmd::utils::load_config;
8+
9+
#[tokio::main]
10+
async fn main() {
11+
let subscriber = FmtSubscriber::builder().finish();
12+
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
13+
14+
let config = load_config();
15+
let mut l2 = L2::new(config);
16+
17+
let proof = l2.update_state_transition().await;
18+
info!("Serializing and saving proof on disk to be verified later on chain");
19+
20+
let proof_bytes = bincode::serialize(&proof).unwrap();
21+
let mut file = File::create("./proof.bin").expect("Unable to create file");
22+
file.write(&proof_bytes).unwrap();
23+
info!("Proof stored in disk, in the next 24hs it will be aggregated and verified and aligned, so you should retrieve its status later");
24+
}
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@ use std::env;
22

33
use aligned_sdk::common::types::Network;
44
use dotenv::dotenv;
5-
use l2_example::{config::Config, l2::start_l2};
6-
use tracing_subscriber::FmtSubscriber;
7-
8-
#[tokio::main]
9-
async fn main() {
10-
let subscriber = FmtSubscriber::builder().finish();
11-
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
5+
use l2::config::Config;
126

7+
pub fn load_config() -> Config {
138
dotenv().ok();
149

1510
let network = match env::var("NETWORK").expect("NETWORK not set").as_str() {
@@ -30,7 +25,8 @@ async fn main() {
3025
.expect("PRIVATE_KEY_STORE_PASSWORD not set"),
3126
state_transition_contract_address: env::var("STATE_TRANSITION_CONTRACT_ADDRESS")
3227
.expect("STATE_TRANSITION_CONTRACT_ADDRESS not set"),
28+
db_path: Some("./db".to_string()),
3329
};
3430

35-
start_l2(config).await;
31+
config
3632
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use l2::l2::L2;
2+
use tracing::info;
3+
use tracing_subscriber::FmtSubscriber;
4+
5+
use l2_cmd::utils::load_config;
6+
7+
#[tokio::main]
8+
async fn main() {
9+
let subscriber = FmtSubscriber::builder().finish();
10+
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
11+
12+
let config = load_config();
13+
let mut l2 = L2::new(config);
14+
15+
info!("Loading proof from disk to verify on chain");
16+
17+
let proof_bytes = std::fs::read("./proof.bin").unwrap();
18+
let proof = bincode::deserialize(&proof_bytes).unwrap();
19+
l2.verify_state_transition(proof).await;
20+
}

examples/l2/crates/l2/Cargo.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "l2_example"
2+
name = "l2"
33
version = "0.1.0"
44
edition = "2021"
55

@@ -21,10 +21,6 @@ tokio = "1.44"
2121
alloy = { version = "0.14", features = ["full", "signer-keystore", "providers"] }
2222
dotenv = "0.15"
2323

24-
[[bin]]
25-
name = "main"
26-
path = "./src/main.rs"
27-
2824
[[bin]]
2925
name = "write_program_id"
3026
path = "./zkvm_programs/bin/write_program_ids.rs"
Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
use aligned_sdk::{
2+
aggregation_layer::ProofStatus,
23
common::types::{AlignedVerificationData, Signer, VerificationData, Wallet},
34
verification_layer::{estimate_fee, get_chain_id},
45
};
5-
use alloy::{
6-
eips::BlockNumberOrTag,
7-
primitives::Address,
8-
providers::{Provider, ProviderBuilder, WsConnect},
9-
rpc::types::Filter,
10-
};
11-
use futures_util::StreamExt;
126
use sp1_sdk::{HashableKey, SP1VerifyingKey};
137

148
use crate::config::Config;
@@ -63,58 +57,45 @@ pub async fn send_proof_to_be_verified_on_aligned(
6357
.expect("Proof to be sent")
6458
}
6559

66-
pub async fn wait_until_proof_is_aggregated(
60+
pub async fn check_proof_proof_aggregation_status(
6761
config: &Config,
6862
proof: &sp1_sdk::SP1ProofWithPublicValues,
6963
vk: &SP1VerifyingKey,
70-
) -> Vec<[u8; 32]> {
71-
let ws_rpc_url = &config.ws_eth_rpc_url;
72-
let ws = WsConnect::new(ws_rpc_url);
73-
let provider = ProviderBuilder::new().on_ws(ws).await.unwrap();
74-
75-
let aligned_proof_agg_address =
76-
Address::from(config.network.get_aligned_proof_agg_service_address().0);
77-
78-
let filter = Filter::new()
79-
.address(aligned_proof_agg_address)
80-
.event("AggregatedProofVerified(bytes32,bytes32)")
81-
.from_block(BlockNumberOrTag::Latest);
82-
83-
// Subscribe to logs.
84-
let sub = provider.subscribe_logs(&filter).await.unwrap();
85-
let mut stream = sub.into_stream();
86-
64+
) -> ProofStatus {
8765
let verification_data = aligned_sdk::aggregation_layer::AggregationModeVerificationData::SP1 {
8866
vk: vk.hash_bytes(),
8967
public_inputs: proof.public_values.to_vec(),
9068
};
9169

92-
let mut merkle_path = vec![];
93-
94-
while stream.next().await.is_some() {
95-
let proof_status = aligned_sdk::aggregation_layer::check_proof_verification(
96-
&verification_data,
97-
config.network.clone(),
98-
config.eth_rpc_url.clone(),
99-
config.beacon_client_url.clone(),
100-
None,
101-
)
102-
.await
103-
.expect("Get merkle path for proof");
70+
let proof_status = aligned_sdk::aggregation_layer::check_proof_verification(
71+
&verification_data,
72+
config.network.clone(),
73+
config.eth_rpc_url.clone(),
74+
config.beacon_client_url.clone(),
75+
// By default it looks back 24 hours
76+
None,
77+
)
78+
.await
79+
.expect("Get merkle path for proof");
10480

105-
match proof_status {
106-
aligned_sdk::aggregation_layer::ProofStatus::Verified {
107-
merkle_path: path, ..
108-
} => {
109-
merkle_path = path;
110-
break;
111-
}
112-
aligned_sdk::aggregation_layer::ProofStatus::Invalid => {
113-
panic!("Proof did pass merkle root verification")
114-
}
115-
aligned_sdk::aggregation_layer::ProofStatus::NotFound => continue,
116-
}
117-
}
81+
proof_status
11882

119-
merkle_path
83+
// match proof_status {
84+
// aligned_sdk::aggregation_layer::ProofStatus::Verified {
85+
// merkle_root,
86+
// merkle_path,
87+
// } => {
88+
// info!(
89+
// "Proof aggregated in aggregation with merkle root {:?}",
90+
// hex::encode(merkle_root)
91+
// );
92+
// merkle_path
93+
// }
94+
// aligned_sdk::aggregation_layer::ProofStatus::Invalid => {
95+
// panic!("Proof did pass merkle root verification");
96+
// }
97+
// aligned_sdk::aggregation_layer::ProofStatus::NotFound => {
98+
// panic!("Proof not found in the last 24 hours logs");
99+
// }
100+
// }
120101
}

0 commit comments

Comments
 (0)