From 8e8e1d1a99a1dd99b8d2c22c8c833f6b09b62138 Mon Sep 17 00:00:00 2001 From: ghe0 Date: Fri, 14 Feb 2025 03:18:08 +0200 Subject: [PATCH] importing new proto file --- Cargo.toml | 6 ++++ scripts/install_daemon.sh | 26 ++++++++++++++ src/global.rs | 18 +++++++++- src/grpc.rs | 2 +- src/main.rs | 32 +++++++++++++++-- src/state.rs | 21 ++--------- vm.proto | 74 +++++++++++++++++++++++++++++++++------ 7 files changed, 146 insertions(+), 33 deletions(-) create mode 100755 scripts/install_daemon.sh diff --git a/Cargo.toml b/Cargo.toml index 14a366e..a06f1d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,3 +26,9 @@ chrono = "0.4.39" [build-dependencies] tonic-build = "0.12" + +[profile.release] +opt-level = "z" +lto = true +codegen-units = 1 +strip = true diff --git a/scripts/install_daemon.sh b/scripts/install_daemon.sh new file mode 100755 index 0000000..a028240 --- /dev/null +++ b/scripts/install_daemon.sh @@ -0,0 +1,26 @@ +#!/bin/bash +set -e +echo "Creating folders..." +mkdir -p /var/lib/detee/boot/ +mkdir -p /etc/detee/daemon/vms/ +mkdir -p /usr/local/bin/detee/ +mkdir -p /opt/detee_vms/ +echo "Installing qemu-system-x86..." +pacman -S qemu-system-x86 qemu-img --noconfirm + +echo "Downloading detee-snp-daemon, systemd unit file and config..." +wget -O /etc/detee/daemon/sample_config.yaml https://registry.detee.ltd/daemon/config.yaml +wget -O /usr/local/bin/detee-snp-daemon https://registry.detee.ltd/daemon/detee-snp-daemon +chmod +x /usr/local/bin/detee-snp-daemon +wget -O /usr/local/bin/detee/start_qemu_vm.sh https://registry.detee.ltd/daemon/start_qemu_vm.sh +chmod +x /usr/local/bin/detee/start_qemu_vm.sh +wget -O /etc/systemd/system/detee-snp-daemon.service https://registry.detee.ltd/daemon/detee-snp-daemon.service + +echo "Take a look at /etc/detee/daemon/sample_config.yaml" +echo "Modify config based on your setup and save it to /etc/detee/daemon/config.yaml" +echo "Press enter when done (this will attempt to start the daemon)" +read my_var + +echo "Starting detee-snp-daemon..." +systemctl daemon-reload +systemctl start detee-snp-daemon.service diff --git a/src/global.rs b/src/global.rs index 67e0994..bec2b0e 100644 --- a/src/global.rs +++ b/src/global.rs @@ -2,7 +2,8 @@ use anyhow::Result; use ed25519_dalek::SigningKey; use lazy_static::lazy_static; use log::{info, warn}; -use std::{fs::File, io::Write}; +use sha2::{Digest, Sha256}; +use std::{fs::File, io::Read, io::Write}; pub(crate) const VM_BOOT_DIR: &str = "/var/lib/detee/boot/"; pub(crate) const USED_RESOURCES: &str = "/etc/detee/daemon/used_resources.yaml"; @@ -74,3 +75,18 @@ fn get_ip_info() -> anyhow::Result { info!("Got the following data from ipinfo.io: {body}"); Ok(serde_json::de::from_str(&body)?) } + +pub fn compute_sha256>(path: P) -> Result { + let mut file = File::open(path)?; + let mut hasher = Sha256::new(); + let mut buffer = [0u8; 8192]; + loop { + let bytes_read = file.read(&mut buffer)?; + if bytes_read == 0 { + break; + } + hasher.update(&buffer[..bytes_read]); + } + let result = hasher.finalize(); + Ok(format!("{:x}", result)) +} diff --git a/src/grpc.rs b/src/grpc.rs index 71cd4fa..ef08779 100644 --- a/src/grpc.rs +++ b/src/grpc.rs @@ -40,7 +40,7 @@ pub async fn register_node(config: &crate::config::Config) -> Result Result<()> { + use reqwest::blocking::get; + use std::os::unix::fs::PermissionsExt; + const TMP_DAEMON: &str = "/usr/local/bin/detee/new-daemon"; + const BINARY: &str = "/usr/local/bin/detee-snp-daemon"; + let response = get("https://registry.detee.ltd/daemon/detee-snp-daemon")?; + if !response.status().is_success() { + return Err(anyhow!("Failed to download file: {}", response.status())); + } + let mut tmp_file = File::create(Path::new(&TMP_DAEMON))?; + std::io::copy(&mut response.bytes()?.as_ref(), &mut tmp_file)?; + let new_hash = crate::global::compute_sha256(&TMP_DAEMON)?; + let old_hash = crate::global::compute_sha256(&BINARY)?; + log::debug!("Old binary hash: {old_hash}. New binary hash: {new_hash}"); + if new_hash != old_hash { + std::fs::rename(BINARY, BINARY.to_string() + "_BACKUP")?; + std::fs::rename(TMP_DAEMON, BINARY)?; + std::fs::set_permissions(BINARY, std::fs::Permissions::from_mode(0o775))?; + std::process::exit(0); + } + Ok(()) +} diff --git a/src/state.rs b/src/state.rs index 0df70bb..d4e6d97 100644 --- a/src/state.rs +++ b/src/state.rs @@ -3,13 +3,11 @@ use crate::{config::Config, global::*, grpc::snp_proto}; use anyhow::{anyhow, Result}; use log::info; use serde::{Deserialize, Serialize}; -use sha2::{Digest, Sha256}; use std::{ collections::{HashMap, HashSet}, fs, fs::{remove_file, File}, - io::{Read, Write}, - path::Path, + io::Write, process::Command, }; @@ -996,7 +994,7 @@ fn download_and_check_sha(url: &str, sha: &str) -> Result<()> { } let mut file = File::create(Path::new(&save_path))?; copy(&mut response.bytes()?.as_ref(), &mut file)?; - match compute_sha256(&save_path) { + match crate::global::compute_sha256(&save_path) { Ok(hash) => { if hash != sha { return Err(anyhow!( @@ -1010,18 +1008,3 @@ fn download_and_check_sha(url: &str, sha: &str) -> Result<()> { } Ok(()) } - -fn compute_sha256>(path: P) -> Result { - let mut file = fs::File::open(path)?; - let mut hasher = Sha256::new(); - let mut buffer = [0u8; 8192]; - loop { - let bytes_read = file.read(&mut buffer)?; - if bytes_read == 0 { - break; - } - hasher.update(&buffer[..bytes_read]); - } - let result = hasher.finalize(); - Ok(format!("{:x}", result)) -} diff --git a/vm.proto b/vm.proto index e755772..f54f54d 100644 --- a/vm.proto +++ b/vm.proto @@ -55,7 +55,7 @@ message MeasurementIP { // This should also include a block hash or similar, for auth message RegisterVmNodeReq { string node_pubkey = 1; - string owner_pubkey = 2; + string operator_wallet = 2; string main_ip = 3; string country = 4; string region = 5; @@ -174,15 +174,14 @@ message VmNodeFilters { } message VmNodeListResp { - string node_pubkey = 1; - string country = 2; - string region = 3; - string city = 4; - string ip = 5; // required for latency test - uint32 server_rating = 6; - uint32 provider_rating = 7; - // nanoLP per unit per minute - uint64 price = 8; + string operator = 1; + string node_pubkey = 2; + string country = 3; + string region = 4; + string city = 5; + string ip = 6; // required for latency test + repeated string reports = 7; // TODO: this will become an enum + uint64 price = 8; // nanoLP per unit per minute } message ExtendVmReq { @@ -196,12 +195,60 @@ message AirdropReq { uint64 tokens = 2; } +message SlashReq { + string pubkey = 1; + uint64 tokens = 2; +} + message Account { string pubkey = 1; uint64 balance = 2; uint64 tmp_locked = 3; } +message RegOperatorReq { + string pubkey = 1; + uint64 escrow = 2; + string email = 3; +} + +message ListOperatorsResp { + string pubkey = 1; + uint64 escrow = 2; + string email = 3; + uint64 app_nodes = 4; + uint64 vm_nodes = 5; + uint64 reports = 6; +} + +message InspectOperatorResp { + ListOperatorsResp operator = 1; + repeated VmNodeListResp nodes = 2; +} + +message ReportNodeReq { + string admin_pubkey = 1; + string node_pubkey = 2; + string contract = 3; + string reason = 4; +} + +message KickReq { + string operator_wallet = 1; + string contract_uuid = 2; + string reason = 3; +} + +message BanUserReq { + string operator_wallet = 1; + string user_wallet = 2; +} + +message KickResp { + uint64 nano_lp = 1; +} + + service BrainCli { rpc GetBalance (Pubkey) returns (AccountBalance); rpc NewVm (NewVmReq) returns (NewVmResp); @@ -211,8 +258,15 @@ service BrainCli { rpc DeleteVm (DeleteVmReq) returns (Empty); rpc UpdateVm (UpdateVmReq) returns (UpdateVmResp); rpc ExtendVm (ExtendVmReq) returns (Empty); + rpc ReportNode (ReportNodeReq) returns (Empty); + rpc ListOperators (Empty) returns (stream ListOperatorsResp); + rpc InspectOperator (Pubkey) returns (InspectOperatorResp); + rpc RegisterOperator (RegOperatorReq) returns (Empty); + rpc KickContract (KickReq) returns (KickResp); + rpc BanUser (BanUserReq) returns (Empty); // admin commands rpc Airdrop (AirdropReq) returns (Empty); + rpc Slash (SlashReq) returns (Empty); rpc ListAllVmContracts (Empty) returns (stream VmContract); rpc ListAccounts (Empty) returns (stream Account); }