What's Changed
Version 0.8.0 of the Stylus SDK brings a brand new stylus_test
crate providing a testing framework for all Stylus contracts and storage types. This crate is re-exported through the Stylus SDK under stylus_sdk::testing
when targeting a native architecture. A new Host
trait is defined in an internal stylus_core
crate that defines all the hostio affordances Stylus contracts have. All global hostio invocations, such as stylus_sdk::msg::value()
are now deprecated in favor of invoking hostios through a Host
access available to contracts.
The Stylus #[storage]
proc macro makes a .vm()
method available on all contracts which offers all hostio methods. Moreover, a stylus_sdk::testing
crate implements a test host for use in unit testing:
use stylus_sdk::{alloy_primitives::U256, prelude::*};
#[entrypoint]
#[storage]
pub struct Counter {
number: StorageU256;
}
#[public]
impl Counter {
pub fn number(&self) -> U256 {
self.number.get()
}
pub fn increment(&mut self) {
let number = self.number.get();
self.set_number(number + U256::from(1));
}
}
#[cfg(test)]
mod test {
use super::*;
use stylus_sdk::testing::*;
#[test]
fn test_counter() {
let vm = TestVM::default();
let mut contract = Counter::from(&vm);
assert_eq!(U256::ZERO, contract.number());
contract.increment();
assert_eq!(U256::from(1), contract.number());
}
}
Caveats
- All the code related to cross-contract calls is not yet updated to use the new
Host
trait, meaning it cannot be fully tested via the testing framework. This is because changing calls requires breaking changes, and will be done in the 1.0 release candidate for the SDK in the near future - In order to leverage and unit test contracts with
stylus_test
, contracts must use hostio access via the.vm()
accessor instead of global hostio invocations, which are now deprecated
- [1/5] - Define a Host Trait for the Stylus SDK by @rauljordan in #199
- [2/5] - Define Initial WasmHost Implementation for Wasm Targets by @rauljordan in #200
- [3/5] - Use a Boxed, Dyn Host Trait for Contract Initialization by @rauljordan in #203
- [4/?] - Define Testing VM to Implement the Mock Trait by @rauljordan in #204
- [5/?] - Rename Stylus-Host to Stylus-Core and Make Calls Part of the VM by @rauljordan in #206
- [6/?] - Make Deployment Logic Part of the Host Trait by @rauljordan in #207
- Fix storage bytes set-len when shrinking by @gligneul in #211
- [7] - Deprecate Old Hostios and Improve TestVM Ergonomics by @rauljordan in #209
- Add unit tests to storage bytes by @gligneul in #213
- [8] - Add Missing Methods to Host Trait by @rauljordan in #210
- Reduce Wasm Code Size Impact of Host Trait by @rauljordan in #216
- Add a Powerful Test VM by @rauljordan in #212
- Minimize calls to storage for bytes/string by @gligneul in #217
- Fix examples and doctest by @gligneul in #219
- Make CI fail for clippy warnings by @gligneul in #220
- v0.8.0 Release Candidate by @rauljordan in #218
Full Changelog: v0.7.0...v0.8.0