2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-12-11 19:15:58 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! Hash-addressed content resolver & fetcher.
|
|
|
|
|
|
|
|
use std::{io, fs};
|
2016-12-22 18:26:39 +01:00
|
|
|
use std::io::Write;
|
2016-12-11 19:15:58 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2016-12-22 18:26:39 +01:00
|
|
|
use fetch::{Fetch, Response, Error as FetchError, Client as FetchClient};
|
|
|
|
use futures::Future;
|
|
|
|
use parity_reactor::Remote;
|
2016-12-11 19:15:58 +01:00
|
|
|
use urlhint::{ContractClient, URLHintContract, URLHint, URLHintResult};
|
2016-12-22 18:26:39 +01:00
|
|
|
use util::{H256, sha3};
|
2016-12-11 19:15:58 +01:00
|
|
|
|
|
|
|
/// API for fetching by hash.
|
|
|
|
pub trait HashFetch: Send + Sync + 'static {
|
|
|
|
/// Fetch hash-addressed content.
|
|
|
|
/// Parameters:
|
|
|
|
/// 1. `hash` - content hash
|
|
|
|
/// 2. `on_done` - callback function invoked when the content is ready (or there was error during fetch)
|
|
|
|
///
|
|
|
|
/// This function may fail immediately when fetch cannot be initialized or content cannot be resolved.
|
2016-12-22 18:26:39 +01:00
|
|
|
fn fetch(&self, hash: H256, on_done: Box<Fn(Result<PathBuf, Error>) + Send>);
|
2016-12-11 19:15:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Hash-fetching error.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
/// Hash could not be resolved to a valid content address.
|
|
|
|
NoResolution,
|
|
|
|
/// Downloaded content hash does not match.
|
|
|
|
HashMismatch {
|
|
|
|
/// Expected hash
|
|
|
|
expected: H256,
|
|
|
|
/// Computed hash
|
|
|
|
got: H256,
|
|
|
|
},
|
2017-02-20 16:30:14 +01:00
|
|
|
/// Server didn't respond with OK status.
|
|
|
|
InvalidStatus,
|
2016-12-11 19:15:58 +01:00
|
|
|
/// IO Error while validating hash.
|
|
|
|
IO(io::Error),
|
|
|
|
/// Error during fetch.
|
|
|
|
Fetch(FetchError),
|
|
|
|
}
|
|
|
|
|
2017-02-20 16:30:14 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
impl PartialEq for Error {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
use Error::*;
|
|
|
|
match (self, other) {
|
|
|
|
(&HashMismatch { expected, got }, &HashMismatch { expected: e, got: g }) => {
|
|
|
|
expected == e && got == g
|
|
|
|
},
|
|
|
|
(&NoResolution, &NoResolution) => true,
|
|
|
|
(&InvalidStatus, &InvalidStatus) => true,
|
|
|
|
(&IO(_), &IO(_)) => true,
|
|
|
|
(&Fetch(_), &Fetch(_)) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 19:15:58 +01:00
|
|
|
impl From<FetchError> for Error {
|
|
|
|
fn from(error: FetchError) -> Self {
|
|
|
|
Error::Fetch(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<io::Error> for Error {
|
|
|
|
fn from(error: io::Error) -> Self {
|
|
|
|
Error::IO(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Default Hash-fetching client using on-chain contract to resolve hashes to URLs.
|
2016-12-22 18:26:39 +01:00
|
|
|
pub struct Client<F: Fetch + 'static = FetchClient> {
|
2016-12-11 19:15:58 +01:00
|
|
|
contract: URLHintContract,
|
2016-12-22 18:26:39 +01:00
|
|
|
fetch: F,
|
|
|
|
remote: Remote,
|
2017-04-12 15:04:53 +02:00
|
|
|
random_path: Arc<Fn() -> PathBuf + Sync + Send>,
|
2016-12-11 19:15:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
2016-12-22 18:26:39 +01:00
|
|
|
/// Creates new instance of the `Client` given on-chain contract client and task runner.
|
|
|
|
pub fn new(contract: Arc<ContractClient>, remote: Remote) -> Self {
|
|
|
|
Client::with_fetch(contract, FetchClient::new().unwrap(), remote)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: Fetch + 'static> Client<F> {
|
|
|
|
|
|
|
|
/// Creates new instance of the `Client` given on-chain contract client, fetch service and task runner.
|
|
|
|
pub fn with_fetch(contract: Arc<ContractClient>, fetch: F, remote: Remote) -> Self {
|
2016-12-11 19:15:58 +01:00
|
|
|
Client {
|
|
|
|
contract: URLHintContract::new(contract),
|
2016-12-22 18:26:39 +01:00
|
|
|
fetch: fetch,
|
|
|
|
remote: remote,
|
2017-04-12 15:04:53 +02:00
|
|
|
random_path: Arc::new(random_temp_path),
|
2016-12-11 19:15:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 18:26:39 +01:00
|
|
|
impl<F: Fetch + 'static> HashFetch for Client<F> {
|
|
|
|
fn fetch(&self, hash: H256, on_done: Box<Fn(Result<PathBuf, Error>) + Send>) {
|
2016-12-11 19:15:58 +01:00
|
|
|
debug!(target: "fetch", "Fetching: {:?}", hash);
|
|
|
|
|
2016-12-22 18:26:39 +01:00
|
|
|
let url = self.contract.resolve(hash.to_vec()).map(|content| match content {
|
2016-12-11 19:15:58 +01:00
|
|
|
URLHintResult::Dapp(dapp) => {
|
|
|
|
dapp.url()
|
|
|
|
},
|
|
|
|
URLHintResult::Content(content) => {
|
|
|
|
content.url
|
|
|
|
},
|
2016-12-22 18:26:39 +01:00
|
|
|
}).ok_or_else(|| Error::NoResolution);
|
2016-12-11 19:15:58 +01:00
|
|
|
|
|
|
|
debug!(target: "fetch", "Resolved {:?} to {:?}. Fetching...", hash, url);
|
|
|
|
|
2016-12-22 18:26:39 +01:00
|
|
|
match url {
|
|
|
|
Err(err) => on_done(Err(err)),
|
|
|
|
Ok(url) => {
|
2017-04-12 15:04:53 +02:00
|
|
|
let random_path = self.random_path.clone();
|
2016-12-22 18:26:39 +01:00
|
|
|
let future = self.fetch.fetch(&url).then(move |result| {
|
|
|
|
fn validate_hash(path: PathBuf, hash: H256, result: Result<Response, FetchError>) -> Result<PathBuf, Error> {
|
2016-12-27 12:53:56 +01:00
|
|
|
let response = result?;
|
2017-02-20 16:30:14 +01:00
|
|
|
if !response.is_success() {
|
|
|
|
return Err(Error::InvalidStatus);
|
|
|
|
}
|
|
|
|
|
2016-12-22 18:26:39 +01:00
|
|
|
// Read the response
|
|
|
|
let mut reader = io::BufReader::new(response);
|
2016-12-27 12:53:56 +01:00
|
|
|
let mut writer = io::BufWriter::new(fs::File::create(&path)?);
|
|
|
|
io::copy(&mut reader, &mut writer)?;
|
|
|
|
writer.flush()?;
|
2016-12-22 18:26:39 +01:00
|
|
|
|
|
|
|
// And validate the hash
|
2016-12-27 12:53:56 +01:00
|
|
|
let mut file_reader = io::BufReader::new(fs::File::open(&path)?);
|
|
|
|
let content_hash = sha3(&mut file_reader)?;
|
2016-12-22 18:26:39 +01:00
|
|
|
if content_hash != hash {
|
|
|
|
Err(Error::HashMismatch{ got: content_hash, expected: hash })
|
|
|
|
} else {
|
|
|
|
Ok(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug!(target: "fetch", "Content fetched, validating hash ({:?})", hash);
|
2017-04-12 15:04:53 +02:00
|
|
|
let path = random_path();
|
2016-12-22 18:26:39 +01:00
|
|
|
let res = validate_hash(path.clone(), hash, result);
|
|
|
|
if let Err(ref err) = res {
|
|
|
|
trace!(target: "fetch", "Error: {:?}", err);
|
|
|
|
// Remove temporary file in case of error
|
2017-04-12 15:04:53 +02:00
|
|
|
let _ = fs::remove_file(&path);
|
2016-12-22 18:26:39 +01:00
|
|
|
}
|
|
|
|
on_done(res);
|
|
|
|
|
|
|
|
Ok(()) as Result<(), ()>
|
|
|
|
});
|
|
|
|
self.remote.spawn(self.fetch.process(future));
|
|
|
|
},
|
|
|
|
}
|
2016-12-11 19:15:58 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-22 18:26:39 +01:00
|
|
|
|
|
|
|
fn random_temp_path() -> PathBuf {
|
|
|
|
use ::rand::Rng;
|
|
|
|
use ::std::env;
|
|
|
|
|
|
|
|
let mut rng = ::rand::OsRng::new().expect("Reliable random source is required to work.");
|
|
|
|
let file: String = rng.gen_ascii_chars().take(12).collect();
|
|
|
|
|
|
|
|
let mut path = env::temp_dir();
|
|
|
|
path.push(file);
|
|
|
|
path
|
|
|
|
}
|
2017-02-20 16:30:14 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::sync::{Arc, mpsc};
|
|
|
|
use util::{Mutex, FromHex};
|
|
|
|
use futures::future;
|
|
|
|
use fetch::{self, Fetch};
|
|
|
|
use parity_reactor::Remote;
|
|
|
|
use urlhint::tests::{FakeRegistrar, URLHINT};
|
2017-04-12 15:04:53 +02:00
|
|
|
use super::{Error, Client, HashFetch, random_temp_path};
|
2017-02-20 16:30:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct FakeFetch {
|
|
|
|
return_success: bool
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Fetch for FakeFetch {
|
|
|
|
type Result = future::Ok<fetch::Response, fetch::Error>;
|
|
|
|
|
|
|
|
fn new() -> Result<Self, fetch::Error> where Self: Sized {
|
|
|
|
Ok(FakeFetch { return_success: true })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fetch_with_abort(&self, url: &str, _abort: fetch::Abort) -> Self::Result {
|
|
|
|
assert_eq!(url, "https://ethcore.io/assets/images/ethcore-black-horizontal.png");
|
|
|
|
future::ok(if self.return_success {
|
|
|
|
let cursor = ::std::io::Cursor::new(b"result");
|
|
|
|
fetch::Response::from_reader(cursor)
|
|
|
|
} else {
|
|
|
|
fetch::Response::not_found()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn registrar() -> FakeRegistrar {
|
|
|
|
let mut registrar = FakeRegistrar::new();
|
|
|
|
registrar.responses = Mutex::new(vec![
|
|
|
|
Ok(format!("000000000000000000000000{}", URLHINT).from_hex().unwrap()),
|
|
|
|
Ok("00000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000deadcafebeefbeefcafedeaddeedfeedffffffff000000000000000000000000000000000000000000000000000000000000003d68747470733a2f2f657468636f72652e696f2f6173736574732f696d616765732f657468636f72652d626c61636b2d686f72697a6f6e74616c2e706e67000000".from_hex().unwrap()),
|
|
|
|
]);
|
|
|
|
registrar
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_return_error_if_hash_not_found() {
|
|
|
|
// given
|
|
|
|
let contract = Arc::new(FakeRegistrar::new());
|
|
|
|
let fetch = FakeFetch { return_success: false };
|
|
|
|
let client = Client::with_fetch(contract.clone(), fetch, Remote::new_sync());
|
|
|
|
|
|
|
|
// when
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
client.fetch(2.into(), Box::new(move |result| {
|
|
|
|
tx.send(result).unwrap();
|
|
|
|
}));
|
|
|
|
|
|
|
|
// then
|
|
|
|
let result = rx.recv().unwrap();
|
|
|
|
assert_eq!(result.unwrap_err(), Error::NoResolution);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_return_error_if_response_is_not_successful() {
|
|
|
|
// given
|
|
|
|
let registrar = Arc::new(registrar());
|
|
|
|
let fetch = FakeFetch { return_success: false };
|
|
|
|
let client = Client::with_fetch(registrar.clone(), fetch, Remote::new_sync());
|
|
|
|
|
|
|
|
// when
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
client.fetch(2.into(), Box::new(move |result| {
|
|
|
|
tx.send(result).unwrap();
|
|
|
|
}));
|
|
|
|
|
|
|
|
// then
|
|
|
|
let result = rx.recv().unwrap();
|
|
|
|
assert_eq!(result.unwrap_err(), Error::InvalidStatus);
|
|
|
|
}
|
2017-04-12 15:04:53 +02:00
|
|
|
|
2017-02-20 16:30:14 +01:00
|
|
|
#[test]
|
|
|
|
fn should_return_hash_mismatch() {
|
|
|
|
// given
|
|
|
|
let registrar = Arc::new(registrar());
|
|
|
|
let fetch = FakeFetch { return_success: true };
|
2017-04-12 15:04:53 +02:00
|
|
|
let mut client = Client::with_fetch(registrar.clone(), fetch, Remote::new_sync());
|
|
|
|
let path = random_temp_path();
|
|
|
|
let path2 = path.clone();
|
|
|
|
client.random_path = Arc::new(move || path2.clone());
|
2017-02-20 16:30:14 +01:00
|
|
|
|
|
|
|
// when
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
client.fetch(2.into(), Box::new(move |result| {
|
|
|
|
tx.send(result).unwrap();
|
|
|
|
}));
|
|
|
|
|
|
|
|
// then
|
|
|
|
let result = rx.recv().unwrap();
|
|
|
|
let hash = "0x06b0a4f426f6713234b2d4b2468640bc4e0bb72657a920ad24c5087153c593c8".into();
|
|
|
|
assert_eq!(result.unwrap_err(), Error::HashMismatch { expected: 2.into(), got: hash });
|
2017-04-12 15:04:53 +02:00
|
|
|
assert!(!path.exists(), "Temporary file should be removed.");
|
2017-02-20 16:30:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_return_path_if_hash_matches() {
|
|
|
|
// given
|
|
|
|
let registrar = Arc::new(registrar());
|
|
|
|
let fetch = FakeFetch { return_success: true };
|
|
|
|
let client = Client::with_fetch(registrar.clone(), fetch, Remote::new_sync());
|
|
|
|
|
|
|
|
// when
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
client.fetch("0x06b0a4f426f6713234b2d4b2468640bc4e0bb72657a920ad24c5087153c593c8".into(), Box::new(move |result| {
|
|
|
|
tx.send(result).unwrap();
|
|
|
|
}));
|
|
|
|
|
|
|
|
// then
|
|
|
|
let result = rx.recv().unwrap();
|
|
|
|
assert!(result.is_ok(), "Should return path, got: {:?}", result);
|
|
|
|
}
|
|
|
|
}
|