Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
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
22 changes: 22 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,28 @@ impl BatcherServer {
};
let state = state.get_ref();

// Checking if this address has submited more proofs than the ones allowed per day
const MAX_PROOFS_PER_DAY: usize = 4;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The max proofs per day should be on a config

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 b5a966c


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 >= 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.
);
Loading