feat: add function to find app contracts by operator and and by uuid

This commit is contained in:
Noor 2025-03-22 03:17:31 +05:30
parent 78525cc638
commit ef629dff88
Signed by: noormohammedb
GPG Key ID: D83EFB8B3B967146
3 changed files with 45 additions and 4 deletions

2
Cargo.lock generated

@ -414,7 +414,7 @@ dependencies = [
[[package]]
name = "detee-shared"
version = "0.1.0"
source = "git+ssh://git@gitea.detee.cloud/testnet/proto?branch=main#70e83dd0e982eeb491212c4a9d265df0b148fe24"
source = "git+ssh://git@gitea.detee.cloud/testnet/proto?branch=main#3024c00b8e1c93e70902793385b92bc0a8d1f26a"
dependencies = [
"base64",
"prost",

@ -1319,6 +1319,23 @@ impl BrainData {
nodes.iter().cloned().find(|n| n.node_pubkey == public_key)
}
pub fn find_app_contracts_by_operator(&self, wallet: &str) -> Vec<AppContract> {
debug!("Searching contracts for operator {wallet}");
let nodes = match self.operators.get(wallet) {
Some(op) => op.vm_nodes.clone(),
None => return Vec::new(),
};
let contracts: Vec<AppContract> = self
.app_contracts
.read()
.unwrap()
.iter()
.filter(|c| nodes.contains(&c.node_pubkey))
.cloned()
.collect();
contracts
}
pub fn find_app_contracts_by_admin_pubkey(&self, admin_pubkey: &str) -> Vec<AppContract> {
debug!("Searching contracts for admin pubkey {admin_pubkey}");
let contracts: Vec<AppContract> = self

@ -544,9 +544,33 @@ impl BrainAppCli for BrainAppCliMock {
req: tonic::Request<ListAppContractsReq>,
) -> Result<tonic::Response<Self::ListAppContractsStream>, Status> {
let req_data = check_sig_from_req(req)?;
let app_contracts = self
info!(
"CLI {} requested ListAppContractsStream. As operator: {}",
req_data.admin_pubkey, req_data.as_operator
);
let mut app_contracts = vec![];
if !req_data.uuid.is_empty() {
if let Ok(specific_contract) = self.data.find_app_contract_by_uuid(&req_data.uuid) {
if specific_contract.admin_pubkey == req_data.admin_pubkey {
app_contracts.push(specific_contract);
}
}
} else {
if req_data.as_operator {
app_contracts.append(
&mut self
.data
.find_app_contracts_by_admin_pubkey(&req_data.admin_pubkey);
.find_app_contracts_by_operator(&req_data.admin_pubkey),
);
} else {
app_contracts.append(
&mut self
.data
.find_app_contracts_by_admin_pubkey(&req_data.admin_pubkey),
);
}
}
let (tx, rx) = mpsc::channel(6);
tokio::spawn(async move {