Feat: list all accounts

admin can list all accounts
tests for list accounts
This commit is contained in:
Noor 2025-05-25 17:37:18 +05:30
parent ff03ec6085
commit e122a4e238
Signed by: noormohammedb
GPG Key ID: D83EFB8B3B967146
5 changed files with 73 additions and 16 deletions

@ -107,6 +107,11 @@ impl Account {
} }
Ok(()) Ok(())
} }
pub async fn list_accounts(db: &Surreal<Client>) -> Result<Vec<Self>, Error> {
let accounts: Vec<Account> = db.select(ACCOUNT).await?;
Ok(accounts)
}
} }
impl Account { impl Account {

@ -178,20 +178,19 @@ impl BrainGeneralCli for GeneralCliServer {
async fn list_accounts( async fn list_accounts(
&self, &self,
_req: Request<Empty>, req: Request<Empty>,
) -> Result<Response<Self::ListAccountsStream>, Status> { ) -> Result<Response<Self::ListAccountsStream>, Status> {
todo!(); check_admin_key(&req)?;
// check_admin_key(&req)?; check_sig_from_req(req)?;
// let _ = check_sig_from_req(req)?; let accounts = db::Account::list_accounts(&self.db).await?;
// let accounts = self.data.list_accounts(); let (tx, rx) = mpsc::channel(6);
// let (tx, rx) = mpsc::channel(6); tokio::spawn(async move {
// tokio::spawn(async move { for account in accounts {
// for account in accounts { let _ = tx.send(Ok(account.into())).await;
// let _ = tx.send(Ok(account.into())).await; }
// } });
// }); let output_stream = ReceiverStream::new(rx);
// let output_stream = ReceiverStream::new(rx); Ok(Response::new(Box::pin(output_stream) as Self::ListAccountsStream))
// Ok(Response::new(Box::pin(output_stream) as Self::ListAccountsStream))
} }
async fn list_all_vm_contracts( async fn list_all_vm_contracts(

@ -2,7 +2,7 @@ use crate::constants::{ACCOUNT, APP_NODE, ID_ALPHABET, NEW_APP_REQ, NEW_VM_REQ,
use crate::db::prelude as db; use crate::db::prelude as db;
use detee_shared::app_proto::AppNodeListResp; use detee_shared::app_proto::AppNodeListResp;
use detee_shared::common_proto::MappedPort; use detee_shared::common_proto::MappedPort;
use detee_shared::general_proto::{AccountBalance, ListOperatorsResp}; use detee_shared::general_proto::{Account, AccountBalance, ListOperatorsResp};
use detee_shared::{app_proto::*, vm_proto::*}; use detee_shared::{app_proto::*, vm_proto::*};
use nanoid::nanoid; use nanoid::nanoid;
@ -14,6 +14,16 @@ impl From<db::Account> for AccountBalance {
} }
} }
impl From<db::Account> for Account {
fn from(account: db::Account) -> Self {
Account {
pubkey: account.id.to_string(),
balance: account.balance,
tmp_locked: account.tmp_locked,
}
}
}
impl From<NewVmReq> for db::NewVmReq { impl From<NewVmReq> for db::NewVmReq {
fn from(new_vm_req: NewVmReq) -> Self { fn from(new_vm_req: NewVmReq) -> Self {
Self { Self {

@ -2,9 +2,10 @@ use super::test_utils::{admin_keys, Key};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use detee_shared::common_proto::Empty; use detee_shared::common_proto::Empty;
use detee_shared::general_proto::brain_general_cli_client::BrainGeneralCliClient; use detee_shared::general_proto::brain_general_cli_client::BrainGeneralCliClient;
use detee_shared::general_proto::{AirdropReq, RegOperatorReq, ReportNodeReq}; use detee_shared::general_proto::{Account, AirdropReq, RegOperatorReq, ReportNodeReq};
use detee_shared::vm_proto; use detee_shared::vm_proto;
use detee_shared::vm_proto::brain_vm_cli_client::BrainVmCliClient; use detee_shared::vm_proto::brain_vm_cli_client::BrainVmCliClient;
use futures::StreamExt;
use surreal_brain::constants::{ACTIVE_VM, NEW_VM_REQ}; use surreal_brain::constants::{ACTIVE_VM, NEW_VM_REQ};
use surreal_brain::db::prelude as db; use surreal_brain::db::prelude as db;
use surrealdb::engine::remote::ws::Client; use surrealdb::engine::remote::ws::Client;
@ -86,3 +87,24 @@ pub async fn register_operator(brain_channel: &Channel, key: &Key, escrow: u64)
cli_client.register_operator(key.sign_request(reg_req.clone()).unwrap()).await?; cli_client.register_operator(key.sign_request(reg_req.clone()).unwrap()).await?;
Ok(()) Ok(())
} }
pub async fn list_accounts(brain_channel: &Channel, admin_key: &Key) -> Result<Vec<Account>> {
let mut cli_client = BrainGeneralCliClient::new(brain_channel.clone());
let mut stream =
cli_client.list_accounts(admin_key.sign_request(Empty {}).unwrap()).await?.into_inner();
let mut accounts = Vec::new();
while let Some(stream_data) = stream.next().await {
match stream_data {
Ok(account) => {
accounts.push(account);
}
Err(e) => {
panic!("Error while listing accounts: {e:?}");
}
}
}
Ok(accounts)
}

@ -2,7 +2,7 @@ use common::prepare_test_env::{
prepare_test_db, run_service_for_stream, run_service_in_background, prepare_test_db, run_service_for_stream, run_service_in_background,
}; };
use common::test_utils::{admin_keys, Key}; use common::test_utils::{admin_keys, Key};
use common::vm_cli_utils::{airdrop, create_new_vm, register_operator, report_node}; use common::vm_cli_utils::{airdrop, create_new_vm, list_accounts, register_operator, report_node};
use common::vm_daemon_utils::{mock_vm_daemon, register_vm_node}; use common::vm_daemon_utils::{mock_vm_daemon, register_vm_node};
use detee_shared::common_proto::{Empty, Pubkey}; use detee_shared::common_proto::{Empty, Pubkey};
use detee_shared::general_proto::brain_general_cli_client::BrainGeneralCliClient; use detee_shared::general_proto::brain_general_cli_client::BrainGeneralCliClient;
@ -376,3 +376,24 @@ async fn test_slash_operator() {
let account = operator_account.unwrap(); let account = operator_account.unwrap();
assert_eq!(account.escrow, (escrew - slash_amt) * TOKEN_DECIMAL); assert_eq!(account.escrow, (escrew - slash_amt) * TOKEN_DECIMAL);
} }
#[tokio::test]
async fn test_admin_list_account() {
let db_conn = prepare_test_db().await.unwrap();
let brain_channel = run_service_for_stream().await.unwrap();
let admin_key = admin_keys()[0].clone();
let unauthenticated = list_accounts(&brain_channel, &Key::new()).await.err().unwrap();
assert!(unauthenticated.to_string().contains("reserved to admin accounts"));
let accounts = list_accounts(&brain_channel, &admin_key).await.unwrap();
assert_eq!(accounts.len(), 19);
airdrop(&brain_channel, &Key::new().pubkey, 10).await.unwrap();
let acc_in_db = db_conn.select::<Vec<db::Account>>(ACCOUNT).await.unwrap();
let accounts = list_accounts(&brain_channel, &admin_key).await.unwrap();
assert_eq!(accounts.len(), acc_in_db.len());
}