casper-node icon indicating copy to clipboard operation
casper-node copied to clipboard

VM2 MVP

Open mpapierski opened this issue 7 months ago • 0 comments

This massive PR adds an MVP new smart contract execution engine based on wasmer.

This aims to address some long-standing issues with the current engine as well as improve developers' experience:

  • URef once granted is forever and can't be reclaimed. The new engine does not support these.
  • As a consequence purses are not exposed to the user (purses == special urefs)
  • There were ~53 host functions in old execution engine. The new engine aims to be thoughtful when adding new host functions.
  • Often there were inefficiencies when passing the data from host to wasm and back
  • Isolation of implementation details from the Wasm SDK
  • Encouraged high-level code (think of struct Contract, trait SharedBehavior), low-level also possible and is easy to explain
  • When going high-level users shouldn't be concerned with low details (no more no_mangle, pub extern "C", e
  • Introduces missing features
    • payable entrypoints
    • contract schemas
    • unit testing

And more

Example contract:

#[casper(contract_state)]
struct Counter {
  count: u64
}


#[casper]
impl Counter {
  #[casper(constructor)]
  pub fn new(initial_value: u64) {
    Self {
      count: initial_value
    }
  }
  pub fn counter_inc(&mut self) {
    self.count += 1;
  }
  pub fn counter_get(&self) -> u64 {
    self.count
  }
}

#[cfg(test)]
mod tests {
  #[test]
  fn counter_works() {
    let mut counter = Counter::default();
    let before = counter.counter_get();
    counter.counter_inc();
    let after = counter.counter_get();
    assert!(after > before);
  }
}

There's self-contained example contract located here https://github.com/mpapierski/vm2-flipper/ that currently pulls the deps through git. There will be more self-contained example contracts as the time goes.

In general, smart contract developers from similar backgrounds should feel productive early on.

Another benefit of the new engine is that it brings us closer in performance to other leading blockchains, has a smaller footprint, and is easier to maintain and explain to the users.

This PR lays the foundation for the MVP new engine at the core level, but at the same time allows you to spin up nctl network and start write smart contracts, and execute contracts with the new runtime target.

mpapierski avatar Jul 15 '24 16:07 mpapierski