Skip to content

Commit 72a3d02

Browse files
committed
Initial implememntation of seeding - not very optimal yet with 2 functions
1 parent 8358521 commit 72a3d02

File tree

6 files changed

+172
-18
lines changed

6 files changed

+172
-18
lines changed

docs/config/ganache.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ admins = [
4747
'0x80690751969B234697e9059e04ed72195c3507fa'
4848
]
4949

50+
seed_db = true
51+
5052
[platform]
5153
# This should be changed for tests and use the wiremock url
5254
url = "https://platform.adex.network"

docs/config/prod.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ validators_whitelist = []
2828
# Galya (for analytics)
2929
admins = ['0x5d6A3F1AD7b124ecDFDf4841D9bB246eD5fBF04c']
3030

31+
seed_db = false
32+
3133
[platform]
3234
# This should be changed for tests and use the wiremock url
3335
url = "https://platform.adex.network"

primitives/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ pub struct Config {
182182
pub chains: HashMap<String, ChainInfo>,
183183
pub platform: PlatformConfig,
184184
pub limits: Limits,
185+
pub seed_db: bool,
185186
}
186187

187188
impl Config {

sentry/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ woothee = "0.13"
7979
reqwest = { version = "0.11", features = ["json", "cookies"] }
8080

8181
[dev-dependencies]
82-
pretty_assertions = "1"
83-
8482
primitives = { version = "0.2", path = "../primitives", features = ["postgres", "test-util"] }
8583
adapter = { version = "0.2", path = "../adapter", features = ["test-util"] }
84+
pretty_assertions = "1"
8685

8786
# we only require `hyper` for `hyper::body::to_bytes` function
8887
hyper = { version = "0.14", default-features = false }

sentry/src/application.rs

Lines changed: 138 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use axum::{
99
http::{Method, StatusCode},
1010
middleware,
1111
routing::get,
12-
Extension, Router,
12+
Extension, Json, Router,
1313
};
1414
use axum_server::{tls_rustls::RustlsConfig, Handle};
1515
use once_cell::sync::Lazy;
@@ -19,14 +19,19 @@ use slog::{error, info, Logger};
1919
use tower::ServiceBuilder;
2020
use tower_http::cors::CorsLayer;
2121

22-
use adapter::{client::Locked, Adapter};
23-
use primitives::{config::Environment, ValidatorId};
22+
use adapter::{client::Locked, Adapter, Dummy, Ethereum};
23+
use primitives::{
24+
config::Environment, sentry::campaign_create::CreateCampaign, test_util::CAMPAIGNS,
25+
unified_num::FromWhole, Campaign, ChainOf, Deposit, UnifiedNum, ValidatorId,
26+
};
2427

2528
use crate::{
2629
db::{CampaignRemaining, DbPool},
2730
middleware::auth::authenticate,
2831
platform::PlatformApi,
2932
routes::{
33+
campaign::create_campaign,
34+
channel::{channel_dummy_deposit, ChannelDummyDeposit},
3035
get_cfg,
3136
routers::{analytics_router, campaigns_router, channels_router, units_for_slot_router},
3237
},
@@ -307,6 +312,136 @@ async fn shutdown_signal(logger: Logger, handle: Handle) {
307312
info!(&logger, "Received Ctrl+C signal. Shutting down..")
308313
}
309314

315+
pub async fn seed_dummy(app: Application<Dummy>) -> Result<(), Box<dyn std::error::Error>> {
316+
// create campaign
317+
// Chain 1337
318+
let campaign_1 = CAMPAIGNS[0].clone();
319+
// Chain 1337
320+
let campaign_2 = CAMPAIGNS[1].clone();
321+
// Chain 1
322+
let campaign_3 = CAMPAIGNS[2].clone();
323+
324+
async fn create_seed_campaign(
325+
app: Application<Dummy>,
326+
campaign: &ChainOf<Campaign>,
327+
) -> Result<(), Box<dyn std::error::Error>> {
328+
let campaign_to_create = CreateCampaign::from_campaign(campaign.context.clone());
329+
let auth = Auth {
330+
era: 0,
331+
uid: ValidatorId::from(campaign_to_create.creator),
332+
chain: campaign.chain.clone(),
333+
};
334+
let _result = create_campaign(
335+
Json(campaign_to_create),
336+
Extension(auth),
337+
Extension(Arc::new(app)),
338+
)
339+
.await
340+
.expect("Should create seed campaigns");
341+
342+
Ok(())
343+
}
344+
345+
async fn dummy_deposit(
346+
app: Application<Dummy>,
347+
campaign: &ChainOf<Campaign>,
348+
) -> Result<(), Box<dyn std::error::Error>> {
349+
let channel = campaign.context.channel;
350+
let auth = Auth {
351+
era: 0,
352+
uid: ValidatorId::from(campaign.context.creator),
353+
chain: campaign.chain.clone(),
354+
};
355+
356+
let request = ChannelDummyDeposit {
357+
channel,
358+
deposit: Deposit {
359+
total: UnifiedNum::from_whole(1_000_000),
360+
},
361+
};
362+
363+
let result =
364+
channel_dummy_deposit(Extension(Arc::new(app)), Extension(auth), Json(request)).await;
365+
366+
assert!(result.is_ok());
367+
368+
Ok(())
369+
}
370+
// chain 1337
371+
dummy_deposit(app.clone(), &campaign_1).await?;
372+
// chain 1337
373+
dummy_deposit(app.clone(), &campaign_2).await?;
374+
// chain 1
375+
dummy_deposit(app.clone(), &campaign_3).await?;
376+
377+
create_seed_campaign(app.clone(), &campaign_1).await?;
378+
create_seed_campaign(app.clone(), &campaign_2).await?;
379+
create_seed_campaign(app.clone(), &campaign_3).await?;
380+
Ok(())
381+
}
382+
383+
pub async fn seed_ethereum(app: Application<Ethereum>) -> Result<(), Box<dyn std::error::Error>> {
384+
// create campaign
385+
// Chain 1337
386+
let campaign_1 = CAMPAIGNS[0].clone();
387+
// Chain 1337
388+
let campaign_2 = CAMPAIGNS[1].clone();
389+
// Chain 1
390+
let campaign_3 = CAMPAIGNS[2].clone();
391+
392+
async fn create_seed_campaign(
393+
app: Application<Ethereum>,
394+
campaign: &ChainOf<Campaign>,
395+
) -> Result<(), Box<dyn std::error::Error>> {
396+
let campaign_to_create = CreateCampaign::from_campaign(campaign.context.clone());
397+
let auth = Auth {
398+
era: 0,
399+
uid: ValidatorId::from(campaign_to_create.creator),
400+
chain: campaign.chain.clone(),
401+
};
402+
let _result = create_campaign(
403+
Json(campaign_to_create),
404+
Extension(auth),
405+
Extension(Arc::new(app)),
406+
)
407+
.await
408+
.expect("Should create seed campaigns");
409+
Ok(())
410+
}
411+
async fn deposit(
412+
app: Application<Ethereum>,
413+
campaign: &ChainOf<Campaign>,
414+
) -> Result<(), Box<dyn std::error::Error>> {
415+
let channel = campaign.context.channel;
416+
let auth = Auth {
417+
era: 0,
418+
uid: ValidatorId::from(campaign.context.creator),
419+
chain: campaign.chain.clone(),
420+
};
421+
let request = ChannelDummyDeposit {
422+
channel,
423+
deposit: Deposit {
424+
total: UnifiedNum::from_whole(1_000_000),
425+
},
426+
};
427+
let result =
428+
channel_dummy_deposit(Extension(Arc::new(app)), Extension(auth), Json(request)).await;
429+
assert!(result.is_ok());
430+
Ok(())
431+
}
432+
// chain 1337
433+
deposit(app.clone(), &campaign_1).await?;
434+
// chain 1337
435+
deposit(app.clone(), &campaign_2).await?;
436+
// chain 1
437+
deposit(app.clone(), &campaign_3).await?;
438+
439+
create_seed_campaign(app.clone(), &campaign_1).await?;
440+
create_seed_campaign(app.clone(), &campaign_2).await?;
441+
create_seed_campaign(app.clone(), &campaign_3).await?;
442+
Ok(())
443+
}
444+
310445
#[cfg(test)]
311446
mod test {
312447
use serde_json::json;

sentry/src/main.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use primitives::{
1313
util::logging::new_logger, ValidatorId,
1414
};
1515
use sentry::{
16-
application::EnableTls,
16+
application::{seed_dummy, seed_ethereum, EnableTls},
1717
db::{postgres_connection, redis_connection, setup_migrations, CampaignRemaining},
1818
platform::PlatformApi,
1919
Application,
@@ -150,32 +150,47 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
150150
)
151151
.expect("Failed to build PlatformApi");
152152

153+
// Like dummy adapter route
153154
match adapter {
154155
AdapterTypes::Ethereum(adapter) => {
155-
Application::new(
156+
let app = Application::new(
156157
*adapter,
157-
config,
158+
config.clone(),
158159
logger,
159-
redis,
160+
redis.clone(),
160161
postgres,
161162
campaign_remaining,
162163
platform_api,
163-
)
164-
.run(enable_tls)
165-
.await
164+
);
165+
166+
if config.seed_db {
167+
redis::cmd("FLUSHDB")
168+
.query_async::<_, String>(&mut redis.clone())
169+
.await?;
170+
seed_ethereum(app.clone()).await?;
171+
}
172+
173+
app.run(enable_tls).await
166174
}
167175
AdapterTypes::Dummy(adapter) => {
168-
Application::new(
176+
let app = Application::new(
169177
*adapter,
170-
config,
178+
config.clone(),
171179
logger,
172-
redis,
180+
redis.clone(),
173181
postgres,
174182
campaign_remaining,
175183
platform_api,
176-
)
177-
.run(enable_tls)
178-
.await
184+
);
185+
186+
if config.seed_db {
187+
redis::cmd("FLUSHDB")
188+
.query_async::<_, String>(&mut redis.clone())
189+
.await?;
190+
seed_dummy(app.clone()).await?;
191+
}
192+
193+
app.run(enable_tls).await
179194
}
180195
};
181196

0 commit comments

Comments
 (0)