Skip to content

thlorenz/create-program-address-cus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Solana create_program_address CU Cost Analysis

A comprehensive tool for measuring the Compute Unit (CU) costs of Solana's create_program_address function across different seed combinations.

Why This Program Exists

Understanding the computational cost of Program Derived Addresses (PDAs) is crucial for Solana developers. The create_program_address function is widely used in Solana programs for:

  • Creating deterministic addresses for program-owned accounts
  • Implementing token accounts, metadata accounts, and various program state accounts
  • Building complex DeFi protocols that require predictable address derivation

However, the CU cost of create_program_address can vary somewhat depending on:

  • Number of seeds used
  • Length and content of seeds
  • Whether seeds are strings, pubkeys, or mixed types
  • The specific combination of seed data

This variability makes it difficult to:

  • Budget CU costs accurately in complex programs
  • Optimize seed combinations for gas efficiency
  • Predict transaction costs for programs with multiple PDA derivations

What This Program Does

This tool provides comprehensive CU cost analysis by:

  1. Testing >50 different seed combinations including:

    • String seeds of varying lengths
    • Single and multiple pubkey seeds
    • Mixed string + pubkey combinations
    • Edge cases (empty strings, very long strings)
    • Real-world patterns (token accounts, metadata, governance)
  2. Running 20 independent test cycles with fresh randomized pubkeys each time

  3. Measuring precise CU costs before and after each create_program_address call

  4. Calculating absolute minimum and maximum CU costs across all runs

Results

Based on our comprehensive testing across 20 runs with 50 different seed combinations each:

================================================================================
🎯 FINAL RESULTS ACROSS 20 RUNS
================================================================================
📉 ABSOLUTE MINIMUM CU COST: 1644 CUs
📈 ABSOLUTE MAXIMUM CU COST: 1669 CUs
📊 ABSOLUTE CU COST RANGE: 25 CUs
💰 AVERAGE ESTIMATE: 1656 CUs

🔍 Analysis:
   • Cheapest create_program_address operation: 1644 CUs
   • Most expensive create_program_address operation: 1669 CUs
   • Cost variability: 25 CUs difference

Key Findings

  • Consistent Performance: The CU cost is remarkably stable, varying by only 25 CUs (1.5%) across all test scenarios
  • Predictable Budget: Developers can safely budget ~1660 CUs per create_program_address call
  • Low Variability: Seed combination complexity has minimal impact on CU cost
  • Reliable Estimation: For programs with multiple PDA derivations, multiply 1660 CUs by the number of calls

How to Run

Prerequisites

  • Rust and Cargo installed
  • Solana CLI tools installed (solana, solana-test-validator)
  • Node.js/npm (if using Solana toolchain)

Setup

  1. Clone and build the program:
git clone <repository-url>
cd create-program-address-cus
cargo build-bpf -p create-program-address-cus
  1. Start the Solana test validator:
./sh/run-test-validator.sh

This script will:

  • Reset the validator ledger
  • Preload the program with the correct program ID
  • Enable logging to see CU measurements
  1. Run the CU measurement client (in a new terminal):
cargo run -p client

What Happens During Execution

  1. Client Setup:

    • Connects to local test validator
    • Requests SOL airdrop for transaction fees
    • Generates 3 random pubkeys for seed testing
  2. Test Execution (20 cycles):

    • Each cycle runs 50 different seed combinations
    • Measures CU cost before/after each create_program_address call
    • Tracks successful vs failed derivations
    • Reports min/max CU costs per cycle
  3. Final Analysis:

    • Calculates absolute minimum and maximum across all 1000 tests (20 × 50)
    • Provides cost range and variability analysis
    • Gives actionable budgeting recommendations

Project Structure

create-program-address-cus/
├── program/                  # Solana program (BPF)
│   ├── src/lib.rs            # Main program logic
│   └── Cargo.toml            # Program dependencies
├── client/                   # Test client
│   ├── src/main.rs           # Client that runs 20 test cycles
│   └── Cargo.toml            # Client dependencies
├── sh/
│   └── run-test-validator.sh # Script to start validator
├── Cargo.toml                # Workspace configuration
└── README.md                 # This file

Technical Details

Program Architecture

  • Language: Rust using solana-program crate
  • Program Type: On-chain Solana program (BPF)
  • CU Measurement: Uses sol_remaining_compute_units() syscall
  • Seed Handling: Accepts 3 pubkey accounts as input for dynamic testing

Test Methodology

  • Sample Size: 1000 total measurements (20 runs × 50 combinations)
  • Seed Variety: Comprehensive coverage of real-world usage patterns
  • Measurement Precision: Direct syscall measurement before/after each operation
  • Error Handling: Failed derivations are logged but excluded from min/max calculations
  • Randomization: Fresh pubkeys generated for each of the 20 test cycles

Use Cases for This Data

  1. Smart Contract Development: Budget CU costs accurately in your programs
  2. Transaction Planning: Estimate total CU consumption for complex operations
  3. Gas Optimization: Understand that seed complexity has minimal impact
  4. Capacity Planning: Plan for maximum CU usage in worst-case scenarios
  5. Cost Analysis: Calculate transaction fees for applications using PDAs

Contributing

Feel free to submit issues or pull requests to improve the test coverage or measurement accuracy.

License

[Add your license here]

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

  •  

Packages

No packages published