From 2d19a6726a840739952ed0099b8fa3b3e1e4f2a2 Mon Sep 17 00:00:00 2001 From: Craig OConnor Date: Thu, 1 Jun 2017 08:16:47 -0400 Subject: [PATCH 01/32] RwLock --- dapps/src/apps/mod.rs | 4 ++-- dapps/src/endpoint.rs | 4 ++-- dapps/src/lib.rs | 2 +- dapps/src/router.rs | 8 ++++---- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/dapps/src/apps/mod.rs b/dapps/src/apps/mod.rs index b3c5a5cef3b..f70bf77c47f 100644 --- a/dapps/src/apps/mod.rs +++ b/dapps/src/apps/mod.rs @@ -16,7 +16,7 @@ use std::collections::BTreeMap; use std::path::PathBuf; -use std::sync::Arc; +use std::sync::{Arc, RwLock}; use endpoint::{Endpoints, Endpoint}; use page::PageEndpoint; @@ -80,7 +80,7 @@ pub fn all_endpoints( pages.insert("proxy".into(), ProxyPac::boxed(ui_address.clone(), dapps_domain)); pages.insert(WEB_PATH.into(), Web::boxed(ui_address.clone(), web_proxy_tokens.clone(), remote.clone(), fetch.clone())); - Arc::new(pages) + Arc::new(RwLock::new(pages)) } fn insert(pages: &mut BTreeMap>, id: &str, embed_at: Embeddable) { diff --git a/dapps/src/endpoint.rs b/dapps/src/endpoint.rs index ea8fd0a3842..c9625150225 100644 --- a/dapps/src/endpoint.rs +++ b/dapps/src/endpoint.rs @@ -16,7 +16,7 @@ //! URL Endpoint traits -use std::sync::Arc; +use std::sync::{Arc, RwLock}; use std::collections::BTreeMap; use hyper::{self, server, net}; @@ -39,7 +39,7 @@ pub struct EndpointInfo { pub icon_url: String, } -pub type Endpoints = Arc>>; +pub type Endpoints = Arc>>>; pub type Handler = server::Handler + Send; pub trait Endpoint : Send + Sync { diff --git a/dapps/src/lib.rs b/dapps/src/lib.rs index 0860f0c1092..f6406cf66c0 100644 --- a/dapps/src/lib.rs +++ b/dapps/src/lib.rs @@ -106,7 +106,7 @@ pub struct Endpoints { impl Endpoints { /// Returns a current list of app endpoints. pub fn list(&self) -> Vec { - self.endpoints.iter().filter_map(|(ref k, ref e)| { + self.endpoints.read().unwrap().iter().filter_map(|(ref k, ref e)| { e.info().map(|ref info| apps::App::from_info(k, info)) }).collect() } diff --git a/dapps/src/router.rs b/dapps/src/router.rs index b3454c78274..76497c4cea2 100644 --- a/dapps/src/router.rs +++ b/dapps/src/router.rs @@ -59,6 +59,7 @@ impl http::RequestMiddleware for Router { let is_origin_set = req.headers().get::().is_some(); let is_get_request = *req.method() == hyper::Method::Get; let is_head_request = *req.method() == hyper::Method::Head; + let endpoint_borrow = self.endpoints.as_ref().unwrap().clone(); trace!(target: "dapps", "Routing request to {:?}. Details: {:?}", url, req); @@ -68,7 +69,7 @@ impl http::RequestMiddleware for Router { // Handle invalid web requests that we can recover from (ref path, SpecialEndpoint::None, Some((ref referer, ref referer_url))) if referer.app_id == apps::WEB_PATH - && self.endpoints.as_ref().map(|ep| ep.contains_key(apps::WEB_PATH)).unwrap_or(false) + && Some(endpoint_borrow.read().unwrap()).map(|ep| ep.contains_key(apps::WEB_PATH)).unwrap_or(false) && !is_web_endpoint(path) => { @@ -87,10 +88,9 @@ impl http::RequestMiddleware for Router { .map(|special| special.to_async_handler(path.clone().unwrap_or_default(), control)) }, // Then delegate to dapp - (Some(ref path), _, _) if self.endpoints.as_ref().map(|ep| ep.contains_key(&path.app_id)).unwrap_or(false) => { + (Some(ref path), _, _) if Some(endpoint_borrow.read().unwrap()).map(|ep| ep.contains_key(&path.app_id)).unwrap_or(false) => { trace!(target: "dapps", "Resolving to local/builtin dapp."); - Some(self.endpoints - .as_ref() + Some(Some(endpoint_borrow.read().unwrap()) .expect("endpoints known to be set; qed") .get(&path.app_id) .expect("endpoints known to contain key; qed") From 11f4fa5c95d0f360c334848e3cfe90b1dc8fba9c Mon Sep 17 00:00:00 2001 From: Craig OConnor Date: Fri, 2 Jun 2017 01:10:04 -0400 Subject: [PATCH 02/32] getting there --- dapps/src/router.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dapps/src/router.rs b/dapps/src/router.rs index 76497c4cea2..eeb1c2e84c1 100644 --- a/dapps/src/router.rs +++ b/dapps/src/router.rs @@ -59,7 +59,7 @@ impl http::RequestMiddleware for Router { let is_origin_set = req.headers().get::().is_some(); let is_get_request = *req.method() == hyper::Method::Get; let is_head_request = *req.method() == hyper::Method::Head; - let endpoint_borrow = self.endpoints.as_ref().unwrap().clone(); + let endpoint_borrow = self.endpoints.as_ref().clone().unwrap(); trace!(target: "dapps", "Routing request to {:?}. Details: {:?}", url, req); @@ -69,7 +69,7 @@ impl http::RequestMiddleware for Router { // Handle invalid web requests that we can recover from (ref path, SpecialEndpoint::None, Some((ref referer, ref referer_url))) if referer.app_id == apps::WEB_PATH - && Some(endpoint_borrow.read().unwrap()).map(|ep| ep.contains_key(apps::WEB_PATH)).unwrap_or(false) + && endpoint_borrow.into_inner().map(|ep| ep.contains_key(apps::WEB_PATH)).unwrap_or(false) && !is_web_endpoint(path) => { @@ -88,9 +88,9 @@ impl http::RequestMiddleware for Router { .map(|special| special.to_async_handler(path.clone().unwrap_or_default(), control)) }, // Then delegate to dapp - (Some(ref path), _, _) if Some(endpoint_borrow.read().unwrap()).map(|ep| ep.contains_key(&path.app_id)).unwrap_or(false) => { + (Some(ref path), _, _) if endpoint_borrow.into_inner().map(|ep| ep.contains_key(&path.app_id)).unwrap_or(false) => { trace!(target: "dapps", "Resolving to local/builtin dapp."); - Some(Some(endpoint_borrow.read().unwrap()) + Some(endpoint_borrow.into_inner() .expect("endpoints known to be set; qed") .get(&path.app_id) .expect("endpoints known to contain key; qed") From 1c8022118e2bf5fb0eec0694e35a92a6ed1e2e50 Mon Sep 17 00:00:00 2001 From: Craig OConnor Date: Fri, 2 Jun 2017 13:54:09 -0400 Subject: [PATCH 03/32] argh --- dapps/src/router.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/dapps/src/router.rs b/dapps/src/router.rs index eeb1c2e84c1..d27e25891a9 100644 --- a/dapps/src/router.rs +++ b/dapps/src/router.rs @@ -59,9 +59,10 @@ impl http::RequestMiddleware for Router { let is_origin_set = req.headers().get::().is_some(); let is_get_request = *req.method() == hyper::Method::Get; let is_head_request = *req.method() == hyper::Method::Head; - let endpoint_borrow = self.endpoints.as_ref().clone().unwrap(); + info!(target: "dapps", "Routing request to {:?}. Details: {:?}", url, req); trace!(target: "dapps", "Routing request to {:?}. Details: {:?}", url, req); + let endpoint_borrow = Some(self.endpoints.as_ref().unwrap().read().unwrap()); let control = control.clone(); debug!(target: "dapps", "Handling endpoint request: {:?}", endpoint); @@ -69,7 +70,7 @@ impl http::RequestMiddleware for Router { // Handle invalid web requests that we can recover from (ref path, SpecialEndpoint::None, Some((ref referer, ref referer_url))) if referer.app_id == apps::WEB_PATH - && endpoint_borrow.into_inner().map(|ep| ep.contains_key(apps::WEB_PATH)).unwrap_or(false) + && endpoint_borrow.as_ref().map(|ep| ep.contains_key(apps::WEB_PATH)).unwrap_or(false) && !is_web_endpoint(path) => { @@ -88,9 +89,10 @@ impl http::RequestMiddleware for Router { .map(|special| special.to_async_handler(path.clone().unwrap_or_default(), control)) }, // Then delegate to dapp - (Some(ref path), _, _) if endpoint_borrow.into_inner().map(|ep| ep.contains_key(&path.app_id)).unwrap_or(false) => { + (Some(ref path), _, _) if endpoint_borrow.as_ref().map(|ep| ep.contains_key(&path.app_id)).unwrap_or(false) => { trace!(target: "dapps", "Resolving to local/builtin dapp."); - Some(endpoint_borrow.into_inner() + Some(endpoint_borrow + .as_ref() .expect("endpoints known to be set; qed") .get(&path.app_id) .expect("endpoints known to contain key; qed") From 9adfdc069b0efce10669e15148518c93d0382d30 Mon Sep 17 00:00:00 2001 From: Craig OConnor Date: Fri, 2 Jun 2017 15:06:20 -0400 Subject: [PATCH 04/32] parking_lot --- Cargo.lock | 1 + dapps/Cargo.toml | 1 + dapps/src/apps/mod.rs | 3 ++- dapps/src/endpoint.rs | 3 ++- dapps/src/lib.rs | 3 ++- dapps/src/router.rs | 8 ++++---- 6 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 49b0c524606..e521e82e493 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1645,6 +1645,7 @@ dependencies = [ "parity-hash-fetch 1.7.0", "parity-reactor 0.1.0", "parity-ui 1.7.0", + "parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/dapps/Cargo.toml b/dapps/Cargo.toml index 429ed01f5b6..173bf145e0a 100644 --- a/dapps/Cargo.toml +++ b/dapps/Cargo.toml @@ -14,6 +14,7 @@ futures = "0.1" linked-hash-map = "0.3" log = "0.3" parity-dapps-glue = "1.7" +parking_lot = "0.4" mime = "0.2" mime_guess = "1.6.1" rand = "0.3" diff --git a/dapps/src/apps/mod.rs b/dapps/src/apps/mod.rs index f70bf77c47f..1b53dae4d0e 100644 --- a/dapps/src/apps/mod.rs +++ b/dapps/src/apps/mod.rs @@ -16,7 +16,8 @@ use std::collections::BTreeMap; use std::path::PathBuf; -use std::sync::{Arc, RwLock}; +use std::sync::Arc; +use parking_lot::RwLock; use endpoint::{Endpoints, Endpoint}; use page::PageEndpoint; diff --git a/dapps/src/endpoint.rs b/dapps/src/endpoint.rs index c9625150225..94c86c0ee2e 100644 --- a/dapps/src/endpoint.rs +++ b/dapps/src/endpoint.rs @@ -16,7 +16,8 @@ //! URL Endpoint traits -use std::sync::{Arc, RwLock}; +use std::sync::Arc; +use parking_lot::RwLock; use std::collections::BTreeMap; use hyper::{self, server, net}; diff --git a/dapps/src/lib.rs b/dapps/src/lib.rs index f6406cf66c0..683005ab90a 100644 --- a/dapps/src/lib.rs +++ b/dapps/src/lib.rs @@ -41,6 +41,7 @@ extern crate parity_dapps_glue as parity_dapps; extern crate parity_hash_fetch as hash_fetch; extern crate parity_reactor; extern crate parity_ui; +extern crate parking_lot; #[macro_use] extern crate log; @@ -106,7 +107,7 @@ pub struct Endpoints { impl Endpoints { /// Returns a current list of app endpoints. pub fn list(&self) -> Vec { - self.endpoints.read().unwrap().iter().filter_map(|(ref k, ref e)| { + self.endpoints.read().iter().filter_map(|(ref k, ref e)| { e.info().map(|ref info| apps::App::from_info(k, info)) }).collect() } diff --git a/dapps/src/router.rs b/dapps/src/router.rs index d27e25891a9..6ac18178e1e 100644 --- a/dapps/src/router.rs +++ b/dapps/src/router.rs @@ -62,7 +62,7 @@ impl http::RequestMiddleware for Router { info!(target: "dapps", "Routing request to {:?}. Details: {:?}", url, req); trace!(target: "dapps", "Routing request to {:?}. Details: {:?}", url, req); - let endpoint_borrow = Some(self.endpoints.as_ref().unwrap().read().unwrap()); + let endpoints = self.endpoints.as_ref().map(|endpoints| endpoints.read()); let control = control.clone(); debug!(target: "dapps", "Handling endpoint request: {:?}", endpoint); @@ -70,7 +70,7 @@ impl http::RequestMiddleware for Router { // Handle invalid web requests that we can recover from (ref path, SpecialEndpoint::None, Some((ref referer, ref referer_url))) if referer.app_id == apps::WEB_PATH - && endpoint_borrow.as_ref().map(|ep| ep.contains_key(apps::WEB_PATH)).unwrap_or(false) + && endpoints.as_ref().map(|ep| ep.contains_key(apps::WEB_PATH)).unwrap_or(false) && !is_web_endpoint(path) => { @@ -89,9 +89,9 @@ impl http::RequestMiddleware for Router { .map(|special| special.to_async_handler(path.clone().unwrap_or_default(), control)) }, // Then delegate to dapp - (Some(ref path), _, _) if endpoint_borrow.as_ref().map(|ep| ep.contains_key(&path.app_id)).unwrap_or(false) => { + (Some(ref path), _, _) if endpoints.as_ref().map(|ep| ep.contains_key(&path.app_id)).unwrap_or(false) => { trace!(target: "dapps", "Resolving to local/builtin dapp."); - Some(endpoint_borrow + Some(endpoints .as_ref() .expect("endpoints known to be set; qed") .get(&path.app_id) From c0e00227d37635580edc23aab5d74d86e192d82b Mon Sep 17 00:00:00 2001 From: Craig OConnor Date: Thu, 22 Jun 2017 09:46:06 -0600 Subject: [PATCH 05/32] rpc --- Cargo.lock | 1 + dapps/src/apps/mod.rs | 14 ++++++++++++++ dapps/src/router.rs | 3 ++- js/src/api/rpc/parity/parity.js | 5 +++++ js/src/jsonrpc/interfaces/parity.js | 11 +++++++++++ js/src/views/Dapps/dapps.js | 11 +++++++++++ js/src/views/Dapps/dappsStore.js | 10 ++++++++++ rpc/Cargo.toml | 1 + rpc/src/lib.rs | 1 + rpc/src/v1/impls/light/parity.rs | 4 ++++ rpc/src/v1/impls/parity.rs | 5 +++++ rpc/src/v1/traits/parity.rs | 4 ++++ 12 files changed, 69 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 179660fa075..033c2d95f8d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1733,6 +1733,7 @@ dependencies = [ "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "multihash 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "order-stat 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "parity-dapps 1.7.0", "parity-reactor 0.1.0", "parity-updater 1.7.0", "pretty_assertions 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/dapps/src/apps/mod.rs b/dapps/src/apps/mod.rs index 1b53dae4d0e..272fcd3bb6c 100644 --- a/dapps/src/apps/mod.rs +++ b/dapps/src/apps/mod.rs @@ -84,6 +84,20 @@ pub fn all_endpoints( Arc::new(RwLock::new(pages)) } +pub fn refresh_local_endpoints(endpoints: Arc>>>) { + let endpoints = endpoints.as_ref().map(|endpoints| endpoints.read()); + + let mut pages = fs::local_endpoints(dapps_path, ui_address.clone()); + + for (k, _) in pages { + if pages.contains_key(k) != false { + endpoints.insert() + } + } + + +} + fn insert(pages: &mut BTreeMap>, id: &str, embed_at: Embeddable) { pages.insert(id.to_owned(), Box::new(match embed_at { Embeddable::Yes(address) => PageEndpoint::new_safe_to_embed(T::default(), address), diff --git a/dapps/src/router.rs b/dapps/src/router.rs index 6ac18178e1e..3bc906f25ee 100644 --- a/dapps/src/router.rs +++ b/dapps/src/router.rs @@ -60,7 +60,8 @@ impl http::RequestMiddleware for Router { let is_get_request = *req.method() == hyper::Method::Get; let is_head_request = *req.method() == hyper::Method::Head; - info!(target: "dapps", "Routing request to {:?}. Details: {:?}", url, req); + // info!(target: "dapps", "Routing request to {:?}. Details: {:?}", url, req); + trace!(target: "dapps", "Routing request to {:?}. Details: {:?}", url, req); let endpoints = self.endpoints.as_ref().map(|endpoints| endpoints.read()); diff --git a/js/src/api/rpc/parity/parity.js b/js/src/api/rpc/parity/parity.js index 4fdaf5b1beb..c2681b3fbc0 100644 --- a/js/src/api/rpc/parity/parity.js +++ b/js/src/api/rpc/parity/parity.js @@ -95,6 +95,11 @@ export default class Parity { .execute('parity_dappsList'); } + dappsRefresh () { + return this._transport + .execute('parity_dappsRefresh'); + } + dappsUrl () { return this._transport .execute('parity_dappsUrl'); diff --git a/js/src/jsonrpc/interfaces/parity.js b/js/src/jsonrpc/interfaces/parity.js index 326ca98314a..b75d42f4751 100644 --- a/js/src/jsonrpc/interfaces/parity.js +++ b/js/src/jsonrpc/interfaces/parity.js @@ -163,6 +163,17 @@ export default { } }, + dappsRefresh: { + subdoc: SUBDOC_SET, + desc: 'Returns a boolean value upon success and error upon failure', + params: [], + returns: { + type: bool, + desc: 'True for success. error details for failure', + example: true + } + }, + dappsUrl: { section: SECTION_NODE, desc: 'Returns the hostname and the port of dapps/rpc server, error if not enabled.', diff --git a/js/src/views/Dapps/dapps.js b/js/src/views/Dapps/dapps.js index 5e5efedf79e..a19106d6658 100644 --- a/js/src/views/Dapps/dapps.js +++ b/js/src/views/Dapps/dapps.js @@ -90,6 +90,17 @@ class Dapps extends Component { /> } buttons={ [ +