From 1cf091d801d5d0ca05e5d122ce92dcad6d449d09 Mon Sep 17 00:00:00 2001 From: ghe0 Date: Tue, 22 Apr 2025 18:34:21 +0300 Subject: [PATCH] get_balance working --- .gitignore | 1 + Cargo.lock | 1 + Cargo.toml | 1 + src/bin/brain.rs | 6 ++-- src/bin/migration0.rs | 4 +-- src/{models.rs => db.rs} | 63 +++++++++++++++++++++++++--------------- src/grpc.rs | 17 +++++++++-- src/lib.rs | 2 +- 8 files changed, 65 insertions(+), 30 deletions(-) rename src/{models.rs => db.rs} (89%) diff --git a/.gitignore b/.gitignore index ea8c4bf..5c919d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +tmp diff --git a/Cargo.lock b/Cargo.lock index 6dfb900..4b4828d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3730,6 +3730,7 @@ dependencies = [ "serde_json", "serde_yaml", "surrealdb", + "thiserror 2.0.12", "tokio", "tokio-stream", "tonic", diff --git a/Cargo.toml b/Cargo.toml index b5faf6f..bfdce67 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ bs58 = "0.5.1" tokio-stream = "0.1.17" log = "0.4.27" env_logger = "0.11.8" +thiserror = "2.0.12" [profile.release] lto = true diff --git a/src/bin/brain.rs b/src/bin/brain.rs index e4f2532..f90db3d 100644 --- a/src/bin/brain.rs +++ b/src/bin/brain.rs @@ -2,18 +2,20 @@ use detee_shared::general_proto::brain_general_cli_server::BrainGeneralCliServer use detee_shared::vm_proto::brain_vm_cli_server::BrainVmCliServer; use surreal_brain::grpc::BrainGeneralCliMock; use surreal_brain::grpc::BrainVmCliMock; +use surreal_brain::db; use tonic::transport::{Identity, Server, ServerTlsConfig}; #[tokio::main] async fn main() { env_logger::builder().filter_level(log::LevelFilter::Debug).init(); + db::init().await.unwrap(); let addr = "0.0.0.0:31337".parse().unwrap(); let snp_cli_server = BrainVmCliServer::new(BrainVmCliMock {}); let general_service_server = BrainGeneralCliServer::new(BrainGeneralCliMock {}); - let cert = std::fs::read_to_string("./brain-crt.pem").unwrap(); - let key = std::fs::read_to_string("./brain-mock/brain-key.pem").unwrap(); + let cert = std::fs::read_to_string("./tmp/brain-crt.pem").unwrap(); + let key = std::fs::read_to_string("./tmp/brain-key.pem").unwrap(); let identity = Identity::from_pem(cert, key); diff --git a/src/bin/migration0.rs b/src/bin/migration0.rs index c04b08a..4b20757 100644 --- a/src/bin/migration0.rs +++ b/src/bin/migration0.rs @@ -1,14 +1,14 @@ // After deleting this migration, also delete old_brain structs // and dangling impls from the model use std::error::Error; -use surreal_brain::{models, old_brain}; +use surreal_brain::{db, old_brain}; #[tokio::main] async fn main() -> Result<(), Box> { let old_brain_data = old_brain::BrainData::load_from_disk()?; // println!("{}", serde_yaml::to_string(&old_brain_data)?); - let result = models::migrate(&old_brain_data).await?; + let result = db::migration0(&old_brain_data).await?; println!("{result:?}"); diff --git a/src/models.rs b/src/db.rs similarity index 89% rename from src/models.rs rename to src/db.rs index 072a1a3..5df9bae 100644 --- a/src/models.rs +++ b/src/db.rs @@ -1,3 +1,4 @@ +use crate::old_brain; use serde::{Deserialize, Serialize}; use std::sync::LazyLock; use surrealdb::{ @@ -7,11 +8,15 @@ use surrealdb::{ RecordId, Surreal, }; -use crate::old_brain; - static DB: LazyLock> = LazyLock::new(Surreal::init); -async fn init() -> surrealdb::Result<()> { +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error(transparent)] + DataBase(#[from] surrealdb::Error), +} + +pub async fn init() -> surrealdb::Result<()> { DB.connect::("localhost:8000").await?; // Sign in to the server DB.signin(Root { username: "root", password: "root" }).await?; @@ -19,7 +24,7 @@ async fn init() -> surrealdb::Result<()> { Ok(()) } -pub async fn migrate(old_data: &old_brain::BrainData) -> surrealdb::Result<()> { +pub async fn migration0(old_data: &old_brain::BrainData) -> surrealdb::Result<()> { let accounts: Vec = old_data.into(); let vm_nodes: Vec = old_data.into(); let app_nodes: Vec = old_data.into(); @@ -42,6 +47,18 @@ pub async fn migrate(old_data: &old_brain::BrainData) -> surrealdb::Result<()> { Ok(()) } +pub async fn account(address: &str) -> Result { + let id = ("account", address); + let account: Option = DB.select(id).await?; + let account = match account { + Some(account) => account, + None => { + Account { id: id.into(), balance: 0, tmp_locked: 0, escrow: 0, email: String::new() } + } + }; + Ok(account) +} + // I am not deleting this example cause I might need it later. // // async fn get_wallet_contracts() -> surrealdb::Result> { @@ -62,29 +79,29 @@ pub async fn migrate(old_data: &old_brain::BrainData) -> surrealdb::Result<()> { #[derive(Debug, Serialize, Deserialize)] pub struct Account { - id: RecordId, - balance: u64, - tmp_locked: u64, - escrow: u64, - email: String, + pub id: RecordId, + pub balance: u64, + pub tmp_locked: u64, + pub escrow: u64, + pub email: String, } #[derive(Debug, Serialize, Deserialize)] pub struct VmNode { - id: RecordId, - country: String, - region: String, - city: String, - ip: String, - avail_mem_mb: u32, - avail_vcpus: u32, - avail_storage_gbs: u32, - avail_ipv4: u32, - avail_ipv6: u32, - avail_ports: u32, - max_ports_per_vm: u32, - price: u64, - offline_minutes: u64, + pub id: RecordId, + pub country: String, + pub region: String, + pub city: String, + pub ip: String, + pub avail_mem_mb: u32, + pub avail_vcpus: u32, + pub avail_storage_gbs: u32, + pub avail_ipv4: u32, + pub avail_ipv6: u32, + pub avail_ports: u32, + pub max_ports_per_vm: u32, + pub price: u64, + pub offline_minutes: u64, } #[derive(Debug, Serialize, Deserialize)] diff --git a/src/grpc.rs b/src/grpc.rs index 088539b..08ff394 100644 --- a/src/grpc.rs +++ b/src/grpc.rs @@ -1,4 +1,5 @@ #![allow(dead_code)] +use crate::db; use detee_shared::app_proto::AppContract; use detee_shared::{ common_proto::{Empty, Pubkey}, @@ -19,6 +20,18 @@ use tonic::{Request, Response, Status}; pub struct BrainGeneralCliMock {} +impl From for AccountBalance { + fn from(account: db::Account) -> Self { + AccountBalance { balance: account.balance, tmp_locked: account.tmp_locked } + } +} + +impl From for tonic::Status { + fn from(e: db::Error) -> Self { + Self::internal(format!("Internal error: {e}")) + } +} + #[tonic::async_trait] impl BrainGeneralCli for BrainGeneralCliMock { type ListAccountsStream = Pin> + Send>>; @@ -29,8 +42,8 @@ impl BrainGeneralCli for BrainGeneralCliMock { Pin> + Send>>; async fn get_balance(&self, req: Request) -> Result, Status> { - let _req = check_sig_from_req(req)?; - todo!("Ok(Response::new(self.data.get_balance(&req.pubkey).into()))") + let req = check_sig_from_req(req)?; + Ok(Response::new(db::account(&req.pubkey).await?.into())) } async fn report_node(&self, req: Request) -> Result, Status> { diff --git a/src/lib.rs b/src/lib.rs index 4377622..250e913 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,3 @@ pub mod grpc; -pub mod models; +pub mod db; pub mod old_brain;