Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions aggregation_mode/batcher/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct Config {
pub db_connection_url: String,
pub eth_rpc_url: String,
pub payment_service_address: String,
pub max_proofs_per_day: usize,
}

impl Config {
Expand Down
15 changes: 15 additions & 0 deletions aggregation_mode/batcher/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ impl Db {
.await
}

pub async fn get_daily_tasks_by_address(
&self,
address: &str,
) -> Result<Vec<Receipt>, sqlx::Error> {
sqlx::query_as::<_, Receipt>(
"SELECT status,merkle_path,nonce,address
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should make a count instead of returning a list and applying a length

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 0a143ac

FROM tasks
WHERE address = $1
AND inserted_at::date = CURRENT_DATE",
)
.bind(address.to_lowercase())
.fetch_all(&self.pool)
.await
}

pub async fn insert_task(
&self,
address: &str,
Expand Down
20 changes: 20 additions & 0 deletions aggregation_mode/batcher/src/server/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,26 @@ impl BatcherServer {
};
let state = state.get_ref();

// Checking if this address has submited more proofs than the ones allowed per day
let daily_tasks_by_address = match state
.db
.get_daily_tasks_by_address(&recovered_address)
.await
{
Ok(receipts) => receipts.len(),
Err(_) => {
return HttpResponse::InternalServerError()
.json(AppResponse::new_unsucessfull("Internal server error", 500))
}
};

if daily_tasks_by_address >= state.config.max_proofs_per_day {
return HttpResponse::InternalServerError().json(AppResponse::new_unsucessfull(
"Request denied: Query limit exceeded.",
400,
));
}

let Ok(count) = state.db.count_tasks_by_address(&recovered_address).await else {
return HttpResponse::InternalServerError()
.json(AppResponse::new_unsucessfull("Internal server error", 500));
Expand Down
6 changes: 4 additions & 2 deletions aggregation_mode/db/migrations/001_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ CREATE TABLE tasks (
program_commitment BYTEA,
merkle_path BYTEA,
status task_status DEFAULT 'pending',
nonce BIGINT NOT NULL
nonce BIGINT NOT NULL,
inserted_at TIMESTAMPTZ NOT NULL DEFAULT now()
Comment thread
JuArce marked this conversation as resolved.
);

CREATE TABLE payment_events (
Expand All @@ -17,5 +18,6 @@ CREATE TABLE payment_events (
amount BIGINT,
started_at BIGINT,
valid_until BIGINT,
tx_hash CHAR(66) UNIQUE
tx_hash CHAR(66) UNIQUE,
inserted_at TIMESTAMPTZ NOT NULL DEFAULT now()
Comment thread
JuArce marked this conversation as resolved.
);
1 change: 1 addition & 0 deletions config-files/config-agg-mode-batcher-ethereum-package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ port: 8089
db_connection_url: "postgres://postgres:postgres@localhost:5435/"
eth_rpc_url: "http://localhost:8545"
payment_service_address: "0x922D6956C99E12DFeB3224DEA977D0939758A1Fe"
max_proofs_per_day: 32
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

max_daily_proofs_per_user

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in d1c88a2

1 change: 1 addition & 0 deletions config-files/config-agg-mode-batcher.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ port: 8089
db_connection_url: "postgres://postgres:postgres@localhost:5435/"
eth_rpc_url: "http://localhost:8545"
payment_service_address: "0x922D6956C99E12DFeB3224DEA977D0939758A1Fe"
max_proofs_per_day: 4
Loading