@@ -166,6 +166,55 @@ pub async fn last_approved<C: Locked + 'static>(
166
166
. unwrap ( ) )
167
167
}
168
168
169
+ pub async fn last_approved_axum < C : Locked + ' static > (
170
+ Extension ( app) : Extension < Arc < Application < C > > > ,
171
+ Extension ( channel_context) : Extension < ChainOf < Channel > > ,
172
+ Qs ( query) : Qs < LastApprovedQuery > ,
173
+ ) -> Result < Json < LastApprovedResponse < UncheckedState > > , ResponseError > {
174
+ // get request Channel
175
+ let channel = channel_context. context ;
176
+
177
+ let default_response = Json ( LastApprovedResponse :: < UncheckedState > {
178
+ last_approved : None ,
179
+ heartbeats : None ,
180
+ } ) ;
181
+
182
+ let approve_state = match latest_approve_state ( & app. pool , & channel) . await ? {
183
+ Some ( approve_state) => approve_state,
184
+ None => return Ok ( default_response) ,
185
+ } ;
186
+
187
+ let state_root = approve_state. msg . state_root . clone ( ) ;
188
+
189
+ let new_state = latest_new_state ( & app. pool , & channel, & state_root) . await ?;
190
+ if new_state. is_none ( ) {
191
+ return Ok ( default_response) ;
192
+ }
193
+
194
+ let validators = vec ! [ channel. leader, channel. follower] ;
195
+ let channel_id = channel. id ( ) ;
196
+
197
+ let heartbeats = if query. with_heartbeat . unwrap_or_default ( ) {
198
+ let result = try_join_all (
199
+ validators
200
+ . iter ( )
201
+ . map ( |validator| latest_heartbeats ( & app. pool , & channel_id, validator) ) ,
202
+ )
203
+ . await ?;
204
+ Some ( result. into_iter ( ) . flatten ( ) . collect :: < Vec < _ > > ( ) )
205
+ } else {
206
+ None
207
+ } ;
208
+
209
+ Ok ( Json ( LastApprovedResponse {
210
+ last_approved : Some ( LastApproved {
211
+ new_state,
212
+ approve_state : Some ( approve_state) ,
213
+ } ) ,
214
+ heartbeats,
215
+ } ) )
216
+ }
217
+
169
218
/// This will make sure to insert/get the `Channel` from DB before attempting to create the `Spendable`
170
219
async fn create_or_update_spendable_document < A : Locked > (
171
220
adapter : & Adapter < A > ,
@@ -1220,8 +1269,9 @@ mod test {
1220
1269
1221
1270
// Testing for no accounting yet
1222
1271
{
1223
- let res = get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) )
1224
- . await ;
1272
+ let res =
1273
+ get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) )
1274
+ . await ;
1225
1275
assert ! ( res. is_ok( ) ) ;
1226
1276
1227
1277
let accounting_response = res. unwrap ( ) ;
@@ -1247,8 +1297,9 @@ mod test {
1247
1297
. await
1248
1298
. expect ( "should spend" ) ;
1249
1299
1250
- let res = get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) )
1251
- . await ;
1300
+ let res =
1301
+ get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) )
1302
+ . await ;
1252
1303
assert ! ( res. is_ok( ) ) ;
1253
1304
1254
1305
let accounting_response = res. unwrap ( ) ;
@@ -1282,8 +1333,9 @@ mod test {
1282
1333
. await
1283
1334
. expect ( "should spend" ) ;
1284
1335
1285
- let res = get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) )
1286
- . await ;
1336
+ let res =
1337
+ get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) )
1338
+ . await ;
1287
1339
assert ! ( res. is_ok( ) ) ;
1288
1340
1289
1341
let accounting_response = res. unwrap ( ) ;
@@ -1304,8 +1356,9 @@ mod test {
1304
1356
. await
1305
1357
. expect ( "should spend" ) ;
1306
1358
1307
- let res = get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) )
1308
- . await ;
1359
+ let res =
1360
+ get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) )
1361
+ . await ;
1309
1362
let expected = ResponseError :: FailedValidation (
1310
1363
"Earners sum is not equal to spenders sum for channel" . to_string ( ) ,
1311
1364
) ;
@@ -1345,8 +1398,8 @@ mod test {
1345
1398
. await ;
1346
1399
assert ! ( res. is_ok( ) ) ;
1347
1400
1348
- let res = get_accounting_for_channel_axum ( app . clone ( ) , Extension ( channel_context . clone ( ) ) )
1349
- . await ;
1401
+ let res =
1402
+ get_accounting_for_channel_axum ( app . clone ( ) , Extension ( channel_context . clone ( ) ) ) . await ;
1350
1403
assert ! ( res. is_ok( ) ) ;
1351
1404
1352
1405
let accounting_response = res. unwrap ( ) ;
@@ -1372,8 +1425,8 @@ mod test {
1372
1425
. await
1373
1426
. expect ( "should spend" ) ;
1374
1427
1375
- let res = get_accounting_for_channel_axum ( app . clone ( ) , Extension ( channel_context . clone ( ) ) )
1376
- . await ;
1428
+ let res =
1429
+ get_accounting_for_channel_axum ( app . clone ( ) , Extension ( channel_context . clone ( ) ) ) . await ;
1377
1430
assert ! ( res. is_ok( ) ) ;
1378
1431
1379
1432
let accounting_response = res. unwrap ( ) ;
@@ -1388,8 +1441,8 @@ mod test {
1388
1441
. await ;
1389
1442
assert ! ( res. is_ok( ) ) ;
1390
1443
1391
- let res = get_accounting_for_channel_axum ( app . clone ( ) , Extension ( channel_context . clone ( ) ) )
1392
- . await ;
1444
+ let res =
1445
+ get_accounting_for_channel_axum ( app . clone ( ) , Extension ( channel_context . clone ( ) ) ) . await ;
1393
1446
assert ! ( res. is_ok( ) ) ;
1394
1447
1395
1448
let accounting_response = res. unwrap ( ) ;
0 commit comments