get_balance working

This commit is contained in:
ghe0 2025-04-22 18:34:21 +03:00
parent bbdceb713e
commit 1cf091d801
Signed by: ghe0
GPG Key ID: 451028EE56A0FBB4
8 changed files with 65 additions and 30 deletions

1
.gitignore vendored

@ -1 +1,2 @@
/target /target
tmp

1
Cargo.lock generated

@ -3730,6 +3730,7 @@ dependencies = [
"serde_json", "serde_json",
"serde_yaml", "serde_yaml",
"surrealdb", "surrealdb",
"thiserror 2.0.12",
"tokio", "tokio",
"tokio-stream", "tokio-stream",
"tonic", "tonic",

@ -19,6 +19,7 @@ bs58 = "0.5.1"
tokio-stream = "0.1.17" tokio-stream = "0.1.17"
log = "0.4.27" log = "0.4.27"
env_logger = "0.11.8" env_logger = "0.11.8"
thiserror = "2.0.12"
[profile.release] [profile.release]
lto = true lto = true

@ -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 detee_shared::vm_proto::brain_vm_cli_server::BrainVmCliServer;
use surreal_brain::grpc::BrainGeneralCliMock; use surreal_brain::grpc::BrainGeneralCliMock;
use surreal_brain::grpc::BrainVmCliMock; use surreal_brain::grpc::BrainVmCliMock;
use surreal_brain::db;
use tonic::transport::{Identity, Server, ServerTlsConfig}; use tonic::transport::{Identity, Server, ServerTlsConfig};
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
env_logger::builder().filter_level(log::LevelFilter::Debug).init(); env_logger::builder().filter_level(log::LevelFilter::Debug).init();
db::init().await.unwrap();
let addr = "0.0.0.0:31337".parse().unwrap(); let addr = "0.0.0.0:31337".parse().unwrap();
let snp_cli_server = BrainVmCliServer::new(BrainVmCliMock {}); let snp_cli_server = BrainVmCliServer::new(BrainVmCliMock {});
let general_service_server = BrainGeneralCliServer::new(BrainGeneralCliMock {}); let general_service_server = BrainGeneralCliServer::new(BrainGeneralCliMock {});
let cert = std::fs::read_to_string("./brain-crt.pem").unwrap(); let cert = std::fs::read_to_string("./tmp/brain-crt.pem").unwrap();
let key = std::fs::read_to_string("./brain-mock/brain-key.pem").unwrap(); let key = std::fs::read_to_string("./tmp/brain-key.pem").unwrap();
let identity = Identity::from_pem(cert, key); let identity = Identity::from_pem(cert, key);

@ -1,14 +1,14 @@
// After deleting this migration, also delete old_brain structs // After deleting this migration, also delete old_brain structs
// and dangling impls from the model // and dangling impls from the model
use std::error::Error; use std::error::Error;
use surreal_brain::{models, old_brain}; use surreal_brain::{db, old_brain};
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> { async fn main() -> Result<(), Box<dyn Error>> {
let old_brain_data = old_brain::BrainData::load_from_disk()?; let old_brain_data = old_brain::BrainData::load_from_disk()?;
// println!("{}", serde_yaml::to_string(&old_brain_data)?); // 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:?}"); println!("{result:?}");

@ -1,3 +1,4 @@
use crate::old_brain;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::sync::LazyLock; use std::sync::LazyLock;
use surrealdb::{ use surrealdb::{
@ -7,11 +8,15 @@ use surrealdb::{
RecordId, Surreal, RecordId, Surreal,
}; };
use crate::old_brain;
static DB: LazyLock<Surreal<Client>> = LazyLock::new(Surreal::init); static DB: LazyLock<Surreal<Client>> = 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::<Ws>("localhost:8000").await?; DB.connect::<Ws>("localhost:8000").await?;
// Sign in to the server // Sign in to the server
DB.signin(Root { username: "root", password: "root" }).await?; DB.signin(Root { username: "root", password: "root" }).await?;
@ -19,7 +24,7 @@ async fn init() -> surrealdb::Result<()> {
Ok(()) 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<Account> = old_data.into(); let accounts: Vec<Account> = old_data.into();
let vm_nodes: Vec<VmNode> = old_data.into(); let vm_nodes: Vec<VmNode> = old_data.into();
let app_nodes: Vec<AppNode> = old_data.into(); let app_nodes: Vec<AppNode> = old_data.into();
@ -42,6 +47,18 @@ pub async fn migrate(old_data: &old_brain::BrainData) -> surrealdb::Result<()> {
Ok(()) Ok(())
} }
pub async fn account(address: &str) -> Result<Account, Error> {
let id = ("account", address);
let account: Option<Account> = 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. // I am not deleting this example cause I might need it later.
// //
// async fn get_wallet_contracts() -> surrealdb::Result<Vec<Wallet>> { // async fn get_wallet_contracts() -> surrealdb::Result<Vec<Wallet>> {
@ -62,29 +79,29 @@ pub async fn migrate(old_data: &old_brain::BrainData) -> surrealdb::Result<()> {
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Account { pub struct Account {
id: RecordId, pub id: RecordId,
balance: u64, pub balance: u64,
tmp_locked: u64, pub tmp_locked: u64,
escrow: u64, pub escrow: u64,
email: String, pub email: String,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct VmNode { pub struct VmNode {
id: RecordId, pub id: RecordId,
country: String, pub country: String,
region: String, pub region: String,
city: String, pub city: String,
ip: String, pub ip: String,
avail_mem_mb: u32, pub avail_mem_mb: u32,
avail_vcpus: u32, pub avail_vcpus: u32,
avail_storage_gbs: u32, pub avail_storage_gbs: u32,
avail_ipv4: u32, pub avail_ipv4: u32,
avail_ipv6: u32, pub avail_ipv6: u32,
avail_ports: u32, pub avail_ports: u32,
max_ports_per_vm: u32, pub max_ports_per_vm: u32,
price: u64, pub price: u64,
offline_minutes: u64, pub offline_minutes: u64,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]

@ -1,4 +1,5 @@
#![allow(dead_code)] #![allow(dead_code)]
use crate::db;
use detee_shared::app_proto::AppContract; use detee_shared::app_proto::AppContract;
use detee_shared::{ use detee_shared::{
common_proto::{Empty, Pubkey}, common_proto::{Empty, Pubkey},
@ -19,6 +20,18 @@ use tonic::{Request, Response, Status};
pub struct BrainGeneralCliMock {} pub struct BrainGeneralCliMock {}
impl From<db::Account> for AccountBalance {
fn from(account: db::Account) -> Self {
AccountBalance { balance: account.balance, tmp_locked: account.tmp_locked }
}
}
impl From<db::Error> for tonic::Status {
fn from(e: db::Error) -> Self {
Self::internal(format!("Internal error: {e}"))
}
}
#[tonic::async_trait] #[tonic::async_trait]
impl BrainGeneralCli for BrainGeneralCliMock { impl BrainGeneralCli for BrainGeneralCliMock {
type ListAccountsStream = Pin<Box<dyn Stream<Item = Result<Account, Status>> + Send>>; type ListAccountsStream = Pin<Box<dyn Stream<Item = Result<Account, Status>> + Send>>;
@ -29,8 +42,8 @@ impl BrainGeneralCli for BrainGeneralCliMock {
Pin<Box<dyn Stream<Item = Result<ListOperatorsResp, Status>> + Send>>; Pin<Box<dyn Stream<Item = Result<ListOperatorsResp, Status>> + Send>>;
async fn get_balance(&self, req: Request<Pubkey>) -> Result<Response<AccountBalance>, Status> { async fn get_balance(&self, req: Request<Pubkey>) -> Result<Response<AccountBalance>, Status> {
let _req = check_sig_from_req(req)?; let req = check_sig_from_req(req)?;
todo!("Ok(Response::new(self.data.get_balance(&req.pubkey).into()))") Ok(Response::new(db::account(&req.pubkey).await?.into()))
} }
async fn report_node(&self, req: Request<ReportNodeReq>) -> Result<Response<Empty>, Status> { async fn report_node(&self, req: Request<ReportNodeReq>) -> Result<Response<Empty>, Status> {

@ -1,3 +1,3 @@
pub mod grpc; pub mod grpc;
pub mod models; pub mod db;
pub mod old_brain; pub mod old_brain;