#gas #execution #near #contracts #run-time #smart-contracts #cache

near-vm-runner

This crate implements the specification of the interface that Near blockchain exposes to the smart contracts

38 releases (18 breaking)

4.0.0-pre.1 Mar 5, 2021
2.2.0 Sep 3, 2020
2.0.0 Aug 22, 2020
1.2.0 Aug 18, 2020
0.4.2 Nov 21, 2019

#7 in #gas

Download history 1286/week @ 2024-07-15 1286/week @ 2024-07-22 2490/week @ 2024-07-29 2558/week @ 2024-08-05 2043/week @ 2024-08-12 946/week @ 2024-08-19 1101/week @ 2024-08-26 1376/week @ 2024-09-02 1438/week @ 2024-09-09 1192/week @ 2024-09-16 1042/week @ 2024-09-23 823/week @ 2024-09-30 914/week @ 2024-10-07 1423/week @ 2024-10-14 1162/week @ 2024-10-21 1311/week @ 2024-10-28

4,892 downloads per month
Used in 12 crates (3 directly)

MIT/Apache

2MB
29K SLoC

near-vm-runner

An engine that runs smart contracts compiled to Wasm. This is the main crate of the "contract runtime" part of nearcore.

"Running smart contracts" is:

  • Wasm instrumentation for gas metering and various safety checks (prepare.rs).
  • Compiling Wasm to a particular VM representation (cache.rs).
  • Exposing blockchain-specific functionality to Wasm code. That is, defining a corresponding host function for each function in near-vm-logic (imports.rs).
  • Actual code execution (wasmer_runner.rs).

The particular runtime used for Wasm execution is an implementation detail. At the moment we support Wasmer 0.x, Wasmer 2.0 and Wasmtime, with Wasmer 2.0 being default.

The primary client of Wasm execution services is the blockchain proper. The second client is the contract sdk tooling. vm-runner provides additional API for contract developers to, for example, get a gas costs breakdown.

See the [FAQ][./faq.md] document for high-level design constraints discussion.

Entry Point

The entry point is the runner::run function.

Testing

There are a bunch of unit-tests in this crate. You can run them with

$ cargo t -p near-vm-runner --features wasmer0_vm,wasmer2_vm,wasmtime_vm,near_vm

The tests use either a short wasm snippet specified inline, or a couple of larger test contracts from the near-test-contracts crate.

We also have a fuzzing setup:

$ cd runtime/near-vm-runner && RUSTC_BOOTSTRAP=1 cargo fuzz run runner

Profiling

tracing crate is used to collect Rust code profile data via manual instrumentation. If you want to know how long a particular function executes, use the following pattern:

fn compute_thing() {
    let _span = tracing::debug_span!(target: "vm", "compute_thing").entered();
    for i in 0..99 {
        do_work()
    }
}

This will record when the _span object is created and dropped, including the time diff between the two events.

To get human-readable output out of these events, you can use the built-in tracing subscriber:

tracing_subscriber::fmt::Subscriber::builder()
    .with_max_level(tracing::level_filters::LevelFilter::DEBUG)
    .with_span_events(tracing_subscriber::fmt::format::FmtSpan::CLOSE)
    .init();

code_to_profile_here();

Alternatively, there's an alternative hierarchical profiler

tracing_span_tree::span_tree().enable();

code_to_profile_here();

The result would look like this:

      112.33ms deserialize_wasmer
      2.64ms run_wasmer/instantiate
      96.34µs run_wasmer/call
    123.15ms run_wasmer
  123.17ms run_vm

Dependencies

~18–38MB
~640K SLoC