commit 50353e36fa0c557b852b2502e22360f40e3e1390
parent 5a6e11812efc0b8c7c85d0cee41541d343bea5e6
Author: parazyd <parazyd@dyne.org>
Date: Wed, 9 Mar 2022 16:30:33 +0100
Move example usage to runtime's test unit.
Diffstat:
5 files changed, 31 insertions(+), 27 deletions(-)
diff --git a/Cargo.toml b/Cargo.toml
@@ -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"
diff --git a/Makefile b/Makefile
@@ -1,3 +1,5 @@
+.POSIX:
+
SRC = \
$(shell find src -type f) \
$(shell find smart-contract -type f) \
@@ -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 $@
@@ -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
diff --git a/README.md b/README.md
@@ -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
diff --git a/src/example.rs b/src/example.rs
@@ -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)
-}
diff --git a/src/runtime.rs b/src/runtime.rs
@@ -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)
+ }
+}