centralize configuration constants
update database initialization for test env
This commit is contained in:
Noor 2025-04-24 15:53:38 +05:30
parent a8cf515061
commit 8d6764d796
Signed by: noormohammedb
GPG Key ID: D83EFB8B3B967146
6 changed files with 52 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::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();
db::init(DB_ADDRESS, DB_NS, DB_NAME).await.unwrap();
let addr = BRAIN_GRPC_ADDR.parse().unwrap();
let snp_cli_server = BrainVmCliServer::new(BrainVmCliMock {});
let general_service_server = BrainGeneralCliServer::new(BrainGeneralCliMock {});
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";

@ -8,11 +8,11 @@ use surrealdb::{
RecordId, Surreal,
};
use crate::constants::{
ACCOUNT, DB_ADDRESS, DB_NAME, DB_NS, DB_PASS, DB_USER, OPERATOR, VM_CONTRACT, VM_NODE,
};
static DB: LazyLock<Surreal<Client>> = LazyLock::new(Surreal::init);
const ACCOUNT: &str = "account";
const OPERATOR: &str = "operator";
const VM_CONTRACT: &str = "vm_contract";
const VM_NODE: &str = "vm_node";
#[derive(thiserror::Error, Debug)]
pub enum Error {
@ -20,11 +20,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(())
}
@ -35,7 +35,7 @@ pub async fn migration0(old_data: &old_brain::BrainData) -> surrealdb::Result<()
let vm_contracts: Vec<VmContract> = old_data.into();
let operators: Vec<OperatorRelation> = old_data.into();
init().await?;
init(DB_ADDRESS, DB_NS, DB_NAME).await?;
println!("Inserting accounts...");
let _: Vec<Account> = DB.insert(()).content(accounts).await?;
@ -293,7 +293,11 @@ pub struct Report {
impl Report {
// TODO: test this functionality and remove this comment
pub async fn create(from_account: RecordId, to_node: RecordId, reason: String) -> Result<(), Error> {
pub async fn create(
from_account: RecordId,
to_node: RecordId,
reason: String,
) -> Result<(), Error> {
let _: Vec<Self> = DB
.insert("report")
.relation(Report { from_account, to_node, created_at: Datetime::default(), reason })

@ -1,4 +1,5 @@
#![allow(dead_code)]
use crate::constants::ADMIN_ACCOUNTS;
use crate::db;
use detee_shared::app_proto::AppContract;
use detee_shared::{
@ -499,12 +500,6 @@ fn check_sig_from_req<T: std::fmt::Debug + PubkeyGetter>(req: Request<T>) -> Res
Ok(req)
}
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)
}