Compare commits

..

1 Commits

Author SHA1 Message Date
751e05a308
add support for operators 2025-02-16 02:56:11 +02:00
4 changed files with 76 additions and 23 deletions

31
Cargo.lock generated

@ -244,7 +244,7 @@ dependencies = [
"prost-types", "prost-types",
"reqwest", "reqwest",
"serde", "serde",
"serde_json", "serde_yaml",
"thiserror", "thiserror",
"tokio", "tokio",
"tokio-stream", "tokio-stream",
@ -305,6 +305,7 @@ dependencies = [
"iana-time-zone", "iana-time-zone",
"js-sys", "js-sys",
"num-traits", "num-traits",
"serde",
"wasm-bindgen", "wasm-bindgen",
"windows-targets", "windows-targets",
] ]
@ -401,6 +402,7 @@ dependencies = [
"lock_api", "lock_api",
"once_cell", "once_cell",
"parking_lot_core", "parking_lot_core",
"serde",
] ]
[[package]] [[package]]
@ -1607,18 +1609,18 @@ checksum = "f79dfe2d285b0488816f30e700a7438c5a73d816b5b7d3ac72fbc48b0d185e03"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.216" version = "1.0.217"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e" checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.216" version = "1.0.217"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e" checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1649,6 +1651,19 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_yaml"
version = "0.9.34+deprecated"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47"
dependencies = [
"indexmap 2.7.0",
"itoa",
"ryu",
"serde",
"unsafe-libyaml",
]
[[package]] [[package]]
name = "sha2" name = "sha2"
version = "0.10.8" version = "0.10.8"
@ -2049,6 +2064,12 @@ version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
[[package]]
name = "unsafe-libyaml"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861"
[[package]] [[package]]
name = "untrusted" name = "untrusted"
version = "0.9.0" version = "0.9.0"

@ -5,16 +5,16 @@ edition = "2021"
[dependencies] [dependencies]
bs58 = "0.5.1" bs58 = "0.5.1"
chrono = "0.4.39" chrono = { version = "0.4.39", features = ["serde"] }
dashmap = "6.1.0" dashmap = { version = "6.1.0", features = ["serde"] }
ed25519-dalek = "2.1.1" ed25519-dalek = "2.1.1"
env_logger = "0.11.6" env_logger = "0.11.6"
log = "0.4.22" log = "0.4.22"
prost = "0.13.4" prost = "0.13.4"
prost-types = "0.13.4" prost-types = "0.13.4"
reqwest = "0.12.10" reqwest = "0.12.10"
serde = { version = "1.0.216", features = ["derive"] } serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.134" serde_yaml = "0.9.34"
thiserror = "2.0.11" thiserror = "2.0.11"
tokio = { version = "1.42.0", features = ["macros", "rt-multi-thread"] } tokio = { version = "1.42.0", features = ["macros", "rt-multi-thread"] }
tokio-stream = "0.1.17" tokio-stream = "0.1.17"

@ -2,12 +2,19 @@ use crate::grpc::snp_proto::{self as grpc};
use chrono::Utc; use chrono::Utc;
use dashmap::DashMap; use dashmap::DashMap;
use log::{debug, info, warn}; use log::{debug, info, warn};
use std::collections::{HashMap, HashSet}; use serde::{Deserialize, Serialize};
use std::str::FromStr; use std::str::FromStr;
use std::sync::RwLock; use std::sync::RwLock;
use std::{
collections::{HashMap, HashSet},
fs::File,
io::Write,
};
use tokio::sync::mpsc::Sender; use tokio::sync::mpsc::Sender;
use tokio::sync::oneshot::Sender as OneshotSender; use tokio::sync::oneshot::Sender as OneshotSender;
const DATA_PATH: &str = "/etc/detee/brain-mock/saved_data.yaml";
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
pub enum Error { pub enum Error {
#[error("We do not allow locking of more than 100000 LP.")] #[error("We do not allow locking of more than 100000 LP.")]
@ -24,7 +31,7 @@ pub enum Error {
AccessDenied, AccessDenied,
} }
#[derive(Clone, Default)] #[derive(Clone, Default, Serialize, Deserialize)]
pub struct AccountData { pub struct AccountData {
pub balance: u64, pub balance: u64,
pub tmp_locked: u64, pub tmp_locked: u64,
@ -35,7 +42,7 @@ pub struct AccountData {
pub banned_by: HashSet<String>, pub banned_by: HashSet<String>,
} }
#[derive(Clone, Default)] #[derive(Clone, Default, Serialize, Deserialize)]
pub struct OperatorData { pub struct OperatorData {
pub escrow: u64, pub escrow: u64,
pub email: String, pub email: String,
@ -52,7 +59,7 @@ impl From<AccountData> for grpc::AccountBalance {
} }
} }
#[derive(Eq, PartialEq, Clone, Debug, Default)] #[derive(Eq, PartialEq, Clone, Debug, Default, Serialize, Deserialize)]
pub struct VmNode { pub struct VmNode {
pub public_key: String, pub public_key: String,
pub operator_wallet: String, pub operator_wallet: String,
@ -89,7 +96,7 @@ impl Into<grpc::VmNodeListResp> for VmNode {
} }
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct VmContract { pub struct VmContract {
pub uuid: String, pub uuid: String,
pub hostname: String, pub hostname: String,
@ -155,28 +162,50 @@ impl Into<grpc::VmContract> for VmContract {
} }
} }
#[derive(Default)] #[derive(Default, Serialize, Deserialize)]
pub struct BrainData { pub struct BrainData {
// amount of nanoLP in each account // amount of nanoLP in each account
accounts: DashMap<String, AccountData>, accounts: DashMap<String, AccountData>,
operators: DashMap<String, OperatorData>, operators: DashMap<String, OperatorData>,
vm_nodes: RwLock<Vec<VmNode>>, vm_nodes: RwLock<Vec<VmNode>>,
vm_contracts: RwLock<Vec<VmContract>>, vm_contracts: RwLock<Vec<VmContract>>,
#[serde(skip_serializing, skip_deserializing)]
tmp_newvm_reqs: DashMap<String, (grpc::NewVmReq, OneshotSender<grpc::NewVmResp>)>, tmp_newvm_reqs: DashMap<String, (grpc::NewVmReq, OneshotSender<grpc::NewVmResp>)>,
#[serde(skip_serializing, skip_deserializing)]
tmp_updatevm_reqs: DashMap<String, (grpc::UpdateVmReq, OneshotSender<grpc::UpdateVmResp>)>, tmp_updatevm_reqs: DashMap<String, (grpc::UpdateVmReq, OneshotSender<grpc::UpdateVmResp>)>,
#[serde(skip_serializing, skip_deserializing)]
daemon_tx: DashMap<String, Sender<grpc::BrainVmMessage>>, daemon_tx: DashMap<String, Sender<grpc::BrainVmMessage>>,
} }
impl BrainData { impl BrainData {
pub fn save_to_disk(&self) -> Result<(), Box<dyn std::error::Error>> {
let mut file = File::create(DATA_PATH)?;
file.write_all(serde_yaml::to_string(self)?.as_bytes())?;
Ok(())
}
fn load_from_disk() -> Result<Self, Box<dyn std::error::Error>> {
let content = std::fs::read_to_string(DATA_PATH)?;
let data: Self = serde_yaml::from_str(&content)?;
Ok(data)
}
pub fn new() -> Self { pub fn new() -> Self {
Self { match Self::load_from_disk() {
accounts: DashMap::new(), Ok(data) => data,
operators: DashMap::new(), Err(e) => {
vm_nodes: RwLock::new(Vec::new()), warn!("Could not data {DATA_PATH} due to error: {e:?}");
vm_contracts: RwLock::new(Vec::new()), info!("Creating new instance of brain.");
tmp_newvm_reqs: DashMap::new(), Self {
tmp_updatevm_reqs: DashMap::new(), accounts: DashMap::new(),
daemon_tx: DashMap::new(), operators: DashMap::new(),
vm_nodes: RwLock::new(Vec::new()),
vm_contracts: RwLock::new(Vec::new()),
tmp_newvm_reqs: DashMap::new(),
tmp_updatevm_reqs: DashMap::new(),
daemon_tx: DashMap::new(),
}
}
} }
} }

@ -21,6 +21,9 @@ async fn main() {
tokio::time::sleep(tokio::time::Duration::from_secs(60)).await; tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
data_clone.vm_nodes_cron().await; data_clone.vm_nodes_cron().await;
data_clone.vm_contracts_cron().await; data_clone.vm_contracts_cron().await;
if let Err(e) = data_clone.save_to_disk() {
log::error!("Could not save data to disk due to error: {e}")
}
} }
}); });
let addr = "0.0.0.0:31337".parse().unwrap(); let addr = "0.0.0.0:31337".parse().unwrap();