harb/onchain/test/mocks/ExtremeOptimizer.sol
johba 2c69963151 feat: Add scenario recording and replay system for invariant debugging
Implements comprehensive fuzzing improvements to find and reproduce invariant violations:

Recording System:
- ScenarioRecorder captures exact trading sequences that violate invariants
- Exports to JSON, replay scripts, and human-readable summaries
- Unique Run IDs (format: YYMMDD-XXXX) for easy communication

Enhanced Fuzzing:
- ImprovedFuzzingAnalysis with larger trades (50-500 ETH) to reach discovery position
- Multiple strategies: Discovery Push, Whale Manipulation, Volatile Swings
- Successfully finds profitable scenarios with 66% success rate

Shell Scripts:
- run-recorded-fuzzing.sh: Automated fuzzing with recording and unique IDs
- replay-scenario.sh: One-command replay of specific scenarios

New Optimizers:
- ExtremeOptimizer: Tests extreme market conditions
- MaliciousOptimizer: Attempts to exploit the protocol

Documentation:
- Updated CLAUDE.md with complete recording workflow
- Enhanced 4-step debugging process
- Quick reference for team collaboration

This system successfully identifies and reproduces the discovery position exploit,
where traders can profit by pushing trades into the unused liquidity at extreme ticks.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-18 20:31:39 +02:00

64 lines
No EOL
2.5 KiB
Solidity

// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.19;
/**
* @title ExtremeOptimizer
* @notice Pushes parameters to extremes to find breaking points
* @dev Cycles through extreme parameter combinations to stress test the protocol
*/
contract ExtremeOptimizer {
uint256 private callCount;
function getOptimalParameters(
uint256, // percentageStaked
uint256, // avgTaxRate
uint256 // sentiment
) external returns (uint256, uint256, uint256, uint256) {
callCount++;
// Cycle through extreme scenarios
uint256 scenario = callCount % 5;
if (scenario == 0) {
// Extreme capital inefficiency with minimal anchor
return (
1e18, // 100% capital inefficiency (KRAIKEN valued at 170%)
0.01e18, // 1% anchor share (99% to floor)
1, // 1% anchor width (extremely narrow)
10e18 // 10x discovery depth
);
} else if (scenario == 1) {
// Zero capital inefficiency with maximum anchor
return (
0, // 0% capital inefficiency (KRAIKEN valued at 70%)
0.99e18, // 99% anchor share (minimal floor)
100, // 100% anchor width (maximum range)
0.1e18 // 0.1x discovery depth (minimal discovery)
);
} else if (scenario == 2) {
// Oscillating between extremes
return (
callCount % 2 == 0 ? 1e18 : 0, // Flip between 0% and 100%
0.5e18, // 50% anchor share
50, // 50% width
callCount % 2 == 0 ? 10e18 : 0.1e18 // Flip discovery depth
);
} else if (scenario == 3) {
// Edge case: Everything at minimum
return (
0, // Minimum capital inefficiency
0, // Minimum anchor share (all to floor)
1, // Minimum width
0 // No discovery liquidity
);
} else {
// Edge case: Everything at maximum
return (
1e18, // Maximum capital inefficiency
1e18, // Maximum anchor share (no floor)
100, // Maximum width
100e18 // Extreme discovery depth
);
}
}
}