get_balance working
This commit is contained in:
parent
bbdceb713e
commit
1cf091d801
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
/target
|
||||
tmp
|
||||
|
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -3730,6 +3730,7 @@ dependencies = [
|
||||
"serde_json",
|
||||
"serde_yaml",
|
||||
"surrealdb",
|
||||
"thiserror 2.0.12",
|
||||
"tokio",
|
||||
"tokio-stream",
|
||||
"tonic",
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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<dyn Error>> {
|
||||
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:?}");
|
||||
|
||||
|
@ -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<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?;
|
||||
// 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<Account> = old_data.into();
|
||||
let vm_nodes: Vec<VmNode> = 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(())
|
||||
}
|
||||
|
||||
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.
|
||||
//
|
||||
// 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)]
|
||||
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)]
|
17
src/grpc.rs
17
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<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]
|
||||
impl BrainGeneralCli for BrainGeneralCliMock {
|
||||
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>>;
|
||||
|
||||
async fn get_balance(&self, req: Request<Pubkey>) -> Result<Response<AccountBalance>, 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<ReportNodeReq>) -> Result<Response<Empty>, Status> {
|
||||
|
@ -1,3 +1,3 @@
|
||||
pub mod grpc;
|
||||
pub mod models;
|
||||
pub mod db;
|
||||
pub mod old_brain;
|
||||
|
Loading…
Reference in New Issue
Block a user