Skip to content

Add TTL bump optimization: batch extend_ttl calls instead of per-read/write #25

Description

@prodbycorne

Overview

Both contracts extend TTL on every read and write, including read-only query functions. This is unnecessarily expensive in terms of Soroban resource fees:

// farming-pool/src/lib.rs — called on EVERY public function, including pure reads
fn bump_instance(env: &Env) {
    env.storage().instance().extend_ttl(TTL_THRESHOLD, TTL_EXTEND_TO);
}

// Also called on every get_user_stake, get_position read
fn bump_user(env: &Env, key: &DataKey) {
    env.storage().persistent().extend_ttl(key, USER_TTL_THRESHOLD, USER_TTL_EXTEND_TO);
}

Problems:

  1. extend_ttl costs fees even when current TTL is already maxed out (well above TTL_THRESHOLD)
  2. Read-only functions (get_credits, calculate_credits, get_stake, get_user_position) should not modify state — bumping TTL makes them state-modifying
  3. In Soroban v22+, extend_ttl below threshold is a no-op at the host level, but the instruction cost is still charged

Fix

  1. Conditional bump — only extend if current TTL is actually below threshold:
fn bump_instance(env: &Env) {
    let ttl = env.storage().instance().get_ttl();
    if ttl < TTL_THRESHOLD {
        env.storage().instance().extend_ttl(TTL_THRESHOLD, TTL_EXTEND_TO);
    }
}
  1. Skip bump in read-only pathscalculate_credits, get_user_position, get_boost_config, get_stake, get_credits, is_paused, pool_count, get_pool, admin, pool_wasm_hash should NOT bump TTL

  2. Batch user bumps — when both UserStake and UserBoost are read in the same call, batch the extends

Acceptance Criteria

  • bump_instance checks current TTL before extending
  • Read-only entry points do not call bump_instance or bump_user
  • State-modifying entry points still bump TTL as required
  • Benchmark: measure instruction count before and after for calculate_credits
  • No regression in TTL coverage (entries still get bumped on writes)

Metadata

Metadata

Assignees

No one assigned

    Labels

    GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardOfficial CampaignCampaign: Official CampaignfactoryFactory contractfarming-poolFarmingPool contractperformanceGas cost and throughput improvements

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions