Comments (4)
As an extension to this we might also want to think about supporting the actual TestEnv
through this so that users are even more powerful at testing their smart contracts off-chain.
from ink.
We can do this by using impl
return type in the following way.
Instead of generating what can be seen here:
We do the following:
#[doc(hidden)]
mod hidden {
fn instantiate_impl() -> impl TestableContract<DeployArgs = u32> {
ContractDecl::using::<Adder>()
.on_deploy(|env, init_val| {
env.state.val.set(init_val)
})
.on_msg_mut::<Inc>(|env, by| {
env.state.val += by
})
.on_msg::<Get>(|env, _| {
*env.state.val
})
.instantiate()
}
}
#[cfg(not(test))]
fn instantiate() -> impl Contract {
hidden::instantiate_impl()
}
#[cfg(test)]
fn instantiate() -> impl TestableContract<DeployArgs = u32> {
hidden::instantiate_impl()
}
from ink.
The current design for testable pdsl_lang
smart contracts is going the following path:
The goal is to be able to write tests in the following way:
#![no_std]
use pdsl_core::storage;
use pdsl_lang::contract;
contract! {
/// This simple dummy contract has a `bool` value that can
/// alter between `true` and `false` using the `flip` message.
/// Users can retrieve its current state using the `get` message.
struct Flipper {
/// The current state of our flag.
value: storage::Value<bool>,
}
impl Deploy for Flipper {
/// Initializes our state to `false` upon deploying our smart contract.
fn deploy(&mut self) {
self.value.set(false)
}
}
impl Flipper {
/// Flips the current state of our smart contract.
pub(external) fn flip(&mut self) {
if *self.value {
self.value.set(false)
} else {
self.value.set(true)
}
}
/// Returns the current state.
pub(external) fn get(&self) -> bool {
*self.value
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let mut flipper = Flipper::instantiate();
assert_eq!(flipper.get(), false);
flipper.flip();
assert_eq!(flipper.get(), true);
}
}
With this model a user does not have to learn psdl_model
data structures and can directly test what they have written themselves. For this we generate a smart contract wrapper that mirrors the interface of the actual contract implementation so that a user can test against this interface.
For the Flipper
smart contract this could look like the following:
pdsl_model::state! {
/// This simple dummy contract has a `bool` value that can
/// alter between `true` and `false` using the `flip` message.
/// Users can retrieve its current state using the `get` message.
struct Flipper {
/// The current state of our flag.
value: storage::Value<bool>,
}
}
use pdsl_model::messages;
pdsl_model::messages! {
/// Flips the current state of our smart contract.
0 => Flip();
/// Returns the current state.
1 => Get() -> bool;
}
#[cfg(test)]
impl Flipper {
/// Returns a testable version of the contract.
pub fn instantiate() -> TestableFlipper {
TestableFlipper::instantiate()
}
}
impl Flipper {
/// Initializes our state to `false` upon deploying our smart contract.
pub fn deploy(&mut self, env: &mut pdsl_model::EnvHandler) {
self.value.set(false)
}
/// Flips the current state of our smart contract.
pub fn flip(&mut self, env: &mut pdsl_model::EnvHandler) {
if *self.value {
*self.value = false
} else {
*self.value = true
}
}
/// Returns the current state.
pub fn get(&self, env: &pdsl_model::EnvHandler) -> bool {
*self.value
}
}
impl Flipper {}
use pdsl_model::Contract;
#[cfg(test)]
struct TestableFlipper {
env: pdsl_model::ExecutionEnv<Flipper>,
}
#[cfg(test)]
impl TestableFlipper {
pub fn instantiate() -> Self {
Self {
env: unsafe {
let mut alloc = BumpAlloc::from_raw_parts(Key([0x0; 32]));
AllocateUsing::allocate_using(&mut alloc)
}
}
}
}
#[cfg(test)]
impl TestableFlipper {
/// Automatically called when the contract is deployed.
pub fn deploy(&mut self, init_value: u32) {
let (handler, state) = self.env.split_mut();
state.deploy(handler, init_value)
}
}
#[cfg(test)]
impl TestableFlipper {
/// Flips the current state of our smart contract.
pub fn flip(&mut self) {
let (handler, state) = self.env.split_mut();
state.flip(handler, by)
}
/// Returns the current state.
pub fn get(&self) -> bool {
let (handler, state) = self.env.split();
state.get(handler,)
}
}
#[cfg(not(test))]
fn instantiate() -> impl pdsl_model::Contract {
pdsl_model::ContractDecl::using::<Flipper>()
.on_deploy(|env| {
let (handler, state) = env.split_mut();
state.deploy(handler, init_value)
})
.on_msg_mut::<Flip>(|env, _| {
let (handler, state) = env.split_mut();
state.flip(handler, by)
})
.on_msg::<Get>(|env, _| {
let (handler, state) = env.split();
state.get(handler,)
})
.instantiate()
}
#[cfg(not(test))] #[no_mangle] fn deploy() { instantiate().deploy() }
#[cfg(not(test))] #[no_mangle] fn call() { instantiate().dispatch() }
from ink.
Implemented by c262f4d. Closed.
from ink.
Related Issues (20)
- The function `set_contract_storage` in integration test it's missing a size check.
- Linter: Unprotected `terminate_contract`
- Linter: Unprotected `DelegateCall` call
- Immutable contracts allowed to change state via `env` HOT 4
- Add a lint for missing `no_main`
- IIP-3: Allow messages without `&self` receiver; opt out of automatic storage loading HOT 5
- Different gas value for `weight_to_fee()` in ink! integration and e2e tests HOT 6
- Split up `ink_linting` into two `dylint` libraries HOT 1
- Use standard Github's machines for test jobs
- Linter: Warn about using non-fallible APIs for `Lazy` and `Mapping` HOT 1
- Support expressions for `extension` and `function` during chain extension definition
- Event defintions with generics
- Deprecation of "legacy" inline events HOT 1
- Unify `call-runtime` and `e2e-call-runtime` examples
- Linter: Dangereous `self.env().transferred_value()` pattern
- Linter: Divide before multiply
- Add support and integration test for `caller_is_root`
- Migrate ink! CI from organization runners to standard GitHub runners
- Add links to linter warning details on use.ink
- Cross contract calling produces unsupported code HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ink.