Giter Club home page Giter Club logo

Comments (4)

Robbepop avatar Robbepop commented on July 23, 2024

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.

Robbepop avatar Robbepop commented on July 23, 2024

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.

Robbepop avatar Robbepop commented on July 23, 2024

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.

Robbepop avatar Robbepop commented on July 23, 2024

Implemented by c262f4d. Closed.

from ink.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.