Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Prev Previous commit
Next Next commit
move native environment stuff to substrate executor
  • Loading branch information
rphmeier committed Feb 8, 2018
commit cbf79ff8d720dc37d1b6f1c337cb194161089aa6
12 changes: 6 additions & 6 deletions polkadot/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
//! Strongly typed API for Polkadot based around the locally-compiled native
//! runtime.

extern crate polkadot_executor as p_executor;
extern crate polkadot_executor as polkadot_executor;
extern crate polkadot_runtime ;
extern crate polkadot_primitives as primitives;
extern crate substrate_client as client;
extern crate substrate_executor as s_executor;
extern crate substrate_executor as substrate_executor;
extern crate substrate_state_machine as state_machine;

#[macro_use]
Expand All @@ -31,8 +31,8 @@ use client::backend::Backend;
use client::blockchain::BlockId;
use client::Client;
use polkadot_runtime::runtime;
use p_executor::LocalNativeExecutionDispatch as LocalDispatch;
use s_executor::{NativeExecutionDispatch, NativeExecutor};
use polkadot_executor::LocalNativeExecutionDispatch as LocalDispatch;
use substrate_executor::{NativeExecutionDispatch, NativeExecutor};
use primitives::{AccountId, SessionKey};
use primitives::parachain::DutyRoster;

Expand All @@ -56,7 +56,7 @@ error_chain! {
}

links {
Executor(s_executor::error::Error, s_executor::error::ErrorKind);
Executor(substrate_executor::error::Error, substrate_executor::error::ErrorKind);
}
}

Expand Down Expand Up @@ -96,7 +96,7 @@ macro_rules! with_runtime {
backend: &state,
};

LocalDispatch::execute_runtime(&mut ext, $exec).map_err(Into::into)
::substrate_executor::with_native_environment(&mut ext, $exec).map_err(Into::into)
})
}}
}
Expand Down
20 changes: 1 addition & 19 deletions polkadot/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,9 @@ use substrate_executor::error::{Error, ErrorKind};
use substrate_executor::{NativeExecutionDispatch, NativeExecutor};
use state_machine::Externalities;

use std::panic::catch_unwind;


/// A null struct which implements `NativeExecutionDispatch` feeding in the hard-coded runtime.
pub struct LocalNativeExecutionDispatch;

impl LocalNativeExecutionDispatch {
/// Set up the externalities and safe calling environment to execute calls to the runtime.
pub fn execute_runtime<F, U>(ext: &mut Externalities, f: F) -> Result<U, Error>
where F: ::std::panic::UnwindSafe + FnOnce() -> U
{
runtime_io::with_externalities(ext, move || safe_call(f))
}
}

impl NativeExecutionDispatch for LocalNativeExecutionDispatch {
fn native_equivalent() -> &'static [u8] {
// WARNING!!! This assumes that the runtime was built *before* the main project. Until we
Expand All @@ -58,17 +46,11 @@ impl NativeExecutionDispatch for LocalNativeExecutionDispatch {
}

fn dispatch(ext: &mut Externalities, method: &str, data: &[u8]) -> Result<Vec<u8>, Error> {
LocalNativeExecutionDispatch::execute_runtime(ext, move || runtime::api::dispatch(method, data))?
::substrate_executor::with_native_environment(ext, move || runtime::api::dispatch(method, data))?
.ok_or_else(|| ErrorKind::MethodNotFound(method.to_owned()).into())
}
}

fn safe_call<F, U>(f: F) -> Result<U, Error>
where F: ::std::panic::UnwindSafe + FnOnce() -> U
{
catch_unwind(f).map_err(|_| ErrorKind::Runtime.into())
}

/// Creates new RustExecutor for contracts.
pub fn executor() -> NativeExecutor<LocalNativeExecutionDispatch> {
NativeExecutor { _dummy: ::std::marker::PhantomData }
Expand Down
2 changes: 1 addition & 1 deletion substrate/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ mod native_executor;

pub mod error;
pub use wasm_executor::WasmExecutor;
pub use native_executor::{NativeExecutor, NativeExecutionDispatch};
pub use native_executor::{with_native_environment, NativeExecutor, NativeExecutionDispatch};
17 changes: 16 additions & 1 deletion substrate/executor/src/native_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,25 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use error::{Error, Result};
use error::{Error, ErrorKind, Result};
use state_machine::{Externalities, CodeExecutor};
use wasm_executor::WasmExecutor;

fn safe_call<F, U>(f: F) -> Result<U>
where F: ::std::panic::UnwindSafe + FnOnce() -> U
{
::std::panic::catch_unwind(f).map_err(|_| ErrorKind::Runtime.into())
}

/// Set up the externalities and safe calling environment to execute calls to a native runtime.
///
/// If the inner closure panics, it will be caught and return an error.
pub fn with_native_environment<F, U>(ext: &mut Externalities, f: F) -> Result<U>
where F: ::std::panic::UnwindSafe + FnOnce() -> U
{
::runtime_io::with_externalities(ext, move || safe_call(f))
}

/// Delegate for dispatching a CodeExecutor call to native code.
pub trait NativeExecutionDispatch {
/// Get the wasm code that the native dispatch will be equivalent to.
Expand Down