Skip to content

Commit 70a6825

Browse files
committed
Merge branch 'aip-61-adex-v5' into issue-471-tests-for-route-units-for-slot
2 parents 6f77d0f + 9ae7eb1 commit 70a6825

File tree

8 files changed

+291
-40
lines changed

8 files changed

+291
-40
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ These examples use the Leader and Follower addresses for testing locally with
7676
Sentry API will be accessible at `localhost:8005`
7777

7878
```bash
79-
IP_ADDR=127.0.0.1 REDIS_URL="redis://127.0.0.1:6379/1" \
79+
IP_ADDR=127.0.0.1 SEED_DB=true REDIS_URL="redis://127.0.0.1:6379/1" \
8080
POSTGRES_DB="sentry_leader" PORT=8005 KEYSTORE_PWD=ganache0 \
8181
cargo run -p sentry -- \
8282
--adapter ethereum \
@@ -89,7 +89,7 @@ cargo run -p sentry -- \
8989
Sentry API will be accessible at `localhost:8006`
9090

9191
```bash
92-
IP_ADDR=127.0.0.1 REDIS_URL="redis://127.0.0.1:6379/2" \
92+
IP_ADDR=127.0.0.1 SEED_DB=true REDIS_URL="redis://127.0.0.1:6379/2" \
9393
POSTGRES_DB="sentry_follower" PORT=8006 KEYSTORE_PWD=ganache1 cargo run -p sentry -- \
9494
--adapter ethereum \
9595
--keystoreFile ./adapter/tests/resources/0xf3f583AEC5f7C030722Fe992A5688557e1B86ef7_keystore.json \
@@ -118,7 +118,7 @@ Request body (`JSON`):
118118
##### Leader (`0x80690751969B234697e9059e04ed72195c3507fa`)
119119

120120
```bash
121-
IP_ADDR=127.0.0.1 REDIS_URL="redis://127.0.0.1:6379/1" \
121+
IP_ADDR=127.0.0.1 SEED_DB=true REDIS_URL="redis://127.0.0.1:6379/1" \
122122
POSTGRES_DB="sentry_leader" PORT=8005 cargo run -p sentry -- \
123123
--adapter dummy \
124124
--dummyIdentity 0x80690751969B234697e9059e04ed72195c3507fa \
@@ -127,7 +127,7 @@ POSTGRES_DB="sentry_leader" PORT=8005 cargo run -p sentry -- \
127127
##### Follower (`0xf3f583AEC5f7C030722Fe992A5688557e1B86ef7`)
128128

129129
```bash
130-
IP_ADDR=127.0.0.1 REDIS_URL="redis://127.0.0.1:6379/2" \
130+
IP_ADDR=127.0.0.1 SEED_DB=true REDIS_URL="redis://127.0.0.1:6379/2" \
131131
POSTGRES_DB="sentry_follower" PORT=8006 cargo run -p sentry -- \
132132
--adapter dummy \
133133
--dummyIdentity 0xf3f583AEC5f7C030722Fe992A5688557e1B86ef7 \
@@ -139,6 +139,7 @@ POSTGRES_DB="sentry_follower" PORT=8006 cargo run -p sentry -- \
139139
- `ENV` - `production` or `development`; *default*: `development` - passing this env. variable will use the default configuration paths - [`docs/config/ganache.toml`](./docs/config/ganache.toml) (for `development`) or [`docs/config/prod.toml`](./docs/config/prod.toml) (for `production`). Otherwise you can pass your own configuration file path to the binary (check `cargo run -p sentry --help` for more information). ~~In `development` it will make sure Sentry to seed the database~~ (seeding is disabled, see #514).
140140
- `PORT` - *default*: `8005` - The local port that Sentry API will be accessible at
141141
- `IP_ADDR` - *default*: `0.0.0.0` - the IP address that the API should be listening to
142+
- `SEED_DB` - *default*: `false` - Flag telling us whether we should seed the database, it can only be turned on for `development`
142143

143144
##### Adapter
144145

adapter/src/ethereum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use web3::signing::keccak256;
88
use crate::{Adapter, LockedState, UnlockedState};
99

1010
pub use {
11-
client::{Ethereum, Options},
11+
client::{ChainTransport, Ethereum, Options},
1212
error::Error,
1313
};
1414

adapter/src/ethereum/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub struct Ethereum<S = LockedWallet> {
3737
pub(crate) state: S,
3838
}
3939

40-
pub(crate) trait ChainTransport {
40+
pub trait ChainTransport {
4141
fn init_web3(&self) -> web3::Result<Web3<Http>>;
4242
}
4343

primitives/src/test_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ pub static DUMMY_IPFS: Lazy<[IPFS; 5]> = Lazy::new(|| {
300300
]
301301
});
302302

303-
/// List of test campaigns with keys `Campaign {Chain id} #{Number campaign}`
303+
/// List of test campaigns
304304
pub static CAMPAIGNS: Lazy<[ChainOf<Campaign>; 3]> = Lazy::new(|| {
305305
let campaign_1337_1 = {
306306
let ganache_chain_info = GANACHE_CONFIG.chains["Ganache #1337"].clone();

sentry/Cargo.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ futures = "0.3"
3333
async-trait = "0.1"
3434
# Primitives
3535
primitives = { version = "0.2", path = "../primitives", features = ["postgres"] }
36-
adapter = { version = "0.2", path = "../adapter" }
36+
adapter = { version = "0.2", path = "../adapter", features = ["test-util"] }
3737
chrono = { version = "0.4", features = ["serde"] }
3838
# used for redis test pool
3939
dashmap = { version = "5", optional = true }
@@ -79,11 +79,9 @@ woothee = "0.13"
7979
reqwest = { version = "0.11", features = ["json", "cookies"] }
8080

8181
[dev-dependencies]
82+
primitives = { version = "0.2", path = "../primitives", features = ["postgres", "test-util"] }
8283
pretty_assertions = "1"
8384
wiremock = "0.5"
8485

85-
primitives = { version = "0.2", path = "../primitives", features = ["postgres", "test-util"] }
86-
adapter = { version = "0.2", path = "../adapter", features = ["test-util"] }
87-
8886
# we only require `hyper` for `hyper::body::to_bytes` function
8987
hyper = { version = "0.14", default-features = false }

sentry/src/application.rs

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ pub struct EnvConfig {
6464
/// Defaults to locally running Redis server: [`DEFAULT_REDIS_URL`]
6565
#[serde(deserialize_with = "redis_url", default = "default_redis_url")]
6666
pub redis_url: ConnectionInfo,
67+
/// Whether or not to seed the database in [`Environment::Development`].
68+
#[serde(default)]
69+
pub seed_db: bool,
6770
}
6871

6972
impl EnvConfig {
@@ -307,6 +310,214 @@ async fn shutdown_signal(logger: Logger, handle: Handle) {
307310
info!(&logger, "Received Ctrl+C signal. Shutting down..")
308311
}
309312

313+
pub mod seed {
314+
use std::sync::Arc;
315+
316+
use axum::{Extension, Json};
317+
318+
use adapter::{
319+
ethereum::{
320+
test_util::{Erc20Token, Outpace},
321+
ChainTransport,
322+
},
323+
Dummy, Ethereum,
324+
};
325+
use primitives::{
326+
sentry::campaign_create::CreateCampaign,
327+
spender::Spendable,
328+
test_util::{ADVERTISER, ADVERTISER_2, CAMPAIGNS, LEADER},
329+
unified_num::FromWhole,
330+
BigNum, Campaign, ChainOf, Deposit, UnifiedNum, ValidatorId,
331+
};
332+
333+
use slog::info;
334+
335+
use crate::{
336+
db::{insert_channel, spendable::insert_spendable},
337+
routes::{
338+
campaign::create_campaign,
339+
channel::{channel_dummy_deposit, ChannelDummyDeposit},
340+
},
341+
Application, Auth,
342+
};
343+
344+
pub async fn seed_dummy(app: Application<Dummy>) -> Result<(), Box<dyn std::error::Error>> {
345+
info!(&app.logger, "Seeding sentry with Dummy adapter");
346+
347+
// create campaign
348+
// Chain 1337
349+
let campaign_1 = CAMPAIGNS[0].clone();
350+
// Chain 1337
351+
let campaign_2 = CAMPAIGNS[1].clone();
352+
// Chain 1
353+
let campaign_3 = CAMPAIGNS[2].clone();
354+
355+
async fn create_seed_campaign(
356+
app: Application<Dummy>,
357+
campaign: &ChainOf<Campaign>,
358+
) -> Result<(), Box<dyn std::error::Error>> {
359+
let campaign_to_create = CreateCampaign::from_campaign(campaign.context.clone());
360+
let auth = Auth {
361+
era: 0,
362+
uid: ValidatorId::from(campaign_to_create.creator),
363+
chain: campaign.chain.clone(),
364+
};
365+
create_campaign(
366+
Json(campaign_to_create),
367+
Extension(auth),
368+
Extension(Arc::new(app)),
369+
)
370+
.await
371+
.expect("Should create seed campaigns");
372+
373+
Ok(())
374+
}
375+
376+
async fn dummy_deposit(
377+
app: Application<Dummy>,
378+
campaign: &ChainOf<Campaign>,
379+
) -> Result<(), Box<dyn std::error::Error>> {
380+
let channel = campaign.context.channel;
381+
let auth = Auth {
382+
era: 0,
383+
uid: ValidatorId::from(campaign.context.creator),
384+
chain: campaign.chain.clone(),
385+
};
386+
387+
let request = ChannelDummyDeposit {
388+
channel,
389+
deposit: Deposit {
390+
total: UnifiedNum::from_whole(1_000_000),
391+
},
392+
};
393+
394+
let result =
395+
channel_dummy_deposit(Extension(Arc::new(app)), Extension(auth), Json(request))
396+
.await;
397+
398+
assert!(result.is_ok());
399+
400+
Ok(())
401+
}
402+
// chain 1337
403+
dummy_deposit(app.clone(), &campaign_1).await?;
404+
// chain 1337
405+
dummy_deposit(app.clone(), &campaign_2).await?;
406+
// chain 1
407+
dummy_deposit(app.clone(), &campaign_3).await?;
408+
409+
create_seed_campaign(app.clone(), &campaign_1).await?;
410+
create_seed_campaign(app.clone(), &campaign_2).await?;
411+
create_seed_campaign(app.clone(), &campaign_3).await?;
412+
Ok(())
413+
}
414+
415+
pub async fn seed_ethereum(
416+
app: Application<Ethereum>,
417+
) -> Result<(), Box<dyn std::error::Error>> {
418+
info!(&app.logger, "Seeding sentry with Ethereum adapter");
419+
// Chain 1337
420+
let campaign_1 = CAMPAIGNS[0].clone();
421+
// Chain 1337
422+
let campaign_2 = CAMPAIGNS[1].clone();
423+
// Chain 1
424+
let campaign_3 = CAMPAIGNS[2].clone();
425+
426+
let web3_chain_1337 = campaign_1.chain.init_web3()?;
427+
let token_1337 = Erc20Token::new(&web3_chain_1337, campaign_1.token.clone());
428+
let outpace_1337 = Outpace::new(&web3_chain_1337, campaign_1.chain.outpace);
429+
let web3_chain_1 = campaign_3.chain.init_web3()?;
430+
let token_1 = Erc20Token::new(&web3_chain_1, campaign_3.token.clone());
431+
let outpace_1 = Outpace::new(&web3_chain_1, campaign_3.chain.outpace);
432+
433+
token_1337
434+
.set_balance(
435+
LEADER.to_bytes(),
436+
ADVERTISER.to_bytes(),
437+
&BigNum::with_precision(3_000_000, token_1337.info.precision.into()),
438+
)
439+
.await
440+
.expect("Failed to set balance");
441+
outpace_1337
442+
.deposit(
443+
&campaign_1.context.channel,
444+
ADVERTISER.to_bytes(),
445+
&BigNum::with_precision(1_000_000, token_1337.info.precision.into()),
446+
)
447+
.await
448+
.expect("Should deposit funds");
449+
outpace_1337
450+
.deposit(
451+
&campaign_2.context.channel,
452+
ADVERTISER.to_bytes(),
453+
&BigNum::with_precision(1_000_000, token_1337.info.precision.into()),
454+
)
455+
.await
456+
.expect("Should deposit funds");
457+
458+
token_1
459+
.set_balance(
460+
LEADER.to_bytes(),
461+
ADVERTISER_2.to_bytes(),
462+
&BigNum::with_precision(2_000_000, token_1.info.precision.into()),
463+
)
464+
.await
465+
.expect("Failed to set balance");
466+
467+
outpace_1
468+
.deposit(
469+
&campaign_3.context.channel,
470+
ADVERTISER_2.to_bytes(),
471+
&BigNum::with_precision(1_000_000, token_1.info.precision.into()),
472+
)
473+
.await
474+
.expect("Should deposit funds");
475+
476+
async fn create_seed_campaign(
477+
app: Application<Ethereum>,
478+
campaign: &ChainOf<Campaign>,
479+
) -> Result<(), Box<dyn std::error::Error>> {
480+
let channel_context = ChainOf::of_channel(campaign);
481+
let campaign_to_create = CreateCampaign::from_campaign(campaign.context.clone());
482+
483+
let auth = Auth {
484+
era: 0,
485+
uid: ValidatorId::from(campaign.context.creator),
486+
chain: campaign.chain.clone(),
487+
};
488+
489+
let spendable = Spendable {
490+
spender: campaign.context.creator,
491+
channel: campaign.context.channel,
492+
deposit: Deposit {
493+
total: UnifiedNum::from_whole(10_000),
494+
},
495+
};
496+
insert_channel(&app.pool, &channel_context)
497+
.await
498+
.expect("Should insert channel of seed campaign");
499+
insert_spendable(app.pool.clone(), &spendable)
500+
.await
501+
.expect("Should insert spendable for campaign creator");
502+
503+
create_campaign(
504+
Json(campaign_to_create),
505+
Extension(auth),
506+
Extension(Arc::new(app.clone())),
507+
)
508+
.await
509+
.expect("should create campaign");
510+
511+
Ok(())
512+
}
513+
514+
create_seed_campaign(app.clone(), &campaign_1).await?;
515+
create_seed_campaign(app.clone(), &campaign_2).await?;
516+
create_seed_campaign(app.clone(), &campaign_3).await?;
517+
Ok(())
518+
}
519+
}
520+
310521
#[cfg(test)]
311522
mod test {
312523
use serde_json::json;

0 commit comments

Comments
 (0)