Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove functions about querying validators. #260

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 63 additions & 64 deletions explorer/src/service/v1/staking.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::service::api::Api;
use crate::service::util::{public_key_from_base64, public_key_from_bech32, public_key_to_base64};
use anyhow::Result;
use log::{debug, error};
use module::schema::{ClaimOpt, DelegationOpt, TdValidator, UnDelegationOpt};
use poem_openapi::param::{Path, Query};
use module::schema::{ClaimOpt, DelegationOpt, UnDelegationOpt};
use poem_openapi::param::Query;
use poem_openapi::{payload::Json, ApiResponse, Object};
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand Down Expand Up @@ -85,26 +84,26 @@ pub async fn get_tx_delegation(
let val: Value = row.try_get("d")?;
let opt: DelegationOpt = serde_json::from_value(val).unwrap();

let validator_detail_url = api
.platform
.rpc
.join(format!("validator_detail/{}", opt.body.validator).as_str())
.unwrap();
let res = reqwest::get(validator_detail_url)
.await?
.json::<TdValidator>()
.await;
if res.is_err() {
error!("failed to parse validator: {}", opt.body.validator);
continue;
}
let validator = res.unwrap();
// let validator_detail_url = api
// .platform
// .rpc
// .join(format!("validator_detail/{}", opt.body.validator).as_str())
// .unwrap();
// let res = reqwest::get(validator_detail_url)
// .await?
// .json::<TdValidator>()
// .await;
// if res.is_err() {
// error!("failed to parse validator: {}", opt.body.validator);
// continue;
// }
// let validator = res.unwrap();

items.push(DelegationItem {
tx_hash,
node_address: validator.addr,
node_name: validator.memo.name,
node_logo: validator.memo.logo,
node_address: "".into(),
node_name: "".into(),
node_logo: "".into(),
amount: opt.body.amount,
timestamp,
});
Expand Down Expand Up @@ -200,28 +199,28 @@ pub async fn get_tx_undelegation(
if opt.body.pu.is_none() {
continue;
}
let validator_address =
hex::encode(opt.body.pu.as_ref().unwrap().target_validator).to_uppercase();
let validator_detail_url = api
.platform
.rpc
.join(format!("validator_detail/{validator_address}").as_str())
.unwrap();
let res = reqwest::get(validator_detail_url)
.await?
.json::<TdValidator>()
.await;
if res.is_err() {
error!("failed to parse validator: {}", validator_address);
continue;
}
let validator = res.unwrap();
// let validator_address =
// hex::encode(opt.body.pu.as_ref().unwrap().target_validator).to_uppercase();
// let validator_detail_url = api
// .platform
// .rpc
// .join(format!("validator_detail/{validator_address}").as_str())
// .unwrap();
// let res = reqwest::get(validator_detail_url)
// .await?
// .json::<TdValidator>()
// .await;
// if res.is_err() {
// error!("failed to parse validator: {}", validator_address);
// continue;
// }
// let validator = res.unwrap();

items.push(UnDelegationItem {
tx_hash,
node_address: validator.addr,
node_name: validator.memo.name,
node_logo: validator.memo.logo,
node_address: "".into(),
node_name: "".into(),
node_logo: "".into(),
amount: opt.body.pu.unwrap().am,
timestamp,
expected_arrival_time: timestamp + 21 * 24 * 60 * 60,
Expand Down Expand Up @@ -351,31 +350,31 @@ pub struct DelegationInfoResult {
pub message: String,
pub data: Option<Value>,
}
#[allow(dead_code)]
pub async fn delegation(api: &Api, pubkey: Path<String>) -> Result<DelegationInfoResponse> {
let delegation_info_url = api
.platform
.rpc
.join(format!("delegation_info/{}", pubkey.0).as_str())
.unwrap();

debug!("delegation_info_url: {}", delegation_info_url);

let res = api
.platform
.client
.get(delegation_info_url)
.send()
.await?
.json()
.await?;

Ok(DelegationInfoResponse::Ok(Json(DelegationInfoResult {
code: 200,
message: "".to_string(),
data: Some(res),
})))
}
// #[allow(dead_code)]
// pub async fn delegation(api: &Api, pubkey: Path<String>) -> Result<DelegationInfoResponse> {
// let delegation_info_url = api
// .platform
// .rpc
// .join(format!("delegation_info/{}", pubkey.0).as_str())
// .unwrap();
//
// debug!("delegation_info_url: {}", delegation_info_url);
//
// let res = api
// .platform
// .client
// .get(delegation_info_url)
// .send()
// .await?
// .json()
// .await?;
//
// Ok(DelegationInfoResponse::Ok(Json(DelegationInfoResult {
// code: 200,
// message: "".to_string(),
// data: Some(res),
// })))
// }

#[derive(ApiResponse)]
pub enum UndelegationResponse {
Expand Down
198 changes: 99 additions & 99 deletions explorer/src/service/v1/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,21 @@ pub struct ValidatorListData {
pub validators: Vec<Validator>,
}

impl ValidatorListData {
fn wrap(self) -> WrapValidatorListData {
let mut wrap_validators: Vec<WrapValidator> = vec![];
for v in self.validators {
wrap_validators.push(v.wrap());
}

WrapValidatorListData {
threshold: self.threshold,
validator_cnt: self.validator_cnt,
cur_height: self.cur_height,
validators: wrap_validators,
}
}
}
// impl ValidatorListData {
// fn wrap(self) -> WrapValidatorListData {
// let mut wrap_validators: Vec<WrapValidator> = vec![];
// for v in self.validators {
// wrap_validators.push(v.wrap());
// }
//
// WrapValidatorListData {
// threshold: self.threshold,
// validator_cnt: self.validator_cnt,
// cur_height: self.cur_height,
// validators: wrap_validators,
// }
// }
// }

#[derive(Serialize, Deserialize, Debug, Default, Object)]
pub struct Validator {
Expand All @@ -134,19 +134,19 @@ pub struct Validator {
pub extra: Memo,
}

impl Validator {
fn wrap(self) -> WrapValidator {
WrapValidator {
addr: self.addr,
power: self.power,
commission_rate: self.commission_rate,
accept_delegation: self.accept_delegation,
online: false,
rank: self.rank,
extra: self.extra,
}
}
}
// impl Validator {
// fn wrap(self) -> WrapValidator {
// WrapValidator {
// addr: self.addr,
// power: self.power,
// commission_rate: self.commission_rate,
// accept_delegation: self.accept_delegation,
// online: false,
// rank: self.rank,
// extra: self.extra,
// }
// }
// }

#[allow(dead_code)]
pub async fn validator_delegation(
Expand Down Expand Up @@ -176,77 +176,77 @@ pub async fn validator_delegation(
)))
}

#[allow(dead_code)]
pub async fn validator_list(api: &Api) -> Result<ValidatorListResponse> {
let mut conn = api.storage.lock().await.acquire().await?;

let validator_list_url = api.platform.rpc.join("validator_list").unwrap();
debug!("validator_list_url: {}", validator_list_url);

let res = api
.platform
.client
.get(validator_list_url)
.send()
.await?
.json()
.await?;

let validator_data: ValidatorListData = serde_json::from_value(res).unwrap();

let cur_height: i64 = validator_data.cur_height - 15;
let sql = format!(
"SELECT address FROM block_generation WHERE height={} AND signature is not null",
cur_height
);

let mut wrap_validator_data = validator_data.wrap();

let rows = sqlx::query(sql.as_str()).fetch_all(&mut *conn).await?;
let mut signers: Vec<String> = vec![];
for r in rows {
let addr: String = r.try_get("address")?;
signers.push(addr);
}

for v in &mut wrap_validator_data.validators {
if signers.contains(&v.addr) {
v.online = true
}
}

Ok(ValidatorListResponse::Ok(Json(ValidatorListResult {
code: 200,
message: "".to_string(),
data: Some(wrap_validator_data),
})))
}

#[allow(dead_code)]
pub async fn validator_detail(api: &Api, address: Path<String>) -> Result<ValidatorDetailResponse> {
let validator_detail_url = api
.platform
.rpc
.join(format!("validator_detail/{}", address.0).as_str())
.unwrap();

debug!("validator_detail_url: {}", validator_detail_url);

let res = api
.platform
.client
.get(validator_detail_url)
.send()
.await?
.json()
.await?;

Ok(ValidatorDetailResponse::Ok(Json(ValidatorDetailResult {
code: 200,
message: "".to_string(),
data: Some(res),
})))
}
// #[allow(dead_code)]
// pub async fn validator_list(api: &Api) -> Result<ValidatorListResponse> {
// let mut conn = api.storage.lock().await.acquire().await?;
//
// let validator_list_url = api.platform.rpc.join("validator_list").unwrap();
// debug!("validator_list_url: {}", validator_list_url);
//
// let res = api
// .platform
// .client
// .get(validator_list_url)
// .send()
// .await?
// .json()
// .await?;
//
// let validator_data: ValidatorListData = serde_json::from_value(res).unwrap();
//
// let cur_height: i64 = validator_data.cur_height - 15;
// let sql = format!(
// "SELECT address FROM block_generation WHERE height={} AND signature is not null",
// cur_height
// );
//
// let mut wrap_validator_data = validator_data.wrap();
//
// let rows = sqlx::query(sql.as_str()).fetch_all(&mut *conn).await?;
// let mut signers: Vec<String> = vec![];
// for r in rows {
// let addr: String = r.try_get("address")?;
// signers.push(addr);
// }
//
// for v in &mut wrap_validator_data.validators {
// if signers.contains(&v.addr) {
// v.online = true
// }
// }
//
// Ok(ValidatorListResponse::Ok(Json(ValidatorListResult {
// code: 200,
// message: "".to_string(),
// data: Some(wrap_validator_data),
// })))
// }
//
// #[allow(dead_code)]
// pub async fn validator_detail(api: &Api, address: Path<String>) -> Result<ValidatorDetailResponse> {
// let validator_detail_url = api
// .platform
// .rpc
// .join(format!("validator_detail/{}", address.0).as_str())
// .unwrap();
//
// debug!("validator_detail_url: {}", validator_detail_url);
//
// let res = api
// .platform
// .client
// .get(validator_detail_url)
// .send()
// .await?
// .json()
// .await?;
//
// Ok(ValidatorDetailResponse::Ok(Json(ValidatorDetailResult {
// code: 200,
// message: "".to_string(),
// data: Some(res),
// })))
// }

#[allow(dead_code)]
pub async fn delegator_list(api: &Api, address: Path<String>) -> Result<DelegatorListResponse> {
Expand Down
Loading
Loading