Skip to content

Reading Elysium

Mine

Invariants.t.sol

124 lines · sha256 ca9c093a752fd790817c540059be4bc9c924f3d6af055684dfe6d7633f3db873 · Raw

1// SPDX-License-Identifier: MIT
2pragma solidity ^0.8.26;
3
4import {KibbleBase} from "./utils/KibbleBase.sol";
5import {Handler} from "./utils/Handler.sol";
6
7abstract contract InvariantChecks is KibbleBase {
8 Handler internal handler;
9
10 function _deployHandler() internal {
11 handler = new Handler(token, emission, genesis, miner, oracle, locker, swapRouter);
12 }
13
14 function _checkAll() internal view {
15 // total minted <= 1M genesis + emission schedule up to now
16 assertLe(token.totalMinted(), 1_000_000e18 + emission.scheduledEmission(), "minted > schedule");
17 assertLe(token.totalSupply(), token.maxSupply());
18 assertEq(emission.totalMinted(), miner.totalClaimed());
19
20 // claimed + pending <= emitted (allocated) <= schedule
21 uint256 sumPending;
22 uint256 sumOn;
23 uint256 n = handler.actorsLength();
24 for (uint256 i; i < n; ++i) {
25 address u = handler.actors(i);
26 sumPending += miner.pending(u);
27 sumOn += miner.rigsOnOf(u);
28 // no tank goes negative (tankOf would underflow) and drained power stays consistent
29 (uint256 h, uint256 kh,, uint256 total) = miner.tankOf(u);
30 assertLe(h + kh, total + 1);
31 }
32 uint256 allocated = miner.totalAllocatedNow();
33 assertLe(miner.totalClaimed() + sumPending, allocated, "claimed + pending > emitted");
34 assertLe(allocated, emission.scheduledEmission(), "emitted > schedule");
35
36 // live online hashrate == sum of active wallets' rigs on
37 assertEq(miner.totalOnlineHashrate(), sumOn, "hashrate mismatch");
38
39 // LP locker liquidity never decreases and is really held by the PoolManager
40 assertGe(locker.totalLiquidity(), handler.maxLiquiditySeen());
41 uint256 onchain;
42 for (uint256 i; i < locker.positionCount(); ++i) {
43 onchain += locker.onchainPositionLiquidity(i);
44 }
45 assertEq(onchain, locker.totalLiquidity());
46
47 // solvency of tanks
48 assertGe(
49 address(miner).balance * 60_000,
50 miner.totalHypeUnits() + miner.pendingBurnHypeUnits(),
51 "HYPE tanks not backed"
52 );
53 assertGe(
54 token.balanceOf(address(miner)), miner.totalKibbleBacking() + miner.pendingBurnKibble(), "KIBBLE tanks"
55 );
56 }
57}
58
59/// @notice Handler-based invariants (foundry invariant runner).
60contract KibbleInvariantTest is InvariantChecks {
61 function setUp() public override {
62 super.setUp();
63 _deployHandler();
64 targetContract(address(handler));
65 }
66
67 function invariant_all() public view {
68 _checkAll();
69 }
70}
71
72/// @notice Fuzz: random sequences of buy / deposit / withdraw / setRigsOn / claim / roll across eras.
73contract KibbleSequenceFuzzTest is InvariantChecks {
74 function setUp() public override {
75 super.setUp();
76 _deployHandler();
77 }
78
79 function testFuzz_randomSequence(uint256 seed) public {
80 uint256 s = seed;
81 for (uint256 i; i < 40; ++i) {
82 s = uint256(keccak256(abi.encode(s, i)));
83 uint256 op = s % 12;
84 uint256 a = s >> 8;
85 uint256 b = s >> 128;
86 if (op == 0) handler.buyRigs(a, b);
87 else if (op == 1) handler.depositPower(a, b);
88 else if (op == 2) handler.depositPowerKibble(a, b);
89 else if (op == 3) handler.withdrawPower(a, b, b % 4 == 0);
90 else if (op == 4) handler.setRigsOn(a, b);
91 else if (op == 5) handler.claim(a);
92 else if (op == 6) handler.roll(b % 3 == 0 ? 216_000 : b);
93 else if (op == 7) handler.checkpoint();
94 else if (op == 8) handler.startMining();
95 else if (op == 9) handler.flushBurn();
96 else if (op == 10) handler.marketSwap(a, b % 2 == 0, b);
97 else handler.settle(a);
98 _checkAll();
99 }
100 }
101
102 /// Deterministic long run across several halvings.
103 function test_longRunAcrossEras() public {
104 handler.buyRigs(0, 100);
105 handler.buyRigs(1, 300);
106 handler.depositPower(0, 500e18);
107 handler.depositPower(1, 500e18);
108 handler.setRigsOn(0, 100);
109 handler.setRigsOn(1, 300);
110 handler.startMining();
111 for (uint256 i; i < 12; ++i) {
112 handler.roll(100_000);
113 if (i % 3 == 0) handler.claim(0);
114 if (i % 4 == 1) handler.depositPower(1, 200e18);
115 if (i % 5 == 2) handler.setRigsOn(1, 300);
116 handler.checkpoint();
117 _checkAll();
118 }
119 handler.claim(0);
120 handler.claim(1);
121 _checkAll();
122 }
123}
124