Rust Smart Contracts and Wasm chaincode - Tech Preview


Matthew White
 

We'd like to formally announce the much anticipated Tech Preview 1 for the Rust Smart Contracts API, running in the Wasm chaincode for Hyperledger Fabric v2.2.  There are several motivations for doing this work.
 
- Wasm is a (relatively) new technology providing a secure and deterministic VM. Originally for in-browser use, it's now seeing adoption by blockchains for smart contracts. We want to find out how this could be used with Fabric.
 - Increasing consumability of the Fabric programming model is an important theme, and over the last few years, we've worked on both the client sdk and smart contracts. So we've taken the existing Contract API for Smart Contracts, and together with the proposed Ledger API evolved it into an API that improves and offers more streamline developer for users. 
 
If you're keen to get started, here's the Getting Started guide and the API Documentation
 
There's a video run-through of the getting started guide
 
Wasm chaincode runtime
There is an existing Hyperledger Lab that worked with Rust and C together with a Wasm engine to run chaincode. It took the approach of multiple different Wasm binaries within a single chaincode. 
 
Taking the experience from there, we've built out a Go chaincode, that hosts a Wasm VM. From the peers perspective, this is regular chaincode created via the Fabric v2 chaincode builders. The builder pattern permits a very flexible approach for creating other types of chaincode.
 
Wasm Ledger API
Wasm was originally designed to work in browser, to offer near-native performance for code where computational intensive work was required in-browser.  To communicate with the 'host' browser (via JavaScript) Wasm defines a series of import and export functions.
 
In our case we needed to be able to define an API that could be used across this import/export boundary. To do this requires both definitions of the calling convention to be used, as well what types of API (eg get/put/delete etc).
 
There are a number of different approaches, but we adopted the waPC project to provide the calling convention, and also established a set of protobuf definitions for the actual API calls. This gives a very powerful approach.
 
With this API definition and 'host' chaincode, any language that can be compiled to Wasm bytecode can call these APIs with a very tight definition in both calling conventions and data formats.  We choose to implement a contract API in Rust for the first instance
 
Rust API
Rust has a great eco-system and community in general but it also put a lot of work into the compilation to Wasm bytecode. Rust is a natural place to start, it is also a very good language for writing safe and correct software, which is what you want from a Smart Contract!
 
We took the opportunity to take the experience of the Contract API, and the proposed Ledger API to evolve the overall experience.  
 
What can you do with this Tech Preview?
 
You can write a Smart Contract in Rust, able to query the world state, private data collections, handle transient data. You can do key-range-based queries.
 
You can compile Rust to Wasm; there are guides on how to do this with a customized version of Microfab and a simple Docker-based Fabric Network 
 
For the more adventurous, you can use another language that compiles to Wasm and interact with the APIs we've provided directly. (Some also write Wasm bytecode by hand, but it is a niche skill)
 
Interoperate with any other Fabric SDK; as far as the Peer is concerned this is another chaincode. Therefore the existing SDKs will work quite happily. 
 
What can't you do with this Tech Preview?
Please don't put it into production! We don't have full test suites, fv or performance.
 
State-Based Endorsement and Rich query aren't implemented, the APIs are present but the implementation is to follow.
 
The other contract APIs will generate and return metadata about the contract, for tools such as the IBM Blockchain VSCode Extension.  This is to be implemented.
 
Call to the Community
Please go and work with this, look at the idea of Wasm running chaincode, and the idea of being able to take other languages such as C and write contracts in them.
 
Please look at the API in Rust - it's an evolution of the current APIs. We would very much like to port that to our existing ContractAPI implementations.
 
Please any fellow rustacens, we're new to Rust so feedback on the idiomatic design of the API more than welcome.
 
Worth also thanking anweiss from the Community for an early PR and testing.. it is appreciated! 
 
Please raise via the github issues on the fabric-contract-api repo. Remember that this is a tech preview, so it's not supported and for exploration only.
 
Links
 
GitHub repos:
 
DockerHub:
 
 
Matthew White / James Taylor