Skip to content

Implement Env via wasm imports, not serde json #688

@ethanfrey

Description

@ethanfrey

Current State

Currently Env is a nested json struct with 5 concrete fields (BlockInfo and ContractInfo).

This is very easy to construct and pass in tests, but it is a bunch of json parsing on every call, which is not ideal. It could also be more compile-time type-safe (not just relying on json parsing for that). This is referenced in each contract as a concrete type: env: Env.

Proposal

  1. Rename Env to MockEnv and we use this just for the tests.
  2. Make trait Env with accessors, which is implemented by MockEnv
  3. Create an ExternalEnv, which provides the same methods but makes calls wasm exports to get the values when needed
  4. handle/init/query all now take env: &dyn Env for the argument. And call eg. env.contract_address() instead of env.contract.address.clone()

Here is the trait I am thinking of, which is just flattened accessors from the current Env type.

pub trait Env {
  fn block_height(&self) -> u64;
  fn block_time(&self) -> u64;
  fn block_time_nanos(&self) -> u64;
  fn chain_id(&self) -> String;
  fn contract_address(&self) -> HumanAddress;
}

Proposing this for 0.14. Either we do it then, or we reject this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking (interface)Breaks VM-contract interface, likely requires `cosmwasm_version` bumpPerformanceLet's try to get things fastermaybequestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions