Skip to content

Commit b6e50e4

Browse files
feat(aggregation-mode): integrate proof aggregator with db (#2186)
1 parent f4a4d26 commit b6e50e4

File tree

21 files changed

+263
-577
lines changed

21 files changed

+263
-577
lines changed

aggregation_mode/Cargo.lock

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aggregation_mode/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ serde_yaml = "0.9"
1313
alloy = { version = "1.1.1", features = ["default", "signer-keystore", "kzg"] }
1414
bincode = "1.3.3"
1515
aligned-sdk = { path = "../crates/sdk/" }
16+
db = { path = "./db" }
1617
sp1-sdk = "5.0.0"
1718
risc0-zkvm = { version = "3.0.3" }
1819

aggregation_mode/db/Cargo.toml

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

aggregation_mode/db/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod types;

aggregation_mode/db/src/types.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use sqlx::{
2+
prelude::FromRow,
3+
types::{BigDecimal, Uuid},
4+
Type,
5+
};
6+
7+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Type)]
8+
#[sqlx(type_name = "task_status", rename_all = "lowercase")]
9+
pub enum TaskStatus {
10+
Pending,
11+
Processing,
12+
Verified,
13+
}
14+
15+
#[derive(Debug, Clone, FromRow)]
16+
pub struct Task {
17+
pub task_id: Uuid,
18+
pub address: String,
19+
pub proving_system_id: i32,
20+
pub proof: Vec<u8>,
21+
pub program_commitment: Vec<u8>,
22+
pub merkle_path: Option<Vec<u8>>,
23+
pub status: TaskStatus,
24+
}
25+
26+
#[derive(Debug, Clone, FromRow)]
27+
pub struct Payment {
28+
pub payment_event_id: Uuid,
29+
pub address: String,
30+
pub amount: i32,
31+
pub started_at: BigDecimal,
32+
pub valid_until: BigDecimal,
33+
pub tx_hash: String,
34+
}

aggregation_mode/proof_aggregator/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ serde_yaml = { workspace = true }
1010
alloy = { workspace = true }
1111
bincode = { workspace = true }
1212
aligned-sdk = { workspace = true }
13+
db = { workspace = true }
1314

1415
tracing = { version = "0.1", features = ["log"] }
1516
tracing-subscriber = { version = "0.3.0", features = ["env-filter"] }
@@ -21,6 +22,7 @@ ciborium = "=0.2.2"
2122
lambdaworks-crypto = { git = "https://github.com/lambdaclass/lambdaworks.git", rev = "5f8f2cfcc8a1a22f77e8dff2d581f1166eefb80b", features = ["serde"]}
2223
rayon = "1.10.0"
2324
backon = "1.2.0"
25+
sqlx = { version = "0.8", features = [ "runtime-tokio", "postgres", "uuid", "bigdecimal" ] }
2426

2527
# zkvms
2628
sp1-sdk = { workspace = true }

aggregation_mode/proof_aggregator/abi/AlignedLayerServiceManager.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

aggregation_mode/proof_aggregator/src/aggregators/mod.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ pub mod sp1_aggregator;
33

44
use std::fmt::Display;
55

6+
use aligned_sdk::aggregation_layer::AggregationModeProvingSystem;
67
use lambdaworks_crypto::merkle_tree::traits::IsMerkleTreeBackend;
78
use risc0_aggregator::{Risc0AggregationError, Risc0ProofReceiptAndImageId};
89
use sha3::{Digest, Keccak256};
9-
use sp1_aggregator::{SP1AggregationError, SP1ProofWithPubValuesAndElf};
10+
use sp1_aggregator::{SP1AggregationError, SP1ProofWithPubValuesAndVk};
1011
use tracing::info;
1112

1213
#[derive(Clone, Debug)]
@@ -44,6 +45,13 @@ impl ZKVMEngine {
4445
Some(engine)
4546
}
4647

48+
pub fn proving_system_id(&self) -> u16 {
49+
match &self {
50+
ZKVMEngine::SP1 => AggregationModeProvingSystem::SP1.as_u16(),
51+
ZKVMEngine::RISC0 => AggregationModeProvingSystem::RISC0.as_u16(),
52+
}
53+
}
54+
4755
/// Aggregates a list of [`AlignedProof`]s into a single [`AlignedProof`].
4856
///
4957
/// Returns a tuple containing:
@@ -61,7 +69,7 @@ impl ZKVMEngine {
6169
) -> Result<(AlignedProof, [u8; 32]), ProofAggregationError> {
6270
let res = match self {
6371
ZKVMEngine::SP1 => {
64-
let proofs: Vec<SP1ProofWithPubValuesAndElf> = proofs
72+
let proofs: Vec<SP1ProofWithPubValuesAndVk> = proofs
6573
.into_iter()
6674
// Fetcher already filtered for SP1
6775
// We do this for type casting, as to avoid using generics
@@ -80,7 +88,7 @@ impl ZKVMEngine {
8088
proofs_per_chunk,
8189
);
8290

83-
let mut agg_proofs: Vec<(SP1ProofWithPubValuesAndElf, Vec<[u8; 32]>)> = vec![];
91+
let mut agg_proofs: Vec<(SP1ProofWithPubValuesAndVk, Vec<[u8; 32]>)> = vec![];
8492
for (i, chunk) in chunks.enumerate() {
8593
let leaves_commitment =
8694
chunk.iter().map(|e| e.hash_vk_and_pub_inputs()).collect();
@@ -154,7 +162,7 @@ impl ZKVMEngine {
154162
}
155163

156164
pub enum AlignedProof {
157-
SP1(Box<SP1ProofWithPubValuesAndElf>),
165+
SP1(Box<SP1ProofWithPubValuesAndVk>),
158166
Risc0(Box<Risc0ProofReceiptAndImageId>),
159167
}
160168

aggregation_mode/proof_aggregator/src/aggregators/sp1_aggregator.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ static SP1_PROVER_CLIENT: LazyLock<EnvProver> = LazyLock::new(ProverClient::from
2525
static SP1_PROVER_CLIENT_CPU: LazyLock<CpuProver> =
2626
LazyLock::new(|| ProverClient::builder().cpu().build());
2727

28-
pub struct SP1ProofWithPubValuesAndElf {
28+
pub struct SP1ProofWithPubValuesAndVk {
2929
pub proof_with_pub_values: SP1ProofWithPublicValues,
30-
pub elf: Vec<u8>,
3130
pub vk: SP1VerifyingKey,
3231
}
3332

@@ -37,16 +36,14 @@ pub enum AlignedSP1VerificationError {
3736
UnsupportedProof,
3837
}
3938

40-
impl SP1ProofWithPubValuesAndElf {
39+
impl SP1ProofWithPubValuesAndVk {
4140
/// Constructs a new instance of the struct by verifying a given SP1 proof with its public values.
4241
pub fn new(
4342
proof_with_pub_values: SP1ProofWithPublicValues,
44-
elf: Vec<u8>,
43+
vk: SP1VerifyingKey,
4544
) -> Result<Self, AlignedSP1VerificationError> {
4645
let client = &*SP1_PROVER_CLIENT_CPU;
4746

48-
let (_pk, vk) = client.setup(&elf);
49-
5047
// only sp1 compressed proofs are supported for aggregation now
5148
match proof_with_pub_values.proof {
5249
sp1_sdk::SP1Proof::Compressed(_) => client
@@ -57,7 +54,6 @@ impl SP1ProofWithPubValuesAndElf {
5754

5855
Ok(Self {
5956
proof_with_pub_values,
60-
elf,
6157
vk,
6258
})
6359
}
@@ -80,8 +76,8 @@ pub enum SP1AggregationError {
8076
}
8177

8278
pub(crate) fn run_user_proofs_aggregator(
83-
proofs: &[SP1ProofWithPubValuesAndElf],
84-
) -> Result<SP1ProofWithPubValuesAndElf, SP1AggregationError> {
79+
proofs: &[SP1ProofWithPubValuesAndVk],
80+
) -> Result<SP1ProofWithPubValuesAndVk, SP1AggregationError> {
8581
let mut stdin = SP1Stdin::new();
8682

8783
let mut program_input = sp1_aggregation_program::UserProofsAggregatorInput {
@@ -131,18 +127,17 @@ pub(crate) fn run_user_proofs_aggregator(
131127
.verify(&proof, &vk)
132128
.map_err(SP1AggregationError::Verification)?;
133129

134-
let proof_and_elf = SP1ProofWithPubValuesAndElf {
130+
let proof_and_vk = SP1ProofWithPubValuesAndVk {
135131
proof_with_pub_values: proof,
136-
elf: USER_PROOFS_PROGRAM_ELF.to_vec(),
137132
vk,
138133
};
139134

140-
Ok(proof_and_elf)
135+
Ok(proof_and_vk)
141136
}
142137

143138
pub(crate) fn run_chunk_aggregator(
144-
proofs: &[(SP1ProofWithPubValuesAndElf, Vec<[u8; 32]>)],
145-
) -> Result<SP1ProofWithPubValuesAndElf, SP1AggregationError> {
139+
proofs: &[(SP1ProofWithPubValuesAndVk, Vec<[u8; 32]>)],
140+
) -> Result<SP1ProofWithPubValuesAndVk, SP1AggregationError> {
146141
let mut stdin = SP1Stdin::new();
147142

148143
let mut program_input = sp1_aggregation_program::ChunkAggregatorInput {
@@ -204,13 +199,12 @@ pub(crate) fn run_chunk_aggregator(
204199
.verify(&proof, &vk)
205200
.map_err(SP1AggregationError::Verification)?;
206201

207-
let proof_and_elf = SP1ProofWithPubValuesAndElf {
202+
let proof_and_vk = SP1ProofWithPubValuesAndVk {
208203
proof_with_pub_values: proof,
209-
elf: CHUNK_PROGRAM_ELF.to_vec(),
210204
vk,
211205
};
212206

213-
Ok(proof_and_elf)
207+
Ok(proof_and_vk)
214208
}
215209

216210
pub fn vk_from_elf(elf: &[u8]) -> SP1VerifyingKey {
Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
use serde::{Deserialize, Serialize};
2-
use std::{fs::File, fs::OpenOptions, io::Read, io::Write};
2+
use std::{fs::File, io::Read};
33

44
#[derive(Debug, Deserialize, Serialize)]
55
pub struct ECDSAConfig {
66
pub private_key_store_path: String,
77
pub private_key_store_password: String,
88
}
99

10-
#[derive(Debug, Deserialize, Serialize)]
11-
pub struct LastAggregatedBlock {
12-
pub last_aggregated_block: u64,
13-
}
14-
1510
#[derive(Debug, Deserialize, Serialize)]
1611
pub struct Config {
1712
pub eth_rpc_url: String,
1813
pub eth_ws_url: String,
1914
pub max_proofs_in_queue: u16,
2015
pub proof_aggregation_service_address: String,
2116
pub aligned_service_manager_address: String,
22-
pub last_aggregated_block_filepath: String,
2317
pub ecdsa: ECDSAConfig,
2418
pub proofs_per_chunk: u16,
2519
pub total_proofs_limit: u16,
2620
pub risc0_chunk_aggregator_image_id: String,
2721
pub sp1_chunk_aggregator_vk_hash: String,
2822
pub monthly_budget_eth: f64,
23+
pub db_connection_url: String,
2924
}
3025

3126
impl Config {
@@ -36,32 +31,4 @@ impl Config {
3631
let config: Config = serde_yaml::from_str(&contents)?;
3732
Ok(config)
3833
}
39-
40-
pub fn get_last_aggregated_block(&self) -> Result<u64, Box<dyn std::error::Error>> {
41-
let mut file = File::open(&self.last_aggregated_block_filepath)?;
42-
let mut contents = String::new();
43-
file.read_to_string(&mut contents)?;
44-
let lab: LastAggregatedBlock = serde_json::from_str(&contents)?;
45-
Ok(lab.last_aggregated_block)
46-
}
47-
48-
pub fn update_last_aggregated_block(
49-
&self,
50-
last_aggregated_block: u64,
51-
) -> Result<(), Box<dyn std::error::Error>> {
52-
let last_aggregated_block_struct = LastAggregatedBlock {
53-
last_aggregated_block,
54-
};
55-
56-
let mut file = OpenOptions::new()
57-
.write(true)
58-
.truncate(true)
59-
.create(true)
60-
.open(&self.last_aggregated_block_filepath)?;
61-
62-
let content = serde_json::to_string(&last_aggregated_block_struct)?;
63-
file.write_all(content.as_bytes())?;
64-
65-
Ok(())
66-
}
6734
}

0 commit comments

Comments
 (0)