Skip to content

Commit 10d6c9c

Browse files
committed
changed GET /v5/channel/:id/last-approved to use axum
1 parent 55cee07 commit 10d6c9c

File tree

2 files changed

+72
-23
lines changed

2 files changed

+72
-23
lines changed

sentry/src/routes/channel.rs

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,55 @@ pub async fn last_approved<C: Locked + 'static>(
166166
.unwrap())
167167
}
168168

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+
169218
/// This will make sure to insert/get the `Channel` from DB before attempting to create the `Spendable`
170219
async fn create_or_update_spendable_document<A: Locked>(
171220
adapter: &Adapter<A>,
@@ -1220,8 +1269,9 @@ mod test {
12201269

12211270
// Testing for no accounting yet
12221271
{
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;
12251275
assert!(res.is_ok());
12261276

12271277
let accounting_response = res.unwrap();
@@ -1247,8 +1297,9 @@ mod test {
12471297
.await
12481298
.expect("should spend");
12491299

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;
12521303
assert!(res.is_ok());
12531304

12541305
let accounting_response = res.unwrap();
@@ -1282,8 +1333,9 @@ mod test {
12821333
.await
12831334
.expect("should spend");
12841335

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;
12871339
assert!(res.is_ok());
12881340

12891341
let accounting_response = res.unwrap();
@@ -1304,8 +1356,9 @@ mod test {
13041356
.await
13051357
.expect("should spend");
13061358

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;
13091362
let expected = ResponseError::FailedValidation(
13101363
"Earners sum is not equal to spenders sum for channel".to_string(),
13111364
);
@@ -1345,8 +1398,8 @@ mod test {
13451398
.await;
13461399
assert!(res.is_ok());
13471400

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;
13501403
assert!(res.is_ok());
13511404

13521405
let accounting_response = res.unwrap();
@@ -1372,8 +1425,8 @@ mod test {
13721425
.await
13731426
.expect("should spend");
13741427

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;
13771430
assert!(res.is_ok());
13781431

13791432
let accounting_response = res.unwrap();
@@ -1388,8 +1441,8 @@ mod test {
13881441
.await;
13891442
assert!(res.is_ok());
13901443

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;
13931446
assert!(res.is_ok());
13941447

13951448
let accounting_response = res.unwrap();

sentry/src/routes/routers.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ use crate::{
2828
campaign::{campaign_list, create_campaign, update_campaign},
2929
channel::{
3030
add_spender_leaf, add_spender_leaf_axum, channel_dummy_deposit, channel_list,
31-
channel_payout, get_accounting_for_channel, get_all_spender_limits,
32-
get_all_spender_limits_axum, get_spender_limits, get_spender_limits_axum,
33-
get_accounting_for_channel_axum,
34-
last_approved,
31+
channel_payout, get_accounting_for_channel, get_accounting_for_channel_axum,
32+
get_all_spender_limits, get_all_spender_limits_axum, get_spender_limits,
33+
get_spender_limits_axum, last_approved, last_approved_axum,
3534
validator_message::{
3635
create_validator_messages, extract_params, list_validator_messages,
3736
},
@@ -155,11 +154,8 @@ pub fn channels_router_axum<C: Locked + 'static>() -> Router {
155154
post(channel_payout_axum::<C>)
156155
.route_layer(middleware::from_fn(authentication_required::<C, _>)),
157156
)
158-
.route(
159-
"/accounting",
160-
get(get_accounting_for_channel_axum::<C>)
161-
.route_layer(middleware::from_fn(authentication_required::<C, _>)),
162-
)
157+
.route("/accounting", get(get_accounting_for_channel_axum::<C>))
158+
.route("/last-approved", get(last_approved_axum::<C>))
163159
.nest("/spender", spender_routes)
164160
.layer(
165161
// keeps the order from top to bottom!

0 commit comments

Comments
 (0)