@@ -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 > ,
@@ -610,6 +659,40 @@ pub async fn get_accounting_for_channel<C: Locked + 'static>(
610
659
Ok ( success_response ( serde_json:: to_string ( & res) ?) )
611
660
}
612
661
662
+ pub async fn get_accounting_for_channel_axum < C : Locked + ' static > (
663
+ Extension ( app) : Extension < Arc < Application < C > > > ,
664
+ Extension ( channel_context) : Extension < ChainOf < Channel > > ,
665
+ ) -> Result < Json < AccountingResponse < CheckedState > > , ResponseError > {
666
+ let channel = channel_context. context ;
667
+
668
+ let accountings = get_all_accountings_for_channel ( app. pool . clone ( ) , channel. id ( ) ) . await ?;
669
+
670
+ let mut unchecked_balances: Balances < UncheckedState > = Balances :: default ( ) ;
671
+
672
+ for accounting in accountings {
673
+ match accounting. side {
674
+ Side :: Earner => unchecked_balances
675
+ . earners
676
+ . insert ( accounting. address , accounting. amount ) ,
677
+ Side :: Spender => unchecked_balances
678
+ . spenders
679
+ . insert ( accounting. address , accounting. amount ) ,
680
+ } ;
681
+ }
682
+
683
+ let balances = match unchecked_balances. check ( ) {
684
+ Ok ( balances) => balances,
685
+ Err ( error) => {
686
+ error ! ( & app. logger, "{}" , & error; "module" => "channel_accounting" ) ;
687
+ return Err ( ResponseError :: FailedValidation (
688
+ "Earners sum is not equal to spenders sum for channel" . to_string ( ) ,
689
+ ) ) ;
690
+ }
691
+ } ;
692
+
693
+ Ok ( Json ( AccountingResponse :: < CheckedState > { balances } ) )
694
+ }
695
+
613
696
pub async fn channel_payout_axum < C : Locked + ' static > (
614
697
Extension ( app) : Extension < Arc < Application < C > > > ,
615
698
Extension ( channel_context) : Extension < ChainOf < Channel > > ,
@@ -1087,7 +1170,6 @@ mod test {
1087
1170
ethereum:: test_util:: { GANACHE_INFO_1 , GANACHE_INFO_1337 } ,
1088
1171
primitives:: Deposit as AdapterDeposit ,
1089
1172
} ;
1090
- use hyper:: StatusCode ;
1091
1173
use primitives:: {
1092
1174
channel:: Nonce ,
1093
1175
test_util:: {
@@ -1170,19 +1252,11 @@ mod test {
1170
1252
assert_eq ! ( updated_spendable. spender, * CREATOR ) ;
1171
1253
}
1172
1254
1173
- async fn res_to_accounting_response ( res : Response < Body > ) -> AccountingResponse < CheckedState > {
1174
- let json = hyper:: body:: to_bytes ( res. into_body ( ) )
1175
- . await
1176
- . expect ( "Should get json" ) ;
1177
-
1178
- let accounting_response: AccountingResponse < CheckedState > =
1179
- serde_json:: from_slice ( & json) . expect ( "Should get AccountingResponse" ) ;
1180
- accounting_response
1181
- }
1182
-
1183
1255
#[ tokio:: test]
1184
1256
async fn get_accountings_for_channel ( ) {
1185
- let app = setup_dummy_app ( ) . await ;
1257
+ let app_guard = setup_dummy_app ( ) . await ;
1258
+
1259
+ let app = Extension ( Arc :: new ( app_guard. app . clone ( ) ) ) ;
1186
1260
let channel_context = app
1187
1261
. config
1188
1262
. find_chain_of ( DUMMY_CAMPAIGN . channel . token )
@@ -1192,20 +1266,16 @@ mod test {
1192
1266
insert_channel ( & app. pool , & channel_context)
1193
1267
. await
1194
1268
. expect ( "should insert channel" ) ;
1195
- let build_request = |channel_context : & ChainOf < Channel > | {
1196
- Request :: builder ( )
1197
- . extension ( channel_context. clone ( ) )
1198
- . body ( Body :: empty ( ) )
1199
- . expect ( "Should build Request" )
1200
- } ;
1269
+
1201
1270
// Testing for no accounting yet
1202
1271
{
1203
- let res = get_accounting_for_channel ( build_request ( & channel_context) , & app)
1204
- . await
1205
- . expect ( "should get response" ) ;
1206
- assert_eq ! ( StatusCode :: OK , res. status( ) ) ;
1272
+ let res =
1273
+ get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) )
1274
+ . await ;
1275
+ assert ! ( res. is_ok( ) ) ;
1276
+
1277
+ let accounting_response = res. unwrap ( ) ;
1207
1278
1208
- let accounting_response = res_to_accounting_response ( res) . await ;
1209
1279
assert_eq ! ( accounting_response. balances. earners. len( ) , 0 ) ;
1210
1280
assert_eq ! ( accounting_response. balances. spenders. len( ) , 0 ) ;
1211
1281
}
@@ -1227,12 +1297,12 @@ mod test {
1227
1297
. await
1228
1298
. expect ( "should spend" ) ;
1229
1299
1230
- let res = get_accounting_for_channel ( build_request ( & channel_context ) , & app )
1231
- . await
1232
- . expect ( "should get response" ) ;
1233
- assert_eq ! ( StatusCode :: OK , res. status ( ) ) ;
1300
+ let res =
1301
+ get_accounting_for_channel_axum ( app . clone ( ) , Extension ( channel_context . clone ( ) ) )
1302
+ . await ;
1303
+ assert ! ( res. is_ok ( ) ) ;
1234
1304
1235
- let accounting_response = res_to_accounting_response ( res) . await ;
1305
+ let accounting_response = res. unwrap ( ) ;
1236
1306
1237
1307
assert_eq ! ( balances, accounting_response. balances) ;
1238
1308
}
@@ -1263,15 +1333,12 @@ mod test {
1263
1333
. await
1264
1334
. expect ( "should spend" ) ;
1265
1335
1266
- let res = get_accounting_for_channel (
1267
- build_request ( & channel_context. clone ( ) . with ( second_channel) ) ,
1268
- & app,
1269
- )
1270
- . await
1271
- . expect ( "should get response" ) ;
1272
- assert_eq ! ( StatusCode :: OK , res. status( ) ) ;
1336
+ let res =
1337
+ get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) )
1338
+ . await ;
1339
+ assert ! ( res. is_ok( ) ) ;
1273
1340
1274
- let accounting_response = res_to_accounting_response ( res) . await ;
1341
+ let accounting_response = res. unwrap ( ) ;
1275
1342
1276
1343
assert_eq ! ( balances, accounting_response. balances)
1277
1344
}
@@ -1289,7 +1356,9 @@ mod test {
1289
1356
. await
1290
1357
. expect ( "should spend" ) ;
1291
1358
1292
- let res = get_accounting_for_channel ( build_request ( & channel_context) , & app) . await ;
1359
+ let res =
1360
+ get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) )
1361
+ . await ;
1293
1362
let expected = ResponseError :: FailedValidation (
1294
1363
"Earners sum is not equal to spenders sum for channel" . to_string ( ) ,
1295
1364
) ;
@@ -1320,13 +1389,6 @@ mod test {
1320
1389
. await
1321
1390
. expect ( "should insert channel" ) ;
1322
1391
1323
- let get_accounting_request = |channel_context : & ChainOf < Channel > | {
1324
- Request :: builder ( )
1325
- . extension ( channel_context. clone ( ) )
1326
- . body ( Body :: empty ( ) )
1327
- . expect ( "Should build Request" )
1328
- } ;
1329
-
1330
1392
// Calling with non existent accounting
1331
1393
let res = add_spender_leaf_axum (
1332
1394
app. clone ( ) ,
@@ -1336,12 +1398,11 @@ mod test {
1336
1398
. await ;
1337
1399
assert ! ( res. is_ok( ) ) ;
1338
1400
1339
- let res = get_accounting_for_channel ( get_accounting_request ( & channel_context) , & app)
1340
- . await
1341
- . expect ( "should get response" ) ;
1342
- assert_eq ! ( StatusCode :: OK , res. status( ) ) ;
1401
+ let res =
1402
+ get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) ) . await ;
1403
+ assert ! ( res. is_ok( ) ) ;
1343
1404
1344
- let accounting_response = res_to_accounting_response ( res) . await ;
1405
+ let accounting_response = res. unwrap ( ) ;
1345
1406
1346
1407
// Making sure a new entry has been created
1347
1408
assert_eq ! (
@@ -1364,12 +1425,11 @@ mod test {
1364
1425
. await
1365
1426
. expect ( "should spend" ) ;
1366
1427
1367
- let res = get_accounting_for_channel ( get_accounting_request ( & channel_context) , & app)
1368
- . await
1369
- . expect ( "should get response" ) ;
1370
- assert_eq ! ( StatusCode :: OK , res. status( ) ) ;
1428
+ let res =
1429
+ get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) ) . await ;
1430
+ assert ! ( res. is_ok( ) ) ;
1371
1431
1372
- let accounting_response = res_to_accounting_response ( res) . await ;
1432
+ let accounting_response = res. unwrap ( ) ;
1373
1433
1374
1434
assert_eq ! ( balances, accounting_response. balances) ;
1375
1435
@@ -1381,12 +1441,11 @@ mod test {
1381
1441
. await ;
1382
1442
assert ! ( res. is_ok( ) ) ;
1383
1443
1384
- let res = get_accounting_for_channel ( get_accounting_request ( & channel_context) , & app)
1385
- . await
1386
- . expect ( "should get response" ) ;
1387
- assert_eq ! ( StatusCode :: OK , res. status( ) ) ;
1444
+ let res =
1445
+ get_accounting_for_channel_axum ( app. clone ( ) , Extension ( channel_context. clone ( ) ) ) . await ;
1446
+ assert ! ( res. is_ok( ) ) ;
1388
1447
1389
- let accounting_response = res_to_accounting_response ( res) . await ;
1448
+ let accounting_response = res. unwrap ( ) ;
1390
1449
1391
1450
// Balances shouldn't change
1392
1451
assert_eq ! (
0 commit comments