Check the user tx counter before updating the counter

This commit is contained in:
sydhds
2025-10-23 10:44:51 +02:00
parent 1f859c0eff
commit 632537f026
2 changed files with 31 additions and 3 deletions

View File

@@ -130,17 +130,30 @@ where
Some(req.estimated_gas_used / self.tx_gas_quota)
};
// Check (and reject) if the user has reached the rate limit
// If the user has previously reached the rate limit, the user will be slashed (but this can take some times)
// so in the meantime, we reject the tx
let (current_tx_counter, _) = self.user_db.get_tx_counter(&sender).unwrap_or_default();
// FIXME: should we implement PartialOrd here instead of u64 conv?
if u64::from(current_tx_counter) > u64::from(self.rate_limit) {
return Err(Status::resource_exhausted(
"Too many transactions sent by this user",
));
}
// Update the counter as soon as possible (should help to prevent spamming...)
let counter = self
.user_db
.on_new_tx(&sender, tx_counter_incr)
.unwrap_or_default();
/*
if counter > self.rate_limit {
return Err(Status::resource_exhausted(
"Too many transactions sent by this user",
));
}
*/
if req.transaction_hash.len() != PROVER_TX_HASH_BYTESIZE {
return Err(Status::invalid_argument(

View File

@@ -118,7 +118,7 @@ async fn test_grpc_register_users() {
}
*/
#[derive(Default)]
#[derive(Default, Clone)]
struct TxData {
chain_id: Option<U256>,
gas_price: Option<U256>,
@@ -501,7 +501,7 @@ async fn test_grpc_tx_exceed_gas_quota() {
proof_sender_channel_size: 500,
registration_min_amount: AppArgs::default_minimal_amount_for_registration(),
rln_identifier: AppArgs::default_rln_identifier_name(),
spam_limit: AppArgs::default_spam_limit(),
spam_limit: 15,
no_grpc_reflection: true,
tx_gas_quota,
};
@@ -518,7 +518,7 @@ async fn test_grpc_tx_exceed_gas_quota() {
..Default::default()
};
// Send a tx with 11 * the tx_gas_quota
proof_sender(port, addresses.clone(), 1, tx_data).await;
proof_sender(port, addresses.clone(), 1, tx_data.clone()).await;
tokio::time::sleep(Duration::from_secs(5)).await;
let res = query_user_info(port, vec![addresses[0]]).await;
@@ -532,4 +532,19 @@ async fn test_grpc_tx_exceed_gas_quota() {
panic!("Unexpected error {:?}", e);
}
}
// Send another tx with 11 * the tx_gas_quota
proof_sender(port, addresses.clone(), 1, tx_data).await;
let res = query_user_info(port, vec![addresses[0]]).await;
let resp = res[0].resp.as_ref().unwrap();
match resp {
Resp::Res(r) => {
// Check the tx counter is updated to the right value
assert_eq!(r.tx_count, quota_mult * 2);
}
Resp::Error(e) => {
panic!("Unexpected error {:?}", e);
}
}
}