Skip to content

Commit 717f857

Browse files
committed
fix: /last-approved route
1 parent 6887a68 commit 717f857

File tree

8 files changed

+30
-47
lines changed

8 files changed

+30
-47
lines changed

primitives/src/big_num.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use num_derive::{Num, NumOps, One, Zero};
1010
use serde::{Deserialize, Deserializer, Serialize, Serializer};
1111

1212
#[derive(
13-
Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, NumOps, One, Zero, Num,
13+
Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, NumOps, One, Zero, Num, Default
1414
)]
1515
pub struct BigNum(
1616
#[serde(

sentry/src/analytics_recorder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ pub fn get_payout(channel: &Channel, event: &Event) -> BigNum {
1717
}
1818
}
1919

20-
BigNum::from(0)
20+
Default::default()
2121
}
22-
_ => BigNum::from(0),
22+
_ => Default::default(),
2323
}
2424
}
2525

sentry/src/db/analytics.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use primitives::analytics::{AnalyticsData, AnalyticsQuery, ANALYTICS_QUERY_LIMIT
77
use primitives::sentry::{AdvancedAnalyticsResponse, ChannelReport, PublisherReport};
88
use bb8_postgres::tokio_postgres::types::{ToSql};
99
use primitives::{ChannelId, ValidatorId};
10-
use redis;
10+
use redis::cmd;
1111
use redis::aio::MultiplexedConnection;
1212
use std::collections::HashMap;
1313
use std::error::Error;
@@ -157,15 +157,15 @@ fn get_time_frame(timeframe: &str) -> (i64, i64) {
157157
}
158158

159159
async fn stat_pair(
160-
conn: MultiplexedConnection,
160+
mut conn: MultiplexedConnection,
161161
key: &str,
162162
) -> Result<HashMap<String, f64>, Box<dyn Error>> {
163-
let data = redis::cmd("ZRANGE")
163+
let data = cmd("ZRANGE")
164164
.arg(key)
165165
.arg(0 as u64)
166166
.arg(-1 as i64)
167167
.arg("WITHSCORES")
168-
.query_async::<_, Vec<String>>(&mut conn.clone())
168+
.query_async::<_, Vec<String>>(&mut conn)
169169
.await?;
170170

171171
Ok(data

sentry/src/event_aggregator.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ use std::time::Duration;
2121
use tokio::time::delay_for;
2222

2323
lazy_static! {
24-
pub static ref ANALYTICS_RECORDER: String =
25-
env::var("ANALYTICS_RECORDER").unwrap_or_else(|_| "false".to_string());
24+
pub static ref ANALYTICS_RECORDER: Option<String> = env::var("ANALYTICS_RECORDER").ok();
2625
}
2726

2827
#[derive(Debug)]
@@ -140,7 +139,7 @@ impl EventAggregator {
140139
.iter()
141140
.for_each(|ev| event_reducer::reduce(&record.channel, &mut record.aggregate, ev));
142141

143-
if ANALYTICS_RECORDER.ne(&"false".to_string()) {
142+
if ANALYTICS_RECORDER.is_some() {
144143
let logger = app.logger.clone();
145144
tokio::spawn(analytics_recorder::record(
146145
redis.clone(),

sentry/src/routes/analytics.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ pub async fn process_analytics<A: Adapter>(
8989

9090
let segment_channel = query
9191
.segment_by_channel
92-
.clone()
93-
.map(|_| true)
94-
.unwrap_or_else(|| false);
92+
.is_some();
93+
9594
let limit = query.limit;
9695

9796
let aggr = get_analytics(

sentry/src/routes/channel.rs

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -103,42 +103,30 @@ pub async fn last_approved<A: Adapter>(
103103
let channel_id = ChannelId::from_hex(route_params.index(0))?;
104104
let channel = get_channel_by_id(&app.pool, &channel_id).await?.unwrap();
105105

106-
let approve_state = lastest_approve_state(&app.pool, &channel).await?;
107-
if approve_state.is_none() {
108-
return Ok(Response::builder()
109-
.header("Content-type", "application/json")
110-
.body(
111-
serde_json::to_string(&LastApprovedResponse {
112-
last_approved: None,
113-
heartbeats: None,
114-
})?
115-
.into(),
116-
)
117-
.unwrap());
118-
}
106+
let default_response = Response::builder()
107+
.header("Content-type", "application/json")
108+
.body(
109+
serde_json::to_string(&LastApprovedResponse {
110+
last_approved: None,
111+
heartbeats: None,
112+
})?
113+
.into(),
114+
)
115+
.expect("should build response");
116+
117+
let approve_state = match lastest_approve_state(&app.pool, &channel).await? {
118+
Some(approve_state) => approve_state,
119+
None => return Ok(default_response)
120+
};
119121

120-
let state_root = match approve_state
121-
.as_ref()
122-
.expect("value should be present")
123-
.msg
124-
.clone()
125-
{
122+
let state_root = match approve_state.msg.clone() {
126123
MessageTypes::ApproveState(approve_state) => approve_state.state_root,
127124
_ => return Err(ResponseError::BadRequest("invalid request".to_string())),
128125
};
129126

130127
let new_state = latest_new_state(&app.pool, &channel, &state_root).await?;
131128
if new_state.is_none() {
132-
return Ok(Response::builder()
133-
.header("Content-type", "application/json")
134-
.body(
135-
serde_json::to_string(&LastApprovedResponse {
136-
last_approved: None,
137-
heartbeats: None,
138-
})?
139-
.into(),
140-
)
141-
.unwrap());
129+
return Ok(default_response);
142130
}
143131

144132
let query = serde_urlencoded::from_str::<LastApprovedQuery>(&req.uri().query().unwrap_or(""))?;
@@ -159,10 +147,10 @@ pub async fn last_approved<A: Adapter>(
159147
Ok(Response::builder()
160148
.header("Content-type", "application/json")
161149
.body(
162-
serde_json::to_string(&&LastApprovedResponse {
150+
serde_json::to_string(&LastApprovedResponse {
163151
last_approved: Some(LastApproved {
164152
new_state,
165-
approve_state,
153+
approve_state: Some(approve_state),
166154
}),
167155
heartbeats,
168156
})?

validator.env.dist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,3 @@ DUMMY_IDENTITY=ce07CbB7e054514D590a0262C93070D838bFBA2e
44
# for Ethereum adapter
55
#KEYSTORE_FILE="/app/keystore.json"
66
#KEYSTORE_PWD="adexvalidator"
7-
8-

validator_worker/src/follower.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ async fn on_new_state<'a, A: Adapter + 'static>(
8282
},
8383
_ => Default::default(),
8484
};
85-
// .map_or(Default::default(), |new_state| new_state.msg.balances);
8685

8786
if !is_valid_transition(&iface.channel, &prev_balances, &proposed_balances) {
8887
return Ok(on_error(&iface, &new_state, InvalidNewState::Transition).await);

0 commit comments

Comments
 (0)