Merge branch 'master' into switchrpcns
This commit is contained in:
commit
bbd024d646
3
Cargo.lock
generated
3
Cargo.lock
generated
@ -272,6 +272,7 @@ dependencies = [
|
|||||||
"jsonrpc-core 2.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
"jsonrpc-core 2.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"jsonrpc-http-server 5.1.0 (git+https://github.com/ethcore/jsonrpc-http-server.git)",
|
"jsonrpc-http-server 5.1.0 (git+https://github.com/ethcore/jsonrpc-http-server.git)",
|
||||||
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"mime_guess 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"parity-dapps 0.3.0 (git+https://github.com/ethcore/parity-dapps-rs.git)",
|
"parity-dapps 0.3.0 (git+https://github.com/ethcore/parity-dapps-rs.git)",
|
||||||
"parity-dapps-builtins 0.5.0 (git+https://github.com/ethcore/parity-dapps-builtins-rs.git)",
|
"parity-dapps-builtins 0.5.0 (git+https://github.com/ethcore/parity-dapps-builtins-rs.git)",
|
||||||
"parity-dapps-dao 0.3.0 (git+https://github.com/ethcore/parity-dapps-dao-rs.git)",
|
"parity-dapps-dao 0.3.0 (git+https://github.com/ethcore/parity-dapps-dao-rs.git)",
|
||||||
@ -868,7 +869,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "parity-dapps-builtins"
|
name = "parity-dapps-builtins"
|
||||||
version = "0.5.0"
|
version = "0.5.0"
|
||||||
source = "git+https://github.com/ethcore/parity-dapps-builtins-rs.git#eb86a2954f04d3aa5547a8c4bb77ae7aad09bf55"
|
source = "git+https://github.com/ethcore/parity-dapps-builtins-rs.git#8bbf0421e376f9496d70adc62c1c6d7f492df817"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"parity-dapps 0.3.0 (git+https://github.com/ethcore/parity-dapps-rs.git)",
|
"parity-dapps 0.3.0 (git+https://github.com/ethcore/parity-dapps-rs.git)",
|
||||||
]
|
]
|
||||||
|
2
cov.sh
2
cov.sh
@ -22,7 +22,6 @@ cargo test \
|
|||||||
-p ethsync \
|
-p ethsync \
|
||||||
-p ethcore-rpc \
|
-p ethcore-rpc \
|
||||||
-p parity \
|
-p parity \
|
||||||
-p ethminer \
|
|
||||||
-p ethcore-signer \
|
-p ethcore-signer \
|
||||||
-p ethcore-dapps \
|
-p ethcore-dapps \
|
||||||
--no-run || exit $?
|
--no-run || exit $?
|
||||||
@ -37,5 +36,4 @@ kcov --exclude-pattern $EXCLUDE --include-pattern src --verify target/coverage t
|
|||||||
kcov --exclude-pattern $EXCLUDE --include-pattern src --verify target/coverage target/debug/deps/ethcore_rpc-*
|
kcov --exclude-pattern $EXCLUDE --include-pattern src --verify target/coverage target/debug/deps/ethcore_rpc-*
|
||||||
kcov --exclude-pattern $EXCLUDE --include-pattern src --verify target/coverage target/debug/deps/ethcore_signer-*
|
kcov --exclude-pattern $EXCLUDE --include-pattern src --verify target/coverage target/debug/deps/ethcore_signer-*
|
||||||
kcov --exclude-pattern $EXCLUDE --include-pattern src --verify target/coverage target/debug/deps/ethcore_dapps-*
|
kcov --exclude-pattern $EXCLUDE --include-pattern src --verify target/coverage target/debug/deps/ethcore_dapps-*
|
||||||
kcov --exclude-pattern $EXCLUDE --include-pattern src --verify target/coverage target/debug/deps/ethminer-*
|
|
||||||
xdg-open target/coverage/index.html
|
xdg-open target/coverage/index.html
|
||||||
|
@ -27,6 +27,7 @@ parity-dapps-builtins = { git = "https://github.com/ethcore/parity-dapps-builtin
|
|||||||
parity-dapps-wallet = { git = "https://github.com/ethcore/parity-dapps-wallet-rs.git", version = "0.5.0", optional = true }
|
parity-dapps-wallet = { git = "https://github.com/ethcore/parity-dapps-wallet-rs.git", version = "0.5.0", optional = true }
|
||||||
parity-dapps-dao = { git = "https://github.com/ethcore/parity-dapps-dao-rs.git", version = "0.3.0", optional = true }
|
parity-dapps-dao = { git = "https://github.com/ethcore/parity-dapps-dao-rs.git", version = "0.3.0", optional = true }
|
||||||
parity-dapps-makerotc = { git = "https://github.com/ethcore/parity-dapps-makerotc-rs.git", version = "0.2.0", optional = true }
|
parity-dapps-makerotc = { git = "https://github.com/ethcore/parity-dapps-makerotc-rs.git", version = "0.2.0", optional = true }
|
||||||
|
mime_guess = { version = "1.6.1" }
|
||||||
clippy = { version = "0.0.69", optional = true}
|
clippy = { version = "0.0.69", optional = true}
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use endpoint::{Endpoint, Endpoints, Handler, EndpointPath};
|
use endpoint::{Endpoint, Endpoints, EndpointInfo, Handler, EndpointPath};
|
||||||
|
|
||||||
use api::response::as_json;
|
use api::response::as_json;
|
||||||
|
|
||||||
@ -23,8 +23,8 @@ pub struct RestApi {
|
|||||||
endpoints: Arc<Endpoints>,
|
endpoints: Arc<Endpoints>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize)]
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||||
struct App {
|
pub struct App {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub description: String,
|
pub description: String,
|
||||||
@ -34,6 +34,19 @@ struct App {
|
|||||||
pub icon_url: String,
|
pub icon_url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl App {
|
||||||
|
fn from_info(id: &str, info: &EndpointInfo) -> Self {
|
||||||
|
App {
|
||||||
|
id: id.to_owned(),
|
||||||
|
name: info.name.to_owned(),
|
||||||
|
description: info.description.to_owned(),
|
||||||
|
version: info.version.to_owned(),
|
||||||
|
author: info.author.to_owned(),
|
||||||
|
icon_url: info.icon_url.to_owned(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl RestApi {
|
impl RestApi {
|
||||||
pub fn new(endpoints: Arc<Endpoints>) -> Box<Endpoint> {
|
pub fn new(endpoints: Arc<Endpoints>) -> Box<Endpoint> {
|
||||||
Box::new(RestApi {
|
Box::new(RestApi {
|
||||||
@ -43,14 +56,7 @@ impl RestApi {
|
|||||||
|
|
||||||
fn list_apps(&self) -> Vec<App> {
|
fn list_apps(&self) -> Vec<App> {
|
||||||
self.endpoints.iter().filter_map(|(ref k, ref e)| {
|
self.endpoints.iter().filter_map(|(ref k, ref e)| {
|
||||||
e.info().map(|ref info| App {
|
e.info().map(|ref info| App::from_info(k, info))
|
||||||
id: k.to_owned().clone(),
|
|
||||||
name: info.name.to_owned(),
|
|
||||||
description: info.description.to_owned(),
|
|
||||||
version: info.version.to_owned(),
|
|
||||||
author: info.author.to_owned(),
|
|
||||||
icon_url: info.icon_url.to_owned(),
|
|
||||||
})
|
|
||||||
}).collect()
|
}).collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,3 +18,4 @@ mod api;
|
|||||||
mod response;
|
mod response;
|
||||||
|
|
||||||
pub use self::api::RestApi;
|
pub use self::api::RestApi;
|
||||||
|
pub use self::api::App;
|
||||||
|
116
dapps/src/apps/fs.rs
Normal file
116
dapps/src/apps/fs.rs
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
||||||
|
// 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/>.
|
||||||
|
|
||||||
|
use serde_json;
|
||||||
|
use std::io;
|
||||||
|
use std::io::Read;
|
||||||
|
use std::fs;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use page::LocalPageEndpoint;
|
||||||
|
use endpoint::{Endpoints, EndpointInfo};
|
||||||
|
use api::App;
|
||||||
|
|
||||||
|
struct LocalDapp {
|
||||||
|
id: String,
|
||||||
|
path: PathBuf,
|
||||||
|
info: EndpointInfo,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn local_dapps(dapps_path: String) -> Vec<LocalDapp> {
|
||||||
|
let files = fs::read_dir(dapps_path.as_str());
|
||||||
|
if let Err(e) = files {
|
||||||
|
warn!(target: "dapps", "Unable to load local dapps from: {}. Reason: {:?}", dapps_path, e);
|
||||||
|
return vec![];
|
||||||
|
}
|
||||||
|
|
||||||
|
let files = files.expect("Check is done earlier");
|
||||||
|
files.map(|dir| {
|
||||||
|
let entry = try!(dir);
|
||||||
|
let file_type = try!(entry.file_type());
|
||||||
|
|
||||||
|
// skip files
|
||||||
|
if file_type.is_file() {
|
||||||
|
return Err(io::Error::new(io::ErrorKind::NotFound, "Not a file"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// take directory name and path
|
||||||
|
entry.file_name().into_string()
|
||||||
|
.map(|name| (name, entry.path()))
|
||||||
|
.map_err(|e| {
|
||||||
|
info!(target: "dapps", "Unable to load dapp: {:?}. Reason: {:?}", entry.path(), e);
|
||||||
|
io::Error::new(io::ErrorKind::NotFound, "Invalid name")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.filter_map(|m| {
|
||||||
|
if let Err(ref e) = m {
|
||||||
|
debug!(target: "dapps", "Ignoring local dapp: {:?}", e);
|
||||||
|
}
|
||||||
|
m.ok()
|
||||||
|
})
|
||||||
|
.map(|(name, path)| {
|
||||||
|
// try to get manifest file
|
||||||
|
let info = read_manifest(&name, path.clone());
|
||||||
|
LocalDapp {
|
||||||
|
id: name,
|
||||||
|
path: path,
|
||||||
|
info: info,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_manifest(name: &str, mut path: PathBuf) -> EndpointInfo {
|
||||||
|
path.push("manifest.json");
|
||||||
|
|
||||||
|
fs::File::open(path.clone())
|
||||||
|
.map_err(|e| format!("{:?}", e))
|
||||||
|
.and_then(|mut f| {
|
||||||
|
// Reat file
|
||||||
|
let mut s = String::new();
|
||||||
|
try!(f.read_to_string(&mut s).map_err(|e| format!("{:?}", e)));
|
||||||
|
// Try to deserialize manifest
|
||||||
|
serde_json::from_str::<App>(&s).map_err(|e| format!("{:?}", e))
|
||||||
|
})
|
||||||
|
.map(|app| EndpointInfo {
|
||||||
|
name: app.name,
|
||||||
|
description: app.description,
|
||||||
|
version: app.version,
|
||||||
|
author: app.author,
|
||||||
|
icon_url: app.icon_url,
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|e| {
|
||||||
|
warn!(target: "dapps", "Cannot read manifest file at: {:?}. Error: {:?}", path, e);
|
||||||
|
|
||||||
|
EndpointInfo {
|
||||||
|
name: name.into(),
|
||||||
|
description: name.into(),
|
||||||
|
version: "0.0.0".into(),
|
||||||
|
author: "?".into(),
|
||||||
|
icon_url: "icon.png".into(),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn local_endpoints(dapps_path: String) -> Endpoints {
|
||||||
|
let mut pages = Endpoints::new();
|
||||||
|
for dapp in local_dapps(dapps_path) {
|
||||||
|
pages.insert(
|
||||||
|
dapp.id,
|
||||||
|
Box::new(LocalPageEndpoint::new(dapp.path, dapp.info))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
pages
|
||||||
|
}
|
@ -19,10 +19,11 @@ use page::PageEndpoint;
|
|||||||
use proxypac::ProxyPac;
|
use proxypac::ProxyPac;
|
||||||
use parity_dapps::WebApp;
|
use parity_dapps::WebApp;
|
||||||
|
|
||||||
|
mod fs;
|
||||||
|
|
||||||
extern crate parity_dapps_status;
|
extern crate parity_dapps_status;
|
||||||
extern crate parity_dapps_builtins;
|
extern crate parity_dapps_builtins;
|
||||||
|
|
||||||
|
|
||||||
pub const DAPPS_DOMAIN : &'static str = ".parity";
|
pub const DAPPS_DOMAIN : &'static str = ".parity";
|
||||||
pub const RPC_PATH : &'static str = "rpc";
|
pub const RPC_PATH : &'static str = "rpc";
|
||||||
pub const API_PATH : &'static str = "api";
|
pub const API_PATH : &'static str = "api";
|
||||||
@ -36,22 +37,24 @@ pub fn utils() -> Box<Endpoint> {
|
|||||||
Box::new(PageEndpoint::with_prefix(parity_dapps_builtins::App::default(), UTILS_PATH.to_owned()))
|
Box::new(PageEndpoint::with_prefix(parity_dapps_builtins::App::default(), UTILS_PATH.to_owned()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn all_endpoints() -> Endpoints {
|
pub fn all_endpoints(dapps_path: String) -> Endpoints {
|
||||||
let mut pages = Endpoints::new();
|
// fetch fs dapps at first to avoid overwriting builtins
|
||||||
pages.insert("proxy".into(), ProxyPac::boxed());
|
let mut pages = fs::local_endpoints(dapps_path);
|
||||||
|
|
||||||
// Home page needs to be safe embed
|
// Home page needs to be safe embed
|
||||||
// because we use Cross-Origin LocalStorage.
|
// because we use Cross-Origin LocalStorage.
|
||||||
// TODO [ToDr] Account naming should be moved to parity.
|
// TODO [ToDr] Account naming should be moved to parity.
|
||||||
pages.insert("home".into(), Box::new(
|
pages.insert("home".into(), Box::new(
|
||||||
PageEndpoint::new_safe_to_embed(parity_dapps_builtins::App::default())
|
PageEndpoint::new_safe_to_embed(parity_dapps_builtins::App::default())
|
||||||
));
|
));
|
||||||
|
pages.insert("proxy".into(), ProxyPac::boxed());
|
||||||
insert::<parity_dapps_status::App>(&mut pages, "status");
|
insert::<parity_dapps_status::App>(&mut pages, "status");
|
||||||
insert::<parity_dapps_status::App>(&mut pages, "parity");
|
insert::<parity_dapps_status::App>(&mut pages, "parity");
|
||||||
|
|
||||||
|
// Optional dapps
|
||||||
wallet_page(&mut pages);
|
wallet_page(&mut pages);
|
||||||
daodapp_page(&mut pages);
|
daodapp_page(&mut pages);
|
||||||
makerotc_page(&mut pages);
|
makerotc_page(&mut pages);
|
||||||
|
|
||||||
pages
|
pages
|
||||||
}
|
}
|
||||||
|
|
@ -30,17 +30,17 @@ pub struct EndpointPath {
|
|||||||
pub port: u16,
|
pub port: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub struct EndpointInfo {
|
pub struct EndpointInfo {
|
||||||
pub name: &'static str,
|
pub name: String,
|
||||||
pub description: &'static str,
|
pub description: String,
|
||||||
pub version: &'static str,
|
pub version: String,
|
||||||
pub author: &'static str,
|
pub author: String,
|
||||||
pub icon_url: &'static str,
|
pub icon_url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Endpoint : Send + Sync {
|
pub trait Endpoint : Send + Sync {
|
||||||
fn info(&self) -> Option<EndpointInfo> { None }
|
fn info(&self) -> Option<&EndpointInfo> { None }
|
||||||
|
|
||||||
fn to_handler(&self, path: EndpointPath) -> Box<server::Handler<HttpStream>>;
|
fn to_handler(&self, path: EndpointPath) -> Box<server::Handler<HttpStream>>;
|
||||||
}
|
}
|
||||||
|
@ -53,6 +53,7 @@ extern crate jsonrpc_core;
|
|||||||
extern crate jsonrpc_http_server;
|
extern crate jsonrpc_http_server;
|
||||||
extern crate parity_dapps;
|
extern crate parity_dapps;
|
||||||
extern crate ethcore_rpc;
|
extern crate ethcore_rpc;
|
||||||
|
extern crate mime_guess;
|
||||||
|
|
||||||
mod endpoint;
|
mod endpoint;
|
||||||
mod apps;
|
mod apps;
|
||||||
@ -73,6 +74,7 @@ static DAPPS_DOMAIN : &'static str = ".parity";
|
|||||||
|
|
||||||
/// Webapps HTTP+RPC server build.
|
/// Webapps HTTP+RPC server build.
|
||||||
pub struct ServerBuilder {
|
pub struct ServerBuilder {
|
||||||
|
dapps_path: String,
|
||||||
handler: Arc<IoHandler>,
|
handler: Arc<IoHandler>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,8 +86,9 @@ impl Extendable for ServerBuilder {
|
|||||||
|
|
||||||
impl ServerBuilder {
|
impl ServerBuilder {
|
||||||
/// Construct new dapps server
|
/// Construct new dapps server
|
||||||
pub fn new() -> Self {
|
pub fn new(dapps_path: String) -> Self {
|
||||||
ServerBuilder {
|
ServerBuilder {
|
||||||
|
dapps_path: dapps_path,
|
||||||
handler: Arc::new(IoHandler::new())
|
handler: Arc::new(IoHandler::new())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -93,13 +96,13 @@ impl ServerBuilder {
|
|||||||
/// Asynchronously start server with no authentication,
|
/// Asynchronously start server with no authentication,
|
||||||
/// returns result with `Server` handle on success or an error.
|
/// returns result with `Server` handle on success or an error.
|
||||||
pub fn start_unsecure_http(&self, addr: &SocketAddr) -> Result<Server, ServerError> {
|
pub fn start_unsecure_http(&self, addr: &SocketAddr) -> Result<Server, ServerError> {
|
||||||
Server::start_http(addr, NoAuth, self.handler.clone())
|
Server::start_http(addr, NoAuth, self.handler.clone(), self.dapps_path.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Asynchronously start server with `HTTP Basic Authentication`,
|
/// Asynchronously start server with `HTTP Basic Authentication`,
|
||||||
/// return result with `Server` handle on success or an error.
|
/// return result with `Server` handle on success or an error.
|
||||||
pub fn start_basic_auth_http(&self, addr: &SocketAddr, username: &str, password: &str) -> Result<Server, ServerError> {
|
pub fn start_basic_auth_http(&self, addr: &SocketAddr, username: &str, password: &str) -> Result<Server, ServerError> {
|
||||||
Server::start_http(addr, HttpBasicAuth::single_user(username, password), self.handler.clone())
|
Server::start_http(addr, HttpBasicAuth::single_user(username, password), self.handler.clone(), self.dapps_path.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,10 +113,10 @@ pub struct Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Server {
|
impl Server {
|
||||||
fn start_http<A: Authorization + 'static>(addr: &SocketAddr, authorization: A, handler: Arc<IoHandler>) -> Result<Server, ServerError> {
|
fn start_http<A: Authorization + 'static>(addr: &SocketAddr, authorization: A, handler: Arc<IoHandler>, dapps_path: String) -> Result<Server, ServerError> {
|
||||||
let panic_handler = Arc::new(Mutex::new(None));
|
let panic_handler = Arc::new(Mutex::new(None));
|
||||||
let authorization = Arc::new(authorization);
|
let authorization = Arc::new(authorization);
|
||||||
let endpoints = Arc::new(apps::all_endpoints());
|
let endpoints = Arc::new(apps::all_endpoints(dapps_path));
|
||||||
let special = Arc::new({
|
let special = Arc::new({
|
||||||
let mut special = HashMap::new();
|
let mut special = HashMap::new();
|
||||||
special.insert(router::SpecialEndpoint::Rpc, rpc::rpc(handler, panic_handler.clone()));
|
special.insert(router::SpecialEndpoint::Rpc, rpc::rpc(handler, panic_handler.clone()));
|
||||||
|
154
dapps/src/page/builtin.rs
Normal file
154
dapps/src/page/builtin.rs
Normal file
@ -0,0 +1,154 @@
|
|||||||
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
||||||
|
// 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/>.
|
||||||
|
|
||||||
|
use page::handler;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use endpoint::{Endpoint, EndpointInfo, EndpointPath, Handler};
|
||||||
|
use parity_dapps::{WebApp, File, Info};
|
||||||
|
|
||||||
|
pub struct PageEndpoint<T : WebApp + 'static> {
|
||||||
|
/// Content of the files
|
||||||
|
pub app: Arc<T>,
|
||||||
|
/// Prefix to strip from the path (when `None` deducted from `app_id`)
|
||||||
|
pub prefix: Option<String>,
|
||||||
|
/// Safe to be loaded in frame by other origin. (use wisely!)
|
||||||
|
safe_to_embed: bool,
|
||||||
|
info: EndpointInfo,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: WebApp + 'static> PageEndpoint<T> {
|
||||||
|
/// Creates new `PageEndpoint` for builtin (compile time) Dapp.
|
||||||
|
pub fn new(app: T) -> Self {
|
||||||
|
let info = app.info();
|
||||||
|
PageEndpoint {
|
||||||
|
app: Arc::new(app),
|
||||||
|
prefix: None,
|
||||||
|
safe_to_embed: false,
|
||||||
|
info: EndpointInfo::from(info),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create new `PageEndpoint` and specify prefix that should be removed before looking for a file.
|
||||||
|
/// It's used only for special endpoints (i.e. `/parity-utils/`)
|
||||||
|
/// So `/parity-utils/inject.js` will be resolved to `/inject.js` is prefix is set.
|
||||||
|
pub fn with_prefix(app: T, prefix: String) -> Self {
|
||||||
|
let info = app.info();
|
||||||
|
PageEndpoint {
|
||||||
|
app: Arc::new(app),
|
||||||
|
prefix: Some(prefix),
|
||||||
|
safe_to_embed: false,
|
||||||
|
info: EndpointInfo::from(info),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates new `PageEndpoint` which can be safely used in iframe
|
||||||
|
/// even from different origin. It might be dangerous (clickjacking).
|
||||||
|
/// Use wisely!
|
||||||
|
pub fn new_safe_to_embed(app: T) -> Self {
|
||||||
|
let info = app.info();
|
||||||
|
PageEndpoint {
|
||||||
|
app: Arc::new(app),
|
||||||
|
prefix: None,
|
||||||
|
safe_to_embed: true,
|
||||||
|
info: EndpointInfo::from(info),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: WebApp> Endpoint for PageEndpoint<T> {
|
||||||
|
|
||||||
|
fn info(&self) -> Option<&EndpointInfo> {
|
||||||
|
Some(&self.info)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_handler(&self, path: EndpointPath) -> Box<Handler> {
|
||||||
|
Box::new(handler::PageHandler {
|
||||||
|
app: BuiltinDapp::new(self.app.clone()),
|
||||||
|
prefix: self.prefix.clone(),
|
||||||
|
path: path,
|
||||||
|
file: None,
|
||||||
|
safe_to_embed: self.safe_to_embed,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Info> for EndpointInfo {
|
||||||
|
fn from(info: Info) -> Self {
|
||||||
|
EndpointInfo {
|
||||||
|
name: info.name.into(),
|
||||||
|
description: info.description.into(),
|
||||||
|
author: info.author.into(),
|
||||||
|
icon_url: info.icon_url.into(),
|
||||||
|
version: info.version.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct BuiltinDapp<T: WebApp + 'static> {
|
||||||
|
app: Arc<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: WebApp + 'static> BuiltinDapp<T> {
|
||||||
|
fn new(app: Arc<T>) -> Self {
|
||||||
|
BuiltinDapp {
|
||||||
|
app: app,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: WebApp + 'static> handler::Dapp for BuiltinDapp<T> {
|
||||||
|
type DappFile = BuiltinDappFile<T>;
|
||||||
|
|
||||||
|
fn file(&self, path: &str) -> Option<Self::DappFile> {
|
||||||
|
self.app.file(path).map(|_| {
|
||||||
|
BuiltinDappFile {
|
||||||
|
app: self.app.clone(),
|
||||||
|
path: path.into(),
|
||||||
|
write_pos: 0,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct BuiltinDappFile<T: WebApp + 'static> {
|
||||||
|
app: Arc<T>,
|
||||||
|
path: String,
|
||||||
|
write_pos: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: WebApp + 'static> BuiltinDappFile<T> {
|
||||||
|
fn file(&self) -> &File {
|
||||||
|
self.app.file(&self.path).expect("Check is done when structure is created.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: WebApp + 'static> handler::DappFile for BuiltinDappFile<T> {
|
||||||
|
fn content_type(&self) -> &str {
|
||||||
|
self.file().content_type
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_drained(&self) -> bool {
|
||||||
|
self.write_pos == self.file().content.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn next_chunk(&mut self) -> &[u8] {
|
||||||
|
&self.file().content[self.write_pos..]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bytes_written(&mut self, bytes: usize) {
|
||||||
|
self.write_pos += bytes;
|
||||||
|
}
|
||||||
|
}
|
207
dapps/src/page/handler.rs
Normal file
207
dapps/src/page/handler.rs
Normal file
@ -0,0 +1,207 @@
|
|||||||
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
||||||
|
// 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/>.
|
||||||
|
|
||||||
|
use std::io::Write;
|
||||||
|
use hyper::header;
|
||||||
|
use hyper::server;
|
||||||
|
use hyper::uri::RequestUri;
|
||||||
|
use hyper::net::HttpStream;
|
||||||
|
use hyper::status::StatusCode;
|
||||||
|
use hyper::{Decoder, Encoder, Next};
|
||||||
|
use endpoint::EndpointPath;
|
||||||
|
|
||||||
|
/// Represents a file that can be sent to client.
|
||||||
|
/// Implementation should keep track of bytes already sent internally.
|
||||||
|
pub trait DappFile: Send {
|
||||||
|
/// Returns a content-type of this file.
|
||||||
|
fn content_type(&self) -> &str;
|
||||||
|
|
||||||
|
/// Checks if all bytes from that file were written.
|
||||||
|
fn is_drained(&self) -> bool;
|
||||||
|
|
||||||
|
/// Fetch next chunk to write to the client.
|
||||||
|
fn next_chunk(&mut self) -> &[u8];
|
||||||
|
|
||||||
|
/// How many files have been written to the client.
|
||||||
|
fn bytes_written(&mut self, bytes: usize);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Dapp as a (dynamic) set of files.
|
||||||
|
pub trait Dapp: Send + 'static {
|
||||||
|
/// File type
|
||||||
|
type DappFile: DappFile;
|
||||||
|
|
||||||
|
/// Returns file under given path.
|
||||||
|
fn file(&self, path: &str) -> Option<Self::DappFile>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A handler for a single webapp.
|
||||||
|
/// Resolves correct paths and serves as a plumbing code between
|
||||||
|
/// hyper server and dapp.
|
||||||
|
pub struct PageHandler<T: Dapp> {
|
||||||
|
/// A Dapp.
|
||||||
|
pub app: T,
|
||||||
|
/// File currently being served (or `None` if file does not exist).
|
||||||
|
pub file: Option<T::DappFile>,
|
||||||
|
/// Optional prefix to strip from path.
|
||||||
|
pub prefix: Option<String>,
|
||||||
|
/// Requested path.
|
||||||
|
pub path: EndpointPath,
|
||||||
|
/// Flag indicating if the file can be safely embeded (put in iframe).
|
||||||
|
pub safe_to_embed: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Dapp> PageHandler<T> {
|
||||||
|
fn extract_path(&self, path: &str) -> String {
|
||||||
|
let app_id = &self.path.app_id;
|
||||||
|
let prefix = "/".to_owned() + self.prefix.as_ref().unwrap_or(app_id);
|
||||||
|
let prefix_with_slash = prefix.clone() + "/";
|
||||||
|
let query_pos = path.find('?').unwrap_or_else(|| path.len());
|
||||||
|
|
||||||
|
// Index file support
|
||||||
|
match path == "/" || path == &prefix || path == &prefix_with_slash {
|
||||||
|
true => "index.html".to_owned(),
|
||||||
|
false => if path.starts_with(&prefix_with_slash) {
|
||||||
|
path[prefix_with_slash.len()..query_pos].to_owned()
|
||||||
|
} else if path.starts_with("/") {
|
||||||
|
path[1..query_pos].to_owned()
|
||||||
|
} else {
|
||||||
|
path[0..query_pos].to_owned()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Dapp> server::Handler<HttpStream> for PageHandler<T> {
|
||||||
|
fn on_request(&mut self, req: server::Request) -> Next {
|
||||||
|
self.file = match *req.uri() {
|
||||||
|
RequestUri::AbsolutePath(ref path) => {
|
||||||
|
self.app.file(&self.extract_path(path))
|
||||||
|
},
|
||||||
|
RequestUri::AbsoluteUri(ref url) => {
|
||||||
|
self.app.file(&self.extract_path(url.path()))
|
||||||
|
},
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
Next::write()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_request_readable(&mut self, _decoder: &mut Decoder<HttpStream>) -> Next {
|
||||||
|
Next::write()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_response(&mut self, res: &mut server::Response) -> Next {
|
||||||
|
if let Some(ref f) = self.file {
|
||||||
|
res.set_status(StatusCode::Ok);
|
||||||
|
res.headers_mut().set(header::ContentType(f.content_type().parse().unwrap()));
|
||||||
|
if !self.safe_to_embed {
|
||||||
|
res.headers_mut().set_raw("X-Frame-Options", vec![b"SAMEORIGIN".to_vec()]);
|
||||||
|
}
|
||||||
|
Next::write()
|
||||||
|
} else {
|
||||||
|
res.set_status(StatusCode::NotFound);
|
||||||
|
Next::write()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_response_writable(&mut self, encoder: &mut Encoder<HttpStream>) -> Next {
|
||||||
|
match self.file {
|
||||||
|
None => Next::end(),
|
||||||
|
Some(ref f) if f.is_drained() => Next::end(),
|
||||||
|
Some(ref mut f) => match encoder.write(f.next_chunk()) {
|
||||||
|
Ok(bytes) => {
|
||||||
|
f.bytes_written(bytes);
|
||||||
|
Next::write()
|
||||||
|
},
|
||||||
|
Err(e) => match e.kind() {
|
||||||
|
::std::io::ErrorKind::WouldBlock => Next::write(),
|
||||||
|
_ => Next::end(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub struct TestWebAppFile;
|
||||||
|
|
||||||
|
impl DappFile for TestWebAppFile {
|
||||||
|
fn content_type(&self) -> &str {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_drained(&self) -> bool {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn next_chunk(&mut self) -> &[u8] {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bytes_written(&mut self, _bytes: usize) {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct TestWebapp;
|
||||||
|
|
||||||
|
impl Dapp for TestWebapp {
|
||||||
|
type DappFile = TestWebAppFile;
|
||||||
|
|
||||||
|
fn file(&self, _path: &str) -> Option<Self::DappFile> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_extract_path_with_appid() {
|
||||||
|
|
||||||
|
// given
|
||||||
|
let path1 = "/";
|
||||||
|
let path2= "/test.css";
|
||||||
|
let path3 = "/app/myfile.txt";
|
||||||
|
let path4 = "/app/myfile.txt?query=123";
|
||||||
|
let page_handler = PageHandler {
|
||||||
|
app: test::TestWebapp,
|
||||||
|
prefix: None,
|
||||||
|
path: EndpointPath {
|
||||||
|
app_id: "app".to_owned(),
|
||||||
|
host: "".to_owned(),
|
||||||
|
port: 8080
|
||||||
|
},
|
||||||
|
file: None,
|
||||||
|
safe_to_embed: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
// when
|
||||||
|
let res1 = page_handler.extract_path(path1);
|
||||||
|
let res2 = page_handler.extract_path(path2);
|
||||||
|
let res3 = page_handler.extract_path(path3);
|
||||||
|
let res4 = page_handler.extract_path(path4);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assert_eq!(&res1, "index.html");
|
||||||
|
assert_eq!(&res2, "test.css");
|
||||||
|
assert_eq!(&res3, "myfile.txt");
|
||||||
|
assert_eq!(&res4, "myfile.txt");
|
||||||
|
}
|
118
dapps/src/page/local.rs
Normal file
118
dapps/src/page/local.rs
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
||||||
|
// 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/>.
|
||||||
|
|
||||||
|
use mime_guess;
|
||||||
|
use std::io::{Seek, Read, SeekFrom};
|
||||||
|
use std::fs;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use page::handler;
|
||||||
|
use endpoint::{Endpoint, EndpointInfo, EndpointPath, Handler};
|
||||||
|
|
||||||
|
pub struct LocalPageEndpoint {
|
||||||
|
path: PathBuf,
|
||||||
|
info: EndpointInfo,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LocalPageEndpoint {
|
||||||
|
pub fn new(path: PathBuf, info: EndpointInfo) -> Self {
|
||||||
|
LocalPageEndpoint {
|
||||||
|
path: path,
|
||||||
|
info: info,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Endpoint for LocalPageEndpoint {
|
||||||
|
fn info(&self) -> Option<&EndpointInfo> {
|
||||||
|
Some(&self.info)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_handler(&self, path: EndpointPath) -> Box<Handler> {
|
||||||
|
Box::new(handler::PageHandler {
|
||||||
|
app: LocalDapp::new(self.path.clone()),
|
||||||
|
prefix: None,
|
||||||
|
path: path,
|
||||||
|
file: None,
|
||||||
|
safe_to_embed: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct LocalDapp {
|
||||||
|
path: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LocalDapp {
|
||||||
|
fn new(path: PathBuf) -> Self {
|
||||||
|
LocalDapp {
|
||||||
|
path: path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl handler::Dapp for LocalDapp {
|
||||||
|
type DappFile = LocalFile;
|
||||||
|
|
||||||
|
fn file(&self, file_path: &str) -> Option<Self::DappFile> {
|
||||||
|
let mut path = self.path.clone();
|
||||||
|
for part in file_path.split('/') {
|
||||||
|
path.push(part);
|
||||||
|
}
|
||||||
|
// Check if file exists
|
||||||
|
fs::File::open(path.clone()).ok().map(|file| {
|
||||||
|
let content_type = mime_guess::guess_mime_type(path);
|
||||||
|
let len = file.metadata().ok().map_or(0, |meta| meta.len());
|
||||||
|
LocalFile {
|
||||||
|
content_type: content_type.to_string(),
|
||||||
|
buffer: [0; 4096],
|
||||||
|
file: file,
|
||||||
|
pos: 0,
|
||||||
|
len: len,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct LocalFile {
|
||||||
|
content_type: String,
|
||||||
|
buffer: [u8; 4096],
|
||||||
|
file: fs::File,
|
||||||
|
len: u64,
|
||||||
|
pos: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl handler::DappFile for LocalFile {
|
||||||
|
fn content_type(&self) -> &str {
|
||||||
|
&self.content_type
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_drained(&self) -> bool {
|
||||||
|
self.pos == self.len
|
||||||
|
}
|
||||||
|
|
||||||
|
fn next_chunk(&mut self) -> &[u8] {
|
||||||
|
let _ = self.file.seek(SeekFrom::Start(self.pos));
|
||||||
|
if let Ok(n) = self.file.read(&mut self.buffer) {
|
||||||
|
&self.buffer[0..n]
|
||||||
|
} else {
|
||||||
|
&self.buffer[0..0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bytes_written(&mut self, bytes: usize) {
|
||||||
|
self.pos += bytes as u64;
|
||||||
|
}
|
||||||
|
}
|
@ -14,216 +14,11 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::io::Write;
|
|
||||||
use hyper::uri::RequestUri;
|
|
||||||
use hyper::server;
|
|
||||||
use hyper::header;
|
|
||||||
use hyper::status::StatusCode;
|
|
||||||
use hyper::net::HttpStream;
|
|
||||||
use hyper::{Decoder, Encoder, Next};
|
|
||||||
use endpoint::{Endpoint, EndpointInfo, EndpointPath};
|
|
||||||
use parity_dapps::{WebApp, Info};
|
|
||||||
|
|
||||||
pub struct PageEndpoint<T : WebApp + 'static> {
|
mod builtin;
|
||||||
/// Content of the files
|
mod local;
|
||||||
pub app: Arc<T>,
|
mod handler;
|
||||||
/// Prefix to strip from the path (when `None` deducted from `app_id`)
|
|
||||||
pub prefix: Option<String>,
|
|
||||||
/// Safe to be loaded in frame by other origin. (use wisely!)
|
|
||||||
safe_to_embed: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: WebApp + 'static> PageEndpoint<T> {
|
pub use self::local::LocalPageEndpoint;
|
||||||
pub fn new(app: T) -> Self {
|
pub use self::builtin::PageEndpoint;
|
||||||
PageEndpoint {
|
|
||||||
app: Arc::new(app),
|
|
||||||
prefix: None,
|
|
||||||
safe_to_embed: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn with_prefix(app: T, prefix: String) -> Self {
|
|
||||||
PageEndpoint {
|
|
||||||
app: Arc::new(app),
|
|
||||||
prefix: Some(prefix),
|
|
||||||
safe_to_embed: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates new `PageEndpoint` which can be safely used in iframe
|
|
||||||
/// even from different origin. It might be dangerous (clickjacking).
|
|
||||||
/// Use wisely!
|
|
||||||
pub fn new_safe_to_embed(app: T) -> Self {
|
|
||||||
PageEndpoint {
|
|
||||||
app: Arc::new(app),
|
|
||||||
prefix: None,
|
|
||||||
safe_to_embed: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: WebApp> Endpoint for PageEndpoint<T> {
|
|
||||||
|
|
||||||
fn info(&self) -> Option<EndpointInfo> {
|
|
||||||
Some(EndpointInfo::from(self.app.info()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn to_handler(&self, path: EndpointPath) -> Box<server::Handler<HttpStream>> {
|
|
||||||
Box::new(PageHandler {
|
|
||||||
app: self.app.clone(),
|
|
||||||
prefix: self.prefix.clone(),
|
|
||||||
path: path,
|
|
||||||
file: None,
|
|
||||||
write_pos: 0,
|
|
||||||
safe_to_embed: self.safe_to_embed,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Info> for EndpointInfo {
|
|
||||||
fn from(info: Info) -> Self {
|
|
||||||
EndpointInfo {
|
|
||||||
name: info.name,
|
|
||||||
description: info.description,
|
|
||||||
author: info.author,
|
|
||||||
icon_url: info.icon_url,
|
|
||||||
version: info.version,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct PageHandler<T: WebApp + 'static> {
|
|
||||||
app: Arc<T>,
|
|
||||||
prefix: Option<String>,
|
|
||||||
path: EndpointPath,
|
|
||||||
file: Option<String>,
|
|
||||||
write_pos: usize,
|
|
||||||
safe_to_embed: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: WebApp + 'static> PageHandler<T> {
|
|
||||||
fn extract_path(&self, path: &str) -> String {
|
|
||||||
let app_id = &self.path.app_id;
|
|
||||||
let prefix = "/".to_owned() + self.prefix.as_ref().unwrap_or(app_id);
|
|
||||||
let prefix_with_slash = prefix.clone() + "/";
|
|
||||||
let query_pos = path.find('?').unwrap_or_else(|| path.len());
|
|
||||||
|
|
||||||
// Index file support
|
|
||||||
match path == "/" || path == &prefix || path == &prefix_with_slash {
|
|
||||||
true => "index.html".to_owned(),
|
|
||||||
false => if path.starts_with(&prefix_with_slash) {
|
|
||||||
path[prefix_with_slash.len()..query_pos].to_owned()
|
|
||||||
} else if path.starts_with("/") {
|
|
||||||
path[1..query_pos].to_owned()
|
|
||||||
} else {
|
|
||||||
path[0..query_pos].to_owned()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: WebApp + 'static> server::Handler<HttpStream> for PageHandler<T> {
|
|
||||||
fn on_request(&mut self, req: server::Request) -> Next {
|
|
||||||
self.file = match *req.uri() {
|
|
||||||
RequestUri::AbsolutePath(ref path) => {
|
|
||||||
Some(self.extract_path(path))
|
|
||||||
},
|
|
||||||
RequestUri::AbsoluteUri(ref url) => {
|
|
||||||
Some(self.extract_path(url.path()))
|
|
||||||
},
|
|
||||||
_ => None,
|
|
||||||
};
|
|
||||||
Next::write()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn on_request_readable(&mut self, _decoder: &mut Decoder<HttpStream>) -> Next {
|
|
||||||
Next::write()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn on_response(&mut self, res: &mut server::Response) -> Next {
|
|
||||||
if let Some(f) = self.file.as_ref().and_then(|f| self.app.file(f)) {
|
|
||||||
res.set_status(StatusCode::Ok);
|
|
||||||
res.headers_mut().set(header::ContentType(f.content_type.parse().unwrap()));
|
|
||||||
if !self.safe_to_embed {
|
|
||||||
res.headers_mut().set_raw("X-Frame-Options", vec![b"SAMEORIGIN".to_vec()]);
|
|
||||||
}
|
|
||||||
Next::write()
|
|
||||||
} else {
|
|
||||||
res.set_status(StatusCode::NotFound);
|
|
||||||
Next::write()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn on_response_writable(&mut self, encoder: &mut Encoder<HttpStream>) -> Next {
|
|
||||||
let (wrote, res) = {
|
|
||||||
let file = self.file.as_ref().and_then(|f| self.app.file(f));
|
|
||||||
match file {
|
|
||||||
None => (None, Next::end()),
|
|
||||||
Some(f) if self.write_pos == f.content.len() => (None, Next::end()),
|
|
||||||
Some(f) => match encoder.write(&f.content[self.write_pos..]) {
|
|
||||||
Ok(bytes) => (Some(bytes), Next::write()),
|
|
||||||
Err(e) => match e.kind() {
|
|
||||||
::std::io::ErrorKind::WouldBlock => (None, Next::write()),
|
|
||||||
_ => (None, Next::end())
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if let Some(bytes) = wrote {
|
|
||||||
self.write_pos += bytes;
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
use parity_dapps::File;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
#[derive(Default)]
|
|
||||||
struct TestWebapp;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
impl WebApp for TestWebapp {
|
|
||||||
fn file(&self, _path: &str) -> Option<&File> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
fn info(&self) -> Info {
|
|
||||||
unimplemented!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn should_extract_path_with_appid() {
|
|
||||||
// given
|
|
||||||
let path1 = "/";
|
|
||||||
let path2= "/test.css";
|
|
||||||
let path3 = "/app/myfile.txt";
|
|
||||||
let path4 = "/app/myfile.txt?query=123";
|
|
||||||
let page_handler = PageHandler {
|
|
||||||
app: Arc::new(TestWebapp),
|
|
||||||
prefix: None,
|
|
||||||
path: EndpointPath {
|
|
||||||
app_id: "app".to_owned(),
|
|
||||||
host: "".to_owned(),
|
|
||||||
port: 8080
|
|
||||||
},
|
|
||||||
file: None,
|
|
||||||
write_pos: 0,
|
|
||||||
safe_to_embed: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
// when
|
|
||||||
let res1 = page_handler.extract_path(path1);
|
|
||||||
let res2 = page_handler.extract_path(path2);
|
|
||||||
let res3 = page_handler.extract_path(path3);
|
|
||||||
let res4 = page_handler.extract_path(path4);
|
|
||||||
|
|
||||||
// then
|
|
||||||
assert_eq!(&res1, "index.html");
|
|
||||||
assert_eq!(&res2, "test.css");
|
|
||||||
assert_eq!(&res3, "myfile.txt");
|
|
||||||
assert_eq!(&res4, "myfile.txt");
|
|
||||||
}
|
|
||||||
|
1
doc.sh
1
doc.sh
@ -10,4 +10,3 @@ cargo doc --no-deps --verbose \
|
|||||||
-p ethcore-signer \
|
-p ethcore-signer \
|
||||||
-p ethcore-dapps \
|
-p ethcore-dapps \
|
||||||
-p parity \
|
-p parity \
|
||||||
-p ethminer
|
|
||||||
|
@ -63,13 +63,43 @@ pub enum Error {
|
|||||||
Internal,
|
Internal,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Evm result.
|
/// A specialized version of Result over EVM errors.
|
||||||
///
|
pub type Result<T> = ::std::result::Result<T, Error>;
|
||||||
/// Returns `gas_left` if execution is successful, otherwise error.
|
|
||||||
pub type Result = result::Result<U256, Error>;
|
|
||||||
|
|
||||||
/// Evm interface.
|
/// Gas Left: either it is a known value, or it needs to be computed by processing
|
||||||
|
/// a return instruction.
|
||||||
|
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
pub enum GasLeft<'a> {
|
||||||
|
/// Known gas left
|
||||||
|
Known(U256),
|
||||||
|
/// Return instruction must be processed.
|
||||||
|
NeedsReturn(U256, &'a [u8]),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Types that can be "finalized" using an EVM.
|
||||||
|
///
|
||||||
|
/// In practice, this is just used to define an inherent impl on
|
||||||
|
/// `Reult<GasLeft<'a>>`.
|
||||||
|
pub trait Finalize {
|
||||||
|
/// Consume the externalities, call return if necessary, and produce a final amount of gas left.
|
||||||
|
fn finalize<E: Ext>(self, ext: E) -> Result<U256>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Finalize for Result<GasLeft<'a>> {
|
||||||
|
fn finalize<E: Ext>(self, ext: E) -> Result<U256> {
|
||||||
|
match self {
|
||||||
|
Ok(GasLeft::Known(gas)) => Ok(gas),
|
||||||
|
Ok(GasLeft::NeedsReturn(gas, ret_code)) => ext.ret(&gas, ret_code),
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Evm interface
|
||||||
pub trait Evm {
|
pub trait Evm {
|
||||||
/// This function should be used to execute transaction.
|
/// This function should be used to execute transaction.
|
||||||
fn exec(&self, params: ActionParams, ext: &mut Ext) -> Result;
|
///
|
||||||
|
/// It returns either an error, a known amount of gas left, or parameters to be used
|
||||||
|
/// to compute the final gas left.
|
||||||
|
fn exec(&mut self, params: ActionParams, ext: &mut Ext) -> Result<GasLeft>;
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
//! Interface for Evm externalities.
|
//! Interface for Evm externalities.
|
||||||
|
|
||||||
use util::common::*;
|
use util::common::*;
|
||||||
use evm::{Schedule, Error};
|
use evm::{self, Schedule};
|
||||||
use env_info::*;
|
use env_info::*;
|
||||||
|
|
||||||
/// Result of externalities create function.
|
/// Result of externalities create function.
|
||||||
@ -85,7 +85,7 @@ pub trait Ext {
|
|||||||
|
|
||||||
/// Should be called when transaction calls `RETURN` opcode.
|
/// Should be called when transaction calls `RETURN` opcode.
|
||||||
/// Returns gas_left if cost of returning the data is not too high.
|
/// Returns gas_left if cost of returning the data is not too high.
|
||||||
fn ret(&mut self, gas: &U256, data: &[u8]) -> Result<U256, Error>;
|
fn ret(self, gas: &U256, data: &[u8]) -> evm::Result<U256> where Self: Sized;
|
||||||
|
|
||||||
/// Should be called when contract commits suicide.
|
/// Should be called when contract commits suicide.
|
||||||
/// Address to which funds should be refunded.
|
/// Address to which funds should be refunded.
|
||||||
|
@ -89,10 +89,10 @@ impl Factory {
|
|||||||
pub fn create(&self) -> Box<Evm> {
|
pub fn create(&self) -> Box<Evm> {
|
||||||
match self.evm {
|
match self.evm {
|
||||||
VMType::Jit => {
|
VMType::Jit => {
|
||||||
Box::new(super::jit::JitEvm)
|
Box::new(super::jit::JitEvm::default())
|
||||||
},
|
},
|
||||||
VMType::Interpreter => {
|
VMType::Interpreter => {
|
||||||
Box::new(super::interpreter::Interpreter)
|
Box::new(super::interpreter::Interpreter::default())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -102,7 +102,7 @@ impl Factory {
|
|||||||
pub fn create(&self) -> Box<Evm> {
|
pub fn create(&self) -> Box<Evm> {
|
||||||
match self.evm {
|
match self.evm {
|
||||||
VMType::Interpreter => {
|
VMType::Interpreter => {
|
||||||
Box::new(super::interpreter::Interpreter)
|
Box::new(super::interpreter::Interpreter::default())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ use trace::VMTracer;
|
|||||||
use super::instructions as instructions;
|
use super::instructions as instructions;
|
||||||
use super::instructions::{Instruction, get_info};
|
use super::instructions::{Instruction, get_info};
|
||||||
use std::marker::Copy;
|
use std::marker::Copy;
|
||||||
use evm::{self, MessageCallResult, ContractCreateResult};
|
use evm::{self, MessageCallResult, ContractCreateResult, GasLeft};
|
||||||
|
|
||||||
#[cfg(not(feature = "evm-debug"))]
|
#[cfg(not(feature = "evm-debug"))]
|
||||||
macro_rules! evm_debug {
|
macro_rules! evm_debug {
|
||||||
@ -279,21 +279,26 @@ enum InstructionResult {
|
|||||||
GasLeft(U256),
|
GasLeft(U256),
|
||||||
UnusedGas(U256),
|
UnusedGas(U256),
|
||||||
JumpToPosition(U256),
|
JumpToPosition(U256),
|
||||||
StopExecutionWithGasLeft(U256),
|
// gas left, init_orf, init_size
|
||||||
StopExecution
|
StopExecutionNeedsReturn(U256, U256, U256),
|
||||||
|
StopExecution,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Intepreter EVM implementation
|
/// Intepreter EVM implementation
|
||||||
pub struct Interpreter;
|
#[derive(Default)]
|
||||||
|
pub struct Interpreter {
|
||||||
|
mem: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
impl evm::Evm for Interpreter {
|
impl evm::Evm for Interpreter {
|
||||||
fn exec(&self, params: ActionParams, ext: &mut evm::Ext) -> evm::Result {
|
fn exec(&mut self, params: ActionParams, ext: &mut evm::Ext) -> evm::Result<GasLeft> {
|
||||||
|
self.mem.clear();
|
||||||
|
|
||||||
let code = ¶ms.code.as_ref().unwrap();
|
let code = ¶ms.code.as_ref().unwrap();
|
||||||
let valid_jump_destinations = self.find_jump_destinations(&code);
|
let valid_jump_destinations = self.find_jump_destinations(&code);
|
||||||
|
|
||||||
let mut current_gas = params.gas;
|
let mut current_gas = params.gas;
|
||||||
let mut stack = VecStack::with_capacity(ext.schedule().stack_limit, U256::zero());
|
let mut stack = VecStack::with_capacity(ext.schedule().stack_limit, U256::zero());
|
||||||
let mut mem = vec![];
|
|
||||||
let mut reader = CodeReader {
|
let mut reader = CodeReader {
|
||||||
position: 0,
|
position: 0,
|
||||||
code: &code
|
code: &code
|
||||||
@ -303,7 +308,7 @@ impl evm::Evm for Interpreter {
|
|||||||
let instruction = code[reader.position];
|
let instruction = code[reader.position];
|
||||||
|
|
||||||
// Calculate gas cost
|
// Calculate gas cost
|
||||||
let (gas_cost, mem_size) = try!(self.get_gas_cost_mem(ext, instruction, &mut mem, &stack));
|
let (gas_cost, mem_size) = try!(self.get_gas_cost_mem(ext, instruction, &stack));
|
||||||
|
|
||||||
// TODO: make compile-time removable if too much of a performance hit.
|
// TODO: make compile-time removable if too much of a performance hit.
|
||||||
let trace_executed = ext.trace_prepare_execute(reader.position, instruction, &gas_cost);
|
let trace_executed = ext.trace_prepare_execute(reader.position, instruction, &gas_cost);
|
||||||
@ -311,7 +316,7 @@ impl evm::Evm for Interpreter {
|
|||||||
reader.position += 1;
|
reader.position += 1;
|
||||||
|
|
||||||
try!(self.verify_gas(¤t_gas, &gas_cost));
|
try!(self.verify_gas(¤t_gas, &gas_cost));
|
||||||
mem.expand(mem_size);
|
self.mem.expand(mem_size);
|
||||||
current_gas = current_gas - gas_cost; //TODO: use operator -=
|
current_gas = current_gas - gas_cost; //TODO: use operator -=
|
||||||
|
|
||||||
evm_debug!({
|
evm_debug!({
|
||||||
@ -331,11 +336,11 @@ impl evm::Evm for Interpreter {
|
|||||||
|
|
||||||
// Execute instruction
|
// Execute instruction
|
||||||
let result = try!(self.exec_instruction(
|
let result = try!(self.exec_instruction(
|
||||||
current_gas, ¶ms, ext, instruction, &mut reader, &mut mem, &mut stack
|
current_gas, ¶ms, ext, instruction, &mut reader, &mut stack
|
||||||
));
|
));
|
||||||
|
|
||||||
if trace_executed {
|
if trace_executed {
|
||||||
ext.trace_executed(current_gas, stack.peek_top(get_info(instruction).ret), mem_written.map(|(o, s)| (o, &(mem[o..(o + s)]))), store_written);
|
ext.trace_executed(current_gas, stack.peek_top(get_info(instruction).ret), mem_written.map(|(o, s)| (o, &(self.mem[o..(o + s)]))), store_written);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Advance
|
// Advance
|
||||||
@ -354,29 +359,25 @@ impl evm::Evm for Interpreter {
|
|||||||
let pos = try!(self.verify_jump(position, &valid_jump_destinations));
|
let pos = try!(self.verify_jump(position, &valid_jump_destinations));
|
||||||
reader.position = pos;
|
reader.position = pos;
|
||||||
},
|
},
|
||||||
InstructionResult::StopExecutionWithGasLeft(gas_left) => {
|
InstructionResult::StopExecutionNeedsReturn(gas, off, size) => {
|
||||||
current_gas = gas_left;
|
return Ok(GasLeft::NeedsReturn(gas, self.mem.read_slice(off, size)));
|
||||||
reader.position = code.len();
|
|
||||||
},
|
},
|
||||||
InstructionResult::StopExecution => {
|
InstructionResult::StopExecution => break,
|
||||||
reader.position = code.len();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(current_gas)
|
Ok(GasLeft::Known(current_gas))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Interpreter {
|
impl Interpreter {
|
||||||
#[cfg_attr(feature="dev", allow(cyclomatic_complexity))]
|
#[cfg_attr(feature="dev", allow(cyclomatic_complexity))]
|
||||||
fn get_gas_cost_mem(
|
fn get_gas_cost_mem(
|
||||||
&self,
|
&mut self,
|
||||||
ext: &evm::Ext,
|
ext: &evm::Ext,
|
||||||
instruction: Instruction,
|
instruction: Instruction,
|
||||||
mem: &mut Memory,
|
|
||||||
stack: &Stack<U256>
|
stack: &Stack<U256>
|
||||||
) -> Result<(U256, usize), evm::Error> {
|
) -> evm::Result<(U256, usize)> {
|
||||||
let schedule = ext.schedule();
|
let schedule = ext.schedule();
|
||||||
let info = instructions::get_info(instruction);
|
let info = instructions::get_info(instruction);
|
||||||
|
|
||||||
@ -492,12 +493,12 @@ impl Interpreter {
|
|||||||
Ok((gas, 0))
|
Ok((gas, 0))
|
||||||
},
|
},
|
||||||
InstructionCost::GasMem(gas, mem_size) => {
|
InstructionCost::GasMem(gas, mem_size) => {
|
||||||
let (mem_gas, new_mem_size) = try!(self.mem_gas_cost(schedule, mem.size(), &mem_size));
|
let (mem_gas, new_mem_size) = try!(self.mem_gas_cost(schedule, self.mem.size(), &mem_size));
|
||||||
let gas = overflowing!(gas.overflowing_add(mem_gas));
|
let gas = overflowing!(gas.overflowing_add(mem_gas));
|
||||||
Ok((gas, new_mem_size))
|
Ok((gas, new_mem_size))
|
||||||
},
|
},
|
||||||
InstructionCost::GasMemCopy(gas, mem_size, copy) => {
|
InstructionCost::GasMemCopy(gas, mem_size, copy) => {
|
||||||
let (mem_gas, new_mem_size) = try!(self.mem_gas_cost(schedule, mem.size(), &mem_size));
|
let (mem_gas, new_mem_size) = try!(self.mem_gas_cost(schedule, self.mem.size(), &mem_size));
|
||||||
let copy = overflowing!(add_u256_usize(©, 31));
|
let copy = overflowing!(add_u256_usize(©, 31));
|
||||||
let copy_gas = U256::from(schedule.copy_gas) * (copy / U256::from(32));
|
let copy_gas = U256::from(schedule.copy_gas) * (copy / U256::from(32));
|
||||||
let gas = overflowing!(gas.overflowing_add(copy_gas));
|
let gas = overflowing!(gas.overflowing_add(copy_gas));
|
||||||
@ -532,7 +533,7 @@ impl Interpreter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mem_gas_cost(&self, schedule: &evm::Schedule, current_mem_size: usize, mem_size: &U256) -> Result<(U256, usize), evm::Error> {
|
fn mem_gas_cost(&self, schedule: &evm::Schedule, current_mem_size: usize, mem_size: &U256) -> evm::Result<(U256, usize)> {
|
||||||
let gas_for_mem = |mem_size: U256| {
|
let gas_for_mem = |mem_size: U256| {
|
||||||
let s = mem_size >> 5;
|
let s = mem_size >> 5;
|
||||||
// s * memory_gas + s * s / quad_coeff_div
|
// s * memory_gas + s * s / quad_coeff_div
|
||||||
@ -557,11 +558,11 @@ impl Interpreter {
|
|||||||
}, req_mem_size_rounded.low_u64() as usize))
|
}, req_mem_size_rounded.low_u64() as usize))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mem_needed_const(&self, mem: &U256, add: usize) -> Result<U256, evm::Error> {
|
fn mem_needed_const(&self, mem: &U256, add: usize) -> evm::Result<U256> {
|
||||||
Ok(overflowing!(mem.overflowing_add(U256::from(add))))
|
Ok(overflowing!(mem.overflowing_add(U256::from(add))))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mem_needed(&self, offset: &U256, size: &U256) -> Result<U256, ::evm::Error> {
|
fn mem_needed(&self, offset: &U256, size: &U256) -> evm::Result<U256> {
|
||||||
if self.is_zero(size) {
|
if self.is_zero(size) {
|
||||||
return Ok(U256::zero());
|
return Ok(U256::zero());
|
||||||
}
|
}
|
||||||
@ -571,15 +572,14 @@ impl Interpreter {
|
|||||||
|
|
||||||
#[cfg_attr(feature="dev", allow(too_many_arguments))]
|
#[cfg_attr(feature="dev", allow(too_many_arguments))]
|
||||||
fn exec_instruction(
|
fn exec_instruction(
|
||||||
&self,
|
&mut self,
|
||||||
gas: Gas,
|
gas: Gas,
|
||||||
params: &ActionParams,
|
params: &ActionParams,
|
||||||
ext: &mut evm::Ext,
|
ext: &mut evm::Ext,
|
||||||
instruction: Instruction,
|
instruction: Instruction,
|
||||||
code: &mut CodeReader,
|
code: &mut CodeReader,
|
||||||
mem: &mut Memory,
|
|
||||||
stack: &mut Stack<U256>
|
stack: &mut Stack<U256>
|
||||||
) -> Result<InstructionResult, evm::Error> {
|
) -> evm::Result<InstructionResult> {
|
||||||
match instruction {
|
match instruction {
|
||||||
instructions::JUMP => {
|
instructions::JUMP => {
|
||||||
let jump = stack.pop_back();
|
let jump = stack.pop_back();
|
||||||
@ -604,7 +604,7 @@ impl Interpreter {
|
|||||||
let init_off = stack.pop_back();
|
let init_off = stack.pop_back();
|
||||||
let init_size = stack.pop_back();
|
let init_size = stack.pop_back();
|
||||||
|
|
||||||
let contract_code = mem.read_slice(init_off, init_size);
|
let contract_code = self.mem.read_slice(init_off, init_size);
|
||||||
let can_create = ext.balance(¶ms.address) >= endowment && ext.depth() < ext.schedule().max_depth;
|
let can_create = ext.balance(¶ms.address) >= endowment && ext.depth() < ext.schedule().max_depth;
|
||||||
|
|
||||||
if !can_create {
|
if !can_create {
|
||||||
@ -671,8 +671,8 @@ impl Interpreter {
|
|||||||
let call_result = {
|
let call_result = {
|
||||||
// we need to write and read from memory in the same time
|
// we need to write and read from memory in the same time
|
||||||
// and we don't want to copy
|
// and we don't want to copy
|
||||||
let input = unsafe { ::std::mem::transmute(mem.read_slice(in_off, in_size)) };
|
let input = unsafe { ::std::mem::transmute(self.mem.read_slice(in_off, in_size)) };
|
||||||
let output = mem.writeable_slice(out_off, out_size);
|
let output = self.mem.writeable_slice(out_off, out_size);
|
||||||
ext.call(&call_gas, sender_address, receive_address, value, input, &code_address, output)
|
ext.call(&call_gas, sender_address, receive_address, value, input, &code_address, output)
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -690,11 +690,8 @@ impl Interpreter {
|
|||||||
instructions::RETURN => {
|
instructions::RETURN => {
|
||||||
let init_off = stack.pop_back();
|
let init_off = stack.pop_back();
|
||||||
let init_size = stack.pop_back();
|
let init_size = stack.pop_back();
|
||||||
let return_code = mem.read_slice(init_off, init_size);
|
|
||||||
let gas_left = try!(ext.ret(&gas, &return_code));
|
return Ok(InstructionResult::StopExecutionNeedsReturn(gas, init_off, init_size))
|
||||||
return Ok(InstructionResult::StopExecutionWithGasLeft(
|
|
||||||
gas_left
|
|
||||||
));
|
|
||||||
},
|
},
|
||||||
instructions::STOP => {
|
instructions::STOP => {
|
||||||
return Ok(InstructionResult::StopExecution);
|
return Ok(InstructionResult::StopExecution);
|
||||||
@ -713,7 +710,7 @@ impl Interpreter {
|
|||||||
.iter()
|
.iter()
|
||||||
.map(H256::from)
|
.map(H256::from)
|
||||||
.collect();
|
.collect();
|
||||||
ext.log(topics, mem.read_slice(offset, size));
|
ext.log(topics, self.mem.read_slice(offset, size));
|
||||||
},
|
},
|
||||||
instructions::PUSH1...instructions::PUSH32 => {
|
instructions::PUSH1...instructions::PUSH32 => {
|
||||||
let bytes = instructions::get_push_bytes(instruction);
|
let bytes = instructions::get_push_bytes(instruction);
|
||||||
@ -721,26 +718,26 @@ impl Interpreter {
|
|||||||
stack.push(val);
|
stack.push(val);
|
||||||
},
|
},
|
||||||
instructions::MLOAD => {
|
instructions::MLOAD => {
|
||||||
let word = mem.read(stack.pop_back());
|
let word = self.mem.read(stack.pop_back());
|
||||||
stack.push(U256::from(word));
|
stack.push(U256::from(word));
|
||||||
},
|
},
|
||||||
instructions::MSTORE => {
|
instructions::MSTORE => {
|
||||||
let offset = stack.pop_back();
|
let offset = stack.pop_back();
|
||||||
let word = stack.pop_back();
|
let word = stack.pop_back();
|
||||||
mem.write(offset, word);
|
Memory::write(&mut self.mem, offset, word);
|
||||||
},
|
},
|
||||||
instructions::MSTORE8 => {
|
instructions::MSTORE8 => {
|
||||||
let offset = stack.pop_back();
|
let offset = stack.pop_back();
|
||||||
let byte = stack.pop_back();
|
let byte = stack.pop_back();
|
||||||
mem.write_byte(offset, byte);
|
self.mem.write_byte(offset, byte);
|
||||||
},
|
},
|
||||||
instructions::MSIZE => {
|
instructions::MSIZE => {
|
||||||
stack.push(U256::from(mem.size()));
|
stack.push(U256::from(self.mem.size()));
|
||||||
},
|
},
|
||||||
instructions::SHA3 => {
|
instructions::SHA3 => {
|
||||||
let offset = stack.pop_back();
|
let offset = stack.pop_back();
|
||||||
let size = stack.pop_back();
|
let size = stack.pop_back();
|
||||||
let sha3 = mem.read_slice(offset, size).sha3();
|
let sha3 = self.mem.read_slice(offset, size).sha3();
|
||||||
stack.push(U256::from(sha3.as_slice()));
|
stack.push(U256::from(sha3.as_slice()));
|
||||||
},
|
},
|
||||||
instructions::SLOAD => {
|
instructions::SLOAD => {
|
||||||
@ -813,15 +810,15 @@ impl Interpreter {
|
|||||||
stack.push(U256::from(len));
|
stack.push(U256::from(len));
|
||||||
},
|
},
|
||||||
instructions::CALLDATACOPY => {
|
instructions::CALLDATACOPY => {
|
||||||
self.copy_data_to_memory(mem, stack, ¶ms.data.clone().unwrap_or_else(|| vec![]));
|
self.copy_data_to_memory(stack, ¶ms.data.clone().unwrap_or_else(|| vec![]));
|
||||||
},
|
},
|
||||||
instructions::CODECOPY => {
|
instructions::CODECOPY => {
|
||||||
self.copy_data_to_memory(mem, stack, ¶ms.code.clone().unwrap_or_else(|| vec![]));
|
self.copy_data_to_memory(stack, ¶ms.code.clone().unwrap_or_else(|| vec![]));
|
||||||
},
|
},
|
||||||
instructions::EXTCODECOPY => {
|
instructions::EXTCODECOPY => {
|
||||||
let address = u256_to_address(&stack.pop_back());
|
let address = u256_to_address(&stack.pop_back());
|
||||||
let code = ext.extcode(&address);
|
let code = ext.extcode(&address);
|
||||||
self.copy_data_to_memory(mem, stack, &code);
|
self.copy_data_to_memory(stack, &code);
|
||||||
},
|
},
|
||||||
instructions::GASPRICE => {
|
instructions::GASPRICE => {
|
||||||
stack.push(params.gas_price.clone());
|
stack.push(params.gas_price.clone());
|
||||||
@ -853,7 +850,7 @@ impl Interpreter {
|
|||||||
Ok(InstructionResult::Ok)
|
Ok(InstructionResult::Ok)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn copy_data_to_memory(&self, mem: &mut Memory, stack: &mut Stack<U256>, data: &[u8]) {
|
fn copy_data_to_memory(&mut self, stack: &mut Stack<U256>, data: &[u8]) {
|
||||||
let dest_offset = stack.pop_back();
|
let dest_offset = stack.pop_back();
|
||||||
let source_offset = stack.pop_back();
|
let source_offset = stack.pop_back();
|
||||||
let size = stack.pop_back();
|
let size = stack.pop_back();
|
||||||
@ -862,9 +859,9 @@ impl Interpreter {
|
|||||||
let output_end = match source_offset > source_size || size > source_size || source_offset + size > source_size {
|
let output_end = match source_offset > source_size || size > source_size || source_offset + size > source_size {
|
||||||
true => {
|
true => {
|
||||||
let zero_slice = if source_offset > source_size {
|
let zero_slice = if source_offset > source_size {
|
||||||
mem.writeable_slice(dest_offset, size)
|
self.mem.writeable_slice(dest_offset, size)
|
||||||
} else {
|
} else {
|
||||||
mem.writeable_slice(dest_offset + source_size - source_offset, source_offset + size - source_size)
|
self.mem.writeable_slice(dest_offset + source_size - source_offset, source_offset + size - source_size)
|
||||||
};
|
};
|
||||||
for i in zero_slice.iter_mut() {
|
for i in zero_slice.iter_mut() {
|
||||||
*i = 0;
|
*i = 0;
|
||||||
@ -876,7 +873,7 @@ impl Interpreter {
|
|||||||
|
|
||||||
if source_offset < source_size {
|
if source_offset < source_size {
|
||||||
let output_begin = source_offset.low_u64() as usize;
|
let output_begin = source_offset.low_u64() as usize;
|
||||||
mem.write_slice(dest_offset, &data[output_begin..output_end]);
|
self.mem.write_slice(dest_offset, &data[output_begin..output_end]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -885,7 +882,7 @@ impl Interpreter {
|
|||||||
info: &instructions::InstructionInfo,
|
info: &instructions::InstructionInfo,
|
||||||
stack_limit: usize,
|
stack_limit: usize,
|
||||||
stack: &Stack<U256>
|
stack: &Stack<U256>
|
||||||
) -> Result<(), evm::Error> {
|
) -> evm::Result<()> {
|
||||||
if !stack.has(info.args) {
|
if !stack.has(info.args) {
|
||||||
Err(evm::Error::StackUnderflow {
|
Err(evm::Error::StackUnderflow {
|
||||||
instruction: info.name,
|
instruction: info.name,
|
||||||
@ -903,14 +900,14 @@ impl Interpreter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn verify_gas(&self, current_gas: &U256, gas_cost: &U256) -> Result<(), evm::Error> {
|
fn verify_gas(&self, current_gas: &U256, gas_cost: &U256) -> evm::Result<()> {
|
||||||
match current_gas < gas_cost {
|
match current_gas < gas_cost {
|
||||||
true => Err(evm::Error::OutOfGas),
|
true => Err(evm::Error::OutOfGas),
|
||||||
false => Ok(())
|
false => Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn verify_jump(&self, jump_u: U256, valid_jump_destinations: &HashSet<usize>) -> Result<usize, evm::Error> {
|
fn verify_jump(&self, jump_u: U256, valid_jump_destinations: &HashSet<usize>) -> evm::Result<usize> {
|
||||||
let jump = jump_u.low_u64() as usize;
|
let jump = jump_u.low_u64() as usize;
|
||||||
|
|
||||||
if valid_jump_destinations.contains(&jump) && jump_u < U256::from(!0 as usize) {
|
if valid_jump_destinations.contains(&jump) && jump_u < U256::from(!0 as usize) {
|
||||||
@ -934,7 +931,7 @@ impl Interpreter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exec_stack_instruction(&self, instruction: Instruction, stack: &mut Stack<U256>) -> Result<(), evm::Error> {
|
fn exec_stack_instruction(&self, instruction: Instruction, stack: &mut Stack<U256>) -> evm::Result<()> {
|
||||||
match instruction {
|
match instruction {
|
||||||
instructions::DUP1...instructions::DUP16 => {
|
instructions::DUP1...instructions::DUP16 => {
|
||||||
let position = instructions::get_dup_position(instruction);
|
let position = instructions::get_dup_position(instruction);
|
||||||
@ -1185,7 +1182,7 @@ fn address_to_u256(value: Address) -> U256 {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_mem_gas_cost() {
|
fn test_mem_gas_cost() {
|
||||||
// given
|
// given
|
||||||
let interpreter = Interpreter;
|
let interpreter = Interpreter::default();
|
||||||
let schedule = evm::Schedule::default();
|
let schedule = evm::Schedule::default();
|
||||||
let current_mem_size = 5;
|
let current_mem_size = 5;
|
||||||
let mem_size = !U256::zero();
|
let mem_size = !U256::zero();
|
||||||
@ -1208,7 +1205,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_find_jump_destinations() {
|
fn test_find_jump_destinations() {
|
||||||
// given
|
// given
|
||||||
let interpreter = Interpreter;
|
let interpreter = Interpreter::default();
|
||||||
let code = "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff5b01600055".from_hex().unwrap();
|
let code = "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff5b01600055".from_hex().unwrap();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
@ -1221,7 +1218,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_calculate_mem_cost() {
|
fn test_calculate_mem_cost() {
|
||||||
// given
|
// given
|
||||||
let interpreter = Interpreter;
|
let interpreter = Interpreter::default();
|
||||||
let schedule = evm::Schedule::default();
|
let schedule = evm::Schedule::default();
|
||||||
let current_mem_size = 0;
|
let current_mem_size = 0;
|
||||||
let mem_size = U256::from(5);
|
let mem_size = U256::from(5);
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
use common::*;
|
use common::*;
|
||||||
use trace::VMTracer;
|
use trace::VMTracer;
|
||||||
use evmjit;
|
use evmjit;
|
||||||
use evm;
|
use evm::{self, Error, GasLeft};
|
||||||
|
|
||||||
/// Should be used to convert jit types to ethcore
|
/// Should be used to convert jit types to ethcore
|
||||||
trait FromJit<T>: Sized {
|
trait FromJit<T>: Sized {
|
||||||
@ -301,10 +301,13 @@ impl<'a> evmjit::Ext for ExtAdapter<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct JitEvm;
|
#[derive(Default)]
|
||||||
|
pub struct JitEvm {
|
||||||
|
ctxt: Option<evmjit::ContextHandle>,
|
||||||
|
}
|
||||||
|
|
||||||
impl evm::Evm for JitEvm {
|
impl evm::Evm for JitEvm {
|
||||||
fn exec(&self, params: ActionParams, ext: &mut evm::Ext) -> evm::Result {
|
fn exec(&mut self, params: ActionParams, ext: &mut evm::Ext) -> evm::Result<GasLeft> {
|
||||||
// Dirty hack. This is unsafe, but we interact with ffi, so it's justified.
|
// Dirty hack. This is unsafe, but we interact with ffi, so it's justified.
|
||||||
let ext_adapter: ExtAdapter<'static> = unsafe { ::std::mem::transmute(ExtAdapter::new(ext, params.address.clone())) };
|
let ext_adapter: ExtAdapter<'static> = unsafe { ::std::mem::transmute(ExtAdapter::new(ext, params.address.clone())) };
|
||||||
let mut ext_handle = evmjit::ExtHandle::new(ext_adapter);
|
let mut ext_handle = evmjit::ExtHandle::new(ext_adapter);
|
||||||
@ -343,15 +346,17 @@ impl evm::Evm for JitEvm {
|
|||||||
// don't really know why jit timestamp is int..
|
// don't really know why jit timestamp is int..
|
||||||
data.timestamp = ext.env_info().timestamp as i64;
|
data.timestamp = ext.env_info().timestamp as i64;
|
||||||
|
|
||||||
let mut context = unsafe { evmjit::ContextHandle::new(data, schedule, &mut ext_handle) };
|
self.context = Some(unsafe { evmjit::ContextHandle::new(data, schedule, &mut ext_handle) });
|
||||||
|
let context = self.context.as_ref_mut().unwrap();
|
||||||
let res = context.exec();
|
let res = context.exec();
|
||||||
|
|
||||||
match res {
|
match res {
|
||||||
evmjit::ReturnCode::Stop => Ok(U256::from(context.gas_left())),
|
evmjit::ReturnCode::Stop => Ok(GasLeft::Known(U256::from(context.gas_left()))),
|
||||||
evmjit::ReturnCode::Return => ext.ret(&U256::from(context.gas_left()), context.output_data()),
|
evmjit::ReturnCode::Return =>
|
||||||
|
Ok(GasLeft::NeedsReturn(U256::from(context.gas_left()), context.output_data())),
|
||||||
evmjit::ReturnCode::Suicide => {
|
evmjit::ReturnCode::Suicide => {
|
||||||
ext.suicide(&Address::from_jit(&context.suicide_refund_address()));
|
ext.suicide(&Address::from_jit(&context.suicide_refund_address()));
|
||||||
Ok(U256::from(context.gas_left()))
|
Ok(GasLeft::Known(U256::from(context.gas_left())))
|
||||||
},
|
},
|
||||||
evmjit::ReturnCode::OutOfGas => Err(evm::Error::OutOfGas),
|
evmjit::ReturnCode::OutOfGas => Err(evm::Error::OutOfGas),
|
||||||
_err => Err(evm::Error::Internal)
|
_err => Err(evm::Error::Internal)
|
||||||
|
@ -29,7 +29,7 @@ mod jit;
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
pub use self::evm::{Evm, Error, Result};
|
pub use self::evm::{Evm, Error, Finalize, GasLeft, Result};
|
||||||
pub use self::ext::{Ext, ContractCreateResult, MessageCallResult};
|
pub use self::ext::{Ext, ContractCreateResult, MessageCallResult};
|
||||||
pub use self::factory::{Factory, VMType};
|
pub use self::factory::{Factory, VMType};
|
||||||
pub use self::schedule::Schedule;
|
pub use self::schedule::Schedule;
|
||||||
|
@ -15,8 +15,7 @@
|
|||||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use common::*;
|
use common::*;
|
||||||
use evm;
|
use evm::{self, Ext, Schedule, Factory, GasLeft, VMType, ContractCreateResult, MessageCallResult};
|
||||||
use evm::{Ext, Schedule, Factory, VMType, ContractCreateResult, MessageCallResult};
|
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
struct FakeLogEntry {
|
struct FakeLogEntry {
|
||||||
@ -58,6 +57,15 @@ struct FakeExt {
|
|||||||
calls: HashSet<FakeCall>,
|
calls: HashSet<FakeCall>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// similar to the normal `finalize` function, but ignoring NeedsReturn.
|
||||||
|
fn test_finalize(res: Result<GasLeft, evm::Error>) -> Result<U256, evm::Error> {
|
||||||
|
match res {
|
||||||
|
Ok(GasLeft::Known(gas)) => Ok(gas),
|
||||||
|
Ok(GasLeft::NeedsReturn(_, _)) => unimplemented!(), // since ret is unimplemented.
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl FakeExt {
|
impl FakeExt {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
FakeExt::default()
|
FakeExt::default()
|
||||||
@ -136,7 +144,7 @@ impl Ext for FakeExt {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ret(&mut self, _gas: &U256, _data: &[u8]) -> result::Result<U256, evm::Error> {
|
fn ret(self, _gas: &U256, _data: &[u8]) -> evm::Result<U256> {
|
||||||
unimplemented!();
|
unimplemented!();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,8 +181,8 @@ fn test_stack_underflow() {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let err = {
|
let err = {
|
||||||
let vm : Box<evm::Evm> = Box::new(super::interpreter::Interpreter);
|
let mut vm : Box<evm::Evm> = Box::new(super::interpreter::Interpreter::default());
|
||||||
vm.exec(params, &mut ext).unwrap_err()
|
test_finalize(vm.exec(params, &mut ext)).unwrap_err()
|
||||||
};
|
};
|
||||||
|
|
||||||
match err {
|
match err {
|
||||||
@ -200,8 +208,8 @@ fn test_add(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(gas_left, U256::from(79_988));
|
assert_eq!(gas_left, U256::from(79_988));
|
||||||
@ -220,8 +228,8 @@ fn test_sha3(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(gas_left, U256::from(79_961));
|
assert_eq!(gas_left, U256::from(79_961));
|
||||||
@ -240,8 +248,8 @@ fn test_address(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(gas_left, U256::from(79_995));
|
assert_eq!(gas_left, U256::from(79_995));
|
||||||
@ -262,8 +270,8 @@ fn test_origin(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(gas_left, U256::from(79_995));
|
assert_eq!(gas_left, U256::from(79_995));
|
||||||
@ -284,8 +292,8 @@ fn test_sender(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(gas_left, U256::from(79_995));
|
assert_eq!(gas_left, U256::from(79_995));
|
||||||
@ -319,8 +327,8 @@ fn test_extcodecopy(factory: super::Factory) {
|
|||||||
ext.codes.insert(sender, sender_code);
|
ext.codes.insert(sender, sender_code);
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(gas_left, U256::from(79_935));
|
assert_eq!(gas_left, U256::from(79_935));
|
||||||
@ -339,8 +347,8 @@ fn test_log_empty(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(gas_left, U256::from(99_619));
|
assert_eq!(gas_left, U256::from(99_619));
|
||||||
@ -371,8 +379,8 @@ fn test_log_sender(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(gas_left, U256::from(98_974));
|
assert_eq!(gas_left, U256::from(98_974));
|
||||||
@ -396,8 +404,8 @@ fn test_blockhash(factory: super::Factory) {
|
|||||||
ext.blockhashes.insert(U256::zero(), blockhash.clone());
|
ext.blockhashes.insert(U256::zero(), blockhash.clone());
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(gas_left, U256::from(79_974));
|
assert_eq!(gas_left, U256::from(79_974));
|
||||||
@ -418,8 +426,8 @@ fn test_calldataload(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(gas_left, U256::from(79_991));
|
assert_eq!(gas_left, U256::from(79_991));
|
||||||
@ -439,8 +447,8 @@ fn test_author(factory: super::Factory) {
|
|||||||
ext.info.author = author;
|
ext.info.author = author;
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(gas_left, U256::from(79_995));
|
assert_eq!(gas_left, U256::from(79_995));
|
||||||
@ -459,8 +467,8 @@ fn test_timestamp(factory: super::Factory) {
|
|||||||
ext.info.timestamp = timestamp;
|
ext.info.timestamp = timestamp;
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(gas_left, U256::from(79_995));
|
assert_eq!(gas_left, U256::from(79_995));
|
||||||
@ -479,8 +487,8 @@ fn test_number(factory: super::Factory) {
|
|||||||
ext.info.number = number;
|
ext.info.number = number;
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(gas_left, U256::from(79_995));
|
assert_eq!(gas_left, U256::from(79_995));
|
||||||
@ -499,8 +507,8 @@ fn test_difficulty(factory: super::Factory) {
|
|||||||
ext.info.difficulty = difficulty;
|
ext.info.difficulty = difficulty;
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(gas_left, U256::from(79_995));
|
assert_eq!(gas_left, U256::from(79_995));
|
||||||
@ -519,8 +527,8 @@ fn test_gas_limit(factory: super::Factory) {
|
|||||||
ext.info.gas_limit = gas_limit;
|
ext.info.gas_limit = gas_limit;
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(gas_left, U256::from(79_995));
|
assert_eq!(gas_left, U256::from(79_995));
|
||||||
@ -537,8 +545,8 @@ fn test_mul(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_store(&ext, 0, "000000000000000000000000000000000000000000000000734349397b853383");
|
assert_store(&ext, 0, "000000000000000000000000000000000000000000000000734349397b853383");
|
||||||
@ -555,8 +563,8 @@ fn test_sub(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000012364ad0302");
|
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000012364ad0302");
|
||||||
@ -573,8 +581,8 @@ fn test_div(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_store(&ext, 0, "000000000000000000000000000000000000000000000000000000000002e0ac");
|
assert_store(&ext, 0, "000000000000000000000000000000000000000000000000000000000002e0ac");
|
||||||
@ -591,8 +599,8 @@ fn test_div_zero(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000000000000000");
|
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000000000000000");
|
||||||
@ -609,8 +617,8 @@ fn test_mod(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000000000076b4b");
|
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000000000076b4b");
|
||||||
@ -628,8 +636,8 @@ fn test_smod(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000000000076b4b");
|
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000000000076b4b");
|
||||||
@ -647,8 +655,8 @@ fn test_sdiv(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_store(&ext, 0, "000000000000000000000000000000000000000000000000000000000002e0ac");
|
assert_store(&ext, 0, "000000000000000000000000000000000000000000000000000000000002e0ac");
|
||||||
@ -666,8 +674,8 @@ fn test_exp(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_store(&ext, 0, "90fd23767b60204c3d6fc8aec9e70a42a3f127140879c133a20129a597ed0c59");
|
assert_store(&ext, 0, "90fd23767b60204c3d6fc8aec9e70a42a3f127140879c133a20129a597ed0c59");
|
||||||
@ -686,8 +694,8 @@ fn test_comparison(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000000000000000");
|
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000000000000000");
|
||||||
@ -707,8 +715,8 @@ fn test_signed_comparison(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000000000000000");
|
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000000000000000");
|
||||||
@ -728,8 +736,8 @@ fn test_bitops(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_store(&ext, 0, "00000000000000000000000000000000000000000000000000000000000000f0");
|
assert_store(&ext, 0, "00000000000000000000000000000000000000000000000000000000000000f0");
|
||||||
@ -751,8 +759,8 @@ fn test_addmod_mulmod(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000000000000001");
|
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000000000000001");
|
||||||
@ -772,8 +780,8 @@ fn test_byte(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000000000000000");
|
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000000000000000");
|
||||||
@ -791,8 +799,8 @@ fn test_signextend(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000000000000fff");
|
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000000000000fff");
|
||||||
@ -811,8 +819,8 @@ fn test_badinstruction_int() {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let err = {
|
let err = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap_err()
|
test_finalize(vm.exec(params, &mut ext)).unwrap_err()
|
||||||
};
|
};
|
||||||
|
|
||||||
match err {
|
match err {
|
||||||
@ -831,8 +839,8 @@ fn test_pop(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_store(&ext, 0, "00000000000000000000000000000000000000000000000000000000000000f0");
|
assert_store(&ext, 0, "00000000000000000000000000000000000000000000000000000000000000f0");
|
||||||
@ -851,8 +859,8 @@ fn test_extops(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000000000000004"); // PC / CALLDATASIZE
|
assert_store(&ext, 0, "0000000000000000000000000000000000000000000000000000000000000004"); // PC / CALLDATASIZE
|
||||||
@ -874,8 +882,8 @@ fn test_jumps(factory: super::Factory) {
|
|||||||
let mut ext = FakeExt::new();
|
let mut ext = FakeExt::new();
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(ext.sstore_clears, 1);
|
assert_eq!(ext.sstore_clears, 1);
|
||||||
@ -903,8 +911,8 @@ fn test_calls(factory: super::Factory) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let gas_left = {
|
let gas_left = {
|
||||||
let vm = factory.create();
|
let mut vm = factory.create();
|
||||||
vm.exec(params, &mut ext).unwrap()
|
test_finalize(vm.exec(params, &mut ext)).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_set_contains(&ext.calls, &FakeCall {
|
assert_set_contains(&ext.calls, &FakeCall {
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
use common::*;
|
use common::*;
|
||||||
use state::*;
|
use state::*;
|
||||||
use engine::*;
|
use engine::*;
|
||||||
use evm::{self, Ext, Factory};
|
use evm::{self, Ext, Factory, Finalize};
|
||||||
use externalities::*;
|
use externalities::*;
|
||||||
use substate::*;
|
use substate::*;
|
||||||
use trace::{Trace, Tracer, NoopTracer, ExecutiveTracer, VMTrace, VMTracer, ExecutiveVMTracer, NoopVMTracer};
|
use trace::{Trace, Tracer, NoopTracer, ExecutiveTracer, VMTrace, VMTracer, ExecutiveVMTracer, NoopVMTracer};
|
||||||
@ -205,13 +205,13 @@ impl<'a> Executive<'a> {
|
|||||||
output_policy: OutputPolicy,
|
output_policy: OutputPolicy,
|
||||||
tracer: &mut T,
|
tracer: &mut T,
|
||||||
vm_tracer: &mut V
|
vm_tracer: &mut V
|
||||||
) -> evm::Result where T: Tracer, V: VMTracer {
|
) -> evm::Result<U256> where T: Tracer, V: VMTracer {
|
||||||
// Ordinary execution - keep VM in same thread
|
// Ordinary execution - keep VM in same thread
|
||||||
if (self.depth + 1) % MAX_VM_DEPTH_FOR_THREAD != 0 {
|
if (self.depth + 1) % MAX_VM_DEPTH_FOR_THREAD != 0 {
|
||||||
let vm_factory = self.vm_factory;
|
let vm_factory = self.vm_factory;
|
||||||
let mut ext = self.as_externalities(OriginInfo::from(¶ms), unconfirmed_substate, output_policy, tracer, vm_tracer);
|
let mut ext = self.as_externalities(OriginInfo::from(¶ms), unconfirmed_substate, output_policy, tracer, vm_tracer);
|
||||||
trace!(target: "executive", "ext.schedule.have_delegate_call: {}", ext.schedule().have_delegate_call);
|
trace!(target: "executive", "ext.schedule.have_delegate_call: {}", ext.schedule().have_delegate_call);
|
||||||
return vm_factory.create().exec(params, &mut ext);
|
return vm_factory.create().exec(params, &mut ext).finalize(ext);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start in new thread to reset stack
|
// Start in new thread to reset stack
|
||||||
@ -222,7 +222,7 @@ impl<'a> Executive<'a> {
|
|||||||
let mut ext = self.as_externalities(OriginInfo::from(¶ms), unconfirmed_substate, output_policy, tracer, vm_tracer);
|
let mut ext = self.as_externalities(OriginInfo::from(¶ms), unconfirmed_substate, output_policy, tracer, vm_tracer);
|
||||||
|
|
||||||
scope.spawn(move || {
|
scope.spawn(move || {
|
||||||
vm_factory.create().exec(params, &mut ext)
|
vm_factory.create().exec(params, &mut ext).finalize(ext)
|
||||||
})
|
})
|
||||||
}).join()
|
}).join()
|
||||||
}
|
}
|
||||||
@ -238,7 +238,7 @@ impl<'a> Executive<'a> {
|
|||||||
mut output: BytesRef,
|
mut output: BytesRef,
|
||||||
tracer: &mut T,
|
tracer: &mut T,
|
||||||
vm_tracer: &mut V
|
vm_tracer: &mut V
|
||||||
) -> evm::Result where T: Tracer, V: VMTracer {
|
) -> evm::Result<U256> where T: Tracer, V: VMTracer {
|
||||||
// backup used in case of running out of gas
|
// backup used in case of running out of gas
|
||||||
self.state.snapshot();
|
self.state.snapshot();
|
||||||
|
|
||||||
@ -351,7 +351,7 @@ impl<'a> Executive<'a> {
|
|||||||
substate: &mut Substate,
|
substate: &mut Substate,
|
||||||
tracer: &mut T,
|
tracer: &mut T,
|
||||||
vm_tracer: &mut V
|
vm_tracer: &mut V
|
||||||
) -> evm::Result where T: Tracer, V: VMTracer {
|
) -> evm::Result<U256> where T: Tracer, V: VMTracer {
|
||||||
// backup used in case of running out of gas
|
// backup used in case of running out of gas
|
||||||
self.state.snapshot();
|
self.state.snapshot();
|
||||||
|
|
||||||
@ -402,7 +402,7 @@ impl<'a> Executive<'a> {
|
|||||||
&mut self,
|
&mut self,
|
||||||
t: &SignedTransaction,
|
t: &SignedTransaction,
|
||||||
substate: Substate,
|
substate: Substate,
|
||||||
result: evm::Result,
|
result: evm::Result<U256>,
|
||||||
output: Bytes,
|
output: Bytes,
|
||||||
trace: Option<Trace>,
|
trace: Option<Trace>,
|
||||||
vm_trace: Option<VMTrace>
|
vm_trace: Option<VMTrace>
|
||||||
@ -470,7 +470,7 @@ impl<'a> Executive<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enact_result(&mut self, result: &evm::Result, substate: &mut Substate, un_substate: Substate) {
|
fn enact_result(&mut self, result: &evm::Result<U256>, substate: &mut Substate, un_substate: Substate) {
|
||||||
match *result {
|
match *result {
|
||||||
Err(evm::Error::OutOfGas)
|
Err(evm::Error::OutOfGas)
|
||||||
| Err(evm::Error::BadJumpDestination {..})
|
| Err(evm::Error::BadJumpDestination {..})
|
||||||
|
@ -203,7 +203,8 @@ impl<'a, T, V> Ext for Externalities<'a, T, V> where T: 'a + Tracer, V: 'a + VMT
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(feature="dev", allow(match_ref_pats))]
|
#[cfg_attr(feature="dev", allow(match_ref_pats))]
|
||||||
fn ret(&mut self, gas: &U256, data: &[u8]) -> Result<U256, evm::Error> {
|
fn ret(mut self, gas: &U256, data: &[u8]) -> evm::Result<U256>
|
||||||
|
where Self: Sized {
|
||||||
let handle_copy = |to: &mut Option<&mut Bytes>| {
|
let handle_copy = |to: &mut Option<&mut Bytes>| {
|
||||||
to.as_mut().map(|b| **b = data.to_owned());
|
to.as_mut().map(|b| **b = data.to_owned());
|
||||||
};
|
};
|
||||||
@ -212,20 +213,14 @@ impl<'a, T, V> Ext for Externalities<'a, T, V> where T: 'a + Tracer, V: 'a + VMT
|
|||||||
handle_copy(copy);
|
handle_copy(copy);
|
||||||
|
|
||||||
let len = cmp::min(slice.len(), data.len());
|
let len = cmp::min(slice.len(), data.len());
|
||||||
unsafe {
|
(&mut slice[..len]).copy_from_slice(&data[..len]);
|
||||||
ptr::copy(data.as_ptr(), slice.as_mut_ptr(), len);
|
|
||||||
}
|
|
||||||
Ok(*gas)
|
Ok(*gas)
|
||||||
},
|
},
|
||||||
OutputPolicy::Return(BytesRef::Flexible(ref mut vec), ref mut copy) => {
|
OutputPolicy::Return(BytesRef::Flexible(ref mut vec), ref mut copy) => {
|
||||||
handle_copy(copy);
|
handle_copy(copy);
|
||||||
|
|
||||||
vec.clear();
|
vec.clear();
|
||||||
vec.reserve(data.len());
|
vec.extend_from_slice(data);
|
||||||
unsafe {
|
|
||||||
ptr::copy(data.as_ptr(), vec.as_mut_ptr(), data.len());
|
|
||||||
vec.set_len(data.len());
|
|
||||||
}
|
|
||||||
Ok(*gas)
|
Ok(*gas)
|
||||||
},
|
},
|
||||||
OutputPolicy::InitContract(ref mut copy) => {
|
OutputPolicy::InitContract(ref mut copy) => {
|
||||||
@ -240,11 +235,8 @@ impl<'a, T, V> Ext for Externalities<'a, T, V> where T: 'a + Tracer, V: 'a + VMT
|
|||||||
handle_copy(copy);
|
handle_copy(copy);
|
||||||
|
|
||||||
let mut code = vec![];
|
let mut code = vec![];
|
||||||
code.reserve(data.len());
|
code.extend_from_slice(data);
|
||||||
unsafe {
|
|
||||||
ptr::copy(data.as_ptr(), code.as_mut_ptr(), data.len());
|
|
||||||
code.set_len(data.len());
|
|
||||||
}
|
|
||||||
self.state.init_code(&self.origin_info.address, code);
|
self.state.init_code(&self.origin_info.address, code);
|
||||||
Ok(*gas - return_cost)
|
Ok(*gas - return_cost)
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ use state::*;
|
|||||||
use executive::*;
|
use executive::*;
|
||||||
use engine::*;
|
use engine::*;
|
||||||
use evm;
|
use evm;
|
||||||
use evm::{Schedule, Ext, Factory, VMType, ContractCreateResult, MessageCallResult};
|
use evm::{Schedule, Ext, Factory, Finalize, VMType, ContractCreateResult, MessageCallResult};
|
||||||
use externalities::*;
|
use externalities::*;
|
||||||
use substate::*;
|
use substate::*;
|
||||||
use tests::helpers::*;
|
use tests::helpers::*;
|
||||||
@ -27,7 +27,7 @@ use ethjson;
|
|||||||
use trace::{Tracer, NoopTracer};
|
use trace::{Tracer, NoopTracer};
|
||||||
use trace::{VMTracer, NoopVMTracer};
|
use trace::{VMTracer, NoopVMTracer};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
struct CallCreate {
|
struct CallCreate {
|
||||||
data: Bytes,
|
data: Bytes,
|
||||||
destination: Option<Address>,
|
destination: Option<Address>,
|
||||||
@ -133,7 +133,7 @@ impl<'a, T, V> Ext for TestExt<'a, T, V> where T: Tracer, V: VMTracer {
|
|||||||
self.ext.log(topics, data)
|
self.ext.log(topics, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ret(&mut self, gas: &U256, data: &[u8]) -> Result<U256, evm::Error> {
|
fn ret(self, gas: &U256, data: &[u8]) -> Result<U256, evm::Error> {
|
||||||
self.ext.ret(gas, data)
|
self.ext.ret(gas, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,9 +208,11 @@ fn do_json_test_for(vm_type: &VMType, json_data: &[u8]) -> Vec<String> {
|
|||||||
&mut tracer,
|
&mut tracer,
|
||||||
&mut vm_tracer,
|
&mut vm_tracer,
|
||||||
);
|
);
|
||||||
let evm = vm_factory.create();
|
let mut evm = vm_factory.create();
|
||||||
let res = evm.exec(params, &mut ex);
|
let res = evm.exec(params, &mut ex);
|
||||||
(res, ex.callcreates)
|
// a return in finalize will not alter callcreates
|
||||||
|
let callcreates = ex.callcreates.clone();
|
||||||
|
(res.finalize(ex), callcreates)
|
||||||
};
|
};
|
||||||
|
|
||||||
match res {
|
match res {
|
||||||
|
2
hook.sh
2
hook.sh
@ -7,6 +7,6 @@ echo "set -e" >> $FILE
|
|||||||
echo "cargo build --features dev" >> $FILE
|
echo "cargo build --features dev" >> $FILE
|
||||||
# Build tests
|
# Build tests
|
||||||
echo "cargo test --no-run --features dev \\" >> $FILE
|
echo "cargo test --no-run --features dev \\" >> $FILE
|
||||||
echo " -p ethash -p ethcore-util -p ethcore -p ethsync -p ethcore-rpc -p parity -p ethminer -p ethcore-dapps -p ethcore-signer" >> $FILE
|
echo " -p ethash -p ethcore-util -p ethcore -p ethsync -p ethcore-rpc -p parity -p ethcore-dapps -p ethcore-signer" >> $FILE
|
||||||
echo "" >> $FILE
|
echo "" >> $FILE
|
||||||
chmod +x $FILE
|
chmod +x $FILE
|
||||||
|
@ -392,16 +392,13 @@ fn implement_client_method_body(
|
|||||||
});
|
});
|
||||||
|
|
||||||
request_serialization_statements.push(
|
request_serialization_statements.push(
|
||||||
quote_stmt!(cx, let mut socket_ref = self.socket.borrow_mut()));
|
quote_stmt!(cx, let mut socket = self.socket.write().unwrap(); ));
|
||||||
|
|
||||||
request_serialization_statements.push(
|
|
||||||
quote_stmt!(cx, let mut socket = socket_ref.deref_mut()));
|
|
||||||
|
|
||||||
request_serialization_statements.push(
|
request_serialization_statements.push(
|
||||||
quote_stmt!(cx, let serialized_payload = ::ipc::binary::serialize(&payload).unwrap()));
|
quote_stmt!(cx, let serialized_payload = ::ipc::binary::serialize(&payload).unwrap()));
|
||||||
|
|
||||||
request_serialization_statements.push(
|
request_serialization_statements.push(
|
||||||
quote_stmt!(cx, ::ipc::invoke($index_ident, &Some(serialized_payload), &mut socket)));
|
quote_stmt!(cx, ::ipc::invoke($index_ident, &Some(serialized_payload), &mut *socket)));
|
||||||
|
|
||||||
|
|
||||||
request_serialization_statements
|
request_serialization_statements
|
||||||
@ -409,17 +406,15 @@ fn implement_client_method_body(
|
|||||||
else {
|
else {
|
||||||
let mut request_serialization_statements = Vec::new();
|
let mut request_serialization_statements = Vec::new();
|
||||||
request_serialization_statements.push(
|
request_serialization_statements.push(
|
||||||
quote_stmt!(cx, let mut socket_ref = self.socket.borrow_mut()));
|
quote_stmt!(cx, let mut socket = self.socket.write().unwrap(); ));
|
||||||
request_serialization_statements.push(
|
request_serialization_statements.push(
|
||||||
quote_stmt!(cx, let mut socket = socket_ref.deref_mut()));
|
quote_stmt!(cx, ::ipc::invoke($index_ident, &None, &mut *socket)));
|
||||||
request_serialization_statements.push(
|
|
||||||
quote_stmt!(cx, ::ipc::invoke($index_ident, &None, &mut socket)));
|
|
||||||
request_serialization_statements
|
request_serialization_statements
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(ref return_ty) = dispatch.return_type_ty {
|
if let Some(ref return_ty) = dispatch.return_type_ty {
|
||||||
let return_expr = quote_expr!(cx,
|
let return_expr = quote_expr!(cx,
|
||||||
::ipc::binary::deserialize_from::<$return_ty, _>(&mut socket).unwrap()
|
::ipc::binary::deserialize_from::<$return_ty, _>(&mut *socket).unwrap()
|
||||||
);
|
);
|
||||||
quote_expr!(cx, {
|
quote_expr!(cx, {
|
||||||
$request
|
$request
|
||||||
@ -525,7 +520,7 @@ fn push_client_struct(cx: &ExtCtxt, builder: &aster::AstBuilder, interface_map:
|
|||||||
|
|
||||||
let client_struct_item = quote_item!(cx,
|
let client_struct_item = quote_item!(cx,
|
||||||
pub struct $client_short_ident $generics {
|
pub struct $client_short_ident $generics {
|
||||||
socket: ::std::cell::RefCell<S>,
|
socket: ::std::sync::RwLock<S>,
|
||||||
phantom: $phantom,
|
phantom: $phantom,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -560,7 +555,7 @@ fn push_with_socket_client_implementation(
|
|||||||
impl $generics ::ipc::WithSocket<S> for $client_ident $where_clause {
|
impl $generics ::ipc::WithSocket<S> for $client_ident $where_clause {
|
||||||
fn init(socket: S) -> $client_ident {
|
fn init(socket: S) -> $client_ident {
|
||||||
$client_short_ident {
|
$client_short_ident {
|
||||||
socket: ::std::cell::RefCell::new(socket),
|
socket: ::std::sync::RwLock::new(socket),
|
||||||
phantom: ::std::marker::PhantomData,
|
phantom: ::std::marker::PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -594,15 +589,13 @@ fn push_client_implementation(
|
|||||||
reserved: vec![0u8; 64],
|
reserved: vec![0u8; 64],
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut socket_ref = self.socket.borrow_mut();
|
|
||||||
let mut socket = socket_ref.deref_mut();
|
|
||||||
::ipc::invoke(
|
::ipc::invoke(
|
||||||
0,
|
0,
|
||||||
&Some(::ipc::binary::serialize(&payload).unwrap()),
|
&Some(::ipc::binary::serialize(&payload).unwrap()),
|
||||||
&mut socket);
|
&mut *self.socket.write().unwrap());
|
||||||
|
|
||||||
let mut result = vec![0u8; 1];
|
let mut result = vec![0u8; 1];
|
||||||
if try!(socket.read(&mut result).map_err(|_| ::ipc::Error::HandshakeFailed)) == 1 {
|
if try!(self.socket.write().unwrap().read(&mut result).map_err(|_| ::ipc::Error::HandshakeFailed)) == 1 {
|
||||||
match result[0] {
|
match result[0] {
|
||||||
1 => Ok(()),
|
1 => Ok(()),
|
||||||
_ => Err(::ipc::Error::RemoteServiceUnsupported),
|
_ => Err(::ipc::Error::RemoteServiceUnsupported),
|
||||||
@ -613,7 +606,7 @@ fn push_client_implementation(
|
|||||||
|
|
||||||
let socket_item = quote_impl_item!(cx,
|
let socket_item = quote_impl_item!(cx,
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub fn socket(&self) -> &::std::cell::RefCell<S> {
|
pub fn socket(&self) -> &::std::sync::RwLock<S> {
|
||||||
&self.socket
|
&self.socket
|
||||||
}).unwrap();
|
}).unwrap();
|
||||||
|
|
||||||
|
@ -230,6 +230,9 @@ fn binary_expr_struct(
|
|||||||
let field_amount = builder.id(&format!("{}",fields.len()));
|
let field_amount = builder.id(&format!("{}",fields.len()));
|
||||||
map_stmts.push(quote_stmt!(cx, let mut map = vec![0usize; $field_amount];).unwrap());
|
map_stmts.push(quote_stmt!(cx, let mut map = vec![0usize; $field_amount];).unwrap());
|
||||||
map_stmts.push(quote_stmt!(cx, let mut total = 0usize;).unwrap());
|
map_stmts.push(quote_stmt!(cx, let mut total = 0usize;).unwrap());
|
||||||
|
|
||||||
|
let mut post_write_stmts = Vec::<ast::Stmt>::new();
|
||||||
|
|
||||||
for (index, field) in fields.iter().enumerate() {
|
for (index, field) in fields.iter().enumerate() {
|
||||||
let field_type_ident = builder.id(
|
let field_type_ident = builder.id(
|
||||||
&::syntax::print::pprust::ty_to_string(&codegen::strip_ptr(&field.ty)));
|
&::syntax::print::pprust::ty_to_string(&codegen::strip_ptr(&field.ty)));
|
||||||
@ -249,6 +252,7 @@ fn binary_expr_struct(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let raw_ident = ::syntax::print::pprust::ty_to_string(&codegen::strip_ptr(&field.ty));
|
let raw_ident = ::syntax::print::pprust::ty_to_string(&codegen::strip_ptr(&field.ty));
|
||||||
|
let range_ident = builder.id(format!("r{}", index));
|
||||||
match raw_ident.as_ref() {
|
match raw_ident.as_ref() {
|
||||||
"u8" => {
|
"u8" => {
|
||||||
write_stmts.push(quote_stmt!(cx, let next_line = offset + 1;).unwrap());
|
write_stmts.push(quote_stmt!(cx, let next_line = offset + 1;).unwrap());
|
||||||
@ -258,15 +262,17 @@ fn binary_expr_struct(
|
|||||||
write_stmts.push(quote_stmt!(cx, let size = $member_expr .len();).unwrap());
|
write_stmts.push(quote_stmt!(cx, let size = $member_expr .len();).unwrap());
|
||||||
write_stmts.push(quote_stmt!(cx, let next_line = offset + size;).unwrap());
|
write_stmts.push(quote_stmt!(cx, let next_line = offset + size;).unwrap());
|
||||||
write_stmts.push(quote_stmt!(cx, length_stack.push_back(size);).unwrap());
|
write_stmts.push(quote_stmt!(cx, length_stack.push_back(size);).unwrap());
|
||||||
write_stmts.push(quote_stmt!(cx, buffer[offset..next_line].clone_from_slice($member_expr); ).unwrap());
|
write_stmts.push(quote_stmt!(cx, let $range_ident = offset..next_line; ).unwrap());
|
||||||
|
post_write_stmts.push(quote_stmt!(cx, buffer[$range_ident].clone_from_slice($member_expr); ).unwrap());
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
write_stmts.push(quote_stmt!(cx, let next_line = offset + match $field_type_ident_qualified::len_params() {
|
write_stmts.push(quote_stmt!(cx, let next_line = offset + match $field_type_ident_qualified::len_params() {
|
||||||
0 => mem::size_of::<$field_type_ident>(),
|
0 => mem::size_of::<$field_type_ident>(),
|
||||||
_ => { let size = $member_expr .size(); length_stack.push_back(size); size },
|
_ => { let size = $member_expr .size(); length_stack.push_back(size); size },
|
||||||
}).unwrap());
|
}).unwrap());
|
||||||
write_stmts.push(quote_stmt!(cx,
|
write_stmts.push(quote_stmt!(cx, let $range_ident = offset..next_line; ).unwrap());
|
||||||
if let Err(e) = $member_expr .to_bytes(&mut buffer[offset..next_line], length_stack) { return Err(e) };).unwrap());
|
post_write_stmts.push(quote_stmt!(cx,
|
||||||
|
if let Err(e) = $member_expr .to_bytes(&mut buffer[$range_ident], length_stack) { return Err(e) };).unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -312,7 +318,7 @@ fn binary_expr_struct(
|
|||||||
|
|
||||||
Ok(BinaryExpressions {
|
Ok(BinaryExpressions {
|
||||||
size: total_size_expr,
|
size: total_size_expr,
|
||||||
write: quote_expr!(cx, { $write_stmts; Ok(()) } ),
|
write: quote_expr!(cx, { $write_stmts; $post_write_stmts; Ok(()) } ),
|
||||||
read: read_expr,
|
read: read_expr,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,7 @@ use nanomsg::{Socket, Protocol, Error, Endpoint, PollRequest, PollFd, PollInOut}
|
|||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
const POLL_TIMEOUT: isize = 100;
|
const POLL_TIMEOUT: isize = 100;
|
||||||
|
const CLIENT_CONNECTION_TIMEOUT: isize = 2500;
|
||||||
|
|
||||||
/// Generic worker to handle service (binded) sockets
|
/// Generic worker to handle service (binded) sockets
|
||||||
pub struct Worker<S> where S: IpcInterface<S> {
|
pub struct Worker<S> where S: IpcInterface<S> {
|
||||||
@ -46,6 +47,12 @@ pub struct GuardedSocket<S> where S: WithSocket<Socket> {
|
|||||||
_endpoint: Endpoint,
|
_endpoint: Endpoint,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<S> GuardedSocket<S> where S: WithSocket<Socket> {
|
||||||
|
pub fn service(&self) -> Arc<S> {
|
||||||
|
self.client.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<S> Deref for GuardedSocket<S> where S: WithSocket<Socket> {
|
impl<S> Deref for GuardedSocket<S> where S: WithSocket<Socket> {
|
||||||
type Target = S;
|
type Target = S;
|
||||||
|
|
||||||
@ -63,6 +70,9 @@ pub fn init_duplex_client<S>(socket_addr: &str) -> Result<GuardedSocket<S>, Sock
|
|||||||
SocketError::DuplexLink
|
SocketError::DuplexLink
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
// 2500 ms default timeout
|
||||||
|
socket.set_receive_timeout(CLIENT_CONNECTION_TIMEOUT).unwrap();
|
||||||
|
|
||||||
let endpoint = try!(socket.connect(socket_addr).map_err(|e| {
|
let endpoint = try!(socket.connect(socket_addr).map_err(|e| {
|
||||||
warn!(target: "ipc", "Failed to bind socket to address '{}': {:?}", socket_addr, e);
|
warn!(target: "ipc", "Failed to bind socket to address '{}': {:?}", socket_addr, e);
|
||||||
SocketError::DuplexLink
|
SocketError::DuplexLink
|
||||||
@ -83,6 +93,9 @@ pub fn init_client<S>(socket_addr: &str) -> Result<GuardedSocket<S>, SocketError
|
|||||||
SocketError::RequestLink
|
SocketError::RequestLink
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
// 2500 ms default timeout
|
||||||
|
socket.set_receive_timeout(CLIENT_CONNECTION_TIMEOUT).unwrap();
|
||||||
|
|
||||||
let endpoint = try!(socket.connect(socket_addr).map_err(|e| {
|
let endpoint = try!(socket.connect(socket_addr).map_err(|e| {
|
||||||
warn!(target: "ipc", "Failed to bind socket to address '{}': {:?}", socket_addr, e);
|
warn!(target: "ipc", "Failed to bind socket to address '{}': {:?}", socket_addr, e);
|
||||||
SocketError::RequestLink
|
SocketError::RequestLink
|
||||||
|
@ -104,14 +104,31 @@ impl<R: BinaryConvertable, E: BinaryConvertable> BinaryConvertable for Result<R,
|
|||||||
|
|
||||||
fn to_bytes(&self, buffer: &mut [u8], length_stack: &mut VecDeque<usize>) -> Result<(), BinaryConvertError> {
|
fn to_bytes(&self, buffer: &mut [u8], length_stack: &mut VecDeque<usize>) -> Result<(), BinaryConvertError> {
|
||||||
match *self {
|
match *self {
|
||||||
Ok(ref r) => { buffer[0] = 0; Ok(try!(r.to_bytes(&mut buffer[1..], length_stack))) },
|
Ok(ref r) => {
|
||||||
Err(ref e) => { buffer[1] = 1; Ok(try!(e.to_bytes(&mut buffer[1..], length_stack))) },
|
buffer[0] = 0;
|
||||||
|
if r.size() > 0 {
|
||||||
|
Ok(try!(r.to_bytes(&mut buffer[1..], length_stack)))
|
||||||
|
}
|
||||||
|
else { Ok(()) }
|
||||||
|
},
|
||||||
|
Err(ref e) => {
|
||||||
|
buffer[0] = 1;
|
||||||
|
if e.size() > 0 {
|
||||||
|
Ok(try!(e.to_bytes(&mut buffer[1..], length_stack)))
|
||||||
|
}
|
||||||
|
else { Ok(()) }
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bytes(buffer: &[u8], length_stack: &mut VecDeque<usize>) -> Result<Self, BinaryConvertError> {
|
fn from_bytes(buffer: &[u8], length_stack: &mut VecDeque<usize>) -> Result<Self, BinaryConvertError> {
|
||||||
match buffer[0] {
|
match buffer[0] {
|
||||||
0 => Ok(Ok(try!(R::from_bytes(&buffer[1..], length_stack)))),
|
0 => {
|
||||||
|
match buffer.len() {
|
||||||
|
1 => Ok(Ok(try!(R::from_empty_bytes()))),
|
||||||
|
_ => Ok(Ok(try!(R::from_bytes(&buffer[1..], length_stack)))),
|
||||||
|
}
|
||||||
|
}
|
||||||
1 => Ok(Err(try!(E::from_bytes(&buffer[1..], length_stack)))),
|
1 => Ok(Err(try!(E::from_bytes(&buffer[1..], length_stack)))),
|
||||||
_ => Err(BinaryConvertError)
|
_ => Err(BinaryConvertError)
|
||||||
}
|
}
|
||||||
@ -154,6 +171,8 @@ impl<T> BinaryConvertable for Vec<T> where T: BinaryConvertable {
|
|||||||
_ => 128,
|
_ => 128,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if buffer.len() == 0 { return Ok(result); }
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let next_size = match T::len_params() {
|
let next_size = match T::len_params() {
|
||||||
0 => mem::size_of::<T>(),
|
0 => mem::size_of::<T>(),
|
||||||
@ -300,8 +319,8 @@ pub fn deserialize_from<T, R>(r: &mut R) -> Result<T, BinaryConvertError>
|
|||||||
let mut payload = Vec::new();
|
let mut payload = Vec::new();
|
||||||
try!(r.read_to_end(&mut payload).map_err(|_| BinaryConvertError));
|
try!(r.read_to_end(&mut payload).map_err(|_| BinaryConvertError));
|
||||||
|
|
||||||
let mut length_stack = VecDeque::<usize>::new();
|
|
||||||
let stack_len = try!(u64::from_bytes(&payload[0..8], &mut fake_stack)) as usize;
|
let stack_len = try!(u64::from_bytes(&payload[0..8], &mut fake_stack)) as usize;
|
||||||
|
let mut length_stack = VecDeque::<usize>::with_capacity(stack_len);
|
||||||
|
|
||||||
if stack_len > 0 {
|
if stack_len > 0 {
|
||||||
for idx in 0..stack_len {
|
for idx in 0..stack_len {
|
||||||
@ -607,7 +626,7 @@ fn deserialize_simple_err() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn deserialize_opt_vec_in_out() {
|
fn serialize_opt_vec_in_out() {
|
||||||
use std::io::{Cursor, SeekFrom, Seek};
|
use std::io::{Cursor, SeekFrom, Seek};
|
||||||
|
|
||||||
let mut buff = Cursor::new(Vec::new());
|
let mut buff = Cursor::new(Vec::new());
|
||||||
@ -619,3 +638,17 @@ fn deserialize_opt_vec_in_out() {
|
|||||||
|
|
||||||
assert!(vec.is_none());
|
assert!(vec.is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serialize_err_opt_vec_in_out() {
|
||||||
|
use std::io::{Cursor, SeekFrom, Seek};
|
||||||
|
|
||||||
|
let mut buff = Cursor::new(Vec::new());
|
||||||
|
let optional_vec: Result<Option<Vec<u8>>, u32> = Ok(None);
|
||||||
|
serialize_into(&optional_vec, &mut buff).unwrap();
|
||||||
|
|
||||||
|
buff.seek(SeekFrom::Start(0)).unwrap();
|
||||||
|
let vec = deserialize_from::<Result<Option<Vec<u8>>, u32>, _>(&mut buff).unwrap();
|
||||||
|
|
||||||
|
assert!(vec.is_ok());
|
||||||
|
}
|
||||||
|
@ -96,6 +96,8 @@ API and Console Options:
|
|||||||
asked for password on startup.
|
asked for password on startup.
|
||||||
--dapps-pass PASSWORD Specify password for Dapps server. Use only in
|
--dapps-pass PASSWORD Specify password for Dapps server. Use only in
|
||||||
conjunction with --dapps-user.
|
conjunction with --dapps-user.
|
||||||
|
--dapps-path PATH Specify directory where dapps should be installed.
|
||||||
|
[default: $HOME/.parity/dapps]
|
||||||
|
|
||||||
--signer Enable Trusted Signer WebSocket endpoint used by
|
--signer Enable Trusted Signer WebSocket endpoint used by
|
||||||
System UIs.
|
System UIs.
|
||||||
@ -239,6 +241,7 @@ pub struct Args {
|
|||||||
pub flag_dapps_interface: String,
|
pub flag_dapps_interface: String,
|
||||||
pub flag_dapps_user: Option<String>,
|
pub flag_dapps_user: Option<String>,
|
||||||
pub flag_dapps_pass: Option<String>,
|
pub flag_dapps_pass: Option<String>,
|
||||||
|
pub flag_dapps_path: String,
|
||||||
pub flag_signer: bool,
|
pub flag_signer: bool,
|
||||||
pub flag_signer_port: u16,
|
pub flag_signer_port: u16,
|
||||||
pub flag_force_sealing: bool,
|
pub flag_force_sealing: bool,
|
||||||
|
@ -40,6 +40,7 @@ pub struct Configuration {
|
|||||||
pub struct Directories {
|
pub struct Directories {
|
||||||
pub keys: String,
|
pub keys: String,
|
||||||
pub db: String,
|
pub db: String,
|
||||||
|
pub dapps: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Configuration {
|
impl Configuration {
|
||||||
@ -325,11 +326,14 @@ impl Configuration {
|
|||||||
&self.args.flag_keys_path
|
&self.args.flag_keys_path
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
::std::fs::create_dir_all(&db_path).unwrap_or_else(|e| die_with_io_error("main", e));
|
::std::fs::create_dir_all(&keys_path).unwrap_or_else(|e| die_with_io_error("main", e));
|
||||||
|
let dapps_path = Configuration::replace_home(&self.args.flag_dapps_path);
|
||||||
|
::std::fs::create_dir_all(&dapps_path).unwrap_or_else(|e| die_with_io_error("main", e));
|
||||||
|
|
||||||
Directories {
|
Directories {
|
||||||
keys: keys_path,
|
keys: keys_path,
|
||||||
db: db_path,
|
db: db_path,
|
||||||
|
dapps: dapps_path,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,6 +32,7 @@ pub struct Configuration {
|
|||||||
pub port: u16,
|
pub port: u16,
|
||||||
pub user: Option<String>,
|
pub user: Option<String>,
|
||||||
pub pass: Option<String>,
|
pub pass: Option<String>,
|
||||||
|
pub dapps_path: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Dependencies {
|
pub struct Dependencies {
|
||||||
@ -63,12 +64,13 @@ pub fn new(configuration: Configuration, deps: Dependencies) -> Option<WebappSer
|
|||||||
(username.to_owned(), password)
|
(username.to_owned(), password)
|
||||||
});
|
});
|
||||||
|
|
||||||
Some(setup_dapps_server(deps, &addr, auth))
|
Some(setup_dapps_server(deps, configuration.dapps_path, &addr, auth))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "dapps"))]
|
#[cfg(not(feature = "dapps"))]
|
||||||
pub fn setup_dapps_server(
|
pub fn setup_dapps_server(
|
||||||
_deps: Dependencies,
|
_deps: Dependencies,
|
||||||
|
_dapps_path: String,
|
||||||
_url: &SocketAddr,
|
_url: &SocketAddr,
|
||||||
_auth: Option<(String, String)>,
|
_auth: Option<(String, String)>,
|
||||||
) -> ! {
|
) -> ! {
|
||||||
@ -78,12 +80,13 @@ pub fn setup_dapps_server(
|
|||||||
#[cfg(feature = "dapps")]
|
#[cfg(feature = "dapps")]
|
||||||
pub fn setup_dapps_server(
|
pub fn setup_dapps_server(
|
||||||
deps: Dependencies,
|
deps: Dependencies,
|
||||||
|
dapps_path: String,
|
||||||
url: &SocketAddr,
|
url: &SocketAddr,
|
||||||
auth: Option<(String, String)>
|
auth: Option<(String, String)>
|
||||||
) -> WebappServer {
|
) -> WebappServer {
|
||||||
use ethcore_dapps as dapps;
|
use ethcore_dapps as dapps;
|
||||||
|
|
||||||
let server = dapps::ServerBuilder::new();
|
let server = dapps::ServerBuilder::new(dapps_path);
|
||||||
let server = rpc_apis::setup_rpc(server, deps.apis.clone(), rpc_apis::ApiSet::UnsafeContext);
|
let server = rpc_apis::setup_rpc(server, deps.apis.clone(), rpc_apis::ApiSet::UnsafeContext);
|
||||||
let start_result = match auth {
|
let start_result = match auth {
|
||||||
None => {
|
None => {
|
||||||
|
@ -231,6 +231,7 @@ fn execute_client(conf: Configuration, spec: Spec, client_config: ClientConfig)
|
|||||||
port: conf.args.flag_dapps_port,
|
port: conf.args.flag_dapps_port,
|
||||||
user: conf.args.flag_dapps_user.clone(),
|
user: conf.args.flag_dapps_user.clone(),
|
||||||
pass: conf.args.flag_dapps_pass.clone(),
|
pass: conf.args.flag_dapps_pass.clone(),
|
||||||
|
dapps_path: conf.directories().dapps,
|
||||||
}, dapps::Dependencies {
|
}, dapps::Dependencies {
|
||||||
panic_handler: panic_handler.clone(),
|
panic_handler: panic_handler.clone(),
|
||||||
apis: deps_for_rpc_apis.clone(),
|
apis: deps_for_rpc_apis.clone(),
|
||||||
|
@ -51,7 +51,7 @@ fn do_start(conf: Configuration, deps: Dependencies) -> SignerServer {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let start_result = {
|
let start_result = {
|
||||||
let server = signer::ServerBuilder::new();
|
let server = signer::ServerBuilder::new(deps.apis.signer_queue.clone());
|
||||||
let server = rpc_apis::setup_rpc(server, deps.apis, rpc_apis::ApiSet::SafeContext);
|
let server = rpc_apis::setup_rpc(server, deps.apis, rpc_apis::ApiSet::SafeContext);
|
||||||
server.start(addr)
|
server.start(addr)
|
||||||
};
|
};
|
||||||
|
@ -14,78 +14,281 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use std::sync::Mutex;
|
use std::thread;
|
||||||
|
use std::time::{Instant, Duration};
|
||||||
|
use std::sync::{mpsc, Mutex, RwLock, Arc};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use v1::types::{TransactionRequest, TransactionConfirmation};
|
use v1::types::{TransactionRequest, TransactionConfirmation};
|
||||||
use util::U256;
|
use util::{U256, H256};
|
||||||
|
|
||||||
|
|
||||||
|
/// Possible events happening in the queue that can be listened to.
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub enum QueueEvent {
|
||||||
|
/// Receiver should stop work upon receiving `Finish` message.
|
||||||
|
Finish,
|
||||||
|
/// Informs about new request.
|
||||||
|
NewRequest(U256),
|
||||||
|
/// Request rejected.
|
||||||
|
RequestRejected(U256),
|
||||||
|
/// Request resolved.
|
||||||
|
RequestConfirmed(U256),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Defines possible errors returned from queue receiving method.
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub enum QueueError {
|
||||||
|
/// Returned when method has been already used (no receiver available).
|
||||||
|
AlreadyUsed,
|
||||||
|
/// Returned when receiver encounters an error.
|
||||||
|
ReceiverError(mpsc::RecvError),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Message Receiver type
|
||||||
|
pub type QueueEventReceiver = mpsc::Receiver<QueueEvent>;
|
||||||
|
|
||||||
/// A queue of transactions awaiting to be confirmed and signed.
|
/// A queue of transactions awaiting to be confirmed and signed.
|
||||||
pub trait SigningQueue: Send + Sync {
|
pub trait SigningQueue: Send + Sync {
|
||||||
/// Add new request to the queue.
|
/// Add new request to the queue.
|
||||||
fn add_request(&self, transaction: TransactionRequest) -> U256;
|
/// Returns a `ConfirmationPromise` that can be used to await for resolution of given request.
|
||||||
|
fn add_request(&self, transaction: TransactionRequest) -> ConfirmationPromise;
|
||||||
|
|
||||||
/// Remove request from the queue.
|
/// Removes a request from the queue.
|
||||||
fn remove_request(&self, id: U256) -> Option<TransactionConfirmation>;
|
/// Notifies possible token holders that transaction was rejected.
|
||||||
|
fn request_rejected(&self, id: U256) -> Option<TransactionConfirmation>;
|
||||||
|
|
||||||
|
/// Removes a request from the queue.
|
||||||
|
/// Notifies possible token holders that transaction was confirmed and given hash was assigned.
|
||||||
|
fn request_confirmed(&self, id: U256, hash: H256) -> Option<TransactionConfirmation>;
|
||||||
|
|
||||||
|
/// Returns a request if it is contained in the queue.
|
||||||
|
fn peek(&self, id: &U256) -> Option<TransactionConfirmation>;
|
||||||
|
|
||||||
/// Return copy of all the requests in the queue.
|
/// Return copy of all the requests in the queue.
|
||||||
fn requests(&self) -> Vec<TransactionConfirmation>;
|
fn requests(&self) -> Vec<TransactionConfirmation>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
enum ConfirmationResult {
|
||||||
|
/// The transaction has not yet been confirmed nor rejected.
|
||||||
|
Waiting,
|
||||||
|
/// The transaction has been rejected.
|
||||||
|
Rejected,
|
||||||
|
/// The transaction has been confirmed.
|
||||||
|
Confirmed(H256),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Time you need to confirm the transaction in UI.
|
||||||
|
/// This is the amount of time token holder will wait before
|
||||||
|
/// returning `None`.
|
||||||
|
/// Unless we have a multi-threaded RPC this will lock
|
||||||
|
/// any other incoming call!
|
||||||
|
const QUEUE_TIMEOUT_DURATION_SEC : u64 = 20;
|
||||||
|
|
||||||
|
/// A handle to submitted request.
|
||||||
|
/// Allows to block and wait for a resolution of that request.
|
||||||
|
pub struct ConfirmationToken {
|
||||||
|
result: Arc<Mutex<ConfirmationResult>>,
|
||||||
|
handle: thread::Thread,
|
||||||
|
request: TransactionConfirmation,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ConfirmationPromise {
|
||||||
|
id: U256,
|
||||||
|
result: Arc<Mutex<ConfirmationResult>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ConfirmationToken {
|
||||||
|
/// Submit solution to all listeners
|
||||||
|
fn resolve(&self, result: Option<H256>) {
|
||||||
|
let mut res = self.result.lock().unwrap();
|
||||||
|
*res = result.map_or(ConfirmationResult::Rejected, |h| ConfirmationResult::Confirmed(h));
|
||||||
|
// Notify listener
|
||||||
|
self.handle.unpark();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_promise(&self) -> ConfirmationPromise {
|
||||||
|
ConfirmationPromise {
|
||||||
|
id: self.request.id,
|
||||||
|
result: self.result.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ConfirmationPromise {
|
||||||
|
/// Blocks current thread and awaits for
|
||||||
|
/// resolution of the transaction (rejected / confirmed)
|
||||||
|
/// Returns `None` if transaction was rejected or timeout reached.
|
||||||
|
/// Returns `Some(hash)` if transaction was confirmed.
|
||||||
|
pub fn wait_with_timeout(&self) -> Option<H256> {
|
||||||
|
let timeout = Duration::from_secs(QUEUE_TIMEOUT_DURATION_SEC);
|
||||||
|
let deadline = Instant::now() + timeout;
|
||||||
|
|
||||||
|
info!(target: "own_tx", "Signer: Awaiting transaction confirmation... ({:?}).", self.id);
|
||||||
|
loop {
|
||||||
|
let now = Instant::now();
|
||||||
|
if now >= deadline {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// Park thread (may wake up spuriously)
|
||||||
|
thread::park_timeout(deadline - now);
|
||||||
|
// Take confirmation result
|
||||||
|
let res = self.result.lock().unwrap();
|
||||||
|
// Check the result
|
||||||
|
match *res {
|
||||||
|
ConfirmationResult::Rejected => return None,
|
||||||
|
ConfirmationResult::Confirmed(h) => return Some(h),
|
||||||
|
ConfirmationResult::Waiting => continue,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// We reached the timeout. Just return `None`
|
||||||
|
trace!(target: "own_tx", "Signer: Confirmation timeout reached... ({:?}).", self.id);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Queue for all unconfirmed transactions.
|
/// Queue for all unconfirmed transactions.
|
||||||
pub struct ConfirmationsQueue {
|
pub struct ConfirmationsQueue {
|
||||||
id: Mutex<U256>,
|
id: Mutex<U256>,
|
||||||
queue: Mutex<HashMap<U256, TransactionConfirmation>>,
|
queue: RwLock<HashMap<U256, ConfirmationToken>>,
|
||||||
|
sender: Mutex<mpsc::Sender<QueueEvent>>,
|
||||||
|
receiver: Mutex<Option<mpsc::Receiver<QueueEvent>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ConfirmationsQueue {
|
impl Default for ConfirmationsQueue {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
|
let (send, recv) = mpsc::channel();
|
||||||
|
|
||||||
ConfirmationsQueue {
|
ConfirmationsQueue {
|
||||||
id: Mutex::new(U256::from(0)),
|
id: Mutex::new(U256::from(0)),
|
||||||
queue: Mutex::new(HashMap::new()),
|
queue: RwLock::new(HashMap::new()),
|
||||||
|
sender: Mutex::new(send),
|
||||||
|
receiver: Mutex::new(Some(recv)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ConfirmationsQueue {
|
||||||
|
/// Blocks the thread and starts listening for notifications regarding all actions in the queue.
|
||||||
|
/// For each event, `listener` callback will be invoked.
|
||||||
|
/// This method can be used only once (only single consumer of events can exist).
|
||||||
|
pub fn start_listening<F>(&self, listener: F) -> Result<(), QueueError>
|
||||||
|
where F: Fn(QueueEvent) -> () {
|
||||||
|
let recv = self.receiver.lock().unwrap().take();
|
||||||
|
if let None = recv {
|
||||||
|
return Err(QueueError::AlreadyUsed);
|
||||||
|
}
|
||||||
|
let recv = recv.expect("Check for none is done earlier.");
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let message = try!(recv.recv().map_err(|e| QueueError::ReceiverError(e)));
|
||||||
|
if let QueueEvent::Finish = message {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
listener(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Notifies consumer that the communcation is over.
|
||||||
|
/// No more events will be sent after this function is invoked.
|
||||||
|
pub fn finish(&self) {
|
||||||
|
self.notify(QueueEvent::Finish);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Notifies receiver about the event happening in this queue.
|
||||||
|
fn notify(&self, message: QueueEvent) {
|
||||||
|
// We don't really care about the result
|
||||||
|
let _ = self.sender.lock().unwrap().send(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Removes transaction from this queue and notifies `ConfirmationPromise` holders about the result.
|
||||||
|
/// Notifies also a receiver about that event.
|
||||||
|
fn remove(&self, id: U256, result: Option<H256>) -> Option<TransactionConfirmation> {
|
||||||
|
let token = self.queue.write().unwrap().remove(&id);
|
||||||
|
|
||||||
|
if let Some(token) = token {
|
||||||
|
// notify receiver about the event
|
||||||
|
self.notify(result.map_or_else(
|
||||||
|
|| QueueEvent::RequestRejected(id),
|
||||||
|
|_| QueueEvent::RequestConfirmed(id)
|
||||||
|
));
|
||||||
|
// notify token holders about resolution
|
||||||
|
token.resolve(result);
|
||||||
|
// return a result
|
||||||
|
return Some(token.request.clone());
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for ConfirmationsQueue {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.finish();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl SigningQueue for ConfirmationsQueue {
|
impl SigningQueue for ConfirmationsQueue {
|
||||||
fn add_request(&self, transaction: TransactionRequest) -> U256 {
|
fn add_request(&self, transaction: TransactionRequest) -> ConfirmationPromise {
|
||||||
// Increment id
|
// Increment id
|
||||||
let id = {
|
let id = {
|
||||||
let mut last_id = self.id.lock().unwrap();
|
let mut last_id = self.id.lock().unwrap();
|
||||||
*last_id = *last_id + U256::from(1);
|
*last_id = *last_id + U256::from(1);
|
||||||
*last_id
|
*last_id
|
||||||
};
|
};
|
||||||
let mut queue = self.queue.lock().unwrap();
|
// Add request to queue
|
||||||
queue.insert(id, TransactionConfirmation {
|
let res = {
|
||||||
|
let mut queue = self.queue.write().unwrap();
|
||||||
|
queue.insert(id, ConfirmationToken {
|
||||||
|
result: Arc::new(Mutex::new(ConfirmationResult::Waiting)),
|
||||||
|
handle: thread::current(),
|
||||||
|
request: TransactionConfirmation {
|
||||||
id: id,
|
id: id,
|
||||||
transaction: transaction,
|
transaction: transaction,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
id
|
debug!(target: "own_tx", "Signer: New transaction ({:?}) in confirmation queue.", id);
|
||||||
|
queue.get(&id).map(|token| token.as_promise()).expect("Token was just inserted.")
|
||||||
|
};
|
||||||
|
// Notify listeners
|
||||||
|
self.notify(QueueEvent::NewRequest(id));
|
||||||
|
res
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_request(&self, id: U256) -> Option<TransactionConfirmation> {
|
fn peek(&self, id: &U256) -> Option<TransactionConfirmation> {
|
||||||
self.queue.lock().unwrap().remove(&id)
|
self.queue.read().unwrap().get(id).map(|token| token.request.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn request_rejected(&self, id: U256) -> Option<TransactionConfirmation> {
|
||||||
|
debug!(target: "own_tx", "Signer: Transaction rejected ({:?}).", id);
|
||||||
|
self.remove(id, None)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn request_confirmed(&self, id: U256, hash: H256) -> Option<TransactionConfirmation> {
|
||||||
|
debug!(target: "own_tx", "Signer: Transaction confirmed ({:?}).", id);
|
||||||
|
self.remove(id, Some(hash))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn requests(&self) -> Vec<TransactionConfirmation> {
|
fn requests(&self) -> Vec<TransactionConfirmation> {
|
||||||
let queue = self.queue.lock().unwrap();
|
let queue = self.queue.read().unwrap();
|
||||||
queue.values().cloned().collect()
|
queue.values().map(|token| token.request.clone()).collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
use std::time::Duration;
|
||||||
|
use std::thread;
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
use util::hash::Address;
|
use util::hash::Address;
|
||||||
use util::numbers::U256;
|
use util::numbers::{U256, H256};
|
||||||
use v1::types::TransactionRequest;
|
use v1::types::TransactionRequest;
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
fn request() -> TransactionRequest {
|
||||||
fn should_work_for_hashset() {
|
TransactionRequest {
|
||||||
// given
|
|
||||||
let queue = ConfirmationsQueue::default();
|
|
||||||
|
|
||||||
let request = TransactionRequest {
|
|
||||||
from: Address::from(1),
|
from: Address::from(1),
|
||||||
to: Some(Address::from(2)),
|
to: Some(Address::from(2)),
|
||||||
gas_price: None,
|
gas_price: None,
|
||||||
@ -93,7 +296,63 @@ mod test {
|
|||||||
value: Some(U256::from(10_000_000)),
|
value: Some(U256::from(10_000_000)),
|
||||||
data: None,
|
data: None,
|
||||||
nonce: None,
|
nonce: None,
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_wait_for_hash() {
|
||||||
|
// given
|
||||||
|
let queue = Arc::new(ConfirmationsQueue::default());
|
||||||
|
let request = request();
|
||||||
|
|
||||||
|
// when
|
||||||
|
let q = queue.clone();
|
||||||
|
let handle = thread::spawn(move || {
|
||||||
|
let v = q.add_request(request);
|
||||||
|
v.wait_with_timeout().expect("Should return hash")
|
||||||
|
});
|
||||||
|
|
||||||
|
let id = U256::from(1);
|
||||||
|
while queue.peek(&id).is_none() {
|
||||||
|
// Just wait for the other thread to start
|
||||||
|
thread::sleep(Duration::from_millis(100));
|
||||||
|
}
|
||||||
|
queue.request_confirmed(id, H256::from(1));
|
||||||
|
|
||||||
|
// then
|
||||||
|
assert_eq!(handle.join().expect("Thread should finish nicely"), H256::from(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_receive_notification() {
|
||||||
|
// given
|
||||||
|
let received = Arc::new(Mutex::new(None));
|
||||||
|
let queue = Arc::new(ConfirmationsQueue::default());
|
||||||
|
let request = request();
|
||||||
|
|
||||||
|
// when
|
||||||
|
let q = queue.clone();
|
||||||
|
let r = received.clone();
|
||||||
|
let handle = thread::spawn(move || {
|
||||||
|
q.start_listening(move |notification| {
|
||||||
|
let mut v = r.lock().unwrap();
|
||||||
|
*v = Some(notification);
|
||||||
|
}).expect("Should be closed nicely.")
|
||||||
|
});
|
||||||
|
queue.add_request(request);
|
||||||
|
queue.finish();
|
||||||
|
|
||||||
|
// then
|
||||||
|
handle.join().expect("Thread should finish nicely");
|
||||||
|
let r = received.lock().unwrap().take();
|
||||||
|
assert_eq!(r, Some(QueueEvent::NewRequest(U256::from(1))));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn should_add_transactions() {
|
||||||
|
// given
|
||||||
|
let queue = ConfirmationsQueue::default();
|
||||||
|
let request = request();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
queue.add_request(request.clone());
|
queue.add_request(request.clone());
|
||||||
|
@ -499,7 +499,7 @@ impl<C, S, A, M, EM> Eth for EthClient<C, S, A, M, EM> where
|
|||||||
.and_then(|(raw_transaction, )| {
|
.and_then(|(raw_transaction, )| {
|
||||||
let raw_transaction = raw_transaction.to_vec();
|
let raw_transaction = raw_transaction.to_vec();
|
||||||
match UntrustedRlp::new(&raw_transaction).as_val() {
|
match UntrustedRlp::new(&raw_transaction).as_val() {
|
||||||
Ok(signed_transaction) => dispatch_transaction(&*take_weak!(self.client), &*take_weak!(self.miner), signed_transaction),
|
Ok(signed_transaction) => to_value(&dispatch_transaction(&*take_weak!(self.client), &*take_weak!(self.miner), signed_transaction)),
|
||||||
Err(_) => to_value(&H256::zero()),
|
Err(_) => to_value(&H256::zero()),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -169,8 +169,9 @@ impl<C, M> EthFilter for EthFilterClient<C, M> where
|
|||||||
logs.extend(new_pending_logs);
|
logs.extend(new_pending_logs);
|
||||||
}
|
}
|
||||||
|
|
||||||
// save current block number as next from block number
|
// save the number of the next block as a first block from which
|
||||||
*block_number = current_number;
|
// we want to get logs
|
||||||
|
*block_number = current_number + 1;
|
||||||
|
|
||||||
to_value(&logs)
|
to_value(&logs)
|
||||||
}
|
}
|
||||||
|
@ -53,9 +53,9 @@ impl EthSigning for EthSigningQueueClient {
|
|||||||
from_params::<(TransactionRequest, )>(params)
|
from_params::<(TransactionRequest, )>(params)
|
||||||
.and_then(|(request, )| {
|
.and_then(|(request, )| {
|
||||||
let queue = take_weak!(self.queue);
|
let queue = take_weak!(self.queue);
|
||||||
queue.add_request(request);
|
let id = queue.add_request(request);
|
||||||
// TODO [ToDr] Block and wait for confirmation?
|
let result = id.wait_with_timeout();
|
||||||
to_value(&H256::zero())
|
to_value(&result.unwrap_or_else(H256::new))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -102,7 +102,7 @@ impl<C, A, M> EthSigning for EthSigningUnsafeClient<C, A, M> where
|
|||||||
.and_then(|(request, )| {
|
.and_then(|(request, )| {
|
||||||
let accounts = take_weak!(self.accounts);
|
let accounts = take_weak!(self.accounts);
|
||||||
match accounts.account_secret(&request.from) {
|
match accounts.account_secret(&request.from) {
|
||||||
Ok(secret) => sign_and_dispatch(&self.client, &self.miner, request, secret),
|
Ok(secret) => to_value(&sign_and_dispatch(&*take_weak!(self.client), &*take_weak!(self.miner), request, secret)),
|
||||||
Err(_) => to_value(&H256::zero())
|
Err(_) => to_value(&H256::zero())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -52,16 +52,14 @@ pub use self::traces::TracesClient;
|
|||||||
pub use self::rpc::RpcClient;
|
pub use self::rpc::RpcClient;
|
||||||
|
|
||||||
use v1::types::TransactionRequest;
|
use v1::types::TransactionRequest;
|
||||||
use std::sync::Weak;
|
|
||||||
use ethcore::miner::{AccountDetails, MinerService};
|
use ethcore::miner::{AccountDetails, MinerService};
|
||||||
use ethcore::client::MiningBlockChainClient;
|
use ethcore::client::MiningBlockChainClient;
|
||||||
use ethcore::transaction::{Action, SignedTransaction, Transaction};
|
use ethcore::transaction::{Action, SignedTransaction, Transaction};
|
||||||
use util::numbers::*;
|
use util::numbers::*;
|
||||||
use util::rlp::encode;
|
use util::rlp::encode;
|
||||||
use util::bytes::ToPretty;
|
use util::bytes::ToPretty;
|
||||||
use jsonrpc_core::{Error, to_value, Value};
|
|
||||||
|
|
||||||
fn dispatch_transaction<C, M>(client: &C, miner: &M, signed_transaction: SignedTransaction) -> Result<Value, Error>
|
fn dispatch_transaction<C, M>(client: &C, miner: &M, signed_transaction: SignedTransaction) -> H256
|
||||||
where C: MiningBlockChainClient, M: MinerService {
|
where C: MiningBlockChainClient, M: MinerService {
|
||||||
let hash = signed_transaction.hash();
|
let hash = signed_transaction.hash();
|
||||||
|
|
||||||
@ -72,13 +70,11 @@ fn dispatch_transaction<C, M>(client: &C, miner: &M, signed_transaction: SignedT
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
to_value(&import.map(|_| hash).unwrap_or(H256::zero()))
|
import.map(|_| hash).unwrap_or(H256::zero())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sign_and_dispatch<C, M>(client: &Weak<C>, miner: &Weak<M>, request: TransactionRequest, secret: H256) -> Result<Value, Error>
|
fn sign_and_dispatch<C, M>(client: &C, miner: &M, request: TransactionRequest, secret: H256) -> H256
|
||||||
where C: MiningBlockChainClient, M: MinerService {
|
where C: MiningBlockChainClient, M: MinerService {
|
||||||
let client = take_weak!(client);
|
|
||||||
let miner = take_weak!(miner);
|
|
||||||
|
|
||||||
let signed_transaction = {
|
let signed_transaction = {
|
||||||
Transaction {
|
Transaction {
|
||||||
|
@ -83,7 +83,7 @@ impl<A: 'static, C: 'static, M: 'static> Personal for PersonalClient<A, C, M>
|
|||||||
.and_then(|(request, password)| {
|
.and_then(|(request, password)| {
|
||||||
let accounts = take_weak!(self.accounts);
|
let accounts = take_weak!(self.accounts);
|
||||||
match accounts.locked_account_secret(&request.from, &password) {
|
match accounts.locked_account_secret(&request.from, &password) {
|
||||||
Ok(secret) => sign_and_dispatch(&self.client, &self.miner, request, secret),
|
Ok(secret) => to_value(&sign_and_dispatch(&*take_weak!(self.client), &*take_weak!(self.miner), request, secret)),
|
||||||
Err(_) => to_value(&H256::zero()),
|
Err(_) => to_value(&H256::zero()),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -63,19 +63,27 @@ impl<A: 'static, C: 'static, M: 'static> PersonalSigner for SignerClient<A, C, M
|
|||||||
|(id, modification, pass)| {
|
|(id, modification, pass)| {
|
||||||
let accounts = take_weak!(self.accounts);
|
let accounts = take_weak!(self.accounts);
|
||||||
let queue = take_weak!(self.queue);
|
let queue = take_weak!(self.queue);
|
||||||
queue.remove_request(id)
|
let client = take_weak!(self.client);
|
||||||
.and_then(|confirmation| {
|
let miner = take_weak!(self.miner);
|
||||||
|
queue.peek(&id).and_then(|confirmation| {
|
||||||
let mut request = confirmation.transaction;
|
let mut request = confirmation.transaction;
|
||||||
// apply modification
|
// apply modification
|
||||||
if let Some(gas_price) = modification.gas_price {
|
if let Some(gas_price) = modification.gas_price {
|
||||||
request.gas_price = Some(gas_price);
|
request.gas_price = Some(gas_price);
|
||||||
}
|
}
|
||||||
match accounts.locked_account_secret(&request.from, &pass) {
|
match accounts.locked_account_secret(&request.from, &pass) {
|
||||||
Ok(secret) => Some(sign_and_dispatch(&self.client, &self.miner, request, secret)),
|
Ok(secret) => {
|
||||||
|
let hash = sign_and_dispatch(&*client, &*miner, request, secret);
|
||||||
|
queue.request_confirmed(id, hash);
|
||||||
|
Some(to_value(&hash))
|
||||||
|
},
|
||||||
Err(_) => None
|
Err(_) => None
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.unwrap_or_else(|| to_value(&H256::zero()))
|
.unwrap_or_else(|| {
|
||||||
|
queue.request_rejected(id);
|
||||||
|
to_value(&H256::zero())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -84,7 +92,7 @@ impl<A: 'static, C: 'static, M: 'static> PersonalSigner for SignerClient<A, C, M
|
|||||||
from_params::<(U256, )>(params).and_then(
|
from_params::<(U256, )>(params).and_then(
|
||||||
|(id, )| {
|
|(id, )| {
|
||||||
let queue = take_weak!(self.queue);
|
let queue = take_weak!(self.queue);
|
||||||
let res = queue.remove_request(id);
|
let res = queue.request_rejected(id);
|
||||||
to_value(&res.is_some())
|
to_value(&res.is_some())
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -30,12 +30,15 @@
|
|||||||
//!
|
//!
|
||||||
//! ```
|
//! ```
|
||||||
//! extern crate ethcore_signer;
|
//! extern crate ethcore_signer;
|
||||||
|
//! extern crate ethcore_rpc;
|
||||||
//!
|
//!
|
||||||
|
//! use std::sync::Arc;
|
||||||
//! use ethcore_signer::ServerBuilder;
|
//! use ethcore_signer::ServerBuilder;
|
||||||
|
//! use ethcore_rpc::ConfirmationsQueue;
|
||||||
//!
|
//!
|
||||||
//! fn main() {
|
//! fn main() {
|
||||||
//! let builder = ServerBuilder::new();
|
//! let queue = Arc::new(ConfirmationsQueue::default());
|
||||||
//! let _server = builder.start("127.0.0.1:8084".parse().unwrap()).unwrap();
|
//! let _server = ServerBuilder::new(queue).start("127.0.0.1:8084".parse().unwrap());
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ use std::sync::Arc;
|
|||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use util::panics::{PanicHandler, OnPanicListener, MayPanic};
|
use util::panics::{PanicHandler, OnPanicListener, MayPanic};
|
||||||
use jsonrpc_core::{IoHandler, IoDelegate};
|
use jsonrpc_core::{IoHandler, IoDelegate};
|
||||||
use rpc::Extendable;
|
use rpc::{Extendable, ConfirmationsQueue};
|
||||||
|
|
||||||
mod session;
|
mod session;
|
||||||
|
|
||||||
@ -49,15 +49,10 @@ impl From<ws::Error> for ServerError {
|
|||||||
|
|
||||||
/// Builder for `WebSockets` server
|
/// Builder for `WebSockets` server
|
||||||
pub struct ServerBuilder {
|
pub struct ServerBuilder {
|
||||||
|
queue: Arc<ConfirmationsQueue>,
|
||||||
handler: Arc<IoHandler>,
|
handler: Arc<IoHandler>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ServerBuilder {
|
|
||||||
fn default() -> Self {
|
|
||||||
ServerBuilder::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Extendable for ServerBuilder {
|
impl Extendable for ServerBuilder {
|
||||||
fn add_delegate<D: Send + Sync + 'static>(&self, delegate: IoDelegate<D>) {
|
fn add_delegate<D: Send + Sync + 'static>(&self, delegate: IoDelegate<D>) {
|
||||||
self.handler.add_delegate(delegate);
|
self.handler.add_delegate(delegate);
|
||||||
@ -66,30 +61,32 @@ impl Extendable for ServerBuilder {
|
|||||||
|
|
||||||
impl ServerBuilder {
|
impl ServerBuilder {
|
||||||
/// Creates new `ServerBuilder`
|
/// Creates new `ServerBuilder`
|
||||||
pub fn new() -> Self {
|
pub fn new(queue: Arc<ConfirmationsQueue>) -> Self {
|
||||||
ServerBuilder {
|
ServerBuilder {
|
||||||
handler: Arc::new(IoHandler::new())
|
queue: queue,
|
||||||
|
handler: Arc::new(IoHandler::new()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Starts a new `WebSocket` server in separate thread.
|
/// Starts a new `WebSocket` server in separate thread.
|
||||||
/// Returns a `Server` handle which closes the server when droped.
|
/// Returns a `Server` handle which closes the server when droped.
|
||||||
pub fn start(self, addr: SocketAddr) -> Result<Server, ServerError> {
|
pub fn start(self, addr: SocketAddr) -> Result<Server, ServerError> {
|
||||||
Server::start(addr, self.handler)
|
Server::start(addr, self.handler, self.queue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `WebSockets` server implementation.
|
/// `WebSockets` server implementation.
|
||||||
pub struct Server {
|
pub struct Server {
|
||||||
handle: Option<thread::JoinHandle<ws::WebSocket<session::Factory>>>,
|
handle: Option<thread::JoinHandle<ws::WebSocket<session::Factory>>>,
|
||||||
broadcaster: ws::Sender,
|
broadcaster_handle: Option<thread::JoinHandle<()>>,
|
||||||
|
queue: Arc<ConfirmationsQueue>,
|
||||||
panic_handler: Arc<PanicHandler>,
|
panic_handler: Arc<PanicHandler>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Server {
|
impl Server {
|
||||||
/// Starts a new `WebSocket` server in separate thread.
|
/// Starts a new `WebSocket` server in separate thread.
|
||||||
/// Returns a `Server` handle which closes the server when droped.
|
/// Returns a `Server` handle which closes the server when droped.
|
||||||
pub fn start(addr: SocketAddr, handler: Arc<IoHandler>) -> Result<Server, ServerError> {
|
fn start(addr: SocketAddr, handler: Arc<IoHandler>, queue: Arc<ConfirmationsQueue>) -> Result<Server, ServerError> {
|
||||||
let config = {
|
let config = {
|
||||||
let mut config = ws::Settings::default();
|
let mut config = ws::Settings::default();
|
||||||
config.max_connections = 5;
|
config.max_connections = 5;
|
||||||
@ -103,6 +100,7 @@ impl Server {
|
|||||||
let panic_handler = PanicHandler::new_in_arc();
|
let panic_handler = PanicHandler::new_in_arc();
|
||||||
let ph = panic_handler.clone();
|
let ph = panic_handler.clone();
|
||||||
let broadcaster = ws.broadcaster();
|
let broadcaster = ws.broadcaster();
|
||||||
|
|
||||||
// Spawn a thread with event loop
|
// Spawn a thread with event loop
|
||||||
let handle = thread::spawn(move || {
|
let handle = thread::spawn(move || {
|
||||||
ph.catch_panic(move || {
|
ph.catch_panic(move || {
|
||||||
@ -110,10 +108,24 @@ impl Server {
|
|||||||
}).unwrap()
|
}).unwrap()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Spawn a thread for broadcasting
|
||||||
|
let ph = panic_handler.clone();
|
||||||
|
let q = queue.clone();
|
||||||
|
let broadcaster_handle = thread::spawn(move || {
|
||||||
|
ph.catch_panic(move || {
|
||||||
|
q.start_listening(|_message| {
|
||||||
|
// TODO [ToDr] Some better structure here for messages.
|
||||||
|
broadcaster.send("new_message").unwrap();
|
||||||
|
}).expect("It's the only place we are running start_listening. It shouldn't fail.");
|
||||||
|
broadcaster.shutdown().expect("Broadcaster should close gently.")
|
||||||
|
}).unwrap()
|
||||||
|
});
|
||||||
|
|
||||||
// Return a handle
|
// Return a handle
|
||||||
Ok(Server {
|
Ok(Server {
|
||||||
handle: Some(handle),
|
handle: Some(handle),
|
||||||
broadcaster: broadcaster,
|
broadcaster_handle: Some(broadcaster_handle),
|
||||||
|
queue: queue,
|
||||||
panic_handler: panic_handler,
|
panic_handler: panic_handler,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -127,7 +139,8 @@ impl MayPanic for Server {
|
|||||||
|
|
||||||
impl Drop for Server {
|
impl Drop for Server {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
self.broadcaster.shutdown().expect("WsServer should close nicely.");
|
self.queue.finish();
|
||||||
|
self.broadcaster_handle.take().unwrap().join().unwrap();
|
||||||
self.handle.take().unwrap().join().unwrap();
|
self.handle.take().unwrap().join().unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user