Skip to content

Commit

Permalink
Merge pull request #190 from AdExNetwork/bb8
Browse files Browse the repository at this point in the history
Updates dependencies and adds bb8
  • Loading branch information
elpiel committed Nov 12, 2019
2 parents 432620a + fa77028 commit d52f7eb
Show file tree
Hide file tree
Showing 9 changed files with 841 additions and 547 deletions.
1,320 changes: 794 additions & 526 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions adapter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ web3 = { git = "https://github.com/tomusdrw/rust-web3" }
eth_checksum = "0.1.1"
ethabi = "8.0.1"
tiny-keccak = "1.5"
ethkey = { git = "https://github.com/paritytech/parity.git" }
ethstore = { git = "https://github.com/paritytech/parity.git" }
parity-crypto = { version = "0.4.2", features = ["publickey"], git = "https://github.com/paritytech/parity-common" }
ethstore = { version = "0.2.1", git = "https://github.com/paritytech/parity-ethereum"}
ethkey = { version = "0.4.0", git = "https://github.com/paritytech/parity-ethereum"}
sha2 = "0.8.0"
base64 = "0.10.1"
lazy_static = "1.4.0"
Expand Down
5 changes: 4 additions & 1 deletion adapter/src/ethereum.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::EthereumChannel;
use chrono::Utc;
use ethkey::{public_to_address, recover, verify_address, Address, Message, Password, Signature};
use ethkey::Password;
use ethstore::SafeAccount;
use lazy_static::lazy_static;
use parity_crypto::publickey::{
public_to_address, recover, verify_address, Address, Message, Signature,
};
use primitives::{
adapter::{Adapter, AdapterError, AdapterResult, KeystoreOptions, Session},
channel_validator::ChannelValidator,
Expand Down
4 changes: 3 additions & 1 deletion sentry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ tokio = "0.2.0-alpha.6"
hyper = {version = "=0.13.0-alpha.4", features = ["unstable-stream"]}
regex = "1"
# Database
redis = {version = "0.13.1-alpha.0", features = ["executor"]}
redis = {version = "0.13.1-alpha.0", features = ["tokio-executor"]}
bb8 = {git = "https://github.com/djc/bb8", branch = "async-await"}
bb8-postgres = {git = "https://github.com/djc/bb8", branch = "async-await"}
# Logger
slog = { version = "^2.2.3" , features = ["max_level_trace"] }
# Serde
Expand Down
8 changes: 4 additions & 4 deletions sentry/src/access.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use chrono::Utc;
use futures::future::try_join_all;
use redis::aio::SharedConnection;
use redis::aio::MultiplexedConnection;

use primitives::event_submission::{RateLimit, Rule};
use primitives::sentry::Event;
Expand All @@ -18,7 +18,7 @@ pub enum Error {

// @TODO: Make pub(crate)
pub async fn check_access(
redis: &SharedConnection,
redis: &MultiplexedConnection,
session: &Session,
rate_limit: &RateLimit,
channel: &Channel,
Expand Down Expand Up @@ -91,7 +91,7 @@ pub async fn check_access(
}

async fn apply_rule(
redis: SharedConnection,
redis: MultiplexedConnection,
rule: &Rule,
events: &[Event],
channel: &Channel,
Expand Down Expand Up @@ -164,7 +164,7 @@ mod test {

use super::*;

async fn setup() -> (Config, SharedConnection) {
async fn setup() -> (Config, MultiplexedConnection) {
let mut redis = redis_connection().await.expect("Couldn't connect to Redis");
let config = configuration("development", None).expect("Failed to get dev configuration");

Expand Down
20 changes: 17 additions & 3 deletions sentry/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
use redis::aio::SharedConnection;
use redis::aio::MultiplexedConnection;
use redis::RedisError;

use bb8::Pool;
use bb8_postgres::{
tokio_postgres::{Error as PostgresError, NoTls},
PostgresConnectionManager,
};
use lazy_static::lazy_static;

lazy_static! {
static ref REDIS_URL: String =
std::env::var("REDIS_URL").unwrap_or_else(|_| String::from("redis://127.0.0.1:6379"));
static ref POSTGRES_URL: String = std::env::var("POSTGRES_URL")
.unwrap_or_else(|_| String::from("postgresql://postgres:postgres@localhost:5432"));
}

pub async fn redis_connection() -> Result<SharedConnection, RedisError> {
pub async fn redis_connection() -> Result<MultiplexedConnection, RedisError> {
let client = redis::Client::open(REDIS_URL.as_str()).expect("Wrong redis connection string");
client.get_shared_async_connection().await
client.get_multiplexed_tokio_connection().await
}

pub async fn postgres_connection() -> Result<Pool<PostgresConnectionManager<NoTls>>, PostgresError>
{
let pg_mgr = PostgresConnectionManager::new_from_stringlike(POSTGRES_URL.as_str(), NoTls)?;

Pool::builder().build(pg_mgr).await
}
13 changes: 9 additions & 4 deletions sentry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

use crate::middleware::auth;
use crate::middleware::cors::{cors, Cors};
use bb8::Pool;
use bb8_postgres::{tokio_postgres::NoTls, PostgresConnectionManager};
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Error, Method, Request, Response, Server, StatusCode};
use primitives::adapter::Adapter;
use primitives::Config;
use redis::aio::SharedConnection;
use redis::aio::MultiplexedConnection;
use slog::{error, info, Logger};

pub mod middleware {
Expand Down Expand Up @@ -40,7 +42,8 @@ pub mod event_reducer;
pub struct Application<A: Adapter> {
adapter: A,
logger: Logger,
redis: SharedConnection,
redis: MultiplexedConnection,
_postgres: Pool<PostgresConnectionManager<NoTls>>,
_clustered: bool,
port: u16,
config: Config,
Expand All @@ -51,7 +54,8 @@ impl<A: Adapter + 'static> Application<A> {
adapter: A,
config: Config,
logger: Logger,
redis: SharedConnection,
redis: MultiplexedConnection,
postgres: Pool<PostgresConnectionManager<NoTls>>,
clustered: bool,
port: u16,
) -> Self {
Expand All @@ -60,6 +64,7 @@ impl<A: Adapter + 'static> Application<A> {
config,
logger,
redis,
_postgres: postgres,
_clustered: clustered,
port,
}
Expand Down Expand Up @@ -120,7 +125,7 @@ where
async fn handle_routing(
req: Request<Body>,
(adapter, config): (&impl Adapter, &Config),
redis: SharedConnection,
redis: MultiplexedConnection,
logger: &Logger,
) -> Response<Body> {
let headers = match cors(&req) {
Expand Down
7 changes: 4 additions & 3 deletions sentry/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use primitives::config::configuration;
use primitives::util::logging::{Async, PrefixedCompactFormat, TermDecorator};
use primitives::util::tests::prep_db::{AUTH, IDS};
use primitives::ValidatorId;
use sentry::db::redis_connection;
use sentry::db::{postgres_connection, redis_connection};
use sentry::Application;
use slog::{o, Drain, Logger};
use std::convert::TryFrom;
Expand Down Expand Up @@ -98,15 +98,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let logger = logger();
let redis = redis_connection().await?;
let postgres = postgres_connection().await?;

match adapter {
AdapterTypes::EthereumAdapter(adapter) => {
Application::new(*adapter, config, logger, redis, clustered, port)
Application::new(*adapter, config, logger, redis, postgres, clustered, port)
.run()
.await
}
AdapterTypes::DummyAdapter(adapter) => {
Application::new(*adapter, config, logger, redis, clustered, port)
Application::new(*adapter, config, logger, redis, postgres, clustered, port)
.run()
.await
}
Expand Down
6 changes: 3 additions & 3 deletions sentry/src/middleware/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::error;

use hyper::header::AUTHORIZATION;
use hyper::{Body, Request};
use redis::aio::SharedConnection;
use redis::aio::MultiplexedConnection;

use primitives::adapter::{Adapter, Session as AdapterSession};

Expand All @@ -13,7 +13,7 @@ use crate::Session;
pub(crate) async fn for_request(
mut req: Request<Body>,
adapter: &impl Adapter,
redis: SharedConnection,
redis: MultiplexedConnection,
) -> Result<Request<Body>, Box<dyn error::Error>> {
let authorization = req.headers().get(AUTHORIZATION);

Expand Down Expand Up @@ -90,7 +90,7 @@ mod test {

use super::*;

async fn setup() -> (DummyAdapter, SharedConnection) {
async fn setup() -> (DummyAdapter, MultiplexedConnection) {
let adapter_options = DummyAdapterOptions {
dummy_identity: IDS["leader"].clone(),
dummy_auth: IDS.clone(),
Expand Down

0 comments on commit d52f7eb

Please sign in to comment.