-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added total voting power, added sse chain sync * fmt, clippy
- Loading branch information
Showing
17 changed files
with
178 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use std::convert::Infallible; | ||
use std::time::Duration; | ||
|
||
use axum::extract::State; | ||
use axum::response::sse::{Event, KeepAlive}; | ||
use axum::response::Sse; | ||
use futures::Stream; | ||
use tokio_stream::StreamExt; | ||
|
||
use crate::state::common::CommonState; | ||
|
||
pub async fn sync_height( | ||
State(state): State<CommonState>, | ||
) -> Sse<impl Stream<Item = Result<Event, Infallible>>> { | ||
let stream = tokio_stream::wrappers::IntervalStream::new( | ||
tokio::time::interval(Duration::from_secs(3)), | ||
) | ||
.then(move |_| { | ||
let state = state.clone(); | ||
|
||
async move { | ||
let height = state.chain_service.find_latest_height().await; | ||
Ok(Event::default().data(height.to_string())) | ||
} | ||
}); | ||
|
||
Sse::new(stream).keep_alive(KeepAlive::default()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod balance; | ||
pub mod chain; | ||
pub mod governance; | ||
pub mod pos; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use axum::async_trait; | ||
use diesel::dsl::max; | ||
use diesel::{QueryDsl, RunQueryDsl}; | ||
use orm::schema::block_crawler_state; | ||
|
||
use crate::appstate::AppState; | ||
|
||
#[derive(Clone)] | ||
pub struct ChainRepository { | ||
pub(crate) app_state: AppState, | ||
} | ||
|
||
#[async_trait] | ||
pub trait ChainRepositoryTrait { | ||
fn new(app_state: AppState) -> Self; | ||
|
||
async fn find_latest_height(&self) -> Result<Option<i32>, String>; | ||
} | ||
|
||
#[async_trait] | ||
impl ChainRepositoryTrait for ChainRepository { | ||
fn new(app_state: AppState) -> Self { | ||
Self { app_state } | ||
} | ||
|
||
async fn find_latest_height(&self) -> Result<Option<i32>, String> { | ||
let conn = self.app_state.get_db_connection().await; | ||
|
||
conn.interact(move |conn| { | ||
block_crawler_state::dsl::block_crawler_state | ||
.select(max(block_crawler_state::dsl::height)) | ||
.first::<Option<i32>>(conn) | ||
}) | ||
.await | ||
.map_err(|e| e.to_string())? | ||
.map_err(|e| e.to_string()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod balance; | ||
pub mod chain; | ||
pub mod governance; | ||
pub mod pos; | ||
pub mod utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use crate::appstate::AppState; | ||
use crate::repository::chain::{ChainRepository, ChainRepositoryTrait}; | ||
|
||
#[derive(Clone)] | ||
pub struct ChainService { | ||
chain_repo: ChainRepository, | ||
} | ||
|
||
impl ChainService { | ||
pub fn new(app_state: AppState) -> Self { | ||
Self { | ||
chain_repo: ChainRepository::new(app_state), | ||
} | ||
} | ||
|
||
// return Result with ChainError | ||
pub async fn find_latest_height(&self) -> u64 { | ||
self.chain_repo | ||
.find_latest_height() | ||
.await | ||
.unwrap() | ||
.unwrap_or_default() as u64 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod balance; | ||
pub mod chain; | ||
pub mod governance; | ||
pub mod pos; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.