Skip to content

Commit f53a546

Browse files
committed
requested changes from review
1 parent dadf4eb commit f53a546

File tree

6 files changed

+103
-69
lines changed

6 files changed

+103
-69
lines changed

primitives/src/sentry.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ pub mod channel_list {
657657
pub page: u64,
658658
/// filters the channels containing a specific validator if provided
659659
pub validator: Option<ValidatorId>,
660+
/// filters the channels by chain_id where the chain id is one of the provided ones
660661
#[serde(default)]
661662
pub chains: Vec<ChainId>,
662663
}

sentry/src/db/accounting.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,31 +185,28 @@ pub async fn spend_amount(
185185

186186
#[cfg(test)]
187187
mod test {
188-
use primitives::test_util::{
188+
use primitives::{test_util::{
189189
ADVERTISER, ADVERTISER_2, CREATOR, DUMMY_CAMPAIGN, PUBLISHER, PUBLISHER_2,
190-
};
190+
}, config::GANACHE_CONFIG};
191191

192192
use crate::{
193193
db::{
194194
insert_channel,
195195
tests_postgres::{setup_test_migrations, DATABASE_POOL},
196196
},
197-
test_util::setup_dummy_app,
198197
};
199198

200199
use super::*;
201200

202201
#[tokio::test]
203202
async fn insert_update_and_get_accounting() {
204203
let database = DATABASE_POOL.get().await.expect("Should get a DB pool");
205-
let app = setup_dummy_app().await;
206204

207205
setup_test_migrations(database.pool.clone())
208206
.await
209207
.expect("Migrations should succeed");
210208

211-
let channel_chain = app
212-
.config
209+
let channel_chain = GANACHE_CONFIG
213210
.find_chain_of(DUMMY_CAMPAIGN.channel.token)
214211
.expect("Channel token should be whitelisted in config!");
215212
let channel_context = channel_chain.with_channel(DUMMY_CAMPAIGN.channel);
@@ -405,14 +402,12 @@ mod test {
405402
#[tokio::test]
406403
async fn test_spend_amount() {
407404
let database = DATABASE_POOL.get().await.expect("Should get a DB pool");
408-
let app = setup_dummy_app().await;
409405

410406
setup_test_migrations(database.pool.clone())
411407
.await
412408
.expect("Migrations should succeed");
413409

414-
let channel_chain = app
415-
.config
410+
let channel_chain = GANACHE_CONFIG
416411
.find_chain_of(DUMMY_CAMPAIGN.channel.token)
417412
.expect("Channel token should be whitelisted in config!");
418413
let channel_context = channel_chain.with_channel(DUMMY_CAMPAIGN.channel);
@@ -499,14 +494,12 @@ mod test {
499494
#[tokio::test]
500495
async fn test_spend_amount_with_multiple_spends() {
501496
let database = DATABASE_POOL.get().await.expect("Should get a DB pool");
502-
let app = setup_dummy_app().await;
503497

504498
setup_test_migrations(database.pool.clone())
505499
.await
506500
.expect("Migrations should succeed");
507501

508-
let channel_chain = app
509-
.config
502+
let channel_chain = GANACHE_CONFIG
510503
.find_chain_of(DUMMY_CAMPAIGN.channel.token)
511504
.expect("Channel token should be whitelisted in config!");
512505
let channel_context = channel_chain.with_channel(DUMMY_CAMPAIGN.channel);

sentry/src/db/campaign.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -576,12 +576,12 @@ mod test {
576576
insert_channel,
577577
tests_postgres::{setup_test_migrations, DATABASE_POOL},
578578
},
579-
test_util::setup_dummy_app,
580579
};
581580
use chrono::TimeZone;
582581
use primitives::{
583582
campaign,
584583
campaign::Validators,
584+
config::GANACHE_CONFIG,
585585
event_submission::{RateLimit, Rule},
586586
sentry::{campaign_modify::ModifyCampaign, CLICK, IMPRESSION},
587587
targeting::Rules,
@@ -598,7 +598,6 @@ mod test {
598598

599599
#[tokio::test]
600600
async fn it_inserts_fetches_and_updates_a_campaign() {
601-
let app = setup_dummy_app().await;
602601
let database = DATABASE_POOL.get().await.expect("Should get a DB pool");
603602

604603
setup_test_migrations(database.pool.clone())
@@ -607,8 +606,7 @@ mod test {
607606

608607
let campaign = DUMMY_CAMPAIGN.clone();
609608

610-
let channel_chain = app
611-
.config
609+
let channel_chain = GANACHE_CONFIG
612610
.find_chain_of(DUMMY_CAMPAIGN.channel.token)
613611
.expect("Channel token should be whitelisted in config!");
614612
let channel_context = channel_chain.with_channel(DUMMY_CAMPAIGN.channel);
@@ -700,7 +698,6 @@ mod test {
700698
#[tokio::test]
701699
async fn it_lists_campaigns_properly() {
702700
let database = DATABASE_POOL.get().await.expect("Should get a DB pool");
703-
let app = setup_dummy_app().await;
704701

705702
setup_test_migrations(database.pool.clone())
706703
.await
@@ -710,8 +707,7 @@ mod test {
710707
let mut channel_with_different_leader = DUMMY_CAMPAIGN.channel;
711708
channel_with_different_leader.leader = IDS[&ADVERTISER_2];
712709

713-
let channel_chain = app
714-
.config
710+
let channel_chain = GANACHE_CONFIG
715711
.find_chain_of(DUMMY_CAMPAIGN.channel.token)
716712
.expect("Channel token should be whitelisted in config!");
717713
let channel_context = channel_chain.clone().with_channel(DUMMY_CAMPAIGN.channel);

sentry/src/db/channel.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ pub async fn insert_channel(
6565
mod list_channels {
6666
use primitives::{
6767
sentry::{
68-
channel_list::{ChannelListQuery, ChannelListResponse},
68+
channel_list::ChannelListResponse,
6969
Pagination,
7070
},
71-
Channel,
71+
Channel, ChainId, ValidatorId,
7272
};
7373

7474
use crate::db::{DbPool, PoolError, TotalCount};
@@ -80,15 +80,15 @@ mod list_channels {
8080
pool: &DbPool,
8181
skip: u64,
8282
limit: u32,
83-
query: &ChannelListQuery,
83+
validator: Option<ValidatorId>,
84+
chains: &[ChainId],
8485
) -> Result<ChannelListResponse, PoolError> {
8586
let client = pool.get().await?;
8687
let mut where_clauses = vec![];
87-
if !query.chains.is_empty() {
88+
if !chains.is_empty() {
8889
where_clauses.push(format!(
8990
"chain_id IN ({})",
90-
query
91-
.chains
91+
chains
9292
.iter()
9393
.map(|id| id.to_u32().to_string())
9494
.collect::<Vec<String>>()
@@ -97,7 +97,7 @@ mod list_channels {
9797
}
9898

9999
// To understand why we use Order by, see Postgres Documentation: https://www.postgresql.org/docs/8.1/queries-limit.html
100-
let rows = match query.validator {
100+
let rows = match validator {
101101
Some(validator) => {
102102
where_clauses.push("(leader = $1 OR follower = $1)".to_string());
103103

@@ -108,8 +108,14 @@ mod list_channels {
108108
client.query(&stmt, &[&validator.to_string()]).await?
109109
}
110110
None => {
111-
let statement = format!("SELECT id, leader, follower, guardian, token, nonce, created FROM channels WHERE {} ORDER BY created ASC LIMIT {} OFFSET {}",
112-
where_clauses.join(" AND "), limit, skip);
111+
let statement = if !where_clauses.is_empty() {
112+
format!("SELECT id, leader, follower, guardian, token, nonce, created FROM channels WHERE {} ORDER BY created ASC LIMIT {} OFFSET {}",
113+
where_clauses.join(" AND "), limit, skip)
114+
} else {
115+
format!("SELECT id, leader, follower, guardian, token, nonce, created FROM channels ORDER BY created ASC LIMIT {} OFFSET {}",
116+
limit, skip)
117+
};
118+
113119
let stmt = client.prepare(&statement).await?;
114120

115121
client.query(&stmt, &[]).await?
@@ -118,8 +124,7 @@ mod list_channels {
118124

119125
let channels = rows.iter().map(Channel::from).collect();
120126

121-
let total_count = list_channels_total_count(pool, query).await?;
122-
127+
let total_count = list_channels_total_count(pool, validator, chains).await?;
123128
// fast ceil for total_pages
124129
let total_pages = if total_count == 0 {
125130
1
@@ -138,24 +143,24 @@ mod list_channels {
138143

139144
async fn list_channels_total_count<'a>(
140145
pool: &DbPool,
141-
query: &ChannelListQuery,
146+
validator: Option<ValidatorId>,
147+
chains: &[ChainId],
142148
) -> Result<u64, PoolError> {
143149
let client = pool.get().await?;
144150

145151
let mut where_clauses = vec![];
146-
if !query.chains.is_empty() {
152+
if !chains.is_empty() {
147153
where_clauses.push(format!(
148154
"chain_id IN ({})",
149-
query
150-
.chains
155+
chains
151156
.iter()
152157
.map(|id| id.to_u32().to_string())
153158
.collect::<Vec<String>>()
154159
.join(",")
155160
));
156161
}
157162

158-
let row = match query.validator {
163+
let row = match validator {
159164
Some(validator) => {
160165
where_clauses.push("(leader = $1 OR follower = $1)".to_string());
161166

@@ -168,10 +173,14 @@ mod list_channels {
168173
client.query_one(&stmt, &[&validator.to_string()]).await?
169174
}
170175
None => {
171-
let statement = format!(
172-
"SELECT COUNT(id)::varchar FROM channels WHERE {}",
173-
where_clauses.join(" AND ")
174-
);
176+
let statement = if !where_clauses.is_empty() {
177+
format!(
178+
"SELECT COUNT(id)::varchar FROM channels WHERE {}",
179+
where_clauses.join(" AND ")
180+
)
181+
} else {
182+
"SELECT COUNT(id)::varchar FROM channels".to_string()
183+
};
175184
let stmt = client.prepare(&statement).await?;
176185

177186
client.query_one(&stmt, &[]).await?
@@ -184,28 +193,25 @@ mod list_channels {
184193

185194
#[cfg(test)]
186195
mod test {
187-
use primitives::{sentry::channel_list::ChannelListQuery, test_util::DUMMY_CAMPAIGN, ChainId};
196+
use primitives::{test_util::DUMMY_CAMPAIGN, config::GANACHE_CONFIG};
188197

189198
use crate::{
190199
db::{
191200
insert_channel,
192201
tests_postgres::{setup_test_migrations, DATABASE_POOL},
193202
},
194-
test_util::setup_dummy_app,
195203
};
196204

197205
use super::list_channels::list_channels;
198206

199207
#[tokio::test]
200208
async fn insert_and_list_channels_return_channels() {
201-
let app = setup_dummy_app().await;
202209
let database = DATABASE_POOL.get().await.expect("Should get database");
203210
setup_test_migrations(database.pool.clone())
204211
.await
205212
.expect("Should setup migrations");
206213

207-
let channel_chain = app
208-
.config
214+
let channel_chain = GANACHE_CONFIG
209215
.find_chain_of(DUMMY_CAMPAIGN.channel.token)
210216
.expect("Channel token should be whitelisted in config!");
211217
let channel_context = channel_chain.with_channel(DUMMY_CAMPAIGN.channel);
@@ -225,13 +231,7 @@ mod test {
225231
only_select
226232
};
227233

228-
let query = ChannelListQuery {
229-
page: 0,
230-
validator: None,
231-
chains: vec![channel_context.chain.chain_id],
232-
};
233-
234-
let response = list_channels(&database.pool, 0, 10, &query)
234+
let response = list_channels(&database.pool, 0, 10, None, &[channel_context.chain.chain_id])
235235
.await
236236
.expect("Should list Channels");
237237

sentry/src/db/spendable.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ mod test {
118118

119119
use primitives::{
120120
spender::Spendable,
121+
config::GANACHE_CONFIG,
121122
test_util::DUMMY_CAMPAIGN,
122123
test_util::{ADVERTISER, CREATOR, FOLLOWER, GUARDIAN, GUARDIAN_2, PUBLISHER},
123124
Deposit, UnifiedNum,
@@ -127,8 +128,7 @@ mod test {
127128
db::{
128129
insert_channel,
129130
tests_postgres::{setup_test_migrations, DATABASE_POOL},
130-
},
131-
test_util::setup_dummy_app,
131+
}
132132
};
133133
use tokio::time::{sleep, Duration};
134134

@@ -137,7 +137,6 @@ mod test {
137137
#[tokio::test]
138138
async fn it_inserts_and_fetches_and_updates_spendable() {
139139
let database = DATABASE_POOL.get().await.expect("Should get a DB pool");
140-
let app = setup_dummy_app().await;
141140

142141
setup_test_migrations(database.pool.clone())
143142
.await
@@ -152,8 +151,7 @@ mod test {
152151
},
153152
};
154153

155-
let channel_chain = app
156-
.config
154+
let channel_chain = GANACHE_CONFIG
157155
.find_chain_of(DUMMY_CAMPAIGN.channel.token)
158156
.expect("Channel token should be whitelisted in config!");
159157
let channel_context = channel_chain.with_channel(spendable.channel);
@@ -193,15 +191,13 @@ mod test {
193191
#[tokio::test]
194192
async fn insert_and_get_single_spendable_for_channel() {
195193
let database = DATABASE_POOL.get().await.expect("Should get a DB pool");
196-
let app = setup_dummy_app().await;
197194

198195
setup_test_migrations(database.pool.clone())
199196
.await
200197
.expect("Migrations should succeed");
201198

202199
let channel = DUMMY_CAMPAIGN.channel;
203-
let channel_chain = app
204-
.config
200+
let channel_chain = GANACHE_CONFIG
205201
.find_chain_of(DUMMY_CAMPAIGN.channel.token)
206202
.expect("Channel token should be whitelisted in config!");
207203
let channel_context = channel_chain.with_channel(channel);
@@ -242,15 +238,13 @@ mod test {
242238
#[tokio::test]
243239
async fn gets_multiple_pages_of_spendables_for_channel() {
244240
let database = DATABASE_POOL.get().await.expect("Should get a DB pool");
245-
let app = setup_dummy_app().await;
246241

247242
setup_test_migrations(database.pool.clone())
248243
.await
249244
.expect("Migrations should succeed");
250245

251246
let channel = DUMMY_CAMPAIGN.channel;
252-
let channel_chain = app
253-
.config
247+
let channel_chain = GANACHE_CONFIG
254248
.find_chain_of(DUMMY_CAMPAIGN.channel.token)
255249
.expect("Channel token should be whitelisted in config!");
256250
let channel_context = channel_chain.with_channel(channel);

0 commit comments

Comments
 (0)