Skip to content

Commit 364d16b

Browse files
committed
fix: roots mismatch between program and backend
1 parent b7f99ff commit 364d16b

8 files changed

Lines changed: 38 additions & 29 deletions

File tree

examples/L2/Makefile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,5 @@
22

33
SHELL := /bin/bash
44

5-
# Either sp1|risc0
6-
ZKVM=sp1
7-
85
run:
9-
cargo run --release
6+
@cd crates/l2 && cargo run --release

examples/L2/crates/l2/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ futures-util = "0.3"
1818
tokio = "1.44"
1919
alloy = { version = "0.14", features = ["full", "providers"] }
2020

21-
2221
[build-dependencies]
2322
sp1-build = { version = "4.1.3" }
2423

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use aligned::{send_proof_to_be_verified_on_aligned, wait_until_proof_is_aggregated};
2+
use alloy::hex;
23
use db::{generate_random_transfers, DB};
4+
use primitive_types::U256;
35
use sp1_state_transition_program::ProgramOutput;
46
use zk::{prove_state_transition, PROGRAM_ELF};
57

@@ -14,30 +16,39 @@ pub async fn start_l2(
1416
wallet: aligned_sdk::core::types::Wallet<aligned_sdk::core::types::SigningKey>,
1517
) {
1618
// 0. Load merkle tree file, if not created, create initial state
17-
let mut db = DB::new("./db".to_string()).expect("create db");
19+
let mut db = DB::new("./db".to_string());
1820

1921
// 1. Create random transfers
20-
let account_updates = generate_random_transfers(&db, 10);
22+
let transfers = generate_random_transfers(&db, 10);
2123

2224
// 2. Call zkvm and pass (MerkleTree, Updates to perform)
23-
let (mut proof, _vk) = prove_state_transition(&mut db, account_updates.clone());
25+
let (mut proof, _vk) = prove_state_transition(&db, transfers.clone());
2426
let ProgramOutput {
2527
initial_state_merkle_root,
2628
post_state_merkle_root,
2729
} = proof.public_values.read::<ProgramOutput>();
2830

2931
// 3. If the proving went alright, update the db and verify that the merkle root matches
3032
assert!(db.commitment() == initial_state_merkle_root);
31-
for update in account_updates {
32-
db.user_states
33-
.get_mut(&update.from)
34-
.expect("User should exist")
35-
.balance -= update.amount;
36-
37-
db.user_states
38-
.get_mut(&update.to)
39-
.expect("User should exist")
40-
.balance += update.amount;
33+
for transfer in transfers {
34+
let mut user_from = db
35+
.user_states
36+
.get(&transfer.from)
37+
.expect("User must exist in state")
38+
.clone();
39+
40+
let mut user_to = db
41+
.user_states
42+
.get(&transfer.to)
43+
.expect("User must exist in state")
44+
.clone();
45+
46+
user_from.balance -= transfer.amount;
47+
user_from.nonce += U256::one();
48+
user_to.balance += transfer.amount;
49+
50+
db.user_states.insert(transfer.from, user_from);
51+
db.user_states.insert(transfer.to, user_to);
4152
}
4253
assert!(db.commitment() == post_state_merkle_root);
4354

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use sp1_sdk::Prover;
12
use types::Transfer;
23

34
use crate::db::DB;
@@ -6,7 +7,7 @@ pub const PROGRAM_ELF: &[u8] =
67
include_bytes!("../zkvm_programs/sp1/elf/sp1_state_transition_program");
78

89
pub fn prove_state_transition(
9-
db: &mut DB,
10+
db: &DB,
1011
transfers: Vec<Transfer>,
1112
) -> (sp1_sdk::SP1ProofWithPublicValues, sp1_sdk::SP1VerifyingKey) {
1213
let mut stdin = sp1_sdk::SP1Stdin::new();
Binary file not shown.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use primitive_types::H160;
22
use serde::{Deserialize, Serialize};
3-
use std::collections::HashMap;
3+
use std::collections::BTreeMap;
44
use types::{Transfer, UserState};
55

66
#[derive(Deserialize, Serialize)]
77
pub struct ProgramInput {
8-
pub user_states: HashMap<H160, UserState>,
8+
pub user_states: BTreeMap<H160, UserState>,
99
pub transfers: Vec<Transfer>,
1010
}
1111

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
sp1_zkvm::entrypoint!(main);
33

44
use lambdaworks_crypto::merkle_tree::merkle::MerkleTree;
5+
use primitive_types::U256;
56
use sp1_state_transition_program::{ProgramInput, ProgramOutput};
67
use types::UserState;
78

@@ -26,15 +27,15 @@ pub fn main() {
2627
.clone();
2728

2829
if user_from.balance >= transfer.amount {
29-
user_from.balance -= transfer.amount
30+
user_from.balance -= transfer.amount;
31+
user_from.nonce += U256::one();
32+
user_to.balance += transfer.amount;
3033
} else {
31-
panic!("User does not have enough balance to perform the transfer");
34+
panic!("User does not have enough balance to perform the transfer",);
3235
}
3336

34-
user_to.balance += transfer.amount;
35-
36-
input.user_states.insert(transfer.from, user_from.clone());
37-
input.user_states.insert(transfer.to, user_to.clone());
37+
input.user_states.insert(transfer.from, user_from);
38+
input.user_states.insert(transfer.to, user_to);
3839
}
3940

4041
let post_state: Vec<UserState> = input.user_states.clone().into_values().collect();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use primitive_types::{H160, U256};
33
use serde::{Deserialize, Serialize};
44
use sha3::{Digest, Keccak256};
55

6-
#[derive(Clone, Default, Serialize, Deserialize)]
6+
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
77
pub struct UserState {
88
pub address: H160,
99
pub balance: U256,
@@ -36,7 +36,7 @@ impl IsMerkleTreeBackend for UserState {
3636
}
3737
}
3838

39-
#[derive(Serialize, Deserialize, Clone)]
39+
#[derive(Serialize, Deserialize, Clone, Debug)]
4040
pub struct Transfer {
4141
pub from: H160,
4242
pub to: H160,

0 commit comments

Comments
 (0)