rusk-vm icon indicating copy to clipboard operation
rusk-vm copied to clipboard

Specify userspace smart contract API

Open ureeves opened this issue 2 years ago • 1 comments

Describe what kind of specification you want to have created The userspace API for writing smart contracts needs to be specified. We've chosen a language to support writing contracts in, but since Rust is a general purpose systems language it requires some additions to ease complying to the smart contract specification.

Such additions should allow the smart contract developer to easily target the VM, while not unduly restricting the possibilities for structuring the code representing the contract.

Ideally, this would look and function in much the same way as normal Rust code, while enforcing the requirements for compliance with the VM.

ureeves avatar May 11 '22 14:05 ureeves

As a suggestion, we could offer something like this - using a simple counter smart contract as example:

#[derive(Query)]
pub struct Read;

#[derive(Transaction)]
pub struct Increment {
    pub value: i64,
}

#[derive(Contract)]
pub struct Counter {
    value: i64,
}

impl Counter {
    #[transaction]
    fn increment(&mut self, inc: Increment) -> i64 {
        *self.get_val_mut() += inc.value;
        *self.get_val()
    }

    #[query]
    fn read(&self, _r: Read) -> i64 {
        self.get_val().clone()
    }

    fn get_val(&self) -> &i64 {
        &self.value
    }

    fn get_val_mut(&mut self) -> &mut i64 {
        &mut self.value
    }
}

Everything in the Counter struct constitute the state of the smart contract while the transaction and query attributes mark the functions that are available for calling as a transaction (mutable) or as a query (immutable) respectively.

The Contract, Query, and Transaction derive macros, together with the transaction and query attributes can be used to generate code compliant with the virtual machine, and could provide suitable ergonomics for contract reuse.

ureeves avatar May 11 '22 14:05 ureeves