Skip to content

Commit a3e0aad

Browse files
feat(aggregation-mode): daily proofs limit
1 parent d7830a2 commit a3e0aad

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

aggregation_mode/batcher/src/db.rs

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

99+
pub async fn get_daily_tasks_by_address(
100+
&self,
101+
address: &str,
102+
) -> Result<Vec<Receipt>, sqlx::Error> {
103+
sqlx::query_as::<_, Receipt>(
104+
"SELECT status,merkle_path,nonce,address
105+
FROM tasks
106+
WHERE address = $1
107+
AND inserted_at::date = CURRENT_DATE",
108+
)
109+
.bind(address.to_lowercase())
110+
.fetch_all(&self.pool)
111+
.await
112+
}
113+
99114
pub async fn insert_task(
100115
&self,
101116
address: &str,

aggregation_mode/batcher/src/server/http.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,30 @@ impl BatcherServer {
101101
};
102102
let state = state.get_ref();
103103

104+
// Checking if this address has submited more proofs than the ones allowed per day
105+
const MAX_PROOFS_PER_DAY: usize = 4;
106+
107+
let daily_tasks_by_address = match state
108+
.db
109+
.get_daily_tasks_by_address(&recovered_address)
110+
.await
111+
{
112+
Ok(receipts) => receipts.len(),
113+
Err(_) => {
114+
return HttpResponse::InternalServerError().json(AppResponse::new_unsucessfull(
115+
format!("Internal server error").as_str(),
116+
500,
117+
))
118+
}
119+
};
120+
121+
if daily_tasks_by_address >= MAX_PROOFS_PER_DAY {
122+
return HttpResponse::InternalServerError().json(AppResponse::new_unsucessfull(
123+
format!("Request denied: Query limit exceeded.").as_str(),
124+
400,
125+
));
126+
}
127+
104128
let Ok(count) = state.db.count_tasks_by_address(&recovered_address).await else {
105129
return HttpResponse::InternalServerError()
106130
.json(AppResponse::new_unsucessfull("Internal server error", 500));

aggregation_mode/db/migrations/001_init.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ CREATE TABLE tasks (
88
program_commitment BYTEA,
99
merkle_path BYTEA,
1010
status task_status DEFAULT 'pending',
11-
nonce BIGINT NOT NULL
11+
nonce BIGINT NOT NULL,
12+
inserted_at TIMESTAMPTZ NOT NULL DEFAULT now()
1213
);
1314

1415
CREATE TABLE payment_events (
@@ -17,5 +18,6 @@ CREATE TABLE payment_events (
1718
amount BIGINT,
1819
started_at BIGINT,
1920
valid_until BIGINT,
20-
tx_hash CHAR(66) UNIQUE
21+
tx_hash CHAR(66) UNIQUE,
22+
inserted_at TIMESTAMPTZ NOT NULL DEFAULT now()
2123
);

0 commit comments

Comments
 (0)