From 5945fc5f0ded38329aa81a5aa0ba27415c533b43 Mon Sep 17 00:00:00 2001 From: parazyd Date: Mon, 27 Nov 2023 19:26:18 +0100 Subject: [PATCH] example: Add a dummy contract for easier iterations of runtime changes. --- Cargo.lock | 11 +++++ Cargo.toml | 1 + example/dummy-contract/Cargo.toml | 30 ++++++++++++ example/dummy-contract/README.md | 11 +++++ example/dummy-contract/src/entrypoint.rs | 43 ++++++++++++++++++ example/dummy-contract/src/lib.rs | 21 +++++++++ example/dummy-contract/src/runtime.rs | 58 ++++++++++++++++++++++++ 7 files changed, 175 insertions(+) create mode 100644 example/dummy-contract/Cargo.toml create mode 100644 example/dummy-contract/README.md create mode 100644 example/dummy-contract/src/entrypoint.rs create mode 100644 example/dummy-contract/src/lib.rs create mode 100644 example/dummy-contract/src/runtime.rs diff --git a/Cargo.lock b/Cargo.lock index 0bbe14039..b0c24e4e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1876,6 +1876,17 @@ dependencies = [ "wasmparser 0.118.0", ] +[[package]] +name = "darkfi_dummy_contract" +version = "0.4.1" +dependencies = [ + "darkfi", + "darkfi-sdk", + "getrandom 0.2.11", + "simplelog", + "sled", +] + [[package]] name = "darkfi_money_contract" version = "0.4.1" diff --git a/Cargo.toml b/Cargo.toml index 9ca0549b6..b53b0a0c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,6 +49,7 @@ members = [ "src/contract/deployooor", #"example/dchat", + "example/dummy-contract", ] [dependencies] diff --git a/example/dummy-contract/Cargo.toml b/example/dummy-contract/Cargo.toml new file mode 100644 index 000000000..77ad77c25 --- /dev/null +++ b/example/dummy-contract/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "darkfi_dummy_contract" +version = "0.4.1" +authors = ["Dyne.org foundation "] +license = "AGPL-3.0-only" +edition = "2021" + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +darkfi-sdk = {path = "../../src/sdk"} + +# We need to disable random using "custom" which makes the crate a noop +# so the wasm32-unknown-unknown target is enabled. +[target.'cfg(target_arch = "wasm32")'.dependencies] +getrandom = { version = "0.2.8", features = ["custom"] } + +[dev-dependencies] +sled = "0.34.7" +darkfi = {path = "../../", features = ["wasm-runtime"]} +simplelog = "0.12.1" + +[[example]] +name = "runtime" +path = "src/runtime.rs" + +[features] +default = [] +no-entrypoint = [] diff --git a/example/dummy-contract/README.md b/example/dummy-contract/README.md new file mode 100644 index 000000000..f1b8cdd96 --- /dev/null +++ b/example/dummy-contract/README.md @@ -0,0 +1,11 @@ +# Build WASM + +``` +$ cargo +nightly build --target=wasm32-unknown-unknown --release --package darkfi_dummy_contract +``` + +# Run runtime.rs + +``` +$ cargo +nightly run --target=x86_64-unknown-linux-gnu --release --example runtime +``` diff --git a/example/dummy-contract/src/entrypoint.rs b/example/dummy-contract/src/entrypoint.rs new file mode 100644 index 000000000..a3369f83e --- /dev/null +++ b/example/dummy-contract/src/entrypoint.rs @@ -0,0 +1,43 @@ +/* This file is part of DarkFi (https://dark.fi) + * + * Copyright (C) 2020-2023 Dyne.org foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +use darkfi_sdk::{crypto::ContractId, error::ContractResult}; + +darkfi_sdk::define_contract!( + init: init_contract, + exec: process_instruction, + apply: process_update, + metadata: get_metadata +); + +fn init_contract(_cid: ContractId, _ix: &[u8]) -> ContractResult { + //panic!("ohai"); + Ok(()) +} + +fn get_metadata(_cid: ContractId, _ix: &[u8]) -> ContractResult { + Ok(()) +} + +fn process_instruction(_cid: ContractId, _ix: &[u8]) -> ContractResult { + Ok(()) +} + +fn process_update(_cid: ContractId, _ix: &[u8]) -> ContractResult { + Ok(()) +} diff --git a/example/dummy-contract/src/lib.rs b/example/dummy-contract/src/lib.rs new file mode 100644 index 000000000..a6a609330 --- /dev/null +++ b/example/dummy-contract/src/lib.rs @@ -0,0 +1,21 @@ +/* This file is part of DarkFi (https://dark.fi) + * + * Copyright (C) 2020-2023 Dyne.org foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#[cfg(not(feature = "no-entrypoint"))] +/// WASM entrypoint functions +pub mod entrypoint; diff --git a/example/dummy-contract/src/runtime.rs b/example/dummy-contract/src/runtime.rs new file mode 100644 index 000000000..af7b8ef5b --- /dev/null +++ b/example/dummy-contract/src/runtime.rs @@ -0,0 +1,58 @@ +/* This file is part of DarkFi (https://dark.fi) + * + * Copyright (C) 2020-2023 Dyne.org foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +use darkfi::{ + blockchain::{Blockchain, BlockchainOverlay}, + runtime::vm_runtime::Runtime, + util::time::{TimeKeeper, Timestamp}, +}; +use darkfi_sdk::{crypto::ContractId, pasta::pallas}; + +fn init_logger() { + let mut cfg = simplelog::ConfigBuilder::new(); + cfg.add_filter_ignore("sled".to_string()); + + simplelog::TermLogger::init( + simplelog::LevelFilter::Debug, + cfg.build(), + simplelog::TerminalMode::Mixed, + simplelog::ColorChoice::Auto, + ) + .unwrap(); +} + +fn main() { + init_logger(); + + let sled_db = sled::Config::new().temporary(true).open().unwrap(); + let blockchain = Blockchain::new(&sled_db).unwrap(); + let overlay = BlockchainOverlay::new(&blockchain).unwrap(); + + let timekeeper = TimeKeeper::new(Timestamp::current_time(), 10, 90, 0); + + let wasm_bytes = + include_bytes!("../../../target/wasm32-unknown-unknown/release/darkfi_dummy_contract.wasm"); + + let contract_id = ContractId::from(pallas::Base::from(69)); + + let mut runtime = Runtime::new(wasm_bytes, overlay, contract_id, timekeeper).unwrap(); + match runtime.deploy(&[]) { + Ok(()) => {} + Err(e) => println!("Error running deploy: {:#?}", e), + } +}