fix: unclosed delimiter in circuit breaker code (creator-keys) - #841
Open
Ajibose wants to merge 1 commit into
Open
fix: unclosed delimiter in circuit breaker code (creator-keys)#841Ajibose wants to merge 1 commit into
Ajibose wants to merge 1 commit into
Conversation
accesslayerorg#838) The circuit breaker block in buy_key_with_referrer referenced an undeclared threshold_pct variable and had the if-block improperly nested outside the else branch of the auction/bonding-curve price match, producing an unclosed delimiter that broke `cargo fmt --all -- check` on main. Reads threshold_pct from CIRCUIT_BREAKER_THRESHOLD storage (defaulting to 30) and nests the price-jump check correctly inside the else branch so it only runs for bonding-curve buys, matching the existing auction-bypass comment.
|
@Ajibose Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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.
Closes #838
Summary
cargo fmt --all -- --checkfailed onmainwith an unclosed-delimiter parse error increator-keys/src/lib.rs, originating in the circuit breaker block insidebuy_key_with_referrer. Two bugs, both introduced by PR #813, caused it:threshold_pctwas referenced but never declared.if pre_price > 0 && post_price > pre_price { ... }block was not properly nested inside theelsebranch of thelet price = if in_auction { ... } else { ... };expression, unbalancing the braces.Changes
Modified files
creator-keys/src/lib.rs— inbuy_key_with_referrer(circuit breaker block, ~line 2595):threshold_pct: u32read fromconstants::storage::CIRCUIT_BREAKER_THRESHOLD, defaulting to30when unset (matching the existingset_circuit_breaker_thresholdadmin setter and its doc comment).elsebranch, alongsidepre_price's computation, and does not run during the fixed-price auction phase (in_auctionbranch) — consistent with the existing comment above theif in_auctionarm that the breaker only guards bonding-curve movement.New files
creator-keys/tests/circuit_breaker_threshold.rs— integration test suite for the fix (see below).Implementation details
The circuit breaker compares the bonding-curve price just before a buy (
pre_price, at current supply) against the price just after (post_price, atsupply + 1). Ifpost_price > pre_priceand the relative increase is>= threshold_pctpercent, the trade is rejected withContractError::CircuitBreakerTriggeredand aCircuitBreakerTriggeredEvent { pre_price, post_price }is published. The threshold is admin-configurable viaset_circuit_breaker_threshold(already present onmain) and defaults to 30% when never set. Auction-phase buys (fixed auction price, in theif in_auctionarm) are unaffected since the check only exists in the bonding-curveelsearm.Tests added (
creator-keys/tests/circuit_breaker_threshold.rs)test_circuit_breaker_default_threshold_blocks_large_price_jump— a 40% jump is rejected under the default 30% threshold, and supply is unchanged after the rejected call.test_circuit_breaker_default_threshold_allows_small_price_jump— a 1% jump succeeds under the default threshold.test_circuit_breaker_flat_curve_never_triggers— with no curve slope configured (flat price), several sequential buys all succeed sincepost_price > pre_pricenever holds.test_circuit_breaker_custom_threshold_relaxes_a_previously_blocked_jump— a 40% jump is blocked by default, then succeeds once the admin raises the threshold to 50% viaset_circuit_breaker_threshold.test_circuit_breaker_custom_threshold_still_blocks_larger_jumps— a 60% jump is still blocked even after raising the threshold to 50%.test_circuit_breaker_exact_threshold_boundary_triggers— an exactprice_change * 100 == pre_price * threshold_pctmatch still triggers (the comparison is>=).test_circuit_breaker_just_below_threshold_boundary_allows— one slope unit below that exact boundary passes.test_circuit_breaker_triggered_event_reports_pre_and_post_price— asserts the emittedCircuitBreakerTriggeredEventcarries the correctpre_price/post_pricepayload.test_circuit_breaker_does_not_block_second_buy_after_supply_advances_past_jump— two sequential small-jump buys both succeed, confirming the breaker re-evaluates per trade.This complements the existing
test_circuit_breaker_threshold_configuration_and_triggerincreator-keys/src/test_new_features.rs, which already exercisedset_circuit_breaker_thresholdand depended onthreshold_pctbeing defined — it could not previously compile due to this bug.How to test
Known pre-existing issue (out of scope for this PR)
While verifying this fix I ran
cargo build -p creator-keys(not justcargo fmt) and found the crate does not currently compile onmainfor reasons unrelated to this issue: duplicate definitions increator-keys/src/events.rs(FeeCollectedEvent,LockupBlockedEvent, and their spec XDR) andcreator-keys/src/lib.rs(credit_staking_rewards_pool, duplicateDataKey::RoyaltyConfig/CurveExponentvariants), plus several types (AuctionConfig,StakingRewardsState,StakePosition, etc.) referenced inlib.rswithout being in scope, and theconfigure_auction/cancel_auction/get_auction_configentrypoints (used bytests/prelaunch_auction.rs) being entirely absent from currentlib.rs. This appears to stem from a bad merge resolution around PR #813 and predates this change —cargo fmtonly parses syntax, so it never surfaced these. I did not attempt to fix it here since it's unrelated to the circuit breaker bug and is a much larger change; recommend filing a separate issue socargo build/cargo testcan be restored for the whole crate.