Compare commits
1 Commits
48f38d8c99
...
751e05a308
Author | SHA1 | Date | |
---|---|---|---|
751e05a308 |
31
Cargo.lock
generated
31
Cargo.lock
generated
@ -244,7 +244,7 @@ dependencies = [
|
||||
"prost-types",
|
||||
"reqwest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serde_yaml",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
"tokio-stream",
|
||||
@ -305,6 +305,7 @@ dependencies = [
|
||||
"iana-time-zone",
|
||||
"js-sys",
|
||||
"num-traits",
|
||||
"serde",
|
||||
"wasm-bindgen",
|
||||
"windows-targets",
|
||||
]
|
||||
@ -401,6 +402,7 @@ dependencies = [
|
||||
"lock_api",
|
||||
"once_cell",
|
||||
"parking_lot_core",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1607,18 +1609,18 @@ checksum = "f79dfe2d285b0488816f30e700a7438c5a73d816b5b7d3ac72fbc48b0d185e03"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.216"
|
||||
version = "1.0.217"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e"
|
||||
checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.216"
|
||||
version = "1.0.217"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e"
|
||||
checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@ -1649,6 +1651,19 @@ dependencies = [
|
||||
"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]]
|
||||
name = "sha2"
|
||||
version = "0.10.8"
|
||||
@ -2049,6 +2064,12 @@ version = "1.0.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
|
||||
|
||||
[[package]]
|
||||
name = "unsafe-libyaml"
|
||||
version = "0.2.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861"
|
||||
|
||||
[[package]]
|
||||
name = "untrusted"
|
||||
version = "0.9.0"
|
||||
|
@ -5,16 +5,16 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
bs58 = "0.5.1"
|
||||
chrono = "0.4.39"
|
||||
dashmap = "6.1.0"
|
||||
chrono = { version = "0.4.39", features = ["serde"] }
|
||||
dashmap = { version = "6.1.0", features = ["serde"] }
|
||||
ed25519-dalek = "2.1.1"
|
||||
env_logger = "0.11.6"
|
||||
log = "0.4.22"
|
||||
prost = "0.13.4"
|
||||
prost-types = "0.13.4"
|
||||
reqwest = "0.12.10"
|
||||
serde = { version = "1.0.216", features = ["derive"] }
|
||||
serde_json = "1.0.134"
|
||||
serde = { version = "1.0.217", features = ["derive"] }
|
||||
serde_yaml = "0.9.34"
|
||||
thiserror = "2.0.11"
|
||||
tokio = { version = "1.42.0", features = ["macros", "rt-multi-thread"] }
|
||||
tokio-stream = "0.1.17"
|
||||
|
41
src/data.rs
41
src/data.rs
@ -2,12 +2,19 @@ use crate::grpc::snp_proto::{self as grpc};
|
||||
use chrono::Utc;
|
||||
use dashmap::DashMap;
|
||||
use log::{debug, info, warn};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::str::FromStr;
|
||||
use std::sync::RwLock;
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
fs::File,
|
||||
io::Write,
|
||||
};
|
||||
use tokio::sync::mpsc::Sender;
|
||||
use tokio::sync::oneshot::Sender as OneshotSender;
|
||||
|
||||
const DATA_PATH: &str = "/etc/detee/brain-mock/saved_data.yaml";
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("We do not allow locking of more than 100000 LP.")]
|
||||
@ -24,7 +31,7 @@ pub enum Error {
|
||||
AccessDenied,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
#[derive(Clone, Default, Serialize, Deserialize)]
|
||||
pub struct AccountData {
|
||||
pub balance: u64,
|
||||
pub tmp_locked: u64,
|
||||
@ -35,7 +42,7 @@ pub struct AccountData {
|
||||
pub banned_by: HashSet<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
#[derive(Clone, Default, Serialize, Deserialize)]
|
||||
pub struct OperatorData {
|
||||
pub escrow: u64,
|
||||
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 public_key: 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 uuid: String,
|
||||
pub hostname: String,
|
||||
@ -155,20 +162,40 @@ impl Into<grpc::VmContract> for VmContract {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Default, Serialize, Deserialize)]
|
||||
pub struct BrainData {
|
||||
// amount of nanoLP in each account
|
||||
accounts: DashMap<String, AccountData>,
|
||||
operators: DashMap<String, OperatorData>,
|
||||
vm_nodes: RwLock<Vec<VmNode>>,
|
||||
vm_contracts: RwLock<Vec<VmContract>>,
|
||||
#[serde(skip_serializing, skip_deserializing)]
|
||||
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>)>,
|
||||
#[serde(skip_serializing, skip_deserializing)]
|
||||
daemon_tx: DashMap<String, Sender<grpc::BrainVmMessage>>,
|
||||
}
|
||||
|
||||
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 {
|
||||
match Self::load_from_disk() {
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
warn!("Could not data {DATA_PATH} due to error: {e:?}");
|
||||
info!("Creating new instance of brain.");
|
||||
Self {
|
||||
accounts: DashMap::new(),
|
||||
operators: DashMap::new(),
|
||||
@ -179,6 +206,8 @@ impl BrainData {
|
||||
daemon_tx: DashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_balance(&self, account: &str) -> AccountData {
|
||||
if let Some(account) = self.accounts.get(account) {
|
||||
|
@ -21,6 +21,9 @@ async fn main() {
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
|
||||
data_clone.vm_nodes_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();
|
||||
|
Loading…
Reference in New Issue
Block a user