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