diff --git a/dan_layer/engine/src/runtime/impl.rs b/dan_layer/engine/src/runtime/impl.rs index 147b41b86..911b748ae 100644 --- a/dan_layer/engine/src/runtime/impl.rs +++ b/dan_layer/engine/src/runtime/impl.rs @@ -2345,9 +2345,9 @@ impl> RuntimeInte Ok(InvokeResult::encode(&address)?) } - fn publish_template(&self, template: &[u8]) -> Result<(), RuntimeError> { + fn publish_template(&self, template: Vec) -> Result<(), RuntimeError> { self.tracker.write_with(|state| { - let binary_hash = template_hasher32().chain(template).result(); + let binary_hash = template_hasher32().chain(&template).result(); let template_address = PublishedTemplateAddress::from_hash( hasher32(EngineHashDomainLabel::TemplateAddress) .chain(&self.transaction_signer_public_key) @@ -2356,9 +2356,7 @@ impl> RuntimeInte ); state.new_substate( template_address, - SubstateValue::Template(PublishedTemplate { - binary: template.to_vec(), - }), + SubstateValue::Template(PublishedTemplate { binary: template }), )?; let scope_mut = state.current_call_scope_mut()?; scope_mut.move_node_to_owned(&template_address.into())?; diff --git a/dan_layer/engine/src/runtime/mod.rs b/dan_layer/engine/src/runtime/mod.rs index d05504ff1..f8f630a12 100644 --- a/dan_layer/engine/src/runtime/mod.rs +++ b/dan_layer/engine/src/runtime/mod.rs @@ -181,7 +181,7 @@ pub trait RuntimeInterface: Send + Sync { fn push_call_frame(&self, frame: PushCallFrame) -> Result<(), RuntimeError>; fn pop_call_frame(&self) -> Result<(), RuntimeError>; - fn publish_template(&self, template: &[u8]) -> Result<(), RuntimeError>; + fn publish_template(&self, template: Vec) -> Result<(), RuntimeError>; } #[derive(Clone)] diff --git a/dan_layer/engine/src/transaction/processor.rs b/dan_layer/engine/src/transaction/processor.rs index 89599bb13..f6663c404 100644 --- a/dan_layer/engine/src/transaction/processor.rs +++ b/dan_layer/engine/src/transaction/processor.rs @@ -382,7 +382,7 @@ impl + 'static> T WasmModule::load_template_from_code(binary.as_slice())?; // creating new substate - runtime.interface().publish_template(binary.as_slice())?; + runtime.interface().publish_template(binary)?; Ok(InstructionResult::empty()) } diff --git a/dan_layer/engine/src/wasm/module.rs b/dan_layer/engine/src/wasm/module.rs index d4d422292..fac2436ae 100644 --- a/dan_layer/engine/src/wasm/module.rs +++ b/dan_layer/engine/src/wasm/module.rs @@ -87,6 +87,10 @@ impl WasmModule { &self.code } + pub fn into_code(self) -> Vec { + self.code + } + fn create_engine() -> Engine { const MEMORY_PAGE_LIMIT: Pages = Pages(32); // 2MiB = 32 * 65,536 let base = BaseTunables::for_target(&Target::default()); diff --git a/dan_layer/engine/tests/publish_template.rs b/dan_layer/engine/tests/publish_template.rs index 9efe38422..1a5d46b2e 100644 --- a/dan_layer/engine/tests/publish_template.rs +++ b/dan_layer/engine/tests/publish_template.rs @@ -1,6 +1,8 @@ // Copyright 2024 The Tari Project // SPDX-License-Identifier: BSD-3-Clause +use std::iter; + use rand::random; use tari_dan_engine::wasm::compile::compile_template; use tari_engine_types::{ @@ -29,7 +31,7 @@ fn publish_template_success() { let result = test.execute_expect_success( Transaction::builder() .fee_transaction_pay_from_component(account_address, Amount(200_000)) - .publish_template(template.code()) + .publish_template(template.code().to_vec()) .sign(&account_key) .build(), vec![owner_proof], @@ -62,7 +64,7 @@ fn publish_template_invalid_binary() { let result = test.execute_expect_failure( Transaction::builder() .fee_transaction_pay_from_component(account_address, Amount(200_000)) - .publish_template([1, 2, 3]) + .publish_template(vec![1, 2, 3]) .sign(&account_key) .build(), vec![owner_proof], @@ -103,10 +105,6 @@ fn publish_template_too_big_binary() { } } -fn generate_random_binary(size_in_bytes: u64) -> Vec { - let mut result = vec![]; - for _ in 1..=size_in_bytes { - result.push(random::()); - } - result +fn generate_random_binary(size_in_bytes: usize) -> Vec { + iter::repeat_with(random).take(size_in_bytes).collect() } diff --git a/dan_layer/template_test_tooling/templates/faucet/Cargo.lock b/dan_layer/template_test_tooling/templates/faucet/Cargo.lock index 0c0ba9d29..40ecd9982 100644 --- a/dan_layer/template_test_tooling/templates/faucet/Cargo.lock +++ b/dan_layer/template_test_tooling/templates/faucet/Cargo.lock @@ -25,9 +25,9 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "base64" -version = "0.13.1" +version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" [[package]] name = "bumpalo" @@ -138,6 +138,12 @@ dependencies = [ "serde", ] +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "faucet2" version = "0.1.0" @@ -163,6 +169,12 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "hashbrown" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" + [[package]] name = "hex" version = "0.4.3" @@ -205,7 +217,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg", - "hashbrown", + "hashbrown 0.12.3", + "serde", +] + +[[package]] +name = "indexmap" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" +dependencies = [ + "equivalent", + "hashbrown 0.15.2", "serde", ] @@ -242,6 +265,12 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d36047f46c69ef97b60e7b069a26ce9a15cd8a7852eddb6991ea94a83ba36a78" +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + [[package]] name = "num-traits" version = "0.2.17" @@ -320,15 +349,17 @@ dependencies = [ [[package]] name = "serde_with" -version = "2.3.3" +version = "3.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07ff71d2c147a7b57362cead5e22f772cd52f6ab31cfcd9edcd7f6aeb2a0afbe" +checksum = "8e28bdad6db2b8340e449f7108f020b3b092e8583a9e3fb82713e1d4e71fe817" dependencies = [ "base64", "chrono", "hex", - "indexmap", + "indexmap 1.9.3", + "indexmap 2.7.0", "serde", + "serde_derive", "serde_json", "serde_with_macros", "time", @@ -336,9 +367,9 @@ dependencies = [ [[package]] name = "serde_with_macros" -version = "2.3.3" +version = "3.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "881b6f881b17d13214e5d494c939ebab463d01264ce1811e9d4ac3a882e7695f" +checksum = "9d846214a9854ef724f3da161b426242d8de7c1fc7de2f89bb1efcb154dca79d" dependencies = [ "darling", "proc-macro2", @@ -416,12 +447,13 @@ dependencies = [ [[package]] name = "time" -version = "0.3.31" +version = "0.3.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f657ba42c3f86e7680e53c8cd3af8abbe56b5491790b46e22e19c0d57463583e" +checksum = "35e7868883861bd0e56d9ac6efcaaca0d6d5d82a2a7ec8209ff492c07cf37b21" dependencies = [ "deranged", "itoa", + "num-conv", "powerfmt", "serde", "time-core", @@ -436,10 +468,11 @@ checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" [[package]] name = "time-macros" -version = "0.2.16" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26197e33420244aeb70c3e8c78376ca46571bc4e701e4791c2cd9f57dcb3a43f" +checksum = "2834e6017e3e5e4b9834939793b282bc03b37a3336245fa820e35e233e2a85de" dependencies = [ + "num-conv", "time-core", ] diff --git a/dan_layer/transaction/src/builder.rs b/dan_layer/transaction/src/builder.rs index b28cd12a8..3669370f6 100644 --- a/dan_layer/transaction/src/builder.rs +++ b/dan_layer/transaction/src/builder.rs @@ -135,10 +135,8 @@ impl TransactionBuilder { } /// Publishing a WASM template. - pub fn publish_template>(self, binary: T) -> Self { - self.add_instruction(Instruction::PublishTemplate { - binary: binary.as_ref().to_vec(), - }) + pub fn publish_template(self, binary: Vec) -> Self { + self.add_instruction(Instruction::PublishTemplate { binary }) } pub fn claim_burn(self, claim: ConfidentialClaim) -> Self {