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

Backup #1

Merged
merged 8 commits into from
Feb 11, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add get commit signature
tq-tuan15 committed Feb 10, 2024
commit 218b7c9b0cb29b248c34deff596eff55324ef295
18 changes: 18 additions & 0 deletions src/database.rs
Original file line number Diff line number Diff line change
@@ -1349,6 +1349,24 @@ impl Database {
.map_err(Error::from)
}

pub async fn count_commit_signatures_by_validator(&self, validator_address: &[u8]) -> Result<i64, Error> {
let q = format!(
"SELECT COUNT(*)
FROM {0}.commit_signatures
WHERE validator_address = $1",
self.network
);

let row: Row = sqlx::query(&q)
.bind(validator_address)
.fetch_one(&*self.pool).await
.map_err(Error::from)?;

let count: i64 = row.get(0);

Ok(count)
}

pub fn pool(&self) -> &PgPool {
self.pool.as_ref()
}
23 changes: 15 additions & 8 deletions src/server/endpoints/validator.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use axum::{
extract::{Path, Query, State},
Json,
};
use serde::{Deserialize, Serialize};
use axum::{ extract::{ Path, Query, State }, Json };
use serde::{ Deserialize, Serialize };
use sqlx::postgres::PgRow as Row;
use sqlx::Row as TRow;
use std::collections::HashMap;
use tracing::{info, instrument};
use tracing::{ info, instrument };

use crate::{server::ServerState, Error};
use crate::{ server::ServerState, Error };

// Retrieve the count of commit for a range of blocks from the sql query result.
#[derive(Debug, Serialize, Deserialize, PartialEq, Default)]
@@ -34,7 +31,7 @@ pub struct UptimeValue {
pub async fn get_validator_uptime(
State(state): State<ServerState>,
Path(validator_address): Path<String>,
Query(params): Query<HashMap<String, i32>>,
Query(params): Query<HashMap<String, i32>>
) -> Result<Json<UptimeValue>, Error> {
info!("calling /validator/:validator_address/uptime");

@@ -58,3 +55,13 @@ pub async fn get_validator_uptime(

Ok(Json(uv))
}

pub async fn get_commit_signature(
State(state): State<ServerState>,
Path(validator_address): Path<String>
) -> Result<Json<i64>, Error> {
match state.db.count_commit_signatures_by_validator(validator_address.as_bytes()).await {
Ok(count) => Ok(Json(count)),
Err(e) => Err(e),
}
}
6 changes: 5 additions & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ use self::endpoints::{
account::get_account_updates,
block::{get_block_by_hash, get_block_by_height, get_last_block, get_blocks},
transaction::{get_shielded_tx, get_tx_by_hash, get_vote_proposal, get_txs},
validator::get_validator_uptime,
validator::{get_validator_uptime,get_commit_signature}
};

pub const HTTP_DURATION_SECONDS_BUCKETS: &[f64; 11] = &[
@@ -55,6 +55,10 @@ fn server_routes(state: ServerState) -> Router<()> {
"/validator/:validator_address/uptime",
get(get_validator_uptime),
)
.route(
"/validator/:validator_address/commit_signatures",
get(get_commit_signature),
)
.with_state(state)
}