Skip to content

Commit

Permalink
Merge pull request #7 from input-output-hk/rustfmt
Browse files Browse the repository at this point in the history
apply rustfmt
  • Loading branch information
vincenthz committed Feb 5, 2019
2 parents 3fcb30d + 1d19efb commit 210e582
Show file tree
Hide file tree
Showing 12 changed files with 302 additions and 209 deletions.
55 changes: 37 additions & 18 deletions src/config.rs
@@ -1,11 +1,16 @@
use serde_yaml;

use cardano_storage::{self, Storage};
use cardano_storage::config::StorageConfig;
use exe_common::config::{net};
use std::{io, result, path::{PathBuf, Path}, env::{VarError, self, home_dir}};
use std::{num::{ParseIntError}, collections::{BTreeMap}, sync::{Arc}};
use cardano_storage::{self, Storage};
use exe_common::config::net;
use std::collections::HashSet;
use std::{collections::BTreeMap, num::ParseIntError, sync::Arc};
use std::{
env::{self, home_dir, VarError},
io,
path::{Path, PathBuf},
result,
};

#[derive(Debug)]
pub enum Error {
Expand All @@ -14,22 +19,32 @@ pub enum Error {
YamlError(serde_yaml::Error),
ParseIntError(ParseIntError),
StorageError(cardano_storage::Error),
BlockchainConfigError(&'static str)
BlockchainConfigError(&'static str),
}
impl From<VarError> for Error {
fn from(e: VarError) -> Error { Error::VarError(e) }
fn from(e: VarError) -> Error {
Error::VarError(e)
}
}
impl From<ParseIntError> for Error {
fn from(e: ParseIntError) -> Error { Error::ParseIntError(e) }
fn from(e: ParseIntError) -> Error {
Error::ParseIntError(e)
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Error { Error::IoError(e) }
fn from(e: io::Error) -> Error {
Error::IoError(e)
}
}
impl From<cardano_storage::Error> for Error {
fn from(e: cardano_storage::Error) -> Error { Error::StorageError(e) }
fn from(e: cardano_storage::Error) -> Error {
Error::StorageError(e)
}
}
impl From<serde_yaml::Error> for Error {
fn from(e: serde_yaml::Error) -> Error { Error::YamlError(e) }
fn from(e: serde_yaml::Error) -> Error {
Error::YamlError(e)
}
}

type Result<T> = result::Result<T, Error>;
Expand Down Expand Up @@ -60,7 +75,9 @@ impl Config {
}
}

pub fn get_networks_dir(&self) -> PathBuf { self.root_dir.clone() }
pub fn get_networks_dir(&self) -> PathBuf {
self.root_dir.clone()
}

pub fn get_networks(&self) -> Result<Networks> {
let mut networks = Networks::new();
Expand All @@ -71,7 +88,7 @@ impl Config {
let network = Network {
path: netcfg_dir,
config: self.get_network_config(name)?,
storage: Arc::new(self.get_storage(name)?)
storage: Arc::new(self.get_storage(name)?),
};

networks.insert(name.to_owned(), network);
Expand All @@ -85,9 +102,11 @@ impl Config {
match net::Config::from_file(&path) {
None => {
error!("error while parsing config file: {:?}", path);
Err(Error::BlockchainConfigError("error while parsing network config file"))
},
Some(cfg) => Ok(cfg)
Err(Error::BlockchainConfigError(
"error while parsing network config file",
))
}
Some(cfg) => Ok(cfg),
}
}

Expand All @@ -96,7 +115,7 @@ impl Config {

if netcfg_dir.exists() {
self.network_names.insert(name.to_string());
return Ok(())
return Ok(());
}

let storage_config = self.get_storage_config(name);
Expand Down Expand Up @@ -132,15 +151,15 @@ pub type Networks = BTreeMap<String, Network>;
///
/// this will include all the cardano network you will connect to (mainnet, testnet, ...),
/// the different wallets you will create and all metadata.
pub static HERMES_PATH_ENV : &'static str = "HERMES_PATH";
pub static HERMES_PATH_ENV: &'static str = "HERMES_PATH";

/// the home directory hidden directory where to find Hermes files.
///
/// # TODO
///
/// This is not standard on windows, set the appropriate setting here
///
pub static HERMES_HOME_PATH : &'static str = ".hermes";
pub static HERMES_HOME_PATH: &'static str = ".hermes";

/// get the root directory of all the hermes path
///
Expand Down
51 changes: 30 additions & 21 deletions src/handlers/block.rs
@@ -1,26 +1,24 @@
use super::super::config::{Networks};
use cardano_storage::{tag, block_location, block_read_location};
use cardano::util::{hex, try_from_slice::TryFromSlice};
use super::super::config::Networks;
use cardano::block;
use std::sync::{Arc};
use cardano::util::{hex, try_from_slice::TryFromSlice};
use cardano_storage::{block_location, block_read_location, tag};
use std::sync::Arc;

use iron;
use iron::{Request, Response, IronResult};
use iron::status;
use iron::{IronResult, Request, Response};

use router;
use router::{Router};
use router::Router;

use super::common;

pub struct Handler {
networks: Arc<Networks>
networks: Arc<Networks>,
}
impl Handler {
pub fn new(networks: Arc<Networks>) -> Self {
Handler {
networks: networks
}
Handler { networks: networks }
}
pub fn route(self, router: &mut Router) -> &mut Router {
router.get(":network/block/:blockid", self, "block")
Expand All @@ -29,25 +27,38 @@ impl Handler {

impl iron::Handler for Handler {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let ref network_name = req.extensions.get::<router::Router>().unwrap().find("network").unwrap();
let ref network_name = req
.extensions
.get::<router::Router>()
.unwrap()
.find("network")
.unwrap();

if ! common::validate_network_name (network_name) {
if !common::validate_network_name(network_name) {
return Ok(Response::with(status::BadRequest));
}

let net = match self.networks.get(network_name.to_owned()) {
None => return Ok(Response::with(status::BadRequest)),
Some(net) => net
Some(net) => net,
};

let ref blockid = req.extensions.get::<router::Router>().unwrap().find("blockid").unwrap();
if ! blockid.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
let ref blockid = req
.extensions
.get::<router::Router>()
.unwrap()
.find("blockid")
.unwrap();
if !blockid
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '_')
{
error!("invalid blockid: {}", blockid);
return Ok(Response::with(status::BadRequest));
}
let hh_bytes = match tag::read(&net.storage, &blockid) {
None => hex::decode(&blockid).unwrap(),
Some(t) => t
Some(t) => t,
};
let hh = block::HeaderHash::try_from_slice(&hh_bytes).expect("blockid invalid");
info!("querying block header: {}", hh);
Expand All @@ -56,17 +67,15 @@ impl iron::Handler for Handler {
None => {
warn!("block `{}' does not exist", hh);
Ok(Response::with((status::NotFound, "Not Found")))
},
}
Some(loc) => {
debug!("blk location: {:?}", loc);
match block_read_location(&net.storage, &loc, &hh.into()) {
None => {
None => {
error!("error while reading block at location: {:?}", loc);
Ok(Response::with(status::InternalServerError))
},
Some(rblk) => {
Ok(Response::with((status::Ok, rblk.as_ref())))
}
Some(rblk) => Ok(Response::with((status::Ok, rblk.as_ref()))),
}
}
}
Expand Down
41 changes: 27 additions & 14 deletions src/handlers/common.rs
@@ -1,44 +1,57 @@
use super::super::config::{Network, Networks};
use cardano::block::EpochId;
use router::{Router};
use iron::{Request};
use super::super::config::{Networks, Network};
use iron::Request;
use router::Router;

pub fn validate_network_name(v: &&str) -> bool {
v.chars().all(|c| c.is_ascii_alphanumeric())
}

pub fn validate_epochid(v: &&str) -> Option<EpochId> {
if ! v.chars().all(|c| c.is_digit(10)) {
if !v.chars().all(|c| c.is_digit(10)) {
None
} else {
Some(v.parse::<EpochId>().unwrap())
}
}

pub fn get_network<'a>(req: &Request, networks : &'a Networks) -> Option<(String, &'a Network)> {
let ref network_name = req.extensions.get::<Router>().unwrap().find("network").unwrap();
pub fn get_network<'a>(req: &Request, networks: &'a Networks) -> Option<(String, &'a Network)> {
let ref network_name = req
.extensions
.get::<Router>()
.unwrap()
.find("network")
.unwrap();

if ! validate_network_name (network_name) {
if !validate_network_name(network_name) {
return None;
}

match networks.get(network_name.to_owned()) {
None => None,
Some(net) => Some((network_name.to_string(), net))
Some(net) => Some((network_name.to_string(), net)),
}
}

pub fn get_network_and_epoch<'a>(req: &Request, networks: &'a Networks) -> Option<(&'a Network, EpochId)> {
pub fn get_network_and_epoch<'a>(
req: &Request,
networks: &'a Networks,
) -> Option<(&'a Network, EpochId)> {
let (_, net) = get_network(req, networks)?;
let ref epochid_str = req.extensions.get::<Router>().unwrap().find("epochid").unwrap();
let ref epochid_str = req
.extensions
.get::<Router>()
.unwrap()
.find("epochid")
.unwrap();

let epochid = match validate_epochid (epochid_str) {
let epochid = match validate_epochid(epochid_str) {
None => {
error!("invalid epochid: {}", epochid_str);
return None;
},
}
Some(e) => e,
};

Some((net, epochid))
}
}
26 changes: 13 additions & 13 deletions src/handlers/epoch.rs
@@ -1,24 +1,22 @@
use cardano_storage::{epoch};
use cardano_storage::epoch;

use std::sync::{Arc};
use std::sync::Arc;

use iron;
use iron::{Request, Response, IronResult};
use iron::status;
use iron::{IronResult, Request, Response};

use router::{Router};
use router::Router;

use super::super::config::{Networks};
use super::super::config::Networks;
use super::common;

pub struct Handler {
networks: Arc<Networks>
networks: Arc<Networks>,
}
impl Handler {
pub fn new(networks: Arc<Networks>) -> Self {
Handler {
networks: networks
}
Handler { networks: networks }
}
pub fn route(self, router: &mut Router) -> &mut Router {
router.get(":network/epoch/:epochid", self, "epoch")
Expand All @@ -28,19 +26,21 @@ impl Handler {
impl iron::Handler for Handler {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let (net, epochid) = match common::get_network_and_epoch(req, &self.networks) {
None => { return Ok(Response::with(status::BadRequest)); }
Some(x) => x
None => {
return Ok(Response::with(status::BadRequest));
}
Some(x) => x,
};

let opackref = epoch::epoch_read_pack(&net.storage.config, epochid);
match opackref {
Err(_) => {
return Ok(Response::with(status::NotFound));
},
}
Ok(packref) => {
let path = net.storage.config.get_pack_filepath(&packref);
Ok(Response::with((status::Ok, path)))
},
}
}
}
}
6 changes: 3 additions & 3 deletions src/handlers/mod.rs
@@ -1,8 +1,8 @@
pub mod common;
pub mod block;
pub mod pack;
pub mod common;
pub mod epoch;
pub mod pack;
pub mod tip;
pub mod tx;
pub mod utxos;
pub mod utxos_delta;
pub mod utxos_delta;

0 comments on commit 210e582

Please sign in to comment.