Feature/compute budget estimation#355
Open
whiteghost0001 wants to merge 3 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Here is a breakdown of the architectural changes and implementation details for Issue #351: Compute Budget Estimation API
Architectural Overview
We designed and integrated a simulation-only compute budget estimation engine into the split contract (contracts/split/src/lib.rs). This provides off-chain SDKs and callers with deterministic resource pre-flight checks (cpu_insns, mem_bytes, fee_stroops) for transaction sizing without incurring state mutations or unnecessary on-chain execution overhead.
Key Technical Deliverables
types.rs
Replaced legacy budget placeholders with exact host resource primitives:
rust
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ComputeEstimate {
pub cpu_insns: u64,
pub mem_bytes: u64,
pub fee_stroops: i128,
lib.rs
Implemented the estimate_compute(env, operation, params) entry point with full dispatch support across the 6 major contract operations:
create_invoice ("create_invoice", "create", "create_i"): Evaluates linear instruction scaling against recipient array counts ($1.2\text{M} - 5.0\text{M}$ CPU instructions).
pay ("pay", "pay_dlgt"): Accounts for base payment routing, multi-shard storage mutations, and optional auto-release transfer overhead when fully funded.
release ("release"): Models transfer iteration, fee-tier platform deductions, and recipient distribution.
refund ("refund"): Computes multi-shard payer iteration and proportional token refund transfers.
open_dispute ("open_dispute", "raise_dispute", "dispute"): Estimates audit trail append and dispute state isolation.
approve_release ("approve_release", "approve", "approve_invoice"): Estimates multi-sig governance approval state updates.
3. Defensive Parameter Extraction & Error Handling
Added robust parameter parsing helpers (Self::get_u64_param, Self::get_recipients_len, Self::load_invoice_opt) supporting canonical key aliases (invoice_id, id, recipients, recipient_count).
Returns contract-native error codes (ContractError::InvalidRecipients, ContractError::InvoiceNotFound, ContractError::InvalidStatus) rather than unhandled panics when passed unexpected types or missing fields.$$\text{fee_stroops} = \left(\frac{\text{cpu_insns}}{10,000}\right) \times \text{STROOPS_PER_10K_INSTRUCTIONS}$$
Strictly read-only: inspects state via env.storage().persistent().get without writing or mutating keys.
4. Dynamic Fee Calculation & Budget Threshold Alerts
Fee Formula: Calculates transaction fee estimates in Stroops:
High-Resource Warning: Emits topic event ("split", "bdgt_w", operation) if an estimated execution exceeds 80% of the Soroban instruction budget ceiling (80,000,000 CPU instructions).
5. Documentation & Technical Reference (
docs/COMPUTE_BUDGETS.md
Updated the repository compute reference with operation matrices, resource ranges, fee calculation formulas, and operational assumptions.
Verification & Quality Assurance
Unit Test Suite (
test.rs
): Added integration tests verifying accurate estimation within 10% tolerance against env.cost_estimate().budget(), repeated call determinism, missing parameter handling, and state immutability.
Workspace Status:
cargo fmt: Clean formatting across workspace.
cargo clippy --all-targets -- -D warnings: 0 warnings, 0 errors.
cargo test --workspace: 270/270 unit tests passing.
closes #351