Mock runtime provides a host that can used in integration & unit testing of kernels for Tezos Smart Rollups.
To do so, [MockHost
] implements the [SmartRollupCore
] trait - allowing
it to be passed as an argument to any function, where the argument is
required to implement either SmartRollupCore
or Runtime
.
Take a simple kernel that will count the number of times it is called, rebooting at most 500 times per level.
``` use tezossmartrollupmock::MockHost; use tezossmartrolluphost::runtime::Runtime; use tezossmartrollup_host::path::RefPath;
const COUNTERPATH: RefPath = RefPath::assertfrom(b"/counter");
const COUNTERSIZE: usize = core::mem::sizeof::
// Kernel entrypoint, to count the number of times called.
// After 1000 calls at a given level, will restart at the
// next level.
fn countcalls
counter += 1;
// A kernel can reboot up to 1000 times per level. if counter % 1000 != 0 { host.markforreboot().expect("Marking for reboot failed."); }
host.storewrite(&COUNTERPATH, &counter.tolebytes(), 0) .expect("Failed to write counter to storage.") }
// Reads COUNTER_PATH
as u32
in little-endian.
// - returns 0 if counter does not exist.
fn readcounter(host: &impl Runtime) -> u32 {
host.storeread(&COUNTERPATH, 0, COUNTERSIZE)
.map(|bytes| bytes.tryinto().unwrapordefault())
.map(u32::fromlebytes)
.unwrapor_default()
}
// Run using mock host - every run_level
should increment
// counter by 500.
let mut host = MockHost::default();
asserteq!(0, readcounter(&host));
host.runlevel(countcalls); asserteq!(1000, readcounter(&host));
host.runlevel(countcalls); asserteq!(2000, readcounter(&host)); ```