-
Notifications
You must be signed in to change notification settings - Fork 4
fix: add quadratic scaling penalty to TimeWeightedVotingPower (#91) #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RonTuretzky
wants to merge
4
commits into
RonTuretzky/108-dust-reentrancy
Choose a base branch
from
RonTuretzky/107-twvp-scaling
base: RonTuretzky/108-dust-reentrancy
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8316ed7
fix: correct TWVP scaling period calculation (#91)
Tranquil-Flow 2385c50
docs: fix NatSpec inconsistency in TWVP scaling period and penalty fo…
Tranquil-Flow 1dd214d
test: assert specific revert selector in setScalingPeriod non-owner t…
Tranquil-Flow 69057ed
fix: address Copilot review feedback
RonTuretzky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,24 +65,24 @@ contract TimeWeightedVotingPowerTest is Test { | |
|
|
||
| cycleModule.setDistributionManager(distManager); | ||
|
|
||
| strategy = new TimeWeightedVotingPower(IVotesCheckpoints(address(token)), ICycleModule(address(cycleModule))); | ||
| strategy = new TimeWeightedVotingPower(IVotesCheckpoints(address(token)), ICycleModule(address(cycleModule)), 0); | ||
| } | ||
|
|
||
| // ============ when constructing ============ | ||
|
|
||
| function test_WhenConstructing_ItShouldSetVotingTokenAndCycleModule() public view { | ||
| assertEq(address(strategy.votingToken()), address(token)); | ||
| assertEq(address(strategy.cycleModule()), address(cycleModule)); | ||
| assertEq(address(strategy.VOTING_TOKEN()), address(token)); | ||
| assertEq(address(strategy.CYCLE_MODULE()), address(cycleModule)); | ||
| } | ||
|
|
||
| function test_RevertWhen_Constructing_InvalidToken() public { | ||
| vm.expectRevert(TimeWeightedVotingPower.InvalidToken.selector); | ||
| new TimeWeightedVotingPower(IVotesCheckpoints(address(0)), ICycleModule(address(cycleModule))); | ||
| new TimeWeightedVotingPower(IVotesCheckpoints(address(0)), ICycleModule(address(cycleModule)), 0); | ||
| } | ||
|
|
||
| function test_RevertWhen_Constructing_InvalidCycleModule() public { | ||
| vm.expectRevert(TimeWeightedVotingPower.InvalidCycleModule.selector); | ||
| new TimeWeightedVotingPower(IVotesCheckpoints(address(token)), ICycleModule(address(0))); | ||
| new TimeWeightedVotingPower(IVotesCheckpoints(address(token)), ICycleModule(address(0)), 0); | ||
| } | ||
|
|
||
| // ============ when calculating exact checkpoint integration ============ | ||
|
|
@@ -386,4 +386,169 @@ contract TimeWeightedVotingPowerTest is Test { | |
| // With exact checkpoint walking and only 1 checkpoint, gas should be very low | ||
| assertLt(gasUsed, 30_000, "Gas should be very low with few checkpoints"); | ||
| } | ||
|
|
||
| // ============ scalingPeriod Feature Tests (Issue #91) ============ | ||
|
|
||
| // Test 1: scalingPeriod=0 disables the quadratic penalty entirely | ||
| function testScalingPeriodDisabledWhenZero() public { | ||
| // strategy was created with scalingPeriod=0 in setUp | ||
| assertEq(strategy.scalingPeriod(), 0); | ||
|
|
||
| vm.roll(10); | ||
| token.mint(user1, 100 ether); | ||
|
|
||
| // Advance to block 500 | ||
| vm.roll(500); | ||
| // Mint more tokens (this checkpoint is outside our query period) | ||
| token.mint(user1, 50 ether); | ||
|
|
||
| vm.roll(501); | ||
|
|
||
| // Query period [10, 14): user held 100 ether the whole interval | ||
| // With scalingPeriod=0, no penalty. intervalLength=4, area=400 ether, avg=100 ether | ||
| uint256 power = strategy.getVotingPowerForPeriod(user1, 10, 14); | ||
|
|
||
| // Baseline: 100 ether held for entire 4-block period, no penalty | ||
| uint256 baseline = 100 ether; | ||
| assertEq(power, baseline, "scalingPeriod=0 should apply no penalty"); | ||
| } | ||
|
|
||
| // Test 2: Quadratic penalty reduces voting power for short intervals | ||
| function testQuadraticPenaltyApplied() public { | ||
| // Create a new strategy with scalingPeriod=100 | ||
| TimeWeightedVotingPower scaledStrategy = new TimeWeightedVotingPower( | ||
| IVotesCheckpoints(address(token)), | ||
| ICycleModule(address(cycleModule)), | ||
| 100 | ||
| ); | ||
|
|
||
| vm.roll(10); | ||
| token.mint(user1, 100 ether); | ||
|
|
||
| // Advance to block 50 (intervalLength = 40, which is < scalingPeriod=100) | ||
| vm.roll(50); | ||
|
|
||
| // Query period [10, 50): 40 blocks | ||
| // Checkpoint at block 10 predates/equals start => intervalLength = upperBound - start = 50-10 = 40 | ||
| // area = 100 ether * 40 = 4000 ether | ||
| // scaled = (4000 ether * 40) / 100 = 1600 ether | ||
| // avg = 1600 ether / 40 = 40 ether | ||
| uint256 power = scaledStrategy.getVotingPowerForPeriod(user1, 10, 50); | ||
| assertEq(power, 40 ether, "Quadratic penalty should reduce power for short intervals"); | ||
|
|
||
| // Without penalty the power would be 100 ether — confirm reduction | ||
| uint256 unpenalizedPower = strategy.getVotingPowerForPeriod(user1, 10, 50); | ||
| assertEq(unpenalizedPower, 100 ether, "Unpenalized strategy should return full balance"); | ||
| assertLt(power, unpenalizedPower, "Penalty should reduce voting power"); | ||
| } | ||
|
|
||
| // Test 3: No penalty when intervalLength exactly equals scalingPeriod | ||
| function testNoPenaltyWhenIntervalExceedsScalingPeriod() public { | ||
| // scalingPeriod=100, intervalLength=100 => factor = 100/100 = 1.0 => no reduction | ||
|
Comment on lines
+445
to
+447
|
||
| TimeWeightedVotingPower scaledStrategy = new TimeWeightedVotingPower( | ||
| IVotesCheckpoints(address(token)), | ||
| ICycleModule(address(cycleModule)), | ||
| 100 | ||
| ); | ||
|
|
||
| vm.roll(10); | ||
| token.mint(user1, 100 ether); | ||
|
|
||
| // Advance to block 110 (intervalLength = 100 == scalingPeriod) | ||
| vm.roll(110); | ||
|
|
||
| // area = 100 ether * 100 = 10000 ether | ||
| // intervalLength (100) >= scalingPeriod (100) => no penalty | ||
| // avg = 10000 ether / 100 = 100 ether | ||
| uint256 power = scaledStrategy.getVotingPowerForPeriod(user1, 10, 110); | ||
| assertEq(power, 100 ether, "No penalty when intervalLength == scalingPeriod"); | ||
| } | ||
|
|
||
| // Test 4: No penalty when intervalLength strictly exceeds scalingPeriod | ||
| function testScalingPeriodOnlyAffectsShortIntervals() public { | ||
| // scalingPeriod=100, intervalLength=150 > scalingPeriod => no penalty | ||
| TimeWeightedVotingPower scaledStrategy = new TimeWeightedVotingPower( | ||
| IVotesCheckpoints(address(token)), | ||
| ICycleModule(address(cycleModule)), | ||
| 100 | ||
| ); | ||
|
|
||
| vm.roll(10); | ||
| token.mint(user1, 100 ether); | ||
|
|
||
| // Advance to block 160 (intervalLength = 150 > scalingPeriod=100) | ||
| vm.roll(160); | ||
|
|
||
| // area = 100 ether * 150 = 15000 ether | ||
| // intervalLength (150) >= scalingPeriod (100) => no penalty | ||
| // avg = 15000 ether / 150 = 100 ether | ||
| uint256 power = scaledStrategy.getVotingPowerForPeriod(user1, 10, 160); | ||
| assertEq(power, 100 ether, "No penalty when intervalLength > scalingPeriod"); | ||
| } | ||
|
|
||
| // Test 5: Owner can update scalingPeriod | ||
| function testSetScalingPeriodByOwner() public { | ||
| // strategy created with scalingPeriod=0 in setUp, owner = address(this) | ||
| assertEq(strategy.scalingPeriod(), 0); | ||
|
|
||
| strategy.setScalingPeriod(100); | ||
|
|
||
| assertEq(strategy.scalingPeriod(), 100, "scalingPeriod should be updated to 100"); | ||
| } | ||
|
|
||
| // Test 6: setScalingPeriod emits ScalingPeriodUpdated event | ||
| function testSetScalingPeriodEmitsEvent() public { | ||
| assertEq(strategy.scalingPeriod(), 0); | ||
|
|
||
| vm.expectEmit(true, true, true, true, address(strategy)); | ||
| emit TimeWeightedVotingPower.ScalingPeriodUpdated(0, 100); | ||
|
|
||
| strategy.setScalingPeriod(100); | ||
| } | ||
|
|
||
| // Test 7: Non-owner cannot set scalingPeriod | ||
| function testSetScalingPeriodRevertsNonOwner() public { | ||
| address nonOwner = address(0xBEEF1234); | ||
| vm.prank(nonOwner); | ||
| vm.expectRevert(abi.encodeWithSignature("Unauthorized()")); | ||
| strategy.setScalingPeriod(100); | ||
| } | ||
|
|
||
| // Test 7b: setScalingPeriod reverts if value exceeds MAX_SCALING_PERIOD | ||
| function testSetScalingPeriodRevertsExceedsMax() public { | ||
| vm.expectRevert(abi.encodeWithSignature("ScalingPeriodTooLarge()")); | ||
| strategy.setScalingPeriod(type(uint64).max); | ||
| } | ||
|
|
||
| // Test 8: Flash loan attack is mitigated by quadratic scaling | ||
| function testFlashLoanMitigatedByScaling() public { | ||
| // scalingPeriod=100: attacker holding tokens for 1 block gets 100x less power | ||
| TimeWeightedVotingPower scaledStrategy = new TimeWeightedVotingPower( | ||
| IVotesCheckpoints(address(token)), | ||
| ICycleModule(address(cycleModule)), | ||
| 100 | ||
| ); | ||
|
|
||
| // Attacker mints 1000 ether at block 500 | ||
| vm.roll(500); | ||
| token.mint(user2, 1000 ether); | ||
|
|
||
| // Move 1 block so checkpoint is visible | ||
| vm.roll(501); | ||
|
|
||
| // Query the 1-block window [500, 501) | ||
| // intervalLength = 1 < scalingPeriod=100 | ||
| // area = 1000 ether * 1 = 1000 ether | ||
| // scaled = (1000 ether * 1) / 100 = 10 ether | ||
| // avg = 10 ether / 1 = 10 ether | ||
| uint256 scaledPower = scaledStrategy.getVotingPowerForPeriod(user2, 500, 501); | ||
| assertEq(scaledPower, 10 ether, "Flash loan with scalingPeriod=100 should give 10 ether not 1000 ether"); | ||
|
|
||
| // Without scaling, the same 1-block window returns the full balance | ||
| uint256 unscaledPower = strategy.getVotingPowerForPeriod(user2, 500, 501); | ||
| assertEq(unscaledPower, 1000 ether, "Without scaling, full balance is returned"); | ||
|
|
||
| // Confirm the 100x reduction | ||
| assertEq(unscaledPower / scaledPower, 100, "Scaling should give 100x reduction for 1-block hold with scalingPeriod=100"); | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TimeWeightedVotingPowernow has an owner-onlysetScalingPeriod(), but the deploy script deploys the strategy directly (owner becomes the broadcasting EOA) and never transfers ownership top.owner. This leaves the deployer with ongoing control over voting power behavior and prevents the intended system owner from managingscalingPeriod. Consider transferring strategy ownership top.ownerafter deployment (or accepting an_ownerconstructor arg).