filecoin-project / ref-fvm

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

Wasm Module Metadata #744

Open Stebalien opened 2 years ago

Stebalien commented 2 years ago

Currently, the FVM references wasm modules directly on-chain as raw-blobs. However, this doesn't leave any room for additional metadata.

Ideally, we'd add a new multicodec for "wasm" code so we can understand the wasm modules as structured data. However, we probably don't want to have to load the entire wasm module just to read the metadata, no matter what we do here.

Proposal

Store on-chain wasm modules behind a "metadata" object.

pub enum CodeType {
    // multicodecs?
}
pub struct Code {
    pub type: CodeType,
    pub code: Cid,
    // ... other metadata?
}

ActorState.Code (the "code" CID) would then refer to this Code object, instead of directly referring to wasm module.

Drawbacks

  1. This adds an additional (small) object per WASM module. But the size should be minimal (less than 50 bytes).
  2. This makes resolving the underlying WASM module slightly more expensive (need to resolve the ActorState object, then the Code object, then the actual "code". However, FVM clients are expected to pre-compile and cache actor code anyways, so this shouldn't matter in practice as long as we key this cache by Cid(Code) and not Code.code.

Alternatives

  1. Only store the metadata in the init actor. This will remove the indirection, but won't save any chain state.
  2. Don't add any metadata. At the moment, the only metadata is the code's "type", which could be recorded in the CID itself. However, I expect we'll want more metadata in the future.
Stebalien commented 2 years ago

NOTE: we could punt this to M2, but I want to at least consider the implications before doing so.