centralize configuration constants
update database initialization for test env
This commit is contained in:
Noor 2025-04-24 15:53:38 +05:30
parent 6a99c146ce
commit 0ece617496
Signed by: noormohammedb
GPG Key ID: D83EFB8B3B967146
6 changed files with 48 additions and 23 deletions

@ -1,21 +1,24 @@
use detee_shared::general_proto::brain_general_cli_server::BrainGeneralCliServer;
use detee_shared::vm_proto::brain_vm_cli_server::BrainVmCliServer;
use surreal_brain::constants::{
BRAIN_GRPC_ADDR, CERT_KEY_PATH, CERT_PATH, DB_ADDRESS, DB_NAME, DB_NS,
};
use surreal_brain::db;
use surreal_brain::grpc::BrainGeneralCliForReal;
use surreal_brain::grpc::BrainVmCliForReal;
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();
db::init(DB_ADDRESS, DB_NS, DB_NAME).await.unwrap();
let addr = BRAIN_GRPC_ADDR.parse().unwrap();
let snp_cli_server = BrainVmCliServer::new(BrainVmCliForReal {});
let general_service_server = BrainGeneralCliServer::new(BrainGeneralCliForReal {});
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 cert = std::fs::read_to_string(CERT_PATH).unwrap();
let key = std::fs::read_to_string(CERT_KEY_PATH).unwrap();
let identity = Identity::from_pem(cert, key);

24
src/constants.rs Normal file

@ -0,0 +1,24 @@
pub const BRAIN_GRPC_ADDR: &str = "0.0.0.0:31337";
pub const CERT_PATH: &str = "./tmp/brain-crt.pem";
pub const CERT_KEY_PATH: &str = "./tmp/brain-key.pem";
pub const DB_ADDRESS: &str = "localhost:8000";
pub const DB_NS: &str = "brain";
pub const DB_NAME: &str = "migration";
// TODO: read from .env
pub const DB_USER: &str = "root";
pub const DB_PASS: &str = "root";
pub const ACCOUNT: &str = "account";
pub const OPERATOR: &str = "operator";
pub const VM_CONTRACT: &str = "vm_contract";
pub const VM_NODE: &str = "vm_node";
pub const ADMIN_ACCOUNTS: &[&str] = &[
"x52w7jARC5erhWWK65VZmjdGXzBK6ZDgfv1A283d8XK",
"FHuecMbeC1PfjkW2JKyoicJAuiU7khgQT16QUB3Q1XdL",
"H21Shi4iE7vgfjWEQNvzmpmBMJSaiZ17PYUcdNoAoKNc",
];
pub const OLD_BRAIN_DATA_PATH: &str = "./saved_data.yaml";

@ -1,3 +1,6 @@
use crate::constants::{
ACCOUNT, DB_ADDRESS, DB_NAME, DB_NS, DB_PASS, DB_USER, VM_CONTRACT, VM_NODE,
};
use crate::old_brain;
use serde::{Deserialize, Serialize};
use std::sync::LazyLock;
@ -9,9 +12,6 @@ use surrealdb::{
};
static DB: LazyLock<Surreal<Client>> = LazyLock::new(Surreal::init);
pub const ACCOUNT: &str = "account";
pub const VM_CONTRACT: &str = "vm_contract";
pub const VM_NODE: &str = "vm_node";
#[derive(thiserror::Error, Debug)]
pub enum Error {
@ -19,11 +19,11 @@ pub enum Error {
DataBase(#[from] surrealdb::Error),
}
pub async fn init() -> surrealdb::Result<()> {
DB.connect::<Ws>("localhost:8000").await?;
pub async fn init(db_address: &str, ns: &str, db: &str) -> surrealdb::Result<()> {
DB.connect::<Ws>(db_address).await?;
// Sign in to the server
DB.signin(Root { username: "root", password: "root" }).await?;
DB.use_ns("brain").use_db("migration").await?;
DB.signin(Root { username: DB_USER, password: DB_PASS }).await?;
DB.use_ns(ns).use_db(db).await?;
Ok(())
}
@ -33,7 +33,7 @@ pub async fn migration0(old_data: &old_brain::BrainData) -> surrealdb::Result<()
let app_nodes: Vec<AppNode> = old_data.into();
let vm_contracts: Vec<VmContract> = old_data.into();
init().await?;
init(DB_ADDRESS, DB_NS, DB_NAME).await?;
println!("Inserting accounts...");
let _: Vec<Account> = DB.insert(()).content(accounts).await?;

@ -1,4 +1,5 @@
#![allow(dead_code)]
use crate::constants::{ACCOUNT, ADMIN_ACCOUNTS, VM_NODE};
use crate::db;
use detee_shared::app_proto::{AppContract, AppNodeListResp};
use detee_shared::{
@ -129,8 +130,8 @@ impl BrainVmDaemon for BrainVmDaemonForReal {
let req = check_sig_from_req(req)?;
info!("Starting registration process for {:?}", req);
db::VmNode {
id: surrealdb::RecordId::from((db::VM_NODE, req.node_pubkey.clone())),
operator: surrealdb::RecordId::from((db::ACCOUNT, req.operator_wallet)),
id: surrealdb::RecordId::from((VM_NODE, req.node_pubkey.clone())),
operator: surrealdb::RecordId::from((ACCOUNT, req.operator_wallet)),
country: req.country,
region: req.region,
city: req.city,
@ -709,12 +710,6 @@ fn check_sig_from_parts(pubkey: &str, time: &str, msg: &str, sig: &str) -> Resul
Ok(())
}
const ADMIN_ACCOUNTS: &[&str] = &[
"x52w7jARC5erhWWK65VZmjdGXzBK6ZDgfv1A283d8XK",
"FHuecMbeC1PfjkW2JKyoicJAuiU7khgQT16QUB3Q1XdL",
"H21Shi4iE7vgfjWEQNvzmpmBMJSaiZ17PYUcdNoAoKNc",
];
fn check_admin_key<T>(req: &Request<T>) -> Result<(), Status> {
let pubkey = match req.metadata().get("pubkey") {
Some(p) => p.clone(),

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

@ -5,6 +5,8 @@ use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use crate::constants::OLD_BRAIN_DATA_PATH;
#[derive(Clone, Default, Serialize, Deserialize, Debug)]
pub struct AccountData {
pub balance: u64,
@ -124,7 +126,7 @@ pub struct BrainData {
impl BrainData {
pub fn load_from_disk() -> Result<Self, Box<dyn std::error::Error>> {
let content = std::fs::read_to_string("./saved_data.yaml")?;
let content = std::fs::read_to_string(OLD_BRAIN_DATA_PATH)?;
let data: Self = serde_yaml::from_str(&content)?;
Ok(data)
}