Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' into fetch-tests
Browse files Browse the repository at this point in the history
Conflicts:
	dapps/src/lib.rs
  • Loading branch information
Tomasz Drwięga committed Dec 27, 2016
2 parents 17a86d1 + 8125b56 commit 2ed212b
Show file tree
Hide file tree
Showing 176 changed files with 1,729 additions and 1,710 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

42 changes: 21 additions & 21 deletions dapps/src/apps/fetcher/installers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,30 @@ fn write_response_and_check_hash(
response: fetch::Response
) -> Result<(fs::File, PathBuf), ValidationError> {
// try to parse id
let id = try!(id.parse().map_err(|_| ValidationError::InvalidContentId));
let id = id.parse().map_err(|_| ValidationError::InvalidContentId)?;

// check if content exists
if content_path.exists() {
warn!(target: "dapps", "Overwriting existing content at 0x{:?}", id);
try!(fs::remove_dir_all(&content_path))
fs::remove_dir_all(&content_path)?
}

// create directory
try!(fs::create_dir_all(&content_path));
fs::create_dir_all(&content_path)?;

// append filename
content_path.push(filename);

// Now write the response
let mut file = io::BufWriter::new(try!(fs::File::create(&content_path)));
let mut file = io::BufWriter::new(fs::File::create(&content_path)?);
let mut reader = io::BufReader::new(response);
try!(io::copy(&mut reader, &mut file));
try!(file.flush());
io::copy(&mut reader, &mut file)?;
file.flush()?;

// Validate hash
// TODO [ToDr] calculate sha3 in-flight while reading the response
let mut file = io::BufReader::new(try!(fs::File::open(&content_path)));
let hash = try!(sha3(&mut file));
let mut file = io::BufReader::new(fs::File::open(&content_path)?);
let hash = sha3(&mut file)?;
if id == hash {
Ok((file.into_inner(), content_path))
} else {
Expand Down Expand Up @@ -93,7 +93,7 @@ impl ContentValidator for Content {
fn validate_and_install(&self, response: fetch::Response) -> Result<ValidatorResponse, ValidationError> {
let validate = |content_path: PathBuf| {
// Create dir
let (_, content_path) = try!(write_response_and_check_hash(self.id.as_str(), content_path.clone(), self.id.as_str(), response));
let (_, content_path) = write_response_and_check_hash(self.id.as_str(), content_path.clone(), self.id.as_str(), response)?;

Ok(LocalPageEndpoint::single_file(content_path, self.mime.clone(), PageCache::Enabled))
};
Expand Down Expand Up @@ -131,7 +131,7 @@ impl Dapp {

fn find_manifest(zip: &mut zip::ZipArchive<fs::File>) -> Result<(Manifest, PathBuf), ValidationError> {
for i in 0..zip.len() {
let mut file = try!(zip.by_index(i));
let mut file = zip.by_index(i)?;

if !file.name().ends_with(MANIFEST_FILENAME) {
continue;
Expand Down Expand Up @@ -159,18 +159,18 @@ impl ContentValidator for Dapp {

fn validate_and_install(&self, response: fetch::Response) -> Result<ValidatorResponse, ValidationError> {
let validate = |dapp_path: PathBuf| {
let (file, zip_path) = try!(write_response_and_check_hash(self.id.as_str(), dapp_path.clone(), &format!("{}.zip", self.id), response));
let (file, zip_path) = write_response_and_check_hash(self.id.as_str(), dapp_path.clone(), &format!("{}.zip", self.id), response)?;
trace!(target: "dapps", "Opening dapp bundle at {:?}", zip_path);
// Unpack archive
let mut zip = try!(zip::ZipArchive::new(file));
let mut zip = zip::ZipArchive::new(file)?;
// First find manifest file
let (mut manifest, manifest_dir) = try!(Self::find_manifest(&mut zip));
let (mut manifest, manifest_dir) = Self::find_manifest(&mut zip)?;
// Overwrite id to match hash
manifest.id = self.id.clone();

// Unpack zip
for i in 0..zip.len() {
let mut file = try!(zip.by_index(i));
let mut file = zip.by_index(i)?;
let is_dir = file.name().chars().rev().next() == Some('/');

let file_path = PathBuf::from(file.name());
Expand All @@ -180,22 +180,22 @@ impl ContentValidator for Dapp {
let p = dapp_path.join(location_in_manifest_base);
// Check if it's a directory
if is_dir {
try!(fs::create_dir_all(p));
fs::create_dir_all(p)?;
} else {
let mut target = try!(fs::File::create(p));
try!(io::copy(&mut file, &mut target));
let mut target = fs::File::create(p)?;
io::copy(&mut file, &mut target)?;
}
}
}

// Remove zip
try!(fs::remove_file(&zip_path));
fs::remove_file(&zip_path)?;

// Write manifest
let manifest_str = try!(serialize_manifest(&manifest).map_err(ValidationError::ManifestSerialization));
let manifest_str = serialize_manifest(&manifest).map_err(ValidationError::ManifestSerialization)?;
let manifest_path = dapp_path.join(MANIFEST_FILENAME);
let mut manifest_file = try!(fs::File::create(manifest_path));
try!(manifest_file.write_all(manifest_str.as_bytes()));
let mut manifest_file = fs::File::create(manifest_path)?;
manifest_file.write_all(manifest_str.as_bytes())?;
// Create endpoint
let endpoint = LocalPageEndpoint::new(dapp_path, manifest.clone().into(), PageCache::Enabled, self.embeddable_on.clone());
Ok(endpoint)
Expand Down
6 changes: 3 additions & 3 deletions dapps/src/apps/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ fn local_dapps(dapps_path: String) -> Vec<LocalDapp> {

let files = files.expect("Check is done earlier");
files.map(|dir| {
let entry = try!(dir);
let file_type = try!(entry.file_type());
let entry = dir?;
let file_type = entry.file_type()?;

// skip files
if file_type.is_file() {
Expand Down Expand Up @@ -79,7 +79,7 @@ fn read_manifest(name: &str, mut path: PathBuf) -> EndpointInfo {
.and_then(|mut f| {
// Reat file
let mut s = String::new();
try!(f.read_to_string(&mut s).map_err(|e| format!("{:?}", e)));
f.read_to_string(&mut s).map_err(|e| format!("{:?}", e))?;
// Try to deserialize manifest
deserialize_manifest(s)
})
Expand Down
6 changes: 3 additions & 3 deletions dapps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl<T: Fetch> ServerBuilder<T> {
self.sync_status.clone(),
self.web_proxy_tokens.clone(),
self.remote.clone(),
try!(self.fetch_client()),
self.fetch_client()?,
)
}

Expand All @@ -220,7 +220,7 @@ impl<T: Fetch> ServerBuilder<T> {
self.sync_status.clone(),
self.web_proxy_tokens.clone(),
self.remote.clone(),
try!(self.fetch_client()),
self.fetch_client()?,
)
}

Expand Down Expand Up @@ -302,7 +302,7 @@ impl Server {
});
let hosts = Self::allowed_hosts(hosts, format!("{}", addr));

try!(hyper::Server::http(addr))
hyper::Server::http(addr)?
.handle(move |ctrl| router::Router::new(
ctrl,
signer_address.clone(),
Expand Down
6 changes: 3 additions & 3 deletions dapps/src/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ impl Url {
_ => None,
};

let port = try!(raw_url.port_or_known_default().ok_or_else(|| format!("Unknown port for scheme: `{}`", raw_url.scheme())));
let host = try!(raw_url.host().ok_or_else(|| "Valid host, because only data:, mailto: protocols does not have host.".to_owned())).to_owned();
let path = try!(raw_url.path_segments().ok_or_else(|| "Valid path segments. In HTTP we won't get cannot-be-a-base URLs".to_owned()))
let port = raw_url.port_or_known_default().ok_or_else(|| format!("Unknown port for scheme: `{}`", raw_url.scheme()))?;
let host = raw_url.host().ok_or_else(|| "Valid host, because only data:, mailto: protocols does not have host.".to_owned())?.to_owned();
let path = raw_url.path_segments().ok_or_else(|| "Valid path segments. In HTTP we won't get cannot-be-a-base URLs".to_owned())?
.map(|part| part.to_owned()).collect();
let query = raw_url.query().map(|x| x.to_owned());

Expand Down
28 changes: 14 additions & 14 deletions db/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ impl WriteCache {

match *cache_entry {
WriteCacheEntry::Write(ref val) => {
try!(batch.put(&key, val));
batch.put(&key, val)?;
},
WriteCacheEntry::Remove => {
try!(batch.delete(&key));
batch.delete(&key)?;
},
}
key.clone()
Expand All @@ -87,14 +87,14 @@ impl WriteCache {
removed_so_far = removed_so_far + 1;
}
if removed_so_far > 0 {
try!(db.write(batch));
db.write(batch)?;
}
Ok(())
}

/// flushes until cache is empty
fn flush_all(&mut self, db: &DB) -> Result<(), Error> {
while !self.is_empty() { try!(self.flush(db, FLUSH_BATCH_SIZE)); }
while !self.is_empty() { self.flush(db, FLUSH_BATCH_SIZE)?; }
Ok(())
}

Expand All @@ -104,7 +104,7 @@ impl WriteCache {

fn try_shrink(&mut self, db: &DB) -> Result<(), Error> {
if self.entries.len() > self.preferred_len {
try!(self.flush(db, FLUSH_BATCH_SIZE));
self.flush(db, FLUSH_BATCH_SIZE)?;
}
Ok(())
}
Expand Down Expand Up @@ -135,7 +135,7 @@ impl Database {
if db_lock.is_none() { return Ok(()); }
let db = db_lock.as_ref().unwrap();

try!(cache_lock.try_shrink(&db));
cache_lock.try_shrink(&db)?;
Ok(())
}

Expand All @@ -145,7 +145,7 @@ impl Database {
if db_lock.is_none() { return Ok(()); }
let db = db_lock.as_ref().expect("we should have exited with Ok(()) on the previous step");

try!(cache_lock.flush_all(&db));
cache_lock.flush_all(&db)?;
Ok(())

}
Expand Down Expand Up @@ -174,7 +174,7 @@ impl DatabaseService for Database {
opts.set_block_based_table_factory(&block_opts);
opts.set_prefix_extractor_fixed_size(size);
}
*db = Some(try!(DB::open(&opts, &path)));
*db = Some(DB::open(&opts, &path)?);

Ok(())
}
Expand All @@ -185,7 +185,7 @@ impl DatabaseService for Database {
}

fn close(&self) -> Result<(), Error> {
try!(self.flush_all());
self.flush_all()?;

let mut db = self.db.write();
if db.is_none() { return Err(Error::IsClosed); }
Expand Down Expand Up @@ -231,9 +231,9 @@ impl DatabaseService for Database {
}
}
let db_lock = self.db.read();
let db = try!(db_lock.as_ref().ok_or(Error::IsClosed));
let db = db_lock.as_ref().ok_or(Error::IsClosed)?;

match try!(db.get(key)) {
match db.get(key)? {
Some(db_vec) => {
Ok(Some(db_vec.to_vec()))
},
Expand All @@ -243,7 +243,7 @@ impl DatabaseService for Database {

fn get_by_prefix(&self, prefix: &[u8]) -> Result<Option<Vec<u8>>, Error> {
let db_lock = self.db.read();
let db = try!(db_lock.as_ref().ok_or(Error::IsClosed));
let db = db_lock.as_ref().ok_or(Error::IsClosed)?;

let mut iter = db.iterator(IteratorMode::From(prefix, Direction::Forward));
match iter.next() {
Expand All @@ -255,14 +255,14 @@ impl DatabaseService for Database {

fn is_empty(&self) -> Result<bool, Error> {
let db_lock = self.db.read();
let db = try!(db_lock.as_ref().ok_or(Error::IsClosed));
let db = db_lock.as_ref().ok_or(Error::IsClosed)?;

Ok(db.iterator(IteratorMode::Start).next().is_none())
}

fn iter(&self) -> Result<IteratorHandle, Error> {
let db_lock = self.db.read();
let db = try!(db_lock.as_ref().ok_or(Error::IsClosed));
let db = db_lock.as_ref().ok_or(Error::IsClosed)?;

let mut iterators = self.iterators.write();
let next_iterator = iterators.keys().last().unwrap_or(&0) + 1;
Expand Down
12 changes: 6 additions & 6 deletions db/src/lib.rs.in
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,27 @@ impl std::convert::From<nanoipc::SocketError> for ServiceError {

pub fn blocks_service_url(db_path: &str) -> Result<String, std::io::Error> {
let mut path = PathBuf::from(db_path);
try!(::std::fs::create_dir_all(db_path));
::std::fs::create_dir_all(db_path)?;
path.push("blocks.ipc");
Ok(format!("ipc://{}", path.to_str().unwrap()))
}

pub fn extras_service_url(db_path: &str) -> Result<String, ::std::io::Error> {
let mut path = PathBuf::from(db_path);
try!(::std::fs::create_dir_all(db_path));
::std::fs::create_dir_all(db_path)?;
path.push("extras.ipc");
Ok(format!("ipc://{}", path.to_str().unwrap()))
}

pub fn blocks_client(db_path: &str) -> Result<DatabaseConnection, ServiceError> {
let url = try!(blocks_service_url(db_path));
let client = try!(nanoipc::generic_client::<DatabaseClient<_>>(&url));
let url = blocks_service_url(db_path)?;
let client = nanoipc::generic_client::<DatabaseClient<_>>(&url)?;
Ok(client)
}

pub fn extras_client(db_path: &str) -> Result<DatabaseConnection, ServiceError> {
let url = try!(extras_service_url(db_path));
let client = try!(nanoipc::generic_client::<DatabaseClient<_>>(&url));
let url = extras_service_url(db_path)?;
let client = nanoipc::generic_client::<DatabaseClient<_>>(&url)?;
Ok(client)
}

Expand Down
14 changes: 7 additions & 7 deletions ethash/src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,17 @@ impl Light {
pub fn from_file(block_number: u64) -> io::Result<Light> {
let seed_compute = SeedHashCompute::new();
let path = Light::file_path(seed_compute.get_seedhash(block_number));
let mut file = try!(File::open(path));
let mut file = File::open(path)?;

let cache_size = get_cache_size(block_number);
if try!(file.metadata()).len() != cache_size as u64 {
if file.metadata()?.len() != cache_size as u64 {
return Err(io::Error::new(io::ErrorKind::Other, "Cache file size mismatch"));
}
let num_nodes = cache_size / NODE_BYTES;
let mut nodes: Vec<Node> = Vec::new();
nodes.resize(num_nodes, unsafe { mem::uninitialized() });
let buf = unsafe { slice::from_raw_parts_mut(nodes.as_mut_ptr() as *mut u8, cache_size) };
try!(file.read_exact(buf));
file.read_exact(buf)?;
Ok(Light {
cache: nodes,
block_number: block_number,
Expand All @@ -144,16 +144,16 @@ impl Light {

if deprecated.exists() {
debug!(target: "ethash", "removing: {:?}", &deprecated);
try!(fs::remove_file(deprecated));
fs::remove_file(deprecated)?;
}
}

try!(fs::create_dir_all(path.parent().unwrap()));
let mut file = try!(File::create(&path));
fs::create_dir_all(path.parent().unwrap())?;
let mut file = File::create(&path)?;

let cache_size = self.cache.len() * NODE_BYTES;
let buf = unsafe { slice::from_raw_parts(self.cache.as_ptr() as *const u8, cache_size) };
try!(file.write(buf));
file.write(buf)?;
Ok(path)
}
}
Expand Down
Loading

0 comments on commit 2ed212b

Please sign in to comment.