Skip to content

Commit 770601d

Browse files
committed
feat: nonce endpoint
1 parent 4f48a7f commit 770601d

File tree

9 files changed

+130
-65
lines changed

9 files changed

+130
-65
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ agg_mode_docker_up:
308308
agg_mode_docker_down:
309309
@cd aggregation_mode && docker-compose down
310310

311+
agg_mode_docker_clean: agg_mode_docker_down
312+
docker volume rm aggregation-mode_postgres_data
313+
311314
agg_mode_run_migrations: agg_mode_docker_up
312315
cargo run --manifest-path ./aggregation_mode/Cargo.toml --release --bin migrate -- postgres://postgres:postgres@localhost:5435/
313316

aggregation_mode/batcher/src/db.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,14 @@ impl Db {
2020

2121
Ok(Self { pool })
2222
}
23+
24+
pub async fn count_proofs_by_address(&self, address: &str) -> Result<i64, sqlx::Error> {
25+
let (count,) =
26+
sqlx::query_as::<_, (i64,)>("SELECT COUNT(*) FROM proofs WHERE address = $1")
27+
.bind(address)
28+
.fetch_one(&self.pool)
29+
.await?;
30+
31+
Ok(count)
32+
}
2333
}

aggregation_mode/batcher/src/main.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::env;
22

33
use agg_mode_batcher::config::Config;
4-
use agg_mode_batcher::{db::Db, server::BatcherServer};
4+
use agg_mode_batcher::{db::Db, server::http::BatcherServer};
5+
use tracing_subscriber::{EnvFilter, FmtSubscriber};
56

67
fn read_config_filepath_from_args() -> String {
78
let args: Vec<String> = env::args().collect();
@@ -17,6 +18,10 @@ fn read_config_filepath_from_args() -> String {
1718

1819
#[actix_web::main]
1920
async fn main() {
21+
let filter = EnvFilter::new("info,sp1_cuda=warn");
22+
let subscriber = FmtSubscriber::builder().with_env_filter(filter).finish();
23+
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
24+
2025
let config_file_path = read_config_filepath_from_args();
2126
tracing::info!("Loading config from {}...", config_file_path);
2227
let config = Config::from_file(&config_file_path).expect("Config is valid");
@@ -25,7 +30,9 @@ async fn main() {
2530
let db = Db::try_new(&config.db_connection_url)
2631
.await
2732
.expect("db to start");
28-
let http_server = BatcherServer::new(db.clone(), config);
2933

34+
let http_server = BatcherServer::new(db.clone(), config.clone());
35+
36+
tracing::info!("Starting server at port {}", config.port);
3037
http_server.start().await.expect("Server to keep running");
3138
}

aggregation_mode/batcher/src/server.rs

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use actix_web::{
2+
web::{self, Data},
3+
App, HttpRequest, HttpResponse, HttpServer, Responder,
4+
};
5+
6+
use super::types::AppResponse;
7+
8+
use crate::{config::Config, db::Db};
9+
10+
#[derive(Clone, Debug)]
11+
pub struct BatcherServer {
12+
db: Db,
13+
config: Config,
14+
}
15+
16+
impl BatcherServer {
17+
pub fn new(db: Db, config: Config) -> Self {
18+
Self { db, config }
19+
}
20+
21+
pub async fn start(&self) -> Result<(), std::io::Error> {
22+
// Note: BatcherServer is thread safe so we can just clone it (no need to add mutexes)
23+
let port = self.config.port;
24+
let state = self.clone();
25+
26+
HttpServer::new(move || {
27+
App::new()
28+
.app_data(Data::new(state.clone()))
29+
.route("/nonce/{address}", web::get().to(Self::get_nonce))
30+
.route(
31+
"/proof/merkle/:receipt",
32+
web::get().to(Self::get_proof_merkle_path),
33+
)
34+
.route("/proof", web::post().to(Self::post_proof))
35+
})
36+
.bind(("127.0.0.1", port))?
37+
.run()
38+
.await
39+
}
40+
41+
async fn get_nonce(req: HttpRequest) -> impl Responder {
42+
let Some(address) = req.match_info().get("address") else {
43+
return HttpResponse::BadRequest()
44+
.json(AppResponse::new_unsucessfull("Missing address", 400));
45+
};
46+
47+
// TODO: validate valid ethereum address
48+
49+
let Some(state) = req.app_data::<Data<BatcherServer>>() else {
50+
return HttpResponse::InternalServerError()
51+
.json(AppResponse::new_unsucessfull("Internal server error", 500));
52+
};
53+
54+
let state = state.get_ref();
55+
match state.db.count_proofs_by_address(address).await {
56+
Ok(count) => HttpResponse::Ok().json(AppResponse::new_sucessfull(serde_json::json!(
57+
{
58+
"nonce": count
59+
}
60+
))),
61+
Err(err) => {
62+
tracing::error!(error = ?err, "failed to count proofs");
63+
HttpResponse::InternalServerError().finish()
64+
}
65+
}
66+
}
67+
68+
// TODO: receive the proof and 1. decode it, 2. verify it, 3. add to the db
69+
async fn post_proof(req: HttpRequest) -> impl Responder {
70+
HttpResponse::Ok()
71+
}
72+
73+
// TODO: get the proof merkle path for the receipt id (proof commitment)
74+
async fn get_proof_merkle_path(req: HttpRequest) -> impl Responder {
75+
HttpResponse::Ok()
76+
}
77+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod http;
2+
mod types;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use serde::{Deserialize, Serialize};
2+
use serde_json::Value;
3+
4+
#[derive(Serialize, Deserialize)]
5+
pub(super) struct AppResponse {
6+
status: u16,
7+
message: String,
8+
data: Value,
9+
}
10+
11+
impl AppResponse {
12+
pub(super) fn new_sucessfull(data: Value) -> Self {
13+
Self {
14+
status: 200,
15+
message: "Ok".to_string(),
16+
data,
17+
}
18+
}
19+
20+
pub(super) fn new_unsucessfull(message: &str, status: u16) -> Self {
21+
Self {
22+
status,
23+
message: message.to_string(),
24+
data: serde_json::json!({}),
25+
}
26+
}
27+
}

aggregation_mode/db/migrations/001_init.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CREATE TABLE proofs (
1313
proof BYTEA,
1414
public_inputs BYTEA,
1515
proof_commitment BYTEA,
16+
merkle_path BYTEA,
1617
task_id UUID REFERENCES tasks(task_id)
1718
);
1819

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
port: 8089
2-
db_connection_url: "postgres://postgres:postgres@localhost/"
2+
db_connection_url: "postgres://postgres:postgres@localhost:5435/"
33
eth_rpc_url: "http://localhost:8545"

0 commit comments

Comments
 (0)