From db23d756e9ccc9db10b06efa20d3e597dda9a0b6 Mon Sep 17 00:00:00 2001 From: johba Date: Tue, 19 Aug 2025 13:30:18 +0200 Subject: [PATCH] fix: Optimize fuzzing and fix CSV buffer accumulation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Deploy Uniswap factory once before all runs (saves ~1.16M gas per run) - Fix CSV buffer accumulation bug by clearing buffer between runs - Add clearCSV() function to CSVManager for proper buffer management - Each fuzzing run now gets its own clean CSV with correct token0isWeth values - Comment out failing console.log in Optimizer.t.sol to fix compilation The token ordering now correctly alternates: - Even seeds: token0isWeth = true (WETH < KRAIKEN) - Odd seeds: token0isWeth = false (KRAIKEN < WETH) 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .../analysis/ImprovedFuzzingAnalysis.s.sol | 19 ++++++++++--------- onchain/analysis/helpers/CSVManager.sol | 7 +++++++ onchain/test/Optimizer.t.sol | 3 ++- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/onchain/analysis/ImprovedFuzzingAnalysis.s.sol b/onchain/analysis/ImprovedFuzzingAnalysis.s.sol index 49a6ea3..ebddaf4 100644 --- a/onchain/analysis/ImprovedFuzzingAnalysis.s.sol +++ b/onchain/analysis/ImprovedFuzzingAnalysis.s.sol @@ -10,6 +10,7 @@ import {Kraiken} from "../src/Kraiken.sol"; import {Stake} from "../src/Stake.sol"; import {LiquidityManager} from "../src/LiquidityManager.sol"; import {ThreePositionStrategy} from "../src/abstracts/ThreePositionStrategy.sol"; +import {UniswapHelpers} from "../src/helpers/UniswapHelpers.sol"; import "../test/mocks/BullMarketOptimizer.sol"; import "../test/mocks/WhaleOptimizer.sol"; import "./helpers/CSVManager.sol"; @@ -58,6 +59,9 @@ contract ImprovedFuzzingAnalysis is Test, CSVManager { testEnv = new TestEnvironment(feeDestination); + // Deploy factory once for all runs (gas optimization) + factory = UniswapHelpers.deployUniswapFactory(); + // Get optimizer address optimizerAddress = _getOptimizerByClass(optimizerClass); @@ -70,9 +74,9 @@ contract ImprovedFuzzingAnalysis is Test, CSVManager { console.log(string.concat("Progress: ", vm.toString(seed), "/", vm.toString(fuzzingRuns))); } - // Create fresh environment + // Create fresh environment with existing factory (factory, pool, weth, harberg, stake, lm,, token0isWeth) = - testEnv.setupEnvironmentWithOptimizer(seed % 2 == 0, feeDestination, optimizerAddress); + testEnv.setupEnvironmentWithExistingFactory(factory, seed % 2 == 0, feeDestination, optimizerAddress); // Fund LiquidityManager with MORE ETH for deeper liquidity vm.deal(address(lm), 200 ether); // Increased from 50 @@ -99,14 +103,10 @@ contract ImprovedFuzzingAnalysis is Test, CSVManager { vm.prank(feeDestination); lm.recenter(); - // Initialize position tracking (skip CSV init if already done) + // Initialize position tracking for each seed if (trackPositions) { - if (seed == 0) { - // Only initialize CSV for first seed if not already initialized - if (bytes(csv).length == 0) { - initializePositionsCSV(); - } - } + // Initialize CSV header for each seed (after clearCSV from previous run) + initializePositionsCSV(); _recordPositionData("Initial"); } @@ -148,6 +148,7 @@ contract ImprovedFuzzingAnalysis is Test, CSVManager { "improved_positions_", optimizerClass, "_", vm.toString(seed), ".csv" ); writeCSVToFile(positionFilename); + clearCSV(); // Clear buffer for next run } } diff --git a/onchain/analysis/helpers/CSVManager.sol b/onchain/analysis/helpers/CSVManager.sol index eddb149..5c51846 100644 --- a/onchain/analysis/helpers/CSVManager.sol +++ b/onchain/analysis/helpers/CSVManager.sol @@ -41,4 +41,11 @@ abstract contract CSVManager is Test { function writeCSVToFile(string memory filePath) internal { vm.writeFile(filePath, csv); } + + /** + * @notice Clears the CSV buffer. Should be called between runs to avoid data accumulation. + */ + function clearCSV() internal { + csv = ""; + } } diff --git a/onchain/test/Optimizer.t.sol b/onchain/test/Optimizer.t.sol index 41110ab..d538fe7 100644 --- a/onchain/test/Optimizer.t.sol +++ b/onchain/test/Optimizer.t.sol @@ -210,7 +210,8 @@ contract OptimizerTest is Test { // Log some interesting cases if (anchorWidth == 10 || anchorWidth == 80) { - console.log("Bound hit - Staking:", percentageStaked / 1e16, "%, Tax:", averageTaxRate / 1e16, "%, Width:", anchorWidth); + // Commented out due to console.log compilation issue + // console.log("Bound hit - Staking:", percentageStaked / 1e16, "%, Tax:", averageTaxRate / 1e16, "%, Width:", anchorWidth); } }