// SPDX-License-Identifier: MIT pragma solidity ^0.8.26; import {KibbleBase} from "./utils/KibbleBase.sol"; import {Handler} from "./utils/Handler.sol"; abstract contract InvariantChecks is KibbleBase { Handler internal handler; function _deployHandler() internal { handler = new Handler(token, emission, genesis, miner, oracle, locker, swapRouter); } function _checkAll() internal view { // total minted <= 1M genesis + emission schedule up to now assertLe(token.totalMinted(), 1_000_000e18 + emission.scheduledEmission(), "minted > schedule"); assertLe(token.totalSupply(), token.maxSupply()); assertEq(emission.totalMinted(), miner.totalClaimed()); // claimed + pending <= emitted (allocated) <= schedule uint256 sumPending; uint256 sumOn; uint256 n = handler.actorsLength(); for (uint256 i; i < n; ++i) { address u = handler.actors(i); sumPending += miner.pending(u); sumOn += miner.rigsOnOf(u); // no tank goes negative (tankOf would underflow) and drained power stays consistent (uint256 h, uint256 kh,, uint256 total) = miner.tankOf(u); assertLe(h + kh, total + 1); } uint256 allocated = miner.totalAllocatedNow(); assertLe(miner.totalClaimed() + sumPending, allocated, "claimed + pending > emitted"); assertLe(allocated, emission.scheduledEmission(), "emitted > schedule"); // live online hashrate == sum of active wallets' rigs on assertEq(miner.totalOnlineHashrate(), sumOn, "hashrate mismatch"); // LP locker liquidity never decreases and is really held by the PoolManager assertGe(locker.totalLiquidity(), handler.maxLiquiditySeen()); uint256 onchain; for (uint256 i; i < locker.positionCount(); ++i) { onchain += locker.onchainPositionLiquidity(i); } assertEq(onchain, locker.totalLiquidity()); // solvency of tanks assertGe( address(miner).balance * 60_000, miner.totalHypeUnits() + miner.pendingBurnHypeUnits(), "HYPE tanks not backed" ); assertGe( token.balanceOf(address(miner)), miner.totalKibbleBacking() + miner.pendingBurnKibble(), "KIBBLE tanks" ); } } /// @notice Handler-based invariants (foundry invariant runner). contract KibbleInvariantTest is InvariantChecks { function setUp() public override { super.setUp(); _deployHandler(); targetContract(address(handler)); } function invariant_all() public view { _checkAll(); } } /// @notice Fuzz: random sequences of buy / deposit / withdraw / setRigsOn / claim / roll across eras. contract KibbleSequenceFuzzTest is InvariantChecks { function setUp() public override { super.setUp(); _deployHandler(); } function testFuzz_randomSequence(uint256 seed) public { uint256 s = seed; for (uint256 i; i < 40; ++i) { s = uint256(keccak256(abi.encode(s, i))); uint256 op = s % 12; uint256 a = s >> 8; uint256 b = s >> 128; if (op == 0) handler.buyRigs(a, b); else if (op == 1) handler.depositPower(a, b); else if (op == 2) handler.depositPowerKibble(a, b); else if (op == 3) handler.withdrawPower(a, b, b % 4 == 0); else if (op == 4) handler.setRigsOn(a, b); else if (op == 5) handler.claim(a); else if (op == 6) handler.roll(b % 3 == 0 ? 216_000 : b); else if (op == 7) handler.checkpoint(); else if (op == 8) handler.startMining(); else if (op == 9) handler.flushBurn(); else if (op == 10) handler.marketSwap(a, b % 2 == 0, b); else handler.settle(a); _checkAll(); } } /// Deterministic long run across several halvings. function test_longRunAcrossEras() public { handler.buyRigs(0, 100); handler.buyRigs(1, 300); handler.depositPower(0, 500e18); handler.depositPower(1, 500e18); handler.setRigsOn(0, 100); handler.setRigsOn(1, 300); handler.startMining(); for (uint256 i; i < 12; ++i) { handler.roll(100_000); if (i % 3 == 0) handler.claim(0); if (i % 4 == 1) handler.depositPower(1, 200e18); if (i % 5 == 2) handler.setRigsOn(1, 300); handler.checkpoint(); _checkAll(); } handler.claim(0); handler.claim(1); _checkAll(); } }