Skip to content

Commit 55cee07

Browse files
committed
channel get accounting is with axum + tests
1 parent 1db431f commit 55cee07

File tree

2 files changed

+71
-59
lines changed

2 files changed

+71
-59
lines changed

sentry/src/routes/channel.rs

Lines changed: 65 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,40 @@ pub async fn get_accounting_for_channel<C: Locked + 'static>(
610610
Ok(success_response(serde_json::to_string(&res)?))
611611
}
612612

613+
pub async fn get_accounting_for_channel_axum<C: Locked + 'static>(
614+
Extension(app): Extension<Arc<Application<C>>>,
615+
Extension(channel_context): Extension<ChainOf<Channel>>,
616+
) -> Result<Json<AccountingResponse<CheckedState>>, ResponseError> {
617+
let channel = channel_context.context;
618+
619+
let accountings = get_all_accountings_for_channel(app.pool.clone(), channel.id()).await?;
620+
621+
let mut unchecked_balances: Balances<UncheckedState> = Balances::default();
622+
623+
for accounting in accountings {
624+
match accounting.side {
625+
Side::Earner => unchecked_balances
626+
.earners
627+
.insert(accounting.address, accounting.amount),
628+
Side::Spender => unchecked_balances
629+
.spenders
630+
.insert(accounting.address, accounting.amount),
631+
};
632+
}
633+
634+
let balances = match unchecked_balances.check() {
635+
Ok(balances) => balances,
636+
Err(error) => {
637+
error!(&app.logger, "{}", &error; "module" => "channel_accounting");
638+
return Err(ResponseError::FailedValidation(
639+
"Earners sum is not equal to spenders sum for channel".to_string(),
640+
));
641+
}
642+
};
643+
644+
Ok(Json(AccountingResponse::<CheckedState> { balances }))
645+
}
646+
613647
pub async fn channel_payout_axum<C: Locked + 'static>(
614648
Extension(app): Extension<Arc<Application<C>>>,
615649
Extension(channel_context): Extension<ChainOf<Channel>>,
@@ -1087,7 +1121,6 @@ mod test {
10871121
ethereum::test_util::{GANACHE_INFO_1, GANACHE_INFO_1337},
10881122
primitives::Deposit as AdapterDeposit,
10891123
};
1090-
use hyper::StatusCode;
10911124
use primitives::{
10921125
channel::Nonce,
10931126
test_util::{
@@ -1170,19 +1203,11 @@ mod test {
11701203
assert_eq!(updated_spendable.spender, *CREATOR);
11711204
}
11721205

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-
11831206
#[tokio::test]
11841207
async fn get_accountings_for_channel() {
1185-
let app = setup_dummy_app().await;
1208+
let app_guard = setup_dummy_app().await;
1209+
1210+
let app = Extension(Arc::new(app_guard.app.clone()));
11861211
let channel_context = app
11871212
.config
11881213
.find_chain_of(DUMMY_CAMPAIGN.channel.token)
@@ -1192,20 +1217,15 @@ mod test {
11921217
insert_channel(&app.pool, &channel_context)
11931218
.await
11941219
.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-
};
1220+
12011221
// Testing for no accounting yet
12021222
{
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());
1223+
let res = get_accounting_for_channel_axum(app.clone(), Extension(channel_context.clone()))
1224+
.await;
1225+
assert!(res.is_ok());
1226+
1227+
let accounting_response = res.unwrap();
12071228

1208-
let accounting_response = res_to_accounting_response(res).await;
12091229
assert_eq!(accounting_response.balances.earners.len(), 0);
12101230
assert_eq!(accounting_response.balances.spenders.len(), 0);
12111231
}
@@ -1227,12 +1247,11 @@ mod test {
12271247
.await
12281248
.expect("should spend");
12291249

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());
1250+
let res = get_accounting_for_channel_axum(app.clone(), Extension(channel_context.clone()))
1251+
.await;
1252+
assert!(res.is_ok());
12341253

1235-
let accounting_response = res_to_accounting_response(res).await;
1254+
let accounting_response = res.unwrap();
12361255

12371256
assert_eq!(balances, accounting_response.balances);
12381257
}
@@ -1263,15 +1282,11 @@ mod test {
12631282
.await
12641283
.expect("should spend");
12651284

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());
1285+
let res = get_accounting_for_channel_axum(app.clone(), Extension(channel_context.clone()))
1286+
.await;
1287+
assert!(res.is_ok());
12731288

1274-
let accounting_response = res_to_accounting_response(res).await;
1289+
let accounting_response = res.unwrap();
12751290

12761291
assert_eq!(balances, accounting_response.balances)
12771292
}
@@ -1289,7 +1304,8 @@ mod test {
12891304
.await
12901305
.expect("should spend");
12911306

1292-
let res = get_accounting_for_channel(build_request(&channel_context), &app).await;
1307+
let res = get_accounting_for_channel_axum(app.clone(), Extension(channel_context.clone()))
1308+
.await;
12931309
let expected = ResponseError::FailedValidation(
12941310
"Earners sum is not equal to spenders sum for channel".to_string(),
12951311
);
@@ -1320,13 +1336,6 @@ mod test {
13201336
.await
13211337
.expect("should insert channel");
13221338

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-
13301339
// Calling with non existent accounting
13311340
let res = add_spender_leaf_axum(
13321341
app.clone(),
@@ -1336,12 +1345,11 @@ mod test {
13361345
.await;
13371346
assert!(res.is_ok());
13381347

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());
1348+
let res = get_accounting_for_channel_axum(app.clone(), Extension(channel_context.clone()))
1349+
.await;
1350+
assert!(res.is_ok());
13431351

1344-
let accounting_response = res_to_accounting_response(res).await;
1352+
let accounting_response = res.unwrap();
13451353

13461354
// Making sure a new entry has been created
13471355
assert_eq!(
@@ -1364,12 +1372,11 @@ mod test {
13641372
.await
13651373
.expect("should spend");
13661374

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());
1375+
let res = get_accounting_for_channel_axum(app.clone(), Extension(channel_context.clone()))
1376+
.await;
1377+
assert!(res.is_ok());
13711378

1372-
let accounting_response = res_to_accounting_response(res).await;
1379+
let accounting_response = res.unwrap();
13731380

13741381
assert_eq!(balances, accounting_response.balances);
13751382

@@ -1381,12 +1388,11 @@ mod test {
13811388
.await;
13821389
assert!(res.is_ok());
13831390

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());
1391+
let res = get_accounting_for_channel_axum(app.clone(), Extension(channel_context.clone()))
1392+
.await;
1393+
assert!(res.is_ok());
13881394

1389-
let accounting_response = res_to_accounting_response(res).await;
1395+
let accounting_response = res.unwrap();
13901396

13911397
// Balances shouldn't change
13921398
assert_eq!(

sentry/src/routes/routers.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use crate::{
3030
add_spender_leaf, add_spender_leaf_axum, channel_dummy_deposit, channel_list,
3131
channel_payout, get_accounting_for_channel, get_all_spender_limits,
3232
get_all_spender_limits_axum, get_spender_limits, get_spender_limits_axum,
33+
get_accounting_for_channel_axum,
3334
last_approved,
3435
validator_message::{
3536
create_validator_messages, extract_params, list_validator_messages,
@@ -154,6 +155,11 @@ pub fn channels_router_axum<C: Locked + 'static>() -> Router {
154155
post(channel_payout_axum::<C>)
155156
.route_layer(middleware::from_fn(authentication_required::<C, _>)),
156157
)
158+
.route(
159+
"/accounting",
160+
get(get_accounting_for_channel_axum::<C>)
161+
.route_layer(middleware::from_fn(authentication_required::<C, _>)),
162+
)
157163
.nest("/spender", spender_routes)
158164
.layer(
159165
// keeps the order from top to bottom!

0 commit comments

Comments
 (0)