Skip to main content

Module budget

Module budget 

Source
Expand description

Compute-unit budget tracking and instrumentation.

Solana programs have a finite CU budget per instruction. Exceeding it is a hard abort. No existing framework provides runtime CU tracking at the substrate level – programs either blindly hope they fit or manually sprinkle sol_log_compute_units() calls.

Hopper’s CuBudget provides:

  1. Snapshot/check pattern: Take a CU snapshot, do work, check how much was consumed. Useful for profiling individual code paths.

  2. Guard pattern: Set a CU floor and periodically check that you have enough budget remaining before expensive operations (like CPI).

  3. Feature-gated tracing: With #[cfg(feature = "cu-trace")], emit structured CU consumption logs at function boundaries that off-chain tools can parse into flame graphs.

§Usage

use hopper_native::budget::CuBudget;

fn process(accounts: &[AccountView], data: &[u8]) -> ProgramResult {
    let budget = CuBudget::snapshot();

    // ... do work ...

    // Before an expensive CPI, check we have at least 50k CU left.
    budget.require_remaining(50_000)?;

    // CPI call...
    Ok(())
}

With cu-trace enabled:

use hopper_native::budget::cu_trace;

fn process_deposit(/* ... */) -> ProgramResult {
    cu_trace!("deposit::start");
    // ... work ...
    cu_trace!("deposit::after_validation");
    // ... CPI ...
    cu_trace!("deposit::end");
    Ok(())
}

Structs§

CuBudget
Compute-unit budget tracker.