Skip to content

Tech debt: Avoid store→fetch round-trip in contract validation #2216

@sanity

Description

@sanity

Overview

During the fix for #2214, we identified an architectural inefficiency where verify_and_store_contract() stores a contract then immediately fetches it back for validation.

Current Flow

// verify_and_store_contract() in runtime.rs

// Line 1076: We HAVE the contract
if let Some(contract) = trying_contract.take() {
    // Line 1092: Store it (moves ownership)
    self.runtime.contract_store.store_contract(contract)?;
}

// Line 1105: Immediately fetch it back for validation
let result = self.runtime.validate_state(&trying_key, ...)?;

The validate_state() call needs the contract bytes to compile a WASM module via prepare_contract_call()fetch_contract().

The Issue

We have the contract data in hand, give it to the store, then immediately ask the store to give it back. This required adding wait() after cache insert (#2215) because stretto uses eventual consistency and the data wasn't visible yet.

Potential Solutions

  1. Pass contract to validate_state directly:

    self.runtime.validate_state_with_contract(&trying_key, &contract, ...)?;
    self.runtime.contract_store.store_contract(contract)?;
  2. Have store_contract also compile and cache the WASM module

  3. Clone contract code before storing and use directly for compilation

Impact

  • Performance: Minimal - wait() is fast (just flushes buffer)
  • Correctness: Current fix works correctly
  • Code clarity: The round-trip is architecturally awkward

Priority

Low - the current fix is correct and not performance-impacting. This is cleanup for code clarity.

Related

[AI-assisted - Claude]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-contractsArea: Contract runtime, SDK, and executionE-easyExperience needed to fix/implement: Easy / not muchP-lowLow priorityT-enhancementType: Improvement to existing functionality

    Type

    No type

    Projects

    Status

    Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions