Skip to content

Commit

Permalink
Add cli param
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteNacked committed Oct 14, 2024
1 parent 4adef59 commit 6975f42
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 10 deletions.
1 change: 1 addition & 0 deletions node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ clap = { workspace = true, features = ["derive"] }
mimalloc = { workspace = true, default-features = false }
log = { workspace = true, features = ["std"] }
futures.workspace = true
derive_more.workspace = true

# Gear
runtime-primitives.workspace = true
Expand Down
27 changes: 27 additions & 0 deletions node/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,29 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use clap::Parser;
use std::str::FromStr;

#[allow(missing_docs)]
#[derive(Debug, Clone, Parser, derive_more::Display)]
pub enum SandboxBackend {
#[display(fmt = "wasmer")]
Wasmer,
#[display(fmt = "wasmi")]
Wasmi,
}

// TODO: use `derive_more::FromStr` when derive_more dependency is updated to 1.0
impl FromStr for SandboxBackend {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"wasmer" => Ok(SandboxBackend::Wasmer),
"wasmi" => Ok(SandboxBackend::Wasmi),
_ => Err(format!("Unknown sandbox executor: {}", s)),
}
}
}

#[allow(missing_docs)]
#[derive(Debug, Parser)]
Expand All @@ -26,6 +49,10 @@ pub struct RunCmd {
#[command(flatten)]
pub base: sc_cli::RunCmd,

/// The Wasm host executor to use in program sandbox.
#[arg(long, default_value_t = SandboxBackend::Wasmer)]
pub sandbox_backend: SandboxBackend,

/// The upper limit for the amount of gas a validator can burn in one block.
#[arg(long)]
pub max_gas: Option<u64>,
Expand Down
10 changes: 9 additions & 1 deletion node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::cli::{Cli, Subcommand};
use crate::{
cli::{Cli, Subcommand},
SandboxBackend,
};
use runtime_primitives::Block;
use sc_cli::{ChainSpec, SubstrateCli};
use sc_service::config::BasePath;
Expand Down Expand Up @@ -130,6 +133,11 @@ macro_rules! unwrap_client {
pub fn run() -> sc_cli::Result<()> {
let cli = Cli::from_args();

gear_runtime_interface::sandbox_init(match cli.run.sandbox_backend {
SandboxBackend::Wasmer => gear_runtime_interface::SandboxBackend::Wasmer,
SandboxBackend::Wasmi => gear_runtime_interface::SandboxBackend::Wasmi,
});

let old_base = BasePath::from_project("", "", "gear-node");
let new_base = BasePath::from_project("", "", &Cli::executable_name());
if old_base.path().exists() && !new_base.path().exists() {
Expand Down
1 change: 0 additions & 1 deletion node/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

fn main() -> gear_cli::Result<()> {
gear_runtime_interface::sandbox_init();
gear_cli::run()
}
13 changes: 7 additions & 6 deletions runtime-interface/sandbox/src/detail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ struct Sandboxes {
}

impl Sandboxes {
pub fn new() -> Self {
pub fn new(sandbox_backend: sandbox_env::SandboxBackend) -> Self {
Self {
store_data_key: 0,
store: sandbox_env::SandboxComponents::new(sandbox_env::SandboxBackend::Wasmi),
store: sandbox_env::SandboxComponents::new(sandbox_backend),
}
}

Expand All @@ -62,12 +62,13 @@ impl Sandboxes {
}

thread_local! {
static SANDBOXES: RefCell<Sandboxes> = RefCell::new(Sandboxes::new());
static SANDBOXES: RefCell<Sandboxes> = panic!("Sandbox not initialized");
}

pub fn init() {
SANDBOXES.with(|sandboxes| {
let _store = sandboxes.borrow_mut().get(0);
pub fn init(sandbox_backend: sandbox_env::SandboxBackend) {
SANDBOXES.set(Sandboxes::new(sandbox_backend));
SANDBOXES.with_borrow_mut(|sandboxes| {
let _store = sandboxes.get(0);
})
}

Expand Down
2 changes: 1 addition & 1 deletion runtime-interface/sandbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "std")]
pub use gear_sandbox_host::sandbox::env::Instantiate;
pub use gear_sandbox_host::sandbox::{env::Instantiate, SandboxBackend};
use sp_runtime_interface::{runtime_interface, Pointer};
use sp_wasm_interface::HostPointer;

Expand Down
4 changes: 3 additions & 1 deletion runtime-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ use {

pub use gear_sandbox_interface::sandbox;
#[cfg(feature = "std")]
pub use gear_sandbox_interface::{detail as sandbox_detail, init as sandbox_init, Instantiate};
pub use gear_sandbox_interface::{
detail as sandbox_detail, init as sandbox_init, Instantiate, SandboxBackend,
};

const _: () = assert!(size_of::<HostPointer>() >= size_of::<usize>());

Expand Down

0 comments on commit 6975f42

Please sign in to comment.