Compare commits

..

1 Commits

Author SHA1 Message Date
b1795eec65
add support for operators 2025-02-14 18:23:17 +02:00
3 changed files with 16 additions and 42 deletions

@ -903,33 +903,17 @@ impl BrainData {
}) })
} }
pub fn find_vm_contracts_by_operator(&self, wallet: &str) -> Vec<VmContract> { pub fn find_vm_contracts_by_admin(&self, admin_pubkey: &str) -> Vec<VmContract> {
debug!("Searching contracts for operator {wallet}"); debug!("Searching contracts for admin pubkey {admin_pubkey}");
let nodes = match self.operators.get(wallet) {
Some(op) => op.vm_nodes.clone(),
None => return Vec::new(),
};
let contracts: Vec<VmContract> = self let contracts: Vec<VmContract> = self
.vm_contracts .vm_contracts
.read() .read()
.unwrap() .unwrap()
.iter() .iter()
.filter(|c| nodes.contains(&c.node_pubkey)) .filter(|c| c.admin_pubkey == admin_pubkey)
.cloned()
.collect();
contracts
}
pub fn find_vm_contracts_by_admin(&self, admin_wallet: &str) -> Vec<VmContract> {
debug!("Searching contracts for admin pubkey {admin_wallet}");
let contracts: Vec<VmContract> = self
.vm_contracts
.read()
.unwrap()
.iter()
.filter(|c| c.admin_pubkey == admin_wallet)
.cloned() .cloned()
.collect(); .collect();
debug!("Found {} contracts or {admin_pubkey}.", contracts.len());
contracts contracts
} }

@ -234,25 +234,14 @@ impl BrainCli for BrainCliMock {
req: Request<ListVmContractsReq>, req: Request<ListVmContractsReq>,
) -> Result<Response<Self::ListVmContractsStream>, Status> { ) -> Result<Response<Self::ListVmContractsStream>, Status> {
let req = check_sig_from_req(req)?; let req = check_sig_from_req(req)?;
info!( info!("CLI {} requested ListVMVmContractsStream", req.admin_pubkey);
"CLI {} requested ListVMVmContractsStream. As operator: {}", let contracts = match req.uuid.is_empty() {
req.wallet, req.as_operator false => match self.data.find_contract_by_uuid(&req.uuid) {
); Ok(contract) => vec![contract],
let mut contracts = Vec::new(); _ => Vec::new(),
if req.uuid.is_empty() { },
if let Ok(specific_contract) = self.data.find_contract_by_uuid(&req.uuid) { true => self.data.find_vm_contracts_by_admin(&req.admin_pubkey),
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); let (tx, rx) = mpsc::channel(6);
tokio::spawn(async move { tokio::spawn(async move {
for contract in contracts { for contract in contracts {
@ -443,7 +432,7 @@ impl_pubkey_getter!(DeleteVmReq, admin_pubkey);
impl_pubkey_getter!(UpdateVmReq, admin_pubkey); impl_pubkey_getter!(UpdateVmReq, admin_pubkey);
impl_pubkey_getter!(ExtendVmReq, admin_pubkey); impl_pubkey_getter!(ExtendVmReq, admin_pubkey);
impl_pubkey_getter!(ReportNodeReq, admin_pubkey); impl_pubkey_getter!(ReportNodeReq, admin_pubkey);
impl_pubkey_getter!(ListVmContractsReq, wallet); impl_pubkey_getter!(ListVmContractsReq, admin_pubkey);
impl_pubkey_getter!(RegisterVmNodeReq, node_pubkey); impl_pubkey_getter!(RegisterVmNodeReq, node_pubkey);
impl_pubkey_getter!(RegOperatorReq, pubkey); impl_pubkey_getter!(RegOperatorReq, pubkey);
impl_pubkey_getter!(KickReq, operator_wallet); impl_pubkey_getter!(KickReq, operator_wallet);

@ -154,8 +154,8 @@ service BrainVmDaemon {
} }
message ListVmContractsReq { message ListVmContractsReq {
string wallet = 1; string admin_pubkey = 1;
bool as_operator = 2; string node_pubkey = 2;
string uuid = 3; string uuid = 3;
} }
@ -248,6 +248,7 @@ message KickResp {
uint64 nano_lp = 1; uint64 nano_lp = 1;
} }
service BrainCli { service BrainCli {
rpc GetBalance (Pubkey) returns (AccountBalance); rpc GetBalance (Pubkey) returns (AccountBalance);
rpc NewVm (NewVmReq) returns (NewVmResp); rpc NewVm (NewVmReq) returns (NewVmResp);