From d82fae161ff055eb72f5a1306032e2c81e46baad Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Tue, 16 Aug 2022 17:47:04 +0000 Subject: [PATCH] use rpm flag when creating a user --- .../src/bin/web3_proxy_cli/create_user.rs | 26 ++++++++++++------- web3_proxy/src/frontend/users.rs | 4 ++- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/web3_proxy/src/bin/web3_proxy_cli/create_user.rs b/web3_proxy/src/bin/web3_proxy_cli/create_user.rs index 554548dd..2534c65f 100644 --- a/web3_proxy/src/bin/web3_proxy_cli/create_user.rs +++ b/web3_proxy/src/bin/web3_proxy_cli/create_user.rs @@ -2,12 +2,12 @@ use anyhow::Context; use argh::FromArgs; use entities::{user, user_keys}; use ethers::prelude::Address; -use sea_orm::ActiveModelTrait; +use sea_orm::{ActiveModelTrait, TransactionTrait}; use tracing::info; use uuid::Uuid; use web3_proxy::users::new_api_key; -fn default_rpm() -> usize { +fn default_rpm() -> u32 { 6_000_000 } @@ -30,11 +30,13 @@ pub struct CreateUserSubCommand { #[argh(option, default = "default_rpm()")] /// maximum requests per minute - rpm: usize, + rpm: u32, } impl CreateUserSubCommand { pub async fn main(self, db: &sea_orm::DatabaseConnection) -> anyhow::Result<()> { + let txn = db.begin().await?; + // TODO: would be nice to use the fixed array instead of a Vec in the entities let address = self .address @@ -49,23 +51,29 @@ impl CreateUserSubCommand { ..Default::default() }; - let u = u.insert(db).await.context("Failed saving new user")?; + let u = u.save(&txn).await.context("Failed saving new user")?; - info!("user #{}: {:?}", u.id, Address::from_slice(&u.address)); + info!( + "user #{}: {:?}", + u.id.as_ref(), + Address::from_slice(u.address.as_ref()) + ); // create a key for the new user // TODO: requests_per_minute should be configurable let uk = user_keys::ActiveModel { - user_id: sea_orm::Set(u.id), + user_id: u.id, api_key: sea_orm::Set(self.api_key), - requests_per_minute: sea_orm::Set(6_000_000), + requests_per_minute: sea_orm::Set(self.rpm), ..Default::default() }; // TODO: if this fails, rever adding the user, too - let uk = uk.insert(db).await.context("Failed saving new user key")?; + let uk = uk.save(&txn).await.context("Failed saving new user key")?; - info!("user key: {}", uk.api_key); + txn.commit().await?; + + info!("user key: {}", uk.api_key.as_ref()); Ok(()) } diff --git a/web3_proxy/src/frontend/users.rs b/web3_proxy/src/frontend/users.rs index b822faa2..a2d19153 100644 --- a/web3_proxy/src/frontend/users.rs +++ b/web3_proxy/src/frontend/users.rs @@ -21,7 +21,7 @@ use std::sync::Arc; use crate::app::Web3ProxyApp; -use super::{rate_limit::RateLimitResult, errors::anyhow_error_into_response}; +use super::{errors::anyhow_error_into_response, rate_limit::RateLimitResult}; pub async fn create_user( // this argument tells axum to parse the request body @@ -65,6 +65,8 @@ pub async fn create_user( // TODO: proper error message let user = user.insert(db).await.unwrap(); + // + // TODO: do not expose user ids (StatusCode::CREATED, Json(user)).into_response() }