diff --git a/Cargo.lock b/Cargo.lock index aa0ce1415d..0172f15ea6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5979,9 +5979,9 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.142" +version = "1.0.143" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e590c437916fb6b221e1d00df6e3294f3fccd70ca7e92541c475d6ed6ef5fee2" +checksum = "53e8e5d5b70924f74ff5c6d64d9a5acd91422117c60f48c4e07855238a254553" dependencies = [ "serde_derive", ] @@ -6026,9 +6026,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.142" +version = "1.0.143" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34b5b8d809babe02f538c2cfec6f2c1ed10804c0e5a6a041a049a4f5588ccc2e" +checksum = "d3d8e8de557aee63c26b85b947f5e59b690d0454c753f3adeb5cd7835ab88391" dependencies = [ "proc-macro2", "quote", @@ -7176,7 +7176,7 @@ dependencies = [ "tari_dan_common_types", "tari_mmr", "tari_template_abi", - "tari_template_types", + "tari_template_lib", "tari_utilities", "thiserror", "wasmer", @@ -7485,15 +7485,15 @@ name = "tari_template_abi" version = "0.1.0" dependencies = [ "borsh", - "tari_template_types", ] [[package]] name = "tari_template_lib" version = "0.1.0" dependencies = [ + "borsh", + "serde", "tari_template_abi", - "tari_template_types", ] [[package]] @@ -7504,15 +7504,6 @@ dependencies = [ "proc-macro2", "quote", "syn", - "tari_template_abi", -] - -[[package]] -name = "tari_template_types" -version = "0.1.0" -dependencies = [ - "borsh", - "serde", ] [[package]] diff --git a/dan_layer/engine/Cargo.toml b/dan_layer/engine/Cargo.toml index c7352bfb61..e312b2bba9 100644 --- a/dan_layer/engine/Cargo.toml +++ b/dan_layer/engine/Cargo.toml @@ -12,7 +12,7 @@ tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", tag = " tari_dan_common_types = { path = "../common_types" } tari_mmr = { path = "../../base_layer/mmr" } tari_template_abi = { path = "../template_abi" } -tari_template_types = { path = "../template_types", features = ["serde"] } +tari_template_lib = { path = "../template_lib", features = ["serde"] } tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", tag = "v0.4.5" } anyhow = "1.0.53" diff --git a/dan_layer/engine/src/instruction/error.rs b/dan_layer/engine/src/instruction/error.rs index e5d0ba22ff..9d4e9bb196 100644 --- a/dan_layer/engine/src/instruction/error.rs +++ b/dan_layer/engine/src/instruction/error.rs @@ -20,7 +20,7 @@ // 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_template_types::models::PackageId; +use tari_template_lib::models::PackageId; use crate::{runtime::RuntimeError, wasm::WasmExecutionError}; diff --git a/dan_layer/engine/src/instruction/mod.rs b/dan_layer/engine/src/instruction/mod.rs index 9b04bcc5c7..f2ac91f665 100644 --- a/dan_layer/engine/src/instruction/mod.rs +++ b/dan_layer/engine/src/instruction/mod.rs @@ -30,7 +30,7 @@ pub use processor::InstructionProcessor; mod signature; pub use signature::InstructionSignature; -use tari_template_types::models::{ComponentId, PackageId}; +use tari_template_lib::models::{ComponentId, PackageId}; #[derive(Debug, Clone)] pub enum Instruction { diff --git a/dan_layer/engine/src/instruction/processor.rs b/dan_layer/engine/src/instruction/processor.rs index e0d4495f0a..2db5ffae4c 100644 --- a/dan_layer/engine/src/instruction/processor.rs +++ b/dan_layer/engine/src/instruction/processor.rs @@ -23,7 +23,7 @@ use std::{collections::HashMap, sync::Arc}; use tari_template_abi::encode; -use tari_template_types::models::PackageId; +use tari_template_lib::models::PackageId; use crate::{ instruction::{error::InstructionError, Instruction, InstructionSet}, diff --git a/dan_layer/engine/src/models/component.rs b/dan_layer/engine/src/models/component.rs index 4fb6943c11..97bc30dc6b 100644 --- a/dan_layer/engine/src/models/component.rs +++ b/dan_layer/engine/src/models/component.rs @@ -21,7 +21,7 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use tari_template_abi::{CreateComponentArg, Decode, Encode}; -use tari_template_types::{ +use tari_template_lib::{ models::{ContractAddress, PackageId}, Hash, }; diff --git a/dan_layer/engine/src/models/resource.rs b/dan_layer/engine/src/models/resource.rs index 753e74bd52..ba2b078d68 100644 --- a/dan_layer/engine/src/models/resource.rs +++ b/dan_layer/engine/src/models/resource.rs @@ -21,7 +21,7 @@ // USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use tari_template_abi::{Decode, Encode}; -use tari_template_types::Hash; +use tari_template_lib::Hash; pub type ResourceAddress = Hash; @@ -29,10 +29,24 @@ pub type ResourceAddress = Hash; pub enum Resource { Coin { address: ResourceAddress, + // type_descriptor: TypeDescriptor, amount: u64, }, Token { address: ResourceAddress, + // type_descriptor: TypeDescriptor, token_ids: Vec, }, } + +pub trait ResourceTypeDescriptor { + fn type_descriptor(&self) -> TypeDescriptor; +} + +// The thinking here, that a resource address + a "local" type id together can used to validate type safety of the +// resources at runtime. The local type id can be defined as a unique id within the scope of the contract. We'll have to +// get further to see if this can work or is even needed. +#[derive(Debug, Clone, Encode, Decode, serde::Deserialize)] +pub struct TypeDescriptor { + type_id: u16, +} diff --git a/dan_layer/engine/src/models/vault.rs b/dan_layer/engine/src/models/vault.rs index 40501c2e22..a9ba3cd941 100755 --- a/dan_layer/engine/src/models/vault.rs +++ b/dan_layer/engine/src/models/vault.rs @@ -19,17 +19,18 @@ // 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 borsh::{BorshDeserialize, BorshSerialize}; -use crate::models::resource::ResourceAddress; +use tari_template_abi::{Decode, Encode}; -#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)] +use crate::models::Resource; + +#[derive(Debug, Clone, Encode, Decode)] pub struct Vault { - address: ResourceAddress, + resource: Resource, } impl Vault { - pub fn new(address: ResourceAddress) -> Self { - Self { address } + pub fn new(resource: Resource) -> Self { + Self { resource } } } diff --git a/dan_layer/engine/src/packager/package.rs b/dan_layer/engine/src/packager/package.rs index 31156d8aca..e0fc3e8c83 100644 --- a/dan_layer/engine/src/packager/package.rs +++ b/dan_layer/engine/src/packager/package.rs @@ -24,7 +24,7 @@ use std::collections::HashMap; use digest::Digest; use rand::{rngs::OsRng, RngCore}; -use tari_template_types::models::PackageId; +use tari_template_lib::models::PackageId; use crate::{ crypto, diff --git a/dan_layer/engine/src/runtime.rs b/dan_layer/engine/src/runtime.rs index f603532666..9681464a0b 100644 --- a/dan_layer/engine/src/runtime.rs +++ b/dan_layer/engine/src/runtime.rs @@ -27,8 +27,10 @@ use std::{ }; use tari_common_types::types::FixedHash; -use tari_template_abi::LogLevel; -use tari_template_types::models::{Component, ComponentId, ComponentInstance}; +use tari_template_lib::{ + args::LogLevel, + models::{Component, ComponentId, ComponentInstance}, +}; use crate::{models::Bucket, state_store::StateStoreError}; diff --git a/dan_layer/engine/src/wasm/process.rs b/dan_layer/engine/src/wasm/process.rs index de0c48e406..70d3e3fbd5 100644 --- a/dan_layer/engine/src/wasm/process.rs +++ b/dan_layer/engine/src/wasm/process.rs @@ -23,19 +23,13 @@ use std::io; use borsh::{BorshDeserialize, BorshSerialize}; -use tari_template_abi::{ - decode, - encode_into, - encode_with_len, +use tari_template_abi::{decode, encode, encode_into, encode_with_len, CallInfo, Type}; +use tari_template_lib::{ + abi_context::AbiContext, + args::{CreateComponentArg, EmitLogArg, GetComponentArg, SetComponentStateArg}, + models::{Component, Contract, ContractAddress, Package, PackageId}, ops, - CallInfo, - CreateComponentArg, - EmitLogArg, - GetComponentArg, - SetComponentStateArg, - Type, }; -use tari_template_types::models::{Component, Contract, ContractAddress, PackageId}; use wasmer::{Function, Instance, Module, Store, Val, WasmerEnv}; use crate::{ @@ -148,6 +142,16 @@ impl Process { // out-of-bounds access error. Ok(ptr.as_i32()) } + + fn encoded_abi_context(&self) -> Vec { + encode(&AbiContext { + package: Package { id: self.package_id }, + contract: Contract { + address: self.contract_address, + }, + }) + .unwrap() + } } impl Invokable for Process { @@ -160,10 +164,7 @@ impl Invokable for Process { .ok_or_else(|| WasmExecutionError::FunctionNotFound { name: name.into() })?; let call_info = CallInfo { - contract: Contract { - address: self.contract_address, - }, - package: tari_template_types::models::Package { id: self.package_id }, + abi_context: self.encoded_abi_context(), func_name: func_def.name.clone(), args, }; diff --git a/dan_layer/engine/tests/hello_world/Cargo.lock b/dan_layer/engine/tests/hello_world/Cargo.lock index de3da82761..e3375cdea0 100644 --- a/dan_layer/engine/tests/hello_world/Cargo.lock +++ b/dan_layer/engine/tests/hello_world/Cargo.lock @@ -91,7 +91,6 @@ dependencies = [ "tari_template_abi", "tari_template_lib", "tari_template_macros", - "tari_template_types", ] [[package]] @@ -155,15 +154,14 @@ name = "tari_template_abi" version = "0.1.0" dependencies = [ "borsh", - "tari_template_types", ] [[package]] name = "tari_template_lib" version = "0.1.0" dependencies = [ + "borsh", "tari_template_abi", - "tari_template_types", ] [[package]] @@ -173,14 +171,6 @@ dependencies = [ "proc-macro2", "quote", "syn", - "tari_template_abi", -] - -[[package]] -name = "tari_template_types" -version = "0.1.0" -dependencies = [ - "borsh", ] [[package]] diff --git a/dan_layer/engine/tests/hello_world/Cargo.toml b/dan_layer/engine/tests/hello_world/Cargo.toml index c4f85580fa..e4fdadcce0 100644 --- a/dan_layer/engine/tests/hello_world/Cargo.toml +++ b/dan_layer/engine/tests/hello_world/Cargo.toml @@ -10,7 +10,6 @@ edition = "2021" tari_template_abi = { path = "../../../template_abi" } tari_template_lib = { path = "../../../template_lib" } tari_template_macros = { path = "../../../template_macros" } -tari_template_types = { path = "../../../template_types" } [profile.release] opt-level = 's' # Optimize for size. diff --git a/dan_layer/engine/tests/mock_runtime_interface.rs b/dan_layer/engine/tests/mock_runtime_interface.rs index 736fca580c..04afb47e18 100644 --- a/dan_layer/engine/tests/mock_runtime_interface.rs +++ b/dan_layer/engine/tests/mock_runtime_interface.rs @@ -28,8 +28,10 @@ use tari_dan_engine::{ runtime::{RuntimeError, RuntimeInterface}, state_store::{memory::MemoryStateStore, AtomicDb, StateReader, StateWriter}, }; -use tari_template_abi::LogLevel; -use tari_template_types::models::{Component, ComponentId, ComponentInstance}; +use tari_template_lib::{ + args::LogLevel, + models::{Component, ComponentId, ComponentInstance}, +}; #[derive(Debug, Clone, Default)] pub struct MockRuntimeInterface { diff --git a/dan_layer/engine/tests/state/Cargo.lock b/dan_layer/engine/tests/state/Cargo.lock index 9de4d62c9b..659d755fe3 100644 --- a/dan_layer/engine/tests/state/Cargo.lock +++ b/dan_layer/engine/tests/state/Cargo.lock @@ -136,7 +136,6 @@ dependencies = [ "tari_template_abi", "tari_template_lib", "tari_template_macros", - "tari_template_types", ] [[package]] @@ -155,15 +154,14 @@ name = "tari_template_abi" version = "0.1.0" dependencies = [ "borsh", - "tari_template_types", ] [[package]] name = "tari_template_lib" version = "0.1.0" dependencies = [ + "borsh", "tari_template_abi", - "tari_template_types", ] [[package]] @@ -173,14 +171,6 @@ dependencies = [ "proc-macro2", "quote", "syn", - "tari_template_abi", -] - -[[package]] -name = "tari_template_types" -version = "0.1.0" -dependencies = [ - "borsh", ] [[package]] diff --git a/dan_layer/engine/tests/state/Cargo.toml b/dan_layer/engine/tests/state/Cargo.toml index 6f085aa29c..9374e4d4a2 100644 --- a/dan_layer/engine/tests/state/Cargo.toml +++ b/dan_layer/engine/tests/state/Cargo.toml @@ -10,7 +10,6 @@ edition = "2021" tari_template_abi = { path = "../../../template_abi" } tari_template_lib = { path = "../../../template_lib" } tari_template_macros = { path = "../../../template_macros" } -tari_template_types = { path = "../../../template_types" } [profile.release] opt-level = 's' # Optimize for size. diff --git a/dan_layer/engine/tests/test.rs b/dan_layer/engine/tests/test.rs index 5e95c465b4..625f25c0d1 100644 --- a/dan_layer/engine/tests/test.rs +++ b/dan_layer/engine/tests/test.rs @@ -33,7 +33,7 @@ use tari_dan_engine::{ wasm::compile::compile_template, }; use tari_template_abi::encode; -use tari_template_types::models::{ComponentId, ComponentInstance, PackageId}; +use tari_template_lib::models::{ComponentId, ComponentInstance, PackageId}; #[test] fn test_hello_world() { diff --git a/dan_layer/template_abi/Cargo.toml b/dan_layer/template_abi/Cargo.toml index 3401f056d0..b2d11f346b 100644 --- a/dan_layer/template_abi/Cargo.toml +++ b/dan_layer/template_abi/Cargo.toml @@ -6,6 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -tari_template_types = { path = "../template_types" } - borsh = "0.9.3" diff --git a/dan_layer/template_abi/src/lib.rs b/dan_layer/template_abi/src/lib.rs index 6beb52428a..12b9d56be5 100644 --- a/dan_layer/template_abi/src/lib.rs +++ b/dan_layer/template_abi/src/lib.rs @@ -27,12 +27,10 @@ mod abi; pub use abi::*; -pub use borsh::{self, BorshDeserialize as Decode, BorshSerialize as Encode}; +pub use borsh::{BorshDeserialize as Decode, BorshSerialize as Encode}; mod encoding; pub use encoding::{decode, decode_len, encode, encode_into, encode_with_len}; -pub mod ops; - mod types; pub use types::*; diff --git a/dan_layer/template_abi/src/types.rs b/dan_layer/template_abi/src/types.rs index e32a51fea4..dcd1726051 100644 --- a/dan_layer/template_abi/src/types.rs +++ b/dan_layer/template_abi/src/types.rs @@ -20,8 +20,6 @@ // 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_template_types::models::{ComponentId, Contract, ContractAddress, Package, PackageId}; - use crate::{Decode, Encode}; #[derive(Debug, Clone, Encode, Decode)] @@ -64,39 +62,5 @@ pub enum Type { pub struct CallInfo { pub func_name: String, pub args: Vec>, - pub package: Package, - pub contract: Contract, -} - -#[derive(Debug, Clone, Encode, Decode)] -pub struct EmitLogArg { - pub message: String, - pub level: LogLevel, -} - -#[derive(Debug, Clone, Encode, Decode)] -pub enum LogLevel { - Error, - Warn, - Info, - Debug, -} - -#[derive(Debug, Clone, Encode, Decode)] -pub struct CreateComponentArg { - pub contract_address: ContractAddress, - pub module_name: String, - pub package_id: PackageId, - pub state: Vec, -} - -#[derive(Debug, Clone, Encode, Decode)] -pub struct GetComponentArg { - pub component_id: ComponentId, -} - -#[derive(Debug, Clone, Encode, Decode)] -pub struct SetComponentStateArg { - pub component_id: ComponentId, - pub state: Vec, + pub abi_context: Vec, } diff --git a/dan_layer/template_lib/Cargo.toml b/dan_layer/template_lib/Cargo.toml index 1a8c5fc29d..950f49bc2d 100644 --- a/dan_layer/template_lib/Cargo.toml +++ b/dan_layer/template_lib/Cargo.toml @@ -5,4 +5,6 @@ edition = "2021" [dependencies] tari_template_abi = { path = "../template_abi" } -tari_template_types = { path = "../template_types" } + +borsh = "0.9.3" +serde = { version = "1.0.143", optional = true } diff --git a/dan_layer/template_types/src/lib.rs b/dan_layer/template_lib/src/abi_context.rs similarity index 88% rename from dan_layer/template_types/src/lib.rs rename to dan_layer/template_lib/src/abi_context.rs index ea44e42488..c0ec3c82cd 100644 --- a/dan_layer/template_types/src/lib.rs +++ b/dan_layer/template_lib/src/abi_context.rs @@ -20,7 +20,12 @@ // 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. -mod hash; -pub use hash::Hash; +use tari_template_abi::{Decode, Encode}; -pub mod models; +use crate::models::{Contract, Package}; + +#[derive(Debug, Decode, Encode)] +pub struct AbiContext { + pub package: Package, + pub contract: Contract, +} diff --git a/dan_layer/template_lib/src/args.rs b/dan_layer/template_lib/src/args.rs new file mode 100644 index 0000000000..6cc2570f0e --- /dev/null +++ b/dan_layer/template_lib/src/args.rs @@ -0,0 +1,57 @@ +// 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_template_abi::{Decode, Encode}; + +use crate::models::{ComponentId, ContractAddress, PackageId}; + +#[derive(Debug, Clone, Encode, Decode)] +pub struct EmitLogArg { + pub message: String, + pub level: LogLevel, +} + +#[derive(Debug, Clone, Encode, Decode)] +pub enum LogLevel { + Error, + Warn, + Info, + Debug, +} + +#[derive(Debug, Clone, Encode, Decode)] +pub struct CreateComponentArg { + pub contract_address: ContractAddress, + pub module_name: String, + pub package_id: PackageId, + pub state: Vec, +} + +#[derive(Debug, Clone, Encode, Decode)] +pub struct GetComponentArg { + pub component_id: ComponentId, +} + +#[derive(Debug, Clone, Encode, Decode)] +pub struct SetComponentStateArg { + pub component_id: ComponentId, + pub state: Vec, +} diff --git a/dan_layer/template_lib/src/context.rs b/dan_layer/template_lib/src/context.rs index d786b4c963..5553cf9e74 100644 --- a/dan_layer/template_lib/src/context.rs +++ b/dan_layer/template_lib/src/context.rs @@ -22,25 +22,21 @@ use std::{borrow::Borrow, cell::RefCell}; -use tari_template_abi::CallInfo; -use tari_template_types::models::{Contract, Package}; +use tari_template_abi::{decode, CallInfo}; + +use crate::{ + abi_context::AbiContext, + models::{Contract, Package}, +}; thread_local! { static CONTEXT: RefCell> = RefCell::new(None); } -#[derive(Debug)] -pub struct AbiContext { - package: Package, - contract: Contract, -} - pub fn set_context_from_call_info(call_info: &CallInfo) { + let abi_context = decode(&call_info.abi_context).expect("Failed to decode ABI context"); with_context(|ctx| { - *ctx = Some(AbiContext { - package: call_info.package.clone(), - contract: call_info.contract.clone(), - }); + *ctx = Some(abi_context); }); } diff --git a/dan_layer/template_lib/src/engine.rs b/dan_layer/template_lib/src/engine.rs index 04829abdf6..111c49e946 100644 --- a/dan_layer/template_lib/src/engine.rs +++ b/dan_layer/template_lib/src/engine.rs @@ -20,22 +20,15 @@ // 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_template_abi::{ - call_engine, - decode, - encode, +use tari_template_abi::{call_engine, decode, encode, Decode, Encode}; + +use crate::{ + args::{CreateComponentArg, EmitLogArg, GetComponentArg, LogLevel, SetComponentStateArg}, + context::Context, + get_context, + models::{Component, ComponentId}, ops::*, - CreateComponentArg, - Decode, - EmitLogArg, - Encode, - GetComponentArg, - LogLevel, - SetComponentStateArg, }; -use tari_template_types::models::{Component, ComponentId}; - -use crate::{context::Context, get_context}; pub fn engine() -> TariEngine { // TODO: I expect some thread local state to be included here diff --git a/dan_layer/template_types/src/hash.rs b/dan_layer/template_lib/src/hash.rs similarity index 95% rename from dan_layer/template_types/src/hash.rs rename to dan_layer/template_lib/src/hash.rs index b606fea1f0..ddc3bbb869 100644 --- a/dan_layer/template_types/src/hash.rs +++ b/dan_layer/template_lib/src/hash.rs @@ -28,7 +28,7 @@ use std::{ ops::Deref, }; -use borsh::{BorshDeserialize, BorshSerialize}; +use tari_template_abi::{Decode, Encode}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] #[cfg_attr(feature = "serde", derive(serde::Deserialize))] @@ -72,15 +72,15 @@ impl Deref for Hash { } } -impl BorshSerialize for Hash { +impl Encode for Hash { fn serialize(&self, writer: &mut W) -> io::Result<()> { self.0.serialize(writer) } } -impl BorshDeserialize for Hash { +impl Decode for Hash { fn deserialize(buf: &mut &[u8]) -> io::Result { - let hash = <[u8; 32] as BorshDeserialize>::deserialize(buf)?; + let hash = <[u8; 32] as Decode>::deserialize(buf)?; Ok(Hash(hash)) } } diff --git a/dan_layer/template_lib/src/lib.rs b/dan_layer/template_lib/src/lib.rs index 56cc3d14f6..92220699d5 100644 --- a/dan_layer/template_lib/src/lib.rs +++ b/dan_layer/template_lib/src/lib.rs @@ -19,9 +19,27 @@ // 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. +pub mod abi_context; +mod hash; +pub use hash::Hash; + +pub mod args; +pub mod models; +pub mod ops; + +// ---------------------------------------- WASM target exports ------------------------------------------------ + +#[cfg(target_arch = "wasm32")] +pub mod template_dependencies; + +#[cfg(target_arch = "wasm32")] mod context; +#[cfg(target_arch = "wasm32")] pub use context::{get_context, set_context_from_call_info}; +#[cfg(target_arch = "wasm32")] mod engine; + +#[cfg(target_arch = "wasm32")] pub use engine::engine; diff --git a/dan_layer/template_lib/src/models/bucket.rs b/dan_layer/template_lib/src/models/bucket.rs new file mode 100644 index 0000000000..ebd2b46180 --- /dev/null +++ b/dan_layer/template_lib/src/models/bucket.rs @@ -0,0 +1,36 @@ +// 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::marker::PhantomData; + +pub type BucketId = u32; + +pub struct Bucket { + id: BucketId, + _t: PhantomData, +} + +impl Bucket { + pub fn id(&self) -> BucketId { + self.id + } +} diff --git a/dan_layer/template_types/src/models/component.rs b/dan_layer/template_lib/src/models/component.rs similarity index 93% rename from dan_layer/template_types/src/models/component.rs rename to dan_layer/template_lib/src/models/component.rs index 0953547e3e..933afb5628 100644 --- a/dan_layer/template_types/src/models/component.rs +++ b/dan_layer/template_lib/src/models/component.rs @@ -20,13 +20,13 @@ // 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 borsh::{BorshDeserialize, BorshSerialize}; +use tari_template_abi::{Decode, Encode}; use crate::models::{ContractAddress, PackageId}; pub type ComponentId = crate::Hash; -#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)] +#[derive(Debug, Clone, Encode, Decode)] pub struct ComponentInstance { pub component_id: ComponentId, pub contract_address: ContractAddress, @@ -34,6 +34,7 @@ pub struct ComponentInstance { pub module_name: String, pub state: Vec, } + impl ComponentInstance { pub fn new(component_id: ComponentId, component: Component) -> Self { Self { @@ -50,7 +51,7 @@ impl ComponentInstance { } } -#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)] +#[derive(Debug, Clone, Encode, Decode)] pub struct Component { pub contract_address: ContractAddress, pub package_id: PackageId, diff --git a/dan_layer/template_types/src/models/contract.rs b/dan_layer/template_lib/src/models/contract.rs similarity index 94% rename from dan_layer/template_types/src/models/contract.rs rename to dan_layer/template_lib/src/models/contract.rs index d56502a23a..a03e25444e 100644 --- a/dan_layer/template_types/src/models/contract.rs +++ b/dan_layer/template_lib/src/models/contract.rs @@ -20,11 +20,11 @@ // 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 borsh::{BorshDeserialize, BorshSerialize}; +use tari_template_abi::{Decode, Encode}; pub type ContractAddress = crate::Hash; -#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)] +#[derive(Debug, Clone, Encode, Decode)] pub struct Contract { pub address: ContractAddress, } diff --git a/dan_layer/template_types/src/models/mod.rs b/dan_layer/template_lib/src/models/mod.rs similarity index 92% rename from dan_layer/template_types/src/models/mod.rs rename to dan_layer/template_lib/src/models/mod.rs index 2b7bad6fa4..b6aaf2bb08 100644 --- a/dan_layer/template_types/src/models/mod.rs +++ b/dan_layer/template_lib/src/models/mod.rs @@ -20,11 +20,20 @@ // 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. +mod bucket; +pub use bucket::{Bucket, BucketId}; + mod component; pub use component::*; mod contract; pub use contract::{Contract, ContractAddress}; +mod resource; +pub use resource::ResourceAddress; + mod package; pub use package::{Package, PackageId}; + +mod vault; +pub use vault::Vault; diff --git a/dan_layer/template_types/src/models/package.rs b/dan_layer/template_lib/src/models/package.rs similarity index 94% rename from dan_layer/template_types/src/models/package.rs rename to dan_layer/template_lib/src/models/package.rs index 79deb3c3b9..c176111549 100644 --- a/dan_layer/template_types/src/models/package.rs +++ b/dan_layer/template_lib/src/models/package.rs @@ -20,13 +20,13 @@ // 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 borsh::{BorshDeserialize, BorshSerialize}; +use tari_template_abi::{Decode, Encode}; use crate::Hash; pub type PackageId = Hash; -#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)] +#[derive(Debug, Clone, Encode, Decode)] pub struct Package { pub id: PackageId, } diff --git a/dan_layer/template_lib/src/models/resource.rs b/dan_layer/template_lib/src/models/resource.rs new file mode 100644 index 0000000000..41c3730e72 --- /dev/null +++ b/dan_layer/template_lib/src/models/resource.rs @@ -0,0 +1,55 @@ +// 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::marker::PhantomData; + +use crate::{hash::HashParseError, Hash}; + +#[derive(Debug)] +pub struct ResourceAddress { + address: Hash, + _t: PhantomData, +} + +impl ResourceAddress { + // pub fn descriptor(&self) -> (Hash, UniversalTypeId) { + // (self.address, T::universal_type_id()) + // } + + pub fn from_hex(s: &str) -> Result { + Ok(ResourceAddress { + address: Hash::from_hex(s)?, + _t: PhantomData, + }) + } +} + +impl Clone for ResourceAddress { + fn clone(&self) -> Self { + Self { + address: self.address, + _t: PhantomData, + } + } +} + +impl Copy for ResourceAddress {} diff --git a/dan_layer/template_lib/src/models/vault.rs b/dan_layer/template_lib/src/models/vault.rs new file mode 100644 index 0000000000..172c3de485 --- /dev/null +++ b/dan_layer/template_lib/src/models/vault.rs @@ -0,0 +1,56 @@ +// 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_template_abi::{Decode, Encode}; + +use crate::models::{Bucket, ResourceAddress}; + +#[derive(Clone, Debug, Decode, Encode)] +pub struct Vault { + resource_address: ResourceAddress, +} + +impl Vault { + pub fn new(resource_address: ResourceAddress) -> Self { + // Call to call_engine will rather be in the ResourceBuilder/VaultBuilder, and the resulting address passed in + // here. let resource_address = call_engine(OP_RESOURCE_INVOKE, ResourceInvoke { + // resource_ref: ResourceRef::Vault, + // action: ResourceAction::Create, + // args: args![], + // }); + + Self { resource_address } + } + + pub fn put(&mut self, _bucket: Bucket) { + // let _ok: () = call_engine(OP_RESOURCE_INVOKE, ResourceInvoke { + // resource_ref: ResourceRef::VaultRef(self.resource_address()), + // action: ResourceAction::Put, + // args: args![bucket], + // }); + todo!() + } + + pub fn resource_address(&self) -> ResourceAddress { + self.resource_address + } +} diff --git a/dan_layer/template_abi/src/ops.rs b/dan_layer/template_lib/src/ops.rs similarity index 97% rename from dan_layer/template_abi/src/ops.rs rename to dan_layer/template_lib/src/ops.rs index 402489cf07..67fcae63d8 100644 --- a/dan_layer/template_abi/src/ops.rs +++ b/dan_layer/template_lib/src/ops.rs @@ -24,3 +24,4 @@ pub const OP_EMIT_LOG: i32 = 0x00; pub const OP_CREATE_COMPONENT: i32 = 0x01; pub const OP_GET_COMPONENT: i32 = 0x02; pub const OP_SET_COMPONENT_STATE: i32 = 0x03; +pub const OP_RESOURCE_INVOKE: i32 = 0x04; diff --git a/dan_layer/template_lib/src/template_dependencies.rs b/dan_layer/template_lib/src/template_dependencies.rs new file mode 100644 index 0000000000..2057b2ec91 --- /dev/null +++ b/dan_layer/template_lib/src/template_dependencies.rs @@ -0,0 +1,33 @@ +// 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. + +//! Public types that are available to all template authors. + +pub use borsh; +pub use tari_template_abi::{call_debug as debug, Decode, Encode}; + +pub use crate::{ + args::LogLevel, + engine, + get_context as context, + models::{Bucket, BucketId, ResourceAddress, Vault}, +}; diff --git a/dan_layer/template_macros/Cargo.toml b/dan_layer/template_macros/Cargo.toml index 6fa508deb0..b7bfa3da3d 100644 --- a/dan_layer/template_macros/Cargo.toml +++ b/dan_layer/template_macros/Cargo.toml @@ -9,11 +9,9 @@ edition = "2021" proc-macro = true [dependencies] -tari_template_abi = { path = "../template_abi" } - -syn = { version = "1.0.98", features = ["full"] } proc-macro2 = "1.0.42" quote = "1.0.20" +syn = { version = "1.0.98", features = ["full"] } [dev-dependencies] indoc = "1.0.6" \ No newline at end of file diff --git a/dan_layer/template_macros/src/template/dependencies.rs b/dan_layer/template_macros/src/template/dependencies.rs index eac8b52101..706cc11431 100644 --- a/dan_layer/template_macros/src/template/dependencies.rs +++ b/dan_layer/template_macros/src/template/dependencies.rs @@ -26,7 +26,6 @@ use quote::quote; /// Returns code that contains global functions and types implicitly available to contract authors pub fn generate_dependencies() -> TokenStream { quote! { - use tari_template_abi::{borsh, call_debug as debug, Decode, Encode}; - use tari_template_lib::{engine, get_context as context}; + use tari_template_lib::template_dependencies::*; } } diff --git a/dan_layer/template_macros/src/template/dispatcher.rs b/dan_layer/template_macros/src/template/dispatcher.rs index 21f5c4c83d..44b57174f7 100644 --- a/dan_layer/template_macros/src/template/dispatcher.rs +++ b/dan_layer/template_macros/src/template/dispatcher.rs @@ -34,7 +34,7 @@ pub fn generate_dispatcher(ast: &TemplateAst) -> Result { let output = quote! { #[no_mangle] pub extern "C" fn #dispatcher_function_name(call_info: *mut u8, call_info_len: usize) -> *mut u8 { - use ::tari_template_abi::{decode, encode_with_len, CallInfo, LogLevel, wrap_ptr}; + use ::tari_template_abi::{decode, encode_with_len, CallInfo, wrap_ptr}; use ::tari_template_lib::set_context_from_call_info; if call_info.is_null() { @@ -92,7 +92,7 @@ fn get_function_block(template_ident: &Ident, ast: FunctionAst) -> Expr { vec![ parse_quote! { let component = - decode::<::tari_template_types::models::ComponentInstance>(&call_info.args[#i]) + decode::<::tari_template_lib::models::ComponentInstance>(&call_info.args[#i]) .unwrap(); }, parse_quote! { diff --git a/dan_layer/template_macros/src/template/mod.rs b/dan_layer/template_macros/src/template/mod.rs index 110f1305bf..6bda44fce0 100644 --- a/dan_layer/template_macros/src/template/mod.rs +++ b/dan_layer/template_macros/src/template/mod.rs @@ -94,8 +94,7 @@ mod tests { let output = generate_template(input).unwrap(); assert_code_eq(output, quote! { - use tari_template_abi::{borsh, call_debug as debug, Decode, Encode}; - use tari_template_lib::{engine, get_context as context}; + use tari_template_lib::public_template_deps::*; pub mod template { use super::*; diff --git a/dan_layer/template_types/Cargo.toml b/dan_layer/template_types/Cargo.toml deleted file mode 100644 index 1f5f66dd42..0000000000 --- a/dan_layer/template_types/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "tari_template_types" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -borsh = "0.9.3" -serde = { version = "1.0.140", optional = true }