From dede79fde116475fdfc2c4ef9eb874437838b0a7 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Wed, 3 Aug 2022 23:17:02 +0000 Subject: [PATCH] regenerate entities --- Cargo.lock | 3 ++ README.md | 8 ++++++ entities/Cargo.toml | 5 ++++ entities/src/block_list.rs | 2 +- entities/src/user.rs | 2 +- entities/src/user_keys.rs | 1 + .../src/m20220101_000001_create_table.rs | 2 +- web3-proxy/src/app.rs | 10 +------ web3-proxy/src/frontend/users.rs | 28 ++++++++++--------- web3-proxy/src/jsonrpc.rs | 1 - 10 files changed, 36 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ddb43149..43b45c2f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1386,6 +1386,9 @@ dependencies = [ [[package]] name = "entities" version = "0.1.0" +dependencies = [ + "sea-orm", +] [[package]] name = "eth-keystore" diff --git a/README.md b/README.md index 7c87e47d..dc6388e8 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,14 @@ $ websocat ws://127.0.0.1:8544 You can copy `config/example.toml` to `config/production-$CHAINNAME.toml` and then run `docker-compose up --build -d` start proxies for many chains. +## Database entities + +Types for database entities are generated from a development database. + +``` +sea-orm-cli generate entity -u mysql://root:dev_web3_proxy@127.0.0.1:3306/dev_web3_proxy -o entities/src +``` + ## Flame Graphs Flame graphs make finding slow code painless: diff --git a/entities/Cargo.toml b/entities/Cargo.toml index f9763dd4..65563b06 100644 --- a/entities/Cargo.toml +++ b/entities/Cargo.toml @@ -3,6 +3,11 @@ name = "entities" version = "0.1.0" edition = "2021" +[lib] +name = "entities" +path = "src/mod.rs" + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +sea-orm = { version = "0.9.1" } diff --git a/entities/src/block_list.rs b/entities/src/block_list.rs index 203f3aec..cbe82db3 100644 --- a/entities/src/block_list.rs +++ b/entities/src/block_list.rs @@ -7,8 +7,8 @@ use sea_orm::entity::prelude::*; pub struct Model { #[sea_orm(primary_key)] pub id: i64, + #[sea_orm(unique)] pub address: String, - pub chain: i32, pub description: String, } diff --git a/entities/src/user.rs b/entities/src/user.rs index 64b50fe0..b75bf964 100644 --- a/entities/src/user.rs +++ b/entities/src/user.rs @@ -10,7 +10,7 @@ pub struct Model { #[sea_orm(unique)] pub address: String, pub description: String, - pub email: String, + pub email: Option, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] diff --git a/entities/src/user_keys.rs b/entities/src/user_keys.rs index 2df86a89..660e88fa 100644 --- a/entities/src/user_keys.rs +++ b/entities/src/user_keys.rs @@ -8,6 +8,7 @@ pub struct Model { #[sea_orm(primary_key)] pub id: i64, pub user_id: i64, + #[sea_orm(unique)] pub api_key: String, pub description: String, pub private_txs: i8, diff --git a/migration/src/m20220101_000001_create_table.rs b/migration/src/m20220101_000001_create_table.rs index 3448c9c3..fcdaf83f 100644 --- a/migration/src/m20220101_000001_create_table.rs +++ b/migration/src/m20220101_000001_create_table.rs @@ -25,7 +25,7 @@ impl MigrationTrait for Migration { .unique_key(), ) .col(ColumnDef::new(User::Description).string().not_null()) - .col(ColumnDef::new(User::Email).string().not_null()) + .col(ColumnDef::new(User::Email).string()) .to_owned(), ) .await?; diff --git a/web3-proxy/src/app.rs b/web3-proxy/src/app.rs index bf5a46ce..138bc26f 100644 --- a/web3-proxy/src/app.rs +++ b/web3-proxy/src/app.rs @@ -465,7 +465,7 @@ impl Web3ProxyApp { // TODO: this only needs to be unique per connection. we don't need it globably unique let subscription_id = subscription_count.fetch_add(1, atomic::Ordering::SeqCst); - let subscription_id = format!("{:#x}", subscription_id); + let subscription_id = U64::from(subscription_id); // save the id so we can use it in the response let id = payload.id.clone(); @@ -476,8 +476,6 @@ impl Web3ProxyApp { Some(x) if x == json!(["newHeads"]) => { let head_block_receiver = self.head_block_receiver.clone(); - let subscription_id = subscription_id.clone(); - trace!(?subscription_id, "new heads subscription"); tokio::spawn(async move { let mut head_block_receiver = Abortable::new( @@ -510,8 +508,6 @@ impl Web3ProxyApp { Some(x) if x == json!(["newPendingTransactions"]) => { let pending_tx_receiver = self.pending_tx_sender.subscribe(); - let subscription_id = subscription_id.clone(); - let mut pending_tx_receiver = Abortable::new( BroadcastStream::new(pending_tx_receiver), subscription_registration, @@ -551,8 +547,6 @@ impl Web3ProxyApp { // TODO: too much copy/pasta with newPendingTransactions let pending_tx_receiver = self.pending_tx_sender.subscribe(); - let subscription_id = subscription_id.clone(); - let mut pending_tx_receiver = Abortable::new( BroadcastStream::new(pending_tx_receiver), subscription_registration, @@ -600,8 +594,6 @@ impl Web3ProxyApp { subscription_registration, ); - let subscription_id = subscription_id.clone(); - trace!(?subscription_id, "pending transactions subscription"); // TODO: do something with this handle? diff --git a/web3-proxy/src/frontend/users.rs b/web3-proxy/src/frontend/users.rs index 339ba502..c80769ea 100644 --- a/web3-proxy/src/frontend/users.rs +++ b/web3-proxy/src/frontend/users.rs @@ -11,6 +11,10 @@ use axum::{http::StatusCode, response::IntoResponse, Json}; use ethers::prelude::{Address, Bytes}; use serde::{Deserialize, Serialize}; +use entities::user; + +// use entities::user::User; + pub async fn create_user( // this argument tells axum to parse the request body // as JSON into a `CreateUser` type @@ -18,28 +22,26 @@ pub async fn create_user( ) -> impl IntoResponse { // TODO: rate limit by ip // TODO: insert your application logic here - let user = User { - id: 1337, - eth_address: payload.eth_address, + let user = user::ActiveModel { + address: sea_orm::Set(payload.address.to_string()), + ..Default::default() }; + // TODO: optional email + + todo!(); + // this will be converted into a JSON response // with a status code of `201 Created` - (StatusCode::CREATED, Json(user)) + // (StatusCode::CREATED, Json(user)) } // the input to our `create_user` handler #[derive(Deserialize)] pub struct CreateUser { - eth_address: Address, - // TODO: validation + address: Address, + // TODO: make sure the email address is valid email: Option, signature: Bytes, -} - -// the output to our `create_user` handler -#[derive(Serialize)] -struct User { - id: u64, - eth_address: Address, + invite_code: String, } diff --git a/web3-proxy/src/jsonrpc.rs b/web3-proxy/src/jsonrpc.rs index 4ae5d9dc..dc81963e 100644 --- a/web3-proxy/src/jsonrpc.rs +++ b/web3-proxy/src/jsonrpc.rs @@ -168,7 +168,6 @@ impl JsonRpcForwardedResponse { JsonRpcForwardedResponse { jsonrpc: "2.0".to_string(), - // TODO: what id can we use? how do we make sure the incoming id gets attached to this? id, result: None, error: Some(JsonRpcErrorData {