tMove example usage to runtime's test unit. - wasm-runtime - A wasm runtime
HTML git clone https://git.parazyd.org/wasm-runtime
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
DIR commit 50353e36fa0c557b852b2502e22360f40e3e1390
DIR parent 5a6e11812efc0b8c7c85d0cee41541d343bea5e6
HTML Author: parazyd <parazyd@dyne.org>
Date: Wed, 9 Mar 2022 16:30:33 +0100
Move example usage to runtime's test unit.
Diffstat:
M Cargo.toml | 4 ----
M Makefile | 7 ++++---
M README.md | 7 ++++---
D src/example.rs | 17 -----------------
M src/runtime.rs | 23 +++++++++++++++++++++++
5 files changed, 31 insertions(+), 27 deletions(-)
---
DIR diff --git a/Cargo.toml b/Cargo.toml
t@@ -28,7 +28,3 @@ smart-contract = { path = "./smart-contract" }
git = "https://github.com/parazyd/pasta_curves"
branch = "optional-borsh-support"
features = ["borsh"]
-
-[[example]]
-name = "runner"
-path = "src/example.rs"
DIR diff --git a/Makefile b/Makefile
t@@ -1,3 +1,5 @@
+.POSIX:
+
SRC = \
$(shell find src -type f) \
$(shell find smart-contract -type f) \
t@@ -8,7 +10,7 @@ CARGO = cargo
DEPS = smart_contract.wasm
all: $(DEPS)
- $(CARGO) run --release --example runner
+ $(CARGO) test --release --lib -- --nocapture
wabt:
git clone --recursive https://github.com/WebAssembly/wabt $@
t@@ -18,5 +20,4 @@ smart_contract.wasm: $(SRC)
cd smart-contract && $(CARGO) build --release --lib --target wasm32-unknown-unknown
cp -f smart-contract/target/wasm32-unknown-unknown/release/$@ $@
-test:
- $(CARGO) test --release --lib
+.PHONY: all
DIR diff --git a/README.md b/README.md
t@@ -8,6 +8,7 @@ $ rustup target add wasm32-unknown-unknown
$ make
```
-* Smart contract is in `smart-contract/src/lib.rs`
-* Contract helpers are in `drk-sdk/src`
-* wasm runtime is in `src`
+* Smart contract is in [`smart-contract/src/lib.rs`](smart-contract/src/lib.rs)
+* Contract helpers are in [`drk-sdk/src`](drk-sdk/src)
+* wasm runtime is in [`src`](src)
+* Example usage is in [`src/runtime.rs`](src/runtime.rs) at the bottom
DIR diff --git a/src/example.rs b/src/example.rs
t@@ -1,17 +0,0 @@
-use anyhow::Result;
-use borsh::BorshSerialize;
-use pasta_curves::pallas;
-use wasm_runtime::{runtime::Runtime, util::serialize_payload};
-
-use smart_contract::Args;
-
-fn main() -> Result<()> {
- let wasm_bytes = std::fs::read("smart_contract.wasm")?;
- let mut runtime = Runtime::new(&wasm_bytes)?;
-
- let args = Args { a: pallas::Base::from(777), b: pallas::Base::from(666) };
- let payload = args.try_to_vec()?;
- let input = serialize_payload(&payload);
-
- runtime.run(&input)
-}
DIR diff --git a/src/runtime.rs b/src/runtime.rs
t@@ -162,3 +162,26 @@ impl Runtime {
Ok(self.instance.exports.get_memory(MEMORY)?)
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::util::serialize_payload;
+
+ use borsh::BorshSerialize;
+ use pasta_curves::pallas;
+ use smart_contract::Args;
+
+ #[test]
+ fn run_contract() -> Result<()> {
+ let wasm_bytes = std::fs::read("smart_contract.wasm")?;
+ let mut runtime = Runtime::new(&wasm_bytes)?;
+
+ let args = Args { a: pallas::Base::from(777), b: pallas::Base::from(666) };
+ let payload = args.try_to_vec()?;
+
+ let input = serialize_payload(&payload);
+
+ runtime.run(&input)
+ }
+}