Skip to content

Commit 26b53f3

Browse files
committed
refactor: move l2 code to l2.rs
1 parent 204ee76 commit 26b53f3

6 files changed

Lines changed: 93 additions & 92 deletions

File tree

examples/L2/crates/l2/src/aligned.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use alloy::{
1111
use futures_util::StreamExt;
1212
use sp1_sdk::{HashableKey, SP1VerifyingKey};
1313

14-
use crate::Config;
14+
use crate::config::Config;
1515

1616
pub async fn send_proof_to_be_verified_on_aligned(
1717
config: &Config,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pub struct Config {
2+
pub network: aligned_sdk::common::types::Network,
3+
pub eth_rpc_url: String,
4+
pub ws_eth_rpc_url: String,
5+
pub beacon_client_url: String,
6+
pub private_key_store_path: String,
7+
pub private_key_store_password: String,
8+
pub state_transition_contract_address: String,
9+
}

examples/L2/crates/l2/src/eth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use alloy::{
55
rpc::types::TransactionReceipt, signers::local::LocalSigner, sol,
66
};
77

8-
use crate::Config;
8+
use crate::config::Config;
99

1010
sol!(
1111
#[sol(rpc)]

examples/L2/crates/l2/src/l2.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
use crate::aligned::{send_proof_to_be_verified_on_aligned, wait_until_proof_is_aggregated};
2+
use crate::config::Config;
3+
use crate::db::{generate_random_transfers, DB};
4+
use crate::eth::send_state_transition_to_chain;
5+
use crate::prover::{prove_state_transition, PROGRAM_ELF};
6+
use primitive_types::U256;
7+
use sp1_state_transition_program::ProgramOutput;
8+
use tracing::info;
9+
10+
pub async fn start_l2(config: Config) {
11+
// 0. Load merkle tree file, if not created, create initial state
12+
let mut db = DB::new("./db".to_string());
13+
14+
loop {
15+
// 1. Create random transfers
16+
let transfers = generate_random_transfers(&db, 10);
17+
18+
// 2. Call zkvm and transfer to perform and verify
19+
info!("Starting prover...");
20+
let (mut proof, vk) = prove_state_transition(&db, transfers.clone());
21+
let ProgramOutput {
22+
initial_state_merkle_root,
23+
post_state_merkle_root,
24+
} = proof.public_values.read::<ProgramOutput>();
25+
info!("Prover finish");
26+
27+
// 3. If the proving went alright, update the db and verify that the merkle root matches
28+
assert_eq!(db.commitment(), initial_state_merkle_root);
29+
// Note: we don't have to verify that the user has enough balance, as the prover already validates it
30+
for transfer in transfers {
31+
let mut user_from = db
32+
.user_states
33+
.get(&transfer.from)
34+
.expect("User must exist in state")
35+
.clone();
36+
37+
let mut user_to = db
38+
.user_states
39+
.get(&transfer.to)
40+
.expect("User must exist in state")
41+
.clone();
42+
43+
user_from.balance -= transfer.amount;
44+
user_from.nonce += U256::one();
45+
user_to.balance += transfer.amount;
46+
47+
db.user_states.insert(transfer.from, user_from);
48+
db.user_states.insert(transfer.to, user_to);
49+
}
50+
assert_eq!(db.commitment(), post_state_merkle_root);
51+
52+
// Fow now, in order for a proof to be aggregated, we first need to submit it via the fast mode or verification layer
53+
// Let's suppose that our L2 would run the prover once every 24hs and submit it on aligned
54+
// Once aligned aggregates the proof we will be notified and we'll send the new state commitment on chain
55+
56+
// 4. Send the proof to aligned and wait for verification
57+
info!("Sending proof to aligned batcher...");
58+
let _ = send_proof_to_be_verified_on_aligned(&config, &proof, PROGRAM_ELF.to_vec()).await;
59+
info!("Proof submitted");
60+
61+
// 5. Wait until proof is aggregated
62+
info!("Waiting until proof is aggregated...");
63+
let merkle_path = wait_until_proof_is_aggregated(&config, &proof, &vk).await;
64+
info!("Proof has been aggregated on aligned, about to send update to chain...");
65+
66+
// 6. Send updateState transaction to Ethereum
67+
let receipt =
68+
send_state_transition_to_chain(&config, proof.public_values.to_vec(), merkle_path)
69+
.await;
70+
71+
info!(
72+
"State update in contracts tx hash: {:?}",
73+
receipt.transaction_hash
74+
);
75+
76+
// 7. Finally save the db to a file to be retrieved later
77+
db.save().unwrap();
78+
}
79+
}

examples/L2/crates/l2/src/lib.rs

Lines changed: 2 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,6 @@
1-
use aligned::{send_proof_to_be_verified_on_aligned, wait_until_proof_is_aggregated};
2-
use db::{generate_random_transfers, DB};
3-
use eth::send_state_transition_to_chain;
4-
use primitive_types::U256;
5-
use prover::{prove_state_transition, PROGRAM_ELF};
6-
use sp1_state_transition_program::ProgramOutput;
7-
use tracing::info;
8-
91
mod aligned;
2+
pub mod config;
103
mod db;
114
mod eth;
5+
pub mod l2;
126
mod prover;
13-
14-
pub struct Config {
15-
pub network: aligned_sdk::common::types::Network,
16-
pub eth_rpc_url: String,
17-
pub ws_eth_rpc_url: String,
18-
pub beacon_client_url: String,
19-
pub private_key_store_path: String,
20-
pub private_key_store_password: String,
21-
pub state_transition_contract_address: String,
22-
}
23-
24-
pub async fn start_l2(config: Config) {
25-
// 0. Load merkle tree file, if not created, create initial state
26-
let mut db = DB::new("./db".to_string());
27-
28-
loop {
29-
// 1. Create random transfers
30-
let transfers = generate_random_transfers(&db, 10);
31-
32-
// 2. Call zkvm and transfer to perform and verify
33-
info!("Starting prover...");
34-
let (mut proof, vk) = prove_state_transition(&db, transfers.clone());
35-
let ProgramOutput {
36-
initial_state_merkle_root,
37-
post_state_merkle_root,
38-
} = proof.public_values.read::<ProgramOutput>();
39-
info!("Prover finish");
40-
41-
// 3. If the proving went alright, update the db and verify that the merkle root matches
42-
assert!(db.commitment() == initial_state_merkle_root);
43-
// Note: we don't have to verify that the user has enough balance, as the prover already validates it
44-
for transfer in transfers {
45-
let mut user_from = db
46-
.user_states
47-
.get(&transfer.from)
48-
.expect("User must exist in state")
49-
.clone();
50-
51-
let mut user_to = db
52-
.user_states
53-
.get(&transfer.to)
54-
.expect("User must exist in state")
55-
.clone();
56-
57-
user_from.balance -= transfer.amount;
58-
user_from.nonce += U256::one();
59-
user_to.balance += transfer.amount;
60-
61-
db.user_states.insert(transfer.from, user_from);
62-
db.user_states.insert(transfer.to, user_to);
63-
}
64-
assert!(db.commitment() == post_state_merkle_root);
65-
66-
// Fow now, in order for a proof to be aggregated, we first need to submit it via the fast mode or verification layer
67-
// Let's suppose that our L2 would run the prover once every 24hs and submit it on aligned
68-
// Once aligned aggregates the proof we will be notified and we'll send the new state commitment on chain
69-
70-
// 4. Send the proof to aligned and wait for verification
71-
info!("Sending proof to aligned batcher...");
72-
let _ = send_proof_to_be_verified_on_aligned(&config, &proof, PROGRAM_ELF.to_vec()).await;
73-
info!("Proof submitted");
74-
75-
// 5. Wait until proof is aggregated
76-
info!("Waiting until proof is aggregated...");
77-
let merkle_path = wait_until_proof_is_aggregated(&config, &proof, &vk).await;
78-
info!("Proof has been aggregated on aligned, about to send update to chain...");
79-
80-
// 6. Send updateState transaction to Ethereum
81-
let receipt =
82-
send_state_transition_to_chain(&config, proof.public_values.to_vec(), merkle_path)
83-
.await;
84-
85-
info!(
86-
"State update in contracts tx hash: {:?}",
87-
receipt.transaction_hash
88-
);
89-
90-
// 7. Finally save the db to a file to be retrieved later
91-
db.save().unwrap();
92-
}
93-
}

examples/L2/crates/l2/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::env;
22

33
use aligned_sdk::common::types::Network;
44
use dotenv::dotenv;
5-
use l2_example::{start_l2, Config};
5+
use l2_example::{config::Config, l2::start_l2};
66
use tracing_subscriber::FmtSubscriber;
77

88
#[tokio::main]

0 commit comments

Comments
 (0)