filecoin-project / ref-fvm

Reference implementation of the Filecoin Virtual Machine
https://fvm.filecoin.io/
Other
384 stars 139 forks source link

EVM Saving & Reloading Contract State #961

Closed Stebalien closed 2 years ago

Stebalien commented 2 years ago

We need to efficiently:

The current runtime abstraction doesn't work for us here, so we'll likely have to extend it. Specifically, we need to, e.g., be able to learn the state "root" without actually reading it.


Examples:

  1. If we call into an actor that doesn't read any slots, it shouldn't load the HAMT.
  2. If we make a call from an actor that hasn't written to any slots, we shouldn't write any state before making the call.
Stebalien commented 2 years ago

Note: in terms of reverts, the FVM handles that for us. The FVM keeps state "layers" for every call. If any call "reverts", all effects of that call (recursively) are discarded.

Stebalien commented 2 years ago

Proposed Design

  1. Introduce "dirty" tracking on first write and/or nonce change.
  2. Wrap rt.send with a function that:
    1. Flushes the current HAMT/state if the state is "dirty", recording the new state root in memory.
    2. Executes a callback.
    3. Gets the new state-root CID from the system (sself::get_root) and compares it with the saved root.
    4. If the state has changed (e.g., due to a recursive call), reload it (discarding the current HAMT and any cache therein).

NOTE: we could exempt method 0 sends, but that's probably not worth it.

maciejwitowski commented 2 years ago

@Stebalien I added this to the checklist based on Quartz milestone assigned.