Skip to content

feat(congestion): congestion-pricing native contract#445

Open
adamkrellenstein wants to merge 3 commits into
mainfrom
feat/congestion-pricing
Open

feat(congestion): congestion-pricing native contract#445
adamkrellenstein wants to merge 3 commits into
mainfrom
feat/congestion-pricing

Conversation

@adamkrellenstein

@adamkrellenstein adamkrellenstein commented May 31, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a congestion native contract implementing the block-utilization fee multiplier β(t) from the economics spec (§Congestion Pricing). β evolves deterministically per block as a function of utilization u:

Region Condition Update
Uncongested u < u_low multiplicative decay β·λ_decay toward 0
Transition u_low ≤ u < u_high smoothstep ramp S(x), floored by the decay term
Congested u ≥ u_high compounding growth max(β,1)·(1 + κ·(u − u_high))

Fee = φ_base · β.

Parameters (u_low=20%, u_high=80%, λ_decay=0.95, κ=2.0, φ_base=2.5e-7) match Documentation/modeling/kontor_v1/congestion.py and specs/params.typ.

What's included

  • New native-contracts/congestion/ workspace member (contract + WIT + built congestion.wasm.br).
  • Registered as native contract ID 6 (append-only) in NATIVE_CONTRACTS.
  • Runtime api module runtime/congestion.rs + two lite tests validating the β recurrence against the reference model (1.2 → 1.44 → 1.368; smoothstep midpoint 0.5; decay floor 0.475).

Deferred (intentionally out of scope)

The runtime gas-application hook — wiring β into call.rs fee charging — is deferred per the agreed split: this PR lands the deterministic state machine + contract only, keeping changes additive and low-risk while the indexer fee path is in flux.

CI note

The repo's cargo audit gate is currently red on the default branch from pre-existing upstream advisories (hickory-proto, rustls-webpki via libp2p/malachite/libsql/rustls; one has no fix available yet). This PR does not touch core/Cargo.lock and its own native-contracts audit is clean — the audit failures are unrelated and pre-existing.


Note

Medium Risk
Appending a genesis native contract is a protocol-level ID/order decision and affects all nodes at genesis; fee application is deferred, so economic impact is limited until a follow-up wires β into charging.

Overview
Introduces a congestion native contract that implements the economics-spec β(t) fee multiplier from block utilization u, with fee = φ_base · β. The contract stores default thresholds and rates (20%/80% bands, decay, κ, φ_base) and exposes core update_beta, views for β and fee_multiplier, and optional set_congestion_params with basic validation.

The change wires it into the stack like other natives: new native-contracts/congestion WASM crate + WIT, append to NATIVE_CONTRACTS (genesis ID 6), embedded congestion.wasm.br, and indexer runtime/congestion API bindings. Lite tests assert β recurrence, smoothstep mid-band, edge continuity, and invariants against the reference Python model.

Runtime gas charging is not hooked up in this PR—the contract and deterministic state machine land first; applying β in procedure fee paths is explicitly deferred.

Reviewed by Cursor Bugbot for commit dfd632b. Bugbot is set up for automated code reviews on this repo. Configure here.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 016164d. Configure here.

model.set_kappa_cong_bps(params.kappa_cong_bps);
model.set_phi_base(params.phi_base);
Ok(())
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set_congestion_params missing validation on decay/growth parameters

Medium Severity

set_congestion_params validates u_low_bps and u_high_bps but accepts any value for lambda_decay_bps, kappa_cong_bps, and phi_base without checks. Setting lambda_decay_bps above BPS_DENOM (10 000) turns the decay factor into a growth multiplier (>1.0), causing β to compound upward even in Region 1 (the "uncongested" region), completely inverting the economic model. The existing partial validation suggests guarding against bad parameter values was intended but incomplete.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 016164d. Configure here.

Introduce a `congestion` native contract implementing the block-utilization
fee multiplier β(t) from the economics spec (§Congestion Pricing). β evolves
deterministically per block as a function of utilization u:

  - u < u_low   : multiplicative decay toward 0 (β·λ_decay)  — uncongested
  - u_low..u_high: smoothstep ramp S(x), floored by the decay  — transition
  - u ≥ u_high  : compounding growth max(β,1)·(1 + κ·(u−u_high)) — congested

Fee = φ_base · β. Parameters (u_low=20%, u_high=80%, λ_decay=0.95,
κ=2.0, φ_base=2.5e-7) match Documentation/modeling/kontor_v1/congestion.py
and specs/params.typ.

The contract is registered as native contract ID 6 (append-only) with a
runtime api module and two lite tests validating the recurrence against the
reference model. The runtime gas-application hook (wiring β into call.rs fee
charging) is deferred — this lands the deterministic state machine only.
@adamkrellenstein adamkrellenstein force-pushed the feat/congestion-pricing branch from 016164d to 80ea913 Compare June 2, 2026 07:06
Add seeded random-walk + band-edge property tests for the congestion β(t)
recurrence:

- congestion_beta_invariants_over_random_walk: over utilizations spanning all
  three regions — region 1 decays (β'≤β); region 3 grows (β'≥max(β,1)≥1);
  region 2 is bounded by the smoothstep ceiling (β'≤max(β,1)); β never negative;
  fee multiplier always == φ_base·β.
- congestion_smoothstep_continuous_at_band_edges: S(1)=1 at u_high (joins
  region 3's floor), S(0)=0 at u_low (joins region 1's decay floor).

Design decision: the random-walk test asserts STRUCTURAL invariants via
comparisons (≤/≥), not re-derived exact β' values — robust to fixed-point
rounding (region-2 x=(u−u_low)/(u_high−u_low) is not always a terminating
decimal). Exact interior values stay covered by the existing point tests.
Test-only.
tests: congestion β(t) region/bound invariant property tests
@adamkrellenstein adamkrellenstein added enhancement New capability or improvement area: economics Token / staking / storage economics labels Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: economics Token / staking / storage economics enhancement New capability or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant