mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-01-09 22:57:59 -05:00
example: Add a dummy contract for easier iterations of runtime changes.
This commit is contained in:
11
Cargo.lock
generated
11
Cargo.lock
generated
@@ -1876,6 +1876,17 @@ dependencies = [
|
|||||||
"wasmparser 0.118.0",
|
"wasmparser 0.118.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "darkfi_dummy_contract"
|
||||||
|
version = "0.4.1"
|
||||||
|
dependencies = [
|
||||||
|
"darkfi",
|
||||||
|
"darkfi-sdk",
|
||||||
|
"getrandom 0.2.11",
|
||||||
|
"simplelog",
|
||||||
|
"sled",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "darkfi_money_contract"
|
name = "darkfi_money_contract"
|
||||||
version = "0.4.1"
|
version = "0.4.1"
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ members = [
|
|||||||
"src/contract/deployooor",
|
"src/contract/deployooor",
|
||||||
|
|
||||||
#"example/dchat",
|
#"example/dchat",
|
||||||
|
"example/dummy-contract",
|
||||||
]
|
]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
30
example/dummy-contract/Cargo.toml
Normal file
30
example/dummy-contract/Cargo.toml
Normal file
@@ -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
example/dummy-contract/README.md
Normal file
11
example/dummy-contract/README.md
Normal file
@@ -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
example/dummy-contract/src/entrypoint.rs
Normal file
43
example/dummy-contract/src/entrypoint.rs
Normal file
@@ -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
example/dummy-contract/src/lib.rs
Normal file
21
example/dummy-contract/src/lib.rs
Normal file
@@ -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
example/dummy-contract/src/runtime.rs
Normal file
58
example/dummy-contract/src/runtime.rs
Normal file
@@ -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),
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user