rename structs so that they say "VM"
This commit is contained in:
parent
64f892c174
commit
c98db7f8c3
2
build.rs
2
build.rs
@ -1,6 +1,6 @@
|
||||
fn main() {
|
||||
tonic_build::configure()
|
||||
.build_server(true)
|
||||
.compile_protos(&["snp.proto"], &["proto"])
|
||||
.compile_protos(&["vm.proto"], &["proto"])
|
||||
.unwrap_or_else(|e| panic!("Failed to compile protos {:?}", e));
|
||||
}
|
||||
|
169
src/data.rs
169
src/data.rs
@ -1,4 +1,3 @@
|
||||
#![allow(dead_code)]
|
||||
use crate::grpc::snp_proto::{self as grpc};
|
||||
use chrono::Utc;
|
||||
use dashmap::DashMap;
|
||||
@ -14,10 +13,8 @@ pub enum Error {
|
||||
TxTooBig,
|
||||
#[error("Account has insufficient funds for this operation")]
|
||||
InsufficientFunds,
|
||||
#[error("I have no idea how this happened. Please report this bug.")]
|
||||
ImpossibleError,
|
||||
#[error("Could not find contract {0}")]
|
||||
ContractNotFound(String),
|
||||
VmContractNotFound(String),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@ -36,7 +33,7 @@ impl From<AccountNanoLP> for grpc::AccountBalance {
|
||||
}
|
||||
|
||||
#[derive(Eq, Hash, PartialEq, Clone, Debug, Default)]
|
||||
pub struct Node {
|
||||
pub struct VmNode {
|
||||
pub public_key: String,
|
||||
pub owner_key: String,
|
||||
pub country: String,
|
||||
@ -54,9 +51,9 @@ pub struct Node {
|
||||
pub price: u64,
|
||||
}
|
||||
|
||||
impl Into<grpc::NodeListResp> for Node {
|
||||
fn into(self) -> grpc::NodeListResp {
|
||||
grpc::NodeListResp {
|
||||
impl Into<grpc::VmNodeListResp> for VmNode {
|
||||
fn into(self) -> grpc::VmNodeListResp {
|
||||
grpc::VmNodeListResp {
|
||||
node_pubkey: self.public_key,
|
||||
country: self.country,
|
||||
region: self.region,
|
||||
@ -70,7 +67,7 @@ impl Into<grpc::NodeListResp> for Node {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Contract {
|
||||
pub struct VmContract {
|
||||
pub uuid: String,
|
||||
pub hostname: String,
|
||||
pub admin_pubkey: String,
|
||||
@ -92,7 +89,7 @@ pub struct Contract {
|
||||
pub collected_at: chrono::DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl Contract {
|
||||
impl VmContract {
|
||||
fn total_units(&self) -> u64 {
|
||||
// TODO: Optimize this based on price of hardware.
|
||||
// I tried, but this can be done better.
|
||||
@ -109,10 +106,10 @@ impl Contract {
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<grpc::Contract> for Contract {
|
||||
fn into(self) -> grpc::Contract {
|
||||
impl Into<grpc::VmContract> for VmContract {
|
||||
fn into(self) -> grpc::VmContract {
|
||||
let nano_per_minute = self.price_per_minute();
|
||||
grpc::Contract {
|
||||
grpc::VmContract {
|
||||
uuid: self.uuid,
|
||||
hostname: self.hostname,
|
||||
admin_pubkey: self.admin_pubkey,
|
||||
@ -138,26 +135,19 @@ impl Into<grpc::Contract> for Contract {
|
||||
pub struct BrainData {
|
||||
// amount of nanoLP in each account
|
||||
accounts: DashMap<String, AccountNanoLP>,
|
||||
nodes: RwLock<Vec<Node>>,
|
||||
contracts: RwLock<Vec<Contract>>,
|
||||
vm_nodes: RwLock<Vec<VmNode>>,
|
||||
vm_contracts: RwLock<Vec<VmContract>>,
|
||||
tmp_newvm_reqs: DashMap<String, (grpc::NewVmReq, OneshotSender<grpc::NewVmResp>)>,
|
||||
tmp_updatevm_reqs: DashMap<String, (grpc::UpdateVmReq, OneshotSender<grpc::UpdateVmResp>)>,
|
||||
daemon_tx: DashMap<String, Sender<grpc::BrainMessage>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum TxType {
|
||||
CliContract,
|
||||
DaemonDeleteVm,
|
||||
DaemonNewVm,
|
||||
daemon_tx: DashMap<String, Sender<grpc::BrainVmMessage>>,
|
||||
}
|
||||
|
||||
impl BrainData {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
accounts: DashMap::new(),
|
||||
nodes: RwLock::new(Vec::new()),
|
||||
contracts: RwLock::new(Vec::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(),
|
||||
@ -191,11 +181,11 @@ impl BrainData {
|
||||
});
|
||||
}
|
||||
|
||||
pub async fn contracts_cron(&self) {
|
||||
pub async fn vm_contracts_cron(&self) {
|
||||
let mut deleted_contracts: Vec<(String, String)> = Vec::new();
|
||||
log::debug!("Running contracts cron...");
|
||||
{
|
||||
let mut contracts = self.contracts.write().unwrap();
|
||||
let mut contracts = self.vm_contracts.write().unwrap();
|
||||
contracts.retain_mut(|c| {
|
||||
let owner_key = self
|
||||
.find_nodes_by_pubkey(&c.node_pubkey)
|
||||
@ -220,8 +210,8 @@ impl BrainData {
|
||||
// inform daemons of the deletion of the contracts
|
||||
for (uuid, node_pubkey) in deleted_contracts.iter() {
|
||||
if let Some(daemon_tx) = self.daemon_tx.get(&node_pubkey.clone()) {
|
||||
let msg = grpc::BrainMessage {
|
||||
msg: Some(grpc::brain_message::Msg::DeleteVm(grpc::DeleteVmReq {
|
||||
let msg = grpc::BrainVmMessage {
|
||||
msg: Some(grpc::brain_vm_message::Msg::DeleteVm(grpc::DeleteVmReq {
|
||||
uuid: uuid.to_string(),
|
||||
admin_pubkey: String::new(),
|
||||
})),
|
||||
@ -232,13 +222,13 @@ impl BrainData {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_node(&self, node: Node) {
|
||||
pub fn insert_node(&self, node: VmNode) {
|
||||
info!("Registering node {node:?}");
|
||||
let mut nodes = self.nodes.write().unwrap();
|
||||
let mut nodes = self.vm_nodes.write().unwrap();
|
||||
for n in nodes.iter_mut() {
|
||||
if n.public_key == node.public_key {
|
||||
// TODO: figure what to do in this case.
|
||||
warn!("Node {} already exists. Updating data.", n.public_key);
|
||||
warn!("VM Node {} already exists. Updating data.", n.public_key);
|
||||
*n = node;
|
||||
return;
|
||||
}
|
||||
@ -262,20 +252,7 @@ impl BrainData {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unlock_nanotockens(&self, account: &str, nano_lp: u64) -> Result<(), Error> {
|
||||
if let Some(mut account) = self.accounts.get_mut(account) {
|
||||
if nano_lp > account.tmp_locked {
|
||||
return Err(Error::ImpossibleError);
|
||||
}
|
||||
account.balance = account.balance.saturating_add(nano_lp);
|
||||
account.tmp_locked = account.tmp_locked.saturating_sub(nano_lp);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::ImpossibleError)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn extend_contract_time(
|
||||
pub fn extend_vm_contract_time(
|
||||
&self,
|
||||
uuid: &str,
|
||||
wallet: &str,
|
||||
@ -289,7 +266,7 @@ impl BrainData {
|
||||
None => return Err(Error::InsufficientFunds),
|
||||
};
|
||||
match self
|
||||
.contracts
|
||||
.vm_contracts
|
||||
.write()
|
||||
.unwrap()
|
||||
.iter_mut()
|
||||
@ -297,7 +274,7 @@ impl BrainData {
|
||||
{
|
||||
Some(contract) => {
|
||||
if contract.admin_pubkey != wallet {
|
||||
return Err(Error::ContractNotFound(uuid.to_string()));
|
||||
return Err(Error::VmContractNotFound(uuid.to_string()));
|
||||
}
|
||||
if account.balance + contract.locked_nano < nano_lp {
|
||||
return Err(Error::InsufficientFunds);
|
||||
@ -306,12 +283,12 @@ impl BrainData {
|
||||
contract.locked_nano = nano_lp;
|
||||
Ok(())
|
||||
}
|
||||
None => Err(Error::ContractNotFound(uuid.to_string())),
|
||||
None => Err(Error::VmContractNotFound(uuid.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn submit_node_resources(&self, res: grpc::NodeResources) {
|
||||
let mut nodes = self.nodes.write().unwrap();
|
||||
pub fn submit_node_resources(&self, res: grpc::VmNodeResources) {
|
||||
let mut nodes = self.vm_nodes.write().unwrap();
|
||||
for n in nodes.iter_mut() {
|
||||
if n.public_key == res.node_pubkey {
|
||||
debug!(
|
||||
@ -329,13 +306,13 @@ impl BrainData {
|
||||
}
|
||||
}
|
||||
debug!(
|
||||
"Node {} not found when trying to update resources.",
|
||||
"VM Node {} not found when trying to update resources.",
|
||||
res.node_pubkey
|
||||
);
|
||||
debug!("Node list:\n{:?}", nodes);
|
||||
debug!("VM Node list:\n{:?}", nodes);
|
||||
}
|
||||
|
||||
pub fn add_daemon_tx(&self, node_pubkey: &str, tx: Sender<grpc::BrainMessage>) {
|
||||
pub fn add_daemon_tx(&self, node_pubkey: &str, tx: Sender<grpc::BrainVmMessage>) {
|
||||
self.daemon_tx.insert(node_pubkey.to_string(), tx);
|
||||
}
|
||||
|
||||
@ -347,12 +324,12 @@ impl BrainData {
|
||||
let contract = match self.find_contract_by_uuid(&delete_vm.uuid) {
|
||||
Some(contract) => {
|
||||
if contract.admin_pubkey != delete_vm.admin_pubkey {
|
||||
return Err(Error::ContractNotFound(delete_vm.uuid));
|
||||
return Err(Error::VmContractNotFound(delete_vm.uuid));
|
||||
}
|
||||
contract
|
||||
}
|
||||
None => {
|
||||
return Err(Error::ContractNotFound(delete_vm.uuid));
|
||||
return Err(Error::VmContractNotFound(delete_vm.uuid));
|
||||
}
|
||||
};
|
||||
info!("Found vm {}. Deleting...", delete_vm.uuid);
|
||||
@ -361,8 +338,8 @@ impl BrainData {
|
||||
"TX for daemon {} found. Informing daemon about deletion of {}.",
|
||||
contract.node_pubkey, delete_vm.uuid
|
||||
);
|
||||
let msg = grpc::BrainMessage {
|
||||
msg: Some(grpc::brain_message::Msg::DeleteVm(delete_vm.clone())),
|
||||
let msg = grpc::BrainVmMessage {
|
||||
msg: Some(grpc::brain_vm_message::Msg::DeleteVm(delete_vm.clone())),
|
||||
};
|
||||
if let Err(e) = daemon_tx.send(msg).await {
|
||||
warn!(
|
||||
@ -375,11 +352,12 @@ impl BrainData {
|
||||
}
|
||||
|
||||
self.add_nano_to_wallet(&contract.admin_pubkey, contract.locked_nano);
|
||||
let mut contracts = self.contracts.write().unwrap();
|
||||
let mut contracts = self.vm_contracts.write().unwrap();
|
||||
contracts.retain(|c| c.uuid != delete_vm.uuid);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// also unlocks nanotokens in case VM creation failed
|
||||
pub async fn submit_newvm_resp(&self, new_vm_resp: grpc::NewVmResp) {
|
||||
let new_vm_req = match self.tmp_newvm_reqs.remove(&new_vm_resp.uuid) {
|
||||
Some((_, r)) => r,
|
||||
@ -426,7 +404,7 @@ impl BrainData {
|
||||
admin_wallet.tmp_locked -= new_vm_req.0.locked_nano;
|
||||
}
|
||||
|
||||
let contract = Contract {
|
||||
let contract = VmContract {
|
||||
uuid: new_vm_resp.uuid,
|
||||
exposed_ports: args.exposed_ports.clone(),
|
||||
public_ipv4,
|
||||
@ -446,7 +424,7 @@ impl BrainData {
|
||||
collected_at: Utc::now(),
|
||||
};
|
||||
info!("Created new contract: {contract:?}");
|
||||
self.contracts.write().unwrap().push(contract);
|
||||
self.vm_contracts.write().unwrap().push(contract);
|
||||
}
|
||||
|
||||
pub async fn submit_updatevm_resp(&self, mut update_vm_resp: grpc::UpdateVmResp) {
|
||||
@ -465,7 +443,7 @@ impl BrainData {
|
||||
if update_vm_resp.error != "" {
|
||||
return;
|
||||
}
|
||||
let mut contracts = self.contracts.write().unwrap();
|
||||
let mut contracts = self.vm_contracts.write().unwrap();
|
||||
match contracts.iter_mut().find(|c| c.uuid == update_vm_resp.uuid) {
|
||||
Some(contract) => {
|
||||
if update_vm_req.0.vcpus != 0 {
|
||||
@ -494,8 +472,8 @@ impl BrainData {
|
||||
contract.updated_at = Utc::now();
|
||||
}
|
||||
None => {
|
||||
log::error!("Contract not found for {}.", update_vm_req.0.uuid);
|
||||
update_vm_resp.error = "Contract not found.".to_string();
|
||||
log::error!("VM Contract not found for {}.", update_vm_req.0.uuid);
|
||||
update_vm_resp.error = "VM Contract not found.".to_string();
|
||||
}
|
||||
}
|
||||
if let Err(e) = update_vm_req.1.send(update_vm_resp.clone()) {
|
||||
@ -527,8 +505,8 @@ impl BrainData {
|
||||
"Found daemon TX for {}. Sending newVMReq {}",
|
||||
req.node_pubkey, req.uuid
|
||||
);
|
||||
let msg = grpc::BrainMessage {
|
||||
msg: Some(grpc::brain_message::Msg::NewVmReq(req.clone())),
|
||||
let msg = grpc::BrainVmMessage {
|
||||
msg: Some(grpc::brain_vm_message::Msg::NewVmReq(req.clone())),
|
||||
};
|
||||
if let Err(e) = daemon_tx.send(msg).await {
|
||||
warn!(
|
||||
@ -544,6 +522,13 @@ impl BrainData {
|
||||
})
|
||||
.await;
|
||||
}
|
||||
} else {
|
||||
self.submit_newvm_resp(grpc::NewVmResp {
|
||||
error: "Daemon is offline.".to_string(),
|
||||
uuid: req.uuid,
|
||||
args: None,
|
||||
})
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
@ -559,7 +544,7 @@ impl BrainData {
|
||||
if contract.admin_pubkey != req.admin_pubkey {
|
||||
let _ = tx.send(grpc::UpdateVmResp {
|
||||
uuid,
|
||||
error: "Contract does not exist.".to_string(),
|
||||
error: "VM Contract does not exist.".to_string(),
|
||||
args: None,
|
||||
});
|
||||
return;
|
||||
@ -573,7 +558,7 @@ impl BrainData {
|
||||
);
|
||||
let _ = tx.send(grpc::UpdateVmResp {
|
||||
uuid,
|
||||
error: "Contract does not exist.".to_string(),
|
||||
error: "VM Contract does not exist.".to_string(),
|
||||
args: None,
|
||||
});
|
||||
return;
|
||||
@ -586,8 +571,8 @@ impl BrainData {
|
||||
"Found daemon TX for {}. Sending updateVMReq {}",
|
||||
node_pubkey, req.uuid
|
||||
);
|
||||
let msg = grpc::BrainMessage {
|
||||
msg: Some(grpc::brain_message::Msg::UpdateVmReq(req.clone())),
|
||||
let msg = grpc::BrainVmMessage {
|
||||
msg: Some(grpc::brain_vm_message::Msg::UpdateVmReq(req.clone())),
|
||||
};
|
||||
match server_tx.send(msg).await {
|
||||
Ok(_) => {
|
||||
@ -617,26 +602,16 @@ impl BrainData {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_contract(&self, contract: Contract) {
|
||||
let mut contracts = self.contracts.write().unwrap();
|
||||
contracts.push(contract);
|
||||
}
|
||||
|
||||
pub fn find_nodes_by_pubkey(&self, public_key: &str) -> Option<Node> {
|
||||
let nodes = self.nodes.read().unwrap();
|
||||
pub fn find_nodes_by_pubkey(&self, public_key: &str) -> Option<VmNode> {
|
||||
let nodes = self.vm_nodes.read().unwrap();
|
||||
nodes.iter().cloned().find(|n| n.public_key == public_key)
|
||||
}
|
||||
|
||||
pub fn find_ns_by_owner_key(&self, owner_key: &str) -> Option<Node> {
|
||||
let nodes = self.nodes.read().unwrap();
|
||||
nodes.iter().cloned().find(|n| n.owner_key == owner_key)
|
||||
}
|
||||
|
||||
pub fn find_nodes_by_filters(
|
||||
&self,
|
||||
filters: &crate::grpc::snp_proto::NodeFilters,
|
||||
) -> Vec<Node> {
|
||||
let nodes = self.nodes.read().unwrap();
|
||||
filters: &crate::grpc::snp_proto::VmNodeFilters,
|
||||
) -> Vec<VmNode> {
|
||||
let nodes = self.vm_nodes.read().unwrap();
|
||||
nodes
|
||||
.iter()
|
||||
.filter(|n| {
|
||||
@ -658,9 +633,9 @@ impl BrainData {
|
||||
// TODO: sort by rating
|
||||
pub fn get_one_node_by_filters(
|
||||
&self,
|
||||
filters: &crate::grpc::snp_proto::NodeFilters,
|
||||
) -> Option<Node> {
|
||||
let nodes = self.nodes.read().unwrap();
|
||||
filters: &crate::grpc::snp_proto::VmNodeFilters,
|
||||
) -> Option<VmNode> {
|
||||
let nodes = self.vm_nodes.read().unwrap();
|
||||
nodes
|
||||
.iter()
|
||||
.find(|n| {
|
||||
@ -679,13 +654,13 @@ impl BrainData {
|
||||
.cloned()
|
||||
}
|
||||
|
||||
pub fn find_contract_by_uuid(&self, uuid: &str) -> Option<Contract> {
|
||||
let contracts = self.contracts.read().unwrap();
|
||||
pub fn find_contract_by_uuid(&self, uuid: &str) -> Option<VmContract> {
|
||||
let contracts = self.vm_contracts.read().unwrap();
|
||||
contracts.iter().cloned().find(|c| c.uuid == uuid)
|
||||
}
|
||||
|
||||
pub fn list_all_contracts(&self) -> Vec<Contract> {
|
||||
let contracts = self.contracts.read().unwrap();
|
||||
pub fn list_all_contracts(&self) -> Vec<VmContract> {
|
||||
let contracts = self.vm_contracts.read().unwrap();
|
||||
contracts.iter().cloned().collect()
|
||||
}
|
||||
|
||||
@ -700,10 +675,10 @@ impl BrainData {
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn find_contracts_by_admin_pubkey(&self, admin_pubkey: &str) -> Vec<Contract> {
|
||||
pub fn find_contracts_by_admin_pubkey(&self, admin_pubkey: &str) -> Vec<VmContract> {
|
||||
debug!("Searching contracts for admin pubkey {admin_pubkey}");
|
||||
let contracts: Vec<Contract> = self
|
||||
.contracts
|
||||
let contracts: Vec<VmContract> = self
|
||||
.vm_contracts
|
||||
.read()
|
||||
.unwrap()
|
||||
.iter()
|
||||
@ -714,8 +689,8 @@ impl BrainData {
|
||||
contracts
|
||||
}
|
||||
|
||||
pub fn find_contracts_by_node_pubkey(&self, node_pubkey: &str) -> Vec<Contract> {
|
||||
let contracts = self.contracts.read().unwrap();
|
||||
pub fn find_contracts_by_node_pubkey(&self, node_pubkey: &str) -> Vec<VmContract> {
|
||||
let contracts = self.vm_contracts.read().unwrap();
|
||||
contracts
|
||||
.iter()
|
||||
.filter(|c| c.node_pubkey == node_pubkey)
|
||||
|
79
src/grpc.rs
79
src/grpc.rs
@ -1,13 +1,14 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
pub mod snp_proto {
|
||||
tonic::include_proto!("snp_proto");
|
||||
tonic::include_proto!("vm_proto");
|
||||
}
|
||||
|
||||
use crate::grpc::vm_daemon_message;
|
||||
use crate::data::BrainData;
|
||||
use log::info;
|
||||
use snp_proto::brain_cli_server::BrainCli;
|
||||
use snp_proto::brain_daemon_server::BrainDaemon;
|
||||
use snp_proto::brain_vm_daemon_server::BrainVmDaemon;
|
||||
use snp_proto::*;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
@ -41,15 +42,15 @@ impl BrainCliMock {
|
||||
}
|
||||
|
||||
#[tonic::async_trait]
|
||||
impl BrainDaemon for BrainDaemonMock {
|
||||
type RegisterNodeStream = Pin<Box<dyn Stream<Item = Result<Contract, Status>> + Send>>;
|
||||
async fn register_node(
|
||||
impl BrainVmDaemon for BrainDaemonMock {
|
||||
type RegisterVmNodeStream = Pin<Box<dyn Stream<Item = Result<VmContract, Status>> + Send>>;
|
||||
async fn register_vm_node(
|
||||
&self,
|
||||
req: Request<RegisterNodeReq>,
|
||||
) -> Result<Response<Self::RegisterNodeStream>, Status> {
|
||||
req: Request<RegisterVmNodeReq>,
|
||||
) -> Result<Response<Self::RegisterVmNodeStream>, Status> {
|
||||
let req = check_sig_from_req(req)?;
|
||||
info!("Starting registration process for {:?}", req);
|
||||
let node = crate::data::Node {
|
||||
let node = crate::data::VmNode {
|
||||
public_key: req.node_pubkey.clone(),
|
||||
owner_key: req.owner_pubkey,
|
||||
country: req.country,
|
||||
@ -71,11 +72,11 @@ impl BrainDaemon for BrainDaemonMock {
|
||||
});
|
||||
let output_stream = ReceiverStream::new(rx);
|
||||
Ok(Response::new(
|
||||
Box::pin(output_stream) as Self::RegisterNodeStream
|
||||
Box::pin(output_stream) as Self::RegisterVmNodeStream
|
||||
))
|
||||
}
|
||||
|
||||
type BrainMessagesStream = Pin<Box<dyn Stream<Item = Result<BrainMessage, Status>> + Send>>;
|
||||
type BrainMessagesStream = Pin<Box<dyn Stream<Item = Result<BrainVmMessage, Status>> + Send>>;
|
||||
async fn brain_messages(
|
||||
&self,
|
||||
req: Request<DaemonStreamAuth>,
|
||||
@ -99,7 +100,7 @@ impl BrainDaemon for BrainDaemonMock {
|
||||
|
||||
async fn daemon_messages(
|
||||
&self,
|
||||
req: Request<Streaming<DaemonMessage>>,
|
||||
req: Request<Streaming<VmDaemonMessage>>,
|
||||
) -> Result<Response<Empty>, Status> {
|
||||
let mut req_stream = req.into_inner();
|
||||
let pubkey: String;
|
||||
@ -108,7 +109,7 @@ impl BrainDaemon for BrainDaemonMock {
|
||||
"demon_messages received the following auth message: {:?}",
|
||||
msg.msg
|
||||
);
|
||||
if let Some(daemon_message::Msg::Auth(auth)) = msg.msg {
|
||||
if let Some(vm_daemon_message::Msg::Auth(auth)) = msg.msg {
|
||||
pubkey = auth.pubkey.clone();
|
||||
check_sig_from_parts(
|
||||
&pubkey,
|
||||
@ -129,13 +130,13 @@ impl BrainDaemon for BrainDaemonMock {
|
||||
while let Some(daemon_message) = req_stream.next().await {
|
||||
match daemon_message {
|
||||
Ok(msg) => match msg.msg {
|
||||
Some(daemon_message::Msg::NewVmResp(new_vm_resp)) => {
|
||||
Some(vm_daemon_message::Msg::NewVmResp(new_vm_resp)) => {
|
||||
self.data.submit_newvm_resp(new_vm_resp).await;
|
||||
}
|
||||
Some(daemon_message::Msg::UpdateVmResp(update_vm_resp)) => {
|
||||
Some(vm_daemon_message::Msg::UpdateVmResp(update_vm_resp)) => {
|
||||
self.data.submit_updatevm_resp(update_vm_resp).await;
|
||||
}
|
||||
Some(daemon_message::Msg::NodeResources(node_resources)) => {
|
||||
Some(vm_daemon_message::Msg::VmNodeResources(node_resources)) => {
|
||||
self.data.submit_node_resources(node_resources);
|
||||
}
|
||||
_ => {}
|
||||
@ -197,20 +198,20 @@ impl BrainCli for BrainCliMock {
|
||||
let req = check_sig_from_req(req)?;
|
||||
match self
|
||||
.data
|
||||
.extend_contract_time(&req.uuid, &req.admin_pubkey, req.locked_nano)
|
||||
.extend_vm_contract_time(&req.uuid, &req.admin_pubkey, req.locked_nano)
|
||||
{
|
||||
Ok(()) => Ok(Response::new(Empty {})),
|
||||
Err(e) => Err(Status::unknown(format!("Could not extend contract: {e}"))),
|
||||
}
|
||||
}
|
||||
|
||||
type ListContractsStream = Pin<Box<dyn Stream<Item = Result<Contract, Status>> + Send>>;
|
||||
async fn list_contracts(
|
||||
type ListVmContractsStream = Pin<Box<dyn Stream<Item = Result<VmContract, Status>> + Send>>;
|
||||
async fn list_vm_contracts(
|
||||
&self,
|
||||
req: Request<ListContractsReq>,
|
||||
) -> Result<Response<Self::ListContractsStream>, Status> {
|
||||
req: Request<ListVmContractsReq>,
|
||||
) -> Result<Response<Self::ListVmContractsStream>, Status> {
|
||||
let req = check_sig_from_req(req)?;
|
||||
info!("CLI {} requested ListVMContractsStream", req.admin_pubkey);
|
||||
info!("CLI {} requested ListVMVmContractsStream", req.admin_pubkey);
|
||||
let contracts = match req.uuid.is_empty() {
|
||||
false => match self.data.find_contract_by_uuid(&req.uuid) {
|
||||
Some(contract) => vec![contract],
|
||||
@ -226,17 +227,17 @@ impl BrainCli for BrainCliMock {
|
||||
});
|
||||
let output_stream = ReceiverStream::new(rx);
|
||||
Ok(Response::new(
|
||||
Box::pin(output_stream) as Self::ListContractsStream
|
||||
Box::pin(output_stream) as Self::ListVmContractsStream
|
||||
))
|
||||
}
|
||||
|
||||
type ListNodesStream = Pin<Box<dyn Stream<Item = Result<NodeListResp, Status>> + Send>>;
|
||||
async fn list_nodes(
|
||||
type ListVmNodesStream = Pin<Box<dyn Stream<Item = Result<VmNodeListResp, Status>> + Send>>;
|
||||
async fn list_vm_nodes(
|
||||
&self,
|
||||
req: Request<NodeFilters>,
|
||||
) -> Result<Response<Self::ListNodesStream>, tonic::Status> {
|
||||
req: Request<VmNodeFilters>,
|
||||
) -> Result<Response<Self::ListVmNodesStream>, tonic::Status> {
|
||||
let req = check_sig_from_req(req)?;
|
||||
info!("Unknown CLI requested ListNodesStream: {req:?}");
|
||||
info!("CLI requested ListVmNodesStream: {req:?}");
|
||||
let nodes = self.data.find_nodes_by_filters(&req);
|
||||
let (tx, rx) = mpsc::channel(6);
|
||||
tokio::spawn(async move {
|
||||
@ -246,16 +247,16 @@ impl BrainCli for BrainCliMock {
|
||||
});
|
||||
let output_stream = ReceiverStream::new(rx);
|
||||
Ok(Response::new(
|
||||
Box::pin(output_stream) as Self::ListNodesStream
|
||||
Box::pin(output_stream) as Self::ListVmNodesStream
|
||||
))
|
||||
}
|
||||
|
||||
async fn get_one_node(
|
||||
async fn get_one_vm_node(
|
||||
&self,
|
||||
req: Request<NodeFilters>,
|
||||
) -> Result<Response<NodeListResp>, Status> {
|
||||
req: Request<VmNodeFilters>,
|
||||
) -> Result<Response<VmNodeListResp>, Status> {
|
||||
let req = check_sig_from_req(req)?;
|
||||
info!("Unknown CLI requested ListNodesStream: {req:?}");
|
||||
info!("Unknown CLI requested ListVmNodesStream: {req:?}");
|
||||
match self.data.get_one_node_by_filters(&req) {
|
||||
Some(node) => Ok(Response::new(node.into())),
|
||||
None => Err(Status::not_found(
|
||||
@ -280,11 +281,11 @@ impl BrainCli for BrainCliMock {
|
||||
Ok(Response::new(Empty{}))
|
||||
}
|
||||
|
||||
type ListAllContractsStream = Pin<Box<dyn Stream<Item = Result<Contract, Status>> + Send>>;
|
||||
async fn list_all_contracts(
|
||||
type ListAllVmContractsStream = Pin<Box<dyn Stream<Item = Result<VmContract, Status>> + Send>>;
|
||||
async fn list_all_vm_contracts(
|
||||
&self,
|
||||
req: Request<Empty>,
|
||||
) -> Result<Response<Self::ListContractsStream>, Status> {
|
||||
) -> Result<Response<Self::ListVmContractsStream>, Status> {
|
||||
check_admin_key(&req)?;
|
||||
let _ = check_sig_from_req(req)?;
|
||||
let contracts = self.data.list_all_contracts();
|
||||
@ -296,7 +297,7 @@ impl BrainCli for BrainCliMock {
|
||||
});
|
||||
let output_stream = ReceiverStream::new(rx);
|
||||
Ok(Response::new(
|
||||
Box::pin(output_stream) as Self::ListContractsStream
|
||||
Box::pin(output_stream) as Self::ListVmContractsStream
|
||||
))
|
||||
}
|
||||
|
||||
@ -356,19 +357,19 @@ impl PubkeyGetter for ExtendVmReq {
|
||||
}
|
||||
}
|
||||
|
||||
impl PubkeyGetter for ListContractsReq {
|
||||
impl PubkeyGetter for ListVmContractsReq {
|
||||
fn get_pubkey(&self) -> Option<String> {
|
||||
Some(self.admin_pubkey.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl PubkeyGetter for NodeFilters {
|
||||
impl PubkeyGetter for VmNodeFilters {
|
||||
fn get_pubkey(&self) -> Option<String> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl PubkeyGetter for RegisterNodeReq {
|
||||
impl PubkeyGetter for RegisterVmNodeReq {
|
||||
fn get_pubkey(&self) -> Option<String> {
|
||||
Some(self.node_pubkey.clone())
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ mod grpc;
|
||||
|
||||
use data::BrainData;
|
||||
use grpc::snp_proto::brain_cli_server::BrainCliServer;
|
||||
use grpc::snp_proto::brain_daemon_server::BrainDaemonServer;
|
||||
use grpc::snp_proto::brain_vm_daemon_server::BrainVmDaemonServer;
|
||||
use grpc::BrainCliMock;
|
||||
use grpc::BrainDaemonMock;
|
||||
use std::sync::Arc;
|
||||
@ -19,12 +19,12 @@ async fn main() {
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
|
||||
data_clone.contracts_cron().await;
|
||||
data_clone.vm_contracts_cron().await;
|
||||
}
|
||||
});
|
||||
let addr = "0.0.0.0:31337".parse().unwrap();
|
||||
|
||||
let daemon_server = BrainDaemonServer::new(BrainDaemonMock::new(data.clone()));
|
||||
let daemon_server = BrainVmDaemonServer::new(BrainDaemonMock::new(data.clone()));
|
||||
let cli_server = BrainCliServer::new(BrainCliMock::new(data.clone()));
|
||||
|
||||
Server::builder()
|
||||
|
@ -1,5 +1,5 @@
|
||||
syntax = "proto3";
|
||||
package snp_proto;
|
||||
package vm_proto;
|
||||
|
||||
message Empty {
|
||||
}
|
||||
@ -13,7 +13,7 @@ message AccountBalance {
|
||||
uint64 tmp_locked = 2;
|
||||
}
|
||||
|
||||
message Contract {
|
||||
message VmContract {
|
||||
string uuid = 1;
|
||||
string hostname = 2;
|
||||
string admin_pubkey = 3;
|
||||
@ -53,7 +53,7 @@ message MeasurementIP {
|
||||
}
|
||||
|
||||
// This should also include a block hash or similar, for auth
|
||||
message RegisterNodeReq {
|
||||
message RegisterVmNodeReq {
|
||||
string node_pubkey = 1;
|
||||
string owner_pubkey = 2;
|
||||
string main_ip = 3;
|
||||
@ -64,7 +64,7 @@ message RegisterNodeReq {
|
||||
uint64 price = 7;
|
||||
}
|
||||
|
||||
message NodeResources {
|
||||
message VmNodeResources {
|
||||
string node_pubkey = 1;
|
||||
uint32 avail_ports = 2;
|
||||
uint32 avail_ipv4 = 3;
|
||||
@ -123,7 +123,7 @@ message DeleteVmReq {
|
||||
string admin_pubkey = 2;
|
||||
}
|
||||
|
||||
message BrainMessage {
|
||||
message BrainVmMessage {
|
||||
oneof Msg {
|
||||
NewVmReq new_vm_req = 1;
|
||||
UpdateVmReq update_vm_req = 2;
|
||||
@ -138,28 +138,28 @@ message DaemonStreamAuth {
|
||||
string signature = 4;
|
||||
}
|
||||
|
||||
message DaemonMessage {
|
||||
message VmDaemonMessage {
|
||||
oneof Msg {
|
||||
DaemonStreamAuth auth = 1;
|
||||
NewVmResp new_vm_resp = 2;
|
||||
UpdateVmResp update_vm_resp = 3;
|
||||
NodeResources node_resources = 4;
|
||||
VmNodeResources vm_node_resources = 4;
|
||||
}
|
||||
}
|
||||
|
||||
service BrainDaemon {
|
||||
rpc RegisterNode (RegisterNodeReq) returns (stream Contract);
|
||||
rpc BrainMessages (DaemonStreamAuth) returns (stream BrainMessage);
|
||||
rpc DaemonMessages (stream DaemonMessage) returns (Empty);
|
||||
service BrainVmDaemon {
|
||||
rpc RegisterVmNode (RegisterVmNodeReq) returns (stream VmContract);
|
||||
rpc BrainMessages (DaemonStreamAuth) returns (stream BrainVmMessage);
|
||||
rpc DaemonMessages (stream VmDaemonMessage) returns (Empty);
|
||||
}
|
||||
|
||||
message ListContractsReq {
|
||||
message ListVmContractsReq {
|
||||
string admin_pubkey = 1;
|
||||
string node_pubkey = 2;
|
||||
string uuid = 3;
|
||||
}
|
||||
|
||||
message NodeFilters {
|
||||
message VmNodeFilters {
|
||||
uint32 free_ports = 1;
|
||||
bool offers_ipv4 = 2;
|
||||
bool offers_ipv6 = 3;
|
||||
@ -173,7 +173,7 @@ message NodeFilters {
|
||||
string node_pubkey = 11;
|
||||
}
|
||||
|
||||
message NodeListResp {
|
||||
message VmNodeListResp {
|
||||
string node_pubkey = 1;
|
||||
string country = 2;
|
||||
string region = 3;
|
||||
@ -205,14 +205,14 @@ message Account {
|
||||
service BrainCli {
|
||||
rpc GetBalance (Pubkey) returns (AccountBalance);
|
||||
rpc NewVm (NewVmReq) returns (NewVmResp);
|
||||
rpc ListContracts (ListContractsReq) returns (stream Contract);
|
||||
rpc ListNodes (NodeFilters) returns (stream NodeListResp);
|
||||
rpc GetOneNode (NodeFilters) returns (NodeListResp);
|
||||
rpc ListVmContracts (ListVmContractsReq) returns (stream VmContract);
|
||||
rpc ListVmNodes (VmNodeFilters) returns (stream VmNodeListResp);
|
||||
rpc GetOneVmNode (VmNodeFilters) returns (VmNodeListResp);
|
||||
rpc DeleteVm (DeleteVmReq) returns (Empty);
|
||||
rpc UpdateVm (UpdateVmReq) returns (UpdateVmResp);
|
||||
rpc ExtendVm (ExtendVmReq) returns (Empty);
|
||||
// admin commands
|
||||
rpc Airdrop (AirdropReq) returns (Empty);
|
||||
rpc ListAllContracts (Empty) returns (stream Contract);
|
||||
rpc ListAllVmContracts (Empty) returns (stream VmContract);
|
||||
rpc ListAccounts (Empty) returns (stream Account);
|
||||
}
|
Loading…
Reference in New Issue
Block a user