11use crate :: config:: Config ;
2- use crate :: quorum_list:: { QuorumList , QuorumListEntry } ;
32use crate :: masternode:: EvoMasternodeList ;
43use crate :: masternode_cache:: MasternodeCache ;
4+ use crate :: quorum_cache:: QuorumCache ;
5+ use crate :: quorum_list:: QuorumListEntry ;
56use axum:: {
67 extract:: { Path , State } ,
78 http:: StatusCode ,
89 response:: Json ,
9- routing:: { get, post } ,
10+ routing:: get,
1011 Router ,
1112} ;
1213use serde:: Serialize ;
13- use std:: sync:: { Arc , RwLock } ;
14+ use std:: sync:: Arc ;
1415use tower_http:: cors:: CorsLayer ;
1516
16- pub type SharedQuorumList = Arc < RwLock < QuorumList > > ;
17+ pub type SharedQuorumCache = Arc < QuorumCache > ;
1718pub type SharedConfig = Arc < Config > ;
1819pub type SharedMasternodeCache = Arc < MasternodeCache > ;
1920
@@ -99,17 +100,16 @@ impl From<&QuorumListEntry> for QuorumEntryResponse {
99100}
100101
101102
102- pub fn create_router ( shared_list : SharedQuorumList , config : Config , masternode_cache : SharedMasternodeCache ) -> Router {
103+ pub fn create_router ( quorum_cache : SharedQuorumCache , config : Config , masternode_cache : SharedMasternodeCache ) -> Router {
103104 let shared_config = Arc :: new ( config) ;
104105 Router :: new ( )
105106 . route ( "/health" , get ( health_check) )
106107 . route ( "/quorums" , get ( get_all_quorums) )
107108 . route ( "/quorums/stats" , get ( get_quorum_stats) )
108- . route ( "/quorums/clear" , post ( clear_quorums) )
109109 . route ( "/previous" , get ( get_previous_quorums) )
110110 . route ( "/quorums/:hash" , get ( get_quorum_by_hash) )
111111 . route ( "/masternodes" , get ( get_masternodes) )
112- . with_state ( ( shared_list , shared_config, masternode_cache) )
112+ . with_state ( ( quorum_cache , shared_config, masternode_cache) )
113113 . layer ( CorsLayer :: permissive ( ) )
114114}
115115
@@ -118,109 +118,70 @@ async fn health_check() -> Json<ApiResponse<String>> {
118118}
119119
120120async fn get_all_quorums (
121- State ( ( shared_list , config , _) ) : State < ( SharedQuorumList , SharedConfig , SharedMasternodeCache ) > ,
121+ State ( ( quorum_cache , _ , _) ) : State < ( SharedQuorumCache , SharedConfig , SharedMasternodeCache ) > ,
122122) -> Result < Json < ApiResponse < Vec < QuorumEntryResponse > > > , StatusCode > {
123- // Fetch fresh quorums from Dash Core
124- match crate :: quorum_loader:: load_initial_quorums ( & config) . await {
125- Ok ( new_quorums) => {
126- // Update the shared list with fresh data
127- match shared_list. write ( ) {
128- Ok ( mut list) => {
129- * list = new_quorums;
130- }
131- Err ( _) => return Err ( StatusCode :: INTERNAL_SERVER_ERROR ) ,
132- }
133-
134- // Now read and return the updated list
135- match shared_list. read ( ) {
136- Ok ( list) => {
137- let quorums: Vec < QuorumEntryResponse > = list. iter ( ) . map ( |entry| entry. into ( ) ) . collect ( ) ;
138- Ok ( Json ( ApiResponse :: success ( quorums) ) )
139- }
140- Err ( _) => Err ( StatusCode :: INTERNAL_SERVER_ERROR ) ,
141- }
123+ match quorum_cache. get_quorums ( ) . await {
124+ Ok ( quorums) => {
125+ let quorums: Vec < QuorumEntryResponse > = quorums. iter ( ) . map ( |entry| entry. into ( ) ) . collect ( ) ;
126+ Ok ( Json ( ApiResponse :: success ( quorums) ) )
142127 }
143128 Err ( e) => Ok ( Json ( ApiResponse :: error ( format ! ( "Failed to load quorums: {}" , e) ) ) )
144129 }
145130}
146131
147132async fn get_quorum_stats (
148- State ( ( shared_list , _, _) ) : State < ( SharedQuorumList , SharedConfig , SharedMasternodeCache ) > ,
133+ State ( ( quorum_cache , _, _) ) : State < ( SharedQuorumCache , SharedConfig , SharedMasternodeCache ) > ,
149134) -> Result < Json < ApiResponse < QuorumStats > > , StatusCode > {
150- match shared_list . read ( ) {
151- Ok ( list ) => {
135+ match quorum_cache . get_quorums ( ) . await {
136+ Ok ( quorums ) => {
152137 let stats = QuorumStats {
153- total_count : list . len ( ) ,
154- is_empty : list . is_empty ( ) ,
138+ total_count : quorums . len ( ) ,
139+ is_empty : quorums . is_empty ( ) ,
155140 } ;
156141 Ok ( Json ( ApiResponse :: success ( stats) ) )
157142 }
158- Err ( _ ) => Err ( StatusCode :: INTERNAL_SERVER_ERROR ) ,
143+ Err ( e ) => Ok ( Json ( ApiResponse :: error ( format ! ( "Failed to get quorum stats: {}" , e ) ) ) )
159144 }
160145}
161146
162147async fn get_quorum_by_hash (
163148 Path ( hash) : Path < String > ,
164- State ( ( shared_list , _, _) ) : State < ( SharedQuorumList , SharedConfig , SharedMasternodeCache ) > ,
149+ State ( ( quorum_cache , _, _) ) : State < ( SharedQuorumCache , SharedConfig , SharedMasternodeCache ) > ,
165150) -> Result < Json < ApiResponse < QuorumEntryResponse > > , StatusCode > {
166151 let hash_bytes = match hex:: decode ( & hash) {
167152 Ok ( bytes) if bytes. len ( ) == 32 => bytes,
168153 _ => return Ok ( Json ( ApiResponse :: error ( "Invalid hash format. Must be 32 bytes hex encoded." . to_string ( ) ) ) ) ,
169154 } ;
170155
171- match shared_list . read ( ) {
172- Ok ( list ) => {
173- if let Some ( entry) = list . get_entry ( & hash_bytes) {
156+ match quorum_cache . get_quorums ( ) . await {
157+ Ok ( quorums ) => {
158+ if let Some ( entry) = quorums . get_entry ( & hash_bytes) {
174159 Ok ( Json ( ApiResponse :: success ( entry. into ( ) ) ) )
175160 } else {
176161 Ok ( Json ( ApiResponse :: error ( "Quorum not found" . to_string ( ) ) ) )
177162 }
178163 }
179- Err ( _) => Err ( StatusCode :: INTERNAL_SERVER_ERROR ) ,
180- }
181- }
182-
183-
184- async fn clear_quorums (
185- State ( ( shared_list, _, _) ) : State < ( SharedQuorumList , SharedConfig , SharedMasternodeCache ) > ,
186- ) -> Result < Json < ApiResponse < String > > , StatusCode > {
187- match shared_list. write ( ) {
188- Ok ( mut list) => {
189- list. clear ( ) ;
190- Ok ( Json ( ApiResponse :: success ( "All quorums cleared successfully" . to_string ( ) ) ) )
191- }
192- Err ( _) => Err ( StatusCode :: INTERNAL_SERVER_ERROR ) ,
164+ Err ( e) => Ok ( Json ( ApiResponse :: error ( format ! ( "Failed to get quorums: {}" , e) ) ) )
193165 }
194166}
195167
196168#[ axum:: debug_handler]
197169async fn get_previous_quorums (
198- State ( ( _ , config , _) ) : State < ( SharedQuorumList , SharedConfig , SharedMasternodeCache ) > ,
170+ State ( ( quorum_cache , _ , _) ) : State < ( SharedQuorumCache , SharedConfig , SharedMasternodeCache ) > ,
199171) -> Result < Json < ApiResponse < QuorumsAtHeightResponse > > , StatusCode > {
200- match crate :: quorum_loader:: get_current_block_height ( & config) . await {
201- Ok ( current_height) => {
202- let previous_height = if current_height >= config. quorum . previous_blocks_offset {
203- current_height - config. quorum . previous_blocks_offset
204- } else {
205- 0
206- } ;
207-
208- match crate :: quorum_loader:: load_quorums_at_height ( & config, previous_height) . await {
209- Ok ( quorum_list) => {
210- let quorums: Vec < QuorumEntryResponse > = quorum_list. iter ( ) . map ( |entry| entry. into ( ) ) . collect ( ) ;
211- let response = QuorumsAtHeightResponse { height : previous_height, quorums } ;
212- Ok ( Json ( ApiResponse :: success ( response) ) )
213- }
214- Err ( e) => Ok ( Json ( ApiResponse :: error ( format ! ( "Failed to load quorums: {}" , e) ) ) )
215- }
172+ match quorum_cache. get_previous_quorums ( ) . await {
173+ Ok ( ( height, quorum_list) ) => {
174+ let quorums: Vec < QuorumEntryResponse > = quorum_list. iter ( ) . map ( |entry| entry. into ( ) ) . collect ( ) ;
175+ let response = QuorumsAtHeightResponse { height, quorums } ;
176+ Ok ( Json ( ApiResponse :: success ( response) ) )
216177 }
217- Err ( e) => Ok ( Json ( ApiResponse :: error ( format ! ( "Failed to get block height : {}" , e) ) ) )
178+ Err ( e) => Ok ( Json ( ApiResponse :: error ( format ! ( "Failed to load previous quorums : {}" , e) ) ) )
218179 }
219180}
220181
221182#[ axum:: debug_handler]
222183async fn get_masternodes (
223- State ( ( _, config, masternode_cache) ) : State < ( SharedQuorumList , SharedConfig , SharedMasternodeCache ) > ,
184+ State ( ( _, config, masternode_cache) ) : State < ( SharedQuorumCache , SharedConfig , SharedMasternodeCache ) > ,
224185) -> Result < Json < ApiResponse < EvoMasternodeList > > , StatusCode > {
225186 match masternode_cache. get_masternodes ( ) . await {
226187 Ok ( masternodes) => {
0 commit comments