Skip to content

Commit

Permalink
Merge pull request #90 from gregdhill/refactor/service
Browse files Browse the repository at this point in the history
Service Crate
  • Loading branch information
gregdhill authored Apr 20, 2021
2 parents 6c60d73 + afb6d42 commit 3dfd428
Show file tree
Hide file tree
Showing 25 changed files with 665 additions and 453 deletions.
49 changes: 42 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ members = [
"testdata-gen",
"vault",
"bitcoin",
"faucet"
"faucet",
"service"
]
1 change: 1 addition & 0 deletions faucet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ git-version = "0.3.4"

# Workspace dependencies
runtime = { path = "../runtime" }
service = { path = "../service" }

[dev-dependencies]

Expand Down
44 changes: 11 additions & 33 deletions faucet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ mod system;
use clap::Clap;
use error::Error;
use git_version::git_version;
use runtime::{substrate_subxt::PairSigner, ConnectionManager, PolkaBtcRuntime};
use runtime::{substrate_subxt::PairSigner, PolkaBtcRuntime};
use service::{ConnectionManager, ServiceConfig};
use system::{FaucetService, FaucetServiceConfig};

const VERSION: &str = git_version!(args = ["--tags"]);
Expand All @@ -24,25 +25,13 @@ struct Opts {
#[clap(flatten)]
parachain: runtime::cli::ConnectionOpts,

/// Address to listen on for JSON-RPC requests.
#[clap(long, default_value = "[::0]:3033")]
http_addr: String,

/// Comma separated list of allowed origins.
#[clap(long, default_value = "*")]
rpc_cors_domain: String,

/// DOT allowance per request for regular users.
#[clap(long, default_value = "1")]
user_allowance: u128,

/// DOT allowance per request for vaults.
#[clap(long, default_value = "500")]
vault_allowance: u128,
/// Settings specific to the faucet client.
#[clap(flatten)]
faucet: FaucetServiceConfig,

/// DOT allowance per request for vaults.
#[clap(long, default_value = "500")]
staked_relayer_allowance: u128,
/// General service settings.
#[clap(flatten)]
service: ServiceConfig,
}

#[tokio::main]
Expand All @@ -55,20 +44,9 @@ async fn main() -> Result<(), Error> {
let (key_pair, _) = opts.account_info.get_key_pair()?;
let signer = PairSigner::<PolkaBtcRuntime, _>::new(key_pair);

ConnectionManager::<_, _, FaucetService>::new(
opts.parachain.polka_btc_url.clone(),
signer.clone(),
FaucetServiceConfig {
http_addr: opts.http_addr.parse()?,
rpc_cors_domain: opts.rpc_cors_domain,
user_allowance: opts.user_allowance,
vault_allowance: opts.vault_allowance,
staked_relayer_allowance: opts.staked_relayer_allowance,
},
opts.parachain.into(),
)
.start()
.await?;
ConnectionManager::<(), _, FaucetService>::new(signer.clone(), opts.parachain, opts.service, opts.faucet)
.start()
.await?;

Ok(())
}
45 changes: 33 additions & 12 deletions faucet/src/system.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
use crate::{http, Error};
use async_trait::async_trait;
use clap::Clap;
use futures::future;
use log::debug;
use runtime::{on_shutdown, wait_or_shutdown, Error as RuntimeError, PolkaBtcProvider, Service, ShutdownSender};
use runtime::{Error as RuntimeError, PolkaBtcProvider};
use service::{on_shutdown, wait_or_shutdown, Service, ShutdownSender};
use std::net::SocketAddr;

#[derive(Clone)]
#[derive(Clap, Clone)]
pub struct FaucetServiceConfig {
pub http_addr: SocketAddr,
pub rpc_cors_domain: String,
pub user_allowance: u128,
pub vault_allowance: u128,
pub staked_relayer_allowance: u128,
/// Address to listen on for JSON-RPC requests.
#[clap(long, default_value = "[::0]:3033")]
http_addr: SocketAddr,

/// Comma separated list of allowed origins.
#[clap(long, default_value = "*")]
rpc_cors_domain: String,

/// DOT allowance per request for regular users.
#[clap(long, default_value = "1")]
user_allowance: u128,

/// DOT allowance per request for vaults.
#[clap(long, default_value = "500")]
vault_allowance: u128,

/// DOT allowance per request for vaults.
#[clap(long, default_value = "500")]
staked_relayer_allowance: u128,
}

pub struct FaucetService {
Expand All @@ -21,12 +37,16 @@ pub struct FaucetService {
}

#[async_trait]
impl Service<FaucetServiceConfig, PolkaBtcProvider> for FaucetService {
async fn initialize(_config: &FaucetServiceConfig) -> Result<(), RuntimeError> {
Ok(())
}
impl Service<(), FaucetServiceConfig> for FaucetService {
const NAME: &'static str = env!("CARGO_PKG_NAME");
const VERSION: &'static str = env!("CARGO_PKG_VERSION");

fn new_service(btc_parachain: PolkaBtcProvider, config: FaucetServiceConfig, shutdown: ShutdownSender) -> Self {
fn new_service(
btc_parachain: PolkaBtcProvider,
_bitcoin_core: (),
config: FaucetServiceConfig,
shutdown: ShutdownSender,
) -> Self {
FaucetService::new(btc_parachain, config, shutdown)
}

Expand Down Expand Up @@ -74,6 +94,7 @@ impl FaucetService {
close_handle.close();
});

log::info!("Running...");
let _ = future::join(block_listener, http_server).await;

Ok(())
Expand Down
31 changes: 8 additions & 23 deletions runtime/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use crate::{
error::{Error, KeyLoadingError},
ConnectionManagerConfig, RestartPolicy,
};

use crate::error::{Error, KeyLoadingError};
use clap::Clap;
use sp_core::{sr25519::Pair, Pair as _};
use sp_keyring::AccountKeyring;
use std::{collections::HashMap, time::Duration};
use std::{collections::HashMap, num::ParseIntError, time::Duration};

#[derive(Clap, Debug, Clone)]
pub struct ProviderUserOpts {
Expand Down Expand Up @@ -55,19 +51,19 @@ fn get_credentials_from_file(file_path: &str, keyname: &str) -> Result<Pair, Key
Ok(pair)
}

pub fn parse_duration_ms(src: &str) -> Result<Duration, ParseIntError> {
Ok(Duration::from_millis(u64::from_str_radix(src, 10)?))
}

#[derive(Clap, Debug, Clone)]
pub struct ConnectionOpts {
/// Parachain websocket URL.
#[clap(long, default_value = "ws://127.0.0.1:9944")]
pub polka_btc_url: String,

/// Timeout in milliseconds to wait for connection to btc-parachain.
#[clap(long, default_value = "60000")]
pub polka_btc_connection_timeout_ms: u64,

/// What to do if the connection to the btc-parachain drops.
#[clap(long, default_value = "always")]
pub restart_policy: RestartPolicy,
#[clap(long, parse(try_from_str = parse_duration_ms), default_value = "60000")]
pub polka_btc_connection_timeout_ms: Duration,

/// Maximum number of concurrent requests
#[clap(long)]
Expand All @@ -77,14 +73,3 @@ pub struct ConnectionOpts {
#[clap(long)]
pub max_notifs_per_subscription: Option<usize>,
}

impl From<ConnectionOpts> for ConnectionManagerConfig {
fn from(opts: ConnectionOpts) -> ConnectionManagerConfig {
ConnectionManagerConfig {
connection_timeout: Duration::from_millis(opts.polka_btc_connection_timeout_ms),
restart_policy: opts.restart_policy,
max_concurrent_requests: opts.max_concurrent_requests,
max_notifs_per_subscription: opts.max_notifs_per_subscription,
}
}
}
Loading

0 comments on commit 3dfd428

Please sign in to comment.