Expand description
Compute-unit budget tracking and instrumentation.
Solana programs have a finite CU budget per instruction. Exceeding it
is a hard abort. Hopper provides runtime CU tracking at the substrate
level so programs can avoid scattering ad hoc sol_log_compute_units()
calls through business logic.
Hopper’s CuBudget provides:
-
Snapshot/check pattern: Take a CU snapshot, do work, check how much was consumed. Useful for profiling individual code paths.
-
Guard pattern: Set a CU floor and periodically check that you have enough budget remaining before expensive operations (like CPI).
-
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.
Constants§
- ERR_
INSUFFICIENT_ CU - Custom error code returned by
CuBudget::require_remainingwhen the remaining compute budget is below the requested floor.