Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Designing a WebAssembly Runtime for Blockchains

Designing a WebAssembly Runtime for Blockchains

In this presentation we will overview how Wasmer is providing the best infrastructure for the Blockchain platforms

Cf18e42df2acc7a1fc1700bcd8e76487?s=128

Syrus Akbary

June 08, 2019
Tweet

Transcript

  1. Designing a WebAssembly runtime for Blockchains

  2. syrus@wasmer.io @wasmerio github.com/wasmerio Hi

  3. syrus@wasmer.io @wasmerio github.com/wasmerio How to execute WebAssembly?

  4. syrus@wasmer.io @wasmerio github.com/wasmerio How to execute WebAssembly? • In your

    Browser, using JavaScript
  5. syrus@wasmer.io @wasmerio github.com/wasmerio What about executing WebAssembly outside the browser?

  6. syrus@wasmer.io @wasmerio github.com/wasmerio How to execute
 WebAssembly
 outside of the

    Browser? • Interpreter • Compile Ahead of Time / Just in Time (AOT / JIT)
  7. syrus@wasmer.io @wasmerio github.com/wasmerio Performance ⏱

  8. syrus@wasmer.io @wasmerio github.com/wasmerio How to execute
 WebAssembly performantly
 outside of

    the Browser? • Interpreter • Compile Ahead of Time / Just in Time (AOT / JIT)
  9. syrus@wasmer.io @wasmerio github.com/wasmerio Easy

  10. syrus@wasmer.io @wasmerio github.com/wasmerio WebAssembly instructions are easy to map to

    underlying chipset instructions
  11. syrus@wasmer.io @wasmerio github.com/wasmerio How?

  12. syrus@wasmer.io @wasmerio github.com/wasmerio • Cranelift
 Internal project of Mozilla to

    eventually replace their IonMonkey JS implementation • Singlepass
 Compiler focused on compilation speed and fast runtime • LLVM
 Compiler focused on optimizations
  13. syrus@wasmer.io @wasmerio github.com/wasmerio Compilation speed Runtime speed Singlepass Fast (1x)

    Efficient (2~3x) Cranelift Slow (10x) A bit faster (1.8x) LLVM Very slow (20x~50x) Almost Native (1~1.2x) Expected to have better optimizations over time
  14. syrus@wasmer.io @wasmerio github.com/wasmerio Speed, Speed, Speed

  15. syrus@wasmer.io @wasmerio github.com/wasmerio We are obsessed about speed

  16. syrus@wasmer.io @wasmerio github.com/wasmerio Having a fast WebAssembly runtime is critical

  17. syrus@wasmer.io @wasmerio github.com/wasmerio Problem: There are programs that take longer

    to compile than they do to execute
  18. syrus@wasmer.io @wasmerio github.com/wasmerio JIT Bombs

  19. syrus@wasmer.io @wasmerio github.com/wasmerio Compiling php.wasm takes
 1:15s with Cranelift

  20. syrus@wasmer.io @wasmerio github.com/wasmerio Singlepass ⏩

  21. syrus@wasmer.io @wasmerio github.com/wasmerio Singlepass is a backend for the WebAssembly

    runtime focused on compilation speed. It emits machine code as the module is being parsed
  22. syrus@wasmer.io @wasmerio github.com/wasmerio Linear O(N) compilation time

  23. syrus@wasmer.io @wasmerio github.com/wasmerio Compiling php.wasm takes
 300ms with Singlepass

  24. syrus@wasmer.io @wasmerio github.com/wasmerio How to use it?

  25. syrus@wasmer.io @wasmerio github.com/wasmerio It’s just one line of code ✍

  26. syrus@wasmer.io @wasmerio github.com/wasmerio

  27. syrus@wasmer.io @wasmerio github.com/wasmerio speed.wasmer.io

  28. syrus@wasmer.io @wasmerio github.com/wasmerio Availability of Wasmer WebAssembly Runtime

  29. syrus@wasmer.io @wasmerio github.com/wasmerio Wasmer is written in Rust

  30. syrus@wasmer.io @wasmerio github.com/wasmerio But… you can have clients in any

    language
  31. syrus@wasmer.io @wasmerio github.com/wasmerio But we have embeddings for almost any

    other language • Go: Perlin, Cosmos, Ethereum • Rust: Polkadot, EVM • Python • …and many more!
  32. syrus@wasmer.io @wasmerio github.com/wasmerio Metering ⛽

  33. syrus@wasmer.io @wasmerio github.com/wasmerio Metering consists on measuring the cost of

    running certain instructions while being able to halt the execution if the costs is greater than expected
  34. syrus@wasmer.io @wasmerio github.com/wasmerio This is of vital importance for Blockchain

    platforms
  35. syrus@wasmer.io @wasmerio github.com/wasmerio Costs should remain deterministic regardless of the

    backend
  36. syrus@wasmer.io @wasmerio github.com/wasmerio How metering is done today • Transforms

    the WebAssembly module, injecting the calls to the gas meter
  37. syrus@wasmer.io @wasmerio github.com/wasmerio

  38. syrus@wasmer.io @wasmerio github.com/wasmerio This is the best strategy if you

    can’t access the VM directly (e.g. browser environments)
  39. syrus@wasmer.io @wasmerio github.com/wasmerio But… can be improved?

  40. syrus@wasmer.io @wasmerio github.com/wasmerio YES

  41. syrus@wasmer.io @wasmerio github.com/wasmerio Our solution: Metering through Middleware

  42. syrus@wasmer.io @wasmerio github.com/wasmerio Middleware let us customize the parsing in

    a backend- agnostic way, at parsing time
  43. syrus@wasmer.io @wasmerio github.com/wasmerio How? • The metering code is generated

    at parsing time through Middleware • Gas functions are inlined into the function body. No extra calls are required
  44. syrus@wasmer.io @wasmerio github.com/wasmerio Metering in Numbers 0us 1.75us 3.5us 5.25us

    7us Not metered Middleware WASM Transforms Baseline Time spent on metering
  45. syrus@wasmer.io @wasmerio github.com/wasmerio Let’s take a closer look • Metering

    8x faster than with code generated with WASM transforms (460us vs 3,680us) • 2x faster in overall runtime speed • No WASM transforms overhead
  46. syrus@wasmer.io @wasmerio github.com/wasmerio What else can we improve?

  47. syrus@wasmer.io @wasmerio github.com/wasmerio Contracts

  48. syrus@wasmer.io @wasmerio github.com/wasmerio A contract is a set of imports

    and exports that must be fulfilled in the module
  49. syrus@wasmer.io @wasmerio github.com/wasmerio WASI is a contract

  50. syrus@wasmer.io @wasmerio github.com/wasmerio Emscripten is a contract

  51. syrus@wasmer.io @wasmerio github.com/wasmerio Ewasm is a contract

  52. syrus@wasmer.io @wasmerio github.com/wasmerio Show me the code

  53. syrus@wasmer.io @wasmerio github.com/wasmerio We can pin a specific version of

    a contract to the WebAssembly module
  54. syrus@wasmer.io @wasmerio github.com/wasmerio Shipping soon

  55. syrus@wasmer.io @wasmerio github.com/wasmerio WebAssembly modules can be analyzed against a

    static spec before running
  56. syrus@wasmer.io @wasmerio github.com/wasmerio Is WASI the ABI we want to

    use for smart contracts?
  57. syrus@wasmer.io @wasmerio github.com/wasmerio OasisLabs: YES https://medium.com/oasislabs/blockchain-flavored-wasi-50e3612b8eba

  58. syrus@wasmer.io @wasmerio github.com/wasmerio How? • Using the filesystem to get

    the contract input and • Use Commands to call a contract • Use environment vars to get the amount of tokens transferred to the contract (VALUE) • Use environment args to get the blockchain address
  59. syrus@wasmer.io @wasmerio github.com/wasmerio Or perhaps we can use a specific

    ABI for it?
  60. syrus@wasmer.io @wasmerio github.com/wasmerio Ewasm

  61. syrus@wasmer.io @wasmerio github.com/wasmerio WASI vs Ewasm • Oasis WASI proposal

    uses the WASI ABI to interact with the environment • It would seem like WASI would help on running other modules that already run on WASI • eWASM have a explicit API
  62. syrus@wasmer.io @wasmerio github.com/wasmerio Packaging

  63. syrus@wasmer.io @wasmerio github.com/wasmerio SPAM Alert:
 wapm.io

  64. syrus@wasmer.io @wasmerio github.com/wasmerio We need decentralized packages

  65. syrus@wasmer.io @wasmerio github.com/wasmerio IPFS integration in wapm coming soon

  66. syrus@wasmer.io @wasmerio github.com/wasmerio …what about tampering?

  67. syrus@wasmer.io @wasmerio github.com/wasmerio Package signing

  68. syrus@wasmer.io @wasmerio github.com/wasmerio Package Signing is like the modern version

    of using sealing wax with a difficult to replicate imprint to verify the authenticity and integrity of the letter.
  69. syrus@wasmer.io @wasmerio github.com/wasmerio Package Signing let us trust that the

    package we are using proceed from the owner
  70. syrus@wasmer.io @wasmerio github.com/wasmerio But wait, there is more! • Preparing

    support for AArch64 (ARM) coming in the next months • More languages! • Tiering!
  71. syrus@wasmer.io @wasmerio github.com/wasmerio Why use Wasmer? • Vendor Neutral. No

    hidden interests. Our business is on creating the best WebAssembly runtime • Trusted and used already by blockchain companies • Rapid iteration process to bring the best runtime EVER
  72. syrus@wasmer.io @wasmerio github.com/wasmerio Recap • Fastest and modular WebAssembly runtime

    • Available in most popular languages (Rust, Go, Python…) • Adapted to work fast for blockchain use cases • Continuously improving
  73. syrus@wasmer.io @wasmerio github.com/wasmerio Our business is solely focused on creating

    the fastest WebAssembly runtime
  74. syrus@wasmer.io @wasmerio github.com/wasmerio Q & A 1

  75. syrus@wasmer.io @wasmerio github.com/wasmerio Technical Demo https://github.com/wasmerio/ewasm-workshop