Просмотр исходного кода

example: Add a dummy contract for easier iterations of runtime changes.

parazyd 2 лет назад
Родитель
Сommit
5945fc5f0d

+ 11 - 0
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"

+ 1 - 0
Cargo.toml

@@ -49,6 +49,7 @@ members = [
     "src/contract/deployooor",
 
     #"example/dchat",
+    "example/dummy-contract",
 ]
 
 [dependencies]

+ 30 - 0
example/dummy-contract/Cargo.toml

@@ -0,0 +1,30 @@
+[package]
+name = "darkfi_dummy_contract"
+version = "0.4.1"
+authors = ["Dyne.org foundation <foundation@dyne.org>"]
+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 = []

+ 11 - 0
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
+```

+ 43 - 0
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 <https://www.gnu.org/licenses/>.
+ */
+
+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(())
+}

+ 21 - 0
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 <https://www.gnu.org/licenses/>.
+ */
+
+#[cfg(not(feature = "no-entrypoint"))]
+/// WASM entrypoint functions
+pub mod entrypoint;

+ 58 - 0
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 <https://www.gnu.org/licenses/>.
+ */
+
+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),
+    }
+}