Skip to content

Commit 84d07af

Browse files
Merge branch 'feataggmode/add-setters-and-subscriptions' into feataggmode/add-subscription-limit-and-arbitrary-expiracy-lists
2 parents 3481449 + 23a863f commit 84d07af

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+825
-598
lines changed

Makefile

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,10 @@ agg_mode_batcher_send_payment:
330330
--private-key 0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d
331331

332332
agg_mode_batcher_send_sp1_proof:
333-
@NONCE=$$(curl -s http://127.0.0.1:8089/nonce/0x70997970C51812dc3A010C7d01b50e0d17dc79C8 | jq -r '.data.nonce'); \
334-
curl -X POST \
335-
-H "Content-Type: multipart/form-data" \
336-
-F "nonce=$${NONCE}" \
337-
-F "proof=@scripts/test_files/sp1/sp1_fibonacci_5_0_0.proof" \
338-
-F "program_vk=@scripts/test_files/sp1/sp1_fibonacci_5_0_0_vk.bin" \
339-
-F "_signature_hex=0x0" \
340-
http://127.0.0.1:8089/proof/sp1
333+
@cargo run --manifest-path aggregation_mode/cli/Cargo.toml -- submit sp1 \
334+
--proof scripts/test_files/sp1/sp1_fibonacci_5_0_0.proof \
335+
--vk scripts/test_files/sp1/sp1_fibonacci_5_0_0_vk.bin \
336+
--private-key "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"
341337

342338
__AGGREGATOR__: ## ____
343339

aggregation_mode/Cargo.lock

Lines changed: 51 additions & 2 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[workspace]
22
resolver = "2"
3-
members = ["./batcher", "./proof_aggregator", "./db"]
3+
members = ["./batcher", "./proof_aggregator", "./db", "./sdk", "./cli"]
44

55
[workspace.package]
66
version = "0.1.0"
@@ -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/batcher/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77
serde = { workspace = true }
88
serde_json = { workspace = true }
99
serde_yaml = { workspace = true }
10+
agg_mode_sdk = { path = "../sdk"}
1011
aligned-sdk = { workspace = true }
1112
sp1-sdk = { workspace = true }
1213
tracing = { version = "0.1", features = ["log"] }

aggregation_mode/batcher/src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ pub struct Config {
88
pub db_connection_url: String,
99
pub eth_rpc_url: String,
1010
pub payment_service_address: String,
11+
pub network: String,
12+
pub max_daily_proofs_per_user: i64,
1113
}
1214

1315
impl Config {

aggregation_mode/batcher/src/db.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ impl Db {
9696
.await
9797
}
9898

99+
pub async fn get_daily_tasks_by_address(&self, address: &str) -> Result<i64, sqlx::Error> {
100+
sqlx::query_scalar::<_, i64>(
101+
"SELECT COUNT(*)
102+
FROM tasks
103+
WHERE address = $1
104+
AND inserted_at::date = CURRENT_DATE",
105+
)
106+
.bind(address.to_lowercase())
107+
.fetch_one(&self.pool)
108+
.await
109+
}
110+
99111
pub async fn insert_task(
100112
&self,
101113
address: &str,

aggregation_mode/batcher/src/server/http.rs

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use actix_web::{
88
web::{self, Data},
99
App, HttpRequest, HttpResponse, HttpServer, Responder,
1010
};
11+
use agg_mode_sdk::types::Network;
1112
use aligned_sdk::aggregation_layer::AggregationModeProvingSystem;
13+
use alloy::signers::Signature;
1214
use sp1_sdk::{SP1ProofWithPublicValues, SP1VerifyingKey};
1315
use sqlx::types::BigDecimal;
1416

@@ -28,11 +30,17 @@ use crate::{
2830
pub struct BatcherServer {
2931
db: Db,
3032
config: Config,
33+
network: Network,
3134
}
3235

3336
impl BatcherServer {
3437
pub fn new(db: Db, config: Config) -> Self {
35-
Self { db, config }
38+
let network = Network::from_str(&config.network).expect("A valid network in config file");
39+
Self {
40+
db,
41+
config,
42+
network,
43+
}
3644
}
3745

3846
pub async fn start(&self) {
@@ -93,13 +101,57 @@ impl BatcherServer {
93101
req: HttpRequest,
94102
MultipartForm(data): MultipartForm<SubmitProofRequestSP1>,
95103
) -> impl Responder {
96-
let recovered_address = "0x70997970C51812dc3A010C7d01b50e0d17dc79C8".to_lowercase();
97-
98104
let Some(state) = req.app_data::<Data<BatcherServer>>() else {
99105
return HttpResponse::InternalServerError()
100106
.json(AppResponse::new_unsucessfull("Internal server error", 500));
101107
};
108+
102109
let state = state.get_ref();
110+
let Ok(signature) = Signature::from_str(&data.signature_hex.0) else {
111+
return HttpResponse::InternalServerError()
112+
.json(AppResponse::new_unsucessfull("Invalid signature", 500));
113+
};
114+
115+
let Ok(proof_content) = tokio::fs::read(data.proof.file.path()).await else {
116+
return HttpResponse::InternalServerError()
117+
.json(AppResponse::new_unsucessfull("Internal server error", 500));
118+
};
119+
120+
let Ok(vk_content) = tokio::fs::read(data.program_vk.file.path()).await else {
121+
return HttpResponse::InternalServerError()
122+
.json(AppResponse::new_unsucessfull("Internal server error", 500));
123+
};
124+
125+
// reconstruct message and recover address
126+
let msg = agg_mode_sdk::gateway::types::SubmitSP1ProofMessage::new(
127+
data.nonce.0,
128+
proof_content.clone(),
129+
vk_content.clone(),
130+
);
131+
let Ok(recovered_address) =
132+
signature.recover_address_from_prehash(&msg.eip712_hash(&state.network).into())
133+
else {
134+
return HttpResponse::InternalServerError()
135+
.json(AppResponse::new_unsucessfull("Internal server error", 500));
136+
};
137+
let recovered_address = recovered_address.to_string().to_lowercase();
138+
139+
// Checking if this address has submited more proofs than the ones allowed per day
140+
let Ok(daily_tasks_by_address) = state
141+
.db
142+
.get_daily_tasks_by_address(&recovered_address)
143+
.await
144+
else {
145+
return HttpResponse::InternalServerError()
146+
.json(AppResponse::new_unsucessfull("Internal server error", 500));
147+
};
148+
149+
if daily_tasks_by_address >= state.config.max_daily_proofs_per_user {
150+
return HttpResponse::InternalServerError().json(AppResponse::new_unsucessfull(
151+
"Request denied: Query limit exceeded.",
152+
400,
153+
));
154+
}
103155

104156
let Ok(count) = state.db.count_tasks_by_address(&recovered_address).await else {
105157
return HttpResponse::InternalServerError()
@@ -143,22 +195,11 @@ impl BatcherServer {
143195
400,
144196
));
145197
}
146-
147-
let Ok(proof_content) = tokio::fs::read(data.proof.file.path()).await else {
148-
return HttpResponse::InternalServerError()
149-
.json(AppResponse::new_unsucessfull("Internal server error", 500));
150-
};
151-
152198
let Ok(proof) = bincode::deserialize::<SP1ProofWithPublicValues>(&proof_content) else {
153199
return HttpResponse::BadRequest()
154200
.json(AppResponse::new_unsucessfull("Invalid SP1 proof", 400));
155201
};
156202

157-
let Ok(vk_content) = tokio::fs::read(data.program_vk.file.path()).await else {
158-
return HttpResponse::InternalServerError()
159-
.json(AppResponse::new_unsucessfull("Internal server error", 500));
160-
};
161-
162203
let Ok(vk) = bincode::deserialize::<SP1VerifyingKey>(&vk_content) else {
163204
return HttpResponse::BadRequest()
164205
.json(AppResponse::new_unsucessfull("Invalid vk", 400));

aggregation_mode/batcher/src/server/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub(super) struct SubmitProofRequestSP1 {
4444
pub nonce: Text<u64>,
4545
pub proof: TempFile,
4646
pub program_vk: TempFile,
47-
pub _signature_hex: Text<String>,
47+
pub signature_hex: Text<String>,
4848
}
4949

5050
#[derive(Debug, MultipartForm)]

aggregation_mode/cli/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "agg_mode_cli"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
serde = { workspace = true }
8+
tracing = { version = "0.1", features = ["log"] }
9+
tracing-subscriber = { version = "0.3.0", features = ["env-filter"] }
10+
bincode = "1.3.3"
11+
tokio = { version = "1", features = ["macros", "rt-multi-thread", "time"] }
12+
alloy = { workspace = true }
13+
agg_mode_sdk = { path = "../sdk"}
14+
sp1-sdk = "5.0.0"
15+
clap = { version = "4.5.4", features = ["derive"] }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod submit;

0 commit comments

Comments
 (0)