@@ -26,6 +26,7 @@ async fn root() -> &'static str {
2626 /oracleInfo - basic information about the oracle
2727 /oracleStatus - status of the oracle
2828 /oracleHealth - returns OK if our collected datapoint box height is the same as the pool box height OR our posted datapoint box height is greater than the pool box height
29+ /poolHealth - returns OK if the pool box height is greater or equal to (current height - epoch length)
2930 "
3031}
3132
@@ -152,6 +153,7 @@ fn pool_status_sync(oracle_pool: Arc<OraclePool>) -> Result<Json<serde_json::Val
152153 . count ( ) ;
153154
154155 let active_oracle_count = collected_count_previous_epoch + posted_count_current_epoch;
156+ let pool_health = pool_health_sync ( oracle_pool) ?;
155157
156158 let json = Json ( json ! ( {
157159 "latest_pool_datapoint" : pool_box. rate( ) ,
@@ -161,6 +163,7 @@ fn pool_status_sync(oracle_pool: Arc<OraclePool>) -> Result<Json<serde_json::Val
161163 "epoch_end_height" : epoch_end_height,
162164 "reward_tokens_in_pool_box" : pool_box. reward_token( ) . amount. as_u64( ) ,
163165 "number_of_oracles" : active_oracle_count,
166+ "pool_health" : pool_health,
164167 } ) ) ;
165168 Ok ( json)
166169}
@@ -228,12 +231,47 @@ fn oracle_health_sync(oracle_pool: Arc<OraclePool>) -> Result<serde_json::Value,
228231 Ok ( json)
229232}
230233
234+ async fn pool_health ( oracle_pool : Arc < OraclePool > ) -> Result < Json < serde_json:: Value > , ApiError > {
235+ let json = task:: spawn_blocking ( || pool_health_sync ( oracle_pool) )
236+ . await
237+ . unwrap ( ) ?;
238+ Ok ( Json ( json) )
239+ }
240+ fn pool_health_sync ( oracle_pool : Arc < OraclePool > ) -> Result < serde_json:: Value , ApiError > {
241+ let pool_conf = & POOL_CONFIG ;
242+ 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 ;
244+ let pool_box_height = oracle_pool
245+ . get_pool_box_source ( )
246+ . get_pool_box ( ) ?
247+ . 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)
266+ }
267+
231268pub async fn start_rest_server (
232269 repost_receiver : Receiver < bool > ,
233270 oracle_pool : Arc < OraclePool > ,
234271) -> Result < ( ) , anyhow:: Error > {
235272 let op_clone = oracle_pool. clone ( ) ;
236273 let op_clone2 = oracle_pool. clone ( ) ;
274+ let op_clone3 = oracle_pool. clone ( ) ;
237275 let app = Router :: new ( )
238276 . route ( "/" , get ( root) )
239277 . route ( "/oracleInfo" , get ( oracle_info) )
@@ -242,6 +280,7 @@ pub async fn start_rest_server(
242280 . route ( "/poolStatus" , get ( || pool_status ( op_clone) ) )
243281 . route ( "/blockHeight" , get ( block_height) )
244282 . route ( "/oracleHealth" , get ( || oracle_health ( op_clone2) ) )
283+ . route ( "/poolHealth" , get ( || pool_health ( op_clone3) ) )
245284 . route (
246285 "/requireDatapointRepost" ,
247286 get ( || require_datapoint_repost ( repost_receiver) ) ,
0 commit comments