Skip to content

Commit f049565

Browse files
committed
did requested changes from PR
1 parent 1db26af commit f049565

File tree

2 files changed

+22
-32
lines changed

2 files changed

+22
-32
lines changed

sentry/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ static CAMPAIGN_UPDATE_BY_ID: Lazy<Regex> = Lazy::new(|| {
7777
Regex::new(r"^/v5/campaign/0x([a-zA-Z0-9]{32})/?$").expect("The regex should be valid")
7878
});
7979
static CHANNEL_ALL_SPENDER_LIMITS: Lazy<Regex> = Lazy::new(|| {
80-
Regex::new(r"^/v5/channel/0x([a-zA-Z0-9]{64})/spender/all/?$").expect("The regex should be valid")
80+
Regex::new(r"^/v5/channel/0x([a-zA-Z0-9]{64})/spender/all/?$")
81+
.expect("The regex should be valid")
8182
});
8283

8384
#[derive(Debug, Clone)]
@@ -398,7 +399,6 @@ async fn channels_router<A: Adapter + 'static>(
398399
get_spender_limits(req, app).await
399400
} else if let (Some(caps), &Method::GET) = (CHANNEL_ALL_SPENDER_LIMITS.captures(&path), method)
400401
{
401-
402402
let param = RouteParams(vec![caps
403403
.get(1)
404404
.map_or("".to_string(), |m| m.as_str().to_string())]);

sentry/src/routes/channel.rs

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ pub async fn get_spender_limits<A: Adapter + 'static>(
312312
}
313313
};
314314

315-
let new_state = match get_corresponding_new_state(&app.pool, &channel).await {
315+
let new_state = match get_corresponding_new_state(&app.pool, &channel).await? {
316316
Some(new_state) => new_state,
317317
None => return spender_response_without_leaf(latest_spendable.deposit.total),
318318
};
@@ -346,9 +346,10 @@ pub async fn get_all_spender_limits<A: Adapter + 'static>(
346346
.expect("Request should have Channel")
347347
.to_owned();
348348

349-
let new_state = get_corresponding_new_state(&app.pool, &channel)
350-
.await
351-
.ok_or(ResponseError::NotFound)?;
349+
let new_state = match get_corresponding_new_state(&app.pool, &channel).await? {
350+
Some(new_state) => new_state,
351+
None => return Err(ResponseError::NotFound),
352+
};
352353

353354
let mut all_spender_limits: HashMap<Address, Spender> = HashMap::new();
354355

@@ -357,15 +358,22 @@ pub async fn get_all_spender_limits<A: Adapter + 'static>(
357358
let latest_spendable =
358359
match fetch_spendable(app.pool.clone(), spender_addr, &channel.id()).await? {
359360
Some(spendable) => spendable,
360-
None => return Err(ResponseError::NotFound),
361+
None => continue, // skipping spender if not found
361362
};
362363

363364
let total_deposited = latest_spendable.deposit.total;
364-
let spender_leaf = get_spender_leaf_for_spender(balance, &total_deposited);
365+
let total_spent = total_deposited.checked_sub(balance).ok_or_else(|| {
366+
ResponseError::FailedValidation("Couldn't calculate total_spent".to_string())
367+
})?;
368+
369+
let spender_leaf = SpenderLeaf {
370+
total_spent,
371+
// merkle_proof: [u8; 32], // TODO
372+
};
365373

366374
let spender_info = Spender {
367375
total_deposited,
368-
spender_leaf,
376+
spender_leaf: Some(spender_leaf),
369377
};
370378

371379
all_spender_limits.insert(*spender_addr, spender_info);
@@ -381,35 +389,17 @@ pub async fn get_all_spender_limits<A: Adapter + 'static>(
381389
async fn get_corresponding_new_state(
382390
pool: &DbPool,
383391
channel: &ChannelV5,
384-
) -> Option<MessageResponse<NewState<UncheckedState>>> {
385-
let approve_state = match latest_approve_state_v5(pool, channel).await.ok()? {
392+
) -> Result<Option<MessageResponse<NewState<UncheckedState>>>, ResponseError> {
393+
let approve_state = match latest_approve_state_v5(pool, channel).await? {
386394
Some(approve_state) => approve_state,
387-
None => return None,
395+
None => return Err(ResponseError::NotFound),
388396
};
389397

390398
let state_root = approve_state.msg.state_root.clone();
391399

392-
let new_state = latest_new_state_v5(pool, channel, &state_root).await.ok()?;
393-
394-
new_state
395-
}
396-
397-
fn get_spender_leaf_for_spender(
398-
spender_balance: &UnifiedNum,
399-
total_deposited: &UnifiedNum,
400-
) -> Option<SpenderLeaf> {
401-
let total_spent = match total_deposited.checked_sub(spender_balance) {
402-
Some(spent) => spent,
403-
None => return None,
404-
};
405-
406-
// Return
407-
let leaf = SpenderLeaf {
408-
total_spent,
409-
// merkle_proof: [u8; 32], // TODO
410-
};
400+
let new_state = latest_new_state_v5(pool, channel, &state_root).await?;
411401

412-
Some(leaf)
402+
Ok(new_state)
413403
}
414404

415405
#[cfg(test)]

0 commit comments

Comments
 (0)