Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dan): add WASM template invocation from user instruction (#4331)
Description --- Adds minimum required code to call a WASM template from a user instruction. Motivation and Context --- This PR adds and stubs out the required code to call WASM templates from a user instruction. This PR makes use of radix's excellent sbor encoding library, but this is not required and could be switched out. It's quite messy and incomplete in places. How Has This Been Tested? --- Adds an integration test that compiles and calls the stubbed-out template code. This is bordering on POC code, so no other tests are written - tests should be implemented after some feedback/iterations from this PR.
- Loading branch information
Showing
26 changed files
with
1,559 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
} | ||
} | ||
} |
Oops, something went wrong.