forked from ghe0/brain-to-surreal
Compare commits
6 Commits
a562265939
...
ab9ffa8a79
Author | SHA1 | Date | |
---|---|---|---|
ab9ffa8a79 | |||
a8cf515061 | |||
71cc0a8d82 | |||
f51e29fc4b | |||
751e81ce37 | |||
1cf091d801 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
/target
|
||||
tmp
|
||||
|
123
Cargo.lock
generated
123
Cargo.lock
generated
@ -482,6 +482,26 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bincode"
|
||||
version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "36eaf5d7b090263e8150820482d5d93cd964a81e4019913c972f4edcc6edb740"
|
||||
dependencies = [
|
||||
"bincode_derive",
|
||||
"serde",
|
||||
"unty",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bincode_derive"
|
||||
version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bf95709a440f45e986983918d0e8a1f30a9b1df04918fc828670606804ac3c09"
|
||||
dependencies = [
|
||||
"virtue",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bit-set"
|
||||
version = "0.5.3"
|
||||
@ -652,6 +672,8 @@ version = "1.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e3a13707ac958681c13b39b458c073d0d9bc8a22cb1b2f4c8e55eb72c13f362"
|
||||
dependencies = [
|
||||
"jobserver",
|
||||
"libc",
|
||||
"shlex",
|
||||
]
|
||||
|
||||
@ -978,15 +1000,17 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "detee-shared"
|
||||
version = "0.1.0"
|
||||
source = "git+ssh://git@gitea.detee.cloud/testnet/proto?branch=main#3024c00b8e1c93e70902793385b92bc0a8d1f26a"
|
||||
source = "git+ssh://git@gitea.detee.cloud/testnet/proto?branch=surreal_brain#fb38352e1b47837b14f32d8df5ae7f6b17202aae"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"bincode 2.0.1",
|
||||
"prost",
|
||||
"serde",
|
||||
"serde_yaml",
|
||||
"tar",
|
||||
"thiserror 2.0.12",
|
||||
"tonic",
|
||||
"tonic-build",
|
||||
"zstd",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1190,6 +1214,18 @@ version = "0.2.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d"
|
||||
|
||||
[[package]]
|
||||
name = "filetime"
|
||||
version = "0.2.25"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "35c0522e981e68cbfa8c3f978441a5f34b30b96e146b33cd3359176b50fe8586"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"libredox",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fixedbitset"
|
||||
version = "0.4.2"
|
||||
@ -1959,6 +1995,16 @@ dependencies = [
|
||||
"syn 2.0.100",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jobserver"
|
||||
version = "0.1.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38f262f097c174adebe41eb73d66ae9c06b2844fb0da69969647bbddd9b0538a"
|
||||
dependencies = [
|
||||
"getrandom 0.3.2",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.77"
|
||||
@ -2050,6 +2096,7 @@ checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -2606,6 +2653,12 @@ dependencies = [
|
||||
"spki",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pkg-config"
|
||||
version = "0.3.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
|
||||
|
||||
[[package]]
|
||||
name = "portable-atomic"
|
||||
version = "1.11.0"
|
||||
@ -3730,6 +3783,7 @@ dependencies = [
|
||||
"serde_json",
|
||||
"serde_yaml",
|
||||
"surrealdb",
|
||||
"thiserror 2.0.12",
|
||||
"tokio",
|
||||
"tokio-stream",
|
||||
"tonic",
|
||||
@ -3743,7 +3797,7 @@ checksum = "cd369a114a892aa1d0851d743f35c4c48672d6b0fb5aaf2b3e4dcacfac68a070"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"async-channel",
|
||||
"bincode",
|
||||
"bincode 1.3.3",
|
||||
"chrono",
|
||||
"dmp",
|
||||
"futures",
|
||||
@ -3792,7 +3846,7 @@ dependencies = [
|
||||
"async-graphql",
|
||||
"base64 0.21.7",
|
||||
"bcrypt",
|
||||
"bincode",
|
||||
"bincode 1.3.3",
|
||||
"blake3",
|
||||
"bytes",
|
||||
"castaway",
|
||||
@ -3925,6 +3979,17 @@ version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
|
||||
|
||||
[[package]]
|
||||
name = "tar"
|
||||
version = "0.4.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d863878d212c87a19c1a610eb53bb01fe12951c0501cf5a0d65f724914a667a"
|
||||
dependencies = [
|
||||
"filetime",
|
||||
"libc",
|
||||
"xattr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tempfile"
|
||||
version = "3.19.1"
|
||||
@ -4416,6 +4481,12 @@ version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
|
||||
|
||||
[[package]]
|
||||
name = "unty"
|
||||
version = "0.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6d49784317cd0d1ee7ec5c716dd598ec5b4483ea832a2dced265471cc0f690ae"
|
||||
|
||||
[[package]]
|
||||
name = "url"
|
||||
version = "2.5.4"
|
||||
@ -4481,6 +4552,12 @@ version = "0.9.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
||||
|
||||
[[package]]
|
||||
name = "virtue"
|
||||
version = "0.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "051eb1abcf10076295e815102942cc58f9d5e3b4560e46e53c21e8ff6f3af7b1"
|
||||
|
||||
[[package]]
|
||||
name = "walkdir"
|
||||
version = "2.5.0"
|
||||
@ -5008,6 +5085,16 @@ dependencies = [
|
||||
"tap",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "xattr"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0d65cbf2f12c15564212d48f4e3dfb87923d25d611f2aed18f4cb23f0413d89e"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"rustix",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "yoke"
|
||||
version = "0.7.5"
|
||||
@ -5120,3 +5207,31 @@ dependencies = [
|
||||
"quote",
|
||||
"syn 2.0.100",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zstd"
|
||||
version = "0.13.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e91ee311a569c327171651566e07972200e76fcfe2242a4fa446149a3881c08a"
|
||||
dependencies = [
|
||||
"zstd-safe",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zstd-safe"
|
||||
version = "7.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f49c4d5f0abb602a93fb8736af2a4f4dd9512e36f7f570d66e65ff867ed3b9d"
|
||||
dependencies = [
|
||||
"zstd-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zstd-sys"
|
||||
version = "2.0.15+zstd.1.5.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eb81183ddd97d0c74cedf1d50d85c8d08c1b8b68ee863bdee9e706eedba1a237"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"pkg-config",
|
||||
]
|
||||
|
@ -13,12 +13,13 @@ serde_yaml = "0.9.34"
|
||||
surrealdb = "2.2.2"
|
||||
tokio = { version = "1.44.2", features = ["macros", "rt-multi-thread"] }
|
||||
tonic = { version = "0.12", features = ["tls"] }
|
||||
detee-shared = { git = "ssh://git@gitea.detee.cloud/testnet/proto", branch = "main" }
|
||||
detee-shared = { git = "ssh://git@gitea.detee.cloud/testnet/proto", branch = "surreal_brain" }
|
||||
ed25519-dalek = "2.1.1"
|
||||
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,19 @@ use surrealdb::{
|
||||
RecordId, Surreal,
|
||||
};
|
||||
|
||||
use crate::old_brain;
|
||||
|
||||
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";
|
||||
|
||||
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,13 +28,13 @@ 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();
|
||||
let vm_contracts: Vec<VmContract> = old_data.into();
|
||||
let operators: Vec<OperatorRelation> = old_data.into();
|
||||
let app_contracts: Vec<AppContract> = old_data.into();
|
||||
let operators: Vec<Operator> = old_data.into();
|
||||
|
||||
init().await?;
|
||||
|
||||
@ -40,78 +49,177 @@ pub async fn migrate(old_data: &old_brain::BrainData) -> surrealdb::Result<()> {
|
||||
println!("Inserting app contracts...");
|
||||
let _: Vec<AppContract> = DB.insert("app_contract").relation(app_contracts).await?;
|
||||
println!("Inserting operators...");
|
||||
let _: Vec<Operator> = DB.insert("operator").relation(operators).await?;
|
||||
let _: Vec<OperatorRelation> = DB.insert(OPERATOR).relation(operators).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// I am not deleting this example cause I might need it later.
|
||||
//
|
||||
// async fn get_wallet_contracts() -> surrealdb::Result<Vec<Wallet>> {
|
||||
// let mut result = DB
|
||||
// .query("select *, ->contract.* from wallet:address1;")
|
||||
// .await?;
|
||||
// let wallets: Vec<Wallet> = result.take(0)?;
|
||||
// Ok(wallets)
|
||||
// }
|
||||
//
|
||||
// #[derive(Debug, Serialize, Deserialize)]
|
||||
// pub struct Wallet {
|
||||
// balance: u64,
|
||||
// id: RecordId,
|
||||
// #[serde(rename = "->contract", default)]
|
||||
// contracts: Vec<Contract>,
|
||||
// }
|
||||
|
||||
#[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,
|
||||
}
|
||||
|
||||
impl Account {
|
||||
pub async fn get(address: &str) -> Result<Self, Error> {
|
||||
let id = (ACCOUNT, address);
|
||||
let account: Option<Self> = DB.select(id).await?;
|
||||
let account = match account {
|
||||
Some(account) => account,
|
||||
None => {
|
||||
Self { id: id.into(), balance: 0, tmp_locked: 0, escrow: 0, email: String::new() }
|
||||
}
|
||||
};
|
||||
Ok(account)
|
||||
}
|
||||
|
||||
pub async fn airdrop(account: &str, tokens: u64) -> Result<(), Error> {
|
||||
let tokens = tokens.saturating_mul(1_000_000_000);
|
||||
let _ = DB
|
||||
.query(format!("upsert account:{account} SET balance = (balance || 0) + {tokens};"))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[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)]
|
||||
pub struct VmContract {
|
||||
id: RecordId,
|
||||
pub id: RecordId,
|
||||
#[serde(rename = "in")]
|
||||
admin: RecordId,
|
||||
pub admin: RecordId,
|
||||
#[serde(rename = "out")]
|
||||
vm_node: RecordId,
|
||||
state: String,
|
||||
hostname: String,
|
||||
mapped_ports: Vec<(u32, u32)>,
|
||||
public_ipv4: String,
|
||||
public_ipv6: String,
|
||||
disk_size_gb: u32,
|
||||
vcpus: u32,
|
||||
memory_mb: u32,
|
||||
dtrfs_sha: String,
|
||||
kernel_sha: String,
|
||||
created_at: Datetime,
|
||||
updated_at: Datetime,
|
||||
price_per_unit: u64,
|
||||
locked_nano: u64,
|
||||
collected_at: Datetime,
|
||||
pub vm_node: RecordId,
|
||||
pub state: String,
|
||||
pub hostname: String,
|
||||
pub mapped_ports: Vec<(u32, u32)>,
|
||||
pub public_ipv4: String,
|
||||
pub public_ipv6: String,
|
||||
pub disk_size_gb: u32,
|
||||
pub vcpus: u32,
|
||||
pub memory_mb: u32,
|
||||
pub dtrfs_sha: String,
|
||||
pub kernel_sha: String,
|
||||
pub created_at: Datetime,
|
||||
pub updated_at: Datetime,
|
||||
pub price_per_unit: u64,
|
||||
pub locked_nano: u64,
|
||||
pub collected_at: Datetime,
|
||||
}
|
||||
|
||||
impl VmContract {
|
||||
/// total hardware units of this VM
|
||||
fn total_units(&self) -> u64 {
|
||||
// TODO: Optimize this based on price of hardware.
|
||||
// I tried, but this can be done better.
|
||||
// Storage cost should also be based on tier
|
||||
(self.vcpus as u64 * 10)
|
||||
+ ((self.memory_mb + 256) as u64 / 200)
|
||||
+ (self.disk_size_gb as u64 / 10)
|
||||
+ (!self.public_ipv4.is_empty() as u64 * 10)
|
||||
}
|
||||
|
||||
/// Returns price per minute in nanoLP
|
||||
pub fn price_per_minute(&self) -> u64 {
|
||||
self.total_units() * self.price_per_unit
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct VmContractWithNode {
|
||||
pub id: RecordId,
|
||||
#[serde(rename = "in")]
|
||||
pub admin: RecordId,
|
||||
#[serde(rename = "out")]
|
||||
pub vm_node: VmNode,
|
||||
pub state: String,
|
||||
pub hostname: String,
|
||||
pub mapped_ports: Vec<(u32, u32)>,
|
||||
pub public_ipv4: String,
|
||||
pub public_ipv6: String,
|
||||
pub disk_size_gb: u32,
|
||||
pub vcpus: u32,
|
||||
pub memory_mb: u32,
|
||||
pub dtrfs_sha: String,
|
||||
pub kernel_sha: String,
|
||||
pub created_at: Datetime,
|
||||
pub updated_at: Datetime,
|
||||
pub price_per_unit: u64,
|
||||
pub locked_nano: u64,
|
||||
pub collected_at: Datetime,
|
||||
}
|
||||
|
||||
impl VmContractWithNode {
|
||||
pub async fn get_by_uuid(uuid: &str) -> Result<Option<Self>, Error> {
|
||||
let contract: Option<Self> =
|
||||
DB.query(format!("select * from {VM_CONTRACT}:{uuid} fetch out;")).await?.take(0)?;
|
||||
Ok(contract)
|
||||
}
|
||||
|
||||
pub async fn list_by_admin(admin: &str) -> Result<Vec<Self>, Error> {
|
||||
let mut result = DB
|
||||
.query(format!("select * from {VM_CONTRACT} where in = {ACCOUNT}:{admin} fetch out;"))
|
||||
.await?;
|
||||
let contracts: Vec<Self> = result.take(0)?;
|
||||
Ok(contracts)
|
||||
}
|
||||
|
||||
pub async fn list_by_operator(operator: &str) -> Result<Vec<Self>, Error> {
|
||||
let mut result = DB
|
||||
.query(format!(
|
||||
"select
|
||||
(select * from ->operator->vm_node<-vm_contract fetch out) as contracts
|
||||
from {ACCOUNT}:{operator};"
|
||||
))
|
||||
.await?;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Wrapper {
|
||||
contracts: Vec<VmContractWithNode>,
|
||||
}
|
||||
|
||||
let c: Option<Wrapper> = result.take(0)?;
|
||||
match c {
|
||||
Some(c) => Ok(c.contracts),
|
||||
None => Ok(Vec::new()),
|
||||
}
|
||||
}
|
||||
|
||||
/// total hardware units of this VM
|
||||
fn total_units(&self) -> u64 {
|
||||
// TODO: Optimize this based on price of hardware.
|
||||
// I tried, but this can be done better.
|
||||
// Storage cost should also be based on tier
|
||||
(self.vcpus as u64 * 10)
|
||||
+ ((self.memory_mb + 256) as u64 / 200)
|
||||
+ (self.disk_size_gb as u64 / 10)
|
||||
+ (!self.public_ipv4.is_empty() as u64 * 10)
|
||||
}
|
||||
|
||||
/// Returns price per minute in nanoLP
|
||||
pub fn price_per_minute(&self) -> u64 {
|
||||
self.total_units() * self.price_per_unit
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
@ -178,7 +286,6 @@ pub struct Kick {
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Report {
|
||||
id: RecordId,
|
||||
#[serde(rename = "in")]
|
||||
from_account: RecordId,
|
||||
#[serde(rename = "out")]
|
||||
@ -187,15 +294,30 @@ pub struct Report {
|
||||
reason: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Operator {
|
||||
#[serde(rename = "in")]
|
||||
account: RecordId,
|
||||
#[serde(rename = "out")]
|
||||
node: RecordId,
|
||||
impl Report {
|
||||
// TODO: test this functionality and remove this comment
|
||||
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 })
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Operator {
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct OperatorRelation {
|
||||
#[serde(rename = "in")]
|
||||
pub account: RecordId,
|
||||
#[serde(rename = "out")]
|
||||
pub node: RecordId,
|
||||
}
|
||||
|
||||
impl OperatorRelation {
|
||||
fn new(account: &str, vm_node: &str) -> Self {
|
||||
Self {
|
||||
account: RecordId::from(("account", account.to_string())),
|
||||
@ -204,6 +326,39 @@ impl Operator {
|
||||
}
|
||||
}
|
||||
|
||||
/// This is the operator obtained from the DB,
|
||||
/// however the relation is defined using OperatorRelation
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Operator {
|
||||
pub account: RecordId,
|
||||
pub app_nodes: u64,
|
||||
pub vm_nodes: u64,
|
||||
pub email: String,
|
||||
pub escrow: u64,
|
||||
pub reports: u64,
|
||||
}
|
||||
|
||||
impl Operator {
|
||||
pub async fn list() -> Result<Vec<Self>, Error> {
|
||||
let mut result = DB
|
||||
.query(format!(
|
||||
"select *,
|
||||
in as account,
|
||||
<-account.email[0] as email,
|
||||
<-account.escrow[0] as escrow,
|
||||
count(->vm_node) as vm_nodes,
|
||||
count(->app_node) as app_nodes,
|
||||
(select in from <-account->operator->vm_node<-report).len() +
|
||||
(select in from <-account->operator->app_node<-report).len()
|
||||
as reports
|
||||
from operator group by in;"
|
||||
))
|
||||
.await?;
|
||||
let operators: Vec<Self> = result.take(0)?;
|
||||
Ok(operators)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: delete all of these From implementation after migration 0 gets executed
|
||||
|
||||
impl From<&old_brain::BrainData> for Vec<VmNode> {
|
||||
@ -240,9 +395,9 @@ impl From<&old_brain::BrainData> for Vec<VmContract> {
|
||||
mapped_ports.push((*port, 8080 as u32));
|
||||
}
|
||||
contracts.push(VmContract {
|
||||
id: RecordId::from(("vm_contract", old_c.uuid.replace("-", ""))),
|
||||
admin: RecordId::from(("account", old_c.admin_pubkey.clone())),
|
||||
vm_node: RecordId::from(("vm_node", old_c.node_pubkey.clone())),
|
||||
id: RecordId::from((VM_CONTRACT, old_c.uuid.replace("-", ""))),
|
||||
admin: RecordId::from((ACCOUNT, old_c.admin_pubkey.clone())),
|
||||
vm_node: RecordId::from((VM_NODE, old_c.node_pubkey.clone())),
|
||||
state: "active".to_string(),
|
||||
hostname: old_c.hostname.clone(),
|
||||
mapped_ports,
|
||||
@ -350,15 +505,15 @@ impl From<&old_brain::BrainData> for Vec<Account> {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&old_brain::BrainData> for Vec<Operator> {
|
||||
impl From<&old_brain::BrainData> for Vec<OperatorRelation> {
|
||||
fn from(old_data: &old_brain::BrainData) -> Self {
|
||||
let mut operator_entries = Vec::new();
|
||||
for operator in old_data.operators.clone() {
|
||||
for vm_node in operator.1.vm_nodes.iter() {
|
||||
operator_entries.push(Operator::new(&operator.0, vm_node));
|
||||
operator_entries.push(OperatorRelation::new(&operator.0, vm_node));
|
||||
}
|
||||
for app_node in operator.1.app_nodes.iter() {
|
||||
operator_entries.push(Operator::new(&operator.0, app_node));
|
||||
operator_entries.push(OperatorRelation::new(&operator.0, app_node));
|
||||
}
|
||||
}
|
||||
operator_entries
|
226
src/grpc.rs
226
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},
|
||||
@ -12,6 +13,8 @@ use detee_shared::{
|
||||
|
||||
use log::info;
|
||||
use std::pin::Pin;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio_stream::wrappers::ReceiverStream;
|
||||
// use tokio::sync::mpsc;
|
||||
// use tokio_stream::{wrappers::ReceiverStream, Stream};
|
||||
use tokio_stream::Stream;
|
||||
@ -19,6 +22,68 @@ 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::VmContractWithNode> for VmContract {
|
||||
fn from(db_c: db::VmContractWithNode) -> Self {
|
||||
let mut exposed_ports = Vec::new();
|
||||
for port in db_c.mapped_ports.iter() {
|
||||
exposed_ports.push(port.0);
|
||||
}
|
||||
VmContract {
|
||||
uuid: db_c.id.key().to_string(),
|
||||
hostname: db_c.hostname.clone(),
|
||||
admin_pubkey: db_c.admin.key().to_string(),
|
||||
node_pubkey: db_c.vm_node.id.key().to_string(),
|
||||
node_ip: db_c.vm_node.ip.clone(),
|
||||
location: format!(
|
||||
"{}, {}, {}",
|
||||
db_c.vm_node.city, db_c.vm_node.region, db_c.vm_node.country
|
||||
),
|
||||
memory_mb: db_c.memory_mb,
|
||||
vcpus: db_c.vcpus,
|
||||
disk_size_gb: db_c.disk_size_gb,
|
||||
mapped_ports: db_c
|
||||
.mapped_ports
|
||||
.iter()
|
||||
.map(|(h, g)| MappedPort { host_port: *h, guest_port: *g })
|
||||
.collect(),
|
||||
vm_public_ipv6: db_c.public_ipv6.clone(),
|
||||
vm_public_ipv4: db_c.public_ipv4.clone(),
|
||||
locked_nano: db_c.locked_nano,
|
||||
dtrfs_sha: db_c.dtrfs_sha.clone(),
|
||||
kernel_sha: db_c.kernel_sha.clone(),
|
||||
nano_per_minute: db_c.price_per_minute(),
|
||||
created_at: db_c.created_at.to_rfc3339(),
|
||||
updated_at: db_c.updated_at.to_rfc3339(),
|
||||
collected_at: db_c.collected_at.to_rfc3339(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<db::Error> for tonic::Status {
|
||||
fn from(e: db::Error) -> Self {
|
||||
Self::internal(format!("Internal error: {e}"))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<db::Operator> for ListOperatorsResp {
|
||||
fn from(db_o: db::Operator) -> Self {
|
||||
ListOperatorsResp {
|
||||
pubkey: db_o.account.key().to_string(),
|
||||
escrow: db_o.escrow,
|
||||
email: db_o.email,
|
||||
app_nodes: db_o.app_nodes,
|
||||
vm_nodes: db_o.vm_nodes,
|
||||
reports: db_o.reports,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tonic::async_trait]
|
||||
impl BrainGeneralCli for BrainGeneralCliMock {
|
||||
type ListAccountsStream = Pin<Box<dyn Stream<Item = Result<Account, Status>> + Send>>;
|
||||
@ -29,47 +94,42 @@ 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::get(&req.pubkey).await?.into()))
|
||||
}
|
||||
|
||||
async fn report_node(&self, req: Request<ReportNodeReq>) -> Result<Response<Empty>, Status> {
|
||||
let _req = check_sig_from_req(req)?;
|
||||
todo!();
|
||||
// match self.data.find_any_contract_by_uuid(&req.contract) {
|
||||
// Ok((Some(vm_contract), _))
|
||||
// if vm_contract.admin_pubkey == req.admin_pubkey
|
||||
// && vm_contract.node_pubkey == req.node_pubkey =>
|
||||
// {
|
||||
// ()
|
||||
// }
|
||||
// Ok((_, Some(app_contract)))
|
||||
// if app_contract.admin_pubkey == req.admin_pubkey
|
||||
// && app_contract.node_pubkey == req.node_pubkey =>
|
||||
// {
|
||||
// ()
|
||||
// }
|
||||
// _ => return Err(Status::unauthenticated("No contract found by this ID.")),
|
||||
// };
|
||||
// self.data.report_any_node(req.admin_pubkey, &req.node_pubkey, req.reason);
|
||||
// Ok(Response::new(Empty {}))
|
||||
let req = check_sig_from_req(req)?;
|
||||
let (account, node) = match db::VmContractWithNode::get_by_uuid(&req.contract).await? {
|
||||
Some(vm_contract)
|
||||
if vm_contract.admin.key().to_string() == req.admin_pubkey
|
||||
&& vm_contract.vm_node.id.key().to_string() == req.node_pubkey =>
|
||||
{
|
||||
(vm_contract.admin, vm_contract.vm_node.id)
|
||||
}
|
||||
_ => {
|
||||
// TODO: Hey, Noor! Please add app contract here.
|
||||
return Err(Status::unauthenticated("No contract found by this ID."));
|
||||
}
|
||||
};
|
||||
db::Report::create(account, node, req.reason).await?;
|
||||
Ok(Response::new(Empty {}))
|
||||
}
|
||||
|
||||
async fn list_operators(
|
||||
&self,
|
||||
_req: Request<Empty>,
|
||||
req: Request<Empty>,
|
||||
) -> Result<Response<Self::ListOperatorsStream>, Status> {
|
||||
todo!();
|
||||
// let _ = check_sig_from_req(req)?;
|
||||
// let operators = self.data.list_operators();
|
||||
// let (tx, rx) = mpsc::channel(6);
|
||||
// tokio::spawn(async move {
|
||||
// for op in operators {
|
||||
// let _ = tx.send(Ok(op.into())).await;
|
||||
// }
|
||||
// });
|
||||
// let output_stream = ReceiverStream::new(rx);
|
||||
// Ok(Response::new(Box::pin(output_stream) as Self::ListOperatorsStream))
|
||||
let _ = check_sig_from_req(req)?;
|
||||
let operators = db::Operator::list().await?;
|
||||
let (tx, rx) = mpsc::channel(6);
|
||||
tokio::spawn(async move {
|
||||
for op in operators {
|
||||
let _ = tx.send(Ok(op.into())).await;
|
||||
}
|
||||
});
|
||||
let output_stream = ReceiverStream::new(rx);
|
||||
Ok(Response::new(Box::pin(output_stream) as Self::ListOperatorsStream))
|
||||
}
|
||||
|
||||
async fn inspect_operator(
|
||||
@ -114,12 +174,11 @@ impl BrainGeneralCli for BrainGeneralCliMock {
|
||||
|
||||
// admin commands
|
||||
|
||||
async fn airdrop(&self, _req: Request<AirdropReq>) -> Result<Response<Empty>, Status> {
|
||||
todo!();
|
||||
// check_admin_key(&req)?;
|
||||
// let req = check_sig_from_req(req)?;
|
||||
// self.data.give_airdrop(&req.pubkey, req.tokens);
|
||||
// Ok(Response::new(Empty {}))
|
||||
async fn airdrop(&self, req: Request<AirdropReq>) -> Result<Response<Empty>, Status> {
|
||||
check_admin_key(&req)?;
|
||||
let req = check_sig_from_req(req)?;
|
||||
db::Account::airdrop(&req.pubkey, req.tokens).await?;
|
||||
Ok(Response::new(Empty {}))
|
||||
}
|
||||
|
||||
async fn slash(&self, _req: Request<SlashReq>) -> Result<Response<Empty>, Status> {
|
||||
@ -268,32 +327,32 @@ impl BrainVmCli for BrainVmCliMock {
|
||||
"CLI {} requested ListVMVmContractsStream. As operator: {}",
|
||||
req.wallet, req.as_operator
|
||||
);
|
||||
todo!();
|
||||
// let mut contracts = Vec::new();
|
||||
// if !req.uuid.is_empty() {
|
||||
// if let Ok(specific_contract) = self.data.find_contract_by_uuid(&req.uuid) {
|
||||
// if specific_contract.admin_pubkey == req.wallet {
|
||||
// contracts.push(specific_contract);
|
||||
// }
|
||||
// // TODO: allow operator to inspect contracts
|
||||
// }
|
||||
// } else {
|
||||
// if req.as_operator {
|
||||
// contracts.append(&mut self.data.find_vm_contracts_by_operator(&req.wallet));
|
||||
// } else {
|
||||
// contracts.append(&mut self.data.find_vm_contracts_by_admin(&req.wallet));
|
||||
// }
|
||||
// }
|
||||
// let (tx, rx) = mpsc::channel(6);
|
||||
// tokio::spawn(async move {
|
||||
// for contract in contracts {
|
||||
// let _ = tx.send(Ok(contract.into())).await;
|
||||
// }
|
||||
// });
|
||||
// let output_stream = ReceiverStream::new(rx);
|
||||
// Ok(Response::new(
|
||||
// Box::pin(output_stream) as Self::ListVmContractsStream
|
||||
// ))
|
||||
let mut contracts = Vec::new();
|
||||
if !req.uuid.is_empty() {
|
||||
if let Some(specific_contract) = db::VmContractWithNode::get_by_uuid(&req.uuid).await? {
|
||||
if specific_contract.admin.key().to_string() == req.wallet {
|
||||
contracts.push(specific_contract.into());
|
||||
}
|
||||
// TODO: allow operator to inspect contracts
|
||||
}
|
||||
} else {
|
||||
if req.as_operator {
|
||||
contracts.append(
|
||||
&mut db::VmContractWithNode::list_by_operator(&req.wallet).await?.into(),
|
||||
);
|
||||
} else {
|
||||
contracts
|
||||
.append(&mut db::VmContractWithNode::list_by_admin(&req.wallet).await?.into());
|
||||
}
|
||||
}
|
||||
let (tx, rx) = mpsc::channel(6);
|
||||
tokio::spawn(async move {
|
||||
for contract in contracts {
|
||||
let _ = tx.send(Ok(contract.into())).await;
|
||||
}
|
||||
});
|
||||
let output_stream = ReceiverStream::new(rx);
|
||||
Ok(Response::new(Box::pin(output_stream) as Self::ListVmContractsStream))
|
||||
}
|
||||
|
||||
async fn list_vm_nodes(
|
||||
@ -356,13 +415,26 @@ macro_rules! impl_pubkey_getter {
|
||||
impl_pubkey_getter!(Pubkey, pubkey);
|
||||
impl_pubkey_getter!(NewVmReq, admin_pubkey);
|
||||
impl_pubkey_getter!(DeleteVmReq, admin_pubkey);
|
||||
impl_pubkey_getter!(ReportNodeReq, admin_pubkey);
|
||||
impl_pubkey_getter!(UpdateVmReq, admin_pubkey);
|
||||
impl_pubkey_getter!(ExtendVmReq, admin_pubkey);
|
||||
impl_pubkey_getter!(ReportNodeReq, admin_pubkey);
|
||||
impl_pubkey_getter!(ListVmContractsReq, wallet);
|
||||
impl_pubkey_getter!(RegisterVmNodeReq, node_pubkey);
|
||||
impl_pubkey_getter!(RegOperatorReq, pubkey);
|
||||
impl_pubkey_getter!(KickReq, operator_wallet);
|
||||
impl_pubkey_getter!(BanUserReq, operator_wallet);
|
||||
|
||||
impl_pubkey_getter!(VmNodeFilters);
|
||||
impl_pubkey_getter!(Empty);
|
||||
impl_pubkey_getter!(AirdropReq);
|
||||
impl_pubkey_getter!(SlashReq);
|
||||
|
||||
// impl_pubkey_getter!(NewAppReq, admin_pubkey);
|
||||
// impl_pubkey_getter!(DelAppReq, admin_pubkey);
|
||||
// impl_pubkey_getter!(ListAppContractsReq, admin_pubkey);
|
||||
//
|
||||
// impl_pubkey_getter!(RegisterAppNodeReq);
|
||||
// impl_pubkey_getter!(AppNodeFilters);
|
||||
|
||||
fn check_sig_from_req<T: std::fmt::Debug + PubkeyGetter>(req: Request<T>) -> Result<T, Status> {
|
||||
let time = match req.metadata().get("timestamp") {
|
||||
@ -426,3 +498,25 @@ 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(),
|
||||
None => return Err(Status::unauthenticated("pubkey not found in metadata.")),
|
||||
};
|
||||
let pubkey = pubkey
|
||||
.to_str()
|
||||
.map_err(|_| Status::unauthenticated("could not parse pubkey metadata to str"))?;
|
||||
|
||||
if !ADMIN_ACCOUNTS.contains(&pubkey) {
|
||||
return Err(Status::unauthenticated("This operation is reserved to admin accounts"));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -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