Skip to content

Commit d4ea993

Browse files
committed
did todo's in the code
1 parent e07992b commit d4ea993

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

primitives/src/sentry.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub mod message {
4242

4343
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
4444
#[serde(try_from = "MessageTypes", into = "MessageTypes")]
45-
pub struct Message<T: Type>(T);
45+
pub struct Message<T: Type>(pub T);
4646

4747
impl<T: Type> Message<T> {
4848
pub fn new(message: T) -> Self {

sentry/src/routes/channel.rs

+27-29
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ use hex::FromHex;
1313
use hyper::{Body, Request, Response};
1414
use primitives::{
1515
adapter::Adapter,
16-
balances::UncheckedState,
16+
balances::{CheckedState, UncheckedState},
1717
channel_v5::Channel as ChannelV5,
1818
config::TokenInfo,
1919
sentry::{
2020
channel_list::{ChannelListQuery, LastApprovedQuery},
21-
AllSpendersResponse, LastApproved, LastApprovedResponse, MessageResponse, Pagination,
22-
SpenderResponse, SuccessResponse,
21+
AllSpendersResponse, LastApproved, LastApprovedResponse, Pagination, SpenderResponse,
22+
SuccessResponse,
2323
},
2424
spender::{Deposit, Spendable, Spender, SpenderLeaf},
2525
validator::{MessageTypes, NewState},
2626
Address, Channel, ChannelId, UnifiedNum,
2727
};
28-
use slog::error;
28+
use slog::{error, Logger};
2929
use std::{collections::HashMap, str::FromStr};
3030
use tokio_postgres::error::SqlState;
3131

@@ -312,14 +312,12 @@ 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, &app.logger, &channel).await? {
316316
Some(new_state) => new_state,
317317
None => return spender_response_without_leaf(latest_spendable.deposit.total),
318318
};
319319

320-
let new_state_checked = new_state.msg.into_inner().try_checked()?;
321-
322-
let total_spent = new_state_checked.balances.spenders.get(&spender);
320+
let total_spent = new_state.balances.spenders.get(&spender);
323321

324322
let spender_leaf = total_spent.map(|total_spent| SpenderLeaf {
325323
total_spent: *total_spent,
@@ -346,7 +344,7 @@ pub async fn get_all_spender_limits<A: Adapter + 'static>(
346344
.expect("Request should have Channel")
347345
.to_owned();
348346

349-
let new_state = get_corresponding_new_state(&app.pool, &channel).await?;
347+
let new_state = get_corresponding_new_state(&app.pool, &app.logger, &channel).await?;
350348

351349
let mut all_spender_limits: HashMap<Address, Spender> = HashMap::new();
352350

@@ -356,21 +354,16 @@ pub async fn get_all_spender_limits<A: Adapter + 'static>(
356354
for spendable in all_spendables {
357355
let spender = spendable.spender;
358356
let spender_leaf = match new_state {
359-
Some(ref new_state) => new_state
360-
.msg
361-
.balances
362-
.spenders
363-
.get(&spender)
364-
.map(|balance| {
365-
SpenderLeaf {
366-
total_spent: spendable
367-
.deposit
368-
.total
369-
.checked_sub(balance)
370-
.unwrap_or_default(),
371-
// merkle_proof: [u8; 32], // TODO
372-
}
373-
}),
357+
Some(ref new_state) => new_state.balances.spenders.get(&spender).map(|balance| {
358+
SpenderLeaf {
359+
total_spent: spendable
360+
.deposit
361+
.total
362+
.checked_sub(balance)
363+
.unwrap_or_default(),
364+
// merkle_proof: [u8; 32], // TODO
365+
}
366+
}),
374367
None => None,
375368
};
376369

@@ -397,8 +390,9 @@ pub async fn get_all_spender_limits<A: Adapter + 'static>(
397390

398391
async fn get_corresponding_new_state(
399392
pool: &DbPool,
393+
logger: &Logger,
400394
channel: &ChannelV5,
401-
) -> Result<Option<MessageResponse<NewState<UncheckedState>>>, ResponseError> {
395+
) -> Result<Option<NewState<CheckedState>>, ResponseError> {
402396
let approve_state = match latest_approve_state_v5(pool, channel).await? {
403397
Some(approve_state) => approve_state,
404398
None => return Ok(None),
@@ -407,11 +401,15 @@ async fn get_corresponding_new_state(
407401
let state_root = approve_state.msg.state_root.clone();
408402

409403
let new_state = match latest_new_state_v5(pool, channel, &state_root).await? {
410-
// TODO: Since it's an approved NewState, then it's safe to make sure it's in `CheckedState`, or if it's not - log the error that the balances are not aligned with the fact it's an Approved NewState and return ResponseError::BadRequest
411-
Some(new_state) => Ok(Some(new_state)),
412-
404+
Some(new_state) => {
405+
let new_state = new_state.msg.into_inner().try_checked().map_err(|err| {
406+
error!(&logger, "Balances are not aligned in an approved NewState: {}", &err; "module" => "get_spender_limits");
407+
ResponseError::BadRequest("Balances are not aligned in an approved NewState".to_string())
408+
})?;
409+
Ok(Some(new_state))
410+
}
413411
None => {
414-
// TODO: Log the error since this should never happen and its crucial to the Channel
412+
error!(&logger, "{}", "Fatal error! The NewState for the last ApproveState was not found"; "module" => "get_spender_limits");
415413
return Err(ResponseError::BadRequest(
416414
"Fatal error! The NewState for the last ApproveState was not found".to_string(),
417415
));

0 commit comments

Comments
 (0)