Skip to content

Commit f4a4d26

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

File tree

6 files changed

+36
-2
lines changed

6 files changed

+36
-2
lines changed

aggregation_mode/batcher/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub struct Config {
88
pub db_connection_url: String,
99
pub eth_rpc_url: String,
1010
pub payment_service_address: String,
11+
pub max_daily_proofs_per_user: i64,
1112
}
1213

1314
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,23 @@ 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+
let Ok(daily_tasks_by_address) = state
106+
.db
107+
.get_daily_tasks_by_address(&recovered_address)
108+
.await
109+
else {
110+
return HttpResponse::InternalServerError()
111+
.json(AppResponse::new_unsucessfull("Internal server error", 500));
112+
};
113+
114+
if daily_tasks_by_address >= state.config.max_daily_proofs_per_user {
115+
return HttpResponse::InternalServerError().json(AppResponse::new_unsucessfull(
116+
"Request denied: Query limit exceeded.",
117+
400,
118+
));
119+
}
120+
104121
let Ok(count) = state.db.count_tasks_by_address(&recovered_address).await else {
105122
return HttpResponse::InternalServerError()
106123
.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
);

config-files/config-agg-mode-batcher-ethereum-package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ port: 8089
22
db_connection_url: "postgres://postgres:postgres@localhost:5435/"
33
eth_rpc_url: "http://localhost:8545"
44
payment_service_address: "0x922D6956C99E12DFeB3224DEA977D0939758A1Fe"
5+
max_daily_proofs_per_user: 32

config-files/config-agg-mode-batcher.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ port: 8089
22
db_connection_url: "postgres://postgres:postgres@localhost:5435/"
33
eth_rpc_url: "http://localhost:8545"
44
payment_service_address: "0x922D6956C99E12DFeB3224DEA977D0939758A1Fe"
5+
max_daily_proofs_per_user: 4

0 commit comments

Comments
 (0)