forked from matter-labs/zksync
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2201: Improve txs count query r=Deniallugo a=Deniallugo Signed-off-by: deniallugo <[email protected]> Co-authored-by: deniallugo <[email protected]>
- Loading branch information
Showing
11 changed files
with
232 additions
and
237 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
117 changes: 117 additions & 0 deletions
117
core/bin/zksync_api/src/api_server/rest/network_status.rs
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,117 @@ | ||
use futures::channel::mpsc; | ||
use serde::{Deserialize, Serialize}; | ||
use std::sync::Arc; | ||
use std::time::Duration; | ||
use tokio::sync::RwLock; | ||
use tokio::{runtime::Runtime, time}; | ||
use zksync_storage::ConnectionPool; | ||
use zksync_types::BlockNumber; | ||
use zksync_utils::panic_notify::ThreadPanicNotify; | ||
|
||
#[derive(Default, Debug, Serialize, Deserialize, Clone)] | ||
pub struct NetworkStatus { | ||
pub next_block_at_max: Option<u64>, | ||
pub last_committed: BlockNumber, | ||
pub last_verified: BlockNumber, | ||
pub total_transactions: u32, | ||
pub outstanding_txs: u32, | ||
pub mempool_size: u32, | ||
} | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct SharedNetworkStatus(Arc<RwLock<NetworkStatus>>); | ||
|
||
impl SharedNetworkStatus { | ||
pub async fn read(&self) -> NetworkStatus { | ||
(*self.0.as_ref().read().await).clone() | ||
} | ||
|
||
pub(crate) async fn update( | ||
&mut self, | ||
connection_pool: &ConnectionPool, | ||
last_tx_seq_no: i64, | ||
) -> Result<i64, anyhow::Error> { | ||
let mut storage = connection_pool.access_storage().await?; | ||
let mut transaction = storage.start_transaction().await?; | ||
let NetworkStatus { | ||
total_transactions, .. | ||
} = self.read().await; | ||
|
||
let last_verified = transaction | ||
.chain() | ||
.block_schema() | ||
.get_last_verified_confirmed_block() | ||
.await | ||
.unwrap_or(BlockNumber(0)); | ||
|
||
let last_committed = transaction | ||
.chain() | ||
.block_schema() | ||
.get_last_committed_block() | ||
.await | ||
.unwrap_or(BlockNumber(0)); | ||
|
||
let (total_new_transactions, last_seq_no) = transaction | ||
.chain() | ||
.stats_schema() | ||
.count_total_transactions(last_tx_seq_no) | ||
.await | ||
.unwrap_or((0, 0)); | ||
|
||
let mempool_size = transaction | ||
.chain() | ||
.mempool_schema() | ||
.get_mempool_size() | ||
.await | ||
.unwrap_or(0); | ||
|
||
let outstanding_txs = transaction | ||
.chain() | ||
.stats_schema() | ||
.count_outstanding_proofs(last_verified) | ||
.await | ||
.unwrap_or(0); | ||
|
||
transaction.commit().await.unwrap_or_default(); | ||
|
||
let status = NetworkStatus { | ||
next_block_at_max: None, | ||
last_committed, | ||
last_verified, | ||
total_transactions: total_transactions + total_new_transactions, | ||
outstanding_txs, | ||
mempool_size, | ||
}; | ||
|
||
// save status to state | ||
*self.0.as_ref().write().await = status; | ||
Ok(last_seq_no) | ||
} | ||
pub fn start_updater_detached( | ||
mut self, | ||
panic_notify: mpsc::Sender<bool>, | ||
connection_pool: ConnectionPool, | ||
) { | ||
std::thread::Builder::new() | ||
.name("rest-state-updater".to_string()) | ||
.spawn(move || { | ||
let _panic_sentinel = ThreadPanicNotify(panic_notify.clone()); | ||
|
||
let runtime = Runtime::new().expect("tokio runtime creation"); | ||
|
||
let state_update_task = async move { | ||
let mut timer = time::interval(Duration::from_millis(30000)); | ||
let mut last_seq_no = 0; | ||
loop { | ||
timer.tick().await; | ||
match self.update(&connection_pool, last_seq_no).await { | ||
Ok(seq_no) => last_seq_no = seq_no, | ||
Err(_) => vlog::error!("Can't update network status"), | ||
} | ||
} | ||
}; | ||
runtime.block_on(state_update_task); | ||
}) | ||
.expect("State update thread"); | ||
} | ||
} |
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,5 +1,4 @@ | ||
pub mod api_decl; | ||
pub mod api_impl; | ||
pub mod caches; | ||
pub mod network_status; | ||
pub mod types; |
107 changes: 0 additions & 107 deletions
107
core/bin/zksync_api/src/api_server/rest/v01/network_status.rs
This file was deleted.
Oops, something went wrong.
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.