Skip to content

feat(settlement): implement round-half-up fee computation to prevent …#36

Merged
elizabetheonoja-art merged 2 commits into
Utility-Protocol:mainfrom
Adeyemi-cmd:Protocol_Fee_Rounding_Extraction_Favoring_Validator_Profit
Jun 25, 2026
Merged

feat(settlement): implement round-half-up fee computation to prevent …#36
elizabetheonoja-art merged 2 commits into
Utility-Protocol:mainfrom
Adeyemi-cmd:Protocol_Fee_Rounding_Extraction_Favoring_Validator_Profit

Conversation

@Adeyemi-cmd

Copy link
Copy Markdown
Contributor

Closes #12

PR Description

Summary

Implements round-half-up (commercial) rounding for protocol fee computation to close a systematic value extraction vector via micro-settlements.

Problem

The fee formula fee = amount * fee_rate_bps / 10000 uses integer division that always truncates. An attacker can submit millions of minimum-dust settlements (amount = 1 in 7-decimal fixed-point), causing every fee to truncate to 0 — extracting 100% of intended protocol fees.

Solution

New compute_fee function in contracts/settlement/src/fees.rs:

pub fn compute_fee(amount: i128, rate_bps: u32) -> i128 {
    ((amount * rate_bps as i128) + 5000) / 10000
}
Round-half-up ensures:
- Values < 0.5 round down (payer-friendly)
- Values >= 0.5 round up (protocol-friendly)
- Maximum per-transaction error ≤ 0.5 units of the smallest denomination
Files Changed
File	Change
contracts/settlement/Cargo.toml	New crate
contracts/settlement/src/lib.rs	Settlement contract with settle() + calculate_fee()
contracts/settlement/src/fees.rs	compute_fee() with round-half-up
contracts/settlement/src/constants.rs	Fee rate constants
contracts/settlement/src/token_utils.rs	Fee collection + invariant verification
contracts/settlement/src/test.rs	7 property-based tests
contracts/Cargo.toml	Added settlement to workspace
Verification
- 13 tests passing (all property-based invariants)
- Clippy: clean with -D warnings
- Invariants verified: |fee * 10000 - amount * rate_bps| <= 5000 across all edge cases
- Exhaustive check over 100,000 combinations (1..=1000 amounts × 1..=100 rates)

@elizabetheonoja-art elizabetheonoja-art merged commit 8f5a2fe into Utility-Protocol:main Jun 25, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Protocol Fee Rounding Extraction Favoring Validator Profit

2 participants