@@ -2,9 +2,10 @@ use std::convert::From;
22use std:: net:: SocketAddr ;
33use std:: sync:: Arc ;
44
5- use crate :: box_kind:: { OracleBoxWrapper , PoolBox } ;
6- use crate :: node_interface:: node_api:: NodeApi ;
7- use crate :: oracle_config:: { get_core_api_port, ORACLE_CONFIG } ;
5+ use crate :: box_kind:: PoolBox ;
6+ use crate :: monitor:: { check_oracle_health, check_pool_health, PoolHealth } ;
7+ use crate :: node_interface:: node_api:: { NodeApi , NodeApiError } ;
8+ use crate :: oracle_config:: ORACLE_CONFIG ;
89use crate :: oracle_state:: { DataSourceError , LocalDatapointState , OraclePool } ;
910use crate :: pool_config:: POOL_CONFIG ;
1011use axum:: http:: StatusCode ;
@@ -135,26 +136,8 @@ fn pool_status_sync(oracle_pool: Arc<OraclePool>) -> Result<Json<serde_json::Val
135136 . epoch_length ( ) ;
136137 let pool_box_height = pool_box. get_box ( ) . creation_height ;
137138 let epoch_end_height = pool_box_height + epoch_length. 0 as u32 ;
138-
139- let posted_boxes = oracle_pool
140- . get_posted_datapoint_boxes_source ( )
141- . get_posted_datapoint_boxes ( ) ?;
142- let posted_count_current_epoch = posted_boxes
143- . into_iter ( )
144- . filter ( |b| b. get_box ( ) . creation_height >= pool_box_height)
145- . count ( ) ;
146-
147- let collected_boxes = oracle_pool
148- . get_collected_datapoint_boxes_source ( )
149- . get_collected_datapoint_boxes ( ) ?;
150- let collected_count_previous_epoch = collected_boxes
151- . into_iter ( )
152- . filter ( |b| b. get_box ( ) . creation_height == pool_box_height)
153- . count ( ) ;
154-
155- let active_oracle_count = collected_count_previous_epoch + posted_count_current_epoch;
156139 let pool_health = pool_health_sync ( oracle_pool) ?;
157-
140+ let active_oracle_count = pool_health . details . active_oracles . len ( ) ;
158141 let json = Json ( json ! ( {
159142 "latest_pool_datapoint" : pool_box. rate( ) ,
160143 "latest_pool_box_height" : pool_box_height,
@@ -202,72 +185,43 @@ fn oracle_health_sync(oracle_pool: Arc<OraclePool>) -> Result<serde_json::Value,
202185 . get_pool_box_source ( )
203186 . get_pool_box ( ) ?
204187 . get_box ( )
205- . creation_height ;
206- let mut check_details = json ! ( {
207- "pool_box_height" : pool_box_height,
208- } ) ;
209- let is_healthy = match oracle_pool
210- . get_local_datapoint_box_source ( )
211- . get_local_oracle_datapoint_box ( ) ?
212- {
213- Some ( b) => match b {
214- OracleBoxWrapper :: Posted ( posted_box) => {
215- let creation_height = posted_box. get_box ( ) . creation_height ;
216- check_details[ "posted_box_height" ] = json ! ( creation_height) ;
217- creation_height > pool_box_height
218- }
219- OracleBoxWrapper :: Collected ( collected_box) => {
220- let creation_height = collected_box. get_box ( ) . creation_height ;
221- check_details[ "collected_box_height" ] = json ! ( creation_height) ;
222- creation_height == pool_box_height
223- }
224- } ,
225- None => false ,
226- } ;
227- let json = json ! ( {
228- "status" : if is_healthy { "OK" } else { "DOWN" } ,
229- "details" : check_details,
230- } ) ;
231- Ok ( json)
188+ . creation_height
189+ . into ( ) ;
190+ let oracle_health = check_oracle_health ( oracle_pool, pool_box_height) ?;
191+ Ok ( serde_json:: to_value ( oracle_health) . unwrap ( ) )
232192}
233193
234194async fn pool_health ( oracle_pool : Arc < OraclePool > ) -> Result < Json < serde_json:: Value > , ApiError > {
235- let json = task:: spawn_blocking ( || pool_health_sync ( oracle_pool) )
195+ let json = task:: spawn_blocking ( || pool_health_sync_json ( oracle_pool) )
236196 . await
237197 . unwrap ( ) ?;
238198 Ok ( Json ( json) )
239199}
240- fn pool_health_sync ( oracle_pool : Arc < OraclePool > ) -> Result < serde_json :: Value , ApiError > {
241- let pool_conf = & POOL_CONFIG ;
200+
201+ fn pool_health_sync ( oracle_pool : Arc < OraclePool > ) -> Result < PoolHealth , ApiError > {
242202 let node_api = NodeApi :: new ( ORACLE_CONFIG . node_api_key . clone ( ) , & ORACLE_CONFIG . node_url ) ;
243- let current_height = node_api. node . current_block_height ( ) ? as u32 ;
203+ let current_height = ( node_api. node . current_block_height ( ) ? as u32 ) . into ( ) ;
244204 let pool_box_height = oracle_pool
245205 . get_pool_box_source ( )
246206 . get_pool_box ( ) ?
247207 . get_box ( )
248- . creation_height ;
249- let epoch_length = pool_conf
250- . refresh_box_wrapper_inputs
251- . contract_inputs
252- . contract_parameters ( )
253- . epoch_length ( )
254- . 0 as u32 ;
255- let check_details = json ! ( {
256- "pool_box_height" : pool_box_height,
257- "current_block_height" : current_height,
258- "epoch_length" : epoch_length,
259- } ) ;
260- let is_healthy = pool_box_height >= current_height - epoch_length;
261- let json = json ! ( {
262- "status" : if is_healthy { "OK" } else { "DOWN" } ,
263- "details" : check_details,
264- } ) ;
265- Ok ( json)
208+ . creation_height
209+ . into ( ) ;
210+ let network_prefix = node_api. get_change_address ( ) ?. network ( ) ;
211+ let pool_health =
212+ check_pool_health ( current_height, pool_box_height, oracle_pool, network_prefix) ?;
213+ Ok ( pool_health)
214+ }
215+
216+ fn pool_health_sync_json ( oracle_pool : Arc < OraclePool > ) -> Result < serde_json:: Value , ApiError > {
217+ let pool_health = pool_health_sync ( oracle_pool) ?;
218+ Ok ( serde_json:: to_value ( pool_health) . unwrap ( ) )
266219}
267220
268221pub async fn start_rest_server (
269222 repost_receiver : Receiver < bool > ,
270223 oracle_pool : Arc < OraclePool > ,
224+ api_port : u16 ,
271225) -> Result < ( ) , anyhow:: Error > {
272226 let op_clone = oracle_pool. clone ( ) ;
273227 let op_clone2 = oracle_pool. clone ( ) ;
@@ -290,7 +244,8 @@ pub async fn start_rest_server(
290244 . allow_origin ( tower_http:: cors:: Any )
291245 . allow_methods ( [ axum:: http:: Method :: GET ] ) ,
292246 ) ;
293- let addr = SocketAddr :: from ( ( [ 0 , 0 , 0 , 0 ] , get_core_api_port ( ) . parse ( ) . unwrap ( ) ) ) ;
247+ let addr = SocketAddr :: from ( ( [ 0 , 0 , 0 , 0 ] , api_port) ) ;
248+ log:: info!( "Starting REST server on {}" , addr) ;
294249 axum:: Server :: try_bind ( & addr) ?
295250 . serve ( app. into_make_service ( ) )
296251 . await ?;
@@ -322,3 +277,9 @@ impl From<anyhow::Error> for ApiError {
322277 ApiError ( format ! ( "Error: {:?}" , err) )
323278 }
324279}
280+
281+ impl From < NodeApiError > for ApiError {
282+ fn from ( err : NodeApiError ) -> Self {
283+ ApiError ( format ! ( "NodeApiError: {:?}" , err) )
284+ }
285+ }
0 commit comments