Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dan): add WASM template invocation from user instruction #4331

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 69 additions & 4 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ members = [
"comms/dht",
"comms/rpc_macros",
"dan_layer/core",
"dan_layer/template_abi",
"dan_layer/storage_sqlite",
"common_sqlite",
"infrastructure/libtor",
Expand Down
18 changes: 11 additions & 7 deletions dan_layer/engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tari_common_types = {path = "../../base_layer/common_types"}
tari_common_types = { path = "../../base_layer/common_types" }
tari_common = { path = "../../common" }
tari_dan_common_types = {path = "../common_types"}
tari_dan_common_types = { path = "../common_types" }
tari_mmr = { path = "../../base_layer/mmr" }
tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", tag = "v0.4.4" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = "v0.15.0" }
tari_template_abi = { path = "../template_abi" }

wasmer = "2.2.1"
digest = "0.9.0"
d3ne = { git="https://github.com/stringhandler/d3ne-rs.git", branch="st-fixes2"}
anyhow = "1.0.53"
serde = "1.0.126"
borsh = "0.9.3"
cargo_toml = "0.11.5"
d3ne = { git = "https://github.com/stringhandler/d3ne-rs.git", branch = "st-fixes2" }
digest = "0.9.0"
log = { version = "0.4.8", features = ["std"] }
serde_json ="1.0.81"
rand = "0.8.1"
serde = "1.0.126"
serde_json = "1.0.81"
thiserror = "^1.0.20"
wasmer = "2.2.1"
69 changes: 69 additions & 0 deletions dan_layer/engine/src/compile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2022. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{fs, io, io::ErrorKind, path::Path, process::Command};

use cargo_toml::{Manifest, Product};

pub fn compile_template<P: AsRef<Path>>(package_dir: P) -> io::Result<Vec<u8>> {
let status = Command::new("cargo")
.current_dir(package_dir.as_ref())
.args(["build", "--target", "wasm32-unknown-unknown", "--release"])
.status()?;
if !status.success() {
return Err(io::Error::new(
ErrorKind::Other,
format!("Failed to compile package: {:?}", package_dir.as_ref()),
));
}

// resolve wasm name
let manifest = Manifest::from_path(&package_dir.as_ref().join("Cargo.toml")).unwrap();
let wasm_name = if let Some(Product { name: Some(name), .. }) = manifest.lib {
// lib name
name
} else if let Some(pkg) = manifest.package {
// package name
pkg.name.replace('-', "_")
} else {
// file name
package_dir
.as_ref()
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_owned()
.replace('-', "_")
};

// path of the wasm executable
let mut path = package_dir.as_ref().to_path_buf();
path.push("target");
path.push("wasm32-unknown-unknown");
path.push("release");
path.push(wasm_name);
path.set_extension("wasm");

// return
fs::read(path)
}
49 changes: 49 additions & 0 deletions dan_layer/engine/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2022. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use rand::rngs::OsRng;
use tari_common_types::types::{PrivateKey, PublicKey};
use tari_crypto::{
hash::blake2::Blake256,
hashing::{DomainSeparatedHasher, DomainSeparation},
keys::PublicKey as PublicKeyT,
};

pub fn create_key_pair() -> (PrivateKey, PublicKey) {
PublicKey::random_keypair(&mut OsRng)
}

pub struct TariEngineDomainSeparation;

impl DomainSeparation for TariEngineDomainSeparation {
fn version() -> u8 {
0
}

fn domain() -> &'static str {
"tari.dan.engine"
}
}

pub fn domain_separated_hasher(label: &str) -> DomainSeparatedHasher<Blake256, TariEngineDomainSeparation> {
DomainSeparatedHasher::new(label)
}
41 changes: 41 additions & 0 deletions dan_layer/engine/src/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2022. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::sync::{Arc, Mutex};

pub fn tari_engine(env: &EngineEnvironment, op: i32, _args_ptr: i32, args_len: i32) -> i32 {
println!("tari_engine CALLED: op: {}, args: {}", op, args_len);
// TODO:
env.inc_counter();
0
}

#[derive(wasmer::WasmerEnv, Clone, Default)]
pub struct EngineEnvironment {
counter: Arc<Mutex<i32>>,
}

impl EngineEnvironment {
pub fn inc_counter(&self) {
*self.counter.lock().unwrap() += 1;
}
}
60 changes: 60 additions & 0 deletions dan_layer/engine/src/instruction/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2022. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use tari_common_types::types::PrivateKey;

use super::{Instruction, InstructionSet};
use crate::instruction::signature::InstructionSignature;

#[derive(Debug, Clone, Default)]
pub struct InstructionBuilder {
instructions: Vec<Instruction>,
signature: Option<InstructionSignature>,
}

impl InstructionBuilder {
pub fn new() -> Self {
Self {
instructions: Vec::new(),
signature: None,
}
}

pub fn add_instruction(&mut self, instruction: Instruction) -> &mut Self {
self.instructions.push(instruction);
// Reset the signature as it is no longer valid
self.signature = None;
self
}

pub fn sign(&mut self, secret_key: &PrivateKey) -> &mut Self {
self.signature = Some(InstructionSignature::sign(secret_key, &self.instructions));
self
}

pub fn build(&mut self) -> InstructionSet {
InstructionSet {
instructions: self.instructions.drain(..).collect(),
signature: self.signature.take().expect("not signed"),
}
}
}
Loading