1
1
use self :: channel_list:: { ChannelListQuery , LastApprovedQuery } ;
2
+ use crate :: db:: event_aggregate:: { lastest_approve_state, latest_heartbeats, latest_new_state} ;
2
3
use crate :: db:: { get_channel_by_id, insert_channel, insert_validator_messages, list_channels} ;
3
- use crate :: db:: event_aggregate:: { lastest_approve_state, latest_new_state, latest_heartbeats} ;
4
4
use crate :: success_response;
5
5
use crate :: Application ;
6
6
use crate :: ResponseError ;
@@ -11,7 +11,7 @@ use hex::FromHex;
11
11
use hyper:: { Body , Request , Response } ;
12
12
use primitives:: adapter:: Adapter ;
13
13
use primitives:: channel:: SpecValidator ;
14
- use primitives:: sentry:: { Event , SuccessResponse , LastApprovedResponse , LastApproved } ;
14
+ use primitives:: sentry:: { Event , LastApproved , LastApprovedResponse , SuccessResponse } ;
15
15
use primitives:: validator:: MessageTypes ;
16
16
use primitives:: { Channel , ChannelId } ;
17
17
use slog:: error;
@@ -99,43 +99,71 @@ pub async fn last_approved<A: Adapter>(
99
99
. extensions ( )
100
100
. get :: < RouteParams > ( )
101
101
. expect ( "request should have route params" ) ;
102
-
102
+
103
103
let channel_id = ChannelId :: from_hex ( route_params. index ( 0 ) ) ?;
104
104
let channel = get_channel_by_id ( & app. pool , & channel_id) . await ?. unwrap ( ) ;
105
105
106
106
let approve_state = lastest_approve_state ( & app. pool , & channel) . await ?;
107
107
if approve_state. is_none ( ) {
108
108
return Ok ( Response :: builder ( )
109
- . header ( "Content-type" , "application/json" )
110
- . body ( serde_json:: to_string ( & LastApprovedResponse { last_approved : None , heartbeats : None } ) ?. into ( ) )
111
- . unwrap ( ) )
109
+ . header ( "Content-type" , "application/json" )
110
+ . body (
111
+ serde_json:: to_string ( & LastApprovedResponse {
112
+ last_approved : None ,
113
+ heartbeats : None ,
114
+ } ) ?
115
+ . into ( ) ,
116
+ )
117
+ . unwrap ( ) ) ;
112
118
}
113
119
114
- let state_root = approve_state. as_ref ( ) . expect ( "value should be present" ) . msg . state_root . clone ( ) ;
120
+ let state_root = approve_state
121
+ . as_ref ( )
122
+ . expect ( "value should be present" )
123
+ . msg
124
+ . state_root
125
+ . clone ( ) ;
115
126
let new_state = latest_new_state ( & app. pool , & channel, & state_root) . await ?;
116
127
if new_state. is_none ( ) {
117
128
return Ok ( Response :: builder ( )
118
- . header ( "Content-type" , "application/json" )
119
- . body ( serde_json:: to_string ( & LastApprovedResponse { last_approved : None , heartbeats : None } ) ?. into ( ) )
120
- . unwrap ( ) )
129
+ . header ( "Content-type" , "application/json" )
130
+ . body (
131
+ serde_json:: to_string ( & LastApprovedResponse {
132
+ last_approved : None ,
133
+ heartbeats : None ,
134
+ } ) ?
135
+ . into ( ) ,
136
+ )
137
+ . unwrap ( ) ) ;
121
138
}
122
139
123
140
let query = serde_urlencoded:: from_str :: < LastApprovedQuery > ( & req. uri ( ) . query ( ) . unwrap_or ( "" ) ) ?;
124
- let validators = channel. spec . validators ;
141
+ let validators = channel. spec . validators ;
125
142
let channel_id = channel. id ;
126
143
let heartbeats = if query. with_heartbeat . is_some ( ) {
127
144
let result = try_join_all (
128
- validators. into_iter ( )
129
- . map ( |validator| latest_heartbeats ( & app. pool , & channel_id, validator. id . clone ( ) ) )
130
- ) . await ?;
145
+ validators
146
+ . into_iter ( )
147
+ . map ( |validator| latest_heartbeats ( & app. pool , & channel_id, validator. id . clone ( ) ) ) ,
148
+ )
149
+ . await ?;
131
150
Some ( result. into_iter ( ) . flatten ( ) . collect :: < Vec < _ > > ( ) )
132
151
} else {
133
152
None
134
153
} ;
135
154
136
155
Ok ( Response :: builder ( )
137
156
. header ( "Content-type" , "application/json" )
138
- . body ( serde_json:: to_string ( & & LastApprovedResponse { last_approved : Some ( LastApproved { new_state, approve_state } ) , heartbeats } ) ?. into ( ) )
157
+ . body (
158
+ serde_json:: to_string ( & & LastApprovedResponse {
159
+ last_approved : Some ( LastApproved {
160
+ new_state,
161
+ approve_state,
162
+ } ) ,
163
+ heartbeats,
164
+ } ) ?
165
+ . into ( ) ,
166
+ )
139
167
. unwrap ( ) )
140
168
}
141
169
@@ -239,9 +267,9 @@ mod channel_list {
239
267
#[ derive( Debug , Deserialize ) ]
240
268
#[ serde( rename_all = "camelCase" ) ]
241
269
pub ( super ) struct LastApprovedQuery {
242
- pub with_heartbeat : Option < String >
270
+ pub with_heartbeat : Option < String > ,
243
271
}
244
-
272
+
245
273
fn default_page ( ) -> u64 {
246
274
0
247
275
}
0 commit comments