importing new proto file

This commit is contained in:
ghe0 2025-02-14 03:18:08 +02:00
parent 76c88810a6
commit 8e8e1d1a99
Signed by: ghe0
GPG Key ID: 451028EE56A0FBB4
7 changed files with 146 additions and 33 deletions

@ -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

26
scripts/install_daemon.sh Executable file

@ -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

@ -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<IPInfo> {
info!("Got the following data from ipinfo.io: {body}");
Ok(serde_json::de::from_str(&body)?)
}
pub fn compute_sha256<P: AsRef<std::path::Path>>(path: P) -> Result<String> {
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))
}

@ -40,7 +40,7 @@ pub async fn register_node(config: &crate::config::Config) -> Result<Vec<VmContr
let ip_info = IP_INFO.clone();
let req = RegisterVmNodeReq {
node_pubkey: PUBLIC_KEY.clone(),
owner_pubkey: config.owner_wallet.clone(),
operator_wallet: config.owner_wallet.clone(),
main_ip: ip_info.ip,
country: ip_info.country,
region: ip_info.region,

@ -5,8 +5,9 @@ mod state;
use crate::global::*;
use crate::{config::Config, grpc::snp_proto};
use anyhow::Result;
use anyhow::{anyhow, Result};
use log::{debug, info, warn};
use std::{fs::File, path::Path};
use tokio::{
sync::mpsc::{Receiver, Sender},
time::{sleep, Duration},
@ -176,7 +177,7 @@ impl VMHandler {
}
async fn run(mut self) {
sleep(Duration::from_millis(100)).await;
sleep(Duration::from_millis(500)).await;
self.send_node_resources().await;
while let Some(brain_msg) = self.receiver.recv().await {
match brain_msg.msg {
@ -236,6 +237,10 @@ async fn main() {
env_logger::builder().filter_level(log::LevelFilter::Debug).init();
loop {
// This upgrade procedure will get replaced in prod. We need this for the testnet.
if let Err(e) = download_and_replace_binary() {
log::error!("Failed to upgrade detee-snp-daemon to newer version: {e}");
}
let (brain_msg_tx, brain_msg_rx) = tokio::sync::mpsc::channel(6);
let (daemon_msg_tx, daemon_msg_rx) = tokio::sync::mpsc::channel(6);
@ -271,3 +276,26 @@ async fn main() {
sleep(Duration::from_secs(3)).await;
}
}
fn download_and_replace_binary() -> 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(())
}

@ -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<P: AsRef<Path>>(path: P) -> Result<String> {
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))
}

@ -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);
}