fix(settlement): enforce strict idempotency guard to prevent double-c…#41
Merged
elizabetheonoja-art merged 1 commit intoJun 25, 2026
Conversation
elizabetheonoja-art
approved these changes
Jun 25, 2026
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 #23
PR: Strict Idempotency Guard for Settlement Finalization
Summary
Adds a strict idempotency guard to
finalize_settlement()by introducing a separate storage flag (DataKey::BatchFinalized) that is checked and set before any state mutation, preventing accidental or malicious double-finalization of batch proposals.Problem
finalize_settlement()could be called multiple times for the same proposal. The existing check onproposal.finalizedhad two issues:release_locked_resources()(a storage write) before checkingproposal.finalized.finalizedflag was stored inside theSettlementProposalstruct, which was only written to storage at the very end of the function after all other operations. If the function panicked mid-execution (e.g., inrelease_locked_resources()), the struct update was lost.Solution
A two-layer defense with an independent storage flag:
DataKey::BatchFinalized(proposal_id)— independentboolin instance storageproposal.finalizedfield on the storedSettlementProposalstructChanges
contracts/utility_contracts/src/lib.rsBatchFinalized(u64)variant to theDataKeyenumcontracts/utility_contracts/src/settlement.rsDataKeyfinalize_settlement()to:DataKey::BatchFinalized(proposal_id)— reject withAlreadyFinalizedif settrue(optimistic — remainstrueeven if the function panics later)test_double_finalization_via_independent_flag— verifies the flag blocks re-finalization even when the storedproposal.finalizedfield is manually reset tofalsetest_partial_execution_after_deadline_blocks_retry— verifies that a failed finalization (deadline exceeded after flag was set) permanently blocks future attemptsDesign Rationale
SettlementProposalstruct, ensuring it survives partial write failures.u64forproposal_idmatching the existing convention, notBytesN<32>.